Skip to content

Commit 6a4f3b7

Browse files
committed
Move util functions to RTOS class.
Former-commit-id: f58187e
1 parent 0ca8de1 commit 6a4f3b7

File tree

10 files changed

+148
-41
lines changed

10 files changed

+148
-41
lines changed

README.md

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ If you are interested in assisting the development of this project please submit
1818

1919
I wanted to have a consistent BLE API on all of the devices I work with. NimBLE is the best choice for this as it is the most feature complete and fully open source library available for Arduino.
2020

21+
## BLE
22+
This Arduino Core does **not** contain any BLE functionality. It has been designed to support using the [NimBLE-Arduino](https://github.com/h2zero/NimBLE-Arduino) library for BLE operation.
23+
**Note:** Only the release version 1.4.0 and above of [NimBLE-Arduino](https://github.com/h2zero/NimBLE-Arduino) supports Arm devices.
24+
2125
## Supported boards
2226

2327
### nRF52840
@@ -52,7 +56,7 @@ I wanted to have a consistent BLE API on all of the devices I work with. NimBLE
5256
* [BBC micro:bit](https://microbit.org)
5357
* [Calliope mini](https://calliope.cc/en)
5458
* [Bluz DK](http://bluz.io)
55-
* Nordic Semiconductor [nRF51822 Development Kit](https://www.nordicsemi.com/eng/Products/Bluetooth-low-energy/nRF51822-Development-Kit)
59+
* [Nordic Semiconductor nRF51822 Development Kit](https://www.nordicsemi.com/eng/Products/Bluetooth-low-energy/nRF51822-Development-Kit)
5660
* [Nordic Semiconductor NRF51 Smart Beacon Kit](https://www.nordicsemi.com/eng/Products/Bluetooth-low-energy/nRF51822-Bluetooth-Smart-Beacon-Kit)
5761
* [Nordic Semiconductor NRF51 Dongle](http://www.nordicsemi.com/eng/Products/nRF51-Dongle)
5862
* [OSHChip](http://www.oshchip.org/)
@@ -66,7 +70,6 @@ I wanted to have a consistent BLE API on all of the devices I work with. NimBLE
6670
## Installing
6771

6872
### Board Manager
69-
7073
1. [Download and install the Arduino IDE](https://www.arduino.cc/en/Main/Software) (At least v1.6.12)
7174
2. Start the Arduino IDE
7275
3. Go into Preferences
@@ -75,7 +78,6 @@ I wanted to have a consistent BLE API on all of the devices I work with. NimBLE
7578
6. Select your board from the Tools -> Board menu
7679

7780
### From git (for core development)
78-
7981
1. Follow steps from Board Manager section above
8082
2. ```cd <SKETCHBOOK>```, where ```<SKETCHBOOK>``` is your Arduino Sketch folder:
8183
* OS X: ```~/Documents/Arduino```
@@ -86,7 +88,6 @@ I wanted to have a consistent BLE API on all of the devices I work with. NimBLE
8688
5. Restart the Arduino IDE
8789

8890
## Flashing your device
89-
9091
1. Select your board from the Tools -> Board menu
9192
2. Select any options you want
9293
3. Select a programmer (J-Link, ST-Link V2, CMSIS-DAP, Black Magic Probe, adafruit-nrfutil or nrfutil) from the Tools -> "Programmer: " menu
@@ -105,19 +106,48 @@ I wanted to have a consistent BLE API on all of the devices I work with. NimBLE
105106
1. Install nrfutil if not already installed `pip install nrfutil`
106107
2. Select nrfutil as the firmware uploader in the tools menu.
107108

108-
## BLE
109-
110-
This Arduino Core does **not** contain any BLE functionality. It has been designed to support using the [NimBLE-Arduino](https://github.com/h2zero/NimBLE-Arduino) library for BLE operation.
111-
**Note:** Currently only the release version 1.4.0 and above of [NimBLE-Arduino](https://github.com/h2zero/NimBLE-Arduino) supports Arm devices.
109+
## Configuration (optional)
110+
You can change the configuration for many settings by creating a `build_opt.h` file in your sketch folder.
111+
In here you can set compile time definitions for settings that will be included directly on the command line.
112+
For example: `'-DCONFIG_MAIN_TASK_STACK_SIZE=2048'` This will set the main task stack size to 2048 bytes.
113+
114+
### Configuration option list
115+
* `CONFIG_MAIN_TASK_STACK_SIZE` - sets the size **in bytes** of the main loop task.
116+
* `CONFIG_RTOS_TICK_RATE_HZ` - set the tick rate for FreeRTOS (default 1024).
117+
* `CONFIG_RTOS_MAX_PRIORITIES` - set the maximum priority level for FreeRTOS tasks.
118+
* `CONFIG_RTOS_MIN_TASK_STACK_SIZE` - set the minimum task stack size.
119+
* `CONFIG_RTOS_TIMER_QUEUE_LENGTH` - set the queue size for the FreeRTOS timers.
120+
* `CONFIG_RTOS_TIMER_STACK_DEPTH` - set the timer task stack size **in 32bit words**.
121+
* Nimble configuration options can also be included, the list of those can be found [here](https://h2zero.github.io/NimBLE-Arduino/md__command_line_config.html)
122+
* Other compiler options or definitions for other libraries can also be specified.
123+
124+
## API
125+
There are a few useful functions available to help with your project.
126+
### RTOS class functions
127+
* `uint32_t RTOS.getMainTaskHwm();` - Returns the high water mark of the Main task stack **in 32bit words**.
128+
* `uint32_t RTOS.getIsrStackHwm();` - Returns the high water mark of the ISR stack **in 32bit words**.
129+
* `uint32_t RTOS.getIdleTaskHwm();` - Returns the high water mark of the Idle task stack **in 32bit words**.
130+
* `uint32_t RTOS.getTimerTaskHwm()` - Returns the high water mark of the Timer task stack **in 32bit words**.
131+
* `uint32_t RTOS.getBleHostTaskHwm();` - Returns the high water mark of the NimBLE Host task stack **in 32bit words**.
132+
* `uint32_t RTOS.getBleLLTaskHwm()` - Returns the high water mark of the NimBLE Link Layer task stack **in 32bit words**.
133+
* `uint32_t RTOS.getFreeHeap();` - Returns the currently free heap size **in bytes**.
134+
135+
### General system functions
136+
* `uint32_t getResetReason();` - Returns the reset reason for the last boot.
137+
* `void systemPowerOff();` - Shuts down the MCU.
138+
* `void systemRestart();` - Reboot.
112139

113140
## Bootloader
114141
Currently only some boards have Adafruit bootloaders available which are provided as options. You may choose to use the bootloader or none.
115142
The provided Adafruit bootloaders have no softdevice, if you currently are using the softdevice based Adafruit bootloader on your nRF52 board you will need to update it to the one provided by selecting it from the boards menu and clicking `Burn Bootloader`.
116143

117144
For boards without the Adafruit bootloader option clicking `Burn Bootloader` will simply erase the flash memory on the device. This is required if you have any bootloader flashed already.
118145

119-
## Credits
146+
## Important notes
147+
* The last four pages of flash, before the bootloader (if applicable) are reserved for user storage and bond information storage (2 pages each).
148+
* Careful attention should be paid to selecting the correct bootloader (tools->Bootloader Type) for your build (if applicable) to ensure the correct linkage.
120149

150+
## Credits
121151
This core is based on [Arduino-nRF5](https://github.com/sandeepmistry/arduino-nRF5) by Sandeep Mistry,
122152
which is based on the [Arduino SAMD Core](https://github.com/arduino/ArduinoCore-samd).
123153
With some code from [Adafruit_nRF52_Arduino](https://github.com/adafruit/Adafruit_nRF52_Arduino)

cores/nRF5/Arduino.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ void yield( void ) ;
3939
/* sketch */
4040
void setup( void ) ;
4141
void loop( void ) ;
42-
uint32_t get_isr_stack_hwm(void);
43-
uint32_t getMainTaskHwm(void);
4442

4543
#include "WVariant.h"
4644

@@ -136,6 +134,7 @@ uint32_t getMainTaskHwm(void);
136134

137135
#ifdef __cplusplus
138136
#include "Uart.h"
137+
#include "rtos.h"
139138
#endif // __cplusplus
140139

141140
#endif // Arduino_h

cores/nRF5/freertos/FreeRTOSConfig.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@
5555
# ifndef CONFIG_RTOS_MAX_PRIORITIES
5656
# define CONFIG_RTOS_MAX_PRIORITIES (3)
5757
# endif
58-
# ifndef CONFIG_RTOS_MIN_TASK_SIZE
59-
# define CONFIG_RTOS_MIN_TASK_SIZE (48)
58+
# ifndef CONFIG_RTOS_MIN_TASK_STACK_SIZE
59+
# define CONFIG_RTOS_MIN_TASK_STACK_SIZE (48)
6060
# endif
6161
# ifndef CONFIG_RTOS_TIMER_QUEUE_LENGTH
6262
# define CONFIG_RTOS_TIMER_QUEUE_LENGTH (6)
@@ -71,8 +71,8 @@
7171
#define CONFIG_RTOS_MAX_PRIORITIES (5)
7272
#endif
7373

74-
#ifndef CONFIG_RTOS_MIN_TASK_SIZE
75-
#define CONFIG_RTOS_MIN_TASK_SIZE (120)
74+
#ifndef CONFIG_RTOS_MIN_TASK_STACK_SIZE
75+
#define CONFIG_RTOS_MIN_TASK_STACK_SIZE (120)
7676
#endif
7777

7878
#ifndef CONFIG_RTOS_TIMER_QUEUE_LENGTH
@@ -97,7 +97,7 @@
9797
#define configCPU_CLOCK_HZ ( SystemCoreClock )
9898
#define configTICK_RATE_HZ CONFIG_RTOS_TICK_RATE_HZ
9999
#define configMAX_PRIORITIES CONFIG_RTOS_MAX_PRIORITIES
100-
#define configMINIMAL_STACK_SIZE CONFIG_RTOS_MIN_TASK_SIZE
100+
#define configMINIMAL_STACK_SIZE CONFIG_RTOS_MIN_TASK_STACK_SIZE
101101
#define configTOTAL_HEAP_SIZE ( 4096 ) //unused - modified heap_4 uses remaining available RAM
102102
#define configMAX_TASK_NAME_LEN ( 4 )
103103
#define configUSE_16_BIT_TICKS 0

cores/nRF5/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,6 @@ int main( void )
7979
return 0;
8080
}
8181

82-
uint32_t getMainTaskHwm() {
83-
return uxTaskGetStackHighWaterMark(_loopTaskHandle);
84-
}
82+
TaskHandle_t getMainLoopTaskHandle() {
83+
return _loopTaskHandle;
84+
}

cores/nRF5/rtos.cpp

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "Arduino.h"
2020
#include "freertos/FreeRTOS.h"
21+
#include "freertos/timers.h"
2122
#include "freertos/task.h"
2223

2324
extern "C" {
@@ -41,4 +42,55 @@ extern uint32_t tud_cdc_write_flush (void);
4142
#endif
4243
}
4344

44-
}
45+
UBaseType_t nimble_port_freertos_get_ll_hwm(void) __attribute__((weak));
46+
UBaseType_t nimble_port_freertos_get_ll_hwm(void) { return 0; }
47+
UBaseType_t nimble_port_freertos_get_hs_hwm(void) __attribute__((weak));
48+
UBaseType_t nimble_port_freertos_get_hs_hwm(void) { return 0; }
49+
50+
} // extern "C"
51+
52+
#include "rtos.h"
53+
extern uint32_t __StackTop;
54+
extern uint32_t __StackLimit;
55+
extern TaskHandle_t getMainLoopTaskHandle();
56+
57+
uint32_t nableRtos::getBleHostTaskHwm() {
58+
return (uint32_t)nimble_port_freertos_get_hs_hwm();
59+
}
60+
61+
uint32_t nableRtos::getBleLLTaskHwm() {
62+
return (uint32_t)nimble_port_freertos_get_ll_hwm();
63+
}
64+
65+
uint32_t nableRtos::getMainTaskHwm() {
66+
return uxTaskGetStackHighWaterMark(getMainLoopTaskHandle());
67+
}
68+
69+
uint32_t nableRtos::getIdleTaskHwm() {
70+
return (uint32_t)uxTaskGetStackHighWaterMark(xTaskGetIdleTaskHandle());
71+
}
72+
73+
uint32_t nableRtos::getFreeHeap() {
74+
return (uint32_t)xPortGetFreeHeapSize();
75+
}
76+
77+
uint32_t nableRtos::getTimerTaskHwm() {
78+
return (uint32_t)uxTaskGetStackHighWaterMark(xTimerGetTimerDaemonTaskHandle());
79+
}
80+
81+
uint32_t nableRtos::getIsrStackHwm() {
82+
uint32_t offset = 0;
83+
uint32_t *address = (uint32_t *) &__StackLimit;
84+
85+
for (; offset<((uint32_t)&__StackTop-(uint32_t)&__StackLimit); offset += 4)
86+
{
87+
if (*(address + offset) != 0xB1E4B1E5 )
88+
{
89+
break;
90+
}
91+
}
92+
93+
return offset;
94+
}
95+
96+
nableRtos RTOS;

cores/nRF5/rtos.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Copyright (c) 2021 Ryan Powell. All right reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
See the GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#ifndef NABLERTOS_H
20+
#define NABLERTOS_H
21+
22+
#include <stdint.h>
23+
24+
class nableRtos {
25+
public:
26+
nableRtos() {}
27+
~nableRtos() {}
28+
uint32_t getIsrStackHwm();
29+
uint32_t getMainTaskHwm();
30+
uint32_t getFreeHeap();
31+
uint32_t getIdleTaskHwm();
32+
uint32_t getTimerTaskHwm();
33+
uint32_t getBleHostTaskHwm();
34+
uint32_t getBleLLTaskHwm();
35+
};
36+
37+
extern nableRtos RTOS;
38+
39+
#endif

cores/nRF5/utils/debug_utils.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
#include "compiler_abstraction.h"
2222
#include <stdlib.h>
2323

24-
extern uint32_t __StackTop;
25-
extern uint32_t __StackLimit;
26-
2724
struct exception_frame {
2825
uint32_t r0;
2926
uint32_t r1;
@@ -110,20 +107,4 @@ void HardFault_Handler(void)
110107
}
111108
#endif
112109

113-
uint32_t get_isr_stack_hwm(void)
114-
{
115-
uint32_t offset = 0;
116-
uint32_t *address = (uint32_t *) &__StackLimit;
117-
118-
for (; offset<((uint32_t)&__StackTop-(uint32_t)&__StackLimit); offset += 4)
119-
{
120-
if (*(address + offset) != 0xB1E4B1E5 )
121-
{
122-
break;
123-
}
124-
}
125-
126-
return offset;
127-
}
128-
129110
} // extern "C"

cores/nRF5/wiring.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,18 @@ void init( void )
123123
NRF_WDT->TASKS_START = 1; //Start the Watchdog timer
124124
}
125125

126-
uint32_t readResetReason(void) {
126+
uint32_t getResetReason(void) {
127127
return _resetReason;
128128
}
129129

130130
void systemPowerOff(void) {
131131
nrf_power_system_off(NRF_POWER);
132132
}
133133

134+
void systemRestart(void) {
135+
NVIC_SystemReset();
136+
}
137+
134138
__attribute__ ((__weak__))
135139
void enterSerialDfu(void) {
136140
NRF_POWER->GPREGRET = DFU_MAGIC_SERIAL_ONLY_RESET;

cores/nRF5/wiring.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ extern "C" {
2525
extern void init(void);
2626
void enterSerialDfu(void);
2727
void systemPowerOff(void);
28-
uint32_t readResetReason(void);
28+
void systemRestart(void);
29+
uint32_t getResetReason(void);
2930

3031
#ifdef __cplusplus
3132
}

platform.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ tools.openocd.erase.pattern="{path}/{cmd}" {bootloader.verbose} -f interface/{pr
171171
tools.openocd.bootloader.params.verbose=-d2
172172
tools.openocd.bootloader.params.quiet=-d0
173173
tools.openocd.bootloader.pattern="{path}/{cmd}" {bootloader.verbose} -f interface/{program.protocol}.cfg -c "{program.setup_command}" -f target/{upload.target}.cfg -c "init; halt; program {{runtime.platform.path}/{build.bootloader.file}} reset; shutdown;"
174+
174175
#
175176
# blackmagic probe upload
176177
#

0 commit comments

Comments
 (0)