aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorGravatar Nick Brassel <nick@tzarc.org>2020-01-23 05:26:52 +1100
committerGravatar Nick Brassel <nick@tzarc.org>2020-02-01 17:44:39 +1100
commit8e550fc11a59326cb759593418ce1a062392daaf (patch)
tree41bb317dfdca1a9bbe95a2729de8a2ad8b73ba4c /drivers
parent1aee492c0f88b1a96fabdf750a03ed128d9d844a (diff)
downloadqmk_firmware-8e550fc11a59326cb759593418ce1a062392daaf.tar.gz
Add thread safety around i2c_master for ChibiOS/ARM.
Diffstat (limited to 'drivers')
-rw-r--r--drivers/arm/i2c_master.c63
1 files changed, 51 insertions, 12 deletions
diff --git a/drivers/arm/i2c_master.c b/drivers/arm/i2c_master.c
index 21aefd497..b3fabb05e 100644
--- a/drivers/arm/i2c_master.c
+++ b/drivers/arm/i2c_master.c
@@ -29,8 +29,6 @@
#include <string.h>
#include <hal.h>
-static uint8_t i2c_address;
-
static const I2CConfig i2cconfig = {
#ifdef USE_I2CV1
I2C1_OPMODE,
@@ -71,27 +69,49 @@ __attribute__((weak)) void i2c_init(void) {
}
i2c_status_t i2c_start(uint8_t address) {
- i2c_address = address;
+#if I2C_USE_MUTUAL_EXCLUSION
+ i2cAcquireBus(&I2C_DRIVER);
+#endif
+
i2cStart(&I2C_DRIVER, &i2cconfig);
return I2C_STATUS_SUCCESS;
}
i2c_status_t i2c_transmit(uint8_t address, const uint8_t* data, uint16_t length, uint16_t timeout) {
- i2c_address = address;
+#if I2C_USE_MUTUAL_EXCLUSION
+ i2cAcquireBus(&I2C_DRIVER);
+#endif
+
i2cStart(&I2C_DRIVER, &i2cconfig);
- msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (i2c_address >> 1), data, length, 0, 0, MS2ST(timeout));
+ msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (address >> 1), data, length, 0, 0, MS2ST(timeout));
+
+#if I2C_USE_MUTUAL_EXCLUSION
+ i2cReleaseBus(&I2C_DRIVER);
+#endif
+
return chibios_to_qmk(&status);
}
i2c_status_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout) {
- i2c_address = address;
+#if I2C_USE_MUTUAL_EXCLUSION
+ i2cAcquireBus(&I2C_DRIVER);
+#endif
+
i2cStart(&I2C_DRIVER, &i2cconfig);
- msg_t status = i2cMasterReceiveTimeout(&I2C_DRIVER, (i2c_address >> 1), data, length, MS2ST(timeout));
+ msg_t status = i2cMasterReceiveTimeout(&I2C_DRIVER, (address >> 1), data, length, MS2ST(timeout));
+
+#if I2C_USE_MUTUAL_EXCLUSION
+ i2cReleaseBus(&I2C_DRIVER);
+#endif
+
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_address = devaddr;
+#if I2C_USE_MUTUAL_EXCLUSION
+ i2cAcquireBus(&I2C_DRIVER);
+#endif
+
i2cStart(&I2C_DRIVER, &i2cconfig);
uint8_t complete_packet[length + 1];
@@ -100,15 +120,34 @@ i2c_status_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, const uint8_t* data,
}
complete_packet[0] = regaddr;
- msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (i2c_address >> 1), complete_packet, length + 1, 0, 0, MS2ST(timeout));
+ msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (devaddr >> 1), complete_packet, length + 1, 0, 0, MS2ST(timeout));
+
+#if I2C_USE_MUTUAL_EXCLUSION
+ i2cReleaseBus(&I2C_DRIVER);
+#endif
+
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_address = devaddr;
+#if I2C_USE_MUTUAL_EXCLUSION
+ i2cAcquireBus(&I2C_DRIVER);
+#endif
+
i2cStart(&I2C_DRIVER, &i2cconfig);
- msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (i2c_address >> 1), &regaddr, 1, data, length, MS2ST(timeout));
+ msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (devaddr >> 1), &regaddr, 1, data, length, MS2ST(timeout));
+
+#if I2C_USE_MUTUAL_EXCLUSION
+ i2cReleaseBus(&I2C_DRIVER);
+#endif
+
return chibios_to_qmk(&status);
}
-void i2c_stop(void) { i2cStop(&I2C_DRIVER); }
+void i2c_stop(void) {
+ i2cStop(&I2C_DRIVER);
+
+#if I2C_USE_MUTUAL_EXCLUSION
+ i2cReleaseBus(&I2C_DRIVER);
+#endif
+}