config_serial=n crashing when calling a thread

Hello again,

I'm trying to lower the power consumption of my thingy91 by turning off the serial by setting config_serial=n. Unfortunately, this causes a crash while running a program which would run fine normally. Because I turn off the serial bus I cannot see the uart to check out where it crashes. I'm running a modified version of the aws_iot sample. My best guess would be that the system crashes when the thread I defined activates with this line of code:

K_THREAD_DEFINE(lightMeter_Thread, 10000, lightmeter, NULL, NULL, NULL, 10, 0, 45000);

In this thread I also make use of I2C. Could this be a possible cause? Here is all the code from the thread:

void lightmeter(void){
	//setup
	printk("Starting Lightmeter Thread\n");
	const struct device *i2c_dev = device_get_binding("I2C_2"); //get the device bindings from the devicetree and put them into a struct
	int err;
	uint8_t read_buf = 0; //Memory buffer to put a read message in.
	uint8_t write_buf = whoami_Address_lightmeter; //memory buffer to put the write message in.

	if (!device_is_ready(i2c_dev)) { //check weather I2C is activated in the devicetree
		printk("I2C: Device is not ready.\n");//print error if not.
		return;
	}
	uint8_t lightConfig[2];

	lightConfig[0] = 0x42;
	lightConfig[1] = 0b00010000;
	err = i2c_write(i2c_dev, lightConfig, 2, lightmeter_Adress);
	if(err != 0){
		printk("I2C config write failed - err: %d\n",err);
	}
	write_buf = 0x42;
	i2c_write_read(i2c_dev, lightmeter_Adress, &write_buf, 1, &read_buf,1);
	printk("0x42 settings set: %x\n", read_buf);
	lightConfig[0] = 0x41;
	lightConfig[1] = 0b00101011;
	err = i2c_write(i2c_dev, lightConfig, 2, lightmeter_Adress);
	if(err != 0){
		printk("I2C config write failed - err: %d\n",err);
	}
	free(lightConfig);
	//loop
	while(1){
		cJSON *lightMeterTransmissionObject = cJSON_CreateObject();
		cJSON *whoAmI = NULL;
		char *Message = NULL;
		
		uint8_t read_buf = 0; //Memory buffer to put a read message in.
		uint8_t write_buf = whoami_Address_lightmeter; //memory buffer to put the write message in.
		int rtrn = i2c_write_read(i2c_dev, lightmeter_Adress, &write_buf, 1, &read_buf,1); //execute the I2C write and read and put the return into an int
		if (rtrn != 0){ //check to see if the transmissions have been succesfull. If not, an error will appear.
			printk("Error on i2c_read, returned: 0x%x read_buffer: 0x%x\n", rtrn, read_buf);
		}else if(read_buf == 0xE0){ //check the whoami value against the known value from the datasheet.
			printk("who am I succes for lightmeter: 0x%x\n", read_buf);
		}else{
			printk("Who am I failed for lightmeter: 0x%x\n", read_buf);
	
		}

		aws_iot_disconnect();
		err = lte_lc_power_off();

		if(err != 0){
			printk("Error during modem shutdown: %d\n", err);
		}


		printk("Creating JSON object\n");
		whoAmI = cJSON_CreateNumber(read_buf);
		printk("Created JSON number\n");
		cJSON_AddItemToObject(lightMeterTransmissionObject, "WhoAmI", whoAmI);
		printk("Added number to object\n");

		printk("Creating measurementArray");
		cJSON *measurementsArray = cJSON_CreateArray();

		int measurementValues[3];
		for(int i = 0; i < amount_Of_Light_Measurements_Before_Send; i++){
			printk("Measurement no %d\n", i);
			write_buf = 0x50;
			rtrn = i2c_write_read(i2c_dev, lightmeter_Adress, &write_buf, 1, &read_buf,1);
			printk("Measurement Result for R light: %d \n", read_buf);
			measurementValues[0] = read_buf;
			write_buf = 0x52;
			rtrn = i2c_write_read(i2c_dev, lightmeter_Adress, &write_buf, 1, &read_buf,1);
			printk("Measurement Result for G light: %d \n", read_buf);
			measurementValues[1] = read_buf;
			write_buf = 0x54;
			rtrn = i2c_write_read(i2c_dev, lightmeter_Adress, &write_buf, 1, &read_buf,1);
			printk("Measurement Result for B light: %d \n", read_buf);
			measurementValues[2] = read_buf;


			printk("Creating measurement object\n");
        	cJSON *measurement = cJSON_CreateObject();

			printk("Creating measurement number %d\n", i);
			cJSON *measurementNumber = cJSON_CreateNumber(i);
			cJSON_AddItemToObject(measurement, "Measurement Number", measurementNumber);

			printk("Creating Rmeasurement\n");
	        cJSON *Rmeasurement = cJSON_CreateNumber(measurementValues[0]);
        	cJSON_AddItemToObject(measurement, "Red", Rmeasurement);

			printk("Creating Gmeasurement\n");
	        cJSON *Gmeasurement = cJSON_CreateNumber(measurementValues[1]);
        	cJSON_AddItemToObject(measurement, "Green", Gmeasurement);

			printk("Creating Bluemeasurement\n");
			cJSON *Bmeasurement = cJSON_CreateNumber(measurementValues[2]);
        	cJSON_AddItemToObject(measurement, "Blue", Bmeasurement);
    		
			printk("Adding item to array\n");
			cJSON_AddItemToArray(measurementsArray, measurement);

			#ifdef DEBUGGING
			k_msleep(500);
			#else
			k_sleep(K_SECONDS(time_Between_Light_Measurements_Seconds));
			#endif
		}
		
		cJSON_AddItemToObject(lightMeterTransmissionObject, "Measurements", measurementsArray);

		printk("Printing JSON message\n");
		Message = cJSON_Print(lightMeterTransmissionObject);

		printk("Storing message into struct\n");
		struct aws_iot_data dataToSend = {
			.qos = MQTT_QOS_0_AT_MOST_ONCE,
			.topic.type = AWS_IOT_SHADOW_TOPIC_UPDATE,
			.ptr = Message,
			.len = strlen(Message)
		};

		printk("%s\n", Message);

		lte_lc_connect();

		k_work_schedule(&connect_work, K_NO_WAIT);
		while(!cloud_connected){
			k_sleep(K_SECONDS(5));
		}

		
		err = aws_iot_send(&dataToSend);
		if (err) {
			printk("I2C, error: %d\n", err);
		}

		cJSON_Delete(lightMeterTransmissionObject);
		cJSON_FreeString(Message);

	}
}

Thanks in advance for any help. <3

Parents
  • Hi 

    The best way to narrow down an issue like this is to run the application in the debugger, and see where it crashes. 

    Do you have an nRF9160DK or a J-Link debugger that you can connect to your Thingy91?

    If you set CONFIG_RESET_ON_FATAL_ERROR=n in your project settings before starting your debug session the problem will be easier to identify, since the core will not immediately reset when the problem occurs. 

    Best regards
    Torbjørn

Reply
  • Hi 

    The best way to narrow down an issue like this is to run the application in the debugger, and see where it crashes. 

    Do you have an nRF9160DK or a J-Link debugger that you can connect to your Thingy91?

    If you set CONFIG_RESET_ON_FATAL_ERROR=n in your project settings before starting your debug session the problem will be easier to identify, since the core will not immediately reset when the problem occurs. 

    Best regards
    Torbjørn

Children
Related