文章目录

  • LPS25HB 寄存器读写程序解读
    • 1、读写功能的统一接口函数
    • 2、设计结构体函数指针来调用统一的读写函数
    • 3、与通信方式无关的寄存器读写抽象函数接口

LPS25HB 寄存器读写程序解读

一般地,芯片公司都会提供芯片驱动的一些驱动代码,以LPS25HB 为例,该Mems工作时,与主MCU通信通过IIC或者SPI的方式进行,从而实现Mems的寄存器的读写。

1、读写功能的统一接口函数

为了兼容IIC和SPI的通信,我们这里设计两个基于STM32 HAL 库的的读写函数platform_write()、platform_read():

static int32_t platform_write(void *handle, uint8_t Reg, uint8_t *Bufp,uint16_t len)
{if (handle == &hi2c1){/* enable auto incremented in multiple read/write commands */Reg |= 0x80;HAL_I2C_Mem_Write(handle, LPS25HB_I2C_ADD_L, Reg,I2C_MEMADD_SIZE_8BIT, Bufp, len, 1000);}
#ifdef MKI109V2else if (handle == &hspi2){/* enable auto incremented in multiple read/write commands */Reg |= 0x40;HAL_GPIO_WritePin(CS_SPI2_GPIO_Port, CS_SPI2_Pin, GPIO_PIN_RESET);HAL_SPI_Transmit(handle, &Reg, 1, 1000);HAL_SPI_Transmit(handle, Bufp, len, 1000);HAL_GPIO_WritePin(CS_SPI2_GPIO_Port, CS_SPI2_Pin, GPIO_PIN_SET);}else if (handle == &hspi1){/* enable auto incremented in multiple read/write commands */Reg |= 0x40;HAL_GPIO_WritePin(CS_SPI1_GPIO_Port, CS_SPI1_Pin, GPIO_PIN_RESET);HAL_SPI_Transmit(handle, &Reg, 1, 1000);HAL_SPI_Transmit(handle, Bufp, len, 1000);HAL_GPIO_WritePin(CS_SPI1_GPIO_Port, CS_SPI1_Pin, GPIO_PIN_SET);}
#endifreturn 0;
}static int32_t platform_read(void *handle, uint8_t Reg, uint8_t *Bufp,uint16_t len)
{if (handle == &hi2c1){/* enable auto incremented in multiple read/write commands */Reg |= 0x80;HAL_I2C_Mem_Read(handle, LPS25HB_I2C_ADD_L, Reg,I2C_MEMADD_SIZE_8BIT, Bufp, len, 1000);}
#ifdef MKI109V2else if (handle == &hspi2){/* enable auto incremented in multiple read/write commands */Reg |= 0xC0;HAL_GPIO_WritePin(CS_DEV_GPIO_Port, CS_DEV_Pin, GPIO_PIN_RESET);HAL_SPI_Transmit(handle, &Reg, 1, 1000);HAL_SPI_Receive(handle, Bufp, len, 1000);HAL_GPIO_WritePin(CS_DEV_GPIO_Port, CS_DEV_Pin, GPIO_PIN_SET);}else{/* enable auto incremented in multiple read/write commands */Reg |= 0xC0;HAL_GPIO_WritePin(CS_RF_GPIO_Port, CS_RF_Pin, GPIO_PIN_RESET);HAL_SPI_Transmit(handle, &Reg, 1, 1000);HAL_SPI_Receive(handle, Bufp, len, 1000);HAL_GPIO_WritePin(CS_RF_GPIO_Port, CS_RF_Pin, GPIO_PIN_SET);}
#endifreturn 0;
}

2、设计结构体函数指针来调用统一的读写函数

typedef int32_t (*lps25hb_write_ptr)(void *, uint8_t, uint8_t*, uint16_t);
typedef int32_t (*lps25hb_read_ptr) (void *, uint8_t, uint8_t*, uint16_t);typedef struct {/** Component mandatory fields **/lps25hb_write_ptr  write_reg;lps25hb_read_ptr   read_reg;/** Customizable optional pointer **/void *handle;
} lps25hb_ctx_t;

这样我们定义了一个结构体lps25hb_ctx_t,该结构体中设计了三个指针。通过声明一个该结构体的变量,将变量的成员指向统一的读写结构函数。

 /* Initialize mems driver interface */lps25hb_ctx_t dev_ctx;dev_ctx.write_reg = platform_write;dev_ctx.read_reg = platform_read;dev_ctx.handle = &hi2c1;

这样Mems 的驱动接口就实现了。但是为了程序更好的可读性,我们需要将Mems 寄存器读写的具体功能函数进一步实现。

这里我们设计与上层接口函数无关的寄存器读写函数,利用还函数实现Mems内部所有寄存器的读写。

3、与通信方式无关的寄存器读写抽象函数接口

/*** @brief  Read generic device register** @param  ctx   read / write interface definitions(ptr)* @param  reg   register to read* @param  data  pointer to buffer that store the data read(ptr)* @param  len   number of consecutive register to read* @retval       interface status (MANDATORY: return 0 -> no Error)**/
int32_t lps25hb_read_reg(lps25hb_ctx_t* ctx, uint8_t reg, uint8_t* data,uint16_t len)
{int32_t ret;ret = ctx->read_reg(ctx->handle, reg, data, len);return ret;
}/*** @brief  Write generic device register** @param  ctx   read / write interface definitions(ptr)* @param  reg   register to write* @param  data  pointer to data to write in register reg(ptr)* @param  len   number of consecutive register to write* @retval       interface status (MANDATORY: return 0 -> no Error)**/
int32_t lps25hb_write_reg(lps25hb_ctx_t* ctx, uint8_t reg, uint8_t* data,uint16_t len)
{int32_t ret;ret = ctx->write_reg(ctx->handle, reg, data, len);return ret;
}

这样,我们就实现了接口函数的对应关系

于是,我们可以根据Mems的寄存器表进行相关的寄存器读写设计了。

如:


/*** @brief  The Reference pressure value is a 24-bit data expressed as 2’s*         complement. The value is used when AUTOZERO or AUTORIFP function*         is enabled.[set]** @param  ctx    Read / write interface definitions.(ptr)* @param  buff   Buffer that contains data to write* @retval        Interface status (MANDATORY: return 0 -> no Error).**/
int32_t lps25hb_pressure_ref_set(lps25hb_ctx_t *ctx, uint8_t *buff)
{int32_t ret;ret = lps25hb_read_reg(ctx, LPS25HB_REF_P_XL,  buff, 3);return ret;
}/*** @brief  The Reference pressure value is a 24-bit data expressed as 2’s*         complement. The value is used when AUTOZERO or AUTORIFP function*         is enabled.[get]** @param  ctx    Read / write interface definitions.(ptr)* @param  buff   Buffer that stores data read.(ptr)* @retval        Interface status (MANDATORY: return 0 -> no Error).**/
int32_t lps25hb_pressure_ref_get(lps25hb_ctx_t *ctx, uint8_t *buff)
{int32_t ret;ret = lps25hb_read_reg(ctx, LPS25HB_REF_P_XL,  buff, 3);return ret;
}

4、寄存器的数据描述:
寄存器表的数据描述包括 define 定义、结构体数据类型定义、枚举变量的定义

例如:


#define LPS25HB_CTRL_REG1       0x20U
typedef struct {uint8_t sim              : 1;uint8_t reset_az         : 1;uint8_t bdu              : 1;uint8_t diff_en          : 1;uint8_t odr              : 4; /* pd + odr -> odr */
} lps25hb_ctrl_reg1_t;#define LPS25HB_CTRL_REG2       0x21U
typedef struct {uint8_t one_shot         : 1;uint8_t autozero         : 1;uint8_t swreset          : 1;uint8_t i2c_dis          : 1;uint8_t fifo_mean_dec    : 1;uint8_t stop_on_fth      : 1;uint8_t fifo_en          : 1;uint8_t boot             : 1;
} lps25hb_ctrl_reg2_t;#define LPS25HB_CTRL_REG3       0x22U
typedef struct {uint8_t int_s           : 2;uint8_t not_used_01     : 4;uint8_t pp_od           : 1;uint8_t int_h_l         : 1;
} lps25hb_ctrl_reg3_t;#define LPS25HB_CTRL_REG4       0x23U
typedef struct {uint8_t drdy            : 1;uint8_t f_ovr           : 1;uint8_t f_fth           : 1;uint8_t f_empty         : 1;uint8_t not_used_01     : 4;
} lps25hb_ctrl_reg4_t;typedef enum {LPS25HB_BYPASS_MODE               = 0,LPS25HB_FIFO_MODE                 = 1,LPS25HB_STREAM_MODE               = 2,LPS25HB_Stream_to_FIFO_mode      = 3,LPS25HB_BYPASS_TO_STREAM_MODE    = 4,LPS25HB_MEAN_MODE                 = 6,LPS25HB_BYPASS_TO_FIFO_MODE      = 7,
} lps25hb_f_mode_t;

这样,依赖这些个寄存器变量的描述,设计相关的具体的某一个寄存器读写函数,Mems 的驱动程序代码就可以轻松实现了。

LPS25HB 寄存器读写程序解读相关推荐

  1. 关于MPU6050学习的一些总结之三MPU6050程序解读

    关于MPU6050学习的一些总结之三MPU6050程序解读 前言 MPU6050.h 初始化函数 获取数据 数据处理(计算零偏) 结语 前言 经过两天的整理终于可以尝试解读MPU6050程序了,话不多 ...

  2. 【Android 逆向】ptrace 函数 ( ptrace 函数族 | 进程附着 | 进程脱离 | 进程数据读写权限 | 进程对应的主线程寄存器读写 | 单步调试 |ptrace 函数族状态转换 )

    文章目录 一.ptrace 函数族 1.进程附着 2.进程脱离 3.进程数据读写权限 4.进程对应的主线程寄存器读写 5.单步调试 6.继续向后执行 二.ptrace 函数族状态转换 一.ptrace ...

  3. UEFI开发历程2—基于SuperIO芯片的寄存器读写

    一.SuperIO介绍 目前市面上的主流SuperIO芯片有NCT.ITE等厂商的生产的芯片,本文主要针对IT8613E这款芯片的逻辑设备寄存器的读写进行介绍. IT8613E主要用来连接外部低速设备 ...

  4. AT88SC0104C读写程序

    AT88SC0104开发包(AT88SC0104C读写程序)C语言 2010-8-22 16:02:51 本站原创 佚名 [字体:大 中 小] 本程序适用于读写AT88SC0104C系列器件.直接调用 ...

  5. c语言中读取flash值的作用,flash读写程序

    原标题:flash读写程序 在电子工程世界为您找到如下关于"flash读写程序"的新闻 msp430F149单片机的flash读写程序 //基于msp430F149单片机的flas ...

  6. 咚咚咚————【封装驱动】DS3231时钟芯片读写程序,分享交流自己编写的程序。

    咚咚咚----[封装驱动]DS3231时钟芯片读写程序,分享交流自己编写的程序. /******************************************** 主控芯片:STM32 Co ...

  7. 单片机编程php,STC单片机内部FLASH读写程序(最新整理)

    <STC单片机内部FLASH读写程序(最新整理)>由会员分享,可在线阅读,更多相关<STC单片机内部FLASH读写程序(最新整理)(6页珍藏版)>请在人人文库网上搜索. 1.S ...

  8. STM32 内部Flash读写 程序源码 [已验证]

    目录 STM32 内部Flash带缓存读写 程序源码 0 Macro 1.Flash_Erase 2. Flash_Read_Byte 3.Flash_Write_NoBuffer 4.Flash_W ...

  9. 24c512 c语言程序,24C32~24C512的51单片机读写程序

    24C32~24C512的51单片机读写程序 来源:-- 作者:-- 浏览:3875 时间:2016-08-10 14:18 标签: 摘要: AT89S52 晶振频率为11.0592MHz 指令周期: ...

最新文章

  1. 助力5G行业应用扬帆启航,第二届5G毫米波产业高峰论坛圆满召开
  2. F - Weakness and Poorness CodeForces - 578C
  3. hbase scan超时设置_如何在优化生产环境的hbase
  4. Go语言 channel
  5. 玩转SmartQQ之登录
  6. 初识vue+elementUi
  7. 本地Jupyter连接远程linux服务器
  8. 数据库编程——intro to JDBC
  9. 三国杀服务器改名 插图修改,《三国杀》大幅修改的武将——新旧两版,你更喜欢哪一位...
  10. 【RTOS】为H7配套的uCOS-III模板也是可以用于MDK AC6的,提供个模板
  11. 电信中兴B860AV2.1-T_线刷固件包
  12. 联想微型计算机安装Win7,联想c340如何安装win7_联想c430一体机改win7系统步骤
  13. 《高性能MySQL》阅读 -Mysql基本特性
  14. python导入模块不存在_基于pycharm导入模块显示不存在的解决方法
  15. 使用WebSocket搭建一个智能聊天系统
  16. Android Activity 生命周期和重要的相关函数(基础一)
  17. 推荐10款 Java 程序员测试工具
  18. 铁路计算机应用期刊级别,铁路单位评审高级职称可以发表哪些期刊呢?
  19. 华为起诉Verizon侵权在美开庭,涉及光传输网络
  20. Android 面试技巧分享~

热门文章

  1. Linux系统\Centos没有网卡eth0配置文件怎么办?
  2. 3DSlicer13:Command Line Interface(CLI)
  3. delphi中的bpl开发注意事项
  4. 保存Delphi中的环境设置中的library path
  5. C#中代理的简单应用
  6. EffectiveC++编程的50个建议
  7. 通过对代码进行调试讲解缓冲区溢出原理
  8. panel.setLayout(null);
  9. Working copy XXX locked and cleanup failed in SVN
  10. Hi3516A开发--目录分析