Sean Burford / i2c_pullup

Dependencies:   max32625pico maxim-dev mbed-rtos USBDevice

Fork of PICO_USB_I2C_SPI by Greg Steiert

Revision:
15:9801f08ce0ee
Parent:
14:7a4b0f9d1474
Child:
16:847788f907eb
--- a/main.cpp	Mon Nov 20 03:29:34 2017 +0000
+++ b/main.cpp	Fri Nov 24 00:10:53 2017 +0000
@@ -3,14 +3,59 @@
 
 #include "gpio_api.h"
 #include "max32625pico.h"
+#include "pwrman_regs.h"
 
 // #define MAX_GPIO_API 1
-#define MAX_I2C_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 result = E_NO_ERROR;
+    MXC_IOMAN->wud_req0 |= (0xc0 << 8);  // Port 1 Pins 6,7
+    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 &= ~(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_pulse0 = 1;
+    }
+    /* Disable configuration each time, required by hardware */
+    MXC_PWRMAN->wud_ctrl = 0;
+    MXC_IOMAN->wud_req0 = 0;
+    return result;
+}
+
 int main()
 {
     rLED = LED_ON;
@@ -20,26 +65,31 @@
     MAX32625PICO pico(
         MAX32625PICO::IOH_3V3, MAX32625PICO::VIO_IOH, MAX32625PICO::VIO_IOH);
 
-#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);
+#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
 
-    Thread::wait(15);
+    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
 
     rLED = LED_OFF;