I2C的IO端口初始化

10个帖子/ 0新
最后一篇
jamesleo-konka
离线
最后一次露面:4年2周前
加入:2017-01-22 02:42
I2C的IO端口初始化

Hi, Dialog
I'm studying peripherals_demo, and find the initializing of UART's GPIO ports/pins.
但是没有代码,做i2c的gpio端口/引脚。
Should I do the initializing of I2C's GPIO port/pins ? How to ? Or I miss something?

Thanks

Device:
mt_dialog.
离线
最后一次露面:2个月1周前
职员
加入:2015-06-08 11:34
Hi jamesleo-konka,

Hi jamesleo-konka,

请在Peripherals_demo \ config \ default目录中查看gpio_setup.h文件,在那里,您将找到每个gpio的定义,然后在periph_setup.c中找到peirph_setup()函数您将通过hw_gpio_configure找到引脚的初始化() 功能。

谢谢mt_dialog.

jamesleo-konka
离线
最后一次露面:4年2周前
加入:2017-01-22 02:42
Hi MT_Dialog,

Hi MT_Dialog,
对不起,我错过了GPIO配置代码,它们被重新定义为CFG_GPIO_I2C1_SCL_PORT,并使用宏HW_GPIO_PINCONFIG初始化。

Thanks

jamesleo-konka
离线
最后一次露面:4年2周前
加入:2017-01-22 02:42
Hi MT_Dialog,

Hi MT_Dialog,
I put the following code in periph_setup in project hrp_sensor:
//------------------------------
// ----- I2C端口/引脚配置------ P3.5 = SCL,P1.2 = SDA --------
hw_gpio_configure_pin(hw_gpio_port_3,hw_gpio_pin_5,hw_gpio_mode_output,hw_gpio_func_gpio,true);
hw_gpio_configure_pin(hw_gpio_port_1,hw_gpio_pin_2,hw_gpio_mode_input_pullup,hw_gpio_func_gpio,true);

hw_gpio_set_pin_function(hw_gpio_port_3,hw_gpio_pin_5,hw_gpio_mode_output,hw_gpio_func_i2c_scl);
hw_gpio_set_pin_function(HW_GPIO_PORT_1, HW_GPIO_PIN_2, HW_GPIO_MODE_INPUT_PULLUP, HW_GPIO_FUNC_I2C_SDA);
//------------------------------
这样对吗?
我对2 init函数有点混淆:hw_gpio_configure_pin,hw_gpio_set_pin_function。
对于PIN,我们可以将其设置为输入/输出(带/无拉,或PUSH_PULL或SPEED),然后我们设置备用功能。
为什么我应该在2个函数中将模式(输入/输出)设置两次?

为什么RX引脚设置为输出:
hw_gpio_configure_pin(HW_GPIO_PORT_2, HW_GPIO_PIN_3, HW_GPIO_MODE_OUTPUT, HW_GPIO_FUNC_GPIO, 1);
hw_gpio_set_pin_function(HW_GPIO_PORT_2, HW_GPIO_PIN_3, HW_GPIO_MODE_OUTPUT, HW_GPIO_FUNC_UART2_RX); ?

in platform_devices.h, there are many line to init the I2C devices ,like:
I2C_SLAVE_DEVICE(I2C1, BH1750, 0x23, HW_I2C_ADDRESSING_7B, HW_I2C_SPEED_FAST);

但我找不到BH1750的定义,MEM_24LC256,FM75 ,,,,,,They在AD_I2C_OPEN()中也使用。

Thanks

mt_dialog.
离线
最后一次露面:2个月1周前
职员
加入:2015-06-08 11:34
Hi jamesleo-konka,

Hi jamesleo-konka,

HW_GPIO_CONFIGURE_PIN()包括HW_GPIO_SET_PIN_FUNCT()您可以查看代码或DOXYGEN以获取每个功能的描述。hw_gpio_set_pin_function()足以配置您的引脚,HW_GPIO_CONFIGURE_PIN()执行相同的操作,但如果它将处于活动状态,则此外设置默认状态。关于将UART RX设置为输出和使用两个GPIO函数的原因,以便设置UART的RX,我想在HRP_Sensor项目的Periph_Init()函数中看到它,所以右上方引脚的设置有一个解释,简而言之,这是jlink仿真串口的解决方法。

There is no definition they are just names used by the macro I2C_SLAVE_DEVICE and after doing that you will be able to use them in order to open the adapter.

谢谢mt_dialog.

jamesleo-konka
离线
最后一次露面:4年2周前
加入:2017-01-22 02:42
Hi MT_Dialog,

Hi MT_Dialog,
以下是使用I2C适配器的正确顺序:
1.首先声明I2C设备:
I2C_BUS(I2C1)
i2c_slave_device(i2c1,bme280,0x76,hw_i2c_addressing_7b,hw_i2c_speed_standard);//
I2C_BUS_END.
2.操作I2C设备
i2c_device dev;
静态char wbuf [5] =“test”;
char rbuf [5];
dev = ad_i2c_open(bme280);/ *打开所选设备* /
ad_i2c_bus_acquire(dev);/ *获取访问总线* /
ad_i2c_write(dev,wbuf,sizeof(wbuf));/ *将某些数据同步写入I2C设备* /// - 这将生成启动,发送从地址(W),发送REG地址,写入数据
ad_i2c_read(dev, rbuf, sizeof(rbuf), 100); /* Read synchronously the data from I2C device */ // I2C change direction,generate RESTART, send SLAVE address(R),send reg address,read data , generate STOP
ad_i2c_bus_release(dev); /*Release the I2C
ad_i2c_close (dev);/ * * /关闭选定的设备

Another Question:
1. when to use ad_i2c_init()?
2. should ad_i2c_xx be called after starting the task ? Can I do the I2C device initialization after power on reset (no task running)?
4.要将一个数据写入I2C设备的寄存器,我可以参考以下内容:
ad_i2c_write(dev,wbuf,2);// 2字节,first = reg地址,第二个=数据

Thanks

jamesleo-konka
离线
最后一次露面:4年2周前
加入:2017-01-22 02:42
Hi MT_dialog

Hi MT_dialog
非常感谢。
有太多的宏,它不容易分析/跟踪源代码。

示例项目peripherals_demo使用户与hw_i2c_xx和ad_i2c_xx混淆,特别是在EEPROM_24XX256.c中。HW_I2C_XX和AD_I2C_XX都同时使用。

用户何时调用ad_i2c_init()?我没有找到调用AD_I2C_INIT()的代码。

詹姆士

mt_dialog.
离线
最后一次露面:2个月1周前
职员
加入:2015-06-08 11:34
Hi jamesleo-konka,

Hi jamesleo-konka,

您调用的命令的序列取决于您使用的SDK,例如在最新的SDK上:

1) Yes, your assignment of the device on the I2C bus is proper.

2) Getting the handle of the device when you are about to start a transaction is proper (ad_i2c_open(BME280)) is also proper, the ad_i2c_bus_aquire() is not a necessity since the ad_i2c_write() and ad_i2c_read() perform the aquire of the bus and the device and also the release of the bus and the device.

Also, just to get things straight since you would like to generate a restart condition, the restart condition is generated when you read from the bus, that means that you will have to provide (therefore write on the i2c bus) the address of the register that you would like to read, the above code i dont think that produces a restart condition between the ad_i2c_write() and the ad_i2c_read() (the RESTART condition is triggered between the write and the read on bus during the ad_i2c_read() operation). Between the ad_i2c_write and the ad_i2c_read there should be a STOP condition as far as i can tell. The restart condition is generated is where you mentioning it in your post.

关于你的其他问题:

1) The ad_i2c_init() is invoked during the system_init() function in the prvSetupHardware(), in the pm_system_init() function there is an additional function called init_adapters() and from that function all the enabled adapters are being initialized.

2) The system will let the adapters know for the wake up or power up and the adapters will initialize the hw as long as you have configured them, that happens when the system is powering up or waking up by default.

3) Yes i suppose that you can do that.

谢谢mt_dialog.

jamesleo-konka
离线
最后一次露面:4年2周前
加入:2017-01-22 02:42
嗨,mt_dialog,

嗨,mt_dialog,
感谢您的帮助,我的AD_I2C_XX已开始,我得到了I2C端口的输出。
It seems like that ad_i2c_write() and ad_i2c_read() can't generate RESTART signal because both of them has done the complete steps of an I2C operation: START, SLAVE address (W/R), data(W/R),,,,STOP.
如果用户使用HW_I2C_XX生成重启信号吗?请告诉我一个示例代码。

Thanks

mt_dialog.
离线
最后一次露面:2个月1周前
职员
加入:2015-06-08 11:34
Hi jamesleo-konka,

Hi jamesleo-konka,

As mentioned above, usually the RESTART condition is generated when you would like to read out a value from the I2C slave (the I2C master will write on the bus the address of the register that it would like to read and then issue a RESTART in order to start reading). The ad_i2c_write() and ad_i2c_read() will execute a seperate write and read. With the current I2C module that the 68x is equipped you wont be able to write and then force a restart condition in order to write the address for a read operation. The reason for that is that, as mentioned the I2C module will automatically generate the STOP condition when its FIFO is emtpy and the RESTART condition when you change operation.

所以让我们假设你在从站开始编写数据,你决定你想读取,所以你必须写的奴隶地址,你想在公共汽车上读取,所以你将提供您希望读到I2C FIFO的地址,以免生成停止(请记住您的FIFO是否为空I2C将发送停止条件)。如果您提供了您希望读取模块的I2C从设备的地址,则没有一种方式知道这是一个地址,并且您现在要读取的方式,并将在与数据相同的交易上发送地址,由于您没有从WRITE切换到读取以便生成重启。

So to sum up there is no way that i am aware of in order to force a RESTART condition on the I2C (perhaps you could make a dummy read in order to force the RESTART and then start the proper reading transaction, write the address and read again, but this will cause two RESTART conditions as far as i can tell and i am not sure how any slave will handle that) although i am not able to see what is wrong with the STOP condition between the actual reads and writes ?

谢谢mt_dialog.