Search code examples
interruptstm32uartusartvisualgdb

Unsuccessfull USART activation on STM32F303K8


Am trying to activate and make use of the USART1 peripheral on the STM32F303K8. Am running it with StdPeriph in VisualGDB. However, i can't get to start the USART using interrupt mode. Even when i probe the pins, they are all silent. What am i missing. Spent quite a number of days on this.

#include <stm32f30x_gpio.h>
#include <stm32f30x_rcc.h>
#include "stm32f30x_usart.h"
#include "stm32f30x_misc.h"

void SysTick_Handler()
{
    msTicks++;
}

void setSysTick()
{
    if (SysTick_Config(SystemCoreClock / 1000))
    {
        while (1) {}
    }
}

void Delay()
{
    int i;
    for (i = 0; i < 1000000; i++)
        asm("nop");
}

void init_peripherals(void)
{
    GPIO_InitTypeDef    GPIO_InitStructure;
    USART_InitTypeDef   USART_InitStructure; 
    NVIC_InitTypeDef    NVIC_InitStructure;

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_9 | GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_1;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9 | GPIO_PinSource10, GPIO_AF_7);

    USART_InitStructure.USART_BaudRate = 115200;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
    USART_Init(USART1, &USART_InitStructure);

    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

    USART_Cmd(USART1, ENABLE);

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_Init(&NVIC_InitStructure);
    USART_Cmd(USART1, ENABLE);
}

void USART_puts(USART_TypeDef* usar_tx, volatile char* str)
{
    while (*str)
    {
        while (USART_GetFlagStatus(usar_tx, USART_FLAG_TC) == RESET) {}
        USART_SendData(usar_tx, *str);
        str++;
    }
}

void  USART1_IRQHandler(void) 
{
    if (USART1->ISR & USART_ISR_RXNE)
    {
        const char ch = USART_ReceiveData(USART1);

        if (ch != '\n' && ch != '\r')
        {
            rcvd_str[cnt++] = ch;
        }
        else
        {
            rcvd_str[cnt] = '\r';
            cnt = 0;
            str_buildup = true;
        }
    }
}

int main()
{

    setSysTick();
    init_peripherals();
    char* strr = "Hello World\n";
    USART_puts(USART1, strr);
    return 0;
}

The expected result is a print of helloWorld in the serial terminal like realterm. I cant get to get there yet.


Solution

  • Once interrupt arrived it will start executing its handler accordingly in the above code handler is checking termination character and storing the character in a string but before exiting from interrupt handler need to clear the interrupt. After NVIC initialization USART_cmd is added again and missed NVIC_init check these.