STM32 USART Pt. 2 : Interrupts
In the previous post I showed you guys how to setup the USART in its most basic mode which is UART with no interrupts. In this second part I will explain the register bits and relevant code to configure the UART in interrupt mode. Interrupt allows for non-blocking reception and transmission of our data. Our program is free to do other task and not concern itself with the constant polling of the status register.
Control Register 1:
From the previous post you will recall the Control Register 1. Whip out your datasheet and take a look or just follow along here. Below you see a screenshot of CR1 and I have highlighted the relevant bit that we must set to enable desired interrupts.
- TXEIE : TX Empty Interrupt Enable: By enabling this bit we will get an interrupts when the TX buffer, in other words the data register, is empty.
- TCIE : Transfer Complete Interrupt Enable: This will generate an interrupt once the data transmission is complete.
- RXNEIE : This will enable an interrupt to be generated when we have received data.
The NVIC:
Below is an snapshot of the NVIC table found on page 199 of RM0008 . The left most value is the position which is equivalent to the IRQ number. The next value is the priority. The "settable" means that the priority is settable. next is the peripheral acronym, followed by the description and ultimately on the far right is the address where the microcontroller will jump to expecting to find an interrupt handler. One neat trick to remember is that the name of the interrupt handling function is just like the acronym plus "_IRQHnadler". So the interrupt handling function for I2C2_ER is nothing more than
void I2C2_ER_IRQHandler(void)
Remember that interrupt handling functions, or interrupt servicing routines (ISR) should always be of void return type and take void arguments. If you can comment below and tell me why this is so, you will get a total of 1 cookie.
Also notice in the image above that the USART interrupts are described as global. This means we do not get an interrupt handler for specifically for each event like RX not empty or TX empty or Transmit complete. We have to use the same exact interrupt handler for all of them and we will not know which one of the 3 generated the interrupt unless we check the status register in our interurpt routine. So what I am saying here is that you HAVE to check your status register in the ISR to find out which interrupt was generated and why you have landed in the ISR, once you know which flag is set in the status register you handle it accordingly. Check out the code below. Skim through it and I will explain the new lines afterwards. This code is the same code for the basic UART code in the last post. I will only explain the new lines.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | /* Includes */ #include "stm32f10x.h" int main(void) { //-----------------------| UART CODE |------------------------------------------ //USART1 / GPIOA clock enable RCC->APB2ENR |= RCC_APB2ENR_USART1EN | RCC_APB2ENR_IOPAEN; //remaping if needed //AFIO->MAPR |= AFIO_MAPR_USART1_REMAP ; //remap RX TX to PB7 PB6 //pin configurations: PA9- TX is set to ALternate-push-pull and 50MHz GPIOA->CRH |= GPIO_CRH_MODE9 | GPIO_CRH_CNF9_1; GPIOA->CRH &= ~(GPIO_CRH_CNF9_0); /* * PIN PA10 is the RX pin and it has to be set to input and FLOATING * this is the rest value of the pin so we really dont have to do anything to it. * * */ //USART DIV value USART1->BRR = 0x1D4C; //for 72MHZ on APB1 bus //enable RXNE and TXE interrupts on USART SIDE USART1->CR1 |= USART_CR1_RXNEIE | USART_CR1_TXEIE; //----------| RX enable TX enable UART enable USART1->CR1 |= USART_CR1_RE | USART_CR1_TE | USART_CR1_UE; //ENABLE interrupt for USART1 on NVIC side NVIC_EnableIRQ(USART1_IRQn); while (1) { } } void USART1_IRQHandler(void) { //check if we are here because of RXNE interrupt if (USART1->SR & USART_SR_RXNE) //if RX is not empty { char temp = USART1->DR; //fetch the data received USART1->DR = temp; //send it back out while (!(USART1->SR & USART_SR_TC)) ; } //check if we are here because of TXEIE interrupt if (USART1->SR & USART_SR_TXE) //if RX is not empty { //handle transmit completion here } } |
Line 28 Enables the inteerupts on the UART. ANd line 34 enables the interrupt on the NVIC block. Lines 42 through 61 are my ISR. As you can see in line 45 I am checking the STATS register to see if RXNE is set, because then that would have generated the interrupt and explain why I am in the ISR and I handle the code accordingly by inserting the exact same cod I had for the echo program in the last post. If you recall the echo program was all in my while loop, but now it is not. My while loop is free to do other things.
Line 55 I am checking to see if I am in the ISR because my Transmit register is empty... I chose not to implement nothing in that case but you surely can if you need to.
Remember to keep your ISR routines short and sweet. You are halting the main program. Also your ISR routines should not call other routines, its best to use flags and state or any kind of variable and then have you main loop check for those flags and call the appropriate routines.
Dont forget to check out the video version of these tutorials on my YouTube channel
Well kiddos that about does it for this. simple UART post. Peace!
hi bro.
ReplyDeletei wathed your USART DMA education on youtube.it was very good.
next i come to your site to visiting it code...
but i could not find it...
please put your code here or give me your **github link** or other thing.
thank you
I can't make my code work for stm32F411. It always jumps to an infinite loop.
ReplyDeleteInfinite_Loop: // startup_stm32f411retx.s
b Infinite_Loop
#include
#include "string.h"
#include "stm32f4xx.h"
char UART2_Read(void);
void USART2_write(int ch);
int main(void){
RCC->AHB1ENR |= 1; //Enable GPIOA clock
RCC->APB1ENR |= 0x20000; //Enable USART2 clock
GPIOA->MODER |= 0x0020 | 0x0080;
GPIOA->AFR[0] |= 0x0700 | 0x7000;
USART2->CR1 |= 0x0008 | 0x0004; //Enable Tx/Rx, 8-bit data
USART2->BRR = 0x0683; //9600 baud @ 16 Mhz
USART2->CR2 = 0x0000; //1 stop bit
USART2->CR3 = 0x0000; //no flow control
USART2->CR1 |= 0x0020; // RXEIE (Recieve Interrupt)?
// USART2->CR1 |= 0x0080; // TXEIE (Transmit Interrupt)
USART2->CR1 |= 0x2000; // Enable USART2
//ENABLE interrupt for USART2 on NVIC side
NVIC_SetPriority(USART2_IRQn,0);
NVIC_EnableIRQ(USART2_IRQn);
while(1){
// USART2_write(toupper(UART2_Read())); // This works!!!
}
}
char UART2_Read(void){
while(!(USART2->SR & 0x0020)){}//Wait till character arrives (blocking code)
return USART2->DR;
}
void USART2_write(int ch){
while(!(USART2->SR & 0x0080)){} //Wait for transfer buffer to be empty
USART2->DR = (ch&0xFF);
}
char temp; // global char variable.
// This does not work. It causes an error exception handle jumping to an infinite loop.
void USART2_IRQHandler(void) {
if (USART2->SR & 0x0020) //Wait till character arrives (blocking code)
temp = USART2->DR;
}
Hey Michael have you checked video version of this write ups on YouTube
DeleteHi Eddie,
ReplyDeleteThank you for your response. No, I have not checked the video version.
But, with the help of the ST community, other developers help me to see the root cause. I did not tell you that this is written in C++ (main.cpp). I need to write it in C++ to be combined with my other C++ code.
Anyway, here is the solution.
extern "C" void USART2_IRQHandler(void);
The extern C will prevent C++ compiler to alter my ISR function name. Now, it is working.
Thank you for your example.
Michael
Hi eddie , can these global interrupts be used ffor other purposes along with data communication like takes sensor data, when txe is 1 or something like that.
ReplyDelete