Init
This commit is contained in:
109
lib/src/iic/iic.c
Normal file
109
lib/src/iic/iic.c
Normal file
@@ -0,0 +1,109 @@
|
||||
#include "iic.h"
|
||||
|
||||
static inline void iic_Start(HW_Dev_IIC dev) {
|
||||
dev.SDA_MODE(OUT);
|
||||
dev.SDA_SET(HIGH);
|
||||
dev.CLK_SET(HIGH);DELAY1US();
|
||||
dev.SDA_SET(LOW);DELAY1US();
|
||||
dev.CLK_SET(LOW);
|
||||
}
|
||||
|
||||
//结束信号
|
||||
static inline void iic_Stop(HW_Dev_IIC dev) {
|
||||
dev.SDA_MODE(OUT);
|
||||
dev.CLK_SET(LOW);
|
||||
dev.SDA_SET(LOW);DELAY1US();
|
||||
dev.CLK_SET(HIGH);
|
||||
dev.SDA_SET(HIGH);
|
||||
}
|
||||
|
||||
//等待信号响应
|
||||
static inline uint8_t iic_WaitAck(HW_Dev_IIC dev, uint16_t timeout) {
|
||||
uint16_t time = 0;
|
||||
dev.SDA_MODE(IN);
|
||||
dev.SDA_SET(HIGH);DELAY1US();
|
||||
dev.CLK_SET(HIGH);DELAY1US();
|
||||
while (dev.SDA) {
|
||||
time++;DELAY1US();
|
||||
if (time > timeout) {
|
||||
iic_Stop(dev);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
dev.CLK_SET(LOW);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline void iic_Ack(HW_Dev_IIC dev) {
|
||||
dev.CLK_SET(LOW);
|
||||
dev.SDA_MODE(OUT);
|
||||
dev.SDA_SET(LOW);DELAY1US();DELAY1US();
|
||||
dev.CLK_SET(HIGH);DELAY1US();DELAY1US();
|
||||
dev.CLK_SET(LOW);
|
||||
}
|
||||
|
||||
static inline void iic_NAck(HW_Dev_IIC dev) {
|
||||
dev.CLK_SET(LOW);
|
||||
dev.SDA_MODE(OUT);
|
||||
dev.SDA_SET(HIGH);DELAY1US();DELAY1US();
|
||||
dev.CLK_SET(HIGH);DELAY1US();DELAY1US();
|
||||
dev.CLK_SET(LOW);
|
||||
}
|
||||
|
||||
//写入一个字节
|
||||
static inline void iic_W(HW_Dev_IIC dev, uint8_t date) {
|
||||
dev.SDA_MODE(OUT);
|
||||
uint8_t i;
|
||||
for (i = 0; i < 8; i++) {
|
||||
dev.CLK_SET(LOW);
|
||||
if (date & 0x80) {
|
||||
dev.SDA_SET(HIGH);
|
||||
} else {
|
||||
dev.SDA_SET(LOW);
|
||||
}DELAY1US();DELAY1US();
|
||||
dev.CLK_SET(HIGH);DELAY1US();DELAY1US();
|
||||
dev.CLK_SET(LOW);DELAY1US();DELAY1US();
|
||||
date <<= 1;
|
||||
}
|
||||
}
|
||||
|
||||
static inline uint8_t iic_R(HW_Dev_IIC dev, uint8_t ack) {
|
||||
unsigned char i, read = 0;
|
||||
dev.SDA_MODE(IN);
|
||||
for (i = 0; i < 8; i++) {
|
||||
dev.CLK_SET(LOW);DELAY1US();DELAY1US();
|
||||
dev.CLK_SET(HIGH);
|
||||
read <<= 1;
|
||||
if (dev.SDA)read++;DELAY1US();
|
||||
}
|
||||
if (ack)
|
||||
iic_Ack(dev); //发送ACK
|
||||
else
|
||||
iic_NAck(dev); //发送NACK
|
||||
return read;
|
||||
}
|
||||
|
||||
void HW_IIC_WL(HW_Dev_IIC Dev, uint8_t InternalAddress, uint8_t *Write_Data, uint32_t Len) {
|
||||
iic_Start(Dev);
|
||||
iic_W(Dev, Dev.ADD);
|
||||
iic_WaitAck(Dev, 200);
|
||||
iic_W(Dev, InternalAddress);
|
||||
iic_WaitAck(Dev, 200);
|
||||
for (uint32_t i = 0; i < Len; i++) {
|
||||
iic_W(Dev, Write_Data[i]);
|
||||
iic_WaitAck(Dev, 200);
|
||||
}
|
||||
iic_Stop(Dev);
|
||||
}
|
||||
|
||||
void HW_IIC_RL(HW_Dev_IIC Dev, uint8_t InternalAddress, uint8_t *Read_Data, uint32_t Len, uint8_t Ack) {
|
||||
iic_Start(Dev);
|
||||
iic_W(Dev, Dev.ADD);
|
||||
iic_WaitAck(Dev, 200);
|
||||
iic_W(Dev, InternalAddress);
|
||||
iic_WaitAck(Dev, 200);
|
||||
for (uint32_t i = 0; i < Len; i++) {
|
||||
Read_Data[i] = iic_R(Dev, Ack);
|
||||
}
|
||||
iic_Stop(Dev);
|
||||
}
|
224
lib/src/spi/spi.c
Normal file
224
lib/src/spi/spi.c
Normal file
@@ -0,0 +1,224 @@
|
||||
#include "spi.h"
|
||||
#include "tool.h"
|
||||
|
||||
#define CS_ENABLE() Dev.CS ? Dev.CS_SET(HIGH) : Dev.CS_SET(LOW)
|
||||
#define CS_DISABLE() Dev.CS ? Dev.CS_SET(LOW) : Dev.CS_SET(HIGH)
|
||||
|
||||
static inline uint8_t spi_RW(HW_Dev_Spi Dev, uint8_t Write_Date) {
|
||||
uint8_t i, read_date = 0;
|
||||
switch (Dev.MODE) {
|
||||
case Mode0:
|
||||
for (i = 0; i < 8; i++) {
|
||||
if (Write_Date & 0x80)
|
||||
Dev.MOSI_SET(HIGH);
|
||||
else
|
||||
Dev.MOSI_SET(LOW);
|
||||
Write_Date <<= 1;DELAY1US();
|
||||
Dev.SCK_SET(HIGH);
|
||||
read_date <<= 1;
|
||||
if (Dev.MISO())
|
||||
read_date++;DELAY1US();
|
||||
Dev.SCK_SET(LOW);NOP();
|
||||
}
|
||||
break;
|
||||
case Mode1:
|
||||
for (i = 0; i < 8; i++) {
|
||||
Dev.SCK_SET(HIGH);
|
||||
if (Write_Date & 0x80)
|
||||
Dev.MOSI_SET(HIGH);
|
||||
else
|
||||
Dev.MOSI_SET(LOW);
|
||||
Write_Date <<= 1;DELAY1US();
|
||||
Dev.SCK_SET(LOW);
|
||||
read_date <<= 1;
|
||||
if (Dev.MISO())
|
||||
read_date++;DELAY1US();
|
||||
}
|
||||
break;
|
||||
case Mode2:
|
||||
for (i = 0; i < 8; i++) {
|
||||
if (Write_Date & 0x80)
|
||||
Dev.MOSI_SET(HIGH);
|
||||
else
|
||||
Dev.MOSI_SET(LOW);
|
||||
Write_Date <<= 1;DELAY1US();
|
||||
Dev.SCK_SET(LOW);
|
||||
read_date <<= 1;
|
||||
if (Dev.MISO())
|
||||
read_date++;DELAY1US();
|
||||
Dev.SCK_SET(HIGH);
|
||||
}
|
||||
break;
|
||||
case Mode3:
|
||||
for (i = 0; i < 8; i++) {
|
||||
Dev.SCK_SET(LOW);
|
||||
if (Write_Date & 0x80)
|
||||
Dev.MOSI_SET(HIGH);
|
||||
else
|
||||
Dev.MOSI_SET(LOW);
|
||||
Write_Date <<= 1;DELAY1US();
|
||||
Dev.SCK_SET(HIGH);
|
||||
read_date <<= 1;
|
||||
if (Dev.MISO())
|
||||
read_date++;DELAY1US();NOP();
|
||||
}
|
||||
break;
|
||||
}
|
||||
return read_date;
|
||||
}
|
||||
|
||||
uint8_t HW_SPI_RW(HW_Dev_Spi Dev, uint8_t Write_Date) {
|
||||
uint8_t read_date;
|
||||
CS_ENABLE();
|
||||
read_date = spi_RW(Dev, Write_Date);
|
||||
CS_DISABLE();
|
||||
return read_date;
|
||||
}
|
||||
|
||||
void HW_SPI_WL(HW_Dev_Spi Dev, uint8_t *Write_Date, uint32_t Len) {
|
||||
CS_ENABLE();
|
||||
for (uint32_t i = 0; i < Len; i++)
|
||||
spi_RW(Dev, Write_Date[i]);
|
||||
CS_DISABLE();
|
||||
}
|
||||
|
||||
void HW_SPI_RL(HW_Dev_Spi Dev, uint8_t *Read_Date, uint32_t Len) {
|
||||
CS_ENABLE();
|
||||
for (uint32_t i = 0; i < Len; i++)
|
||||
Read_Date[i] = spi_RW(Dev, 0xFF);
|
||||
CS_DISABLE();
|
||||
}
|
||||
|
||||
void HW_SPI_RWL(HW_Dev_Spi Dev, uint8_t *Read_Date, uint8_t *Write_Date, uint32_t Len) {
|
||||
CS_ENABLE();
|
||||
for (uint32_t i = 0; i < Len; i++)
|
||||
Read_Date[i] = spi_RW(Dev, Write_Date[i]);
|
||||
CS_DISABLE();
|
||||
}
|
||||
|
||||
uint16_t HW_SPI_RW16(HW_Dev_Spi Dev, uint16_t Write_Date) {
|
||||
Data16 wdate16, rdate16;
|
||||
wdate16.u16 = Write_Date;
|
||||
CS_ENABLE();
|
||||
if (Dev.ENDIAN) {
|
||||
rdate16.u8[0] = spi_RW(Dev, wdate16.u8[0]);
|
||||
rdate16.u8[1] = spi_RW(Dev, wdate16.u8[1]);
|
||||
} else {
|
||||
rdate16.u8[0] = spi_RW(Dev, wdate16.u8[1]);
|
||||
rdate16.u8[1] = spi_RW(Dev, wdate16.u8[0]);
|
||||
}
|
||||
CS_DISABLE();
|
||||
return rdate16.u16;
|
||||
}
|
||||
|
||||
void HW_SPI_RL16(HW_Dev_Spi Dev, uint16_t *Read_Date, uint32_t Len) {
|
||||
Data16 rdate16;
|
||||
CS_ENABLE();
|
||||
switch (Dev.ENDIAN) {
|
||||
case LTL:
|
||||
for (uint32_t i = 0; i < Len; i++) {
|
||||
rdate16.u8[0] = spi_RW(Dev, 0xFF);
|
||||
rdate16.u8[1] = spi_RW(Dev, 0xFF);
|
||||
Read_Date[i] = rdate16.u16;
|
||||
}
|
||||
break;
|
||||
case LTB:
|
||||
for (uint32_t i = 0; i < Len; i++) {
|
||||
rdate16.u8[0] = spi_RW(Dev, 0xFF);
|
||||
rdate16.u8[1] = spi_RW(Dev, 0xFF);
|
||||
Read_Date[i] = rdate16.u16;
|
||||
}
|
||||
break;
|
||||
case BTB:
|
||||
for (uint32_t i = 0; i < Len; i++) {
|
||||
rdate16.u8[1] = spi_RW(Dev, 0xFF);
|
||||
rdate16.u8[0] = spi_RW(Dev, 0xFF);
|
||||
Read_Date[i] = rdate16.u16;
|
||||
}
|
||||
break;
|
||||
case BTL:
|
||||
for (uint32_t i = 0; i < Len; i++) {
|
||||
rdate16.u8[1] = spi_RW(Dev, 0xFF);
|
||||
rdate16.u8[0] = spi_RW(Dev, 0xFF);
|
||||
Read_Date[i] = rdate16.u16;
|
||||
}
|
||||
break;
|
||||
}
|
||||
CS_DISABLE();
|
||||
}
|
||||
|
||||
void HW_SPI_WL16(HW_Dev_Spi Dev, uint16_t *Write_Date, uint32_t Len) {
|
||||
Data16 wdate16;
|
||||
CS_ENABLE();
|
||||
switch (Dev.ENDIAN) {
|
||||
case LTL:
|
||||
for (uint32_t i = 0; i < Len; i++) {
|
||||
wdate16.u16 = Write_Date[i];
|
||||
spi_RW(Dev, wdate16.u8[0]);
|
||||
spi_RW(Dev, wdate16.u8[1]);
|
||||
}
|
||||
break;
|
||||
case LTB:
|
||||
for (uint32_t i = 0; i < Len; i++) {
|
||||
wdate16.u16 = Write_Date[i];
|
||||
spi_RW(Dev, wdate16.u8[1]);
|
||||
spi_RW(Dev, wdate16.u8[0]);
|
||||
}
|
||||
break;
|
||||
case BTB:
|
||||
for (uint32_t i = 0; i < Len; i++) {
|
||||
wdate16.u16 = Write_Date[i];
|
||||
spi_RW(Dev, wdate16.u8[1]);
|
||||
spi_RW(Dev, wdate16.u8[0]);
|
||||
}
|
||||
break;
|
||||
case BTL:
|
||||
for (uint32_t i = 0; i < Len; i++) {
|
||||
wdate16.u16 = Write_Date[i];
|
||||
spi_RW(Dev, wdate16.u8[0]);
|
||||
spi_RW(Dev, wdate16.u8[1]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
CS_DISABLE();
|
||||
}
|
||||
|
||||
void HW_SPI_RWL16(HW_Dev_Spi Dev, uint16_t *Read_Date, uint16_t *Write_Date, uint32_t Len) {
|
||||
Data16 wdate16, rdate16;
|
||||
CS_ENABLE();
|
||||
switch (Dev.ENDIAN) {
|
||||
case LTL:
|
||||
for (uint32_t i = 0; i < Len; i++) {
|
||||
wdate16.u16 = Write_Date[i];
|
||||
rdate16.u8[0] = spi_RW(Dev, wdate16.u8[0]);
|
||||
rdate16.u8[1] = spi_RW(Dev, wdate16.u8[1]);
|
||||
Read_Date[i] = rdate16.u16;
|
||||
}
|
||||
break;
|
||||
case LTB:
|
||||
for (uint32_t i = 0; i < Len; i++) {
|
||||
wdate16.u16 = Write_Date[i];
|
||||
rdate16.u8[0] = spi_RW(Dev, wdate16.u8[1]);
|
||||
rdate16.u8[1] = spi_RW(Dev, wdate16.u8[0]);
|
||||
Read_Date[i] = rdate16.u16;
|
||||
}
|
||||
break;
|
||||
case BTB:
|
||||
for (uint32_t i = 0; i < Len; i++) {
|
||||
wdate16.u16 = Write_Date[i];
|
||||
rdate16.u8[1] = spi_RW(Dev, wdate16.u8[1]);
|
||||
rdate16.u8[0] = spi_RW(Dev, wdate16.u8[0]);
|
||||
Read_Date[i] = rdate16.u16;
|
||||
}
|
||||
break;
|
||||
case BTL:
|
||||
for (uint32_t i = 0; i < Len; i++) {
|
||||
wdate16.u16 = Write_Date[i];
|
||||
rdate16.u8[1] = spi_RW(Dev, wdate16.u8[0]);
|
||||
rdate16.u8[0] = spi_RW(Dev, wdate16.u8[1]);
|
||||
Read_Date[i] = rdate16.u16;
|
||||
}
|
||||
break;
|
||||
}
|
||||
CS_DISABLE();
|
||||
}
|
30
lib/src/tool.c
Normal file
30
lib/src/tool.c
Normal file
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// Created by lydxh on 2024/5/9.
|
||||
//
|
||||
#include <stdio.h>
|
||||
#include "tool.h"
|
||||
|
||||
float Mapping(float val, float I_Min, float I_Max, float O_Min, float O_Max)
|
||||
{
|
||||
return(((val-I_Min)*((O_Max-O_Min)/(I_Max-I_Min)))+O_Min);
|
||||
}
|
||||
|
||||
void BufPrint(char *name,void *buf,unsigned char size,unsigned int len,unsigned char frame){
|
||||
printf("\n%s:\n",name);
|
||||
for (int i = 0; i < len; ++i) {
|
||||
if(i%frame==0&&i!=0) printf("\n");
|
||||
switch (size) {
|
||||
case 8:
|
||||
printf("%02x ",*((unsigned char*)buf + i));
|
||||
break;
|
||||
case 16:
|
||||
printf("%04x ",*((unsigned short *)buf + i));
|
||||
break;
|
||||
case 32:
|
||||
printf("%08x ",*((unsigned int *)buf + i));
|
||||
break;
|
||||
default:
|
||||
printf("未指定类型大小");
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user