i2c_api.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /** \addtogroup hal */
  2. /** @{*/
  3. /* mbed Microcontroller Library
  4. * Copyright (c) 2006-2015 ARM Limited
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef MBED_I2C_API_H
  19. #define MBED_I2C_API_H
  20. #include "device.h"
  21. #include "hal/buffer.h"
  22. #if DEVICE_I2C_ASYNCH
  23. #include "hal/dma_api.h"
  24. #endif
  25. #if DEVICE_I2C
  26. /**
  27. * @defgroup hal_I2CEvents I2C Events Macros
  28. *
  29. * @{
  30. */
  31. #define I2C_EVENT_ERROR (1 << 1)
  32. #define I2C_EVENT_ERROR_NO_SLAVE (1 << 2)
  33. #define I2C_EVENT_TRANSFER_COMPLETE (1 << 3)
  34. #define I2C_EVENT_TRANSFER_EARLY_NACK (1 << 4)
  35. #define I2C_EVENT_ALL (I2C_EVENT_ERROR | I2C_EVENT_TRANSFER_COMPLETE | I2C_EVENT_ERROR_NO_SLAVE | I2C_EVENT_TRANSFER_EARLY_NACK)
  36. /**@}*/
  37. #if DEVICE_I2C_ASYNCH
  38. /** Asynch I2C HAL structure
  39. */
  40. typedef struct {
  41. struct i2c_s i2c; /**< Target specific I2C structure */
  42. struct buffer_s tx_buff; /**< Tx buffer */
  43. struct buffer_s rx_buff; /**< Rx buffer */
  44. } i2c_t;
  45. #else
  46. /** Non-asynch I2C HAL structure
  47. */
  48. typedef struct i2c_s i2c_t;
  49. #endif
  50. enum {
  51. I2C_ERROR_NO_SLAVE = -1,
  52. I2C_ERROR_BUS_BUSY = -2
  53. };
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. /**
  58. * \defgroup hal_GeneralI2C I2C Configuration Functions
  59. * @{
  60. */
  61. /** Initialize the I2C peripheral. It sets the default parameters for I2C
  62. * peripheral, and configures its specifieds pins.
  63. *
  64. * @param obj The I2C object
  65. * @param sda The sda pin
  66. * @param scl The scl pin
  67. */
  68. void i2c_init(i2c_t *obj, PinName sda, PinName scl);
  69. /** Configure the I2C frequency
  70. *
  71. * @param obj The I2C object
  72. * @param hz Frequency in Hz
  73. */
  74. void i2c_frequency(i2c_t *obj, int hz);
  75. /** Send START command
  76. *
  77. * @param obj The I2C object
  78. */
  79. int i2c_start(i2c_t *obj);
  80. /** Send STOP command
  81. *
  82. * @param obj The I2C object
  83. */
  84. int i2c_stop(i2c_t *obj);
  85. /** Blocking reading data
  86. *
  87. * @param obj The I2C object
  88. * @param address 7-bit address (last bit is 1)
  89. * @param data The buffer for receiving
  90. * @param length Number of bytes to read
  91. * @param stop Stop to be generated after the transfer is done
  92. * @return Number of read bytes
  93. */
  94. int i2c_read(i2c_t *obj, int address, char *data, int length, int stop);
  95. /** Blocking sending data
  96. *
  97. * @param obj The I2C object
  98. * @param address 7-bit address (last bit is 0)
  99. * @param data The buffer for sending
  100. * @param length Number of bytes to write
  101. * @param stop Stop to be generated after the transfer is done
  102. * @return
  103. * zero or non-zero - Number of written bytes
  104. * negative - I2C_ERROR_XXX status
  105. */
  106. int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop);
  107. /** Reset I2C peripheral. TODO: The action here. Most of the implementation sends stop()
  108. *
  109. * @param obj The I2C object
  110. */
  111. void i2c_reset(i2c_t *obj);
  112. /** Read one byte
  113. *
  114. * @param obj The I2C object
  115. * @param last Acknoledge
  116. * @return The read byte
  117. */
  118. int i2c_byte_read(i2c_t *obj, int last);
  119. /** Write one byte
  120. *
  121. * @param obj The I2C object
  122. * @param data Byte to be written
  123. * @return 0 if NAK was received, 1 if ACK was received, 2 for timeout.
  124. */
  125. int i2c_byte_write(i2c_t *obj, int data);
  126. /**@}*/
  127. #if DEVICE_I2CSLAVE
  128. /**
  129. * \defgroup SynchI2C Synchronous I2C Hardware Abstraction Layer for slave
  130. * @{
  131. */
  132. /** Configure I2C as slave or master.
  133. * @param obj The I2C object
  134. * @param enable_slave Enable i2c hardware so you can receive events with ::i2c_slave_receive
  135. * @return non-zero if a value is available
  136. */
  137. void i2c_slave_mode(i2c_t *obj, int enable_slave);
  138. /** Check to see if the I2C slave has been addressed.
  139. * @param obj The I2C object
  140. * @return The status - 1 - read addresses, 2 - write to all slaves,
  141. * 3 write addressed, 0 - the slave has not been addressed
  142. */
  143. int i2c_slave_receive(i2c_t *obj);
  144. /** Configure I2C as slave or master.
  145. * @param obj The I2C object
  146. * @param data The buffer for receiving
  147. * @param length Number of bytes to read
  148. * @return non-zero if a value is available
  149. */
  150. int i2c_slave_read(i2c_t *obj, char *data, int length);
  151. /** Configure I2C as slave or master.
  152. * @param obj The I2C object
  153. * @param data The buffer for sending
  154. * @param length Number of bytes to write
  155. * @return non-zero if a value is available
  156. */
  157. int i2c_slave_write(i2c_t *obj, const char *data, int length);
  158. /** Configure I2C address.
  159. * @param obj The I2C object
  160. * @param idx Currently not used
  161. * @param address The address to be set
  162. * @param mask Currently not used
  163. */
  164. void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask);
  165. #endif
  166. /**@}*/
  167. #if DEVICE_I2C_ASYNCH
  168. /**
  169. * \defgroup hal_AsynchI2C Asynchronous I2C Hardware Abstraction Layer
  170. * @{
  171. */
  172. /** Start I2C asynchronous transfer
  173. *
  174. * @param obj The I2C object
  175. * @param tx The transmit buffer
  176. * @param tx_length The number of bytes to transmit
  177. * @param rx The receive buffer
  178. * @param rx_length The number of bytes to receive
  179. * @param address The address to be set - 7bit or 9bit
  180. * @param stop If true, stop will be generated after the transfer is done
  181. * @param handler The I2C IRQ handler to be set
  182. * @param event Event mask for the transfer. See \ref hal_I2CEvents
  183. * @param hint DMA hint usage
  184. */
  185. void i2c_transfer_asynch(i2c_t *obj, const void *tx, size_t tx_length, void *rx, size_t rx_length, uint32_t address, uint32_t stop, uint32_t handler, uint32_t event, DMAUsage hint);
  186. /** The asynchronous IRQ handler
  187. *
  188. * @param obj The I2C object which holds the transfer information
  189. * @return Event flags if a transfer termination condition was met, otherwise return 0.
  190. */
  191. uint32_t i2c_irq_handler_asynch(i2c_t *obj);
  192. /** Attempts to determine if the I2C peripheral is already in use
  193. *
  194. * @param obj The I2C object
  195. * @return Non-zero if the I2C module is active or zero if it is not
  196. */
  197. uint8_t i2c_active(i2c_t *obj);
  198. /** Abort asynchronous transfer
  199. *
  200. * This function does not perform any check - that should happen in upper layers.
  201. * @param obj The I2C object
  202. */
  203. void i2c_abort_asynch(i2c_t *obj);
  204. #endif
  205. /**@}*/
  206. #ifdef __cplusplus
  207. }
  208. #endif
  209. #endif
  210. #endif
  211. /** @}*/