Line 71... |
Line 71... |
char buffer[8];
|
char buffer[8];
|
int length = 0;
|
int length = 0;
|
int bus_claimed = 0;
|
int bus_claimed = 0;
|
|
|
// check if UART unit is implemented at all
|
// check if UART unit is implemented at all
|
if (neorv32_uart_available() == 0) {
|
if (neorv32_uart0_available() == 0) {
|
return 1;
|
return 1;
|
}
|
}
|
|
|
|
|
// capture all exceptions and give debug info via UART
|
// capture all exceptions and give debug info via UART
|
// this is not required, but keeps us safe
|
// this is not required, but keeps us safe
|
neorv32_rte_setup();
|
neorv32_rte_setup();
|
|
|
|
|
// init UART at default baud rate, no parity bits, ho hw flow control
|
// init UART at default baud rate, no parity bits, ho hw flow control
|
neorv32_uart_setup(BAUD_RATE, PARITY_NONE, FLOW_CONTROL_NONE);
|
neorv32_uart0_setup(BAUD_RATE, PARITY_NONE, FLOW_CONTROL_NONE);
|
|
|
// check available hardware extensions and compare with compiler flags
|
// check available hardware extensions and compare with compiler flags
|
neorv32_rte_check_isa(0); // silent = 0 -> show message if isa mismatch
|
neorv32_rte_check_isa(0); // silent = 0 -> show message if isa mismatch
|
|
|
// intro
|
// intro
|
neorv32_uart_printf("\n--- TWI Bus Explorer ---\n\n");
|
neorv32_uart0_printf("\n--- TWI Bus Explorer ---\n\n");
|
|
|
|
|
// check if TWI unit is implemented at all
|
// check if TWI unit is implemented at all
|
if (neorv32_twi_available() == 0) {
|
if (neorv32_twi_available() == 0) {
|
neorv32_uart_printf("No TWI unit implemented.");
|
neorv32_uart0_printf("No TWI unit implemented.");
|
return 1;
|
return 1;
|
}
|
}
|
|
|
|
|
// info
|
// info
|
neorv32_uart_printf("This program allows to create TWI transfers by hand.\n"
|
neorv32_uart0_printf("This program allows to create TWI transfers by hand.\n"
|
"Type 'help' to see the help menu.\n\n");
|
"Type 'help' to see the help menu.\n\n");
|
|
|
// configure TWI, second slowest clock, no clock-stretching
|
// configure TWI, second slowest clock, no clock-stretching
|
neorv32_twi_setup(CLK_PRSC_2048, 0);
|
neorv32_twi_setup(CLK_PRSC_2048, 0);
|
|
|
// no active bus session yet
|
// no active bus session yet
|
bus_claimed = 0;
|
bus_claimed = 0;
|
|
|
// Main menu
|
// Main menu
|
for (;;) {
|
for (;;) {
|
neorv32_uart_printf("TWI_EXPLORER:> ");
|
neorv32_uart0_printf("TWI_EXPLORER:> ");
|
length = neorv32_uart_scan(buffer, 8, 1);
|
length = neorv32_uart0_scan(buffer, 8, 1);
|
neorv32_uart_printf("\n");
|
neorv32_uart0_printf("\n");
|
|
|
if (!length) // nothing to be done
|
if (!length) // nothing to be done
|
continue;
|
continue;
|
|
|
// decode input and execute command
|
// decode input and execute command
|
if (!strcmp(buffer, "help")) {
|
if (!strcmp(buffer, "help")) {
|
neorv32_uart_printf("Available commands:\n"
|
neorv32_uart0_printf("Available commands:\n"
|
" help - show this text\n"
|
" help - show this text\n"
|
" scan - scan bus for devices\n"
|
" scan - scan bus for devices\n"
|
" start - generate START condition\n"
|
" start - generate START condition\n"
|
" stop - generate STOP condition\n"
|
" stop - generate STOP condition\n"
|
" send - write & read single byte to/from bus\n"
|
" send - write & read single byte to/from bus\n"
|
Line 136... |
Line 136... |
neorv32_twi_generate_start(); // generate START condition
|
neorv32_twi_generate_start(); // generate START condition
|
bus_claimed = 1;
|
bus_claimed = 1;
|
}
|
}
|
else if (!strcmp(buffer, "stop")) {
|
else if (!strcmp(buffer, "stop")) {
|
if (bus_claimed == 0) {
|
if (bus_claimed == 0) {
|
neorv32_uart_printf("No active I2C transmission.\n");
|
neorv32_uart0_printf("No active I2C transmission.\n");
|
continue;
|
continue;
|
}
|
}
|
neorv32_twi_generate_stop(); // generate STOP condition
|
neorv32_twi_generate_stop(); // generate STOP condition
|
bus_claimed = 0;
|
bus_claimed = 0;
|
}
|
}
|
Line 150... |
Line 150... |
else if (!strcmp(buffer, "speed")) {
|
else if (!strcmp(buffer, "speed")) {
|
set_speed();
|
set_speed();
|
}
|
}
|
else if (!strcmp(buffer, "send")) {
|
else if (!strcmp(buffer, "send")) {
|
if (bus_claimed == 0) {
|
if (bus_claimed == 0) {
|
neorv32_uart_printf("No active I2C transmission. Generate a START condition first.\n");
|
neorv32_uart0_printf("No active I2C transmission. Generate a START condition first.\n");
|
continue;
|
continue;
|
}
|
}
|
else {
|
else {
|
send_twi();
|
send_twi();
|
}
|
}
|
}
|
}
|
else {
|
else {
|
neorv32_uart_printf("Invalid command. Type 'help' to see all commands.\n");
|
neorv32_uart0_printf("Invalid command. Type 'help' to see all commands.\n");
|
}
|
}
|
}
|
}
|
|
|
return 0;
|
return 0;
|
}
|
}
|
Line 173... |
Line 173... |
**************************************************************************/
|
**************************************************************************/
|
void set_speed(void) {
|
void set_speed(void) {
|
|
|
char terminal_buffer[2];
|
char terminal_buffer[2];
|
|
|
neorv32_uart_printf("Select new clock prescaler (0..7): ");
|
neorv32_uart0_printf("Select new clock prescaler (0..7): ");
|
neorv32_uart_scan(terminal_buffer, 2, 1); // 1 hex char plus '\0'
|
neorv32_uart0_scan(terminal_buffer, 2, 1); // 1 hex char plus '\0'
|
uint8_t prsc = (uint8_t)hexstr_to_uint(terminal_buffer, strlen(terminal_buffer));
|
uint8_t prsc = (uint8_t)hexstr_to_uint(terminal_buffer, strlen(terminal_buffer));
|
|
|
if ((prsc >= 0) && (prsc < 8)) { // valid?
|
if ((prsc >= 0) && (prsc < 8)) { // valid?
|
NEORV32_TWI.CTRL = 0; // reset
|
NEORV32_TWI.CTRL = 0; // reset
|
NEORV32_TWI.CTRL = (1 << TWI_CTRL_EN) | (prsc << TWI_CTRL_PRSC0);
|
NEORV32_TWI.CTRL = (1 << TWI_CTRL_EN) | (prsc << TWI_CTRL_PRSC0);
|
neorv32_uart_printf("\nDone.\n");
|
neorv32_uart0_printf("\nDone.\n");
|
}
|
}
|
else {
|
else {
|
neorv32_uart_printf("\nInvalid selection!\n");
|
neorv32_uart0_printf("\nInvalid selection!\n");
|
return;
|
return;
|
}
|
}
|
|
|
// print new clock frequency
|
// print new clock frequency
|
uint32_t div = 0;
|
uint32_t div = 0;
|
Line 201... |
Line 201... |
case 6: div = 4 * 2048; break;
|
case 6: div = 4 * 2048; break;
|
case 7: div = 4 * 4096; break;
|
case 7: div = 4 * 4096; break;
|
default: div = 0; break;
|
default: div = 0; break;
|
}
|
}
|
uint32_t clock = NEORV32_SYSINFO.CLK / div;
|
uint32_t clock = NEORV32_SYSINFO.CLK / div;
|
neorv32_uart_printf("New I2C clock: %u Hz\n", clock);
|
neorv32_uart0_printf("New I2C clock: %u Hz\n", clock);
|
}
|
}
|
|
|
|
|
/**********************************************************************//**
|
/**********************************************************************//**
|
* Scan 7-bit TWI address space and print results
|
* Scan 7-bit TWI address space and print results
|
**************************************************************************/
|
**************************************************************************/
|
void scan_twi(void) {
|
void scan_twi(void) {
|
|
|
neorv32_uart_printf("Scanning TWI bus...\n");
|
neorv32_uart0_printf("Scanning TWI bus...\n");
|
uint8_t i, num_devices = 0;
|
uint8_t i, num_devices = 0;
|
for (i=0; i<128; i++) {
|
for (i=0; i<128; i++) {
|
uint8_t twi_ack = neorv32_twi_start_trans((uint8_t)(2*i+1));
|
uint8_t twi_ack = neorv32_twi_start_trans((uint8_t)(2*i+1));
|
neorv32_twi_generate_stop();
|
neorv32_twi_generate_stop();
|
|
|
if (twi_ack == 0) {
|
if (twi_ack == 0) {
|
neorv32_uart_printf("+ Found device at write-address 0x%x\n", (uint32_t)(2*i));
|
neorv32_uart0_printf("+ Found device at write-address 0x%x\n", (uint32_t)(2*i));
|
num_devices++;
|
num_devices++;
|
}
|
}
|
}
|
}
|
|
|
if (!num_devices) {
|
if (!num_devices) {
|
neorv32_uart_printf("No devices found.\n");
|
neorv32_uart0_printf("No devices found.\n");
|
}
|
}
|
}
|
}
|
|
|
|
|
/**********************************************************************//**
|
/**********************************************************************//**
|
Line 236... |
Line 236... |
void send_twi(void) {
|
void send_twi(void) {
|
|
|
char terminal_buffer[4];
|
char terminal_buffer[4];
|
|
|
// enter data
|
// enter data
|
neorv32_uart_printf("Enter TX data (2 hex chars): ");
|
neorv32_uart0_printf("Enter TX data (2 hex chars): ");
|
neorv32_uart_scan(terminal_buffer, 3, 1); // 2 hex chars for address plus '\0'
|
neorv32_uart0_scan(terminal_buffer, 3, 1); // 2 hex chars for address plus '\0'
|
uint8_t tmp = (uint8_t)hexstr_to_uint(terminal_buffer, strlen(terminal_buffer));
|
uint8_t tmp = (uint8_t)hexstr_to_uint(terminal_buffer, strlen(terminal_buffer));
|
uint8_t res = neorv32_twi_trans(tmp);
|
uint8_t res = neorv32_twi_trans(tmp);
|
neorv32_uart_printf("\nRX data: 0x%x\n", (uint32_t)neorv32_twi_get_data());
|
neorv32_uart0_printf("\nRX data: 0x%x\n", (uint32_t)neorv32_twi_get_data());
|
neorv32_uart_printf("Response: ");
|
neorv32_uart0_printf("Response: ");
|
if (res == 0)
|
if (res == 0)
|
neorv32_uart_printf("ACK\n");
|
neorv32_uart0_printf("ACK\n");
|
else
|
else
|
neorv32_uart_printf("NACK\n");
|
neorv32_uart0_printf("NACK\n");
|
|
|
}
|
}
|
|
|
|
|
/**********************************************************************//**
|
/**********************************************************************//**
|