Ejemplo n.º 1
0
/*****************************************************************************
* I2C_ZtcTransmitBuffer
*
* Send an arbitrary buffer to the external client
******************************************************************************/
bool_t IIC_Transmit(uint8_t * pSrc, uint16_t length, void (*pfCallBack)(unsigned char const *pBuf))
{
#if !gIIC_Enabled_d      
  (void) pSrc;
  (void) length;
  (void) pfCallBack;
  
  return TRUE;
#else  
  /* Stay here until the message is placed in the I2C queue. Call I2C Tx with no callback */
  mI2cModuleEx.sendBuff = (uint8_t*)pSrc;
  mI2cModuleEx.sentBytesNo = 0;
  mI2cModuleEx.sendBuffLength = length;
  /* Wait for the completion of the tx operation */
  Gpio_SetPinData(gTxDataAvailablePin_c, gGpioPinStateLow_c);
  while (IIC_IsTxActive());  
  
  /* Signal to the master that there is no data to be sent. */
  Gpio_SetPinData(gTxDataAvailablePin_c, gGpioPinStateHigh_c);

  while (I2cBusBusy());

  /* Execute callback manually */
  if (pfCallBack) 
    pfCallBack(NULL);
  
  return TRUE;
#endif  
}
Ejemplo n.º 2
0
int16_t qssp::sf_ReceivePacket()
{
    int16_t value = FALSE;

    if( ISBITSET(thisport->rxBuf[SEQNUM], ACK_BIT ) ) {
        //  Received an ACK packet, need to check if it matches the previous sent packet
        if( ( thisport->rxBuf[SEQNUM] & 0x7F) == (thisport->txSeqNo & 0x7f)) {
            //  It matches the last packet sent by us
            SETBIT( thisport->txSeqNo, ACK_BIT );
            thisport->SendState = SSP_ACKED;
            value = FALSE;
             if (debug)
                qDebug()<<"Received ACK:"<<(thisport->txSeqNo & 0x7F);
        }
        // else ignore the ACK packet
    } else {
        //  Received a 'data' packet, figure out what type of packet we received...
        if( thisport->rxBuf[SEQNUM] == 0 ) {
             if (debug)
                qDebug()<<"Received SYNC Request";
            // Synchronize sequence number with host
#ifdef ACTIVE_SYNCH
            thisport->sendSynch = TRUE;
#endif
            sf_SendAckPacket(thisport->rxBuf[SEQNUM] );
            thisport->rxSeqNo = 0;
            value = FALSE;
        } else if( thisport->rxBuf[SEQNUM] == thisport->rxSeqNo ) {
            // Already seen this packet, just ack it, don't act on the packet.
            sf_SendAckPacket(thisport->rxBuf[SEQNUM] );
            value = FALSE;
        } else {
            //New Packet
            thisport->rxSeqNo = thisport->rxBuf[SEQNUM];
            // Let the application do something with the data/packet.

            // skip the first two bytes (length and seq. no.) in the buffer.
             if (debug)
                qDebug()<<"Received DATA PACKET seq="<<thisport->rxSeqNo<<"Data="<<(uint8_t)thisport->rxBuf[2]<<(uint8_t)thisport->rxBuf[3]<<(uint8_t)thisport->rxBuf[4];
            pfCallBack( &(thisport->rxBuf[2]), thisport->rxBufLen);

            // after we send the ACK, it is possible for the host to send a new packet.
            // Thus the application needs to copy the data and reset the receive buffer
            // inside of thisport->pfCallBack()
            sf_SendAckPacket(thisport->rxBuf[SEQNUM] );
            value = TRUE;
        }
    }
    return value;
}
Ejemplo n.º 3
0
/*****************************************************************************
* Timer task. Called by the kernel when the timer ISR posts a timer event.
******************************************************************************/
void TMR_Task
(
event_t events
)
{
  static bool_t timerHardwareIsRunning = FALSE;
  tmrTimerTicks16_t nextInterruptTime;
  pfTmrCallBack_t pfCallBack;
  tmrTimerTicks16_t currentTimeInTicks;
  tmrTimerStatus_t status;
  tmrTimerTicks16_t ticksSinceLastHere, ticksdiff; 
  uint8_t timerID;
  unsigned int saveInt;
  tmrTimerType_t timerType;
  (void)events;
  
  TmrReadValue(gTmrNumber_d,&currentTimeInTicks);
  /* calculate difference between current and previous.  */
  ticksSinceLastHere = (currentTimeInTicks - previousTimeInTicks);
  /* remember for next time */
  previousTimeInTicks = currentTimeInTicks;
  
  for (timerID = 0; timerID < NumberOfElements(maTmrTimerTable); ++timerID) {
    saveInt = IntDisableAll();
    status = TMR_GetTimerStatus(timerID);
    /* If TMR_StartTimer() has been called for this timer, start it's count */
    /* down as of now. */
    if (status == mTmrStatusReady_c) {
      TMR_SetTimerStatus(timerID, mTmrStatusActive_c);
      IntRestoreAll(saveInt);
      continue;
    }
    IntRestoreAll(saveInt);
    
    /* Ignore any timer that is not active. */
    if (status != mTmrStatusActive_c) {
      continue;
    }
    
    /* This timer is active. Decrement it's countdown.. */
    if (maTmrTimerTable[timerID].remainingTicks > ticksSinceLastHere) {
      maTmrTimerTable[timerID].remainingTicks -= ticksSinceLastHere;
      continue;
    }
    
    timerType = TMR_GetTimerType(timerID);
    /* If this is an interval timer, restart it. Otherwise, mark it as inactive. */
    if ( (timerType & gTmrSingleShotTimer_c) ||
         (timerType & gTmrSetMinuteTimer_c) ||
         (timerType & gTmrSetSecondTimer_c)  ) {
      TMR_StopTimer(timerID);
    } else {
      maTmrTimerTable[timerID].remainingTicks = maTmrTimerTable[timerID].intervalInTicks;
    }
    /* This timer has expired. */
    pfCallBack = maTmrTimerTable[timerID].pfCallBack;
    /*Call callback if it is not NULL
    This is done after the timer got updated,
    in case the timer gets stopped or restarted in the callback*/
    if (pfCallBack) {
      pfCallBack(timerID);
    }
    
  }  /* for (timerID = 0; timerID < ... */
  
  /* Find the shortest active timer. */
  nextInterruptTime = mMaxToCountDown_c;
  for (timerID = 0; timerID < NumberOfElements(maTmrTimerTable); ++timerID) {
    if (TMR_GetTimerStatus(timerID) == mTmrStatusActive_c) {
      if (nextInterruptTime > maTmrTimerTable[timerID].remainingTicks) {
        nextInterruptTime = maTmrTimerTable[timerID].remainingTicks;
      }
    }
  }
  
  /* Check to be sure that the timer is not programmed in the past */    
  saveInt = IntDisableAll();
  TmrReadValue(gTmrNumber_d,&ticksdiff);
  /* Number of ticks to be here */
  ticksdiff = (uint16_t)(ticksdiff - currentTimeInTicks); 
   /* Next ticks to count already expired?? */
  if(ticksdiff >= nextInterruptTime)
  {  
    /* Is assumed that a task has to be executed in 4ms...
       so if the ticks already expired enter in TMR_Task() after 4ms*/
    nextInterruptTime = ticksdiff + mTicksFor4ms;
  } 
  else 
  {
    /* Time reference is 4ms...
       so be sure that won't be loaded in Cmp Reg. less that 4ms in ticks 
    */
     if((nextInterruptTime - ticksdiff) < mTicksFor4ms) 
     {
       nextInterruptTime = ticksdiff + mTicksFor4ms;
     }
  
  }
  /* Update the compare register */
  nextInterruptTime += currentTimeInTicks;
  SetComp1Val(gTmrNumber_d, nextInterruptTime);
  IntRestoreAll(saveInt);
  
  if (!numberOfActiveTimers && !numberOfLowPowerActiveTimers)
  {
    TmrStopTimerHardware();
    timerHardwareIsRunning = FALSE;
  } 
  else 
    if (!timerHardwareIsRunning) 
    {
      TmrStartTimerHardware();
      timerHardwareIsRunning = TRUE;
    }
}                                       /* TMR_Task() */