diff options
-rw-r--r-- | drivers/arm/i2c_master.c | 63 |
1 files changed, 12 insertions, 51 deletions
diff --git a/drivers/arm/i2c_master.c b/drivers/arm/i2c_master.c index b3fabb05e..21aefd497 100644 --- a/drivers/arm/i2c_master.c +++ b/drivers/arm/i2c_master.c | |||
@@ -29,6 +29,8 @@ | |||
29 | #include <string.h> | 29 | #include <string.h> |
30 | #include <hal.h> | 30 | #include <hal.h> |
31 | 31 | ||
32 | static uint8_t i2c_address; | ||
33 | |||
32 | static const I2CConfig i2cconfig = { | 34 | static const I2CConfig i2cconfig = { |
33 | #ifdef USE_I2CV1 | 35 | #ifdef USE_I2CV1 |
34 | I2C1_OPMODE, | 36 | I2C1_OPMODE, |
@@ -69,49 +71,27 @@ __attribute__((weak)) void i2c_init(void) { | |||
69 | } | 71 | } |
70 | 72 | ||
71 | i2c_status_t i2c_start(uint8_t address) { | 73 | i2c_status_t i2c_start(uint8_t address) { |
72 | #if I2C_USE_MUTUAL_EXCLUSION | 74 | i2c_address = address; |
73 | i2cAcquireBus(&I2C_DRIVER); | ||
74 | #endif | ||
75 | |||
76 | i2cStart(&I2C_DRIVER, &i2cconfig); | 75 | i2cStart(&I2C_DRIVER, &i2cconfig); |
77 | return I2C_STATUS_SUCCESS; | 76 | return I2C_STATUS_SUCCESS; |
78 | } | 77 | } |
79 | 78 | ||
80 | i2c_status_t i2c_transmit(uint8_t address, const uint8_t* data, uint16_t length, uint16_t timeout) { | 79 | i2c_status_t i2c_transmit(uint8_t address, const uint8_t* data, uint16_t length, uint16_t timeout) { |
81 | #if I2C_USE_MUTUAL_EXCLUSION | 80 | i2c_address = address; |
82 | i2cAcquireBus(&I2C_DRIVER); | ||
83 | #endif | ||
84 | |||
85 | i2cStart(&I2C_DRIVER, &i2cconfig); | 81 | i2cStart(&I2C_DRIVER, &i2cconfig); |
86 | msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (address >> 1), data, length, 0, 0, MS2ST(timeout)); | 82 | msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (i2c_address >> 1), data, length, 0, 0, MS2ST(timeout)); |
87 | |||
88 | #if I2C_USE_MUTUAL_EXCLUSION | ||
89 | i2cReleaseBus(&I2C_DRIVER); | ||
90 | #endif | ||
91 | |||
92 | return chibios_to_qmk(&status); | 83 | return chibios_to_qmk(&status); |
93 | } | 84 | } |
94 | 85 | ||
95 | i2c_status_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout) { | 86 | i2c_status_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout) { |
96 | #if I2C_USE_MUTUAL_EXCLUSION | 87 | i2c_address = address; |
97 | i2cAcquireBus(&I2C_DRIVER); | ||
98 | #endif | ||
99 | |||
100 | i2cStart(&I2C_DRIVER, &i2cconfig); | 88 | i2cStart(&I2C_DRIVER, &i2cconfig); |
101 | msg_t status = i2cMasterReceiveTimeout(&I2C_DRIVER, (address >> 1), data, length, MS2ST(timeout)); | 89 | msg_t status = i2cMasterReceiveTimeout(&I2C_DRIVER, (i2c_address >> 1), data, length, MS2ST(timeout)); |
102 | |||
103 | #if I2C_USE_MUTUAL_EXCLUSION | ||
104 | i2cReleaseBus(&I2C_DRIVER); | ||
105 | #endif | ||
106 | |||
107 | return chibios_to_qmk(&status); | 90 | return chibios_to_qmk(&status); |
108 | } | 91 | } |
109 | 92 | ||
110 | i2c_status_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, const uint8_t* data, uint16_t length, uint16_t timeout) { | 93 | i2c_status_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, const uint8_t* data, uint16_t length, uint16_t timeout) { |
111 | #if I2C_USE_MUTUAL_EXCLUSION | 94 | i2c_address = devaddr; |
112 | i2cAcquireBus(&I2C_DRIVER); | ||
113 | #endif | ||
114 | |||
115 | i2cStart(&I2C_DRIVER, &i2cconfig); | 95 | i2cStart(&I2C_DRIVER, &i2cconfig); |
116 | 96 | ||
117 | uint8_t complete_packet[length + 1]; | 97 | 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, | |||
120 | } | 100 | } |
121 | complete_packet[0] = regaddr; | 101 | complete_packet[0] = regaddr; |
122 | 102 | ||
123 | msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (devaddr >> 1), complete_packet, length + 1, 0, 0, MS2ST(timeout)); | 103 | msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (i2c_address >> 1), complete_packet, length + 1, 0, 0, MS2ST(timeout)); |
124 | |||
125 | #if I2C_USE_MUTUAL_EXCLUSION | ||
126 | i2cReleaseBus(&I2C_DRIVER); | ||
127 | #endif | ||
128 | |||
129 | return chibios_to_qmk(&status); | 104 | return chibios_to_qmk(&status); |
130 | } | 105 | } |
131 | 106 | ||
132 | i2c_status_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout) { | 107 | i2c_status_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout) { |
133 | #if I2C_USE_MUTUAL_EXCLUSION | 108 | i2c_address = devaddr; |
134 | i2cAcquireBus(&I2C_DRIVER); | ||
135 | #endif | ||
136 | |||
137 | i2cStart(&I2C_DRIVER, &i2cconfig); | 109 | i2cStart(&I2C_DRIVER, &i2cconfig); |
138 | msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (devaddr >> 1), ®addr, 1, data, length, MS2ST(timeout)); | 110 | msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (i2c_address >> 1), ®addr, 1, data, length, MS2ST(timeout)); |
139 | |||
140 | #if I2C_USE_MUTUAL_EXCLUSION | ||
141 | i2cReleaseBus(&I2C_DRIVER); | ||
142 | #endif | ||
143 | |||
144 | return chibios_to_qmk(&status); | 111 | return chibios_to_qmk(&status); |
145 | } | 112 | } |
146 | 113 | ||
147 | void i2c_stop(void) { | 114 | void i2c_stop(void) { i2cStop(&I2C_DRIVER); } |
148 | i2cStop(&I2C_DRIVER); | ||
149 | |||
150 | #if I2C_USE_MUTUAL_EXCLUSION | ||
151 | i2cReleaseBus(&I2C_DRIVER); | ||
152 | #endif | ||
153 | } | ||