本篇以STM32F030C8+FT800为例说明如何实现平台移植。
从驱动篇来看,平台移植只要实现API函数ft8xxSleep、spiWriteBytes、spiReadBytes就可以实现EVE芯片的指令操作。
为了能准确延时,所以采用Systick实现(也可以采用Timer实现)。因为STM32足够快,定时器采用1ms定时。
#define SYSTICK_TIMER ((SystemCoreClock / 1000) * TIMER_MS) //1ms
SysTick_Config(SYSTICK_TIMER);
然后在中断函数内计时
void SysTick_Handler(void)
{
if (gTimerDelayCount != 0u)
{
--gTimerDelayCount;
}
}
ft8xxSleep函数如下:
void ft8xxSleep(uint16_t ms)
{
gTimerDelayCount = ms;
while (gTimerDelayCount != 0u);
}
spi使用的是STM32F030的SPI1,CS脚由GPIOA4控制。SPI1的GPIO初始化代码如下:
void gpioInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
/* SPI 1*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA, GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7);
}
SPI配置函数
void spiCfg(SPI_TypeDef* SPIx, uint16_t prescaler)
{
SPI_InitTypeDef SPI_InitStructure;
SPI_Cmd(SPIx, 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;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = prescaler;//SPI_BaudRatePrescaler_256;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPIx, &SPI_InitStructure);
/*Enable SPIx.NSS as a GPIO*/
//SPI_SSOutputCmd(SPIx, ENABLE);
SPI_Cmd(SPIx, ENABLE);
SPI_RxFIFOThresholdConfig(SPIx,SPI_RxFIFOThreshold_QF);
SPI_Cmd(SPIx, ENABLE);
}
定义一个全局变量gSPIx表示当前SPI驱动操作的SPI。
SPI_TypeDef* gSPIx;
SPI初始化函数如下:
void spiInit(uint16_t clk)
{
gSPIx = SPI1;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
spiCfg(SPI1, SPI_BaudRatePrescaler_2);
}
SPI可以是全双工通信,函数如下:
void spiTransferBytes(uint8_t* wrBuf, uint8_t* rdBuf, uint16_t len)
{
uint16_t i;
for(i = 0; i < len; i++)
{
// Loop while DR register in not emplty
while( SPI_I2S_GetFlagStatus(gSPIx, SPI_I2S_FLAG_TXE) == RESET);
// Send byte through the SPI1 peripheral
if(wrBuf != NULL)
SPI_SendData8(gSPIx, wrBuf[i]);
else
SPI_SendData8(gSPIx, 0xff);
// Wait to receive a byte
//while( SPI_I2S_GetFlagStatus(gSPIx, SPI_I2S_FLAG_TXE)== RESET);
while( SPI_I2S_GetFlagStatus(gSPIx, SPI_I2S_FLAG_RXNE) == RESET);
while( SPI_I2S_GetFlagStatus(gSPIx, SPI_I2S_FLAG_BSY) == SET);
if(rdBuf != NULL)
rdBuf[i] = SPI_ReceiveData8(gSPIx);
else
SPI_ReceiveData8(gSPIx);
}
}
spiWriteBytes、spiReadBytes用宏定义定义:
#define spiWriteBytes(wrBuf, len) spiTransferBytes(wrBuf, NULL, len)
#define spiReadBytes(wrBuf, len) spiTransferBytes(NULL, rdBuf, len)