spi_master.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /**
  2. * \file
  3. *
  4. * \brief SPI 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 SPI_MASTER_H_INCLUDED
  47. #define SPI_MASTER_H_INCLUDED
  48. #include <parts.h>
  49. #if XMEGA
  50. # include "xmega_spi/spi_master.h"
  51. #elif MEGA_RF
  52. # include "megarf_spi/spi_master.h"
  53. #elif UC3
  54. # include "uc3_spi/spi_master.h"
  55. #elif SAM
  56. # include "sam_spi/spi_master.h"
  57. #else
  58. # error Unsupported chip type
  59. #endif
  60. /**
  61. *
  62. * \defgroup spi_group Serial Peripheral Interface (SPI)
  63. *
  64. * This is the common API for SPI interface. Additional features are available
  65. * in the documentation of the specific modules.
  66. *
  67. * \section spi_group_platform Platform Dependencies
  68. *
  69. * The SPI API is partially chip- or platform-specific. While all
  70. * platforms provide mostly the same functionality, there are some
  71. * variations around how different bus types and clock tree structures
  72. * are handled.
  73. *
  74. * The following functions are available on all platforms, but there may
  75. * be variations in the function signature (i.e. parameters) and
  76. * behavior. These functions are typically called by platform-specific
  77. * parts of drivers, and applications that aren't intended to be
  78. * portable:
  79. * - spi_master_init()
  80. * - spi_master_setup_device()
  81. * - spi_select_device()
  82. * - spi_deselect_device()
  83. * - spi_write_single()
  84. * - spi_write_packet()
  85. * - spi_read_single()
  86. * - spi_read_packet()
  87. * - spi_is_tx_empty()
  88. * - spi_is_tx_ready()
  89. * - spi_is_rx_full()
  90. * - spi_is_rx_ready()
  91. * - spi_enable()
  92. * - spi_disable()
  93. * - spi_is_enabled()
  94. *
  95. * \section spi_master_quickstart_section Quick Start Guide
  96. * See \ref common_spi_master_quickstart
  97. * @{
  98. */
  99. //! @}
  100. /**
  101. * \page common_spi_master_quickstart Quick Start Guide for the SPI Master Driver
  102. *
  103. * This is the quick start guide for the \ref spi_group "SPI Driver", with
  104. * step-by-step instructions on how to configure and use the driver for a
  105. * specific use case.
  106. *
  107. * The use case contain several code fragments. The code fragments in the
  108. * steps for setup can be copied into a custom initialization function, while
  109. * the steps for usage can be copied into, e.g., the main application function.
  110. *
  111. * The steps for setting up the SPI master for XMEGA and UC3 use exactly the
  112. * same approach, but note that there are different names on the peripherals. So
  113. * to use this Quick Start for UC3 please make sure that all the peripheral
  114. * names are updated according to the UC3 datasheet.
  115. * - \subpage spi_master_xmega
  116. *
  117. */
  118. /**
  119. * \page spi_master_xmega Basic setup for SPI master on XMEGA devices
  120. *
  121. * \section spi_master_xmega_basic Basic setup for XMEGA devices
  122. * The SPI module will be set up as master:
  123. * - SPI on PORTD
  124. * - 1MHz SPI clock speed
  125. * - Slave Chip Select connected on PORTD pin 1
  126. * - SPI mode 0 (data on rising clock edge)
  127. *
  128. * \section spi_master_xmega_basic_setup Setup steps
  129. * \subsection spi_master_xmega_basic_setup_code Example code
  130. * Add to application C-file (e.g. main.c):
  131. * \code
  132. void spi_init_pins(void)
  133. {
  134. ioport_configure_port_pin(&PORTD, PIN1_bm, IOPORT_INIT_HIGH | IOPORT_DIR_OUTPUT);
  135. ioport_configure_port_pin(&PORTD, PIN4_bm, IOPORT_PULL_UP | IOPORT_DIR_INPUT);
  136. ioport_configure_port_pin(&PORTD, PIN5_bm, IOPORT_INIT_HIGH | IOPORT_DIR_OUTPUT);
  137. ioport_configure_port_pin(&PORTD, PIN6_bm, IOPORT_DIR_INPUT);
  138. ioport_configure_port_pin(&PORTD, PIN7_bm, IOPORT_INIT_HIGH | IOPORT_DIR_OUTPUT);
  139. }
  140. void spi_init_module(void)
  141. {
  142. struct spi_device spi_device_conf = {
  143. .id = IOPORT_CREATE_PIN(PORTD, 1)
  144. };
  145. spi_master_init(&SPID);
  146. spi_master_setup_device(&SPID, &spi_device_conf, SPI_MODE_0, 1000000, 0);
  147. spi_enable(&SPID);
  148. }
  149. \endcode
  150. *
  151. * \subsection spi_master_xmega_basic_setup Workflow
  152. * -# Ensure that \ref conf_spi_master.h is present for the driver.
  153. * - \note This file is only for the driver and should not be included by the
  154. * user. In this example the file can be left empty.
  155. * -# Initialize the pins used by the SPI interface (this initialization is for
  156. * the ATxmega32A4U device).
  157. * -# Set the pin used for slave select as output high:
  158. * \code
  159. ioport_configure_port_pin(&PORTD, PIN1_bm, IOPORT_INIT_HIGH | IOPORT_DIR_OUTPUT);
  160. \endcode
  161. * -# Enable pull-up on own chip select (SS):
  162. * \code
  163. ioport_configure_port_pin(&PORTD, PIN4_bm, IOPORT_PULL_UP | IOPORT_DIR_INPUT);
  164. \endcode
  165. * \attention If this pin is pulled low the SPI module will go into slave mode.
  166. * -# Set MOSI and SCL as output high, and set MISO as input:
  167. * \code
  168. ioport_configure_port_pin(&PORTD, PIN5_bm, IOPORT_INIT_HIGH | IOPORT_DIR_OUTPUT);
  169. ioport_configure_port_pin(&PORTD, PIN6_bm, IOPORT_DIR_INPUT);
  170. ioport_configure_port_pin(&PORTD, PIN7_bm, IOPORT_INIT_HIGH | IOPORT_DIR_OUTPUT);
  171. \endcode
  172. * -# Define the SPI device configuration struct to describe which pin the
  173. * slave select (slave chip select) is connected to, in this case the slave
  174. * select pin has been connected to PORTD pin 1 (PD1):
  175. * - \code
  176. struct spi_device spi_device_conf = {
  177. .id = IOPORT_CREATE_PIN(PORTD, 1)
  178. };
  179. \endcode
  180. * -# Initialize the SPI module, in this case SPI on PORTD has been chosen:
  181. * - \code
  182. spi_master_init(&SPID);
  183. \endcode
  184. * -# Setup the SPI master module for a specific device:
  185. * - \code
  186. spi_master_setup_device(&SPID, &spi_device_conf, SPI_MODE_0, 1000000, 0);
  187. \endcode
  188. * - \note The last argument, which is zero in this case, can be ignored and is
  189. * only included for compatibility purposes.
  190. * -# Then enable the SPI:
  191. * - \code
  192. spi_enable(&SPID);
  193. \endcode
  194. *
  195. * \section spi_master_xmega_basic_usage Usage steps
  196. * \subsection spi_master_xmega_basic_usage_code Example code
  197. * Add to, e.g., the main loop in the application C-file:
  198. * \code
  199. uint8_t data_buffer[1] = {0xAA};
  200. struct spi_device spi_device_conf = {
  201. .id = IOPORT_CREATE_PIN(PORTD, 1)
  202. };
  203. spi_select_device(&SPID, &spi_device_conf);
  204. spi_write_packet(&SPID, data_buffer, 1);
  205. spi_read_packet(&SPID, data_buffer, 1);
  206. spi_deselect_device(&SPID, &spi_device_conf);
  207. \endcode
  208. *
  209. * \subsection spi_master_xmega_basic_usage_flow Workflow
  210. * -# Create a buffer for data to be sent/received on the SPI bus, in this case
  211. * a single byte buffer is used. The buffer can be of arbitrary size as long as
  212. * there is space left in SRAM:
  213. * - \code
  214. uint8_t data_buffer[1] = {0xAA};
  215. \endcode
  216. * -# Define the SPI device configuration struct to describe which pin the
  217. * slave select (slave chip select) is connected to, in this case the slave
  218. * select pin has been connected to PORTD pin 1 (PD1):
  219. * - \code
  220. struct spi_device spi_device_conf = {
  221. .id = IOPORT_CREATE_PIN(PORTD, 1)
  222. };
  223. \endcode
  224. * - \note As this struct is the same for both the initializing part and the usage
  225. * part it could be a good idea to make the struct global, and hence accessible
  226. * for both the initializing part and usage part. Another solution could be to
  227. * create the struct in the main function and pass the address of the struct to
  228. * the spi_init_module() function, e.g.:
  229. * \code
  230. void spi_init_module(struct spi_device *spi_device_conf)
  231. {
  232. ...
  233. spi_master_setup_device(&SPID, spi_device_conf, SPI_MODE_0, 1000000, 0);
  234. ...
  235. }
  236. \endcode
  237. * -# Write data to the SPI slave device, in this case write one byte from the
  238. * data_buffer:
  239. * - \code
  240. spi_write_packet(&SPID, data_buffer, 1);
  241. \endcode
  242. * -# Read data from the SPI slave device, in this case read one byte and put it
  243. * into the data_buffer:
  244. * - \code
  245. spi_read_packet(&SPID, data_buffer, 1);
  246. \endcode
  247. * - \attention As the SPI works as a shift register so that data is shifted in at
  248. * the same time as data is shifted out a read operation will mean that a dummy
  249. * byte \ref CONFIG_SPI_MASTER_DUMMY is written to the SPI bus. \ref CONFIG_SPI_MASTER_DUMMY
  250. * defaults to 0xFF, but can be changed by defining it inside the \ref conf_spi_master.h
  251. * file.
  252. * -# When read and write operations is done de-select the slave:
  253. * - \code
  254. spi_deselect_device(&SPID, &spi_device_conf);
  255. \endcode
  256. *
  257. */
  258. #endif /* SPI_MASTER_H_INCLUDED */