Skip to content

Commit 536fba8

Browse files
committed
further delay() revision for #114
1 parent d79ee96 commit 536fba8

File tree

5 files changed

+27
-19
lines changed

5 files changed

+27
-19
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=FreeRTOS
2-
version=10.4.6-5
2+
version=10.4.6-6
33
author=Richard Barry <[email protected]>
44
maintainer=Phillip Stevens <[email protected]>
55
sentence=<h3>FreeRTOS Real Time Operating System implemented for AVR (Uno, Nano, Leonardo, Mega).</h3>

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Watchdog period options:
3636

3737
Note that Timer resolution (or granularity) is affected by integer math division and the time slice selected. Trying to measure 50ms, using a 120ms time slice for example, won't work.
3838

39-
The Arduino `delay()` function has been redefined to automatically use the FreeRTOS `vTaskDelay()` function when the delay required is one Tick or longer, so that simple Arduino example sketches and tutorials work as expected. If you would like to measure a short millisecond delay of less than one Tick, then preferably use [`millis()`](https://www.arduino.cc/reference/en/language/functions/time/millis/) (or with greater granularity use [`micros()`](https://www.arduino.cc/reference/en/language/functions/time/micros/)) to achieve this outcome (for example see [BlinkWithoutDelay](https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay)). However, when the delay requested is less than one Tick then the original Arduino `delay()` function will be automatically selected.
39+
The Arduino `delay()` function has been redefined to automatically use the FreeRTOS `vTaskDelay()` function when the delay required is one Tick or longer, by setting `configUSE_PORT_DELAY` to `1`, so that simple Arduino example sketches and tutorials work as expected. If you would like to measure a short millisecond delay of less than one Tick, then preferably use [`millis()`](https://www.arduino.cc/reference/en/language/functions/time/millis/) (or with greater granularity use [`micros()`](https://www.arduino.cc/reference/en/language/functions/time/micros/)) to achieve this outcome (for example see [BlinkWithoutDelay](https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay)). However, when the delay requested is less than one Tick then the original Arduino `delay()` function will be automatically selected.
4040

4141
The 8-bit AVR Timer0 has been added as an option for the experienced user. Please examine the source code to figure out how to use it. Reconfiguring Timer0 for FreeRTOS will break Arduino `millis()` and `micros()` though, as these functions rely on Timer0.
4242

src/FreeRTOSConfig.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,9 @@
4343
* See https://www.freertos.org/a00110.html.
4444
*----------------------------------------------------------*/
4545

46-
// And on to the things the same no matter the AVR type...
46+
/* And on to the things the same no matter the AVR type... */
4747
#define configUSE_PREEMPTION 1
4848

49-
// Define configUSE_IDLE_HOOK
50-
#ifndef configUSE_IDLE_HOOK
51-
#define configUSE_IDLE_HOOK 1
52-
#endif
53-
54-
#define configUSE_TICK_HOOK 0
5549
#define configCPU_CLOCK_HZ ( ( uint32_t ) F_CPU ) // This F_CPU variable set by the environment
5650
#define configMAX_PRIORITIES 4
5751
#define configIDLE_SHOULD_YIELD 1
@@ -74,6 +68,13 @@
7468
#define configSUPPORT_DYNAMIC_ALLOCATION 1
7569
#define configSUPPORT_STATIC_ALLOCATION 0
7670

71+
#define configUSE_IDLE_HOOK 1
72+
#define configUSE_TICK_HOOK 0
73+
74+
/* Delay definition - here, the user can choose which delay implementation is required.
75+
* The default is to change nothing. */
76+
#define configUSE_PORT_DELAY 1
77+
7778
/* Timer definitions. */
7879
#define configUSE_TIMERS 1
7980
#define configTIMER_TASK_PRIORITY configMAX_PRIORITIES-1

src/port.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
/* Start tasks with interrupts enabled. */
4545
#define portFLAGS_INT_ENABLED ( (StackType_t) 0x80 )
4646

47-
#if defined( portUSE_WDTO)
47+
#if defined( portUSE_WDTO )
4848
#define portSCHEDULER_ISR WDT_vect
4949

5050
#elif defined( portUSE_TIMER0 )
@@ -90,7 +90,7 @@ extern volatile TCB_t * volatile pxCurrentTCB;
9090
Updated to match avr-libc 2.0.0
9191
*/
9292

93-
#if defined( portUSE_WDTO)
93+
#if defined( portUSE_WDTO )
9494

9595
static __inline__
9696
__attribute__ ((__always_inline__))
@@ -156,7 +156,7 @@ void wdt_interrupt_enable (const uint8_t value)
156156
Updated to match avr-libc 2.0.0
157157
*/
158158

159-
#if defined( portUSE_WDTO)
159+
#if defined( portUSE_WDTO )
160160

161161
static __inline__
162162
__attribute__ ((__always_inline__))
@@ -649,11 +649,13 @@ void vPortEndScheduler( void )
649649
#undef delay
650650
#endif
651651

652+
extern void delay ( unsigned long ms );
653+
652654
void vPortDelay( const uint32_t ms ) __attribute__ ((hot, flatten));
653655
void vPortDelay( const uint32_t ms )
654656
{
655657
if ( ms < portTICK_PERIOD_MS )
656-
delay( ms );
658+
delay( (unsigned long) (ms) );
657659
else
658660
vTaskDelay( (TickType_t) (ms) / portTICK_PERIOD_MS );
659661
}
@@ -710,7 +712,7 @@ void vPortYieldFromTick( void )
710712
}
711713
/*-----------------------------------------------------------*/
712714

713-
#if defined(portUSE_WDTO)
715+
#if defined( portUSE_WDTO )
714716
/*
715717
* Setup WDT to generate a tick interrupt.
716718
*/
@@ -723,7 +725,7 @@ void prvSetupTimerInterrupt( void )
723725
wdt_interrupt_enable( portUSE_WDTO );
724726
}
725727

726-
#elif defined (portUSE_TIMER0)
728+
#elif defined( portUSE_TIMER0 )
727729
/*
728730
* Setup Timer0 compare match A to generate a tick interrupt.
729731
*/

src/portable.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,18 @@
8585
#define portARCH_NAME NULL
8686
#endif
8787

88+
#if configUSE_PORT_DELAY == 1
8889
/*
89-
* Delay is implemented as either Arduino delay() if the delay is less than one Tick,
90-
* or otherwise FreeRTOS vTaskDelay().
91-
*
90+
* If configUSE_PORT_DELAY is defined,
91+
* delay() is implemented as either the Arduino delay()
92+
* if the delay is less than one Tick, or
93+
* otherwise as FreeRTOS vTaskDelay() for longer delays.
9294
*/
95+
9396
#ifndef delay
94-
#define delay vPortDelay
97+
#define delay vPortDelay
98+
#endif
99+
95100
#endif
96101

97102
#ifndef configSTACK_ALLOCATION_FROM_SEPARATE_HEAP

0 commit comments

Comments
 (0)