/* * * STM32F030F4P6 Uart Example using Interrupt Read & Write * */ // Includes #include "stm32f0xx.h" #include "stm32f0xx_rcc.h" #include "stm32f0xx_gpio.h" #include "stm32f0xx_misc.h" #include "stm32f0xx_usart.h" #include "stm32f0xx_flash.h" #include // Defines #define TX_BUFFER_SIZE 32 #define RX_BUFFER_SIZE 256 #if TX_BUFFER_SIZE < 2 #error TX_BUFFER_SIZE is too small, It must be larger then 1. #elif ((TX_BUFFER_SIZE & (TX_BUFFER_SIZE-1)) != 0 ) #error TX_BUFFER_SIZE must be a power of 2 #endif #if RX_BUFFER_SIZE < 2 #error RX_BUFFER_SIZE is too small, It must be larger then 1. #elif ((RX_BUFFER_SIZE & (RX_BUFFER_SIZE-1)) != 0 ) #error RX_BUFFER_SIZE must be a power of 2 #endif // Structures struct tx_buf_st { unsigned int in; // Next In Index unsigned int out; // Next Out Index char buf [TX_BUFFER_SIZE]; // Buffer }; struct rx_buf_st { unsigned int in; // Next In Index unsigned int out; // Next Out Index char buf [RX_BUFFER_SIZE]; // Buffer }; // Global Variables static struct rx_buf_st rbuf = { 0, 0, }; #define SIO_RBUFLEN ((unsigned short)(rbuf.in - rbuf.out)) static struct tx_buf_st tbuf = { 0, 0, }; #define SIO_TBUFLEN ((unsigned short)(tbuf.in - tbuf.out)) int tx_restart; int usart_timeout; int toggle; int main(void) { // Setting up STM32F030F4P6 to 48 MHz FLASH->ACR |= FLASH_ACR_PRFTBE; FLASH->ACR |= FLASH_ACR_LATENCY; RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_6); RCC_PLLCmd(ENABLE); while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) {} RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); // Init NVIC initNVIC(); // Init USART initUSART(); // Init GPIO initGPIO(); // Init Systick SysTick_Config(8000); //SysTick_Config(8000000); int i; int test; int wait; char buffer[16]; tx_restart = 1; test = 0; i=0; GPIO_SetBits(GPIOA,GPIO_Pin_2); while(1) { i++; SendChar(0xD); SendChar(0xA); SendChar(0xD); SendChar(0xA); SendString("USART Test "); SendInteger(i); SendChar(0xD); SendChar(0xA); for(wait=0; wait < 30000; wait++); SendString("Lese UART "); test = WaitForString(500,"OK"); if (test == 0) { SendString("OK"); } else { SendString("Fail"); } } } void SysTick_Handler(void) { usart_timeout++; if (toggle >= 1) { GPIO_SetBits(GPIOA,GPIO_Pin_0); toggle = 0; } else { GPIO_ResetBits(GPIOA,GPIO_Pin_0); toggle = 1; } } /* * * * * USART Funktionen * * */ void USART1_IRQHandler(void) { struct rx_buf_st *p; if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) // read interrupt { p = &rbuf; if (((p->in - p->out) & ~(RX_BUFFER_SIZE-1)) == 0 ) { p->buf[p->in & (RX_BUFFER_SIZE-1)] = USART_ReceiveData(USART1); p->in++; } if (SIO_RBUFLEN >= (RX_BUFFER_SIZE-2)) { USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); // Verhindert einen Pufferüberlauf } } if (USART_GetITStatus(USART1, USART_IT_TXE) != RESET) // read interrupt { p = &tbuf; if (p->in != p->out) { USART_SendData(USART1,p->buf [p->out & (TX_BUFFER_SIZE-1)]); p->out++; tx_restart = 0; } else { tx_restart = 1; USART_ITConfig(USART1, USART_IT_TXE, DISABLE); } USART_ClearITPendingBit(USART1, USART_IT_TXE); } } int WaitForString(int timeout, char *text) { int wfsLen; int wfsCount; int wfsData; wfsLen = strlen(text); usart_timeout = 0; wfsCount = 0; do { wfsData = GetKey(); if (wfsData == text[wfsCount]) { wfsCount++; } else { wfsCount = 0; } } while ((usart_timeout <= timeout) & (wfsCount != wfsLen)); if (wfsCount == wfsLen) return 0; return -1; } void SendString (char *text) { int count; int length; length = strlen(text); for(count = 0; count < length; count++) { SendChar(text[count]); } } void SendInteger (int Zahl) { int factor; int modRes; int Result[32]; int countR; factor = 10; countR = 0; do { modRes = Zahl % factor; Zahl = Zahl - modRes; modRes = (modRes / (factor / 10)) + 48; Result[countR]=modRes; countR++; factor = factor * 10; } while (Zahl != 0); for(modRes = countR; modRes >= 0; modRes--) { SendChar(Result[modRes]); } } int SendChar (int c) { struct tx_buf_st *p = &tbuf; // If the buffer is full, return an error value if (SIO_TBUFLEN >= TX_BUFFER_SIZE) return (-1); p->buf [p->in & (TX_BUFFER_SIZE - 1)] = c; // Add data to the transmit buffer. p->in++; if (tx_restart) { // If transmit interrupt is disabled, enable it tx_restart = 0; USART_ITConfig(USART1, USART_IT_TXE, ENABLE); // enable TX interrupt } return (0); } int GetKey (void) { struct rx_buf_st *p = &rbuf; if (SIO_RBUFLEN == 0) { return (-1); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); } else if (SIO_RBUFLEN < (RX_BUFFER_SIZE-2)) { USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); } return (p->buf [(p->out++) & (RX_BUFFER_SIZE - 1)]); } void initNVIC(void) { NVIC_InitTypeDef NVIC_InitStructure; /* Enable the USART Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void initGPIO(void) { GPIO_InitTypeDef GPIO_InitStructure; // Outputs GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); } void initUSART(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 19200; //115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; /* Enable GPIO clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); /* Enable USART clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /* Connect PXx to USARTx_Tx */ GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1); /* Connect PXx to USARTx_Rx */ GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1); /* Configure USART Tx, Rx as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); /* USART configuration */ USART_Init(USART1, &USART_InitStructure); /* Enable USART */ USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); USART_Cmd(USART1, ENABLE); }