Menu ▾ ▴

#194 FreeRTOS ST32F303

v1.0 (example)
closed-rejected
nobody
None
5
2019-06-05
2019-06-05
No

Hi, i really don't undesrtand why this code dosn't work. I'm using STM32F303RE nucleo board.

Thanks in advance

Fausto

#include <string.h>
#include <stdint.h>
#include "stm32f30x.h"

#include "FreeRTOS.h"
#include "task.h"

#define TRUE 1
#define FALSE 0

#define NOT_PRESSED FALSE
#define PRESSED TRUE

//function prototypes
static void prvSetupHardware(void);
void printmsg(char *msg);
static void prvSetupUart(void);
void prvSetupGpio(void);

//tasks prototypes
void led_task_handler(void *params);
void button_task_handler(void *params);

//global space for some variable
uint8_t button_status_flag = NOT_PRESSED;

int main(void)
{
    //1.  Reset the RCC clock configuration to the default reset state.
    //HSI ON, PLL OFF, HSE OFF, system clock = 16MHz, cpu_clock = 16MHz
    RCC_DeInit();

    //2. update the SystemCoreClock variable
    SystemCoreClockUpdate();

    prvSetupHardware();

    //Start Recording
    SEGGER_SYSVIEW_Conf();
    SEGGER_SYSVIEW_Start();

    //lets create led_task
    xTaskCreate(led_task_handler,"LED-TASK",configMINIMAL_STACK_SIZE,NULL,1,NULL);

    //lets create button_task
    xTaskCreate(button_task_handler,"BUTTON-TASK",configMINIMAL_STACK_SIZE,NULL,1,NULL);

    //lets start the scheduler
    vTaskStartScheduler();

    for(;;);
}


void led_task_handler(void *params)
{
    while(1)
    {
        if(button_status_flag == PRESSED)
        {
            //turn on the led
            GPIO_WriteBit(GPIOA,GPIO_Pin_5,Bit_SET);
        }else
        {
            //turn off the led
            GPIO_WriteBit(GPIOA,GPIO_Pin_5,Bit_RESET);
        }

    }
}


void button_task_handler(void *params)
{
    while(1)
    {
        if( GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_13) )
        {
            //button is not pressed on the nucleo board
            button_status_flag = NOT_PRESSED;
        }else
        {
            //button is pressed on the nucleo board
            button_status_flag = PRESSED;
        }

    }
}


static void prvSetupHardware(void)
{
    //Setup Button and LED
    prvSetupGpio();

    //setup UART2
    prvSetupUart();
}

void printmsg(char *msg)
{
    for(uint32_t i=0; i < strlen(msg); i++)
    {
        while ( USART_GetFlagStatus(USART2,USART_FLAG_TXE) != SET);
        USART_SendData(USART2,msg[i]);
    }

    while ( USART_GetFlagStatus(USART2,USART_FLAG_TC) != SET);

}


static void prvSetupUart(void)
{
    GPIO_InitTypeDef gpio_uart_pins;
    USART_InitTypeDef uart2_init;

    //1. Enable the UART2  and GPIOA Peripheral clock
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);

    //PA2 is UART2_TX, PA3 is UART2_RX

    //2. Alternate function configuration of MCU pins to behave as UART2 TX and RX

    //zeroing each and every member element of the structure
    memset(&gpio_uart_pins,0,sizeof(gpio_uart_pins));

    gpio_uart_pins.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
    gpio_uart_pins.GPIO_Mode = GPIO_Mode_AF;
    gpio_uart_pins.GPIO_PuPd = GPIO_PuPd_UP;
    gpio_uart_pins.GPIO_OType= GPIO_OType_PP;
    gpio_uart_pins.GPIO_Speed = GPIO_Speed_Level_3;
    GPIO_Init(GPIOA, &gpio_uart_pins);


    //3. AF mode settings for the pins
    GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_7); //PA2
    GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_7); //PA3

    //4. UART parameter initializations
    //zeroing each and every member element of the structure
    memset(&uart2_init,0,sizeof(uart2_init));

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


    //5. Enable the UART2 peripheral
    USART_Cmd(USART2,ENABLE);

}


void prvSetupGpio(void)
{
// this function is board specific

    //Peripheral clock enable for GPIOA and GPIOC
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC,ENABLE);

    GPIO_InitTypeDef led_init, button_init;
    led_init.GPIO_Mode = GPIO_Mode_OUT;
    led_init.GPIO_OType = GPIO_OType_PP;
    led_init.GPIO_Pin = GPIO_Pin_5;
    led_init.GPIO_Speed = GPIO_Speed_Level_3;
    led_init.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOA,&led_init);

    button_init.GPIO_Mode = GPIO_Mode_IN;
    button_init.GPIO_OType = GPIO_OType_PP;
    button_init.GPIO_Pin = GPIO_Pin_13;
    button_init.GPIO_Speed = GPIO_Speed_Level_3;
    button_init.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOC,&button_init);

}

Discussion

  • Richard Barry

    Richard Barry - 2019-06-05
    • status: open --> closed-rejected
     
  • Richard Barry

    Richard Barry - 2019-06-05

    If reporting a bug please give details of what the bug is and how to reproduce it. "Doesn't work" gives us no information to enable us to reply. If reposting please do so to the support forum, not the bug tracker. Thanks.

     
  • Fausto Tromba

    Fausto Tromba - 2019-06-05

    Hi,
    there are 2 tasks, the button and led task. button taks works properly, but the led task seems like if it doen't receive any information. Also looking the debug, i could see only the button task works and never the led task.

    Thanks in advance for any helps
    Fausto

     

Log in to post a comment.