I2C/SPI/GPIO example for MAX32625PICO board
Dependencies: SerialInterface USBDevice max32625pico
Fork of PICO_USB_I2C_SPI by
main.cpp@17:b4c32ceab541, 2018-08-22 (annotated)
- Committer:
- switches
- Date:
- Wed Aug 22 17:38:12 2018 +0000
- Revision:
- 17:b4c32ceab541
- Parent:
- 15:e8b28cadf100
Switched to official mbed library
Who changed what in which revision?
User | Revision | Line number | New 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 | 14:dd5a96c353ed | 4 | #include "max32625pico.h" |
switches | 3:aa55728c8e09 | 5 | #include "SerialInterface.h" |
switches | 3:aa55728c8e09 | 6 | |
switches | 15:e8b28cadf100 | 7 | #define USB_MAX_RESP_LENGTH 511 |
switches | 15:e8b28cadf100 | 8 | #define USB_MAX_CMD_LENGTH 511 |
switches | 0:60a522ae2e35 | 9 | |
switches | 14:dd5a96c353ed | 10 | // configure VDDIOH to local 3.3V supply, set dipVio and swdVio to 1.8V supply |
switches | 14:dd5a96c353ed | 11 | MAX32625PICO pico(MAX32625PICO::IOH_3V3, MAX32625PICO::VIO_1V8, MAX32625PICO::VIO_1V8); |
switches | 0:60a522ae2e35 | 12 | |
switches | 14:dd5a96c353ed | 13 | // Virtual serial port over USB |
switches | 14:dd5a96c353ed | 14 | USBSerial microUSB(0x0B6A, 0x4360); |
switches | 3:aa55728c8e09 | 15 | |
switches | 3:aa55728c8e09 | 16 | // Serial Interfaces |
switches | 13:fed6ff32bf5d | 17 | I2C i2c(P1_6, P1_7); |
switches | 14:dd5a96c353ed | 18 | SPI spi(P0_5, P0_6, P0_4); |
switches | 13:fed6ff32bf5d | 19 | DigitalInOut gpio[] = {P0_0, P0_1, P0_2, P0_3, P4_4, P4_5, P4_6, P4_7}; |
switches | 9:e59eb5f5ce32 | 20 | AnalogIn ain[] = {AIN_0, AIN_1, AIN_2, AIN_3, AIN_4, AIN_5, AIN_6, AIN_7}; |
switches | 9:e59eb5f5ce32 | 21 | |
switches | 9:e59eb5f5ce32 | 22 | // Serial Interface Adapter |
switches | 15:e8b28cadf100 | 23 | SerialInterface serInt(&i2c, &spi, gpio, ain); |
switches | 3:aa55728c8e09 | 24 | |
switches | 3:aa55728c8e09 | 25 | // Threads |
switches | 3:aa55728c8e09 | 26 | Thread threadUSB; |
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 | 15:e8b28cadf100 | 34 | char obuf[USB_MAX_RESP_LENGTH +1]; |
switches | 15:e8b28cadf100 | 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 | 15:e8b28cadf100 | 48 | bLED = 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 | 15:e8b28cadf100 | 58 | bLED = 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 | |
switches | 0:60a522ae2e35 | 65 | // main() runs in its own thread in the OS |
switches | 0:60a522ae2e35 | 66 | // (note the calls to Thread::wait below for delays) |
switches | 0:60a522ae2e35 | 67 | int main() |
switches | 0:60a522ae2e35 | 68 | { |
switches | 2:57500e991166 | 69 | rLED = LED_ON; |
switches | 2:57500e991166 | 70 | gLED = LED_ON; |
switches | 2:57500e991166 | 71 | bLED = LED_OFF; |
switches | 3:aa55728c8e09 | 72 | |
switches | 14:dd5a96c353ed | 73 | // Configure P4_4 through P4_7 for 3.3V I/O |
switches | 14:dd5a96c353ed | 74 | pico.vddioh(P4_4, MAX32625PICO::VIO_IOH); |
switches | 14:dd5a96c353ed | 75 | pico.vddioh(P4_5, MAX32625PICO::VIO_IOH); |
switches | 14:dd5a96c353ed | 76 | pico.vddioh(P4_6, MAX32625PICO::VIO_IOH); |
switches | 14:dd5a96c353ed | 77 | pico.vddioh(P4_7, MAX32625PICO::VIO_IOH); |
switches | 2:57500e991166 | 78 | |
switches | 2:57500e991166 | 79 | rLED = LED_OFF; |
switches | 5:2436ae0a9eb1 | 80 | |
switches | 3:aa55728c8e09 | 81 | // Start USB serial thread |
switches | 3:aa55728c8e09 | 82 | threadUSB.start(usb_thread); |
switches | 5:2436ae0a9eb1 | 83 | |
switches | 3:aa55728c8e09 | 84 | // Start main loop |
switches | 1:6923b075c8d7 | 85 | while(1) { |
switches | 3:aa55728c8e09 | 86 | Thread::wait(500); |
switches | 3:aa55728c8e09 | 87 | gLED = !gLED; |
switches | 0:60a522ae2e35 | 88 | } |
switches | 0:60a522ae2e35 | 89 | } |
switches | 0:60a522ae2e35 | 90 |