/platform/board/stm32f103rb-nucleo/drivers/src/spi.c

https://github.com/alibaba/AliOS-Things · C · 153 lines · 58 code · 28 blank · 67 comment · 6 complexity · 3cf160802df321b9a5b3284b4145bd8f MD5 · raw file

  1. /**
  2. ******************************************************************************
  3. * File Name : SPI.c
  4. * Description : This file provides code for the configuration
  5. * of the SPI instances.
  6. ******************************************************************************
  7. ** This notice applies to any and all portions of this file
  8. * that are not between comment pairs USER CODE BEGIN and
  9. * USER CODE END. Other portions of this file, whether
  10. * inserted by the user or by software development tools
  11. * are owned by their respective copyright owners.
  12. *
  13. * COPYRIGHT(c) 2018 STMicroelectronics
  14. *
  15. * Redistribution and use in source and binary forms, with or without modification,
  16. * are permitted provided that the following conditions are met:
  17. * 1. Redistributions of source code must retain the above copyright notice,
  18. * this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  34. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. ******************************************************************************
  38. */
  39. /* Includes ------------------------------------------------------------------*/
  40. #include "aos/hal/spi.h"
  41. #include "stm32f1xx_hal.h"
  42. #include "aos/hal/gpio.h"
  43. /* USER CODE BEGIN 0 */
  44. DMA_HandleTypeDef hdma_spi1_rx;
  45. DMA_HandleTypeDef hdma_spi1_tx;
  46. /* USER CODE END 0 */
  47. void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
  48. {
  49. GPIO_InitTypeDef GPIO_InitStruct;
  50. if(spiHandle->Instance==SPI1)
  51. {
  52. /* USER CODE BEGIN SPI1_MspInit 0 */
  53. /* USER CODE END SPI1_MspInit 0 */
  54. /* SPI1 clock enable */
  55. __HAL_RCC_SPI1_CLK_ENABLE();
  56. #if 0
  57. /* DMA */
  58. /* SPI1 DMA Init */
  59. /* SPI1 Init */
  60. hdma_spi1_tx.Instance = DMA1_Channel1;
  61. //hdma_spi1_tx.Init.Channel = DMA_CHANNEL_1;
  62. hdma_spi1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
  63. hdma_spi1_tx.Init.PeriphInc = DMA_PINC_DISABLE;
  64. hdma_spi1_tx.Init.MemInc = DMA_MINC_ENABLE;
  65. hdma_spi1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  66. hdma_spi1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  67. hdma_spi1_tx.Init.Mode = DMA_NORMAL;
  68. hdma_spi1_tx.Init.Priority = DMA_PRIORITY_LOW;
  69. //hdma_spi1_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  70. if (HAL_DMA_Init(&hdma_spi1_tx) != HAL_OK)
  71. {
  72. Error_Handler(__FILE__, __LINE__);
  73. }
  74. __HAL_LINKDMA(spiHandle,hdmatx,hdma_spi1_tx);
  75. HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
  76. HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
  77. hdma_spi1_rx.Instance = DMA1_Channel2;
  78. //hdma_spi1_rx.Init.Channel = DMA_CHANNEL_1;
  79. hdma_spi1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  80. hdma_spi1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  81. hdma_spi1_rx.Init.MemInc = DMA_MINC_ENABLE;
  82. hdma_spi1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  83. hdma_spi1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  84. hdma_spi1_rx.Init.Mode = DMA_NORMAL;
  85. hdma_spi1_rx.Init.Priority = DMA_PRIORITY_LOW;
  86. //hdma_spi1_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  87. if (HAL_DMA_Init(&hdma_spi1_rx) != HAL_OK)
  88. {
  89. Error_Handler(__FILE__, __LINE__);
  90. }
  91. __HAL_LINKDMA(spiHandle,hdmarx,hdma_spi1_rx);
  92. HAL_NVIC_SetPriority(DMA1_Channel2_IRQn, 0, 0);
  93. HAL_NVIC_EnableIRQ(DMA1_Channel2_IRQn);
  94. #endif
  95. /* USER CODE BEGIN SPI1_MspInit 1 */
  96. /* USER CODE END SPI1_MspInit 1 */
  97. }
  98. }
  99. void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle)
  100. {
  101. if(spiHandle->Instance==SPI1)
  102. {
  103. /* USER CODE BEGIN SPI1_MspDeInit 0 */
  104. /* USER CODE END SPI1_MspDeInit 0 */
  105. /* Peripheral clock disable */
  106. __HAL_RCC_SPI1_CLK_DISABLE();
  107. #if 0
  108. HAL_DMA_DeInit(spiHandle->hdmatx);
  109. HAL_DMA_DeInit(spiHandle->hdmarx);
  110. HAL_NVIC_DisableIRQ(DMA1_Channel1_IRQn);
  111. HAL_NVIC_DisableIRQ(DMA1_Channel2_IRQn);
  112. #endif
  113. /* USER CODE BEGIN SPI1_MspDeInit 1 */
  114. /* USER CODE END SPI1_MspDeInit 1 */
  115. }
  116. }
  117. /* USER CODE BEGIN 1 */
  118. /* USER CODE END 1 */
  119. /**
  120. * @}
  121. */
  122. /**
  123. * @}
  124. */
  125. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/