Advertisement
Guest User

Slave

a guest
Oct 10th, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.98 KB | None | 0 0
  1. #include "stm32f10x.h"
  2. #include "stm32f10x_conf.h"
  3.  
  4. uint16_t Reciever_SPIBuffer[255];
  5.  
  6. int main(void)
  7. {
  8. GPIO_InitTypeDef GPIO_InitStructure;
  9. SPI_InitTypeDef SPI_InitStructure;
  10. DMA_InitTypeDef DMA_InitStructure;
  11. NVIC_InitTypeDef NVIC_InitStructure;
  12. uint16_t SPIBuffer[] = {0xAAAA, 0xAAAA, 0xAAAA};
  13.  
  14.  
  15. SystemInit();
  16.  LEDS_ini();
  17. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
  18. RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
  19. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
  20.  
  21. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15;
  22. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  23. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  24. GPIO_Init(GPIOB, &GPIO_InitStructure);
  25.  
  26. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
  27. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  28. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  29. GPIO_Init(GPIOB, &GPIO_InitStructure);
  30.  
  31. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  32. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  33. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  34. GPIO_Init(GPIOB, &GPIO_InitStructure);
  35.  
  36. GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_SET);
  37.  
  38. SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
  39. SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  40. SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  41. SPI_InitStructure.SPI_CRCPolynomial = 0;
  42. SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  43. SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  44. SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  45. SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
  46. SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;
  47. SPI_Init(SPI2, &SPI_InitStructure);
  48.  
  49. SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx, ENABLE);
  50. SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Rx, ENABLE);
  51.  
  52. SPI_Cmd(SPI2, ENABLE);
  53.  
  54. // DMA Channel 4 - SPI RX
  55. DMA_InitStructure.DMA_BufferSize = 22;
  56. DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  57. DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  58. DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Reciever_SPIBuffer;
  59. DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  60. DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  61. DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  62. DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&SPI2->DR;
  63. DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  64. DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  65. DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  66. DMA_Init(DMA1_Channel4, &DMA_InitStructure);
  67.  
  68. DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE);
  69.  
  70. // DMA Channel 5 - SPI TX
  71. DMA_InitStructure.DMA_BufferSize = 22;
  72. DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
  73. DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  74. DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SPIBuffer;
  75. DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  76. DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  77. DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  78. DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&SPI2->DR;
  79. DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  80. DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  81. DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  82. DMA_Init(DMA1_Channel5, &DMA_InitStructure);
  83.  
  84. NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn;
  85. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  86. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  87. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  88. NVIC_Init(&NVIC_InitStructure);
  89.  
  90.  
  91.  
  92. DMA_Cmd(DMA1_Channel4, DISABLE);
  93. DMA_Cmd(DMA1_Channel5, DISABLE);
  94. DMA_SetCurrDataCounter(DMA1_Channel4, 22);
  95. DMA_SetCurrDataCounter(DMA1_Channel5, 22);
  96.  
  97. // Chip Select Low
  98. GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_RESET);
  99.  
  100. DMA_Cmd(DMA1_Channel4, ENABLE);
  101. DMA_Cmd(DMA1_Channel5, ENABLE);
  102.  
  103. while(1);
  104. }
  105.  
  106. void spi_handleDMA1Ch4Interrupt(void){
  107.   // Chip Select High
  108.   GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_SET);
  109.  
  110. }
  111.  
  112. void DMA1_Channel4_IRQHandler(void){
  113.   spi_handleDMA1Ch4Interrupt();
  114.   DMA_ClearFlag(DMA1_FLAG_TC4);
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement