LPS25HB 寄存器读写程序解读
文章目錄
- LPS25HB 寄存器讀寫程序解讀
- 1、讀寫功能的統(tǒng)一接口函數(shù)
- 2、設(shè)計結(jié)構(gòu)體函數(shù)指針來調(diào)用統(tǒng)一的讀寫函數(shù)
- 3、與通信方式無關(guān)的寄存器讀寫抽象函數(shù)接口
LPS25HB 寄存器讀寫程序解讀
一般地,芯片公司都會提供芯片驅(qū)動的一些驅(qū)動代碼,以LPS25HB 為例,該Mems工作時,與主MCU通信通過IIC或者SPI的方式進(jìn)行,從而實(shí)現(xiàn)Mems的寄存器的讀寫。
1、讀寫功能的統(tǒng)一接口函數(shù)
為了兼容IIC和SPI的通信,我們這里設(shè)計兩個基于STM32 HAL 庫的的讀寫函數(shù)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、設(shè)計結(jié)構(gòu)體函數(shù)指針來調(diào)用統(tǒng)一的讀寫函數(shù)
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;這樣我們定義了一個結(jié)構(gòu)體lps25hb_ctx_t,該結(jié)構(gòu)體中設(shè)計了三個指針。通過聲明一個該結(jié)構(gòu)體的變量,將變量的成員指向統(tǒng)一的讀寫結(jié)構(gòu)函數(shù)。
/* 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 的驅(qū)動接口就實(shí)現(xiàn)了。但是為了程序更好的可讀性,我們需要將Mems 寄存器讀寫的具體功能函數(shù)進(jìn)一步實(shí)現(xiàn)。
這里我們設(shè)計與上層接口函數(shù)無關(guān)的寄存器讀寫函數(shù),利用還函數(shù)實(shí)現(xiàn)Mems內(nèi)部所有寄存器的讀寫。
3、與通信方式無關(guān)的寄存器讀寫抽象函數(shù)接口
/*** @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; }這樣,我們就實(shí)現(xiàn)了接口函數(shù)的對應(yīng)關(guān)系
于是,我們可以根據(jù)Mems的寄存器表進(jìn)行相關(guān)的寄存器讀寫設(shè)計了。
如:
/*** @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、寄存器的數(shù)據(jù)描述:
寄存器表的數(shù)據(jù)描述包括 define 定義、結(jié)構(gòu)體數(shù)據(jù)類型定義、枚舉變量的定義
例如:
#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;這樣,依賴這些個寄存器變量的描述,設(shè)計相關(guān)的具體的某一個寄存器讀寫函數(shù),Mems 的驅(qū)動程序代碼就可以輕松實(shí)現(xiàn)了。
總結(jié)
以上是生活随笔為你收集整理的LPS25HB 寄存器读写程序解读的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LPS25HB 气压计 参考手册中关于
- 下一篇: TREK1000 评估套件的软件技术分析