cancel
Showing results for 
Search instead for 
Did you mean: 

How to use HAL I2C Library

astree
Associate II
Posted on July 29, 2015 at 11:29

Hello, 

I need to make my STM32F072 communicate with a HIH sensor through I2C.

I want to use HAL_I2C_Master_Transmit and HAL_I2C_Master_Receive.

For exemple if I just need to generate a write condition bit may I use it even if txbuffer is a uint8 type? 

#sensor #i2c #i2c #hal
7 REPLIES 7
Posted on July 29, 2015 at 14:56

Hi pwdusid,

You can create a new project with STM32CubeMx 4.9.0 and try to be inspired by the set example under

STM32Cube_FW_F0_V1.3.0\Projects\STM32F072B-Discovery\Examples\I2C.

 

-Shahrzad-
astree
Associate II
Posted on July 29, 2015 at 15:33

I have already tried und I know how to manege HAL I2C library to send byte, word but I am wondering if I can just send a sequence like : 

Start-A6-A5-A4-A3-A2-A1-A0-WriteBite(==0). with A5...A0 slave address.

Posted on July 30, 2015 at 17:17

Hi pwdusid,

I'm sorry to tell you that your problem is not clear for me. TxBuffer is already defined with uint8_t type and the sequence you have mentioned does not contain the data to transmit.

Please explain more the issue, so I can help.

 

-Shahrzad-

Posted on July 30, 2015 at 19:03

The OP is defining an addressing bit pattern sent over the wire, not the data that transits afterward.

The Transmit and Receive should dictate the low order bit.

It might help to specify exactly what part you're trying to talk too. Cite a data sheet.

And show what code you have now, the're not that may ways to talk to I2C devices, so one of more example I2C driver should be sufficient to mash together a working example.

ST needs to focus on providing clear, concise, and robust examples of as many use cases as possible.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
astree
Associate II
Posted on July 31, 2015 at 14:23

Ok I haven't been clear sorry.

I have attached the datasheet of sensor I want to communicate with.

I am trying to send the ''I2C Measurement Request'' where no data are required to be sent and I want to useHAL_I2C_Master_Transmit function but it is not possible to send empty buffer.

________________

Attachments :

I2C_Comms_HumidIcon_.pdf : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HzO0&d=%2Fa%2F0X0000000bMk%2Fc7DD4pJZymGJI9Fe4s9VFXTSbKs8DN6pCv1aHY3WUWc&asPdf=false
astree
Associate II
Posted on July 31, 2015 at 14:42

And for now i have something very simple:

DB.h
#define TXBUFFERSIZE (COUNTOF(aTxBuffer) - 1)
DB.c
#define I2C_ADDRESS 0x4E // 6 LSBs of Slave address ('x27' as mentionne on attached datasheet) + write bit
#define I2C_TIMING 0x00B21847 //400k
uint8_t aTxBuffer[]=''BONJOUR''; // I don't need to send this data 
void DB_I2C_Init(void){
I2cHandle.Instance = I2Cx;
I2cHandle.Init.Timing = I2C_TIMING;
I2cHandle.Init.OwnAddress1 = 0;
I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
I2cHandle.Init.OwnAddress2 = 0;
I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; 
if(HAL_I2C_Init(&I2cHandle) != HAL_OK)
{
/* Initialization Error */
Error_Handler(); 
} 
}
void DB_I2C_Measurement_Request_Honey(void){
HAL_I2C_Master_Transmit(&I2cHandle, (uint16_t)I2C_ADDRESS, (uint8_t *)aTxBuffer, TXBUFFERSIZE, 1000);
}

Posted on August 17, 2015 at 16:59

Hi pwdusid,

Regarding the needs to manage I2C device with data size null, please try the fix in attachment (

line 501 and line 984)

Goal of this fix is to remove size null condition only for these 2 interfaces :

- HAL_I2C_Master_Transmit()

- HAL_I2C_Master_Transmit_IT()

For other interface, no specific needs to remove null condition.

-Shahrzad-

________________

Attachments :

stm32f0xx_hal_i2c.c : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HzUm&d=%2Fa%2F0X0000000bMl%2F1FmRSildVTEn0N5nydq.CVB1wLV1munSCPKUbwXCYIQ&asPdf=false