i2c_common.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /**
  2. * \file
  3. *
  4. * \brief I2C Common Driver for SAMB
  5. *
  6. * Copyright (c) 2015-2016 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * 3. The name of Atmel may not be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * 4. This software may only be redistributed and used in connection with an
  26. * Atmel microcontroller product.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * \asf_license_stop
  41. *
  42. */
  43. /*
  44. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  45. */
  46. #ifndef I2C_COMMON_H_INCLUDED
  47. #define I2C_COMMON_H_INCLUDED
  48. #include <compiler.h>
  49. #include <system_sam_b.h>
  50. #include <gpio.h>
  51. #ifdef __cplusplus
  52. extern "C" {
  53. #endif
  54. /**
  55. * \if (I2C_MASTER_MODE && I2C_SLAVE_MODE)
  56. * \defgroup asfdoc_samb_i2c_group SAM I2C Driver (I2C)
  57. * \elseif I2C_MASTER_MODE
  58. * \defgroup asfdoc_samb_i2c_group SAM I2C Master Mode Driver (I2C)
  59. * \elseif I2C_SLAVE_MODE
  60. * \defgroup asfdoc_samb_i2c_group SAM I2C Slave Mode Driver (I2C)
  61. * \endif
  62. *
  63. * This driver for Atmel&reg; | SMART SAM devices provides an interface for the configuration
  64. * and management of the device's I<SUP>2</SUP>C module, for the transfer
  65. * of data via an I<SUP>2</SUP>C bus. The following driver API modes are covered
  66. * by this manual:
  67. *
  68. * \if I2C_MASTER_MODE
  69. * - Master Mode Polled APIs
  70. * \endif
  71. * \if I2C_MASTER_CALLBACK_MODE
  72. * - Master Mode Callback APIs
  73. * \endif
  74. * \if I2C_SLAVE_MODE
  75. * - Slave Mode Polled APIs
  76. * \endif
  77. * \if I2C_SLAVE_CALLBACK_MODE
  78. * - Slave Mode Callback APIs
  79. * \endif
  80. *
  81. * The following peripheral is used by this module:
  82. * - Serial Communication Interface
  83. *
  84. * The following devices can use this module:
  85. * - Atmel | SMART SAM B11
  86. *
  87. * The outline of this documentation is as follows:
  88. * - \ref asfdoc_samb_i2c_prerequisites
  89. * - \ref asfdoc_samb_i2c_overview
  90. * - \ref asfdoc_samb_i2c_special_considerations
  91. * - \ref asfdoc_samb_i2c_extra
  92. * - \ref asfdoc_samb_i2c_examples
  93. * - \ref asfdoc_samb_i2c_api_overview
  94. *
  95. * \section asfdoc_samb_i2c_prerequisites Prerequisites
  96. * There are no prerequisites.
  97. *
  98. * \section asfdoc_samb_i2c_overview Module Overview
  99. *
  100. * The outline of this section is as follows:
  101. * - \ref asfdoc_samb_i2c_functional_desc
  102. * - \ref asfdoc_samb_i2c_bus_topology
  103. * - \ref asfdoc_samb_i2c_transactions
  104. * - \ref asfdoc_samb_i2c_multi_master
  105. * - \ref asfdoc_samb_i2c_bus_states
  106. *
  107. * \subsection asfdoc_samb_i2c_functional_desc Functional Description
  108. * The I<SUP>2</SUP>C provides a simple two-wire bidirectional bus consisting of a
  109. * wired-AND type serial clock line (SCL) and a wired-AND type serial data line
  110. * (SDA).
  111. *
  112. * The I<SUP>2</SUP>C bus provides a simple, but efficient method of interconnecting
  113. * multiple master and slave devices. An arbitration mechanism is provided for
  114. * resolving bus ownership between masters, as only one master device may own
  115. * the bus at any given time. The arbitration mechanism relies on the wired-AND
  116. * connections to avoid bus drivers short-circuiting.
  117. *
  118. * A unique address is assigned to all slave devices connected to the bus. A
  119. * device can contain both master and slave logic, and can emulate multiple
  120. * slave devices by responding to more than one address.
  121. *
  122. * \subsection asfdoc_samb_i2c_bus_topology Bus Topology
  123. * The I<SUP>2</SUP>C bus topology is illustrated in
  124. * \ref asfdoc_samb_i2c_bus_topology_figure "the figure below". The pull-up
  125. * resistors (Rs) will provide a high level on the bus lines when none of the
  126. * I<SUP>2</SUP>C devices are driving the bus. These are optional, and can be
  127. * replaced with a constant current source.
  128. *
  129. * \anchor asfdoc_samb_i2c_bus_topology_figure
  130. * \image html bus_topology.svg "I2C Bus Topology" Width=100%
  131. *
  132. * \subsection asfdoc_samb_i2c_transactions Transactions
  133. * The I<SUP>2</SUP>C standard defines three fundamental transaction formats:
  134. * - Master Write
  135. * - The master transmits data packets to the slave after addressing it
  136. * - Master Read
  137. * - The slave transmits data packets to the master after being addressed
  138. * - Combined Read/Write
  139. * - A combined transaction consists of several write and read transactions
  140. *
  141. * A data transfer starts with the master issuing a \b Start condition on the
  142. * bus, followed by the address of the slave together with a bit to indicate
  143. * whether the master wants to read from or write to the slave.
  144. * The addressed slave must respond to this by sending an \b ACK back to the
  145. * master.
  146. *
  147. * After this, data packets are sent from the master or slave, according to the
  148. * read/write bit. Each packet must be acknowledged (ACK) or not
  149. * acknowledged (NACK) by the receiver.
  150. *
  151. * If a slave responds with a NACK, the master must assume that the slave
  152. * cannot receive any more data and cancel the write operation.
  153. *
  154. * The master completes a transaction by issuing a \b Stop condition.
  155. *
  156. * A master can issue multiple \b Start conditions during a transaction; this
  157. * is then called a \b Repeated \b Start condition.
  158. *
  159. * \subsubsection asfdoc_samb_i2c_address_packets Address Packets
  160. * The slave address consists of seven bits. The 8<SUP>th</SUP> bit in the transfer
  161. * determines the data direction (read or write). An address packet always
  162. * succeeds a \b Start or \b Repeated \b Start condition. The 8<SUP>th</SUP> bit is handled
  163. * in the driver, and the user will only have to provide the 7-bit address.
  164. *
  165. * \subsubsection asfdoc_samb_i2c_data_packets Data Packets
  166. * Data packets are nine bits long, consisting of one 8-bit data byte, and an
  167. * acknowledgement bit. Data packets follow either an address packet or another
  168. * data packet on the bus.
  169. *
  170. * \subsubsection asfdoc_samb_i2c_trans_examples Transaction Examples
  171. * The gray bits in the following examples are sent from master to slave, and
  172. * the white bits are sent from slave to master.
  173. * Example of a read transaction is shown in
  174. * \ref asfdoc_samb_i2c_trans_examples_i2c_read "the figure below". Here, the
  175. * master first issues a \b Start condition and gets ownership of the bus. An
  176. * address packet with the direction flag set to read is then sent and
  177. * acknowledged by the slave. Then the slave sends one data packet which is
  178. * acknowledged by the master. The slave sends another packet, which is not
  179. * acknowledged by the master and indicates that the master will terminate the
  180. * transaction. In the end, the transaction is terminated by the master issuing
  181. * a \b Stop condition.
  182. *
  183. * \anchor asfdoc_samb_i2c_trans_examples_i2c_read
  184. * \image html i2c_read.svg "I2C Packet Read" Width=100%
  185. *
  186. * Example of a write transaction is shown in
  187. * \ref asfdoc_samb_i2c_trans_examples_i2c_write "the figure below". Here, the
  188. * master first issues a \b Start condition and gets ownership of the bus. An
  189. * address packet with the dir flag set to write is then sent and acknowledged
  190. * by the slave. Then the master sends two data packets, each acknowledged by
  191. * the slave. In the end, the transaction is terminated by the master issuing
  192. * a \b Stop condition.
  193. *
  194. * \anchor asfdoc_samb_i2c_trans_examples_i2c_write
  195. * \image html i2c_write.svg "I2C Packet Write" Width=100%
  196. *
  197. * \subsubsection asfdoc_samb_i2c_repeated_start Repeated Start
  198. * To issue a \b Repeated \b Start, the functions postfixed \c _no_stop must be
  199. * used.
  200. * These functions will not send a \b Stop condition when the transfer is done,
  201. * thus the next transfer will start with a \b Repeated \b Start. To end the
  202. * transaction, the functions without the \c _no_stop postfix must be used
  203. * for the last read/write.
  204. *
  205. * \subsection asfdoc_samb_i2c_multi_master Multi Master
  206. * In a multi master environment, arbitration of the bus is important, as only
  207. * one master can own the bus at any point.
  208. *
  209. * \subsubsection asfdoc_samb_i2c_arbitration Arbitration
  210. *
  211. * \par Clock stretching
  212. * The serial clock line is always driven by a master device. However, all
  213. * devices connected to the bus are allowed stretch the low period of the clock
  214. * to slow down the overall clock frequency or to insert wait states while
  215. * processing data.
  216. * Both master and slave can randomly stretch the clock, which will force the
  217. * other device into a wait-state until the clock line goes high again.
  218. *
  219. * \par Arbitration on the data line
  220. * If two masters start transmitting at the same time, they will both transmit
  221. * until one master detects that the other master is pulling the data line low.
  222. * When this is detected, the master not pulling the line low, will stop the
  223. * transmission and wait until the bus is idle.
  224. * As it is the master trying to contact the slave with the lowest address that
  225. * will get the bus ownership, this will create an arbitration scheme always
  226. * prioritizing the slaves with the lowest address in case of a bus collision.
  227. *
  228. * \subsubsection asfdoc_samb_i2c_clock_sync Clock Synchronization
  229. * In situations where more than one master is trying to control the bus clock
  230. * line at the same time, a clock synchronization algorithm based on the same
  231. * principles used for clock stretching is necessary.
  232. *
  233. *
  234. * \subsection asfdoc_samb_i2c_bus_states Bus States
  235. * As the I<SUP>2</SUP>C bus is limited to one transaction at the time,
  236. * a master that wants to perform a bus transaction must wait until the bus is
  237. * free.
  238. * Because of this, it is necessary for all masters in a multi-master system to
  239. * know the current status of the bus to be able to avoid conflicts and to
  240. * ensure data integrity.
  241. * \li \b IDLE No activity on the bus (between a \b Stop and a new \b Start
  242. * condition)
  243. * \li \b BUSY If another master is driving the bus
  244. *
  245. * \section asfdoc_samb_i2c_special_considerations Special Considerations
  246. * There are no special considerations related to this driver.
  247. *
  248. *
  249. * \section asfdoc_samb_i2c_extra Extra Information
  250. * For extra information, see \ref asfdoc_samb_i2c_extra_info_page.
  251. * This includes:
  252. * - \ref asfdoc_samb_i2c_acronyms
  253. * - \ref asfdoc_samb_i2c_extra_dependencies
  254. * - \ref asfdoc_samb_i2c_extra_errata
  255. * - \ref asfdoc_samb_i2c_extra_history
  256. *
  257. * \section asfdoc_samb_i2c_examples Examples
  258. *
  259. * For a list of examples related to this driver, see
  260. * \ref asfdoc_samb_i2c_exqsg.
  261. *
  262. * \section asfdoc_samb_i2c_api_overview API Overview
  263. * @{
  264. */
  265. /** \brief Transfer direction
  266. *
  267. * For master: transfer direction or setting direction bit in address.
  268. * For slave: direction of request from master.
  269. */
  270. enum i2c_transfer_direction {
  271. /** Master write operation is in progress */
  272. I2C_TRANSFER_WRITE = 0,
  273. /** Master read operation is in progress */
  274. I2C_TRANSFER_READ = 1,
  275. };
  276. /**
  277. * \brief I2C module clock input
  278. *
  279. * I2C module clock.
  280. *
  281. */
  282. enum i2c_clock_input {
  283. /** source from clock input 0: 26MHz */
  284. I2C_CLK_INPUT_0 = 0,
  285. /** source from clock input 1: 13MHz */
  286. I2C_CLK_INPUT_1,
  287. /** source from clock input 2: 6.5MHz */
  288. I2C_CLK_INPUT_2,
  289. /** source from clock input 3: 3MHz */
  290. I2C_CLK_INPUT_3,
  291. };
  292. /**
  293. * \brief Wait for idle
  294. *
  295. * Wait for hardware module to sync
  296. *
  297. * \param[in] i2c_module Pointer to software module structure
  298. */
  299. static inline void i2c_wait_for_idle(I2c *const i2c_module)
  300. {
  301. while (i2c_module->I2C_STATUS.bit.I2C_ACTIVE) {
  302. /* Wait for I2C module to sync. */
  303. }
  304. }
  305. /**
  306. * \brief Enable driver instance
  307. *
  308. * This function enable driver instance
  309. *
  310. * \param[in,out] module Pointer to the driver instance to enable
  311. *
  312. */
  313. static inline void i2c_enable(I2c *const i2c_module)
  314. {
  315. /* Wait for module to sync. */
  316. i2c_wait_for_idle(i2c_module);
  317. /* Enable module. */
  318. i2c_module->I2C_MODULE_ENABLE.reg = (1 << I2C_MODULE_ENABLE_ENABLE_Pos);
  319. }
  320. /**
  321. * \brief Disable driver instance
  322. *
  323. * This function disable driver instance
  324. *
  325. * \param[in,out] i2c_module Pointer to the driver instance to disable
  326. *
  327. */
  328. static inline void i2c_disable(I2c *const i2c_module)
  329. {
  330. i2c_wait_for_idle(i2c_module);
  331. i2c_module->I2C_MODULE_ENABLE.reg = 0;
  332. }
  333. /**
  334. * \brief Flush the contents of both the Tx and Rx FIFOs
  335. *
  336. * This function flushes the contents of both the Tx and Rx FIFOs.
  337. *
  338. * \param[in,out] i2c_module Pointer to the driver instance
  339. *
  340. */
  341. static inline void i2c_slave_flush_fifo(I2c *const i2c_module)
  342. {
  343. i2c_wait_for_idle(i2c_module);
  344. i2c_module->I2C_FLUSH.reg = 1;
  345. }
  346. /**
  347. * \brief Enable or Disable slave rx interrupt
  348. *
  349. * This function enables or disables slave rx interrupt.
  350. *
  351. * \param[in,out] i2c_module Pointer to the driver
  352. * \param[in] enable Enable or Disable
  353. *
  354. */
  355. static inline void i2c_slave_rx_interrupt(I2c *const i2c_module, bool enable)
  356. {
  357. if (enable) {
  358. i2c_module->RX_INTERRUPT_MASK.bit.RX_FIFO_NOT_EMPTY_MASK = 1;
  359. } else {
  360. i2c_module->RX_INTERRUPT_MASK.bit.RX_FIFO_NOT_EMPTY_MASK = 0;
  361. }
  362. }
  363. /**
  364. * \brief Enable or Disable slave tx interrupt
  365. *
  366. * This function enables or disables slave tx interrupt.
  367. *
  368. * \param[in,out] i2c_module Pointer to the driver
  369. * \param[in] enable Enable or Disable
  370. *
  371. */
  372. static inline void i2c_slave_tx_interrupt(I2c *const i2c_module, bool enable)
  373. {
  374. if (enable) {
  375. i2c_module->TX_INTERRUPT_MASK.bit.TX_FIFO_NOT_FULL_MASK = 1;
  376. i2c_module->RX_INTERRUPT_MASK.bit.NAK_MASK = 1;
  377. } else {
  378. i2c_module->TX_INTERRUPT_MASK.bit.TX_FIFO_NOT_FULL_MASK = 0;
  379. i2c_module->RX_INTERRUPT_MASK.bit.NAK_MASK = 0;
  380. }
  381. }
  382. /** @} */
  383. #ifdef __cplusplus
  384. }
  385. #endif
  386. /**
  387. * \page asfdoc_samb_i2c_extra_info_page Extra Information for I2C Driver
  388. *
  389. * \section asfdoc_samb_i2c_acronyms Acronyms
  390. * \ref asfdoc_samb_i2c_acronyms_table "Below" is a table listing the acronyms
  391. * used in this module, along with their intended meanings.
  392. *
  393. * \anchor asfdoc_samb_i2c_acronyms_table
  394. * <table>
  395. * <caption>Acronyms</caption>
  396. * <tr>
  397. * <th>Acronym</th>
  398. * <th>Description</th>
  399. * </tr>
  400. * <tr>
  401. * <td>SDA</td>
  402. * <td>Serial Data Line</td>
  403. * </tr>
  404. * <tr>
  405. * <td>SCL</td>
  406. * <td>Serial Clock Line</td>
  407. * </tr>
  408. * </table>
  409. *
  410. * \section asfdoc_samb_i2c_extra_dependencies Dependencies
  411. * There are no dependencies related to this driver.
  412. *
  413. *
  414. * \section asfdoc_samb_i2c_extra_errata Errata
  415. * There are no errata related to this driver.
  416. *
  417. * \section asfdoc_samb_i2c_extra_history Module History
  418. * \ref asfdoc_samb_i2c_extra_history_table "Below" is an overview of the
  419. * module history, detailing enhancements and fixes made to the module since
  420. * its first release. The current version of this corresponds to the newest
  421. * version listed in
  422. * \ref asfdoc_samb_i2c_extra_history_table "the table below".
  423. *
  424. * \anchor asfdoc_samb_i2c_extra_history_table
  425. * <table>
  426. * <caption>Module History</caption>
  427. * <tr>
  428. * <th>Changelog</th>
  429. * </tr>
  430. * <tr>
  431. * <td>Initial Release</td>
  432. * </tr>
  433. * </table>
  434. */
  435. /**
  436. * \page asfdoc_samb_i2c_exqsg Examples for I2C Driver
  437. *
  438. * This is a list of the available Quick Start guides (QSGs) and example
  439. * applications for \ref asfdoc_samb_i2c_group. QSGs are simple examples with
  440. * step-by-step instructions to configure and use this driver in a selection of
  441. * use cases. Note that QSGs can be compiled as a standalone application or be
  442. * added to the user application.
  443. *
  444. * \if I2C_MASTER_MODE
  445. * - \subpage asfdoc_samb_i2c_master_basic_use_case "Quick Start Guide for the I2C Master module - Basic Use Case"
  446. * \endif
  447. * \if I2C_MASTER_CALLBACK_MODE
  448. * - \subpage asfdoc_samb_i2c_master_callback_use_case "Quick Start Guide for the I2C Master module - Callback Use Case"
  449. * \endif
  450. * \if I2C_SLAVE_MODE
  451. * - \subpage asfdoc_samb_i2c_slave_basic_use_case "Quick Start Guide for the I2C Slave module - Basic Use Case"
  452. * \endif
  453. * \if I2C_SLAVE_CALLBACK_MODE
  454. * - \subpage asfdoc_samb_i2c_slave_callback_use_case "Quick Start Guide for the I2C Slave module - Callback Use Case"
  455. * \endif
  456. *
  457. * \page asfdoc_samb_i2c_document_revision_history Document Revision History
  458. *
  459. * <table>
  460. * <tr>
  461. * <th>Doc. Rev.</td>
  462. * <th>Date</td>
  463. * <th>Comments</td>
  464. * </tr>
  465. * <tr>
  466. * <td>A</td>
  467. * <td>09/2015</td>
  468. * <td>Initial release</td>
  469. * </tr>
  470. * </table>
  471. */
  472. #endif /* I2C_COMMON_H_INCLUDED */