i2c_slave.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /**
  2. * \file
  3. *
  4. * \brief I2C Slave Interrupt 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. #include "i2c_slave.h"
  47. #if I2C_SLAVE_CALLBACK_MODE == true
  48. #include "i2c_slave_interrupt.h"
  49. #endif
  50. /**
  51. * \brief Gets the I<SUP>2</SUP>C master default configurations
  52. *
  53. * Use to initialize the configuration structure to known default values.
  54. *
  55. * The default configuration is as follows:
  56. * - I2C core I2C_CORE1
  57. * - Clock sourc I2C_CLK_INPUT_3
  58. * - Clock divider = 0x10
  59. * - Pinmux pad0 PINMUX_LP_GPIO_8_MUX2_I2C0_SDA
  60. * - Pinmux pad1 PINMUX_LP_GPIO_9_MUX2_I2C0_SCK
  61. *
  62. * \param[out] config Pointer to configuration structure to be initiated
  63. */
  64. void i2c_slave_get_config_defaults(
  65. struct i2c_slave_config *const config)
  66. {
  67. /* Sanity check */
  68. Assert(config);
  69. config->clock_source = I2C_CLK_INPUT_3;
  70. config->clock_divider = 0x10;
  71. config->pin_number_pad0 = PIN_LP_GPIO_8;
  72. config->pin_number_pad1 = PIN_LP_GPIO_9;
  73. config->pinmux_sel_pad0 = ((PIN_LP_GPIO_8 << 16) | MUX_LP_GPIO_8_I2C0_SDA);
  74. config->pinmux_sel_pad1 = ((PIN_LP_GPIO_9 << 16) | MUX_LP_GPIO_9_I2C0_SCL);
  75. }
  76. /**
  77. * \internal Sets configurations to module
  78. *
  79. * \param[out] module Pointer to software module structure
  80. * \param[in] config Configuration structure with configurations to set
  81. *
  82. * \return Status of setting configuration.
  83. * \retval STATUS_OK If module was configured correctly
  84. * \retval STATUS_ERR_ALREADY_INITIALIZED If setting other GCLK generator than
  85. * previously set
  86. * \retval STATUS_ERR_BAUDRATE_UNAVAILABLE If given baud rate is not compatible
  87. * with set GCLK frequency
  88. */
  89. static enum status_code _i2c_slave_set_config(
  90. struct i2c_slave_module *const module,
  91. const struct i2c_slave_config *const config)
  92. {
  93. /* Sanity check */
  94. Assert(module);
  95. Assert(module->hw);
  96. Assert(config);
  97. enum status_code status = STATUS_OK;
  98. I2c *const i2c_module = (module->hw);
  99. /* Set the pinmux for this i2c module. */
  100. gpio_pinmux_cofiguration(config->pin_number_pad0, (uint16_t)(config->pinmux_sel_pad0));
  101. gpio_pinmux_cofiguration(config->pin_number_pad1, (uint16_t)(config->pinmux_sel_pad1));
  102. /* Find and set baudrate. */
  103. i2c_module->CLOCK_SOURCE_SELECT.reg = config->clock_source;
  104. i2c_module->I2C_CLK_DIVIDER.reg = I2C_CLK_DIVIDER_I2C_DIVIDE_RATIO(config->clock_divider);
  105. /* I2C slave address */
  106. i2c_module->I2C_SLAVE_ADDRESS.reg = I2C_SLAVE_ADDRESS_ADDRESS(config->address);
  107. /* I2C slave mode */
  108. i2c_module->I2C_MASTER_MODE.reg = I2C_MASTER_MODE_MASTER_ENABLE_0;
  109. return status;
  110. }
  111. /**
  112. * \brief Initializes the requested I<SUP>2</SUP>C hardware module
  113. *
  114. * Initializes the I<SUP>2</SUP>C slave device requested and sets the provided
  115. * software module struct. Run this function before any further use of
  116. * the driver.
  117. *
  118. * \param[out] module Pointer to software module struct
  119. * \param[in] config Pointer to the configuration struct
  120. *
  121. * \return Status of initialization.
  122. * \retval STATUS_OK Module initiated correctly
  123. * \retval STATUS_ERR_INVALID_ARG Invalid argument in module or config structure.
  124. * \retval STATUS_ERR_ALREADY_INITIALIZED If the Pinmux is not a valid one for I2C signals.
  125. *
  126. */
  127. enum status_code i2c_slave_init(
  128. struct i2c_slave_module *const module,
  129. I2c *const hw,
  130. const struct i2c_slave_config *const config)
  131. {
  132. /* Sanity check */
  133. Assert(module);
  134. Assert(module->hw);
  135. Assert(config);
  136. module->hw = hw;
  137. /* Sanity check arguments. */
  138. if ((module == NULL) || (config == NULL))
  139. return STATUS_ERR_INVALID_ARG;
  140. i2c_disable(module->hw);
  141. if (module->hw == I2C0)
  142. system_peripheral_reset(PERIPHERAL_I2C0_CORE);
  143. else if (module->hw == I2C1) {
  144. system_peripheral_reset(PERIPHERAL_I2C1_CORE);
  145. } else {
  146. return STATUS_ERR_INVALID_ARG;
  147. }
  148. #if I2C_SLAVE_CALLBACK_MODE == true
  149. /* Initialize values in module. */
  150. module->registered_callback = 0;
  151. module->enabled_callback = 0;
  152. module->buffer_length = 0;
  153. module->buffer_remaining = 0;
  154. module->buffer = NULL;
  155. module->status = STATUS_OK;
  156. _i2c_instances = (void*)module;
  157. if (module->hw == I2C0) {
  158. system_register_isr(RAM_ISR_TABLE_I2CRX0_INDEX, (uint32_t)_i2c_slave_rx_isr_handler);
  159. system_register_isr(RAM_ISR_TABLE_I2CTX0_INDEX, (uint32_t)_i2c_slave_tx_isr_handler);
  160. NVIC_EnableIRQ(I2C0_RX_IRQn);
  161. NVIC_EnableIRQ(I2C0_TX_IRQn);
  162. } else if (module->hw == I2C1) {
  163. system_register_isr(RAM_ISR_TABLE_I2CRX1_INDEX, (uint32_t)_i2c_slave_rx_isr_handler);
  164. system_register_isr(RAM_ISR_TABLE_I2CTX1_INDEX, (uint32_t)_i2c_slave_tx_isr_handler);
  165. NVIC_EnableIRQ(I2C1_RX_IRQn);
  166. NVIC_EnableIRQ(I2C1_TX_IRQn);
  167. }
  168. #endif
  169. /* Set config and return status. */
  170. if(_i2c_slave_set_config(module, config) != STATUS_OK)
  171. return STATUS_ERR_NOT_INITIALIZED;
  172. return STATUS_OK;
  173. }
  174. /**
  175. * \brief Reads a packet from the master
  176. *
  177. * Reads a packet from the master. This will wait for the master to issue a
  178. * request.
  179. *
  180. * \param[in] module Pointer to software module structure
  181. * \param[out] packet Packet to read from master
  182. *
  183. * \return Status of packet read.
  184. * \retval STATUS_OK Packet was read successfully
  185. * \retval STATUS_ERR_INVALID_ARG Invalid argument(s) was provided
  186. */
  187. enum status_code i2c_slave_read_packet_wait(
  188. struct i2c_slave_module *const module,
  189. struct i2c_slave_packet *const packet)
  190. {
  191. /* Sanity check */
  192. Assert(module);
  193. Assert(module->hw);
  194. Assert(packet);
  195. I2c *const i2c_module = (module->hw);
  196. uint16_t counter = 0;
  197. uint32_t status = 0;
  198. uint16_t length = packet->data_length;
  199. if (length == 0) {
  200. return STATUS_ERR_INVALID_ARG;
  201. }
  202. do {
  203. status = i2c_module->RECEIVE_STATUS.reg;
  204. if (status & I2C_RECEIVE_STATUS_RX_FIFO_NOT_EMPTY)
  205. packet->data[counter++] = i2c_module->RECEIVE_DATA.reg;
  206. } while (counter < length);
  207. /* Now check whether the core has sent the data out and free the bus. */
  208. while (!(status & I2C_TRANSMIT_STATUS_TX_FIFO_EMPTY)) {
  209. status = i2c_module->TRANSMIT_STATUS.reg;
  210. }
  211. return STATUS_OK;
  212. }
  213. /**
  214. * \brief Writes a packet to the master
  215. *
  216. * Writes a packet to the master. This will wait for the master to issue
  217. * a request.
  218. *
  219. * \param[in] module Pointer to software module structure
  220. * \param[in] packet Packet to write to master
  221. *
  222. * \return Status of packet write.
  223. * \retval STATUS_OK Packet was written successfully
  224. * \retval STATUS_ERR_INVALID_ARG Invalid argument(s) was provided
  225. */
  226. enum status_code i2c_slave_write_packet_wait(
  227. struct i2c_slave_module *const module,
  228. struct i2c_slave_packet *const packet)
  229. {
  230. I2c *const i2c_module = (module->hw);
  231. uint16_t i = 0;
  232. uint32_t status = 0;
  233. uint16_t length = packet->data_length;
  234. if (length == 0) {
  235. return STATUS_ERR_INVALID_ARG;
  236. }
  237. i2c_wait_for_idle(i2c_module);
  238. /* Flush the FIFO */
  239. i2c_module->I2C_FLUSH.reg = 1;
  240. do {
  241. status = i2c_module->TRANSMIT_STATUS.reg;
  242. if (status & I2C_TRANSMIT_STATUS_TX_FIFO_NOT_FULL_Msk) {
  243. i2c_module->TRANSMIT_DATA.reg = packet->data[i++];
  244. }
  245. } while (i < length);
  246. /* Now check whether the core has sent the data out and its good to free the bus */
  247. while (!(status & I2C_TRANSMIT_STATUS_TX_FIFO_EMPTY)) {
  248. status = i2c_module->TRANSMIT_STATUS.reg;
  249. }
  250. return STATUS_OK;
  251. }