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 SerialInterface USBDevice
Revision 3:aa55728c8e09, committed 2016-12-08
- Comitter:
- switches
- Date:
- Thu Dec 08 05:23:14 2016 +0000
- Parent:
- 2:57500e991166
- Child:
- 4:bf2b2b0cd5d3
- Commit message:
- USB serial adapter enables communication with I2C and SPI devices over USB.
Changed in this revision
| SerialInterface.lib | Show annotated file Show diff for this revision Revisions of this file |
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SerialInterface.lib Thu Dec 08 05:23:14 2016 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/gsteiert/code/PmodInterface/#3f6a8ac111a9
--- a/main.cpp Thu Dec 08 00:06:59 2016 +0000
+++ b/main.cpp Thu Dec 08 05:23:14 2016 +0000
@@ -1,40 +1,124 @@
#include "mbed.h"
#include "max32630fthr.h"
#include "USBSerial.h"
+#include "SerialInterface.h"
+
+#define UART_MAX_RESP_LENGTH 255
+#define UART_MAX_CMD_LENGTH 255
+#define USB_MAX_RESP_LENGTH 255
+#define USB_MAX_CMD_LENGTH 255
MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
// Hardware serial port over DAPLink
Serial daplink(P2_1, P2_0);
+// Virtual serial port over USB
+USBSerial microUSB;
+
+// Serial Interface Adapter
+SerialInterface serInt;
+
+// Serial Interfaces
+I2C i2c(P3_4, P3_5);
+SPI spi(P5_1, P5_2, P5_0, P5_3);
+
+// Threads
+Thread threadUSB;
+Thread threadUART;
+
DigitalOut rLED(LED1);
DigitalOut gLED(LED2);
DigitalOut bLED(LED3);
+void usb_thread()
+{
+ char obuf[USB_MAX_RESP_LENGTH+1];
+ char ibuf[USB_MAX_CMD_LENGTH+1];
+ int i = 0;
+
+ while(1) {
+ if (microUSB.readable()) {
+ ibuf[i]=microUSB.getc();
+ if (ibuf[i]!='\r') {
+ if (i < USB_MAX_CMD_LENGTH) {
+ i += 1;
+ }
+ } else {
+ rLED = LED_ON;
+ if (i < USB_MAX_CMD_LENGTH) {
+ ibuf[i]=0;
+// microUSB.printf("UART CMD: %s=", ibuf);
+ serInt.call(ibuf, obuf);
+ microUSB.printf("%s\r\n", obuf);
+ } else {
+ microUSB.printf("[-1]\r\n");
+ }
+ i=0;
+ rLED = LED_OFF;
+ }
+ }
+ }
+}
+
+void uart_thread()
+{
+ char obuf[UART_MAX_RESP_LENGTH+1];
+ char ibuf[UART_MAX_CMD_LENGTH+1];
+ int i = 0;
+
+ while(1) {
+ if (daplink.readable()) {
+ ibuf[i]=daplink.getc();
+ if (ibuf[i]!='\r') {
+ if (i < UART_MAX_CMD_LENGTH) {
+ i += 1;
+ }
+ } else {
+ bLED = LED_ON;
+ if (i < UART_MAX_CMD_LENGTH) {
+ ibuf[i]=0;
+// daplink.printf("UART CMD: %s=", ibuf);
+ serInt.call(ibuf, obuf);
+ daplink.printf("%s\r\n", obuf);
+ } else {
+ daplink.printf("[-1]\r\n");
+ }
+ i=0;
+ bLED = LED_OFF;
+ }
+ }
+ }
+}
+
// main() runs in its own thread in the OS
// (note the calls to Thread::wait below for delays)
int main()
{
- int c;
-
daplink.printf("daplink serial port\r\n");
+ microUSB.printf("micro USB serial port\r\n");
rLED = LED_ON;
gLED = LED_ON;
bLED = LED_OFF;
+
+// Board Initialization
pegasus.init();
- // Virtual serial port over USB
- USBSerial microUSB;
- microUSB.printf("micro USB serial port\r\n");
-
+// Serial Interface Setup
+ serInt.init(&i2c, &spi);
rLED = LED_OFF;
+
+// Start USB serial thread
+ threadUSB.start(usb_thread);
+
+// Start UART serial thread
+ threadUART.start(uart_thread);
+// Start main loop
while(1) {
- c = microUSB.getc();
- microUSB.putc(c);
- daplink.putc(c);
- bLED = c & 1;
+ Thread::wait(500);
+ gLED = !gLED;
}
}