Data Queues crashing Zephyr code

  1. Hello,
    I am writing a code that has 2 threads. One puts data in a queue and the other gets the data from the queue. The problem is after I get the data from the queue and it's printed, the device gives an error and reboots. I think the problem might be in my definition of the queue, since I have a different queue that works normally. Here is how I define it:
    in queues.h
    struct data_time{
    uint64_t time1;
    uint64_t time2;
    uint64_t time3;
    uint64_t time4;
    };
    K_MSGQ_DEFINE(data_timeq, sizeof(struct data_time), 4, 1);
    In main:
    struct sensor_q_adrrs{
    struct k_msgq* msgq_data_pointer;
    struct k_msgq* msgq_time_pointer;
    };
    struct sensor_q_adrrs sensor_q = {
    .msgq_data_pointer = &data_senq,
    .msgq_time_pointer = &data_timeq,
    } ;
    In the other files that I use so it's connected to the main:
    struct k_msgq* time_q1;
    void register_msgq_addres2(struct k_msgq* ptr_from_main_data, struct k_msgq* ptr_from_main_time){
             printk("\n gets to register_msgq_addres1");
             data_q1 = ptr_from_main_data; time_q1 = ptr_from_main_time;
    }
    I would really appreciate it if any tips can be given on what I do wrong with the definition.
    The error I get when the code reboots is: 
    Faulting instruction address (r15/pc): 0x00000000
Parents
  • Hi, 

    From the team:

    The message queue definition looks fine, and the used alignment to one byte should not matter here. However, there is too little code to say something more - no definition of data_senq, it is not shown how an element is taken from the queue, when register_msgq_address2 is called, etc. Is it possible to elaborate in more detail? It's better to put some more effort into preparing this question if you expect some real help. The full error message could also be helpful. 

    Regards,
    Amanda

Reply
  • Hi, 

    From the team:

    The message queue definition looks fine, and the used alignment to one byte should not matter here. However, there is too little code to say something more - no definition of data_senq, it is not shown how an element is taken from the queue, when register_msgq_address2 is called, etc. Is it possible to elaborate in more detail? It's better to put some more effort into preparing this question if you expect some real help. The full error message could also be helpful. 

    Regards,
    Amanda

Children
  • Hello,

    The data_senq is not a problem and the code just with it works without a problem that's why I didn't include more of it in the explanation.

    For the other parts, the .h file I changed a bit:


    struct data_time{
        uint64_t time1;
    };

    K_MSGQ_DEFINE(data_timeq, sizeof(struct data_time), 1, 1);

    This is how I call the time_q in data_gen.c file to withe a variable in the queue:

    struct k_msgq* time_q;

    int data_gen(){
        uint64_t message_ts = 0;

        err = date_time_now(&message_ts);
        if (err) {
            printk("date_time_now, error: %d\n", err);
            return err;
        }
        aws_put_to_dataq_timeq(sine_table[i], message_ts);
    }
    void register_msgq_addres1(struct k_msgq* ptr_from_main_data, struct k_msgq* ptr_from_main_time){
        data_q = ptr_from_main_data;
        time_q = ptr_from_main_time;
    }
    void aws_put_to_dataq_timeq(uint8_t put_data, uint64_t put_time){
       
        if (data_q == NULL){
            printk("Eda msgq is null! Did you forget to call register_msgq_address?");
            return;
        }
        if (time_q == NULL){
            printk("Eda msgq is null! Did you forget to call register_msgq_address?");
            return;
        }
       
        k_msgq_put(data_q, &put_data, K_NO_WAIT);
        k_msgq_put(time_q, &put_time, K_NO_WAIT);
     
    }
    and this is in MQTT_AWS.c file how I read the data from the queue:
    static int data_publish()
    {
        int err=0;
        char *message;
        uint64_t message_ts = 0;
        uint8_t sen_val = 0;
     

        if(k_msgq_num_used_get(data_q1) > 0){
            if(k_msgq_num_used_get(time_q1) > 0){
                aws_get_from_q(&message_ts, &sen_val);
                if (root_obj == NULL){
                    cJSON_Delete(root_obj);
                    err = -ENOMEM;
                    return err;
                }

                err += json_add_number(root_obj, "sen", sen_val);
                err += json_add_number(root_obj, "ts", message_ts);

                if (err) {
                    printk("json_add, error: %d\n", err);
                    goto cleanup;
                }

                message = cJSON_Print(root_obj);
                if (message == NULL) {
                    printk("cJSON_Print, error: returned NULL\n");
                    err = -ENOMEM;
                    goto cleanup;
                }

                struct aws_iot_data tx_data = {
                    .qos = MQTT_QOS_0_AT_MOST_ONCE,
                    .topic.type = AWS_IOT_SHADOW_TOPIC_UNKNOWN,
                    .topic.str = topic_string,
                    .topic.len = sizeof(topic_string),
                    .ptr = message,
                    .len = strlen(message)
                };


                   
               
                printk("Publishing: %s to AWS IoT broker\n", message);

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

                cJSON_FreeString(message);
               
            }
           
        }
       
    cleanup:
        cJSON_Delete(root_obj);
       
        return err;
    }
    void aws_get_from_q(uint64_t* get_time, uint8_t *get_data){
            k_msgq_get(time_q1, get_time, K_NO_WAIT);
            k_msgq_get(data_q1, get_data, K_NO_WAIT);
    }

    void register_msgq_addres2(struct k_msgq* ptr_from_main_data, struct k_msgq* ptr_from_main_time){
        data_q1 = ptr_from_main_data;
        time_q1 = ptr_from_main_time;
    }
    Currently the code gets data out and it diesn't crash, but the data is not correct and it's always the same number. It's UNIX timestamp, but for a completely different time that it currently is.
Related