cancel
Showing results for 
Search instead for 
Did you mean: 

NucleoF303K8 NVIC_Init() hang

Li JW
Associate II
Posted on January 27, 2017 at 13:40

Hi, everyone! I am working on a NucleoF303K8. I am using the standard periph library. I am trying to use USART interrupt in my project, but being held by a problem. When I NVIC_Init() the USART interrupts, it hangs. When I comment out the NVIC_Init() call, It works. I am new in STM32 programming, so I don'thave much to find the cause. I have replaced the startup files, some from the cube libandsome from the std library, not work.When I use the CubeMX to generate the hold project, it work with the USART interrupts. Since some requirements are not fit with the HAL library, so I use the std library. The following is my code:

Hoping someone can give me a little help, thanks a lot!

int main(void)
{
 board_init();
 uint8_t c = 1;
 while(1){
 GPIO_SetBits(GPIOB,GPIO_Pin_3);
 Delay_us(1000000);
 //Serial_Write(&Serial2, c);
 USART_SendData(USART2,c);
 GPIO_ResetBits(GPIOB,GPIO_Pin_3); 
 Delay_us(1000000);
 }
}
void board_init(){
 GPIO_InitTypeDef GPIO_InitStructure;
 NVIC_InitTypeDef NVIC_InitStructure;
 USART_InitTypeDef USART_InitStructure;
 
 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
 
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
 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);
 // UART Init
 //fifo_queue_reset(&usart_rx_q[0],usart_rx_buf[0],USART_RX_BUFFER_SIZE);
 //fifo_queue_reset(&usart_rx_q[1],usart_rx_buf[1],USART_RX_BUFFER_SIZE);
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
 GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_7);
 GPIO_PinAFConfig(GPIOA, GPIO_PinSource15, GPIO_AF_7);
 GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7);
 GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7);
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_15;
 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_UP;
 GPIO_Init(GPIOA, &GPIO_InitStructure);
 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_Init(USART1,&USART_InitStructure);
 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
 USART_ITConfig(USART1, USART_IT_TC, ENABLE);
 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
 NVIC_Init(&NVIC_InitStructure);
 USART_Cmd(USART1, ENABLE);
 //RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
 //USART_Init(USART2,&USART_InitStructure);
 //USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
 //USART_ITConfig(USART2, USART_IT_TC, ENABLE);
 //NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
 //NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
 //NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
 //NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
 //NVIC_Init(&NVIC_InitStructure);
 //USART_Cmd(USART2, ENABLE);
 
 SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
 SysTick_Config(SystemCoreClock/1000);
 //NVIC_SetPriority (SysTick_IRQn, 0);
 RCC_ClocksTypeDef clocks;
 RCC_GetClocksFreq(&clocks);
 usTicks = clocks.SYSCLK_Frequency / 1000000;
 //Serial_Init(SERIAL0, &Serial1);
 //Serial_Init(SERIAL1, &Serial2);
}
void USART1_IRQHandler(void){
 if(USART_GetITStatus(USART1, USART_IT_ORE) != RESET){
 USART_ClearFlag(USART1,USART_FLAG_ORE);
 USART_ClearITPendingBit(USART1,USART_IT_ORE);
 }
 if(USART_GetITStatus(USART1, USART_IT_NE) != RESET){
 USART_ClearFlag(USART1,USART_FLAG_NE);
 USART_ClearITPendingBit(USART1,USART_IT_NE);
 }
 if(USART_GetITStatus(USART1, USART_IT_FE) != RESET){
 USART_ClearFlag(USART1,USART_FLAG_FE);
 USART_ClearITPendingBit(USART1,USART_IT_FE);
 }
 if(USART_GetITStatus(USART1, USART_IT_PE) != RESET){
 USART_ClearFlag(USART1,USART_FLAG_PE);
 USART_ClearITPendingBit(USART1,USART_IT_PE);
 }
 if(USART_GetITStatus(USART1, USART_IT_WU) != RESET){
 USART_ClearFlag(USART1,USART_FLAG_WU);
 USART_ClearITPendingBit(USART1,USART_IT_WU);
 }
 
 if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET){
 GPIO_SetBits(GPIOB,GPIO_Pin_3);
 USART_ReceiveData(USART1);
 //fifo_queue_put(&usart_rx_q[0],USART_ReceiveData(USART1));
 }
 if(USART_GetITStatus(USART1, USART_IT_TC) != RESET){ 
 //uint8_t c;
 //if(fifo_queue_get(&usart_tx_q[0],&c)){
 // USART_SendData(USART1,c);
 //}
 //else {
 // fifo_queue_reset(&usart_tx_q[0],(void*)0,0);
 //}
 }
}
uint32_t Micros(void){
 register uint32_t ms, cycle_cnt;
 do {
 ms = sysTickUptime;
 cycle_cnt = SysTick->VAL;
 __ASM('\tnop\n');
 } while (ms != sysTickUptime);
 return (ms * 1000) + (usTicks * 1000 - cycle_cnt) / usTicks;
}
void Delay_us(uint32_t us){
 uint32_t now = Micros();
 while (Micros() - now < us);
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

1 ACCEPTED SOLUTION

Accepted Solutions
Posted on January 27, 2017 at 14:47

Hi Clive, Thanks again. You give me the hint, and I solve the problem now. It is the TC interrupt hold the procudure, I find it by lighting up the led when it happens. Finally, I disable the TC interrupt when there is no more data to send. Thanks a lot.

View solution in original post

3 REPLIES 3
Posted on January 27, 2017 at 14:07

You'd need to clear the TC flag in the IRQHandler 

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on January 27, 2017 at 14:36

Hi Clive, Thanks for reply. I have updated the code as the following:

But it doesn't help......it still hangs when calling the NVIC_Init();

void USART1_IRQHandler(void){

 if(USART_GetITStatus(USART1, USART_IT_ORE) != RESET){

    USART_ClearFlag(USART1,USART_FLAG_ORE);

  USART_ClearITPendingBit(USART1,USART_IT_ORE);

 }

 if(USART_GetITStatus(USART1, USART_IT_NE) != RESET){

    USART_ClearFlag(USART1,USART_FLAG_NE);

  USART_ClearITPendingBit(USART1,USART_IT_NE);

 }

 if(USART_GetITStatus(USART1, USART_IT_FE) != RESET){

    USART_ClearFlag(USART1,USART_FLAG_FE);

  USART_ClearITPendingBit(USART1,USART_IT_FE);

 }

 if(USART_GetITStatus(USART1, USART_IT_PE) != RESET){

    USART_ClearFlag(USART1,USART_FLAG_PE);

  USART_ClearITPendingBit(USART1,USART_IT_PE);

 }

 if(USART_GetITStatus(USART1, USART_IT_WU) != RESET){

    USART_ClearFlag(USART1,USART_FLAG_WU);

  USART_ClearITPendingBit(USART1,USART_IT_WU);

 }

 

  if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET){

  GPIO_SetBits(GPIOB,GPIO_Pin_3);

  USART_ReceiveData(USART1);

  //fifo_queue_put(&usart_rx_q[0],USART_ReceiveData(USART1));

  }

  if(USART_GetITStatus(USART1, USART_IT_TC) != RESET){  

      USART_ClearFlag(USART1,USART_FLAG_TC);

//  uint8_t c;

//  if(fifo_queue_get(&usart_tx_q[0],&c)){

//   USART_SendData(USART1,c);

//  }

//  else {

//   fifo_queue_reset(&usart_tx_q[0],(void*)0,0);

//  }

  }

}
Posted on January 27, 2017 at 14:47

Hi Clive, Thanks again. You give me the hint, and I solve the problem now. It is the TC interrupt hold the procudure, I find it by lighting up the led when it happens. Finally, I disable the TC interrupt when there is no more data to send. Thanks a lot.