i2c.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2018 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "py/mperrno.h"
  27. #include "py/mphal.h"
  28. #include "i2c.h"
  29. #if MICROPY_HW_ENABLE_HW_I2C
  30. #define I2C_POLL_TIMEOUT_MS (50)
  31. #if defined(STM32F4)
  32. int i2c_init(i2c_t *i2c, mp_hal_pin_obj_t scl, mp_hal_pin_obj_t sda, uint32_t freq) {
  33. uint32_t i2c_id = ((uint32_t)i2c - I2C1_BASE) / (I2C2_BASE - I2C1_BASE);
  34. // Init pins
  35. if (!mp_hal_pin_config_alt(scl, MP_HAL_PIN_MODE_ALT_OPEN_DRAIN, MP_HAL_PIN_PULL_UP, AF_FN_I2C, i2c_id + 1)) {
  36. return -MP_EPERM;
  37. }
  38. if (!mp_hal_pin_config_alt(sda, MP_HAL_PIN_MODE_ALT_OPEN_DRAIN, MP_HAL_PIN_PULL_UP, AF_FN_I2C, i2c_id + 1)) {
  39. return -MP_EPERM;
  40. }
  41. // Force reset I2C peripheral
  42. RCC->APB1RSTR |= RCC_APB1RSTR_I2C1RST << i2c_id;
  43. RCC->APB1RSTR &= ~(RCC_APB1RSTR_I2C1RST << i2c_id);
  44. // Enable I2C peripheral clock
  45. RCC->APB1ENR |= RCC_APB1ENR_I2C1EN << i2c_id;
  46. volatile uint32_t tmp = RCC->APB1ENR; // delay after RCC clock enable
  47. (void)tmp;
  48. uint32_t PCLK1 = HAL_RCC_GetPCLK1Freq();
  49. // Initialise I2C peripheral
  50. i2c->CR1 = 0;
  51. i2c->CR2 = PCLK1 / 1000000;
  52. i2c->OAR1 = 0;
  53. i2c->OAR2 = 0;
  54. freq = MIN(freq, 400000);
  55. // SM: MAX(4, PCLK1 / (F * 2))
  56. // FM, 16:9 duty: 0xc000 | MAX(1, (PCLK1 / (F * (16 + 9))))
  57. if (freq <= 100000) {
  58. i2c->CCR = MAX(4, PCLK1 / (freq * 2));
  59. } else {
  60. i2c->CCR = 0xc000 | MAX(1, PCLK1 / (freq * 25));
  61. }
  62. // SM: 1000ns / (1/PCLK1) + 1 = PCLK1 * 1e-6 + 1
  63. // FM: 300ns / (1/PCLK1) + 1 = 300e-3 * PCLK1 * 1e-6 + 1
  64. if (freq <= 100000) {
  65. i2c->TRISE = PCLK1 / 1000000 + 1; // 1000ns rise time in SM
  66. } else {
  67. i2c->TRISE = PCLK1 / 1000000 * 3 / 10 + 1; // 300ns rise time in FM
  68. }
  69. #if defined(I2C_FLTR_ANOFF)
  70. i2c->FLTR = 0; // analog filter on, digital filter off
  71. #endif
  72. return 0;
  73. }
  74. STATIC int i2c_wait_sr1_set(i2c_t *i2c, uint32_t mask) {
  75. uint32_t t0 = HAL_GetTick();
  76. while (!(i2c->SR1 & mask)) {
  77. if (HAL_GetTick() - t0 >= I2C_POLL_TIMEOUT_MS) {
  78. i2c->CR1 &= ~I2C_CR1_PE;
  79. return -MP_ETIMEDOUT;
  80. }
  81. }
  82. return 0;
  83. }
  84. STATIC int i2c_wait_stop(i2c_t *i2c) {
  85. uint32_t t0 = HAL_GetTick();
  86. while (i2c->CR1 & I2C_CR1_STOP) {
  87. if (HAL_GetTick() - t0 >= I2C_POLL_TIMEOUT_MS) {
  88. i2c->CR1 &= ~I2C_CR1_PE;
  89. return -MP_ETIMEDOUT;
  90. }
  91. }
  92. i2c->CR1 &= ~I2C_CR1_PE;
  93. return 0;
  94. }
  95. // For write: len = 0, 1 or N
  96. // For read: len = 1, 2 or N; stop = true
  97. int i2c_start_addr(i2c_t *i2c, int rd_wrn, uint16_t addr, size_t next_len, bool stop) {
  98. if (!(i2c->CR1 & I2C_CR1_PE) && (i2c->SR2 & I2C_SR2_MSL)) {
  99. // The F4 I2C peripheral can sometimes get into a bad state where it's disabled
  100. // (PE low) but still an active master (MSL high). It seems the best way to get
  101. // out of this is a full reset.
  102. uint32_t i2c_id = ((uint32_t)i2c - I2C1_BASE) / (I2C2_BASE - I2C1_BASE);
  103. RCC->APB1RSTR |= RCC_APB1RSTR_I2C1RST << i2c_id;
  104. RCC->APB1RSTR &= ~(RCC_APB1RSTR_I2C1RST << i2c_id);
  105. }
  106. // It looks like it's possible to terminate the reading by sending a
  107. // START condition instead of STOP condition but we don't support that.
  108. if (rd_wrn) {
  109. if (!stop) {
  110. return -MP_EINVAL;
  111. }
  112. }
  113. // Repurpose OAR1 to hold stop flag
  114. i2c->OAR1 = stop;
  115. // Enable peripheral and send START condition
  116. i2c->CR1 |= I2C_CR1_PE;
  117. i2c->CR1 |= I2C_CR1_START;
  118. // Wait for START to be sent
  119. int ret;
  120. if ((ret = i2c_wait_sr1_set(i2c, I2C_SR1_SB))) {
  121. return ret;
  122. }
  123. // Send the 7-bit address with read/write bit
  124. i2c->DR = addr << 1 | rd_wrn;
  125. // Wait for address to be sent
  126. if ((ret = i2c_wait_sr1_set(i2c, I2C_SR1_AF | I2C_SR1_ADDR))) {
  127. return ret;
  128. }
  129. // Check if the slave responded or not
  130. if (i2c->SR1 & I2C_SR1_AF) {
  131. // Got a NACK
  132. i2c->CR1 |= I2C_CR1_STOP;
  133. i2c_wait_stop(i2c); // Don't leak errors from this call
  134. return -MP_ENODEV;
  135. }
  136. if (rd_wrn) {
  137. // For reading, set up ACK/NACK control based on number of bytes to read (at least 1 byte)
  138. if (next_len <= 1) {
  139. // NACK next received byte
  140. i2c->CR1 &= ~I2C_CR1_ACK;
  141. } else if (next_len <= 2) {
  142. // NACK second received byte
  143. i2c->CR1 |= I2C_CR1_POS;
  144. i2c->CR1 &= ~I2C_CR1_ACK;
  145. } else {
  146. // ACK next received byte
  147. i2c->CR1 |= I2C_CR1_ACK;
  148. }
  149. }
  150. // Read SR2 to clear SR1_ADDR
  151. uint32_t sr2 = i2c->SR2;
  152. (void)sr2;
  153. return 0;
  154. }
  155. // next_len = 0 or N (>=2)
  156. int i2c_read(i2c_t *i2c, uint8_t *dest, size_t len, size_t next_len) {
  157. if (len == 0) {
  158. return -MP_EINVAL;
  159. }
  160. if (next_len == 1) {
  161. return -MP_EINVAL;
  162. }
  163. size_t remain = len + next_len;
  164. if (remain == 1) {
  165. // Special case
  166. i2c->CR1 |= I2C_CR1_STOP;
  167. int ret;
  168. if ((ret = i2c_wait_sr1_set(i2c, I2C_SR1_RXNE))) {
  169. return ret;
  170. }
  171. *dest = i2c->DR;
  172. } else {
  173. for (; len; --len) {
  174. remain = len + next_len;
  175. int ret;
  176. if ((ret = i2c_wait_sr1_set(i2c, I2C_SR1_BTF))) {
  177. return ret;
  178. }
  179. if (remain == 2) {
  180. // In this case next_len == 0 (it's not allowed to be 1)
  181. i2c->CR1 |= I2C_CR1_STOP;
  182. *dest++ = i2c->DR;
  183. *dest = i2c->DR;
  184. break;
  185. } else if (remain == 3) {
  186. // NACK next received byte
  187. i2c->CR1 &= ~I2C_CR1_ACK;
  188. }
  189. *dest++ = i2c->DR;
  190. }
  191. }
  192. if (!next_len) {
  193. // We sent a stop above, just wait for it to be finished
  194. return i2c_wait_stop(i2c);
  195. }
  196. return 0;
  197. }
  198. // next_len = 0 or N
  199. int i2c_write(i2c_t *i2c, const uint8_t *src, size_t len, size_t next_len) {
  200. int ret;
  201. if ((ret = i2c_wait_sr1_set(i2c, I2C_SR1_AF | I2C_SR1_TXE))) {
  202. return ret;
  203. }
  204. // Write out the data
  205. int num_acks = 0;
  206. while (len--) {
  207. i2c->DR = *src++;
  208. if ((ret = i2c_wait_sr1_set(i2c, I2C_SR1_AF | I2C_SR1_BTF))) {
  209. return ret;
  210. }
  211. if (i2c->SR1 & I2C_SR1_AF) {
  212. // Slave did not respond to byte so stop sending
  213. break;
  214. }
  215. ++num_acks;
  216. }
  217. if (!next_len) {
  218. if (i2c->OAR1) {
  219. // Send a STOP and wait for it to finish
  220. i2c->CR1 |= I2C_CR1_STOP;
  221. if ((ret = i2c_wait_stop(i2c))) {
  222. return ret;
  223. }
  224. }
  225. }
  226. return num_acks;
  227. }
  228. #elif defined(STM32F0) || defined(STM32F7)
  229. int i2c_init(i2c_t *i2c, mp_hal_pin_obj_t scl, mp_hal_pin_obj_t sda, uint32_t freq) {
  230. uint32_t i2c_id = ((uint32_t)i2c - I2C1_BASE) / (I2C2_BASE - I2C1_BASE);
  231. // Init pins
  232. if (!mp_hal_pin_config_alt(scl, MP_HAL_PIN_MODE_ALT_OPEN_DRAIN, MP_HAL_PIN_PULL_UP, AF_FN_I2C, i2c_id + 1)) {
  233. return -MP_EPERM;
  234. }
  235. if (!mp_hal_pin_config_alt(sda, MP_HAL_PIN_MODE_ALT_OPEN_DRAIN, MP_HAL_PIN_PULL_UP, AF_FN_I2C, i2c_id + 1)) {
  236. return -MP_EPERM;
  237. }
  238. // Enable I2C peripheral clock
  239. RCC->APB1ENR |= RCC_APB1ENR_I2C1EN << i2c_id;
  240. volatile uint32_t tmp = RCC->APB1ENR; // delay after RCC clock enable
  241. (void)tmp;
  242. // Initialise I2C peripheral
  243. i2c->CR1 = 0;
  244. i2c->CR2 = 0;
  245. i2c->OAR1 = 0;
  246. i2c->OAR2 = 0;
  247. // These timing values are for f_I2CCLK=54MHz and are only approximate
  248. if (freq >= 1000000) {
  249. i2c->TIMINGR = 0x50100103;
  250. } else if (freq >= 400000) {
  251. i2c->TIMINGR = 0x70330309;
  252. } else if (freq >= 100000) {
  253. i2c->TIMINGR = 0xb0420f13;
  254. } else {
  255. return -MP_EINVAL;
  256. }
  257. i2c->TIMEOUTR = 0;
  258. return 0;
  259. }
  260. STATIC int i2c_wait_cr2_clear(i2c_t *i2c, uint32_t mask) {
  261. uint32_t t0 = HAL_GetTick();
  262. while (i2c->CR2 & mask) {
  263. if (HAL_GetTick() - t0 >= I2C_POLL_TIMEOUT_MS) {
  264. i2c->CR1 &= ~I2C_CR1_PE;
  265. return -MP_ETIMEDOUT;
  266. }
  267. }
  268. return 0;
  269. }
  270. STATIC int i2c_wait_isr_set(i2c_t *i2c, uint32_t mask) {
  271. uint32_t t0 = HAL_GetTick();
  272. while (!(i2c->ISR & mask)) {
  273. if (HAL_GetTick() - t0 >= I2C_POLL_TIMEOUT_MS) {
  274. i2c->CR1 &= ~I2C_CR1_PE;
  275. return -MP_ETIMEDOUT;
  276. }
  277. }
  278. return 0;
  279. }
  280. // len = 0, 1 or N
  281. int i2c_start_addr(i2c_t *i2c, int rd_wrn, uint16_t addr, size_t len, bool stop) {
  282. // Enable the peripheral and send the START condition with slave address
  283. i2c->CR1 |= I2C_CR1_PE;
  284. i2c->CR2 = stop << I2C_CR2_AUTOEND_Pos
  285. | (len > 1) << I2C_CR2_RELOAD_Pos
  286. | (len > 0) << I2C_CR2_NBYTES_Pos
  287. | rd_wrn << I2C_CR2_RD_WRN_Pos
  288. | (addr & 0x7f) << 1;
  289. i2c->CR2 |= I2C_CR2_START;
  290. // Wait for address to be sent
  291. int ret;
  292. if ((ret = i2c_wait_cr2_clear(i2c, I2C_CR2_START))) {
  293. return ret;
  294. }
  295. // Check if the slave responded or not
  296. if (i2c->ISR & I2C_ISR_NACKF) {
  297. // If we get a NACK then I2C periph unconditionally sends a STOP
  298. i2c_wait_isr_set(i2c, I2C_ISR_STOPF); // Don't leak errors from this call
  299. i2c->CR1 &= ~I2C_CR1_PE;
  300. return -MP_ENODEV;
  301. }
  302. // Repurpose OAR1 to indicate that we loaded CR2
  303. i2c->OAR1 = 1;
  304. return 0;
  305. }
  306. STATIC int i2c_check_stop(i2c_t *i2c) {
  307. if (i2c->CR2 & I2C_CR2_AUTOEND) {
  308. // Wait for the STOP condition and then disable the peripheral
  309. int ret;
  310. if ((ret = i2c_wait_isr_set(i2c, I2C_ISR_STOPF))) {
  311. return ret;
  312. }
  313. i2c->CR1 &= ~I2C_CR1_PE;
  314. }
  315. return 0;
  316. }
  317. // next_len = 0 or N
  318. int i2c_read(i2c_t *i2c, uint8_t *dest, size_t len, size_t next_len) {
  319. if (i2c->OAR1) {
  320. i2c->OAR1 = 0;
  321. } else {
  322. goto load_cr2;
  323. }
  324. // Read in the data
  325. while (len--) {
  326. int ret;
  327. if ((ret = i2c_wait_isr_set(i2c, I2C_ISR_RXNE))) {
  328. return ret;
  329. }
  330. *dest++ = i2c->RXDR;
  331. load_cr2:
  332. if (len) {
  333. i2c->CR2 = (i2c->CR2 & I2C_CR2_AUTOEND)
  334. | (len + next_len > 1) << I2C_CR2_RELOAD_Pos
  335. | 1 << I2C_CR2_NBYTES_Pos;
  336. }
  337. }
  338. if (!next_len) {
  339. int ret;
  340. if ((ret = i2c_check_stop(i2c))) {
  341. return ret;
  342. }
  343. }
  344. return 0;
  345. }
  346. // next_len = 0 or N
  347. int i2c_write(i2c_t *i2c, const uint8_t *src, size_t len, size_t next_len) {
  348. int num_acks = 0;
  349. if (i2c->OAR1) {
  350. i2c->OAR1 = 0;
  351. } else {
  352. goto load_cr2;
  353. }
  354. // Write out the data
  355. while (len--) {
  356. int ret;
  357. if ((ret = i2c_wait_isr_set(i2c, I2C_ISR_TXE))) {
  358. return ret;
  359. }
  360. i2c->TXDR = *src++;
  361. if ((ret = i2c_wait_isr_set(i2c, I2C_ISR_TCR | I2C_ISR_TC | I2C_ISR_STOPF))) {
  362. return ret;
  363. }
  364. uint32_t isr = i2c->ISR;
  365. if (isr & I2C_ISR_NACKF) {
  366. // Slave did not respond to byte so stop sending
  367. if (!(isr & I2C_ISR_TXE)) {
  368. // The TXDR is still full so the byte previous to that wasn't actually ACK'd
  369. --num_acks;
  370. }
  371. break;
  372. }
  373. ++num_acks;
  374. load_cr2:
  375. if (len) {
  376. i2c->CR2 = (i2c->CR2 & I2C_CR2_AUTOEND)
  377. | (len + next_len > 1) << I2C_CR2_RELOAD_Pos
  378. | 1 << I2C_CR2_NBYTES_Pos;
  379. }
  380. }
  381. if (!next_len) {
  382. int ret;
  383. if ((ret = i2c_check_stop(i2c))) {
  384. return ret;
  385. }
  386. }
  387. return num_acks;
  388. }
  389. #endif
  390. #if defined(STM32F0) || defined(STM32F4) || defined(STM32F7)
  391. int i2c_readfrom(i2c_t *i2c, uint16_t addr, uint8_t *dest, size_t len, bool stop) {
  392. int ret;
  393. if ((ret = i2c_start_addr(i2c, 1, addr, len, stop))) {
  394. return ret;
  395. }
  396. return i2c_read(i2c, dest, len, 0);
  397. }
  398. int i2c_writeto(i2c_t *i2c, uint16_t addr, const uint8_t *src, size_t len, bool stop) {
  399. int ret;
  400. if ((ret = i2c_start_addr(i2c, 0, addr, len, stop))) {
  401. return ret;
  402. }
  403. return i2c_write(i2c, src, len, 0);
  404. }
  405. #endif
  406. #endif // MICROPY_HW_ENABLE_HW_I2C