Files
2024-09-18 11:50:26 +08:00

86 lines
2.6 KiB
C

/*
* 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
typedef struct sAT24C {
I2C_ptr i2c;
uint16_t address;
//drivers
uint16_t (*read_drv)(I2C_ptr ptr, uint16_t address, uint16_t reg, uint8_t *data, uint16_t len);
uint16_t (*write_drv)(I2C_ptr ptr, uint16_t address, uint16_t reg, uint8_t *data, uint16_t len);
//global
uint16_t (*read)(uint16_t reg, uint8_t *data, uint16_t len);
uint16_t (*write)(uint16_t reg, uint8_t *data, uint16_t len);
}AT24C_t;
typedef AT24C_t* AT24C_ptr;
void at24cxx_init(AT24C_ptr ptr,I2C_ptr i2c,uint16_t address);
uint16_t at24xcc_read(AT24C_ptr ptr, uint8_t reg,uint8_t *data, uint16_t len);
uint16_t at24xcc_write(AT24C_ptr ptr, uint8_t reg,uint8_t *data, uint16_t len);
#endif /* BSP_AT24CXX_H_ */