i2c hangs when reading just one byte

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

User avatar
alex31
Posts: 445
Joined: Fri May 25, 2012 10:23 am
Has thanked: 57 times
Been thanked: 88 times

i2c hangs when reading just one byte

Post by alex31 »

Hello.

I use chibios 2.5.1 up2date from git, on a specific build dev board with stm32f4.
The problems occurs with i2c1 ou i2c2, not tested other one.

when using i2cMasterTransmitTimeout to send a register address to be read, it works without any problem
if rxlen is > 1.

if rxlen equals 1, it hangs with stange behavior :

Code: Select all

 static const I2CConfig i2cfg1 = {
    OPMODE_I2C,
    400000,
    STD_DUTY_CYCLE,
};


i2cStart(&I2CD1, &i2cfg1);
static uint8_t rxbuf[16];
static uint8_t txbuf[16];
txbuf[0] = 0;
status = i2cMasterTransmitTimeout(&I2CD1, 0x1d, txbuf, 1, rxbuf, 1, 1000);




at first i2cMasterTransmitTimeout, it does not hangs, but when can see on the chronogram that i2c master ask for 2 bytes on the bus
instead one :
Image


then, if i resend the same i2cMasterTransmitTimeout for one byte, it hangs :
Image

^C0x08000d06 in _port_irq_epilogue () at ../../ChibiOS/os/ports/GCC/ARMCMx/chcore_v7m.c:144
144 if ((SCB_ICSR & ICSR_RETTOBASE) != 0) {
(gdb) where
#0 0x08000d06 in _port_irq_epilogue () at ../../ChibiOS/os/ports/GCC/ARMCMx/chcore_v7m.c:144
#1 0x080065d2 in VectorBC () at ../../ChibiOS/os/hal/platforms/STM32/i2c_lld.c:473
#2 0xfffffffc in ?? ()

I have tried to see in source code an obvious error, but does not find anything, execpt
a thing which is not relative to my bug but that i don't understand :

in file llc_i2c.c :

Code: Select all

  chDbgCheck(((rxbytes == 0) || ((rxbytes > 1) && (rxbuf != NULL))),
             "i2c_lld_master_transmit_timeout");


if rxbytes is equal to 1, the test that rxbuf if not null is not done, does'nt the function need a valid buffer even for just one byte ?

Thanks for any help
Alexandre
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: i2c hangs when reading just one byte

Post by Giovanni »

Hi,

Thanks, I will ask Barthess to give a look at this.

Giovanni
User avatar
barthess
Posts: 861
Joined: Wed Dec 08, 2010 7:55 pm
Been thanked: 7 times

Re: i2c hangs when reading just one byte

Post by barthess »

Alexandre,
alex31 wrote:the test that rxbuf if not null is not done

because it done in high level driver.
Can you provide more info? What I2C slave cause such problem? Are I2C interrupts priorities higher than other peripherals? (post your mcuconf.h somewhere)

Giovanni,
This single byte reading workaround for F4x looks too uglier than double byte reading for whole stm32. I thinking about returning to it if Alexandre problem will not be solved.
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: i2c hangs when reading just one byte

Post by Giovanni »

It is OK Barthess, let's see how it evolves.

Giovanni
User avatar
alex31
Posts: 445
Joined: Fri May 25, 2012 10:23 am
Has thanked: 57 times
Been thanked: 88 times

Re: i2c hangs when reading just one byte

Post by alex31 »

The i2c slave is an accelerometer analog device ADXL345
http://www.analog.com/en/mems-sensors/mems-inertial-sensors/adxl345/products/product.html

my mcuconf.h :

Code: Select all

/*                                                                                                                                     
    ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,                                                                               
                 2011,2012 Giovanni Di Sirio.                                                                                         
                                                                                                                                       
    This file is part of ChibiOS/RT.                                                                                                   
                                                                                                                                       
    ChibiOS/RT is free software; you can redistribute it and/or modify                                                                 
    it under the terms of the GNU General Public License as published by                                                               
    the Free Software Foundation; either version 3 of the License, or                                                                 
    (at your option) any later version.                                                                                               
                                                                                                                                       
    ChibiOS/RT is distributed in the hope that it will be useful,                                                                     
    but WITHOUT ANY WARRANTY; without even the implied warranty of                                                                     
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                                                     
    GNU General Public License for more details.                                                                                       
                                                                                                                                       
    You should have received a copy of the GNU General Public License                                                                 
    along with this program.  If not, see <http://www.gnu.org/licenses/>.                                                             
*/                                                                                                                                     
                                                                                                                                       
/*                                                                                                                                     
 * STM32F4xx drivers configuration.                                                                                                   
 * The following settings override the default settings present in                                                                     
 * the various device driver implementation headers.                                                                                   
 * Note that the settings for each driver only have effect if the whole                                                               
 * driver is enabled in halconf.h.                                                                                                     
 *                                                                                                                                     
 * IRQ priorities:                                                                                                                     
 * 15...0       Lowest...Highest.                                                                                                     
 *                                                                                                                                     
 * DMA priorities:                                                                                                                     
 * 0...3        Lowest...Highest.                                                                                                     
 */                                                                                                                                   
                                                                                                                                       
#define STM32F4xx_MCUCONF                                                                                                             
                                                                                                                                       
/*                                                                                                                                     
 * HAL driver system settings.                                                                                                         
 */                                                                                                                                   
#define STM32_NO_INIT                       FALSE                                                                                     
#define STM32_HSI_ENABLED                   TRUE                                                                                       
#define STM32_LSI_ENABLED                   TRUE                                                                                       
#define STM32_HSE_ENABLED                   TRUE                                                                                       
#define STM32_LSE_ENABLED                   FALSE                                                                                     
#define STM32_CLOCK48_REQUIRED              TRUE                                                                                       
#define STM32_SW                            STM32_SW_PLL                                                                               
#define STM32_PLLSRC                        STM32_PLLSRC_HSE                                                                           
#define STM32_PLLM_VALUE                    16
#define STM32_PLLN_VALUE                    336
#define STM32_PLLP_VALUE                    2
#define STM32_PLLQ_VALUE                    7
#define STM32_HPRE                          STM32_HPRE_DIV1
#define STM32_PPRE1                         STM32_PPRE1_DIV4
#define STM32_PPRE2                         STM32_PPRE2_DIV2
#define STM32_RTCSEL                        STM32_RTCSEL_LSI
#define STM32_RTCPRE_VALUE                  8
#define STM32_MCO1SEL                       STM32_MCO1SEL_HSI
#define STM32_MCO1PRE                       STM32_MCO1PRE_DIV1
#define STM32_MCO2SEL                       STM32_MCO2SEL_SYSCLK
#define STM32_MCO2PRE                       STM32_MCO2PRE_DIV5
#define STM32_I2SSRC                        STM32_I2SSRC_CKIN
#define STM32_PLLI2SN_VALUE                 192
#define STM32_PLLI2SR_VALUE                 5
#define STM32_VOS                           STM32_VOS_HIGH
#define STM32_PVD_ENABLE                    FALSE
#define STM32_PLS                           STM32_PLS_LEV0

/*
 * ADC driver system settings.
 */
#define STM32_ADC_ADCPRE                    ADC_CCR_ADCPRE_DIV4
#define STM32_ADC_USE_ADC1                  FALSE
#define STM32_ADC_USE_ADC2                  FALSE
#define STM32_ADC_USE_ADC3                  FALSE
#define STM32_ADC_ADC1_DMA_STREAM           STM32_DMA_STREAM_ID(2, 4)
#define STM32_ADC_ADC2_DMA_STREAM           STM32_DMA_STREAM_ID(2, 2)
#define STM32_ADC_ADC3_DMA_STREAM           STM32_DMA_STREAM_ID(2, 1)
#define STM32_ADC_ADC1_DMA_PRIORITY         2
#define STM32_ADC_ADC2_DMA_PRIORITY         2
#define STM32_ADC_ADC3_DMA_PRIORITY         2
#define STM32_ADC_IRQ_PRIORITY              6
#define STM32_ADC_ADC1_DMA_IRQ_PRIORITY     6
#define STM32_ADC_ADC2_DMA_IRQ_PRIORITY     6
#define STM32_ADC_ADC3_DMA_IRQ_PRIORITY     6

/*
 * CAN driver system settings.
 */
#define STM32_CAN_USE_CAN1                  FALSE
#define STM32_CAN_USE_CAN2                  FALSE
#define STM32_CAN_CAN1_IRQ_PRIORITY         11
#define STM32_CAN_CAN2_IRQ_PRIORITY         11

/*
 * EXT driver system settings.
 */
#define STM32_EXT_EXTI0_IRQ_PRIORITY        6
#define STM32_EXT_EXTI1_IRQ_PRIORITY        6
#define STM32_EXT_EXTI2_IRQ_PRIORITY        6
#define STM32_EXT_EXTI3_IRQ_PRIORITY        6
#define STM32_EXT_EXTI4_IRQ_PRIORITY        6
#define STM32_EXT_EXTI5_9_IRQ_PRIORITY      6
#define STM32_EXT_EXTI10_15_IRQ_PRIORITY    6
#define STM32_EXT_EXTI16_IRQ_PRIORITY       6
#define STM32_EXT_EXTI17_IRQ_PRIORITY       15
#define STM32_EXT_EXTI18_IRQ_PRIORITY       6
#define STM32_EXT_EXTI19_IRQ_PRIORITY       6
#define STM32_EXT_EXTI20_IRQ_PRIORITY       6
#define STM32_EXT_EXTI21_IRQ_PRIORITY       15
#define STM32_EXT_EXTI22_IRQ_PRIORITY       15

/*
 * GPT driver system settings.
 */
#define STM32_GPT_USE_TIM1                  FALSE
#define STM32_GPT_USE_TIM2                  FALSE
#define STM32_GPT_USE_TIM3                  FALSE
#define STM32_GPT_USE_TIM4                  FALSE
#define STM32_GPT_USE_TIM5                  FALSE
#define STM32_GPT_USE_TIM8                  FALSE
#define STM32_GPT_TIM1_IRQ_PRIORITY         7
#define STM32_GPT_TIM2_IRQ_PRIORITY         7
#define STM32_GPT_TIM3_IRQ_PRIORITY         7
#define STM32_GPT_TIM4_IRQ_PRIORITY         7
#define STM32_GPT_TIM5_IRQ_PRIORITY         7
#define STM32_GPT_TIM8_IRQ_PRIORITY         7

/*
 * I2C driver system settings.
 */
#define STM32_I2C_USE_I2C1                  FALSE
#define STM32_I2C_USE_I2C2                  TRUE
#define STM32_I2C_USE_I2C3                  FALSE
#define STM32_I2C_I2C1_RX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 0)
#define STM32_I2C_I2C1_TX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 6)
#define STM32_I2C_I2C2_RX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 2)
#define STM32_I2C_I2C2_TX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 7)
#define STM32_I2C_I2C3_RX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 2)
#define STM32_I2C_I2C3_TX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 4)
#define STM32_I2C_I2C1_IRQ_PRIORITY         5
#define STM32_I2C_I2C2_IRQ_PRIORITY         5
#define STM32_I2C_I2C3_IRQ_PRIORITY         5
#define STM32_I2C_I2C1_DMA_PRIORITY         3
#define STM32_I2C_I2C2_DMA_PRIORITY         3
#define STM32_I2C_I2C3_DMA_PRIORITY         3
#define STM32_I2C_I2C1_DMA_ERROR_HOOK()     chSysHalt()
#define STM32_I2C_I2C2_DMA_ERROR_HOOK()     chSysHalt()
#define STM32_I2C_I2C3_DMA_ERROR_HOOK()     chSysHalt()

/*
 * ICU driver system settings.
 */
#define STM32_ICU_USE_TIM1                  FALSE
#define STM32_ICU_USE_TIM2                  FALSE
#define STM32_ICU_USE_TIM3                  FALSE
#define STM32_ICU_USE_TIM4                  FALSE
#define STM32_ICU_USE_TIM5                  FALSE
#define STM32_ICU_USE_TIM8                  FALSE
#define STM32_ICU_TIM1_IRQ_PRIORITY         7
#define STM32_ICU_TIM2_IRQ_PRIORITY         7
#define STM32_ICU_TIM3_IRQ_PRIORITY         7
#define STM32_ICU_TIM4_IRQ_PRIORITY         7
#define STM32_ICU_TIM5_IRQ_PRIORITY         7
#define STM32_ICU_TIM8_IRQ_PRIORITY         7

/*
 * PWM driver system settings.
 */
#define STM32_PWM_USE_ADVANCED              FALSE
#define STM32_PWM_USE_TIM1                  FALSE
#define STM32_PWM_USE_TIM2                  FALSE
#define STM32_PWM_USE_TIM3                  FALSE
#define STM32_PWM_USE_TIM4                  FALSE
#define STM32_PWM_USE_TIM5                  FALSE
#define STM32_PWM_USE_TIM8                  FALSE
#define STM32_PWM_TIM1_IRQ_PRIORITY         7
#define STM32_PWM_TIM2_IRQ_PRIORITY         7
#define STM32_PWM_TIM3_IRQ_PRIORITY         7
#define STM32_PWM_TIM4_IRQ_PRIORITY         7
#define STM32_PWM_TIM5_IRQ_PRIORITY         7
#define STM32_PWM_TIM8_IRQ_PRIORITY         7

/*
 * SERIAL driver system settings.
 */
#define STM32_SERIAL_USE_USART1             TRUE
#define STM32_SERIAL_USE_USART2             TRUE
#define STM32_SERIAL_USE_USART3             FALSE
#define STM32_SERIAL_USE_UART4              FALSE
#define STM32_SERIAL_USE_UART5              FALSE
#define STM32_SERIAL_USE_USART6             FALSE
#define STM32_SERIAL_USART1_PRIORITY        12
#define STM32_SERIAL_USART2_PRIORITY        12
#define STM32_SERIAL_USART3_PRIORITY        12
#define STM32_SERIAL_UART4_PRIORITY         12
#define STM32_SERIAL_UART5_PRIORITY         12
#define STM32_SERIAL_USART6_PRIORITY        12

/*
 * SPI driver system settings.
 */
#define STM32_SPI_USE_SPI1                  FALSE
#define STM32_SPI_USE_SPI2                  FALSE
#define STM32_SPI_USE_SPI3                  FALSE
#define STM32_SPI_SPI1_RX_DMA_STREAM        STM32_DMA_STREAM_ID(2, 0)
#define STM32_SPI_SPI1_TX_DMA_STREAM        STM32_DMA_STREAM_ID(2, 3)
#define STM32_SPI_SPI2_RX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 3)
#define STM32_SPI_SPI2_TX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 4)
#define STM32_SPI_SPI3_RX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 0)
#define STM32_SPI_SPI3_TX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 7)
#define STM32_SPI_SPI1_DMA_PRIORITY         1
#define STM32_SPI_SPI2_DMA_PRIORITY         1
#define STM32_SPI_SPI3_DMA_PRIORITY         1
#define STM32_SPI_SPI1_IRQ_PRIORITY         10
#define STM32_SPI_SPI2_IRQ_PRIORITY         10
#define STM32_SPI_SPI3_IRQ_PRIORITY         10
#define STM32_SPI_DMA_ERROR_HOOK(spip)      chSysHalt()

/*
 * UART driver system settings.
 */
#define STM32_UART_USE_USART1               FALSE
#define STM32_UART_USE_USART2               FALSE
#define STM32_UART_USE_USART3               FALSE
#define STM32_UART_USE_USART6               FALSE
#define STM32_UART_USART1_RX_DMA_STREAM     STM32_DMA_STREAM_ID(2, 5)
#define STM32_UART_USART1_TX_DMA_STREAM     STM32_DMA_STREAM_ID(2, 7)
#define STM32_UART_USART2_RX_DMA_STREAM     STM32_DMA_STREAM_ID(1, 5)
#define STM32_UART_USART2_TX_DMA_STREAM     STM32_DMA_STREAM_ID(1, 6)
#define STM32_UART_USART3_RX_DMA_STREAM     STM32_DMA_STREAM_ID(1, 1)
#define STM32_UART_USART3_TX_DMA_STREAM     STM32_DMA_STREAM_ID(1, 3)
#define STM32_UART_USART6_RX_DMA_STREAM     STM32_DMA_STREAM_ID(2, 2)
#define STM32_UART_USART6_TX_DMA_STREAM     STM32_DMA_STREAM_ID(2, 7)
#define STM32_UART_USART1_IRQ_PRIORITY      12
#define STM32_UART_USART2_IRQ_PRIORITY      12
#define STM32_UART_USART3_IRQ_PRIORITY      12
#define STM32_UART_USART6_IRQ_PRIORITY      12
#define STM32_UART_USART1_DMA_PRIORITY      0
#define STM32_UART_USART2_DMA_PRIORITY      0
#define STM32_UART_USART3_DMA_PRIORITY      0
#define STM32_UART_USART6_DMA_PRIORITY      0
#define STM32_UART_DMA_ERROR_HOOK(uartp)    chSysHalt()

/*
 * USB driver system settings.
 */
#define STM32_USB_USE_OTG1                  TRUE
#define STM32_USB_USE_OTG2                  FALSE
#define STM32_USB_OTG1_IRQ_PRIORITY         14
#define STM32_USB_OTG2_IRQ_PRIORITY         14
#define STM32_USB_OTG1_RX_FIFO_SIZE         512
#define STM32_USB_OTG2_RX_FIFO_SIZE         1024
#define STM32_USB_OTG_THREAD_PRIO           LOWPRIO
#define STM32_USB_OTG_THREAD_STACK_SIZE     128
#define STM32_USB_OTGFIFO_FILL_BASEPRI      0



I am @home for the w.e without access to the board, but there is 2 other i2c slave connected, a gyrometer and a magnetometer, so i will try on Monday to
get just one byte with these slave and report the result here.

Thanks again for your support.

ps: i can tar.gz the entire directory if it can help. The idea is to have
a small autopilot like this one http://paparazzi.enac.fr/wiki/Umarim_v10 but with an stm32f4 and chibios instead
of lpc2148 and one giant mainloop. For beginning, the peripheral will stay the same.


Alexandre
User avatar
alex31
Posts: 445
Joined: Fri May 25, 2012 10:23 am
Has thanked: 57 times
Been thanked: 88 times

Re: i2c hangs when reading just one byte

Post by alex31 »

I am @home for the w.e without access to the board, but there is 2 other i2c slave connected, a gyrometer and a magnetometer, so i will try on Monday to
get just one byte with these slave and report the result here.


I just have tested with honeywell hmc5883l compass, and ivensense itg3200 gyroscope, and problem is the same.

reading 2 bytes or more is ok, reading one byte works first time and hangs second time.

Alexandre
User avatar
barthess
Posts: 861
Joined: Wed Dec 08, 2010 7:55 pm
Been thanked: 7 times

Re: i2c hangs when reading just one byte

Post by barthess »

Alexandre,
There are nothing bad found in your mcuconf.h for the first look.
I will try to reproduce bug with my ITG3200 this evening.
User avatar
barthess
Posts: 861
Joined: Wed Dec 08, 2010 7:55 pm
Been thanked: 7 times

Re: i2c hangs when reading just one byte

Post by barthess »

Alexandre,
I can not reproduce bug in my test environment. ITG3200 works fine in single byte mode.
Could you provide you full project sources and photo(scan) of MCU's surface with marking?
User avatar
alex31
Posts: 445
Joined: Fri May 25, 2012 10:23 am
Has thanked: 57 times
Been thanked: 88 times

Re: i2c hangs when reading just one byte

Post by alex31 »

I can not reproduce bug in my test environment. ITG3200 works fine in single byte mode.
Could you provide you full project sources


http://alexandre.bustico.free.fr/ETC/i2chang.tar.xz

i2c stuff is in i2cMaster.c, the three i2cGet*DevId function.
in the project, i now read two bytes to avoid hanging, you've to replace
with one to experience the problem, but since you don't have our board,
i think you will just examine the source.

it used a slighly augmented version of chibios where i add my board definition if board directory,
ld script, enhanced version of chprintf ... http://alexandre.bustico.free.fr/ETC/chibiosMod.tar.xz

Tomorrow i will work on extracting mod and make the project compile against std chibios.

and photo(scan) of MCU's surface with marking?


and i 'll make a photo of the mcu (will bring a reflex camera with macro lens to have good quality picture).

EDIT : here is the picture of the chip :
Image

Alexandre
User avatar
barthess
Posts: 861
Joined: Wed Dec 08, 2010 7:55 pm
Been thanked: 7 times

Re: i2c hangs when reading just one byte

Post by barthess »

As I can see on photo we have MCU with different revisions, but I can not find any differences
related to I2C in errata.
Today I have tested data exchanging with oscilloscope. Single byte reading goes exactly as expected.

Last string looks like caused by overflow or inaccurate pointer handling:
alex31 wrote:#0 0x08000d06 in _port_irq_epilogue () at ../../ChibiOS/os/ports/GCC/ARMCMx/chcore_v7m.c:144
#1 0x080065d2 in VectorBC () at ../../ChibiOS/os/hal/platforms/STM32/i2c_lld.c:473
#2 0xfffffffc in ?? ()


Giovanni
Previously I have developing and testing my software with -O0 option and all goes fine, but yesterday I try to enable
optimization levels. O2, O3 and Os cause immediate fallt in unhandled exception, O1 and O0 works fine (for the first look).
Probably we observe the same problem here. I will try to reduce code amount to localize problem.
Does you have any ideas where too look first?
Post Reply