Skip to content

Instantly share code, notes, and snippets.

@P4

P4/main-template.c

Last active Aug 29, 2015
Embed
What would you like to do?
FreeRTOS
#include <stdio.h>
/* Scheduler includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
/* Library includes. */
#include "stm32f10x.h"
#include "stm32f10x_it.h"
#include "stm32f10x_conf.h"
#include "dbgu.h"
#include "term_io.h"
static void init(void)
{
//enable clocks for selected peripherals
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD |
RCC_APB2Periph_GPIOE | RCC_APB2Periph_USART1 | RCC_APB2Periph_AFIO, ENABLE);
/* Set the Vector Table base address at 0x08000000 */
NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );
NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );
/* Configure HCLK clock as SysTick clock source. */
SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );
ledsInit();
//custom drivers init
debug_init_default();
}
int main( void )
{
init();
//your code here
vTaskStartScheduler();
xprintf("unexpected scheduler exit!\n");
while(1);
}
void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed portCHAR *pcTaskName )
{
xprintf("vApplicationStackOverflowHook called!\n");
( void ) pxTask;
( void ) pcTaskName;
while(1);
}
//..., init(...){...}
xTaskHandle taskBlueHandle;
void taskGreen(void *parameters){
for(;;) {
ledGreenOn();
vTaskDelay(45);
ledGreenOff();
vTaskDelay(30);
}
}
void taskBlue(void *parameters){
portTickType last_time = xTaskGetTickCount();
for(;;) {
vTaskDelayUntil(&last_time, 100);
ledBlueOn();
vTaskDelay(20);
ledBlueOff();
}
}
int main( void )
{
init();
//your code here
xTaskCreate(taskGreen, NULL, configMINIMAL_STACK_SIZE, NULL, 1, NULL);
xTaskCreate(taskBlue, NULL, configMINIMAL_STACK_SIZE, NULL, 1, &taskBlueHandle);
vTaskStartScheduler();
xprintf("unexpected scheduler exit!\n");
while(1);
}
//...
//..., init(...){...}
xTaskHandle taskBlueHandle;
xQueueHandle rxQueue;
xQueueHandle txQueue;
void taskGreen(void *parameters){
char buf;
for(;;) {
if( xQueueReceive(rxQueue, &buf, 20) != pdTRUE ) { continue; }
ledGreenToggle();
xQueueSend(txQueue, &buf, 0);
//vTaskDelay(100);
}
}
//void taskBlue(void *parameters){ ... }
int main( void )
{
init();
//your code here
usartInit(&rxQueue,&txQueue);
xTaskCreate(taskGreen, NULL, configMINIMAL_STACK_SIZE, NULL, 1, NULL);
xTaskCreate(taskBlue, NULL, configMINIMAL_STACK_SIZE, NULL, 1, &taskBlueHandle);
vTaskStartScheduler();
xprintf("unexpected scheduler exit!\n");
while(1);
}
//...
void taskGreen(void *parameters){
char buf;
for(;;) {
if( xQueueReceive(rxQueue, &buf, 20) != pdTRUE ) { continue; }
ledGreenToggle();
if (buf == 's') vTaskSuspend(taskBlueHandle);
else if (buf == 'r') vTaskResume(taskBlueHandle);
xQueueSend(txQueue, &buf, 0);
vTaskDelay(100);
}
}
//..., init(...){...}
xSemaphoreHandle sem;
void taskGreen(void *parameters){
for(;;) {
vTaskDelay(2);
xSemaphoreTake(sem, portMAX_DELAY); // no timeout
ledGreenOn();
xprintf("G"
"gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg"
"gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg"
"gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg"
"gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg"
"reen");
ledGreenOff();
xSemaphoreGive(sem);
}
}
void taskBlue(void *parameters){
for(;;) {
vTaskDelay(2);
xSemaphoreTake(sem, portMAX_DELAY); // no timeout
ledGreenOn();
xprintf("B"
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
"lue");
ledGreenOff();
xSemaphoreGive(sem);
}
}
int main( void )
{
init();
//your code here
vSemaphoreCreateBinary(sem);
if (sem == NULL) { xprintf("semaphore was not created"); while(1); }
xTaskCreate(taskGreen, NULL, configMINIMAL_STACK_SIZE, NULL, 1, NULL);
xTaskCreate(taskBlue, NULL, configMINIMAL_STACK_SIZE, NULL, 2, NULL);
vTaskStartScheduler();
xprintf("unexpected scheduler exit!\n");
while(1);
}
//...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment