twi_master.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /**
  2. * \file
  3. *
  4. * \brief TWI Master Mode management
  5. *
  6. * Copyright (c) 2010-2015 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 TWI_MASTER_H_INCLUDED
  47. #define TWI_MASTER_H_INCLUDED
  48. #include <compiler.h>
  49. #if (SAM4L)
  50. # include "sam_twim/twi_master.h"
  51. #elif (SAM3S || SAM3U || SAM3N || SAM3XA || SAM4S || SAM4E || SAM4N || SAM4C || SAMG || SAM4CP || SAM4CM)
  52. # include "sam_twi/twi_master.h"
  53. #elif XMEGA
  54. # include "xmega_twi/twi_master.h"
  55. #elif MEGA_RF
  56. # include "megarf_twi/twi_master.h"
  57. #elif UC3
  58. # if (defined AVR32_TWI)
  59. # include "uc3_twi/twi_master.h"
  60. # else
  61. # include "uc3_twim/twi_master.h"
  62. # endif
  63. #else
  64. # error Unsupported chip type
  65. #endif
  66. /**
  67. *
  68. * \defgroup twi_group Two Wire-interface(TWI)
  69. *
  70. * This is the common API for TWIs. Additional features are available
  71. * in the documentation of the specific modules.
  72. *
  73. * See \ref twi_quickstart.
  74. *
  75. * \section twi_group_platform Platform Dependencies
  76. *
  77. * The TWI API is partially chip- or platform-specific. While all
  78. * platforms provide mostly the same functionality, there are some
  79. * variations around how different bus types and clock tree structures
  80. * are handled.
  81. *
  82. * The following functions are available on all platforms, but there may
  83. * be variations in the function signature (i.e. parameters) and
  84. * behaviour. These functions are typically called by platform-specific
  85. * parts of drivers, and applications that aren't intended to be
  86. * portable:
  87. * - Master TWI Module initialization
  88. * \code status_code_t twi_master_setup(*twi_module_pointer, twi_master_options_t *opt) \endcode
  89. * - Enables TWI Module
  90. * \code void twi_master_enable(*twi_module_pointer) \endcode
  91. * - Disables TWI Module
  92. * \code void twi_master_disable(*twi_module_pointer) \endcode
  93. * - Read data from a slave device
  94. * \code status_code_t twi_master_read(*twi_module_pointer, twi_package_t *package) \endcode
  95. * - Write data from to a slave device
  96. * \code status_code_t twi_master_write(*twi_module_pointer, twi_package_t *package) \endcode
  97. *
  98. * @{
  99. */
  100. /**
  101. * \typedef twi_master_t
  102. * This type can be used independently to refer to TWI master module for the
  103. * architecture used. It refers to the correct type definition for the
  104. * architecture, ie. TWI_t* for XMEGA or avr32_twim_t* for UC3
  105. */
  106. //! @}
  107. /**
  108. * \page twi_quickstart Quickstart guide for Common service TWI
  109. *
  110. * This is the quickstart guide for the \ref twi_group "Common service TWI",
  111. * with step-by-step instructions on how to configure and use the driver in a
  112. * selection of use cases.
  113. *
  114. * The use cases contain several code fragments. The code fragments in the
  115. * steps for setup can be copied into a custom initialization function, while
  116. * the steps for usage can be copied into, e.g., the main application function.
  117. *
  118. * \section twi_basic_use_case Basic use case
  119. * In the most basic use case, the TWI module is configured for
  120. * - Master operation
  121. * - addressing one slave device of the bus at address 0x50
  122. * - TWI clock of 50kHz
  123. * - polled read/write handling
  124. *
  125. * \section twi_basic_use_case_setup Setup steps
  126. * \subsection twi_basic_use_case_setup_code Example code
  127. * Add to your application C-file:
  128. * \code
  129. void twi_init(void)
  130. {
  131. twi_master_options_t opt = {
  132. .speed = 50000,
  133. .chip = 0x50
  134. };
  135. twi_master_setup(&TWIM0, &opt);
  136. }
  137. \endcode
  138. *
  139. * \subsection twi_basic_use_case_setup_flow Workflow
  140. * -# Ensure that board_init() has configured selected I/Os for TWI function.
  141. * -# Ensure that \ref conf_twim.h is present for the driver.
  142. * - \note This file is only for the driver and should not be included by the
  143. * user.
  144. * -# Define and initialize config structs for TWI module in your TWI initialization
  145. * function:
  146. * - \code
  147. twi_master_options_t opt = {
  148. .speed = 50000,
  149. .chip = 0x50
  150. }; \endcode
  151. * - field \ref speed sets the baudrate of the TWI bus
  152. * - field \ref chip sets the address of the slave device you want to communicate with
  153. * -# Call twi_master_setup and optionally check its return code
  154. * - \note The config structs can be reused for other TWI modules
  155. * after this step. Simply reconfigure and write to others modules.
  156. *
  157. * \section twi_basic_use_case_usage Usage steps
  158. * \subsection twi_basic_use_case_usage_code_writing Example code : Writing to a slave device
  159. * Use in application C-file:
  160. * \code
  161. const uint8_t test_pattern[] = {0x55,0xA5,0x5A,0x77,0x99};
  162. twi_package_t packet_write = {
  163. .addr = EEPROM_MEM_ADDR, // TWI slave memory address data
  164. .addr_length = sizeof (uint16_t), // TWI slave memory address data size
  165. .chip = EEPROM_BUS_ADDR, // TWI slave bus address
  166. .buffer = (void *)test_pattern, // transfer data source buffer
  167. .length = sizeof(test_pattern) // transfer data size (bytes)
  168. };
  169. while (twi_master_write(&TWIM0, &packet_write) != TWI_SUCCESS);
  170. \endcode
  171. *
  172. * \subsection twi_basic_use_case_usage_flow Workflow
  173. * -# Prepare the data you want to send to the slave device:
  174. * - \code const uint8_t test_pattern[] = {0x55,0xA5,0x5A,0x77,0x99}; \endcode
  175. * -# Prepare a twi_package_t structure
  176. * \code twi_package_t packet_write; \endcode
  177. * Fill all the fields of the structure :
  178. * - addr is the address in the slave device
  179. * - addr_length is the size of the address in the slave (support for large TWI memory devices)
  180. * - chip sets the 7 bit address of the slave device you want to communicate with
  181. * - buffer is a pointer on the data to write to slave
  182. * - length is the number of data to write
  183. *
  184. * -# Finally, call twi_master_write \code twi_master_write(&TWIM0, &packet_write); \endcode
  185. * and optionally check its return value for TWI_SUCCESS.
  186. * \subsection twi_basic_use_case_usage_code_reading Example code : Reading from a slave device
  187. * Use in application C-file:
  188. * \code
  189. uint8_t data_received[10];
  190. twi_package_t packet_read = {
  191. .addr = EEPROM_MEM_ADDR, // TWI slave memory address data
  192. .addr_length = sizeof (uint16_t), // TWI slave memory address data size
  193. .chip = EEPROM_BUS_ADDR, // TWI slave bus address
  194. .buffer = data_received, // transfer data destination buffer
  195. .length = 10 // transfer data size (bytes)
  196. };
  197. // Perform a multi-byte read access then check the result.
  198. if(twi_master_read(&TWIM0, &packet_read) == TWI_SUCCESS){
  199. //Check read content
  200. if(data_received[0]==0x55)
  201. do_something();
  202. }
  203. \endcode
  204. *
  205. * \subsection twi_basic_use_case_usage_flow Workflow
  206. * -# Prepare a data buffer that will receive the data from the slave device:
  207. * \code uint8_t data_received[10]; \endcode
  208. * -# Prepare a twi_package_t structure
  209. * \code twi_package_t packet_read; \endcode
  210. * Fill all the fields of the structure :
  211. * - addr is the address in the slave device
  212. * - addr_length is the size of the address in the slave (support for large TWI memory devices)
  213. * - chip sets the 7 bit address of the slave device you want to communicate with
  214. * - buffer is a pointer on the data buffer that will receive the data from the slave device
  215. * - length is the number of data to read
  216. *
  217. * -# Finally, call twi_master_read \code twi_master_read(&TWIM0, &packet_read); \endcode
  218. * and optionally check its return value for TWI_SUCCESS.
  219. * the data read from the device are now in data_received.
  220. */
  221. #endif /* TWI_MASTER_H_INCLUDED */