Skip to content

Commit

Permalink
Merge pull request #352 from KarlK90/fix/rp2040-i2c-driver-number
Browse files Browse the repository at this point in the history
[RP2040] update i2c drivers to reflect peripheral number
  • Loading branch information
fpoussin authored Dec 9, 2022
2 parents e016436 + 91cfd80 commit 1130173
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
28 changes: 14 additions & 14 deletions os/hal/ports/RP/LLD/I2Cv1/hal_i2c_lld.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@
* @brief I2C1 driver identifier.
*/
#if (RP_I2C_USE_I2C0 == TRUE) || defined(__DOXYGEN__)
I2CDriver I2CD1;
I2CDriver I2CD0;
#endif

/**
* @brief I2C2 driver identifier.
*/
#if (RP_I2C_USE_I2C1 == TRUE) || defined(__DOXYGEN__)
I2CDriver I2CD2;
I2CDriver I2CD1;
#endif

/*===========================================================================*/
Expand Down Expand Up @@ -354,7 +354,7 @@ static void i2c_lld_serve_interrupt(I2CDriver *i2cp) {
OSAL_IRQ_HANDLER(RP_I2C0_IRQ_HANDLER) {
OSAL_IRQ_PROLOGUE();

i2c_lld_serve_interrupt(&I2CD1);
i2c_lld_serve_interrupt(&I2CD0);

OSAL_IRQ_EPILOGUE();
}
Expand All @@ -366,7 +366,7 @@ OSAL_IRQ_HANDLER(RP_I2C0_IRQ_HANDLER) {
OSAL_IRQ_HANDLER(RP_I2C1_IRQ_HANDLER) {
OSAL_IRQ_PROLOGUE();

i2c_lld_serve_interrupt(&I2CD2);
i2c_lld_serve_interrupt(&I2CD1);

OSAL_IRQ_EPILOGUE();
}
Expand All @@ -385,18 +385,18 @@ OSAL_IRQ_HANDLER(RP_I2C1_IRQ_HANDLER) {
void i2c_lld_init(void) {

#if RP_I2C_USE_I2C0 == TRUE
i2cObjectInit(&I2CD1);
I2CD1.i2c = I2C0;
I2CD1.thread = NULL;
i2cObjectInit(&I2CD0);
I2CD0.i2c = I2C0;
I2CD0.thread = NULL;

/* Reset I2C */
hal_lld_peripheral_reset(RESETS_ALLREG_I2C0);
#endif

#if RP_I2C_USE_I2C1 == TRUE
i2cObjectInit(&I2CD2);
I2CD2.i2c = I2C1;
I2CD2.thread = NULL;
i2cObjectInit(&I2CD1);
I2CD1.i2c = I2C1;
I2CD1.thread = NULL;

/* Reset I2C */
hal_lld_peripheral_reset(RESETS_ALLREG_I2C1);
Expand All @@ -416,15 +416,15 @@ void i2c_lld_start(I2CDriver *i2cp) {
if (i2cp->state == I2C_STOP) {

#if RP_I2C_USE_I2C0 == TRUE
if (&I2CD1 == i2cp) {
if (&I2CD0 == i2cp) {
hal_lld_peripheral_unreset(RESETS_ALLREG_I2C0);

nvicEnableVector(RP_I2C0_IRQ_NUMBER, RP_IRQ_I2C0_PRIORITY);
}
#endif

#if RP_I2C_USE_I2C1 == TRUE
if (&I2CD2 == i2cp) {
if (&I2CD1 == i2cp) {
hal_lld_peripheral_unreset(RESETS_ALLREG_I2C1);

nvicEnableVector(RP_I2C1_IRQ_NUMBER, RP_IRQ_I2C1_PRIORITY);
Expand Down Expand Up @@ -477,15 +477,15 @@ void i2c_lld_stop(I2CDriver *i2cp) {
i2c_lld_abort_transmissionS(i2cp);
}
#if RP_I2C_USE_I2C0 == TRUE
if (&I2CD1 == i2cp) {
if (&I2CD0 == i2cp) {
nvicDisableVector(RP_I2C0_IRQ_NUMBER);

hal_lld_peripheral_reset(RESETS_ALLREG_I2C0);
}
#endif

#if RP_I2C_USE_I2C1 == TRUE
if (&I2CD2 == i2cp) {
if (&I2CD1 == i2cp) {
nvicDisableVector(RP_I2C1_IRQ_NUMBER);

hal_lld_peripheral_reset(RESETS_ALLREG_I2C1);
Expand Down
4 changes: 2 additions & 2 deletions os/hal/ports/RP/LLD/I2Cv1/hal_i2c_lld.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@ struct I2CDriver {
/*===========================================================================*/

#if (RP_I2C_USE_I2C0 == TRUE) && !defined(__DOXYGEN__)
extern I2CDriver I2CD1;
extern I2CDriver I2CD0;
#endif

#if (RP_I2C_USE_I2C1 == TRUE) && !defined(__DOXYGEN__)
extern I2CDriver I2CD2;
extern I2CDriver I2CD1;
#endif

#ifdef __cplusplus
Expand Down
17 changes: 7 additions & 10 deletions testhal/RP/RP2040/RT-RP2040-PICO-I2C-24AA01/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
#define I2C0_SDA_PIN 12U
#define I2C0_SCL_PIN 13U

// This is I2C0
#define I2CN I2CD1

/*
* Green LED blinker thread, times are in milliseconds.
*/
Expand Down Expand Up @@ -72,7 +69,7 @@ int main(void) {
I2CConfig i2cConfig = {
400000, // baudrate
};
i2cStart(&I2CN, &i2cConfig);
i2cStart(&I2CD0, &i2cConfig);

/* Set up I2C pins. */
palSetLineMode(I2C0_SDA_PIN, PAL_MODE_ALTERNATE_I2C | PAL_RP_PAD_PUE | PAL_RP_PAD_DRIVE4);
Expand All @@ -96,15 +93,15 @@ int main(void) {

msg_t msg;

msg = i2cMasterTransmitTimeout(&I2CN, EEPROM_ADDRESS, (uint8_t*)&initData, sizeof(initData), NULL, 0, 1000);
msg = i2cMasterTransmitTimeout(&I2CD0, EEPROM_ADDRESS, (uint8_t *)&initData, sizeof(initData), NULL, 0, 1000);
chprintf((BaseSequentialStream *)&SIOD0, "write: %d\r\n", msg);
if (msg == MSG_OK) {

uint8_t readData[10];
memset(readData, 0xAA, 10);

// read 4 bytes
msg = i2cMasterTransmitTimeout(&I2CN, EEPROM_ADDRESS, (uint8_t*)&target_address, 1, (uint8_t*)&readData, 4, 1000);
msg = i2cMasterTransmitTimeout(&I2CD0, EEPROM_ADDRESS, (uint8_t *)&target_address, 1, (uint8_t *)&readData, 4, 1000);
chprintf((BaseSequentialStream *)&SIOD0, "write and read: %d\r\n", msg);
chprintf((BaseSequentialStream *)&SIOD0, "data: %d\r\n",
readData[0] == initData[1] &&
Expand All @@ -116,7 +113,7 @@ int main(void) {
}

// read next 4 bytes
msg = i2cMasterReceiveTimeout(&I2CN, EEPROM_ADDRESS, (uint8_t*)&readData[4], 4, 1000);
msg = i2cMasterReceiveTimeout(&I2CD0, EEPROM_ADDRESS, (uint8_t *)&readData[4], 4, 1000);
chprintf((BaseSequentialStream *)&SIOD0, "read: %d\r\n", msg);
chprintf((BaseSequentialStream *)&SIOD0, "data: %d\r\n",
readData[4] == initData[5] &&
Expand All @@ -126,9 +123,9 @@ int main(void) {
for (int i = 0; i < 10; i++) {
chprintf((BaseSequentialStream *)&SIOD0, "[%d]: 0x%X\r\n", i, readData[i]);
}
if (msg = MSG_OK) {
uint32_t error = i2cGetErrors(&I2CN);
chprintf((BaseSequentialStream *)&SIOD0, "error: %d\r\n", error);
if (msg != MSG_OK) {
uint32_t error = i2cGetErrors(&I2CD0);
chprintf((BaseSequentialStream *)&SIOD0, "error: %d\r\n", error);
}
}

Expand Down

0 comments on commit 1130173

Please sign in to comment.