2017-06-28 01:16 PM
I am using CubeMx (HAL driver)for initialization code generation and using HAL libraries/functions after that. The MCU is
I am having problem with themailto:I@C
peripheral. It is a custom board and for the time being, no external device is attached to the I2C pins. The pins are just pulled up using pull-up resistors. I am trying to use it in master transmit/receive mode. After MX_I2C1_Init() is executed : -Observations: Using an oscilloscope, i can see that there is no clock signal on SCL pin. The voltage at the SCL pin is always LOW. The voltage on the SDA pin is always HIGH. And I2C1 -> ISR-> BUSY flag is SET.
Below I have copied portion of the code from main() function:
int main(void)
{HAL_Init();
/* Configure the system clock */
SystemClock_Config();/* Initialize all configured peripherals */
MX_GPIO_Init(); MX_CAN_Init(); MX_SPI1_Init(); MX_USART1_UART_Init(); MX_I2C1_Init();accl_buff[0] = 'a'; // transmit buffer for I2C
while (1)
{ HAL_I2C_Master_Transmit(&hi2c1, 0x18, accl_buff, 1, 10 ); // 0x18 is slave address accl_buff[0] = accl_buff[0]+1; // update I2C transmit buffer for(i=0;i<10000;i++); // delay function}
}
All I have done is add HAL_I2C_Master_Transmit() to the while loop in main(). Otherwise, the code is as generated by CubeMx. But the I2C peripheral DOES NOT generate any clock signal on SCL pins(always held LOW) & neither give out any data on the SDA pins(held HIGH).
Where am I mistaken ?
I think my problem is similar to
https://community.st.com/message/152756-i2c-generated-cubemx
, though a different MCU. I did not find any I2C example for this MCU to test. I have also tried generating code with LowLevel Driver from CubeMx, and the BUSY flag is in reset condition after initialization.Thanks in advance.
#stm32f0-hal-driver-i2c #stm32f042 #stm32f0-i2c #cubemx-i2c2017-06-29 02:50 AM
Hello
Jana.Saikat
,can you please share with us configuration file (MSP file).
Since board is custom, can you verify you are checking correct pins (the same used in CubeMX for init) and that device is properly soldered?
Best regards,
Tilen
2017-06-29 03:34 AM
>HAL_I2C_Master_Transmit(&hi2c1, 0x18, accl_buff, 1, 10 );
HAL_I2C_Master_Transmit(&hi2c1, (uint16_t)(0x18<<1), accl_buff, 1,10);
2017-06-29 07:27 AM
Hi Jane
Here is the document that tells you the steps to follow to use the I2C on stm32f0 chips. The part that you should go take a look at are pages 216 to 221.The document seems well made just make sure that you read the section corresponding the type of communication you are looking for. ex: by polling,dma,etc.You mentionned that the ISR flag is always busy.To start I would try to get it working by polling that means that you can disable all I2C interrupts.When thats working move on to interrupt.Also if you have many peripherals working with interrupts remember that you may run out of stack.You can increase the stack either in project->settings if you are using cubeMX or in your config files in your project.(not sure in what file exacly)Good luck-Andy2017-06-29 07:54 AM
2017-06-29 09:31 AM
Ps:I would highly recommend that you use a logic analyser if you have acces to one.Or you can buy a chinese replica thats works fine (have one and it works great) for about 10$ and it works with Saleae's software which is free.
2018-03-05 12:07 PM
Hi!
If you look at the CubeMX generated code, you will not see the initialization of the I2C port pins. Possible it is a CubeMX bug.
Adding the missing code should help you.GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; .......HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
If you soldered pullup resistors use
GPIO_MODE_AF_OD value in mode field.
Good luck!
2018-07-15 09:04 AM
The OP said the problem was that there is no I2C clock signal. Perhaps the problem is that the clock source has not been configured.
Per stm32l4xx_hal_rcc.c :
After reset the device is running from Multiple Speed Internal oscillator
(4 MHz) with Flash 0 wait state. Flash prefetch buffer, D-Cache
and I-Cache are disabled, and all peripherals are off except internal
SRAM, Flash and JTAG.
(+) There is no prescaler on High speed (AHBs) and Low speed (APBs) busses:
all peripherals mapped on these busses are running at MSI speed.
(
+) The clock for all peripherals is switched off,
except the SRAM and FLASH.(+) All GPIOs are in analog mode, except the JTAG pins which
are assigned to be used for debug purpose.
[..]
Once the device started from reset, the user application has to:
(+) Configure the clock source to be used to drive the System clock
(if the application needs higher frequency/performance)
(+) Configure the System clock frequency and Flash settings
(+) Configure the AHB and APB busses prescalers
(+) Enable the clock for the peripheral(s) to be used
(+) Configure the clock source(s) for peripherals which clocks are not
derived from the System clock (SAIx, RTC, ADC, USB OTG FS/SDMMC1/RNG)