Skip to content

Commit

Permalink
Merge pull request #4 from calliope-mini/interpreter-ohne-pairing
Browse files Browse the repository at this point in the history
Interpreter ohne pairing
  • Loading branch information
thinkberg authored Jul 8, 2018
2 parents 8198123 + 98e23e1 commit db679a5
Show file tree
Hide file tree
Showing 54 changed files with 3,589 additions and 1,053 deletions.
2 changes: 2 additions & 0 deletions .yotta_ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cmake-build-debug/*
CMakeLists.txt
140 changes: 140 additions & 0 deletions README-STACK.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#How to configure the Stack and heap

The Stack and the heap are configured in 2 different ways.

## 1: Standard memory configuration
The basic file is [startup_NRF51822.S](yotta_modules/mbed-classic/targets/cmsis/TARGET_NORDIC/TARGET_MCU_NRF51822/TOOLCHAIN_GCC_ARM/startup_NRF51822.S),
which should not be modified, if you do not exactly know what to do.

However in this file there are two defines, `__STACK_SIZE` and `__HEAP_SIZE`
which can be used to modify the stack and the heap from outside this function.

### 1.1: Modifying toolchain.cmake
One option to do so, is to extend the file [toolchain.cmake](yotta_targets/calliope-mini-classic-gcc/CMake/toolchain.cmake). Add `-D__STACK_SIZE=2632 -D__HEAP_SIZE=1464` at the end of `add_definitions("...`
and the stack will have the size of 2632 Bytes and the heap will have the size of 1464 Bytes.

### 1.2: Adding defines.json
The alternative option is to add a [defines.json](defines.json) file to the same directory, where [module.json](module.json) is located.
Therein macro definitions can be added for the application. In this case the file would look like this:
```[bash]
{
"__STACK_SIZE": 2632,
"__HEAP_SIZE": 1464
}
```
The advantage of this option is, that you do not have to modify any dependencies, but can add extra definitions to the application.
The disadvantage is, that these definitions will apply to all application code, which can lead multiple definitions.

**NOTE: The size of Stack + Heap has to fit into the RAM**

#### 1.2.1 How much space for Heap and Stack is in the RAM

To find out how much space is in the RAM, you have to read the [calliope-demo.elf](build/calliope-mini-classic-gcc/source/calliope-demo)
file. Therefore enter the following command in the terminal:
```[bash]
$ readelf -S calliope-demo
```
The output will look like this:
```[bash]
There are 22 section headers, starting at offset 0x326b10:
Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
[ 0] NULL 00000000 000000 000000 00 0 0 0
[ 1] .text PROGBITS 00018000 008000 016494 00 AX 0 0 8
[ 2] .ARM.exidx ARM_EXIDX 0002e494 01e494 000008 00 AL 1 0 4
[ 3] .data PROGBITS 20002000 022000 0000fc 00 WA 0 0 4
[ 4] .bss NOBITS 20002100 022100 000a20 00 WA 0 0 8
[ 5] .heap PROGBITS 20002b20 022100 000acc 00 0 0 8
[ 6] .stack_dummy PROGBITS 20002b20 022bd0 000a00 00 0 0 8
[ 7] .ARM.attributes ARM_ATTRIBUTES 00000000 0235d0 000028 00 0 0 1
[ 8] .comment PROGBITS 00000000 0235f8 00007e 01 MS 0 0 1
[ 9] .debug_info PROGBITS 00000000 023676 220d76 00 0 0 1
[10] .debug_abbrev PROGBITS 00000000 2443ec 028b2e 00 0 0 1
[11] .debug_loc PROGBITS 00000000 26cf1a 02800e 00 0 0 1
[12] .debug_aranges PROGBITS 00000000 294f28 003268 00 0 0 8
[13] .debug_ranges PROGBITS 00000000 298190 006958 00 0 0 1
[14] .debug_line PROGBITS 00000000 29eae8 035e48 00 0 0 1
[15] .debug_str PROGBITS 00000000 2d4930 02f121 01 MS 0 0 1
[16] .debug_frame PROGBITS 00000000 303a54 009790 00 0 0 4
[17] .stab PROGBITS 00000000 30d1e4 00003c 0c 18 0 4
[18] .stabstr STRTAB 00000000 30d220 000076 00 0 0 1
[19] .symtab SYMTAB 00000000 30d298 00dac0 10 20 2352 4
[20] .strtab STRTAB 00000000 31ad58 00bcde 00 0 0 1
[21] .shstrtab STRTAB 00000000 326a36 0000d8 00 0 0 1
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings)
I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
O (extra OS processing required) o (OS specific), p (processor specific)
```
The important number here is at `[ 5] .heap PROGBITS 20002b20`,
which is the hexadecimal address = **0x2002b20** where the **Heap starts**. Since the **Stack ends** at the address **0x20004000**,
the space in beetween can be used for Heap and Stack and defined in [defines.json](defines.json) and [config.json](config.json)

**Important for config.json:**
The following settings have to be set:


```[bash]
{
...
"debug": 0,
"heap_debug": 0,
"reuse_sd": 0,
"heap_allocator": 0,
"stack_size": 2560,
}
}
```
where `stack_size` has to be the same as `__STACK_SIZE`




# !!!DEPRECATED!!!

## 2: MicroBit custom heap configuration
Since the [microbit-dal](yotta_modules/microbit-dal)
has a custom heap allocator [MicroBitHeapAllocator.cpp](yotta_modules/microbit-dal/source/core/MicroBitHeapAllocator.cpp)
and [MicroBitHeapAllocator.h](yotta_modules/microbit-dal/inc/core/MicroBitHeapAllocator.h),
which is configured from [config.json](config.json), this file has to be modified to fit the standard configuration,
by adding

```[bash]
"microbit-dal": {
...
"nested_heap_proportion": 0.6,
"stack_size": 2632,
...
}
```

### 2.1: Calculation of the custom heap configuration
(this chapter is based on my findings and has to be approved by the people who made coded the microbit-dal)

Currently the custom heap assumes that Heap + Stack = 4672 Bytes.
Unfortunatelly this seems to be not the case.

Running the [memsum.py](contrib/memsum.py) script with debug and hepa-debug enabled,
it shows that the custom heap starts from `0x200030F0` and from [MicroBitConfig.h](yotta_modules/microbit-dal/inc/core/MicroBitConfig.h)
we know that the Stack ends at `0x20004000` which is the upper bound of the RAM.

This means the real space for Heap + Stack is 3856 Bytes.

During testing, I found out, that whenever the Heap goes out of bounds, the controller crashes.
Also the overlapping of the stack and the Heap region brings the controller to crash sometimes.

The calculation follows:

`heap_size = (4672 - stack_size) * nested_heap_proportion`

However with the given knowledge the calculation should be:
`nested_heap_proportion' = (stack_size - 3856) / (stack_size - 4672)`
in which case the stack and the nested heap will not overlap.


## Further remarks
The option `"reuse_sd": 0` has to be 0 if BLE is used, otherwise heap will be allocated in the Softdevice RAM region,
for BLE nad thus this region will be overriden by the Softdevice and caus the controller to crash.


42 changes: 42 additions & 0 deletions README-UUIDS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[BluetoothServiceNotify](source/BluetoothServiceNotify.cpp)

BluetoothServiceNotifyUUID = 0xff,0x55,0xdd,0xee, 0x25,0x1d, 0x47,0x0a, 0xa0,0x62, 0xfa,0x19,0x22,0xdf,0xa9,0xa8

[BluetoothServiceProgram](source/BluetoothServiceProgram.h)

BluetoothServiceProgramUUID = 0xff, 0x66, 0xdd, 0xee, 0x25, 0x1d, 0x47, 0x0a, 0xa0, 0x62, 0xfa, 0x19, 0x22, 0xdf, 0xa9, 0xa8

[MicroBitAccelerometerService](yotta_modules/microbit-dal/source/bluetooth/MicroBitAccelerometerService.cpp)

MicroBitAccelerometerServiceUUID = 0xe9,0x5d,0x07,0x53,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8

MicroBitAccelerometerServiceDataUUID = 0xe9,0x5d,0xca,0x4b,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8

MicroBitAccelerometerServicePeriodUUID = 0xe9,0x5d,0xfb,0x24,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8

[MicroBitDFUService](yotta_modules/microbit-dal/source/bluetooth/MicroBitDFUService.cpp)

MicroBitDFUServiceUUID = 0xe9,0x5d,0x93,0xb0,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8

MicroBitDFUServiceControlCharacteristicUUID = 0xe9,0x5d,0x93,0xb1,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8

[DeviceInfoService](yotta_modules/ble/ble/GattCharacteristic.h)

UUID_DEVICE_INFORMATION_SERVICE = 0x180A

UUID_MANUFACTURER_NAME_STRING_CHAR = 0x2A29
UUID_MODEL_NUMBER_STRING_CHAR = 0x2A24
UUID_SERIAL_NUMBER_STRING_CHAR = 0x2A25
UUID_HARDWARE_REVISION_STRING_CHAR = 0x2A27
UUID_FIRMWARE_REVISION_STRING_CHAR = 0x2A26
UUID_SOFTWARE_REVISION_STRING_CHAR = 0x2A28



**Currently not in Use**


[BluetoothServiceDebug](source/BluetoothServiceDebug.cpp)

BluetoothServiceDebugUUID = 0xff, 0x44, 0xdd, 0xee, 0x25, 0x1d, 0x47, 0x0a, 0xa0, 0x62, 0xfa, 0x19, 0x22, 0xdf, 0xa9, 0xa8

33 changes: 26 additions & 7 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,35 @@
"enabled": 1,
"pairing_mode": 1,
"private_addressing": 0,
"open": 0,
"whitelist": 1,
"open": 1,
"security_level": "SECURITY_MODE_ENCRYPTION_OPEN_LINK",
"whitelist": 0,
"advertising_timeout": 0,
"tx_power": 6,
"tx_power": 7,
"dfu_service": 1,
"event_service": 0,
"device_info_service": 1,
"security_level": "SECURITY_MODE_ENCRYPTION_NO_MITM"
"device_info_service": 1
},
"gatt_table_size": "0x700",
"debug": 0
"gatt_table_size": "0x600",
"debug": 0,
"heap_debug": 0,
"reuse_sd": 0,
"default_pullmode": "PullDown",
"heap_allocator": 0,
"nested_heap_proportion": 0.6,
"system_tick_period": 6,
"system_components": 10,
"idle_components": 6,
"use_accel_lsb": 0,
"min_display_brightness": 1,
"max_display_brightness": 255,
"display_scroll_speed": 120,
"display_scroll_stride": -1,
"display_print_speed": 400,
"panic_on_heap_full": 0,
"stack_size": 2560,
"sram_base": "0x20000008",
"sram_end": "0x20004000",
"sd_limit": "0x20002000"
}
}
7 changes: 7 additions & 0 deletions contrib/copy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

SRC=../../calliope_playbook_mini/source/
DST=`pwd`/source/

rsync -rtuv --progress $SRC $DST \
--exclude Main.*
8 changes: 8 additions & 0 deletions contrib/log.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

#!/bin/sh

ROOT="$( cd "$(dirname "$0")" ; pwd -P )"
DEVICE=/dev/cu.usbmodem1422

( stty speed 115200 cs8 1>/dev/null 2>&1; cat ) <$DEVICE
#( stty speed 115200 cs8 1>/dev/null 2>&1; hexdump -C ) <$DEVICE
65 changes: 65 additions & 0 deletions contrib/memsum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import re
import sys
import time


mem = {}
allocated = 0

reset = None
if len(sys.argv) > 1:
reset = sys.argv[1]
print("RESETTING tracer on '%s'" % reset)

r_malloc = re.compile("^(microbit_)malloc:\\s+(NATIVE\\s+)?(ALLOCATED:)\\s+(\\d+)\\s+\\[(0x[0-9a-f]+)\\]")
r_free = re.compile("^(microbit_)free:\\s+(0x[0-9a-f]+)")

partial = ""
#broken in python2
#for line in sys.stdin:
for line in iter(sys.stdin.readline, ''):
# we sometimes get incomplete lines, wait for a full line
if not (line[-1] == '\n' or line[-1] == '\r'):
partial = line
continue
else:
line = partial + line
partial = ""

# strip newline and carriage return
line = line.rstrip('\n').rstrip('\r')

# if we detect the reset keyword, rest the map and memory counter
if reset != None and reset in line:
mem = {}
allocated = 0
# print("\n\n\033[91m>> RESET >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\033[0m")
print("\033[H\033[J")

# match malloc, realloc and free
m = r_malloc.search(line)
if m:
mem[m.group(5)] = int(m.group(4))
allocated += int(m.group(4))
print("\033[1m== (%03d) \033[34m%8d\033[0m [%8x] \033[31m+%-6d\033[0m (%s)" % \
(len(mem), allocated, allocated, int(m.group(4)),
m.group(0).replace(m.group(1), "").replace(m.group(3), "")))
continue

m = r_free.search(line)
if m:
# print "f", m.group(3)
freed = 0
if mem.has_key(m.group(2)):
freed = mem[m.group(2)]
allocated -= freed
del mem[m.group(2)]
else:
print ("\033[33m!! WARN: %s\033[0m" % (line))
print ("\033[1m== (%03d) \033[34m%8d\033[0m [%8x] \033[92m-%-6d\033[0m (%s)" % \
(len(mem), allocated, allocated, freed, m.group(0).replace(m.group(1), "")))
continue

# print all other lines as is, so we can still use the log functionality
print(line)
sys.stdout.flush()
7 changes: 7 additions & 0 deletions contrib/memsum.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

#!/bin/sh

ROOT="$( cd "$(dirname "$0")" ; pwd -P )"
DEVICE=/dev/ttyACM0

( stty speed 115200 cs8 1>/dev/null 2>&1; python2 $ROOT/memsum.py Calliope) <$DEVICE
10 changes: 10 additions & 0 deletions contrib/openocd.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
source [find interface/cmsis-dap.cfg]
source [find target/nrf51.cfg]

$_TARGETNAME configure -event gdb-attach {
reset init
}

$_TARGETNAME configure -event gdb-flash-write-end {
reset init
}
7 changes: 7 additions & 0 deletions contrib/upload.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

cp build/calliope-mini-classic-gcc/source/*-combined.hex /Volumes/MINI/

if [ -x $HOME/Shared/Manolis ]; then
cp build/calliope-mini-classic-gcc/source/*-combined.hex $HOME/Shared/Manolis
fi
3 changes: 3 additions & 0 deletions contrib/yotta.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

docker run -v $(pwd):/project:rw -it calliopeedu/yotta yotta $1
4 changes: 4 additions & 0 deletions defines.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"__STACK_SIZE": 2560,
"__HEAP_SIZE": 2764
}
4 changes: 2 additions & 2 deletions module.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"description": "Calliope mini Demo",
"license": "Apache 2.0",
"dependencies": {
"microbit": "calliope-mini/microbit#v2.0.0-rc8-calliope-1.0.2"
"microbit": "calliope-mini/microbit#v2.0.0-calliope-1.0.5"
},
"targetDependencies": {},
"bin": "./source"
}
}
Loading

0 comments on commit db679a5

Please sign in to comment.