通过FreeRTOS中的队列发送数据

时间:2016-04-11 06:46:27

标签: c freertos

我有两个带代码的文件。

  1. 以太网
  2. CAN
  3. 在这些文件之间我想发送数据。 所以,在 CAN文件中我创建了结构:

    struct screenData {
            uint16_t gyroskop_x;
            uint16_t gyroskop_y;
            uint16_t gyroskop_z;
            uint16_t euler_x;
            uint16_t euler_y;
            uint16_t euler_z;
        } data;
    

    我尝试用我的测试值初始化这个结构。

    struct screenData * toSend;
        data.gyroskop_x = 1;
        data.gyroskop_y = 2;
        data.gyroskop_z = 3;
        data.euler_x = 4;
        data.euler_y = 5;
        data.euler_z = 6;
        toSend = &data;
    

    我创建了新任务,我每隔1秒发送一次数据。

    xQueueSend ( fronta_gyroskop, ( void * ) &toSend , 10 );
    

    以太网文件中,我初始化队列。 在以太网文件中:

    1. 初始化队列
    2. 为数据创建接收结构
    3. 从队列
    4. 接收数据

      代码

      xQueueHandle fronta_gyroskop = 0;
      
      extern void init_queue(void)
       {
         fronta_gyroskop = xQueueCreate(6, sizeof(struct ethernetData *));
       }
      
      struct ethernetData {
              uint16_t gyroskop_x;
              uint16_t gyroskop_y;
              uint16_t gyroskop_z;
              uint16_t euler_x;
              uint16_t euler_y;
              uint16_t euler_z;
          } gyro;
      

      在任务中我每隔1秒接收一次数据。

      if (xQueueReceive(fronta_gyroskop, &gyro, 20)){ 
      }
      

      但陀螺仪中的值不是我的值(1,2,3,4,5,6),但值是随机的32位值。任何想法,我做错了什么?

      修改

      CAN端代码

      #include "Tasky/Ethernet.h"
      void *fronta_gyroskop;
      
      can_mb_conf_t tx_mailbox;
      can_mb_conf_t rx_mailbox;
      /** Receive status */
      volatile uint32_t g_ul_recv_status = 0;
      
      void CAN0_Handler(void)
      {
          uint32_t ul_status;
      
          ul_status = can_mailbox_get_status(CAN0, 0);
          if (ul_status & CAN_MSR_MRDY) {
              rx_mailbox.ul_status = ul_status;
              can_mailbox_read(CAN0, &rx_mailbox);
              g_ul_recv_status = 1;
          }
      }
      
      struct screenData {
              uint16_t gyroskop_x;
              uint16_t gyroskop_y;
              uint16_t gyroskop_z;
              uint16_t euler_x;
              uint16_t euler_y;
              uint16_t euler_z;
          } data;
      
      extern void task_can_read(void *pvParameters)
      {
          UNUSED(pvParameters);
          const portTickType xDelayTime = 10;
          uint8_t nacti = 1;
      
          /* init test value to sending */
          struct screenData * toSend;
          data.gyroskop_x = 1;
          data.gyroskop_y = 2;
          data.gyroskop_z = 3;
          data.euler_x = 4;
          data.euler_y = 5;
          data.euler_z = 6;
          toSend = &data;
      
          for (;;){
              if (!g_ul_recv_status) {
                  //puts("zadna zprava na CAN sbernici\r");
              } else {
                  if ((rx_mailbox.ul_id >> 18) == FIRST_GYRO_ID) //rotation to 11bit number
                  {
                      //calculate
                      nacti = 1;
                  }
      
                  if ((rx_mailbox.ul_id >> 18) == SECOND_GYRO_ID)
                  {
                      //calculate
                      xQueueSend ( fronta_gyroskop, ( void * ) &data , 10 );  //send data over queue
                      nacti = 0;
                  }
      
                  if (nacti == 1)
                  {
                      rx_mailbox.ul_id = CAN_MID_MIDvA(SECOND_GYRO_ID);
                      can_mailbox_init(CAN0, &rx_mailbox);
                  }
                  if (nacti == 0)
                  {
                      rx_mailbox.ul_id = CAN_MID_MIDvA(FIRST_GYRO_ID);
                      can_mailbox_init(CAN0, &rx_mailbox);
                  }
      
                  g_ul_recv_status = 0;   //clear flag
              }
      
              vTaskDelay(xDelayTime);
          }
      }
      

      ETH代码

      #include "Tasky/CAN_TASKs.h"
      
      struct tcp_pcb *output = NULL;
      void *dataeth;
      
      struct netif gs_net_if;
      uint32_t g_ip_mode;
      int8_t g_c_ipconfig[];
      
      struct ethernetData {
              uint16_t gyroskop_x;
              uint16_t gyroskop_y;
              uint16_t gyroskop_z;
              uint16_t euler_x;
              uint16_t euler_y;
              uint16_t euler_z;
          } gyro;
      
      
      /************************************************************************/
      /*                  INIT QUEU to analyze data from TCPIP                */
      /************************************************************************/
      xQueueHandle fronta_gyroskop = 0;
      
      extern void init_fronta_ethernet(void)
      {
          fronta_gyroskop = xQueueCreate(6, sizeof(struct ethernetData *));
      }
      
      /************************************************************************/
      /*                  TASK of FREERTOS to TCPIP                           */
      /************************************************************************/
      extern void TCP_connection(void *pvParameters)
      {
          UNUSED(pvParameters);
      
          const portTickType xDelayTime = 1000 / portTICK_RATE_MS;
      
          uint16_t trest[6];
      
          while (1){  
      
              if (xQueueReceive(fronta_gyroskop, &gyro, 10)){ //receive data over queue
                  tcp_write(output, (void *)&gyro, sizeof(gyro), TCP_WRITE_FLAG_COPY);    //Write data for sending (but does not send it immediately)
                  tcp_sent(output, NULL); //Used to specify the function that should be called when TCP data has been successfully delivered to the remote host
                  tcp_output(output); //Find out what we can send and send it.
              }
              vTaskDelay(xDelayTime);
          }
      }
      

0 个答案:

没有答案