-
Notifications
You must be signed in to change notification settings - Fork 497
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from pimoroni/patch-gpio-int-types
Add a common header for pins and settings + common I2C class for managing I2C busses across drivers
- Loading branch information
Showing
121 changed files
with
1,195 additions
and
1,088 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
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,9 @@ | ||
add_library(pimoroni_i2c INTERFACE) | ||
|
||
target_sources(pimoroni_i2c INTERFACE | ||
${CMAKE_CURRENT_LIST_DIR}/pimoroni_i2c.cpp) | ||
|
||
target_include_directories(pimoroni_i2c INTERFACE ${CMAKE_CURRENT_LIST_DIR}) | ||
|
||
# Pull in pico libraries that we need | ||
target_link_libraries(pimoroni_i2c INTERFACE pico_stdlib) |
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,41 @@ | ||
#pragma once | ||
#include <stdint.h> | ||
#include <climits> | ||
|
||
#define PIMORONI_I2C_DEFAULT_INSTANCE i2c0 | ||
#define PIMORONI_SPI_DEFAULT_INSTANCE spi0 | ||
|
||
namespace pimoroni { | ||
static const unsigned int PIN_UNUSED = INT_MAX; // Intentionally INT_MAX to avoid overflowing MicroPython's int type | ||
|
||
// I2C | ||
static const unsigned int I2C_DEFAULT_BAUDRATE = 400000; | ||
static const unsigned int I2C_DEFAULT_SDA = 20; | ||
static const unsigned int I2C_DEFAULT_SCL = 21; | ||
static const unsigned int I2C_DEFAULT_INT = 22; | ||
|
||
static const unsigned int I2C_BG_SDA = 4; | ||
static const unsigned int I2C_BG_SCL = 5; | ||
static const unsigned int I2C_BG_INT = 3; | ||
|
||
// SPI | ||
static const unsigned int SPI_DEFAULT_MOSI = 19; | ||
static const unsigned int SPI_DEFAULT_MISO = 16; | ||
static const unsigned int SPI_DEFAULT_SCK = 18; | ||
|
||
static const unsigned int SPI_BG_FRONT_PWM = 20; | ||
static const unsigned int SPI_BG_FRONT_CS = 17; | ||
|
||
static const unsigned int SPI_BG_BACK_PWM = 21; | ||
static const unsigned int SPI_BG_BACK_CS = 22; | ||
|
||
enum BG_SPI_SLOT { | ||
BG_SPI_FRONT, | ||
BG_SPI_BACK | ||
}; | ||
|
||
enum BOARD { | ||
BREAKOUT_GARDEN, | ||
PICO_EXPLORER | ||
}; | ||
} |
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,91 @@ | ||
#include "pimoroni_common.hpp" | ||
#include "pimoroni_i2c.hpp" | ||
|
||
namespace pimoroni { | ||
void I2C::init() { | ||
i2c = ((sda / 2) & 0b1) ? i2c1 : i2c0; | ||
|
||
i2c_init(i2c, baudrate); | ||
|
||
gpio_set_function(sda, GPIO_FUNC_I2C); gpio_pull_up(sda); | ||
gpio_set_function(scl, GPIO_FUNC_I2C); gpio_pull_up(scl); | ||
} | ||
|
||
/* Basic wrappers for devices using i2c functions directly */ | ||
int I2C::write_blocking(uint8_t addr, const uint8_t *src, size_t len, bool nostop) { | ||
return i2c_write_blocking(i2c, addr, src, len, nostop); | ||
} | ||
|
||
int I2C::read_blocking(uint8_t addr, uint8_t *dst, size_t len, bool nostop) { | ||
return i2c_read_blocking(i2c, addr, dst, len, nostop); | ||
} | ||
|
||
/* Convenience functions for various common i2c operations */ | ||
void I2C::reg_write_uint8(uint8_t address, uint8_t reg, uint8_t value) { | ||
uint8_t buffer[2] = {reg, value}; | ||
i2c_write_blocking(i2c, address, buffer, 2, false); | ||
} | ||
|
||
uint8_t I2C::reg_read_uint8(uint8_t address, uint8_t reg) { | ||
uint8_t value; | ||
i2c_write_blocking(i2c, address, ®, 1, false); | ||
i2c_read_blocking(i2c, address, (uint8_t *)&value, sizeof(uint8_t), false); | ||
return value; | ||
} | ||
|
||
uint16_t I2C::reg_read_uint16(uint8_t address, uint8_t reg) { | ||
uint16_t value; | ||
i2c_write_blocking(i2c, address, ®, 1, true); | ||
i2c_read_blocking(i2c, address, (uint8_t *)&value, sizeof(uint16_t), false); | ||
return value; | ||
} | ||
|
||
uint32_t I2C::reg_read_uint32(uint8_t address, uint8_t reg) { | ||
uint32_t value; | ||
i2c_write_blocking(i2c, address, ®, 1, true); | ||
i2c_read_blocking(i2c, address, (uint8_t *)&value, sizeof(uint32_t), false); | ||
return value; | ||
} | ||
|
||
int16_t I2C::reg_read_int16(uint8_t address, uint8_t reg) { | ||
int16_t value; | ||
i2c_write_blocking(i2c, address, ®, 1, true); | ||
i2c_read_blocking(i2c, address, (uint8_t *)&value, sizeof(int16_t), false); | ||
return value; | ||
} | ||
|
||
int I2C::write_bytes(uint8_t address, uint8_t reg, uint8_t *buf, int len) { | ||
uint8_t buffer[len + 1]; | ||
buffer[0] = reg; | ||
for(int x = 0; x < len; x++) { | ||
buffer[x + 1] = buf[x]; | ||
} | ||
return i2c_write_blocking(i2c, address, buffer, len + 1, false); | ||
}; | ||
|
||
int I2C::read_bytes(uint8_t address, uint8_t reg, uint8_t *buf, int len) { | ||
i2c_write_blocking(i2c, address, ®, 1, true); | ||
i2c_read_blocking(i2c, address, buf, len, false); | ||
return len; | ||
}; | ||
|
||
uint8_t I2C::get_bits(uint8_t address, uint8_t reg, uint8_t shift, uint8_t mask) { | ||
uint8_t value; | ||
read_bytes(address, reg, &value, 1); | ||
return value & (mask << shift); | ||
} | ||
|
||
void I2C::set_bits(uint8_t address, uint8_t reg, uint8_t shift, uint8_t mask) { | ||
uint8_t value; | ||
read_bytes(address, reg, &value, 1); | ||
value |= mask << shift; | ||
write_bytes(address, reg, &value, 1); | ||
} | ||
|
||
void I2C::clear_bits(uint8_t address, uint8_t reg, uint8_t shift, uint8_t mask) { | ||
uint8_t value; | ||
read_bytes(address, reg, &value, 1); | ||
value &= ~(mask << shift); | ||
write_bytes(address, reg, &value, 1); | ||
} | ||
} |
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,72 @@ | ||
#pragma once | ||
#include <stdint.h> | ||
#include <climits> | ||
#include "hardware/i2c.h" | ||
#include "hardware/gpio.h" | ||
#include "pimoroni_common.hpp" | ||
#include "pimoroni_i2c.hpp" | ||
|
||
namespace pimoroni { | ||
class I2C { | ||
private: | ||
i2c_inst_t *i2c = PIMORONI_I2C_DEFAULT_INSTANCE; | ||
uint sda = I2C_DEFAULT_SDA; | ||
uint scl = I2C_DEFAULT_SCL; | ||
uint interrupt = PIN_UNUSED; | ||
uint32_t baudrate = I2C_DEFAULT_BAUDRATE; | ||
|
||
public: | ||
I2C(BOARD board, uint32_t baudrate = I2C_DEFAULT_BAUDRATE) : baudrate(baudrate) { | ||
switch(board) { | ||
case BREAKOUT_GARDEN: | ||
sda = I2C_BG_SDA; | ||
scl = I2C_BG_SCL; | ||
interrupt = I2C_BG_INT; | ||
break; | ||
case PICO_EXPLORER: | ||
default: | ||
sda = I2C_DEFAULT_SDA; | ||
scl = I2C_DEFAULT_SCL; | ||
interrupt = I2C_DEFAULT_INT; | ||
break; | ||
} | ||
init(); | ||
} | ||
|
||
I2C(uint sda, uint scl, uint32_t baudrate = I2C_DEFAULT_BAUDRATE) : sda(sda), scl(scl), baudrate(baudrate) { | ||
init(); | ||
} | ||
|
||
I2C() : I2C(I2C_DEFAULT_SDA, I2C_DEFAULT_SCL) {} | ||
|
||
~I2C() { | ||
i2c_deinit(i2c); | ||
gpio_disable_pulls(sda); | ||
gpio_set_function(sda, GPIO_FUNC_NULL); | ||
gpio_disable_pulls(scl); | ||
gpio_set_function(scl, GPIO_FUNC_NULL); | ||
} | ||
|
||
void reg_write_uint8(uint8_t address, uint8_t reg, uint8_t value); | ||
uint8_t reg_read_uint8(uint8_t address, uint8_t reg); | ||
uint16_t reg_read_uint16(uint8_t address, uint8_t reg); | ||
int16_t reg_read_int16(uint8_t address, uint8_t reg); | ||
uint32_t reg_read_uint32(uint8_t address, uint8_t reg); | ||
|
||
int write_bytes(uint8_t address, uint8_t reg, uint8_t *buf, int len); | ||
int read_bytes(uint8_t address, uint8_t reg, uint8_t *buf, int len); | ||
uint8_t get_bits(uint8_t address, uint8_t reg, uint8_t shift, uint8_t mask=0b1); | ||
void set_bits(uint8_t address, uint8_t reg, uint8_t shift, uint8_t mask=0b1); | ||
void clear_bits(uint8_t address, uint8_t reg, uint8_t shift, uint8_t mask=0b1); | ||
|
||
int write_blocking(uint8_t addr, const uint8_t *src, size_t len, bool nostop); | ||
int read_blocking(uint8_t addr, uint8_t *dst, size_t len, bool nostop); | ||
|
||
i2c_inst_t* get_i2c() {return i2c;} | ||
uint get_scl() {return scl;} | ||
uint get_sda() {return sda;} | ||
uint32_t get_baudrate() {return baudrate;} | ||
private: | ||
void init(); | ||
}; | ||
} |
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
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
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
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
Oops, something went wrong.