November 22, 2024, 11:28:12 PM

MOD-IO2: GPIO3

Started by markus2330, June 15, 2022, 06:12:55 PM

Previous topic - Next topic

markus2330

Dear Olimex users,

according to the documentation¹ GPIO3 is always pulled up. When I use it as switch, however, it is not always high when I leave the switch open. Instead GET\_PORT = 0x03 toggles between up and down or stays down.

GPIO1, GPIO2 and GPIO4 do not have the problem, and stay up when the switch is open. (Obviously they need the SET\_PU = 0x04, which is specified to be x = don't care for GPIO3).

I can reproduce this with two different boards, with own code and with modio2tool.

Any hints for me?

best regards,
Markus

¹ The documentation says: "Note that only GPIO0 to GPIO4 have pullups, and GPIO3 is with pullup always enabled"

LubOlimex

Can you give me all and exact commands that you use regarding GPIO3?
Technical support and documentation manager at Olimex

markus2330

Thank you for your reply!

Given an open GPIO3, I enter:

olimex:~$./modio2tool -T 0x7F
olimex:~$ ./modio2tool -i 3
GPIO3: 0
olimex:~$ ./modio2tool -i 3
GPIO3: 1

Following works (Rust Code):
// BSD licensed

use i2cdev::core::*;
use i2cdev::linux::LinuxI2CDevice;

use std::{thread, time};

const SET_TRIS: u8 = 0x01;
const GET_PORTS: u8 = 0x03;
const SET_PULLUPS: u8 = 0x04;

const BUTTON_3: u8 = 0x04;

fn main() {
        const BOARD: u16 = 0x20;
        let mut dev = LinuxI2CDevice::new("/dev/i2c-2", BOARD).unwrap();
        dev.smbus_write_byte_data(SET_TRIS, BUTTON_3).unwrap();  // input
        dev.smbus_write_byte_data(SET_PULLUPS, BUTTON_3).unwrap();  // input
        let mut opins : u8 = 0;
        let mut pins : u8;
        loop {
                pins = dev.smbus_read_byte_data(GET_PORTS).unwrap();
                if pins != opins {
                        println!("Pin changed from {} to {}", opins, pins);
                        opins = pins;
                }
                thread::sleep(time::Duration::from_millis(10));
        }
}


But it stops working when I use all 4 ports (GPIO0-GPIO3) as input. Then the button 3 toggles between up/down while open. It works when only GPIO0-GPIO2 are used.


best regards,
Markus

markus2330

Dear Olimex users,

for me it would be actually also okay to add an external pull-up resistor. Would this make sense? How many Ohms? (Is it then parallel to some internal pull-up?)

best regards,
Markus

markus2330

Even with a 1MΩ pull-up the problem on GPIO3 is gone (in a few short tests).

LubOlimex

Hmm, I don't know why GPIO3 is handled that way in the firmware. It is no different than RA9, RA1 and RA2. Same for pull ups - it is the same for all GPIOs. There is no reason for what the documentation says. Or I fail to see why GPIO3 is handled differently I dunno.


I believe someone didn't look carefully at the schematic or some copy-paste error. Will investigate the firmware.
Technical support and documentation manager at Olimex

markus2330

Thank you very much for investigating!

The 1MΩ pull-up was too weak for on one of the boards, I am using 47kΩ now.