1
0
Fork 0

Rollback PR #7967 in preference of fixing I2C start/stop properly, in a followup PR. (#8173)

This commit is contained in:
Nick Brassel 2020-02-24 10:08:00 +11:00 committed by GitHub
parent f76f9c7d2a
commit 716c29881c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -29,6 +29,8 @@
#include <string.h> #include <string.h>
#include <hal.h> #include <hal.h>
static uint8_t i2c_address;
static const I2CConfig i2cconfig = { static const I2CConfig i2cconfig = {
#ifdef USE_I2CV1 #ifdef USE_I2CV1
I2C1_OPMODE, I2C1_OPMODE,
@ -69,49 +71,27 @@ __attribute__((weak)) void i2c_init(void) {
} }
i2c_status_t i2c_start(uint8_t address) { i2c_status_t i2c_start(uint8_t address) {
#if I2C_USE_MUTUAL_EXCLUSION i2c_address = address;
i2cAcquireBus(&I2C_DRIVER);
#endif
i2cStart(&I2C_DRIVER, &i2cconfig); i2cStart(&I2C_DRIVER, &i2cconfig);
return I2C_STATUS_SUCCESS; return I2C_STATUS_SUCCESS;
} }
i2c_status_t i2c_transmit(uint8_t address, const uint8_t* data, uint16_t length, uint16_t timeout) { i2c_status_t i2c_transmit(uint8_t address, const uint8_t* data, uint16_t length, uint16_t timeout) {
#if I2C_USE_MUTUAL_EXCLUSION i2c_address = address;
i2cAcquireBus(&I2C_DRIVER);
#endif
i2cStart(&I2C_DRIVER, &i2cconfig); i2cStart(&I2C_DRIVER, &i2cconfig);
msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (address >> 1), data, length, 0, 0, MS2ST(timeout)); msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (i2c_address >> 1), data, length, 0, 0, MS2ST(timeout));
#if I2C_USE_MUTUAL_EXCLUSION
i2cReleaseBus(&I2C_DRIVER);
#endif
return chibios_to_qmk(&status); return chibios_to_qmk(&status);
} }
i2c_status_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout) { i2c_status_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout) {
#if I2C_USE_MUTUAL_EXCLUSION i2c_address = address;
i2cAcquireBus(&I2C_DRIVER);
#endif
i2cStart(&I2C_DRIVER, &i2cconfig); i2cStart(&I2C_DRIVER, &i2cconfig);
msg_t status = i2cMasterReceiveTimeout(&I2C_DRIVER, (address >> 1), data, length, MS2ST(timeout)); msg_t status = i2cMasterReceiveTimeout(&I2C_DRIVER, (i2c_address >> 1), data, length, MS2ST(timeout));
#if I2C_USE_MUTUAL_EXCLUSION
i2cReleaseBus(&I2C_DRIVER);
#endif
return chibios_to_qmk(&status); return chibios_to_qmk(&status);
} }
i2c_status_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, const uint8_t* data, uint16_t length, uint16_t timeout) { i2c_status_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, const uint8_t* data, uint16_t length, uint16_t timeout) {
#if I2C_USE_MUTUAL_EXCLUSION i2c_address = devaddr;
i2cAcquireBus(&I2C_DRIVER);
#endif
i2cStart(&I2C_DRIVER, &i2cconfig); i2cStart(&I2C_DRIVER, &i2cconfig);
uint8_t complete_packet[length + 1]; uint8_t complete_packet[length + 1];
@ -120,34 +100,15 @@ i2c_status_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, const uint8_t* data,
} }
complete_packet[0] = regaddr; complete_packet[0] = regaddr;
msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (devaddr >> 1), complete_packet, length + 1, 0, 0, MS2ST(timeout)); msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (i2c_address >> 1), complete_packet, length + 1, 0, 0, MS2ST(timeout));
#if I2C_USE_MUTUAL_EXCLUSION
i2cReleaseBus(&I2C_DRIVER);
#endif
return chibios_to_qmk(&status); return chibios_to_qmk(&status);
} }
i2c_status_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout) { i2c_status_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout) {
#if I2C_USE_MUTUAL_EXCLUSION i2c_address = devaddr;
i2cAcquireBus(&I2C_DRIVER);
#endif
i2cStart(&I2C_DRIVER, &i2cconfig); i2cStart(&I2C_DRIVER, &i2cconfig);
msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (devaddr >> 1), &regaddr, 1, data, length, MS2ST(timeout)); msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (i2c_address >> 1), &regaddr, 1, data, length, MS2ST(timeout));
#if I2C_USE_MUTUAL_EXCLUSION
i2cReleaseBus(&I2C_DRIVER);
#endif
return chibios_to_qmk(&status); return chibios_to_qmk(&status);
} }
void i2c_stop(void) { void i2c_stop(void) { i2cStop(&I2C_DRIVER); }
i2cStop(&I2C_DRIVER);
#if I2C_USE_MUTUAL_EXCLUSION
i2cReleaseBus(&I2C_DRIVER);
#endif
}