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.
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
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.
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!