一个稳定可靠的STM32I2C程序解析(精编版)_第1页
一个稳定可靠的STM32I2C程序解析(精编版)_第2页
一个稳定可靠的STM32I2C程序解析(精编版)_第3页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、/*file : i2c.hpurpose : */#ifndef _i2c_h_#define _i2c_h_/* includes */* defines */#define sensors_i2c i2c2#define i2c_speed 400000#define i2c_own_address 0 x00#define i2c_config() i2cmaster_init();/* prototypes */void i2cmaster_init(void);unsigned long sensors_i2c_writeregister(unsigned char address

2、, unsigned char registeraddr, unsigned short registerlen, const unsigned char *registervalue);unsigned long sensors_i2c_readregister(unsigned char address, unsigned char registeraddr, unsigned short registerlen, unsigned char *registervalue);int sensors_i2c_writeregister_swap(unsigned char slave_add

3、r,unsigned char reg_addr,unsigned char len, unsigned char *data_ptr);int sensors_i2c_readregister_swap(unsigned char slave_addr,unsigned char reg_addr,unsigned char len, unsigned char *data_ptr);int i2c_reset_user();#endif / _i2c_h_/*file : i2c.cpurpose : i2c 3 to communicate with the sensorsauthor

4、: * includes */#include #include stm32l1xx.h#include i2c.h#include gpio.h#include log.h#include discover_board.h#include main.h/* defines */#define i2cx_flag_timeout (uint32_t) 900)#define i2cx_long_timeout (uint32_t)(300 * i2cx_flag_timeout)#define sensors_i2c_scl_gpio_port gpiob#define sensors_i2c

5、_scl_gpio_clk rcc_ahbperiph_gpiob#define sensors_i2c_scl_gpio_pin gpio_pin_10#define sensors_i2c_scl_gpio_pinsource gpio_pinsource10#define sensors_i2c_sda_gpio_port gpiob#define sensors_i2c_sda_gpio_clk rcc_ahbperiph_gpiob#define sensors_i2c_sda_gpio_pin gpio_pin_11#define sensors_i2c_sda_gpio_pins

6、ource gpio_pinsource11#define sensors_i2c_rcc_clk rcc_apb1periph_i2c2#define sensors_i2c_af gpio_af_i2c2#define wait_for_flag(flag, value, timeout, errorcode) #define clear_addr_bit /* globals */* prototypes */* function */static uint32_t i2cx_timeout_usercallback(char value);void i2cmaster_init(voi

7、d)gpio_inittypedef gpio_initstructure;i2c_inittypedef i2c_initstructure;/* enable i2cx clock */rcc_apb1periphclockcmd(sensors_i2c_rcc_clk, enable);/* enable i2c gpio clock */rcc_ahbperiphclockcmd(sensors_i2c_scl_gpio_clk | sensors_i2c_sda_gpio_clk, enable);/* configure i2cx pin: scl -*/gpio_initstru

8、cture.gpio_pin = sensors_i2c_scl_gpio_pin; gpio_initstructure.gpio_mode = gpio_mode_af;gpio_initstructure.gpio_speed = gpio_speed_10mhz;gpio_initstructure.gpio_otype = gpio_otype_od;gpio_initstructure.gpio_pupd = gpio_pupd_nopull;/* connect pins to periph */gpio_pinafconfig(sensors_i2c_scl_gpio_port

9、, sensors_i2c_scl_gpio_pinsource, sensors_i2c_af); gpio_init(sensors_i2c_scl_gpio_port, &gpio_initstructure);/* configure i2cx pin: sda -*/gpio_initstructure.gpio_pin = sensors_i2c_sda_gpio_pin; /* connect pins to periph */gpio_pinafconfig(sensors_i2c_sda_gpio_port, sensors_i2c_sda_gpio_pinsourc

10、e, sensors_i2c_af); gpio_init(sensors_i2c_sda_gpio_port, &gpio_initstructure); i2c_deinit(sensors_i2c);i2c_initstructure.i2c_mode = i2c_mode_i2c;i2c_initstructure.i2c_dutycycle = i2c_dutycycle_2;i2c_initstructure.i2c_ownaddress1 = i2c_own_address;i2c_initstructure.i2c_ack = i2c_ack_enable;i2c_in

11、itstructure.i2c_acknowledgedaddress = i2c_acknowledgedaddress_7bit;i2c_initstructure.i2c_clockspeed = i2c_speed;/* enable the i2c peripheral */i2c_cmd(sensors_i2c, enable); /* initialize the i2c peripheral */i2c_init(sensors_i2c, &i2c_initstructure);/* brief basic management of the timeout situa

12、tion.* param none.* retval none.*/static uint32_t i2cx_timeout_usercallback(char value)/* the following code allows i2c error recovery and return to normal communicationif the error source doesnt still exist (ie. hardware issue.) */i2c_inittypedef i2c_initstructure;i2c_generatestop(sensors_i2c, enab

13、le);i2c_softwareresetcmd(sensors_i2c, enable);i2c_softwareresetcmd(sensors_i2c, disable);i2c_deinit(sensors_i2c);i2c_initstructure.i2c_mode = i2c_mode_i2c;i2c_initstructure.i2c_dutycycle = i2c_dutycycle_2;i2c_initstructure.i2c_ownaddress1 = i2c_own_address;i2c_initstructure.i2c_ack = i2c_ack_enable;

14、i2c_initstructure.i2c_acknowledgedaddress = i2c_acknowledgedaddress_7bit;i2c_initstructure.i2c_clockspeed = i2c_speed;/* enable the i2c peripheral */i2c_cmd(sensors_i2c, enable); /* initialize the i2c peripheral */i2c_init(sensors_i2c, &i2c_initstructure);#ifdef user_debug mpl_logi(i2c restarted

15、.n);#endifreturn 1;int i2c_reset_user()return i2cx_timeout_usercallback(0); /* brief writes a byte to a given register to the sensors through the control interface (i2c)* param registeraddr: the address (location) of the register to be written.* param registervalue: the byte value to be written into

16、 destination register.* retval 0 if correct communication, else wrong communication*/unsigned long sensors_i2c_writeregister(unsigned char address, unsigned char registeraddr, unsigned short registerlen, const unsigned char *registervalue)uint32_t result = 0;uint32_t i = registerlen;_io uint32_t i2c

17、timeout = i2cx_long_timeout;/registervalue = registervalue + (registerlen - 1);/* wait for the busy flag to be cleared */wait_for_flag (i2c_flag_busy, reset, i2cx_long_timeout, 1);/* start the config sequence */i2c_generatestart(sensors_i2c, enable);/* wait for the start bit to be set */wait_for_fla

18、g (i2c_flag_sb, set, i2cx_flag_timeout, 2);/* transmit the slave address and enable writing operation */i2c_send7bitaddress(sensors_i2c, (address1), i2c_direction_transmitter);/* wait for address bit to be set */wait_for_flag (i2c_flag_addr, set, i2cx_flag_timeout, 3);/* clear the addr interrupt bit

19、 - this is done by reading sr1 and sr2*/clear_addr_bit/* wait for address bit to be set */wait_for_flag (i2c_flag_txe, set, i2cx_flag_timeout, 4);/* transmit the first address for write operation */i2c_senddata(sensors_i2c, registeraddr);while (i-)/* wait for address bit to be set */wait_for_flag (i

20、2c_flag_txe, set, i2cx_flag_timeout, 5);/* prepare the register value to be sent */i2c_senddata(sensors_i2c, *registervalue-);i2c_senddata(sensors_i2c, *registervalue+);/* wait for address bit to be set */wait_for_flag (i2c_flag_btf, set, i2cx_flag_timeout, 6);/* end the configuration sequence */i2c

21、_generatestop(sensors_i2c, enable); /* return the verifying value: 0 (passed) or 1 (failed) */return result; unsigned long sensors_i2c_readregister(unsigned char address, unsigned char registeraddr, unsigned short registerlen, unsigned char *registervalue)uint32_t result = 0;uint8_t bytesremaining =

22、 registerlen;_io uint32_t i2ctimeout = i2cx_long_timeout;/* wait for the busy flag to be cleared */wait_for_flag (i2c_flag_busy, reset, i2cx_long_timeout, 7);/* start the config sequence */i2c_generatestart(sensors_i2c, enable);/* wait for the start bit to be set */wait_for_flag (i2c_flag_sb, set, i

23、2cx_flag_timeout, 8);/* transmit the slave address and enable writing operation */i2c_send7bitaddress(sensors_i2c, (address1), i2c_direction_transmitter);/* wait for the start bit to be set */wait_for_flag (i2c_flag_addr, set, i2cx_flag_timeout, 9);/* clear the addr interrupt bit - this is done by r

24、eading sr1 and sr2*/clear_addr_bit;/* wait for address bit to be set */wait_for_flag (i2c_flag_txe, set, i2cx_flag_timeout, 10);/* transmit the register address to be read */i2c_senddata(sensors_i2c, registeraddr);/* wait for address bit to be set */wait_for_flag (i2c_flag_txe, set, i2cx_flag_timeou

25、t, 11); /*! send start condition a second time */ i2c_generatestart(sensors_i2c, enable);/* wait for the start bit to be set */wait_for_flag (i2c_flag_sb, set, i2cx_flag_timeout, 12);/*! send address for read */i2c_send7bitaddress(sensors_i2c, (address1), i2c_direction_receiver); /* wait for the sta

26、rt bit to be set */wait_for_flag (i2c_flag_addr, set, i2cx_flag_timeout, 13);if (registerlen = 1) /*! disable acknowledgment */i2c_acknowledgeconfig(sensors_i2c, disable);/* clear the addr interrupt bit - this is done by reading sr1 and sr2*/clear_addr_bit;/*! send stop condition */i2c_generatestop(

27、sensors_i2c, enable);/* wait for the rxne bit to be set */wait_for_flag (i2c_flag_rxne, set, i2cx_flag_timeout, 14);/bytesremaining-;/registervaluebytesremaining = i2c_receivedata(sensors_i2c);registervalue0 = i2c_receivedata(sensors_i2c); else if( registerlen = 2) /*!cr1 |= i2c_cr1_pos;/* clear the

28、 addr interrupt bit - this is done by reading sr1 and sr2*/clear_addr_bit; /* wait for the buffer full bit to be set */wait_for_flag (i2c_flag_btf, set, i2cx_flag_timeout, 15);/*! send stop condition */i2c_generatestop(sensors_i2c, enable);/* read 2 bytes */bytesremaining-;/registervaluebytesremaini

29、ng = i2c_receivedata(sensors_i2c);/registervaluebytesremaining = i2c_receivedata(sensors_i2c);registervalue0 = i2c_receivedata(sensors_i2c);registervalue1 = i2c_receivedata(sensors_i2c); else /* more than 2 bytes */* clear the addr interrupt bit - this is done by reading sr1 and sr2*/clear_addr_bit;

30、while(bytesremaining)if( bytesremaining = 3)/* wait for the buffer full bit to be set */wait_for_flag (i2c_flag_btf, set, i2cx_flag_timeout, 16);/*! disable acknowledgment */i2c_acknowledgeconfig(sensors_i2c, disable);/* read 1 bytes */bytesremaining-;/registervaluebytesremaining = i2c_receivedata(s

31、ensors_i2c);*registervalue+ = i2c_receivedata(sensors_i2c);/*! send stop condition */i2c_generatestop(sensors_i2c, enable); /* read 1 bytes */bytesremaining-;/registervaluebytesremaining = i2c_receivedata(sensors_i2c);*registervalue+ = i2c_receivedata(sensors_i2c);/* wait for the buffer full bit to

32、be set */wait_for_flag (i2c_flag_rxne, set, i2cx_flag_timeout, 17);/* read 1 bytes */bytesremaining-;/registervaluebytesremaining = i2c_receivedata(sensors_i2c); *registervalue+ = i2c_receivedata(sensors_i2c);else/* wait for the rxne bit to be set */wait_for_flag (i2c_flag_rxne, set, i2cx_flag_timeo

33、ut, 18);/* read 2 bytes */bytesremaining-;/registervaluebytesremaining = i2c_receivedata(sensors_i2c); *registervalue+ = i2c_receivedata(sensors_i2c); /* clear btf flag */i2c_clearflag(sensors_i2c, i2c_flag_btf);/* wait for the busy flag to be cleared */wait_for_flag (i2c_flag_busy, reset, i2cx_long_timeout, 19); /*!cr1 &=

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论