我们产品上用的是GD32F103VCT6 为了充分发挥SPI的优势,开启了SPI的DMA功能,即从外部扩展的SRAM中用DMA方式将数据发送到SPI->DR寄存器,然后再让SPI自己发送出去,整个功能可以正常工作。但当做空气放电实验时,发现只要打十几下静电(4KV),spi就挂掉了,即用示波器看不到SPI的CLK管脚上的波形了。而我们换成ST的相同类型Pin对Pin的STM32F103VCT6,做同样的实验就没有问题。
还有GD32F103VCT6如果不启用SPI的DMA功能,及直接调用SPI_I2S_SendData(PRINTHEAD_SPI, TestPrintBuf[0]);发送数据,也没有问题。SPI和DMA的配置如下:
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
//开启复用功能时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
PRINTHEAD_SPI_PIN_ClockEnable();
PRINTHEAD_SPI_ClockEnable();
/* 配置SPI引脚: SCK, MOSI*/
GPIO_InitStructure.GPIO_Pin = PRINTHEAD_SPI_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(PRINTHEAD_SPI_PORT, &GPIO_InitStructure);
/* 单线发 */
SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
/* 主设备 */
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
/* 8位数据宽度 */
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
/* SPI模式选择 */
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
/* 软件实现CS,即使用GPIO代替CS */
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
/* APB1时钟为36MHZ/2 = 18MHZ */
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
/* 大端模式 */
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(PRINTHEAD_SPI, &SPI_InitStructure);
/* 使能SPI3的发送DMA */
SPI_I2S_DMACmd(PRINTHEAD_SPI, SPI_I2S_DMAReq_Tx, ENABLE);
SPI_Cmd(PRINTHEAD_SPI, ENABLE);
PRINTHEAD_SPI_DMA_ClockEnable();
DMA_DeInit(PRINTHEAD_SPI_DMA_CHANNEL);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(PRINTHEAD_SPI->DR);
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&TestPrintBuf[0];
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = sizeof(TestPrintBuf);
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(PRINTHEAD_SPI_DMA_CHANNEL, &DMA_InitStructure);
DMA_Cmd(PRINTHEAD_SPI_DMA_CHANNEL, ENABLE);
主函数中代码流程如下:
while(1)
{
while((PRINTHEAD_SPI_DMAx->ISR & PRINTHEAD_SPI_DMA_FLAG) == (uint32_t)RESET)
{
UI_LED_FlashEver(LED_Green, 200, 300);
}
PRINTHEAD_SPI_DMAx->IFCR = (uint32_t)(PRINTHEAD_SPI_DMA_FLAG);
PRINTHEAD_SPI_DMA_CHANNEL->CCR &= (uint32_t)(~DMA_CCR2_EN);
PRINTHEAD_SPI_DMA_CHANNEL->CMAR = (uint32_t)&TestPrintBuf[0];
PRINTHEAD_SPI_DMA_CHANNEL->CNDTR = sizeof(TestPrintBuf);
PRINTHEAD_SPI_DMA_CHANNEL->CCR |= DMA_CCR2_EN;
}
如有高手遇到过这样的情况,还请帮忙看一下,问题出在哪?
|