diff options
Diffstat (limited to 'platforms/chibios/drivers/i2c_master.c')
| -rw-r--r-- | platforms/chibios/drivers/i2c_master.c | 37 |
1 files changed, 30 insertions, 7 deletions
diff --git a/platforms/chibios/drivers/i2c_master.c b/platforms/chibios/drivers/i2c_master.c index fc4bb2ab3..63e85ae87 100644 --- a/platforms/chibios/drivers/i2c_master.c +++ b/platforms/chibios/drivers/i2c_master.c | |||
| @@ -63,16 +63,16 @@ __attribute__((weak)) void i2c_init(void) { | |||
| 63 | is_initialised = true; | 63 | is_initialised = true; |
| 64 | 64 | ||
| 65 | // Try releasing special pins for a short time | 65 | // Try releasing special pins for a short time |
| 66 | palSetPadMode(I2C1_SCL_BANK, I2C1_SCL, PAL_MODE_INPUT); | 66 | palSetLineMode(I2C1_SCL_PIN, PAL_MODE_INPUT); |
| 67 | palSetPadMode(I2C1_SDA_BANK, I2C1_SDA, PAL_MODE_INPUT); | 67 | palSetLineMode(I2C1_SDA_PIN, PAL_MODE_INPUT); |
| 68 | 68 | ||
| 69 | chThdSleepMilliseconds(10); | 69 | chThdSleepMilliseconds(10); |
| 70 | #if defined(USE_GPIOV1) | 70 | #if defined(USE_GPIOV1) |
| 71 | palSetPadMode(I2C1_SCL_BANK, I2C1_SCL, I2C1_SCL_PAL_MODE); | 71 | palSetLineMode(I2C1_SCL_PIN, I2C1_SCL_PAL_MODE); |
| 72 | palSetPadMode(I2C1_SDA_BANK, I2C1_SDA, I2C1_SDA_PAL_MODE); | 72 | palSetLineMode(I2C1_SDA_PIN, I2C1_SDA_PAL_MODE); |
| 73 | #else | 73 | #else |
| 74 | palSetPadMode(I2C1_SCL_BANK, I2C1_SCL, PAL_MODE_ALTERNATE(I2C1_SCL_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN); | 74 | palSetLineMode(I2C1_SCL_PIN, PAL_MODE_ALTERNATE(I2C1_SCL_PAL_MODE) | PAL_OUTPUT_TYPE_OPENDRAIN); |
| 75 | palSetPadMode(I2C1_SDA_BANK, I2C1_SDA, PAL_MODE_ALTERNATE(I2C1_SDA_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN); | 75 | palSetLineMode(I2C1_SDA_PIN, PAL_MODE_ALTERNATE(I2C1_SDA_PAL_MODE) | PAL_OUTPUT_TYPE_OPENDRAIN); |
| 76 | #endif | 76 | #endif |
| 77 | } | 77 | } |
| 78 | } | 78 | } |
| @@ -102,7 +102,7 @@ i2c_status_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, const uint8_t* data, | |||
| 102 | i2cStart(&I2C_DRIVER, &i2cconfig); | 102 | i2cStart(&I2C_DRIVER, &i2cconfig); |
| 103 | 103 | ||
| 104 | uint8_t complete_packet[length + 1]; | 104 | uint8_t complete_packet[length + 1]; |
| 105 | for (uint8_t i = 0; i < length; i++) { | 105 | for (uint16_t i = 0; i < length; i++) { |
| 106 | complete_packet[i + 1] = data[i]; | 106 | complete_packet[i + 1] = data[i]; |
| 107 | } | 107 | } |
| 108 | complete_packet[0] = regaddr; | 108 | complete_packet[0] = regaddr; |
| @@ -111,6 +111,21 @@ i2c_status_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, const uint8_t* data, | |||
| 111 | return chibios_to_qmk(&status); | 111 | return chibios_to_qmk(&status); |
| 112 | } | 112 | } |
| 113 | 113 | ||
| 114 | i2c_status_t i2c_writeReg16(uint8_t devaddr, uint16_t regaddr, const uint8_t* data, uint16_t length, uint16_t timeout) { | ||
| 115 | i2c_address = devaddr; | ||
| 116 | i2cStart(&I2C_DRIVER, &i2cconfig); | ||
| 117 | |||
| 118 | uint8_t complete_packet[length + 2]; | ||
| 119 | for (uint16_t i = 0; i < length; i++) { | ||
| 120 | complete_packet[i + 2] = data[i]; | ||
| 121 | } | ||
| 122 | complete_packet[0] = regaddr >> 8; | ||
| 123 | complete_packet[1] = regaddr & 0xFF; | ||
| 124 | |||
| 125 | msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (i2c_address >> 1), complete_packet, length + 2, 0, 0, TIME_MS2I(timeout)); | ||
| 126 | return chibios_to_qmk(&status); | ||
| 127 | } | ||
| 128 | |||
| 114 | i2c_status_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout) { | 129 | i2c_status_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout) { |
| 115 | i2c_address = devaddr; | 130 | i2c_address = devaddr; |
| 116 | i2cStart(&I2C_DRIVER, &i2cconfig); | 131 | i2cStart(&I2C_DRIVER, &i2cconfig); |
| @@ -118,4 +133,12 @@ i2c_status_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16 | |||
| 118 | return chibios_to_qmk(&status); | 133 | return chibios_to_qmk(&status); |
| 119 | } | 134 | } |
| 120 | 135 | ||
| 136 | i2c_status_t i2c_readReg16(uint8_t devaddr, uint16_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout) { | ||
| 137 | i2c_address = devaddr; | ||
| 138 | i2cStart(&I2C_DRIVER, &i2cconfig); | ||
| 139 | uint8_t register_packet[2] = {regaddr >> 8, regaddr & 0xFF}; | ||
| 140 | msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (i2c_address >> 1), register_packet, 2, data, length, TIME_MS2I(timeout)); | ||
| 141 | return chibios_to_qmk(&status); | ||
| 142 | } | ||
| 143 | |||
| 121 | void i2c_stop(void) { i2cStop(&I2C_DRIVER); } | 144 | void i2c_stop(void) { i2cStop(&I2C_DRIVER); } |
