| 1 |
2 |
zero_gravi |
// #################################################################################################
|
| 2 |
|
|
// # << NEORV32 - TWI Bus Explorer Demo Program >> #
|
| 3 |
|
|
// # ********************************************************************************************* #
|
| 4 |
|
|
// # BSD 3-Clause License #
|
| 5 |
|
|
// # #
|
| 6 |
|
|
// # Copyright (c) 2020, Stephan Nolting. All rights reserved. #
|
| 7 |
|
|
// # #
|
| 8 |
|
|
// # Redistribution and use in source and binary forms, with or without modification, are #
|
| 9 |
|
|
// # permitted provided that the following conditions are met: #
|
| 10 |
|
|
// # #
|
| 11 |
|
|
// # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
| 12 |
|
|
// # conditions and the following disclaimer. #
|
| 13 |
|
|
// # #
|
| 14 |
|
|
// # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
| 15 |
|
|
// # conditions and the following disclaimer in the documentation and/or other materials #
|
| 16 |
|
|
// # provided with the distribution. #
|
| 17 |
|
|
// # #
|
| 18 |
|
|
// # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
| 19 |
|
|
// # endorse or promote products derived from this software without specific prior written #
|
| 20 |
|
|
// # permission. #
|
| 21 |
|
|
// # #
|
| 22 |
|
|
// # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
| 23 |
|
|
// # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
| 24 |
|
|
// # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
| 25 |
|
|
// # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
| 26 |
|
|
// # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
| 27 |
|
|
// # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
| 28 |
|
|
// # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
| 29 |
|
|
// # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
| 30 |
|
|
// # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
| 31 |
|
|
// # ********************************************************************************************* #
|
| 32 |
|
|
// # The NEORV32 Processor - https://github.com/stnolting/neorv32 (c) Stephan Nolting #
|
| 33 |
|
|
// #################################################################################################
|
| 34 |
|
|
|
| 35 |
|
|
|
| 36 |
|
|
/**********************************************************************//**
|
| 37 |
|
|
* @file demo_twi/main.c
|
| 38 |
|
|
* @author Stephan Nolting
|
| 39 |
|
|
* @brief TWI bus explorer.
|
| 40 |
|
|
**************************************************************************/
|
| 41 |
|
|
|
| 42 |
|
|
#include <neorv32.h>
|
| 43 |
14 |
zero_gravi |
#include <string.h>
|
| 44 |
2 |
zero_gravi |
|
| 45 |
|
|
|
| 46 |
|
|
/**********************************************************************//**
|
| 47 |
|
|
* @name User configuration
|
| 48 |
|
|
**************************************************************************/
|
| 49 |
|
|
/**@{*/
|
| 50 |
|
|
/** UART BAUD rate */
|
| 51 |
|
|
#define BAUD_RATE 19200
|
| 52 |
|
|
/**@}*/
|
| 53 |
|
|
|
| 54 |
|
|
|
| 55 |
|
|
// Prototypes
|
| 56 |
|
|
void scan_twi(void);
|
| 57 |
|
|
void set_speed(void);
|
| 58 |
|
|
void send_twi(void);
|
| 59 |
|
|
uint32_t hexstr_to_uint(char *buffer, uint8_t length);
|
| 60 |
|
|
|
| 61 |
|
|
|
| 62 |
|
|
/**********************************************************************//**
|
| 63 |
35 |
zero_gravi |
* This program provides an interactive console to communicate with TWI devices.
|
| 64 |
2 |
zero_gravi |
*
|
| 65 |
35 |
zero_gravi |
* @note This program requires the UART and the PWM to be synthesized.
|
| 66 |
2 |
zero_gravi |
*
|
| 67 |
|
|
* @return Irrelevant.
|
| 68 |
|
|
**************************************************************************/
|
| 69 |
|
|
int main() {
|
| 70 |
|
|
|
| 71 |
|
|
char buffer[8];
|
| 72 |
|
|
int length = 0;
|
| 73 |
|
|
int bus_claimed = 0;
|
| 74 |
|
|
|
| 75 |
|
|
// check if UART unit is implemented at all
|
| 76 |
|
|
if (neorv32_uart_available() == 0) {
|
| 77 |
|
|
return 0;
|
| 78 |
|
|
}
|
| 79 |
|
|
|
| 80 |
|
|
|
| 81 |
|
|
// capture all exceptions and give debug info via UART
|
| 82 |
|
|
// this is not required, but keeps us safe
|
| 83 |
14 |
zero_gravi |
neorv32_rte_setup();
|
| 84 |
2 |
zero_gravi |
|
| 85 |
|
|
|
| 86 |
|
|
// init UART at default baud rate, no rx interrupt, no tx interrupt
|
| 87 |
|
|
neorv32_uart_setup(BAUD_RATE, 0, 0);
|
| 88 |
|
|
|
| 89 |
|
|
// intro
|
| 90 |
|
|
neorv32_uart_printf("\n--- TWI Bus Explorer ---\n\n");
|
| 91 |
|
|
|
| 92 |
|
|
|
| 93 |
|
|
// check if TWI unit is implemented at all
|
| 94 |
|
|
if (neorv32_twi_available() == 0) {
|
| 95 |
|
|
neorv32_uart_printf("No TWI unit implemented.");
|
| 96 |
|
|
return 0;
|
| 97 |
|
|
}
|
| 98 |
|
|
|
| 99 |
|
|
|
| 100 |
|
|
// info
|
| 101 |
|
|
neorv32_uart_printf("This program allows to create TWI transfers by hand.\n"
|
| 102 |
|
|
"Type 'help' to see the help menu.\n\n");
|
| 103 |
|
|
|
| 104 |
35 |
zero_gravi |
// configure TWI, second slowest clock, no IRQ, no clock-stretching
|
| 105 |
|
|
neorv32_twi_setup(CLK_PRSC_2048, 0, 0);
|
| 106 |
2 |
zero_gravi |
|
| 107 |
|
|
// no active bus session yet
|
| 108 |
|
|
bus_claimed = 0;
|
| 109 |
|
|
|
| 110 |
|
|
// Main menu
|
| 111 |
|
|
for (;;) {
|
| 112 |
|
|
neorv32_uart_printf("TWI_EXPLORER:> ");
|
| 113 |
|
|
length = neorv32_uart_scan(buffer, 8, 1);
|
| 114 |
|
|
neorv32_uart_printf("\n");
|
| 115 |
|
|
|
| 116 |
|
|
if (!length) // nothing to be done
|
| 117 |
|
|
continue;
|
| 118 |
|
|
|
| 119 |
|
|
// decode input and execute command
|
| 120 |
|
|
if (!strcmp(buffer, "help")) {
|
| 121 |
|
|
neorv32_uart_printf("Available commands:\n"
|
| 122 |
|
|
" help - show this text\n"
|
| 123 |
|
|
" scan - scan bus for devices\n"
|
| 124 |
|
|
" start - generate START condition\n"
|
| 125 |
|
|
" stop - generate STOP condition\n"
|
| 126 |
|
|
" send - write & read single byte to/from bus\n"
|
| 127 |
|
|
" speed - select bus clock\n"
|
| 128 |
|
|
"Start a new transmission by generating a START condition. Next, transfer the 7-bit device address\n"
|
| 129 |
|
|
"and the R/W flag. After that, transfer your data to be written or send a 0xFF if you want to read\n"
|
| 130 |
|
|
"data from the bus. Finish the transmission by generating a STOP condition.\n\n");
|
| 131 |
|
|
}
|
| 132 |
|
|
else if (!strcmp(buffer, "start")) {
|
| 133 |
|
|
neorv32_twi_generate_start(); // generate START condition
|
| 134 |
|
|
bus_claimed = 1;
|
| 135 |
|
|
}
|
| 136 |
|
|
else if (!strcmp(buffer, "stop")) {
|
| 137 |
|
|
if (bus_claimed == 0) {
|
| 138 |
|
|
neorv32_uart_printf("No active I2C transmission.\n");
|
| 139 |
|
|
continue;
|
| 140 |
|
|
}
|
| 141 |
|
|
neorv32_twi_generate_stop(); // generate STOP condition
|
| 142 |
|
|
bus_claimed = 0;
|
| 143 |
|
|
}
|
| 144 |
|
|
else if (!strcmp(buffer, "scan")) {
|
| 145 |
|
|
scan_twi();
|
| 146 |
|
|
}
|
| 147 |
|
|
else if (!strcmp(buffer, "speed")) {
|
| 148 |
|
|
set_speed();
|
| 149 |
|
|
}
|
| 150 |
|
|
else if (!strcmp(buffer, "send")) {
|
| 151 |
|
|
if (bus_claimed == 0) {
|
| 152 |
|
|
neorv32_uart_printf("No active I2C transmission. Generate a START condition first.\n");
|
| 153 |
|
|
continue;
|
| 154 |
|
|
}
|
| 155 |
|
|
else {
|
| 156 |
|
|
send_twi();
|
| 157 |
|
|
}
|
| 158 |
|
|
}
|
| 159 |
|
|
else {
|
| 160 |
|
|
neorv32_uart_printf("Invalid command. Type 'help' to see all commands.\n");
|
| 161 |
|
|
}
|
| 162 |
|
|
}
|
| 163 |
|
|
|
| 164 |
|
|
return 0;
|
| 165 |
|
|
}
|
| 166 |
|
|
|
| 167 |
|
|
|
| 168 |
|
|
/**********************************************************************//**
|
| 169 |
|
|
* TWI clock speed menu
|
| 170 |
|
|
**************************************************************************/
|
| 171 |
|
|
void set_speed(void) {
|
| 172 |
|
|
|
| 173 |
|
|
char terminal_buffer[2];
|
| 174 |
|
|
|
| 175 |
|
|
neorv32_uart_printf("Select new clock prescaler (0..7): ");
|
| 176 |
|
|
neorv32_uart_scan(terminal_buffer, 2, 1); // 1 hex char plus '\0'
|
| 177 |
|
|
uint8_t prsc = (uint8_t)hexstr_to_uint(terminal_buffer, strlen(terminal_buffer));
|
| 178 |
|
|
if ((prsc >= 0) && (prsc < 8)) { // valid?
|
| 179 |
|
|
TWI_CT = 0; // reset
|
| 180 |
|
|
TWI_CT = (1 << TWI_CT_EN) | (prsc << TWI_CT_PRSC0);
|
| 181 |
|
|
neorv32_uart_printf("\nDone.\n");
|
| 182 |
|
|
}
|
| 183 |
|
|
else {
|
| 184 |
|
|
neorv32_uart_printf("\nInvalid selection!\n");
|
| 185 |
|
|
return;
|
| 186 |
|
|
}
|
| 187 |
|
|
|
| 188 |
|
|
// print new clock frequency
|
| 189 |
12 |
zero_gravi |
uint32_t clock = SYSINFO_CLK;
|
| 190 |
2 |
zero_gravi |
switch (prsc) {
|
| 191 |
|
|
case 0: clock = clock / 2; break;
|
| 192 |
|
|
case 1: clock = clock / 4; break;
|
| 193 |
|
|
case 2: clock = clock / 8; break;
|
| 194 |
|
|
case 3: clock = clock / 64; break;
|
| 195 |
|
|
case 4: clock = clock / 128; break;
|
| 196 |
|
|
case 5: clock = clock / 1024; break;
|
| 197 |
|
|
case 6: clock = clock / 2048; break;
|
| 198 |
|
|
case 7: clock = clock / 4096; break;
|
| 199 |
|
|
default: clock = 0; break;
|
| 200 |
|
|
}
|
| 201 |
|
|
neorv32_uart_printf("New I2C clock: %u Hz\n", clock);
|
| 202 |
|
|
}
|
| 203 |
|
|
|
| 204 |
|
|
|
| 205 |
|
|
/**********************************************************************//**
|
| 206 |
|
|
* Scan 7-bit TWI address space and print results
|
| 207 |
|
|
**************************************************************************/
|
| 208 |
|
|
void scan_twi(void) {
|
| 209 |
|
|
|
| 210 |
|
|
neorv32_uart_printf("Scanning TWI bus...\n");
|
| 211 |
|
|
uint8_t i, num_devices = 0;
|
| 212 |
|
|
for (i=0; i<128; i++) {
|
| 213 |
|
|
uint8_t twi_ack = neorv32_twi_start_trans((uint8_t)(2*i+1));
|
| 214 |
|
|
neorv32_twi_generate_stop();
|
| 215 |
|
|
|
| 216 |
|
|
if (twi_ack == 0) {
|
| 217 |
|
|
neorv32_uart_printf("+ Found device at write-address 0x%x\n", (uint32_t)(2*i));
|
| 218 |
|
|
num_devices++;
|
| 219 |
|
|
}
|
| 220 |
|
|
}
|
| 221 |
|
|
|
| 222 |
|
|
if (!num_devices) {
|
| 223 |
|
|
neorv32_uart_printf("No devices found.\n");
|
| 224 |
|
|
}
|
| 225 |
|
|
}
|
| 226 |
|
|
|
| 227 |
|
|
|
| 228 |
|
|
/**********************************************************************//**
|
| 229 |
|
|
* Read/write menu to transfer 1 byte from/to bus
|
| 230 |
|
|
**************************************************************************/
|
| 231 |
|
|
void send_twi(void) {
|
| 232 |
|
|
|
| 233 |
|
|
char terminal_buffer[4];
|
| 234 |
|
|
|
| 235 |
|
|
// enter data
|
| 236 |
|
|
neorv32_uart_printf("Enter TX data (2 hex chars): ");
|
| 237 |
|
|
neorv32_uart_scan(terminal_buffer, 3, 1); // 2 hex chars for address plus '\0'
|
| 238 |
|
|
uint8_t tmp = (uint8_t)hexstr_to_uint(terminal_buffer, strlen(terminal_buffer));
|
| 239 |
|
|
uint8_t res = neorv32_twi_trans(tmp);
|
| 240 |
|
|
neorv32_uart_printf("\nRX data: 0x%x\n", (uint32_t)neorv32_twi_get_data());
|
| 241 |
|
|
neorv32_uart_printf("Response: ");
|
| 242 |
|
|
if (res == 0)
|
| 243 |
|
|
neorv32_uart_printf("ACK\n");
|
| 244 |
|
|
else
|
| 245 |
|
|
neorv32_uart_printf("NACK\n");
|
| 246 |
|
|
|
| 247 |
|
|
}
|
| 248 |
|
|
|
| 249 |
|
|
|
| 250 |
|
|
/**********************************************************************//**
|
| 251 |
|
|
* Helper function to convert N hex chars string into uint32_T
|
| 252 |
|
|
*
|
| 253 |
|
|
* @param[in,out] buffer Pointer to array of chars to convert into number.
|
| 254 |
|
|
* @param[in,out] length Length of the conversion string.
|
| 255 |
|
|
* @return Converted number.
|
| 256 |
|
|
**************************************************************************/
|
| 257 |
|
|
uint32_t hexstr_to_uint(char *buffer, uint8_t length) {
|
| 258 |
|
|
|
| 259 |
|
|
uint32_t res = 0, d = 0;
|
| 260 |
|
|
char c = 0;
|
| 261 |
|
|
|
| 262 |
|
|
while (length--) {
|
| 263 |
|
|
c = *buffer++;
|
| 264 |
|
|
|
| 265 |
|
|
if ((c >= '0') && (c <= '9'))
|
| 266 |
|
|
d = (uint32_t)(c - '0');
|
| 267 |
|
|
else if ((c >= 'a') && (c <= 'f'))
|
| 268 |
|
|
d = (uint32_t)((c - 'a') + 10);
|
| 269 |
|
|
else if ((c >= 'A') && (c <= 'F'))
|
| 270 |
|
|
d = (uint32_t)((c - 'A') + 10);
|
| 271 |
|
|
else
|
| 272 |
|
|
d = 0;
|
| 273 |
|
|
|
| 274 |
|
|
res = res + (d << (length*4));
|
| 275 |
|
|
}
|
| 276 |
|
|
|
| 277 |
|
|
return res;
|
| 278 |
|
|
}
|