2012-05-22 06:25 AM
Hello,
I try to work with the UART4 RXNE interrupt but something went wrong. // USART2-TX, UART4-TX GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_UART4); // USART2-RX, UART4-RX GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_UART4); //configure NVIC //select NVIC channel to configure NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn; //set priority to lowest NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x1; //set subpriority to lowest NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x1; //enable IRQ channel NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //update NVIC registers NVIC_Init(&NVIC_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x2; NVIC_Init(&NVIC_InitStructure); USART_DeInit(UART4); USART_ClockStructInit(&USART_ClockInitStructure); USART_ClockInitStructure.USART_CPHA = USART_CPHA_1Edge; USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low; USART_ClockInitStructure.USART_Clock = USART_Clock_Enable; USART_ClockInitStructure.USART_LastBit = USART_LastBit_Enable; USART_ClockInit(UART4, &USART_ClockInitStructure); USART_StructInit(&USART_InitStructure); USART_InitStructure.USART_BaudRate = 19200; USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_Init(UART4, &USART_InitStructure); USART_Cmd(UART4, ENABLE); //enable Receive Data register not empty interrupt USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); USART_ITConfig(UART4, USART_IT_TXE, DISABLE); This is my init source code. I can send strings over my functions but if I try to send a char from the computer, the STM32 dont jump into the interrupt handler. Here is my function, which will be called in the UART4_IRQHandler: if(USART_GetITStatus(UART4, USART_IT_RXNE) == SET) { uint16_t temp = UART4->DR; UART_SendChar(temp); } My idea is that every character send back to the computer. Whats wrong with this code? Thanks, Dennis #interrupt #usart #tags-are-pointless #uart #stm322012-05-22 07:18 AM
With the cut-n-paste, it's not so much what's there, but what's missing.
Hopefully you've enabled the clocks on the peripheral. Things generally fail if the clocks are not on, the interrupt name is wrong (does not match name in vector table), the vector table is not correctly situated.RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
void UART4_IRQHandler(void)
{
// Ignore framing/parity/error issues for now
if(USART_GetITStatus(UART4, USART_IT_RXNE) == SET)
{
uint16_t temp = UART4->DR; // Or USART_ReceiveData(UART4);
USART_SendChar(UART4, temp); // Ignore if it's busy
}
}
2012-05-22 07:42 AM
Yes the clocks are enabled. Transmittng bytes is no problem for me. Now I changed the handler but no change.
2012-05-22 11:03 AM
The interrupt handler is called if I send chars over the UART interface. But if I send from the computer the STM32 do nothing.
The signal at the UART4_RX pin is correct. Any idea?2012-05-22 12:32 PM
Any idea?
I don't know how you have this wired up. ie suitably converting CMOS to RS232 levels. You could try looping back the UART4 Tx and Rx pins. You could do this a the pins, and/or beyond the level conversion, if present. You could check the status register and see if it is flagging any errors (framing, parity, overrun, etc). Reception will be a problem if they are set. Does the receive work if you just use polling techniques instead of interrupts?2012-05-22 01:04 PM
I might start be demonstrating it with something like this:
// STM32 UART4 (Tx PA.0, Rx PA.1) STM32F4 Discovery - sourcer32@gmail.com
#include ''stm32f4_discovery.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
/* --------------------------- System Clocks Configuration -----------------*/
/* UART4 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
/* GPIOA clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*-------------------------- GPIO Configuration ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Connect USART pins to AF */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_UART4);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_UART4);
}
/**************************************************************************************/
void UART4_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
/* USARTx configuration ------------------------------------------------------*/
/* USARTx configured as follow:
- BaudRate = 19200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = 19200;
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_Init(UART4, &USART_InitStructure);
USART_Cmd(UART4, ENABLE);
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
UART4_Configuration();
while(1)
{
uint16_t Data;
while(USART_GetFlagStatus(UART4, USART_FLAG_RXNE) == RESET) // Wait for Char
{
if (USART_GetFlagStatus(UART4, (USART_FLAG_ORE | USART_FLAG_NE | USART_FLAG_FE | USART_FLAG_PE)))
USART_ReceiveData(UART4); // Clear Error
}
Data = USART_ReceiveData(UART4); // Collect Char
while(USART_GetFlagStatus(UART4, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(UART4, Data); // Echo Char
}
while(1); // Don't want to exit
}
/**************************************************************************************/
Standalone/Complete example
2014-02-05 10:19 PM
Have anyone testing the example success?
Testing the polling mode on STM32F429I-Discovery board still failed.TX is fine, but RX can not work (from Host send data to STM32F429)