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 |
|
|
|
44 |
|
|
|
45 |
|
|
/**********************************************************************//**
|
46 |
|
|
* @name User configuration
|
47 |
|
|
**************************************************************************/
|
48 |
|
|
/**@{*/
|
49 |
|
|
/** UART BAUD rate */
|
50 |
|
|
#define BAUD_RATE 19200
|
51 |
|
|
/**@}*/
|
52 |
|
|
|
53 |
|
|
|
54 |
|
|
// Prototypes
|
55 |
|
|
void scan_twi(void);
|
56 |
|
|
void set_speed(void);
|
57 |
|
|
void send_twi(void);
|
58 |
|
|
uint32_t hexstr_to_uint(char *buffer, uint8_t length);
|
59 |
|
|
|
60 |
|
|
|
61 |
|
|
/**********************************************************************//**
|
62 |
|
|
* This program generates a simple dimming sequence for PWM channel 0,1,2.
|
63 |
|
|
*
|
64 |
|
|
* @note This program requires the UART and the TWI to be synthesized.
|
65 |
|
|
*
|
66 |
|
|
* @return Irrelevant.
|
67 |
|
|
**************************************************************************/
|
68 |
|
|
int main() {
|
69 |
|
|
|
70 |
|
|
char buffer[8];
|
71 |
|
|
int length = 0;
|
72 |
|
|
int bus_claimed = 0;
|
73 |
|
|
|
74 |
|
|
// check if UART unit is implemented at all
|
75 |
|
|
if (neorv32_uart_available() == 0) {
|
76 |
|
|
return 0;
|
77 |
|
|
}
|
78 |
|
|
|
79 |
|
|
|
80 |
|
|
// capture all exceptions and give debug info via UART
|
81 |
|
|
// this is not required, but keeps us safe
|
82 |
|
|
neorv32_rte_enable_debug_mode();
|
83 |
|
|
|
84 |
|
|
|
85 |
|
|
// init UART at default baud rate, no rx interrupt, no tx interrupt
|
86 |
|
|
neorv32_uart_setup(BAUD_RATE, 0, 0);
|
87 |
|
|
|
88 |
|
|
// intro
|
89 |
|
|
neorv32_uart_printf("\n--- TWI Bus Explorer ---\n\n");
|
90 |
|
|
|
91 |
|
|
|
92 |
|
|
// check if TWI unit is implemented at all
|
93 |
|
|
if (neorv32_twi_available() == 0) {
|
94 |
|
|
neorv32_uart_printf("No TWI unit implemented.");
|
95 |
|
|
return 0;
|
96 |
|
|
}
|
97 |
|
|
|
98 |
|
|
|
99 |
|
|
// info
|
100 |
|
|
neorv32_uart_printf("This program allows to create TWI transfers by hand.\n"
|
101 |
|
|
"Type 'help' to see the help menu.\n\n");
|
102 |
|
|
|
103 |
|
|
// configure TWI, second slowest clock, no IRQ
|
104 |
|
|
neorv32_twi_setup(CLK_PRSC_2048, 0);
|
105 |
|
|
|
106 |
|
|
// no active bus session yet
|
107 |
|
|
bus_claimed = 0;
|
108 |
|
|
|
109 |
|
|
// Main menu
|
110 |
|
|
for (;;) {
|
111 |
|
|
neorv32_uart_printf("TWI_EXPLORER:> ");
|
112 |
|
|
length = neorv32_uart_scan(buffer, 8, 1);
|
113 |
|
|
neorv32_uart_printf("\n");
|
114 |
|
|
|
115 |
|
|
if (!length) // nothing to be done
|
116 |
|
|
continue;
|
117 |
|
|
|
118 |
|
|
// decode input and execute command
|
119 |
|
|
if (!strcmp(buffer, "help")) {
|
120 |
|
|
neorv32_uart_printf("Available commands:\n"
|
121 |
|
|
" help - show this text\n"
|
122 |
|
|
" scan - scan bus for devices\n"
|
123 |
|
|
" start - generate START condition\n"
|
124 |
|
|
" stop - generate STOP condition\n"
|
125 |
|
|
" send - write & read single byte to/from bus\n"
|
126 |
|
|
" speed - select bus clock\n"
|
127 |
|
|
"Start a new transmission by generating a START condition. Next, transfer the 7-bit device address\n"
|
128 |
|
|
"and the R/W flag. After that, transfer your data to be written or send a 0xFF if you want to read\n"
|
129 |
|
|
"data from the bus. Finish the transmission by generating a STOP condition.\n\n");
|
130 |
|
|
}
|
131 |
|
|
else if (!strcmp(buffer, "start")) {
|
132 |
|
|
neorv32_twi_generate_start(); // generate START condition
|
133 |
|
|
bus_claimed = 1;
|
134 |
|
|
}
|
135 |
|
|
else if (!strcmp(buffer, "stop")) {
|
136 |
|
|
if (bus_claimed == 0) {
|
137 |
|
|
neorv32_uart_printf("No active I2C transmission.\n");
|
138 |
|
|
continue;
|
139 |
|
|
}
|
140 |
|
|
neorv32_twi_generate_stop(); // generate STOP condition
|
141 |
|
|
bus_claimed = 0;
|
142 |
|
|
}
|
143 |
|
|
else if (!strcmp(buffer, "scan")) {
|
144 |
|
|
scan_twi();
|
145 |
|
|
}
|
146 |
|
|
else if (!strcmp(buffer, "speed")) {
|
147 |
|
|
set_speed();
|
148 |
|
|
}
|
149 |
|
|
else if (!strcmp(buffer, "send")) {
|
150 |
|
|
if (bus_claimed == 0) {
|
151 |
|
|
neorv32_uart_printf("No active I2C transmission. Generate a START condition first.\n");
|
152 |
|
|
continue;
|
153 |
|
|
}
|
154 |
|
|
else {
|
155 |
|
|
send_twi();
|
156 |
|
|
}
|
157 |
|
|
}
|
158 |
|
|
else {
|
159 |
|
|
neorv32_uart_printf("Invalid command. Type 'help' to see all commands.\n");
|
160 |
|
|
}
|
161 |
|
|
}
|
162 |
|
|
|
163 |
|
|
return 0;
|
164 |
|
|
}
|
165 |
|
|
|
166 |
|
|
|
167 |
|
|
/**********************************************************************//**
|
168 |
|
|
* TWI clock speed menu
|
169 |
|
|
**************************************************************************/
|
170 |
|
|
void set_speed(void) {
|
171 |
|
|
|
172 |
|
|
char terminal_buffer[2];
|
173 |
|
|
|
174 |
|
|
neorv32_uart_printf("Select new clock prescaler (0..7): ");
|
175 |
|
|
neorv32_uart_scan(terminal_buffer, 2, 1); // 1 hex char plus '\0'
|
176 |
|
|
uint8_t prsc = (uint8_t)hexstr_to_uint(terminal_buffer, strlen(terminal_buffer));
|
177 |
|
|
if ((prsc >= 0) && (prsc < 8)) { // valid?
|
178 |
|
|
TWI_CT = 0; // reset
|
179 |
|
|
TWI_CT = (1 << TWI_CT_EN) | (prsc << TWI_CT_PRSC0);
|
180 |
|
|
neorv32_uart_printf("\nDone.\n");
|
181 |
|
|
}
|
182 |
|
|
else {
|
183 |
|
|
neorv32_uart_printf("\nInvalid selection!\n");
|
184 |
|
|
return;
|
185 |
|
|
}
|
186 |
|
|
|
187 |
|
|
// print new clock frequency
|
188 |
|
|
uint32_t clock = neorv32_cpu_csr_read(CSR_MCLOCK);
|
189 |
|
|
switch (prsc) {
|
190 |
|
|
case 0: clock = clock / 2; break;
|
191 |
|
|
case 1: clock = clock / 4; break;
|
192 |
|
|
case 2: clock = clock / 8; break;
|
193 |
|
|
case 3: clock = clock / 64; break;
|
194 |
|
|
case 4: clock = clock / 128; break;
|
195 |
|
|
case 5: clock = clock / 1024; break;
|
196 |
|
|
case 6: clock = clock / 2048; break;
|
197 |
|
|
case 7: clock = clock / 4096; break;
|
198 |
|
|
default: clock = 0; break;
|
199 |
|
|
}
|
200 |
|
|
neorv32_uart_printf("New I2C clock: %u Hz\n", clock);
|
201 |
|
|
}
|
202 |
|
|
|
203 |
|
|
|
204 |
|
|
/**********************************************************************//**
|
205 |
|
|
* Scan 7-bit TWI address space and print results
|
206 |
|
|
**************************************************************************/
|
207 |
|
|
void scan_twi(void) {
|
208 |
|
|
|
209 |
|
|
neorv32_uart_printf("Scanning TWI bus...\n");
|
210 |
|
|
uint8_t i, num_devices = 0;
|
211 |
|
|
for (i=0; i<128; i++) {
|
212 |
|
|
uint8_t twi_ack = neorv32_twi_start_trans((uint8_t)(2*i+1));
|
213 |
|
|
neorv32_twi_generate_stop();
|
214 |
|
|
|
215 |
|
|
if (twi_ack == 0) {
|
216 |
|
|
neorv32_uart_printf("+ Found device at write-address 0x%x\n", (uint32_t)(2*i));
|
217 |
|
|
num_devices++;
|
218 |
|
|
}
|
219 |
|
|
}
|
220 |
|
|
|
221 |
|
|
if (!num_devices) {
|
222 |
|
|
neorv32_uart_printf("No devices found.\n");
|
223 |
|
|
}
|
224 |
|
|
}
|
225 |
|
|
|
226 |
|
|
|
227 |
|
|
/**********************************************************************//**
|
228 |
|
|
* Read/write menu to transfer 1 byte from/to bus
|
229 |
|
|
**************************************************************************/
|
230 |
|
|
void send_twi(void) {
|
231 |
|
|
|
232 |
|
|
char terminal_buffer[4];
|
233 |
|
|
|
234 |
|
|
// enter data
|
235 |
|
|
neorv32_uart_printf("Enter TX data (2 hex chars): ");
|
236 |
|
|
neorv32_uart_scan(terminal_buffer, 3, 1); // 2 hex chars for address plus '\0'
|
237 |
|
|
uint8_t tmp = (uint8_t)hexstr_to_uint(terminal_buffer, strlen(terminal_buffer));
|
238 |
|
|
uint8_t res = neorv32_twi_trans(tmp);
|
239 |
|
|
neorv32_uart_printf("\nRX data: 0x%x\n", (uint32_t)neorv32_twi_get_data());
|
240 |
|
|
neorv32_uart_printf("Response: ");
|
241 |
|
|
if (res == 0)
|
242 |
|
|
neorv32_uart_printf("ACK\n");
|
243 |
|
|
else
|
244 |
|
|
neorv32_uart_printf("NACK\n");
|
245 |
|
|
|
246 |
|
|
}
|
247 |
|
|
|
248 |
|
|
|
249 |
|
|
/**********************************************************************//**
|
250 |
|
|
* Helper function to convert N hex chars string into uint32_T
|
251 |
|
|
*
|
252 |
|
|
* @param[in,out] buffer Pointer to array of chars to convert into number.
|
253 |
|
|
* @param[in,out] length Length of the conversion string.
|
254 |
|
|
* @return Converted number.
|
255 |
|
|
**************************************************************************/
|
256 |
|
|
uint32_t hexstr_to_uint(char *buffer, uint8_t length) {
|
257 |
|
|
|
258 |
|
|
uint32_t res = 0, d = 0;
|
259 |
|
|
char c = 0;
|
260 |
|
|
|
261 |
|
|
while (length--) {
|
262 |
|
|
c = *buffer++;
|
263 |
|
|
|
264 |
|
|
if ((c >= '0') && (c <= '9'))
|
265 |
|
|
d = (uint32_t)(c - '0');
|
266 |
|
|
else if ((c >= 'a') && (c <= 'f'))
|
267 |
|
|
d = (uint32_t)((c - 'a') + 10);
|
268 |
|
|
else if ((c >= 'A') && (c <= 'F'))
|
269 |
|
|
d = (uint32_t)((c - 'A') + 10);
|
270 |
|
|
else
|
271 |
|
|
d = 0;
|
272 |
|
|
|
273 |
|
|
res = res + (d << (length*4));
|
274 |
|
|
}
|
275 |
|
|
|
276 |
|
|
return res;
|
277 |
|
|
}
|