AnsweredAssumed Answered

RTOS programming with STM32100RB Discovery

Question asked by tom.abel on May 17, 2013
Latest reply on May 21, 2013 by fm
Hi i am writin a code that does multitasking, this code runs two tasks in parallel, does anyone know how i can make one task wait, while the other is running?

here is my code!

/**
  *****************************************************************************
  * @title   main.c
  * @author  slachist@gmail.com
  * @date    31 Oct 2012
  * @brief
  *******************************************************************************
  */
////// The above comment is automatically generated by CoIDE ///////////////////

#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_usart.h"
#include <CoOs.h>
#include <OsConfig.h>
#include <misc.h>


#define STACK_SIZE_DEFAULT 512
#define TRUE 1
#define FALSE 0
OS_STK task1_stk[STACK_SIZE_DEFAULT];
OS_STK task2_stk[STACK_SIZE_DEFAULT];

OS_TID Task1Id;
OS_TID Task2Id;

OS_MutexID task1_mutex,task2_mutex;
OS_FlagID flag1,flag2;


int flag;




void init_USART1(uint32_t baudrate){

/* This is a concept that has to do with the libraries provided by ST
* to make development easier the have made up something similar to
* classes, called TypeDefs, which actually just define the common
* parameters that every peripheral needs to work correctly
*
* They make our life easier because we don't have to mess around with
* the low level stuff of setting bits in the correct registers
*/
GPIO_InitTypeDef GPIO_InitStructure; // this is for the GPIO pins used as TX and RX
USART_InitTypeDef USART_InitStruct; // this is for the USART1 initilization
NVIC_InitTypeDef NVIC_InitStructure; // this is used to configure the NVIC (nested vector interrupt controller)
USART_ClockInitTypeDef USART_ClockInitStructure;
/* enable APB2 peripheral clock for USART1
* note that only USART1 and USART6 are connected to APB2
* the other USARTs are connected to APB1
*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);

/* enable the peripheral clock for the pins used by
* USART1, PB6 for TX and PB7 for RX
*/



/* Now the USART_InitStruct is used to define the
* properties of USART1
*/
USART_InitStruct.USART_BaudRate = baudrate;    // the baudrate is set to the value we passed into this init function
USART_InitStruct.USART_WordLength = USART_WordLength_8b;// we want the data frame size to be 8 bits (standard)
USART_InitStruct.USART_StopBits = USART_StopBits_1;    // we want 1 stop bit (standard)
USART_InitStruct.USART_Parity = USART_Parity_No;    // we don't want a parity bit (standard)
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // we don't want flow control (standard)
USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; // we want to enable the transmitter and the receiver
USART_Init(USART1, &USART_InitStruct);    // again all the properties are passed to the USART_Init function which takes care of all the bit setting


/* This sequence sets up the TX and RX pins
* so they work correctly with the USART1 peripheral
*/
/*Set USART1 Tx (PA.09) as AF push-pull */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);

//Set USART1 Rx (PA.10) as input floating
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_ClockStructInit(&USART_ClockInitStructure);
USART_ClockInit(USART1, &USART_ClockInitStructure);
USART_InitStruct.USART_BaudRate = 9600;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_Parity = USART_Parity_No ;
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
/* Here the USART1 receive interrupt is enabled
* and the interrupt controller is configured
* to jump to the USART1_IRQHandler() function
* if the USART1 receive interrupt occurs
*/
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // enable the USART1 receive interrupt

NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;    // we want to configure the USART1 interrupts
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;// this sets the priority group of the USART1 interrupts
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;    // this sets the subpriority inside the group
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;    // the USART1 interrupts are globally enabled
NVIC_Init(&NVIC_InitStructure);    // the properties are passed to the NVIC_Init function which takes care of the low level stuff

// finally this enables the complete USART1 peripheral
USART_Cmd(USART1, ENABLE);
}


void USART_puts(USART_TypeDef* USARTx, volatile char *s){

while(*s){
// wait until data register is empty
while( !(USARTx->SR & 0x000040) );
USART_SendData(USARTx, *s);
*s++;
}
}
void initializeBoard(){

    GPIO_InitTypeDef GPIO_InitStructure_Led;
    GPIO_InitTypeDef GPIO_InitStructure_Button;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);//for LEds
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//for on-board button

    GPIO_InitStructure_Led.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
    GPIO_InitStructure_Led.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure_Led.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_InitStructure_Button.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStructure_Button.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure_Button.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_Init(GPIOC,&GPIO_InitStructure_Led);
    GPIO_Init(GPIOA,&GPIO_InitStructure_Button);

}

void task1 (void* pdata){
    
    while(1){

USART_puts(USART1,"Task1 running \r\n");
GPIO_WriteBit(GPIOC,GPIO_Pin_8,Bit_SET);
CoTickDelay(30);
    }
}

void task2 (void* pdata){


    int i;

while(1)

   {
      i = GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0);
      GPIO_WriteBit(GPIOC,GPIO_Pin_9,i);

        if(i!=0)
      {
            CoDelTask(Task1Id);
            GPIO_WriteBit(GPIOC,GPIO_Pin_8,Bit_RESET);
       //  CoSetFlag(flag2);
         USART_puts(USART1,"Task2 running \r\n");
        // flag=0;
          CoTickDelay(30);
        //CoClearFlag(flag1);
      }
       
   }

  }


int main(void)
{
    init_USART1(9600);
    initializeBoard();
    CoInitOS();
    Task1Id=CoCreateTask(task1,0,0,&task1_stk[STACK_SIZE_DEFAULT-1],STACK_SIZE_DEFAULT);
    Task2Id=CoCreateTask(task2,0,1,&task2_stk[STACK_SIZE_DEFAULT-1],STACK_SIZE_DEFAULT);
    CoStartOS();
    while(1);
}



Outcomes