i2c_slave.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**
  2. * \file
  3. *
  4. * \brief I2C Slave 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_SLAVE_H_INCLUDED
  47. #define I2C_SLAVE_H_INCLUDED
  48. #include "i2c_common.h"
  49. #ifdef __cplusplus
  50. extern "C" {
  51. #endif
  52. /**
  53. * \addtogroup asfdoc_samb_i2c_group
  54. *
  55. * @{
  56. *
  57. */
  58. /**
  59. * \brief I<SUP>2</SUP>C slave packet for read/write
  60. *
  61. * Structure to be used when transferring I<SUP>2</SUP>C slave packets.
  62. */
  63. struct i2c_slave_packet {
  64. /** Length of data array */
  65. uint16_t data_length;
  66. /** Data array containing all data to be transferred */
  67. uint8_t *data;
  68. };
  69. #if I2C_SLAVE_CALLBACK_MODE == true
  70. /**
  71. * \brief Callback types
  72. *
  73. * The available callback types for the I<SUP>2</SUP>C slave.
  74. */
  75. enum i2c_slave_callback {
  76. /** Callback for packet write complete */
  77. I2C_SLAVE_CALLBACK_WRITE_COMPLETE,
  78. /** Callback for packet read complete */
  79. I2C_SLAVE_CALLBACK_READ_COMPLETE,
  80. /**
  81. * Callback for read request from master - can be used to
  82. * issue a write
  83. */
  84. I2C_SLAVE_CALLBACK_READ_REQUEST,
  85. /**
  86. * Callback for write request from master - can be used to issue a read
  87. */
  88. I2C_SLAVE_CALLBACK_WRITE_REQUEST,
  89. /** Callback for error */
  90. I2C_SLAVE_CALLBACK_ERROR,
  91. # if !defined(__DOXYGEN__)
  92. /** Total number of callbacks */
  93. _I2C_SLAVE_CALLBACK_N,
  94. # endif
  95. };
  96. # if !defined(__DOXYGEN__)
  97. /** Software module prototype. */
  98. struct i2c_slave_module;
  99. /** Callback type. */
  100. typedef void (*i2c_slave_callback_t)(
  101. struct i2c_slave_module *const module);
  102. # endif
  103. #endif
  104. /**
  105. * \brief Enum for the direction of a request
  106. *
  107. * Enum for the direction of a request.
  108. */
  109. enum i2c_slave_direction {
  110. /** Read */
  111. I2C_SLAVE_DIRECTION_READ,
  112. /** Write */
  113. I2C_SLAVE_DIRECTION_WRITE,
  114. /** No direction */
  115. I2C_SLAVE_DIRECTION_NONE,
  116. };
  117. /**
  118. * \brief I<SUP>2</SUP>C Slave driver software device instance structure.
  119. *
  120. * I<SUP>2</SUP>C Slave driver software instance structure, used to
  121. * retain software state information of an associated hardware module instance.
  122. *
  123. * \note The fields of this structure should not be altered by the user
  124. * application; they are reserved for module-internal use only.
  125. */
  126. struct i2c_slave_module {
  127. #if !defined(__DOXYGEN__)
  128. /** Hardware instance initialized for the struct */
  129. I2c *hw;
  130. /** Module lock */
  131. volatile bool locked;
  132. /** Timeout value for polled functions */
  133. uint16_t buffer_timeout;
  134. # if I2C_SLAVE_CALLBACK_MODE == true
  135. /** Pointers to callback functions */
  136. volatile i2c_slave_callback_t callbacks[_I2C_SLAVE_CALLBACK_N];
  137. /** Mask for registered callbacks */
  138. volatile uint8_t registered_callback;
  139. /** Mask for enabled callbacks */
  140. volatile uint8_t enabled_callback;
  141. /** The total number of bytes to transfer */
  142. volatile uint16_t buffer_length;
  143. /**
  144. * Counter used for bytes left to send in write and to count number of
  145. * obtained bytes in read
  146. */
  147. uint16_t buffer_remaining;
  148. /** Data buffer for packet write and read */
  149. volatile uint8_t *buffer;
  150. /** Save direction of request from master. 1 = read, 0 = write. */
  151. volatile enum i2c_transfer_direction transfer_direction;
  152. /** Status for status read back in error callback */
  153. volatile enum status_code status;
  154. # endif
  155. #endif
  156. };
  157. /**
  158. * \brief Configuration structure for the I<SUP>2</SUP>C Slave device
  159. *
  160. * This is the configuration structure for the I<SUP>2</SUP>C Slave device. It is used
  161. * as an argument for \ref i2c_slave_init to provide the desired
  162. * configurations for the module. The structure should be initialized using the
  163. * \ref i2c_slave_get_config_defaults.
  164. */
  165. struct i2c_slave_config {
  166. /** Timeout to wait for master in polled functions */
  167. uint16_t buffer_timeout;
  168. /** Address or upper limit of address range */
  169. uint16_t address;
  170. /** CLOCK INPUT to use as clock source */
  171. enum i2c_clock_input clock_source;
  172. /** Divide ratio used to generate the sck clock */
  173. uint16_t clock_divider;
  174. /** PAD0 (SDA) pin number */
  175. uint32_t pin_number_pad0;
  176. /** PAD0 (SDA) pinmux selection */
  177. uint32_t pinmux_sel_pad0;
  178. /** PAD1 (SCL) pin numer */
  179. uint32_t pin_number_pad1;
  180. /** PAD1 (SCL) pinmux selection */
  181. uint32_t pinmux_sel_pad1;
  182. };
  183. /**
  184. * \name Configuration and Initialization
  185. * @{
  186. */
  187. void i2c_slave_get_config_defaults(
  188. struct i2c_slave_config *const config);
  189. enum status_code i2c_slave_init(struct i2c_slave_module *const module,
  190. I2c *const hw,
  191. const struct i2c_slave_config *const config);
  192. enum status_code i2c_slave_write_packet_wait(
  193. struct i2c_slave_module *const module,
  194. struct i2c_slave_packet *const packet);
  195. enum status_code i2c_slave_read_packet_wait(
  196. struct i2c_slave_module *const module,
  197. struct i2c_slave_packet *const packet);
  198. /** @} */
  199. /**
  200. * \name Status Management
  201. * @{
  202. */
  203. uint32_t i2c_slave_get_status(
  204. struct i2c_slave_module *const module);
  205. void i2c_slave_clear_status(
  206. struct i2c_slave_module *const module,
  207. uint32_t status_flags);
  208. /** @} */
  209. /** @} */
  210. #ifdef __cplusplus
  211. }
  212. #endif
  213. #endif /* I2C_SLAVE_H_INCLUDED */