Question
STM32F4 why I cant comunicate with USART1 ?
Posted on February 23, 2013 at 15:36
hello forum ,
please have a look at my STM32F4 code - the USART1 interrupt never gets called when a character arrives at the serial port I am sure about my PC side program becouse it is tested with F103 beforevoid
init_serp_1(
void
) {
RCC_APB1PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
USART_InitStructure.USART_BaudRate = 9600;
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(USART1, &USART_InitStructure);
Tx_len = 0;
Tx_ptr = 0;
Rx_ptr = 0;
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
//*******************************************************
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource7,GPIO_AF_USART1);
//*******************************************************
USART_Cmd(USART1,ENABLE);
USART1->SR &= ~USART_FLAG_TC;
// clear interrupt
}
//**************************
void
USART1_IRQHandler(
void
)
{
volatile unsigned
int
IIR;
IIR = USART1->SR;
if
(IIR & USART_FLAG_RXNE) {
// read interrupt
USART1->SR &= ~USART_FLAG_RXNE;
Rx_buffer[Rx_ptr] = USART_ReceiveData(USART1);
Rx_ptr++ ;
if
(Rx_ptr > 511) {Rx_ptr=0;com0err=1;}
if
(ending_char()) do_instruction();
}
if
(IIR & USART_FLAG_TC) {
USART1->SR &= ~USART_FLAG_TC;
// clear interrupt
if
(Tx_ptr < Tx_len) {
Tx_ptr++; send_char (Tx_buffer[Tx_ptr]);}
else
USART_ITConfig(USART1, USART_IT_TC, DISABLE);
}
}
//**********************************
void
NVIC_Configuration(
void
)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* Enable the USARTx Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
#stm32f4-usart