LPS25HB 寄存器读写程序解读
生活随笔
收集整理的這篇文章主要介紹了
LPS25HB 寄存器读写程序解读
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 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 寄存器读写程序解读的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LPS25HB 气压计 参考手册中关于
- 下一篇: TREK1000 评估套件的软件技术分析