Naze32 clone with Frysky receiver
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

i2c_api.h 6.2KB

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