Sean Burford / i2c_pullup

Dependencies:   max32625pico maxim-dev mbed-rtos USBDevice

Fork of PICO_USB_I2C_SPI by Greg Steiert

Revision:
16:847788f907eb
Parent:
15:9801f08ce0ee
Child:
17:43cc59f768af
--- a/main.cpp	Fri Nov 24 00:10:53 2017 +0000
+++ b/main.cpp	Fri Nov 24 05:37:12 2017 +0000
@@ -5,52 +5,27 @@
 #include "max32625pico.h"
 #include "pwrman_regs.h"
 
-// #define MAX_GPIO_API 1
-#define MAX_GPIO_DIRECT 1
-
-// #define MAX_I2C_API 1
-
 static DigitalOut rLED(LED1);
 static DigitalOut gLED(LED2);
 static DigitalOut bLED(LED3);
 
-void gpioOpenDrainWeakPullup(PinName name) {
-    const unsigned int port = PINNAME_TO_PORT(name);
-    const unsigned int pin = PINNAME_TO_PIN(name);
-    /* Set function; Firmware Control (GPIO mode) */
-    MXC_GPIO->func_sel[port] &= ~(0xF << (4 * pin));
-    /* Normal input is always enabled */
-    MXC_GPIO->in_mode[port] &= ~(0xF << (4 * pin));
-    const uint32_t new_mode = MXC_V_GPIO_OUT_MODE_OPEN_DRAIN_WEAK_PULLUP;
-    uint32_t out_mode = MXC_GPIO->out_mode[port];
-    out_mode &= ~(0xF << (pin * 4));
-    out_mode |= (new_mode << (pin * 4));
-    MXC_GPIO->out_mode[port] = out_mode;
-}
-
-int gpioAlternatePullup() {
-    // AN6350 5.2.4.1
+int gpioAlternatePullup(PinName name) {
+    // Enable high resistance pullups per AN6350 section 5.2.4.1
     int result = E_NO_ERROR;
-    MXC_IOMAN->wud_req0 |= (0xc0 << 8);  // Port 1 Pins 6,7
+    unsigned int port = PINNAME_TO_PORT(name);
+    unsigned int pin = PINNAME_TO_PIN(name);
+    MXC_IOMAN->wud_req0 |= ((1 << pin) << (port << 3));  // pinmask
     if (MXC_IOMAN->wud_ack0 != MXC_IOMAN->wud_req0) {
         result = E_BUSY;
     }
     if (result == E_NO_ERROR) {
-        /* Enable modifications to WUD configuration */
         MXC_PWRMAN->wud_ctrl = MXC_F_PWRMAN_WUD_CTRL_CTRL_ENABLE;
-        MXC_PWRMAN->wud_ctrl |= (1 * 8) + 6;  // Pad 1.6
+        MXC_PWRMAN->wud_ctrl |= (port * 8) + pin;  // Pad select
         MXC_PWRMAN->wud_ctrl &= ~(MXC_F_PWRMAN_WUD_CTRL_PAD_MODE);
-        MXC_PWRMAN->wud_ctrl |= (MXC_E_PWRMAN_PAD_MODE_WEAK_HI_LO << MXC_F_PWRMAN_WUD_CTRL_PAD_MODE_POS);
-        MXC_PWRMAN->wud_pulse0 = 1;
-
-        /* Enable modifications to WUD configuration */
-        MXC_PWRMAN->wud_ctrl = MXC_F_PWRMAN_WUD_CTRL_CTRL_ENABLE;
-        MXC_PWRMAN->wud_ctrl |= (1 * 8) + 7;  // Pad 1.7
-        MXC_PWRMAN->wud_ctrl &= ~(MXC_F_PWRMAN_WUD_CTRL_PAD_MODE);
-        MXC_PWRMAN->wud_ctrl |= (MXC_E_PWRMAN_PAD_MODE_WEAK_HI_LO << MXC_F_PWRMAN_WUD_CTRL_PAD_MODE_POS);
+        MXC_PWRMAN->wud_ctrl |= (MXC_E_PWRMAN_PAD_MODE_WEAK_HI_LO <<
+                                 MXC_F_PWRMAN_WUD_CTRL_PAD_MODE_POS);
         MXC_PWRMAN->wud_pulse0 = 1;
     }
-    /* Disable configuration each time, required by hardware */
     MXC_PWRMAN->wud_ctrl = 0;
     MXC_IOMAN->wud_req0 = 0;
     return result;
@@ -65,32 +40,11 @@
     MAX32625PICO pico(
         MAX32625PICO::IOH_3V3, MAX32625PICO::VIO_IOH, MAX32625PICO::VIO_IOH);
 
-    gpio_t gpio_p1_6;
-    gpio_t gpio_p1_7;
-    gpio_init(&gpio_p1_6, P1_6);
-    gpio_init(&gpio_p1_7, P1_7);
-#ifdef MAX_GPIO_API
-    // 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);
-#elif MAX_GPIO_DIRECT
-    // gpioOpenDrainWeakPullup(P1_6);
-    // gpioOpenDrainWeakPullup(P1_7);
-    gpioAlternatePullup();
-#else
-    DigitalInOut(P1_6, PIN_INPUT, PullUp, 1);
-    DigitalInOut(P1_7, PIN_INPUT, PullUp, 1);
-#endif
+    gpioAlternatePullup(P1_6);
+    gpioAlternatePullup(P1_7);
 
-    Thread::wait(5);
-
-#ifdef MAX_I2C_API
-    i2c_t i2c_o;
-    i2c_init(&i2c_o, P1_6, P1_7);
-#else
     I2C i2c(P1_6, P1_7);
-    i2c.frequency(10000);
-#endif
+    i2c.frequency(100000);
 
     rLED = LED_OFF;
     gLED = LED_OFF;
@@ -98,15 +52,10 @@
     char dbuf[32];
     while(1) {
         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;
         }
     }
-}
-
+}
\ No newline at end of file