Skip to content

Commit

Permalink
Add OS support
Browse files Browse the repository at this point in the history
  • Loading branch information
MaJerle committed Aug 23, 2020
1 parent 7dcc865 commit e167fb0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
5 changes: 4 additions & 1 deletion lwprintf/src/include/lwprintf/lwprintf.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ typedef int (*lwprintf_output_fn)(int ch, struct lwprintf* lw);
* \brief LwPRINTF instance
*/
typedef struct lwprintf {
lwprintf_output_fn out; /*!< Output function for direct print operations */
lwprintf_output_fn out_fn; /*!< Output function for direct print operations */
#if LWPRINTF_CFG_OS || __DOXYGEN__
LWPRINTF_CFG_OS_MUTEX_HANDLE mutex; /*!< OS mutex handle */
#endif /* LWPRINTF_CFG_OS || __DOXYGEN__ */
} lwprintf_t;

uint8_t lwprintf_init_ex(lwprintf_t* lw, lwprintf_output_fn out_fn);
Expand Down
28 changes: 23 additions & 5 deletions lwprintf/src/lwprintf/lwprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ prv_default_output_func(int ch, struct lwprintf* lw) {
*/
static lwprintf_t
lwprintf_default = {
.out = prv_default_output_func
.out_fn = prv_default_output_func
};

/**
Expand Down Expand Up @@ -195,7 +195,7 @@ prv_rotate_string(char* str, size_t len) {
*/
static int
prv_out_fn_print(lwprintf_int_t* p, const char c) {
p->lw->out(c, p->lw); /*!< Send character to output */
p->lw->out_fn(c, p->lw); /*!< Send character to output */
++p->n;
return 1;
}
Expand Down Expand Up @@ -503,10 +503,17 @@ prv_format(lwprintf_int_t* p, va_list arg) {
uint8_t detected = 0;
const char* fmt = p->fmt;

#if LWPRINTF_CFG_OS
if (!lwprintf_sys_mutex_isvalid(&p->lw) /* Invalid mutex handle */
|| !lwprintf_sys_mutex_wait(&p->lw)) { /* Cannot acquire mutex */
return 0;
}
#endif /* LWPRINTF_CFG_OS */

while (fmt != NULL && *fmt != '\0') {
/* Parse format */
/* %[parameter][flags][width][.precision][length]type */
/* Go to https://en.wikipedia.org/wiki/Printf_format_string for more info */
/* %[flags][width][.precision][length]type */
/* Go to https://docs.majerle.eu for more info about supported features */
memset(&p->m, 0x00, sizeof(p->m)); /* Reset structure */

/* Detect beginning */
Expand Down Expand Up @@ -771,6 +778,9 @@ prv_format(lwprintf_int_t* p, va_list arg) {
}
++fmt;
}
#if LWPRINTF_CFG_OS
lwprintf_sys_mutex_release(&p->lw);
#endif /* LWPRINTF_CFG_OS */
return 1;
}

Expand All @@ -782,7 +792,15 @@ prv_format(lwprintf_int_t* p, va_list arg) {
*/
uint8_t
lwprintf_init_ex(lwprintf_t* lw, lwprintf_output_fn out_fn) {
LWPRINTF_GET_LW(lw)->out = out_fn;
LWPRINTF_GET_LW(lw)->out_fn = out_fn;

#if LWPRINTF_CFG_OS
/* Create system mutex */
if (lwprintf_sys_mutex_isvalid(&LWPRINTF_GET_LW(lw)->mutex)
|| !lwprintf_sys_mutex_create(&LWPRINTF_GET_LW(lw)->mutex)) {
return 0;
}
#endif /* LWPRINTF_CFG_OS */
return 1;
}

Expand Down
2 changes: 0 additions & 2 deletions lwprintf/src/system/lwprintf_sys_template.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@

#if LWPRINTF_CFG_OS || __DOXYGEN__

#include "cmsis_os.h"

/**
* \brief Create a new mutex and assign value to handle
* \param[out] m: Output variable to save mutex handle
Expand Down

0 comments on commit e167fb0

Please sign in to comment.