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:
Jonas Thörnblad 2025-03-24 14:14:43 +01:00 committed by Michael Cardell Widerkrantz
parent 7de49d2021
commit d94387a9e7
No known key found for this signature in database
GPG key ID: D3DB3DDF57E704E5
13 changed files with 560 additions and 262 deletions

View file

@ -1,15 +1,28 @@
#include <stdint.h>
#include <string.h>
#include "config.h"
#include "debug.h"
#include "lib.h"
#include "mem.h"
#include "print.h"
#define MODE_CH552 0x10
void printStr(uint8_t *str)
{
#ifdef DEBUG_PRINT
#ifdef USE_DEBUG_PRINT
#if defined(DEBUG_PRINT_HW)
while (*str != 0) {
CH554UART0SendByte(*str);
++str;
}
#elif defined(DEBUG_PRINT_SW)
uint32_t str_len = strlen(str);
CH554UART1SendByte(MODE_CH552);
CH554UART1SendByte(str_len);
CH554UART1SendBuffer(str, str_len);
#endif
#else
(void)str;
#endif
@ -17,131 +30,31 @@ void printStr(uint8_t *str)
void printChar(uint8_t c)
{
#ifdef DEBUG_PRINT
#ifdef USE_DEBUG_PRINT
#if defined(DEBUG_PRINT_HW)
CH554UART0SendByte(c);
#elif defined(DEBUG_PRINT_SW)
CH554UART1SendByte(MODE_CH552);
CH554UART1SendByte(1);
CH554UART1SendByte(c);
#endif
#else
(void)c;
#endif
}
#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
#ifdef USE_NUM_U8
void printNumU8(uint8_t num)
{
#ifdef DEBUG_PRINT
uint8_t num_str[4] = { 0 };
#ifdef USE_DEBUG_PRINT
int8_t ret;
uint8_t num_str[4] = { 0 };
ret = uint8_to_str(num_str, 4, num);
if (!ret) {
printStr(num_str);
}
#else
(void)num;
#endif
}
#endif
@ -149,9 +62,9 @@ void printNumU8(uint8_t num)
#ifdef USE_NUM_U32
void printNumU32(uint32_t num)
{
#ifdef DEBUG_PRINT
uint8_t num_str[11] = { 0 };
#ifdef USE_DEBUG_PRINT
int8_t ret;
uint8_t num_str[11] = { 0 };
ret = uint32_to_str(num_str, 10, num);
if (!ret) {
printStr(num_str);
@ -164,7 +77,7 @@ void printNumU32(uint32_t num)
void printNumHex(uint8_t num)
{
#ifdef DEBUG_PRINT
#ifdef USE_DEBUG_PRINT
// High nibble
uint8_t val = num >> 4;
if (val <= 9) {
@ -186,3 +99,47 @@ void printNumHex(uint8_t num)
(void)num;
#endif
}
#ifdef USE_NUM_U8HEX
void printNumU8Hex(uint8_t num)
{
#ifdef USE_DEBUG_PRINT
printStr("0x");
printNumHex(num);
#else
(void)num;
#endif
}
#endif
#ifdef USE_NUM_U16HEX
void printNumU16Hex(uint16_t num)
{
#ifdef USE_DEBUG_PRINT
uint8_t buf[2];
memcpy(buf, &num, 2);
printStr("0x");
for (int8_t i = 1; i > -1; i--) {
printNumHex(buf[i]);
}
#else
(void)num;
#endif
}
#endif
#ifdef USE_NUM_U32HEX
void printNumU32Hex(uint32_t num)
{
#ifdef USE_DEBUG_PRINT
uint8_t buf[4];
memcpy(buf, &num, 4);
printStr("0x");
for (int8_t i = 3; i > -1; i--) {
printNumHex(buf[i]);
}
#else
(void)num;
#endif
}
#endif