Concept

septembre

Lez 27

FreeRTOS Vendredi FreeRTOS

1 Lez Concept 15 chemin neuf 34 980 Montferrier Concept

septembre

Lez 27

[email protected] Vendredi FreeRTOS

04 67 59 92 53 2 Free RTOS

• Free Libre  «Gratuit»

Concept

septembre

• RT Real Time  Temps réel Lez 27

Vendredi FreeRTOS • OS Operating System  System Exploitation

3 Free RTOS

• Définition (http://www.freertos.org) :

« FreeRTOS is a portable, open source, royalty free,

mini Real Time Kernel ‐ a free to download and free to deploy RTOS that can be used in commercial Concept

septembre

applications without any requirement to expose your Lez 27

proprietary source code. » Vendredi FreeRTOS

4 FreeRTOS

• Obligations (licence GPL modifiée) : • Indiquer que le produit utilise FreeRTOS (un lien vers www..org est suffisant) • Indiquer la version de FreeRTOS

• Proposer de fournir le code source de FreeRTOS aux Concept utilisateurs de son application septembre

Lez 27 • Les changements dans le noyau sont open source

L'application doit être une vraie application «métier». Vendredi FreeRTOS

5 Principales fonctionnalités

• Temps réel : préemption, coopération • Petit (noyau 4Ko à 9Ko) • Facile à utiliser (globalement écrit en C) • Nombre « illimité » de tâches et de niveau de priorité

• Gestion flexible des priorités • Communication entre tâches ou entre interruptions et tâches : Concept

septembre

• Queues (échange de donnée) Lez 27 • Sémaphores (synchronisation)

• Mutexes (sémaphore avec héritage de priorité, partage de ressources) Vendredi FreeRTOS • Software timer • Détection de dépassement de la pile • Fonction Idle (priorité la plus basse) 6 • Trace Ce qui est livré (free)

• Le noyau complet sans librairie de I/O • Certaines fonctionnalités spécifique

Concept

septembre

Lez 27

Vendredi FreeRTOS

7 Ce qui n’est pas livré (free)

• Aucun driver spécifique • Usart • I2C

• SPI Concept •

Bus CAN septembre

Lez 27 • CAN

• Flash… Vendredi FreeRTOS

8 Ce qui existe (payant)

• Des librairies d’I/O • Des versions non free du noyaux (pas de licence GPL) • Des versions « durcis » et certifié du noyaux

• Des livres,

• Concept

De la formations….. septembre

Lez 27

Vendredi FreeRTOS

9 Dimensionnement

• FreeRTOS est conçu pour des microcontrôleurs pas pour des gros systèmes : • Aucun interface graphique prévu • Petit noyaux (quelques kilo octets)

• Pas de gestion de I/O disque (ATA,SCSI,SATA…) Concept • Pas de gestion des formatages (FAT, FS…) septembre

Lez 27 • …

Vendredi FreeRTOS

10 Plateforme opérationnelles

• Altera • NEC • • Microsemi (formally Actel) • Cortus • NXP

• Cypress • Renesas • Energy Micro • Concept

septembre

• Freescale • ST Microelectronics Lez 27

• Fujitsu • • Infineon • Xilinx Vendredi FreeRTOS • Luminary Micro • x86 (real mode) • Microchip 11 ARM

• Architecture d’unité centrale • Une constante évolution depuis 1987 • En 32 bits depuis 1987

• Bientôt des versions 64 bits Concept

septembre

Lez 27

Vendredi FreeRTOS

12 2 familles d’architectures

• Version microcontrôleur • Version microprocesseur • Préfixe M (M0 à M4) • Préfixe…A15Mp • I2C • MMU • SPI • FPU

• ADC • GPU Concept • … • … septembre

Lez 27

Vendredi FreeRTOS

13 STM32Mxxx

• Chez ST on trouve : • Des versions M0 à M4 référencées STM32F0xx à STM32F4xx • Les M0 à 48Mhz à M4 @ 180Mhz • De 32Ko à 1Mo de Flash

• De 4Ko à 256k de RAM Concept • De TSSOP 20 pins (M0) à BGA 176 (M4) septembre

Lez 27 • De 18 I/O (M0) à 168 I/O (M4)

• … Vendredi FreeRTOS

14 Outils de développement ARM • Eclipse • OpenOCD • GNU debugger

Concept

• GCC septembre

• Compilateur Lez 27

• C

• ASM

• C++ Vendredi

• Link FreeRTOS

• Make • Lib 15 • jtag Génération de code

Solution Libre • GCC • Solution payante • • Mentor IAR http://www.mentor.com/embedded‐ software/sourcery‐tools/sourcery‐ • KEIL Concept codebench/editions/lite‐edition/ septembre

• …. Lez 27 • Yagartoo

http://www.yagarto.org/ • … Vendredi FreeRTOS

16 Debug

• Eclipse • Environnement de développement • Super console • Gestion multi‐projet • OpenOCD Concept

• Open On Chip Debugger septembre

Lez

• Plugin Eclipse 27

• GNU debugger • Code sur la cible de gestion de debug Vendredi FreeRTOS

17 JTAG

• Le JTAG pour Joint Test Action Group est le nom de la norme IEEE 1149.1 intitulé « Standard Test Access Port and Boundary‐ Scan Architecture ». Le JTAG a été normalisé en 1990.

• Connectique standard: Concept • ICE 20 point (2x10) au pas de 2,54 septembre

Lez 27 • Micro 10 point (2x5) au pas de 1,27

• Custom possible Vendredi

• Config minimum FreeRTOS • GND • NRST • SWDIO • SWCLK 18 Sonde JTAG

• Le standard de l’industrie : FTDI2232C • 1 Port Jtag • 1 Port série

Concept

septembre

Lez 27

Vendredi FreeRTOS

19 Sonde JTAG

• Version propriétaire STM

Concept

septembre

Lez 27

Vendredi FreeRTOS

20 Carte eval

Concept

septembre

Lez 27

Vendredi FreeRTOS

21 Connectiques

Concept

septembre

Lez 27

Vendredi FreeRTOS

22 Libraires

• Chez ST (st.com) • STM32FxxxStandard periph.lib • Drivers • CMSI • Exemples • … Concept

septembre

Lez 27

• Drivers et couches d’abstraction matériel Vendredi FreeRTOS

23 Principe du multitâche

• (1) La tâche 1 est en cours d'exécution. • (2) Le noyau suspend la tâche 1 ... • ... et réactive (3) la tâche 2. • Pendant son exécution (4), la tâche 2 se réserve un périphérique pour son accès exclusif. Concept • (5) Le noyau suspend la tâche 2 ... septembre

• ... et réactive (6) la tâche 3. Lez 27 • La tâche 3 essaie d'accéder au même périphérique. Elle voit qu'il est déjà réservé. Elle ne peut pas continuer et se • suspend (7). • (8) le noyau réactive la tâche 1. Vendredi • Etc FreeRTOS • Le tick OS d'après la tâche 2 est en cours d'exécution (9). Elle termine son utilisation du périphérique et le • débloque. • Le tick OS d'après la tâche 3 est en cours d'exécution (10). Elle voit que le périphérique peut être accédé et l'utilise 24 • avant d'être suspendue par le noyau. FreeRTOS

• Lancement du noyau

Concept

septembre

Lez 27

Vendredi FreeRTOS

25 Lancement du noyau int main(void) { vTaskStartScheduler(); return 0; Concept

septembre

} Lez 27

Vendredi FreeRTOS

26 FreeRTOS

• Lancement du noyau

Concept

septembre

Lez 27

Vendredi FreeRTOS

27 Lancement de tache int main(void) { PPMOutputs_init(); xTaskCreate( LEDTask, (signed char *)"LED", Concept

septembre

configMINIMAL_STACK_SIZE, Lez 27 NULL,

tskIDLE_PRIORITY, NULL); Vendredi FreeRTOS vTaskStartScheduler(); return 0; 28 } Lancement de tache

• http://www.freertos.org/a00125.html portBASE_TYPE xTaskCreate( pdTASK_CODE pvTaskCode, const portCHAR * const pcName,

unsigned portSHORT usStackDepth, void *pvParameters, Concept

septembre

unsigned portBASE_TYPE uxPriority, Lez 27

xTaskHandle *pvCreatedTask ); Vendredi FreeRTOS

29 Lancement de tache

• pvTaskCode Pointer to the task entry function. Tasks must be implemented to never return (i.e. continuous loop). • pcName A descriptive name for the task. This is mainly used to facilitate debugging. Max length defined by configMAX_TASK_NAME_LEN. • usStackDepth

The size of the task stack specified as the number of variables the stack can hold ‐ not the number of bytes. For example, if the stack is 16 bits wide and usStackDepth is defined Concept as 100, 200 bytes will be allocated for stack storage. The stack depth multiplied by the stack septembre

width must not exceed the maximum value that can be contained in a variable of type size_t. Lez 27 • pvParameters Pointer that will be used as the parameter for the task being created. • uxPriority Vendredi The priority at which the task should run. Systems that include MPU support can optionally create tasks in a privileged (system) mode by setting bit portPRIVILEGE_BIT of the FreeRTOS priority parameter. For example, to create a privileged task at priority 2 the uxPriority parameter should be set to ( 2 | portPRIVILEGE_BIT ). • pvCreatedTask Used to pass back a handle by which the created task can be referenced. 30 FreeRTOS

• Lancement du noyau • Lancement de tache

Concept

septembre

Lez 27

Vendredi FreeRTOS

31 Tuer une tache

• vTaskDelete

• task. h • void vTaskDelete( xTaskHandle xTask );

Concept

septembre

xTask The handle of the task to be deleted. Lez 27

Passing NULL will cause the calling task to be deleted. Vendredi FreeRTOS

32 Tuer une tache void vOtherFunction( void ) { xTaskHandle xHandle;

// Create the task, storing the handle. Concept

septembre xTaskCreate( vTaskCode, "NAME", STACK_SIZE, NULL, Lez 27

tskIDLE_PRIORITY, &xHandle ); Vendredi FreeRTOS // Use the handle to delete the task. vTaskDelete( xHandle ); } 33 Une tache void LEDTask(void * pvArg) { //déclaration & initialisation RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); GPIO_InitTypeDef led; uint8_t test = 1;

led.GPIO_Mode = GPIO_Mode_OUT; led.GPIO_OType = GPIO_OType_PP;

led.GPIO_Pin = GPIO_Pin_4; Concept

septembre

GPIO_Init(GPIOA, &led); Lez 27 while(1)

{ Vendredi

if(test) GPIO_ResetBits(GPIOA, GPIO_Pin_4); FreeRTOS else GPIO_SetBits(GPIOA, GPIO_Pin_4); test ^= 1; vTaskDelay(100); //point de retour du scheduler dans 100ms } 34 } Une tache

• Cette structure en deux blocs permet: • Un premier bloc : • Il fait office de déclaration  réservation mémoire avec un compilateur standard, et sans appel système • Il fait office d’initialisation, des variable et périphériques nécessaires

• Le second bloc une boucle infinie Concept • C’est la partie de code utilisé à chaque appel du scheduler septembre

Lez 27 • vTaskDelay(xxxx) permet un retour au scheduler dans xxxx tic

• Le rappel par le scheduler sera fait à la suite de cette ligne de code Vendredi • Cette simplicité de concept permet : FreeRTOS • L’utilisation de compilateur standard • Aucune connaissance complexe de la programmation système n’est nécessaire 35 FreeRTOS

• Lancement du noyau • Lancement de tache • Tuer une tache

Concept

septembre

Lez 27

Vendredi FreeRTOS

36 Priorités?

• Le niveau de priorité permet de définir qui a accès aux • CPU • Ressources partagées • Le noyau (ou kernel, ou tick OS…) à la plus haute priorité

• L’idle à la priorité la plus basse, normalement zéro Concept

septembre

Lez 27

• En direct de FreeRTOS.com:

• Each task is assigned a priority from 0 to (configMAX_PRIORITIES ‐ Vendredi 1 ), where configMAX_PRIORITIES is defined within FreeRTOS FreeRTOSConfig.h and can be set on an application by application basis. • Low priority numbers denote low priority tasks. The idle task has 37 priority zero (tskIDLE_PRIORITY). Priorités?

Concept

septembre

Lez 27

Vendredi FreeRTOS

38 FreeRTOS

• Lancement du noyau • Lancement de tache • Tuer une tache • Priorité? Concept

septembre

Lez 27

Vendredi FreeRTOS

39 La mémoire

• FreeRTOS Sait gérer la mémoire • On peut (doit) définir la taille de la pile lors du lancement d’une tache. • Des fonctionnalités de gestion de dépassement de piles existent • Des possibilités de gestion d’allocations sont aussi implémentées Concept

septembre

Lez 27

Vendredi FreeRTOS

40 FreeRTOS

• Lancement du noyau • Lancement de tache • Tuer une tache • Priorité? Concept

septembre

• Gestion de la mémoire Lez 27

Vendredi FreeRTOS

41 Les interruptions

• Gestion des priorités du contrôleur matériel (NVIC) • Possibilité de masque en direct par niveau de priorité

• Masquage de TOUTES les IT pour un cours instant

• Test d’une variable interprocessus et set ou clear Concept

septembre

Lez 27 • Fonctions spécifiques (xxxxFromISR) appelable d’un routine it matériel Vendredi FreeRTOS • Gestion de l’it timer pour le tic system

42 • Toute la gestion des it est TRES dépendante du chip utilisé FreeRTOS

• Lancement du noyau • Lancement de tache • Tuer une tache • Priorité? Concept

septembre

• Gestion de la mémoire Lez 27

• Gestion des interruptions Vendredi FreeRTOS

43 binarysemaphores

• Un binary semaphore est mis à 1 par une tache • Il sera mis à 0 par une autre tache ou par le noyau

Concept

septembre

Lez 27

Vendredi FreeRTOS

44 binarysemaphores

• A la création un binarysemaphore est disponible. xSemaphoreHandle xSemaphore; void vATask( void * pvParameters )

{ // Semaphore cannot be used before a call to vSemaphoreCreateBinary (). Concept

// This is a macro so pass the variable in directly. septembre

Lez

vSemaphoreCreateBinary( xSemaphore ); 27

if( xSemaphore != NULL ) Vendredi

{ FreeRTOS // The semaphore was created successfully. // The semaphore can now be used. } } 45 binarysemaphores

• Prise d’un binarysemaphore … if( xSemaphore != NULL ) {

// See if we can obtain the semaphore. If the semaphore is not available // wait 10 ticks to see if it becomes free. Concept if( xSemaphoreTake( xSemaphore, ( portTickType ) 10 ) == pdTRUE ) septembre

Lez 27

{

// on a obtenu xSemaphore

} Vendredi

else FreeRTOS { // time overflow, xSemaphore n’est pas disponible } } 46 binarysemaphores

• Libération d’un binarysemaphore … if( xSemaphoreGive( xSemaphore ) != pdTRUE )

{ // nous sommes ici sans avoir à liberer xSemaphore…. Concept

septembre

} Lez 27

Vendredi FreeRTOS

47 binarysemaphores

• Libération d’un binarysemaphore sous it xSemaphoreGiveFromISR ( xSemaphoreHandle xSemaphore, signed portBASE_TYPE *pxHigherPriorityTaskWoken) Concept

septembre

Lez 27

Vendredi FreeRTOS

48 FreeRTOS

• Lancement du noyau • Lancement de tache • Tuer une tache • Priorité? Concept

septembre

• Gestion de la mémoire Lez 27

• Gestion des interruptions Vendredi

• Semaphore FreeRTOS

49 Mutex

• Outils de partage de ressource

Concept

septembre

Lez 27

• Une tache a besoin d’une ressource elle tente de prendre le Vendredi FreeRTOS mutex, elle l’obtient • Une seconde tache a besoin de la ressource, elle tente de prendre la ressource, elle ne l’obtient pas 50 • S’utilise comme un semaphore Mutex héritage de priorité

• Si la seconde tache est plus prioritaire : • Elle reste en attente • Elle fait hériter la ressource de son niveau de priorité pour moins attendre

Concept • La tache en attente garde son niveau de priorité initial septembre

Lez 27

Vendredi FreeRTOS

51 Mutex

xSemaphoreHandle xSemaphore; void vATask( void * pvParameters ) { // Mutex semaphores cannot be used before a call to

// xSemaphoreCreateMutex(). The created mutex is returned. Concept xSemaphore = xSemaphoreCreateMutex(); septembre

Lez 27 if( xSemaphore != NULL )

{ Vendredi

// The semaphore was created successfully. FreeRTOS // The semaphore can now be used. } } 52 FreeRTOS

• Lancement du noyau • Lancement de tache • Tuer une tache • Priorité? Concept

septembre

• Gestion de la mémoire Lez 27

• Gestion des interruptions Vendredi

• Semaphore FreeRTOS • Mutex

53 FAT SL

• http://www.freertos.org/FreeRTOS‐ Plus/FreeRTOS_Plus_FAT_SL/FreeRTOS_Plus_FAT_SL.shtml • sous licence GPL V2

Standard File System API f_initvolume() f_delvolume() f_format() Concept

septembre

f_getfreespace() f_findfirst() f_findnext() Lez 27

f_getserial() f_mkdir() f_chdir()

f_rmdir() f_getcwd() f_open() Vendredi

f_close() f_flush() f_write() FreeRTOS f_read() f_seek() f_tell() f_rewind() f_putc() f_getc() f_seteof() f_truncate() f_eof() 54 f_delete() f_filelength() FATfs

• http://elm‐chan.org/fsw/ff/00index_e.html • 100% libre • Non spécifique FreeRTOS

• Non spécifique à une plateforme matériel Concept

septembre

Lez 27

Vendredi FreeRTOS

55 FAT

TACHE 1TACHE 2 TACHE 3

Gestion de la ressource Concept

septembre

Lez 27

Système FAT

Gestion protocole SPI Vendredi FreeRTOS

Gestion hard SPI

Carte SD 56 FreeRTOS

• Lancement du noyau • Lancement de tache • Tuer une tache • Priorité? Concept

septembre

• Gestion de la mémoire Lez 27

• Gestion des interruptions Vendredi

• Semaphore FreeRTOS • Mutex • Mémoire de masse 57 Ethernet

• De très (trop) nombreuses solutions

Concept

septembre

Lez 27

• La bonne solution dépend du projet et de Vendredi l’environnement FreeRTOS

58 Questions?

Concept

septembre

Lez 27

Vendredi FreeRTOS

59 Questions?

Lez Concept 15 chemin neuf 34 980 Montferrier Concept

septembre

Lez 27

[email protected] Vendredi FreeRTOS

04 67 59 92 53 60 61 FreeRTOS Lez Concept Vendredi 27 septembre