So, hello to all viewers and welcome back to Gettobyte Platform. In This blog you are going to know about RFID Reader MFRC522, which is designed by NXP Semiconductors. Objective would be to interface this module with Host MCU’s like of NXP Semiconductors, STMicroelectronics or other vendors MCU’s. Will make the driver to interface the RFID Reader with any MCU, not unlike just with Arduino and Arduino IDE environment. To make the driver of RFID reader at first, we need to dig into its datasheet, to understand its various sub parts. And that’s all about this blog is gotten going to be, to make the datasheet understand in easy way-out.
Table of Contents
Next & Previous Blog
RFID Technology
RFID modules is a wireless sensing technology which is used to track/identify/monitor the objects.
MFRC522 RFID Reader/PCD
MFRC522 is a highly integrated reader/writer IC for contactless communication at 13.56 MHz. These reader supports the ISO 14443 A protocol for communicating with RFID Tags. They are used to detect the MIFRAME RFID tags.
MFRC522 has internal RF transceiver, which provides a robust and efficient implementation for demodulating and decoding signals from MIFRAME compatible cards using ISO 14443 A protocol. The digital module of MFRC522 manages the complete ISO/IEC 14443 A framing and error detection (parity and CRC) functionality.
MFRC522 supports 3 tags of MIFRAME family, that are MF1xxS20, MF1xxS70 and MF1S50 products.
MFRC522 features
MFRC522 though quite old RFID reader and in today’s time many new advance RFID readers have come up. But so as to get started with this technology as a hobbyist/student/DIY project, it is perfect module to lay your hands on this Technology.
- MFRC522 has highly integrated analog circuitry to demodulate and decode responses when RFID tags are brought in close proximity of these devices.
- RFID readers are connected with some host MCU, where the processing of data which is received via RFID tags happens according to the application. MFRC522 can connect with host MCU, using SPI, Serial UART and I2C -bus interface.
- It supports ISO 14443 A protocol and can be used with MIFRAME family of RFID tags. And in MIFRAME family it supports only MF1xxS20, MF1xxS70 and MF1xxS50 products.
- It has internal CRC-coprocessor.
- Internal FIFO buffer which can handle 64 bytes of sending and receiving.
- It uses the Crypto-1 cipher for authenticating.
- It supports Internal oscillator for connection to 27.12 MHz quartz crytsal.
- It is low power device, need 2.5 V to 3.3 V power supply.
- It also has flexible interrupt modes when some RFID tags are detected and trigering events too. In addition to flexible interrupt, it has programmabe I/O pins and timer.
- It can perform Internal self-test too.
MFRC522 Functional description
MFRC522 Host Interfaces
MFRC522 can be connected to Host MCU using 3 serial protocols: UART, I2C or SPI.
MFRC522 checks the current host interface type.
automatically after performing a power-on or hard reset. The MFRC522 IC identifies the host
interface by sensing the logic levels on the below pins after the reset phase. 
The MFRC522 is equipped with a series of registers that allow the Host MCU to access its functional description blocks. To ensure the proper functioning of the MFRC522, the Host MCU must initialize and configure these functional blocks by sending the corresponding register addresses.
Each register is essentially an address byte that is transmitted from the Host MCU. Depending on the function described in the register section, read/write operations are performed on the corresponding address byte.
It is crucial to properly initialize and configure these functional blocks to ensure the optimal performance of the MFRC522. By understanding the purpose of each register and its corresponding function, the Host MCU can effectively communicate with the MFRC522 and achieve the desired results.
–> MFRC522_write_register()
–> MFRC522_Read_register()
MFRC522 Interrupts
MFRC522 can trigger the interrupts, when certain events occur. There are 8 events as shown in below table when interrupt can be triggered.
When above event occurs, IRQ pin is used to interrupt the host. IRQ pin signal is asserted and host MCU can use its interrupt handling capabilities (basically NVIC if we are talking about ARM based MCU) on what to do when corresponding interrupt has occurred.
- Status1Reg Register IRq bit is used to indicate if any interrupt source has been triggerered.
- Which interrupt has been triggered is indicated by ComIrqReg and DivIrqReg Register. 
- Which interrupts to be configured and behavior of IRQ pin is configured by ComIEReg and DivIEReg Register.
MFRC522 Time Unit
There is a Timer unit in MFRC522, that is used for multiple purposes. Timer unit is essential for maintaing the configuring the clock and analog interfaces. Also timer unit can be used for following features:
- Timeout counter
- Watchdog counter
- Stopwatch
- Programmable one shot
- Periodical trigger
Timer has an input clock of 13.56 MHz derived from the 27.12 MHz quartz crystal oscillator. The timer consists of 2 stages: prescaler and counter.
- The prescaler(TPrescaler) is a 12-bit counter. That can be configured using TModeReg register’s TPrescaler_Hi[3:0] and TPrescalerReg register’s TPrescaler[7:0] bits.
- The Reload value for the counter is defined by 16 bits between 0 & 65535 in the TReloadReg register.
- The current value of the timer is indicated in the TCounterVAlReg Register.
MFRC522 FIFO
FIFO overview
The MFRC522 contains an internal FIFO buffer of 64 bytes, which is equivalent to 8 x 64 bits. This buffer is utilized for both input and output data streams. The host MCU has the capability to perform both Read and Write operations on this FIFO. The host MCU sends commands to PCD for communication with PICC. these commands are specified in ISO14443 A standard, which is then inputted into the FIFO. When the PICC responds to these commands, the response is also stored in the FIFO. The host MCU can then read the FIFO to obtain the response from the PICC.
The FIFO buffer is a crucial component in the communication process between the host MCU and the PICC. It allows for efficient data transfer and ensures that all responses are stored in a centralized location. The ability to perform both Read and Write operations on the FIFO provides flexibility and control to the host MCU. By utilizing the FIFO buffer, the communication process is streamlined and optimized for maximum efficiency.
About FIFO registers
FIFO buffer input and output data bus is connected to the FIFODataReg register. Writing to this register stores one byte in the FIFO buffer and increments the internal FIFO buffer write pointer. Reading from this register shows the FIFO buffer contents stored in FIFO read pointer and decrements the FIFO buffer read pointer.
Only one FIFO buffer has been implemented which can be used for input and output. The microcontroller must
ensure that there is not any unintentional FIFO buffer accesses.
The FIFOLevelReg is utilized to determine the number of bytes stored in the FIFO. This register is particularly useful in checking the number of bytes received in the FIFO buffer when the PICC sends a response to the PCD command. By using the FIFOLevelReg, you can easily keep track of the amount of data stored in the FIFO, which is essential in ensuring the smooth operation of your system.
One can also get the status of FIFO buffer using status and error registers.
- FIFO buffer almost full warning can be got to know from Status1Reg register’s HiAlert bit.
- FIFO buffer almost empty warning can be got to know from Status1Reg register’s LoAlert bit.
- FIFO buffer overflow warning, using ErrorReg register’s BufferOvfl bit.
MFRC522 CRC
MFRC522 has a Cyclic Rebudency Check (CRC) coprocessor to check the integrity of the data when data from PICC is received or when data is wriiten to PICC.
- CRC has preset value 0000h, 6363h, A671h or FFFFh. We can configure the preset values using ModeReg CRCPreset[1:0].
- CRC polynomial for the 16-bit CRC is fixed to x^16 + x^12 + x^5 + 1.
- CRCResultReg register indicates the result of the CRC calculation.
- CRC algorithm which is used is according to ISO/IEC 14443 A and ITU-T.
To perform the CRC calculation refer its state machine in below section.
MFRC522 Command Set
MFRC522 operation is determined by certain commands. According to these commands, correspondingly MFRC522 would be performing some action.
So, it’s like at first, we will configure the FIFO, CRC, Interrupts and timer unit. After that Host MCU will instruct the commands to MFRC522 in order to tell what it has to do. Host MCU will write the command code to the CommandReg Register.
Main commands that would be used are Idle, Transceive, CalcCRC and Transmit commands. Transceive command is the one which has to be sent from Host MCU to MFRC522 to send the FIFO buffer data to the antenna, which is further propagated to the RFID readers. And according to the data which we have written in FIFO RFID readers would act and give response.
Each command that needs a data bit stream (or data byte stream) as an input immediately processes any data in the FIFO buffer. An exception to this rule is the
Transceive command. Using this command, transmission is started with the BitFramingReg register’s StartSend bit.
MFRC522 Block Diagram
Above is the simplified block diagram of MFRC522 module. MFRC522 has an internal memory, power supply, interrupt pins, FIFO buffer, Antenna and analog interface, digital module for communicating with Host MCU.
Analog Interface handles the modulation and demodulation of the analog signals. FIFO Buffer ensures fast and convenient data transfer.
Register bank are the set of registers through which MFRC522 would be configured and initialized to use. Parameters like Clock, Interrupts, status of errors while communicating with RFID readers, CRC calculation, FIFO configuration and etc need to be configured before using the MFRC522.
How the MFRC522 Internal system works?
Host MCU will send PCD Commands to MFRC522, according to which it will perform some operations. MCU will send these commands by writing into one of the registers of PCD.( For MFRC522 CommandReg is register)
Operations like:
- Activation of CRC Coprocessor
- Transmission of data from FIFO buffer of MFRC522 and activation of receiver circuits to get the response from PICC
- transfer of data from FIFO to an internal buffer
- soft reset and authentication-based operations
Further MFRC522 will emit the commands for communicating with PICC, here referred to as PICC Commands, according to which operation with PICC takes place, like scanning of PICC, selecting the PICC, reading and writing the data on PICC. MFRC522 emits these commands via electromagnetic induction and electric coupling. ( That is the main working principle behind the RFID technology)
Host MCU to PCD
Host MCU will send the commands to PCD, according to which PCD will perform the operations like activation of FIFO buffer or CRC coprocessor, and transmission of data from FIFO. We will name these commands as MFRC522 Commands sets. Host MCU will send those commands to PCD (here MFRC522) by writing into the CommandReg Register using low level Host Interface write API.
- Idle Command: Places the MFRC522 in Idle mode.
- Mem Command: Transfers the 25 bytes from the FIFO Buffer to the internal buffer. This command is used when, we want to read the data from PICC( As described in ISO-14443-4 doc), as PICC response is always recorded in PCD FIFO.
- Generate RandomID: generates a 10-byte random ID number.
- CalcCRC: Activates the CRC coprocessor or performs a self test.
- Transmit: transmit data from the FIFO buffer
- NoCmdChange: no command change, can be used to modify the CommandRef register bits without affecting the command
- Receive: activates the receiver circuits
- Transceive: Transmit data from the FIFO buffer to antenna and automatically activates the receiver after transmission.
- MFAuthent: performs the MIFARE standard authentication as a reader
- SoftReset: This command performs a reset of the device. The configuration data of the internal buffer remains unchanged. All registers are set to the reset values. This command automatically terminates when finished.
PCD to PICC
Further there are also command set which PCD have to emit to communicate with PICC and enter the PICC into some state machine.
PICC States:
- Power-off State: In the POWER-OFF state, the PICC is not powered by a PCD operating field
- Idle State: In the IDLE state, the PICC is powered. It listens for commands and shall recognize REQA and WUPA commands.
- Ready State: Cascade levels are handled inside this state to get the complete UID. PICC enters the ACTIVE state when it is selected with its complete UID.
- Active State: PICC complies with ISO-14443-4 to accept protocol activation commands (RATS)
- HALT State: In this state PICC only responds to WUPA command.
- Protocol State: PICC behaves according to 1s014443-4 specifications.
We will name these commands as PICC commands. Host MCU will write these PICC command into the PCD FIFO (refer the MFRC522 FIFO section on how to write the data on FIFO). PCD would transmit the FIFO data when it enters the Transceiver state machine (refer above topic).
PCD would transmit these commands via the electromagnetic induction and communicates with PICC via electronic coupling. PCD’s have RF transceivers through which it emits the electromagnetic waves with commands sets encoded in them. PICC have antenna and small circuitry, which gets energized via electromagnetic waves emitted by PCD and then further both PCD and PICC get coupled together to have 2 way wireless communication. How PCD would send commands to PICC and in return how PICC would respond, that is done according to IS014443 protocol. ISO14443 specifies all the frame formats,PICC command and their format , modulation schemes for RFID technology. 
PICC Frame formats:
- Short Frame:
- Standard Frames
PICC Command Set:
- REQA/WUPA Command: This command is send by PCD to scan the valid and IS014443 compatible PICC’s. This command is in short frame format. Host MCU will write this command into the PCD FIFO, from where this command is transmitted and emitted wirelessly. Response to this command is with ATQA. PICC are in Idle state when listening to this command and after successfully transmitting the ATQA response they enter into Ready State. These commands consist of 7 bits, as specified below:
- ANTICOLLISION/SELECT Command: PICC are in ready state when listening to this command and after this command they enter into ACTIVE STATE. This Command is used to get the UID of the scanned PICC and further select the corresponding PICC for Reading and writing data from it. For the MF1S503 PICC’s UID is of 4 bytes, for that only 1 cascade level is needed.
These commands need to be configured and then these bytes are written into The PCD FIFO for transmission and emission of signal to PICC wirelessly. This Command consists of:
- Select code SEL(1byte): Specify the cascade level.
- No of valid bits NVB (1 byte): specify number of bytes that would be transmitted by PCD to PICC (including SEL, NVB and below point)
- 0 to 40 data bits of UID according to value of NVB.
ANTICOLLISION Command consist of:
- Select Code: Cascade level 1= 0x93
- No of valid bits NVB = 0x20
Response to ANTICOLLISION Command is with the UID of the Scanned PICC(0xEA, 0x24, 0x77, 0x15) and its CRC(0xAC).
- SELECT Command consists of:
- Select code: Cascade level 1 = 0x93
- No of valid bits NVB – 0x70
- 0 to 40 data bits of UID that has been received in ANTICOLLISION Command( 0xEA, 0x24, 0x77, 0x15)
- CRCA(1 byte) : CRC of SELECT Command would be calculated by PCD and then it would be sent to PICC. And When PICC receives the SELECT Command it will check this CRCA and then will send the ACK.(0xAC)
Response to SELECT Command is with the Select Acknowledge:
MFRC522 Hardware and Pinout
MFRC522 IC has 32 pins in total and it comes in SOT617-1 package.
Pin description can be found from the datasheet of MFRC522. To use the MFRC522 IC its module is widely available and quite inexpensive. Can be brought from Robu at cost of 150 rupees. With the module an RFID tag and a key fob tag of MIFRAME Classic 1KB comes in.
In MFRC522 module, IC has 8 pins exposed out for connection and connecting to host MCU.
- The module has 4 pins exposed out for connecting it to host MCU using either SPI, I2C or UART. By default, reader can communicate with a microcontroller over a 4-pin SPI with a maximum data rate of 10 Mbps. It also supports I2C and UART protocols. As told in MFRC522 functional description part, MFRC522 checks the host interface type automatically depending on the signals on it’s control pins.
- MFRC522 module has 1 interrupt pin also exposed out, which can be used to trigger interrupts to alert the microcontroller when a RFID tag is in the vicinity.
- And remaining 3 pins are power supply pins and a reset pin. Reset pin is used for power down mode and reset signal. Module requires the power supply of 3.3 V, that is provided via VCC and GND pins.
Other vendor RFID readers
STMicroelectronics
Texas Instruments
Infenion technologies
Sensor & Modules to explore
Technologies to Explore
Other Blog to Explore
Introduction on PlatformIO
Content Index Table of Contents Complete Beginner’s Guide to Platform IO Introduction Platform IO is a platform that allows core developers to compile the same code with different platforms using only one command i.e. platform io run. It is an extension in VSCode and hence can be used with our favorite code editor. It has many features that allow core developers to have a smooth development experience. Also it allows you to export and edit the same project in windows as well as windows or any other systems without any changes required. Installing Platform IO It is a VSCode extension and can be simply installed by adding Platform IO from the extensions center of VSCode Figure 1 Creating a Project in Platform IO Click on Platform IO Logo in VS Code Figure 2 In PIO Home 🡺 Go to Open 🡺 Home Screen of PlatformIO is visible Select New Project 🡺 Give a name and select board for project 🡺 Select Location 🡺 Done Figure 3 Figure 4 Figure 5 Figure 6 New Project will be created You can write code in main.cpp under the src folder, we can edit the platform under platformio.ini to upload to different boards. Different Folders and files in Platform IO Platform IO consists of many folders to support embedded development on multiple systems and compiling information. Platform IO consists of: .pio: This folder consists of complete debug and compiler information of the project. This folder need not be uploaded and is included in gitignore as well to not upload on Github. <br> We don’t have any work in this folder and we don’t need to edit this folder. .vscode: This folder consists of JSON files required to run platform io and its services in VS Code editor. This folder also doesn’t need to be touched or edited to do our work. include: This folder is present to allow you to add “.h” header files into the PlatformIO project that are not added as libraries in your project. You can place libraries defined by you with .h extension to use them. Also, this code is not compiled and there is no use of putting a .c/.cpp file in this folder. lib: This folder is used to define complete libraries that are private that are made by you. In include, we only add .h files but this folder can consist of complete details like documentation, examples, source files, JSON, etc. Basically, it contains complete information related to a library. src: This folder consists of main.CPP and will contain the code that we want to write. We can add more files in this folder to integrate into our code. This is the main folder we will be working on. Code will start executing from main.CPP. test: This folder is used to write tests that can be used to automate the testing of the code. These tests can be used to see the efficiency of code and how code behaves on boundary conditions. These are generally useful in bigger projects. .gitignore: This folder is a part of the git family and consists of the name of the folder that needs to be and will not be uploaded on git while uploading the project. It generally includes the .pio folder and some files of the .vscode folder. platformio.ini: It is a kind of settings folder where you can decide the platform on which code to be uploaded, add libraries, control monitor speed, select framework, and many more things. Visit the given link to explore multiple options and their application in platform io : https://docs.platformio.org/page/projectconf.html Adding Libraries in PlatformIO project Platform IO provides multiple easy ways for you to add libraries to a project. We are going to discuss one of the easiest and most useful ways of adding libraries in Platform IO: Go to Platform IO symbol in the left pane of extension 🡺 Under PIO Home select Open. In the main window select Libraries 🡺 Search the library you need 🡺 Click on that library 🡺 Click on Add to Project 🡺 Select project in which you want to add library and click add. Now the library is added to your project. There are also other ways to add libraries in Platform IO by editing the platformio.ini folder. See the below link under ‘Project Dependencies’ to explore those methods: https://docs.platformio.org/en/latest/librarymanager/quickstart.html Uploading code to hardware using PlatformIO Platform IO provides a very easy way to upload code on the hardware. The following steps should be followed to upload the code: After writing the code, verify the details and environment in platformio.ini. You can select different hardware by removing the lines that look like the following and adding the setting for the required board. Setting can be googled to find the exact lines: [env:pro16MHzatmega328] platform = Atmel Avr board = pro16MHzatmega328 framework = Arduino You can also select multiple boards at one time using the method mentioned in the following link: https://community.platformio.org/t/changing-destination-boards/4751/2 Click on Platform IO Icon in left sidebar of VS Code Under Project Tasks 🡺 General, you have multiple options and operations that can be done for the project. Build 🡺 Compiles the code to see if any errors Upload 🡺 Compiles and upload the code to the hardware connected. Monitor 🡺 To open serial monitor in VS Code Upload and Monitor 🡺 Compiles uploads the code and open the serial monitor just after uploading code Clean and Clean All 🡺 Used to remove and delete any dangling pointer or function that is left in the stack and not deleted or removed. ** You can also have few options present in the bottom bar to compile, upload and monitor the code. Conclusion In the end I want to say that platform Io is an amazing platform that is used to develop embedded systems code, and has integrated most capabilities like writing, uploading, and even debugging of code in one place. It is a great platform to work on and will help you guys a lot. Author: Kunal
W25Q128JV SPI Flash Memory: Part1
Table of Contents W25Q128JV SPI Flash Memory interfacing with STM32 and AVR MCU So hello guys, welcome back to the Gettobyte once again. As I have told you that we are going to start with application codes also, so now in this blog what we are going to do?? We are going to interface the W25Q128JV SPI Serial flash memory module to our STM32 MCU and AVR8 MCU. Now let’s look at how this module looks. This module is very small and packed into small sizes. One can buy it easily from the Amazon Website. This module has 6 pins: VCC, GND, and 4 SPI Communication pins( MOSI, MISO, CE & SCK). But the IC has much more pins, that I will be briefing below. W25Q128JV SPI Flash Memory module Now starting with this Flash memory, In this blog, I will be telling you about its: General description of the IC. Features of this Serial Flash Memory. Pin Description. In the next subsequent blogs will be making the application code on AVR and STM32 MCU using SPI peripheral and peripheral driver. At first, will be making the application code on AVR MCU and then on STM32 MCU. So now moving forward, let’s begin our journey to it. General Description of W25Q SPI flash memory So starting with W25Q128JV. These are the Serial communication-based Flash memories into which we can store data. These can work as RAM memory for memory constraint embedded MCUs. We can transfer data from these memory chips in standard SPI serial communication up to a frequency of 133 MHZ and when used in Dual/Quad SPI Serial communication, data transfer frequency can go up to 236MHZ/532 MHZ. So one can read, write and fetch data from these memory chips at very high frequencies. These have 65536 programmable page lengths and in total there are 256 pages. That means it has 256 pages and on each page, we can write 65536 bytes. W25Q SPI flash memory depicted as a book On each byte of these pages, we can read and write at a redundancy of 10000 times. Up to 256 bytes can be programmed at a time. We will go into more detail when we will understand its block diagram of memory mapping and management. Features of W25Q SPI Flash Memory It can run on Standard SPI, DUAL SPI, and QUAD SPI. Standard SPI: is traditional SPI Protocol which has CLK,CS,MISO(DO),MOSI(DI) pins. In this, we have 1 pin(MOSI) for sending data from Master to Slave and another pin (MISO) for sending data from slave to master. It can run at Max speed of 133MHZ for standard SPI. To know more about SPI, refer to this blog Dual SPI: In DUAL SPI, we have 2 Output/Input pins. Which means at a time we can send data from 2 pins and receive data from 2 pins. Refer the below image, DI becomes IO0 and D0 becomes IO1, so at a time we can send and receive data from both of those pins. As 1 byte has 8 bits and bits 0 and 1 of my one byte are being transmitted or received simultaneously, thus our data transfer becomes 2x then standard SPI, where only 1 bit is commuted at a time. In Dual SPI Max speed it can run at is 266 MHZ. DUAL SPI QUAD SPI: In QUAD SPI, we have 4 Output/Input pins. Which means at a time we can send data from 4 pins and receive data from 4 pins. IO0,IO1,IO2,IO3 are the 4 pins from which data is commuted between slave and master. In Quad SPI MAX speed it can run at is 532 MHZ. QUAD SPI One can perform 100k program-erase cycles per sector and it has data retention for more than 20 years. Efficient continuously read for about 8/16/32 byte warp. Byte warp here means that it can read memory continuously in the chunks of 8/16/32 bytes in one single time. Lets say it is reading in 8 byte wrap, so at first read it will read 0-7 bytes, then in next 8-15 bytes, then 16-23 bytes. Then the other important thing is Advance Security features which this IC has. You will be able to understand these features in better way when we will go through the Status and Configuration registers of this IC. On the memory chip, We can lock the certain memory bytes, that is no one can write or read on configured memory bytes or size. We can use the OTP (one time password) to have password based memory protection We can access the memory bytes of the IC, in Blocks(64 KB), sectors(4KB) or single byte. Starting from the Top of memory or from the Bottom of memory. PINOUT of the W25Q128 Flash memory chip So W25Q128 has 8 pins. Depending on the package we have, the number of pins of the IC can increase or decrease. The module which we will be using is packaging WSON.Pin number 1 is CE (Chip Select), used to select the SPI slave by making a LOW signal to this pinPin number 2 is DO (Data Output), that is MISO pin in case of standard SPI and IO1 in case of DUAL/QUAD SPI.Pin number 3 is /WP (Write Protection pin) ( will tell you about it in the below section) and IO2 in case of Dual/Quad SPI.Pin number 4 is GND (Ground).Pin number 5 is DI(Data Input), that is MOSI pin in case of standard SPI and IO0 in case of DUAL/QUAD SPI.Pin number 6 is CLK, the Clock pin of SPI communication.Pin number 7 is /HOLD or /RESET pin ( will tell you about it in the below section) and IO3 in case of Dual/Quad SPI.Pin number 8 is VCC. Let’s just deep dive into the pin description of this IC Chip Select is held high, that is master has not selected the slave and all my pins would be at High impedance. When the CS pin is held low, the master has selected
STM32F103 & ADC: Single Channel Conversion
In the last post, we have gotten to know about the features of ADC Peripheral that we have in STM32 MCU. Now in this blog with the series of bare Metal Programming for Blue Pill, we will understand different register bytes and bits of ADC Peripheral to be used for using it in different features, configurations, and modes as we get to know about in the Previous Post. In this blog, we will be going through how to use ADC Peripheral for converting a Single Channel of ADC (only one ADC pin) and see its bare metal code. Single Channel Conversion Mode: Only one ADC pin is used, this mode is like when we have say connected only one analog sensor to one of the ADC pins of MCU like the Potentiometer is connected at PA0 pin. ADC Registers In depth for: Let\’s get into an in-depth understanding of which registers and their bits are used for configuring the ADC peripheral of Blue Pill in Single Channel Configuration. We will focus on bits and will give an explanation of why those bits are used for the 1. ADC_SR(ADC status Register) –>This register tells the status of ADC channel Conversion, as it name says. STM32F103 ADC Status Register: 11.12.1 Bit 1[EOC ]: This bit is set by hardware when a single channel of any group (Regular or Injected) is converted successfully. So this bit is used for monitoring when the ADC conversion is completed by using it inside the while loop if interrupts are disabled. 0: ADC Channel Conversion is not completed 1: ADC Channel Conversion is completed When hardware set this bit, we can clear this bit from the firmware end by setting the bit to 0 or by reading ADC_DR(ADC Data Register) Bit 4[STRT]: This bit is set by hardware when regular channel conversion has begun. So when we start the Regular Channel Conversion, we will use this bit inside the while loop to check that whether Regular Channel conversion has started or not. 0: No regular Channel Conversion has started 1: Regular Channel Conversion has started When hardware sets this bit, we can clear this bit from the firmware end by setting the bit to 0. Even if we don\’t clear this bit it will cause no effect during ADC Conversion. But its good practise to clear all bits of Status Register before starting the new conversion 2. ADC_DR(ADC Data Register) –> This register stores the converted digital data at a 12-bit resolution of the converted ADC channel. STM32F103 ADC Data Register 11.12.14 Bit 15:0 [DATA 15:0]: The ADC_DR is divided into two 16 bits groups. The first 16 bits from 0-15 contain the Converted value of the configured ADC Regular Channel. As our ADC is of 12-bit resolution, so this is left aligned or right aligned to 4 bits so as to get the 12 ADC converted data . Left Aligned or Right alignment of ADC Data Depends on ALIGN bit of ADC_CR2 So we will have following code and algorithm for ADC_SR & ADC_DR register:while(!(ADC1->SR & ADC_SR_STRT)) while(!(ADC1->SR & ADC_SR_EOC)); // wait till a group channel converstion has completed adc_data = ADC1->DR; //clear the EOC bit by reading DR register ADC1->SR &= ~ADC_SR_STRT; 3. ADC_CR1(ADC Configuration register 1) –> This register is used for the Configuration of ADC peripheral for Analog Watchdog Discontinuous Mode Interrupt Enable/disable Dual Mode configuration Scan Mode As we are not using any of these features, so all the bits for these registers will be set to zero, and to know about these features and their bit functions, u can navigate to corresponding blogs for those. STM32F103 ADC Configuration Register 1. 11.12.2 Bits 19:16 [DUALMODE 3:0]: These bits are used to configure the type of operating mode. In the blue pill, we have two ADC peripherals: ADC1 & ADC2. We can use these 2 ADC peripherals simultaneously by configuring the respective ADC in different modes or in independent modes. We are going to use these ADC in independent mode as will be using only ADC1 peripheral, so DUALMODE[3:0] will be set to 0. Bit 8[SCAN]: This bit is used to enable/disable the SCAN Mode feature in the ADC peripheral of BLUEPILL. Scan Mode is used when we convert more than 1 channel to scan all the configured channels in a Regular Group. As we are using only a single channel, means only one ADC pin is used so SCAN mode is not used and this bit will be set to 0. 0: Scan Mode disabled. 1:Scan Mode Enabled. So we will have following code and algorith for ADC_CR1 register:ADC1->CR1 &= ~(ADC_CR1_SCAN); // SCAN DISABLED, if using scan mode then dma must be enabled ADC1->CR1 &= ~(ADC_CR1_JDISCEN | ADC_CR1_DISCEN); // Discontinous mode disabled for both injected and regular groups ADC1->CR1 &= ~(ADC_CR1_DISCNUM_2 | ADC_CR1_DISCNUM_1 | ADC_CR1_DISCNUM_2 ); // no channels are configured in discontinous way. // if discontinous mode is enabled then number of //conversions to be done by discontinous mode has to be configured // DISNUM bits ADC1->CR1 &= ~(ADC_CR1_DUALMOD_0 | ADC_CR1_DUALMOD_1 | ADC_CR1_DUALMOD_2); // INDEPENDENT MODE SELECTED ADC1->CR1 &= ~(ADC_CR1_AWDEN | ADC_CR1_JAWDEN); // Analog watchdog disabled for both groups: regular and ibnjected 4. ADC_CR2(ADC Configuration Register 2) –> This register is used for the configuration of ADC Peripheral for: ADC Conversion enables/disabled for regular and injected groups and ADC peripheral enable/disable. Trigger source configuration for regular and injected groups. ADC Data Alignment, DMA, Continous, temperature sensor setting. STM32F103 ADC Configuration Register 2 11.12.3 Bit 0 [ADON]: ADC Peripheral On/OFF. This bit will be set to 1 to enable the ADC peripheral. All channel configurations and ADC Peripheral configurations have to be made before setting this bit to 1. 0: Disable ADC Peripheral 1: Enable ADC Peripheral Bit 1 [CONT]: This bit configures between Single Conversion mode and Continous Conversion mode. Continuous Conversion mode is selected when we have more than 1 ADC channel to be converted. As in this blog, we have
W25Q128JV SPI Flash Memory: Part2
Table of Contents So guys this is the continued blog on my interfacing W25Q128JV SPI flash memory with STM32 and AVR MCU. So in the last blog we have started with W25Q128 overview, its features and pin descriptions. Now in this blog we are going to talk about following topic: SPI standard instructions. Status and Configuration Registers of W25Q128JV SPI Serial Flash memory. Write protection features. About its block diagram of memory mapping & management. and then going to understand Status and Configuration Registers. Then in next blog we are going to start with its driver implementation on STM32 and AVR MCU. SPI Standard Instructions So as I have mentioned that module which we are going to use has standard SPI pins only on the breakout module( one can buy this module from robu). Remember the pinout of W25Q128 from last blog??? If not kindly refer to that W25Q128JV SPI Flash Memory: Part1 once before going further in this topic. W25Q128JV IO2 and IO1 pins are not available in the modules which we are going to use and buy. We can operate the SPI at mode 0(0,0) or mode 3(1,1), that is SPI CPHA or CPOL bits would be either 0,0 or 1,1. W25Q128JV would be used as a slave and host MCU would be used as a master. In standard SPI we can run this IC at frequency of 133MHZ for read-write operations. But in our sample codes i would be using the IC at 1 MHZ. Most Significant bit(MSB) is sent first during the SPI communication. Chip select pin(CS) would be used for selecting the slave. When CS is set as low, the slave is selected and when CS is set as HIGH, the slave would not be selected. Serial Data Input( DI) is the MOSI pin and Serial Data Output(DO) is the MISO pin. Serial Clock Input (CLK) pin is used as Serial Clock for SPI communication. During configuring of SPI pins for our host MCU, CS pin of the host MCU would be configured as Output pin. MOSI pin of the host MCU would be configured as OUTPUT pin. SCLK pin of the host MCU would be configured as INPUT pin. MISO pin of the host MCU would be configured as INPUT pin. Dual and Quad SPI are not of our concern, so we are not going to dig deep into those in this blog. though we are going to discuss standard SPI only if anybody has any things to ask related to Dual and Quad SPI they can reach out to me via gettobyte community. Status and Configuration Registers These are very important registers, plays an important role in configuring and using these memory chips. Their are 3 Status registers, SR1,SR2,SR3. Status register provide the status on the availability of the flash memory array, whether the device is write enabled or disabled, the state of the write protection, QUAD SPI settings, Security register lock status, and Erase/Program suspend status, output driver strength, and power up status. Also, status registers are used to configure the device write protection failures, QUAD SPI settings, Security register OTP locks and output driver strength. Each Status register can be read and write by specific commands. For reading the status register we have to issue the Read status register instructions for reading the corresponding Status register. One can read the Status registers of the memory chip when we want to know the status on the availability of the flash memory array, whether the device is write enabled or disabled, the state of the write protection, QUAD SPI settings, Security register lock status, Erase/Program suspend status, output driver strength, and power up status. For writing the status register we have to issue the write status register instructions for the corresponding Status register. One can write on the status registers when we want to configure the chip for the device write protection failures, QUAD SPI settings, Security register OTP locks and output driver strength. Status Register 1 S0: BUSY bit –> BUSY is a read-only bit in the status register (S0) that is set to a 1 state when the device is executing a Page Program(02h), Quad Page Program(32h), Sector Erase(20h), Block Erase(52h), Chip Erase(60h), Write Status Register(01h,31h,11h) or Erase/Program Security Register instruction(44h/42h). During this time the device will ignore further instructions except for the Read Status Register. So in short we can use this bit inside the while loop or if loop to check whether the device is ready for further instructions or not. erase_sector4KB(read_addr1); // device is executing a erase sector instruction if((ReadSR(ReadSR1) & BUSY_BIT) == 0x01) // Busy bit is set when erase sector instruction is send, so checking that { erase_sector4KB(read_addr1); } S1:WEL –> Write enable latch is also a read-only bit that is set to 1 after executing a Write enable instruction and making the chip in write enabled mode. Prior to every Page Program(02h), Quad Page Program(32h), Sector Erase(20h), Block Erase(52h), Chip Erase(60h), Write Status Register(01h,31h,11h) or Erase/Program Security Register instruction(44h/42h) we have to send the Write enable instruction. So after sending the write enable instruction we can read this bit to check whether Write enable Instruction is executed or not. It is cleared to 0 when the device is written disabled. Write disabled state occurs after the Page Program(02h), Quad Page Program(32h), Sector Erase(20h), Block Erase(52h), Chip Erase(60h), Write Status Register(01h,31h,11h) or Erase/Program Security Register instruction(44h/42h). So in short we can say that the WEL bit is used to check whether Write enable Instruction is executed or not. S2-S3-S4: Block Protect Bits(BP2, BP1, BP0) –> are read/write bits that can be used to protect the memory array from Program or erase instructions. One can protect ALL, NONE, or PORTION of the memory, corresponding configurations can be done for BP2, BP1, and BP0 bits according to the below tables. Also, see the TB and SEC bits for Write protection configurations S5: Top/Bottom BLOCK Protect(TB)–> This bit controls whether the memory protection has to be performed from TOP
PDB Explained
Prerequisites for PDB of ADC What is the PDB? The PDB is a powerful peripheral in microcontroller’s designed to provide controllable delays and trigger events based on specific conditions for ADC peripheral. Those control delays and trigger events allows us to get ADC Data, making it valuable for applications requiring accurate timing or synchronization. // In this elaborate following things: Point to be learned: Q: What do we mean by controllable delays?A: Controllable delays refer to the ability to set specific time intervals in a system to control when certain actions or events occur. It provides you with flexibility, configurable time delay, and precision. Q: What do we mean by trigger event?A: A trigger event is a signal(which can be external or internal) that prompts the system to begin a predefined action or series of actions. Q: What and which specific conditions we mean here?A: Configurable delay can be a counter-based delay, timer-based delay, or PWM-based delay. If we talk about PDB, here we use counter-based delay which involves using a hardware counter that increments or decrements based on a clock source. When the counter reaches this value, an event (such as a trigger or interrupt) is generated. And talking about the trigger event can be also classified into internal or external triggers, it is quite self-explanatory that if a trigger is received from a physical pin of the MCU then it is considered an external trigger and if triggers are initiated inside the MCU then it is called software trigger. Why is This Important? Understanding how to use the PDB effectively can greatly enhance your control over ADC operations. Whether you’re working on a high-precision measurement system, synchronizing multiple ADCs, or simply improving the timing accuracy of your applications, the PDB offers the flexibility and precision you need. How does PDB Peripheral works? ///// In this explain the relation of working of PDB peripheral. Q: How does PDB generate delaysA: As explained before, PDB works on a counter-based delay where we can specify the value of the counter. When the counter reaches this value, it will be reset back to zero. This counter register is labeled as “Modulus register (PDB_MOD)”. Let’s see how you can get a perfect modulus value for your desired requirement. Example: Peripheral(PDB) bus clock is 48MHz(this can also be modified in the clock configuration tool) and before moving to modulus calculation you are always asked to set the value of pre-scalar and multiplication factor(these two can be in Status and Control Register (PDB_SC)). Prescalar value is used to divide the peripheral clock and set your desired value of clock for operation and multiplication factor as it defines its name, it is used for multiplying with prescalar division. Now we are considering the MULT(Multiplication factor) as 40 and PRESCALAR(Prescalar Divider) as 64.PDB Period = ( (System Clock) / (Prescaler x Mult factor) )PDB Period = (48 MHz / (64 x 40)) = 18750HzNow, we have a clock value for PDB and you can create multiple MOD values from this clock. If we need a counter value for 1 second, we just multiply the PBD Period with “n” second.1 second = 18750 x 1 = 18750(MOD value)2 second = 18750 x 2 = 37500(MOD value)3 second = 18750 x 3 = 56250(MOD value)NOTE: The maximum value for MOD is 65536(MOD register is 16-bit register), which means with my current setting I cannot create a 4-second counter value(4 seconds = 18750 x 4 = 75000 > 65536). In this case, you need to reconfigure the values of PRESCALAR and MULT. Q: Explain the concept of channels in PDB peripheralA: In the Programmable Delay Block (PDB) peripheral, the concept of channels is central to its functionality. Each channel in the PDB is a separate entity that can be configured to trigger specific actions after a programmable delay. Each PDB channel is typically associated with a specific Analog-to-Digital Converter (ADC) block. For example, PDB0 will be linked to ADC0, and so on. PDB channels can be enabled or disabled independently. The concept of channels in the PDB provides flexibility in timing and synchronization, essential for applications requiring precise control over ADC conversions. Q: Concept of triggering in PDB by explaining terminologies like trigger event, trigger input, trigger sourceA: The concept of triggering in the Programmable Delay Block (PDB) is essential for coordinating and controlling actions such as ADC conversions. Trigger Input: The trigger input is the physical or logical signal that the PDB monitors to detect a trigger event. This input can come from various sources like pulling a pin up or down, triggering through Bit Manipulation of register, and all.Trigger Event:A trigger event refers to the occurrence of a specific condition or signal that initiates an action within the PDB.Trigger Source:The trigger source defines where the trigger input originates. Just mention the concept of the triggering mechanism of ADC peripheral and how is it achieved through PDB Relation of PDB delays with ADC peripheral How does the PDB peripheral control the ADC peripheral The Programmable Delay Block (PDB) includes a counter that compares its output to various preset digital values. When the PDB is enabled, a trigger input event will reset the counter and start counting. A trigger input event can be a rising edge detected on a chosen external trigger source, or it can be initiated by a software trigger. For each channel, a delay determines the time between the assertion of the trigger input event to the time at which changes in the pretrigger output signal are started. Each channel in the Programmable Delay Block (PDB) is linked to a specific ADC block. These pre-trigger outputs are connected to the ADC’s hardware trigger select. The role of the pre-triggers is to prepare the ADC block before the main trigger signal is applied. When the ADC detects a rising edge on the main trigger input, it starts the conversion process based on the settings and conditions established by the pre-triggers. The pre-trigger outputs are used to specify which signal will
Silicon Labs Thunderbolt EFM32BG22
IDE To be used: Simplicity Studio – Silicon Labs (silabs.com) SDK to be used: Gecko Platform Silicon Labs (silabs.com) SDK documentation: EFR32BG22 Gecko MCU and Peripheral Software Documentation – v5.9 – MCU EFR32BG22 API Documentation Silicon Labs (silabs.com) HDK to be used:SLTB010A EFR32BG22 Thunderboard Kit – Silicon Labs (silabs.com) HDK manual: UG415: Thunderboard EFR32BG22 User’s Guide (silabs.com) Microcontroller datasheet: EFR32BG22C112 Wireless Gecko SoC Data Sheet (silabs.com) Microcontroller Reference manual: EFR32xG22 Wireless Gecko Reference Manual (silabs.com) Author: Kunal Gupta
Author: Kunal Gupta
Author