Advertisement
vinifr

SPI DMA

Jul 8th, 2016
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.94 KB | None | 0 0
  1. void DMAInit(void)
  2. {
  3.     DMA_InitTypeDef  DMA_InitStructure;
  4.     // Enable DMA1 Peripheral Clock (SPI_DECAWAVE and SPI_BUS)
  5.     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
  6.  
  7.     // Configure SPI_BUS RX Channel
  8.     DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; // From SPI to memory
  9.     DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SPIx_DR_ADDRESS; //(uint32_t)&SPI2->DR;
  10.     DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  11.     DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  12.     DMA_InitStructure.DMA_MemoryBaseAddr = 0; // To be set later
  13.     DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  14.     DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  15.     DMA_InitStructure.DMA_BufferSize = 1; // To be set later
  16.     DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  17.     DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
  18.     DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  19.     DMA_Init(SPIx_RX_DMA_CHANNEL, &DMA_InitStructure);
  20.    
  21.     // Configure SPI_BUS TX Channel
  22.     DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; // From memory to SPI
  23.     DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SPIx_DR_ADDRESS; //(uint32_t)&SPI2->DR
  24.     DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  25.     DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  26.     DMA_InitStructure.DMA_MemoryBaseAddr = 0; // To be set later
  27.     DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  28.     DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  29.     DMA_InitStructure.DMA_BufferSize = 1; // To be set later
  30.     DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  31.     DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
  32.     DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  33.     DMA_Init(SPIx_TX_DMA_CHANNEL, &DMA_InitStructure);
  34. }
  35.  
  36. __IO uint32_t TimeOut = 0x0;
  37.  
  38. void spiFlashRead(uint16_t DataLength, uint8_t *DataBuffer, uint32_t ReadAddr)
  39. {
  40.     uint8_t CommandLength=4, CommandBuffer[4];
  41.    
  42.     /* Send "Read from Memory " instruction */
  43.     CommandBuffer[0] = READ; //SPI_FLASH_SendByte(READ);
  44.     /* Send ReadAddr high nibble address byte to read from */
  45.     CommandBuffer[1] = (ReadAddr & 0xFF0000) >> 16; //SPI_FLASH_SendByte((ReadAddr & 0xFF0000) >> 16);
  46.     /* Send ReadAddr medium nibble address byte to read from */
  47.     CommandBuffer[2] =  (ReadAddr& 0xFF00) >> 8; //SPI_FLASH_SendByte((ReadAddr& 0xFF00) >> 8);
  48.     /* Send ReadAddr low nibble address byte to read from */
  49.     CommandBuffer[3] =  ReadAddr & 0xFF; //SPI_FLASH_SendByte(ReadAddr & 0xFF);
  50.    
  51.     // Prepare the DMA
  52.     SPIx_TX_DMA_CHANNEL->CNDTR = CommandLength; //DMA1_Channel5->CNDTR = CommandLength;
  53.     SPIx_TX_DMA_CHANNEL->CMAR = (uint32_t)CommandBuffer;  //DMA1_Channel5->CMAR = (uint32_t)CommandBuffer;
  54.     SPIx_RX_DMA_CHANNEL->CNDTR = DataLength; //DMA1_Channel4->CNDTR = DataLength;
  55.     SPIx_RX_DMA_CHANNEL->CMAR = (uint32_t)DataBuffer;  //DMA1_Channel4->CMAR = (uint32_t)DataBuffer;
  56.  
  57.     // Enable the DMAs - They will await signals from the SPI hardware
  58.     DMA_Cmd(SPIx_TX_DMA_CHANNEL, ENABLE);  //DMA_Cmd(DMA1_Channel5, ENABLE); // TX
  59.     DMA_Cmd(SPIx_RX_DMA_CHANNEL, ENABLE);  //DMA_Cmd(DMA1_Channel4, ENABLE); // RX
  60.  
  61.     // Activate the Flash CS
  62.     //GPIO_ResetBits(SPI_MEM_CS_GPIO, SPI_MEM_CS);
  63.     /* Select the FLASH: Chip Select low */
  64.     SPI_FLASH_CS_LOW();
  65.  
  66.     // Enable the SPI communication to the TX DMA, which will send the command
  67.     SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Tx, ENABLE);
  68.  
  69.     // Wait until the command is sent to the DR
  70.     //while (!DMA_GetFlagStatus(DMA1_FLAG_TC5));
  71.     while ((DMA_GetFlagStatus(SPIx_TX_DMA_FLAG_TC) == RESET)&&(TimeOut != 0x00))
  72.     {}
  73.  
  74.     // Wait until the transmission is completed
  75.     while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET);
  76.     while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_BSY));
  77.  
  78.     // Disable the TX DMA and clear DMA flags
  79.     SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Tx, DISABLE);
  80.     DMA_Cmd(SPIx_TX_DMA_CHANNEL, DISABLE);
  81.     /* Clear DMA1 global flags*/    
  82.     DMA_ClearFlag(SPIx_RX_DMA_FLAG_GL);
  83.     //NOTE: I checked the SPI OVR flag here, and it wasn't set...
  84.  
  85.     // Enable SPI communication to the RX DMA, which should receive the data
  86.     SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Rx, ENABLE);
  87.  
  88.     // Wait until the data is received
  89.     //while (!DMA_GetFlagStatus(DMA1_FLAG_TC4));
  90.     while ((DMA_GetFlagStatus(SPIx_RX_DMA_FLAG_TC) == RESET)&&(TimeOut != 0x00))
  91.     {}
  92.  
  93.     // Disable the DMAs
  94.     DMA_Cmd(SPIx_TX_DMA_CHANNEL, DISABLE); //DMA_Cmd(DMA1_Channel4, DISABLE); // RX
  95.     DMA_Cmd(SPIx_RX_DMA_CHANNEL, DISABLE);  //DMA_Cmd(DMA1_Channel5, DISABLE); // TX
  96.  
  97.     /* Deselect the FLASH: Chip Select high */
  98.     SPI_FLASH_CS_HIGH();
  99.     // Release the Flash CS
  100.     //GPIO_SetBits(SPI_MEM_CS_GPIO, SPI_MEM_CS);
  101.     SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Rx, DISABLE);
  102.     DMA_Cmd(SPIx_RX_DMA_CHANNEL, DISABLE);
  103.     DMA_ClearFlag(SPIx_RX_DMA_FLAG_GL);
  104.  
  105. } // end spiFlashRead()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement