mirror of
https://github.com/tillitis/tillitis-key1.git
synced 2025-07-29 09:48:34 -04:00
ch552: Split functionality into separate files
- Move GPIO functions to gpio.c and gpio.h - Move string handling functions for debug to lib.c and lib.h - Add config.h and move config defines there - Add debug print functionality via FPGA with framing protocol - Add Flash Code and Flash Data functions to flash.c and flash.h
This commit is contained in:
parent
7de49d2021
commit
d94387a9e7
13 changed files with 560 additions and 262 deletions
200
hw/usb_interface/ch552_fw/src/lib.c
Normal file
200
hw/usb_interface/ch552_fw/src/lib.c
Normal file
|
@ -0,0 +1,200 @@
|
|||
#include <stdint.h>
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#ifdef USE_NUM_U8
|
||||
int8_t uint8_to_str(uint8_t *buf, uint8_t bufsize, uint8_t n)
|
||||
{
|
||||
uint8_t *start;
|
||||
|
||||
#ifdef USE_NEGATIVE_NUMS
|
||||
if (n < 0) { // Handle negative numbers.
|
||||
if (!bufsize) {
|
||||
return -1;
|
||||
}
|
||||
*buf++ = '-';
|
||||
bufsize--;
|
||||
}
|
||||
#endif
|
||||
|
||||
start = buf; // Remember the start of the string. This will come into play at the end.
|
||||
|
||||
do {
|
||||
// Handle the current digit.
|
||||
uint8_t digit;
|
||||
if (!bufsize) {
|
||||
return -1;
|
||||
}
|
||||
digit = n % 10;
|
||||
#ifdef USE_NEGATIVE_NUMS
|
||||
if (digit < 0) {
|
||||
digit *= -1;
|
||||
}
|
||||
#endif
|
||||
*buf++ = digit + '0';
|
||||
bufsize--;
|
||||
n /= 10;
|
||||
} while (n);
|
||||
|
||||
// Terminate the string.
|
||||
if (!bufsize) {
|
||||
return -1;
|
||||
}
|
||||
*buf = 0;
|
||||
|
||||
// We wrote the string backwards, i.e. with least significant digits first. Now reverse the string.
|
||||
--buf;
|
||||
while (start < buf) {
|
||||
uint8_t a = *start;
|
||||
*start = *buf;
|
||||
*buf = a;
|
||||
++start;
|
||||
--buf;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_NUM_U32
|
||||
int8_t uint32_to_str(uint8_t *buf, uint8_t bufsize, uint32_t n)
|
||||
{
|
||||
uint8_t *start;
|
||||
|
||||
#ifdef USE_NEGATIVE_NUMS
|
||||
if (n < 0) { // Handle negative numbers.
|
||||
if (!bufsize) {
|
||||
return -1;
|
||||
}
|
||||
*buf++ = '-';
|
||||
bufsize--;
|
||||
}
|
||||
#endif
|
||||
|
||||
start = buf; // Remember the start of the string. This will come into play at the end.
|
||||
|
||||
do {
|
||||
// Handle the current digit.
|
||||
uint8_t digit;
|
||||
if (!bufsize) {
|
||||
return -1;
|
||||
}
|
||||
digit = n % 10;
|
||||
#ifdef USE_NEGATIVE_NUMS
|
||||
if (digit < 0) {
|
||||
digit *= -1;
|
||||
}
|
||||
#endif
|
||||
*buf++ = digit + '0';
|
||||
bufsize--;
|
||||
n /= 10;
|
||||
} while (n);
|
||||
|
||||
// Terminate the string.
|
||||
if (!bufsize) {
|
||||
return -1;
|
||||
}
|
||||
*buf = 0;
|
||||
|
||||
// We wrote the string backwards, i.e. with least significant digits first. Now reverse the string.
|
||||
--buf;
|
||||
while (start < buf) {
|
||||
uint8_t a = *start;
|
||||
*start = *buf;
|
||||
*buf = a;
|
||||
++start;
|
||||
--buf;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
uint8_t hex_to_uint8(const char *hex, uint8_t len)
|
||||
{
|
||||
uint8_t val = 0;
|
||||
while (len) {
|
||||
char c = *hex++;
|
||||
val <<= 4;
|
||||
if (c >= '0' && c <= '9') {
|
||||
val |= c - '0';
|
||||
} else if (c >= 'A' && c <= 'F') {
|
||||
val |= c - 'A' + 10;
|
||||
} else if (c >= 'a' && c <= 'f') {
|
||||
val |= c - 'a' + 10;
|
||||
} else {
|
||||
return 0; // Invalid character
|
||||
}
|
||||
len--;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
uint16_t hex_to_uint16(const char *hex, uint8_t len)
|
||||
{
|
||||
uint16_t val = 0;
|
||||
while (len) {
|
||||
char c = *hex++;
|
||||
val <<= 4;
|
||||
if (c >= '0' && c <= '9') {
|
||||
val |= c - '0';
|
||||
} else if (c >= 'A' && c <= 'F') {
|
||||
val |= c - 'A' + 10;
|
||||
} else if (c >= 'a' && c <= 'f') {
|
||||
val |= c - 'a' + 10;
|
||||
} else {
|
||||
return 0; // Invalid character
|
||||
}
|
||||
len--;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
#endif
|
||||
|
||||
uint32_t hex_to_uint32(const char *hex, uint8_t len)
|
||||
{
|
||||
uint32_t val = 0;
|
||||
while (len) {
|
||||
char c = *hex++;
|
||||
val <<= 4;
|
||||
if (c >= '0' && c <= '9') {
|
||||
val |= c - '0';
|
||||
} else if (c >= 'A' && c <= 'F') {
|
||||
val |= c - 'A' + 10;
|
||||
} else if (c >= 'a' && c <= 'f') {
|
||||
val |= c - 'a' + 10;
|
||||
} else {
|
||||
return 0; // Invalid character
|
||||
}
|
||||
len--;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
uint8_t ascii_hex_char_to_byte(uint8_t c)
|
||||
{
|
||||
if (c >= '0' && c <= '9')
|
||||
return c - '0';
|
||||
if (c >= 'A' && c <= 'F')
|
||||
return c - 'A' + 10;
|
||||
if (c >= 'a' && c <= 'f')
|
||||
return c - 'a' + 10;
|
||||
return 0; // Invalid character, should not happen if input is valid
|
||||
}
|
||||
|
||||
#if 0
|
||||
int ascii_hex_string_to_bytes(uint8_t *hex_str, uint8_t *out_bytes, size_t out_len)
|
||||
{
|
||||
if (!hex_str || !out_bytes || out_len < 32)
|
||||
return -1; // Error handling
|
||||
|
||||
for (size_t i = 0; i < 32; i++) {
|
||||
out_bytes[i] = (ascii_hex_char_to_byte(hex_str[i * 2]) << 4) | ascii_hex_char_to_byte(hex_str[i * 2 + 1]);
|
||||
}
|
||||
|
||||
return 0; // Success
|
||||
}
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue