GD32除錯Si4438,SPI通訊終於調通
阿新 • • 發佈:2018-11-14
最近用GD32去除錯si4438,SPI讀取資料總是出錯,因為之前做過GD32移植STM32的SPI程式,初始化程式碼如下:訪問訪問W25Q16 FLASH晶片是沒有任何問題的,但是用在si4438上就是不行。
讀寫函式如下:
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); //使能SPI時鐘 SPI_Cmd(SPI2, DISABLE); SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; // SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //STM32 // SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //STM32 SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //GD SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //GD SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI1, &SPI_InitStructure); SPI_Cmd(SPI1, ENABLE);
最後我嘗試著修改各種配置,結果卻狠狠的扇了我一巴掌,原來上面就應該使用STM32的配置, 配置時鐘空閒時為Low電平,第1個時鐘沿捕獲資料;
正確配置如下:
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); //使能SPI時鐘 SPI_Cmd(SPI2, DISABLE); SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //STM32 SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //STM32 // SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //GD // SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //GD SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI1, &SPI_InitStructure); SPI_Cmd(SPI1, ENABLE);
讀寫函式如下:
u8 SPI_SendByte(u8 byte) { /* Loop while DR register in not emplty */ while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); /* Send byte through the SPI2 peripheral */ SPI_I2S_SendData(SPI1, byte); /* Wait to receive a byte */ while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); /* Return the byte read from the SPI bus */ return SPI_I2S_ReceiveData(SPI1); } u8 SPI_ReadByte(void) { return (SPI_SendByte(0x00)); }