I2C/SPI/GPIO example for MAX32625PICO board

Dependencies:   SerialInterface USBDevice max32625pico

Fork of PICO_USB_I2C_SPI by Greg Steiert

Committer:
switches
Date:
Tue Dec 13 21:30:06 2016 +0000
Revision:
7:5de026dd3a82
Parent:
6:bff339370df6
Child:
8:9b85634550c7
Updated pegasus_dev library and max32630fthr library.

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 7:5de026dd3a82 11 MAX32630FTHR pegasus;
switches 7:5de026dd3a82 12
switches 7:5de026dd3a82 13 // Virtual serial port over USB
switches 7:5de026dd3a82 14 USBSerial microUSB;
switches 0:60a522ae2e35 15
switches 3:aa55728c8e09 16 // Serial Interface Adapter
switches 3:aa55728c8e09 17 SerialInterface serInt;
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 6:bff339370df6 22 DigitalOut ssel(P5_3);
switches 3:aa55728c8e09 23
switches 3:aa55728c8e09 24 // Threads
switches 3:aa55728c8e09 25 Thread threadUSB;
switches 3:aa55728c8e09 26 Thread threadUART;
switches 3:aa55728c8e09 27
switches 2:57500e991166 28 DigitalOut rLED(LED1);
switches 2:57500e991166 29 DigitalOut gLED(LED2);
switches 2:57500e991166 30 DigitalOut bLED(LED3);
switches 0:60a522ae2e35 31
switches 3:aa55728c8e09 32 void usb_thread()
switches 3:aa55728c8e09 33 {
switches 3:aa55728c8e09 34 char obuf[USB_MAX_RESP_LENGTH+1];
switches 3:aa55728c8e09 35 char ibuf[USB_MAX_CMD_LENGTH+1];
switches 3:aa55728c8e09 36 int i = 0;
switches 3:aa55728c8e09 37
switches 5:2436ae0a9eb1 38 microUSB.printf("micro USB serial port\r\n");
switches 5:2436ae0a9eb1 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 5:2436ae0a9eb1 66 // Hardware serial port over DAPLink
switches 5:2436ae0a9eb1 67 Serial daplink(P2_1, P2_0);
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 3:aa55728c8e09 106 // Board Initialization
switches 7:5de026dd3a82 107 pegasus.init(MAX32630FTHR::VIO_3V3);
switches 0:60a522ae2e35 108
switches 3:aa55728c8e09 109 // Serial Interface Setup
switches 6:bff339370df6 110 serInt.init(&i2c, &spi, &ssel);
switches 2:57500e991166 111
switches 2:57500e991166 112 rLED = LED_OFF;
switches 5:2436ae0a9eb1 113
switches 3:aa55728c8e09 114 // Start USB serial thread
switches 3:aa55728c8e09 115 threadUSB.start(usb_thread);
switches 5:2436ae0a9eb1 116
switches 3:aa55728c8e09 117 // Start UART serial thread
switches 3:aa55728c8e09 118 threadUART.start(uart_thread);
switches 1:6923b075c8d7 119
switches 3:aa55728c8e09 120 // Start main loop
switches 1:6923b075c8d7 121 while(1) {
switches 3:aa55728c8e09 122 Thread::wait(500);
switches 3:aa55728c8e09 123 gLED = !gLED;
switches 0:60a522ae2e35 124 }
switches 0:60a522ae2e35 125 }
switches 0:60a522ae2e35 126