CMSIS RTOSv2
CMSIS RTOSv2
Trevor Martin
CMSIS-RTOS2
Introduction
Design Techniques
Design Study
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 2
Effort
Complexity
▪ At the beginning there will be more effort caused by using an RTOS. Once
you are familiar with RTOS based development there are big increases in
productivity
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 3
Why use a Real-Time Kernel?
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 4
Cortex-M3 Processor Overview
Central Core:
Wake-Up Interrupt Controller: Integrated Nested Vectored Interrupt 1.25 DMIPS/MHz
for Low Power Stand by Controller and SYSTICK Timer 1 Cycle Multiply
Operation
Hardware Divide
Instrumentation Trace
Debug Access Port: Macrocell (ITM ) for
JTAG or Serial Wire Data Trace via Single
Wire Output
1x AHB-Lite Buses
2x AHB-Lite Buses
SYSTEM (SRAM & Fast Peripherals)
I_CODE (Instruction Code Bus)
1x APB Bus
D_CODE (Data / Coefficients Code Bus)
ARM Peripheral Bus (Internal & Slow Peripherals)
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 5
CMSIS
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 6
CMSIS-RTOS2 Features
Wait functions
Virtual Timer
Threads
Scheduler Thread
Semaphore
Flags
Memory Pool ISR Support
Message queue
▪ The CMSIS-RTOS2 Kernel provides a scheduler and additional objects and services
− Scheduler
− Objects
− Semaphores,Event Flags,Mutex,Threads,Message passing
− Services
− Timers, Memory management, ISR support
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 8
SYSTICK Timer
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 9
Converting from bare metal to an RTOS project
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 11
Building your first program
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 12
Building your first program
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 13
Building your first program
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 14
Building your first program
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 15
Building your first program
− Startup_stm32f10x_md.s
− Configures the stack and heap
− System_xxxxx.c
− Configures the system clocks
− RTE_Device.h
− Configures the peripheral physical
interfaces
− rtx_config.c
− We will examine the RTX configuration
file later in the workshop
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 16
Building your first program
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 18
Building your first program
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 19
Building your first program
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 20
Simulator
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 21
Building your first program
▪ This will start the simulator and run the code to Main
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 22
Building your first Project
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 23
Building your first program
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 24
Processor Mode
▪ Handler Mode
− Used to handle exceptions. The processor returns to Thread mode when it has finished
exception processing.
▪ Thread Mode
− Used to execute application software. The processor enters Thread mode when it comes
out of reset.
− In Thread mode, the CONTROL register controls whether software execution is privileged
or unprivileged, see CONTROL register. In Handler mode, software execution is always
privileged.
− RTX5 uses processor modes
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 25
Privilege Levels
▪ Unprivileged
The software:
− has limited access to the MSR and MRS instructions, and cannot use the CPS instruction
− cannot access the system timer, NVIC, or system control block
− might have restricted access to memory or peripherals.
− Unprivileged software executes at the unprivileged level
▪ Privileged
− The software can use all the instructions and has access to all resources.
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 26
Cortex M Processor Stacks
R0
▪ In addition to the different operating modes
R1 the Cortex M processors can support two
R2
R3 stacks
R4
R5 − Main Stack
R6
R7
− Configured at startup
−
R8
R9
Process Stack
R10
− Configured by the user application code
R11
R12
R13 (MSP) R13 (PSP)
R14 (LR)
PC
PSR
PRIMASK
FAULTMASK*
BASEPRI*
CONTROL
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 27
Configuring the Cortex Processor
▪ The CPU CONTROL register is used to configure the operating mode and
enable the second stack pointer
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 28
Privilege, Modes and Stacks Summary
Operations Stacks
(privilege out of reset) (Main out of reset)
Privileged or
Thread Main or Process
- No exception is being processed
Unprivileged
- Normal code execution
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 29
Exercise
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 30
CMSIS RTOS2 API
▪ Thread management
▪ Time management
▪ ThreadFlags
▪ EventFlags
▪ Semaphores
▪ Mutex
▪ Messages
▪ Memory Pool
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 31
CMSIS RTOS2 osStatus codes
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 32
Threads - The RTOS building block
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 33
Concurrency
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 34
Threads – Program structure
Send
PROCESS / Task
Receive
Message
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 35
Threads - Timeslice
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 36
Threads Control block and ‘stack’
Thread
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 37
Threads Control block and ‘stack’
Thread
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 38
Threads - States
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 39
Thread Lifecycle
Waiting
Ready
Inactive
Run
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 40
Thread
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 41
Thread
osThreadId_t t_thread1,t_thread2,t_thread3;
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 42
Threads -Management
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 43
Threads – Priority Levels
osPriorityLow
osPriorityLow 1-7
osPriorityBelowNormal
osPriorityBelowNormal 1-7
osPriorityNormal
osPriorityNormal 1-7
osPriorityHigh
osPriorityHigh 1-7
osPriorityRealTime
osPriorityRealTime 1-7
osPriorityError
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 44
Threads Creation
osThreadId_t led_ID1;
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 45
Starting the CMSIS RTOS
void app_main (void *argument)
{
// System Initialization
SystemCoreClockUpdate();
LED_Initialize();
while(1);
}
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 47
Memory allocation
▪ You can override the default stack size for a given thread
− In the thread attributes set the stack_size to the required size
Object Specific memory pool
os_thread_t led_thread_tcb_1;
uint64_t led_thread_stk_1[64];
#include <rtx_os.h>
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 54
Exercise Static memory allocation
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 55
Attributes – Joinable and Detached
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 56
Exercise Joinable Threads
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 57
Threads
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 58
Exercise
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 59
Time Management
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 60
Delay
▪ Within a task you can delay a function for a specific number of system
ticks
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 61
Software interrupt
16 0
Op code Ordinal
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 63
Software interrupt
Vector Table
SVC vector
SVC 0
SVC 2
▪ By encoding a number into the ‘ordinal’ section of the SVC instruction you
can call different privileged functions
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 64
RTOS objects
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 65
Kernel Time functions
uint64_t osKernelTickCount(void)
uint32_t osKernelGetTickFreq(void)
uint32_t osKernelGetSysTimerCount(void)
uint32_t osKernelGetSysTimerFreq(void)
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 66
DelayUntil
▪ Within a task you can delay a function until a specific OS tick count is
reached
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 67
Exercise
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 68
Delay ‘Jitter’
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 69
Virtual Timer
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 70
Virtual Timer - Timer API
osTimerId_t osTimerNew (os_timer_func_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr)
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 71
Virtual Timer – Creating a timer
osTimerId_t timer0
osTimerStart(timer0, periodInTicks);
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 72
Timer
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 73
Timer
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 74
Exercise
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 75
Idle Thread
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 76
Exercise
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 77
Suspend and resume
Function Description
uint32_t osKernelSuspend(void) Suspends the scheduler
osKernelResume(uint32_t ticks) Restarts the scheduler
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 78
Suspend and resume
Function Description
uint32_t osKernelSuspend(void) Suspends the scheduler
osKernelResume(uint32_t ticks) Restarts the scheduler
▪ osKernelSuspend will stop the sysTick and return the maximum allowable
sleep period
▪ osKernel Resume updates the scheduler with the actual sleep period in
ticks
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 79
Synchronisation
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 80
Intertask communication
▪ CMSIS-RTOS2 has the following objects that allow inter task communication
− Thread Flags
− Event Flags
− Semaphores
− Mutex
− Message queue
− Memory pool
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 81
Thread Flags
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 82
Thread Flags API
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 83
Thread Flags
for(;;;)
for(;;;)
{
{
……..
……..
osThreadFlagsSet (T_led_ID1,0x01);
osThreadFlagsWait (0x03,osFlagsWaitAny,osWaitForever);
…….
}
}
▪ CMSIS RTOS Thread Flags are used to trigger execution states between
threads
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 84
Thread Flags
Option Description
osFlagsWaitAny Wait for any flag (Default)
osFlagsWaitAll Wait for all flags
osFlagsNoClear Do not clear flags that are
being waited on
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 85
Thread Flags
▪ Thread may set one or more event flags in any given thread
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 86
Thread Flags
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 87
Thread Flags
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 88
Exercise
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 89
Pend exception
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 90
PEND interrupt
SysTick
ADC ISR SysTick SVC SysTick
Delay
ADC Thread Thread Thread Thread
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 91
PEND Interrupt
SysTick
ADC ISR
RTOS
ADC
Pend
Thread Thread
◼ When we enter the RTOS scheduler we can check is another ISR is pending
◼ If this is the case we can make the PEND interrupt active and quit the RTOS
ISR
◼ This allows the USER ISR to run
◼ When the user ISR finishes the PEND interrupt will run and resume execution
of the RTOS code
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 92
Integrating ISR’s with an RTOS
ISR B level 0
ISR A level 1
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 93
Servicing ISR
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 94
ISR/Thread Flags overhead
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 95
Exercise Interrupt Thread Flag
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 100
Event Flags
Thread 1
Thread 2 Thread 3
osEventFlagsNew
Function
osEventFlagsId_t osEventFlagsNew (const osEventFlagsAttr_t *attr)
int32_t osEventFlagsWait (osEventFlagsId_t ef_id, int32_t flags, uint32_t options, uint32_t timeout)
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 102
Event Flags
osEventFlagsId_t myEventFlag;
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 103
Exercise Event Flags
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 104
Semaphores
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 105
Exclusive access instructions
write read
write
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 106
Semaphore API
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 107
Semaphore
osSemaphoreId_t sem1;
Sem1 = osSemaphoreNew(maxCount,initalCount,&semaphoreAttributes);
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 108
Semaphores
While(1) While(1)
{ {
…….. ……..
osSemaphoreAcquire(sem1, osWaitForever); ……. osSemaphoreAcquire(sem1, osWaitForever); …….
……. …….
osSemaphoreRelease(sem1); osSemaphoreRelease(sem1);
} }
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 109
Semaphore
▪ Once a semaphore has been initialised threads request a token from the
semaphore to run and then return the token when they are finished
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 110
Semaphore
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 111
Semaphore
▪ Semaphore uses
− Controlling shared resource access
− Memory barrier
− synchronizing events
− Reference text ‘Little book of Semaphores‘ Downey
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 112
Exercise
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 113
Mutex
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 114
Mutex API
Mutex
osMutexId_t osMutexNew (const osMutexAttr_t *attr)
const char osMutexGetName (osMutexId_t mutex_id)
osStatus_t osMutexAcquire (osMutexId_t mutex_id, uint32_t timeout)
osStatus_t osMutexRelease (osMutexId_t mutex_id)
osThreadId_t osMutexGetOwner (osMutexId_t mutex_id)
osStatus_t osMutexDelete (osMutexId_t mutex_id)
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 115
Mutex
osMutexId_t uart_mutex;
uart_mutex = osMutexNew(&osMutex);
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 116
Mutex
▪ A Mutex (mutual exclusion) The threads can then acquire and release the mutex
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 117
Mutex
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 118
Mutex
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 119
Exercise
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 120
Passing data between Threads
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 121
Message Queue
Message queue
Thread Thread
1 2
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 122
Message Queue API
osStatus_t osMessageQueuePut (osMessageQueueId_t mq_id, const void *msg_ptr, uint8_t msg_prio, uint32_t timeout)
osStatus_t osMessageQueueGet (osMessageQueueId_t mq_id, void *msg_ptr, uint8_t *msg_prio, uint32_t timeout)
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 123
Message Queue
osMessageQueueId_t Q_LED;
Q_LED = osMessageQueueNew(maxNumberMssg,mssgWidth,&queueAttr_Q_LED );
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 124
Message Queue
uint32_t dataIn;
osMessageQueuePut(Q_LED,&dataIn,osPriorityNormal,osWaitForever);
uint32_t dataOut;
uint8_t priority;
osMessageQueueGet(Q_LED,&dataOut,&priority,osWaitForever);
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 125
Exercise
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 126
Memory pool
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 127
Memory Pool API
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 128
Creating a Memory Pool
osMemoryPoolId_t mpool;
typedef struct
{
uint8_t canData[8];
} message_t;
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 129
Memory Pool
message_t *message;
for(index =0;index<8;index++)
{
message->canData[index] = testData; //populate buffer with data
}
......................
osMemoryPoolFree(mpool, message);
▪ The memory blocks can be allocated, used and then freed back to the
pool
▪ Management of the blocks is done by the RTOS
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 130
Zero copy mailbox
Message queue
Thread Thread
1 2
alloc free
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 131
Exercise Zero copy mailbox
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 132
Kernel Information
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 133
TrustZone
Secure MPU
Secure SysTick
Secure System Control Block
Security Attribute Unit
Trust zone
tz_context.h
TZ_InitContextSystem_S
TZ_AllocModuleContext_S
TZ_FreeModuleContext_S
TZ_LoadContext_S
TZ_StoreContext_S
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 136
Trust Zone
4/11/2019 137
Exercise
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 138
Running critical code
void osKernellLock(void)
void osKernelUnlock(void)
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 139
Configuration
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 140
Configuration
▪ Thread configuration
− Can enable an object memory pool
− Default stack size
− Debug options
− Stack checking
Stack watermark ( for maximum memory usage)
Thread operating mode
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 141
Configuration
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 142
Co Operative multitasking
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 143
Test Driven Development
▪ http://nparc.cisti-icist.nrc-
cnrc.gc.ca/npsi/ctrl?action=shwart&index=an&req=5763742&lang=en
In a Nutshell
▪ 3 rules
− You are not allowed to write any production code unless it is to
make a failing unit test pass.
− You are not allowed to write any more of a unit test than is sufficient
to fail; and compilation failures are failures.
− You are not allowed to write any more production code than is
sufficient to pass the one failing unit test.
Developer Test Frameworks
− CppUTest
− C\C++
− www.cpputest.org
Unity in MDK-ARM
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 159
Low power Management
Power
Cortex-M
management Sleep Deep
4/11/2019 160
Low Power management
▪ osSuspend returns the number of ticks a device may be in a low power state
▪ It will return 0xFFFFFFFF for an infinite low power state
− If all the threads block waiting for an input
▪ When we detect this state the system can be placed in the lowest power state
4/11/2019 161
Watchdog Management
4/11/2019 162
Low Power management
T1 T2
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 163
Watchdogs
T1 T2
WDog
▪ This technique has an initial latency while the first block is processed
Exercise
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 167
Middleware
4/11/2019 169
Design Rules
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 170
Design Rules
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 171
Design Rules
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 172
Design Rules
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 173
Design Rules
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 174
Design Rules
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 175
Design Rules
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 176
Design Rules
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 177
Modbus Gateway
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 178
Modbus Gateway
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 179
Exercise
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 180
Exercise
▪ Design study
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 181
Exercise
▪ Design study
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 182
Project Architecture
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 183
▪ Source code layout is generally poor in embedded systems middleware
− Functionality – Good
− Use of the ‘C’ language - Good
− MISRA C
− PC-Lint
− But in many projects
− A single source file may contain
− Interrupt code
− Application functions
− Peripheral driver code for different peripherals
− Large numbers of include files and defines
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 184
Project Definition
Requirements
Units of Functional
Drivers
concurrency components
( Peripherals)
(threads) (modules)
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 185
Main
▪ The main module shall contain the minimum possible amount of code
− This allows main to be easily removed for testing
− You shall not modify code under test ( ie comment code out)
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 186
System header file
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 187
Threads
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 188
Supervisor thread
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 189
RTOS
▪ Use CMSIS-RTOS2
− Standard API
− Supported by Keil RTX, FreeRTOS
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 190
Component
▪ Software component
− A set of related functions encapsulated in a single repository (module)
− Breaks the complexity into manageable parts
− Provides a standard interface to the thread code
− Interfaces to the hardware through the driver layer
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 191
Software components
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 192
Software components
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 193
▪ Functional isolation allows
− Development and test before integration into the main project code
− Create IP for reuse
− Create, document and test once
− Increased productivity
− Increased reliability
− Enhanced IDE Support
− Configuration Wizard Annotations
− Keil component viewer
− Keil Event Recorder
− Pack installer
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 194
Header files
Module Description
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 195
Header files
▪ There shall be a standard layout for header files
#include “system.h”
#include “myHeader_conf.h”
#include “component1.h”
#include “component2.h”
#include <stdio.h>
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 196
▪ The drivers use a custom CMSIS Driver profile
− Defined and maintained in house
− Most of the initial work involves
− Creating the profile documentation
− Creating the profile template
− Then you can create a standard driver for any MCU
− Minimal runtime overhead
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 197
▪ CMSIS- currently provides support for
− USART
− SPI
− SAI
− I2C
− USB HOST and Device
− FLASH NAND and NOR
− Ethernet
− CAN
− MCI
▪ Initially develop profiles for
− Timer
− ADC
− DAC
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 198
CMSIS-Driver API
Function Description
<peripheral>_Initialize Installs the IRQ callback, configures the GPIO secondary functions.
<peripheral>_PowerControl Sets the peripheral power mode ie full power, low power, standby.
<peripheral>_Control Provides the main control interface to the peripheral. Configures the
operating mode, allows updating peripheral features during run-time.
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 199
RTOS Validation
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 200
CMSIS-Driver Validation
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 201
CMSIS-Driver Validation
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 202
▪ Component testing
− Unity test framework for developer test
− Tessy for formal unit test
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 203
Application
project
2014-07-11 Copyright © Hitex Development Tools 2014. All rights reserved. Page 204