mirror of
https://gitee.com/dlmu-cone/tronone-h7-scaffold
synced 2026-07-24 03:27:45 +08:00
bsp_flash
This commit is contained in:
268
User_Code/bsp/flash/bsp_flash.c
Normal file
268
User_Code/bsp/flash/bsp_flash.c
Normal file
@@ -0,0 +1,268 @@
|
||||
#include "bsp_flash.h"
|
||||
#include "main.h"
|
||||
#include "string.h"
|
||||
|
||||
static uint32_t ger_sector(uint32_t address);
|
||||
|
||||
/**
|
||||
* @brief erase flash
|
||||
* @param[in] address: flash address
|
||||
* @param[in] len: page num
|
||||
* @retval none
|
||||
*/
|
||||
/**
|
||||
* @brief 擦除flash
|
||||
* @param[in] address: flash 地址
|
||||
* @param[in] len: 页数量
|
||||
* @retval none
|
||||
*/
|
||||
void flash_erase_address(uint32_t address, uint16_t len)
|
||||
{
|
||||
FLASH_EraseInitTypeDef flash_erase;
|
||||
uint32_t error;
|
||||
|
||||
flash_erase.Sector = ger_sector(address);
|
||||
flash_erase.TypeErase = FLASH_TYPEERASE_SECTORS;
|
||||
flash_erase.VoltageRange = FLASH_VOLTAGE_RANGE_3;
|
||||
flash_erase.NbSectors = len;
|
||||
|
||||
HAL_FLASH_Unlock();
|
||||
HAL_FLASHEx_Erase(&flash_erase, &error);
|
||||
HAL_FLASH_Lock();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief write data to one page of flash
|
||||
* @param[in] start_address: flash address
|
||||
* @param[in] buf: data point
|
||||
* @param[in] len: data num
|
||||
* @retval success 0, fail -1
|
||||
*/
|
||||
/**
|
||||
* @brief 往一页flash写数据
|
||||
* @param[in] start_address: flash 地址
|
||||
* @param[in] buf: 数据指针
|
||||
* @param[in] len: 数据长度
|
||||
* @retval success 0, fail -1
|
||||
*/
|
||||
int8_t flash_write_single_address(uint32_t start_address, uint32_t *buf, uint32_t len)
|
||||
{
|
||||
static uint32_t uw_address;
|
||||
static uint32_t end_address;
|
||||
static uint32_t *data_buf;
|
||||
static uint32_t data_len;
|
||||
|
||||
HAL_FLASH_Unlock();
|
||||
|
||||
uw_address = start_address;
|
||||
end_address = get_next_flash_address(start_address);
|
||||
data_buf = buf;
|
||||
data_len = 0;
|
||||
|
||||
while (uw_address <= end_address)
|
||||
{
|
||||
|
||||
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, uw_address, *data_buf) == HAL_OK)
|
||||
{
|
||||
uw_address += 4;
|
||||
data_buf++;
|
||||
data_len++;
|
||||
if (data_len == len)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_FLASH_Lock();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
HAL_FLASH_Lock();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief write data to some pages of flash
|
||||
* @param[in] start_address: flash start address
|
||||
* @param[in] end_address: flash end address
|
||||
* @param[in] buf: data point
|
||||
* @param[in] len: data num
|
||||
* @retval success 0, fail -1
|
||||
*/
|
||||
/**
|
||||
* @brief 往几页flash写数据
|
||||
* @param[in] start_address: flash 开始地址
|
||||
* @param[in] end_address: flash 结束地址
|
||||
* @param[in] buf: 数据指针
|
||||
* @param[in] len: 数据长度
|
||||
* @retval success 0, fail -1
|
||||
*/
|
||||
int8_t flash_write_muli_address(uint32_t start_address, uint32_t end_address, uint32_t *buf, uint32_t len)
|
||||
{
|
||||
uint32_t uw_address = 0;
|
||||
uint32_t *data_buf;
|
||||
uint32_t data_len;
|
||||
|
||||
HAL_FLASH_Unlock();
|
||||
|
||||
uw_address = start_address;
|
||||
data_buf = buf;
|
||||
data_len = 0;
|
||||
while (uw_address <= end_address)
|
||||
{
|
||||
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, uw_address, *data_buf) == HAL_OK)
|
||||
{
|
||||
uw_address += 4;
|
||||
data_buf++;
|
||||
data_len++;
|
||||
if (data_len == len)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_FLASH_Lock();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
HAL_FLASH_Lock();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief read data for flash
|
||||
* @param[in] address: flash address
|
||||
* @param[out] buf: data point
|
||||
* @param[in] len: data num
|
||||
* @retval none
|
||||
*/
|
||||
/**
|
||||
* @brief 从flash读数据
|
||||
* @param[in] start_address: flash 地址
|
||||
* @param[out] buf: 数据指针
|
||||
* @param[in] len: 数据长度
|
||||
* @retval none
|
||||
*/
|
||||
void flash_read(uint32_t address, uint32_t *buf, uint32_t len)
|
||||
{
|
||||
memcpy(buf, (void *) address, len * 4);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief get the sector number of flash
|
||||
* @param[in] address: flash address
|
||||
* @retval sector number
|
||||
*/
|
||||
/**
|
||||
* @brief 获取flash的sector号
|
||||
* @param[in] address: flash 地址
|
||||
* @retval sector号
|
||||
*/
|
||||
static uint32_t ger_sector(uint32_t address)
|
||||
{
|
||||
uint32_t sector = 0;
|
||||
if ((address < ADDR_FLASH_SECTOR_1) && (address >= ADDR_FLASH_SECTOR_0))
|
||||
{
|
||||
sector = FLASH_SECTOR_0;
|
||||
}
|
||||
else if ((address < ADDR_FLASH_SECTOR_2) && (address >= ADDR_FLASH_SECTOR_1))
|
||||
{
|
||||
sector = FLASH_SECTOR_1;
|
||||
}
|
||||
else if ((address < ADDR_FLASH_SECTOR_3) && (address >= ADDR_FLASH_SECTOR_2))
|
||||
{
|
||||
sector = FLASH_SECTOR_2;
|
||||
}
|
||||
else if ((address < ADDR_FLASH_SECTOR_4) && (address >= ADDR_FLASH_SECTOR_3))
|
||||
{
|
||||
sector = FLASH_SECTOR_3;
|
||||
}
|
||||
else if ((address < ADDR_FLASH_SECTOR_5) && (address >= ADDR_FLASH_SECTOR_4))
|
||||
{
|
||||
sector = FLASH_SECTOR_4;
|
||||
}
|
||||
else if ((address < ADDR_FLASH_SECTOR_6) && (address >= ADDR_FLASH_SECTOR_5))
|
||||
{
|
||||
sector = FLASH_SECTOR_5;
|
||||
}
|
||||
else if ((address < ADDR_FLASH_SECTOR_7) && (address >= ADDR_FLASH_SECTOR_6))
|
||||
{
|
||||
sector = FLASH_SECTOR_6;
|
||||
}
|
||||
else if ((address < ADDR_FLASH_SECTOR_8) && (address >= ADDR_FLASH_SECTOR_7))
|
||||
{
|
||||
sector = FLASH_SECTOR_7;
|
||||
}
|
||||
return sector;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief get the next page flash address
|
||||
* @param[in] address: flash address
|
||||
* @retval next page flash address
|
||||
*/
|
||||
/**
|
||||
* @brief 获取下一页flash地址
|
||||
* @param[in] address: flash 地址
|
||||
* @retval 下一页flash地址
|
||||
*/
|
||||
uint32_t get_next_flash_address(uint32_t address)
|
||||
{
|
||||
uint32_t sector = 0;
|
||||
|
||||
if ((address < ADDR_FLASH_SECTOR_1) && (address >= ADDR_FLASH_SECTOR_0))
|
||||
{
|
||||
sector = ADDR_FLASH_SECTOR_1;
|
||||
}
|
||||
else if ((address < ADDR_FLASH_SECTOR_2) && (address >= ADDR_FLASH_SECTOR_1))
|
||||
{
|
||||
sector = ADDR_FLASH_SECTOR_2;
|
||||
}
|
||||
else if ((address < ADDR_FLASH_SECTOR_3) && (address >= ADDR_FLASH_SECTOR_2))
|
||||
{
|
||||
sector = ADDR_FLASH_SECTOR_3;
|
||||
}
|
||||
else if ((address < ADDR_FLASH_SECTOR_4) && (address >= ADDR_FLASH_SECTOR_3))
|
||||
{
|
||||
sector = ADDR_FLASH_SECTOR_4;
|
||||
}
|
||||
else if ((address < ADDR_FLASH_SECTOR_5) && (address >= ADDR_FLASH_SECTOR_4))
|
||||
{
|
||||
sector = ADDR_FLASH_SECTOR_5;
|
||||
}
|
||||
else if ((address < ADDR_FLASH_SECTOR_6) && (address >= ADDR_FLASH_SECTOR_5))
|
||||
{
|
||||
sector = ADDR_FLASH_SECTOR_6;
|
||||
}
|
||||
else if ((address < ADDR_FLASH_SECTOR_7) && (address >= ADDR_FLASH_SECTOR_6))
|
||||
{
|
||||
sector = ADDR_FLASH_SECTOR_7;
|
||||
}
|
||||
else if ((address < ADDR_FLASH_SECTOR_8) && (address >= ADDR_FLASH_SECTOR_7))
|
||||
{
|
||||
sector = ADDR_FLASH_SECTOR_8;
|
||||
}
|
||||
else if ((address < ADDR_FLASH_SECTOR_9) && (address >= ADDR_FLASH_SECTOR_8))
|
||||
{
|
||||
sector = ADDR_FLASH_SECTOR_9;
|
||||
}
|
||||
else if ((address < ADDR_FLASH_SECTOR_10) && (address >= ADDR_FLASH_SECTOR_9))
|
||||
{
|
||||
sector = ADDR_FLASH_SECTOR_10;
|
||||
}
|
||||
else if ((address < ADDR_FLASH_SECTOR_11) && (address >= ADDR_FLASH_SECTOR_10))
|
||||
{
|
||||
sector = ADDR_FLASH_SECTOR_11;
|
||||
}
|
||||
else /*(address < FLASH_END_ADDR) && (address >= ADDR_FLASH_SECTOR_23))*/
|
||||
{
|
||||
sector = FLASH_END_ADDR;
|
||||
}
|
||||
return sector;
|
||||
}
|
||||
79
User_Code/bsp/flash/bsp_flash.h
Normal file
79
User_Code/bsp/flash/bsp_flash.h
Normal file
@@ -0,0 +1,79 @@
|
||||
#ifndef _BSP_FLASH_H
|
||||
#define _BSP_FLASH_H
|
||||
#include "main.h"
|
||||
|
||||
/* Base address of the Flash sectors */
|
||||
#define ADDR_FLASH_SECTOR_0 ((uint32_t)0x08000000) /* Base address of Sector 0, 16 Kbytes */
|
||||
#define ADDR_FLASH_SECTOR_1 ((uint32_t)0x08004000) /* Base address of Sector 1, 16 Kbytes */
|
||||
#define ADDR_FLASH_SECTOR_2 ((uint32_t)0x08008000) /* Base address of Sector 2, 16 Kbytes */
|
||||
#define ADDR_FLASH_SECTOR_3 ((uint32_t)0x0800C000) /* Base address of Sector 3, 16 Kbytes */
|
||||
#define ADDR_FLASH_SECTOR_4 ((uint32_t)0x08010000) /* Base address of Sector 4, 64 Kbytes */
|
||||
#define ADDR_FLASH_SECTOR_5 ((uint32_t)0x08020000) /* Base address of Sector 5, 128 Kbytes */
|
||||
#define ADDR_FLASH_SECTOR_6 ((uint32_t)0x08040000) /* Base address of Sector 6, 128 Kbytes */
|
||||
#define ADDR_FLASH_SECTOR_7 ((uint32_t)0x08060000) /* Base address of Sector 7, 128 Kbytes */
|
||||
#define ADDR_FLASH_SECTOR_8 ((uint32_t)0x08080000) /* Base address of Sector 8, 128 Kbytes */
|
||||
#define ADDR_FLASH_SECTOR_9 ((uint32_t)0x080A0000) /* Base address of Sector 9, 128 Kbytes */
|
||||
#define ADDR_FLASH_SECTOR_10 ((uint32_t)0x080C0000) /* Base address of Sector 10, 128 Kbytes */
|
||||
#define ADDR_FLASH_SECTOR_11 ((uint32_t)0x080E0000) /* Base address of Sector 11, 128 Kbytes */
|
||||
|
||||
#define FLASH_END_ADDR ((uint32_t)0x08100000) /* Base address of Sector 23, 128 Kbytes */
|
||||
|
||||
|
||||
#define ADDR_FLASH_SECTOR_12 ((uint32_t)0x08100000) /* Base address of Sector 12, 16 Kbytes */
|
||||
#define ADDR_FLASH_SECTOR_13 ((uint32_t)0x08104000) /* Base address of Sector 13, 16 Kbytes */
|
||||
#define ADDR_FLASH_SECTOR_14 ((uint32_t)0x08108000) /* Base address of Sector 14, 16 Kbytes */
|
||||
#define ADDR_FLASH_SECTOR_15 ((uint32_t)0x0810C000) /* Base address of Sector 15, 16 Kbytes */
|
||||
#define ADDR_FLASH_SECTOR_16 ((uint32_t)0x08110000) /* Base address of Sector 16, 64 Kbytes */
|
||||
#define ADDR_FLASH_SECTOR_17 ((uint32_t)0x08120000) /* Base address of Sector 17, 128 Kbytes */
|
||||
#define ADDR_FLASH_SECTOR_18 ((uint32_t)0x08140000) /* Base address of Sector 18, 128 Kbytes */
|
||||
#define ADDR_FLASH_SECTOR_19 ((uint32_t)0x08160000) /* Base address of Sector 19, 128 Kbytes */
|
||||
#define ADDR_FLASH_SECTOR_20 ((uint32_t)0x08180000) /* Base address of Sector 20, 128 Kbytes */
|
||||
#define ADDR_FLASH_SECTOR_21 ((uint32_t)0x081A0000) /* Base address of Sector 21, 128 Kbytes */
|
||||
#define ADDR_FLASH_SECTOR_22 ((uint32_t)0x081C0000) /* Base address of Sector 22, 128 Kbytes */
|
||||
#define ADDR_FLASH_SECTOR_23 ((uint32_t)0x081E0000) /* Base address of Sector 23, 128 Kbytes */
|
||||
|
||||
|
||||
/**
|
||||
* @brief erase flash
|
||||
* @param[in] address: flash address
|
||||
* @param[in] len: page num
|
||||
* @retval none
|
||||
*/
|
||||
void flash_erase_address(uint32_t address, uint16_t len);
|
||||
|
||||
/**
|
||||
* @brief write data to one page of flash
|
||||
* @param[in] start_address: flash address
|
||||
* @param[in] buf: data point
|
||||
* @param[in] len: data num
|
||||
* @retval success 0, fail -1
|
||||
*/
|
||||
int8_t flash_write_single_address(uint32_t start_address, uint32_t *buf, uint32_t len);
|
||||
|
||||
|
||||
/**
|
||||
* @brief write data to some pages of flash
|
||||
* @param[in] start_address: flash start address
|
||||
* @param[in] end_address: flash end address
|
||||
* @param[in] buf: data point
|
||||
* @param[in] len: data num
|
||||
* @retval success 0, fail -1
|
||||
*/
|
||||
int8_t flash_write_muli_address(uint32_t start_address, uint32_t end_address, uint32_t *buf, uint32_t len);
|
||||
|
||||
/**
|
||||
* @brief read data for flash
|
||||
* @param[in] address: flash address
|
||||
* @param[out] buf: data point
|
||||
* @param[in] len: data num
|
||||
* @retval none
|
||||
*/
|
||||
void flash_read(uint32_t address, uint32_t *buf, uint32_t len);
|
||||
|
||||
/**
|
||||
* @brief get the next page flash address
|
||||
* @param[in] address: flash address
|
||||
* @retval next page flash address
|
||||
*/
|
||||
uint32_t get_next_flash_address(uint32_t address);
|
||||
#endif
|
||||
0
User_Code/bsp/flash/bsp_flash.md
Normal file
0
User_Code/bsp/flash/bsp_flash.md
Normal file
146
User_Code/bsp/iic/bsp_iic.c
Normal file
146
User_Code/bsp/iic/bsp_iic.c
Normal file
@@ -0,0 +1,146 @@
|
||||
// #include "bsp_iic.h"
|
||||
// #include "memory.h"
|
||||
// #include "stdlib.h"
|
||||
//
|
||||
// static uint8_t idx = 0; // 配合中断以及初始化
|
||||
// static IICInstance *iic_instance[IIC_DEVICE_CNT] = {NULL};
|
||||
//
|
||||
// IICInstance *IICRegister(IIC_Init_Config_s *conf)
|
||||
// {
|
||||
// if (idx >= MX_IIC_SLAVE_CNT) // 超过最大实例数
|
||||
// while (1) // 酌情增加允许的实例上限,也有可能是内存泄漏
|
||||
// ;
|
||||
// // 申请到的空间未必是0, 所以需要手动初始化
|
||||
// IICInstance *instance = (IICInstance *) malloc(sizeof(IICInstance));
|
||||
// instance = (IICInstance *) malloc(sizeof(IICInstance));
|
||||
// memset(instance, 0, sizeof(IICInstance));
|
||||
//
|
||||
// instance->dev_address = conf->dev_address << 1; // 地址左移一位,最低位为读写位
|
||||
// instance->callback = conf->callback;
|
||||
// instance->work_mode = conf->work_mode;
|
||||
// instance->handle = conf->handle;
|
||||
// instance->id = conf->id;
|
||||
//
|
||||
// iic_instance[idx++] = instance;
|
||||
// return instance;
|
||||
// }
|
||||
//
|
||||
// void IICSetMode(IICInstance *iic, IIC_Work_Mode_e mode)
|
||||
// {
|
||||
// // HAL自带重入保护,不需要手动终止或等待传输完成
|
||||
// if (iic->work_mode != mode)
|
||||
// {
|
||||
// iic->work_mode = mode; // 如果不同才需要修改
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// void IICTransmit(IICInstance *iic, uint8_t *data, uint16_t size, IIC_Seq_Mode_e seq_mode)
|
||||
// {
|
||||
// if (seq_mode != IIC_SEQ_RELEASE && seq_mode != IIC_SEQ_HOLDON)
|
||||
// while (1); // 未知传输模式, 程序停止
|
||||
//
|
||||
// // 根据不同的工作模式进行不同的传输
|
||||
// switch (iic->work_mode)
|
||||
// {
|
||||
// case IIC_BLOCK_MODE:
|
||||
// if (seq_mode != IIC_SEQ_RELEASE)
|
||||
// while (1); // 阻塞模式下不支持HOLD ON模式!!!只能传输完成后立刻释放总线
|
||||
// HAL_I2C_Master_Transmit(iic->handle, iic->dev_address, data, size, 100); // 默认超时时间100ms
|
||||
// break;
|
||||
// case IIC_IT_MODE:
|
||||
// if (seq_mode == IIC_SEQ_RELEASE)
|
||||
// HAL_I2C_Master_Seq_Transmit_IT(iic->handle, iic->dev_address, data, size, I2C_OTHER_AND_LAST_FRAME);
|
||||
// else if (seq_mode == IIC_SEQ_HOLDON)
|
||||
// HAL_I2C_Master_Seq_Transmit_IT(iic->handle, iic->dev_address, data, size, I2C_OTHER_FRAME);
|
||||
// break;
|
||||
// case IIC_DMA_MODE:
|
||||
// if (seq_mode == IIC_SEQ_RELEASE)
|
||||
// HAL_I2C_Master_Seq_Transmit_DMA(iic->handle, iic->dev_address, data, size, I2C_OTHER_AND_LAST_FRAME);
|
||||
// else if (seq_mode == IIC_SEQ_HOLDON)
|
||||
// HAL_I2C_Master_Seq_Transmit_DMA(iic->handle, iic->dev_address, data, size, I2C_OTHER_FRAME);
|
||||
// break;
|
||||
// default:
|
||||
// while (1); // 未知传输模式, 程序停止
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// void IICReceive(IICInstance *iic, uint8_t *data, uint16_t size, IIC_Seq_Mode_e seq_mode)
|
||||
// {
|
||||
// if (seq_mode != IIC_SEQ_RELEASE && seq_mode != IIC_SEQ_HOLDON)
|
||||
// while (1); // 未知传输模式, 程序停止,请检查指针越界
|
||||
//
|
||||
// // 初始化接收缓冲区地址以及接受长度, 用于中断回调函数
|
||||
// iic->rx_buffer = data;
|
||||
// iic->rx_len = size;
|
||||
//
|
||||
// switch (iic->work_mode)
|
||||
// {
|
||||
// case IIC_BLOCK_MODE:
|
||||
// if (seq_mode != IIC_SEQ_RELEASE)
|
||||
// while (1); // 阻塞模式下不支持HOLD ON模式!!!
|
||||
// HAL_I2C_Master_Receive(iic->handle, iic->dev_address, data, size, 100); // 默认超时时间100ms
|
||||
// break;
|
||||
// case IIC_IT_MODE:
|
||||
// if (seq_mode == IIC_SEQ_RELEASE)
|
||||
// HAL_I2C_Master_Seq_Receive_IT(iic->handle, iic->dev_address, data, size, I2C_OTHER_AND_LAST_FRAME);
|
||||
// else if (seq_mode == IIC_SEQ_HOLDON)
|
||||
// HAL_I2C_Master_Seq_Receive_IT(iic->handle, iic->dev_address, data, size, I2C_OTHER_FRAME);
|
||||
// break;
|
||||
// case IIC_DMA_MODE:
|
||||
// if (seq_mode == IIC_SEQ_RELEASE)
|
||||
// HAL_I2C_Master_Seq_Receive_DMA(iic->handle, iic->dev_address, data, size, I2C_OTHER_AND_LAST_FRAME);
|
||||
// else if (seq_mode == IIC_SEQ_HOLDON)
|
||||
// HAL_I2C_Master_Seq_Receive_DMA(iic->handle, iic->dev_address, data, size, I2C_OTHER_FRAME);
|
||||
// break;
|
||||
// default:
|
||||
// while (1); // 未知传输模式, 程序停止
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// void IICAccessMem(IICInstance *iic, uint16_t mem_addr, uint8_t *data, uint16_t size, IIC_Mem_Mode_e mem_mode,
|
||||
// uint8_t mem8bit_flag)
|
||||
// {
|
||||
// uint16_t bit_flag = mem8bit_flag ? I2C_MEMADD_SIZE_8BIT : I2C_MEMADD_SIZE_16BIT;
|
||||
// if (mem_mode == IIC_WRITE_MEM)
|
||||
// {
|
||||
// HAL_I2C_Mem_Write(iic->handle, iic->dev_address, mem_addr, bit_flag, data, size, 100);
|
||||
// }
|
||||
// else if (mem_mode == IIC_READ_MEM)
|
||||
// {
|
||||
// HAL_I2C_Mem_Read(iic->handle, iic->dev_address, mem_addr, bit_flag, data, size, 100);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// while (1); // 未知模式, 程序停止
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * @brief IIC接收完成回调函数
|
||||
// *
|
||||
// * @param hi2c handle
|
||||
// */
|
||||
// void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
|
||||
// {
|
||||
// // 如果是当前i2c硬件发出的complete,且dev_address和之前发起接收的地址相同,同时回到函数不为空, 则调用回调函数
|
||||
// for (uint8_t i = 0; i < idx; i++)
|
||||
// {
|
||||
// if (iic_instance[i]->handle == hi2c && hi2c->Devaddress == iic_instance[i]->dev_address)
|
||||
// {
|
||||
// if (iic_instance[i]->callback != NULL) // 回调函数不为空
|
||||
// iic_instance[i]->callback(iic_instance[i]);
|
||||
// return;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * @brief 内存访问回调函数,仅做形式上的封装,仍然使用HAL_I2C_MasterRxCpltCallback
|
||||
// *
|
||||
// * @param hi2c handle
|
||||
// */
|
||||
// void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c)
|
||||
// {
|
||||
// HAL_I2C_MasterRxCpltCallback(hi2c);
|
||||
// }
|
||||
108
User_Code/bsp/iic/bsp_iic.h
Normal file
108
User_Code/bsp/iic/bsp_iic.h
Normal file
@@ -0,0 +1,108 @@
|
||||
// #include "i2c.h"
|
||||
// #include "stdint.h"
|
||||
//
|
||||
// #define IIC_DEVICE_CNT 2 // C板引出了I2C2和I2C3
|
||||
// #define MX_IIC_SLAVE_CNT 8 // 最大从机数目,根据需要修改
|
||||
//
|
||||
// /* i2c 工作模式枚举 */
|
||||
// typedef enum
|
||||
// {
|
||||
// // 基本工作模式
|
||||
// IIC_BLOCK_MODE = 0, // 阻塞模式
|
||||
// IIC_IT_MODE, // 中断模式
|
||||
// IIC_DMA_MODE, // DMA模式
|
||||
//
|
||||
// } IIC_Work_Mode_e;
|
||||
//
|
||||
// /* I2C MEM工作模式枚举,这两种方法都是阻塞 */
|
||||
// typedef enum
|
||||
// {
|
||||
// IIC_READ_MEM = 0, // 读取从机内部的寄存器or内存
|
||||
// IIC_WRITE_MEM, // 写入从机内部的寄存器or内存
|
||||
// } IIC_Mem_Mode_e;
|
||||
//
|
||||
// /* Seq传输工作模式枚举,注意HOLD_ON要在IT或DMA下使用 */
|
||||
// // 必须以IIC_RELEASE为最后一次传输,否则会导致总线占有权无法释放
|
||||
// typedef enum
|
||||
// {
|
||||
// IIC_SEQ_RELEASE, // 完成传输后释放总线占有权,这是默认的传输方式
|
||||
// IIC_SEQ_HOLDON = 0, // 保持总线占有权不释放,只支持IT和DMA模式
|
||||
// } IIC_Seq_Mode_e;
|
||||
//
|
||||
// /* i2c实例 */
|
||||
// typedef struct iic_temp_s
|
||||
// {
|
||||
// I2C_HandleTypeDef *handle; // i2c handle
|
||||
// uint8_t dev_address; // 暂时只支持7位地址(还有一位是读写位)
|
||||
//
|
||||
// IIC_Work_Mode_e work_mode; // 工作模式
|
||||
// uint8_t *rx_buffer; // 接收缓冲区指针
|
||||
// uint8_t rx_len; // 接收长度
|
||||
// void (*callback)(struct iic_temp_s *); // 接收完成后的回调函数
|
||||
//
|
||||
// void *id; // 用于标识i2c instance
|
||||
// } IICInstance;
|
||||
//
|
||||
// /* I2C 初始化结构体配置 */
|
||||
// typedef struct
|
||||
// {
|
||||
// I2C_HandleTypeDef *handle; // i2c handle
|
||||
// uint8_t dev_address; // 暂时只支持7位地址(还有一位是读写位),注意不需要左移
|
||||
// IIC_Work_Mode_e work_mode; // 工作模式
|
||||
// void (*callback)(IICInstance *); // 接收完成后的回调函数
|
||||
// void *id; // 用于标识i2c instance
|
||||
// } IIC_Init_Config_s;
|
||||
//
|
||||
// /**
|
||||
// * @brief IIC初始化
|
||||
// *
|
||||
// * @param conf 初始化配置
|
||||
// * @return IICInstance*
|
||||
// */
|
||||
// IICInstance *IICRegister(IIC_Init_Config_s *conf);
|
||||
//
|
||||
// /**
|
||||
// * @brief IIC设置工作模式
|
||||
// *
|
||||
// * @param iic 要设置的iic实例
|
||||
// * @param mode 工作模式
|
||||
// */
|
||||
// void IICSetMode(IICInstance *iic, IIC_Work_Mode_e mode);
|
||||
//
|
||||
// /**
|
||||
// * @brief IIC发送数据
|
||||
// *
|
||||
// * @param iic iic实例
|
||||
// * @param data 待发送的数据首地址指针
|
||||
// * @param size 发送长度
|
||||
// * @param mode 序列传输模式
|
||||
// * @note 注意,如果发送结构体,那么该结构体在声明时务必使用#pragma pack(1)进行对齐,并在声明结束后使用#pragma pack()恢复对齐
|
||||
// *
|
||||
// */
|
||||
// void IICTransmit(IICInstance *iic, uint8_t *data, uint16_t size, IIC_Seq_Mode_e mode);
|
||||
//
|
||||
// /**
|
||||
// * @brief IIC接收数据
|
||||
// *
|
||||
// * @param iic iic实例
|
||||
// * @param data 接收数据的首地址指针
|
||||
// * @param size 接收长度
|
||||
// * @param mode 序列传输模式
|
||||
// * @note 注意,如果直接将接收数据memcpy到目标结构体或通过强制类型转换进行逐字节写入,
|
||||
// * 那么该结构体在声明时务必使用#pragma pack(1)进行对齐,并在声明结束后使用#pragma pack()恢复对齐
|
||||
// */
|
||||
// void IICReceive(IICInstance *iic, uint8_t *data, uint16_t size, IIC_Seq_Mode_e mode);
|
||||
//
|
||||
// /**
|
||||
// * @brief IIC读取从机寄存器(内存),只支持阻塞模式,超时默认为1ms
|
||||
// *
|
||||
// * @param iic iic实例
|
||||
// * @param mem_addr 要读取的从机内存地址,目前只支持8位地址
|
||||
// * @param data 要读取或写入的数据首地址指针
|
||||
// * @param size 要读取或写入的数据长度
|
||||
// * @param mode 写入或读取模式: IIC_READ_MEM or IIC_WRITE_MEM
|
||||
// * @param mem8bit_flag 从机内存地址是否为8位
|
||||
// */
|
||||
// void IICAccessMem(IICInstance *iic, uint16_t mem_addr, uint8_t *data, uint16_t size, IIC_Mem_Mode_e mode,
|
||||
// uint8_t mem8bit_flag);
|
||||
|
||||
19
User_Code/bsp/iic/bsp_iic.md
Normal file
19
User_Code/bsp/iic/bsp_iic.md
Normal file
@@ -0,0 +1,19 @@
|
||||
# bsp iic(暂未启用)
|
||||
|
||||
> 预计增加模拟iic
|
||||
|
||||
## 注意事项
|
||||
|
||||
使用时写入地址,不需要左移!!!
|
||||
|
||||
cubemx未配置dma时请勿使用dma传输,其行为是未定义的。
|
||||
|
||||
## 总线机制详解
|
||||
|
||||
关于I2C的序列传输,Restart condition和总线仲裁,请看:
|
||||
|
||||
https://blog.csdn.net/NeoZng/article/details/128496694
|
||||
|
||||
https://blog.csdn.net/NeoZng/article/details/128486366
|
||||
|
||||
使用序列通信则在单次通信后不会释放总线,继续占用直到调用传输函数时传入`IIC_RELEASE`参数. 这个功能只在一条总线上挂载多个主机的时候有用.
|
||||
Reference in New Issue
Block a user