#include "stm32f4xx.h" #include "stm32f4xx_rcc.c" #include "stm32f4xx_gpio.c" #include "stm32f4xx_usart.c" #include "LCD2x16.c" #include "dd.h" char *OutString; // string must be terminated by '\0' GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; //USART3 initialization, GPIOD 8:9, TX&RX, IRQ on RX&TX void USART3_init(int BaudRate) { RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3); GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); USART_DeInit(USART3); USART_InitStructure.USART_BaudRate = BaudRate; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_Init(USART3, &USART_InitStructure); USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); // enable IRQ on RX USART_Cmd(USART3, ENABLE); NVIC_EnableIRQ(USART3_IRQn); // Enable IRQ for USART3 in NVIC } // initialize port D (LEDS), Gpio_Pin_12 to GPIO_Pin_15 as outputs void LEDinit (void) { RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOD, &GPIO_InitStructure); } // initialize port E (switches), Gpio_Pin_3 to GPIO_Pin_6 as inputs void SWITCHinit (void) { RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; GPIO_Init(GPIOE, &GPIO_InitStructure); } void main(void) { LEDinit(); SWITCHinit(); USART3_init(921600); LCD_init(); while (1) { if (GPIOE->IDR & S370) LED_GR_ON; else LED_GR_OFF; if (GPIOE->IDR & S373) USART3->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 figure this out; do not step into the same trap. void USART3_IRQHandler(void) { // TX IRQ part if (USART3->SR & USART_SR_TXE) { // If TXE flag in SR is on then if (*OutString != '\0') // if not end of string USART3->DR = *OutString++; // send next character & increment pointer else // else USART_ITConfig(USART3, USART_IT_TXE, DISABLE); }; // RX IRQ part if (USART3->SR & USART_SR_RXNE) { // if RXNE flag in SR is on then int RXch = USART3->DR; // save received character & clear flag LCD_uInt16(RXch, 0x05, 1); // show ASCII on LCD if (RXch == 'a') LED_BL_ON; if (RXch == 'b') LED_BL_OFF; if (RXch >= 'A' && RXch <= 'Z') USART3->DR = ++RXch; // echo next character if (RXch == 'c') { // if 'c' => return string OutString = "ABCDEFGH"; // Init string & ptr USART_ITConfig(USART3, USART_IT_TXE, ENABLE); USART3->DR = *OutString++; // Send first character }; }; }