hal_i2c_lld.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file common/I2C/hal_i2c_lld.c
  15. * @brief SW I2C subsystem low level driver source.
  16. *
  17. * @addtogroup I2C
  18. * @{
  19. */
  20. #include "hal.h"
  21. #if HAL_USE_I2C || defined(__DOXYGEN__)
  22. /*===========================================================================*/
  23. /* Driver local definitions. */
  24. /*===========================================================================*/
  25. #define CHECK_ERROR(msg) \
  26. if ((msg) < (msg_t)0) { \
  27. return MSG_TIMEOUT; \
  28. }
  29. /*===========================================================================*/
  30. /* Driver constants. */
  31. /*===========================================================================*/
  32. /*===========================================================================*/
  33. /* Driver exported variables. */
  34. /*===========================================================================*/
  35. /** @brief I2C1 driver identifier.*/
  36. #if SW_I2C_USE_I2C1 || defined(__DOXYGEN__)
  37. I2CDriver I2CD1;
  38. #endif
  39. /** @brief I2C2 driver identifier.*/
  40. #if SW_I2C_USE_I2C2 || defined(__DOXYGEN__)
  41. I2CDriver I2CD2;
  42. #endif
  43. /** @brief I2C3 driver identifier.*/
  44. #if SW_I2C_USE_I2C3 || defined(__DOXYGEN__)
  45. I2CDriver I2CD3;
  46. #endif
  47. /** @brief I2C4 driver identifier.*/
  48. #if SW_I2C_USE_I2C4 || defined(__DOXYGEN__)
  49. I2CDriver I2CD4;
  50. #endif
  51. /*===========================================================================*/
  52. /* Driver local variables and types. */
  53. /*===========================================================================*/
  54. /*===========================================================================*/
  55. /* Driver local functions. */
  56. /*===========================================================================*/
  57. static msg_t i2c_write_stop(I2CDriver *i2cp);
  58. static inline void i2c_delay(I2CDriver *i2cp) {
  59. #if SW_I2C_USE_OSAL_DELAY || defined(__DOXYGEN__)
  60. osalThreadSleep(i2cp->config->ticks);
  61. #else
  62. i2cp->config->delay();
  63. #endif
  64. }
  65. static inline msg_t i2c_check_arbitration(I2CDriver *i2cp) {
  66. if (palReadLine(i2cp->config->sda) == PAL_LOW) {
  67. i2cp->errors |= I2C_ARBITRATION_LOST;
  68. return MSG_RESET;
  69. }
  70. return MSG_OK;
  71. }
  72. static inline msg_t i2c_check_timeout(I2CDriver *i2cp) {
  73. if (!osalOsIsTimeWithinX(osalOsGetSystemTimeX(), i2cp->start, i2cp->end)) {
  74. i2c_write_stop(i2cp);
  75. return MSG_TIMEOUT;
  76. }
  77. return MSG_OK;
  78. }
  79. static msg_t i2c_wait_clock(I2CDriver *i2cp) {
  80. while (palReadLine(i2cp->config->scl) == PAL_LOW) {
  81. if (!osalOsIsTimeWithinX(osalOsGetSystemTimeX(), i2cp->start, i2cp->end)) {
  82. return MSG_TIMEOUT;
  83. }
  84. i2c_delay(i2cp);
  85. }
  86. return MSG_OK;
  87. }
  88. static inline msg_t i2c_write_start(I2CDriver *i2cp) {
  89. /* Arbitration check.*/
  90. CHECK_ERROR(i2c_check_arbitration(i2cp));
  91. palClearLine(i2cp->config->sda);
  92. i2c_delay(i2cp);
  93. palClearLine(i2cp->config->scl);
  94. return MSG_OK;
  95. }
  96. static msg_t i2c_write_restart(I2CDriver *i2cp) {
  97. palSetLine(i2cp->config->sda);
  98. i2c_delay(i2cp);
  99. palSetLine(i2cp->config->scl);
  100. /* Clock stretching.*/
  101. CHECK_ERROR(i2c_wait_clock(i2cp));
  102. i2c_delay(i2cp);
  103. i2c_write_start(i2cp);
  104. return MSG_OK;
  105. }
  106. static msg_t i2c_write_stop(I2CDriver *i2cp) {
  107. palClearLine(i2cp->config->sda);
  108. i2c_delay(i2cp);
  109. palSetLine(i2cp->config->scl);
  110. /* Clock stretching.*/
  111. CHECK_ERROR(i2c_wait_clock(i2cp));
  112. i2c_delay(i2cp);
  113. palSetLine(i2cp->config->sda);
  114. i2c_delay(i2cp);
  115. /* Arbitration check.*/
  116. CHECK_ERROR(i2c_check_arbitration(i2cp));
  117. i2c_delay(i2cp);
  118. return MSG_OK;
  119. }
  120. static msg_t i2c_write_bit(I2CDriver *i2cp, unsigned bit) {
  121. palWriteLine(i2cp->config->sda, bit);
  122. i2c_delay(i2cp);
  123. palSetLine(i2cp->config->scl);
  124. i2c_delay(i2cp);
  125. /* Clock stretching.*/
  126. CHECK_ERROR(i2c_wait_clock(i2cp));
  127. /* Arbitration check.*/
  128. if (bit == PAL_HIGH) {
  129. CHECK_ERROR(i2c_check_arbitration(i2cp));
  130. }
  131. palClearLine(i2cp->config->scl);
  132. return MSG_OK;
  133. }
  134. static msg_t i2c_read_bit(I2CDriver *i2cp) {
  135. msg_t bit;
  136. palSetLine(i2cp->config->sda);
  137. i2c_delay(i2cp);
  138. palSetLine(i2cp->config->scl);
  139. /* Clock stretching.*/
  140. CHECK_ERROR(i2c_wait_clock(i2cp));
  141. i2c_delay(i2cp);
  142. bit = palReadLine(i2cp->config->sda);
  143. palClearLine(i2cp->config->scl);
  144. return bit;
  145. }
  146. static msg_t i2c_write_byte(I2CDriver *i2cp, uint8_t byte) {
  147. msg_t msg;
  148. uint8_t mask;
  149. CHECK_ERROR(i2c_check_timeout(i2cp));
  150. for (mask = 0x80U; mask > 0U; mask >>= 1U) {
  151. CHECK_ERROR(i2c_write_bit(i2cp, (byte & mask) != 0));
  152. }
  153. msg = i2c_read_bit(i2cp);
  154. CHECK_ERROR(msg);
  155. /* Checking for NACK.*/
  156. if (msg == PAL_HIGH) {
  157. i2cp->errors |= I2C_ACK_FAILURE;
  158. return MSG_RESET;
  159. }
  160. return MSG_OK;
  161. }
  162. static msg_t i2c_read_byte(I2CDriver *i2cp, unsigned nack) {
  163. msg_t byte;
  164. unsigned i;
  165. CHECK_ERROR(i2c_check_timeout(i2cp));
  166. byte = 0U;
  167. for (i = 0; i < 8; i++) {
  168. msg_t msg = i2c_read_bit(i2cp);
  169. CHECK_ERROR(msg);
  170. byte = (byte << 1U) | msg;
  171. }
  172. CHECK_ERROR(i2c_write_bit(i2cp, nack));
  173. return byte;
  174. }
  175. static msg_t i2c_write_header(I2CDriver *i2cp, i2caddr_t addr, bool rw) {
  176. /* Check for 10 bits addressing.*/
  177. if (i2cp->config->addr10) {
  178. /* It is 10 bits.*/
  179. uint8_t b1, b2;
  180. b1 = 0xF0U | ((addr >> 8U) << 1U);
  181. b2 = (uint8_t)(addr & 255U);
  182. if (rw) {
  183. b1 |= 1U;
  184. }
  185. CHECK_ERROR(i2c_write_byte(i2cp, b1));
  186. CHECK_ERROR(i2c_write_byte(i2cp, b2));
  187. }
  188. else {
  189. /* It is 7 bits.*/
  190. if (rw) {
  191. /* Read.*/
  192. CHECK_ERROR(i2c_write_byte(i2cp, (addr << 1U) | 1U));
  193. }
  194. else {
  195. /* Write.*/
  196. CHECK_ERROR(i2c_write_byte(i2cp, (addr << 1U) | 0U));
  197. }
  198. }
  199. return MSG_OK;
  200. }
  201. /*===========================================================================*/
  202. /* Driver interrupt handlers. */
  203. /*===========================================================================*/
  204. /*===========================================================================*/
  205. /* Driver exported functions. */
  206. /*===========================================================================*/
  207. /**
  208. * @brief Low level I2C driver initialization.
  209. *
  210. * @notapi
  211. */
  212. void i2c_lld_init(void) {
  213. #if SW_I2C_USE_I2C1
  214. i2cObjectInit(&I2CD1);
  215. #endif
  216. #if SW_I2C_USE_I2C2
  217. i2cObjectInit(&I2CD2);
  218. #endif
  219. #if SW_I2C_USE_I2C3
  220. i2cObjectInit(&I2CD3);
  221. #endif
  222. #if SW_I2C_USE_I2C4
  223. i2cObjectInit(&I2CD4);
  224. #endif
  225. }
  226. /**
  227. * @brief Configures and activates the I2C peripheral.
  228. *
  229. * @param[in] i2cp pointer to the @p I2CDriver object
  230. *
  231. * @notapi
  232. */
  233. void i2c_lld_start(I2CDriver *i2cp) {
  234. /* Does nothing.*/
  235. (void)i2cp;
  236. }
  237. /**
  238. * @brief Deactivates the I2C peripheral.
  239. *
  240. * @param[in] i2cp pointer to the @p I2CDriver object
  241. *
  242. * @notapi
  243. */
  244. void i2c_lld_stop(I2CDriver *i2cp) {
  245. /* Does nothing.*/
  246. (void)i2cp;
  247. }
  248. /**
  249. * @brief Deactivates the I2C peripheral.
  250. *
  251. * @param[in] i2cp pointer to the @p I2CDriver object
  252. *
  253. * @notapi
  254. */
  255. void i2c_lld_soft_stop(I2CDriver *i2cp) {
  256. /* just stop if there's no "soft stop option" */
  257. i2c_lld_stop(i2cp);
  258. }
  259. /**
  260. * @brief Receives data via the I2C bus as master.
  261. *
  262. * @param[in] i2cp pointer to the @p I2CDriver object
  263. * @param[in] addr slave device address
  264. * @param[out] rxbuf pointer to the receive buffer
  265. * @param[in] rxbytes number of bytes to be received
  266. * @param[in] timeout the number of ticks before the operation timeouts,
  267. * the following special values are allowed:
  268. * - @a TIME_INFINITE no timeout.
  269. * .
  270. * @return The operation status.
  271. * @retval MSG_OK if the function succeeded.
  272. * @retval MSG_RESET if one or more I2C errors occurred, the errors can
  273. * be retrieved using @p i2cGetErrors().
  274. * @retval MSG_TIMEOUT if a timeout occurred before operation end. <b>After a
  275. * timeout the driver must be stopped and restarted
  276. * because the bus is in an uncertain state</b>.
  277. *
  278. * @notapi
  279. */
  280. msg_t i2c_lld_master_receive_timeout(I2CDriver *i2cp, i2caddr_t addr,
  281. uint8_t *rxbuf, size_t rxbytes,
  282. systime_t timeout) {
  283. /* Setting timeout fields.*/
  284. i2cp->start = osalOsGetSystemTimeX();
  285. i2cp->end = i2cp->start;
  286. if (timeout != TIME_INFINITE) {
  287. i2cp->end += timeout;
  288. }
  289. CHECK_ERROR(i2c_write_start(i2cp));
  290. /* Sending anddress and mode.*/
  291. CHECK_ERROR(i2c_write_header(i2cp, addr, true));
  292. do {
  293. /* Last byte sends a NACK.*/
  294. msg_t msg = i2c_read_byte(i2cp, rxbytes > 1U ? 0U : 1U);
  295. CHECK_ERROR(msg);
  296. *rxbuf++ = (uint8_t)msg;
  297. } while (--rxbytes);
  298. return i2c_write_stop(i2cp);
  299. }
  300. /**
  301. * @brief Transmits data via the I2C bus as master.
  302. * @details Number of receiving bytes must be 0 or more than 1 on STM32F1x.
  303. * This is hardware restriction.
  304. *
  305. * @param[in] i2cp pointer to the @p I2CDriver object
  306. * @param[in] addr slave device address
  307. * @param[in] txbuf pointer to the transmit buffer
  308. * @param[in] txbytes number of bytes to be transmitted
  309. * @param[out] rxbuf pointer to the receive buffer
  310. * @param[in] rxbytes number of bytes to be received
  311. * @param[in] timeout the number of ticks before the operation timeouts,
  312. * the following special values are allowed:
  313. * - @a TIME_INFINITE no timeout.
  314. * .
  315. * @return The operation status.
  316. * @retval MSG_OK if the function succeeded.
  317. * @retval MSG_RESET if one or more I2C errors occurred, the errors can
  318. * be retrieved using @p i2cGetErrors().
  319. * @retval MSG_TIMEOUT if a timeout occurred before operation end. <b>After a
  320. * timeout the driver must be stopped and restarted
  321. * because the bus is in an uncertain state</b>.
  322. *
  323. * @notapi
  324. */
  325. msg_t i2c_lld_master_transmit_timeout(I2CDriver *i2cp, i2caddr_t addr,
  326. const uint8_t *txbuf, size_t txbytes,
  327. uint8_t *rxbuf, size_t rxbytes,
  328. systime_t timeout) {
  329. /* Setting timeout fields.*/
  330. i2cp->start = osalOsGetSystemTimeX();
  331. i2cp->end = i2cp->start;
  332. if (timeout != TIME_INFINITE) {
  333. i2cp->end += timeout;
  334. }
  335. /* send start condition */
  336. CHECK_ERROR(i2c_write_start(i2cp));
  337. /* Sending anddress and mode.*/
  338. CHECK_ERROR(i2c_write_header(i2cp, addr, false));
  339. do {
  340. CHECK_ERROR(i2c_write_byte(i2cp, *txbuf++));
  341. } while (--txbytes);
  342. /* Is there a read phase? */
  343. if (rxbytes > 0U) {
  344. /* send restart condition */
  345. CHECK_ERROR(i2c_write_restart(i2cp));
  346. /* Sending anddress and mode.*/
  347. CHECK_ERROR(i2c_write_header(i2cp, addr, true));
  348. do {
  349. /* Last byte sends a NACK.*/
  350. msg_t msg = i2c_read_byte(i2cp, rxbytes > 1U ? 0U : 1U);
  351. CHECK_ERROR(msg);
  352. *rxbuf++ = (uint8_t)msg;
  353. } while (--rxbytes);
  354. }
  355. return i2c_write_stop(i2cp);
  356. }
  357. #endif /* HAL_USE_I2C */
  358. /** @} */