/***********************************************************************************
* @fn      bufPut
*
* @brief   Add bytes to the buffer.
*
* @param   pBuf - pointer to the ringbuffer
*          pData - pointer to data to be appended to the buffer
*          nBytes - number of bytes
*
* @return  Number of bytes copied to the buffer
*/
uint8_t bufPut( ringBuf_t *pBuf, const uint8_t *pData, uint8_t nBytes ) {
	uint8_t i = 0;
	uint16_t s = 0;

	// Critical section start
	s = halIntLock( );

	if( pBuf->nBytes + nBytes < BUF_SIZE ) {
		i = 0;
		while( i < nBytes ) {
			pBuf->pData[pBuf->iTail] = pData[i];
			pBuf->iTail++;
			if( pBuf->iTail == BUF_SIZE )
				pBuf->iTail = 0;
			i++;
		}
		pBuf->nBytes += i;
	} else {
		i = 0;
	}

	// Critical section end
	halIntUnlock( s );

	return i;
}
Пример #2
0
/******************************************************************************
* @fn         mrfiLinkSend
*
* @brief      Send data on the RX link.
*
* @param      pBuf - buffer to be transmitted
*
* @param      len -  number of bytes to be transmitted
*
* @return     Return code indicates success or failure of transmit:
*                  MRFI_TX_RESULT_SUCCESS - transmit succeeded
*                  MRFI_TX_RESULT_FAILED  - transmit failed because CCA or ACK failed
*/
uint8 mrfiLinkSend(uint8 *pBuf, uint8 len, uint8 nRetrans)
{
    uint8 v,i,status;

    v= halIntLock();

    MRFI_SET_PAYLOAD_LEN(&pkt, len+2);
    memcpy(MRFI_P_DST_ADDR(&pkt), dest_addr, 4);
    memcpy(MRFI_P_SRC_ADDR(&pkt), src_addr, 4);

    MRFI_P_PAYLOAD(&pkt)[0]= seqSend;
    MRFI_P_PAYLOAD(&pkt)[1]= MRFI_LINK_DATA;

    memcpy(MRFI_P_PAYLOAD(&pkt)+2, pBuf, len);
    halIntUnlock(v);

    for (i=0;i<nRetrans;i++) {
        status= MRFI_Transmit(&pkt, MRFI_TX_TYPE_CCA);
        if (status==MRFI_TX_RESULT_SUCCESS) {
            if (waitForAck(20)) {
                seqSend++;
                break;
            } else {
                status= MRFI_TX_RESULT_FAILED;
                // wait random time if sending is not successful
                // (20-40 milliseconds)
                halMcuWaitUs( (20000/255*MRFI_RandomByte()) + 20000 );
            }
        }
    }

    return status;

}
/***********************************************************************************
* @fn      bufInit
*
* @brief   Initialise a ringbuffer. The buffer must be allocated by the
*          application.
*
* @param   pBuf - pointer to the ringbuffer
*
* @return  none
*/
void bufInit( ringBuf_t *pBuf ) {
	uint16_t s = 0;

	// Critical section start
	s = halIntLock( );

	pBuf->nBytes = 0;
	pBuf->iHead = 0;
	pBuf->iTail = 0;

	// Critical section end
	halIntUnlock( s );
}
Пример #4
0
/******************************************************************************
* @fn          sendAck
*
* @brief       Send an acknowledge packet (no payload)
*
* @param       none
*
* @return      none
*/
static void sendAck(void)
{
    uint8 v;

    v= halIntLock();
    MRFI_SET_PAYLOAD_LEN(&pkt, 2);
    memcpy(MRFI_P_DST_ADDR(&pkt), dest_addr, 4);
    memcpy(MRFI_P_SRC_ADDR(&pkt), src_addr, 4);
    MRFI_P_PAYLOAD(&pkt)[0]= seqRecv;
    MRFI_P_PAYLOAD(&pkt)[1]= MRFI_LINK_ACK;
    halIntUnlock(v);
    MRFI_Transmit(&pkt, MRFI_TX_TYPE_FORCED);

}
/***********************************************************************************
* @fn      bufGet
*
* @brief   Extract bytes from the buffer.
*
* @param   pBuf   - pointer to the ringbuffer
*          pData  - pointer to data to be extracted
*          nBytes - number of bytes
*
* @return  Bytes actually returned
*/
uint8_t bufGet( ringBuf_t *pBuf, uint8_t *pData, uint8_t nBytes ) {
	uint8_t i = 0;
	uint16_t s = 0;

	// Critical section start
	s = halIntLock( );

	while( i < nBytes && i < pBuf->nBytes ) {
		pData[i] = pBuf->pData[pBuf->iHead];
		pBuf->iHead++;
		if( pBuf->iHead == BUF_SIZE )
			pBuf->iHead = 0;
		i++;
	}
	pBuf->nBytes -= i;

	// Critical section end
	halIntUnlock( s );
	return i;
}
/**************************************************************************************************
 * @fn      hidSendHidInReport
 *
 * @brief   Send HID Consumer Control report.
 *
 * input parameters
 *
 * @param   pReport - report to be sent
 * @param   endPoint - endPoint associated with report.
 * @param   len - length of report
 *
 * output parameters
 *
 * None.
 *
 * @return  TRUE if report was sent; FALSE otherwise.
 */
uint8 hidSendHidInReport(uint8 *pReport, uint8 endPoint, uint8 len)
{
  uint8 result = FALSE;

  if (endPoint < 6)
  {
    uint8 ea = halIntLock();

    USBFW_SELECT_ENDPOINT(endPoint);
    if (!(USBCSIL & USBCSIL_INPKT_RDY))
    {
      usbfwWriteFifo(((&USBF0) + (endPoint << 1)), len, pReport);
      USBCSIL |= USBCSIL_INPKT_RDY;
      result = TRUE;
    }
    halIntUnlock(ea);
  }

  return result;
}
/***********************************************************************************
* @fn      bufPeek
*
* @brief   Read bytes from the buffer but leave them in the queue.
*
* @param   pBuf   - pointer to the ringbuffer
*          pData  - pointer to data to be extracted
*          nBytes - number of bytes
*
* @return  Bytes actually returned
*/
uint8_t bufPeek( ringBuf_t *pBuf, uint8_t *pData, uint8_t nBytes ) {
	uint8_t i = 0;
	uint8_t j = 0;
	uint16_t s = 0;

	// Critical section start
	s = halIntLock( );

	i = 0;
	j = pBuf->iHead;
	while( i < nBytes && i < pBuf->nBytes ) {
		pData[i] = pBuf->pData[j];
		j++;
		if( j == BUF_SIZE )
			j = 0;
		i++;
	}

	// Critical section end
	halIntUnlock( s );
	return i;
}
Пример #8
0
/***********************************************************************************
* @fn      bufGet
*
* @brief   Extract bytes from the buffer.
*
* @param   pBuf   - pointer to the ringbuffer
*          pData  - pointer to data to be extracted
*          nBytes - number of bytes
*
* @return  Bytes actually returned
*/
uint8 bufGet(ringBuf_t *pBuf, uint8 *pData, uint8 nBytes)
{
    uint8 i;
    uint16 s;

    // Critical section start
    s = halIntLock();

    i= 0;
    while(i<nBytes && i<pBuf->nBytes) {
        pData[i]= pBuf->pData[pBuf->pHead];
        pBuf->pHead++;
        if ((pBuf->pHead+1)==BUF_SIZE)
            pBuf->pHead= 0;
        i++;
    }
    pBuf->nBytes-= i;

    // Critical section end
    halIntUnlock(s);
    return i;
}
Пример #9
0
/***********************************************************************************
* @fn      bufPeek
*
* @brief   Read bytes from the buffer but leave them in the queue.
*
* @param   pBuf   - pointer to the ringbuffer
*          pData  - pointer to data to be extracted
*          nBytes - number of bytes
*
* @return  Bytes actually returned
*/
uint8 bufPeek(ringBuf_t *pBuf, uint8 *pData, uint8 nBytes)
{
    uint8 i,j;
    uint16 s;

    // Critical section start
    s = halIntLock();

    i= 0;
    j= pBuf->pHead;
    while(i<nBytes && i<pBuf->nBytes) {
        pData[i]= pBuf->pData[j];
        j++;
        if ((j+1)==BUF_SIZE)
            j= 0;
        i++;
    }

    // Critical section end
    halIntUnlock(s);
    return i;
}
Пример #10
0
/******************************************************************************
* @fn         mrfiLinkRecv
*
* @brief      Read data from the RX buffer
*
* @param      pBuf - buffer for storage of received data
*
* @return     Number of bytes received
*
*/
uint8 mrfiLinkRecv(uint8 *pBuf)
{
    uint8 n;

    if (mrfiPktRdy) {
        uint8 v, pktType;

        // Process the packet
        v= halIntLock();
        n= MRFI_GET_PAYLOAD_LEN(&pkt)-2;
        seqRecv= MRFI_P_PAYLOAD(&pkt)[0];
        pktType= MRFI_P_PAYLOAD(&pkt)[1];
        memcpy(pBuf,MRFI_P_PAYLOAD(&pkt)+2,n);
        mrfiPktRdy= FALSE;
        if ( pktType==MRFI_LINK_DATA && n>0)
            sendAck();
        halIntUnlock(v);
    } else {
        n= 0;
    }

    return n;
}
Пример #11
0
/***********************************************************************************
* @fn      bufPut
*
* @brief   Add bytes to the buffer.
*
* @param   pBuf - pointer to the ringbuffer
*          pData - pointer to data to be appended to the buffer
*          nBytes - number of bytes
*
* @return  Number of bytes copied to the buffer
*/
uint8 bufPut(ringBuf_t *pBuf, const uint8 *pData, uint8 nBytes)
{
    uint8 i;
    istate_t s;
    
    // Critical section start
    s = halIntLock();
    
    i= 0;
    while(i<nBytes) {
        pBuf->data[pBuf->iTail]= pData[i];
        pBuf->iTail++;
        if (pBuf->iTail==BUF_SIZE)
            pBuf->iTail= 0;
        i++;
    }
    pBuf->nBytes+= i;
    
    // Critical section end
    halIntUnlock(s);
    
    return i;
}