我这几天在调STM32G030C8T6的硬件SPI1碰到了问题,SPI_SCK和SPI_MOSI没有信号输出,请帮忙看看,谢谢。
#define SPI_RX_MODE 0
#define SPI_TX_MODE 1
#define LCD_CS 'A',4
#define LCD_SCK 'A',5
#define LCD_SDA 'A',7
#define LCD_CD 'A',6
#define LCD_RST 'B',0
void GPIO_Config(UCHAR port,UCHAR pin,UCHAR mode,UCHAR altfun,UCHAR speed,UCHAR od,UCHAR pull,UCHAR level);
void SPI1_Init(void)
{
RCC->IOPENR |= 0x1; //Enable GPIO A Clock
GPIO_Config(LCD_CS, GPIO_OutputMode, GPIO_AF0, GPIO_SpeedMedium, GPIO_PushPull, GPIO_PullNo, 1);
GPIO_Config(LCD_SDA, GPIO_AltFunMode, GPIO_AF0, GPIO_SpeedMedium, GPIO_OD, GPIO_PullNo, 1);
GPIO_Config(LCD_SCK, GPIO_AltFunMode, GPIO_AF0, GPIO_SpeedMedium, GPIO_OD, GPIO_PullNo, 1);
RCC->APBENR2 |= 0x1000; //Enable SPI1 Clock
Delay(0x100);
RCC->APBRSTR2 |= 0x1000; //Reset SPI1
Delay(0x100);
RCC->APBRSTR2 &= 0xffffefff; //Release Reset SPI1
Delay(0x100);
SPI1->CR1 = 0xc324; //Bidi mode, Output enable, BaudRate=PCLK/32=2.25M, Master, PHASE0
SPI1->CR2 = 0x1700; //FIFO threshold: 8bit, Data size: 8bit, Motorola mode
SPI1->CR1 |= 0x40; //SPI enable
}
void SPI1_Dir_Sel(u8 mode)
{
if(SPI_RX_MODE==mode)
{
SPI1->CR1 &= 0xbfff;
}
else //TX mode
{
SPI1->CR1 |= 0x4000;
}
}
u8 TFT_WR(u8 cmd, u8 *txdat, u16 len)
{
u8 i;
u16 cnt;
ClrPin(LCD_CS);
ClrPin(LCD_CD); //command
/*cnt=0xffff;
while((SPI1->SR)>=0xfff && cnt--); //wait for TX FIFO empty
if(0==cnt)
{
SetPin(LCD_CS);
return FAIL;
}*/
SPI1->DR=cmd; //send command
Delay(20);
SetPin(LCD_CD); //data
for(i=0;i<len;i++)
{
/*cnt=0xffff;
while((SPI1->SR)>=0xfff && cnt--); //wait for TX FIFO empty
if(0==cnt)
{
SetPin(LCD_CS);
return FAIL;
}*/
Delay(20);
SPI1->DR=*(txdat+i);
}
SetPin(LCD_CS);
return OK;
}
u8 TFT_RD(u8 cmd, u8 *rxbuf, u16 len)
{
u8 i;
u16 cnt;
/*cnt=0xffff;
while((SPI1->SR)>=0xfff && cnt--); //wait for TX FIFO empty
if(0==cnt)
{
return FAIL;
}*/
ClrPin(LCD_CS);
ClrPin(LCD_CD); //command
Delay(0x100);
SPI1->DR=cmd; //send read command
/*while((SPI1->SR) & 0x600) //wait untill RX FIFO empty
{
i=(u8)(SPI1->DR);
}*/
Delay(80);
SPI1_Dir_Sel(SPI_RX_MODE); //change direction for RX
SetPin(LCD_CD); //data
for(i=0;i<len;i++)
{
/*cnt=0xffff;
while((SPI1->SR)>=0xfff && cnt--); //wait for FIFO empty
if(0==cnt)
{
SetPin(LCD_CS);
SPI1_Dir_Sel(SPI_TX_MODE); //change direction for TX(default)
return FAIL;
}*/
Delay(80);
SPI1->DR=0;
*(rxbuf+i)=SPI1->DR;
}
SetPin(LCD_CS);
SPI1_Dir_Sel(SPI_TX_MODE); //change direction for TX(default)
return OK;
} |