This is a simple terminal used for send and receive data via SPI (p5, p6, p7). It use the usb as serial (9600bps) interface port.
Commands:
#
All the numeric constants MUST be in Hexadecimal #
- C[level] - Change the [level] of the CS line (p8)
- D[data] - Send single byte of [data]
- M[data],[data], .. ,[data] - Send multiple bytes of [data]
- F[data],[size] - Send a flow sized [size] of [data] bytes
- R[size] - Receive [size] bytes
Examples
- C0 - Put the CS at logic level 0
- C1 - Put the CS at logic level 1
- Dab - Send out the byte 0xab
- M55,aa,01,ab,cd - Send out the bytes 0x55, 0xaa, 0x01, 0xab, 0xcd
- Fff,05 - Send out 5 bytes of value 0xff
- R10 - Receive 16 bytes
Diff: main.cpp
- Revision:
- 0:7beba59b8a7d
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon May 19 18:45:27 2014 +0000 @@ -0,0 +1,104 @@ +#include "mbed.h" +#include <string.h> +#include <stdlib.h> + +/* Commands: + Dd - Data out + Cx - CS line state (0/1) + Fd,l - Data flow out, the total size is l + Rl - Receive data, l is the size + Md,d.. - Multiple data write +*/ + +DigitalOut myled(LED1); + +SPI spi_device(p5, p6, p7); +DigitalOut spi_cs(p8); + +Serial tty_usb(USBTX, USBRX); +char tty_buffer[0x100]; + +void receive_command() { + uint8_t buffer_ptr = 0; + char ch; + + tty_usb.putc('>'); + while (1) { + if (tty_usb.readable()) { + ch = tty_usb.getc(); + tty_usb.putc(ch); + + tty_buffer[buffer_ptr] = ch; + buffer_ptr++; + if ((ch == '\n') || (ch == '\r')) { + tty_usb.putc('\n'); + tty_usb.putc('\r'); + break; + } + if (ch == 0x7f) { + buffer_ptr -= 2; + tty_usb.putc(0x8); + tty_usb.putc(' '); + tty_usb.putc(0x8); + } + } + } +} + +int main() { + uint8_t arg_data, arg_data_in; + uint16_t arg_size; + char *arg_ptr; + + tty_usb.baud(9600); + + while(1) { + + receive_command(); + switch (tty_buffer[0]) { + case 'D': + case 'd': + arg_data = strtol(&tty_buffer[1], NULL, 16); + arg_data_in = spi_device.write(arg_data); + tty_usb.printf("#\tSingle data: OUT: $%02X - IN: $%02X\n\r", arg_data, arg_data_in); + break; + case 'C': + case 'c': + spi_cs = (tty_buffer[1] == '0') ? 0 : 1; + break; + case 'R': + case 'r': + arg_size = strtol(&tty_buffer[1], NULL, 16); + for (int i = 0; i < arg_size; i++) + tty_usb.printf("#\tReceived data [$%04X]: $%02X\n\r", i, spi_device.write(0x00)); + break; + + case 'F': + case 'f': + arg_size = strtol(strchr(tty_buffer, ',')+1, NULL, 16); + *(strchr(tty_buffer, ',') + 1) = 0x00; + arg_data = strtol(&tty_buffer[1], NULL, 16); + + for (int i = 0; i < arg_size; i++) { + arg_data_in = spi_device.write(arg_data); + tty_usb.printf("#\tFlow data [$%04X]: OUT: $%02X - IN: $%02X\n\r", i, arg_data, arg_data_in); + + } + break; + + case 'M': + case 'm': + arg_ptr = strtok(&tty_buffer[1], ",\n\r"); + arg_size = 0; + while (arg_ptr != NULL) { + arg_data = strtol(arg_ptr, NULL, 16); + arg_data_in = spi_device.write(arg_data); + tty_usb.printf("#\tMultiple data [$%04X]: OUT: $%02X - IN: $%02X\n\r", arg_size, arg_data, arg_data_in); + arg_ptr = strtok(NULL, ",\n\r"); + arg_size++; + } + break; + + } + } +}