PageRenderTime 25ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/configs/photon/src/stm32_spi.c

https://bitbucket.org/hg42/nuttx
C | 194 lines | 71 code | 28 blank | 95 comment | 3 complexity | 75a37388a0f8d9c6c1e0289e16593cc3 MD5 | raw file
Possible License(s): 0BSD
  1. /************************************************************************************
  2. * configs/photon/src/stm32_spi.c
  3. *
  4. * Copyright (C) 2017 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ************************************************************************************/
  35. /************************************************************************************
  36. * Included Files
  37. ************************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <stdint.h>
  40. #include <stdbool.h>
  41. #include <errno.h>
  42. #include <debug.h>
  43. #include <nuttx/spi/spi.h>
  44. #include <arch/board/board.h>
  45. #include "up_arch.h"
  46. #include "chip.h"
  47. #include "stm32.h"
  48. #include "photon.h"
  49. #if defined(CONFIG_STM32_SPI1) || defined(CONFIG_STM32_SPI2) || defined(CONFIG_STM32_SPI3)
  50. /************************************************************************************
  51. * Public Functions
  52. ************************************************************************************/
  53. /************************************************************************************
  54. * Name: stm32_spidev_initialize
  55. *
  56. * Description:
  57. * Called to configure SPI chip select GPIO pins for the Mikroe Clicker2 STM32 board.
  58. *
  59. ************************************************************************************/
  60. void weak_function stm32_spidev_initialize(void)
  61. {
  62. }
  63. /****************************************************************************
  64. * Name: stm32_spi1/2/3select and stm32_spi1/2/3status
  65. *
  66. * Description:
  67. * The external functions, stm32_spi1/2/3select and stm32_spi1/2/3status must be
  68. * provided by board-specific logic. They are implementations of the select
  69. * and status methods of the SPI interface defined by struct spi_ops_s (see
  70. * include/nuttx/spi/spi.h). All other methods (including stm32_spibus_initialize())
  71. * are provided by common STM32 logic. To use this common SPI logic on your
  72. * board:
  73. *
  74. * 1. Provide logic in stm32_boardinitialize() to configure SPI chip select
  75. * pins.
  76. * 2. Provide stm32_spi1/2/3select() and stm32_spi1/2/3status() functions in your
  77. * board-specific logic. These functions will perform chip selection and
  78. * status operations using GPIOs in the way your board is configured.
  79. * 3. Add a calls to stm32_spibus_initialize() in your low level application
  80. * initialization logic
  81. * 4. The handle returned by stm32_spibus_initialize() may then be used to bind the
  82. * SPI driver to higher level logic (e.g., calling
  83. * mmcsd_spislotinitialize(), for example, will bind the SPI driver to
  84. * the SPI MMC/SD driver).
  85. *
  86. ****************************************************************************/
  87. #ifdef CONFIG_STM32_SPI1
  88. void stm32_spi1select(FAR struct spi_dev_s *dev, uint32_t devid, bool selected)
  89. {
  90. spiinfo("devid: %d CS: %s\n", (int)devid, selected ? "assert" : "de-assert");
  91. }
  92. uint8_t stm32_spi1status(FAR struct spi_dev_s *dev, uint32_t devid)
  93. {
  94. return 0;
  95. }
  96. #endif
  97. #ifdef CONFIG_STM32_SPI2
  98. void stm32_spi2select(FAR struct spi_dev_s *dev, uint32_t devid, bool selected)
  99. {
  100. spiinfo("devid: %d CS: %s\n", (int)devid, selected ? "assert" : "de-assert");
  101. }
  102. uint8_t stm32_spi2status(FAR struct spi_dev_s *dev, uint32_t devid)
  103. {
  104. return 0;
  105. }
  106. #endif
  107. #ifdef CONFIG_STM32_SPI3
  108. void stm32_spi3select(FAR struct spi_dev_s *dev, uint32_t devid, bool selected)
  109. {
  110. spiinfo("devid: %d CS: %s\n", (int)devid, selected ? "assert" : "de-assert");
  111. }
  112. uint8_t stm32_spi3status(FAR struct spi_dev_s *dev, uint32_t devid)
  113. {
  114. switch (devid)
  115. {
  116. default:
  117. break;
  118. }
  119. return 0;
  120. }
  121. #endif
  122. /****************************************************************************
  123. * Name: stm32_spi1cmddata
  124. *
  125. * Description:
  126. * Set or clear the SH1101A A0 or SD1306 D/C n bit to select data (true)
  127. * or command (false). This function must be provided by platform-specific
  128. * logic. This is an implementation of the cmddata method of the SPI
  129. * interface defined by struct spi_ops_s (see include/nuttx/spi/spi.h).
  130. *
  131. * Input Parameters:
  132. *
  133. * spi - SPI device that controls the bus the device that requires the CMD/
  134. * DATA selection.
  135. * devid - If there are multiple devices on the bus, this selects which one
  136. * to select cmd or data. NOTE: This design restricts, for example,
  137. * one one SPI display per SPI bus.
  138. * cmd - true: select command; false: select data
  139. *
  140. * Returned Value:
  141. * None
  142. *
  143. ****************************************************************************/
  144. #ifdef CONFIG_SPI_CMDDATA
  145. #ifdef CONFIG_STM32_SPI1
  146. int stm32_spi1cmddata(FAR struct spi_dev_s *dev, uint32_t devid, bool cmd)
  147. {
  148. return -ENODEV;
  149. }
  150. #endif
  151. #ifdef CONFIG_STM32_SPI2
  152. int stm32_spi2cmddata(FAR struct spi_dev_s *dev, uint32_t devid, bool cmd)
  153. {
  154. /* To be provided */
  155. return -ENODEV;
  156. }
  157. #endif
  158. #ifdef CONFIG_STM32_SPI3
  159. int stm32_spi3cmddata(FAR struct spi_dev_s *dev, uint32_t devid, bool cmd)
  160. {
  161. /* To be provided */
  162. return -ENODEV;
  163. }
  164. #endif
  165. #endif /* CONFIG_SPI_CMDDATA */
  166. #endif /* CONFIG_STM32_SPI1 || CONFIG_STM32_SPI2 */