API  2.3.0
TSmarT Software Library
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
freertos_basic.c

This is an example about how to manage the operating system FreeRTOS.

/* @file freertos_basic.c
* @author TST
* @version 0.1
* @date 13/06/2012
* @brief This is an example of how to manage the operating system FreeRTOS.
*
* FreeRTOS is a real-time operating system for embedded devices,
* being ported to 31 micro-controllers. This example shows the user how to create
* tasks (setting stack size and priorities) and queues mainly. In addition, it
* shows to the users how to start with TSmarT programming:
*
* - Filling the "Init()" function
* - Using the most easy device: DIO (digital input/output).
*
* User can learn more about FreeRTOS in http://www.freertos.org/
*
* API reference for FreeRTOS:
* - http://www.freertos.org/a00106.html
*
*/
/* Platform headers */
#include "tsmart.h"
/* Global variables to share between tasks: queue handlers */
xQueueHandle led1_queue;
xQueueHandle led2_queue;
/* @brief vLED1 task
*
* This task toggle the LED1 and send a value(1) to report to
* other task that can toggle its LED(LED2).
* After that it wait for a value in a queue.
*
* @param pvParameters
*/
void vLED1(void *pvParameters) {
/* Variables */
uint8_t value_received;
uint8_t value_sent = 1;
while(1){
/* Toggle LED1 */
/* Send value to task2(vLED2). User can learn more about send
* in http://www.freertos.org/a00117.html */
xQueueSend(led2_queue, &value_sent, portMAX_DELAY);
/* Wait for new value in queue from task2(vLED2). User can
* learn more receive about in http://www.freertos.org/a00118.html */
xQueueReceive(led1_queue, &value_received, portMAX_DELAY);
/* Wait 1 second. User can learn more about delay
* in http://www.freertos.org/a00127.html */
vTaskDelay(1000/portTICK_RATE_MS);
}
}
/* @brief vLED2 task
*
* This task is waiting for new value from the task1(vLED1)
* and when the value is received it toggles the LED2 and
* send a value to task1 (vLED2).
*
* @param pvParameters
*/
void vLED2(void *pvParameters) {
/* Variables */
uint8_t value_received;
uint8_t value_sent = 1;
while(1){
/* Wait for new value in queue from task1(vLED1) User can
* learn more about receive in http://www.freertos.org/a00118.html */
xQueueReceive(led2_queue, &value_received, portMAX_DELAY);
/* Toggle LED2 */
/* Send value to task1(vLED1). User can learn more about send
* in http://www.freertos.org/a00117.html */
xQueueSend(led1_queue, &value_sent, portMAX_DELAY);
/* Wait 1 second. User can learn more about delay
* in http://www.freertos.org/a00127.html */
vTaskDelay(1000/portTICK_RATE_MS);
}
}
/* @brief "Init()" function.
*
* This is the first thing that the user must do for using TSmarT.
* It initializes specific hardware resources (GPRS, GPS, AI, DIO, MODBUS, MSA...)
* and software resources (queues, mutex, tasks...) for the user application.
*
* The way to fill in this function properly is to initialize first
* hardware resources and after that software resources.
*
* This function must return: TSMART_PASS when every thing is OK or
* TSMART_FAIL when a failure happened.
*
* @return
* @arg TSMART_PASS
* @arg TSMART_FAIL
*/
int32_t init() {
/* Variables */
tsmart_dio_config_t dio_config;
portBASE_TYPE xReturn;
/* ************************************************************************* */
/* Debug Mode */
/* ************************************************************************* */
/* ************************************************************************* */
/* Initialize resources */
/* ************************************************************************* */
/* The specific hardware for this application are two digital input and output
* located in:
* - DIOa0
* - DIOa1
*/
/* LED1 - DIOa0 */
dio_config.mode = TSMART_DIO_OD;
TSMART_DIO_Init(&tsmart_dio0a, &dio_config);
/* LED2 - DIOa1 */
dio_config.mode = TSMART_DIO_OD;
TSMART_DIO_Init(&tsmart_dio1a, &dio_config);
/* ************************************************************************* */
/* Application task */
/* ************************************************************************* */
/* This examples will need two task:
*
* - LED1 task: Toggle a led and send a value using a queue to a LED2 task
* to toggle other led.
*
*
*
* - LED2 task: wait for value in a queue and when a value is received, toggle
* a led and send a value to a LED1 task.
*
* And two queue to send values one task to another and vice versa:
* - led1_queue
* - led2_queue
*
*/
/* User can learn more about create queues in http://www.freertos.org/a00116.html */
/* Create LED1 queue
* The number of elements that can support is 1, this element must be uint8_t.
*/
led1_queue = xQueueCreate(1, sizeof(uint8_t));
if (led1_queue == NULL) {
return TSMART_FAIL;
}
/* Create LED2 queue
* The number of elements that can support is 1, this element must be uint8_t.
*/
led2_queue = xQueueCreate(1, sizeof(uint8_t));
if (led2_queue == NULL) {
return TSMART_FAIL;
}
/* User can learn more about create tasks in http://www.freertos.org/a00125.html */
/* Create LED1 Task */
xReturn = xTaskCreate(vLED1, "LED1", 128, NULL, 7, NULL);
if (xReturn != pdPASS) {
return TSMART_FAIL;
}
/* Create LED2 Task */
xReturn = xTaskCreate(vLED2, "LED2", 128, NULL, 8, NULL);
if (xReturn != pdPASS) {
return TSMART_FAIL;
}
/* Everything OK */
return TSMART_PASS;
}