#include "spirw.h" #ifndef SOFT_SPI1 #define SOFT_SPI1 0 #endif #ifndef SOFT_SPI2 #define SOFT_SPI2 0 #endif #ifndef SOFT_SPI3 #define SOFT_SPI3 0 #endif #ifndef SOFT_SPI4 #define SOFT_SPI4 0 #endif #define SPIRW_PROC(spi) \ u8 g_spi##spi##InitFlag = 0; \ void InitSpi##spi(void) \ { \ LL_SPI_Enable(SPI##spi); \ g_spi##spi##InitFlag = 1; \ } \ u8 IsSpi##spi##Inited(void) \ { \ return g_spi##spi##InitFlag; \ } \ /* \ // 功能: SPI交换1个字节数据 \ // 参数: dat, 待发送的数据 \ // 返回: 读到的数据 \ // 说明: 无 \ */ \ u8 Spi##spi##SwapByte(u8 dat) \ { \ while (LL_SPI_IsActiveFlag_TXE(SPI##spi) == 0); /* 等待数据寄存器空 */ \ LL_SPI_TransmitData8(SPI##spi, dat); \ while (LL_SPI_IsActiveFlag_RXNE(SPI##spi) == 0); /* 等待发送完成--发送完成时接收也完成 */ \ return LL_SPI_ReceiveData8(SPI##spi); \ } \ \ /* 发送字节 */ \ void Spi##spi##WriteByte(u8 txData) \ { \ Spi##spi##SwapByte(txData); \ } \ \ /* 接收字节 */ \ u8 Spi##spi##ReadByte(void) \ { \ return Spi##spi##SwapByte(0xff); \ } \ /* \ // 功能: SPI交换1个字数据 \ // 参数: dat, 待发送的数据 \ // 返回: 读到的数据 \ // 说明: 无 \ */ \ u16 Spi##spi##SwapWord(u16 dat) \ { \ while (LL_SPI_IsActiveFlag_TXE(SPI##spi) == 0); /* 等待数据寄存器空 */ \ LL_SPI_TransmitData16(SPI##spi, dat); \ while (LL_SPI_IsActiveFlag_RXNE(SPI##spi) == 0); /* 等待发送完成--发送完成时接收也完成 */ \ return LL_SPI_ReceiveData16(SPI##spi); \ } \ \ /* 发送字 */ \ void Spi##spi##WriteWord(u16 txData) \ { \ Spi##spi##SwapWord(txData); \ } \ \ /* 接收字 */ \ u16 Spi##spi##ReadWord(void) \ { \ return Spi##spi##SwapWord(0xffff); \ } \ //------ #define SOFT_SPIRW_PROC(spi) \ u8 g_spi##spi##InitFlag = 0; \ void InitSpi##spi(void) \ { \ g_spi##spi##InitFlag = 1; \ } \ u8 IsSpi##spi##Inited(void) \ { \ return g_spi##spi##InitFlag; \ } \ /* \ // 功能: SPI交换1个字节数据 \ // 参数: dat, 待发送的数据 \ // 返回: 读到的数据 \ */ \ u8 Spi##spi##SwapByte(u8 dat) \ {/* 软件模拟标准SPI,CLK上升沿读写数据 */ \ u8 rslt, mod; \ rslt = 0; \ mod = 0x80; \ \ Spi##spi##ClockLow(); \ \ for (int i = 0; i < 8; i++) \ { \ /* 设置好数据 */ \ if ((dat & mod) != 0) \ { \ Spi##spi##MdoWrite(DAT_ONE); \ } \ else \ { \ Spi##spi##MdoWrite(DAT_ZERO); \ } \ /* 先准备好数据,再拉高CLK */ \ Spi##spi##ClockHigh(); \ \ if(Spi##spi##MdiRead() == DAT_ONE) /* 读取数据 */ \ { \ rslt |= mod; \ } \ mod >>= 1; \ \ Spi##spi##ClockLow(); \ } \ return rslt; \ } \ \ /* 发送字节 */ \ void Spi##spi##WriteByte(u8 txData) \ { \ Spi##spi##SwapByte(txData); \ } \ \ /* 接收字节 */ \ u8 Spi##spi##ReadByte(void) \ { \ return Spi##spi##SwapByte(0xff); \ } \ /* \ // 功能: SPI交换1个字数据 \ // 参数: dat, 待发送的数据 \ // 返回: 读到的数据 \ */ \ u16 Spi##spi##SwapWord(u16 dat) \ { \ return 0;\ } \ \ /* 发送字 */ \ void Spi##spi##WriteWord(u16 txData) \ { \ Spi##spi##SwapWord(txData); \ } \ \ /* 接收字 */ \ u16 Spi##spi##ReadWord(void) \ { \ return Spi##spi##SwapWord(0xffff); \ } \ //------ //-------------------- #ifdef SRW_SPI1 #if (SOFT_SPI1 == 0) SPIRW_PROC(1); #else SOFT_SPIRW_PROC(1); #endif #endif #ifdef SRW_SPI2 #if (SOFT_SPI2 == 0) SPIRW_PROC(2); #else SOFT_SPIRW_PROC(2); #endif #endif #ifdef SRW_SPI3 #if (SOFT_SPI3 == 0) SPIRW_PROC(3); #else SOFT_SPIRW_PROC(3); #endif #endif #ifdef SRW_SPI4 #if (SOFT_SPI4 == 0) SPIRW_PROC(4); #else SOFT_SPIRW_PROC(4); #endif #endif