Morse Micro IoT SDK  2.9.7
mmosal_shim_bootloader.c
1/*
2 * Copyright 2023 Morse Micro
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#include <stddef.h>
8#include <errno.h>
9#include <stdint.h>
10#include <stdbool.h>
11#include <unistd.h>
12#include "mmosal.h"
13#include "mmutils.h"
14
16#define LOOPS_PER_MS 1000
17
18/* See mmosal.h for declarations */
19
21{
22 app_init_cb();
23 return 0;
24}
25
26void *mmosal_malloc_(size_t size)
27{
28 return malloc(size);
29}
30
31void *mmosal_malloc_dbg(size_t size, const char *name, unsigned line_number)
32{
33 (void)name;
34 (void)line_number;
35 return malloc(size);
36}
37
38void *mmosal_calloc(size_t nitems, size_t size)
39{
40 return calloc(nitems, size);
41}
42
43void mmosal_free(void *p)
44{
45 free(p);
46}
47
48/* --------------------------------------------------------------------------------------------- */
49
50struct mmosal_mutex *mmosal_mutex_create(const char *name)
51{
52 MM_UNUSED(name);
53 return NULL;
54}
55
56void mmosal_mutex_delete(struct mmosal_mutex *mutex)
57{
58 MM_UNUSED(mutex);
59}
60
61bool mmosal_mutex_get(struct mmosal_mutex *mutex, uint32_t timeout_ms)
62{
63 MM_UNUSED(mutex);
64 MM_UNUSED(timeout_ms);
65 return true;
66}
67
68bool mmosal_mutex_release(struct mmosal_mutex *mutex)
69{
70 MM_UNUSED(mutex);
71 return true;
72}
73
74/* --------------------------------------------------------------------------------------------- */
75
76struct mmosal_semb *mmosal_semb_create(const char *name)
77{
78 MM_UNUSED(name);
79 return NULL;
80}
81
82void mmosal_semb_delete(struct mmosal_semb *semb)
83{
84 MM_UNUSED(semb);
85}
86
87bool mmosal_semb_give(struct mmosal_semb *semb)
88{
89 MM_UNUSED(semb);
90 return true;
91}
92
93bool mmosal_semb_give_from_isr(struct mmosal_semb *semb)
94{
95 MM_UNUSED(semb);
96 return true;
97}
98
99bool mmosal_semb_wait(struct mmosal_semb *semb, uint32_t timeout_ms)
100{
101 MM_UNUSED(semb);
102 MM_UNUSED(timeout_ms);
103 return true;
104}
105
106/* --------------------------------------------------------------------------------------------- */
107
109{
110 while (1)
111 {}
112}
113
115{
116 MM_UNUSED(info);
117}
118
119/* --------------------------------------------------------------------------------------------- */
120
121uint32_t mmosal_get_time_ms(void)
122{
123 return mmosal_get_time_ticks();
124}
125
127{
128 static uint32_t tick = 0;
129 static uint16_t microtick = 0;
130
131 if (microtick++ == LOOPS_PER_MS)
132 {
133 tick++;
134 microtick = 0;
135 }
136 return tick;
137}
138
139/*
140 * Note: This function is defined in unistd.h
141 * It is called when main() exits.
142 */
143void _exit(int status)
144{
145 (void) status;
146 while (1)
147 {
148 }
149}
150
151/*
152 * Note: This function is defined in unistd.h
153 * It is used to kill a thread.
154 */
155void _kill(pid_t pid)
156{
157 (void)pid;
158}
159
160/*
161 * Note: This function is defined in unistd.h
162 * It is used to get the current PID.
163 */
164pid_t _getpid(void)
165{
166 return 0;
167}
168
172static uint8_t *__sbrk_heap_end = NULL;
173
174/*
175 * Note: This function is defined in unistd.h
176 * It is used by libc to determine the heap size for malloc().
177 */
178void *_sbrk(ptrdiff_t incr)
179{
180 /* The bootloader needs a basic malloc() implementation */
181 extern uint8_t _end; /* Symbol defined in the linker script */
182 extern uint8_t _estack; /* Symbol defined in the linker script */
183 extern uint32_t _Min_Stack_Size; /* Symbol defined in the linker script */
184 const uint32_t stack_limit = (uint32_t)&_estack - (uint32_t)&_Min_Stack_Size;
185 const uint8_t *max_heap = (uint8_t *)stack_limit;
186 uint8_t *prev_heap_end;
187
188 /* Initialize heap end at first call */
189 if (NULL == __sbrk_heap_end)
190 {
191 __sbrk_heap_end = &_end;
192 }
193
194 /* Protect heap from growing into the reserved MSP stack */
195 if (__sbrk_heap_end + incr > max_heap)
196 {
197 errno = ENOMEM;
198 return (void *)-1;
199 }
200
201 prev_heap_end = __sbrk_heap_end;
202 __sbrk_heap_end += incr;
203
204 return (void *)prev_heap_end;
205}
void mmosal_log_failure_info(const struct mmosal_failure_info *info)
Log failure information in a way that it is preserved across reboots so that it can be available for ...
void mmosal_impl_assert(void)
Assertion handler implementation.
int mmosal_main(mmosal_app_init_cb_t app_init_cb)
OS main function.
void(* mmosal_app_init_cb_t)(void)
Application initialization callback (see mmosal_main for details).
Definition: mmosal.h:39
void * mmosal_calloc(size_t nitems, size_t size)
Equivalent of standard library calloc().
void * mmosal_malloc_dbg(size_t size, const char *name, unsigned line_number)
Allocate memory of the given size and return a pointer to it (malloc) – debug version.
void mmosal_free(void *p)
Free the given memory allocation.
void * mmosal_malloc_(size_t size)
Allocate memory of the given size and return a pointer to it (malloc).
struct mmosal_mutex * mmosal_mutex_create(const char *name)
Create a new mutex.
bool mmosal_mutex_release(struct mmosal_mutex *mutex)
Release a mutex.
void mmosal_mutex_delete(struct mmosal_mutex *mutex)
Delete a mutex.
bool mmosal_mutex_get(struct mmosal_mutex *mutex, uint32_t timeout_ms)
Acquire a mutex.
bool mmosal_semb_give_from_isr(struct mmosal_semb *semb)
Give a binary semaphore (from ISR context).
bool mmosal_semb_wait(struct mmosal_semb *semb, uint32_t timeout_ms)
Wait for a counting semaphore.
struct mmosal_semb * mmosal_semb_create(const char *name)
Create a new binary semaphore.
bool mmosal_semb_give(struct mmosal_semb *semb)
Give a binary semaphore.
void mmosal_semb_delete(struct mmosal_semb *semb)
Delete the given binary semaphore.
uint32_t mmosal_get_time_ms(void)
Get the system time in milliseconds.
uint32_t mmosal_get_time_ticks(void)
Get the system time in ticks.
#define MM_UNUSED(_x)
Casts the given expression to void to avoid "unused" warnings from the compiler.
Definition: mmutils.h:69
Data structure used to store information about a failure that can be preserved across reset.
Definition: mmosal.h:869