⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 otapmodule.c

📁 采用MC13213做的在SMAC协议基础上的发送程序,可作为点对点实验中的END DEVICE.
💻 C
📖 第 1 页 / 共 2 页
字号:
  {
     OTAPScheduleFlashContextChange();
     OTAPFlashExecute();
  }
  return;
#endif
  if (gu8OTAPRxState == OFF)
  {
    (void) MLMERXEnableRequest(&gsRxOtapPacket, OTAP_TIMEOUT); /* Enable RX only if it is off */
    gu8OTAPRxState = ON;
  }
}

/*Name: OTAPRxOff
  Desc: Turns the receiver off
  Para: void
  returns: void*/
void OTAPRxOff(void)
{
  (void) MLMERXDisableRequest(); /* Disable RX */
  gu8OTAPRxState = OFF;
}

/*Name: OTAPTxOn
  Desc: Transmits the last configured packet with the packet type requested
  Para: PacketType: Type of packet to be transmitted
  returns: void*/
void OTAPTxOn(UINT8 *Response)
{
  UINT8 i = 0, pt = 0, a = 0, n = 0;
  if (*Response == OTAP_NORESPONSE)
    return;
  if (gu8OTAPRxState == OFF)
  {
    
    if (*Response == OTAP_ACK_WACK)
    {
      pt = 'W';
      a  = 'A';
      n  = 'C';
    } else if (*Response == OTAP_ACK_WNAK)
    {
      pt = 'W';
      a  = 'N';
      n  = 'A';
    } else if (*Response == OTAP_ACK_SACK)
    {
      pt = 'S';
      a  = 'A';
      n  = 'C';
    } else if (*Response == OTAP_ACK_SNAK)
    {
      pt = 'S';
      a  = 'N';
      n  = 'A';
    } else if (*Response == OTAP_ACK_DACK)
    {
      pt = 'D';
      a  = 'A';
      n  = 'C';
    } else if (*Response == OTAP_ACK_DNAK)
    {
      pt = 'D';
      a  = 'N';
      n  = 'A';
    } else if (*Response == OTAP_ACK_EACK)
    {
      pt = 'E';
      a  = 'A';
      n  = 'C';
    } else if (*Response == OTAP_ACK_ENAK)
    {
      pt = 'E';
      a  = 'N';
      n  = 'A';
    } else if (*Response == OTAP_ACK_CACK)
    {
      pt = 'C';
      a  = 'A';
      n  = 'C';
    } else if (*Response == OTAP_ACK_CNAK)
    {
      pt = 'C';
      a  = 'N';
      n  = 'A';
    } else 
    {
      return;
    }

    gsTxOtapPacket.pu8Data[0]  = 'O';
    gsTxOtapPacket.pu8Data[1]  = 'T';
    gsTxOtapPacket.pu8Data[2]  = 'A';
    gsTxOtapPacket.pu8Data[3]  = 'P';
    gsTxOtapPacket.pu8Data[4]  = '-';
    gsTxOtapPacket.pu8Data[5]  = pt;
    gsTxOtapPacket.pu8Data[6]  = a;
    gsTxOtapPacket.pu8Data[7]  = n;
    gsTxOtapPacket.pu8Data[8]  = 'K';
    gsTxOtapPacket.pu8Data[9]  = '-';

    (void) MCPSDataRequest(&gsTxOtapPacket); /* Transmit packet only if RX is off */
    for(i = 0; i < MAX_TX_SIZE; i++)
      gsTxOtapPacket.pu8Data[i] = 0;
    *Response = OTAP_NORESPONSE;
  }
}

/*Name: OTAPFindReqProcess
  Desc: Determines if WREQ included this device's address in the 
        shut-up list.  If not, it creates the TX packet to be transmitted
        in the next state of the main state machine.
  Para: void
  returns: void*/
void OTAPFindReqProcess(void){
  UINT8 i = 0;
  UINT8 flag = 0;
  UINT8 *pData = &gau8RxOtapDataBuffer[10]; /* RX buffer, payload section */
  
  while (i < (gsRxOtapPacket.u8DataLength-10))
  {
    if (MY_MAC_ADDR[i%8] == pData[i])
      flag++;
    else 
      flag = 0;
    
    if (flag == 0){
      i = ((i>>3) + 1)<<3;
      flag = 0;
    } else
      i++;
    if (flag == 8)
      break;
  }
  
  if (flag != 8)
  {
    gau8TxOtapDataBuffer[0]  = 'O';
    gau8TxOtapDataBuffer[1]  = 'T';
    gau8TxOtapDataBuffer[2]  = 'A';
    gau8TxOtapDataBuffer[3]  = 'P';
    gau8TxOtapDataBuffer[4]  = '-';
    gau8TxOtapDataBuffer[5]  = 'W';
    gau8TxOtapDataBuffer[6]  = 'A';
    gau8TxOtapDataBuffer[7]  = 'C';
    gau8TxOtapDataBuffer[8]  = 'K';
    gau8TxOtapDataBuffer[9]  = '-';
    gau8TxOtapDataBuffer[10] = MY_MAC_ADDR[0];
    gau8TxOtapDataBuffer[11] = MY_MAC_ADDR[1];
    gau8TxOtapDataBuffer[12] = MY_MAC_ADDR[2];
    gau8TxOtapDataBuffer[13] = MY_MAC_ADDR[3];
    gau8TxOtapDataBuffer[14] = MY_MAC_ADDR[4];
    gau8TxOtapDataBuffer[15] = MY_MAC_ADDR[5];
    gau8TxOtapDataBuffer[16] = MY_MAC_ADDR[6];
    gau8TxOtapDataBuffer[17] = MY_MAC_ADDR[7];
    gau8TxOtapDataBuffer[18] = 0;
    gsTxOtapPacket.u8DataLength = 18;
    gu8Response = OTAP_ACK_WACK;
    return;
  }
    gu8Response = OTAP_NORESPONSE;
}


/*Name: OTAPClearEvents
  Desc: Clears the Events in the variable.
        This version can only have one event active.
  Para: Pointer to the Variable that holds an event.
  returns: void*/
void OTAPClearEvents(UINT8 *pu8Data)
{
#ifndef OTAP_DEBUG
  *pu8Data = OTAP_NOEVENT;
#endif  
  return;
}


/* INCOMPLETE
Name: OTAPDataRequestOK
  Desc: Validates the data in the global data pointer
  Para: void
  returns: UINT8:
          TRUE:  Data Valid
          FALSE: Data Invalid*/
UINT8 OTAPDataRequestOK(void)
{
  UINT16 dsn = 0x0000;
  dsn = (UINT16) (gau8RxOtapDataBuffer[18]<<8) | (gau8RxOtapDataBuffer[19]);
  if (gsOTAP.u8NextACK)
  {
    if ((gsOTAP.u16CurrDSN-1) == dsn) 
    {
      /* Data Valid: Write received data to flash */
      OTAPScheduleFlashWrite(&gau8RxOtapDataBuffer[20], OTAP_BLOCKSIZE);
      gsOTAP.u16CurrDSN--;
    }
  }
  return gsOTAP.u8NextACK;
}

/*Name: OTAPValidateImage_OK
  Desc: Validates the data in the image region of flash
  Para: void
  returns: UINT8:
          TRUE:  Data Valid
          FALSE: Data Invalid*/
UINT8 OTAPValidateImage_OK(void)
{
  
  return FImageValidation();
}

/*Name: OTAPContextChange
  Desc: Schedules a context change in the flash library
  Para: void
  returns: void*/
void OTAPScheduleFlashContextChange(void)
{
  if (gu8FlashReq == OTAP_FLASH_IDLE)
    gu8FlashReq = OTAP_FLASH_CONTEXT_CHANGE;
  return;
}

/* INCOMPLETE
Name: OTAPReset
  Desc: Validates the data in the global data pointer
  Para: void
  returns: UINT8:
          TRUE:  Data Valid
          FALSE: Data Invalid*/
void OTAPReset(void)
{
  (void)gpu8OTAPData;
  for(;;)
    ;
  //return TRUE;
}

/*Name: OTAPStartProcessEvent
  Desc: Determines if a new event is available.
  Para: Pointer to the Event pointer.
  returns: UINT8:
          TRUE:  There is an event waiting, process can start
          FALSE: There is not an event waiting, process cannot start*/
UINT8 OTAPStartProcessEvent(UINT8 *Event)
{
  if (*Event == OTAP_NOEVENT ||
      *Event == OTAP_RX_TIMEOUT)
    return FALSE;/* Do not start processing events, there is none*/
  else
    return TRUE;  /* Start processing of event */
}

/*Name: OTAPValidateStartRequestOK
  Desc: Determines if the SREQ command received is valid for this device
  Para: void
  returns: UINT8:
          TRUE:  Valid SREQ
          FALSE: Invalid SREQ*/
UINT8 OTAPValidateStartRequestOK(void)
{
  tSREQ *sSREQ; /* Pointer used for formating of incoming SREQ */
  UINT8 ret = TRUE;
  UINT8 i = 0;
  /* Format received packet for further processing*/
  sSREQ = (tSREQ *) &gau8RxOtapDataBuffer[18];
  
  /* Evaluation of the specific parameters*/
  if (sSREQ->u16Size > OTAP_IMAGE_MAX_SIZE)
  {
    ret = FALSE;
  } else
  {
    if(sSREQ->Flags.u1NVMOverwrite)
      gsOTAP.u8NVMOverwrite = TRUE;
    else
      gsOTAP.u8NVMOverwrite = FALSE;
    
    gsOTAP.u16ImageSize = (sSREQ->u16Size)*OTAP_BLOCKSIZE;
    gsOTAP.u16CurrDSN = sSREQ->u16Size;

    for (i = 0; i < 16; i++)
      gsOTAP.au8ImageName[i] = sSREQ->au8ImageName[i];
    
    gsOTAP.u8LRC = 0;
    
    #pragma MESSAGE DISABLE C2705
    #pragma MESSAGE DISABLE C5900
        
    gau8TxOtapDataBuffer[10] = (UINT8)((UINT32)OTAP_DELAY >> 24);
    gau8TxOtapDataBuffer[11] = (UINT8)((UINT32)OTAP_DELAY >> 16);
    gau8TxOtapDataBuffer[12] = (UINT8)((UINT32)OTAP_DELAY >> 8);
    gau8TxOtapDataBuffer[13] = (UINT8)((UINT32)OTAP_DELAY);
    
    gau8TxOtapDataBuffer[14] = (UINT8)((UINT32)OTAP_ID >> 24);
    gau8TxOtapDataBuffer[15] = (UINT8)((UINT32)OTAP_ID >> 16);
    gau8TxOtapDataBuffer[16] = (UINT8)((UINT32)OTAP_ID >> 8);
    gau8TxOtapDataBuffer[17] = (UINT8)((UINT32)OTAP_ID);

    #pragma MESSAGE WARNING C2705


    
    gau8TxOtapDataBuffer[24] = (UINT8)((UINT32)OTAP_BLOCKSIZE);
    
    gsTxOtapPacket.u8DataLength = 25;
  }
  
  return ret;
}

/*Name: OTAPScheduleFlashWrite
  Desc: Schedules a FlashWrite
  Para: pData: pointer to the data buffer to write
        len: length of the data buffer to write
  returns: void*/
void OTAPScheduleFlashWrite(UINT8 * pData, UINT8 len)
{
  UINT8 i;
  if(gu8FlashReq != OTAP_FLASH_IDLE)
  {
    return;
  } else 
  {
    gpu8OTAPData = &pData[0];
    gu8OTAPDataLen = len;
    /* Get new LRC value */
    for (i=0; i<len; i++)
      gsOTAP.u8LRC ^= gpu8OTAPData[i];
      gu8FlashReq = OTAP_FLASH_WRITE;

  }
  
}

/*Name: OTAPScheduleFlashErase
  Desc: Schedules a FlashErase
  Para: void
  returns: void*/
void OTAPScheduleFlashEraseImage(void)
{
  //UINT8 i;
  if(gu8FlashReq != OTAP_FLASH_IDLE)
  {
    return;
  } else 
  {
    gu8FlashReq = OTAP_FLASH_ERASE_IMAGE;
  }
  
}


void OTAPFlashExecute(void)
{
  UINT8 i=0,o=0, ret=F_OK;
  if(gu8FlashReq == OTAP_FLASH_WRITE)
  {  DisableInterrupts;
    /* Programming */
//    SCITransmitStr("\rFlash write");

    while(i < gu8OTAPDataLen &&
          ret == F_OK        &&
          gsOTAP.u16BytesWriten <= (gsOTAP.u16ImageSize+1))
    {
      ret = FWriteByte(gpu8OTAPData[i]);
//      SCIStartTransmit(gpu8OTAPData[i]);
      gsOTAP.u16BytesWriten++;
      i++;
    }
    if (gsOTAP.u16BytesWriten > (gsOTAP.u16ImageSize+1))
      ret = F_ERR;
     EnableInterrupts;
  } else   if(gu8FlashReq == OTAP_FLASH_ERASE_IMAGE)
  { DisableInterrupts;
    /* Erasing Image */
    (void) FSetSize(gsOTAP.u16ImageSize);
    ret = FEraseImage();
//    SCITransmitStr("\rFlash erase");
    EnableInterrupts;
  } else   if(gu8FlashReq == OTAP_FLASH_CONTEXT_CHANGE)
  {
    /* Context Change */
    DisableInterrupts;
    for(i=0;i++;i<100)
      for(o=0;o++;o<100);
    FContextChange();
    (void) OTAPReset();
  }


  if (ret == F_OK)
    gsOTAP.u8NextACK = TRUE;
  else
    gsOTAP.u8NextACK = FALSE;    
  
  gu8FlashReq = OTAP_FLASH_IDLE;
}




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -