I2C/SPI/GPIO example for MAX32625PICO board

Dependencies:   SerialInterface USBDevice max32625pico

Fork of PICO_USB_I2C_SPI by Greg Steiert

Committer:
switches
Date:
Fri Dec 16 01:00:01 2016 +0000
Revision:
11:b9d7ab7626b5
Parent:
10:a3f06fa13bda
Child:
13:fed6ff32bf5d
Updated libraries.

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