本帖最后由 Gohome_soon 于 2016-11-23 13:24 编辑
最近在调试stm32f042 spi 从模式时遇到一些问题
如下图,黄 {MOD}是发送,另外两个是时钟和042发送数据0XA1,
示波器打波形发现数据个别出现如
红 {MOD}圈出部分
本应该是低电平,确出现了比低电平高,比高电平低的现象,
另外一段ARM接受到的数据个别也有0xA1变成0xA7
SPI 接受DMA程序如下;
[mw_shl_code=c,true]u8 RxBuff;
void SPI1_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
//NVIC_InitTypeDef NVIC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource5,GPIO_AF_0);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_0);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_0);
/*!< Configure SPI_FLASH_SPI pins: SCK */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*!< Configure SPI_FLASH_SPI pins: MISO */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*!< Configure SPI_FLASH_SPI pins: MOSI */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init(SPI1, &SPI_InitStructure);
/* Initialize the FIFO threshold */
SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_QF);
SPI_Cmd(SPI1, ENABLE);
// SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE);
/* Enable the DMA peripheral */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* DMA Configuration -------------------------------------------------------*/
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
/* DMA channel Rx of SPI Configuration */
DMA_InitStructure.DMA_BufferSize = RxLen;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(SPI1->DR);
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RxData;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_Init(DMA1_Channel2, &DMA_InitStructure);
/* Enable the SPI Rx and Tx DMA requests */
SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Rx, ENABLE);
// SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Tx, ENABLE);
/* Enable the DMA channels */
DMA_Cmd(DMA1_Channel2, ENABLE);
// DMA_Cmd(SPIx_TX_DMA_CHANNEL, ENABLE);
}[/mw_shl_code]
[mw_shl_code=c,true]int main(void)
{
u32 i;
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
SYSCFG->CFGR1 |= SYSCFG_CFGR1_PA11_PA12_RMP;
USBD_Init(&USB_Device_dev,
&USR_desc,
&USBD_CDC_cb,
&USR_cb);
LED_GPIO_Config();
SPI1_Init();
USART_Config();
printf("
System Init......
");
while (1)
{
if(DMA_GetFlagStatus(DMA1_FLAG_TC2) == SET)//接受数据完成,
{
printf("%2X
",*(u32*)RxData);
if(*(u32*)RxData==0xFFCB5656)
{
SPI_SendData8(SPI1,0xC1);
i++;
}
*(u16*)RxData=0xFFFF;
DMA_ClearFlag(DMA1_FLAG_GL2);//清除DMA完成标志
DMA1_Channel2->CCR&=~(1<<0); //关闭DMA传输
DMA1_Channel2->CNDTR=RxLen; //DMA1,传输数据量
DMA1_Channel2->CCR|=1<<0; //开启DMA传输
}
}
}[/mw_shl_code]
042 SPI1 速度不是应该最高有12M啥,可是我使用042 从模式DMA接受速度只有3.5M,在快接受数据就出错了,
一周热门 更多>