Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to allow mbed and the mbed online compiler to build. #449

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions inc/core/MicroBitHeapAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ DEALINGS IN THE SOFTWARE.
#define MICROBIT_HEAP_ALLOCTOR_H

#include "MicroBitConfig.h"
#include <new>

// The maximum number of heap segments that can be created.
#define MICROBIT_MAXIMUM_HEAPS 2
Expand Down Expand Up @@ -85,4 +86,27 @@ struct HeapDefinition
int microbit_create_heap(uint32_t start, uint32_t end);
void microbit_heap_print();

/**
* Attempt to allocate a given amount of memory from any of our configured heap areas.
*
* @param size The amount of memory, in bytes, to allocate.
*
* @return A pointer to the allocated memory, or NULL if insufficient memory is available.
*/
void *_microbit_malloc(size_t size);
void* _microbit_calloc (size_t num, size_t size);
void* _microbit_realloc (void* ptr, size_t size);

/**
* Release a given area of memory from the heap.
*
* @param mem The memory area to release.
*/
void _microbit_free(void *mem);

#define malloc _microbit_malloc
#define free _microbit_free
#define calloc _microbit_calloc
#define realloc _microbit_realloc

#endif
2 changes: 1 addition & 1 deletion inc/core/MicroBitUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ DEALINGS IN THE SOFTWARE.

#include "MicroBitConfig.h"

#define CREATE_KEY_VALUE_TABLE(NAME, PAIRS) const KeyValueTable NAME { PAIRS, sizeof(PAIRS) / sizeof(KeyValueTableEntry) };
#define CREATE_KEY_VALUE_TABLE(NAME, PAIRS) const KeyValueTable NAME = { PAIRS, sizeof(PAIRS) / sizeof(KeyValueTableEntry) };

/**
* Provides a simple key/value pair lookup table with range lookup support.
Expand Down
14 changes: 6 additions & 8 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,15 @@ if (YOTTA_CFG_MICROBIT_CONFIGFILE)
endif ()

if(CMAKE_COMPILER_IS_GNUCC)
file(REMOVE "asm/CortexContextSwitch.s")
configure_file("asm/CortexContextSwitch.s.gcc" "asm/CortexContextSwitch.s" COPYONLY)
set(YOTTA_AUTO_MICROBIT-DAL_S_FILES
"asm/TOOLCHAIN_GCC_ARM/CortexContextSwitch.s"
)
else()
file(REMOVE "asm/CortexContextSwitch.s")
configure_file("asm/CortexContextSwitch.s.armcc" "asm/CortexContextSwitch.s" COPYONLY)
set(YOTTA_AUTO_MICROBIT-DAL_S_FILES
"asm/TOOLCHAIN_ARM_STD/CortexContextSwitch.S"
)
endif()

set(YOTTA_AUTO_MICROBIT-DAL_S_FILES
"asm/CortexContextSwitch.s"
)

add_library(microbit-dal
${YOTTA_AUTO_MICROBIT-DAL_CPP_FILES}
${YOTTA_AUTO_MICROBIT-DAL_S_FILES}
Expand Down
18 changes: 12 additions & 6 deletions source/core/MicroBitHeapAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ DEALINGS IN THE SOFTWARE.
// A list of all active heap regions, and their dimensions in memory.
HeapDefinition heap[MICROBIT_MAXIMUM_HEAPS] = { };
uint8_t heap_count = 0;
extern "C" int __end__;
#if defined __CC_ARM || defined __ARMCC_VERSION
#define HEAP_START Image$$RW_IRAM1$$ZI$$Limit
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no experience with armcc, but this is a really weird-looking and cryptic symbol, is there perhaps a better one for this value?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see this is also defined here: https://github.com/lancaster-university/mbed-classic/blob/master/targets/cmsis/TARGET_NORDIC/TARGET_MCU_NRF51822/TOOLCHAIN_ARM_STD/sys.cpp#L15

As weird looking at it seems, I guess it is what it is ¯\_(ツ)_/¯

#else
#define HEAP_START __end__
#endif

extern "C" int HEAP_START;

#if CONFIG_ENABLED(MICROBIT_DBG) && CONFIG_ENABLED(MICROBIT_HEAP_DBG)
// Diplays a usage summary about a given heap...
Expand Down Expand Up @@ -271,7 +277,7 @@ void *microbit_malloc(size_t size, HeapDefinition &heap)
*
* @return A pointer to the allocated memory, or NULL if insufficient memory is available.
*/
void *malloc(size_t size)
void *_microbit_malloc(size_t size)
{
static uint8_t initialised = 0;
void *p;
Expand All @@ -280,7 +286,7 @@ void *malloc(size_t size)
{
heap_count = 0;

if(microbit_create_heap((uint32_t)(&__end__), (uint32_t)(MICROBIT_HEAP_END)) == MICROBIT_INVALID_PARAMETER)
if(microbit_create_heap((uint32_t)(&HEAP_START), (uint32_t)(MICROBIT_HEAP_END)) == MICROBIT_INVALID_PARAMETER)
microbit_panic(MICROBIT_HEAP_ERROR);

initialised = 1;
Expand Down Expand Up @@ -319,7 +325,7 @@ void *malloc(size_t size)
*
* @param mem The memory area to release.
*/
void free(void *mem)
void _microbit_free(void *mem)
{
uint32_t *memory = (uint32_t *)mem;
uint32_t *cb = memory-1;
Expand Down Expand Up @@ -351,7 +357,7 @@ void free(void *mem)
microbit_panic(MICROBIT_HEAP_ERROR);
}

void* calloc (size_t num, size_t size)
void* _microbit_calloc (size_t num, size_t size)
{
void *mem = malloc(num*size);

Expand All @@ -361,7 +367,7 @@ void* calloc (size_t num, size_t size)
return mem;
}

void* realloc (void* ptr, size_t size)
void* _microbit_realloc (void* ptr, size_t size)
{
void *mem = malloc(size);

Expand Down
4 changes: 2 additions & 2 deletions source/drivers/MicroBitCompassCalibrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ CompassCalibration MicroBitCompassCalibrator::spherify(Sample3D centre, Sample3D
Sample3D MicroBitCompassCalibrator::approximateCentre(Sample3D *data, int samples)
{
Sample3D c,t;
Sample3D centre = { 0,0,0 };
Sample3D best = { 0,0,0 };
Sample3D centre;
Sample3D best;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default constructor initialises the values to zero, but is there any specific reason to remove the {0,0,0}?
Also, to reduce the diff as much as possible, can we undo all the whitespace character changes?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the other initializer removals. I will remove the whitespace.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the constructor code has been reverted (was there constructor code to initialise these to 0?), shouldn't these 3 lines be reverted as well?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There wasn't constructor code to initialise these as they are initialised to zero by default.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@geowor01 I don't think there are any guarantees that struct fields are zero initialised unless they are placed in bss (globals).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jamesadevine The default constructor sets the value to zero.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@geowor01 Ah cool 😄, my bad!


float score;

Expand Down