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
Diff: main.cpp
- Revision:
- 14:7a4b0f9d1474
- Parent:
- 13:fed6ff32bf5d
- Child:
- 15:9801f08ce0ee
diff -r fed6ff32bf5d -r 7a4b0f9d1474 main.cpp
--- a/main.cpp Thu Apr 13 15:03:15 2017 +0000
+++ b/main.cpp Mon Nov 20 03:29:34 2017 +0000
@@ -1,130 +1,62 @@
#include "mbed.h"
#include "rtos.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
-
-// Virtual serial port over USB
-USBSerial microUSB;
-
-// Hardware serial port over DAPLink
-Serial daplink(P2_1, P2_0);
-
-// Serial Interfaces
-I2C i2c(P1_6, P1_7);
-SPI spi(P1_5, P1_6, P1_4);
-DigitalInOut gpio[] = {P0_0, P0_1, P0_2, P0_3, P4_4, P4_5, P4_6, P4_7};
-AnalogIn ain[] = {AIN_0, AIN_1, AIN_2, AIN_3, AIN_4, AIN_5, AIN_6, AIN_7};
-
-// Serial Interface Adapter
-SerialInterface serInt(i2c, spi, gpio, ain);
-
-// 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;
-
- microUSB.printf("micro USB serial port\r\n");
+#include "gpio_api.h"
+#include "max32625pico.h"
- 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;
- }
- }
- }
-}
+// #define MAX_GPIO_API 1
+#define MAX_I2C_API 1
-void uart_thread()
-{
- char obuf[UART_MAX_RESP_LENGTH+1];
- char ibuf[UART_MAX_CMD_LENGTH+1];
- int i = 0;
-
- daplink.printf("daplink serial port\r\n");
+static DigitalOut rLED(LED1);
+static DigitalOut gLED(LED2);
+static DigitalOut bLED(LED3);
- 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()
{
rLED = LED_ON;
gLED = LED_ON;
bLED = LED_OFF;
-/* Board Initialization
- * This is done automatically if you specify vio
- * when you instantiate the MAX32630FTHR library
- */
-// pegasus.init(MAX32630FTHR::VIO_3V3);
+ MAX32625PICO pico(
+ MAX32625PICO::IOH_3V3, MAX32625PICO::VIO_IOH, MAX32625PICO::VIO_IOH);
-// Initialize primary SPI CS
- gpio[0].write(1);
- gpio[0].output();
+#ifdef MAX_GPIO_API
+ gpio_t gpio_p1_6;
+ gpio_t gpio_p1_7;
+ gpio_init(&gpio_p1_6, P1_6);
+ gpio_init(&gpio_p1_7, P1_7);
+ // gpio_api.c:104 sets the pin to MXC_V_GPIO_OUT_MODE_HIGH_Z_WEAK_PULLUP
+ pin_dir_mode(P1_6, PIN_INPUT, PullUp);
+ pin_dir_mode(P1_7, PIN_INPUT, PullUp);
+#else
+ DigitalInOut(P1_6, PIN_INPUT, PullUp, 1);
+ DigitalInOut(P1_7, PIN_INPUT, PullUp, 1);
+#endif
+
+ Thread::wait(15);
+
+#ifdef MAX_I2C_API
+ i2c_t i2c_o;
+ i2c_init(&i2c_o, P1_6, P1_7);
+#else
+ I2C i2c(P1_6, P1_7);
+#endif
rLED = LED_OFF;
-
-// Start USB serial thread
- threadUSB.start(usb_thread);
+ gLED = LED_OFF;
-// Start UART serial thread
- threadUART.start(uart_thread);
-
-// Start main loop
+ char dbuf[32];
while(1) {
- Thread::wait(500);
- gLED = !gLED;
+ Thread::wait(15);
+#ifdef MAX_I2C_API
+ if (i2c_read(&i2c_o, 0x55, dbuf, 8, 1) > 0) {
+#else
+ if (i2c.read(0x55, dbuf, 8) == 0) {
+#endif
+ gLED = !gLED;
+ } else {
+ rLED = !rLED;
+ }
}
}
