23
23
24
24
#include " mbed.h"
25
25
#include " rtos/rtos_idle.h"
26
+ #include " mbed_assert.h"
27
+
28
+ #define ALIGN_UP (pos, align ) ((pos) % (align) ? (pos) + ((align) - (pos) % (align)) : (pos))
29
+ MBED_STATIC_ASSERT (ALIGN_UP(0 , 8 ) == 0, "ALIGN_UP macro error");
30
+ MBED_STATIC_ASSERT (ALIGN_UP(1 , 8 ) == 8, "ALIGN_UP macro error");
31
+
32
+ #define ALIGN_DOWN (pos, align ) ((pos) - ((pos) % (align)))
33
+ MBED_STATIC_ASSERT (ALIGN_DOWN(7 , 8 ) == 0, "ALIGN_DOWN macro error");
34
+ MBED_STATIC_ASSERT (ALIGN_DOWN(8 , 8 ) == 8, "ALIGN_DOWN macro error");
26
35
27
36
static void (*terminate_hook)(osThreadId_t id) = 0;
28
37
extern " C" void thread_terminate_hook (osThreadId_t id)
@@ -36,15 +45,21 @@ namespace rtos {
36
45
37
46
void Thread::constructor (osPriority priority,
38
47
uint32_t stack_size, unsigned char *stack_mem, const char *name) {
48
+
49
+ const uintptr_t unaligned_mem = reinterpret_cast <uintptr_t >(stack_mem);
50
+ const uintptr_t aligned_mem = ALIGN_UP (unaligned_mem, 8 );
51
+ const uint32_t offset = aligned_mem - unaligned_mem;
52
+ const uint32_t aligned_size = ALIGN_DOWN (stack_size - offset, 8 );
53
+
39
54
_tid = 0 ;
40
55
_dynamic_stack = (stack_mem == NULL );
41
56
_finished = false ;
42
57
memset (&_obj_mem, 0 , sizeof (_obj_mem));
43
58
memset (&_attr, 0 , sizeof (_attr));
44
59
_attr.priority = priority;
45
- _attr.stack_size = stack_size ;
60
+ _attr.stack_size = aligned_size ;
46
61
_attr.name = name ? name : " application_unnamed_thread" ;
47
- _attr.stack_mem = ( uint32_t *)stack_mem ;
62
+ _attr.stack_mem = reinterpret_cast < uint32_t *>(aligned_mem) ;
48
63
}
49
64
50
65
void Thread::constructor (Callback<void ()> task,
0 commit comments