2015-06-04 08:01 AM
Dear community,
I am trying to initialize the UART of the STM32F051 µC – however receiving of data does not work because the interrupt is not triggered. I recognized only the weak initialization of the UART1 interrupt handler in the startup file so I added myself the required lines. Unfortunately it does not work. I would be very happy if someone could check the following code for mistakes.
void COM_USART1_Init() { //COM_TypeDef COM, USART_InitTypeDef* USART_InitStruct) GPIO_InitTypeDef GPIO_InitStructure1; //GPIO STruct GPIO_InitTypeDef GPIO_InitStructure2; //GPIO STruct USART_InitTypeDef USART_InitStructure; //USART Struct NVIC_InitTypeDef NVIC_InitStructure; //for USART //GPIO for USART RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); //GPIOA_AHBPeriph_CLOCK // Configure the GPIO_struct USART Tx as alternate function push-pull GPIO_InitStructure1.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure1.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure1.GPIO_OType = GPIO_OType_PP; //PushPull (Gegentakt) GPIO_InitStructure1.GPIO_PuPd = GPIO_PuPd_UP; //NO Pull GPIO_InitStructure1.GPIO_Speed = GPIO_Speed_10MHz; // Configure the GPIO_struct USART Rx as alternate function push-pull GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure2.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure2.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_10MHz; //Init GpioStruct GPIO_Init(GPIOA, &GPIO_InitStructure1); GPIO_Init(GPIOA, &GPIO_InitStructure2); GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1); //Connect the PINS and USART BUS GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1); //NVIC for USART USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // enable the USART1 receive interrupt USART_ITConfig(USART1, USART_IT_TXE, ENABLE); // enable the USART1 send interrupt // Enable the USARTx Interrupt NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); //USART 1 // USARTx configured as follow: // - BaudRate = 115200 9600 baud // - Word Length = 8 Bits // - One Stop Bit // - No parity // - Hardware flow control disabled (RTS and CTS signals) // - Receive and transmit enabled RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //ENABLE USART3 CLK USART_InitStructure.USART_BaudRate = 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; // USART configuration USART_Init(USART1, &USART_InitStructure); // Enable USART USART_Cmd(USART1, ENABLE);}void USART1_IRQHandler (void) {if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { unsigned char received = USART_ReceiveData(USART1); //Receiver //set DAC }}//STARTUPCODE.sUSART1_IRQHandler PROC EXPORT USART1_IRQHandler [WEAK] B . ENDP ; EXPORT USART1_IRQHandler [WEAK] ; EXPORT USART1_IRQHandler [WEAK]Thank you very much for reviewing my code.
2015-06-04 08:17 AM
Look, you're using synchronous logic, you need to ENABLE the clock for it to function. Enable the USART1 clock *BEFORE* you touch anything inside the peripheral.
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // Do it earlier
If you're using C++ (.CPP) watch for name mangling.2015-06-04 08:59 AM
Thank you for responding…
It sounds very promising however for other processors such as the STM32L100RC it seems to be not relevant since I copied and pasted the UART initialization (and just changed a few lines). Is there a significant difference between both the processor types STM32L100RC and STM32F051 regarding “synchronicity�? ?
2015-06-04 09:19 AM
They are all dependent on the correct sequencing of code, you can't just slap-dash the cut-n-paste. You need the right code, in the right order.
void COM_USART1_Init() //COM_TypeDef COM, USART_InitTypeDef* USART_InitStruct)
{
GPIO_InitTypeDef GPIO_InitStructure; //GPIO STruct
USART_InitTypeDef USART_InitStructure; //USART Struct
NVIC_InitTypeDef NVIC_InitStructure; //for USART
//GPIO for USART
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //PushPull (Gegentakt)
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1); //Connect the PINS and USART BUS
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
//NVIC for USART
// Enable the USARTx Interrupt
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// USART1 configured as follow:
// - BaudRate = 115200 baud
// - Word Length = 8 Bits
// - One Stop Bit
// - No parity
// - Hardware flow control disabled (RTS and CTS signals)
// - Receive and transmit enabled
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //ENABLE USART1 CLK
USART_InitStructure.USART_BaudRate = 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;
// USART configuration
USART_Init(USART1, &USART_InitStructure);
// Enable USART
USART_Cmd(USART1, ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // enable the USART1 receive interrupt
// DON'T ENABLE UNLESS YOU HAVE DATA TO SERVICE IT
// USART_ITConfig(USART1, USART_IT_TXE, ENABLE); // enable the USART1 send interrupt
}
2015-06-04 11:35 PM
Dear Clive1,
thank you very much - now USART1 is able to receive data while triggering the interrupt.