Technical topic: TASTE on MSP430 with FreeRTOS

From TASTE
Revision as of 14:02, 22 May 2020 by Rbabski (talk | contribs)
Jump to: navigation, search

This page describes how FreeRTOS support was added to TASTE.

Hardware Description

This article describes steps of adding support for MSP-430 platform using MSP43FR5969 processor as example. The MSP430FR5969 LaunchPad Development Kit was used for testing purposes.

Microcontroller's parameters:

  • 16-bit RISC architecture;
  • 16‑MHz Clock;
  • 64KB FRAM / 2KB SRAM.

On this very limited platform using PolyORB-HI-Ada or PolyORB-HI-C is impossible, due to memory requirements. In the next sections the different approach of code generation is described.

Memory models in MSP430FR5969

The MSP430FR5969 can operate in two memory models:

  • small memory model;
  • large memory model.

In small memory model only 48 KB of FRAM are available for programmer with additional 2 KB for stack and global or static variables. This limitation is caused by the fact, that in small memory model, only 16 bits for addressing are avaiable. The 16 bit should allow to address 64 KB of memory, but some memory areas are used by bootlader or perpherals.

In large memory model, the programmer can use additional 16 KB of FRAM, which gives 64 KB of memory + additional 2 KB for stack and bss. In this model all 20 bits of address line are used and also the size of pointers are bigger - 4 bytes instead of 2 and the different instruction set is used. It means that simple change from small memory model to large memory model increases size of generated code.

In small memory model the static and global variables are allocated in 2KB of RAM, which may be too small memory segment of are variables. The variables may be allocated outside the RAM segment by adding the special attribute, bellow is the example but it can be changed by additing special attribute. For an example:

__attribute__ ((persistent)) int global_variable = 0;

The programmes should reintialize value of this variable after reset of the MCU.

In large memory model the variables may be allocated outside the RAM memory segment, which allows the programmer to declare large number of variables. There's also an compiler option -mdata-region=upper to use only memory segment from higher memory, the almost whole RAM segment may be used by stack.

The problem with 2 KB of RAM segment is that the stack may overwrite the other variables allocated in this segment during runtime. This may happen before start of FreeRTOS scheduler, during initialization. Currently about 200 bytes of stack are required. The programmer should make sure that, there's enaugh space for stack in the RAM segment after building the partition image.

TBD Script which checks this constraint since the msp430-elf-ld does not allow to set the stack size (The TI compiler allows to do this).

FreeRTOS port for MSP430FR5969

The FreeRTOS port for MSP430FR5969 supports both small memory model and large memory model. When large memory model is used the following options should be passed to the compiler:

  • -D__LARGE_DATA_MODEL__=1
  • -mlarge

There are options to specify in which memory segment the code and data should be placed:

  • -mcode-region=[either,lower,upper]
  • -mdata-region=[either,lower,upper]

The options -mcode-region=either,lower,upper -mdata-region=upper are recommended for TASTE.

For small memory model, the only required compiler option is -msmall.

Memory allocation in FreeRTOS

FreeRTOS may be configured to use dynamic memory allocation or static memory allocation or both. When the dynamic memory allocation is used the programmer shoud provide memory region for special FreeRTOS heap. When static memory allocation is used the programmer shoud allocate required structures before creating FreeRTOS objects. For TASTE the dynamic memory allocation is disabled, the generated code uses only static memory allocation.

More information at Static Vs Dynamic Memory Allocation

New execution platform

Add new platform Platform_MSP430_FreeRTOS to TASTE. Follow instructions from Technical topic: Add a new target platform to TASTE.

TBC

Kazoo templates for MSP430

Kazoo generates multiplatform skeletons and glue source code for function blocks. The asn1scc generates code responsible for encoding/decoding data, which is also platform agnostic.

The rest of the code, which should be generated to create image of the partition, comes from Concurrency View. This code is responsible for device initialization, the initialization of drivers and function blocks from Interface View, and communication between these objects. To cope with this the following entities should be used:

  • Threads;
  • Queues;
  • Timers;
  • Mutexes/Semaphores.

Before support for MSP430 this was achived by using code generated by Ocarina which uses PolyORB-HI-Ada or PolyORB-HI-C as middleware.

Support for MSP430 is based on FreeRTOS constructs:

This set of constructs is enaugh to create code which ensures cooperation of function blocks and communication with remote partitions.

The templates should also generate the Makefile and GNAT Project Manager configuration file, which are responsible for building process of the partition image.



The Concurrency View code is responsible for initialization of devices, drivers, threads and other structures. The created structures are responsible for passing information between functions and thread synchronization. Generally this code is generated by Ocarina. The generated code uses PolyORB-HI-Ada or PolyORB-HI-C as a middleware.

Kazoo templates for Concurrency View has parameters, which describes threads, mutexes, functions, etc. and relations between them. These parameters may be used to generate code without PolyORB-HI-Ada or PolyORB-HI-C. This section described this approach.

TBC

Mapping of TASTE structures

TASTE InterfaceView: Function: every function has initialization function and mutex. Interfaces:

  • cyclic: cyclic interface has dedicated thread with timer and queue;
  • sporadic: sporadic interface has dedicated thread and queue for input requests;
  • protected: protected interface locks function mutex before calling actual implementation;
  • unprotected: no mutex, only function wrapper is generated.

Every queue has length property.

TASTE ConcurrencyView: Every thread has StackSize property and Priority.

TASTE DeploymentView: Every node has optional devices.

The threads will be mapped to FreeRTOS Tasks. Task has own stack, priority and main function.

The communication and synchronization between tasks will be realised using queues and semaphores or mutexes.

Cyclic provided interfaces of functions will be implemented using software timers.

TBC

Gathering FreeRTOS source files

The FreeRTOS source files must be present in the partition source directory before compilation. There are two possible solutions:

  • embed FreeRTOS source files in kazoo templates;
  • create template that generates shell script, which copies requires FreeRTOS files from known location.

TBC

main.c

To generate this file, the new Concurrency View subdirectory should be created. This file is a entry point, the function main for partition on MSP430. This file should be responsible for intialization of FreeRTOS primitives like tasks and queues. The device drivers should be initialized here. The main.c should also contain all necessary functions and other structures, which are required by FreeRTOS.

TBC

Function wrappers

The wrappers around provided interfaces of the functions will be generated in files build/<node name>/<partition name>/freertos_interface.{c,h}. The responsibilities of wrapper function is:

  • acquire lock on mutex before calling the function if provided interface is protected if necessary;
  • send #Requests to other partitions.

As a base for templates following subdirectories will be used: pohic_wrappers_body and pohic_wrappers_header.

Requests

This file will be contains structures responsible for communication between other partitions. As a prototype for this template, the files request.c and request.h, currently generated by Ocarina, will be used.

TBC

Marshallers

This file will be contain function responsible for decoding and encoding #Requests. As a prototype for this template, the files marshallers.c and marshallers.h, currently generated by Ocarina will be used.

TBC

Subprograms

This file will be contain functions corresponding to subprograms for #Device drivers. As a prototype for this template, the files subprograms.c and subprograms.h, currently generated by Ocarina will be used.

TBC

Activity

This file will be contain activities like, waiting for events or queues and calling functions responsible for realization of provided interfaces. As a prototype for this template, the files activity.c and activity.h, currently generated by Ocarina will be used.

TBC

FreeRTOSConfig.h

To generate this file, the new concurrency_view subdirectory should be created.

The file FreeRTOSConfig.h contains configuration for FreeRTOS. This file defines a set of macros. Example macros are presented in table below:

Macro name Description
configTICK_RATE_HZ Used to configure tick frequency for scheduler
configMAX_PRIORITIES Used to configure max priority of the task
configGENERATE_RUN_TIME_STATS Used to enable or disable run time statistics
configTOTAL_HEAP_SIZE Used to configure total heap size
configMINIMAL_STACK_SIZE Uset to configure minimal stack size

Some macros, like configTICK_RATE_HZ, will be generated based on kazoo template parameters. FreeRTOSConfig.h will be used for tailoring FreeRTOS for TASTE purposes. For an example to disable coroutines following line will be added:

#define configUSE_CO_ROUTINES ( 0 )

Or to do not include FreeRTOS function to compilation to reduce memory usage:

#define INCLUDE_vTaskDelete ( 1 )

TBC

Modification in Makefiles and GNAT Project Manager files

The source code files generated by kazoo, should be compiled. For this purpose the kazoo template should be created based on existing template for other platform, e.g. c_pohi_gpr.

   $ cd ~/tool-src/kazoo/templates/concurrency_view
   $ cp -r c_pohi_gpr freertos_msp430_gpr

Within new subdirectory, the file partition.tmplt is the main file responsible for generation of .gpr file, but other files, like trigger.tmplt, should be changed. The generated file is responsible for building partition. The main modification in partition.tmplt is exclusion of PolyORB files from compilation and inclusion of source files generated for FreeRTOS partition. This file should also include FreeRTOS source files and source code for device drivers.

Another modification in this file is introduction of C compiler for MPS430 platform: msp430-elf-gcc.

TBC

Device drivers

The devices and their drivers are described in file ocarina_components.aadl. This file describes subprograms required by device drivers. The device drivers for MSP430 and FreeRTOS will be implemented independly using driver library for MSP430FR5969.

ocarina_components.aadl

The file ocarina_components.aadl is part of the taste-setup project. On the Taste VM the location of this file is /home/taste/tool-src/install/misc/aadl-library/ocarina_components.aadl.

Adding new bus

To add new bus or implementation of bus add entry to the package ocarina_buses. For the MSP430 the new bus implementation serial.minimal was added.

Ocarina components adding bus.png

Adding new drivers

To add new device add entry to the package ocarina_drivers Fot MSP430 two devices was added.


Ocarina components adding device.png

UART

TBC