1 |
2 |
zero_gravi |
// #################################################################################################
|
2 |
|
|
// # << NEORV32 - TWI Bus Explorer Demo Program >> #
|
3 |
|
|
// # ********************************************************************************************* #
|
4 |
|
|
// # BSD 3-Clause License #
|
5 |
|
|
// # #
|
6 |
44 |
zero_gravi |
// # Copyright (c) 2021, Stephan Nolting. All rights reserved. #
|
7 |
2 |
zero_gravi |
// # #
|
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 |
68 |
zero_gravi |
void print_hex_byte(uint8_t data);
|
61 |
2 |
zero_gravi |
|
62 |
|
|
|
63 |
|
|
/**********************************************************************//**
|
64 |
35 |
zero_gravi |
* This program provides an interactive console to communicate with TWI devices.
|
65 |
2 |
zero_gravi |
*
|
66 |
35 |
zero_gravi |
* @note This program requires the UART and the PWM to be synthesized.
|
67 |
2 |
zero_gravi |
*
|
68 |
60 |
zero_gravi |
* @return 0 if execution was successful
|
69 |
2 |
zero_gravi |
**************************************************************************/
|
70 |
|
|
int main() {
|
71 |
|
|
|
72 |
|
|
char buffer[8];
|
73 |
|
|
int length = 0;
|
74 |
|
|
int bus_claimed = 0;
|
75 |
|
|
|
76 |
|
|
// check if UART unit is implemented at all
|
77 |
65 |
zero_gravi |
if (neorv32_uart0_available() == 0) {
|
78 |
60 |
zero_gravi |
return 1;
|
79 |
2 |
zero_gravi |
}
|
80 |
|
|
|
81 |
|
|
|
82 |
|
|
// capture all exceptions and give debug info via UART
|
83 |
|
|
// this is not required, but keeps us safe
|
84 |
14 |
zero_gravi |
neorv32_rte_setup();
|
85 |
2 |
zero_gravi |
|
86 |
|
|
|
87 |
51 |
zero_gravi |
// init UART at default baud rate, no parity bits, ho hw flow control
|
88 |
65 |
zero_gravi |
neorv32_uart0_setup(BAUD_RATE, PARITY_NONE, FLOW_CONTROL_NONE);
|
89 |
2 |
zero_gravi |
|
90 |
44 |
zero_gravi |
// check available hardware extensions and compare with compiler flags
|
91 |
|
|
neorv32_rte_check_isa(0); // silent = 0 -> show message if isa mismatch
|
92 |
|
|
|
93 |
2 |
zero_gravi |
// intro
|
94 |
65 |
zero_gravi |
neorv32_uart0_printf("\n--- TWI Bus Explorer ---\n\n");
|
95 |
2 |
zero_gravi |
|
96 |
|
|
|
97 |
|
|
// check if TWI unit is implemented at all
|
98 |
|
|
if (neorv32_twi_available() == 0) {
|
99 |
65 |
zero_gravi |
neorv32_uart0_printf("No TWI unit implemented.");
|
100 |
60 |
zero_gravi |
return 1;
|
101 |
2 |
zero_gravi |
}
|
102 |
|
|
|
103 |
|
|
|
104 |
|
|
// info
|
105 |
65 |
zero_gravi |
neorv32_uart0_printf("This program allows to create TWI transfers by hand.\n"
|
106 |
2 |
zero_gravi |
"Type 'help' to see the help menu.\n\n");
|
107 |
|
|
|
108 |
68 |
zero_gravi |
// configure TWI, second slowest clock
|
109 |
|
|
neorv32_twi_setup(CLK_PRSC_2048);
|
110 |
2 |
zero_gravi |
|
111 |
|
|
// no active bus session yet
|
112 |
|
|
bus_claimed = 0;
|
113 |
|
|
|
114 |
|
|
// Main menu
|
115 |
|
|
for (;;) {
|
116 |
65 |
zero_gravi |
neorv32_uart0_printf("TWI_EXPLORER:> ");
|
117 |
|
|
length = neorv32_uart0_scan(buffer, 8, 1);
|
118 |
|
|
neorv32_uart0_printf("\n");
|
119 |
2 |
zero_gravi |
|
120 |
|
|
if (!length) // nothing to be done
|
121 |
|
|
continue;
|
122 |
|
|
|
123 |
|
|
// decode input and execute command
|
124 |
|
|
if (!strcmp(buffer, "help")) {
|
125 |
65 |
zero_gravi |
neorv32_uart0_printf("Available commands:\n"
|
126 |
2 |
zero_gravi |
" help - show this text\n"
|
127 |
|
|
" scan - scan bus for devices\n"
|
128 |
|
|
" start - generate START condition\n"
|
129 |
|
|
" stop - generate STOP condition\n"
|
130 |
|
|
" send - write & read single byte to/from bus\n"
|
131 |
|
|
" speed - select bus clock\n"
|
132 |
|
|
"Start a new transmission by generating a START condition. Next, transfer the 7-bit device address\n"
|
133 |
|
|
"and the R/W flag. After that, transfer your data to be written or send a 0xFF if you want to read\n"
|
134 |
|
|
"data from the bus. Finish the transmission by generating a STOP condition.\n\n");
|
135 |
|
|
}
|
136 |
|
|
else if (!strcmp(buffer, "start")) {
|
137 |
|
|
neorv32_twi_generate_start(); // generate START condition
|
138 |
|
|
bus_claimed = 1;
|
139 |
|
|
}
|
140 |
|
|
else if (!strcmp(buffer, "stop")) {
|
141 |
|
|
if (bus_claimed == 0) {
|
142 |
65 |
zero_gravi |
neorv32_uart0_printf("No active I2C transmission.\n");
|
143 |
2 |
zero_gravi |
continue;
|
144 |
|
|
}
|
145 |
|
|
neorv32_twi_generate_stop(); // generate STOP condition
|
146 |
|
|
bus_claimed = 0;
|
147 |
|
|
}
|
148 |
|
|
else if (!strcmp(buffer, "scan")) {
|
149 |
|
|
scan_twi();
|
150 |
|
|
}
|
151 |
|
|
else if (!strcmp(buffer, "speed")) {
|
152 |
|
|
set_speed();
|
153 |
|
|
}
|
154 |
|
|
else if (!strcmp(buffer, "send")) {
|
155 |
|
|
if (bus_claimed == 0) {
|
156 |
65 |
zero_gravi |
neorv32_uart0_printf("No active I2C transmission. Generate a START condition first.\n");
|
157 |
2 |
zero_gravi |
continue;
|
158 |
|
|
}
|
159 |
|
|
else {
|
160 |
|
|
send_twi();
|
161 |
|
|
}
|
162 |
|
|
}
|
163 |
|
|
else {
|
164 |
65 |
zero_gravi |
neorv32_uart0_printf("Invalid command. Type 'help' to see all commands.\n");
|
165 |
2 |
zero_gravi |
}
|
166 |
|
|
}
|
167 |
|
|
|
168 |
|
|
return 0;
|
169 |
|
|
}
|
170 |
|
|
|
171 |
|
|
|
172 |
|
|
/**********************************************************************//**
|
173 |
|
|
* TWI clock speed menu
|
174 |
|
|
**************************************************************************/
|
175 |
|
|
void set_speed(void) {
|
176 |
|
|
|
177 |
|
|
char terminal_buffer[2];
|
178 |
|
|
|
179 |
65 |
zero_gravi |
neorv32_uart0_printf("Select new clock prescaler (0..7): ");
|
180 |
|
|
neorv32_uart0_scan(terminal_buffer, 2, 1); // 1 hex char plus '\0'
|
181 |
2 |
zero_gravi |
uint8_t prsc = (uint8_t)hexstr_to_uint(terminal_buffer, strlen(terminal_buffer));
|
182 |
50 |
zero_gravi |
|
183 |
2 |
zero_gravi |
if ((prsc >= 0) && (prsc < 8)) { // valid?
|
184 |
64 |
zero_gravi |
NEORV32_TWI.CTRL = 0; // reset
|
185 |
|
|
NEORV32_TWI.CTRL = (1 << TWI_CTRL_EN) | (prsc << TWI_CTRL_PRSC0);
|
186 |
65 |
zero_gravi |
neorv32_uart0_printf("\nDone.\n");
|
187 |
2 |
zero_gravi |
}
|
188 |
|
|
else {
|
189 |
65 |
zero_gravi |
neorv32_uart0_printf("\nInvalid selection!\n");
|
190 |
2 |
zero_gravi |
return;
|
191 |
|
|
}
|
192 |
|
|
|
193 |
|
|
// print new clock frequency
|
194 |
50 |
zero_gravi |
uint32_t div = 0;
|
195 |
2 |
zero_gravi |
switch (prsc) {
|
196 |
50 |
zero_gravi |
case 0: div = 4 * 2; break;
|
197 |
|
|
case 1: div = 4 * 4; break;
|
198 |
|
|
case 2: div = 4 * 8; break;
|
199 |
|
|
case 3: div = 4 * 64; break;
|
200 |
|
|
case 4: div = 4 * 128; break;
|
201 |
|
|
case 5: div = 4 * 1024; break;
|
202 |
|
|
case 6: div = 4 * 2048; break;
|
203 |
|
|
case 7: div = 4 * 4096; break;
|
204 |
|
|
default: div = 0; break;
|
205 |
2 |
zero_gravi |
}
|
206 |
64 |
zero_gravi |
uint32_t clock = NEORV32_SYSINFO.CLK / div;
|
207 |
65 |
zero_gravi |
neorv32_uart0_printf("New I2C clock: %u Hz\n", clock);
|
208 |
2 |
zero_gravi |
}
|
209 |
|
|
|
210 |
|
|
|
211 |
|
|
/**********************************************************************//**
|
212 |
|
|
* Scan 7-bit TWI address space and print results
|
213 |
|
|
**************************************************************************/
|
214 |
|
|
void scan_twi(void) {
|
215 |
|
|
|
216 |
65 |
zero_gravi |
neorv32_uart0_printf("Scanning TWI bus...\n");
|
217 |
2 |
zero_gravi |
uint8_t i, num_devices = 0;
|
218 |
|
|
for (i=0; i<128; i++) {
|
219 |
|
|
uint8_t twi_ack = neorv32_twi_start_trans((uint8_t)(2*i+1));
|
220 |
|
|
neorv32_twi_generate_stop();
|
221 |
|
|
|
222 |
|
|
if (twi_ack == 0) {
|
223 |
68 |
zero_gravi |
neorv32_uart0_printf(" + Found device at write-address 0x");
|
224 |
|
|
print_hex_byte(2*i);
|
225 |
|
|
neorv32_uart0_printf("\n");
|
226 |
2 |
zero_gravi |
num_devices++;
|
227 |
|
|
}
|
228 |
|
|
}
|
229 |
|
|
|
230 |
|
|
if (!num_devices) {
|
231 |
65 |
zero_gravi |
neorv32_uart0_printf("No devices found.\n");
|
232 |
2 |
zero_gravi |
}
|
233 |
|
|
}
|
234 |
|
|
|
235 |
|
|
|
236 |
|
|
/**********************************************************************//**
|
237 |
|
|
* Read/write menu to transfer 1 byte from/to bus
|
238 |
|
|
**************************************************************************/
|
239 |
|
|
void send_twi(void) {
|
240 |
|
|
|
241 |
|
|
char terminal_buffer[4];
|
242 |
|
|
|
243 |
|
|
// enter data
|
244 |
65 |
zero_gravi |
neorv32_uart0_printf("Enter TX data (2 hex chars): ");
|
245 |
|
|
neorv32_uart0_scan(terminal_buffer, 3, 1); // 2 hex chars for address plus '\0'
|
246 |
2 |
zero_gravi |
uint8_t tmp = (uint8_t)hexstr_to_uint(terminal_buffer, strlen(terminal_buffer));
|
247 |
|
|
uint8_t res = neorv32_twi_trans(tmp);
|
248 |
68 |
zero_gravi |
neorv32_uart0_printf("\n RX data: 0x");
|
249 |
|
|
print_hex_byte((uint8_t)neorv32_twi_get_data());
|
250 |
|
|
neorv32_uart0_printf("\n Response: ");
|
251 |
2 |
zero_gravi |
if (res == 0)
|
252 |
65 |
zero_gravi |
neorv32_uart0_printf("ACK\n");
|
253 |
2 |
zero_gravi |
else
|
254 |
65 |
zero_gravi |
neorv32_uart0_printf("NACK\n");
|
255 |
2 |
zero_gravi |
|
256 |
|
|
}
|
257 |
|
|
|
258 |
|
|
|
259 |
|
|
/**********************************************************************//**
|
260 |
68 |
zero_gravi |
* Helper function to convert N hex chars string into uint32_t
|
261 |
2 |
zero_gravi |
*
|
262 |
|
|
* @param[in,out] buffer Pointer to array of chars to convert into number.
|
263 |
|
|
* @param[in,out] length Length of the conversion string.
|
264 |
|
|
* @return Converted number.
|
265 |
|
|
**************************************************************************/
|
266 |
|
|
uint32_t hexstr_to_uint(char *buffer, uint8_t length) {
|
267 |
|
|
|
268 |
|
|
uint32_t res = 0, d = 0;
|
269 |
|
|
char c = 0;
|
270 |
|
|
|
271 |
|
|
while (length--) {
|
272 |
|
|
c = *buffer++;
|
273 |
|
|
|
274 |
|
|
if ((c >= '0') && (c <= '9'))
|
275 |
|
|
d = (uint32_t)(c - '0');
|
276 |
|
|
else if ((c >= 'a') && (c <= 'f'))
|
277 |
|
|
d = (uint32_t)((c - 'a') + 10);
|
278 |
|
|
else if ((c >= 'A') && (c <= 'F'))
|
279 |
|
|
d = (uint32_t)((c - 'A') + 10);
|
280 |
|
|
else
|
281 |
|
|
d = 0;
|
282 |
|
|
|
283 |
|
|
res = res + (d << (length*4));
|
284 |
|
|
}
|
285 |
|
|
|
286 |
|
|
return res;
|
287 |
68 |
zero_gravi |
}
|
288 |
|
|
|
289 |
|
|
|
290 |
|
|
/**********************************************************************//**
|
291 |
|
|
* Print byte as hex chars via UART0.
|
292 |
|
|
*
|
293 |
|
|
* @param data 8-bit data to be printed as two hex chars.
|
294 |
|
|
**************************************************************************/
|
295 |
|
|
void print_hex_byte(uint8_t data) {
|
296 |
|
|
|
297 |
|
|
static const char symbols[] = "0123456789abcdef";
|
298 |
|
|
|
299 |
|
|
neorv32_uart0_putc(symbols[(data >> 4) & 15]);
|
300 |
|
|
neorv32_uart0_putc(symbols[(data >> 0) & 15]);
|
301 |
|
|
}
|
302 |
|
|
|