cancel
Showing results for 
Search instead for 
Did you mean: 

F103 USART1 RX IRQ happens premature.

zhausq
Associate III
Posted on December 26, 2015 at 01:55

I am using the USART1 interface of an F103 and I'm havin trouble getting interrupts correctly. When receiving a byte the first interrupts get triggered while bit 6 is still underway and then a second interrupt happens for some unknown reason.

row1 is debug pin that is being toggled ever irq

row2 is rx pin

row3 is tx pin

http://imgur.com/IgfKKvJ

void USART1_Init(void)

{

    /* USART configuration structure for USART1 */

    USART_InitTypeDef usart1_init_struct;

    /* Bit configuration structure for GPIOA PIN9 and PIN10 */

    GPIO_InitTypeDef gpioa_init_struct;

    /* Enalbe clock for USART1, AFIO and GPIOA */

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_AFIO |

                           RCC_APB2Periph_GPIOA, ENABLE);

    /* GPIOA PIN9 alternative function Tx */

    gpioa_init_struct.GPIO_Pin = GPIO_Pin_9;

    gpioa_init_struct.GPIO_Speed = GPIO_Speed_50MHz;

    gpioa_init_struct.GPIO_Mode = GPIO_Mode_AF_PP;

    GPIO_Init(GPIOA, &gpioa_init_struct);

    /* GPIOA PIN9 alternative function Rx */

    gpioa_init_struct.GPIO_Pin = GPIO_Pin_10;

    gpioa_init_struct.GPIO_Speed = GPIO_Speed_50MHz;

    gpioa_init_struct.GPIO_Mode = GPIO_Mode_IN_FLOATING;

    GPIO_Init(GPIOA, &gpioa_init_struct);

    /* Baud rate 9600, 8-bit data, One stop bit

     * No parity, Do both Rx and Tx, No HW flow control

     */

    usart1_init_struct.USART_BaudRate = 9600;

    usart1_init_struct.USART_WordLength = USART_WordLength_8b;

    usart1_init_struct.USART_StopBits = USART_StopBits_1;

    usart1_init_struct.USART_Parity = USART_Parity_No ;

    usart1_init_struct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

    usart1_init_struct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

    USART_Init(USART1, &usart1_init_struct);

    /* Enable RXNE interrupt */

    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

    NVIC_InitTypeDef NVIC_InitStructure;

    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;

    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStructure);

    /* Enable USART1 global interrupt */

    NVIC_EnableIRQ(USART1_IRQn);

    /* Enable USART1 */

    USART_Cmd(USART1, ENABLE);

}

void USART1_IRQHandler(void)

{

static int tx_index = 0;

static int rx_index = 0;

static char rx_byte;

    if(USART_GetITStatus(USART1, USART_IT_RXNE))

    {

     rx_byte=USART1->DR;

     if(rx_byte != 'x')

     {

     USART_SendData(USART1, rx_byte);

     }

     /*telemetry_rx_buffer[rx_index++] = USART_ReceiveData(USART1);

     if (rx_index >= (sizeof(telemetry_rx_buffer) - 1))

     rx_index = 0;*/

     debugpin_cycle();

    }

}

What am i doing wrong here?

#stm32-f1-usart-uart-problem
3 REPLIES 3
Posted on December 26, 2015 at 02:14

What am i doing wrong here?

 

debugpin_cycle(); // infers something different than toggling, code not shown

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
zhausq
Associate III
Posted on December 26, 2015 at 10:53

Here's what debugpin is about:

void debugpin_init(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

//configure pin as output

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

GPIO_Init(GPIOB, &GPIO_InitStructure);

}

void debugpin_set(int value)

{

GPIO_WriteBit(GPIOB, GPIO_Pin_1, !value);

}

void debugpin_cycle(void)

{

GPIO_WriteBit(GPIOB, GPIO_Pin_1, 1);

GPIO_WriteBit(GPIOB, GPIO_Pin_1, 0);

}

zhausq
Associate III
Posted on December 27, 2015 at 12:56

Fuck me, i was uploading release when working on debug. That's why nothing seemed to fix it. It now works great.