STM32F0 LL库 机智云适配

1.下载机智云的MCU固件,选STM32F103

 

2.将 Utils Gizwits 文件都放一起

 

 

 

3.添加一下工程c文件

 

 

 4.添加include

 

 

 5.适配接口  ..\Gizwits\gizwits_protocol.c

其实就是1个串口接收,一个串口发送,接收用中断(否则处理其他事情的时候,他喵的没收到)

下面列出适配的接口

/**
************************************************************
* @file         gizwits_product.c
* @brief        Gizwits control protocol processing, and platform-related       hardware initialization 
* @author       Gizwits
* @date         2017-07-19
* @version      V03030000
* @copyright    Gizwits
* 
* @note         机智云.只为智能硬件而生
*               Gizwits Smart Cloud  for Smart Products
*               链接|增值ֵ|开放|中立|安全|自有|自由|生态
*               www.gizwits.com
*
***********************************************************/

#include <stdio.h>
#include <string.h>
#include "gizwits_product.h"
#include "common.h"

static uint32_t timerMsCount;
uint8_t aRxBuffer;

/** User area the current device state structure*/
dataPoint_t currentDataPoint;

/**@} */
/**@name Gizwits User Interface
* @{
*/

/**
* @brief Event handling interface

* Description:

* 1. Users can customize the changes in WiFi module status

* 2. Users can add data points in the function of event processing logic, such as calling the relevant hardware peripherals operating interface

* @param [in] info: event queue
* @param [in] data: protocol data
* @param [in] len: protocol data length
* @return NULL
* @ref gizwits_protocol.h
*/
int8_t gizwitsEventProcess(eventInfo_t *info, uint8_t *gizdata, uint32_t len)
{
  uint8_t i = 0;
  dataPoint_t *dataPointPtr = (dataPoint_t *)gizdata;
  moduleStatusInfo_t *wifiData = (moduleStatusInfo_t *)gizdata;
  protocolTime_t *ptime = (protocolTime_t *)gizdata;
  
#if MODULE_TYPE
  gprsInfo_t *gprsInfoData = (gprsInfo_t *)gizdata;
#else
  moduleInfo_t *ptModuleInfo = (moduleInfo_t *)gizdata;
#endif

  if((NULL == info) || (NULL == gizdata))
  {
    return -1;
  }

  for(i=0; i<info->num; i++)
  {
    switch(info->event[i])
    {

      case EVENT_run_mode:
        currentDataPoint.valuerun_mode = dataPointPtr->valuerun_mode;
        GIZWITS_LOG("Evt: EVENT_run_mode %d\n", currentDataPoint.valuerun_mode);
        switch(currentDataPoint.valuerun_mode)
        {
          case run_mode_VALUE0:
            //user handle
            break;
          case run_mode_VALUE1:
            //user handle
            break;
          case run_mode_VALUE2:
            //user handle
            break;
          case run_mode_VALUE3:
            //user handle
            break;
          case run_mode_VALUE4:
            //user handle
            break;
          default:
            break;
        }
        break;

      case EVENT_light:
        currentDataPoint.valuelight = dataPointPtr->valuelight;
        GIZWITS_LOG("Evt:EVENT_light %d\n",currentDataPoint.valuelight);
        //user handle
        break;
      case EVENT_temp:
        currentDataPoint.valuetemp = dataPointPtr->valuetemp;
        GIZWITS_LOG("Evt:EVENT_temp %d\n",currentDataPoint.valuetemp);
        //user handle
        break;
      case EVENT_wind:
        currentDataPoint.valuewind = dataPointPtr->valuewind;
        GIZWITS_LOG("Evt:EVENT_wind %d\n",currentDataPoint.valuewind);
        //user handle
        break;


      case WIFI_SOFTAP:
        break;
      case WIFI_AIRLINK:
        break;
      case WIFI_STATION:
        break;
      case WIFI_CON_ROUTER:
 
        break;
      case WIFI_DISCON_ROUTER:
 
        break;
      case WIFI_CON_M2M:
 
        break;
      case WIFI_DISCON_M2M:
        break;
      case WIFI_RSSI:
        GIZWITS_LOG("RSSI %d\n", wifiData->rssi);
        break;
      case TRANSPARENT_DATA:
        GIZWITS_LOG("TRANSPARENT_DATA \n");
        //user handle , Fetch data from [data] , size is [len]
        break;
      case WIFI_NTP:
        GIZWITS_LOG("WIFI_NTP : [%d-%d-%d %02d:%02d:%02d][%d] \n",ptime->year,ptime->month,ptime->day,ptime->hour,ptime->minute,ptime->second,ptime->ntp);
        break;
      case MODULE_INFO:
            GIZWITS_LOG("MODULE INFO ...\n");
      #if MODULE_TYPE
            GIZWITS_LOG("GPRS MODULE ...\n");
            //Format By gprsInfo_t
      #else
            GIZWITS_LOG("WIF MODULE ...\n");
            //Format By moduleInfo_t
            GIZWITS_LOG("moduleType : [%d] \n",ptModuleInfo->moduleType);
      #endif
    break;
      default:
        break;
    }
  }

  return 0;
}

/**
* User data acquisition

* Here users need to achieve in addition to data points other than the collection of data collection, can be self-defined acquisition frequency and design data filtering algorithm

* @param none
* @return none
*/
void userHandle(void)
{
 /*

    */
    
}

/**
* Data point initialization function

* In the function to complete the initial user-related data
* @param none
* @return none
* @note The developer can add a data point state initialization value within this function
*/
void userInit(void)
{
    memset((uint8_t*)&currentDataPoint, 0, sizeof(dataPoint_t));
    
    /** Warning !!! DataPoint Variables Init , Must Within The Data Range **/ 
    /*
      currentDataPoint.valuerun_mode = ;
      currentDataPoint.valuelight = ;
      currentDataPoint.valuetemp = ;
      currentDataPoint.valuewind = ;
    */

}


/**
* @brief Millisecond timing maintenance function, milliseconds increment, overflow to zero

* @param none
* @return none
*/
void gizTimerMs(void)
{
    timerMsCount++;
}

/**
* @brief Read millisecond count

* @param none
* @return millisecond count
*/
uint32_t gizGetTimerCount(void)
{
    return timerMsCount;
}

/**
* @brief MCU reset function

* @param none
* @return none
*/
void mcuRestart(void)
{
   // __set_FAULTMASK(1);
    NVIC_SystemReset();
}

/**@} */



/**
* @brief USART init function

* Serial communication between WiFi modules and device MCU
* @param none
* @return none
*/
void uartInit(void)
{
    //HAL_UART_Receive_IT(&huart2, (uint8_t *)&aRxBuffer, 1);//开启下一次接收中断  
}

/**
* @brief Serial port write operation, send data to WiFi module
*
* @param buf      : buf address
* @param len      : buf length
*
* @return : Return effective data length;-1,return failure
*/
int32_t uartWrite(uint8_t *buf, uint32_t len)
{
        uint8_t crc[1] = {0x55};
    uint32_t i = 0;
    
    if(NULL == buf)
    {
        return -1;
    }

    for(i=0; i<len; i++)
    {
          LL_USART_TransmitData8(USART2, (uint8_t) buf[i]);
          while(LL_USART_IsActiveFlag_TXE(USART2) == RESET)
          {
          }
     //   HAL_UART_Transmit_IT(&huart2, (uint8_t *)&buf[i], 1);
    //            while (huart2.gState != HAL_UART_STATE_READY);//Loop until the end of transmission

        if(i >=2 && buf[i] == 0xFF)
        {
                      LL_USART_TransmitData8(USART2, (uint8_t) crc[0]);
                      while(LL_USART_IsActiveFlag_TXE(USART2) == RESET)
                      {
                      }
        //                HAL_UART_Transmit_IT(&huart2, (uint8_t *)&crc, 1);
        //                while (huart2.gState != HAL_UART_STATE_READY);//Loop until the end of transmission
        }
    }

#ifdef PROTOCOL_DEBUG
    GIZWITS_LOG("MCU2WiFi[%4d:%4d]: ", gizGetTimerCount(), len);
    for(i=0; i<len; i++)
    {
        GIZWITS_LOG("%02x ", buf[i]);

        if(i >=2 && buf[i] == 0xFF)
        {
            GIZWITS_LOG("%02x ", 0x55);
        }
    }
    GIZWITS_LOG("\n");
#endif
        
        return len;
}  

 

中断部分,将时基放进去 1MS 定时器里面 串口中断,其中一个用来设置一下复位,配网等等状态

 

/**
  * @brief This function handles TIM16 global interrupt.
  */
void TIM16_IRQHandler(void)
{
  /* USER CODE BEGIN TIM16_IRQn 0 */
    if (LL_TIM_IsActiveFlag_UPDATE(TIM16))
    {
        gizTimerMs();/* 时基 */
        LL_TIM_ClearFlag_UPDATE(TIM16);
    }
  /* USER CODE END TIM16_IRQn 0 */
  /* USER CODE BEGIN TIM16_IRQn 1 */

  /* USER CODE END TIM16_IRQn 1 */
}

/**
  * @brief This function handles USART1 global interrupt.
  */
void USART1_IRQHandler(void)
{
  /* USER CODE BEGIN USART1_IRQn 0 */
  /* ´®¿Ú1²âÊÔ */
  if(LL_USART_IsActiveFlag_RXNE(USART1))
  {
      uint8_t uart1_data = USART1->RDR;
      
      if( uart1_data == 1)
      {
          gizwitsSetMode(WIFI_SOFTAP_MODE);
      }
      else if( uart1_data ==2 )
      {
          gizwitsSetMode(WIFI_AIRLINK_MODE);
          
      }
      else if( uart1_data == 3 )
      {
          gizwitsSetMode(WIFI_RESET_MODE);
      }
      else if( uart1_data == 4 )
      {
          gizwitsSetMode(WIFI_PRODUCTION_TEST);
      }
      
      
  }
  /* USER CODE END USART1_IRQn 0 */
  /* USER CODE BEGIN USART1_IRQn 1 */

  /* USER CODE END USART1_IRQn 1 */
}

/**
  * @brief This function handles USART2 global interrupt.
  */
void USART2_IRQHandler(void)
{
  /* USER CODE BEGIN USART2_IRQn 0 */
  extern int32_t gizPutData(uint8_t *buf, uint32_t len);
  
  if(LL_USART_IsActiveFlag_RXNE(USART2))
  {
     /* 数据传入 */
      uint8_t uart2_data = 0;
      uart2_data = USART2->RDR;
      gizPutData((uint8_t *)&uart2_data, 1); 
      //USART1->TDR = USART2->RDR;
  }
  /* USER CODE END USART2_IRQn 0 */
  /* USER CODE BEGIN USART2_IRQn 1 */

  /* USER CODE END USART2_IRQn 1 */
}

 

 

 

主函数循环前初始化

  /* WIFI初始化 */
    userInit();
    gizwitsInit();

 

主函数循环调用


      gizwitsHandle((dataPoint_t *)&currentDataPoint);/* 主处理逻辑,反复调用 */

 

posted @ 2020-08-03 17:36  XZHDJH  阅读(452)  评论(0)    收藏  举报