1 Star 0 Fork 8

辛晓宇/vtor_elec_module

forked from vtor3478/vtor_elec_module 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
vtor_spi.c 1.63 KB
一键复制 编辑 原始数据 按行查看 历史
vtor3478 提交于 2023-11-19 23:41 . 精简开关宏
#include "vtor_spi.h"
#ifdef __VTOR_SPI__
void VtorSpi_WriteClk(VtorSpiStruct* spi, char level)
{
spi->writeClk(level);
}
void VtorSpi_WriteMosi(VtorSpiStruct* spi, uint8_t level)
{
spi->writeMosi(level);
}
uint8_t VtorSpi_ReadMiso(VtorSpiStruct* spi)
{
return spi->readMiso();
}
void VtorSpi_WriteCs(VtorSpiStruct* spi, uint8_t level)
{
if(spi->writeCs)
{
spi->writeCs(level);
}
}
void VtorSpi_SetClkRun(VtorSpiStruct* spi)
{
if (VTOR_SPI_POLARITY_LOW == spi->cpol)
{
VtorSpi_WriteClk(spi, GPIO_LEVEL_HIGH);
}
else
{
VtorSpi_WriteClk(spi, GPIO_LEVEL_LOW);
}
}
void VtorSpi_SetClkIdle(VtorSpiStruct* spi)
{
if (VTOR_SPI_POLARITY_LOW == spi->cpol)
{
VtorSpi_WriteClk(spi, GPIO_LEVEL_LOW);
}
else
{
VtorSpi_WriteClk(spi, GPIO_LEVEL_HIGH);
}
}
// o h i l
// h o l i
// o l i h
// l o h i
// https://www.cnblogs.com/gmpy/p/12461461.html
// https://blog.csdn.net/baidu_39594043/article/details/120362744
void VtorSpi_WriteRead(VtorSpiStruct* spi, uint8_t* txBuf, uint8_t* rxBuf, uint16_t len)
{
uint8_t cnt;
uint16_t i;
VtorSpi_WriteCs(spi, GPIO_LEVEL_LOW);
for(i = 0; i < len; i++)
{
uint8_t txByte = 0x55;
uint8_t rxByte = 0xaa;
if (NULL != txBuf)
{
txByte = *txBuf;
txBuf++;
}
for(cnt=0;cnt<8;cnt++)
{
VtorSpi_WriteMosi(spi, txByte & 0x80);
txByte <<= 1;
VtorSpi_Delay(1);
VtorSpi_SetClkRun(spi);
VtorSpi_Delay(1);
VtorSpi_SetClkIdle(spi);
VtorSpi_Delay(1);
rxByte<<=1;
if(VtorSpi_ReadMiso(spi))
{
rxByte |= 0x01;
}
}
if (NULL != rxBuf)
{
*rxBuf = rxByte;
rxBuf++;
}
}
VtorSpi_WriteCs(spi, GPIO_LEVEL_HIGH);
}
#endif // ELEC_MODULE_VTOR_SPI_ENABLED
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xin-xiaoyu/vtor_elec_module.git
git@gitee.com:xin-xiaoyu/vtor_elec_module.git
xin-xiaoyu
vtor_elec_module
vtor_elec_module
main

搜索帮助