I2C/SPI/GPIO example for MAX32625PICO board

Dependencies:   SerialInterface USBDevice max32625pico

Fork of PICO_USB_I2C_SPI by Greg Steiert

Committer:
switches
Date:
Thu Dec 08 05:23:14 2016 +0000
Revision:
3:aa55728c8e09
Parent:
2:57500e991166
Child:
5:2436ae0a9eb1
USB serial adapter enables communication with I2C and SPI devices over USB.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
switches 0:60a522ae2e35 1 #include "mbed.h"
switches 2:57500e991166 2 #include "max32630fthr.h"
switches 1:6923b075c8d7 3 #include "USBSerial.h"
switches 3:aa55728c8e09 4 #include "SerialInterface.h"
switches 3:aa55728c8e09 5
switches 3:aa55728c8e09 6 #define UART_MAX_RESP_LENGTH 255
switches 3:aa55728c8e09 7 #define UART_MAX_CMD_LENGTH 255
switches 3:aa55728c8e09 8 #define USB_MAX_RESP_LENGTH 255
switches 3:aa55728c8e09 9 #define USB_MAX_CMD_LENGTH 255
switches 0:60a522ae2e35 10
switches 2:57500e991166 11 MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
switches 0:60a522ae2e35 12
switches 1:6923b075c8d7 13 // Hardware serial port over DAPLink
switches 2:57500e991166 14 Serial daplink(P2_1, P2_0);
switches 1:6923b075c8d7 15
switches 3:aa55728c8e09 16 // Virtual serial port over USB
switches 3:aa55728c8e09 17 USBSerial microUSB;
switches 3:aa55728c8e09 18
switches 3:aa55728c8e09 19 // Serial Interface Adapter
switches 3:aa55728c8e09 20 SerialInterface serInt;
switches 3:aa55728c8e09 21
switches 3:aa55728c8e09 22 // Serial Interfaces
switches 3:aa55728c8e09 23 I2C i2c(P3_4, P3_5);
switches 3:aa55728c8e09 24 SPI spi(P5_1, P5_2, P5_0, P5_3);
switches 3:aa55728c8e09 25
switches 3:aa55728c8e09 26 // Threads
switches 3:aa55728c8e09 27 Thread threadUSB;
switches 3:aa55728c8e09 28 Thread threadUART;
switches 3:aa55728c8e09 29
switches 2:57500e991166 30 DigitalOut rLED(LED1);
switches 2:57500e991166 31 DigitalOut gLED(LED2);
switches 2:57500e991166 32 DigitalOut bLED(LED3);
switches 0:60a522ae2e35 33
switches 3:aa55728c8e09 34 void usb_thread()
switches 3:aa55728c8e09 35 {
switches 3:aa55728c8e09 36 char obuf[USB_MAX_RESP_LENGTH+1];
switches 3:aa55728c8e09 37 char ibuf[USB_MAX_CMD_LENGTH+1];
switches 3:aa55728c8e09 38 int i = 0;
switches 3:aa55728c8e09 39
switches 3:aa55728c8e09 40 while(1) {
switches 3:aa55728c8e09 41 if (microUSB.readable()) {
switches 3:aa55728c8e09 42 ibuf[i]=microUSB.getc();
switches 3:aa55728c8e09 43 if (ibuf[i]!='\r') {
switches 3:aa55728c8e09 44 if (i < USB_MAX_CMD_LENGTH) {
switches 3:aa55728c8e09 45 i += 1;
switches 3:aa55728c8e09 46 }
switches 3:aa55728c8e09 47 } else {
switches 3:aa55728c8e09 48 rLED = LED_ON;
switches 3:aa55728c8e09 49 if (i < USB_MAX_CMD_LENGTH) {
switches 3:aa55728c8e09 50 ibuf[i]=0;
switches 3:aa55728c8e09 51 // microUSB.printf("UART CMD: %s=", ibuf);
switches 3:aa55728c8e09 52 serInt.call(ibuf, obuf);
switches 3:aa55728c8e09 53 microUSB.printf("%s\r\n", obuf);
switches 3:aa55728c8e09 54 } else {
switches 3:aa55728c8e09 55 microUSB.printf("[-1]\r\n");
switches 3:aa55728c8e09 56 }
switches 3:aa55728c8e09 57 i=0;
switches 3:aa55728c8e09 58 rLED = LED_OFF;
switches 3:aa55728c8e09 59 }
switches 3:aa55728c8e09 60 }
switches 3:aa55728c8e09 61 }
switches 3:aa55728c8e09 62 }
switches 3:aa55728c8e09 63
switches 3:aa55728c8e09 64 void uart_thread()
switches 3:aa55728c8e09 65 {
switches 3:aa55728c8e09 66 char obuf[UART_MAX_RESP_LENGTH+1];
switches 3:aa55728c8e09 67 char ibuf[UART_MAX_CMD_LENGTH+1];
switches 3:aa55728c8e09 68 int i = 0;
switches 3:aa55728c8e09 69
switches 3:aa55728c8e09 70 while(1) {
switches 3:aa55728c8e09 71 if (daplink.readable()) {
switches 3:aa55728c8e09 72 ibuf[i]=daplink.getc();
switches 3:aa55728c8e09 73 if (ibuf[i]!='\r') {
switches 3:aa55728c8e09 74 if (i < UART_MAX_CMD_LENGTH) {
switches 3:aa55728c8e09 75 i += 1;
switches 3:aa55728c8e09 76 }
switches 3:aa55728c8e09 77 } else {
switches 3:aa55728c8e09 78 bLED = LED_ON;
switches 3:aa55728c8e09 79 if (i < UART_MAX_CMD_LENGTH) {
switches 3:aa55728c8e09 80 ibuf[i]=0;
switches 3:aa55728c8e09 81 // daplink.printf("UART CMD: %s=", ibuf);
switches 3:aa55728c8e09 82 serInt.call(ibuf, obuf);
switches 3:aa55728c8e09 83 daplink.printf("%s\r\n", obuf);
switches 3:aa55728c8e09 84 } else {
switches 3:aa55728c8e09 85 daplink.printf("[-1]\r\n");
switches 3:aa55728c8e09 86 }
switches 3:aa55728c8e09 87 i=0;
switches 3:aa55728c8e09 88 bLED = LED_OFF;
switches 3:aa55728c8e09 89 }
switches 3:aa55728c8e09 90 }
switches 3:aa55728c8e09 91 }
switches 3:aa55728c8e09 92 }
switches 3:aa55728c8e09 93
switches 0:60a522ae2e35 94 // main() runs in its own thread in the OS
switches 0:60a522ae2e35 95 // (note the calls to Thread::wait below for delays)
switches 0:60a522ae2e35 96 int main()
switches 0:60a522ae2e35 97 {
switches 2:57500e991166 98 daplink.printf("daplink serial port\r\n");
switches 3:aa55728c8e09 99 microUSB.printf("micro USB serial port\r\n");
switches 2:57500e991166 100 rLED = LED_ON;
switches 2:57500e991166 101 gLED = LED_ON;
switches 2:57500e991166 102 bLED = LED_OFF;
switches 3:aa55728c8e09 103
switches 3:aa55728c8e09 104 // Board Initialization
switches 2:57500e991166 105 pegasus.init();
switches 0:60a522ae2e35 106
switches 3:aa55728c8e09 107 // Serial Interface Setup
switches 3:aa55728c8e09 108 serInt.init(&i2c, &spi);
switches 2:57500e991166 109
switches 2:57500e991166 110 rLED = LED_OFF;
switches 3:aa55728c8e09 111
switches 3:aa55728c8e09 112 // Start USB serial thread
switches 3:aa55728c8e09 113 threadUSB.start(usb_thread);
switches 3:aa55728c8e09 114
switches 3:aa55728c8e09 115 // Start UART serial thread
switches 3:aa55728c8e09 116 threadUART.start(uart_thread);
switches 1:6923b075c8d7 117
switches 3:aa55728c8e09 118 // Start main loop
switches 1:6923b075c8d7 119 while(1) {
switches 3:aa55728c8e09 120 Thread::wait(500);
switches 3:aa55728c8e09 121 gLED = !gLED;
switches 0:60a522ae2e35 122 }
switches 0:60a522ae2e35 123 }
switches 0:60a522ae2e35 124