Esempio n. 1
0
File: npi.c Progetto: XMingyu/BLE
/*******************************************************************************
 * @fn          NPI_WriteTransport
 *
 * @brief       This routine writes data from the buffer to the transport layer.
 *
 * input parameters
 *
 * @param       buf - Pointer to buffer to write data from.
 * @param       len - Number of bytes to write.
 *
 * output parameters
 *
 * @param       None.
 *
 * @return      Returns the number of bytes written to transport.
 */
uint16 NPI_WriteTransport( uint8 *buf, uint16 len )
{
#if 0
// amomcu 优化, 处理数据量较大时需要等待数据发送完毕再发送, 否则数据就会丢失
// 当然, 也可以增大发送缓冲区, 但是内存有限, 只能这么干了     
  uint16 ret = 0;
  // 该函数延时时间为1ms, 用示波器测量过, 稍有误差, 但误差很小  --amomcu.com
  extern void simpleBLE_Delay_1ms(int times);

  // 波特率越低, 延时要求越大, 想传输数据快,还是要115200波特率的 --amomcu.com
  // 这里不能延时太长时间, 否则会断开网络连接的   --amomcu.com
  for(uint16 i = 0; i < 100; i++)
  {
     ret =  HalUARTWrite( NPI_UART_PORT, buf, len );   
     if(ret == len)
     {
        break;
     }
     simpleBLE_Delay_1ms(10);
  }

  return ret;  
#else
  return( HalUARTWrite( NPI_UART_PORT, buf, len ) );
#endif
}
Esempio n. 2
0
/*********************************************************************
 * @fn      rxCB_Loopback
 *
 * @brief   Process UART Rx event handling.
 *          May be triggered by an Rx timer expiration - less than max
 *          Rx bytes have arrived within the Rx max age time.
 *          May be set by failure to alloc max Rx byte-buffer for the DMA Rx -
 *          system resources are too low, so set flow control?
 *
 * @param   none
 *
 * @return  none
 */
static void rxCB_Loopback( uint8 port, uint8 event )
{

  if ( rxLen )
  {
    if ( !HalUARTWrite( SERIAL_APP_PORT, rxBuf, rxLen ) )
    {
      osal_start_timerEx( SerialApp_TaskID, SERIALAPP_TX_RTRY_EVT,
                                            SERIALAPP_TX_RTRY_TIMEOUT );
      return;
    }
    else
    {
      osal_stop_timerEx( SerialApp_TaskID, SERIALAPP_TX_RTRY_EVT );
    }
  }

  // HAL UART Manager will turn flow control back on if it can after read.
  if ( !(rxLen = HalUARTRead( port, rxBuf, SERIAL_APP_RX_CNT )) )
  {
    return;
  }

  if ( HalUARTWrite( SERIAL_APP_PORT, rxBuf, rxLen ) )
  {
    rxLen = 0;
  }
  else
  {
    osal_start_timerEx( SerialApp_TaskID, SERIALAPP_TX_RTRY_EVT,
                                          SERIALAPP_TX_RTRY_TIMEOUT );
  }
}
Esempio n. 3
0
File: protocol.c Progetto: cuu/weiyi
void gateway_to_pda(uint16 shortaddr, uint8*buf, uint8 len)// 这儿的buf也 是一完整的协议data
{
   uint8*mbuf;
   uint8 i;
   mbuf = osal_mem_alloc( (len+4)*sizeof(uint8));
   
   mbuf[0] = len+3;
   mbuf[1] = 0x08;
   
   mbuf[2] = HI_UINT16(shortaddr);
   mbuf[3] = LO_UINT16(shortaddr);
   
   for( i = 0; i < len; i++)
   {
     mbuf[i+4] = buf[i];
   }
   
   // uart to send
   if( HalUARTWrite(0,mbuf, len+4) > 0)
   {
     osal_mem_free(mbuf);
   }else
   {
     HalUARTWrite(SER_PORT,mbuf, len+4);
     osal_mem_free(mbuf);
   }
   
   return;
}
Esempio n. 4
0
uint8 sendDataToHost(uint8* data, uint8 len)
{
    uint8* buf = osal_mem_alloc((3+len)*sizeof(uint8));
    if (buf)  //if allocated
    {
        buf[0] = SERIAL_MSG_START_ID;
        buf[1]= SERIAL_DATA;
        buf[2]= len;
        osal_memcpy(&buf[3], data, len);
        uint8 bytes_sent = HalUARTWrite(NPI_UART_PORT, (uint8*)buf, len+3);
        osal_mem_free(buf);
        if (bytes_sent == len + 3)
        {
            return SUCCESS; /*SUCCESS = 0*/
        }
        else
        {
            return 1;  //data not sent over UART
        }
    }
    else
    {
        return bleMemAllocError;
    }
}
Esempio n. 5
0
/*************************************************************************************************
 * @fn          sysPingRsp
 *
 * @brief       Build and send Ping response
 *
 * @param       none
 *
 * @return      none
**************************************************************************************************
 */
static void sysPingRsp(void)
{
    uint8 pingBuff[7];

    // Start of Frame Delimiter
    pingBuff[0] = 0xFE;

    // Length
    pingBuff[1] = 0x02;

    // Command type
    pingBuff[2] = LO_UINT16(0x0161);
    pingBuff[3] = HI_UINT16(0x0161);

    // Stack profile
    pingBuff[4] = LO_UINT16(0x0041);
    pingBuff[5] = HI_UINT16(0x0041);

    // Frame Check Sequence
    pingBuff[6] = calcFCS(&pingBuff[1], 5);


    HalUARTWrite(MHMS_PORT,pingBuff, 7);

}
Esempio n. 6
0
void writeUart(uint8 whichport, cmd_msg_t* command)
{
  if ( HalUARTWrite ( whichport, command->controlmsg, command->length))
  {
    HalLedBlink( HAL_LED_1, 2, 25, 50);
  }
}
/*********************************************************************
 * @fn		Monitor_SendTopologyInformation
 *
 * @brief	send the topology information to the the coordinator
 *
 *	| SOF  | LEN  |  CMD |  CMD2  | SRC  |  SRC |  DATA  | FCS  |
 */
static void Monitor_SendTopologyInformation()
{
  // Create frame buffer
  byte pFrame[11];
  // fill SOF
  pFrame[0] = 0xFE;
  // fill len
  pFrame[1] = 0x0004;
  // file CMD
  pFrame[2] = LO_UINT16(TOPOLOGY_CMD);
  pFrame[3] = HI_UINT16(TOPOLOGY_CMD);
  // file source address
  pFrame[4] = LO_UINT16(0);
  pFrame[5] = HI_UINT16(0);
  // fill type
  pFrame[6] = LO_UINT16(COORDINATOR);
  pFrame[7] = HI_UINT16(COORDINATOR);
  // file parent
  pFrame[8] = LO_UINT16(0);
  pFrame[9] = HI_UINT16(0);
  // fill fcs
  pFrame[10] = Monitor_CalcFCS((byte*)&pFrame[1], 9);
  // send out use uart
  HalUARTWrite(UART_PORT, pFrame, 11);
}
Esempio n. 8
0
/**************************************************************************************************
 * @fn          zapSBL_Rx
 *
 * @brief       This function drives an Rx from the ZNP. When the ZNP SBL is in "Rx mode" and has
 *              nothing to transmit, it will be transmitting only zeroes.
 *              So the expected simplistic lock-step sequence:
 *              MRDY Set - SRDY Set
 *              Rx an expected complete SBL message
 *              MRDY Clr - SRDY Clr
 *
 * input parameters
 *
 * None.
 *
 * output parameters
 *
 * None.
 *
 * @return      None.
 **************************************************************************************************
 */
static void zapSBL_Rx(void)
{
  uint8 len, err = TRUE;

  zapSBL_GetSRDY();
  osal_memset(zapSBL_Buf, 0, 2);

  HalSpiWrite(zapAppPort, zapSBL_Buf, 1);

  if (zapSBL_Buf[0] == SB_SOF)
  {
    HalSpiWrite(zapAppPort, zapSBL_Buf+1, 1);
    if ((len = zapSBL_Buf[1]) <= SB_BUF_SIZE-SB_FCS_STATE)
    {
      HalSpiWrite(zapAppPort, zapSBL_Buf+2, len+3);  // CMD1/2 & FCS.
      err = FALSE;
    }

#if ZAP_SBL_EXT_EXEC
    (void)HalUARTWrite(ZAP_SBL_EXT_PORT, zapSBL_Buf, len+SB_FCS_STATE);  // SOF, LEN, CMD1/2 & FCS.
#else
#error Need to parse the response and act accordingly.
#endif
  }

  HAL_ZNP_MRDY_CLR();
  if (err)
  {
    zapSBL_ResetZNP();
  }
}
Esempio n. 9
0
void zclHomelink_UARTCallback(uint8 port, uint8 event)
{
  uint8 ch;
  while (Hal_UART_RxBufLen(port))
  {
    HalUARTRead (port, &ch, 1);
  }
  HalUARTWrite(HAL_UART_PORT_0, (uint8 *)"\r", 1);
}
Esempio n. 10
0
File: protocol.c Progetto: cuu/weiyi
static void check_alive(void)
{
  uint8 tmp[2];
  tmp[0] = 0x01;
  tmp[1] = 0x90;
  
  /// serial port send back ,tmp[2];
  // or wireless
  HalUARTWrite(SER_PORT,tmp, 2);
  
}
Esempio n. 11
0
/*********************************************************************
 * @fn      SampleApp_SendPeriodicMessage
 *
 * @brief   Send the periodic message.
 *
 * @param   none
 *
 * @return  none
 */
void SampleApp_SendPeriodicMessage( void )
{
  uchar data[] = "From Ep\n"; 
  if ( AF_DataRequest( &SampleApp_Periodic_DstAddr, &SampleApp_epDesc,
                       SAMPLEAPP_PERIODIC_CLUSTERID,
                       sizeof(data),
                       data,
                       &SampleApp_TransID,
                       AF_DISCV_ROUTE,
                       AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
  {
    HalUARTWrite(0, "In Ep SampleApp_SendPeriodMessage : Succeed\n", sizeof("In Ep SampleApp_SendPeriodMessage : Succeed\n")-1);
    HalUARTWrite(0, data, sizeof(data));
  }
  else
  {
    // Error occurred in request to send.
    HalUARTWrite(0, "In Ep SampleApp_SendPeriodMessage : Failed\n", sizeof("In Ep SampleApp_SendPeriodMessage : Failed\n")-1);
  }
}
Esempio n. 12
0
static void LCD_SendOneMessage( ){
  // just return
  if(LCD_IsMessageEmpty() == 0x01){
	return;
  }	
  // pop a message from the message array
  MessageNode* item = LCD_PopMessage();
  // send
  HalUARTWrite(UART_PORT, item->data, item->len);
  // free it after send
  osal_mem_free((MessageNode*)item);
}
Esempio n. 13
0
/////////JUST FOR TEST//////////////begin/////////////////
void sendData(U8* bin,U16 size)
{
    static int flag = 0;
    if(flag){
      P1 &= ~0x02;
      flag = 0;
    }else{
      flag = 1;
      P1 |= 0x02;
    } 
    HalUARTWrite(HAL_UART_PORT_0,bin,size);
}
Esempio n. 14
0
void SampleApp_SendPointMessage(void)
{
  HalUARTWrite(0, "PointMsg\n", sizeof("PointMsg\n")-1);
  uint8 T[5];
  Temp_test();
  if (temp > 27)
    LED1 = !LED1;
  T[0] = temp / 10 + 48;
  T[1] = temp % 10 + 48;
  T[2] = ' ';
  T[3] = 'C';
  T[4] = '\0';
  HalUARTWrite(0, "temp=", 5);
  HalUARTWrite(0, T, 2);
  HalUARTWrite(0, "\n", 1);
  if ( AF_DataRequest( &SampleApp_Point_DstAddr, &SampleApp_epDesc,
                       SAMPLEAPP_POINT_CLUSTERID,
                       5,
                       T,
                       &SampleApp_TransID,
                       AF_DISCV_ROUTE,
                       AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
  {
    HalUARTWrite(0, "PointMsg Success\n", sizeof("PointMsg Success\n")-1);
  }
  else
  {
    // Error occurred in request to send.
    HalUARTWrite(0, "PointMsg Failed\n", sizeof("PointMsg Failed\n")-1);
  }
  
}
Esempio n. 15
0
/*********************************************************************
 * @fn      SerialApp_ProcessZDOMsgs()
 *
 * @brief   Process response messages
 *
 * @param   none
 *
 * @return  none
 */
static void SerialApp_ProcessZDOMsgs( zdoIncomingMsg_t *inMsg )
{
  switch ( inMsg->clusterID )
  {
    case End_Device_Bind_rsp:
      if ( ZDO_ParseBindRsp( inMsg ) == ZSuccess )
      {
        // Light LED
        HalLedSet( HAL_LED_1, HAL_LED_MODE_ON );
        osal_stop_timerEx(SerialApp_TaskID,SERIALAPP_MSG_AUTOMATCH);
             
      }
#if defined(BLINK_LEDS)
      else
      {
        // Flash LED to show failure
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_FLASH );
      }
#endif
      break;

    case Match_Desc_rsp:
      {
        ZDO_ActiveEndpointRsp_t *pRsp = ZDO_ParseEPListRsp( inMsg );
        if ( pRsp )
        {
          if ( pRsp->status == ZSuccess && pRsp->cnt  
               && SerialApp_DstAddr.addrMode == afAddrNotPresent
                )
          {
            osal_stop_timerEx(SerialApp_TaskID,SERIALAPP_MSG_AUTOMATCH);
            SerialApp_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;
#if defined(ZDO_COORDINATOR)          
            SerialApp_DstAddr.addr.shortAddr = pRsp->nwkAddr;
#else
            SerialApp_DstAddr.addr.shortAddr = NWK_BROADCAST_SHORTADDR_DEVZCZR;
#endif            
            // Take the first endpoint, Can be changed to search through endpoints
            SerialApp_DstAddr.endPoint = pRsp->epList[0];
            // Light LED
            HalLedSet( HAL_LED_2, HAL_LED_MODE_ON );
            
#if defined(ZDO_COORDINATOR)
            HalUARTWrite(SERIAL_APP_PORT,"OK\n",3);
#endif 
          }
          osal_mem_free( pRsp );
        }
      }
      break;
  }
}
Esempio n. 16
0
/**************************************************************************************************
 * @fn          zapSBL_RxExt
 *
 * @brief       This function is the registered callback for the UART to the external application
 *              that is driving the serial boot load to the ZNP.
 *
 * input parameters
 *
 * @param       port - Don't care.
 * @param       event - Don't care.
 *
 * output parameters
 *
 * None.
 *
 * @return      None.
 **************************************************************************************************
 */
static void zapSBL_RxExt(uint8 port, uint8 event)
{
  uint8 ch;

  (void)port;
  (void)event;

  // Use external UART Rx as the trigger to start an SBL session.
  if (!zapSBL_Active)
  {
    // Usurp and save to restore the currently registered UART transport callback.
    usurpedCB = uartRecord.callBackFunc;
    uartRecord.callBackFunc = zapSBL_Rx;

    znpSystemReset(ZNP_RESET_SOFT);
    HalBoardDelay(1000, TRUE);

    ch = SB_FORCE_BOOT;
    (void)HalUARTWrite(ZAP_SBL_ZNP_PORT, &ch, 1);
    (void)HalUARTWrite(ZAP_SBL_ZNP_PORT, &ch, 1);

    zapSBL_Active = TRUE;

    if (ZSuccess != osal_start_timerEx(zapSBL_TaskId, ZAP_SBL_EVT_EXIT, ZAP_SBL_DLY_EXIT))
    {
      (void)osal_set_event(zapSBL_TaskId, ZAP_SBL_EVT_EXIT);
    }
  }

  while (HalUARTRead(ZAP_SBL_EXT_PORT, &ch, 1))
  {
    (void)HalUARTWrite(ZAP_SBL_ZNP_PORT, &ch, 1);

    if (ZSuccess != osal_start_timerEx(zapSBL_TaskId, ZAP_SBL_EVT_EXIT, ZAP_SBL_DLY_EXIT))
    {
      (void)osal_set_event(zapSBL_TaskId, ZAP_SBL_EVT_EXIT);
    }
  }
}
Esempio n. 17
0
/*********************************************************************
 * @fn      uApp_HandleKeys
 *
 * @brief   Handles all key events for this device.
 *
 * @param   port - where the Rx data
 *          event - type of event
 *                  HAL_UART_RX_FULL
 *                  HAL_UART_RX_ABOUT_FULL
 *                  HAL_UART_RX_TIMEOUT      
 *                  HAL_UART_TX_FULL         
 *                  HAL_UART_TX_EMPTY        
 *
 * @return  none
 *********************************************************************/
void uApp_UartProcessRxData ( uint8 port, uint8 events )
{
    uint8 rxData[10] = {'\n'};
    uint16 totaluint8Read = 0;

	if (events & (HAL_UART_RX_FULL | HAL_UART_RX_ABOUT_FULL | HAL_UART_RX_TIMEOUT))
	{
        DebugMsg("Received UART Event:", events);
        totaluint8Read = HalUARTRead(0, rxData, 10);
        HalUARTWrite(0, rxData, totaluint8Read);
	}


}
Esempio n. 18
0
void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
{
  //uint16 shortAddr;
  //uint8 data[4] = {'F','U','C','K'} ;
 
  switch ( pkt->clusterId )
  {
    case SAMPLEAPP_PERIODIC_CLUSTERID:
 
      break;
    case SAMPLEAPP_FLASH_CLUSTERID:
 //if coordinator  
      HalUARTWrite(SERIAL_APP_PORT, pkt->cmd.Data, pkt->cmd.DataLength); //�p�G�����ʥ]�A�e�����F�赹console�h����
      break;  
  }
}
Esempio n. 19
0
/*********************************************************************
 * @fn      SampleApp_MessageMSGCB
 *
 * @brief   Data message processor callback.  This function processes
 *          any incoming data - probably from other devices.  So, based
 *          on cluster ID, perform the intended action.
 *
 * @param   none
 *
 * @return  none
 */
void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
{
  uint16 flashTime;

  switch ( pkt->clusterId )
  {
    case SAMPLEAPP_PERIODIC_CLUSTERID:
      HalUARTWrite(0, "In MSGCB\n", sizeof("In MSGCB")-1);
      break;

    case SAMPLEAPP_FLASH_CLUSTERID:
      flashTime = BUILD_UINT16(pkt->cmd.Data[1], pkt->cmd.Data[2] );
      HalLedBlink( HAL_LED_4, 4, 50, (flashTime / 4) );
      break;
  }
}
Esempio n. 20
0
uint8 sendAckMessage(uint8 bytes_sent)
{
    uint8 data[3] = {0};

    data[0]= SERIAL_MSG_START_ID;
    data[1]= SERIAL_ACK;
    data[2]= bytes_sent;
    uint8 success_len = HalUARTWrite(NPI_UART_PORT, (uint8*)data, 3);
    if (success_len == 3)
    {
        return SUCCESS;
    }
    else
    {
        return 1;   //ack wasn't sent over UAR
    }
}
Esempio n. 21
0
void CommandToBox(uint8 cmd)
{
    uint8 data[7];//format {0xAA, 0x75, 0x14, 0x00, 0x00 ,0x00, 0xCB};
    if (cmd==0)
    {
      reset();
    }
    data[0]=0xAA;
    data[1]=0x75;
    data[6]=data[0]^data[1];
    data[2]=cmd;
    data[6]=data[6]^data[2];
    data[3]=0;
    data[4]=0;
    data[5]=0;
    HalUARTWrite(SERIAL_APP_PORT,(uint8*)data,7);
}
Esempio n. 22
0
void calcCRC16( uint8* data, uint8 len)
{
  uint8 *pData = osal_mem_alloc( len);
  if( pData)
    osal_memcpy( pData, data, len);

  pData[rssi_send_hi] = 255;
  pData[rssi_send_lo] = modbus_rssi;
  initCRC16();
  for( uint8 i = 0; i<(len-2); i++)
  {
    CRC16_byte(pData[i]);
  }
  pData[len-2] = CRChi;
  pData[len-1] = CRClo;

  HalUARTWrite( 0, pData, len);
  osal_mem_free( pData);
}
Esempio n. 23
0
/**************************************************************************************************
 * @fn          zapSBL_Rx
 *
 * @brief       This function is temporarily set as the registered callback for the UART to the ZNP
 *              in order to complete a serial boot load.
 *
 * input parameters
 *
 * @param       port - Don't care.
 * @param       event - Don't care.
 *
 * output parameters
 *
 * None.
 *
 * @return      None.
 **************************************************************************************************
 */
static void zapSBL_Rx(uint8 port, uint8 event)
{
  uint8 ch;

  (void)port;
  (void)event;

  while (HalUARTRead(ZAP_SBL_ZNP_PORT, &ch, 1))
  {
    (void)HalUARTWrite(ZAP_SBL_EXT_PORT, &ch, 1);
  }

  /*
   * TODO: Need to design a way to terminate the SBL session and return back to normal ZAP
   *       operations. Among possible other things, need to restore the usurped UART callback.
   */
  //uartRecord.callBackFunc = usurpedCB;
  //zapSBL_Active = FALSE;
}
Esempio n. 24
0
File: protocol.c Progetto: cuu/weiyi
static void reboot_zigbee(uint8*buf)
{
  uint8 opera_bit;
  uint8 tmp[4];
  
  opera_bit = buf[1];
  
  switch(opera_bit)
  {
  case 0x01://表示清空 NV_RESTORE 保存的内容,然后系统重启
    
    SystemReset();
    break;
  case 0x02://表示清空 NV_RESTORE 保存的内容,将网关设置为“协调器”,然后系统重启
      
      set_coordi();
      SystemReset();
    break;
  case 0x03://表示清空 NV_RESTORE 保存的内容,将网关设置为“路由”,然后系统重启
      set_router();
      SystemReset();
    break;
  case 0x04://表示写模块panid;后面跟panid参数;
            //04     A8    04      yy          xx 
            // 长度  命令  写指令 panid高字节 panid低字节
      
    break;
  case 0x05:
        //03    95       yy    xx
        tmp[0] = 0x03;
        tmp[1] = 0x95;
        tmp[2] = HI_UINT16(_NIB.nwkPanId);
        tmp[3] = LO_UINT16(_NIB.nwkPanId);
        
        HalUARTWrite(SER_PORT,tmp, 4);
    break;
  default:break;
  
  }
}
Esempio n. 25
0
/*********************************************************************
 * @fn      SampleApp_HandleKeys
 *
 * @brief   Handles all key events for this device.
 *
 * @param   shift - true if in shift/alt.
 * @param   keys - bit field for key events. Valid entries:
 *                 HAL_KEY_SW_2
 *                 HAL_KEY_SW_1
 *
 * @return  none
 */
void SampleApp_HandleKeys( uint8 shift, uint8 keys )
{
  (void)shift;  // Intentionally unreferenced parameter
  
  if ( keys & HAL_KEY_SW_1 )
  {
    /* This key sends the Flash Command is sent to Group 1.
     * This device will not receive the Flash Command from this
     * device (even if it belongs to group 1).
     */
    SampleApp_SendFlashMessage( SAMPLEAPP_FLASH_DURATION );
  }

  if ( keys & HAL_KEY_SW_2 )
  {
    /* The Flashr Command is sent to Group 1.
     * This key toggles this device in and out of group 1.
     * If this device doesn't belong to group 1, this application
     * will not receive the Flash command sent to group 1.
     */
    aps_Group_t *grp;
    grp = aps_FindGroup( SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP );
    if ( grp )
    {
      // Remove from the group
      aps_RemoveGroup( SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP );
    }
    else
    {
      // Add to the flash group
      aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group );
    }
  }
  
  if (keys &  HAL_KEY_SW_6)
  {
    HalUARTWrite(0, "In Handlekey\n", sizeof("In HandleKey\n")-1);
    HalLedBlink(HAL_LED_1, 2, 50, 500);
  }
}
Esempio n. 26
0
/**
 * @brief   Envía un mensaje por UART según el protocolo implementado. Este se
 *          crea a partir de un mensaje y otros datos (genéricos) pasados como
 *          argumento. Para el mensaje de tipo REPORT, los otros datos
 *          corresponden a los flags de alarmas, y son añadidos al mensaje antes
 *          de los parámetros eléctricos.
 *          Los mensajes tienen el siguiente formato:
 *              HEADER                                     DATA
 *              SOF | DATA LENGTH | COMMAND | SENSOR MAC | DATA[DATA LENGTH]
 *          Donde SOF=0xFE es el delimitador del inicio del frame; DATA LENGTH 
 *          es la cantidad de bytes de datos envíados luego del HEADER; COMMAND
 *          es el comando enviado a la PC (REPORT, etc.); SENSOR MAC es la 
 *          dirección MAC de 64 bytes del nodo al cual se refiere la información;
 *          y DATA corresponde a los datos enviados en formato Little Endian.
 *
 * @param   msg     Mensaje que se desea retransmitir por UART.
 * @param   other   Otros datos para añadir al mensaje.
 */
static void sendUartMessage(struct message_t *msg, void *other)
{
    uint8 pFrame[UART_FRAME_MAX_LENGTH];

    // start of frame
    pFrame[UART_FRAME_SOF_OFFSET] = CPT_SOP;
    // tipo de mensaje
    pFrame[UART_FRAME_CMD_OFFSET] = msg->msgType;
    // MAC
    memcpy(&(pFrame[UART_FRAME_MAC_OFFSET]), &(msg->mac), sizeof(msg->mac));
    
    if (msg->msgType == MSG_TYPE_REPORT)
    {
        // length
        pFrame[UART_FRAME_LENGTH_OFFSET] = UART_FRAME_REPORT_LENGTH;
        
        // secuencia
        pFrame[UART_FRAME_DATA_OFFSET] = LO_UINT16(msg->sequence);
        pFrame[UART_FRAME_DATA_OFFSET+1] = HI_UINT16(msg->sequence);
        // flags alarmas
        uint16 flags = *((uint16*)other);
        pFrame[UART_FRAME_DATA_OFFSET+2] = LO_UINT16(flags);
        pFrame[UART_FRAME_DATA_OFFSET+3] = HI_UINT16(flags);
        // parámetros eléctricos
        memcpy(&(pFrame[UART_FRAME_DATA_OFFSET+4]), &(msg->data), msg->lenData);
    }
    else
    {
        // otros tipos de mensaje
        pFrame[UART_FRAME_LENGTH_OFFSET] = 0;
    }
    
    // escribe mensaje en UART
    uint16 b = HalUARTWrite(HAL_UART_PORT_0, pFrame,
                 UART_FRAME_HEADER_LENGTH+pFrame[UART_FRAME_LENGTH_OFFSET]);
    if (b > 0)
        HalLcdWriteStringValue("UART", b, 10, 3);
    else
        HalLcdWriteString("", HAL_LCD_LINE_3);
}
Esempio n. 27
0
/***************************************************************************************************
 * @fn      MT_TransportSend
 *
 * @brief   Fill in SOP and FCS then send out the msg
 *
 * @param   uint8 *pBuf - pointer to the message that contains CMD, length, data and FCS
 *
 * @return  None
 ***************************************************************************************************/
void MT_TransportSend(uint8 *pBuf)
{
  uint8 *msgPtr;
  uint8 dataLen = pBuf[0]; /* Data length is on byte #1 from the pointer */

  /* Move back to the SOP */
  msgPtr = pBuf-1;

  /* Insert SOP */
  msgPtr[0] = MT_UART_SOF;

  /* Insert FCS */
  msgPtr[SPI_0DATA_MSG_LEN - 1 + dataLen] = MT_UartCalcFCS (pBuf, (3 + dataLen));

  /* Send to UART */
#ifdef MT_UART_DEFAULT_PORT
  HalUARTWrite(MT_UART_DEFAULT_PORT, msgPtr, dataLen + SPI_0DATA_MSG_LEN);
#endif

  /* Deallocate */
  osal_msg_deallocate(msgPtr);
}
//static uint8 data_len = 0, exsit_data_len = 0, send_times = 0, send_len = 0;
//#define one_time_data_len 125
void SbpHalUARTWrite(uint8 *pBuffer, uint16 length) {

//	data_len = osal_strlen(pBuffer);
//
//	do {
//
//		if(Hal_UART_TxBufLen() > 0){
//			continue;
//		}
//
//		if (data_len <= one_time_data_len) {
//			HalUARTWrite(SBP_UART_PORT, pBuffer, length);
//			exsit_data_len = 0;
//
//		} else if (exsit_data_len == 0 || exsit_data_len >= one_time_data_len) {
//			exsit_data_len = data_len - one_time_data_len;
//			HalUARTWrite(SBP_UART_PORT, (pBuffer + (send_times * one_time_data_len)), one_time_data_len);
//
//		} else if (exsit_data_len > 0 && exsit_data_len < one_time_data_len) {
//			HalUARTWrite(SBP_UART_PORT, (pBuffer + (send_times * one_time_data_len)), exsit_data_len);
//			exsit_data_len = 0;
//		} else {
//		}
//
//		send_times++;
//
//	} while (exsit_data_len > 0);
//
//	data_len = 0;
//	send_len = 0;
//	exsit_data_len = 0;
//	send_times = 0;

	//do {
	//	UART_HAL_DELAY(10);
//	} while (Hal_UART_TxBufLen() > 0);

	HalUARTWrite(SBP_UART_PORT, pBuffer, length);
}
Esempio n. 29
0
/**************************************************************************************************
 * @fn          sblResp
 *
 * @brief       Make the SB response.
 *
 * input parameters
 *
 * None.
 *
 * output parameters
 *
 * None.
 *
 * @return      TRUE if the downloaded code has been enabled; FALSE otherwise.
 */
static uint8 sblResp(void)
{
  uint8 fcs = 0, len = sbBuf[RPC_POS_LEN] + RPC_FRAME_HDR_SZ;
  uint8 rtrn = FALSE;

  for (uint8 idx = RPC_POS_LEN; idx < len; idx++)
  {
    fcs ^= sbBuf[idx];
  }
  sbBuf[len] = fcs;

  if ((sbBuf[RPC_POS_CMD1] == (SBL_ENABLE_CMD | SBL_RSP_MASK)) &&
      (sbBuf[RPC_POS_DAT0] == SBL_SUCCESS))
  {
    len++;  // Send an extra garbage byte to flush the last good one before resetting.
    rtrn = TRUE;
  }

  rpcBuf[0] = RPC_UART_SOF;
  (void)HalUARTWrite(0, rpcBuf, len + RPC_UART_FRAME_OVHD);

  return rtrn;
}
Esempio n. 30
0
void SendUART(uint8 *data,uint8 len)
{
  HalUARTWrite(HAL_UART_PORT_0, data, len);
}