Error while performing the infinite loop

Hi, 

Could you please advise, am designing a device using the nrf52dk-52832 in nrf sdk connect 2.5.1 and toolchain 2.5.2 that reads the previously adquired samples data from and ADC placed in an Eeprom thru I2c then placed in a 25000 bytes 16 bit array and then the 8 byte from the eeprom group them in a 16 byte register then filtered out to be 0 or 1 and finally turn on or off a pin with a delay in order to recreate the original waveform adquired in the eeprom.

the problem is that if i make shorts the loops of all this it works ok but if make the complete loops to complete the task the program goes into fatal error mode and restarts again. 

the idea its that once it reaches the point of recreating the waveform enters an infinite loop and stays there but in my case keep on crashing the software in the ksleep instruction, then i changed it for a while loop and then crashes at the end of the first output of the signal finishing the loop and so on.

I attached here to code for your reference:

/*
 * Copyright (c) 2016 Intel Corporation
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/gpio.h>

/* STEP 3 - Include the header file of the I2C API */
#include <zephyr/drivers/i2c.h>
/* STEP 4.1 - Include the header file of printk() */
#include<zephyr/sys/printk.h>
#include <zephyr/drivers/i2c/target/eeprom.h>
//#include <zephyr/timing/timing.h>
//#include <zephyr/drivers/entropy.h>


/* STEP 9 - Increase the sleep time from 100ms to 10 minutes  */
#define SLEEP_TIME_MS   600

/* SW0_NODE is the devicetree node identifier for the "sw0" alias */
#define SW0_NODE	DT_ALIAS(sw0) 
static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET(SW0_NODE, gpios);

/* LED0_NODE is the devicetree node identifier for the "led0" alias. */
#define LED0_NODE	DT_ALIAS(led0)
static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios);


/* LED0_NODE is the devicetree node identifier for the "led0" alias. */
#define LED1_NODE	DT_ALIAS(led1)
static const struct gpio_dt_spec led1 = GPIO_DT_SPEC_GET(LED1_NODE, gpios);



/* gpiocus0_NODE is the devicetree node identifier for the "gpiocus0" alias. */
#define GPIOCUS0_NODE	DT_ALIAS(gpiocus0)
static const struct gpio_dt_spec gpio0 = GPIO_DT_SPEC_GET(GPIOCUS0_NODE, gpios);


/* gpiocus0_NODE is the devicetree node identifier for the "gpiocus0" alias. */
#define GPIOCUS1_NODE	DT_ALIAS(gpiocus1)
static const struct gpio_dt_spec gpio1 = GPIO_DT_SPEC_GET(GPIOCUS1_NODE, gpios);


/* STEP 8 - Define the I2C slave device address and the addresses of relevant registers */
#define I2C0_NODE DT_NODELABEL(mysensor)
#define Eeprom_addr 0b1010000
#define Eeprom_addr_write 0b10100000
#define Eeprom_addr_read 0b10100001


/* STEP 4.2 - Declare the buffer de donde voy a guardar los datos leidos de la memoria */
static int16_t senal[25000];


/* STEP 6 - Get the node identifier of the sensor */
static const struct i2c_dt_spec dev_i2c = I2C_DT_SPEC_GET(I2C0_NODE);


/*define timer*/
struct k_timer mytimer;







/* STEP 4 - Define the callback function */
void button_pressed(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
{
    gpio_pin_toggle_dt(&led);
}
/* STEP 5 - Define a variable of type static struct gpio_callback */
static struct gpio_callback button_cb_data;

/* STEP 4 - Define the timer callback function */
static void mytimer_cb(struct k_timer *dummy){
	//do something
	gpio_pin_toggle_dt(&led);
}



int main(void)
{

	k_timer_init(&mytimer,mytimer_cb,NULL);
	

	int ret;

	if (!device_is_ready(led.port)) {
		return -1;
	}

	
	if (!device_is_ready(led1.port)) {
		return -1;
	}

	if (!device_is_ready(button.port)) {
		return -1;
	}

if (!device_is_ready(gpio0.port)) {
		return -1;
	}
if (!device_is_ready(gpio1.port)) {
		return -1;
	}

	ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);
	if (ret < 0) {
		return -1;
	}

	ret = gpio_pin_configure_dt(&led1, GPIO_OUTPUT_ACTIVE);
	if (ret < 0) {
		return -1;
	}

	

	ret = gpio_pin_configure_dt(&button, GPIO_INPUT);
	if (ret < 0) {
		return -1;
	}

	ret = gpio_pin_configure_dt(&gpio0, GPIO_OUTPUT_ACTIVE);
	if (ret < 0) {
		return -1;
	}

	ret = gpio_pin_configure_dt(&gpio1, GPIO_OUTPUT_ACTIVE);
	if (ret < 0) {
		return -1;
	}


	/* STEP 3 - Configure the interrupt on the button's pin */
	ret = gpio_pin_interrupt_configure_dt(&button, GPIO_INT_EDGE_TO_ACTIVE);
if (ret < 0) {
		return -1;
	}
	/* STEP 6 - Initialize the static struct gpio_callback variable   */
gpio_init_callback(&button_cb_data, button_pressed, BIT(button.pin)); 	
	/* STEP 7 - Add the callback function by calling gpio_add_callback()   */
gpio_add_callback(button.port, &button_cb_data);

	  int count=0;//cuenta para no hacerlo mas de una vez la lectura de la memoria
	  int memoria=0;
	  int16_t cuenta =0x0000;
	  uint8_t x= 0x00;
		uint8_t y=0x00;
	//while (1) {
	//int ret;
	//uint8_t waveform[128];
	uint8_t sensor_regs[3] ={Eeprom_addr_write,0x00,0x00};
	//uint8_t sensor_regs1[256] ={Eeprom_addr_write,0x00,0x00};
	uint8_t sensor_regs1[2] ={0x00,0x00};//en este array solo se da de alta las direcciones de i2c que se van a leer
	uint8_t temp_reading[128];
    sensor_regs1[0]=x;//direccion inicial
	sensor_regs1[1]=y;//direccion inicial a leer en la eeprom
	//uint8_t x=0x00;
	//uint8_t y=0x00;
	//uint8_t senal[512];
	uint8_t contador=0;

	// if(count<=0){ 
	 // while (memoria<=268){
	  while (memoria<=194){
	// while (memoria<=2){
    	sensor_regs1[0]=x;
		sensor_regs1[1]=y;
	  
	  // ret = i2c_write_read_dt(&dev_i2c,&sensor_regs1,3,&waveform,128);
        ret = i2c_write_read_dt(&dev_i2c,&sensor_regs1,2,&temp_reading,128);//cuando usas este metodo puedes accesar cualquier locaclidad en la memoria. le das las direcciones nada mas
       // ret = i2c_read_dt(&dev_i2c, &sensor_regs1, sizeof(sensor_regs1));//en este metodo cada que leas la memria vas avanzando en las direcciones sin que tu tengas el control
		gpio_pin_toggle_dt(&led1);
        k_sleep(K_MSEC(100));
         
		if(ret != 0){
			printk("Failed to write/Read I2C device address %x at Reg. %x \n\r", dev_i2c.addr,sensor_regs[0]);
		}
		//else{
		int show=0;
		while (show<=128){
				
				senal[cuenta]=temp_reading[show];
				//senal[contador]=waveform[show];
                printk("memory content write&read %x\n\r",senal[cuenta]);
				//printk("memory content write&read %x\n\r",sensor_regs1[show]);
				//printk("memory content write&read %x\n\r",waveform[show]);
				printk("row  %x and %d  \n\r",cuenta,y);
		show= show+1;
		contador=contador+0x01;
		cuenta=cuenta+0x0001;
		}
		//memoria=memoria+1;
		x=((cuenta>>8)&0xff);
		y= (cuenta&0xff);

		//}
		
		memoria=memoria+1;
		//count=count+1;
	printk("aqui estoy1\n\r");
	printk("count %d\n\r", count);

	  }
	count=count+1;
	printk("aqui estoy2\n\r");
	printk("count %d\n\r", count);
	//  }
	 //else
	 // {
	//salto:
	while (1) {
	  printk("aqui estoy3\n\r");
	  int16_t transitory[2];
	  int16_t loop=0x0000;
	  int16_t llenado=0x0000;

	while (loop<=0x6000){
	//while (loop<=0x0004){

		transitory[loop]=senal[llenado];
		transitory[loop]=transitory[loop]<<8;
		transitory[loop]|=senal[llenado+1];
		printk("reconvoluted data %x\n\r",transitory[loop]);
		loop=loop+0x0001;
		llenado=llenado+0x0002;
	if (transitory[loop]<=0x000f){
			gpio_pin_set_dt(&led,0);
			int delay=0;
			while (delay<3000){
				delay=delay+1;
			}
		//k_sleep(K_MSEC(100));
	}
	else{
		gpio_pin_set_dt(&led,1);
		int delay=0;
		while (delay<3000){
				delay=delay+1;
			}
		//k_sleep(K_MSEC(100));
	}

	
  
		/* STEP 8 - Remove the polling code */
       //	gpio_pin_toggle_dt(&led);
    //   gpio_pin_toggle_dt(&gpio0);
	  //k_timer_start(&mytimer,K_SECONDS(1),K_SECONDS(1));
	 //  k_msleep(SLEEP_TIME_MS); 
	//   k_sleep(K_MSEC(100));
	  // gpio_pin_toggle_dt(&gpio1);
	  // k_msleep(SLEEP_TIME_MS); 
        //k_msleep(SLEEP_TIME_MS); 

}	
}	
//}
}

Parents
  • Starting from line 223, you are accessing elements outside of array transitory

    	while (1) {
    	  printk("aqui estoy3\n\r");
    	  int16_t transitory[2]; // array of 2 elements
    	  int16_t loop=0x0000;
    	  int16_t llenado=0x0000;
    
    	while (loop<=0x6000){ // loop going from 0 to 0x6000
    	//while (loop<=0x0004){
    
    		transitory[loop]=senal[llenado]; // accessing data outside of the 2 element array when `loop` is >= 2
    		transitory[loop]=transitory[loop]<<8;
    		transitory[loop]|=senal[llenado+1];
    		printk("reconvoluted data %x\n\r",transitory[loop]);
    		loop=loop+0x0001;
    		llenado=llenado+0x0002;

    Unless you manage to overwrite the thread stack, you should be able to see the point of crash in your stack trace.

  • Thx Xorly, that was it! cant belive i didn´t notice that typo. but what you mean i should do with the thread stack? i didnt understand, could you please explain me what i need to do?

  • That is great news!

    If you attach debugger, you should be able see the stack trace and also the point in the code where it was interrupted by some fault handler when the fault happened.

    Unless your out of bounds writes managed to overwrite your current thread stack, then the debugger can't help.

    Complete OT: I suggest you to improve your code formatting (mainly indentations) next time and also have look on for loops. It would improve readability.

Reply
  • That is great news!

    If you attach debugger, you should be able see the stack trace and also the point in the code where it was interrupted by some fault handler when the fault happened.

    Unless your out of bounds writes managed to overwrite your current thread stack, then the debugger can't help.

    Complete OT: I suggest you to improve your code formatting (mainly indentations) next time and also have look on for loops. It would improve readability.

Children
No Data
Related