/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
- /**
- ******************************************************************************
- * File Name : SPI.c
- * Description : This file provides code for the configuration
- * of the SPI instances.
- ******************************************************************************
- ** This notice applies to any and all portions of this file
- * that are not between comment pairs USER CODE BEGIN and
- * USER CODE END. Other portions of this file, whether
- * inserted by the user or by software development tools
- * are owned by their respective copyright owners.
- *
- * COPYRIGHT(c) 2018 STMicroelectronics
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of STMicroelectronics nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "aos/hal/spi.h"
- #include "stm32f1xx_hal.h"
- #include "aos/hal/gpio.h"
- /* USER CODE BEGIN 0 */
- DMA_HandleTypeDef hdma_spi1_rx;
- DMA_HandleTypeDef hdma_spi1_tx;
- /* USER CODE END 0 */
- void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- if(spiHandle->Instance==SPI1)
- {
- /* USER CODE BEGIN SPI1_MspInit 0 */
- /* USER CODE END SPI1_MspInit 0 */
- /* SPI1 clock enable */
- __HAL_RCC_SPI1_CLK_ENABLE();
- #if 0
- /* DMA */
- /* SPI1 DMA Init */
- /* SPI1 Init */
- hdma_spi1_tx.Instance = DMA1_Channel1;
- //hdma_spi1_tx.Init.Channel = DMA_CHANNEL_1;
- hdma_spi1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
- hdma_spi1_tx.Init.PeriphInc = DMA_PINC_DISABLE;
- hdma_spi1_tx.Init.MemInc = DMA_MINC_ENABLE;
- hdma_spi1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
- hdma_spi1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
- hdma_spi1_tx.Init.Mode = DMA_NORMAL;
- hdma_spi1_tx.Init.Priority = DMA_PRIORITY_LOW;
- //hdma_spi1_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
- if (HAL_DMA_Init(&hdma_spi1_tx) != HAL_OK)
- {
- Error_Handler(__FILE__, __LINE__);
- }
- __HAL_LINKDMA(spiHandle,hdmatx,hdma_spi1_tx);
- HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
- hdma_spi1_rx.Instance = DMA1_Channel2;
- //hdma_spi1_rx.Init.Channel = DMA_CHANNEL_1;
- hdma_spi1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
- hdma_spi1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
- hdma_spi1_rx.Init.MemInc = DMA_MINC_ENABLE;
- hdma_spi1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
- hdma_spi1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
- hdma_spi1_rx.Init.Mode = DMA_NORMAL;
- hdma_spi1_rx.Init.Priority = DMA_PRIORITY_LOW;
- //hdma_spi1_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
- if (HAL_DMA_Init(&hdma_spi1_rx) != HAL_OK)
- {
- Error_Handler(__FILE__, __LINE__);
- }
- __HAL_LINKDMA(spiHandle,hdmarx,hdma_spi1_rx);
- HAL_NVIC_SetPriority(DMA1_Channel2_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(DMA1_Channel2_IRQn);
- #endif
- /* USER CODE BEGIN SPI1_MspInit 1 */
- /* USER CODE END SPI1_MspInit 1 */
- }
- }
- void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle)
- {
- if(spiHandle->Instance==SPI1)
- {
- /* USER CODE BEGIN SPI1_MspDeInit 0 */
- /* USER CODE END SPI1_MspDeInit 0 */
- /* Peripheral clock disable */
- __HAL_RCC_SPI1_CLK_DISABLE();
- #if 0
- HAL_DMA_DeInit(spiHandle->hdmatx);
- HAL_DMA_DeInit(spiHandle->hdmarx);
- HAL_NVIC_DisableIRQ(DMA1_Channel1_IRQn);
- HAL_NVIC_DisableIRQ(DMA1_Channel2_IRQn);
- #endif
- /* USER CODE BEGIN SPI1_MspDeInit 1 */
- /* USER CODE END SPI1_MspDeInit 1 */
- }
- }
- /* USER CODE BEGIN 1 */
- /* USER CODE END 1 */
- /**
- * @}
- */
- /**
- * @}
- */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/