Commit 86d57a65 authored by Rahix's avatar Rahix
Browse files

Merge branch 'rahix/epic-freertos' into 'master'

Use FreeRTOS in epicardium

See merge request card10/firmware!11
parents 875d9f8f 3e0a8fbb
Pipeline #908 passed with stage
in 1 minute and 57 seconds
#ifndef FREERTOS_CONFIG_H
#define FREERTOS_CONFIG_H
#include "max32665.h"
/* CMSIS keeps a global updated with current system clock in Hz */
#define configCPU_CLOCK_HZ ((unsigned long)120000000)
/* TODO: Adjust this for tickless idle */
#define configTICK_RATE_HZ ((portTickType)1000)
/* Memory */
#define configTOTAL_HEAP_SIZE ((size_t)(26 * 1024))
#define configMINIMAL_STACK_SIZE ((unsigned short)128)
#define configMAX_PRIORITIES 5
/* # of priority bits (configured in hardware) is provided by CMSIS */
#define configPRIO_BITS __NVIC_PRIO_BITS
/* Priority 7, or 255 as only the top three bits are implemented. This is the lowest priority. */
#define configKERNEL_INTERRUPT_PRIORITY ( ( unsigned char ) 7 << ( 8 - configPRIO_BITS) )
/* Priority 5, or 160 as only the top three bits are implemented. */
#define configMAX_SYSCALL_INTERRUPT_PRIORITY ( ( unsigned char ) 5 << ( 8 - configPRIO_BITS) )
/* We want to use preemption to easier integrate components */
#define configUSE_PREEMPTION 1
/* TODO: Adjust */
#define configUSE_IDLE_HOOK 0
#define configUSE_TICK_HOOK 0
#define configUSE_CO_ROUTINES 0
#define configUSE_16_BIT_TICKS 0
#define configUSE_MUTEXES 1
#define INCLUDE_vTaskSuspend 1
#define INCLUDE_vTaskDelay 1
/* Allow static allocation of data structures */
#define configSUPPORT_STATIC_ALLOCATION 1
/* Alias the default handler names to match CMSIS weak symbols */
#define vPortSVCHandler SVC_Handler
#define xPortPendSVHandler PendSV_Handler
#define xPortSysTickHandler SysTick_Handler
#endif /* FREERTOS_CONFIG_H */
......@@ -344,7 +344,10 @@ void USB_IRQHandler(void)
}
/******************************************************************************/
/* TODO: We probably need to fix something related to this */
#if 0
void SysTick_Handler(void)
{
mxc_delay_handler();
}
#endif /* 0 */
#include <stdio.h>
#include "card10.h"
#include <stdlib.h>
#include "uart.h"
#include "cdcacm.h"
#include "card10.h"
#include "leds.h"
#include "api/dispatcher.h"
#include "serial.h"
extern mxc_uart_regs_t * ConsoleUart;
#include "FreeRTOS.h"
#include "task.h"
void epic_uart_write_str(char*str, intptr_t length)
void epic_leds_set(int led, uint8_t r, uint8_t g, uint8_t b)
{
UART_Write(ConsoleUart, (uint8_t*)str, length);
cdcacm_write((uint8_t*)str, length);
leds_set(led, r, g, b);
leds_update();
}
char epic_uart_read_chr(void)
void vApiDispatcher(void*pvParameters)
{
while(1) {
if(UART_NumReadAvail(ConsoleUart) > 0) {
return UART_ReadByte(ConsoleUart);
}
if(cdcacm_num_read_avail() > 0) {
return cdcacm_read();
}
while (1) {
api_dispatcher_poll();
vTaskDelay(portTICK_PERIOD_MS * 10);
}
}
void epic_leds_set(int led, uint8_t r, uint8_t g, uint8_t b)
void vApplicationGetIdleTaskMemory(
StaticTask_t**ppxIdleTaskTCBBuffer,
StackType_t**ppxIdleTaskStackBuffer,
uint32_t *pulIdleTaskStackSize)
{
leds_set(led, r, g, b);
leds_update();
/*
* If the buffers to be provided to the Idle task are declared inside this
* function then they must be declared static - otherwise they will be allocated on
* the stack and so not exists after this function exits.
*/
static StaticTask_t xIdleTaskTCB;
static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
/*
* Pass out a pointer to the StaticTask_t structure in which the Idle task's
* ktate will be stored.
*/
*ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
/* Pass out the array that will be used as the Idle task's stack. */
*ppxIdleTaskStackBuffer = uxIdleTaskStack;
/*
* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.
* Note that, as the array is necessarily of type StackType_t,
* configMINIMAL_STACK_SIZE is specified in words, not bytes.
*/
*pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
}
int main(void)
......@@ -39,14 +63,46 @@ int main(void)
cdcacm_init();
printf("Initializing tasks ...\n");
/* Serial */
if (xTaskCreate(
vSerialTask,
(const char*)"Serial",
configMINIMAL_STACK_SIZE,
NULL,
tskIDLE_PRIORITY + 1,
NULL
) != pdPASS) {
printf("Failed to create serial-comms task!\n");
abort();
}
if (xTaskCreate(
vApiDispatcher,
(const char*)"API Dispatcher",
configMINIMAL_STACK_SIZE,
NULL,
tskIDLE_PRIORITY + 2,
NULL
) != pdPASS) {
printf("Failed to create api dispatcher task!\n");
abort();
}
printf("Initializing dispatcher ...\n");
api_dispatcher_init();
printf("Staring core1 payload ...\n");
core1_start();
vTaskStartScheduler();
printf("ERROR: FreeRTOS did not start due to above error!\n");
#if 0
while(1) {
__WFE();
api_dispatcher_poll();
}
#endif
}
......@@ -39,6 +39,22 @@ api_dispatcher_lib = static_library(
dependencies: periphdriver,
)
##########################################################################
#
# FreeRTOS
#
##########################################################################
freertos = static_library(
'freertos',
freertos_sources,
dependencies: periphdriver,
include_directories: [
freertos_includes,
include_directories('./'),
],
)
##########################################################################
#
# Epicardium executable
......@@ -49,9 +65,11 @@ elf = executable(
name + '.elf',
'main.c',
'cdcacm.c',
'serial.c',
dependencies: [libcard10, max32665_startup_core0, maxusb],
link_with: api_dispatcher_lib,
link_with: [api_dispatcher_lib, freertos],
link_whole: [max32665_startup_core0_lib, board_card10_lib],
include_directories: [freertos_includes],
link_args: [
'-Wl,-Map=' + meson.current_build_dir() + '/' + name + '.map',
],
......
#include <stdint.h>
#include <stdio.h>
#include "serial.h"
#include "cdcacm.h"
#include "uart.h"
#include "tmr_utils.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
extern mxc_uart_regs_t * ConsoleUart;
static QueueHandle_t read_queue;
void epic_uart_write_str(char*str, intptr_t length)
{
UART_Write(ConsoleUart, (uint8_t*)str, length);
cdcacm_write((uint8_t*)str, length);
}
char epic_uart_read_chr(void)
{
char chr;
xQueueReceive(read_queue, &chr, portMAX_DELAY);
return chr;
}
void vSerialTask(void*pvParameters)
{
static uint8_t buffer[sizeof(char) * SERIAL_READ_BUFFER_SIZE];
static StaticQueue_t read_queue_data;
/* Setup read queue */
read_queue = xQueueCreateStatic(
SERIAL_READ_BUFFER_SIZE,
sizeof(char),
buffer,
&read_queue_data
);
/* Setup UART interrupt */
NVIC_ClearPendingIRQ(UART0_IRQn);
NVIC_DisableIRQ(UART0_IRQn);
NVIC_SetPriority(UART0_IRQn, 1);
NVIC_EnableIRQ(UART0_IRQn);
while (1) {
char chr;
/* TODO: Wait for interrupt on either device */
vTaskDelay(portTICK_PERIOD_MS * 10);
if(UART_NumReadAvail(ConsoleUart) > 0) {
chr = UART_ReadByte(ConsoleUart);
} else if(cdcacm_num_read_avail() > 0) {
chr = cdcacm_read();
} else {
continue;
}
if (chr == 0x3) {
/* Control-C */
TMR_TO_Start(MXC_TMR5, 1, 0);
}
if (xQueueSend(read_queue, &chr, 100) == errQUEUE_FULL) {
/* Queue overran, wait a bit */
vTaskDelay(portTICK_PERIOD_MS * 50);
}
}
}
#ifndef EPIC_SERIAL_H
#define EPIC_SERIAL_H
#define SERIAL_READ_BUFFER_SIZE 128
void vSerialTask(void*pvParameters);
#endif /* EPIC_SERIAL_H */
file ../../build/hw-tests/hello-freertos/hello-freertos.elf
source ../../.gdbinit
name = 'hello-freertos'
freertos = static_library(
'freertos-sdk',
freertos_sdk_sources,
'freertos',
freertos_sources,
freertos_cli_sources,
dependencies: periphdriver,
include_directories: [
freertos_sdk_includes,
freertos_includes,
freertos_cli_includes,
include_directories('./'),
]
)
......@@ -19,7 +21,7 @@ sources = files(
elf = executable(
name + '.elf',
sources,
include_directories: freertos_sdk_includes,
include_directories: [freertos_includes, freertos_cli_includes],
dependencies: [libcard10, max32665_startup],
link_with: freertos,
link_whole: [max32665_startup_lib, board_card10_lib],
......
/*
* FreeRTOS+CLI V1.0.4
* Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* http://www.FreeRTOS.org
* http://aws.amazon.com/freertos
*
* 1 tab == 4 spaces!
*/
/* Standard includes. */
#include <string.h>
#include <stdint.h>
/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "task.h"
/* Utils includes. */
#include "FreeRTOS_CLI.h"
/* If the application writer needs to place the buffer used by the CLI at a
fixed address then set configAPPLICATION_PROVIDES_cOutputBuffer to 1 in
FreeRTOSConfig.h, then declare an array with the following name and size in
one of the application files:
char cOutputBuffer[ configCOMMAND_INT_MAX_OUTPUT_SIZE ];
*/
#ifndef configAPPLICATION_PROVIDES_cOutputBuffer
#define configAPPLICATION_PROVIDES_cOutputBuffer 0
#endif
typedef struct xCOMMAND_INPUT_LIST
{
const CLI_Command_Definition_t *pxCommandLineDefinition;
struct xCOMMAND_INPUT_LIST *pxNext;
} CLI_Definition_List_Item_t;
/*
* The callback function that is executed when "help" is entered. This is the
* only default command that is always present.
*/
static BaseType_t prvHelpCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
/*
* Return the number of parameters that follow the command name.
*/
static int8_t prvGetNumberOfParameters( const char *pcCommandString );
/* The definition of the "help" command. This command is always at the front
of the list of registered commands. */
static const CLI_Command_Definition_t xHelpCommand =
{
"help",
"\r\nhelp:\r\n Lists all the registered commands\r\n\r\n",
prvHelpCommand,
0
};
/* The definition of the list of commands. Commands that are registered are
added to this list. */
static CLI_Definition_List_Item_t xRegisteredCommands =
{
&xHelpCommand, /* The first command in the list is always the help command, defined in this file. */
NULL /* The next pointer is initialised to NULL, as there are no other registered commands yet. */
};
/* A buffer into which command outputs can be written is declared here, rather
than in the command console implementation, to allow multiple command consoles
to share the same buffer. For example, an application may allow access to the
command interpreter by UART and by Ethernet. Sharing a buffer is done purely
to save RAM. Note, however, that the command console itself is not re-entrant,
so only one command interpreter interface can be used at any one time. For that
reason, no attempt at providing mutual exclusion to the cOutputBuffer array is
attempted.
configAPPLICATION_PROVIDES_cOutputBuffer is provided to allow the application
writer to provide their own cOutputBuffer declaration in cases where the
buffer needs to be placed at a fixed address (rather than by the linker). */
#if( configAPPLICATION_PROVIDES_cOutputBuffer == 0 )
static char cOutputBuffer[ configCOMMAND_INT_MAX_OUTPUT_SIZE ];
#else
extern char cOutputBuffer[ configCOMMAND_INT_MAX_OUTPUT_SIZE ];
#endif
/*-----------------------------------------------------------*/
BaseType_t FreeRTOS_CLIRegisterCommand( const CLI_Command_Definition_t * const pxCommandToRegister )
{
static CLI_Definition_List_Item_t *pxLastCommandInList = &xRegisteredCommands;
CLI_Definition_List_Item_t *pxNewListItem;
BaseType_t xReturn = pdFAIL;
/* Check the parameter is not NULL. */
configASSERT( pxCommandToRegister );
/* Create a new list item that will reference the command being registered. */
pxNewListItem = ( CLI_Definition_List_Item_t * ) pvPortMalloc( sizeof( CLI_Definition_List_Item_t ) );
configASSERT( pxNewListItem );
if( pxNewListItem != NULL )
{
taskENTER_CRITICAL();
{
/* Reference the command being registered from the newly created
list item. */
pxNewListItem->pxCommandLineDefinition = pxCommandToRegister;
/* The new list item will get added to the end of the list, so
pxNext has nowhere to point. */
pxNewListItem->pxNext = NULL;
/* Add the newly created list item to the end of the already existing
list. */
pxLastCommandInList->pxNext = pxNewListItem;
/* Set the end of list marker to the new list item. */
pxLastCommandInList = pxNewListItem;
}
taskEXIT_CRITICAL();
xReturn = pdPASS;
}
return xReturn;
}
/*-----------------------------------------------------------*/
BaseType_t FreeRTOS_CLIProcessCommand( const char * const pcCommandInput, char * pcWriteBuffer, size_t xWriteBufferLen )
{
static const CLI_Definition_List_Item_t *pxCommand = NULL;
BaseType_t xReturn = pdTRUE;
const char *pcRegisteredCommandString;
size_t xCommandStringLength;
/* Note: This function is not re-entrant. It must not be called from more
thank one task. */
if( pxCommand == NULL )
{
/* Search for the command string in the list of registered commands. */
for( pxCommand = &xRegisteredCommands; pxCommand != NULL; pxCommand = pxCommand->pxNext )
{
pcRegisteredCommandString = pxCommand->pxCommandLineDefinition->pcCommand;
xCommandStringLength = strlen( pcRegisteredCommandString );
/* To ensure the string lengths match exactly, so as not to pick up
a sub-string of a longer command, check the byte after the expected
end of the string is either the end of the string or a space before
a parameter. */
if( ( pcCommandInput[ xCommandStringLength ] == ' ' ) || ( pcCommandInput[ xCommandStringLength ] == 0x00 ) )
{
if( strncmp( pcCommandInput, pcRegisteredCommandString, xCommandStringLength ) == 0 )
{
/* The command has been found. Check it has the expected
number of parameters. If cExpectedNumberOfParameters is -1,
then there could be a variable number of parameters and no
check is made. */
if( pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters >= 0 )
{
if( prvGetNumberOfParameters( pcCommandInput ) != pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters )
{
xReturn = pdFALSE;
}
}
break;
}
}
}
}
if( ( pxCommand != NULL ) && ( xReturn == pdFALSE ) )
{
/* The command was found, but the number of parameters with the command
was incorrect. */
strncpy( pcWriteBuffer, "Incorrect command parameter(s). Enter \"help\" to view a list of available commands.\r\n\r\n", xWriteBufferLen );
pxCommand = NULL;
}
else if( pxCommand != NULL )
{
/* Call the callback function that is registered to this command. */
xReturn = pxCommand->pxCommandLineDefinition->pxCommandInterpreter( pcWriteBuffer, xWriteBufferLen, pcCommandInput );
/* If xReturn is pdFALSE, then no further strings will be returned
after this one, and pxCommand can be reset to NULL ready to search
for the next entered command. */
if( xReturn == pdFALSE )
{
pxCommand = NULL;
}
}
else
{
/* pxCommand was NULL, the command was not found. */
strncpy( pcWriteBuffer, "Command not recognised. Enter 'help' to view a list of available commands.\r\n\r\n", xWriteBufferLen );
xReturn = pdFALSE;
}
return xReturn;
}
/*-----------------------------------------------------------*/
char *FreeRTOS_CLIGetOutputBuffer( void )
{
return cOutputBuffer;
}
/*-----------------------------------------------------------*/
const char *FreeRTOS_CLIGetParameter( const char *pcCommandString, UBaseType_t uxWantedParameter, BaseType_t *pxParameterStringLength )
{
UBaseType_t uxParametersFound = 0;
const char *pcReturn = NULL;
*pxParameterStringLength = 0;
while( uxParametersFound < uxWantedParameter )
{
/* Index the character pointer past the current word. If this is the start
of the command string then the first word is the command itself. */
while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) != ' ' ) )
{
pcCommandString++;
}
/* Find the start of the next string. */
while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) == ' ' ) )
{
pcCommandString++;
}
/* Was a string found? */
if( *pcCommandString != 0x00 )
{
/* Is this the start of the required parameter? */
uxParametersFound++;
if( uxParametersFound == uxWantedParameter )
{
/* How long is the parameter? */
pcReturn = pcCommandString;
while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) != ' ' ) )
{
( *pxParameterStringLength )++;
pcCommandString++;
}
if( *pxParameterStringLength == 0 )
{
pcReturn = NULL;
}
break;
}
}
else
{
break;
}
}
return pcReturn;
}
/*-----------------------------------------------------------*/
static BaseType_t prvHelpCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
{
static const CLI_Definition_List_Item_t * pxCommand = NULL;
BaseType_t xReturn;
( void ) pcCommandString;