I2C's IO port Initialize

10 posts / 0 new
Last post
孔卡
Offline
Last seen:3 years 9 months ago
Joined:2017-01-22 02:42
I2C's IO port Initialize

嗨,Dialog
我正在学习外设演示,并找到UART的GPIO端口/引脚初始化。
But thers is no code doing the I2C's GPIO port/pins.
我应该初始化I2C的GPIO端口/引脚吗?如何?或者我错过了什么?

谢谢

关键词:
设备:
MT_dialog
Offline
Last seen:6天17小时前
Staff
Joined:2015-06-08 11:34
嗨,孔卡先生,

嗨,孔卡先生,

Please check in the gpio_setup.h file in the peripherals_demo\config\default directory, there you will find the definitions for every gpio used and then the peirph_setup() function in the periph_setup.c you will find the initialization of the pins by the hw_gpio_configure() function.

谢谢MT_dialog

孔卡
Offline
Last seen:3 years 9 months ago
Joined:2017-01-22 02:42
嗨,MT\u Dialog,

嗨,MT\u Dialog,
Sorry I missed the GPIO config code, they are redefined as CFG_GPIO_I2C1_SCL_PORT, and use the macro HW_GPIO_PINCONFIG to initialize.

谢谢

孔卡
Offline
Last seen:3 years 9 months ago
Joined:2017-01-22 02:42
嗨,MT\u Dialog,

嗨,MT\u Dialog,
我在项目hrp\u sensor的periph\u设置中输入了以下代码:
//------------------------------
/ /——I2C端口/销配置——P3.5 = sci, 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);
硬件gpio\设置\引脚\功能(硬件gpio\端口\ 1、硬件gpio\引脚\ 2、硬件gpio\模式\输入\上拉、硬件gpio\功能\ I2C \ SDA);
//------------------------------
Is it right?
I'm a bit confused about the 2 init functions: hw_gpio_configure_pin, hw_gpio_set_pin_function.
For a pin, we can set it as input/output (with/without pull, or push_pull, or speed), and then we set the alternal functions.
Why should I set the mode (input/output) two times in the 2 functions?

Why the RX pin is set as output:
硬件gpio配置引脚(硬件gpio端口2,硬件gpio引脚3,硬件gpio模式输出,硬件gpio功能gpio,1);
hw_gpio_set_pin_function(hw_gpio_PORT_2,hw_gpio_pin_3,hw_gpio_MODE_OUTPUT,hw_gpio_FUNC_UART2_RX)?

在platform_devices.h中,有许多行用于初始化I2C设备,如:
I2C\从设备(I2C1,BH1750,0x23,HW\ U I2C\ U寻址\U 7B,HW\ U I2C\ U速度\U快);

but I can't find the definning of BH1750, MEM_24LC256,FM75,,,, --they are used in ad_i2c_open() also.

谢谢

MT_dialog
Offline
Last seen:6天17小时前
Staff
Joined:2015-06-08 11:34
嗨,孔卡先生,

嗨,孔卡先生,

The hw_gpio_configure_pin() includes the hw_gpio_set_pin_function() you can have a look at the code or at the doxygen for the description of each function. The hw_gpio_set_pin_function() is enough to configure your pins, the hw_gpio_configure_pin() does the same thing but it additionally sets a default state of the pin if its going to be active of inactive. Regarding the reason for the setting of the UART RX as output and using both of the gpio function in order to set the RX of the UART i suppose that you saw this in the periph_init() function of the hrp_sensor project, so, right above the settings of the pin there is an explanation for this, in short, this is workaround for the Jlink emulated serial port.

没有定义,它们只是宏I2C\u SLAVE\u设备使用的名称,这样做之后,您就可以使用它们来打开适配器。

谢谢MT_dialog

孔卡
Offline
Last seen:3 years 9 months ago
Joined:2017-01-22 02:42
嗨,MT\u Dialog,

嗨,MT\u Dialog,
Is the following the right sequence for use of I2C adapter:
1. first declaring the I2C devices:
I2C_BUS(I2C1)
I2C_SLAVE_DEVICE(I2C1, BME280, 0x76, HW_I2C_ADDRESSING_7B, HW_I2C_SPEED_STANDARD); //
I2C_BUS_END
2. operate I2C devices
i2c_device dev;
static char wbuf[5] = „Test”;
char rbuf[5];
dev = ad_i2c_open(BME280); /* Open selected device */
ad_i2c_bus_acquire(dev); /* Acquire access to bus */
ad_i2c_write(dev, wbuf, sizeof(wbuf)); /* Write synchronously some data to I2C device *///--this will generate START,send SLAVE address(W),send reg address,write data
ad_i2c_read(dev,rbuf,sizeof(rbuf),100);/*从i2c设备同步读取数据*///i2c改变方向,生成重启,发送从属地址(R),发送注册地址,读取数据,生成停止
ad_i2c_bus_release(dev);/*释放i2c
ad_i2c_close(dev); /* Close selected device */

另一个问题:
1什么时候使用ad\u i2c\u init()?
2启动任务后是否应调用ad\u i2c\u xx?上电复位(无任务运行)后是否可以进行I2C设备初始化?
4. to write one data into I2C device's register, may I refer to the following:
ad_i2c_write(dev, wbuf, 2); // 2 byte,first= reg address, second= data

谢谢

孔卡
Offline
Last seen:3 years 9 months ago
Joined:2017-01-22 02:42
Hi MT\u对话框

Hi MT\u对话框
谢谢a lot.
There are too much Macros and it's not easy to analyse/trace the source code.

The sample project peripherals_demo make user confused with hw_i2c_xx and ad_i2c_xx, especialy in eeprom_24xx256.c . Both hw_i2c_xx and ad_i2c_xx are used at the same time.

When will user call the ad_i2c_init() ? I don't find the code which calling ad_i2c_init().

James

MT_dialog
Offline
Last seen:6天17小时前
Staff
Joined:2015-06-08 11:34
嗨,孔卡先生,

嗨,孔卡先生,

That sequence of the commands that you invoke depends on the SDK that you are using, for example on the latest SDK:

1) 是的,您在I2C总线上分配的设备是正确的。

2) 当您要开始一个事务时,获取设备的句柄是正确的(ad\u i2c\u open(BME280))也是正确的,ad\u i2c\u bus\u aquire()不是必需的,因为ad\u i2c\u write()和ad\u i2c\u read()执行总线和设备的获取以及总线和设备的释放。

另外,由于您想生成一个重新启动条件,所以为了把事情弄清楚,重新启动条件是在您从总线读取时生成的,这意味着您必须提供(因此在i2c总线上写入)您想读取的寄存器的地址,我认为上面的代码不会在adïi2cïwrite()和adïi2cïread()之间产生重新启动条件(在adïi2cïread()操作期间,在总线上的写入和读取之间触发重新启动条件)。据我所知,在adèi2cè写入和adèi2cè读取之间应该有一个停止条件。重新启动条件是在您在帖子中提到的地方生成的。

Regarding your other questions:

1) 在prvSetupHardware()中的system\u init()函数期间调用ad\u i2c\u init(),在pm\u system\u init()函数中还有一个名为init\u adapters()的附加函数,从该函数中初始化所有启用的适配器。

2) 系统将让适配器知道唤醒或通电,并且只要您配置了适配器,适配器就会初始化硬件,这在系统默认情况下通电或唤醒时发生。

3) 是的,我想你能做到。

谢谢MT_dialog

孔卡
Offline
Last seen:3 years 9 months ago
Joined:2017-01-22 02:42
Hi, MT_Dialog,

Hi, MT_Dialog,
谢谢for your help, my ad_i2c_xx has begun and I got the output of I2C port.
似乎adèi2cèwrite()和adèi2cèread()无法生成重新启动信号,因为它们都完成了i2c操作的完整步骤:启动、从属地址(W/R)、数据(W/R)、、、、、停止。
Should the user use hw_i2c_xx to generate RESTART signal? pls show me a sample code.

谢谢

MT_dialog
Offline
Last seen:6天17小时前
Staff
Joined:2015-06-08 11:34
嗨,孔卡先生,

嗨,孔卡先生,

如上所述,当您想从I2C从机读取一个值时,通常会生成重新启动条件(I2C主机会在总线上写入它想读取的寄存器地址,然后发出重新启动以开始读取)。ad\ U i2c\ U write()和ad\ U i2c\ U read()将分别执行写入和读取操作。使用68x配备的当前I2C模块,您将无法写入然后强制重新启动以写入读操作的地址。原因是,如前所述,I2C模块将在其FIFO为emtpy时自动生成停止条件,并在您更改操作时自动生成重新启动条件。

So lets assume that you start writing data at your slave and you decide that you would like to read, so you will have to write the register address of the slave, that you would like to read, on the bus, so you will provide the address that you would like to read to the I2C FIFO in order not to generate a STOP (remember if your FIFO is empty the I2C will send a STOP condition). If you provide the address of the I2C slave that you would like to read the module doesn't have a way of knowing that this is an address and that you would like to read now, and will send the address on the same transaction as data, since you didn't switch from write to read in order to generate a restart.

总之,我不知道有什么方法可以强制I2C重新启动(也许你可以做一个虚拟读取来强制重新启动,然后启动正确的读取事务,写入地址并再次读取,但据我所知,这将导致两种重新启动条件,我不确定任何从机将如何处理这一点),尽管我无法看到实际读写之间的停止条件有什么问题?

谢谢MT_dialog