STM32F429 Discovery + FreeRTOS + Interrupts - Page 1
Forum section: electronics / general

author: damiandoles
author_rank: Newbie
timestamp: November 06, 2016, 03:49:03 pm
content: Hi all,I'm beginner at FreeRTOS and any OS programming. I have some problems with configuring/using any Cortex interrupts. I want to use exti0 line which is connected to PA0 (User button on Discovery Kit). When I push the button, uC have HardFault_Handler. I am reading about interrupts, but I still don't know what should be done. In theory it is understandable, but in practise any change in code cause the same reaction - HardFault. I read about preemption priorities and subpriorities. In Cortex-M4 we have 255 maskable interrupts - 4 bits for preemptions and 4 bits for subpriorities, which mean preemptions from 0x0 to 0xF and the same range for subpriorities. I don't understand kernel interrupts. FreeRTOS uses Systick, PendSV and PortSVC - this is the most important priorities (kernel OS). I read that, other interrupts (wrote by programmer, for example usart, timers, exti) must be lower than kernel interrupts. It means that the value of interrupt priority should be higher than kernel interrupts - have I right ? I don't have any ideas for resolve this problem, and I don't know what am I doing wrong.Code:int main (void){vHardwareSetup();vStartLEDTasks();vTaskStartScheduler();return 0;}static void vhButton_Init(void){RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);GPIO_InitTypeDef GPIO_InitDef;GPIO_InitDef.GPIO_Pin = BUTTON_PIN;GPIO_InitDef.GPIO_OType = GPIO_OType_PP;GPIO_InitDef.GPIO_Mode = GPIO_Mode_IN;GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_NOPULL;GPIO_InitDef.GPIO_Speed = GPIO_Speed_100MHz;//Initialize pinsGPIO_Init(BUTTON_GPIO, &GPIO_InitDef);RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);EXTI_InitTypeDef EXTI_InitDef;EXTI_InitDef.EXTI_Line = EXTI_Line0;EXTI_InitDef.EXTI_Mode = EXTI_Mode_Interrupt;EXTI_InitDef.EXTI_Trigger = EXTI_Trigger_Rising;EXTI_InitDef.EXTI_LineCmd = ENABLE;EXTI_Init(&EXTI_InitDef);NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );NVIC_InitTypeDef NVIC_EXTI_InitDef;NVIC_EXTI_InitDef.NVIC_IRQChannel = EXTI0_IRQn;NVIC_EXTI_InitDef.NVIC_IRQChannelPreemptionPriority = 0x0D;NVIC_EXTI_InitDef.NVIC_IRQChannelSubPriority = 0x00;NVIC_EXTI_InitDef.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_EXTI_InitDef);}void EXTI0_IRQHandler(void){long xHigherPriorityTaskWoken = pdFALSE;if(EXTI_GetITStatus(EXTI_Line0) != RESET){vhToggleLED2();EXTI_ClearITPendingBit(EXTI_Line0);}portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );}/*FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.All rights reservedVISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.This file is part of the FreeRTOS distribution.FreeRTOS is free software; you can redistribute it and/or modify it underthe terms of the GNU General Public License (version 2) as published by theFree Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.***************************************************************************>>! NOTE: The modification to the GPL is included to allow you to !<<>>! distribute a combined work that includes FreeRTOS without being !<<>>! obliged to provide the source code for proprietary components !<<>>! outside of the FreeRTOS kernel. !<<***************************************************************************FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANYWARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESSFOR A PARTICULAR PURPOSE. Full license text is available on the followinglink: http://www.freertos.org/a00114.html**************************************************************************** ** FreeRTOS provides completely free yet professionally developed, ** robust, strictly quality controlled, supported, and cross ** platform software that is more than just the market leader, it ** is the industry's de facto standard. ** ** Help yourself get started quickly while simultaneously helping ** to support the FreeRTOS project by purchasing a FreeRTOS ** tutorial book, reference manual, or both: ** http://www.FreeRTOS.org/Documentation ** ****************************************************************************http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by readingthe FAQ page "My application does not run, what could be wrong?". Have youdefined configASSERT()?http://www.FreeRTOS.org/support - In return for receiving this top qualityembedded software for free we request you assist our global community byparticipating in the support forum.http://www.FreeRTOS.org/training - Investing in training allows your team tobe as productive as possible as early as possible. Now you can receiveFreeRTOS training directly from Richard Barry, CEO of Real Time EngineersLtd, and the world's leading authority on the world's leading RTOS.http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,including FreeRTOS+Trace - an indispensable productivity tool, a DOScompatible FAT file system, and our tiny thread aware UDP/IP stack.http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to HighIntegrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOSlicenses offer ticketed support, indemnification and commercial middleware.http://www.SafeRTOS.com - High Integrity Systems also provide a safetyengineered and independently SIL3 certified version for use in safety andmission critical applications that require provable dependability.1 tab == 4 spaces!*/#ifndef FREERTOS_CONFIG_H#define FREERTOS_CONFIG_H/*-----------------------------------------------------------* Application specific definitions.** These definitions should be adjusted for your particular hardware and* application requirements.** THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE* FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE.** See http://www.freertos.org/a00110.html.*----------------------------------------------------------*//* Ensure stdint is only used by the compiler, and not the assembler. */#ifdef __ICCARM__#include <stdint.h>extern uint32_t SystemCoreClock;#endif#include "system_stm32f4xx.h"#include "stm32f4xx.h"#define configUSE_PREEMPTION1#define configUSE_IDLE_HOOK1#define configUSE_TICK_HOOK0#define configCPU_CLOCK_HZ( SystemCoreClock )#define configTICK_RATE_HZ( ( TickType_t ) 1000 )#define configMAX_PRIORITIES( 5 )#define configMINIMAL_STACK_SIZE( ( unsigned short ) 200 )#define configTOTAL_HEAP_SIZE( ( size_t ) ( 75 * 1024 ) )#define configMAX_TASK_NAME_LEN( 10 )#define configUSE_TRACE_FACILITY1#define configUSE_16_BIT_TICKS0#define configIDLE_SHOULD_YIELD1#define configUSE_MUTEXES1#define configQUEUE_REGISTRY_SIZE8#define configCHECK_FOR_STACK_OVERFLOW2#define configUSE_RECURSIVE_MUTEXES1#define configUSE_MALLOC_FAILED_HOOK1#define configUSE_APPLICATION_TASK_TAG0#define configUSE_COUNTING_SEMAPHORES1#define configGENERATE_RUN_TIME_STATS0/* Co-routine definitions. */#define configUSE_CO_ROUTINES0#define configMAX_CO_ROUTINE_PRIORITIES ( 2 )/* Software timer definitions. */#define configUSE_TIMERS1#define configTIMER_TASK_PRIORITY( 2 )#define configTIMER_QUEUE_LENGTH10#define configTIMER_TASK_STACK_DEPTH( configMINIMAL_STACK_SIZE * 2 )/* Set the following definitions to 1 to include the API function, or zeroto exclude the API function. */#define INCLUDE_vTaskPrioritySet1#define INCLUDE_uxTaskPriorityGet1#define INCLUDE_vTaskDelete1#define INCLUDE_vTaskCleanUpResources1#define INCLUDE_vTaskSuspend1#define INCLUDE_vTaskDelayUntil1#define INCLUDE_vTaskDelay1/* Cortex-M specific definitions. */#ifdef __NVIC_PRIO_BITS/* __BVIC_PRIO_BITS will be specified when CMSIS is being used. */#define configPRIO_BITS__NVIC_PRIO_BITS#else#define configPRIO_BITS4 /* 15 priority levels */#endif//#define configLIBRARY_LOWEST_INTERRUPT_PRIORITY15//#define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY11//#define configLIBRARY_KERNEL_INTERRUPT_PRIORITY15///* The lowest interrupt priority that can be used in a call to a "set priority"//function. *///#define configLIBRARY_LOWEST_INTERRUPT_PRIORITY0xf/////* The highest interrupt priority that can be used by any interrupt service//routine that makes calls to interrupt safe FreeRTOS API functions. DO NOT CALL//INTERRUPT SAFE FREERTOS API FUNCTIONS FROM ANY INTERRUPT THAT HAS A HIGHER//PRIORITY THAN THIS! (higher priorities are lower numeric values. *///#define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY5/////* Interrupt priorities used by the kernel port layer itself. These are generic//to all Cortex-M ports, and do not rely on any particular library functions. *///#define configKERNEL_INTERRUPT_PRIORITY( configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )///* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!!//See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. *///#define configMAX_SYSCALL_INTERRUPT_PRIORITY( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )/* This is the raw value as per the Cortex-M3 NVIC. Values can be 255(lowest) to 0 (1?) (highest). */#define configKERNEL_INTERRUPT_PRIORITY255/* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!!See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */#define configMAX_SYSCALL_INTERRUPT_PRIORITY191 /* equivalent to 0xb0, or priority 11. *//* This is the value being used as per the ST library which permits 16priority values, 0 to 15. This must correspond to theconfigKERNEL_INTERRUPT_PRIORITY setting. Here 15 corresponds to the lowestNVIC value of 255. */#define configLIBRARY_KERNEL_INTERRUPT_PRIORITY15/* Normal assert() semantics without relying on the provision of an assert.hheader file. */#define configASSERT( x ) if( ( x ) == 0 ) { taskDISABLE_INTERRUPTS(); for( ;; ); }/* Definitions that map the FreeRTOS port interrupt handlers to their CMSISstandard names. */#define vPortSVCHandler SVC_Handler#define xPortPendSVHandler PendSV_Handler#define xPortSysTickHandler SysTick_Handler#endif /* FREERTOS_CONFIG_H */As you see int FreeRTOSConfig.h I tried with different priorities configurations but nothing resolved my problem. Thank you in advance for your help

author: RichardBarry
author_rank: Newbie
timestamp: November 07, 2016, 03:15:22 pm
content: Your configuration looks ok (there is a lot of information on running FreeRTOS on Cortex-Mhere), although I have not studied it in detail, but I don't think your problem is FreeRTOS related. One of the reasons I say that is, although your interrupt service routine is calling portEND_SWITCHING_ISR(), it hasn't actually called any FreeRTOS API functions, so xHigherPriorityTaskWoken will still be pdFALSE (I'm surprised your compiler does not warn you about testing a value that will always be false) so portEND_SWITCHING_ISR() will not do anything. In other words, your interrupt service routine is not using any FreeRTOS functionality.Have you installed a handler for the interrupt?Have you got the interrupt working in a basic "hello world" dummy application that does not use FreeRTOS?Are you using a recent version of FreeRTOS withconfigASSERT()defined - the newer the version the more assert points there are and the more help you will get debugging any FreeRTOS configuration problems.

author: Frank
author_rank: Contributor
timestamp: November 07, 2016, 04:05:19 pm
content: Did you run the FreeRTOS demo app for this target? Everything works?With the demo running correctly, you may try to process some other interrupts, like timers or uart. There are a lot of examples for these.Code:#define configMINIMAL_STACK_SIZE( ( unsigned short ) 200 )#define configTOTAL_HEAP_SIZE( ( size_t ) ( 75 * 1024 ) )this looks like a small stack size and a big heap size... Are these the recommended values for this microcontroller?

author: Kremmen
author_rank: Super Contributor
timestamp: November 07, 2016, 09:50:17 pm
content: Also you should possibly not use SysTick as the FreeRTOS scheduler clock. Due to priority issues it will cause deadlocks in the kernel.UM1718 - STM32CubeMX user manual has this to say about the issue:“By default, the STM32Cube HAL is built around a unique timebase source which is the ARM-Cortex system timer (SysTick).However, HAL-timebase related functions are defined as weak so that they can be overloaded to use another hardware timebase source. This is strongly recommended when the application uses an RTOS, since this middleware has full control on the SysTick configuration (tick and priority) and most RTOSs force the SysTick priority to be the lowest.Using the SysTick remains acceptable if the application respects the HAL programming model, that is, does not perform any call to HAL timebase services within an Interrupt Service Request context (no dead lock issue).To change the HAL timebase source, go to the SYS peripheral in the IP tree pane and select a clock among the available clock sources: SysTick, TIM1, TIM2,...”