I2C/SPI/GPIO example for MAX32625PICO board

Dependencies:   SerialInterface USBDevice max32625pico

Fork of PICO_USB_I2C_SPI by Greg Steiert

Committer:
switches
Date:
Thu Apr 13 15:03:15 2017 +0000
Revision:
13:fed6ff32bf5d
Parent:
11:b9d7ab7626b5
Child:
14:dd5a96c353ed
New serial adapter example for PICO board

Who changed what in which revision?

UserRevisionLine numberNew contents of line
switches 0:60a522ae2e35 1 #include "mbed.h"
switches 13:fed6ff32bf5d 2 #include "rtos.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 7:5de026dd3a82 11 // Virtual serial port over USB
switches 7:5de026dd3a82 12 USBSerial microUSB;
switches 0:60a522ae2e35 13
switches 9:e59eb5f5ce32 14 // Hardware serial port over DAPLink
switches 9:e59eb5f5ce32 15 Serial daplink(P2_1, P2_0);
switches 3:aa55728c8e09 16
switches 3:aa55728c8e09 17 // Serial Interfaces
switches 13:fed6ff32bf5d 18 I2C i2c(P1_6, P1_7);
switches 13:fed6ff32bf5d 19 SPI spi(P1_5, P1_6, P1_4);
switches 13:fed6ff32bf5d 20 DigitalInOut gpio[] = {P0_0, P0_1, P0_2, P0_3, P4_4, P4_5, P4_6, P4_7};
switches 9:e59eb5f5ce32 21 AnalogIn ain[] = {AIN_0, AIN_1, AIN_2, AIN_3, AIN_4, AIN_5, AIN_6, AIN_7};
switches 9:e59eb5f5ce32 22
switches 9:e59eb5f5ce32 23 // Serial Interface Adapter
switches 10:a3f06fa13bda 24 SerialInterface serInt(i2c, spi, gpio, ain);
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 5:2436ae0a9eb1 40 microUSB.printf("micro USB serial port\r\n");
switches 5:2436ae0a9eb1 41
switches 3:aa55728c8e09 42 while(1) {
switches 3:aa55728c8e09 43 if (microUSB.readable()) {
switches 3:aa55728c8e09 44 ibuf[i]=microUSB.getc();
switches 3:aa55728c8e09 45 if (ibuf[i]!='\r') {
switches 3:aa55728c8e09 46 if (i < USB_MAX_CMD_LENGTH) {
switches 3:aa55728c8e09 47 i += 1;
switches 3:aa55728c8e09 48 }
switches 3:aa55728c8e09 49 } else {
switches 3:aa55728c8e09 50 rLED = LED_ON;
switches 3:aa55728c8e09 51 if (i < USB_MAX_CMD_LENGTH) {
switches 3:aa55728c8e09 52 ibuf[i]=0;
switches 3:aa55728c8e09 53 // microUSB.printf("UART CMD: %s=", ibuf);
switches 3:aa55728c8e09 54 serInt.call(ibuf, obuf);
switches 3:aa55728c8e09 55 microUSB.printf("%s\r\n", obuf);
switches 3:aa55728c8e09 56 } else {
switches 3:aa55728c8e09 57 microUSB.printf("[-1]\r\n");
switches 3:aa55728c8e09 58 }
switches 3:aa55728c8e09 59 i=0;
switches 3:aa55728c8e09 60 rLED = LED_OFF;
switches 3:aa55728c8e09 61 }
switches 3:aa55728c8e09 62 }
switches 3:aa55728c8e09 63 }
switches 3:aa55728c8e09 64 }
switches 3:aa55728c8e09 65
switches 3:aa55728c8e09 66 void uart_thread()
switches 3:aa55728c8e09 67 {
switches 3:aa55728c8e09 68 char obuf[UART_MAX_RESP_LENGTH+1];
switches 3:aa55728c8e09 69 char ibuf[UART_MAX_CMD_LENGTH+1];
switches 3:aa55728c8e09 70 int i = 0;
switches 3:aa55728c8e09 71
switches 5:2436ae0a9eb1 72 daplink.printf("daplink serial port\r\n");
switches 5:2436ae0a9eb1 73
switches 3:aa55728c8e09 74 while(1) {
switches 3:aa55728c8e09 75 if (daplink.readable()) {
switches 3:aa55728c8e09 76 ibuf[i]=daplink.getc();
switches 3:aa55728c8e09 77 if (ibuf[i]!='\r') {
switches 3:aa55728c8e09 78 if (i < UART_MAX_CMD_LENGTH) {
switches 3:aa55728c8e09 79 i += 1;
switches 3:aa55728c8e09 80 }
switches 3:aa55728c8e09 81 } else {
switches 3:aa55728c8e09 82 bLED = LED_ON;
switches 3:aa55728c8e09 83 if (i < UART_MAX_CMD_LENGTH) {
switches 3:aa55728c8e09 84 ibuf[i]=0;
switches 3:aa55728c8e09 85 // daplink.printf("UART CMD: %s=", ibuf);
switches 3:aa55728c8e09 86 serInt.call(ibuf, obuf);
switches 3:aa55728c8e09 87 daplink.printf("%s\r\n", obuf);
switches 3:aa55728c8e09 88 } else {
switches 3:aa55728c8e09 89 daplink.printf("[-1]\r\n");
switches 3:aa55728c8e09 90 }
switches 3:aa55728c8e09 91 i=0;
switches 3:aa55728c8e09 92 bLED = LED_OFF;
switches 3:aa55728c8e09 93 }
switches 3:aa55728c8e09 94 }
switches 3:aa55728c8e09 95 }
switches 3:aa55728c8e09 96 }
switches 3:aa55728c8e09 97
switches 0:60a522ae2e35 98 // main() runs in its own thread in the OS
switches 0:60a522ae2e35 99 // (note the calls to Thread::wait below for delays)
switches 0:60a522ae2e35 100 int main()
switches 0:60a522ae2e35 101 {
switches 2:57500e991166 102 rLED = LED_ON;
switches 2:57500e991166 103 gLED = LED_ON;
switches 2:57500e991166 104 bLED = LED_OFF;
switches 3:aa55728c8e09 105
switches 11:b9d7ab7626b5 106 /* Board Initialization
switches 11:b9d7ab7626b5 107 * This is done automatically if you specify vio
switches 11:b9d7ab7626b5 108 * when you instantiate the MAX32630FTHR library
switches 11:b9d7ab7626b5 109 */
switches 11:b9d7ab7626b5 110 // pegasus.init(MAX32630FTHR::VIO_3V3);
switches 0:60a522ae2e35 111
switches 10:a3f06fa13bda 112 // Initialize primary SPI CS
switches 8:9b85634550c7 113 gpio[0].write(1);
switches 8:9b85634550c7 114 gpio[0].output();
switches 2:57500e991166 115
switches 2:57500e991166 116 rLED = LED_OFF;
switches 5:2436ae0a9eb1 117
switches 3:aa55728c8e09 118 // Start USB serial thread
switches 3:aa55728c8e09 119 threadUSB.start(usb_thread);
switches 5:2436ae0a9eb1 120
switches 3:aa55728c8e09 121 // Start UART serial thread
switches 3:aa55728c8e09 122 threadUART.start(uart_thread);
switches 1:6923b075c8d7 123
switches 3:aa55728c8e09 124 // Start main loop
switches 1:6923b075c8d7 125 while(1) {
switches 3:aa55728c8e09 126 Thread::wait(500);
switches 3:aa55728c8e09 127 gLED = !gLED;
switches 0:60a522ae2e35 128 }
switches 0:60a522ae2e35 129 }
switches 0:60a522ae2e35 130