Skip to content

Commit

Permalink
[sw/example] improve hexstring to uint function
Browse files Browse the repository at this point in the history
  • Loading branch information
stnolting committed Jun 29, 2024
1 parent 6aafbfb commit ff2f921
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 32 deletions.
27 changes: 18 additions & 9 deletions sw/example/demo_sdi/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
// Prototypes
void sdi_put(void);
void sdi_get(void);
uint32_t hexstr_to_uint(char *buffer, uint8_t length);
uint32_t hexstr_to_uint32(char *buffer, uint8_t length);


/**********************************************************************//**
Expand Down Expand Up @@ -115,7 +115,7 @@ void sdi_put(void) {

neorv32_uart0_printf("Enter TX data (2 hex chars): 0x");
neorv32_uart0_scan(terminal_buffer, sizeof(terminal_buffer), 1);
uint32_t tx_data = (uint32_t)hexstr_to_uint(terminal_buffer, strlen(terminal_buffer));
uint32_t tx_data = (uint32_t)hexstr_to_uint32(terminal_buffer, strlen(terminal_buffer));

neorv32_uart0_printf("\nWriting 0x%x to SDI TX buffer... ", tx_data);

Expand Down Expand Up @@ -145,30 +145,39 @@ void sdi_get(void) {


/**********************************************************************//**
* Helper function to convert N hex chars string into uint32_T
* Helper function to convert N hex chars string into uint32_t
*
* @param[in,out] buffer Pointer to array of chars to convert into number.
* @param[in,out] length Length of the conversion string.
* @return Converted number.
**************************************************************************/
uint32_t hexstr_to_uint(char *buffer, uint8_t length) {
uint32_t hexstr_to_uint32(char *buffer, uint8_t length) {

uint32_t res = 0, d = 0;
char c = 0;

while (length--) {
c = *buffer++;

if ((c >= '0') && (c <= '9'))
if (c == '\0') {
break;
}

if ((c >= '0') && (c <= '9')) {
d = (uint32_t)(c - '0');
else if ((c >= 'a') && (c <= 'f'))
}
else if ((c >= 'a') && (c <= 'f')) {
d = (uint32_t)((c - 'a') + 10);
else if ((c >= 'A') && (c <= 'F'))
}
else if ((c >= 'A') && (c <= 'F')) {
d = (uint32_t)((c - 'A') + 10);
else
}
else {
d = 0;
}

res = res + (d << (length*4));
res <<= 4;
res |= d & 0xf;
}

return res;
Expand Down
35 changes: 22 additions & 13 deletions sw/example/demo_spi/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ uint32_t spi_configured;
void spi_cs(uint32_t type);
void spi_trans(void);
void spi_setup(void);
uint32_t hexstr_to_uint(char *buffer, uint8_t length);
uint32_t hexstr_to_uint32(char *buffer, uint8_t length);
void aux_print_hex_byte(uint8_t byte);


Expand Down Expand Up @@ -142,7 +142,7 @@ void spi_cs(uint32_t type) {
neorv32_uart0_printf("Chip-select line to ENABLE (set low) [0..7]: ");
while (1) {
neorv32_uart0_scan(terminal_buffer, 2, 1); // 1 hex char plus '\0'
channel = (uint8_t)hexstr_to_uint(terminal_buffer, strlen(terminal_buffer));
channel = (uint8_t)hexstr_to_uint32(terminal_buffer, strlen(terminal_buffer));
if (channel > 7) {
neorv32_uart0_printf("\nInvalid channel selection!\n");
return;
Expand Down Expand Up @@ -175,7 +175,7 @@ void spi_trans(void) {

neorv32_uart0_printf("Enter TX data (2 hex chars): 0x");
neorv32_uart0_scan(terminal_buffer, 2+1, 1);
uint32_t tx_data = (uint32_t)hexstr_to_uint(terminal_buffer, strlen(terminal_buffer));
uint32_t tx_data = (uint32_t)hexstr_to_uint32(terminal_buffer, strlen(terminal_buffer));

uint32_t rx_data = neorv32_spi_trans(tx_data);

Expand Down Expand Up @@ -203,7 +203,7 @@ void spi_setup(void) {
while (1) {
neorv32_uart0_printf("Select SPI clock prescaler (0..7): ");
neorv32_uart0_scan(terminal_buffer, 2, 1);
tmp = (uint32_t)hexstr_to_uint(terminal_buffer, strlen(terminal_buffer));
tmp = (uint32_t)hexstr_to_uint32(terminal_buffer, strlen(terminal_buffer));
if (tmp > 8) {
neorv32_uart0_printf("\nInvalid selection!\n");
}
Expand All @@ -215,7 +215,7 @@ void spi_setup(void) {

neorv32_uart0_printf("\nEnter clock divider (0..15, as one hex char): ");
neorv32_uart0_scan(terminal_buffer, 2, 1);
clk_div = (uint8_t)hexstr_to_uint(terminal_buffer, strlen(terminal_buffer));
clk_div = (uint8_t)hexstr_to_uint32(terminal_buffer, strlen(terminal_buffer));

uint32_t clock = NEORV32_SYSINFO->CLK / (2 * PRSC_LUT[spi_prsc] * (1 + clk_div));
neorv32_uart0_printf("\n+ New SPI clock speed = %u Hz\n", clock);
Expand All @@ -225,7 +225,7 @@ void spi_setup(void) {
while (1) {
neorv32_uart0_printf("Select SPI clock mode (0..3): ");
neorv32_uart0_scan(terminal_buffer, 2, 1);
tmp = (uint32_t)hexstr_to_uint(terminal_buffer, strlen(terminal_buffer));
tmp = (uint32_t)hexstr_to_uint32(terminal_buffer, strlen(terminal_buffer));
if (tmp > 4) {
neorv32_uart0_printf("\nInvalid selection!\n");
}
Expand All @@ -243,30 +243,39 @@ void spi_setup(void) {


/**********************************************************************//**
* Helper function to convert N hex chars string into uint32_T
* Helper function to convert N hex chars string into uint32_t
*
* @param[in,out] buffer Pointer to array of chars to convert into number.
* @param[in,out] length Length of the conversion string.
* @return Converted number.
**************************************************************************/
uint32_t hexstr_to_uint(char *buffer, uint8_t length) {
uint32_t hexstr_to_uint32(char *buffer, uint8_t length) {

uint32_t res = 0, d = 0;
char c = 0;

while (length--) {
c = *buffer++;

if ((c >= '0') && (c <= '9'))
if (c == '\0') {
break;
}

if ((c >= '0') && (c <= '9')) {
d = (uint32_t)(c - '0');
else if ((c >= 'a') && (c <= 'f'))
}
else if ((c >= 'a') && (c <= 'f')) {
d = (uint32_t)((c - 'a') + 10);
else if ((c >= 'A') && (c <= 'F'))
}
else if ((c >= 'A') && (c <= 'F')) {
d = (uint32_t)((c - 'A') + 10);
else
}
else {
d = 0;
}

res = res + (d << (length*4));
res <<= 4;
res |= d & 0xf;
}

return res;
Expand Down
29 changes: 19 additions & 10 deletions sw/example/demo_twi/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
void scan_twi(void);
void set_clock(void);
void send_twi(void);
uint32_t hexstr_to_uint(char *buffer, uint8_t length);
uint32_t hexstr_to_uint32(char *buffer, uint8_t length);
void print_hex_byte(uint8_t data);


Expand Down Expand Up @@ -131,7 +131,7 @@ void set_clock(void) {
// clock prescaler
neorv32_uart0_printf("Select new clock prescaler (0..7; one hex char): ");
neorv32_uart0_scan(terminal_buffer, 2, 1); // 1 hex char plus '\0'
int prsc = (int)hexstr_to_uint(terminal_buffer, strlen(terminal_buffer));
int prsc = (int)hexstr_to_uint32(terminal_buffer, strlen(terminal_buffer));

if ((prsc < 0) || (prsc > 7)) { // invalid?
neorv32_uart0_printf("\nInvalid selection!\n");
Expand All @@ -141,7 +141,7 @@ void set_clock(void) {
// clock divider
neorv32_uart0_printf("\nSelect new clock divider (0..15; one hex char): ");
neorv32_uart0_scan(terminal_buffer, 2, 1); // 1 hex char plus '\0'
int cdiv = (int)hexstr_to_uint(terminal_buffer, strlen(terminal_buffer));
int cdiv = (int)hexstr_to_uint32(terminal_buffer, strlen(terminal_buffer));

if ((cdiv < 0) || (cdiv > 15)) { // invalid?
neorv32_uart0_printf("\nInvalid selection!\n");
Expand Down Expand Up @@ -217,7 +217,7 @@ void send_twi(void) {
// TX data
neorv32_uart0_printf("Enter TX data (2 hex chars): ");
neorv32_uart0_scan(terminal_buffer, 3, 1); // 2 hex chars for address plus '\0'
data = (uint8_t)hexstr_to_uint(terminal_buffer, strlen(terminal_buffer));
data = (uint8_t)hexstr_to_uint32(terminal_buffer, strlen(terminal_buffer));

// host ACK
neorv32_uart0_printf("\nIssue ACK by host (y/n)? ");
Expand Down Expand Up @@ -257,24 +257,33 @@ void send_twi(void) {
* @param[in,out] length Length of the conversion string.
* @return Converted number.
**************************************************************************/
uint32_t hexstr_to_uint(char *buffer, uint8_t length) {
uint32_t hexstr_to_uint32(char *buffer, uint8_t length) {

uint32_t res = 0, d = 0;
char c = 0;

while (length--) {
c = *buffer++;

if ((c >= '0') && (c <= '9'))
if (c == '\0') {
break;
}

if ((c >= '0') && (c <= '9')) {
d = (uint32_t)(c - '0');
else if ((c >= 'a') && (c <= 'f'))
}
else if ((c >= 'a') && (c <= 'f')) {
d = (uint32_t)((c - 'a') + 10);
else if ((c >= 'A') && (c <= 'F'))
}
else if ((c >= 'A') && (c <= 'F')) {
d = (uint32_t)((c - 'A') + 10);
else
}
else {
d = 0;
}

res = res + (d << (length*4));
res <<= 4;
res |= d & 0xf;
}

return res;
Expand Down

0 comments on commit ff2f921

Please sign in to comment.