Exemplo n.º 1
0
void
rf_router_task(void)
{
  if(rf_router_status == RF_ROUTER_INACTIVE)
    return;

  uint8_t hsec = (uint8_t)ticks;

  if(rf_router_status == RF_ROUTER_GOT_DATA) {

    uint8_t len = cc1100_readReg(CC1100_RXFIFO);
    uint8_t proto = 0;

    if(len > 5) {
      rb_reset(&TTY_Rx_Buffer);
      CC1100_ASSERT;
      cc1100_sendbyte( CC1100_READ_BURST | CC1100_RXFIFO );
      proto = cc1100_sendbyte(0);
      while(--len)
        rb_put(&TTY_Rx_Buffer, cc1100_sendbyte(0));
      CC1100_DEASSERT;
    }
    set_txrestore();
    rf_router_status = RF_ROUTER_INACTIVE;

    if(proto == RF_ROUTER_PROTO_ID) {
      uint8_t id;
      if(fromhex(TTY_Rx_Buffer.buf, &id, 1) == 1 &&     // it is for us
         id == rf_router_myid) {

        if(TTY_Rx_Buffer.buf[4] == 'U') {               // "Display" the data
          while(TTY_Rx_Buffer.nbytes)                   // downlink: RFR->CUL
            DC(rb_get(&TTY_Rx_Buffer));
          DNL();

        } else {                                        // uplink: CUL->RFR
          TTY_Rx_Buffer.nbytes -= 4;                    // Skip dest/src bytes
          TTY_Rx_Buffer.getoff = 4;
          rb_put(&TTY_Rx_Buffer, '\n');
          input_handle_func(DISPLAY_RFROUTER);          // execute the command
        }

      } else {
        rb_reset(&TTY_Rx_Buffer);
      }
    }

  } else if(rf_router_status == RF_ROUTER_DATA_WAIT) {
    uint8_t diff = hsec - rf_router_hsec;
    if(diff > 7) {              // 3 (delay above) + 3 ( ((4+64)*8)/250kBaud )
      set_txrestore();
      rf_router_status = RF_ROUTER_INACTIVE;
    }

  } else if(rf_router_status == RF_ROUTER_SYNC_RCVD) {
    ccInitChip(EE_FASTRF_CFG);
    ccRX();
    rf_router_status = RF_ROUTER_DATA_WAIT;
    rf_router_hsec = hsec;

  } 
}
Exemplo n.º 2
0
////////////////////
// Fill data from USB to the RingBuffer and vice-versa
void
CDC_Task(void)
{
  static char inCDC_TASK = 0;

  if(!USB_IsConnected)
    return;

#ifdef ARM

  if(!inCDC_TASK){ // USB -> RingBuffer

    inCDC_TASK = 1;
    output_flush_func = CDC_Task;
    input_handle_func(DISPLAY_USB);
    inCDC_TASK = 0;
  }

	if(TTY_Tx_Buffer.nbytes) {
		uint16_t i=0;

		while(TTY_Tx_Buffer.nbytes && i<DATABUFFERSIZEOUT) {

			 usbBufferOut[i++]=rb_get(&TTY_Tx_Buffer);
		}

		while (CDCDSerialDriver_Write(usbBufferOut,i, 0, 0) != USBD_STATUS_SUCCESS);

	}


#else
  Endpoint_SelectEndpoint(CDC_RX_EPNUM);          // First data in

  if(!inCDC_TASK && Endpoint_ReadWriteAllowed()){ // USB -> RingBuffer

    while (Endpoint_BytesInEndpoint()) {          // Discard data on buffer full
      rb_put(&TTY_Rx_Buffer, Endpoint_Read_Byte());
    }
    Endpoint_ClearCurrentBank(); 
    inCDC_TASK = 1;
    output_flush_func = CDC_Task;
    input_handle_func(DISPLAY_USB);
    inCDC_TASK = 0;
  }


  Endpoint_SelectEndpoint(CDC_TX_EPNUM);          // Then data out
  if(TTY_Tx_Buffer.nbytes && Endpoint_ReadWriteAllowed()) {

    cli();
    while(TTY_Tx_Buffer.nbytes &&
          (Endpoint_BytesInEndpoint() < USB_BUFSIZE))
      Endpoint_Write_Byte(rb_get(&TTY_Tx_Buffer));
    sei();
    
    Endpoint_ClearCurrentBank();                  // Send the data

  }
#endif
}