Skip to content

Commit b2351b0

Browse files
committed
Default configASSERT macro
1 parent efc1c28 commit b2351b0

File tree

4 files changed

+129
-58
lines changed

4 files changed

+129
-58
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ script:
1212
- platformio ci --lib="." --board=uno --board=leonardo --board=sanguino_atmega1284p --board=megaatmega2560 examples/AnalogRead_DigitalRead/AnalogRead_DigitalRead.ino
1313
- platformio ci --lib="." --board=uno --board=leonardo --board=sanguino_atmega1284p --board=megaatmega2560 examples/Blink_AnalogRead/Blink_AnalogRead.ino
1414
- platformio ci --lib="." --board=uno --board=leonardo --board=sanguino_atmega1284p --board=megaatmega2560 examples/TaskUtilities/TaskUtilities.ino
15+
- platformio ci --lib="." --board=uno --board=leonardo --board=sanguino_atmega1284p --board=megaatmega2560 examples/Assert/Assert.ino

examples/Assert/Assert.ino

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Example of FreeRTOS configASSERT macro
3+
* https://www.freertos.org/a00110.html#configASSERT
4+
*/
5+
6+
#include <Arduino_FreeRTOS.h>
7+
8+
const boolean valueToAssert = true;
9+
10+
// The setup function runs once when you press reset or power the board
11+
void setup() {
12+
13+
// Assert value is true, execution doesn't stop.
14+
configASSERT(valueToAssert == true);
15+
16+
// Assert value is false, FreeRTOS execution stops and start to blink main led two times with 4 second cycle.
17+
configASSERT(valueToAssert == false);
18+
}
19+
20+
21+
void loop()
22+
{
23+
// Empty. Things are done in Tasks.
24+
}

src/FreeRTOSConfig.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,28 @@ to exclude the API function. */
106106
#define configMAX(a,b) ({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _a > _b ? _a : _b; })
107107
#define configMIN(a,b) ({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _a < _b ? _a : _b; })
108108

109+
/**
110+
* configASSERT macro: https://www.freertos.org/a00110.html#configASSERT
111+
*/
112+
#ifndef configASSERT
113+
/**
114+
* Enable configASSERT macro by default if it is not defined.
115+
*/
116+
#ifndef configDEFAULT_ASSERT
117+
#define configDEFAULT_ASSERT 1
118+
#endif
119+
120+
/**
121+
* Define a hook method for configASSERT macro if configASSERT is enabled.
122+
*/
123+
#if configDEFAULT_ASSERT == 1
124+
extern void vApplicationAssertHook();
125+
#define configASSERT( x ) if (( x ) == 0) { vApplicationAssertHook(); }
126+
#endif
127+
128+
#else
129+
#define configDEFAULT_ASSERT 0
130+
#endif
131+
132+
109133
#endif /* FREERTOS_CONFIG_H */

src/variantHooks.cpp

Lines changed: 80 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -82,27 +82,14 @@ void vApplicationIdleHook( void )
8282
#endif /* configUSE_IDLE_HOOK == 1 */
8383
/*-----------------------------------------------------------*/
8484

85+
#if ( configUSE_MALLOC_FAILED_HOOK == 1 || configCHECK_FOR_STACK_OVERFLOW >= 1 || configDEFAULT_ASSERT == 1 )
8586

86-
#if ( configUSE_MALLOC_FAILED_HOOK == 1 )
87-
/*---------------------------------------------------------------------------*\
88-
Usage:
89-
called by task system when a malloc failure is noticed
90-
Description:
91-
Malloc failure handler -- Shut down all interrupts, send serious complaint
92-
to command port. FAST Blink on main LED.
93-
Arguments:
94-
pxTask - pointer to task handle
95-
pcTaskName - pointer to task name
96-
Results:
97-
<none>
98-
Notes:
99-
This routine will never return.
100-
This routine is referenced in the task.c file of FreeRTOS as an extern.
101-
\*---------------------------------------------------------------------------*/
102-
void vApplicationMallocFailedHook( void ) __attribute__((weak));
103-
104-
void vApplicationMallocFailedHook( void )
87+
/**
88+
* Private function to enable board led to use it in application hooks
89+
*/
90+
void prvSetMainLedOn( void )
10591
{
92+
10693
#if defined(__AVR_ATmega640__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) // Arduino Mega with 2560
10794
DDRB |= _BV(DDB7);
10895
PORTB |= _BV(PORTB7); // Main (red PB7) LED on. Main LED on.
@@ -121,24 +108,60 @@ void vApplicationMallocFailedHook( void )
121108

122109
#endif
123110

124-
for(;;)
125-
{
126-
_delay_ms(50);
111+
}
112+
113+
/**
114+
* Private function to blink board led to use it in application hooks
115+
*/
116+
void prvBlinkMainLed( void )
117+
{
127118

128119
#if defined(__AVR_ATmega640__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) // Mega with 2560
129-
PINB |= _BV(PINB7); // Main (red PB7) LED toggle. Main LED fast blink.
120+
PINB |= _BV(PINB7); // Main (red PB7) LED toggle.
130121

131122
#elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega1284PA__) // Seeed Goldilocks with 1284p
132-
PINB |= _BV(PINB7); // Main (red PB7) LED toggle. Main LED fast blink.
123+
PINB |= _BV(PINB7); // Main (red PB7) LED toggle.
133124

134125
#elif defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__) // assume we're using an Arduino Uno with 328p
135-
PINB |= _BV(PINB5); // Main (red PB5) LED toggle. Main LED fast blink.
126+
PINB |= _BV(PINB5); // Main (red PB5) LED toggle.
136127

137128
#elif defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega16U4__) // assume we're using an Arduino Leonardo with 32u4
138-
PINC |= _BV(PINC7); // Main (red PC7) LED toggle. Main LED fast blink.
129+
PINC |= _BV(PINC7); // Main (red PC7) LED toggle.
130+
131+
#endif
132+
133+
}
139134

140135
#endif
141136

137+
#if ( configUSE_MALLOC_FAILED_HOOK == 1 )
138+
/*---------------------------------------------------------------------------*\
139+
Usage:
140+
called by task system when a malloc failure is noticed
141+
Description:
142+
Malloc failure handler -- Shut down all interrupts, send serious complaint
143+
to command port. FAST Blink on main LED.
144+
Arguments:
145+
pxTask - pointer to task handle
146+
pcTaskName - pointer to task name
147+
Results:
148+
<none>
149+
Notes:
150+
This routine will never return.
151+
This routine is referenced in the task.c file of FreeRTOS as an extern.
152+
\*---------------------------------------------------------------------------*/
153+
void vApplicationMallocFailedHook( void ) __attribute__((weak));
154+
155+
void vApplicationMallocFailedHook( void )
156+
{
157+
prvSetMainLedOn(); // Main LED on.
158+
159+
for(;;)
160+
{
161+
_delay_ms(50);
162+
163+
prvBlinkMainLed(); // Main LED fast blink.
164+
142165
}
143166
}
144167

@@ -166,42 +189,12 @@ void vApplicationStackOverflowHook( TaskHandle_t xTask, char *pcTaskName ) __att
166189

167190
void vApplicationStackOverflowHook( TaskHandle_t xTask __attribute__((unused)), char *pcTaskName __attribute__((unused)) )
168191
{
169-
#if defined(__AVR_ATmega640__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) // Arduino Mega with 2560
170-
DDRB |= _BV(DDB7);
171-
PORTB |= _BV(PORTB7); // Main (red PB7) LED on. Main LED on.
172-
173-
#elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega1284PA__) // Seeed Goldilocks with 1284p
174-
DDRB |= _BV(DDB7);
175-
PORTB |= _BV(PORTB7); // Main (red PB7) LED on. Main LED on.
176-
177-
#elif defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__) // assume we're using an Arduino Uno with 328p
178-
DDRB |= _BV(DDB5);
179-
PORTB |= _BV(PORTB5); // Main (red PB5) LED on. Main LED on.
180-
181-
#elif defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega16U4__) // assume we're using an Arduino Leonardo with 32u4
182-
DDRC |= _BV(DDC7);
183-
PORTC |= _BV(PORTC7); // Main (red PC7) LED on. Main LED on.
184-
185-
#endif
192+
prvSetMainLedOn(); // Main LED on.
186193

187194
for(;;)
188195
{
189196
_delay_ms(2000);
190-
191-
#if defined(__AVR_ATmega640__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) // Arduino Mega with 2560
192-
PINB |= _BV(PINB7); // Main (red PB7) LED toggle. Main LED slow blink.
193-
194-
#elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega1284PA__) // Seeed Goldilocks with 1284p
195-
PINB |= _BV(PINB7); // Main (red PB7) LED toggle. Main LED slow blink.
196-
197-
#elif defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__) // assume we're using an Arduino Uno with 328p
198-
PINB |= _BV(PINB5); // Main (red PB5) LED toggle. Main LED slow blink.
199-
200-
#elif defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega16U4__) // assume we're using an Arduino Leonardo with 32u4
201-
PINC |= _BV(PINC7); // Main (red PC7) LED toggle. Main LED slow blink.
202-
203-
#endif
204-
197+
prvBlinkMainLed(); // Main LED slow blink.
205198
}
206199
}
207200

@@ -247,3 +240,32 @@ void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer,
247240
#endif /* configUSE_TIMERS >= 1 */
248241

249242
#endif /* configSUPPORT_STATIC_ALLOCATION >= 1 */
243+
244+
/**
245+
* configASSERT default implementation
246+
*/
247+
#if configDEFAULT_ASSERT == 1
248+
249+
void vApplicationAssertHook() {
250+
251+
taskDISABLE_INTERRUPTS(); // Disable task interrupts
252+
253+
prvSetMainLedOn(); // Main LED on.
254+
for(;;)
255+
{
256+
_delay_ms(100);
257+
prvBlinkMainLed(); // Led off.
258+
259+
_delay_ms(2000);
260+
261+
prvBlinkMainLed(); // Led on.
262+
_delay_ms(100);
263+
264+
prvBlinkMainLed(); // Lef off
265+
_delay_ms(100);
266+
267+
prvBlinkMainLed(); // Led on.
268+
269+
}
270+
}
271+
#endif

0 commit comments

Comments
 (0)