AnsweredAssumed Answered

CMSIS RTOS Problem

Question asked by perera.duminda on Jul 2, 2013
I want to count the number of milliseconds elapsed since the time the program was started in one thread, and use this number for some calculations in another thread.

Code:

#include <stdio.h>
#include <stm32f0xx.h>
#include <stm32f0_discovery.h>
#include "cmsis_os.h"

uint32_t   milliSeconds=0;

osThreadId Thread_Millis;
osThreadId Thread_Funct;

void Funct(void const *args);
void Millis(void const *args);

osThreadDef(Funct, osPriorityNormal, 1, 0);
osThreadDef(Millis, osPriorityNormal, 1, 0);

void GPIO_Config(void);
void USART_Config(void);

void USART_Config() {
    USART_InitTypeDef USART_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_Cmd(USART1, ENABLE);
}

int fputc(int ch, FILE *stream) {
  USART_SendData(USART1, (uint16_t)ch);
  while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) != SET);
  return ch;
}

void Board_Init() {
  // Configure SysTick for 1-msec interval
  if (SysTick_Config(SystemCoreClock / 1000)){
        while (1);
    }
    
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
    
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
    
    GPIO_Config();
    USART_Config();
}

void GPIO_Config(){
    GPIO_InitTypeDef GPIO_InitStructure;
 
    //USART1 communication TX
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    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_AF_1);
    
    //USART1 communication RX
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
}

void Millis(void const *args){
    osSignalWait(0x0002, osWaitForever);
    while(1){
        osDelay(1);
        milliSeconds++;
    }
}

void Funct(void const *args){
    osSignalWait(0x0004, osWaitForever);
    
    while(1){
          printf("Output %d ->\n", milliSeconds);
          osDelay(500);
    }
}

int main(){
    SystemInit();
    Board_Init();

    Thread_Millis= osThreadCreate(osThread(Millis), NULL);
    Thread_Funct= osThreadCreate(osThread(Funct), NULL);

    osSignalSet(Thread_Millis, 0x0002);
    osSignalSet(Thread_Funct, 0x0004);

    osThreadYield();    
}

What happens here is in the UART terminal in my PC I receive;
Output   0   ->                 (Showing that the loop inside the Funct thread has looped once)
This is as expected, however it stops here. Pressing the reset button on the board makes it transmit the same again and stops again. The UART communication does not happen continuously, which means that the thread gets stuck. Why is that?

What am I doing wrong or is there a better way to get the millisecond count? I only recently started programming in my STM32F0, so any help will be greatly appreciated. Thanks.

Outcomes