Morse Micro IoT SDK  2.6.4-esp32
mmosal.h
1/*
2 * Copyright 2021-2023 Morse Micro
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
16#pragma once
17
18#include <stdbool.h>
19#include <stdint.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
24#include "mmport.h"
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
39typedef void (*mmosal_app_init_cb_t)(void);
40
55
60/*
61 * ---------------------------------------------------------------------------------------------
62 */
63
83void *mmosal_malloc_(size_t size);
84
98void *mmosal_malloc_dbg(size_t size, const char *name, unsigned line_number);
99
105void mmosal_free(void *p);
106
115void *mmosal_realloc(void *ptr, size_t size);
116
125void *mmosal_calloc(size_t nitems, size_t size);
126
127#ifndef MMOSAL_TRACK_ALLOCATIONS
137#define mmosal_malloc(size) mmosal_malloc_(size)
138#else
139/* Note: we use function name because it should be shorter than the full filename path. */
149#define mmosal_malloc(size) mmosal_malloc_dbg(size, __FUNCTION__, __LINE__)
150#endif
151
156/*
157 * ---------------------------------------------------------------------------------------------
158 */
159
173typedef void (*mmosal_task_fn_t)(void *arg);
174
177{
188};
189
201struct mmosal_task *mmosal_task_create(mmosal_task_fn_t task_fn, void *argument,
202 enum mmosal_task_priority priority,
203 unsigned stack_size_u32, const char *name);
204
212void mmosal_task_delete(struct mmosal_task *task);
213
224void mmosal_task_join(struct mmosal_task *task);
225
231struct mmosal_task *mmosal_task_get_active(void);
232
237
243void mmosal_task_sleep(uint32_t duration_ms);
244
252#define MMOSAL_TASK_ENTER_CRITICAL() \
253 do { \
254 MMPORT_MEM_SYNC(); \
255 mmosal_task_enter_critical(); \
256 } while (0)
257
263#define MMOSAL_TASK_EXIT_CRITICAL() \
264 do { \
265 MMPORT_MEM_SYNC(); \
266 mmosal_task_exit_critical(); \
267 } while (0)
268
279
288
296
304
310const char *mmosal_task_name(void);
311
324bool mmosal_task_wait_for_notification(uint32_t timeout_ms);
325
335void mmosal_task_notify(struct mmosal_task *task);
336
346void mmosal_task_notify_from_isr(struct mmosal_task *task);
347
352/*
353 * ---------------------------------------------------------------------------------------------
354 */
355
371struct mmosal_mutex *mmosal_mutex_create(const char *name);
372
378void mmosal_mutex_delete(struct mmosal_mutex *mutex);
379
388bool mmosal_mutex_get(struct mmosal_mutex *mutex, uint32_t timeout_ms);
389
397bool mmosal_mutex_release(struct mmosal_mutex *mutex);
398
402#define MMOSAL_MUTEX_GET_INF(_mutex) \
403 do { \
404 bool ok__ = mmosal_mutex_get((_mutex), UINT32_MAX); \
405 MMOSAL_ASSERT(ok__); \
406 } while (0)
407
411#define MMOSAL_MUTEX_RELEASE(_mutex) \
412 do { \
413 bool ok__ = mmosal_mutex_release(_mutex); \
414 MMOSAL_ASSERT(ok__); \
415 } while (0)
416
424bool mmosal_mutex_is_held_by_active_task(struct mmosal_mutex *mutex);
425
430/*
431 * ---------------------------------------------------------------------------------------------
432 */
433
452struct mmosal_sem *mmosal_sem_create(unsigned max_count, unsigned initial_count, const char *name);
453
459void mmosal_sem_delete(struct mmosal_sem *sem);
460
470bool mmosal_sem_give(struct mmosal_sem *sem);
471
481bool mmosal_sem_give_from_isr(struct mmosal_sem *sem);
482
491bool mmosal_sem_wait(struct mmosal_sem *sem, uint32_t timeout_ms);
492
500uint32_t mmosal_sem_get_count(struct mmosal_sem *sem);
501
506/*
507 * ---------------------------------------------------------------------------------------------
508 */
509
525struct mmosal_semb *mmosal_semb_create(const char *name);
526
532void mmosal_semb_delete(struct mmosal_semb *semb);
533
543bool mmosal_semb_give(struct mmosal_semb *semb);
544
554bool mmosal_semb_give_from_isr(struct mmosal_semb *semb);
555
564bool mmosal_semb_wait(struct mmosal_semb *semb, uint32_t timeout_ms);
565
570/*
571 * ---------------------------------------------------------------------------------------------
572 */
573
591struct mmosal_queue *mmosal_queue_create(size_t num_items, size_t item_size, const char *name);
592
598void mmosal_queue_delete(struct mmosal_queue *queue);
599
613bool mmosal_queue_pop(struct mmosal_queue *queue, void *item, uint32_t timeout_ms);
614
628bool mmosal_queue_push(struct mmosal_queue *queue, const void *item, uint32_t timeout_ms);
629
641bool mmosal_queue_pop_from_isr(struct mmosal_queue *queue, void *item);
642
654bool mmosal_queue_push_from_isr(struct mmosal_queue *queue, const void *item);
655
660/*
661 * ---------------------------------------------------------------------------------------------
662 */
663
677uint32_t mmosal_get_time_ms(void);
678
685
692
710static inline bool mmosal_time_lt(uint32_t a, uint32_t b)
711{
712 return ((int32_t)(a - b)) < 0;
713}
714
715
733static inline bool mmosal_time_le(uint32_t a, uint32_t b)
734{
735 return ((int32_t)(a - b)) <= 0;
736}
737
754static inline uint32_t mmosal_time_max(uint32_t a, uint32_t b)
755{
756 if (mmosal_time_lt(a, b))
757 {
758 return b;
759 }
760 else
761 {
762 return a;
763 }
764}
765
773static inline bool mmosal_time_has_passed(uint32_t t)
774{
776}
777
782/*
783 * ---------------------------------------------------------------------------------------------
784 */
785
801struct mmosal_timer;
802
808typedef void (*timer_callback_t)(struct mmosal_timer *timer);
809
826struct mmosal_timer *mmosal_timer_create(const char *name, uint32_t timer_period_ms,
827 bool auto_reload, void *arg, timer_callback_t callback);
828
829
835void mmosal_timer_delete(struct mmosal_timer *timer);
836
844bool mmosal_timer_start(struct mmosal_timer *timer);
845
853bool mmosal_timer_stop(struct mmosal_timer *timer);
854
855
866bool mmosal_timer_change_period(struct mmosal_timer *timer, uint32_t new_period);
867
875void *mmosal_timer_get_arg(struct mmosal_timer *timer);
876
884bool mmosal_is_timer_active(struct mmosal_timer *timer);
885
890/*
891 * ---------------------------------------------------------------------------------------------
892 */
893
902#ifndef MMOSAL_FILEID
904#define MMOSAL_FILEID 0
905#endif
906
909{
911 uint32_t pc;
913 uint32_t lr;
915 uint32_t fileid;
917 uint32_t line;
920 uint32_t platform_info[4];
921};
922
930
931#ifdef MMOSAL_NO_DEBUGLOG
933#define MMOSAL_LOG_FAILURE_INFO(...)
934#else
937#define MMOSAL_LOG_FAILURE_INFO(...) \
938 do { \
939 void *pc; \
940 struct mmosal_failure_info info = { \
941 .lr = (uint32_t)MMPORT_GET_LR(), \
942 .fileid = MMOSAL_FILEID, \
943 .line = __LINE__, \
944 .platform_info = { __VA_ARGS__ }, \
945 }; \
946 MMPORT_GET_PC(pc); \
947 info.pc = (uint32_t)pc; \
948 mmosal_log_failure_info(&info); \
949 } while (0)
950#endif
951
958
959#ifndef MMOSAL_NOASSERT
966#ifndef MMOSAL_ASSERT
967#define MMOSAL_ASSERT(expr) \
968 do { \
969 if (!(expr)) { \
970 MMOSAL_LOG_FAILURE_INFO(0); \
971 mmosal_impl_assert(); \
972 while (1) \
973 {} \
974 } \
975 } while (0)
976#endif
977
985#ifndef MMOSAL_ASSERT_LOG_DATA
986#define MMOSAL_ASSERT_LOG_DATA(expr, ...) \
987 do { \
988 if (!(expr)) { \
989 MMOSAL_LOG_FAILURE_INFO(__VA_ARGS__); \
990 mmosal_impl_assert(); \
991 while (1) \
992 {} \
993 } \
994 } while (0)
995#endif
996#else
998#define MMOSAL_ASSERT(expr) (void)(expr)
1000#define MMOSAL_ASSERT_LOG_DATA(expre, ...) (void)(expr)
1001#endif
1002
1024static inline bool mmosal_safer_strcpy(char *dst, const char *src, size_t size)
1025{
1026 bool ret;
1027
1028 if (size == 0)
1029 {
1030 return true;
1031 }
1032
1033 strncpy(dst, src, size);
1034
1035 ret = dst[size-1] != '\0';
1036 dst[size-1] = '\0';
1037 return ret;
1038}
1039
1042#ifdef __cplusplus
1043}
1044#endif
1045
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).
void * mmosal_realloc(void *ptr, size_t size)
Equivalent of standard library realloc().
static bool mmosal_safer_strcpy(char *dst, const char *src, size_t size)
A safer version of strncpy.
Definition: mmosal.h:1024
struct mmosal_mutex * mmosal_mutex_create(const char *name)
Create a new mutex.
bool mmosal_mutex_is_held_by_active_task(struct mmosal_mutex *mutex)
Check whether the given mutex is held by the active thread.
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_queue_pop_from_isr(struct mmosal_queue *queue, void *item)
Pop an item from the queue (from ISR context).
struct mmosal_queue * mmosal_queue_create(size_t num_items, size_t item_size, const char *name)
Create a new queue.
bool mmosal_queue_push(struct mmosal_queue *queue, const void *item, uint32_t timeout_ms)
Push an item into the queue.
void mmosal_queue_delete(struct mmosal_queue *queue)
Delete a queue.
bool mmosal_queue_push_from_isr(struct mmosal_queue *queue, const void *item)
Push an item into the queue (from ISR context).
bool mmosal_queue_pop(struct mmosal_queue *queue, void *item, uint32_t timeout_ms)
Pop an item from the queue.
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.
bool mmosal_sem_give(struct mmosal_sem *sem)
Give a counting semaphore.
void mmosal_sem_delete(struct mmosal_sem *sem)
Delete the given counting semaphore.
bool mmosal_sem_give_from_isr(struct mmosal_sem *sem)
Give a counting semaphore (from ISR context).
struct mmosal_sem * mmosal_sem_create(unsigned max_count, unsigned initial_count, const char *name)
Create a new counting semaphore.
uint32_t mmosal_sem_get_count(struct mmosal_sem *sem)
Returns the current count of the semaphore.
bool mmosal_sem_wait(struct mmosal_sem *sem, uint32_t timeout_ms)
Wait for a counting semaphore.
void mmosal_task_join(struct mmosal_task *task)
Block until the given task has terminated.
struct mmosal_task * mmosal_task_create(mmosal_task_fn_t task_fn, void *argument, enum mmosal_task_priority priority, unsigned stack_size_u32, const char *name)
Create a new task.
void mmosal_task_notify_from_isr(struct mmosal_task *task)
Notifies a waiting task (mmosal_task_wait_for_notification()) that it can continue.
void mmosal_task_yield(void)
Yield the active task.
struct mmosal_task * mmosal_task_get_active(void)
Get the handle of the active task.
void mmosal_task_exit_critical(void)
Exit critical section.
void mmosal_enable_interrupts(void)
Enable interrupts.
const char * mmosal_task_name(void)
Get the name of the running task.
void mmosal_task_sleep(uint32_t duration_ms)
Sleep for a period of time, yielding during that time.
void(* mmosal_task_fn_t)(void *arg)
Type definition for task main functions.
Definition: mmosal.h:173
void mmosal_task_notify(struct mmosal_task *task)
Notifies a waiting task (mmosal_task_wait_for_notification()) that it can continue.
void mmosal_disable_interrupts(void)
Disable interrupts.
bool mmosal_task_wait_for_notification(uint32_t timeout_ms)
Blocks the current task until a notification is received.
void mmosal_task_delete(struct mmosal_task *task)
Delete the given task.
void mmosal_task_enter_critical(void)
Enter critical section.
mmosal_task_priority
Enumeration of task priorities (ordered lowest to highest).
Definition: mmosal.h:177
@ MMOSAL_TASK_PRI_LOW
Low priority.
Definition: mmosal.h:183
@ MMOSAL_TASK_PRI_MIN
Minimum priority.
Definition: mmosal.h:181
@ MMOSAL_TASK_PRI_HIGH
High priority.
Definition: mmosal.h:187
@ MMOSAL_TASK_PRI_NORM
Normal priority.
Definition: mmosal.h:185
@ MMOSAL_TASK_PRI_IDLE
Idle task priority.
Definition: mmosal.h:179
bool mmosal_timer_start(struct mmosal_timer *timer)
Start a timer.
void mmosal_timer_delete(struct mmosal_timer *timer)
Delete a timer.
bool mmosal_timer_change_period(struct mmosal_timer *timer, uint32_t new_period)
Change timer period.
bool mmosal_is_timer_active(struct mmosal_timer *timer)
Queries the timer to determine if it running.
void * mmosal_timer_get_arg(struct mmosal_timer *timer)
Get the opaque argument associated with a given timer.
void(* timer_callback_t)(struct mmosal_timer *timer)
Function type definition for timer callbacks.
Definition: mmosal.h:808
bool mmosal_timer_stop(struct mmosal_timer *timer)
Stop a timer.
struct mmosal_timer * mmosal_timer_create(const char *name, uint32_t timer_period_ms, bool auto_reload, void *arg, timer_callback_t callback)
Create a new timer.
uint32_t mmosal_get_time_ms(void)
Get the system time in milliseconds.
static uint32_t mmosal_time_max(uint32_t a, uint32_t b)
Given two times, return the one that is greatest (taking into account wrapping).
Definition: mmosal.h:754
uint32_t mmosal_ticks_per_second(void)
Get the number of ticks in a second.
static bool mmosal_time_le(uint32_t a, uint32_t b)
Check if time a is less than or equal to time b, taking into account wrapping.
Definition: mmosal.h:733
uint32_t mmosal_get_time_ticks(void)
Get the system time in ticks.
static bool mmosal_time_has_passed(uint32_t t)
Check if the given time has already passed.
Definition: mmosal.h:773
static bool mmosal_time_lt(uint32_t a, uint32_t b)
Check if time a is less than time b, taking into account wrapping.
Definition: mmosal.h:710
Data structure used to store information about a failure that can be preserved across reset.
Definition: mmosal.h:909
uint32_t fileid
File identifier.
Definition: mmosal.h:915
uint32_t pc
Content of the PC register at assertion.
Definition: mmosal.h:911
uint32_t lr
Content of the LR register at assertion.
Definition: mmosal.h:913
uint32_t line
Source code line at which the assertion was triggered.
Definition: mmosal.h:917
uint32_t platform_info[4]
Arbitrary platform-specific failure info.
Definition: mmosal.h:920