twihs_master.h 8.9 KB

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