Morse Micro IoT SDK  2.10.4
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}
114
116{
117 MM_UNUSED(info);
118}
119
120/* --------------------------------------------------------------------------------------------- */
121
122uint32_t mmosal_get_time_ms(void)
123{
124 return mmosal_get_time_ticks();
125}
126
128{
129 static uint32_t tick = 0;
130 static uint16_t microtick = 0;
131
132 if (microtick++ == LOOPS_PER_MS)
133 {
134 tick++;
135 microtick = 0;
136 }
137 return tick;
138}
139
140/*
141 * Note: This function is defined in unistd.h
142 * It is called when main() exits.
143 */
144void _exit(int status)
145{
146 (void)status;
147 while (1)
148 {
149 }
150}
151
152/*
153 * Note: This function is defined in unistd.h
154 * It is used to kill a thread.
155 */
156void _kill(pid_t pid)
157{
158 (void)pid;
159}
160
161/*
162 * Note: This function is defined in unistd.h
163 * It is used to get the current PID.
164 */
165pid_t _getpid(void)
166{
167 return 0;
168}
169
173static uint8_t *__sbrk_heap_end = NULL;
174
175/*
176 * Note: This function is defined in unistd.h
177 * It is used by libc to determine the heap size for malloc().
178 */
179void *_sbrk(ptrdiff_t incr)
180{
181 /* The bootloader needs a basic malloc() implementation */
182 extern uint8_t _end; /* Symbol defined in the linker script */
183 extern uint8_t _estack; /* Symbol defined in the linker script */
184 extern uint32_t _Min_Stack_Size; /* Symbol defined in the linker script */
185 const uint32_t stack_limit = (uint32_t)&_estack - (uint32_t)&_Min_Stack_Size;
186 const uint8_t *max_heap = (uint8_t *)stack_limit;
187 uint8_t *prev_heap_end;
188
189 /* Initialize heap end at first call */
190 if (NULL == __sbrk_heap_end)
191 {
192 __sbrk_heap_end = &_end;
193 }
194
195 /* Protect heap from growing into the reserved MSP stack */
196 if (__sbrk_heap_end + incr > max_heap)
197 {
198 errno = ENOMEM;
199 return (void *)-1;
200 }
201
202 prev_heap_end = __sbrk_heap_end;
203 __sbrk_heap_end += incr;
204
205 return (void *)prev_heap_end;
206}
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:40
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:70
Data structure used to store information about a failure that can be preserved across reset.
Definition: mmosal.h:875