Skip to content

Commit

Permalink
added read write and init (#68)
Browse files Browse the repository at this point in the history
* added read write and init

* typedefed the function pointers

---------

Co-authored-by: Dylan Donahue <[email protected]>
  • Loading branch information
dyldonahue and Dylan Donahue authored Feb 19, 2024
1 parent 4004b01 commit 3e9ba18
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
19 changes: 19 additions & 0 deletions general/include/mcp23008.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef MCP23008_H
#define MCP23008_H

#include <stdint.h>

typedef int (*write_ptr)(uint8_t addr, const uint8_t *data, uint8_t len);
typedef int (*read_ptr)(uint8_t addr, uint8_t *data, uint8_t len);
typedef struct {
write_ptr write;
read_ptr read;
} mcp23008_t;

int mcp23008_init(mcp23008_t *obj, write_ptr write, read_ptr read);

int mcp23008_write_reg(mcp23008_t obj, uint8_t reg, uint8_t data);
int mcp23008_read_reg(mcp23008_t obj, uint8_t reg, uint8_t *data);

#endif // MCP23008_H

35 changes: 35 additions & 0 deletions general/src/mcp23008.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
MCP23008 GPIO Expander Source File
Link to part Datasheet for reference:
https://ww1.microchip.com/downloads/en/DeviceDoc/MCP23008-MCP23S08-Data-Sheet-20001919F.pdf
Author: Dylan Donahue
*/

#include "mcp23008.h"

#define MCP23008_I2C_ADDR 0x20
#define IO_CONFIG_REG 0x00
#define IO_CONFIG_OUTPUT 0x00 /* = 0000 0000, all output */
#define IO_CONFIG_INPUT 0xFF /* = 1111 1111, all input */
#define INPUT_REG 0x09

int mcp23008_init(mcp23008_t *obj, write_ptr write_func, read_ptr read_func)
{
obj->write = write_func;
obj->read = read_func;

uint8_t buf[2] = {IO_CONFIG_REG, IO_CONFIG_OUTPUT};
return obj->write(MCP23008_I2C_ADDR, buf, 2);
}

int mcp23008_write_reg(mcp23008_t *obj, uint8_t reg, uint8_t val)
{
uint8_t buf[2] = {reg, val};
return obj->write(MCP23008_I2C_ADDR, buf, 2);
}

int mcp23008_read_reg(mcp23008_t *obj, uint8_t reg, uint8_t *val)
{
return obj->read(MCP23008_I2C_ADDR, val, 1);
}

0 comments on commit 3e9ba18

Please sign in to comment.