SPI and I2C losing synchronization after reset

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

SPI and I2C losing synchronization after reset

跳至解决方案
1,780 次查看
kamilla_peixoto
Contributor II

Hello everybody!

I am using a S32K144EVB to read some data from different sensors using both I2C and SPI communication protocols

The communication works fine until a press the reset button on the board or disconnect the board from the power supply. When the board is initialized, it loses the synchronization with the sensors and I am not able to read the data correctly anymore.

I have tried to program it using both the MBDT and the SDK, but I face the same problem in both environments.

May someone help me to understand what is going on, please?

I appreciate your assistance. Thank you!

标记 (3)
0 项奖励
回复
1 解答
1,722 次查看
kamilla_peixoto
Contributor II

I could solve the problem by powering the sensor using a GPIO pin. Thus, I just turn it off and on again before entering the main loop.

Thank you.

在原帖中查看解决方案

标记 (1)
0 项奖励
回复
4 回复数
1,770 次查看
PetrS
NXP TechSupport
NXP TechSupport

Hi,

do your MCU properly run the application from flash after reset/power on?
Do you see right signals on SPI and I2C buses that a connected sensor's protocol requires? 
If MCU run as master, the MCU initiates and controls communication and sensor/slave should respond accordingly.

BR, Petr

0 项奖励
回复
1,763 次查看
kamilla_peixoto
Contributor II

Thank you for your reply, Petr.

The MCU properly runs the application after the reset because I can see the data in FreeMaster and the LED also changes accordingly to the read data.

Using a data analyzer, I realized the data signals are shifted after the reset, so I usually miss a couple of bits, i.e. I have the same data being sent by the sensors, but they are not synchronized with the board anymore. Inserting a delay before initializing the pins and adding extra capacitors between the Vcc and GND helped, but didn't solve the problem completely.

 The board runs as Master and I tried to add some delays (SCK to PCS, PCS to SCK and between transfers), but it also didn’t work.

0 项奖励
回复
1,741 次查看
kamilla_peixoto
Contributor II

The first picture shows the data as I am expecting, and the second data is what I get after pressing the "reset button". Basically, there is one bit missing, so all my data is shifted.

If I keep resetting it, I keep missing more bits.

 

My code is like this:

 

/* Including needed modules to compile this module/procedure */
#include "Cpu.h"
#include "clockMan1.h"
#include "pin_mux.h"
#include "Send.h"
#include "Receive.h"
#include "adConv1.h"
#include "PWM.h"
#include "osif1.h"
#include "dmaController1.h"
#if CPU_INIT_CONFIG
#include "Init_Config.h"
#endif
#define BUFFER_SIZE 2U
#define TIMEOUT 100U
volatile int exit_code = 0;
/* User includes (#include below this line is not maintained by Processor Expert) */

#include <stdint.h>
#include <stdbool.h>

//Received Data
uint8_t masterDataReceive[BUFFER_SIZE];

//Status of the SPI transfer
status_t sts;

// Delay function
void delay(volatile int cycles)
{
/* Delay function - do nothing for a number of cycles */
while(cycles--);
}

 


/*!
\brief The main function for the project.
\details The startup initialization sequence is the following:
* - __start (startup asm routine)
* - __init_hardware()
* - main()
* - PE_low_level_init()
* - Common_Init()
* - Peripherals_Init()
*/
int main(void)
{
/* Write your local variable definition here */

/*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
#ifdef PEX_RTOS_INIT
PEX_RTOS_INIT(); /* Initialization of the selected RTOS. Macro is defined by the RTOS component. */
#endif
/*** End of Processor Expert internal initialization. ***/

ftm_state_t ftmStateStruct;
uint8_t masterDataSend[BUFFER_SIZE];

lpspi_state_t masterState;

 

/* Initialize pins
* - See PinSettings component for more info
*/


/* Initialize and configure clocks
* - see clock manager component for details
*/
CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,
g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_AGREEMENT);


PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);


LPSPI_DRV_MasterDeinit(0U);

delay(5000000);

lpspi_master_config_t MasterConfigSPI = {
.bitsPerSec = 100000U,
.whichPcs = LPSPI_PCS0,
.pcsPolarity = LPSPI_ACTIVE_LOW,
.isPcsContinuous = true,
.bitcount = 8U,
.lpspiSrcClk = 8000000U, //48000000U,
.clkPhase = LPSPI_CLOCK_PHASE_1ST_EDGE,
.clkPolarity = LPSPI_SCK_ACTIVE_HIGH,
.lsbFirst = false,
.transferType = LPSPI_USING_INTERRUPTS,
};


/* Initialize LPSPI0 (Send)*/
LPSPI_DRV_MasterInit(0U, &masterState, &MasterConfigSPI);
LPSPI_DRV_MasterSetDelay(0U, 10U, 10U, 10U);

/* Variable used for the loop that initializes the data buffer */
uint16_t i;

// Data to be sent

for (i = 0u; i < BUFFER_SIZE; i++)
{
masterDataSend[i] = 255u;
}

while(1)
{
sts= LPSPI_DRV_MasterTransferBlocking(0U, &masterDataSend, &masterDataReceive, BUFFER_SIZE, TIMEOUT);
delay(50000);

}
/* Write your code here */
/* For example: for(;;) { } */

/*** Don't write any code pass this line, or it will be deleted during code generation. ***/
/*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
#ifdef PEX_RTOS_START
PEX_RTOS_START(); /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
#endif
/*** End of RTOS startup code. ***/
/*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
for(;;) {
if(exit_code != 0) {
break;
}
}
return exit_code;
/*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/

/* END main */
/*!
** @}
*/
/*
** ###################################################################
**
** This file was created by Processor Expert 10.1 [05.21]
** for the Freescale S32K series of microcontrollers.
**
** ###################################################################
*/

 

0 项奖励
回复
1,723 次查看
kamilla_peixoto
Contributor II

I could solve the problem by powering the sensor using a GPIO pin. Thus, I just turn it off and on again before entering the main loop.

Thank you.

标记 (1)
0 项奖励
回复