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

Subversion Repositories neo430

[/] [neo430/] [trunk/] [neo430/] [sw/] [example/] [wb_terminal/] [main.c] - Blame information for rev 198

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 198 zero_gravi
// #################################################################################################
2
// #  < Wishbone bus explorer >                                                                    #
3
// # ********************************************************************************************* #
4
// # Manual access to the registers of modules, which are connected to the Wishbone bus. This tool #
5
// # uses NONBLOCKING Wishbone transactions.                                                       #
6
// # ********************************************************************************************* #
7
// # BSD 3-Clause License                                                                          #
8
// #                                                                                               #
9
// # Copyright (c) 2020, Stephan Nolting. All rights reserved.                                     #
10
// #                                                                                               #
11
// # Redistribution and use in source and binary forms, with or without modification, are          #
12
// # permitted provided that the following conditions are met:                                     #
13
// #                                                                                               #
14
// # 1. Redistributions of source code must retain the above copyright notice, this list of        #
15
// #    conditions and the following disclaimer.                                                   #
16
// #                                                                                               #
17
// # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
18
// #    conditions and the following disclaimer in the documentation and/or other materials        #
19
// #    provided with the distribution.                                                            #
20
// #                                                                                               #
21
// # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
22
// #    endorse or promote products derived from this software without specific prior written      #
23
// #    permission.                                                                                #
24
// #                                                                                               #
25
// # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
26
// # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
27
// # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
28
// # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
29
// # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
30
// # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
31
// # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
32
// # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
33
// # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
34
// # ********************************************************************************************* #
35
// # The NEO430 Processor - https://github.com/stnolting/neo430                                    #
36
// #################################################################################################
37
 
38
 
39
// Libraries
40
#include <stdint.h>
41
#include <string.h>
42
#include <neo430.h>
43
 
44
// Global variables
45
uint8_t wb_config = 0;
46
 
47
// Prototypes
48
void setup_wb(void);
49
void read_wb_address(void);
50
void write_wb_address(void);
51
void dump_wb(void);
52
 
53
// Configuration
54
#define MAX_CMD_LENGTH 16
55
#define BAUD_RATE 19200
56
 
57
 
58
/* ------------------------------------------------------------
59
 * INFO Main function
60
 * ------------------------------------------------------------ */
61
int main(void) {
62
 
63
  // setup UART
64
  neo430_uart_setup(BAUD_RATE);
65
 
66
  char buffer[MAX_CMD_LENGTH];
67
  uint16_t length = 0;
68
  uint16_t selection = 0;
69
 
70
  neo430_uart_br_print("\n--------------------------------------\n"
71
                  "--- Wishbone Bus Explorer Terminal ---\n"
72
                  "--------------------------------------\n\n");
73
 
74
  // check if WB unit was synthesized, exit if no WB is available
75
  if (!(SYS_FEATURES & (1<<SYS_WB32_EN))) {
76
    neo430_uart_br_print("Error! No Wishbone adapter synthesized!");
77
    return 1;
78
  }
79
 
80
  // default config
81
  wb_config = 4;
82
  neo430_wishbone_terminate(); // terminate current transfer
83
 
84
  neo430_uart_br_print("Configure the actual data transfer size (1, 2 or 4 bytes)\n"
85
                       "using 'setup'. Addresses are always 32-bit wide.\n"
86
                       "This tool uses non-blocking Wishbone transactions.\n\n"
87
                       "Type 'help' to see the help menu.\n\n");
88
 
89
  // Main menu
90
  for (;;) {
91
    neo430_uart_br_print("WB_EXPLORER:> ");
92
    length = neo430_uart_scan(buffer, MAX_CMD_LENGTH, 1);
93
    neo430_uart_br_print("\n");
94
 
95
    if (!length) // nothing to be done
96
     continue;
97
 
98
    // decode input
99
    selection = 0;
100
    if (!strcmp(buffer, "help"))
101
      selection = 1;
102
    if (!strcmp(buffer, "setup"))
103
      selection = 2;
104
    if (!strcmp(buffer, "read"))
105
      selection = 3;
106
    if (!strcmp(buffer, "write"))
107
      selection = 4;
108
    if (!strcmp(buffer, "dump"))
109
      selection = 5;
110
    if (!strcmp(buffer, "reset"))
111
      selection = 6;
112
    if (!strcmp(buffer, "exit"))
113
      selection = 7;
114
 
115
    // execute command
116
    switch(selection) {
117
 
118
      case 1: // print help menu
119
        neo430_uart_br_print("Available commands:\n"
120
                             " help  - show this text\n"
121
                             " setup - configure WB interface\n"
122
                             " read  - read from WB address\n"
123
                             " write - write to WB address\n"
124
                             " dump  - dump data from WB addresses\n"
125
                             " reset - perform soft-reset\n"
126
                             " exit  - exit program and return to bootloader\n");
127
        break;
128
 
129
      case 2: // setup Wishbone adapter
130
        setup_wb();
131
        break;
132
 
133
      case 3: // read from address
134
        read_wb_address();
135
        break;
136
 
137
      case 4: // write to address
138
        write_wb_address();
139
        break;
140
 
141
      case 5: // dump data
142
        dump_wb();
143
        break;
144
 
145
      case 6: // restart
146
        while ((UART_CT & (1<<UART_CT_TX_BUSY)) != 0); // wait for current UART transmission
147
        neo430_soft_reset();
148
        break;
149
 
150
      case 7: // goto bootloader
151
        if (!(SYS_FEATURES & (1<<SYS_BTLD_EN)))
152
          neo430_uart_br_print("No bootloader installed!\n");
153
        else
154
          asm volatile ("mov #0xF000, r0");
155
        break;
156
 
157
      default: // invalid command
158
        neo430_uart_br_print("Invalid command. Type 'help' to see all commands.\n");
159
        break;
160
    }
161
  }
162
 
163
  return 0;
164
}
165
 
166
 
167
/* ------------------------------------------------------------
168
 * INFO Configure Wishbone adapter
169
 * ------------------------------------------------------------ */
170
void setup_wb(void) {
171
 
172
  char buffer[2];
173
 
174
  neo430_uart_br_print("Select transfer size in bytes (1,2,4): ");
175
  neo430_uart_scan(buffer, 2, 1);
176
 
177
  // process input
178
  if (!strcmp(buffer, "1"))
179
    wb_config = 1;
180
  else if (!strcmp(buffer, "2"))
181
    wb_config = 2;
182
  else if (!strcmp(buffer, "4"))
183
    wb_config = 4;
184
  else {
185
    neo430_uart_br_print("\nInvalid input. Cancelling setup.\n");
186
    return;
187
  }
188
 
189
  neo430_uart_br_print("\nSetup done.\n");
190
}
191
 
192
 
193
/* ------------------------------------------------------------
194
 * INFO Read data from Wishbone address
195
 * ------------------------------------------------------------ */
196
void read_wb_address(void) {
197
 
198
  char buffer[9];
199
 
200
  neo430_uart_br_print("Enter hexadecimal target address: 0x");
201
  neo430_uart_scan(buffer, 9, 1); // 8 hex chars for address plus '\0'
202
  uint32_t address = neo430_hexstr_to_uint(buffer, strlen(buffer));
203
 
204
  neo430_uart_br_print("\nReading from [0x");
205
  neo430_uart_print_hex_dword(address);
206
  neo430_uart_br_print("]... ");
207
 
208
  // print result
209
  neo430_uart_br_print("Read data: 0x");
210
 
211
  if (wb_config == 1)
212
    neo430_wishbone32_read8_start(address);
213
  else if (wb_config == 2)
214
    neo430_wishbone32_read16_start(address);
215
  else if (wb_config == 4)
216
    neo430_wishbone32_read32_start(address);
217
 
218
  // wait for transfer to finish
219
  uint16_t timeout = 0;
220
  while(1){
221
    if (!neo430_wishbone_busy())
222
      break;
223
    if (timeout++ == 100) {
224
      neo430_uart_br_print("\nError! Device not responding! Press key to proceed.\n");
225
      neo430_wishbone_terminate(); // terminate current transfer
226
      while(!neo430_uart_char_received());
227
      return;
228
    }
229
  }
230
 
231
  // read data
232
  if (wb_config == 1)
233
    neo430_uart_print_hex_byte(neo430_wishbone32_get_data8(address));
234
  else if (wb_config == 2)
235
    neo430_uart_print_hex_word(neo430_wishbone32_get_data16(address));
236
  else if (wb_config == 4)
237
    neo430_uart_print_hex_dword(neo430_wishbone32_get_data32());
238
 
239
  neo430_uart_br_print("\n");
240
}
241
 
242
 
243
/* ------------------------------------------------------------
244
 * INFO Write data to Wishbone address
245
 * ------------------------------------------------------------ */
246
void write_wb_address(void) {
247
 
248
  char buffer[9];
249
 
250
  neo430_uart_br_print("Enter hexadecimal target address: 0x");
251
  neo430_uart_scan(buffer, 9, 1); // 8 hex chars for address plus '\0'
252
  uint32_t address = neo430_hexstr_to_uint(buffer, strlen(buffer));
253
 
254
  neo430_uart_br_print("\nEnter hexadecimal write data: 0x");
255
  neo430_uart_scan(buffer, wb_config*2+1, 1); // get right number of hex chars for data plus '\0'
256
  uint32_t data = neo430_hexstr_to_uint(buffer, strlen(buffer));
257
 
258
  neo430_uart_br_print("\nWriting '0x");
259
  neo430_uart_print_hex_dword(data);
260
  neo430_uart_br_print("' to [0x");
261
  neo430_uart_print_hex_dword(address);
262
  neo430_uart_br_print("]... ");
263
 
264
  // perform access
265
  if (wb_config == 1)
266
    neo430_wishbone32_write8_start(address, (uint8_t)data);
267
  else if (wb_config == 2)
268
    neo430_wishbone32_write16_start(address, (uint16_t)data);
269
  else if (wb_config == 4)
270
    neo430_wishbone32_write32_start(address, data);
271
 
272
  // wait for transfer to finish
273
  uint16_t timeout = 0;
274
  while(1){
275
    if (!neo430_wishbone_busy())
276
      break;
277
    if (timeout++ == 100) {
278
      neo430_uart_br_print("\nError! Device not responding! Press key to proceed.\n");
279
      neo430_wishbone_terminate(); // terminate current transfer
280
      while(!neo430_uart_char_received());
281
      return;
282
    }
283
  }
284
 
285
  neo430_uart_br_print("Done.\n");
286
}
287
 
288
 
289
/* ------------------------------------------------------------
290
 * INFO Dump data from Wishbone address
291
 * ------------------------------------------------------------ */
292
void dump_wb(void) {
293
 
294
  char buffer[9];
295
  uint16_t i = 0;
296
 
297
  neo430_uart_br_print("Enter hexadecimal start address: 0x");
298
  neo430_uart_scan(buffer, 9, 1); // 8 hex chars for address plus '\0'
299
  uint32_t address = neo430_hexstr_to_uint(buffer, strlen(buffer));
300
 
301
  neo430_uart_br_print("\nPress any key to start.\n"
302
                       "You can abort dumping by pressing any key.\n");
303
  while(!neo430_uart_char_received());
304
 
305
  while(1) {
306
    neo430_uart_br_print("0x");
307
    neo430_uart_print_hex_dword(address);
308
    neo430_uart_br_print(":  ");
309
 
310
    uint16_t border = 16 / wb_config;
311
    for (i=0; i<border; i++) {
312
 
313
      // trigger access
314
      if (wb_config == 1)
315
        neo430_wishbone32_read8_start(address);
316
      else if (wb_config == 2)
317
        neo430_wishbone32_read16_start(address);
318
      else if (wb_config == 4)
319
        neo430_wishbone32_read32_start(address);
320
 
321
      // wait for transfer to finish
322
      uint16_t timeout = 0;
323
      while(1){
324
        if (!neo430_wishbone_busy())
325
          break;
326
        if (timeout++ == 100) {
327
          neo430_uart_br_print("\nError! Device not responding! Press key to proceed.\n");
328
          neo430_wishbone_terminate(); // terminate current transfer
329
          while(!neo430_uart_char_received());
330
          return;
331
        }
332
      }
333
 
334
      // read data
335
      if (wb_config == 1) {
336
        neo430_uart_print_hex_byte(neo430_wishbone32_get_data8(address));
337
        address += 1;
338
      }
339
      else if (wb_config == 2) {
340
        neo430_uart_print_hex_word(neo430_wishbone32_get_data16(address));
341
        address += 2;
342
      }
343
      else if (wb_config == 4) {
344
        neo430_uart_print_hex_dword(neo430_wishbone32_get_data32());
345
        address += 4;
346
      }
347
 
348
      neo430_uart_putc(' ');
349
    }
350
 
351
    neo430_uart_br_print("\n");
352
    if (neo430_uart_char_received()) // abort
353
      return;
354
  }
355
}
356
 

powered by: WebSVN 2.1.0

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