1 |
198 |
zero_gravi |
// #################################################################################################
|
2 |
|
|
// # < TWI bus explorer > #
|
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 NEO430 Processor - https://github.com/stnolting/neo430 #
|
33 |
|
|
// #################################################################################################
|
34 |
|
|
|
35 |
|
|
|
36 |
|
|
// Libraries
|
37 |
|
|
#include <stdint.h>
|
38 |
|
|
#include <string.h>
|
39 |
|
|
#include <neo430.h>
|
40 |
|
|
|
41 |
|
|
// Prototypes
|
42 |
|
|
void scan_twi(void);
|
43 |
|
|
void set_speed(void);
|
44 |
|
|
void send_twi(void);
|
45 |
|
|
|
46 |
|
|
// Configuration
|
47 |
|
|
#define BAUD_RATE 19200
|
48 |
|
|
|
49 |
|
|
// Global variables
|
50 |
|
|
uint16_t bus_claimed;
|
51 |
|
|
|
52 |
|
|
|
53 |
|
|
/* ------------------------------------------------------------
|
54 |
|
|
* INFO Main function
|
55 |
|
|
* ------------------------------------------------------------ */
|
56 |
|
|
int main(void) {
|
57 |
|
|
|
58 |
|
|
// setup UART
|
59 |
|
|
neo430_uart_setup(BAUD_RATE);
|
60 |
|
|
|
61 |
|
|
char buffer[8];
|
62 |
|
|
uint16_t length = 0;
|
63 |
|
|
|
64 |
|
|
neo430_uart_br_print("\n---------------------------------\n"
|
65 |
|
|
"--- TWI Bus Explorer Terminal ---\n"
|
66 |
|
|
"---------------------------------\n\n");
|
67 |
|
|
|
68 |
|
|
// check if TWI unit was synthesized, exit if no TWI is available
|
69 |
|
|
if (!(SYS_FEATURES & (1<<SYS_TWI_EN))) {
|
70 |
|
|
neo430_uart_br_print("Error! No TWI synthesized!");
|
71 |
|
|
return 1;
|
72 |
|
|
}
|
73 |
|
|
|
74 |
|
|
neo430_uart_br_print("This program allows to create TWI transfers by hand.\n"
|
75 |
|
|
"Type 'help' to see the help menu.\n\n");
|
76 |
|
|
|
77 |
|
|
// init TWI
|
78 |
|
|
// SCL clock speed = f_cpu / (4 * PRSC)
|
79 |
|
|
neo430_twi_enable(TWI_PRSC_2048); // second slowest
|
80 |
|
|
bus_claimed = 0; // no active bus session
|
81 |
|
|
|
82 |
|
|
// Main menu
|
83 |
|
|
for (;;) {
|
84 |
|
|
neo430_uart_br_print("TWI_EXPLORER:> ");
|
85 |
|
|
length = neo430_uart_scan(buffer, 8, 1);
|
86 |
|
|
neo430_uart_br_print("\n");
|
87 |
|
|
|
88 |
|
|
if (!length) // nothing to be done
|
89 |
|
|
continue;
|
90 |
|
|
|
91 |
|
|
// decode input and execute command
|
92 |
|
|
if (!strcmp(buffer, "help")) {
|
93 |
|
|
neo430_uart_br_print("Available commands:\n"
|
94 |
|
|
" help - show this text\n"
|
95 |
|
|
" scan - scan bus for devices\n"
|
96 |
|
|
" start - generate START condition\n"
|
97 |
|
|
" stop - generate STOP condition\n"
|
98 |
|
|
" send - write & read single byte to/from bus\n"
|
99 |
|
|
" speed - select bus clock\n"
|
100 |
|
|
" reset - perform soft-reset\n"
|
101 |
|
|
" exit - exit program and return to bootloader\n\n"
|
102 |
|
|
"Start a new transmission by generating a START condition. Next, transfer the 7-bit device address\n"
|
103 |
|
|
"and the R/W flag. After that, transfer your data to be written or send a 0xFF if you want to read\n"
|
104 |
|
|
"data from the bus. Finish the transmission by generating a STOP condition.\n");
|
105 |
|
|
}
|
106 |
|
|
else if (!strcmp(buffer, "start")) {
|
107 |
|
|
neo430_twi_generate_start(); // generate START condition
|
108 |
|
|
bus_claimed = 1;
|
109 |
|
|
}
|
110 |
|
|
else if (!strcmp(buffer, "stop")) {
|
111 |
|
|
if (bus_claimed == 0) {
|
112 |
|
|
neo430_uart_br_print("No active I2C transmission.\n");
|
113 |
|
|
continue;
|
114 |
|
|
}
|
115 |
|
|
neo430_twi_generate_stop(); // generate STOP condition
|
116 |
|
|
bus_claimed = 0;
|
117 |
|
|
}
|
118 |
|
|
else if (!strcmp(buffer, "scan")) {
|
119 |
|
|
scan_twi();
|
120 |
|
|
}
|
121 |
|
|
else if (!strcmp(buffer, "speed")) {
|
122 |
|
|
set_speed();
|
123 |
|
|
}
|
124 |
|
|
else if (!strcmp(buffer, "send")) {
|
125 |
|
|
send_twi();
|
126 |
|
|
}
|
127 |
|
|
else if (!strcmp(buffer, "reset")) {
|
128 |
|
|
while ((UART_CT & (1<<UART_CT_TX_BUSY)) != 0); // wait for current UART transmission
|
129 |
|
|
neo430_twi_disable();
|
130 |
|
|
neo430_soft_reset();
|
131 |
|
|
}
|
132 |
|
|
else if (!strcmp(buffer, "exit")) {
|
133 |
|
|
if (!(SYS_FEATURES & (1<<SYS_BTLD_EN)))
|
134 |
|
|
neo430_uart_br_print("No bootloader installed!\n");
|
135 |
|
|
else
|
136 |
|
|
asm volatile ("mov #0xF000, r0");
|
137 |
|
|
}
|
138 |
|
|
else {
|
139 |
|
|
neo430_uart_br_print("Invalid command. Type 'help' to see all commands.\n");
|
140 |
|
|
}
|
141 |
|
|
}
|
142 |
|
|
|
143 |
|
|
return 0;
|
144 |
|
|
}
|
145 |
|
|
|
146 |
|
|
|
147 |
|
|
/* ------------------------------------------------------------
|
148 |
|
|
* INFO Set TWI speed
|
149 |
|
|
* ------------------------------------------------------------ */
|
150 |
|
|
void set_speed(void) {
|
151 |
|
|
|
152 |
|
|
char terminal_buffer[2];
|
153 |
|
|
|
154 |
|
|
neo430_uart_br_print("Select new clock prescaler (0..7): ");
|
155 |
|
|
neo430_uart_scan(terminal_buffer, 2, 1); // 1 hex char plus '\0'
|
156 |
|
|
uint8_t prsc = (uint8_t)neo430_hexstr_to_uint(terminal_buffer, strlen(terminal_buffer));
|
157 |
|
|
if ((prsc >= 0) && (prsc < 8)) { // valid?
|
158 |
|
|
TWI_CT = 0; // reset
|
159 |
|
|
TWI_CT = (1 << TWI_CT_EN) | (prsc << TWI_CT_PRSC0);
|
160 |
|
|
neo430_uart_br_print("\nDone.\n");
|
161 |
|
|
}
|
162 |
|
|
else {
|
163 |
|
|
neo430_uart_br_print("\nInvalid selection!\n");
|
164 |
|
|
return;
|
165 |
|
|
}
|
166 |
|
|
|
167 |
|
|
// print new clock frequency
|
168 |
|
|
uint32_t clock = CLOCKSPEED_32bit;
|
169 |
|
|
switch (prsc) {
|
170 |
|
|
case 0: clock = clock / 2; break;
|
171 |
|
|
case 1: clock = clock / 4; break;
|
172 |
|
|
case 2: clock = clock / 8; break;
|
173 |
|
|
case 3: clock = clock / 64; break;
|
174 |
|
|
case 4: clock = clock / 128; break;
|
175 |
|
|
case 5: clock = clock / 1024; break;
|
176 |
|
|
case 6: clock = clock / 2048; break;
|
177 |
|
|
case 7: clock = clock / 4096; break;
|
178 |
|
|
default: clock = 0; break;
|
179 |
|
|
}
|
180 |
|
|
neo430_printf("New I2C clock: %n Hz\n", clock);
|
181 |
|
|
}
|
182 |
|
|
|
183 |
|
|
|
184 |
|
|
/* ------------------------------------------------------------
|
185 |
|
|
* INFO Scan 7-bit TWI address space
|
186 |
|
|
* ------------------------------------------------------------ */
|
187 |
|
|
void scan_twi(void) {
|
188 |
|
|
|
189 |
|
|
neo430_uart_br_print("Scanning TWI bus...\n");
|
190 |
|
|
uint8_t i, num_devices = 0;
|
191 |
|
|
for (i=0; i<128; i++) {
|
192 |
|
|
uint8_t twi_ack = neo430_twi_start_trans((uint8_t)(2*i+1));
|
193 |
|
|
neo430_twi_generate_stop();
|
194 |
|
|
|
195 |
|
|
if (twi_ack == 0) {
|
196 |
|
|
neo430_uart_br_print("+ Found device at write-address 0x");
|
197 |
|
|
neo430_uart_print_hex_byte(2*i);
|
198 |
|
|
neo430_uart_br_print("\n");
|
199 |
|
|
num_devices++;
|
200 |
|
|
}
|
201 |
|
|
}
|
202 |
|
|
|
203 |
|
|
if (!num_devices) {
|
204 |
|
|
neo430_uart_br_print("No devices found.\n");
|
205 |
|
|
}
|
206 |
|
|
}
|
207 |
|
|
|
208 |
|
|
|
209 |
|
|
/* ------------------------------------------------------------
|
210 |
|
|
* INFO Read/write 1 byte from/to bus
|
211 |
|
|
* ------------------------------------------------------------ */
|
212 |
|
|
void send_twi(void) {
|
213 |
|
|
|
214 |
|
|
char terminal_buffer[4];
|
215 |
|
|
|
216 |
|
|
if (bus_claimed == 0) {
|
217 |
|
|
neo430_uart_br_print("No active I2C transmission. Generate a START condition first.\n");
|
218 |
|
|
return;
|
219 |
|
|
}
|
220 |
|
|
|
221 |
|
|
// enter data
|
222 |
|
|
neo430_uart_br_print("Enter TX data (2 hex chars): ");
|
223 |
|
|
neo430_uart_scan(terminal_buffer, 3, 1); // 2 hex chars for address plus '\0'
|
224 |
|
|
uint8_t tmp = (uint8_t)neo430_hexstr_to_uint(terminal_buffer, strlen(terminal_buffer));
|
225 |
|
|
uint8_t res = neo430_twi_trans(tmp);
|
226 |
|
|
neo430_uart_br_print("\nRX data: 0x");
|
227 |
|
|
neo430_uart_print_hex_byte(neo430_twi_get_data());
|
228 |
|
|
neo430_uart_br_print("\nResponse: ");
|
229 |
|
|
if (res == 0)
|
230 |
|
|
neo430_uart_br_print("ACK\n");
|
231 |
|
|
else
|
232 |
|
|
neo430_uart_br_print("NACK\n");
|
233 |
|
|
|
234 |
|
|
}
|
235 |
|
|
|