添加很多设备

This commit is contained in:
2024-03-04 01:01:14 +08:00
parent bef8342c93
commit cb1b2eb7d4
35 changed files with 22897 additions and 20328 deletions
+139
View File
@@ -0,0 +1,139 @@
/*
* at24cxx.c
*
* Created on: Mar 3, 2024
* Author: gxms0
*/
#include "at24cxx.h"
void at24cxx_init()
{
}
/*
uint8_t x24Cxx_ReadByte(uint16_t u16Addr)
{
uint8_t u8Data = 0;
IIC_Start();//起始信号
IIC_WriteByte(DEV_ADDR | WRITE_CMD);//器件寻址+读/写选择
IIC_WaitAck();//等待应答
IIC_WriteByte((uint8_t)((u16Addr >> 8) & 0xFF));
IIC_WaitAck();//等待应答
IIC_WriteByte((uint8_t)(u16Addr & 0xFF));
IIC_WaitAck();//等待应答
IIC_Start();//起始信号
IIC_WriteByte(DEV_ADDR | READ_CMD);//器件寻址+读
IIC_WaitAck();//等待应答
u8Data = IIC_ReadByte();
IIC_NoAck();
IIC_Stop();//停止信号
return u8Data;
}
void at24cxx_WriteByte(uint16_t u16Addr, uint8_t u8Data)
{
x24Cxx_WriteEnable();//使能写入
IIC_Start();//起始信号
IIC_WriteByte(DEV_ADDR | WRITE_CMD);//器件寻址+读/写选择
IIC_WaitAck();//等待应答
IIC_WriteByte((uint8_t)((u16Addr >> 8) & 0xFF));
IIC_WaitAck();//等待应答
IIC_WriteByte((uint8_t)(u16Addr & 0xFF));
IIC_WaitAck();//等待应答
IIC_WriteByte(u8Data);
IIC_WaitAck();//等待应答
IIC_Stop();//停止信号
x24Cxx_WriteDisble();//禁止写入
}
void x24Cxx_ReadPage(uint16_t u16Addr, uint8_t u8Len, uint8_t *pBuff)
{
uint8_t i;
IIC_Start();//起始信号
IIC_WriteByte(DEV_ADDR | WRITE_CMD);//器件寻址+读/写选择
IIC_WaitAck();//等待应答
IIC_WriteByte((uint8_t)((u16Addr >> 8) & 0xFF));
IIC_WaitAck();//等待应答
IIC_WriteByte((uint8_t)(u16Addr & 0xFF));
IIC_WaitAck();//等待应答
IIC_Start();//起始信号
IIC_WriteByte(DEV_ADDR | READ_CMD);//器件寻址+读
IIC_WaitAck();//等待应答
if (u8Len > PAGE_SIZE)//长度大于页的长度
{
u8Len = PAGE_SIZE;
}
if ((u16Addr + (uint16_t)u8Len) > CAPACITY_SIZE)//超过容量
{
u8Len = (uint8_t)(CAPACITY_SIZE - u16Addr);
}
if (((u16Addr % PAGE_SIZE) + (uint16_t)u8Len) > PAGE_SIZE)//判断是否跨页
{
u8Len -= (uint8_t)((u16Addr + (uint16_t)u8Len) % PAGE_SIZE);//跨页,截掉跨页的部分
}
for (i = 0; i < (u8Len - 1); i++)
{
*(pBuff + i) = IIC_ReadByte();
IIC_Ack();//主机应答
}
*(pBuff + u8Len - 1) = IIC_ReadByte();
IIC_NoAck();//最后一个不应答
IIC_Stop();//停止信号
}
void at24cxx_WritePage(uint16_t u16Addr, uint8_t u8Len, uint8_t *pData)
{
uint8_t i;
x24Cxx_WriteEnable();//使能写入
IIC_Start();//起始信号
IIC_WriteByte(DEV_ADDR | WRITE_CMD);//器件寻址+读/写选择
IIC_WaitAck();//等待应答
IIC_WriteByte((uint8_t)((u16Addr >> 8) & 0xFF));
IIC_WaitAck();//等待应答
IIC_WriteByte((uint8_t)(u16Addr & 0xFF));
IIC_WaitAck();//等待应答
if (u8Len > PAGE_SIZE)//长度大于页的长度
{
u8Len = PAGE_SIZE;
}
if ((u16Addr + (uint16_t)u8Len) > CAPACITY_SIZE)//超过容量
{
u8Len = (uint8_t)(CAPACITY_SIZE - u16Addr);
}
if (((u16Addr % PAGE_SIZE) + (uint16_t)u8Len) > PAGE_SIZE)//判断是否跨页
{
u8Len -= (uint8_t)((u16Addr + (uint16_t)u8Len) % PAGE_SIZE);//跨页,截掉跨页的部分
}
for (i = 0; i < u8Len; i++)
{
IIC_WriteByte(*(pData + i));
IIC_WaitAck();//等待应答
}
IIC_Stop();//停止信号
x24Cxx_WriteDisble();//禁止写入
}
*/
+60
View File
@@ -0,0 +1,60 @@
/*
* at24cxx.h
*
* Created on: Mar 3, 2024
* Author: gxms0
*/
#ifndef BSP_AT24CXX_H_
#define BSP_AT24CXX_H_
#include "ch32v20x.h"
#include <string.h>
#include <stdlib.h>
#include "drv_i2c.h"
#define READ_CMD 1
#define WRITE_CMD 0
#define x24C32//器件名称,AT24C32、AT24C64、AT24C128、AT24C256、AT24C512
#define DEV_ADDR 0xA0 //设备硬件地址
#ifdef x24C32
#define PAGE_NUM 128 //页数
#define PAGE_SIZE 32 //页面大小(字节)
#define CAPACITY_SIZE (PAGE_NUM * PAGE_SIZE) //总容量(字节)
#define ADDR_BYTE_NUM 2 //地址字节个数
#endif
#ifdef x24C64
#define PAGE_NUM 256 //页数
#define PAGE_SIZE 32 //页面大小(字节)
#define CAPACITY_SIZE (PAGE_NUM * PAGE_SIZE) //总容量(字节)
#define ADDR_BYTE_NUM 2 //地址字节个数
#endif
#ifdef x24C128
#define PAGE_NUM 256 //页数
#define PAGE_SIZE 64 //页面大小(字节)
#define CAPACITY_SIZE (PAGE_NUM * PAGE_SIZE) //总容量(字节)
#define ADDR_BYTE_NUM 2 //地址字节个数
#endif
#ifdef x24C256
#define PAGE_NUM 512 //页数
#define PAGE_SIZE 64 //页面大小(字节)
#define CAPACITY_SIZE (PAGE_NUM * PAGE_SIZE) //总容量(字节)
#define ADDR_BYTE_NUM 2 //地址字节个数
#endif
#ifdef x24C512
#define PAGE_NUM 512 //页数
#define PAGE_SIZE 128 //页面大小(字节)
#define CAPACITY_SIZE (PAGE_NUM * PAGE_SIZE) //总容量(字节)
#define ADDR_BYTE_NUM 2 //地址字节个数
#endif
#endif /* BSP_AT24CXX_H_ */
+8
View File
@@ -0,0 +1,8 @@
/*
* bmp280.c
*
* Created on: Mar 4, 2024
* Author: gxms0
*/
+13
View File
@@ -0,0 +1,13 @@
/*
* bmp280.h
*
* Created on: Mar 4, 2024
* Author: gxms0
*/
#ifndef BSP_BMP280_H_
#define BSP_BMP280_H_
#endif /* BSP_BMP280_H_ */
+8
View File
@@ -0,0 +1,8 @@
/*
* mpu6500.c
*
* Created on: Mar 4, 2024
* Author: gxms0
*/
+13
View File
@@ -0,0 +1,13 @@
/*
* mpu6500.h
*
* Created on: Mar 4, 2024
* Author: gxms0
*/
#ifndef BSP_MPU6500_H_
#define BSP_MPU6500_H_
#endif /* BSP_MPU6500_H_ */
+122
View File
@@ -0,0 +1,122 @@
/*
* pmw3901.c
*
* Created on: Mar 2, 2024
* Author: gxms0
*/
#include "pmw3901.h"
PMW_t pmw3901_1;
void pmw3901_init()
{
pmw3901_1.parser.stage = 0;
}
void pmw3901_thread()
{
pmw3901_update(&pmw3901_1);
}
void pmw3901_update(PMW_ptr ptr)
{
uint8_t buff[50];
size_t len = usart_read(&uart3, buff, sizeof(buff));
if (len) {
for(int i = 0;i< len;i++)
{
int rst = pmw3901_parser_char(&ptr->parser, buff[i]);
switch(rst)
{
case 1:
{
ptr->dx = (int16_t)(ptr->parser.buff[3] << 8) | ptr->parser.buff[2];
ptr->dy = (int16_t)(ptr->parser.buff[5] << 8) | ptr->parser.buff[4];
ptr->dz = (int16_t)(ptr->parser.buff[7] << 8) | ptr->parser.buff[6];
ptr->quality = ptr->parser.buff[9];
}break;
}
}
}
}
int pmw3901_parser_char(Parser_t *parser, uint8_t c)
{
int rslt = -1;
switch (parser->stage)
{
case 0:
if (c == 0xFE)
{
parser->stage++;
parser->idx = 0;
parser->id = 0;
parser->buff[parser->idx++] = c;
}
break;
case 1:
parser->len = c+2;
parser->stage++;
parser->buff[parser->idx++] = c;
parser->sum = 0;
break;
case 2:
parser->buff[parser->idx++] = c;
parser->sum += c;
if (parser->idx == (parser->len+2))
{
parser->stage++;
}
break;
case 3:
parser->buff[parser->idx++] = c;
parser->sum_r = c;
parser->stage++;
break;
case 4:
parser->buff[parser->idx++] = c;
parser->stage++;
break;
case 5:
if (c == 0xAA)
{
if((parser->sum & 0xff) == parser->sum_r)
{
parser->id = 1;
rslt = parser->id;
}
}
parser->stage = 0;
break;
default:
parser->stage = 0;
break;
}
return rslt;
}
+58
View File
@@ -0,0 +1,58 @@
/*
* pmw3901.h
*
* Created on: Mar 2, 2024
* Author: gxms0
*/
#ifndef BSP_PMW3901_H_
#define BSP_PMW3901_H_
#include "ch32v20x.h"
#include <string.h>
#include <stdlib.h>
#include "drv_uart.h"
typedef struct{
int stage;
uint16_t idx;
uint16_t len;
uint8_t seq;
uint16_t id;
uint8_t sum;
uint8_t sum_r;
uint32_t head_err_cnt;
uint8_t buff[50];
} Parser_t;
typedef struct sPMW {
UART_ptr uart;
Parser_t parser;
float dx;
float dy;
float dz;
uint8_t count;
uint8_t quality;
uint8_t valid;
}PMW_t;
typedef PMW_t* PMW_ptr;
extern PMW_t pmw3901_1;
void pmw3901_init();
void pmw3901_thread();
void pmw3901_update();
int pmw3901_parser_char(Parser_t *parser, uint8_t c);
#endif /* BSP_PMW3901_H_ */
+8
View File
@@ -0,0 +1,8 @@
/*
* qmc5883l.c
*
* Created on: Mar 4, 2024
* Author: gxms0
*/
+13
View File
@@ -0,0 +1,13 @@
/*
* qmc5883l.h
*
* Created on: Mar 4, 2024
* Author: gxms0
*/
#ifndef BSP_QMC5883L_H_
#define BSP_QMC5883L_H_
#endif /* BSP_QMC5883L_H_ */
+8
View File
@@ -0,0 +1,8 @@
/*
* si24r1.c
*
* Created on: Mar 4, 2024
* Author: gxms0
*/
+13
View File
@@ -0,0 +1,13 @@
/*
* si24r1.h
*
* Created on: Mar 4, 2024
* Author: gxms0
*/
#ifndef BSP_SI24R1_H_
#define BSP_SI24R1_H_
#endif /* BSP_SI24R1_H_ */