This repository has been archived on 2025-02-28. You can view files and clone it, but cannot push or open issues or pull requests.
controller-hd/User/system/bsp/i2cs.h

66 lines
2.8 KiB
C

#ifndef __I2CS_H__
#define __I2CS_H__
#include "lib.h"
#include "gpios.h"
typedef struct I2CS i2c_t;
typedef void i2cs_dma_callback(i2c_t *handle);
typedef struct
{
void (*start)(i2c_t *handle); // 启动
void (*stop)(i2c_t *handle); // 停止
BOOL(*wait_ack)
(i2c_t *handle); // 等待应答
void (*write_byte)(i2c_t *handle, uint8_t data); // 写入一个字节
uint8_t (*read_byte)(i2c_t *handle, BOOL ack); // 读取一个字节
void (*write_word)(i2c_t *handle, uint16_t data); // 写入二个字节
BOOL(*write_mem_dma)
(i2c_t *handle, uint16_t mem_address, uint16_t mem_addsize, uint8_t *data, uint16_t size); // 写入多个字节
BOOL(*read_mem_dma)
(i2c_t *handle, uint16_t mem_address, uint16_t mem_addsize, uint8_t *data, uint16_t size); // 读取多个字节
} i2c_interface_t;
typedef struct
{
struct GPIO *scl; // SCL
struct GPIO *sda; // SDA
} i2c_gpio_group_t;
struct I2CS
{
// 模拟部分定义
i2c_gpio_group_t gpios; // i2c引脚
uint16_t delay_ticks; // 延时多少个NOP
// 硬件部分定义
I2C_TypeDef *i2c; // I2C外设
DMA_TypeDef *dma; // DMA外设
uint32_t dma_rx_channel; // 外部设置
uint32_t dma_tx_channel; // 外部设置
uint8_t *rxbuf;
uint16_t rxsize;
uint8_t *txbuf;
uint16_t txsize;
uint8_t w_address; // 7位地址
uint8_t r_address; // 7位地址
__IO BOOL rx_dma_ok; // 接收DMA完成标志
__IO BOOL tx_dma_ok; // 发送DMA完成标志
i2cs_dma_callback *dma_rx_cb; // DMA接收完成回调函数
i2cs_dma_callback *dma_tx_cb; // DMA发送完成回调函数
i2c_interface_t interface; // I2C接口
};
extern i2c_t *i2c_create(i2c_gpio_group_t gpios, uint16_t delay_ticks); // 创建I2C
extern i2c_t *i2c_create_dma(I2C_TypeDef *i2c, DMA_TypeDef *dma, uint16_t rxsize, uint32_t dma_rx_channel,
i2cs_dma_callback *dma_rx_cb, uint16_t txsize, uint32_t dma_tx_channel, i2cs_dma_callback *dma_tx_cb); // 创建I2C DMA
extern void i2c_dma_set_address(i2c_t *handle, uint8_t w_address, uint8_t r_address); // 设置I2C地址 DMA
extern void i2c_free(i2c_t *handle); // 释放I2C资源
extern void i2c_ev_callback(i2c_t *handle); // I2C事件回调函数
extern void i2c_dma_callback(i2c_t *handle); // I2C DMA回调函数
#endif // __I2CS_H__