i2c_master.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**
  2. * \file
  3. *
  4. * \brief I2C Master 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_MASTER_H_INCLUDED
  47. #define I2C_MASTER_H_INCLUDED
  48. #include <compiler.h>
  49. #include "i2c_common.h"
  50. #ifdef __cplusplus
  51. extern "C" {
  52. #endif
  53. /**
  54. * \addtogroup asfdoc_samb_i2c_group
  55. *
  56. * @{
  57. */
  58. /**
  59. * \brief I<SUP>2</SUP>C master packet for read/write
  60. *
  61. * Structure to be used when transferring I<SUP>2</SUP>C master packets.
  62. */
  63. struct i2c_master_packet {
  64. /** Address to slave device */
  65. uint16_t address;
  66. /** Length of data array */
  67. uint16_t data_length;
  68. /** Data array containing all data to be transferred */
  69. uint8_t *data;
  70. };
  71. /** \brief Interrupt flags
  72. *
  73. * Flags used when reading or setting interrupt flags.
  74. */
  75. enum i2c_master_interrupt_flag {
  76. /** Interrupt flag used for write */
  77. I2C_MASTER_INTERRUPT_WRITE = 0,
  78. /** Interrupt flag used for read */
  79. I2C_MASTER_INTERRUPT_READ = 1,
  80. };
  81. /**
  82. * \brief I<SUP>2</SUP>C frequencies
  83. *
  84. * Values for I<SUP>2</SUP>C speeds supported by the module.
  85. *
  86. */
  87. enum i2c_master_baud_rate {
  88. /** Baud rate at 100KHz (Standard-mode) */
  89. I2C_MASTER_BAUD_RATE_100KHZ = 100,
  90. /** Baud rate at 400KHz (Fast-mode) */
  91. I2C_MASTER_BAUD_RATE_400KHZ = 400,
  92. };
  93. #if I2C_MASTER_CALLBACK_MODE == true
  94. /**
  95. * \brief Callback types
  96. *
  97. * The available callback types for the I<SUP>2</SUP>C master module.
  98. */
  99. enum i2c_master_callback {
  100. /** Callback for packet write complete */
  101. I2C_MASTER_CALLBACK_WRITE_COMPLETE = 0,
  102. /** Callback for packet read complete */
  103. I2C_MASTER_CALLBACK_READ_COMPLETE = 1,
  104. # if !defined(__DOXYGEN__)
  105. /** Total number of callbacks */
  106. _I2C_MASTER_CALLBACK_N = 2,
  107. # endif
  108. };
  109. # if !defined(__DOXYGEN__)
  110. /** Prototype for software module. */
  111. struct i2c_master_module;
  112. /** Prototype for I2C Callback function */
  113. typedef void (*i2c_master_callback_t)(
  114. struct i2c_master_module *const module);
  115. # endif
  116. #endif
  117. /**
  118. * \brief I<SUP>2</SUP>C driver software device instance structure.
  119. *
  120. * I<SUP>2</SUP>C 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_master_module {
  127. #if !defined(__DOXYGEN__)
  128. /** Hardware instance initialized for the struct */
  129. I2c *hw;
  130. /** Module lock */
  131. volatile bool locked;
  132. /** If true, stop condition will be sent after a read/write */
  133. bool no_stop;
  134. # if I2C_MASTER_CALLBACK_MODE == true
  135. /** Pointers to callback functions */
  136. volatile i2c_master_callback_t callbacks[_I2C_MASTER_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. volatile uint16_t buffer_remaining;
  148. /** Data buffer for packet write and read */
  149. volatile uint8_t *buffer;
  150. /** Save direction of async request. 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 Master device
  159. *
  160. * This is the configuration structure for the I<SUP>2</SUP>C Master device. It
  161. * is used as an argument for \ref i2c_master_init to provide the desired
  162. * configurations for the module. The structure should be initialized using the
  163. * \ref i2c_master_get_config_defaults .
  164. */
  165. struct i2c_master_config {
  166. /** CLOCK INPUT to use as clock source */
  167. enum i2c_clock_input clock_source;
  168. /** Divide ratio used to generate the sck clock */
  169. uint16_t clock_divider;
  170. /** PAD0 (SDA) pin number */
  171. uint32_t pin_number_pad0;
  172. /** PAD0 (SDA) pinmux selection */
  173. uint32_t pinmux_sel_pad0;
  174. /** PAD1 (SCL) pin numer */
  175. uint32_t pin_number_pad1;
  176. /** PAD1 (SCL) pinmux selection */
  177. uint32_t pinmux_sel_pad1;
  178. };
  179. void i2c_master_get_config_defaults(
  180. struct i2c_master_config *const config);
  181. enum status_code i2c_master_init(
  182. struct i2c_master_module *const module,
  183. I2c *const hw,
  184. const struct i2c_master_config *const config);
  185. void i2c_master_reset(struct i2c_master_module *const module);
  186. enum status_code i2c_master_write_address(
  187. struct i2c_master_module *const module,
  188. uint8_t address,
  189. uint8_t command);
  190. enum status_code i2c_master_read_packet_wait(
  191. struct i2c_master_module *const module,
  192. struct i2c_master_packet *const packet);
  193. enum status_code i2c_master_read_packet_wait_no_stop(
  194. struct i2c_master_module *const module,
  195. struct i2c_master_packet *const packet);
  196. enum status_code i2c_master_write_packet_wait(
  197. struct i2c_master_module *const module,
  198. struct i2c_master_packet *const packet);
  199. enum status_code i2c_master_write_packet_wait_no_stop(
  200. struct i2c_master_module *const module,
  201. struct i2c_master_packet *const packet);
  202. void i2c_master_send_stop(struct i2c_master_module *const module);
  203. void i2c_master_send_start(struct i2c_master_module *const module);
  204. enum status_code i2c_master_read_byte(
  205. struct i2c_master_module *const module,
  206. uint8_t *byte);
  207. enum status_code i2c_master_write_byte(
  208. struct i2c_master_module *const module,
  209. uint8_t byte);
  210. /** @} */
  211. #ifdef __cplusplus
  212. }
  213. #endif
  214. #endif /* I2C_MASTER_H_INCLUDED */