Set up UART Interrupt using register
My UART example works very well. But, I want to do with the UART interrupt. Currently it does not work because it always jumps to an infinite loop in the start up assembly code.
Infinite_Loop: // startup_stm32f411retx.s
b Infinite_Loop
Here is my source code. It works great without USART receive interrupt enabled. USART2_IRQHandler is never executed. I have put a breakpoint inside it. Instead, it always jumps to the Infinit_loop line. What did I do wrong? Why your HAL / LL examples work? I looked into the HAL / LL code. It is very similar to my source code.
Thank you for any help you can provide.
Michael
//#include "my_stm32f4_uart_driver.h" // No NVIC_SetPriority, NVIC_EnableIRQ
#include <ctype.h>
#include "string.h"
#include "stm32f4xx.h" // I need to use this library for NVIC_SetPriority, NVIC_EnableIRQ
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 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;
}
