贴出从机处理的代码,从机在中断中完成接收和准备下一次发送的数据,主机是定时发送一串数据。 可见原帖https://www.stmcu.org.cn/module/forum/thread-621583-1-2.html #define FUNCTION_TO_MOTOR_NUM_MAX 10 #define MOTOR_TO_FUNCTION_NUM_MAX 10 uint8_t gSendToMotorDataBuff1[FUNCTION_TO_MOTOR_NUM_MAX]; uint8_t gSendToFunctionDataBuff1[MOTOR_TO_FUNCTION_NUM_MAX]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}; /** * @brief GPIO的初始化配置函数 * @param 无 * @retval 无 */ void spi2_Init(void) { spi2_dma1_init(); /* Enable the peripheral clock SPI2 */ RCC->APB1ENR |= RCC_APB1ENR_SPI2EN; /* Configure SPI2 in slave */ /* nSS hard, slave, CPOL and CPHA at zero (rising first edge) */ /* (1) TX and RX with DMA, RXNE IT, 8-bit Rx fifo */ /* (2) Enable SPI2 */ SPI2->CR2 = SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN | SPI_CR2_RXNEIE | SPI_CR2_FRXTH | SPI_CR2_DS_2 | SPI_CR2_DS_1 | SPI_CR2_DS_0|SPI_CR2_FRF; /* (1) */ SPI2->CR1 |= SPI_CR1_SPE; /* (2) */ } void spi2_dma1_init(void) { /* Enable the peripheral clock DMA11 */ RCC->AHBENR |= RCC_AHBENR_DMA1EN; /* DMA1 Channel4 SPI2_RX config */ /* (1) Peripheral address */ /* (2) Memory address */ /* (3) Data size */ /* (4) Memory increment */ /* Peripheral to memory */ /* 8-bit transfer */ /* Transfer complete IT */ DMA1_Channel4->CPAR = (uint32_t)&(SPI2->DR); /* (1) */ DMA1_Channel4->CMAR = (uint32_t)gSendToMotorDataBuff1; /* (2) */ DMA1_Channel4->CNDTR = FUNCTION_TO_MOTOR_NUM_MAX; /* (3) */ DMA1_Channel4->CCR |= DMA_CCR_MINC | DMA_CCR_TCIE | DMA_CCR_EN; /* (4) */ /* DMA1 Channel5 SPI2_TX config */ /* (5) Peripheral address */ /* (6) Memory address */ /* (7) Memory increment */ /* Memory to peripheral*/ /* 8-bit transfer */ DMA1_Channel5->CPAR = (uint32_t)&(SPI2->DR); /* (5) */ DMA1_Channel5->CMAR = (uint32_t)gSendToFunctionDataBuff1; /* (6) */ DMA1_Channel5->CCR |= DMA_CCR_MINC | DMA_CCR_DIR; /* (7) */ DMA1_Channel5->CNDTR = MOTOR_TO_FUNCTION_NUM_MAX; /* Data size */ DMA1_Channel5->CCR |= DMA_CCR_EN; /* Configure IT */ /* (8) Set priority for DMA1_Channel4_IRQn */ /* (9) Enable DMA1_Channel4_IRQn */ NVIC_SetPriority(DMA1_Channel4_IRQn, 0); /* (8) */ NVIC_EnableIRQ(DMA1_Channel4_IRQn); /* (9) */ } /** * @brief This function handles DMA1 channel 4 TC interrupt request. * @param None * @retval None */ void DMA1_Channel4_IRQHandler(void) { if((DMA1->ISR & DMA_ISR_TCIF4) == DMA_ISR_TCIF4) { DMA1->IFCR |= DMA_IFCR_CTCIF4; /* Clear TC flag */ DMA1_Channel4->CCR &=~ DMA_CCR_EN; DMA1_Channel4->CMAR = (uint32_t)gSendToMotorDataBuff1; /* (2) */ DMA1_Channel4->CNDTR = FUNCTION_TO_MOTOR_NUM_MAX; /* Data size */ DMA1_Channel4->CCR |= DMA_CCR_EN; spi2_send_data(); } } void spi2_send_data(void) { uint8_t i; gSendToFunctionDataBuff1[0]=gSendToMotorDataBuff1[0]; for(i=1;i<MOTOR_TO_FUNCTION_NUM_MAX;i++) { gSendToFunctionDataBuff1=gSendToMotorDataBuff1; } /* start 16-bit transmission with DMA*/ /* Prepare Slave */ DMA1_Channel5->CCR &=~ DMA_CCR_EN; DMA1_Channel5->CMAR = (uint32_t)gSendToFunctionDataBuff1; /* (6) */ DMA1_Channel5->CNDTR = MOTOR_TO_FUNCTION_NUM_MAX; /* Data size */ DMA1_Channel5->CCR |= DMA_CCR_EN; } /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
主机发送一个字节的数据,同时从机也要发送一个字节。
不能是主机发送完一整串数据之后,再由从机回应数据,所以你要认真考虑一下主从机的数据发送应答的处理流程。
以上是我的愚见
我也知道是从机处理问题,但在DMA下如何操作呢,直接往SPI->DR中先写一个数据来同步?
评分
查看全部评分