OpenCores
URL https://opencores.org/ocsvn/neorv32/neorv32/trunk

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [sw/] [example/] [demo_twi/] [main.c] - Blame information for rev 65

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.