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

[freertos-plus-tcp] Add includes #475

Merged
merged 4 commits into from
Sep 18, 2020
Merged
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
81 changes: 81 additions & 0 deletions examples/nucleo_f429zi/freertos_plus_tcp/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2014, Georgi Grinshpun
* Copyright (c) 2014, Sascha Schade
* Copyright (c) 2015-2017, 2019 Niklas Hauser
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#include <modm/board.hpp>
#include <modm/processing/rtos.hpp>

using namespace modm::platform;

/**
* This example uses four threads to check if task switching works correctly.
*
* It also check if the FreeRTOS TCP stack can be compiled.
* No TCP functionality in this example (yet).
*
* What to expect?
* ---------------
* - All our LEDs blinking at different rates, about 3 to 4 Hz
* - A string at 115200 baud
*
* 0aA!1bB"2cC#3dD$4eE%5fF&6gG'7hH(8iI9)jJ0*aA1!bB2"cC
*
* Each thread prints out a sequence
* 0123456789
* abcdefghij
* ABCDEFGHIJ
* !"#$%&'()*
* respectivly.
*/

// ----------------------------------------------------------------------------
template <typename Gpio, int SleepTime>
class P: modm::rtos::Thread
{
char c;
uint8_t i = 0;
volatile float a = 10.f;
public:
P(char c): Thread(2,1<<10), c(c) {}

void run()
{
Gpio::setOutput();
while (true)
{
sleep(SleepTime * MILLISECONDS);

Gpio::toggle();
{
static modm::rtos::Mutex lm;
modm::rtos::MutexGuard m(lm);
MODM_LOG_INFO << char(i + c);
}
i = (i+1)%10;
a *= 3.141f;
}
}
};

P< Board::LedRed, 260 > p1('0');
P< Board::LedGreen, 260 + 10 > p2('a');
P< Board::LedBlue, 260 + 20 > p3('A');


// ----------------------------------------------------------------------------
int
main()
{
Board::initialize();
modm::rtos::Scheduler::schedule();
return 0;
}
12 changes: 12 additions & 0 deletions examples/nucleo_f429zi/freertos_plus_tcp/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<library>
<extends>modm:nucleo-f429zi</extends>
<options>
<option name="modm:build:build.path">../../../build/nucleo_f429zi/freertos_plus_tcp</option>
</options>
<modules>
<module>modm:processing:rtos</module>
<module>modm:freertos:tcp</module>
<module>modm:platform:heap</module>
<module>modm:build:scons</module>
</modules>
</library>
2 changes: 1 addition & 1 deletion ext/aws/freertos
Submodule freertos updated 72 files
+10 −10 FreeRTOS-Plus-TCP/FreeRTOS_ARP.c
+20 −10 FreeRTOS-Plus-TCP/FreeRTOS_DHCP.c
+71 −30 FreeRTOS-Plus-TCP/FreeRTOS_DNS.c
+70 −55 FreeRTOS-Plus-TCP/FreeRTOS_IP.c
+75 −42 FreeRTOS-Plus-TCP/FreeRTOS_Sockets.c
+1 −1 FreeRTOS-Plus-TCP/FreeRTOS_Stream_Buffer.c
+67 −46 FreeRTOS-Plus-TCP/FreeRTOS_TCP_IP.c
+30 −22 FreeRTOS-Plus-TCP/FreeRTOS_TCP_WIN.c
+4 −4 FreeRTOS-Plus-TCP/FreeRTOS_UDP_IP.c
+1 −1 FreeRTOS-Plus-TCP/include/FreeRTOSIPConfigDefaults.h
+1 −1 FreeRTOS-Plus-TCP/include/FreeRTOS_ARP.h
+1 −1 FreeRTOS-Plus-TCP/include/FreeRTOS_DHCP.h
+1 −1 FreeRTOS-Plus-TCP/include/FreeRTOS_DNS.h
+8 −17 FreeRTOS-Plus-TCP/include/FreeRTOS_IP.h
+140 −21 FreeRTOS-Plus-TCP/include/FreeRTOS_IP_Private.h
+18 −13 FreeRTOS-Plus-TCP/include/FreeRTOS_Sockets.h
+1 −1 FreeRTOS-Plus-TCP/include/FreeRTOS_Stream_Buffer.h
+1 −1 FreeRTOS-Plus-TCP/include/FreeRTOS_TCP_IP.h
+1 −1 FreeRTOS-Plus-TCP/include/FreeRTOS_TCP_WIN.h
+1 −1 FreeRTOS-Plus-TCP/include/FreeRTOS_UDP_IP.h
+1 −1 FreeRTOS-Plus-TCP/include/FreeRTOS_errno_TCP.h
+1 −1 FreeRTOS-Plus-TCP/include/IPTraceMacroDefaults.h
+5 −1 FreeRTOS-Plus-TCP/include/NetworkBufferManagement.h
+4 −6 FreeRTOS-Plus-TCP/include/NetworkInterface.h
+1 −1 FreeRTOS-Plus-TCP/include/tcp_dump_packets.h
+1 −1 FreeRTOS-Plus-TCP/include/tcp_mem_stats.h
+4 −3 FreeRTOS-Plus-TCP/portable/BufferManagement/BufferAllocation_1.c
+6 −5 FreeRTOS-Plus-TCP/portable/BufferManagement/BufferAllocation_2.c
+1 −1 FreeRTOS-Plus-TCP/portable/Compiler/GCC/pack_struct_end.h
+1 −1 FreeRTOS-Plus-TCP/portable/Compiler/GCC/pack_struct_start.h
+1 −1 FreeRTOS-Plus-TCP/tools/tcp_dump_packets.c
+1 −1 FreeRTOS-Plus-TCP/tools/tcp_mem_stats.c
+272 −270 FreeRTOS/Source/croutine.c
+628 −610 FreeRTOS/Source/event_groups.c
+494 −476 FreeRTOS/Source/include/FreeRTOS.h
+5 −106 FreeRTOS/Source/include/StackMacros.h
+134 −131 FreeRTOS/Source/include/atomic.h
+419 −388 FreeRTOS/Source/include/croutine.h
+82 −82 FreeRTOS/Source/include/deprecated_definitions.h
+361 −343 FreeRTOS/Source/include/event_groups.h
+95 −90 FreeRTOS/Source/include/list.h
+267 −249 FreeRTOS/Source/include/message_buffer.h
+162 −59 FreeRTOS/Source/include/mpu_prototypes.h
+150 −152 FreeRTOS/Source/include/mpu_wrappers.h
+78 −61 FreeRTOS/Source/include/portable.h
+66 −70 FreeRTOS/Source/include/projdefs.h
+813 −752 FreeRTOS/Source/include/queue.h
+506 −473 FreeRTOS/Source/include/semphr.h
+60 −62 FreeRTOS/Source/include/stack_macros.h
+277 −269 FreeRTOS/Source/include/stream_buffer.h
+1,045 −870 FreeRTOS/Source/include/task.h
+211 −169 FreeRTOS/Source/include/timers.h
+145 −133 FreeRTOS/Source/list.c
+395 −389 FreeRTOS/Source/portable/GCC/ARM_CM0/port.c
+62 −63 FreeRTOS/Source/portable/GCC/ARM_CM0/portmacro.h
+526 −538 FreeRTOS/Source/portable/GCC/ARM_CM3/port.c
+153 −153 FreeRTOS/Source/portable/GCC/ARM_CM3/portmacro.h
+585 −577 FreeRTOS/Source/portable/GCC/ARM_CM4F/port.c
+153 −153 FreeRTOS/Source/portable/GCC/ARM_CM4F/portmacro.h
+577 −569 FreeRTOS/Source/portable/GCC/ARM_CM7/r0p1/port.c
+158 −158 FreeRTOS/Source/portable/GCC/ARM_CM7/r0p1/portmacro.h
+73 −75 FreeRTOS/Source/portable/MemMang/heap_1.c
+174 −172 FreeRTOS/Source/portable/MemMang/heap_2.c
+39 −42 FreeRTOS/Source/portable/MemMang/heap_3.c
+380 −378 FreeRTOS/Source/portable/MemMang/heap_4.c
+412 −409 FreeRTOS/Source/portable/MemMang/heap_5.c
+2,597 −2,529 FreeRTOS/Source/queue.c
+1,094 −1,031 FreeRTOS/Source/stream_buffer.c
+4,582 −4,544 FreeRTOS/Source/tasks.c
+932 −915 FreeRTOS/Source/timers.c
+23 −0 FreeRTOS_IP.c.patch
+4 −1 update.py
58 changes: 56 additions & 2 deletions ext/aws/modm_port.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,66 @@

/*
* Copyright (c) 2019-2020 Niklas Hauser
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <modm/architecture/interface/assert.hpp>
#include <cstdlib>

extern "C" void vApplicationStackOverflowHook(TaskHandle_t, char *);

void vApplicationStackOverflowHook(TaskHandle_t /*pxTask*/, char *pcTaskName)
{
modm_assert(false, "freertos.stack", "FreeRTOS detected a stack overflow!", pcTaskName);
}

// ----------------------------------------------------------------------------
// Make the Newlib heap thread-safe with FreeRTOS

extern "C" void __malloc_lock(struct _reent *);
void __malloc_lock(struct _reent *)
salkinium marked this conversation as resolved.
Show resolved Hide resolved
{
vTaskSuspendAll();
}

extern "C" void __malloc_unlock(struct _reent *);
void __malloc_unlock(struct _reent *)
{
xTaskResumeAll();
}

// ----------------------------------------------------------------------------
// Make the FreeRTOS heap use Newlib heap

extern "C" void *pvPortMalloc(size_t xWantedSize);
void *pvPortMalloc(size_t xWantedSize)
{
void *pvReturn = malloc(xWantedSize);
traceMALLOC(pvReturn, xWantedSize);

#if configUSE_MALLOC_FAILED_HOOK == 1
if(pvReturn == nullptr)
{
extern "C" void vApplicationMallocFailedHook(void);
vApplicationMallocFailedHook();
}
#endif

return pvReturn;
}

extern "C" void vPortFree(void *pv);
void vPortFree(void *pv)
{
if(pv)
{
free(pv);
traceFREE(pv, 0);
}
}
8 changes: 5 additions & 3 deletions ext/aws/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ the config macros, then redefine them with your options, for example:
def build(self, env):
env.outbasepath = "modm/ext"
env.copy("freertos/FreeRTOS-Plus-TCP", "freertos_plus_tcp",
ignore=env.ignore_files("BufferAllocation_1.c"))
ignore=env.ignore_files("BufferAllocation_1.c", "portable"))
# Copy the compiler support files
env.copy("freertos/FreeRTOS-Plus-TCP/portable/Compiler/GCC/pack_struct_start.h", "freertos_plus_tcp/include/pack_struct_start.h")
env.copy("freertos/FreeRTOS-Plus-TCP/portable/Compiler/GCC/pack_struct_end.h", "freertos_plus_tcp/include/pack_struct_end.h")

env.collect(":build:path.include", "modm/ext/freertos_plus_tcp/include")
env.template("FreeRTOSIPConfig.h.in", "freertos_plus_tcp/include/FreeRTOSIPConfig.h")
# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -100,8 +104,6 @@ def build(env):
env.copy("{}/port.c".format(path), "freertos/port.c")
# Copy the portmacro.h file
env.copy("{}/portmacro.h".format(path), "freertos/inc/freertos/portmacro.h")
# Copy the head_3.c file that just wraps the libc malloc
env.copy("freertos/FreeRTOS/Source/portable/MemMang/heap_3.c", "freertos/heap_3.c")

# Generate the FreeRTOSConfig.h file
env.template("FreeRTOSConfig.h.in", "freertos/inc/freertos/FreeRTOSConfig.h")
Expand Down
11 changes: 9 additions & 2 deletions src/modm/platform/heap/cortex/heap_block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,14 @@ void __modm_initialize_memory(void)
allocator.initialize((void*)heap_start, (void*)heap_end);
}

void* __wrap__malloc_r(struct _reent *, size_t size)
extern void __malloc_lock(struct _reent *);
extern void __malloc_unlock(struct _reent *);

void* __wrap__malloc_r(struct _reent *r, size_t size)
{
__malloc_lock(r);
void *ptr = allocator.allocate(size);
__malloc_unlock(r);
modm_assert_continue_fail_debug(ptr, "malloc",
"No memory left in Block heap!", size);
return ptr;
Expand All @@ -74,9 +79,11 @@ void* __wrap__realloc_r(struct _reent *, void *, size_t size)
return NULL;
}

void __wrap__free_r(struct _reent *, void *p)
void __wrap__free_r(struct _reent *r, void *p)
{
__malloc_lock(r);
allocator.free(p);
__malloc_unlock(r);
}

} // extern "C"
33 changes: 21 additions & 12 deletions src/modm/platform/heap/cortex/heap_tlsf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ get_tlsf_for_ptr(void *p)
return NULL;
}

extern void __malloc_lock(struct _reent *);
extern void __malloc_unlock(struct _reent *);

void * malloc_traits(size_t size, uint32_t traits)
{
try_again:
Expand All @@ -97,7 +100,9 @@ void * malloc_traits(size_t size, uint32_t traits)
{
if ((pool->traits & traits) == traits)
{
__malloc_lock(_REENT);
void *p = tlsf_malloc(pool->tlsf, size);
__malloc_unlock(_REENT);
if (p) return p;
}
}
Expand All @@ -124,7 +129,8 @@ void * malloc_traits(size_t size, uint32_t traits)
void *__wrap__malloc_r(struct _reent *, size_t size)
{
// default is accessible by S-Bus and DMA-able
return malloc_traits(size, 0x8 | 0x1);
return malloc_traits(size, uint32_t(modm::MemoryTrait::AccessSBus) |
uint32_t(modm::MemoryTrait::AccessDMA));
}

void *__wrap__calloc_r(struct _reent *r, size_t size)
Expand All @@ -134,26 +140,29 @@ void *__wrap__calloc_r(struct _reent *r, size_t size)
return ptr;
}

void *__wrap__realloc_r(struct _reent *, void *p, size_t size)
void *__wrap__realloc_r(struct _reent *r, void *p, size_t size)
{
tlsf_t pool = get_tlsf_for_ptr(p);
// if pointer belongs to no pool, exit.
if (!pool) return NULL;
void *ptr = NULL;

__malloc_lock(r);
const tlsf_t pool = get_tlsf_for_ptr(p);
if (pool) ptr = tlsf_realloc(pool, p, size);
__malloc_unlock(r);

void *ptr = tlsf_realloc(pool, p, size);
modm_assert_continue_fail_debug(0, "realloc",
modm_assert_continue_fail_debug(ptr, "realloc",
"Unable to realloc in TLSF pool!", size);
return ptr;
}

void __wrap__free_r(struct _reent *, void *p)
void __wrap__free_r(struct _reent *r, void *p)
{
// do nothing if NULL pointer
if (!p) return;
tlsf_t pool = get_tlsf_for_ptr(p);
// if pointer belongs to no pool, exit.
if (!pool) return;
tlsf_free(pool, p);
__malloc_lock(r);
const tlsf_t pool = get_tlsf_for_ptr(p);
// free if pointer belongs to a pool.
if (pool) tlsf_free(pool, p);
__malloc_unlock(r);
}

} // extern "C"