Skip to content

Commit

Permalink
Do not use vfprintf for Zephyr it causes strange intermittent memory …
Browse files Browse the repository at this point in the history
…issues
  • Loading branch information
erlingrj committed Sep 21, 2024
1 parent 400edb8 commit 85e56ec
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions low_level_platform/api/platform/lf_zephyr_support.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ typedef struct {
} lf_cond_t;
typedef struct k_thread* lf_thread_t;

void _lf_initialize_clock_zephyr_common();

#endif // !LF_SINGLE_THREADED

#endif // LF_ZEPHYR_SUPPORT_H
2 changes: 2 additions & 0 deletions low_level_platform/impl/src/lf_zephyr_clock_counter.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ void _lf_initialize_clock() {
uint32_t counter_max_ticks = 0;
int res;

_lf_initialize_clock_zephyr_common();

// Verify that we have the device
if (!device_is_ready(counter_dev)) {
lf_print_error_and_exit("ERROR: counter device not ready.\n");
Expand Down
1 change: 1 addition & 0 deletions low_level_platform/impl/src/lf_zephyr_clock_kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ static volatile bool async_event = false;
K_SEM_DEFINE(sleeping_sem, 0, 1)

void _lf_initialize_clock() {
_lf_initialize_clock_zephyr_common();
timer_freq = CONFIG_SYS_CLOCK_TICKS_PER_SEC;
lf_print("--- Using LF Zephyr Kernel Clock with a frequency of %u Hz", timer_freq);
}
Expand Down
13 changes: 13 additions & 0 deletions low_level_platform/impl/src/lf_zephyr_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,27 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "platform/lf_platform_util.h"
#include "low_level_platform.h"
#include "tag.h"
#include "logging.h"

#include <zephyr/kernel.h>
#include <zephyr/sys/cbprintf.h>

// Keep track of nested critical sections
static uint32_t num_nested_critical_sections = 0;
// Keep track of IRQ mask when entering critical section so we can enable again after
static volatile unsigned irq_mask = 0;

// Catch kernel panics from Zephyr
void k_sys_fatal_error_handler(unsigned int reason, const struct arch_esf* esf) {
lf_print_error_and_exit("Zephyr kernel panic reason=%d", reason);
}

void _lf_initialize_clock_zephyr_common() {
// Use the Zephyr implementation of printf. This avoids some wierd memory
// issues that intermittently arise when calling vfprintf.
lf_register_print_function(vfprintfcb, LOG_LEVEL_ERROR);
}

int lf_sleep(interval_t sleep_duration) {
k_sleep(K_NSEC(sleep_duration));
return 0;
Expand Down

0 comments on commit 85e56ec

Please sign in to comment.