#include "stm32f4xx.h" #include "LCD2x16.c" char *OutString; // string must be terminated by '\0' void main(void) { // GPIO clock enable, digital pin definitions RCC->AHB1ENR |= 0x00000001; // Enable clock for GPIOA RCC->AHB1ENR |= 0x00000010; // Enable clock for GPIOE GPIOE->MODER |= 0x00010000; // output pin PE08: time mark GPIOE->MODER |= 0x00040000; // output pin PE09: toggle GPIOA->MODER |= 0x00001000; // output pin PA06: LED D390 GPIOA->MODER |= 0x00004000; // output pin PA06: LED D391 GPIOC->MODER |= 0x00900000; // PC10, PC11 => AF mode GPIOC->AFR[0] |= 0x00000010; // select AF1 (TIM2) for PA01 -> TIM2_CH2 //UART4 initialization RCC->APB1ENR |= 0x00080000; // Enable clock for UART4 RCC->AHB1ENR |= 0x00000004; // Enable clock for GPIOC GPIOC->MODER |= 0x00a00000; // PC10, PC11 => AF mode GPIOC->AFR[1] |= 0x00008800; // select AF8 (UART4,5,6) for PA10, PA11 UART4->BRR = 0x1120; // 9600 baud //UART4->BRR = 0x0890; // 19200 baud //UART4->BRR = 0x0450; // 38400 baud //UART4->BRR = 0x02e0; // 57600 baud //UART4->BRR = 0x016d; // 115200 baud UART4->CR1 |= 0x200c; // Enable UART for TX, RX UART4->CR1 |= 0x0020; // Enable RX interrupt // LCD init LCD_init(); // Init LCD //NVIC init NVIC_EnableIRQ(UART4_IRQn); // Enable IRQ for UART4 in NVIC // endless loop while (1) { if (GPIOE->IDR & 0x0001) GPIOA->ODR |= 0x0040; // LED on else GPIOA->ODR &= ~0x0040; // else LED off if (GPIOE->IDR & 0x0002) UART4->DR = 'a'; // send for (int i = 0; i<100000; i++) {}; // waste time }; } // IRQ function // caution: placing breakpoints in IRQ corrupts the execution! // this happends due to the reading of the DR by the IAR and changes IRQ flags! // I wasted sevaral hours to find this out. Do not step into the same trap. void UART4_IRQHandler(void) { // TX IRQ part if (UART4->SR & 0x0080) { // If TXE flag in SR is on then if (*OutString != '\0') // if not end of string UART4->DR = *OutString++; // send next character and increment pointer else // else UART4->CR1 &= ~0x0080; // disable TX interrupt }; // RX IRQ part if (UART4->SR & 0x0020) { // if RXNE flag in SR is on then int RXch = UART4->DR; // save received character & clear flag LCD_uInt16(RXch, 0x05, 1); // show ASCII on LCD if (RXch == 'a') GPIOA->ODR |= 0x0080; // if 'a' => LED on if (RXch == 'b') GPIOA->ODR &= ~0x0080; // if 'b' => LED off if (RXch >= 'A' && RXch <= 'Z') UART4->DR = RXch; // echo character if (RXch == 'c') { // if 'c' => return string OutString = "ABCDEFGH"; // Init string & ptr UART4->CR1 |= 0x0080; // Enable TX IRQ UART4->DR = *OutString++; // Send first character }; }; }