Firmware development for use with LF21
Dependencies: USBDevice max32630fthr
Fork of FTHR_USB_serial by
Diff: main.cpp
- Revision:
- 8:e985d3e26b75
- Parent:
- 6:684c51f32c1d
- Child:
- 9:3c7f27df0b01
--- a/main.cpp Mon Feb 06 23:40:01 2017 +0000 +++ b/main.cpp Thu Sep 07 01:51:21 2017 +0000 @@ -1,8 +1,7 @@ #include "mbed.h" #include "max32630fthr.h" #include "USBSerial.h" - -MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); +//#include "string.h" // Hardware serial port over DAPLink Serial daplink(P2_1, P2_0); @@ -10,29 +9,214 @@ // Virtual serial port over USB USBSerial microUSB; +SPI spi(P5_1, P5_2, P5_0); // mosi, miso, sclk +DigitalOut spicsb(P4_0); // spi csb line + +DigitalOut p3_0(P3_0); +DigitalOut p3_1(P3_1); +DigitalOut p3_2(P3_2); +DigitalOut p3_3(P3_3); +DigitalOut p5_3(P5_3); +DigitalOut p5_4(P5_4); +DigitalOut p5_5(P5_5); +DigitalOut p5_6(P5_6); + DigitalOut rLED(LED1); DigitalOut gLED(LED2); DigitalOut bLED(LED3); +MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); + +int ascii(char a, char b); // main() runs in its own thread in the OS // (note the calls to Thread::wait below for delays) int main() { - int c; - - daplink.printf("daplink serial port\r\n"); - microUSB.printf("micro USB serial port\r\n"); - rLED = LED_ON; - gLED = LED_ON; - bLED = LED_OFF; - + char buf[10]; + + gLED = LED_OFF; + bLED = LED_ON; rLED = LED_OFF; - + spicsb = 1; + pegasus.max14690.ldo3SetVoltage(3600); + wait(1.0); + + p3_0 = 0; + p3_1 = 1; + + p3_2 = 1; + p3_3 = 0; + + p5_3 = 1; + p5_4 = 0; + + p5_5 = 1; + p5_6 = 1; + while(1) { - c = microUSB.getc(); - microUSB.putc(c); - daplink.putc(c); - bLED = c & 1; + + microUSB.scanf("%s", buf); + + // ping command, responds with pong + if(buf[0]=='p' && buf[1]=='i' && buf[2]=='n' && buf[3]=='g') { + microUSB.printf("pong\r\n"); + gLED=LED_OFF; + wait(0.01); + gLED=LED_ON; + wait(0.01); + gLED=LED_OFF; + } + // spi write command: spiw.addr.data + // addr and data in hex + else if (buf[0]=='s' && buf[1]=='p' && buf[2]=='i' && buf[3]=='w') { + int response = 0; + int payload[2]; + payload[0] = ascii((int)buf[5],(int)buf[6]); + payload[1] = ascii((int)buf[8],(int)buf[9]); + spicsb = 0; + response = spi.write(payload[0]); + response = spi.write(payload[1]); + spicsb = 1; + microUSB.printf("ack\r\n"); + gLED=LED_OFF; + wait(0.01); + gLED=LED_ON; + wait(0.01); + gLED=LED_OFF; + } + // spi read command: spir.addr + else if (buf[0]=='s' && buf[1]=='p' && buf[2]=='i' && buf[3]=='r') { + int response = 0; + int payload; + payload = ascii((int)buf[5],(int)buf[6]); + spicsb = 0; + response = spi.write(payload); + spicsb = 1; + microUSB.printf("ack.%i\r\n", response); + gLED=LED_OFF; + wait(0.01); + gLED=LED_ON; + wait(0.01); + gLED=LED_OFF; + } + // digital pin p5_5 + else if (buf[0]=='p' && buf[1]=='5' && buf[2]=='_' && buf[3]=='5') { + if (buf[5]=='1') p5_5 = 1; + else p5_5 = 0; + microUSB.printf("ack\r\n"); + gLED=LED_OFF; + wait(0.01); + gLED=LED_ON; + wait(0.01); + gLED=LED_OFF; + } + // digital pin p5_6 + else if (buf[0]=='p' && buf[1]=='5' && buf[2]=='_' && buf[3]=='6') { + if (buf[5]=='1') p5_6 = 1; + else p5_6 = 0; + microUSB.printf("ack\r\n"); + gLED=LED_OFF; + wait(0.01); + gLED=LED_ON; + wait(0.01); + gLED=LED_OFF; + } + // digital pin p5_3 + else if (buf[0]=='p' && buf[1]=='5' && buf[2]=='_' && buf[3]=='3') { + if (buf[5]=='1') p5_3 = 1; + else p5_3 = 0; + microUSB.printf("ack\r\n"); + gLED=LED_OFF; + wait(0.01); + gLED=LED_ON; + wait(0.01); + gLED=LED_OFF; + } + // digital pin p5_4 + else if (buf[0]=='p' && buf[1]=='5' && buf[2]=='_' && buf[3]=='4') { + if (buf[5]=='1') p5_4 = 1; + else p5_4 = 0; + microUSB.printf("ack\r\n"); + gLED=LED_OFF; + wait(0.01); + gLED=LED_ON; + wait(0.01); + gLED=LED_OFF; + } + // digital pin p3_1 + else if (buf[0]=='p' && buf[1]=='3' && buf[2]=='_' && buf[3]=='1') { + if (buf[5]=='1') p3_1 = 1; + else p3_1 = 0; + microUSB.printf("ack\r\n"); + gLED=LED_OFF; + wait(0.01); + gLED=LED_ON; + wait(0.01); + gLED=LED_OFF; + } + // digital pin p3_0 + else if (buf[0]=='p' && buf[1]=='3' && buf[2]=='_' && buf[3]=='0') { + if (buf[5]=='1') p3_0 = 1; + else p3_0 = 0; + microUSB.printf("ack\r\n"); + gLED=LED_OFF; + wait(0.01); + gLED=LED_ON; + wait(0.01); + gLED=LED_OFF; + } + // digital pin p3_3 + else if (buf[0]=='p' && buf[1]=='3' && buf[2]=='_' && buf[3]=='3') { + if (buf[5]=='1') p3_3 = 1; + else p3_3 = 0; + microUSB.printf("ack\r\n"); + gLED=LED_OFF; + wait(0.01); + gLED=LED_ON; + wait(0.01); + gLED=LED_OFF; + } + // digital pin p3_2 + else if (buf[0]=='p' && buf[1]=='3' && buf[2]=='_' && buf[3]=='2') { + if (buf[5]=='1') p3_2 = 1; + else p3_2 = 0; + microUSB.printf("ack\r\n"); + gLED=LED_OFF; + wait(0.01); + gLED=LED_ON; + wait(0.01); + gLED=LED_OFF; + } } + } +int ascii(char a, char b) +{ + int val1 = 0; + int val2 = 0; + int val = 0; + if(a>64 && a<71) + { + val1 = (int)a-55; + } + else if(a>96 && a<103) + { + val1 = (int)a-87; + } + else val1 = (int)a-48; + + if(b>64 && b<71) + { + val2 = (int)b-55; + } + else if(b>96 && b<103) + { + val2 = (int)b-87; + } + else val2 = (int)b-48; + + val=16*val1+val2; + + return (char)val; +} \ No newline at end of file