Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: max32625pico maxim-dev mbed-rtos USBDevice
Fork of PICO_USB_I2C_SPI by
main.cpp@5:2436ae0a9eb1, 2016-12-08 (annotated)
- Committer:
- switches
- Date:
- Thu Dec 08 21:10:46 2016 +0000
- Revision:
- 5:2436ae0a9eb1
- Parent:
- 3:aa55728c8e09
- Child:
- 6:bff339370df6
Fixed power sequencing issue. I2C works, SPI only does single byte transactions.
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:aa55728c8e09 | 13 | // Serial Interface Adapter |
switches | 3:aa55728c8e09 | 14 | SerialInterface serInt; |
switches | 3:aa55728c8e09 | 15 | |
switches | 3:aa55728c8e09 | 16 | // Serial Interfaces |
switches | 3:aa55728c8e09 | 17 | I2C i2c(P3_4, P3_5); |
switches | 3:aa55728c8e09 | 18 | SPI spi(P5_1, P5_2, P5_0, P5_3); |
switches | 3:aa55728c8e09 | 19 | |
switches | 3:aa55728c8e09 | 20 | // Threads |
switches | 3:aa55728c8e09 | 21 | Thread threadUSB; |
switches | 3:aa55728c8e09 | 22 | Thread threadUART; |
switches | 3:aa55728c8e09 | 23 | |
switches | 2:57500e991166 | 24 | DigitalOut rLED(LED1); |
switches | 2:57500e991166 | 25 | DigitalOut gLED(LED2); |
switches | 2:57500e991166 | 26 | DigitalOut bLED(LED3); |
switches | 0:60a522ae2e35 | 27 | |
switches | 3:aa55728c8e09 | 28 | void usb_thread() |
switches | 3:aa55728c8e09 | 29 | { |
switches | 5:2436ae0a9eb1 | 30 | // Virtual serial port over USB |
switches | 5:2436ae0a9eb1 | 31 | USBSerial microUSB; |
switches | 3:aa55728c8e09 | 32 | char obuf[USB_MAX_RESP_LENGTH+1]; |
switches | 3:aa55728c8e09 | 33 | char ibuf[USB_MAX_CMD_LENGTH+1]; |
switches | 3:aa55728c8e09 | 34 | int i = 0; |
switches | 3:aa55728c8e09 | 35 | |
switches | 5:2436ae0a9eb1 | 36 | microUSB.printf("micro USB serial port\r\n"); |
switches | 5:2436ae0a9eb1 | 37 | |
switches | 3:aa55728c8e09 | 38 | while(1) { |
switches | 3:aa55728c8e09 | 39 | if (microUSB.readable()) { |
switches | 3:aa55728c8e09 | 40 | ibuf[i]=microUSB.getc(); |
switches | 3:aa55728c8e09 | 41 | if (ibuf[i]!='\r') { |
switches | 3:aa55728c8e09 | 42 | if (i < USB_MAX_CMD_LENGTH) { |
switches | 3:aa55728c8e09 | 43 | i += 1; |
switches | 3:aa55728c8e09 | 44 | } |
switches | 3:aa55728c8e09 | 45 | } else { |
switches | 3:aa55728c8e09 | 46 | rLED = LED_ON; |
switches | 3:aa55728c8e09 | 47 | if (i < USB_MAX_CMD_LENGTH) { |
switches | 3:aa55728c8e09 | 48 | ibuf[i]=0; |
switches | 3:aa55728c8e09 | 49 | // microUSB.printf("UART CMD: %s=", ibuf); |
switches | 3:aa55728c8e09 | 50 | serInt.call(ibuf, obuf); |
switches | 3:aa55728c8e09 | 51 | microUSB.printf("%s\r\n", obuf); |
switches | 3:aa55728c8e09 | 52 | } else { |
switches | 3:aa55728c8e09 | 53 | microUSB.printf("[-1]\r\n"); |
switches | 3:aa55728c8e09 | 54 | } |
switches | 3:aa55728c8e09 | 55 | i=0; |
switches | 3:aa55728c8e09 | 56 | rLED = LED_OFF; |
switches | 3:aa55728c8e09 | 57 | } |
switches | 3:aa55728c8e09 | 58 | } |
switches | 3:aa55728c8e09 | 59 | } |
switches | 3:aa55728c8e09 | 60 | } |
switches | 3:aa55728c8e09 | 61 | |
switches | 3:aa55728c8e09 | 62 | void uart_thread() |
switches | 3:aa55728c8e09 | 63 | { |
switches | 5:2436ae0a9eb1 | 64 | // Hardware serial port over DAPLink |
switches | 5:2436ae0a9eb1 | 65 | Serial daplink(P2_1, P2_0); |
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 | 5:2436ae0a9eb1 | 70 | daplink.printf("daplink serial port\r\n"); |
switches | 5:2436ae0a9eb1 | 71 | |
switches | 3:aa55728c8e09 | 72 | while(1) { |
switches | 3:aa55728c8e09 | 73 | if (daplink.readable()) { |
switches | 3:aa55728c8e09 | 74 | ibuf[i]=daplink.getc(); |
switches | 3:aa55728c8e09 | 75 | if (ibuf[i]!='\r') { |
switches | 3:aa55728c8e09 | 76 | if (i < UART_MAX_CMD_LENGTH) { |
switches | 3:aa55728c8e09 | 77 | i += 1; |
switches | 3:aa55728c8e09 | 78 | } |
switches | 3:aa55728c8e09 | 79 | } else { |
switches | 3:aa55728c8e09 | 80 | bLED = LED_ON; |
switches | 3:aa55728c8e09 | 81 | if (i < UART_MAX_CMD_LENGTH) { |
switches | 3:aa55728c8e09 | 82 | ibuf[i]=0; |
switches | 3:aa55728c8e09 | 83 | // daplink.printf("UART CMD: %s=", ibuf); |
switches | 3:aa55728c8e09 | 84 | serInt.call(ibuf, obuf); |
switches | 3:aa55728c8e09 | 85 | daplink.printf("%s\r\n", obuf); |
switches | 3:aa55728c8e09 | 86 | } else { |
switches | 3:aa55728c8e09 | 87 | daplink.printf("[-1]\r\n"); |
switches | 3:aa55728c8e09 | 88 | } |
switches | 3:aa55728c8e09 | 89 | i=0; |
switches | 3:aa55728c8e09 | 90 | bLED = LED_OFF; |
switches | 3:aa55728c8e09 | 91 | } |
switches | 3:aa55728c8e09 | 92 | } |
switches | 3:aa55728c8e09 | 93 | } |
switches | 3:aa55728c8e09 | 94 | } |
switches | 3:aa55728c8e09 | 95 | |
switches | 0:60a522ae2e35 | 96 | // main() runs in its own thread in the OS |
switches | 0:60a522ae2e35 | 97 | // (note the calls to Thread::wait below for delays) |
switches | 0:60a522ae2e35 | 98 | int main() |
switches | 0:60a522ae2e35 | 99 | { |
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 | 5:2436ae0a9eb1 | 111 | |
switches | 3:aa55728c8e09 | 112 | // Start USB serial thread |
switches | 3:aa55728c8e09 | 113 | threadUSB.start(usb_thread); |
switches | 5:2436ae0a9eb1 | 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 |