Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stm32f10x.h"
- #include "stm32f10x_conf.h"
- uint16_t Reciever_SPIBuffer[255];
- int main(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- SPI_InitTypeDef SPI_InitStructure;
- DMA_InitTypeDef DMA_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- uint16_t SPIBuffer[] = {0xAAAA, 0xAAAA, 0xAAAA};
- SystemInit();
- LEDS_ini();
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_SET);
- SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
- SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
- SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
- SPI_InitStructure.SPI_CRCPolynomial = 0;
- SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
- SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
- SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
- SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
- SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;
- SPI_Init(SPI2, &SPI_InitStructure);
- SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx, ENABLE);
- SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Rx, ENABLE);
- SPI_Cmd(SPI2, ENABLE);
- // DMA Channel 4 - SPI RX
- DMA_InitStructure.DMA_BufferSize = 22;
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
- DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
- DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Reciever_SPIBuffer;
- DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
- DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
- DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
- DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&SPI2->DR;
- DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
- DMA_InitStructure.DMA_Priority = DMA_Priority_High;
- DMA_Init(DMA1_Channel4, &DMA_InitStructure);
- DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE);
- // DMA Channel 5 - SPI TX
- DMA_InitStructure.DMA_BufferSize = 22;
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
- DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
- DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SPIBuffer;
- DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
- DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
- DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
- DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&SPI2->DR;
- DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
- DMA_InitStructure.DMA_Priority = DMA_Priority_High;
- DMA_Init(DMA1_Channel5, &DMA_InitStructure);
- NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- DMA_Cmd(DMA1_Channel4, DISABLE);
- DMA_Cmd(DMA1_Channel5, DISABLE);
- DMA_SetCurrDataCounter(DMA1_Channel4, 22);
- DMA_SetCurrDataCounter(DMA1_Channel5, 22);
- // Chip Select Low
- GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_RESET);
- DMA_Cmd(DMA1_Channel4, ENABLE);
- DMA_Cmd(DMA1_Channel5, ENABLE);
- while(1);
- }
- void spi_handleDMA1Ch4Interrupt(void){
- // Chip Select High
- GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_SET);
- }
- void DMA1_Channel4_IRQHandler(void){
- spi_handleDMA1Ch4Interrupt();
- DMA_ClearFlag(DMA1_FLAG_TC4);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement