cancel
Showing results for 
Search instead for 
Did you mean: 

RCC_APB2PeriphClockCmd, GPIO_InitStructure.GPIO_Pin, GPIO_InitStructure.GPIO_Mode etc. what is it used for?

ZKURT.1
Senior

Hi. I'm trying to translate the SHT30 library for stm32l0. But I could not understand and translate the "IIC_Init" part of the code. I didn't understand the init part, I couldn't translate it and it only gives a line there What do you think are the operations it does in this function? I think it doesn't work with i2c because there are parts called SDA_IN() and SDA_OUT(). It sets it as GPIO input and GPIO output. Can you explain what it does in the init part? source code-> https://github.com/thejbte/Example_OS_QuartKTS__STM32

void IIC_Init(void)

{    

GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );

  

GPIO_InitStructure.GPIO_Pin = IIC_SDA_Pin|IIC_SCL_Pin;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ;  //�������

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOC, &GPIO_InitStructure);

GPIO_SetBits(GPIOB,IIC_SDA_Pin|IIC_SCL_Pin); //PB6,PB7�����

}

void SDA_IN()  

GPIO_InitTypeDef GPIO_InitStruct = {0};

 /* GPIO Ports Clock Enable */

 __HAL_RCC_GPIOC_CLK_ENABLE();

 __HAL_RCC_GPIOH_CLK_ENABLE();

 __HAL_RCC_GPIOB_CLK_ENABLE();

 __HAL_RCC_GPIOA_CLK_ENABLE();

 /*Configure GPIO pin : IIC_SDA_Pin */

 GPIO_InitStruct.Pin = IIC_SDA_Pin;

 GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

 GPIO_InitStruct.Pull = GPIO_NOPULL;

 HAL_GPIO_Init(IIC_SDA_GPIO_Port, &GPIO_InitStruct);

}

void SDA_OUT()

GPIO_InitTypeDef GPIO_InitStruct = {0};

 /* GPIO Ports Clock Enable */

 __HAL_RCC_GPIOC_CLK_ENABLE();

 __HAL_RCC_GPIOH_CLK_ENABLE();

 __HAL_RCC_GPIOB_CLK_ENABLE();

 __HAL_RCC_GPIOA_CLK_ENABLE();

 /*Configure GPIO pin Output Level */

 HAL_GPIO_WritePin(IIC_SDA_GPIO_Port, IIC_SDA_Pin, GPIO_PIN_RESET);

 /*Configure GPIO pin : IIC_SDA_Pin */

 GPIO_InitStruct.Pin = IIC_SDA_Pin;

 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

 GPIO_InitStruct.Pull = GPIO_NOPULL;

 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

 HAL_GPIO_Init(IIC_SDA_GPIO_Port, &GPIO_InitStruct);

}

5 REPLIES 5
Tom Schrauf
Associate II

Well imo you don't need the IIC_Init function above and it would not make sense to configure both pins as outputs and setting them anyways ( IIC_SDA_Pin | IIC_SCL_Pin are ored so both pins will be configured at the same time).

Your SDA_IN and SDA_OUT functions configure the pins seperately so maybe:

void IIC_Init( void )

{

SDA_IN();

SDA_OUT();

}

will do the trick ?!

BR, Tom

TDK
Guru

> Can you explain what it does in the init part?

It enables the GPIOB clock, then it configures the pins as output push-pull, then it sets those pins high.

CubeMX will generate this part for you if you don't know the syntax.

If you feel a post has answered your question, please click "Accept as Solution".

Hi. Does commenting these lines prevent a sensor working with I2C from working? I commented out that function and made other edits and the code didn't work. Normally, the SHT30 sensor is an I2C sensor. but when I examine the codes, I see that it runs with I2c as output and input. i couldn't run it.

Hi. Does commenting these lines prevent a sensor working with I2C from working? I commented out that function and made other edits and the code didn't work. Normally, the SHT30 sensor is an I2C sensor. but when I examine the codes, I see that it runs with I2c as output and input. i couldn't run it.

0693W00000HppWNQAZ.png

TDK
Guru

> Does commenting these lines prevent a sensor working with I2C from working?

Commenting what lines?

Generally, preventing I2C from being initialized will prevent I2C from working. That should be straightforward to understand.

If you feel a post has answered your question, please click "Accept as Solution".