20231023_InSituLab_emb/InsituLabProject_QRS/Src/Usr/Bsp/McuBspSpi.c
Rjh913828050 4e26d518da 类型:更细
内容:更新十四五程序
人员:任家豪
2024-07-31 08:56:07 +08:00

260 lines
10 KiB
C

#include "McuBspSpi.h"
typedef struct {
uint8_t SpiEnable;
uint32_t SpiSpeed;
uint8_t SpiMoto;
uint8_t SpiMode;
uint8_t SpiDataWidth;
uint8_t ReadWriteSta;
} BSP_SPI_OBJ;
static BSP_SPI_OBJ gMcuBspSpiObj[MCU_BSP_SPI_ALL_NUM] = {0};
int McuBspSpiInit(uint8_t SpiId, uint8_t SpiMoto, uint8_t SpiMode, uint32_t SpiSpeed, uint8_t SpiDataWidth)
{
uint32_t pui32DataRx;
if ((SpiId >= MCU_BSP_SPI_ALL_NUM) || (SpiDataWidth < 4) || (SpiDataWidth > 16)) {
return -1;
}
while (gMcuBspSpiObj[SpiId].ReadWriteSta == MCU_BSP_SPI_READWRITE_ENABLE) {
}
gMcuBspSpiObj[SpiId].SpiEnable = MCU_BSP_SPI_OFF;
if (SpiId == MCU_BSP_SPI0) {
SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI0);
SysCtlPeripheralEnable(MCU_BSP_SPI0_CLK_GPIO_CLK);
SysCtlPeripheralEnable(MCU_BSP_SPI0_FSS_GPIO_CLK);
SysCtlPeripheralEnable(MCU_BSP_SPI0_TX_GPIO_CLK);
SysCtlPeripheralEnable(MCU_BSP_SPI0_RX_GPIO_CLK);
GPIOPinConfigure(MCU_BSP_SPI0_CLK_GPIO_CONF);
GPIOPinConfigure(MCU_BSP_SPI0_FSS_GPIO_CONF);
GPIOPinConfigure(MCU_BSP_SPI0_TX_GPIO_CONF);
GPIOPinConfigure(MCU_BSP_SPI0_RX_GPIO_CONF);
GPIOPinTypeSSI(MCU_BSP_SPI0_CLK_GPIO, MCU_BSP_SPI0_CLK_GPIO_PIN);
GPIOPinTypeSSI(MCU_BSP_SPI0_FSS_GPIO, MCU_BSP_SPI0_FSS_GPIO_PIN);
GPIOPinTypeSSI(MCU_BSP_SPI0_TX_GPIO, MCU_BSP_SPI0_TX_GPIO_PIN);
GPIOPinTypeSSI(MCU_BSP_SPI0_RX_GPIO, MCU_BSP_SPI0_RX_GPIO_PIN);
SSIConfigSetExpClk(SSI0_BASE, SysFreqGet(), SpiMoto, SpiMode, SpiSpeed, SpiDataWidth);
SSIEnable(SSI0_BASE);
while(SSIDataGetNonBlocking(SSI0_BASE, &pui32DataRx)) {
}
} else if (SpiId == MCU_BSP_SPI1) {
SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI1);
SysCtlPeripheralEnable(MCU_BSP_SPI1_CLK_GPIO_CLK);
SysCtlPeripheralEnable(MCU_BSP_SPI1_FSS_GPIO_CLK);
SysCtlPeripheralEnable(MCU_BSP_SPI1_TX_GPIO_CLK);
SysCtlPeripheralEnable(MCU_BSP_SPI1_RX_GPIO_CLK);
GPIOPinConfigure(MCU_BSP_SPI1_CLK_GPIO_CONF);
GPIOPinConfigure(MCU_BSP_SPI1_FSS_GPIO_CONF);
GPIOPinConfigure(MCU_BSP_SPI1_TX_GPIO_CONF);
GPIOPinConfigure(MCU_BSP_SPI1_RX_GPIO_CONF);
GPIOPinTypeSSI(MCU_BSP_SPI1_CLK_GPIO, MCU_BSP_SPI1_CLK_GPIO_PIN);
GPIOPinTypeSSI(MCU_BSP_SPI1_FSS_GPIO, MCU_BSP_SPI1_FSS_GPIO_PIN);
GPIOPinTypeSSI(MCU_BSP_SPI1_TX_GPIO, MCU_BSP_SPI1_TX_GPIO_PIN);
GPIOPinTypeSSI(MCU_BSP_SPI1_RX_GPIO, MCU_BSP_SPI1_RX_GPIO_PIN);
SSIConfigSetExpClk(SSI1_BASE, SysFreqGet(), SpiMoto, SpiMode, SpiSpeed, SpiDataWidth);
SSIEnable(SSI1_BASE);
while(SSIDataGetNonBlocking(SSI1_BASE, &pui32DataRx)) {
}
} else if (SpiId == MCU_BSP_SPI2) {
SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI2);
SysCtlPeripheralEnable(MCU_BSP_SPI2_CLK_GPIO_CLK);
SysCtlPeripheralEnable(MCU_BSP_SPI2_FSS_GPIO_CLK);
SysCtlPeripheralEnable(MCU_BSP_SPI2_TX_GPIO_CLK);
SysCtlPeripheralEnable(MCU_BSP_SPI2_RX_GPIO_CLK);
GPIOPinConfigure(MCU_BSP_SPI2_CLK_GPIO_CONF);
GPIOPinConfigure(MCU_BSP_SPI2_FSS_GPIO_CONF);
GPIOPinConfigure(MCU_BSP_SPI2_TX_GPIO_CONF);
GPIOPinConfigure(MCU_BSP_SPI2_RX_GPIO_CONF);
GPIOPinTypeSSI(MCU_BSP_SPI2_CLK_GPIO, MCU_BSP_SPI2_CLK_GPIO_PIN);
GPIOPinTypeSSI(MCU_BSP_SPI2_FSS_GPIO, MCU_BSP_SPI2_FSS_GPIO_PIN);
GPIOPinTypeSSI(MCU_BSP_SPI2_TX_GPIO, MCU_BSP_SPI2_TX_GPIO_PIN);
GPIOPinTypeSSI(MCU_BSP_SPI2_RX_GPIO, MCU_BSP_SPI2_RX_GPIO_PIN);
SSIConfigSetExpClk(SSI2_BASE, SysFreqGet(), SpiMoto, SpiMode, SpiSpeed, SpiDataWidth);
SSIEnable(SSI2_BASE);
while(SSIDataGetNonBlocking(SSI2_BASE, &pui32DataRx)) {
}
} else if (SpiId == MCU_BSP_SPI3) {
SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI3);
SysCtlPeripheralEnable(MCU_BSP_SPI3_CLK_GPIO_CLK);
SysCtlPeripheralEnable(MCU_BSP_SPI3_FSS_GPIO_CLK);
SysCtlPeripheralEnable(MCU_BSP_SPI3_TX_GPIO_CLK);
SysCtlPeripheralEnable(MCU_BSP_SPI3_RX_GPIO_CLK);
GPIOPinConfigure(MCU_BSP_SPI3_CLK_GPIO_CONF);
GPIOPinConfigure(MCU_BSP_SPI3_FSS_GPIO_CONF);
GPIOPinConfigure(MCU_BSP_SPI3_TX_GPIO_CONF);
GPIOPinConfigure(MCU_BSP_SPI3_RX_GPIO_CONF);
GPIOPinTypeSSI(MCU_BSP_SPI3_CLK_GPIO, MCU_BSP_SPI3_CLK_GPIO_PIN);
GPIOPinTypeSSI(MCU_BSP_SPI3_FSS_GPIO, MCU_BSP_SPI3_FSS_GPIO_PIN);
GPIOPinTypeSSI(MCU_BSP_SPI3_TX_GPIO, MCU_BSP_SPI3_TX_GPIO_PIN);
GPIOPinTypeSSI(MCU_BSP_SPI3_RX_GPIO, MCU_BSP_SPI3_RX_GPIO_PIN);
SSIConfigSetExpClk(SSI3_BASE, SysFreqGet(), SpiMoto, SpiMode, SpiSpeed, SpiDataWidth);
SSIEnable(SSI3_BASE);
while(SSIDataGetNonBlocking(SSI3_BASE, &pui32DataRx)) {
}
}
gMcuBspSpiObj[SpiId].SpiMode = SpiMode;
gMcuBspSpiObj[SpiId].SpiDataWidth = SpiDataWidth;
gMcuBspSpiObj[SpiId].SpiSpeed = SpiSpeed;
gMcuBspSpiObj[SpiId].SpiMoto = SpiMoto;
gMcuBspSpiObj[SpiId].SpiEnable = MCU_BSP_SPI_ON;
return 0;
}
int McuBspSpiWriteReadByte(uint8_t SpiId, uint8_t TxData, uint8_t *RxData)
{
uint32_t rec = 0;
CPU_SR_ALLOC();
if ((SpiId >= MCU_BSP_SPI_ALL_NUM) || (RxData == NULL)) {
return -1;
}
if (gMcuBspSpiObj[SpiId].SpiEnable != MCU_BSP_SPI_ON) {
return -1;
}
gMcuBspSpiObj[SpiId].ReadWriteSta = MCU_BSP_SPI_READWRITE_ENABLE;
OS_ENTER_CRITICAL();
if (SpiId == MCU_BSP_SPI0) {
while(SSIDataGetNonBlocking(SSI0_BASE, &rec)) {
}
SSIDataPut(SSI0_BASE, TxData);
while (SSIBusy(SSI0_BASE)){
}
SSIDataGet(SSI0_BASE, &rec);
} else if (SpiId == MCU_BSP_SPI1) {
while(SSIDataGetNonBlocking(SSI1_BASE, &rec)) {
}
SSIDataPut(SSI1_BASE, TxData);
while (SSIBusy(SSI1_BASE)){
}
SSIDataGet(SSI1_BASE, &rec);
} else if (SpiId == MCU_BSP_SPI2) {
while(SSIDataGetNonBlocking(SSI2_BASE, &rec)) {
}
SSIDataPut(SSI2_BASE, TxData);
while (SSIBusy(SSI2_BASE)){
}
SSIDataGet(SSI2_BASE, &rec);
} else if (SpiId == MCU_BSP_SPI3) {
while(SSIDataGetNonBlocking(SSI3_BASE, &rec)) {
}
SSIDataPut(SSI3_BASE, TxData);
while (SSIBusy(SSI3_BASE)){
}
SSIDataGet(SSI3_BASE, &rec);
}
*RxData = (uint8_t)(rec & 0xff);
OS_EXIT_CRITICAL();
gMcuBspSpiObj[SpiId].ReadWriteSta = MCU_BSP_SPI_READWRITE_DISABLE;
return 0;
}
int McuBspSpiModifyConf(uint8_t SpiId, uint8_t *SpiMoto, uint8_t *SpiMode, uint32_t *SpiSpeed, uint8_t *SpiDataWidth)
{
if (SpiId >= MCU_BSP_SPI_ALL_NUM) {
return -1;
}
if (SpiDataWidth != NULL) {
if ((*SpiDataWidth < 4) || (*SpiDataWidth > 16) ) {
return -1;
}
}
if (gMcuBspSpiObj[SpiId].SpiEnable == MCU_BSP_SPI_OFF) {
return -1;
}
while (gMcuBspSpiObj[SpiId].ReadWriteSta == MCU_BSP_SPI_READWRITE_ENABLE) {
}
gMcuBspSpiObj[SpiId].SpiEnable = MCU_BSP_SPI_OFF;
if (SpiMoto != NULL) {
gMcuBspSpiObj[SpiId].SpiMoto = *SpiMoto;
}
if (SpiMode != NULL) {
gMcuBspSpiObj[SpiId].SpiMode = *SpiMode;
}
if (SpiSpeed != NULL) {
gMcuBspSpiObj[SpiId].SpiSpeed = *SpiSpeed;
}
if (SpiDataWidth != NULL) {
gMcuBspSpiObj[SpiId].SpiDataWidth = *SpiDataWidth;
}
if (SpiId == MCU_BSP_SPI0) {
while (SSIBusy(SSI0_BASE)){
}
SSIDisable(SSI0_BASE);
SSIConfigSetExpClk(SSI0_BASE, SysFreqGet(), gMcuBspSpiObj[SpiId].SpiMoto, gMcuBspSpiObj[SpiId].SpiMode,
gMcuBspSpiObj[SpiId].SpiSpeed, gMcuBspSpiObj[SpiId].SpiDataWidth);
SSIEnable(SSI0_BASE);
} else if (SpiId == MCU_BSP_SPI1) {
while (SSIBusy(SSI1_BASE)){
}
SSIDisable(SSI1_BASE);
SSIConfigSetExpClk(SSI1_BASE, SysFreqGet(), gMcuBspSpiObj[SpiId].SpiMoto, gMcuBspSpiObj[SpiId].SpiMode,
gMcuBspSpiObj[SpiId].SpiSpeed, gMcuBspSpiObj[SpiId].SpiDataWidth);
SSIEnable(SSI1_BASE);
} else if (SpiId == MCU_BSP_SPI2) {
while (SSIBusy(SSI2_BASE)){
}
SSIDisable(SSI2_BASE);
SSIConfigSetExpClk(SSI2_BASE, SysFreqGet(), gMcuBspSpiObj[SpiId].SpiMoto, gMcuBspSpiObj[SpiId].SpiMode,
gMcuBspSpiObj[SpiId].SpiSpeed, gMcuBspSpiObj[SpiId].SpiDataWidth);
SSIEnable(SSI2_BASE);
} else if (SpiId == MCU_BSP_SPI3) {
while (SSIBusy(SSI3_BASE)){
}
SSIDisable(SSI3_BASE);
SSIConfigSetExpClk(SSI3_BASE, SysFreqGet(), gMcuBspSpiObj[SpiId].SpiMoto, gMcuBspSpiObj[SpiId].SpiMode,
gMcuBspSpiObj[SpiId].SpiSpeed, gMcuBspSpiObj[SpiId].SpiDataWidth);
SSIEnable(SSI3_BASE);
}
gMcuBspSpiObj[SpiId].SpiEnable = MCU_BSP_SPI_ON;
return 0;
}
int McuBspSpiCs(uint8_t SpiId, uint8_t Status)
{
if (SpiId >= MCU_BSP_SPI_ALL_NUM) {
return -1;
}
if (gMcuBspSpiObj[SpiId].SpiEnable == MCU_BSP_SPI_OFF) {
return -1;
}
if (SpiId == MCU_BSP_SPI0) {
if (Status == MCU_BSP_SPI_CS_DOWN) {
GPIOPinWrite(MCU_BSP_SPI0_FSS_GPIO, MCU_BSP_SPI0_FSS_GPIO_PIN, ~MCU_BSP_SPI0_FSS_GPIO_PIN);
} else {
GPIOPinWrite(MCU_BSP_SPI0_FSS_GPIO, MCU_BSP_SPI0_FSS_GPIO_PIN, MCU_BSP_SPI0_FSS_GPIO_PIN);
}
} else if (SpiId == MCU_BSP_SPI1) {
if (Status == MCU_BSP_SPI_CS_DOWN) {
GPIOPinWrite(MCU_BSP_SPI1_FSS_GPIO, MCU_BSP_SPI1_FSS_GPIO_PIN, ~MCU_BSP_SPI1_FSS_GPIO_PIN);
} else {
GPIOPinWrite(MCU_BSP_SPI1_FSS_GPIO, MCU_BSP_SPI1_FSS_GPIO_PIN, MCU_BSP_SPI1_FSS_GPIO_PIN);
}
} else if (SpiId == MCU_BSP_SPI2) {
if (Status == MCU_BSP_SPI_CS_DOWN) {
GPIOPinWrite(MCU_BSP_SPI2_FSS_GPIO, MCU_BSP_SPI2_FSS_GPIO_PIN, ~MCU_BSP_SPI2_FSS_GPIO_PIN);
} else {
GPIOPinWrite(MCU_BSP_SPI2_FSS_GPIO, MCU_BSP_SPI2_FSS_GPIO_PIN, MCU_BSP_SPI2_FSS_GPIO_PIN);
}
} else if (SpiId == MCU_BSP_SPI3) {
if (Status == MCU_BSP_SPI_CS_DOWN) {
GPIOPinWrite(MCU_BSP_SPI3_FSS_GPIO, MCU_BSP_SPI3_FSS_GPIO_PIN, ~MCU_BSP_SPI3_FSS_GPIO_PIN);
} else {
GPIOPinWrite(MCU_BSP_SPI3_FSS_GPIO, MCU_BSP_SPI3_FSS_GPIO_PIN, MCU_BSP_SPI3_FSS_GPIO_PIN);
}
}
return 0;
}