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
Getting started with AVR and Arduino
Home Category Child Category Part I – Untangling the wires: On your journey of exploring the different areas of embedded systems and microcontroller programming, you must’ve come across the term ‘AVR’. This blog (or series of blogs) will try to demystify all the terminologies. Prerequisites Before we proceed, this blog assumes that you already have a basic overview and understanding of what microcontrollers are, what their basic functionalities are, and how they differ from microprocessors. Knowledge of the C/CPP programming language would be beneficial. Table of Contents Introduction Arduino vs AVR The family of AVR microcontrollers Keywords Further reading References Introduction The technical definition of what AVR is, is: The AVR microcontroller is a family of mostly 8-bit microcontrollers developed by Atmel (now Microchip), that follow the Harvard RISC architecture. Let\’s try to break all this technical jargon down. Harvard architecture is a type of architecture where the instructions (program code) and data are stored in different parts of memory. This is in contrast to Von Neumann\’s architecture where no such distinction is made. The following illustration might help you understand the concept a little better. Now coming to the other piece of jargon that might’ve thrown you off RISC. It stands for Reduced Instruction Set-Computer. It is a computer architecture philosophy that generally follows the idea of having smaller and atomic instruction sizes when compared to its counterpart CISC (Complex Instruction Set-Computer), which has relatively bigger instructions. We won\’t be diving into this as this is a topic for another day. Arduino vs AVR Before we move any further let\’s clear one big misconception out of the way. People tend to confuse the terms Arduino and AVR quite often. Although it wouldn\’t be fair to compare these two (it would be like comparing an engine to a car), the following table clearly explains what are the differences between the two. In this article when we use the word “Arduino”, we will be referring to the Arduino platform which includes all the hardware and software made by the company. Arduino AVR 1. Is hardware and software platform which includes, but is not limited to: the Arduino IDE, Arduino Uno board, and the Arduino programming language 1. Is a family of microcontrollers developed in 1996 by Atmel. It is only a piece of hardware. 2. The Arduino Uno is a board that is built around the ATMega328, which is an AVR microcontroller. Apart from the microcontroller itself, it has other components such as a USB2.0 cable and a PCB on top of which the components such as resistors and capacitors are wired together. 2. Is a microcontroller that requires additional passive and active components (clock, LEDs, etc.) to be wired up by the end-user for it to work. 3. Products are developed and maintained by Arduino (company) 3. Products are developed and maintained by Microchip Technology Inc 4. An Arduino Uno board:Notice the various components such as voltage regulators, connectors, switches, LEDs that are already soldered onto the board.The rectangular black IC (Integrated Circuit)/chip on the bottom right-hand corner of the board is the engine that drives the car (board), ie, the ATmega328, which is an AVR microcontroller 4. The ATmega328P, an AVR microcontroller: It doesn’t come packaged with any components, which have to be attached by the vendor/programmer themselves Difference Between Arduino and AVR The family of AVR microcontrollers Microchip offers a wide selection of microcontrollers to choose from, each meeting a different set of requirements. They can broadly be divided into three categories. Series Package size FlashSize Operating Frequency Example ATtiny(TinyAVR) 8-32 pins 0.5kB – 32kB 1.6MHz-32Mhz ATtiny85 ATMega(megaAVR) 28-100 pins 4kB – 256kB 1.6MHz-32Mhz ATMega328 ATxmegaXMEGA 44-100pins 16kB – 256kB 1.6MHz-32Mhz ATXmega128 AVR MCU Family Series Differences The above table isn’t exhaustive as there are many other families of microcontrollers such as the 32-bit AVR32 and the newly released AVR Dx series. The family of microcontrollers you end up choosing, totally depends on the scope of your project. Looking for a very small compact and lower power microcontroller which can automate a basic task, like toggling an LED-based on an input switch? Then go for the ATtiny family. Looking to control a servo motor that is connected to an IR sensor? You definitely won\’t go wrong with the ATmega series. Still, looking for more firepower for controlling multiple sensors? The ATxmega series has got your back. In the upcoming blog, we shall deep dive into one of the most widely loved and used microcontrollers – the humble yet mighty ATmega328. Till next time! Ciao! Further Reading http://ce.sharif.edu/~pourmohammadi/AVR%20Microcontroller%20and%20Embedded%20Systems/AVR%20Microcontroller%20and%20Embedded%20Systems.pdf https://en.wikipedia.org/wiki/AVR_microcontrollers http://www.avrbeginners.net/ References https://www.microchip.com/en-us/product/ATmega328P Intro Video on Device Drivers and Application Code for AVR(Arduino Boards) – YouTube Author: Kunal Gupta
Static Variables in C
Static is a keyword in the c/cpp language. It is a storage class specifier that can be applied to any data type. A static keyword can be used with both variables and functions. The static keyword tells the compiler to make the variable or function limited to scope but allows it to persist throughout the life of a program. The static keyword limits the scope of variables or functions to the current source file only. So static keyword helps in hiding the data to one source file only. Static words help in achieving encapsulation in c language. In this blog, we are going to cover the use of static keywords with variables. Static variables are stored in BSS or data segment of the C program Memory Layout. The BSS segment contains the uninitialized data. The DATA segment keeps the initialized data. The static variable is only initialized once, if it is not initialized then it is automatically initialized to 0. void demo(int value) { static int count = 0; count = value; cout<<count; } count = value: is not initialization, it is an assignment. Static can be assigned as many times as we wish. static int count = 0: is initialization & that happens only once for static variables. We can have static global variables or static local variables. Static local variables Static variables are able to retain their value b/w function calls. Static keywords, when used with variables inside the function, are then called static local variables. Static global variables The static keywords when used with variables outside the functions, are then called static global variables. Static global variables help in achieving enumeration, that is they make the scope of the variable limited to the present file only. References: The static keyword in C (c-programming-simple-steps.com) Author: Kunal Gupta
GPIO Peripheral in S32K144 MCU
What is GPIO peripheral in NXP S32K1xx MCU || Briefing about how to use GPIO peripheral in NXP S32K1xx MCU’s
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
Implementation of BLE on STM32WB55
Table of Contents About GPIO Peripheral The pins which can be configured by the software at runtime to perform various functions are called GENERAL-PURPOSE INPUT/OUTPUT (GPIO) pins. With the help of software one can program the GPIO pins mainly as: Digital Output It compares the external voltage signal with a predefined threshold. Digital Input It controls the Voltage of the pin Analog Function It performs ADC (Analog to Digital Conversion) or DAC (Digital to Analog Conversion) Other Functions or Alternate Functions It makes the pin to perform other functions like PWM output, timer-based captures, external interrupts, and various other interfaces like SPI, I2C, UART communications. Before coming to the Schmitt Trigger Understanding, have a quick recap of Pull Up, Pull Down and Open Drain Configurations from MPU6050 Implementation blog Implementation of MPU6050 with STM32 – gettobyte GPIO Input: Schmitt Trigger GPIO Output Speed Slew Rate GPIO Input: Schmitt Trigger A SCHMITT TRIGGER is a device which uses a voltage comparator to convert a noisy or slow signal edge into a clean and desirable edge instantaneously. For a real time system, the external signals do not change instantly, due to slower slew rate which depends on inheritance parasitic capacitance, resistance or an inductor at the input side. As the processor chip has a Schmitt Trigger, it increases the slew rate and increases the noise immunity for the signals which are captured. Let us understand the implementation of the Schmitt Trigger. It consists of a voltage comparator with positive feedback. The output Vout depends between two input voltage V+ and V–. If V+ > V–, Vout is quickly saturated to VSAT, otherwise Vout = 0 For an ideal op-amp, the current flowing through resistor R3 is zero and thus we have Vref = V– The op-amp output Vout has two saturation values, as shown below Vout = VSAT if V– <V+ 0 if V– < V+ However, V+ depends on Vout and Vin· Therefore, Vout depends on both the input Vin and the recent history of Vout· Such an effect is called hysteresis. Using KCL, assuming that the current flow in the non inverting input terminal of op-amp is zero, Vin – V+ /R2 = V+ – Vout / R1 On solving the above equation we will get, Vin = R2Vout + R1Vin / R1 + R2 At the time instant when Vout transits from one saturation value to the other saturation value, we have V+ = Vref Thus, Vref = R2Vout + R1Vin / R1 + R2 Solving further we get, Vin = (1 + R2/ R1)Vref – (R2/R1)Vout As discussed earlier, Vout has only two possible values. If Vout = 0 initially and Vin increases, we can obtain the trigger high threshold VTH at which Vout transits to VSAT: VTH = (1 + R2/R1) Vref – (R2/R1)*0 = (1 + R2/R1)Vref On the other hand, if Vout = VSAT initially and Vin decreases, we can obtain the trigger low threshold VTL at which Vout transits to 0: VTL = (1 + R2/R1) Vref – (R2/R1)VSAT Vout can be determined by comparing it with two thresholds VTH and VTL. When Vin climbs through VTH , Vout is rapidly switched to the upper limit VSAT· Conversely, once Vin falls below VTL, Vout makes a transition to the lower limit. Note that VTH > VTL , i.e., the threshold for switching to high is greater than the threshold of switching to low. A Schmitt trigger when compared, Provide a better boise rejection. Larger threshold for switching high and low for switching. Immune to undesired noise. GPIO Output Speed Slew Rate The SLEW RATE of a GPIO pin is the speed of change of output voltage with respect to unit time. Slew Rate = ΔV/ Δt In simple words, If the GPIO pin changes from LOGIC LEVEL 0 to LOGIC LEVEL 1, the voltage changes from 0V to 5V in just 5µs, then the slew rate is simply 1V/µs. The higher the slew rate, the shorter time the output voltage takes to rise or fall to desired values. Therefore, a higher slew rate allows faster speed at which the processor can toggle the logic level of a GPIO pin. A shorter rise and fall time allows a GPIO pin to change its logic value more rapidly. A high slew rate can result in significant electromagnetic interference (EMI), also known as radio frequency interference (RFI), to nearby electronic circuits. This is due to the large-amplitude and high-frequency harmonics produced by a fast-rising and falling signal, which can cause malfunctions in a victim circuit through radiation, conduction, or induction. To reduce EMI disturbance, a slower slew rate is generally preferred. GPIO IN STM32WB Each GPIO port has 4 32-bit Configuration Registers (GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR and GPIOx_PUPDR) 2 32-bit Data Register (GPIOx_IDR and GPIOx_ODR) A 32-bit Set/Reset Register (GPIOx_BSRR) A 32-bit locking Register (GPIOx_LCKR0 and 2 32-bit Alternate Function Select Register (GPIOX_AFHR and GPIOX_AFLR) Main feature sog GPIO are Output states: push-pull or open drain + pull-up/down Speed selection for each I/O Input states: floating, pull-up/down, analog Fast toggle capable of changing every two clock cycles Highly flexible pin multiplexing allows the use of I/O pins as GPIOs or as one of several peripheral functions GPIO Functional Description The port bit of GPIO can be configured by the software depending upon the hardware characteristics in various modes such as: Input floating Input pull-up Input-pull-down Analog Output open-drain with pull-up or pull-down capability Output push-pull with pull-up or pull-down capability Alternate function push-pull with pull-up or pull-down capability Alternate function open-drain with pull-up or pull-down capability Each I/O port bit is freely programmable, however the I/O port registers have to be accessed as 32-bit words, half-words or bytes. The purpose of the GPIOx_BSRR register is to allow atomic read/modify access to any of the GPIOx_ODR registers. In this way, there is no risk of an IRQ occurring between the read and the modify access. The above diagram shows the basic structure of a
Author: Kunal Gupta
Author