Skip to content

Instantly share code, notes, and snippets.

@stevenlr
Created December 30, 2015 15:16
Show Gist options
  • Save stevenlr/23247444143a80f5d8ac to your computer and use it in GitHub Desktop.
Save stevenlr/23247444143a80f5d8ac to your computer and use it in GitHub Desktop.
/**
* @file utask.c
* @brief user tasks and kernel config source
* @author
* @date
*/
#include "kuart.h"
/* INCLUDES DEPENDENCIES ***************/
#include <utask.h>
#include "task.h"
#include "queue.h"
#include "adc.h"
#include "croutine.h"
#include "semphr.h"
xQueueHandle tempQueue;
xQueueHandle tempQueue1;
xQueueHandle unhandledQueue;
/**
* @fn void kernelConfig(void)
* @brief kernel configuration
*/
void kernelConfig(void){
tempQueue = xQueueCreate(100, sizeof(float));
tempQueue1 = xQueueCreate(1, sizeof(float));
xTaskCreate(taskCommands, "task commands", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, NULL);
xTaskCreate(taskSensor, "task sensor", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 3, NULL);
xTaskCreate(taskLed, "task led", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL);
}
void printCommandLine() {
uartPutS("freertos:~# ");
}
void commandHelp() {
uartPutS("read\t\treturns the last converted value\r\n");
uartPutS("dump\t\treturns the last one hundred values\r\n");
uartPutS("stream\t\treturns the last converted values\r\n");
uartPutS("\t\tpress ENTER to quit this mode\r\n");
uartPutS("reset\t\tsoftware processor reset\r\n");
uartPutS("baudrate\tupdate serial communication baud rate of processor\r\n");
uartPutS("help\t\tReturns supported commands\r\n");
}
void commandBaudrate() {
char buffer[64];
int baudrate;
uartPutS("Enter new baudrate: ");
uartGetS(buffer, 64);
baudrate = atoi(buffer);
if (baudrate == 0) {
uartPutS("Invalid value\r\n");
} else {
uartSetBps(baudrate);
}
}
void commandStream(void *params) {
portTickType tick;
float value;
char buffer[32];
while (1) {
if (xQueuePeek(tempQueue1, &value, portMAX_DELAY) == pdTRUE) {
sprintf(buffer, "\r%.1f�C", value);
uartPutS(buffer);
}
tick = xTaskGetTickCount();
vTaskDelayUntil(&tick, 20);
}
}
void taskCommands(void *params) {
char buffer[64];
float value;
xTaskHandle streamTaskHandle = NULL;
uartPutS("\r\n");
printCommandLine();
uartPutS("Hello world!\r\n");
while (1) {
printCommandLine();
uartGetS(buffer, 64);
if (strlen(buffer) == 0) {
continue;
}
if (strcmp(buffer, "help") == 0) {
commandHelp();
} else if (strcmp(buffer, "reset") == 0) {
uartPutS("\r\n\r\n");
SoftReset();
} else if (strcmp(buffer, "baudrate") == 0) {
commandBaudrate();
} else if (strcmp(buffer, "read") == 0) {
if (xQueuePeek(tempQueue1, &value, portMAX_DELAY) == pdTRUE) {
sprintf(buffer, "%.1f�C\r\n", value);
uartPutS(buffer);
}
} else if (strcmp(buffer, "stream") == 0) {
if (streamTaskHandle == NULL) {
xTaskCreate(commandStream, "task stream", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &streamTaskHandle);
} else {
vTaskResume(streamTaskHandle);
}
while (1) {
if (uartGetC() == '\r') {
vTaskSuspend(streamTaskHandle);
break;
}
}
uartPutS("\r\n");
} else if (strcmp(buffer, "dump") == 0) {
int i;
for (i = 0; i < 100; ++i) {
if (xQueueReceive(tempQueue, &value, portMAX_DELAY) == pdTRUE) {
sprintf(buffer, "%d\t%.1f�C\r\n", i + 1, value);
uartPutS(buffer);
} else {
break;
}
}
}
else {
uartPutS("Unknown command. Type help.\r\n");
}
}
}
void taskSensor(void *params) {
int data;
float value, tmp;
portTickType tick;
while (1) {
data = adcRead();
value = ((data * 3.3f / 1024.f) - 0.5f) * 100.f;
xQueueOverwrite(tempQueue1, &value);
if (xQueueSend(tempQueue, &value, 0) == errQUEUE_FULL) {
xQueueReceive(tempQueue, &tmp, 0);
xQueueSend(tempQueue, &value, 0);
}
tick = xTaskGetTickCount();
vTaskDelayUntil(&tick, 20);
}
}
void taskLed(void *params) {
portTickType tick;
float temp;
char state = 0;
while (1) {
if (xQueuePeek(tempQueue1, &temp, portMAX_DELAY) == pdTRUE) {
if (temp < 38.5f) {
state ^= 0x80;
} else {
state = 0x80;
}
}
PORTA = state;
tick = xTaskGetTickCount();
vTaskDelayUntil(&tick, 500);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment