test
Dependencies: SDFileSystem mbed-dev
Fork of Nucleo_Ex06_EMU by
TFT/spidma.cpp@3:8d6e6aec9b01, 2017-05-27 (annotated)
- Committer:
- charliex
- Date:
- Sat May 27 02:15:43 2017 +0000
- Revision:
- 3:8d6e6aec9b01
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
charliex | 3:8d6e6aec9b01 | 1 | /*===================================================================*/ |
charliex | 3:8d6e6aec9b01 | 2 | /* */ |
charliex | 3:8d6e6aec9b01 | 3 | /* spidma.cpp : SPI DMA function */ |
charliex | 3:8d6e6aec9b01 | 4 | /* */ |
charliex | 3:8d6e6aec9b01 | 5 | /* 2016/1/20 Racoon */ |
charliex | 3:8d6e6aec9b01 | 6 | /* */ |
charliex | 3:8d6e6aec9b01 | 7 | /*===================================================================*/ |
charliex | 3:8d6e6aec9b01 | 8 | |
charliex | 3:8d6e6aec9b01 | 9 | #include "mbed.h" |
charliex | 3:8d6e6aec9b01 | 10 | #include "stm32f4xx_hal.h" |
charliex | 3:8d6e6aec9b01 | 11 | |
charliex | 3:8d6e6aec9b01 | 12 | SPI_HandleTypeDef SpiHandle; |
charliex | 3:8d6e6aec9b01 | 13 | |
charliex | 3:8d6e6aec9b01 | 14 | /*-------------------------------------------------------------------*/ |
charliex | 3:8d6e6aec9b01 | 15 | /* callback */ |
charliex | 3:8d6e6aec9b01 | 16 | /*-------------------------------------------------------------------*/ |
charliex | 3:8d6e6aec9b01 | 17 | extern "C" { |
charliex | 3:8d6e6aec9b01 | 18 | void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi) |
charliex | 3:8d6e6aec9b01 | 19 | { |
charliex | 3:8d6e6aec9b01 | 20 | static DMA_HandleTypeDef hdma_tx; |
charliex | 3:8d6e6aec9b01 | 21 | |
charliex | 3:8d6e6aec9b01 | 22 | GPIO_InitTypeDef GPIO_InitStruct; |
charliex | 3:8d6e6aec9b01 | 23 | |
charliex | 3:8d6e6aec9b01 | 24 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
charliex | 3:8d6e6aec9b01 | 25 | __HAL_RCC_SPI1_CLK_ENABLE(); |
charliex | 3:8d6e6aec9b01 | 26 | __HAL_RCC_DMA2_CLK_ENABLE(); |
charliex | 3:8d6e6aec9b01 | 27 | |
charliex | 3:8d6e6aec9b01 | 28 | GPIO_InitStruct.Pin = GPIO_PIN_5; |
charliex | 3:8d6e6aec9b01 | 29 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
charliex | 3:8d6e6aec9b01 | 30 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
charliex | 3:8d6e6aec9b01 | 31 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
charliex | 3:8d6e6aec9b01 | 32 | GPIO_InitStruct.Alternate = GPIO_AF5_SPI1; |
charliex | 3:8d6e6aec9b01 | 33 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
charliex | 3:8d6e6aec9b01 | 34 | |
charliex | 3:8d6e6aec9b01 | 35 | GPIO_InitStruct.Pin = GPIO_PIN_6; |
charliex | 3:8d6e6aec9b01 | 36 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
charliex | 3:8d6e6aec9b01 | 37 | |
charliex | 3:8d6e6aec9b01 | 38 | GPIO_InitStruct.Pin = GPIO_PIN_7; |
charliex | 3:8d6e6aec9b01 | 39 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
charliex | 3:8d6e6aec9b01 | 40 | |
charliex | 3:8d6e6aec9b01 | 41 | hdma_tx.Instance = DMA2_Stream3; |
charliex | 3:8d6e6aec9b01 | 42 | hdma_tx.Init.Channel = DMA_CHANNEL_3; |
charliex | 3:8d6e6aec9b01 | 43 | hdma_tx.Init.Direction = DMA_MEMORY_TO_PERIPH; |
charliex | 3:8d6e6aec9b01 | 44 | hdma_tx.Init.PeriphInc = DMA_PINC_DISABLE; |
charliex | 3:8d6e6aec9b01 | 45 | hdma_tx.Init.MemInc = DMA_MINC_ENABLE; |
charliex | 3:8d6e6aec9b01 | 46 | hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; |
charliex | 3:8d6e6aec9b01 | 47 | hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; |
charliex | 3:8d6e6aec9b01 | 48 | hdma_tx.Init.Mode = DMA_NORMAL; |
charliex | 3:8d6e6aec9b01 | 49 | hdma_tx.Init.Priority = DMA_PRIORITY_HIGH; |
charliex | 3:8d6e6aec9b01 | 50 | hdma_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; |
charliex | 3:8d6e6aec9b01 | 51 | hdma_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL; |
charliex | 3:8d6e6aec9b01 | 52 | hdma_tx.Init.MemBurst = DMA_MBURST_SINGLE; |
charliex | 3:8d6e6aec9b01 | 53 | hdma_tx.Init.PeriphBurst = DMA_PBURST_SINGLE; |
charliex | 3:8d6e6aec9b01 | 54 | |
charliex | 3:8d6e6aec9b01 | 55 | HAL_DMA_Init(&hdma_tx); |
charliex | 3:8d6e6aec9b01 | 56 | |
charliex | 3:8d6e6aec9b01 | 57 | __HAL_LINKDMA(hspi, hdmatx, hdma_tx); |
charliex | 3:8d6e6aec9b01 | 58 | |
charliex | 3:8d6e6aec9b01 | 59 | HAL_NVIC_SetPriority(DMA2_Stream3_IRQn, 0, 1); |
charliex | 3:8d6e6aec9b01 | 60 | HAL_NVIC_EnableIRQ(DMA2_Stream3_IRQn); |
charliex | 3:8d6e6aec9b01 | 61 | } |
charliex | 3:8d6e6aec9b01 | 62 | |
charliex | 3:8d6e6aec9b01 | 63 | void DMA2_Stream3_IRQHandler(void) |
charliex | 3:8d6e6aec9b01 | 64 | { |
charliex | 3:8d6e6aec9b01 | 65 | HAL_DMA_IRQHandler(SpiHandle.hdmatx); |
charliex | 3:8d6e6aec9b01 | 66 | } |
charliex | 3:8d6e6aec9b01 | 67 | |
charliex | 3:8d6e6aec9b01 | 68 | } // extern "C" |
charliex | 3:8d6e6aec9b01 | 69 | |
charliex | 3:8d6e6aec9b01 | 70 | uint8_t spi_read(void) |
charliex | 3:8d6e6aec9b01 | 71 | { |
charliex | 3:8d6e6aec9b01 | 72 | uint8_t recvBuf; |
charliex | 3:8d6e6aec9b01 | 73 | |
charliex | 3:8d6e6aec9b01 | 74 | //while (SpiHandle.State != HAL_SPI_STATE_READY); |
charliex | 3:8d6e6aec9b01 | 75 | |
charliex | 3:8d6e6aec9b01 | 76 | HAL_SPI_Receive(&SpiHandle, &recvBuf, 1, 100); |
charliex | 3:8d6e6aec9b01 | 77 | |
charliex | 3:8d6e6aec9b01 | 78 | // while(HAL_SPI_GetState(&SpiHandle) == HAL_SPI_STATE_BUSY); |
charliex | 3:8d6e6aec9b01 | 79 | return recvBuf; |
charliex | 3:8d6e6aec9b01 | 80 | } |
charliex | 3:8d6e6aec9b01 | 81 | |
charliex | 3:8d6e6aec9b01 | 82 | |
charliex | 3:8d6e6aec9b01 | 83 | /*-------------------------------------------------------------------*/ |
charliex | 3:8d6e6aec9b01 | 84 | /* Write a byte data */ |
charliex | 3:8d6e6aec9b01 | 85 | /*-------------------------------------------------------------------*/ |
charliex | 3:8d6e6aec9b01 | 86 | void spi_write(uint8_t data) |
charliex | 3:8d6e6aec9b01 | 87 | { |
charliex | 3:8d6e6aec9b01 | 88 | while (SpiHandle.State != HAL_SPI_STATE_READY); |
charliex | 3:8d6e6aec9b01 | 89 | |
charliex | 3:8d6e6aec9b01 | 90 | HAL_SPI_Transmit(&SpiHandle, &data, 1, 100); |
charliex | 3:8d6e6aec9b01 | 91 | while(HAL_SPI_GetState(&SpiHandle) == HAL_SPI_STATE_BUSY); |
charliex | 3:8d6e6aec9b01 | 92 | } |
charliex | 3:8d6e6aec9b01 | 93 | |
charliex | 3:8d6e6aec9b01 | 94 | /*-------------------------------------------------------------------*/ |
charliex | 3:8d6e6aec9b01 | 95 | /* Write a word data */ |
charliex | 3:8d6e6aec9b01 | 96 | /*-------------------------------------------------------------------*/ |
charliex | 3:8d6e6aec9b01 | 97 | void spi_writew(uint16_t data) |
charliex | 3:8d6e6aec9b01 | 98 | { |
charliex | 3:8d6e6aec9b01 | 99 | while (SpiHandle.State != HAL_SPI_STATE_READY); |
charliex | 3:8d6e6aec9b01 | 100 | HAL_SPI_Transmit(&SpiHandle, (uint8_t *)&data, 2, 100); |
charliex | 3:8d6e6aec9b01 | 101 | while(HAL_SPI_GetState(&SpiHandle) == HAL_SPI_STATE_BUSY); |
charliex | 3:8d6e6aec9b01 | 102 | } |
charliex | 3:8d6e6aec9b01 | 103 | |
charliex | 3:8d6e6aec9b01 | 104 | /*-------------------------------------------------------------------*/ |
charliex | 3:8d6e6aec9b01 | 105 | /* Initialize SPI DMA */ |
charliex | 3:8d6e6aec9b01 | 106 | /*-------------------------------------------------------------------*/ |
charliex | 3:8d6e6aec9b01 | 107 | void spi_init() |
charliex | 3:8d6e6aec9b01 | 108 | { |
charliex | 3:8d6e6aec9b01 | 109 | |
charliex | 3:8d6e6aec9b01 | 110 | SPI pad_spi(PC_12, PC_11, PC_10); // MOSI(should pullup), MISO, SCK |
charliex | 3:8d6e6aec9b01 | 111 | |
charliex | 3:8d6e6aec9b01 | 112 | |
charliex | 3:8d6e6aec9b01 | 113 | SpiHandle.Instance = SPI1; |
charliex | 3:8d6e6aec9b01 | 114 | SpiHandle.Init.Mode = SPI_MODE_MASTER; |
charliex | 3:8d6e6aec9b01 | 115 | SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2; |
charliex | 3:8d6e6aec9b01 | 116 | SpiHandle.Init.Direction = SPI_DIRECTION_2LINES; |
charliex | 3:8d6e6aec9b01 | 117 | SpiHandle.Init.CLKPhase = SPI_PHASE_1EDGE; |
charliex | 3:8d6e6aec9b01 | 118 | SpiHandle.Init.CLKPolarity = SPI_POLARITY_HIGH; |
charliex | 3:8d6e6aec9b01 | 119 | SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; |
charliex | 3:8d6e6aec9b01 | 120 | SpiHandle.Init.CRCPolynomial = 7; |
charliex | 3:8d6e6aec9b01 | 121 | SpiHandle.Init.DataSize = SPI_DATASIZE_8BIT; |
charliex | 3:8d6e6aec9b01 | 122 | SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB; |
charliex | 3:8d6e6aec9b01 | 123 | SpiHandle.Init.NSS = SPI_NSS_SOFT; |
charliex | 3:8d6e6aec9b01 | 124 | SpiHandle.Init.TIMode = SPI_TIMODE_DISABLE; |
charliex | 3:8d6e6aec9b01 | 125 | |
charliex | 3:8d6e6aec9b01 | 126 | |
charliex | 3:8d6e6aec9b01 | 127 | if (HAL_SPI_Init(&SpiHandle) != HAL_OK) |
charliex | 3:8d6e6aec9b01 | 128 | while(1); |
charliex | 3:8d6e6aec9b01 | 129 | |
charliex | 3:8d6e6aec9b01 | 130 | |
charliex | 3:8d6e6aec9b01 | 131 | } |
charliex | 3:8d6e6aec9b01 | 132 |