S32K144 - flexio_i2c (i2c) does not work as expected when compiler optimization level set at -O0

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

S32K144 - flexio_i2c (i2c) does not work as expected when compiler optimization level set at -O0

Jump to solution
1,972 Views
rcortes
Contributor I

I am trying to use I2C with the S32K144 EVB. I decided to start with the flexio_i2c_s32k144 SDK example project from the S32SDK_S32K1xx_RTM_3.0.0.

After facing several issues, and debugging the project, I have found the following:

When compiling the flexio_i2c_s32k144 SDK example project using the S32 Design Studio, and setting the C Compiler and Assembler Optimization level to -O0, the project is not working properly. 

After the FLEXIO_I2C_DRV_MasterSendDataBlocking() is called, the i2cMasterState flexio_i2c_master_state_t.status is STATUS_I2C_TX_UNDERRUN, lpi2cSlaveState lpi2c_slave_state_t.status is STATUS_SUCCESS but the slave buffer is filled up with incorrect values. 

Then when the FLEXIO_I2C_DRV_MasterReceiveDataBlocking() is called, the lpi2cSlaveState lpi2c_slave_state_t.status is STATUS_BUSY, the  i2cMasterState flexio_i2c_master_state_t.status  is STATUS_I2C_RECEIVED_NACK, and the master buffer is filled up again with incorrect values. 

When I set up the optimization level to -O1, the project works correctly as expected. 

Per my project requirements, I should set the optimization level to -O0.

I verified the code size, and I can confirm that the code is not large enough to not fit the flash memory. Here are the results for the .elf file generated after running the arm-none-eabi-size utility tool with and without compiler optimization. It can be seen that the .text section without optimization is 0x96ac, which is fairly smaller than the available size of 512KB defined in the linker file as: 0x0007FBF0

 

******************************WITHOUT OPTIMIZATION (-O0)********************************

$ ./arm-none-eabi-size.exe --format=SysV -x /c/Users/rcortes/workspaceS32DS.ARM.2018.R1/flexio_i2c_s32k144/flexio_i2c_s32k144/Debug_FLASH/flexio_i2c_s32k144.elf

section size addr
.interrupts 0x400 0x0
.flash_config 0x10 0x400
.text 0x96ac 0x410
.interrupts_ram 0x400 0x1fff8000
.data 0x1e4 0x1fff8400
.code 0x0 0x1fff85e4
.bss 0x54 0x20000000
.heap 0x404 0x20000054
.stack 0x400 0x20006c00
.ARM.attributes 0x30 0x0
.debug_info 0x195b2 0x0
.debug_abbrev 0x2374 0x0
.debug_aranges 0x1288 0x0
.debug_ranges 0x1108 0x0
.debug_macro 0x20ebd 0x0
.debug_line 0xa1a4 0x0
.debug_str 0x73661 0x0
.comment 0xa0 0x0
.debug_frame 0x4ebc 0x0
Total 0xcbbfc

******************************WITH OPTIMIZATION (-O1)********************************

$ ./arm-none-eabi-size.exe --format=SysV -x /c/Users/rcortes/workspaceS32DS.ARM.2018.R1/flexio_i2c_s32k144/flexio_i2c_s32k144/Debug_FLASH/flexio_i2c_s32k144.elf

section size addr
.interrupts 0x400 0x0
.flash_config 0x10 0x400
.text 0x4044 0x410
.interrupts_ram 0x400 0x1fff8000
.data 0x1e4 0x1fff8400
.code 0x0 0x1fff85e4
.bss 0x54 0x20000000
.heap 0x404 0x20000054
.stack 0x400 0x20006c00
.ARM.attributes 0x30 0x0
.debug_info 0x22c1d 0x0
.debug_abbrev 0x2c50 0x0
.debug_loc 0x1177a 0x0
.debug_aranges 0x8f8 0x0
.debug_ranges 0xdf8 0x0
.debug_macro 0x20ebd 0x0
.debug_line 0x9b90 0x0
.debug_str 0x73660 0x0
.comment 0xa0 0x0
.debug_frame 0x1688 0x0
Total 0xdd16c

Thanks in advance for any inputs. I would expect the code to behave correctly regardless the compiler optimization level (specially if no optimization is selected)..

 

Best Regards,

Rodrigo

1 Solution
1,667 Views
victorjimenez
NXP TechSupport
NXP TechSupport

Hello Rodrigo,

In the S32 SDK Release Notes Version 3.0.0 pdf, you will find the following information about the FlexIO_I2C example.

pastedImage_1.png

When you turned off the optimizations you are making less effective the functions to receive and send information. Without the capability to do clock stretching, you are seeing that Tx underflows and Rx overflows occur. The correct way to avoid this to happen is to low the baudrate of the flexio_i2c_master. By default, the baudrate is 400000, if you modify this value for 40000, the project will work correctly even without optimizations because no clock stretching would be needed. You can make this modification with the help of Processor Expert.

pastedImage_3.png

Best Regards,

Victor

View solution in original post

3 Replies
1,667 Views
victorjimenez
NXP TechSupport
NXP TechSupport

Hello Rodrigo,

Sorry for the late reply. I made some tests and I was able to reproduce the behavior you mentioned. I will make more tests on my side to figure out what is causing this.

Best Regards,

Victor

0 Kudos
1,668 Views
victorjimenez
NXP TechSupport
NXP TechSupport

Hello Rodrigo,

In the S32 SDK Release Notes Version 3.0.0 pdf, you will find the following information about the FlexIO_I2C example.

pastedImage_1.png

When you turned off the optimizations you are making less effective the functions to receive and send information. Without the capability to do clock stretching, you are seeing that Tx underflows and Rx overflows occur. The correct way to avoid this to happen is to low the baudrate of the flexio_i2c_master. By default, the baudrate is 400000, if you modify this value for 40000, the project will work correctly even without optimizations because no clock stretching would be needed. You can make this modification with the help of Processor Expert.

pastedImage_3.png

Best Regards,

Victor

1,667 Views
rcortes
Contributor I

Thank you Victor for your help. Effectively, without optimization the project works with a smaller baud rate. I will have this in consideration for future developments.