STM32使用HAL库硬件SPI驱动WK2124一拖四SPI转四路串口芯片
STM32基础工程生成
首先使用STM32CUBEMX生成STM32的基础工程,配置时钟到72M主频:
配置SPI前,首先查看WK2124的芯片手册,手册里面说明了WK2124的SPI最高支持到10M并且使用SPI模式0,SPI的模式0就是时钟空闲电平为低电平且在第一个时钟延采样: 所以STM32CUBEMX需要这样配置: IO配置:
- WK2124的硬件复位引脚,这个引脚最好接上,使用STM32的IO进行控制
- WK2124的片选引脚
- 如果用WK2124的中断,那么还需要配置一个IO为外部中断
WK2124驱动代码
代码已经测试通过无问题,贴一下:
头文件
#ifndef SRC_WK2XXX_H_
#define SRC_WK2XXX_H_
#include "main.h"
#define WK2XXX_GENA 0X00
#define WK2XXX_GRST 0X01
#define WK2XXX_GMUT 0X02
#define WK2XXX_GIER 0X10
#define WK2XXX_GIFR 0X11
#define WK2XXX_GPDIR 0X21
#define WK2XXX_GPDAT 0X31
#define WK2XXX_SPAGE 0X03
#define WK2XXX_SCR 0X04
#define WK2XXX_LCR 0X05
#define WK2XXX_FCR 0X06
#define WK2XXX_SIER 0X07
#define WK2XXX_SIFR 0X08
#define WK2XXX_TFCNT 0X09
#define WK2XXX_RFCNT 0X0A
#define WK2XXX_FSR 0X0B
#define WK2XXX_LSR 0X0C
#define WK2XXX_FDAT 0X0D
#define WK2XXX_FWCR 0X0E
#define WK2XXX_RS485 0X0F
#define WK2XXX_BAUD1 0X04
#define WK2XXX_BAUD0 0X05
#define WK2XXX_PRES 0X06
#define WK2XXX_RFTL 0X07
#define WK2XXX_TFTL 0X08
#define WK2XXX_FWTH 0X09
#define WK2XXX_FWTL 0X0A
#define WK2XXX_XON1 0X0B
#define WK2XXX_XOFF1 0X0C
#define WK2XXX_SADR 0X0D
#define WK2XXX_SAEN 0X0E
#define WK2XXX_RTSDLY 0X0F
#define WK2XXX_UT4EN 0x08
#define WK2XXX_UT3EN 0x04
#define WK2XXX_UT2EN 0x02
#define WK2XXX_UT1EN 0x01
#define WK2XXX_UT4SLEEP 0x80
#define WK2XXX_UT3SLEEP 0x40
#define WK2XXX_UT2SLEEP 0x20
#define WK2XXX_UT1SLEEP 0x10
#define WK2XXX_UT4RST 0x08
#define WK2XXX_UT3RST 0x04
#define WK2XXX_UT2RST 0x02
#define WK2XXX_UT1RST 0x01
#define WK2XXX_UT4IE 0x08
#define WK2XXX_UT3IE 0x04
#define WK2XXX_UT2IE 0x02
#define WK2XXX_UT1IE 0x01
#define WK2XXX_UT4INT 0x08
#define WK2XXX_UT3INT 0x04
#define WK2XXX_UT2INT 0x02
#define WK2XXX_UT1INT 0x01
#define WK2XXX_SPAGE0 0x00
#define WK2XXX_SPAGE1 0x01
#define WK2XXX_SLEEPEN 0x04
#define WK2XXX_TXEN 0x02
#define WK2XXX_RXEN 0x01
#define WK2XXX_BREAK 0x20
#define WK2XXX_IREN 0x10
#define WK2XXX_PAEN 0x08
#define WK2XXX_PAM1 0x04
#define WK2XXX_PAM0 0x02
#define WK2XXX_STPL 0x01
#define WK2XXX_FERR_IEN 0x80
#define WK2XXX_CTS_IEN 0x40
#define WK2XXX_RTS_IEN 0x20
#define WK2XXX_XOFF_IEN 0x10
#define WK2XXX_TFEMPTY_IEN 0x08
#define WK2XXX_TFTRIG_IEN 0x04
#define WK2XXX_RXOUT_IEN 0x02
#define WK2XXX_RFTRIG_IEN 0x01
#define WK2XXX_FERR_INT 0x80
#define WK2XXX_CTS_INT 0x40
#define WK2XXX_RTS_INT 0x20
#define WK2XXX_XOFF_INT 0x10
#define WK2XXX_TFEMPTY_INT 0x08
#define WK2XXX_TFTRIG_INT 0x04
#define WK2XXX_RXOVT_INT 0x02
#define WK2XXX_RFTRIG_INT 0x01
#define WK2XXX_RFOE 0x80
#define WK2XXX_RFBI 0x40
#define WK2XXX_RFFE 0x20
#define WK2XXX_RFPE 0x10
#define WK2XXX_RDAT 0x08
#define WK2XXX_TDAT 0x04
#define WK2XXX_TFULL 0x02
#define WK2XXX_TBUSY 0x01
#define WK2XXX_OE 0x08
#define WK2XXX_BI 0x04
#define WK2XXX_FE 0x02
#define WK2XXX_PE 0x01
uint16_t WK_PortSendData(uint8_t port, const void* buf, uint16_t len);
uint16_t WK_PortRecvData(uint8_t port, void* buf, uint16_t len);
void WK_PortInit(uint8_t port, uint32_t fosc, uint32_t baudrate);
void WK_IRQHandler(void);
void WK_Rst(void);
#endif
源文件
#include "wk2xxx.h"
#include "spi.h"
#define WK_SPI_Handle (&hspi2)
#define WK_GPIO_RST_L() HAL_GPIO_WritePin(WK2124_RST1_GPIO_Port, WK2124_RST1_Pin, GPIO_PIN_RESET)
#define WK_GPIO_RST_H() HAL_GPIO_WritePin(WK2124_RST1_GPIO_Port, WK2124_RST1_Pin, GPIO_PIN_SET)
#define WK_GPIO_SEL_L() HAL_GPIO_WritePin(WK2124_CS1_GPIO_Port, WK2124_CS1_Pin, GPIO_PIN_RESET)
#define WK_GPIO_SEL_H() HAL_GPIO_WritePin(WK2124_CS1_GPIO_Port, WK2124_CS1_Pin, GPIO_PIN_SET)
static void wk_delay_ms(uint32_t nms)
{
HAL_Delay(nms);
}
static uint8_t WK_SPI_ReadWriteByte(uint8_t TxData)
{
uint8_t RxData = 0X00;
if (HAL_SPI_TransmitReceive(WK_SPI_Handle, &TxData, &RxData, 1, 10) != HAL_OK)
{
RxData = 0XFF;
}
return RxData;
}
void WK_Rst(void)
{
WK_GPIO_RST_L();
wk_delay_ms(50);
WK_GPIO_RST_H();
wk_delay_ms(50);
}
void WK_WriteGReg(uint8_t reg, uint8_t value)
{
WK_GPIO_SEL_L();
WK_SPI_ReadWriteByte(reg);
WK_SPI_ReadWriteByte(value);
WK_GPIO_SEL_H();
}
uint8_t WK_ReadGReg(uint8_t reg)
{
uint8_t value = 0X00;
WK_GPIO_SEL_L();
WK_SPI_ReadWriteByte(0X40 | reg);
value = WK_SPI_ReadWriteByte(0X00);
WK_GPIO_SEL_H();
return value;
}
void WK_WriteSReg(uint8_t port, uint8_t reg, uint8_t value)
{
WK_GPIO_SEL_L();
WK_SPI_ReadWriteByte(((port - 1) << 4) | reg);
WK_SPI_ReadWriteByte(value);
WK_GPIO_SEL_H();
}
uint8_t WK_ReadSReg(uint8_t port, uint8_t reg)
{
uint8_t value = 0X00;
WK_GPIO_SEL_L();
WK_SPI_ReadWriteByte(0X40 | ((port - 1) << 4) | reg);
value = WK_SPI_ReadWriteByte(0X00);
WK_GPIO_SEL_H();
return value;
}
void WK_WriteFIFO(uint8_t port, const void* buf, uint16_t len)
{
const uint8_t* p = (const uint8_t *)buf;
WK_GPIO_SEL_L();
WK_SPI_ReadWriteByte(0X80 | ((port - 1) << 4));
while (len--)
{
WK_SPI_ReadWriteByte(*p++);
}
WK_GPIO_SEL_H();
}
void WK_ReadFIFO(uint8_t port, void* buf, uint16_t len)
{
uint8_t* p = (uint8_t *)buf;
WK_GPIO_SEL_L();
WK_SPI_ReadWriteByte(0XC0 | ((port - 1) << 4));
while (len--)
{
*p++ = WK_SPI_ReadWriteByte(0X00);
}
WK_GPIO_SEL_H();
}
void WK_PortCalcBaudrate(uint32_t fosc, uint32_t baudrate, uint8_t* BAUD0, uint8_t* BAUD1, uint8_t* PRES)
{
float value = (float)fosc / (float)(baudrate << 4);
*BAUD0 = (((uint32_t)value) - 1) & 0XFF;
*BAUD1 = ((((uint32_t)value) - 1) >> 8) & 0XFF;
*PRES = ((uint32_t)(value * 10)) % 10;
}
uint16_t WK_PortSendData(uint8_t port, const void* buf, uint16_t len)
{
uint8_t state = 0;
uint16_t nsend = 0;
state = WK_ReadSReg(port, WK2XXX_FSR);
if (state & WK2XXX_TFULL)
{
nsend = 0;
} else
{
state = WK_ReadSReg(port, WK2XXX_TFCNT);
nsend = 256 - state;
nsend = nsend >= len ? len : nsend;
WK_WriteFIFO(port, buf, nsend);
}
return nsend;
}
uint16_t WK_PortRecvData(uint8_t port, void* buf, uint16_t len)
{
uint8_t state = 0;
uint8_t nread = 0;
state = WK_ReadSReg(port, WK2XXX_FSR);
if (state & WK2XXX_RDAT)
{
nread = WK_ReadSReg(port, WK2XXX_RFCNT);
nread = len >= nread ? nread : len;
WK_ReadFIFO(port, buf, nread);
}
return nread;
}
void WK_PortInit(uint8_t port, uint32_t fosc, uint32_t baudrate)
{
uint8_t BAUD0 = 0, BAUD1 = 0, PRES = 0, value = 0, ret = 0;
value = WK_ReadGReg(WK2XXX_GENA);
value |= (1 << (port - 1));
WK_WriteGReg(WK2XXX_GENA, value);
ret = WK_ReadGReg(WK2XXX_GENA);
value = WK_ReadGReg(WK2XXX_GRST);
value |= (1 << (port - 1));
WK_WriteGReg(WK2XXX_GRST, value);
ret = WK_ReadGReg(WK2XXX_GRST);
value = WK_ReadGReg(WK2XXX_GIER);
value |= (1 << (port - 1));
WK_WriteGReg(WK2XXX_GIER, value);
ret = WK_ReadGReg(WK2XXX_GIER);
value = WK_ReadSReg(port, WK2XXX_SIER);
value |= WK2XXX_RFTRIG_IEN;
WK_WriteSReg(port, WK2XXX_SIER, value);
ret = WK_ReadSReg(port, WK2XXX_SIER);
value = 0;
value |= (1 << 0);
value |= (1 << 1);
value |= (1 << 2);
value |= (1 << 3);
value |= (0 << 4);
value |= (0 << 6);
WK_WriteSReg(port, WK2XXX_FCR, value);
ret = WK_ReadSReg(port, WK2XXX_FCR);
WK_WriteSReg(port, WK2XXX_SPAGE, 1);
ret = WK_ReadSReg(port, WK2XXX_SPAGE);
WK_WriteSReg(port, WK2XXX_RFTL, 10);
ret = WK_ReadSReg(port, WK2XXX_RFTL);
WK_WriteSReg(port, WK2XXX_TFTL, 10);
ret = WK_ReadSReg(port, WK2XXX_TFTL);
WK_PortCalcBaudrate(fosc, baudrate, &BAUD0, &BAUD1, &PRES);
WK_WriteSReg(port, WK2XXX_BAUD1, BAUD1);
ret = WK_ReadSReg(port, WK2XXX_BAUD1);
WK_WriteSReg(port, WK2XXX_BAUD0, BAUD0);
ret = WK_ReadSReg(port, WK2XXX_BAUD0);
WK_WriteSReg(port, WK2XXX_PRES, PRES);
ret = WK_ReadSReg(port, WK2XXX_PRES);
WK_WriteSReg(port, WK2XXX_SPAGE, 0);
ret = WK_ReadSReg(port, WK2XXX_SPAGE);
value = WK_ReadSReg(port, WK2XXX_SCR);
value |= WK2XXX_TXEN;
value |= WK2XXX_RXEN;
WK_WriteSReg(port, WK2XXX_SCR, value);
ret = WK_ReadSReg(port, WK2XXX_SCR);
}
void WK_IRQHandler(void)
{
int i = 0;
uint8_t GIFR = 0, SIFR = 0;
GIFR = WK_ReadGReg(WK2XXX_GIFR);
for (i = 0; i < 4; i++)
{
if ((GIFR >> i) & 0X01)
{
SIFR = WK_ReadSReg((i + 1), WK2XXX_SIFR);
if (SIFR & WK2XXX_RFTRIG_INT)
{
}
if (SIFR & WK2XXX_RXOVT_INT)
{
}
if (SIFR & WK2XXX_TFTRIG_INT)
{
}
if (SIFR & WK2XXX_TFEMPTY_INT)
{
}
if (SIFR & WK2XXX_FERR_INT)
{
}
}
}
}
|