-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added read write and init * typedefed the function pointers --------- Co-authored-by: Dylan Donahue <[email protected]>
- Loading branch information
1 parent
4004b01
commit 3e9ba18
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |