drv_iic.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /******************************************************************//**
  2. * @file drv_iic.c
  3. * @brief Serial API of RT-Thread RTOS for EFM32
  4. * COPYRIGHT (C) 2011, RT-Thread Development Team
  5. * @author onelife
  6. * @version 0.4 beta
  7. **********************************************************************
  8. * @section License
  9. * The license and distribution terms for this file may be found in the file LICENSE in this
  10. * distribution or at http://www.rt-thread.org/license/LICENSE
  11. **********************************************************************
  12. * @section Change Logs
  13. * Date Author Notes
  14. * 2011-01-06 onelife Initial creation for EFM32
  15. * 2011-06-17 onelife Modify init function for efm32lib v2 upgrading
  16. *********************************************************************/
  17. /******************************************************************//**
  18. * @addtogroup efm32
  19. * @{
  20. *********************************************************************/
  21. /* Includes -------------------------------------------------------------------*/
  22. #include "board.h"
  23. #include "hdl_interrupt.h"
  24. #include "drv_iic.h"
  25. #if (defined(RT_USING_IIC0) || defined(RT_USING_IIC1))
  26. /* Private typedef -------------------------------------------------------------*/
  27. /* Private define --------------------------------------------------------------*/
  28. /* Private macro --------------------------------------------------------------*/
  29. #ifdef RT_IIC_DEBUG
  30. #define iic_debug(format,args...) rt_kprintf(format, ##args)
  31. #else
  32. #define iic_debug(format,args...)
  33. #endif
  34. /* Private variables ------------------------------------------------------------*/
  35. #ifdef RT_USING_IIC0
  36. #if (RT_USING_IIC0 > 3)
  37. #error "The location number range of IIC is 0~3"
  38. #endif
  39. struct rt_device iic0_device;
  40. static struct rt_device iic0_rx_index;
  41. #endif
  42. #ifdef RT_USING_IIC1
  43. #if (RT_USING_IIC1 > 3)
  44. #error "The location number range of IIC is 0~3"
  45. #endif
  46. struct rt_device iic1_device;
  47. static struct rt_device iic1_rx_index;
  48. #endif
  49. /* Private function prototypes ---------------------------------------------------*/
  50. /* Private functions ------------------------------------------------------------*/
  51. /******************************************************************//**
  52. * @brief
  53. * Initialize IIC device
  54. *
  55. * @details
  56. *
  57. * @note
  58. *
  59. * @param[in] dev
  60. * Pointer to device descriptor
  61. *
  62. * @return
  63. * Error code
  64. *********************************************************************/
  65. static rt_err_t rt_iic_init (rt_device_t dev)
  66. {
  67. struct efm32_iic_device_t* iic;
  68. iic = (struct efm32_iic_device_t*)dev->user_data;
  69. if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
  70. {
  71. /* Enable IIC */
  72. I2C_Enable(iic->iic_device, true);
  73. iic->rx_buffer = RT_NULL;
  74. iic->state = 0;
  75. dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
  76. }
  77. return RT_EOK;
  78. }
  79. /******************************************************************//**
  80. * @brief
  81. * Open IIC device
  82. *
  83. * @details
  84. *
  85. * @note
  86. *
  87. * @param[in] dev
  88. * Pointer to device descriptor
  89. *
  90. * @param[in] oflag
  91. * Device open flag
  92. *
  93. * @return
  94. * Error code
  95. *********************************************************************/
  96. static rt_err_t rt_iic_open(rt_device_t dev, rt_uint16_t oflag)
  97. {
  98. RT_ASSERT(dev != RT_NULL);
  99. iic_debug("IIC: Open with flag %x\n", oflag);
  100. return RT_EOK;
  101. }
  102. /******************************************************************//**
  103. * @brief
  104. * Close IIC device
  105. *
  106. * @details
  107. *
  108. * @note
  109. *
  110. * @param[in] dev
  111. * Pointer to device descriptor
  112. *
  113. * @return
  114. * Error code
  115. *********************************************************************/
  116. static rt_err_t rt_iic_close(rt_device_t dev)
  117. {
  118. struct efm32_iic_device_t *iic;
  119. iic = (struct efm32_iic_device_t *)(dev->user_data);
  120. rt_free(iic->rx_buffer->data_ptr);
  121. rt_free(iic->rx_buffer);
  122. iic->rx_buffer = RT_NULL;
  123. return RT_EOK;
  124. }
  125. /******************************************************************//**
  126. * @brief
  127. * Read from IIC device
  128. *
  129. * @details
  130. *
  131. * @note
  132. *
  133. * @param[in] dev
  134. * Pointer to device descriptor
  135. *
  136. * @param[in] pos
  137. * Offset
  138. *
  139. * @param[in] buffer
  140. * Poniter to the buffer
  141. *
  142. * @param[in] size
  143. * Buffer size in byte
  144. *
  145. * @return
  146. * Error code
  147. *********************************************************************/
  148. static rt_size_t rt_iic_read (
  149. rt_device_t dev,
  150. rt_off_t pos,
  151. void* buffer,
  152. rt_size_t size)
  153. {
  154. rt_err_t err_code;
  155. rt_size_t read_size;
  156. struct efm32_iic_device_t* iic;
  157. I2C_TransferSeq_TypeDef seq;
  158. I2C_TransferReturn_TypeDef ret;
  159. rt_uint8_t data[1];
  160. if (!size)
  161. {
  162. return 0;
  163. }
  164. err_code = RT_EOK;
  165. read_size = 0;
  166. iic = (struct efm32_iic_device_t*)dev->user_data;
  167. data[0] = (rt_uint8_t)(pos & 0x000000FF);
  168. if (iic->state & IIC_STATE_MASTER)
  169. {
  170. seq.addr = iic->slave_address;
  171. seq.flags = I2C_FLAG_WRITE_READ;
  172. /* Select register to be read */
  173. seq.buf[0].data = data;
  174. seq.buf[0].len = 1;
  175. /* Select location/length of data to be read */
  176. seq.buf[1].data = (rt_uint8_t *)buffer;
  177. seq.buf[1].len = size;
  178. /* Do a polled transfer */
  179. ret = I2C_TransferInit(iic->iic_device, &seq);
  180. while (ret == i2cTransferInProgress)
  181. {
  182. ret = I2C_Transfer(iic->iic_device);
  183. }
  184. if (ret != i2cTransferDone)
  185. {
  186. iic_debug("IIC0 read error: %x\n", ret);
  187. iic_debug("IIC0 read address: %x\n", seq.addr);
  188. iic_debug("IIC0 read data0: %x -> %x\n", seq.buf[0].data, *seq.buf[0].data);
  189. iic_debug("IIC0 read len0: %x\n", seq.buf[0].len);
  190. iic_debug("IIC0 read data1: %x -> %x\n", seq.buf[1].data, *seq.buf[1].data);
  191. iic_debug("IIC0 read len1: %x\n", seq.buf[1].len);
  192. err_code = (rt_err_t)ret;
  193. }
  194. else
  195. {
  196. read_size = size;
  197. iic_debug("IIC0 read size: %d\n", read_size);
  198. }
  199. }
  200. else
  201. {
  202. rt_uint8_t* ptr;
  203. ptr = buffer;
  204. /* interrupt mode Rx */
  205. while (size)
  206. {
  207. rt_base_t level;
  208. struct efm32_iic_int_mode_t *int_rx;
  209. int_rx = iic->rx_buffer;
  210. /* disable interrupt */
  211. level = rt_hw_interrupt_disable();
  212. if (int_rx->read_index != int_rx->save_index)
  213. {
  214. /* read a character */
  215. *ptr++ = int_rx->data_ptr[int_rx->read_index];
  216. size--;
  217. /* move to next position */
  218. int_rx->read_index ++;
  219. if (int_rx->read_index >= IIC_RX_BUFFER_SIZE)
  220. {
  221. int_rx->read_index = 0;
  222. }
  223. }
  224. else
  225. {
  226. /* set error code */
  227. err_code = -RT_EEMPTY;
  228. /* enable interrupt */
  229. rt_hw_interrupt_enable(level);
  230. break;
  231. }
  232. /* enable interrupt */
  233. rt_hw_interrupt_enable(level);
  234. }
  235. read_size = (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  236. iic_debug("IIC0 slave read size: %d\n", read_size);
  237. }
  238. /* set error code */
  239. rt_set_errno(err_code);
  240. return read_size;
  241. }
  242. /******************************************************************//**
  243. * @brief
  244. * Write to IIC device
  245. *
  246. * @details
  247. *
  248. * @note
  249. *
  250. * @param[in] dev
  251. * Pointer to device descriptor
  252. *
  253. * @param[in] pos
  254. * Offset
  255. *
  256. * @param[in] buffer
  257. * Poniter to the buffer
  258. *
  259. * @param[in] size
  260. * Buffer size in byte
  261. *
  262. * @return
  263. * Error code
  264. *********************************************************************/
  265. static rt_size_t rt_iic_write (
  266. rt_device_t dev,
  267. rt_off_t pos,
  268. const void* buffer,
  269. rt_size_t size)
  270. {
  271. rt_err_t err_code;
  272. rt_size_t write_size;
  273. struct efm32_iic_device_t* iic;
  274. I2C_TransferSeq_TypeDef seq;
  275. I2C_TransferReturn_TypeDef ret;
  276. //rt_uint8_t data[1];
  277. if (!size)
  278. {
  279. return 0;
  280. }
  281. err_code = RT_EOK;
  282. write_size = 0;
  283. iic = (struct efm32_iic_device_t*)dev->user_data;
  284. //data[0] = (rt_uint8_t)(pos & 0x000000FF);
  285. if (iic->state & IIC_STATE_MASTER)
  286. {
  287. seq.addr = iic->slave_address;
  288. if (pos != (rt_off_t)(-1))
  289. {
  290. seq.flags = I2C_FLAG_WRITE_WRITE;
  291. /* Select register to be write */
  292. seq.buf[0].data = (rt_uint8_t *)(pos & 0x000000FF);
  293. seq.buf[0].len = 1;
  294. /* Select location/length of data to be write */
  295. seq.buf[1].data = (rt_uint8_t *)buffer;
  296. seq.buf[1].len = size;
  297. }
  298. else
  299. {
  300. seq.flags = I2C_FLAG_WRITE;
  301. /* Select location/length of data to be write */
  302. seq.buf[0].data = (rt_uint8_t *)buffer;
  303. seq.buf[0].len = size;
  304. }
  305. }
  306. else
  307. {
  308. // TODO: Slave mode TX
  309. }
  310. /* Do a polled transfer */
  311. ret = I2C_TransferInit(iic->iic_device, &seq);
  312. while (ret == i2cTransferInProgress)
  313. {
  314. ret = I2C_Transfer(iic->iic_device);
  315. }
  316. if (ret != i2cTransferDone)
  317. {
  318. err_code = (rt_err_t)ret;
  319. }
  320. else
  321. {
  322. write_size = size;
  323. }
  324. /* set error code */
  325. rt_set_errno(err_code);
  326. return write_size;
  327. }
  328. /******************************************************************//**
  329. * @brief
  330. * Configure IIC device
  331. *
  332. * @details
  333. *
  334. * @note
  335. *
  336. * @param[in] dev
  337. * Pointer to device descriptor
  338. *
  339. * @param[in] cmd
  340. * IIC control command
  341. *
  342. * @param[in] args
  343. * Arguments
  344. *
  345. * @return
  346. * Error code
  347. *********************************************************************/
  348. static rt_err_t rt_iic_control (
  349. rt_device_t dev,
  350. rt_uint8_t cmd,
  351. void *args)
  352. {
  353. RT_ASSERT(dev != RT_NULL);
  354. struct efm32_iic_device_t *iic;
  355. iic = (struct efm32_iic_device_t*)dev->user_data;
  356. switch (cmd)
  357. {
  358. case RT_DEVICE_CTRL_SUSPEND:
  359. /* suspend device */
  360. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  361. I2C_Enable(iic->iic_device, false);
  362. break;
  363. case RT_DEVICE_CTRL_RESUME:
  364. /* resume device */
  365. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  366. I2C_Enable(iic->iic_device, true);
  367. break;
  368. case RT_DEVICE_CTRL_IIC_SETTING:
  369. {
  370. /* change device setting */
  371. struct efm32_iic_control_t *control;
  372. control = (struct efm32_iic_control_t *)args;
  373. iic->state = control->config & (IIC_STATE_MASTER | IIC_STATE_BROADCAST);
  374. iic->master_address = control->master_address << 1;
  375. iic->slave_address = control->slave_address << 1;
  376. if (!(iic->state & IIC_STATE_MASTER))
  377. {
  378. if (iic->rx_buffer == RT_NULL)
  379. {
  380. iic->rx_buffer = rt_malloc(sizeof(struct efm32_iic_int_mode_t));
  381. if (iic->rx_buffer == RT_NULL)
  382. {
  383. iic_debug("no memory for IIC RX structure\n");
  384. return -RT_ENOMEM;
  385. }
  386. /* Allocate RX buffer */
  387. if ((iic->rx_buffer->data_ptr = \
  388. rt_malloc(IIC_RX_BUFFER_SIZE)) == RT_NULL)
  389. {
  390. iic_debug("no memory for IIC RX buffer\n");
  391. rt_free(iic->rx_buffer);
  392. return -RT_ENOMEM;
  393. }
  394. rt_memset(iic->rx_buffer->data_ptr, 0, IIC_RX_BUFFER_SIZE);
  395. iic->rx_buffer->data_size = IIC_RX_BUFFER_SIZE;
  396. iic->rx_buffer->read_index = 0;
  397. iic->rx_buffer->save_index = 0;
  398. }
  399. /* Enable slave mode */
  400. I2C_SlaveAddressSet(iic->iic_device, iic->slave_address);
  401. I2C_SlaveAddressMaskSet(iic->iic_device, 0xFF);
  402. iic->iic_device->CTRL |= I2C_CTRL_SLAVE | I2C_CTRL_AUTOACK | I2C_CTRL_AUTOSN;
  403. /* Enable interrupts */
  404. I2C_IntEnable(iic->iic_device, I2C_IEN_ADDR | I2C_IEN_RXDATAV | I2C_IEN_SSTOP);
  405. I2C_IntClear(iic->iic_device, _I2C_IFC_MASK);
  406. /* Enable I2Cn interrupt vector in NVIC */
  407. #ifdef RT_USING_IIC0
  408. if (dev == &iic0_device)
  409. {
  410. NVIC_ClearPendingIRQ(I2C0_IRQn);
  411. NVIC_SetPriority(I2C0_IRQn, EFM32_IRQ_PRI_DEFAULT);
  412. NVIC_EnableIRQ(I2C0_IRQn);
  413. }
  414. #endif
  415. #ifdef RT_USING_IIC1
  416. if (dev == &iic1_device)
  417. {
  418. NVIC_ClearPendingIRQ(I2C1_IRQn);
  419. NVIC_SetPriority(I2C1_IRQn, EFM32_IRQ_PRI_DEFAULT);
  420. NVIC_EnableIRQ(I2C1_IRQn);
  421. }
  422. #endif
  423. }
  424. }
  425. break;
  426. }
  427. return RT_EOK;
  428. }
  429. /******************************************************************//**
  430. * @brief
  431. * Register IIC device
  432. *
  433. * @details
  434. *
  435. * @note
  436. *
  437. * @param[in] device
  438. * Pointer to device descriptor
  439. *
  440. * @param[in] name
  441. * Device name
  442. *
  443. * @param[in] flag
  444. * Configuration flags
  445. *
  446. * @param[in] iic
  447. * Pointer to IIC device descriptor
  448. *
  449. * @return
  450. * Error code
  451. *********************************************************************/
  452. rt_err_t rt_hw_iic_register(
  453. rt_device_t device,
  454. const char *name,
  455. rt_uint32_t flag,
  456. struct efm32_iic_device_t *iic)
  457. {
  458. RT_ASSERT(device != RT_NULL);
  459. if ((flag & RT_DEVICE_FLAG_DMA_TX) || (flag & RT_DEVICE_FLAG_DMA_RX) ||
  460. (flag & RT_DEVICE_FLAG_INT_TX))
  461. {
  462. RT_ASSERT(0);
  463. }
  464. device->type = RT_Device_Class_I2C;
  465. device->rx_indicate = RT_NULL;
  466. device->tx_complete = RT_NULL;
  467. device->init = rt_iic_init;
  468. device->open = rt_iic_open;
  469. device->close = rt_iic_close;
  470. device->read = rt_iic_read;
  471. device->write = rt_iic_write;
  472. device->control = rt_iic_control;
  473. device->user_data = iic;
  474. /* register a character device */
  475. return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | flag);
  476. }
  477. /******************************************************************//**
  478. * @brief
  479. * IIC slave mode RX data valid interrupt handler
  480. *
  481. * @details
  482. *
  483. * @note
  484. *
  485. * @param[in] dev
  486. * Pointer to device descriptor
  487. *********************************************************************/
  488. static void rt_hw_iic_slave_isr(rt_device_t dev)
  489. {
  490. struct efm32_iic_device_t *iic;
  491. struct efm32_iic_int_mode_t *int_rx;
  492. rt_uint32_t status;
  493. volatile rt_uint32_t temp;
  494. /* interrupt mode receive */
  495. RT_ASSERT(dev->flag & RT_DEVICE_FLAG_INT_RX);
  496. iic = (struct efm32_iic_device_t*)dev->user_data;
  497. int_rx = iic->rx_buffer;
  498. status = iic->iic_device->IF;
  499. if (status & I2C_IF_ADDR)
  500. {
  501. /* Address Match */
  502. /* Indicating that reception is started */
  503. temp = iic->iic_device->RXDATA & 0xFFUL;
  504. if ((temp != 0x00) || (iic->state & IIC_STATE_BROADCAST))
  505. {
  506. iic->state |= IIC_STATE_RX_BUSY;
  507. }
  508. }
  509. else if (status & I2C_IF_RXDATAV)
  510. {
  511. if (iic->state & IIC_STATE_RX_BUSY)
  512. {
  513. rt_base_t level;
  514. /* disable interrupt */
  515. level = rt_hw_interrupt_disable();
  516. /* save character */
  517. int_rx->data_ptr[int_rx->save_index] = \
  518. (rt_uint8_t)(iic->iic_device->RXDATA & 0xFFUL);
  519. int_rx->save_index ++;
  520. if (int_rx->save_index >= IIC_RX_BUFFER_SIZE)
  521. int_rx->save_index = 0;
  522. /* if the next position is read index, discard this 'read char' */
  523. if (int_rx->save_index == int_rx->read_index)
  524. {
  525. int_rx->read_index ++;
  526. if (int_rx->read_index >= IIC_RX_BUFFER_SIZE)
  527. {
  528. int_rx->read_index = 0;
  529. }
  530. }
  531. /* enable interrupt */
  532. rt_hw_interrupt_enable(level);
  533. }
  534. else
  535. {
  536. temp = iic->iic_device->RXDATA;
  537. }
  538. }
  539. if(status & I2C_IF_SSTOP)
  540. {
  541. /* Stop received, reception is ended */
  542. iic->state &= ~(rt_uint8_t)IIC_STATE_RX_BUSY;
  543. }
  544. }
  545. /******************************************************************//**
  546. * @brief
  547. * Initialize the specified IIC unit
  548. *
  549. * @details
  550. *
  551. * @note
  552. *
  553. * @param[in] unitNumber
  554. * Unit number
  555. *
  556. * @param[in] location
  557. * Pin location number
  558. *********************************************************************/
  559. static struct efm32_iic_device_t *rt_hw_iic_unit_init(
  560. rt_device_t device,
  561. rt_uint8_t unitNumber,
  562. rt_uint8_t location)
  563. {
  564. struct efm32_iic_device_t *iic;
  565. CMU_Clock_TypeDef iicClock;
  566. I2C_Init_TypeDef init = I2C_INIT_DEFAULT;
  567. efm32_irq_hook_init_t hook;
  568. do
  569. {
  570. /* Allocate device */
  571. iic = rt_malloc(sizeof(struct efm32_iic_device_t));
  572. if (iic == RT_NULL)
  573. {
  574. iic_debug("no memory for IIC%d driver\n", unitNumber);
  575. break;
  576. }
  577. iic->state |= IIC_STATE_MASTER;
  578. iic->master_address = 0x0000;
  579. iic->slave_address = 0x0000;
  580. iic->rx_buffer = RT_NULL;
  581. /* Initialization */
  582. if (unitNumber >= I2C_COUNT)
  583. {
  584. break;
  585. }
  586. switch (unitNumber)
  587. {
  588. case 0:
  589. iic->iic_device = I2C0;
  590. iicClock = (CMU_Clock_TypeDef)cmuClock_I2C0;
  591. break;
  592. #if (I2C_COUNT > 1)
  593. case 1:
  594. iic->iic_device = I2C1;
  595. iicClock = (CMU_Clock_TypeDef)cmuClock_I2C1;
  596. break;
  597. #endif
  598. default:
  599. break;
  600. }
  601. /* Enabling clock */
  602. CMU_ClockEnable(iicClock, true);
  603. /* Reset */
  604. I2C_Reset(iic->iic_device);
  605. /* Config GPIO */
  606. GPIO_PinModeSet(
  607. (GPIO_Port_TypeDef)AF_PORT(AF_I2C_SCL(unitNumber), location),
  608. AF_PIN(AF_I2C_SCL(unitNumber), location),
  609. gpioModeWiredAndPullUpFilter,
  610. 1);
  611. GPIO_PinModeSet(
  612. (GPIO_Port_TypeDef)AF_PORT(AF_I2C_SDA(unitNumber), location),
  613. AF_PIN(AF_I2C_SDA(unitNumber), location),
  614. gpioModeWiredAndPullUpFilter,
  615. 1);
  616. hook.type = efm32_irq_type_iic;
  617. hook.unit = unitNumber;
  618. hook.cbFunc = rt_hw_iic_slave_isr;
  619. hook.userPtr = device;
  620. efm32_irq_hook_register(&hook);
  621. /* Enable SDZ and SCL pins and set location */
  622. iic->iic_device->ROUTE = I2C_ROUTE_SDAPEN | I2C_ROUTE_SCLPEN | \
  623. (location << _I2C_ROUTE_LOCATION_SHIFT);
  624. /* Initializing IIC */
  625. init.enable = false;
  626. I2C_Init(iic->iic_device, &init);
  627. /* Abort current TX data and clear TX buffers */
  628. iic->iic_device->CMD = I2C_CMD_ABORT | I2C_CMD_CLEARPC | I2C_CMD_CLEARTX;
  629. return iic;
  630. } while(0);
  631. if (iic)
  632. {
  633. rt_free(iic);
  634. }
  635. rt_kprintf("IIC: Init failed!\n");
  636. return RT_NULL;
  637. }
  638. /******************************************************************//**
  639. * @brief
  640. * Initialize all IIC module related hardware and register IIC device to kernel
  641. *
  642. * @details
  643. *
  644. * @note
  645. *********************************************************************/
  646. void rt_hw_iic_init(void)
  647. {
  648. struct efm32_iic_device_t *iic;
  649. rt_uint32_t flag;
  650. flag = RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX;
  651. /* register iic0 */
  652. #ifdef RT_USING_IIC0
  653. if ((iic = rt_hw_iic_unit_init(&iic0_device, 0, RT_USING_IIC0)) != RT_NULL)
  654. {
  655. rt_hw_iic_register(&iic0_device, RT_IIC0_NAME, flag, iic);
  656. }
  657. #endif
  658. /* register iic1 */
  659. #ifdef RT_USING_IIC1
  660. if ((iic = rt_hw_iic_unit_init(&iic1_device, 1, RT_USING_IIC1)) != RT_NULL)
  661. {
  662. rt_hw_iic_register(&iic1_device, RT_IIC1_NAME, flag, iic);
  663. }
  664. #endif
  665. }
  666. #endif
  667. /******************************************************************//**
  668. * @}
  669. *********************************************************************/