mmuyu 发表于 2023-10-4 23:43:58

机智云协议移植到tencent os

    腾讯物联网终端操作系统(TencentOS tiny)是腾讯面向物联网领域开发的实时操作系统,具有低功耗,低资源占用,模块化,安全可靠等特点,可有效提升物联网终端产品开发效率。TencentOS tiny 提供精简的 RTOS 内核,内核组件可裁剪可配置,可快速移植到多种主流 MCU 及模组芯片上。而且,基于RTOS内核提供了丰富的物联网组件,内部集成主流物联网协议栈(如 CoAP/MQTT/TLS/DTLS/LoRaWAN/NB-IoT 等),可助力物联网终端设备及业务快速接入腾讯云物联网平台。
在tencentos 移植机智云协议,可以为后续腾讯云平台联合机智云平台开发提供便捷的途径。

首先,新建一个项目,选择腾讯os,选择对应的单片机芯片



然后,我们创建Gizwits 和utils文件夹,并且把从机智云平台下载的通用代码对应文件夹内的文件复制进去



然后到编译器里包含对应的文件夹,格式如图所示:



配置对应的串口中断和定时器中断:

void UART5_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void TIM6_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));

/* Global Variable */
void Uart5_Init(void){
    GPIO_InitTypeDefGPIO_InitStructure = {0};
    USART_InitTypeDef USART_InitStructure = {0};
    NVIC_InitTypeDefNVIC_InitStructure = {0};
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOD, &GPIO_InitStructure);

    USART_InitStructure.USART_BaudRate = 9600;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;

    USART_Init(UART5, &USART_InitStructure);
    USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);

    NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    USART_Cmd(UART5, ENABLE);

}
void Tim6_init(){
    TIM_TimeBaseInitTypeDef TIM_InitStructure={0};
    NVIC_InitTypeDefNVIC_InitStructure = {0};
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
    TIM_InitStructure.TIM_Period=1000-1;
    TIM_InitStructure.TIM_CounterMode=TIM_CounterMode_Up;
    TIM_InitStructure.TIM_Prescaler=96-1;
    TIM_InitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
    TIM_TimeBaseInit(TIM6, &TIM_InitStructure);

    TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE);

    NVIC_InitStructure.NVIC_IRQChannel=TIM6_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
    NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    TIM_Cmd(TIM6, ENABLE);
}


在中断里引用对应机智云需要的函数:



完善机智云对应的函数:

int32_t uartWrite(uint8_t *buf, uint32_t len)
{
    uint32_t i = 0;
   
    if(NULL == buf)
    {
      return -1;
    }
   
    #ifdef PROTOCOL_DEBUG
    GIZWITS_LOG("MCU2WiFi[%4d:%4d]: ", gizGetTimerCount(), len);
    for(i=0; i<len; i++)
    {
      GIZWITS_LOG("%02x ", buf);
    }
    GIZWITS_LOG("\n");
    #endif

    for(i=0; i<len; i++)
    {
      //USART_SendData(UART, buf);//STM32 test demo
      //Serial port to achieve the function, the buf sent to the module
      USART_SendData(UART5, buf);
      while (USART_GetFlagStatus(UART5, USART_FLAG_TXE) == RESET);
      if(i >=2 && buf == 0xFF)
      {
          //Serial port to achieve the function, the 0x55 sent to the module
          //USART_SendData(UART, 0x55);//STM32 test demo
            USART_SendData(UART5, 0x55);
            while (USART_GetFlagStatus(UART5, USART_FLAG_TXE) == RESET);
      }
    }


   
    return len;
}

在tencent os里新建任务,任务里运行机智云的线程:

#define TASK1_STK_SIZE       2048
k_task_t task1;
__aligned(4) uint8_t task1_stk;



void task1_entry(void *arg)
{
    while (1)
    {
      userHandle();
      gizwitsHandle((dataPoint_t *)&currentDataPoint);
      tos_task_delay(10);
    }
}

在开始任务调度前初始化数据点,然后启动任务和进程调度:
userInit();
      gizwitsInit();
    tos_knl_init();
    tos_task_create(&task1, "task1", task1_entry, NULL, 3, task1_stk, TASK1_STK_SIZE, 0); // Create task1
    tos_knl_start();

效果展示:


页: [1]
查看完整版本: 机智云协议移植到tencent os