Hi, I have a board with an MCP23017 GPIO expander. It’s address is set to 0x27 and there are some things on bank A. Pins 0, 1 and 2 have LEDs and I am trying to get them to blink. I am able to make this work with the Arduino wire library so I know the hardware is good. Also, with the mongoose os app below I can see data on the I2C bus with my logic analyzer. On my board SDA is 4 and SCL is 5.
I have written the code I’ll paste below using the mongoose mgos_mcp23xxx library but I can’t get the LEDs to come on. I have tried many variations with no luck. It does say it successfully create the expander struct.
My expectation is, of course, that I learn to control the LEDs and they come on and blink. There are also other things on this expander like buttons and such I ultimately need to interface with.
I have this in my mos.yml file but as I mentioned, the logic analyzer shows data on the I2C bus.
# Enable I2C
- ["i2c.enable", "b", true, {title: "Enable I2C"}]
# - ["i2c.freq", "i", 100000, {title: "I2C bus speed"}]
# - ["i2c.unit_no", "i", 0, {title: "Which hardware unit ot use, 0 or 1"}]
- ["i2c.sda_gpio", "i", 4, {title: "GPIO to use for SDA"}]
- ["i2c.scl_gpio", "i", 5, {title: "GPIO to use for SCL"}]
Here is the code. If someone could let me know what I did wrong I would appreciate it thank you!
#include "mgos.h"
#include "mgos_config.h"
#include "mgos_mcp23xxx.h"
/* Scheduler includes. */
#include "FreeRTOS.h"
#include "task.h"
// Global for now
struct mgos_mcp23xxx *d;
static void vLEDTask(void* pvParameters)
{
LOG(LL_ERROR, ("========>>> Toggle..."));
mgos_mcp23xxx_gpio_toggle(d, 0);
mgos_mcp23xxx_gpio_toggle(d, 1);
mgos_mcp23xxx_gpio_toggle(d, 2);
}
enum mgos_app_init_result mgos_app_init(void)
{
// I2C ADDR 0x27
// EXP_INT IO0
#define ADDRESS 0x27
if (!(d = mgos_mcp23017_create(mgos_i2c_get_global(), ADDRESS, 0) ) )
{
LOG(LL_ERROR, ("************>>> Could not create MCP230XX"));
return MGOS_APP_INIT_ERROR;
}
else
LOG(LL_ERROR, ("========>>> created MCP230XX"));
mgos_mcp23xxx_gpio_set_mode(d, 0, MGOS_GPIO_MODE_OUTPUT); // LED A
mgos_mcp23xxx_gpio_set_mode(d, 1, MGOS_GPIO_MODE_OUTPUT); // LED G
mgos_mcp23xxx_gpio_set_mode(d, 2, MGOS_GPIO_MODE_OUTPUT); // LED B
// Fails with and without the next 3 lines
// mgos_mcp23xxx_gpio_write(d, 0, 1);
// mgos_mcp23xxx_gpio_write(d, 1, 0);
// mgos_mcp23xxx_gpio_write(d, 2, 1);
mgos_set_timer(1000, MGOS_TIMER_REPEAT, vLEDTask, NULL);
return MGOS_APP_INIT_SUCCESS;
}