firmware/common: Add a few helpers

Signed-off-by: Sylvain Munaut <tnt@246tNt.com>
This commit is contained in:
Sylvain Munaut 2022-05-02 15:32:38 +02:00
parent de10cfd45e
commit a6a5c12c1e
4 changed files with 66 additions and 0 deletions

View File

@ -39,6 +39,13 @@ static volatile struct wb_misc * const misc_regs = (void*)(MISC_BASE);
#define GPIO_SC_CLKDIV_8 (0 << 0)
/* Warm reboot */
void
reboot(int n)
{
misc_regs->boot = (1 << 2) | (n << 0);
}
/* "Calibrated" wait loop */
void
wait(int i)

View File

@ -7,6 +7,7 @@
#pragma once
void reboot(int n);
void wait(int i);
void ncn8025_init(void);

45
firmware/common/utils.c Normal file
View File

@ -0,0 +1,45 @@
/*
* utils.c
*
* Copyright (C) 2019-2020 Sylvain Munaut <tnt@246tNt.com>
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#include <stdarg.h>
#include <stdint.h>
#include <stdbool.h>
char *
hexstr(void *d, int n, bool space)
{
static const char * const hex = "0123456789abcdef";
static char buf[96];
uint8_t *p = d;
char *s = buf;
char c;
while (n--) {
c = *p++;
*s++ = hex[c >> 4];
*s++ = hex[c & 0xf];
if (space)
*s++ = ' ';
}
s[space?-1:0] = '\0';
return buf;
}
uint8_t
hexval(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
else if (c >= 'a' && c <= 'f')
return 10 + (c - 'a');
else if (c >= 'A' && c <= 'F')
return 10 + (c - 'A');
else
return 0;
}

13
firmware/common/utils.h Normal file
View File

@ -0,0 +1,13 @@
/*
* utils.h
*
* Copyright (C) 2019-2020 Sylvain Munaut <tnt@246tNt.com>
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#pragma once
#include <stdbool.h>
char *hexstr(void *d, int n, bool space);
uint8_t hexval(char c);