From a8fe7e2311c0dbf6bbef27b9c4fa719090652b0c Mon Sep 17 00:00:00 2001 From: Yann Sionneau Date: Mon, 25 Mar 2024 21:18:27 +0100 Subject: [PATCH] libuartbone: move get_reg_addr into the lib Signed-off-by: Yann Sionneau --- software/libuartbone/linux_cli.c | 60 --------------------------- software/libuartbone/uartbone.h | 2 + software/libuartbone/uartbone_linux.c | 60 +++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 60 deletions(-) diff --git a/software/libuartbone/linux_cli.c b/software/libuartbone/linux_cli.c index 502fcf8..7d9204d 100644 --- a/software/libuartbone/linux_cli.c +++ b/software/libuartbone/linux_cli.c @@ -5,66 +5,6 @@ #include #include "uartbone.h" -int get_reg_addr(FILE *csv, char *reg_name, uint32_t *res) { - char *line = NULL; - char *tok; - size_t len = 0; - bool csr_base_found = false; - bool csr_reg_found = false; - uint32_t csr_base = 0; - uint32_t reg_addr = 0; - ssize_t nread; - size_t reg_name_len = strlen(reg_name); - - if (!reg_name) - return -1; - - while ((nread = getline(&line, &len, csv)) != -1) { - - // Find the csd_base address - if (strncmp(line, "csr_base", strlen("csr_base")) == 0) { - strtok(line, ","); - tok = strtok(NULL, ","); - if (tok && (strncmp(tok, reg_name, reg_name_len) == 0)) { - if (tok[reg_name_len] != '\0') - continue; - - tok = strtok(NULL, ","); - if (tok) { - csr_base = strtol(tok, NULL, 0); - csr_base_found = true; - } - } - } - - // Find the register address - if (strncmp(line, "csr_register", strlen("csr_register")) == 0) { - strtok(line, ","); - tok = strtok(NULL, ","); - - if (tok && reg_name && (strncmp(tok, reg_name, reg_name_len) == 0)) { - if (tok[reg_name_len] != '\0') - continue; - - tok = strtok(NULL, ","); - if (tok) { - reg_addr = strtol(tok, NULL, 0); - csr_reg_found = true; - } - } - } - } - - if (csr_reg_found) - *res = reg_addr; - else if (csr_base_found) - *res = csr_base; - else - return -1; - - return 0; -} - void print_usage(char *prog_name) { printf("usage: %s [-u uart_port] [-V] [-b baudrate] [-r addr|reg_name] [-w addr|reg_name -v value] [-i] [-a addr_width]\n", prog_name); } diff --git a/software/libuartbone/uartbone.h b/software/libuartbone/uartbone.h index 52415d6..6dc0130 100644 --- a/software/libuartbone/uartbone.h +++ b/software/libuartbone/uartbone.h @@ -1,5 +1,6 @@ #include #include +#include /* * There can exist only 16 OUT endpoints and 16 IN endpoints @@ -38,6 +39,7 @@ struct uartbone_ctx { void *usb_handle; }; +int get_reg_addr(FILE *csv, char *reg_name, uint32_t *res); uint32_t uartbone_read(struct uartbone_ctx *ctx, uint64_t addr); void uartbone_unix_init(struct uartbone_ctx *ctx, char *file, unsigned int baudrate, unsigned int addr_width); void uartbone_write(struct uartbone_ctx *ctx, uint64_t addr, uint32_t val); diff --git a/software/libuartbone/uartbone_linux.c b/software/libuartbone/uartbone_linux.c index 7c11c90..b3defe4 100644 --- a/software/libuartbone/uartbone_linux.c +++ b/software/libuartbone/uartbone_linux.c @@ -14,6 +14,66 @@ #include "uartbone.h" +int get_reg_addr(FILE *csv, char *reg_name, uint32_t *res) { + char *line = NULL; + char *tok; + size_t len = 0; + bool csr_base_found = false; + bool csr_reg_found = false; + uint32_t csr_base = 0; + uint32_t reg_addr = 0; + ssize_t nread; + size_t reg_name_len = strlen(reg_name); + + if (!reg_name) + return -1; + + while ((nread = getline(&line, &len, csv)) != -1) { + + // Find the csd_base address + if (strncmp(line, "csr_base", strlen("csr_base")) == 0) { + strtok(line, ","); + tok = strtok(NULL, ","); + if (tok && (strncmp(tok, reg_name, reg_name_len) == 0)) { + if (tok[reg_name_len] != '\0') + continue; + + tok = strtok(NULL, ","); + if (tok) { + csr_base = strtol(tok, NULL, 0); + csr_base_found = true; + } + } + } + + // Find the register address + if (strncmp(line, "csr_register", strlen("csr_register")) == 0) { + strtok(line, ","); + tok = strtok(NULL, ","); + + if (tok && reg_name && (strncmp(tok, reg_name, reg_name_len) == 0)) { + if (tok[reg_name_len] != '\0') + continue; + + tok = strtok(NULL, ","); + if (tok) { + reg_addr = strtol(tok, NULL, 0); + csr_reg_found = true; + } + } + } + } + + if (csr_reg_found) + *res = reg_addr; + else if (csr_base_found) + *res = csr_base; + else + return -1; + + return 0; +} + static speed_t baudrate_to_speed(unsigned int baudrate) { switch (baudrate) { case 9600: