你的浏览器版本过低,可能导致网站不能正常访问!为了你能正常使用网站功能,请使用这些浏览器。
scfor123456 发表于 2019-8-27 18:46 这个到底是怎么回事?
wenyangzeng 发表于 2019-8-27 19:09 楼主那个SPI每次发送1个字节的无符号整数,如何发送有符号的数呢?
uwyciw100 发表于 2019-8-28 11:27 做过一次SPI从设备。当时是配置从设备使用NSS,和DMA。两者都要使用,才能正常工作。 ...
楼主那个SPI每次发送1个字节的无符号整数,如何发送有符号的数呢?
我发的是16位数据
#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接收中断中更新下次需发送的数据,主机是定时发送10个数据,从机接收后元数据返回给主机。