Skip to content

Commit

Permalink
libuartbone: move get_reg_addr into the lib
Browse files Browse the repository at this point in the history
Signed-off-by: Yann Sionneau <[email protected]>
  • Loading branch information
fallen committed Mar 25, 2024
1 parent 49c0cf5 commit a8fe7e2
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 60 deletions.
60 changes: 0 additions & 60 deletions software/libuartbone/linux_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,6 @@
#include <errno.h>
#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);
}
Expand Down
2 changes: 2 additions & 0 deletions software/libuartbone/uartbone.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>

/*
* There can exist only 16 OUT endpoints and 16 IN endpoints
Expand Down Expand Up @@ -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);
Expand Down
60 changes: 60 additions & 0 deletions software/libuartbone/uartbone_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit a8fe7e2

Please sign in to comment.