Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8093

General • Problems with i2c

$
0
0
Hi all,

OK setup is this I have a Pico 1 mounted on a board I'm successfully using some of the I/O to talk to both a PS/2 keyboard (via a level shifter), and a USB keyboard via TP2 & TP3, and then outputting to an MT8816 crosspoint switch. This is all working correctly.

I'm now trying to use gpio 2 and 3 to talk to an MCP23017, via a 3.3V->5V level shifter. The level shifter is the type made with 2 pullup resistors and a FET. The pullups are 10K.

However when I try and send data to the I2C, I get nothing on the pins (measured with scope) , even doing a bus scan (code & defines below).

Defines :

Code:

// IIC channel, and address of EXIO#define EXIO_I2C          i2c1#define EXIO_ADDRESS      0x20#define EXIO_SDA          2#define EXIO_SCL          3#define EXIO_SPEED        100
In the main file I call the scan functions with :

Code:

i2c_init_bus_scan(EXIO_I2C,EXIO_SDA,EXIO_SCL,EXIO_SPEED);
The bus scan is basically the same as the Pi example, just broken out into a couple of functions so I can call it from my own code....

Code:

bool reserved_addr(uint8_t addr) {    return (addr & 0x78) == 0 || (addr & 0x78) == 0x78;}void i2c_init_bus_scan(i2c_inst_t    *i2c,                       uint8_t       sda_pin,                       uint8_t       scl_pin,                       uint32_t      speed_khz){#if (DEBUG_PINS == 1)    printf("SDA gpio: %d, SCL gpio: %d, Speed %ldKHz\n",sda_pin,scl_pin,speed_khz);#endif    // Initialise IIC    i2c_init(i2c, speed_khz * 1000);    gpio_init(sda_pin);    gpio_init(scl_pin);    // Set function of pins to IIC, and enable pullups.    gpio_set_function(sda_pin, GPIO_FUNC_I2C);    gpio_set_function(scl_pin, GPIO_FUNC_I2C);    i2c_bus_scan(i2c);}void i2c_bus_scan(i2c_inst_t    *i2c){    int     ret;    uint8_t rxdata;    printf("\nI2C Bus Scan\n");    printf("   0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F\n");    for (int addr = 0; addr < (1 << 7); ++addr)     {        if (addr % 16 == 0)         {            printf("%02x ", addr);        }        // Perform a 1-byte dummy read from the probe address. If a slave        // acknowledges this address, the function returns the number of bytes        // transferred. If the address byte is ignored, the function returns        // -1.        // Skip over any reserved addresses.        if (reserved_addr(addr))            ret = PICO_ERROR_GENERIC;        else            ret = i2c_read_blocking(i2c, addr, &rxdata, 1, false);        printf(ret < 0 ? "." : "@");        printf(addr % 16 == 15 ? "\n" : "  ");    }    printf("Done.\n");}
But like I say no activity on gpio 2 or 3, the pins just stay high. The code just prints out a load of . as if there is nothing found on the bus.

Any clues as to what I'm doing wrong?

Cheers.

Phill.

Statistics: Posted by PhillHS — Thu Jul 10, 2025 3:06 pm



Viewing all articles
Browse latest Browse all 8093

Trending Articles