IIC Demo
parent
97aa90ab4a
commit
84bc401ba4
|
@ -1 +1,2 @@
|
|||
/cmake-build-debug/
|
||||
/.idea/
|
|
@ -9,6 +9,7 @@ include_directories(
|
|||
lib/inc/spi
|
||||
lib/inc/iic
|
||||
demo/spi
|
||||
demo/iic
|
||||
)
|
||||
|
||||
file(GLOB_RECURSE SOURCES
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
//
|
||||
// Created by lydxh on 2024/5/9.
|
||||
//
|
||||
|
||||
#ifndef HW_LIB_T_IIC_H
|
||||
#define HW_LIB_T_IIC_H
|
||||
|
||||
void Test_iic();
|
||||
|
||||
#endif //HW_LIB_T_IIC_H
|
|
@ -0,0 +1,51 @@
|
|||
#include <stdio.h>
|
||||
#include "iic.h"
|
||||
#include "log.h"
|
||||
#include "tool.h"
|
||||
|
||||
uint8_t CLK_Pin(uint8_t l) {
|
||||
// LOGT("CLK", "P:%d", l);
|
||||
return l;
|
||||
}
|
||||
|
||||
uint8_t SDA_Set(uint8_t l) {
|
||||
// LOGT("SDA", "P:%d", l);
|
||||
return l;
|
||||
}
|
||||
|
||||
uint8_t SDA_Mode(uint8_t l) {
|
||||
if (l) {
|
||||
// LOGT("SDA", "M:IN");
|
||||
} else {
|
||||
// LOGT("SDA", "M:OUT");
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
uint8_t SDA_Read() {
|
||||
uint8_t l = rand() % 2;
|
||||
// LOGT("SDA", "P:%d", l);
|
||||
return l;
|
||||
}
|
||||
|
||||
void Test_iic() {
|
||||
HW_Dev_IIC dev = {
|
||||
.CLK_SET = CLK_Pin,
|
||||
.SDA_SET = SDA_Set,
|
||||
.SDA = SDA_Read,
|
||||
.SDA_MODE = SDA_Mode,
|
||||
.ADD = 0x34
|
||||
};
|
||||
uint8_t internalAddress = 0x56;
|
||||
|
||||
uint32_t len = 10;
|
||||
uint8_t writeData[len];
|
||||
uint8_t readData[len];
|
||||
for (int i = 0; i < len; ++i) {
|
||||
writeData[i] = rand() % 10;
|
||||
}
|
||||
HW_IIC_WL(dev, internalAddress, writeData, len);
|
||||
BufPrint("<IIC> TX", writeData, 8, len, 16);
|
||||
HW_IIC_RL(dev, internalAddress, readData, len, 1);
|
||||
BufPrint("<IIC> RX", readData, 8, len, 16);
|
||||
}
|
|
@ -44,19 +44,19 @@ void Test_spi() {
|
|||
};
|
||||
uint8_t r, t = rand() % 10;
|
||||
r = HW_SPI_RW(ltl, t);
|
||||
LOGI("Tx:%d,Rx:%d", t, r);
|
||||
LOGI("<SPI> Tx:%d,Rx:%d", t, r);
|
||||
uint8_t rbuf[64], tbuf[64];
|
||||
for (int i = 0; i < 64; ++i) {
|
||||
tbuf[i] = rand() % 10;
|
||||
}
|
||||
HW_SPI_RWL(ltl, rbuf, tbuf, 64);
|
||||
BufPrint("TX", tbuf, 8, 64, 8);
|
||||
BufPrint("RX", rbuf, 8, 64, 8);
|
||||
BufPrint("<SPI> TX", tbuf, 8, 64, 8);
|
||||
BufPrint("<SPI> RX", rbuf, 8, 64, 8);
|
||||
uint16_t rbuf16[64], tbuf16[64];
|
||||
for (int i = 0; i < 64; ++i) {
|
||||
tbuf16[i] = rand() % 1000;
|
||||
}
|
||||
HW_SPI_RWL16(ltl, rbuf16, tbuf16, 64);
|
||||
BufPrint("TX", tbuf16, 16,64, 16);
|
||||
BufPrint("RX", rbuf16, 16,64, 16);
|
||||
BufPrint("<SPI> TX[16]", tbuf16, 16, 64, 16);
|
||||
BufPrint("<SPI> RX[16]", rbuf16, 16, 64, 16);
|
||||
}
|
|
@ -20,7 +20,7 @@
|
|||
typedef struct {
|
||||
uint8_t (*CLK_SET)(uint8_t); // 设置时钟引脚状态的函数指针
|
||||
uint8_t (*SDA_SET)(uint8_t); // 设置数据引脚状态的函数指针
|
||||
uint8_t (*SDA); // 读取数据引脚状态的函数指针
|
||||
uint8_t (*SDA)(); // 读取数据引脚状态的函数指针
|
||||
uint8_t (*SDA_MODE)(uint8_t); // 设置数据引脚模式的函数指针
|
||||
uint8_t ADD; // IIC设备地址
|
||||
} HW_Dev_IIC;
|
||||
|
|
|
@ -23,7 +23,7 @@ static inline uint8_t iic_WaitAck(HW_Dev_IIC dev, uint16_t timeout) {
|
|||
dev.SDA_MODE(IN);
|
||||
dev.SDA_SET(HIGH);DELAY1US();
|
||||
dev.CLK_SET(HIGH);DELAY1US();
|
||||
while (dev.SDA) {
|
||||
while (dev.SDA()) {
|
||||
time++;DELAY1US();
|
||||
if (time > timeout) {
|
||||
iic_Stop(dev);
|
||||
|
@ -74,7 +74,7 @@ static inline uint8_t iic_R(HW_Dev_IIC dev, uint8_t ack) {
|
|||
dev.CLK_SET(LOW);DELAY1US();DELAY1US();
|
||||
dev.CLK_SET(HIGH);
|
||||
read <<= 1;
|
||||
if (dev.SDA)read++;DELAY1US();
|
||||
if (dev.SDA())read++;DELAY1US();
|
||||
}
|
||||
if (ack)
|
||||
iic_Ack(dev); //发送ACK
|
||||
|
|
|
@ -27,4 +27,5 @@ void BufPrint(char *name,void *buf,unsigned char size,unsigned int len,unsigned
|
|||
printf("未指定类型大小");
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
Loading…
Reference in New Issue