stm32_i2cbus_v1.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. /*
  2. * Copyright (C) 2013 Uwe Bonnes(bon@elektron.ikp.physik.tu-darmstadt.de
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*!
  35. * \file arch/cm3/dev/stm/stm32_i2cbus_v1.c
  36. * \brief I2C bus driver for I2C hardware in STM32F1/2/4 and STM32L1.
  37. *
  38. * It doesn't consider Slave operation yet
  39. *
  40. * It is intended that this driver replaces the current STM TWI driver,
  41. * which doesn't allow to have different types of busses in a single
  42. * application, for example TWI hardware and bit banging interfaces.
  43. * This new I2C driver layout allows to attach any I2C slave driver to
  44. * any I2C bus driver by calling NutRegisterI2cSlave().
  45. *
  46. * \verbatim
  47. * $Id: stm32_i2cbus_v1.c 5623 2014-04-02 16:26:30Z u_bonnes $
  48. * \endverbatim
  49. */
  50. /*!
  51. * \brief I2C bus driver for STM32F1/2/4 and L1 hardware.
  52. *
  53. * This is an interrupt driver, which supports master mode only.
  54. * Most error handling
  55. */
  56. #include <dev/irqreg.h>
  57. #include <sys/nutdebug.h>
  58. #include <sys/timer.h>
  59. #include <sys/event.h>
  60. #include <stdlib.h>
  61. #include <cfg/arch.h>
  62. #include <arch/cm3.h>
  63. #include <dev/gpio.h>
  64. #include <arch/cm3/stm/stm32_gpio.h>
  65. #include <arch/cm3/stm/stm32_irqreg.h>
  66. #include <dev/i2cbus.h>
  67. #include <cfg/twi.h>
  68. /*!
  69. * \addtogroup xgI2cBusSTM32
  70. */
  71. /*@{*/
  72. /*!
  73. * \brief Local data of the STM32 I2C bus driver.
  74. */
  75. typedef struct _STM32_I2CCB {
  76. /*! \brief Register base. */
  77. uptr_t icb_base;
  78. /*! \brief SDA_PIN. */
  79. uint32_t sda_pin;
  80. /*! \brief SCL_PIN. */
  81. uint32_t scl_pin;
  82. /*! \brief SMBA_PIN. */
  83. uint32_t smba_pin;
  84. /*! \brief System event handler. */
  85. IRQ_HANDLER *icb_sig_ev;
  86. /*! \brief System error handler. */
  87. IRQ_HANDLER *icb_sig_er;
  88. /*! \brief I2C message. */
  89. NUTI2C_MSG *icb_msg;
  90. /*! \brief Thread waiting for completion. */
  91. HANDLE icb_queue;
  92. /*! \brief Current Slave/ */
  93. NUTI2C_SLAVE *slave;
  94. uint32_t errors;
  95. } STM32_I2CCB;
  96. /*
  97. * STM32V1 I2C Event interrupt function.
  98. */
  99. static void I2cEventBusIrqHandler(void *arg)
  100. {
  101. STM32_I2CCB *icb = (STM32_I2CCB *) arg;
  102. NUTI2C_MSG *msg = icb->icb_msg;
  103. I2C_TypeDef *I2Cx = (I2C_TypeDef *) icb->icb_base;
  104. if(I2Cx->SR1 & I2C_SR1_SB)
  105. { /* Receiver EV5 */
  106. if(msg->msg_widx >= msg->msg_wlen)
  107. {
  108. /* All bytes written, (re)start reading */
  109. I2Cx->DR = icb->slave->slave_address<<1|1;
  110. }
  111. else
  112. I2Cx->DR = icb->slave->slave_address<<1;
  113. }
  114. if(I2Cx->SR1 & I2C_SR1_ADDR)
  115. {
  116. if (I2Cx->SR2 & I2C_SR2_TRA)
  117. {
  118. /* Transmitting*/
  119. if (msg->msg_widx < msg->msg_wlen)
  120. {
  121. I2Cx->DR = msg->msg_wdat[msg->msg_widx];
  122. msg->msg_widx++;
  123. }
  124. }
  125. else
  126. {
  127. /* Inhibit IRQ storm as TXE not cleared as we
  128. * have nothing written to DR Register
  129. */
  130. I2Cx->CR2 |= I2C_CR2_ITBUFEN;
  131. if (msg->msg_rsiz >1)
  132. /* Receiver EV6*/
  133. I2Cx->CR1 |= I2C_CR1_ACK;
  134. else
  135. { /* EV6_1*/
  136. I2Cx->CR1 &= ~I2C_CR1_ACK;
  137. I2Cx->CR1 |= I2C_CR1_STOP;
  138. }
  139. }
  140. }
  141. if (I2Cx->SR1 & I2C_SR1_TXE)
  142. {
  143. if (msg->msg_widx < msg->msg_wlen)
  144. {
  145. I2Cx->DR = msg->msg_wdat[msg->msg_widx];
  146. msg->msg_widx++;
  147. }
  148. else
  149. {
  150. I2Cx->CR2 &= ~I2C_CR2_ITBUFEN;
  151. if (msg->msg_rsiz)
  152. I2Cx->CR1 |= I2C_CR1_START;
  153. else
  154. I2Cx->CR1 |= I2C_CR1_STOP;
  155. }
  156. }
  157. if (I2Cx->SR1 & I2C_SR1_RXNE)
  158. { /* Receiver EV7*/
  159. msg->msg_rdat[msg->msg_ridx]= I2Cx->DR;
  160. msg->msg_ridx++;
  161. if(msg->msg_ridx + 2 > msg->msg_rsiz)
  162. {
  163. I2Cx->CR1 &= ~I2C_CR1_ACK;
  164. I2Cx->CR1 |= I2C_CR1_STOP;
  165. }
  166. if (msg->msg_ridx + 1 > msg->msg_rsiz)
  167. {
  168. NutEventPostFromIrq(&icb->icb_queue);
  169. }
  170. }
  171. if (I2Cx->SR1 & I2C_SR1_BTF)
  172. {
  173. /* Terminate write transaction without read*/
  174. if ((I2Cx->SR2 & I2C_SR2_TRA) && !(msg->msg_rsiz))
  175. {
  176. I2Cx->CR2 &= ~(I2C_CR2_ITEVTEN|I2C_CR2_ITBUFEN);
  177. NutEventPostFromIrq(&icb->icb_queue);
  178. }
  179. }
  180. }
  181. /*
  182. * STM32V1 I2C Error interrupt function.
  183. */
  184. static void I2cErrorBusIrqHandler(void *arg)
  185. {
  186. STM32_I2CCB *icb = (STM32_I2CCB *) arg;
  187. I2C_TypeDef *I2Cx = (I2C_TypeDef *) icb->icb_base;
  188. icb->errors = I2Cx->SR1;
  189. I2Cx->SR1 &= ~(I2C_SR1_SMBALERT|I2C_SR1_TIMEOUT|I2C_SR1_PECERR|I2C_SR1_OVR|
  190. I2C_SR1_AF|I2C_SR1_ARLO|I2C_SR1_BERR);
  191. NutEventPostFromIrq(&icb->icb_queue);
  192. }
  193. /*!
  194. * \brief I2C bus transfer (STM I2C implementation).
  195. *
  196. * This function is called by the platform independent code via the
  197. * NUTI2C_BUS::bus_tran function pointer.
  198. */
  199. static int I2cBusTran(NUTI2C_SLAVE *slave, NUTI2C_MSG *msg)
  200. {
  201. NUTI2C_BUS *bus;
  202. STM32_I2CCB *icb;
  203. I2C_TypeDef *I2Cx;
  204. int rc;
  205. bus = slave->slave_bus;
  206. NUTASSERT(bus != NULL);
  207. NUTASSERT(bus->bus_icb != NULL);
  208. icb = (STM32_I2CCB *) bus->bus_icb;
  209. icb->icb_msg = msg;
  210. I2Cx = (I2C_TypeDef *) icb->icb_base;
  211. msg->msg_widx = 0;
  212. msg->msg_ridx = 0;
  213. icb->slave = slave;
  214. icb->errors = 0;
  215. /* Enable Interrupts */
  216. I2Cx->CR2 |= I2C_CR2_ITEVTEN|I2C_CR2_ITBUFEN |I2C_CR2_ITERREN;
  217. I2Cx->CR1 |= I2C_CR1_START;
  218. rc = NutEventWait(&icb->icb_queue, slave->slave_timeout);
  219. I2Cx->CR2 &= ~I2C_CR2_ITEVTEN|I2C_CR2_ITBUFEN |I2C_CR2_ITERREN;
  220. if(icb->errors)
  221. {
  222. I2Cx->CR1 |= I2C_CR1_STOP;
  223. msg->msg_ridx = -1;
  224. }
  225. if(rc)
  226. msg->msg_ridx = -1;
  227. return msg->msg_ridx;
  228. }
  229. static int checkpin_and_config(STM32_I2CCB *icb)
  230. {
  231. uint32_t sda_port, scl_port;
  232. if (icb->icb_base == I2C1_BASE)
  233. {
  234. sda_port= NUTGPIO_PORTB;
  235. scl_port= NUTGPIO_PORTB;
  236. #if defined (GPIO_AF_I2C1)
  237. if ((icb->sda_pin != 7) && (icb->sda_pin != 9))
  238. return -1;
  239. if ((icb->scl_pin != 6) && (icb->scl_pin != 8))
  240. return -1;
  241. if ((icb->smba_pin != 5) && (icb->smba_pin != -1))
  242. return -1;
  243. GpioPinConfigSet(
  244. sda_port, icb->sda_pin, GPIO_CFG_OUTPUT| GPIO_CFG_PERIPHAL|
  245. GPIO_CFG_MULTIDRIVE| GPIO_CFG_PULLUP | GPIO_CFG_SPEED_FAST);
  246. GpioPinConfigSet(
  247. scl_port ,icb->scl_pin, GPIO_CFG_OUTPUT| GPIO_CFG_PERIPHAL|
  248. GPIO_CFG_MULTIDRIVE| GPIO_CFG_PULLUP | GPIO_CFG_SPEED_FAST);
  249. GPIO_PinAFConfig((GPIO_TypeDef*) sda_port, icb->sda_pin, GPIO_AF_I2C1);
  250. GPIO_PinAFConfig((GPIO_TypeDef*) scl_port, icb->scl_pin, GPIO_AF_I2C1);
  251. #else
  252. if ((icb->sda_pin == 7) && (icb->scl_pin == 6))
  253. AFIO->MAPR &= ~AFIO_MAPR_I2C1_REMAP;
  254. else if ((icb->sda_pin == 9) && (icb->scl_pin == 8))
  255. AFIO->MAPR |= AFIO_MAPR_I2C1_REMAP;
  256. else
  257. return -1;
  258. GpioPinConfigSet(
  259. sda_port, icb->sda_pin, GPIO_CFG_OUTPUT| GPIO_CFG_PERIPHAL|
  260. GPIO_CFG_MULTIDRIVE| GPIO_CFG_PULLUP | GPIO_CFG_SPEED_FAST);
  261. GpioPinConfigSet(
  262. scl_port ,icb->scl_pin, GPIO_CFG_OUTPUT| GPIO_CFG_PERIPHAL|
  263. GPIO_CFG_MULTIDRIVE| GPIO_CFG_PULLUP | GPIO_CFG_SPEED_FAST);
  264. #endif
  265. if (icb->smba_pin == 5)
  266. {
  267. /* TODO: How should SMBA pin be set?*/
  268. GpioPinConfigSet(
  269. NUTGPIO_PORTA, icb->scl_pin, GPIO_CFG_OUTPUT|
  270. GPIO_CFG_PERIPHAL| GPIO_CFG_MULTIDRIVE| GPIO_CFG_PULLUP |
  271. GPIO_CFG_SPEED_FAST);
  272. #if defined(GPIO_AF_I2C1)
  273. GPIO_PinAFConfig(GPIOA, icb->smba_pin, GPIO_AF_I2C1);
  274. #endif
  275. }
  276. RCC->APB1ENR |= RCC_APB1ENR_I2C1EN;
  277. RCC->APB1RSTR |= RCC_APB1RSTR_I2C1RST;
  278. RCC->APB1RSTR &= ~RCC_APB1RSTR_I2C1RST;
  279. }
  280. else if (icb->icb_base == I2C2_BASE)
  281. {
  282. #if defined(GPIO_AF_I2C1)
  283. if ((icb->sda_pin != 0) && (icb->sda_pin != 5)&& (icb->sda_pin != 11))
  284. return -1;
  285. if ((icb->scl_pin != 1) && (icb->scl_pin != 4) && (icb->sda_pin != 10))
  286. return -1;
  287. if ((icb->smba_pin != 12) && (icb->smba_pin != 2) && (icb->smba_pin != 6)
  288. && (icb->smba_pin != -1))
  289. return -1;
  290. if (icb->sda_pin == 11)
  291. sda_port= NUTGPIO_PORTB;
  292. #if defined(GPIOF_BASE)
  293. else if (icb->sda_pin == 0)
  294. sda_port= NUTGPIO_PORTF;
  295. #endif
  296. #if defined(GPIOH_BASE)
  297. else if (icb->sda_pin == 5)
  298. sda_port= NUTGPIO_PORTH;
  299. #endif
  300. else
  301. return -1;
  302. if (icb->scl_pin == 10)
  303. scl_port= NUTGPIO_PORTB;
  304. #if defined(GPIOF_BASE)
  305. else if (icb->scl_pin == 1)
  306. scl_port= NUTGPIO_PORTF;
  307. #endif
  308. #if defined(GPIOH_BASE)
  309. else if (icb->scl_pin == 4)
  310. scl_port= NUTGPIO_PORTH;
  311. #endif
  312. else
  313. return -1;
  314. if (icb->smba_pin == 12)
  315. scl_port= NUTGPIO_PORTB;
  316. #if defined(GPIOF_BASE)
  317. else if (icb->smba_pin == 2)
  318. scl_port= NUTGPIO_PORTF;
  319. #endif
  320. #if defined(GPIOH_BASE)
  321. else if (icb->scl_pin == 4)
  322. scl_port= NUTGPIO_PORTH;
  323. #endif
  324. else
  325. return -1;
  326. if (icb->smba_pin == 12)
  327. scl_port= NUTGPIO_PORTB;
  328. #if defined(GPIOF_BASE)
  329. else if (icb->smba_pin == 2)
  330. scl_port= NUTGPIO_PORTF;
  331. #endif
  332. #if defined(GPIOH_BASE)
  333. else if (icb->smba_pin == 6)
  334. scl_port= NUTGPIO_PORTH;
  335. #endif
  336. /* Fixme: Handle SMBA Pin*/
  337. GPIO_PinAFConfig((GPIO_TypeDef*) sda_port, icb->sda_pin, GPIO_AF_I2C1);
  338. GPIO_PinAFConfig((GPIO_TypeDef*) scl_port, icb->scl_pin, GPIO_AF_I2C1);
  339. #else
  340. if ((icb->sda_pin != 11) || (icb->scl_pin != 10))
  341. return -1;
  342. if ((icb->smba_pin != -1) && (icb->smba_pin != 12))
  343. return -1;
  344. sda_port= NUTGPIO_PORTB;
  345. scl_port= NUTGPIO_PORTB;
  346. #endif
  347. GpioPinConfigSet(
  348. sda_port, icb->sda_pin, GPIO_CFG_OUTPUT| GPIO_CFG_PERIPHAL|
  349. GPIO_CFG_MULTIDRIVE| GPIO_CFG_PULLUP | GPIO_CFG_SPEED_FAST);
  350. GpioPinConfigSet(
  351. scl_port ,icb->scl_pin, GPIO_CFG_OUTPUT| GPIO_CFG_PERIPHAL|
  352. GPIO_CFG_MULTIDRIVE| GPIO_CFG_PULLUP | GPIO_CFG_SPEED_FAST);
  353. if (icb->smba_pin == 12)
  354. {
  355. /* TODO: How should SMBA pin be set?*/
  356. GpioPinConfigSet(
  357. NUTGPIO_PORTA, icb->scl_pin, GPIO_CFG_OUTPUT|
  358. GPIO_CFG_PERIPHAL| GPIO_CFG_MULTIDRIVE| GPIO_CFG_PULLUP |
  359. GPIO_CFG_SPEED_FAST);
  360. #if defined(GPIO_AF_I2C1)
  361. GPIO_PinAFConfig(GPIOA, icb->smba_pin, GPIO_AF_I2C1);
  362. #endif
  363. }
  364. RCC->APB1ENR |= RCC_APB1ENR_I2C2EN;
  365. RCC->APB1RSTR |= RCC_APB1RSTR_I2C2RST;
  366. RCC->APB1RSTR &= ~RCC_APB1RSTR_I2C2RST;
  367. }
  368. #if defined(I2C3_BASE)
  369. else if (icb->icb_base == I2C3_BASE)
  370. {
  371. #if defined(GPIO_AF_I2C1)
  372. if ((icb->sda_pin != 9) && (icb->sda_pin != 8))
  373. return -1;
  374. if ((icb->scl_pin != 8) && (icb->scl_pin != 7))
  375. return -1;
  376. if ((icb->smba_pin != 9) && (icb->smba_pin != 809) && (icb->smba_pin != -1))
  377. return -1;
  378. if (icb->sda_pin == 9)
  379. sda_port= NUTGPIO_PORTC;
  380. else if (icb->sda_pin == 8)
  381. sda_port= NUTGPIO_PORTH;
  382. else
  383. return -1;
  384. if (icb->scl_pin == 8)
  385. scl_port= NUTGPIO_PORTA;
  386. else if (icb->scl_pin == 7)
  387. scl_port= NUTGPIO_PORTH;
  388. else
  389. return -1;
  390. if (icb->smba_pin == 9)
  391. scl_port= NUTGPIO_PORTA;
  392. else if (icb->smba_pin == 809)
  393. scl_port= NUTGPIO_PORTH;
  394. /* Fixme: Handle SMNa Pin*/
  395. GPIO_PinAFConfig((GPIO_TypeDef*) sda_port, icb->sda_pin, GPIO_AF_I2C1);
  396. GPIO_PinAFConfig((GPIO_TypeDef*) scl_port, icb->scl_pin, GPIO_AF_I2C1);
  397. #else
  398. if ((icb->sda_pin != 11) || (icb->scl_pin != 10))
  399. return -1;
  400. if ((icb->smba_pin != -1) || (icb->smba_pin != 12))
  401. return -1;
  402. #endif
  403. GpioPinConfigSet(
  404. sda_port, icb->sda_pin, GPIO_CFG_OUTPUT| GPIO_CFG_PERIPHAL|
  405. GPIO_CFG_MULTIDRIVE| GPIO_CFG_PULLUP | GPIO_CFG_SPEED_FAST);
  406. GpioPinConfigSet(
  407. scl_port ,icb->scl_pin, GPIO_CFG_OUTPUT| GPIO_CFG_PERIPHAL|
  408. GPIO_CFG_MULTIDRIVE| GPIO_CFG_PULLUP | GPIO_CFG_SPEED_FAST);
  409. if (icb->smba_pin == 12)
  410. {
  411. /* TODO: How should SMBA pin be set?*/
  412. GpioPinConfigSet(
  413. NUTGPIO_PORTA, icb->scl_pin, GPIO_CFG_OUTPUT|
  414. GPIO_CFG_PERIPHAL| GPIO_CFG_MULTIDRIVE| GPIO_CFG_PULLUP |
  415. GPIO_CFG_SPEED_FAST);
  416. #if defined(GPIO_AF_I2C1)
  417. GPIO_PinAFConfig(GPIOA, icb->smba_pin, GPIO_AF_I2C1);
  418. #endif
  419. }
  420. RCC->APB1ENR |= RCC_APB1ENR_I2C3EN;
  421. RCC->APB1RSTR |= RCC_APB1RSTR_I2C3RST;
  422. RCC->APB1RSTR &= ~RCC_APB1RSTR_I2C3RST;
  423. }
  424. #endif
  425. else
  426. return -1;
  427. return 0;
  428. }
  429. /*!
  430. * \brief Configure the I2C bus controller (STM32V1 I2C implementation).
  431. *
  432. * This function is called by the platform independent code via the
  433. * NUTI2C_BUS::bus_conf function pointer. Most implementations will
  434. * also call this function during initialization to set the
  435. * default configuration.
  436. *
  437. */
  438. static int I2cBusConf(NUTI2C_BUS *bus)
  439. {
  440. STM32_I2CCB *icb;
  441. uint32_t speed;
  442. I2C_TypeDef *I2Cx;
  443. uint16_t ccr;
  444. uint32_t apbclk = NutClockGet(NUT_HWCLK_PCLK1);
  445. uint16_t frqrange = (uint16_t)(apbclk/1000000);
  446. uint16_t cr1;
  447. /* Check parameters. */
  448. NUTASSERT(bus != NULL);
  449. NUTASSERT(bus->bus_icb != NULL);
  450. icb = (STM32_I2CCB *) bus->bus_icb;
  451. I2Cx = (I2C_TypeDef*) icb->icb_base;
  452. cr1 = I2Cx->CR1;
  453. /* Get requested speed or use the default. */
  454. speed = bus->bus_rate;
  455. if (speed == 0)
  456. {
  457. speed = 100000L;
  458. }
  459. if (speed > 400000)
  460. {
  461. /* Speed out of range */
  462. speed = 400000;
  463. }
  464. /* Configure speed in fast mode */
  465. if( speed > 100000 )
  466. {
  467. /* calculate CCR value */
  468. ccr = (uint16_t)(apbclk/(speed*25));
  469. if( ccr == 0 ) {
  470. /* keep minimum allowed value */
  471. ccr = 0x0001;
  472. }
  473. /* Set DUTY bit and set F/S bit for fast mode */
  474. ccr |= (I2C_CCR_DUTY|I2C_CCR_FS);
  475. /* Set Maximum Rise Time for fast mode */
  476. I2Cx->TRISE = (uint16_t)(((frqrange*300)/1000)+1);
  477. }
  478. else
  479. {
  480. /* Standard mode speed calculate */
  481. ccr = (uint16_t)(apbclk/(speed<<1));
  482. /* Test if CCR value is under 0x4 */
  483. if( ccr < 4 ) {
  484. /* Set minimum allowed value */
  485. ccr = 4;
  486. }
  487. else if ( ccr > I2C_CCR_CCR ) {
  488. ccr = I2C_CCR_CCR;
  489. }
  490. /* Set Maximum Rise Time for standard mode */
  491. I2Cx->TRISE = frqrange+1;
  492. }
  493. /* Write CCR register */
  494. I2Cx->CCR = ccr;
  495. /* Restore the CR1 register */
  496. I2Cx->CR1 = cr1;
  497. return 0;
  498. }
  499. /*!
  500. * \brief Initialize the I2C bus controller (STM32 implementation).
  501. *
  502. * This function is called by the platform independent code via the
  503. * NUTI2C_BUS::bus_init function pointer when the first slave is
  504. * attached to this bus. Note, that NUTI2C_BUS::bus_rate must be zero
  505. * initially. Otherwise no call to this function will take place.
  506. *
  507. * This function must do all required initializations so that the
  508. * driver will be ready to process transfers via NUTI2C_BUS::bus_tran.
  509. *
  510. * This function must return 0 on success or -1 otherwise.
  511. */
  512. static int I2cBusInit(NUTI2C_BUS *bus)
  513. {
  514. I2C_TypeDef *I2Cx;
  515. STM32_I2CCB *icb;
  516. icb = (STM32_I2CCB *) bus->bus_icb;
  517. if (checkpin_and_config(icb))
  518. return -1;
  519. /* Try to configure the bus*/
  520. if (I2cBusConf(bus)) {
  521. return -1;
  522. }
  523. I2Cx = (I2C_TypeDef*) icb->icb_base;
  524. I2Cx->CR2 = I2C_CR2_FREQ;
  525. I2Cx->CR2 |= (NutClockGet(NUT_HWCLK_PCLK1)/1000000);
  526. I2Cx->CR1 |= I2C_CR1_PE;
  527. if (NutRegisterIrqHandler(icb->icb_sig_ev, I2cEventBusIrqHandler, icb))
  528. return -1;
  529. if (NutRegisterIrqHandler(icb->icb_sig_er, I2cErrorBusIrqHandler, icb))
  530. return -1;
  531. NutIrqEnable(icb->icb_sig_ev);
  532. NutIrqEnable(icb->icb_sig_er);
  533. return 0;
  534. }
  535. /*!
  536. * \brief Probe the I2C bus for a specified slave address
  537. * (STM32V1 implementation).
  538. *
  539. * This function is called by the platform independent code via the
  540. * NUTI2C_BUS::bus_probe function pointer. This may happen even if no
  541. * slave device had been attached to the bus and thus without any
  542. * previous call to NUTI2C_BUS::bus_init. However, in that case
  543. * NUTI2C_BUS::bus_configure will have been called.
  544. *
  545. *
  546. */
  547. static int I2cBusProbe(NUTI2C_BUS *bus, int sla)
  548. {
  549. STM32_I2CCB *icb;
  550. I2C_TypeDef *I2Cx;
  551. uint32_t tmo;
  552. int res = -1;
  553. icb = (STM32_I2CCB *) bus->bus_icb;
  554. I2Cx = (I2C_TypeDef*) icb->icb_base;
  555. if ((I2Cx->CR1 & I2C_CR1_PE)== 0)
  556. {
  557. int res;
  558. res = I2cBusInit(bus);
  559. if (res)
  560. return res;
  561. }
  562. I2Cx->CR1 |= I2C_CR1_START;
  563. /* Wait until START has been sent*/
  564. for(tmo = NutGetMillis(); NutGetMillis() - tmo < bus->bus_timeout;)
  565. {
  566. res = !(I2Cx->SR1 & I2C_SR1_SB);
  567. if (res == 0)
  568. break;
  569. }
  570. if (res)
  571. return -1;
  572. I2Cx->DR = sla<<1;
  573. /* Sequence Read SR1, write DR clears SB*/
  574. /* Wait until Adress has been sent*/
  575. for(; NutGetMillis() - tmo < bus->bus_timeout;)
  576. {
  577. res = I2Cx->SR1;
  578. if (res & I2C_SR1_ADDR)
  579. {
  580. (void)I2Cx->SR2;
  581. break;
  582. }
  583. if (res & I2C_SR1_AF)
  584. {
  585. I2Cx->SR1 &= ~I2C_SR1_AF;
  586. break;
  587. }
  588. }
  589. I2Cx->CR1 &= (uint16_t)~I2C_CR1_ACK;
  590. I2Cx->CR1 |= I2C_CR1_STOP;
  591. return (res & I2C_SR1_ADDR)?0:-1;
  592. }
  593. /*
  594. F10:
  595. I2C1 SDA PB7 PB9
  596. I2C1_SCL PB6 PB8
  597. I2C1_SMBA PB5
  598. I2C2_SDA PB11
  599. I2C2_SCL PB10
  600. I2C2_SMBA PB12
  601. F2/4: AF4/ L1: AFIO4
  602. I2C1 SDA PB7 PB9
  603. I2C1_SCL PB6 PB8
  604. I2C1_SMBA PB5
  605. I2C2_SDA PB11 PF0 PH5
  606. I2C2_SCL PB10 PF1 PH4
  607. I2C2_SMBA PB12 PF2 PH6
  608. I2C3_SDA PC9 PH8
  609. I2C3 SCL PA8 PH7
  610. I2C3 SMBA PA9 PH9
  611. */
  612. static STM32_I2CCB i2c1cb = {
  613. I2C1_BASE, /* Register Base */
  614. #if defined(MCU_STM32F1)
  615. #if defined(I2C1_REMAP)
  616. 7, /* SDA Default PB7*/
  617. 6, /* SCL Default PB6*/
  618. #else
  619. 9, /* SDA Remap PB9*/
  620. 8, /* SCL Remap PB8*/
  621. #endif
  622. #else
  623. #if defined (I2C1_SDA_PIN)
  624. I2C1_SDA_PIN, /* SDA Pin number */
  625. #else
  626. -1, /* SDA Pin number */
  627. #endif
  628. #if defined (I2C1_SCL_PIN)
  629. I2C1_SCL_PIN, /* SDA Pin number */
  630. #else
  631. -1, /* SDA Pin number */
  632. #endif
  633. #endif
  634. #if defined (I2C1_SMBA_PIN)
  635. I2C1_SMBA_PIN, /* SDA Pin number */
  636. #else
  637. -1, /* SDA Pin number */
  638. #endif
  639. &sig_TWI1_EV, /* Event signal */
  640. &sig_TWI1_ER, /* Error signal */
  641. NULL, /* icb_msg */
  642. NULL /* icb_queue */
  643. };
  644. static STM32_I2CCB i2c2cb = {
  645. I2C2_BASE, /* Register Base */
  646. #if defined(MCU_STM32F1)
  647. 11, /* SDA Pin number */
  648. 10, /* SCL Pin number */
  649. #else
  650. #if defined (I2C2_SDA_PIN)
  651. I2C2_SDA_PIN, /* SDA Pin number */
  652. #else
  653. -1, /* SDA Pin number */
  654. #endif
  655. #if defined (I2C2_SCL_PIN)
  656. I2C2_SCL_PIN, /* SCL Pin number */
  657. #else
  658. -1, /* SCL Pin number */
  659. #endif
  660. #endif
  661. #if defined (I2C2_SMBA_PIN)
  662. I2C2_SMBA_PIN, /* SMBA Pin number */
  663. #else
  664. -1, /* SMBA Pin number */
  665. #endif
  666. &sig_TWI2_EV, /* Event signal */
  667. &sig_TWI2_ER, /* Error signal */
  668. NULL, /* icb_msg */
  669. NULL /* icb_queue */
  670. };
  671. #if defined(I2C3_BASE)
  672. static STM32_I2CCB i2c3cb = {
  673. I2C3_BASE, /* Register Base */
  674. #if defined (I2C3_SDA_PIN)
  675. I2C3_SDA_PIN, /* SDA Pin number */
  676. #else
  677. -1, /* SDA Pin number */
  678. #endif
  679. #if defined (I2C3_SCL_PIN)
  680. I2C3_SCL_PIN, /* SDA Pin number */
  681. #else
  682. -1, /* SDA Pin number */
  683. #endif
  684. #if defined (I2C3_SMBA_PIN)
  685. I2C3_SMBA_PIN, /* SDA Pin number */
  686. #else
  687. -1, /* SDA Pin number */
  688. #endif
  689. &sig_TWI3_EV, /* Event signal */
  690. &sig_TWI3_ER, /* Error signal */
  691. NULL, /* icb_msg */
  692. NULL /* icb_queue */
  693. };
  694. #endif
  695. NUTI2C_BUS i2cBus1Stm32 = {
  696. &i2c1cb, /* bus_icb */
  697. I2cBusInit, /* bus_init */
  698. I2cBusConf, /* bus_configure */
  699. I2cBusProbe,/* bus_probe */
  700. I2cBusTran, /* bus_transceive */
  701. 100, /* bus_timeout */
  702. 0, /* bus_rate */
  703. 0, /* bus_flags */
  704. NULL /* bus_mutex */
  705. };
  706. NUTI2C_BUS i2cBus2Stm32 = {
  707. &i2c2cb, /* bus_icb */
  708. I2cBusInit, /* bus_init */
  709. I2cBusConf, /* bus_configure */
  710. I2cBusProbe,/* bus_probe */
  711. I2cBusTran, /* bus_transceive */
  712. 100, /* bus_timeout */
  713. 0, /* bus_rate */
  714. 0, /* bus_flags */
  715. NULL /* bus_mutex */
  716. };
  717. #if defined(I2C3_BASE)
  718. NUTI2C_BUS i2cBus3Stm32 = {
  719. &i2c3cb, /* bus_icb */
  720. I2cBusInit, /* bus_init */
  721. I2cBusConf, /* bus_configure */
  722. I2cBusProbe,/* bus_probe */
  723. I2cBusTran, /* bus_transceive */
  724. 100, /* bus_timeout */
  725. 0, /* bus_rate */
  726. 0, /* bus_flags */
  727. NULL /* bus_mutex */
  728. };
  729. #endif