// Configures RF parameters before Enhanced Shockburst can be used.
void configureRF()
{
  packet_received = false;
  send_success = false;

  // Enable the radio clock
  RFCKEN = 1;
  // Set payload width to 32 bytes
  hal_nrf_set_rx_payload_width((int)HAL_NRF_PIPE0, PAYLOAD_SIZE);
  // Set auto-retries to 5 with 500 us intervals
  hal_nrf_set_auto_retr(5, 500);
  // Set pipe address
  hal_nrf_set_address(HAL_NRF_PIPE0, default_pipe_address);
  hal_nrf_set_address(HAL_NRF_TX, default_pipe_address);
  // Set initial channel
  hal_nrf_set_rf_channel(default_channels[1]);
  // Configure radio as primary receiver (PTX)
  hal_nrf_set_operation_mode(HAL_NRF_PRX);
  // Wait for the xtal to power up
  while (hal_clk_get_16m_source() != HAL_CLK_XOSC16M) ;
  // Power up radio
  hal_nrf_set_power_mode(HAL_NRF_PWR_UP);
  // Enable receiver
  CE_HIGH();

  return;
}
Exemple #2
0
/** Initialized the RF configurations and powre up the RF.
 * Use this function to initialize RF configurations.
 * Note that the pipe isn't opened in this function,
 * please use "rf_rcv_pipe_config" after using this
 * function to configure RX pipe.
 *
 * @param in_channel RF Frequency (in_channel + 2400MHz)
 * @param in_datarate Data rate of the RF transmission. (1Mbps or 2Mbps)
 * @param in_output_power RF output power configuration.
 * @param in_auto_retr Enable auto retransmission or not.
 * @param in_auto_retr_delay Auto retransmission delay.
 * @param in_addr_width Address width configuration for both PTX and PRX pipe.
 * @param in_crc_mode CRC enable or disable configuration.
 * @param in_spi_clk_rate SPI clock rate. (SPI speed)
 * @param in_rf_int RF interrupt enable bit.
 */
void epl_rf_en_init(unsigned char in_channel, epl_rf_en_datarate_t in_datarate, char in_output_power, unsigned char in_auto_retr, unsigned int in_auto_retr_delay, char in_addr_width, epl_rf_en_crc_mode_t in_crc_mode, unsigned char in_rf_int)
{

	RFCKEN = 1;	// RF clock enable.

	CE_LOW();

	//--- Default static setup. These setting is optimized to match the RF protocol with nRF24E1. ---//
	hal_nrf_close_pipe(HAL_NRF_ALL); // Close all pipes first. By default, pipe0 and pipe1 are opened.

	hal_nrf_set_datarate(in_datarate);
	hal_nrf_set_auto_retr(in_auto_retr, in_auto_retr_delay); // First parameter is set to zero indicating the auto retransmission is off.
	hal_nrf_set_output_power(in_output_power); // Maximum radio output power (0dbm).
	hal_nrf_set_crc_mode(in_crc_mode);

	hal_nrf_set_address_width(in_addr_width); // Both RX and TX's address width are Configured.

	hal_nrf_set_operation_mode(HAL_NRF_PTX); // Enter RF TX mode

	hal_nrf_set_rf_channel(in_channel);
	hal_nrf_set_power_mode(HAL_NRF_PWR_UP); // Power up radio

	hal_nrf_get_clear_irq_flags();

	// IEN1 RF interrupt enable bit
	RF = in_rf_int;
}
Exemple #3
0
void radio_sb_init( hal_nrf_operation_mode_t operational_mode )
{
    if( !bus_initialized  )
        radio_bus_init();
        
    switch( operational_mode )
    {
        case HAL_NRF_PRX:
            radio_mode = DEVICE_PRX_SB;
            break;
        case HAL_NRF_PTX:
            radio_mode = DEVICE_PTX_SB;
            break;
    }

    hal_nrf_close_pipe(HAL_NRF_ALL);               // First close all radio pipes
    // Pipe 0 and 1 open by default
    hal_nrf_open_pipe(HAL_NRF_PIPE0, false);       // Open pipe0, without/autoack

    hal_nrf_set_crc_mode(HAL_NRF_CRC_16BIT);       // Operates in 16bits CRC mode
    hal_nrf_set_auto_retr(0, RF_RETRANS_DELAY);    // Disables auto retransmit

    hal_nrf_set_address_width(HAL_NRF_AW_5BYTES);  // 5 bytes address width
    hal_nrf_set_address(HAL_NRF_TX, address);      // Set device's addresses
    hal_nrf_set_address(HAL_NRF_PIPE0, address);   // Sets recieving address on
    // pipe0

    if(operational_mode == HAL_NRF_PTX)            // Mode depentant settings
    {
        hal_nrf_set_operation_mode(HAL_NRF_PTX);     // Enter TX mode
    }
    else
    {
        hal_nrf_set_operation_mode(HAL_NRF_PRX);     // Enter RX mode
        hal_nrf_set_rx_pload_width((uint8_t)HAL_NRF_PIPE0, RF_PAYLOAD_LENGTH);
        // Pipe0 expect
        // PAYLOAD_LENGTH byte payload
        // PAYLOAD_LENGTH in radio.h
    }

    hal_nrf_set_rf_channel(RF_CHANNEL);            // Operating on static channel
    // Defined in radio.h.
    // Frequenzy =
    //        2400 + RF_CHANNEL
    hal_nrf_set_power_mode(HAL_NRF_PWR_UP);        // Power up device

    //hal_nrf_set_datarate(HAL_NRF_1MBPS);           // Uncomment this line for
    // compatibility with nRF2401
    // and nRF24E1

    radio_wait();

    radio_set_status (RF_IDLE);                    // Radio now ready
}
Exemple #4
0
void radio_esb_init( hal_nrf_operation_mode_t operational_mode )
{
    if( !bus_initialized  )
        radio_bus_init();
        
    switch( operational_mode )
    {
        case HAL_NRF_PRX:
            radio_mode = DEVICE_PRX_ESB;
            break;
        case HAL_NRF_PTX:
            radio_mode = DEVICE_PTX_ESB;
            break;
    }

    hal_nrf_close_pipe( HAL_NRF_ALL );               // First close all radio pipes
    // Pipe 0 and 1 open by default
    hal_nrf_open_pipe( HAL_NRF_PIPE0, true );        // Then open pipe0, w/autoack
    // Changed from sb/radio_sb.c

    hal_nrf_set_crc_mode( HAL_NRF_CRC_16BIT );       // Operates in 16bits CRC mode
    hal_nrf_set_auto_retr( RF_RETRANSMITS, RF_RETRANS_DELAY );
    // Enables auto retransmit.
    // 3 retrans with 250ms delay
    // Changed from sb/radio_sb.c

    hal_nrf_set_address_width( HAL_NRF_AW_5BYTES );  // 5 bytes address width
    hal_nrf_set_address( HAL_NRF_TX, address );      // Set device's addresses
    hal_nrf_set_address( HAL_NRF_PIPE0, address );   // Sets recieving address on
    // pipe0

    if( operational_mode == HAL_NRF_PTX )            // Mode depentant settings
    {
        hal_nrf_set_operation_mode( HAL_NRF_PTX );     // Enter TX mode
    }
    else
    {
        hal_nrf_set_operation_mode( HAL_NRF_PRX );     // Enter RX mode
        hal_nrf_set_rx_pload_width( (uint8_t)HAL_NRF_PIPE0, RF_PAYLOAD_LENGTH );
        // Pipe0 expect
        // PAYLOAD_LENGTH byte payload
        // PAYLOAD_LENGTH in radio.h
    }

    hal_nrf_set_rf_channel( RF_CHANNEL );            // Operating on static channel
    // Defined in radio.h.
    // Frequenzy =
    //        2400 + RF_CHANNEL
    hal_nrf_set_power_mode( HAL_NRF_PWR_UP );        // Power up device

    radio_wait();

    radio_set_status( RF_IDLE );                     // Radio now ready
}
Exemple #5
0
void gzll_set_channels(uint8_t *channels, uint8_t channel_tab_size)
{
  gzll_interupts_disable_rfck_enable();

  ASSERT((gzll_state_var == GZLL_IDLE));
  ASSERT((channel_tab_size <= GZLL_MAX_CHANNEL_TAB_SIZE));

  gzll_channel_tab_index = 0;
  gzll_channel_tab_size = channel_tab_size;
  memcpy(gzll_channel_tab, channels, gzll_channel_tab_size);

  hal_nrf_set_rf_channel(gzll_channel_tab[gzll_channel_tab_index]);

  gzll_interupts_enable_rfck_disable();
}
Exemple #6
0
void gzll_set_channels(uint8_t *channels, uint8_t channel_tab_size)
{
  uint32_t flag;
  flag = gzll_interupts_save();

  ASSERT((gzll_state_var == GZLL_IDLE));
  ASSERT((channel_tab_size <= GZLL_MAX_CHANNEL_TAB_SIZE));

  gzll_channel_tab_index = 0;
  gzll_channel_tab_size = channel_tab_size;
  memcpy(gzll_channel_tab, channels, gzll_channel_tab_size);

  hal_nrf_set_rf_channel(gzll_channel_tab[gzll_channel_tab_index]);

  gzll_interupts_restore(flag);
}
Exemple #7
0
void radio_sb_init (const uint8_t *address, hal_nrf_operation_mode_t operational_mode)
{
  hal_nrf_close_pipe(HAL_NRF_ALL);               // First close all radio pipes
                                                 // Pipe 0 and 1 open by default
  hal_nrf_open_pipe(HAL_NRF_PIPE0, FALSE);       // Open pipe0, without/autoack

  hal_nrf_set_crc_mode(HAL_NRF_CRC_16BIT);       // Operates in 16bits CRC mode
  hal_nrf_set_auto_retr(0, RF_RETRANS_DELAY);    // Disables auto retransmit

  hal_nrf_set_address_width(HAL_NRF_AW_5BYTES);  // 5 bytes address width
  hal_nrf_set_address(HAL_NRF_TX, address);      // Set device's addresses
  hal_nrf_set_address(HAL_NRF_PIPE0, address);   // Sets recieving address on 
                                                 // pipe0  
  
  if(operational_mode == HAL_NRF_PTX)            // Mode depentant settings
  {
    hal_nrf_set_operation_mode(HAL_NRF_PTX);     // Enter TX mode
  }
  else
  {
    hal_nrf_set_operation_mode(HAL_NRF_PRX);     // Enter RX mode
    hal_nrf_set_rx_pload_width((uint8_t)HAL_NRF_PIPE0, RF_PAYLOAD_LENGTH);
                                                 // Pipe0 expect 
                                                 // PAYLOAD_LENGTH byte payload
                                                 // PAYLOAD_LENGTH in radio.h
  }

  hal_nrf_set_rf_channel(RF_CHANNEL);            // Operating on static channel 
                                                 // Defined in radio.h. 
                                                 // Frequenzy = 
                                                 //        2400 + RF_CHANNEL
  hal_nrf_set_power_mode(HAL_NRF_PWR_UP);        // Power up device

  hal_nrf_set_datarate(HAL_NRF_250KBPS);         // Uncomment this line for 
                                                 // compatibility with nRF2401 
                                                 // and nRF24E1
  //hal_nrf_set_output_power(hal_nrf_output_power_t power); //default reset value is 0dbm

  // Wait for the radio to power up, max. 4.5ms depending on crystal Ls
  Timeout_SetTimeout2(5);
  while(!Timeout_IsTimeout2());

  radio_set_status (RF_IDLE);                    // Radio now ready
}                                                
Exemple #8
0
void init_radio()
{
  // Enable the radio clock
  RFCKEN = 1;
  // Enable RF interrupt
  RF = 1;
    // Power up radio
  hal_nrf_set_power_mode(HAL_NRF_PWR_UP);
	hal_nrf_set_output_power(HAL_NRF_0DBM);
  	hal_nrf_enable_ack_payload(1);
	hal_nrf_enable_dynamic_payload(1);
	hal_nrf_setup_dynamic_payload(1); // Set up PIPE 0 to handle dynamic lengths
	hal_nrf_set_rf_channel(125); // 2525 MHz
   	hal_nrf_set_auto_retr(5, 250); // Retry 5x
    // Configure radio as primary receiver (PTX) 
  hal_nrf_set_operation_mode(HAL_NRF_PTX);
 
      // Set payload width to 32 bytes
//  hal_nrf_set_rx_payload_width(HAL_NRF_PIPE0, MAXLENGTH);
   // Enable global interrupt
  EA = 1;
}
// Resets RF parameters to default values.
// Must be called before jumping to new firmware.
void resetRF()
{
  // Reset values set by the RF setup.
  CE_LOW();
  // PWR_UP = 0
  hal_nrf_set_power_mode(HAL_NRF_PWR_DOWN);
  // PRIM_RX = 0
  hal_nrf_set_operation_mode(HAL_NRF_PTX);
  // RF_CH = 0x02;
  hal_nrf_set_rf_channel(reset_channel);
  // AW = 11 (Default = 5 bytes)
  // RX_ADDR_P0 = TX_ADDR = 0xE7E7E7E7E7
  hal_nrf_set_address(HAL_NRF_TX, reset_pipe_address);
  hal_nrf_set_address(HAL_NRF_PIPE0, reset_pipe_address);
  // ARD = 0000, ARC = 0011
  hal_nrf_set_auto_retr(3, 250);
  // RX_PW_P0 = 0x00
  hal_nrf_set_rx_payload_width((int)HAL_NRF_PIPE0, 0);
  // Disable radio clock
  RFCKEN = 0;

  return;
}
Exemple #10
0
void gzll_init(void)
{
  uint32_t flag;
  uint8_t temp_adr[GZLL_ADDRESS_WIDTH] = GZLL_DEFAULT_ADDRESS_PIPE1;

  flag = gzll_interupts_save();
  GZLL_RFCE_LOW();

  hal_nrf_enable_ack_payload(true);
  hal_nrf_enable_dynamic_payload(true);
  hal_nrf_setup_dynamic_payload(0xff);

  /*
  Initialize status variables.
  */
  gzll_channel_tab_index = 0;
  gzll_channel_tab_size = GZLL_DEFAULT_CHANNEL_TAB_SIZE;

  gzll_pending_goto_idle = false;
  gzll_timer_period_modified = false;

  gzll_current_tx_pipe = 0;
  gzll_pending_tx_start = false;
  gzll_tx_setup_modified = true;
  gzll_rx_setup_modified = true;
  gzll_radio_active_f = false;
  gzll_tx_success_f = true;

  gzll_sync_period = 0;
  gzll_sync_on = false;

  gzll_rx_dr = false;
  gzll_rx_power_high_f = false;
  gzll_ack_rx_pipe_fifo_cnt = 0;

  /*
  Set up default addresses.
  */
  hal_nrf_set_address(HAL_NRF_PIPE0, gzll_p0_adr);
  hal_nrf_set_address(HAL_NRF_PIPE1, temp_adr);

  temp_adr[0] = GZLL_DEFAULT_ADDRESS_PIPE2;
  hal_nrf_set_address(HAL_NRF_PIPE2, temp_adr);

  temp_adr[0] = GZLL_DEFAULT_ADDRESS_PIPE3;
  hal_nrf_set_address(HAL_NRF_PIPE3, temp_adr);

  temp_adr[0] = GZLL_DEFAULT_ADDRESS_PIPE4;
  hal_nrf_set_address(HAL_NRF_PIPE4, temp_adr);

  temp_adr[0] = GZLL_DEFAULT_ADDRESS_PIPE5;
  hal_nrf_set_address(HAL_NRF_PIPE5, temp_adr);

  /*
  Set up default channel.
  */
  hal_nrf_set_rf_channel(gzll_channel_tab[gzll_channel_tab_index]);

  /*
  Initialize dynamic parameters using default values.
  */
  gzll_dyn_params[GZLL_PARAM_DEVICE_MODE] = GZLL_DEFAULT_PARAM_DEVICE_MODE;
  gzll_dyn_params[GZLL_PARAM_TX_TIMEOUT] = GZLL_DEFAULT_PARAM_TX_TIMEOUT;
  gzll_dyn_params[GZLL_PARAM_TX_ATTEMPTS_PR_CHANNEL_WHEN_SYNC_ON] = GZLL_DEFAULT_PARAM_TX_ATTEMPTS_PR_CHANNEL_WHEN_SYNC_ON;
  gzll_dyn_params[GZLL_PARAM_TX_ATTEMPTS_PR_CHANNEL_WHEN_SYNC_OFF] = GZLL_DEFAULT_PARAM_TX_ATTEMPTS_PR_CHANNEL_WHEN_SYNC_OFF;
  gzll_dyn_params[GZLL_PARAM_HOST_MODE] = GZLL_DEFAULT_PARAM_HOST_MODE;
  gzll_dyn_params[GZLL_PARAM_RX_PIPES] = GZLL_DEFAULT_PARAM_RX_PIPES;
  gzll_dyn_params[GZLL_PARAM_CRYPT_PIPES] = GZLL_DEFAULT_PARAM_CRYPT_PIPES;
  gzll_dyn_params[GZLL_PARAM_RX_TIMEOUT] = GZLL_DEFAULT_PARAM_RX_TIMEOUT;
  gzll_dyn_params[GZLL_PARAM_HOST_MODE_1_CYCLE_PERIOD] = GZLL_DEFAULT_PARAM_HOST_MODE_1_CYCLE_PERIOD;
  gzll_dyn_params[GZLL_PARAM_RX_PERIOD] = GZLL_DEFAULT_PARAM_RX_PERIOD;
  gzll_dyn_params[GZLL_PARAM_RX_PERIOD_MODIFIER] = GZLL_DEFAULT_PARAM_RX_PERIOD_MODIFIER;
  gzll_dyn_params[GZLL_PARAM_RX_CHANNEL_HOLD_PERIODS] = GZLL_DEFAULT_PARAM_RX_CHANNEL_HOLD_PERIODS;
  gzll_dyn_params[GZLL_PARAM_OUTPUT_POWER] = GZLL_DEFAULT_PARAM_OUTPUT_POWER;
  gzll_dyn_params[GZLL_PARAM_POWER_DOWN_IDLE_ENABLE] = GZLL_DEFAULT_PARAM_POWER_DOWN_IDLE_ENABLE;
  gzll_dyn_params[GZLL_PARAM_MAX_SYNC_PERIOD] = GZLL_DEFAULT_PARAM_MAX_SYNC_PERIOD;
  gzll_dyn_params[GZLL_PARAM_COLLISION_CHANNEL_SWITCH_LIMIT] = GZLL_DEFAULT_PARAM_COLLISION_CHANNEL_SWITCH_LIMIT;

  /*
  Set up default output power.
  */
  hal_nrf_set_output_power((hal_nrf_output_power_t) gzll_dyn_params[GZLL_PARAM_OUTPUT_POWER]);

  /*
  Static radio setup.
  */
  hal_nrf_set_datarate(GZLL_HAL_DATARATE);
  hal_nrf_set_crc_mode(GZLL_CRC);
  hal_nrf_set_address_width(GZLL_ADDRESS_WIDTH);

  /*
  Clear radio IRQ flags.
  */
  //lint -esym(534, hal_nrf_get_clear_irq_flags) "return value ignored"
  hal_nrf_get_clear_irq_flags();

  hal_nrf_flush_rx();
  hal_nrf_flush_tx();

  gzll_set_timer_period(GZLL_DEFAULT_PARAM_RX_PERIOD);
  gzll_set_system_idle();
  gzll_interupts_restore(flag);
}
Exemple #11
0
void radio_pl_init (const uint8_t *address, hal_nrf_operation_mode_t operational_mode)
{
  hal_nrf_close_pipe(HAL_NRF_ALL);               // First close all radio pipes
                                                 // Pipe 0 and 1 open by default
  hal_nrf_open_pipe(HAL_NRF_PIPE0, true);        // Then open pipe0, w/autoack 

  hal_nrf_set_crc_mode(HAL_NRF_CRC_16BIT);       // Operates in 16bits CRC mode
  hal_nrf_set_auto_retr(RF_RETRANSMITS, RF_RETRANS_DELAY);
                                                 // Enables auto retransmit.
                                                 // 3 retrans with 250ms delay

  hal_nrf_set_address_width(HAL_NRF_AW_5BYTES);  // 5 bytes address width
  hal_nrf_set_address(HAL_NRF_TX, address);      // Set device's addresses
  hal_nrf_set_address(HAL_NRF_PIPE0, address);   // Sets recieving address on 
                                                 // pipe0

/*****************************************************************************
 * Changed from esb/radio_esb.c                                              *
 * Enables:                                                                  *
 *  - ACK payload                                                            *
 *  - Dynamic payload width                                                  *
 *  - Dynamic ACK                                                            *
 *****************************************************************************/
  hal_nrf_enable_ack_pl();                       // Try to enable ack payload

  // When the features are locked, the FEATURE and DYNPD are read out 0x00
  // even after we have tried to enable ack payload. This mean that we need to
  // activate the features.
  if(hal_nrf_read_reg(FEATURE) == 0x00 && (hal_nrf_read_reg(DYNPD) == 0x00))
  {
    hal_nrf_lock_unlock ();                      // Activate features
    hal_nrf_enable_ack_pl();                     // Enables payload in ack
  }

  hal_nrf_enable_dynamic_pl();                   // Enables dynamic payload
  hal_nrf_setup_dyn_pl(ALL_PIPES);               // Sets up dynamic payload on
                                                 // all data pipes.
/*****************************************************************************
 * End changes from esb/radio_esb.c                                          *
 *****************************************************************************/
   
  if(operational_mode == HAL_NRF_PTX)            // Mode depentant settings
  {
    hal_nrf_set_operation_mode(HAL_NRF_PTX);     // Enter TX mode
  }
  else
  {
    hal_nrf_set_operation_mode(HAL_NRF_PRX);     // Enter RX mode
    hal_nrf_set_rx_pload_width((uint8_t)HAL_NRF_PIPE0, RF_PAYLOAD_LENGTH);
                                                 // Pipe0 expect 
                                                 // PAYLOAD_LENGTH byte payload
                                                 // PAYLOAD_LENGTH in radio.h
  }

  hal_nrf_set_rf_channel(RF_CHANNEL);            // Operating on static channel
                                                 // Defined in radio.h. 
                                                 // Frequenzy = 
                                                 //        2400 + RF_CHANNEL
  hal_nrf_set_power_mode(HAL_NRF_PWR_UP);        // Power up device
  
  start_timer(RF_POWER_UP_DELAY);                // Wait for the radio to 
  wait_for_timer();                              // power up
  
  radio_set_status (RF_IDLE);                    // Radio now ready
}    
/* Ma-ma-ma-main function! */
void main()
{
  state_t state = LISTENING;
  command_t cmd = CMD_NO_CMD;
	firmware_start firmware;

  uint16_t channel_timer = 0, bootloader_timer = 0, connection_timer = 0;
  uint8_t ch_i = 0, firmware_number = 0;
  bool running;
	
  uint16_t bytes_received = 0;
  uint16_t bytes_total = 0;

  uint8_t ea_default, rf_default;

  // Disable RF interrupt
  rf_default = RF;
  RF = 0;
  // Disable global interrupt
  ea_default = EA;
  EA = 0;
  
  // Set up parameters for RF communication.
  configureRF();

  #ifdef DEBUG_LED_
  P0DIR = 0;
  P0 = 0x55;
  #endif 

  running = true;
  // Boot loader loop.
  // Will terminate after a couple of seconds if firmware has been successfully
  // installed.
  while (running) {
		
    // Polls the RF-interrupt bit every iteration. 
    if (RFF) {
      RFF = 0;
      nrf_irq();

      if (packet_received) {
        packet_received = false;
        connection_timer = 0;
        cmd = MSG_CMD;
     
        switch (cmd) {
          // Host initiates contact with the device.
          case CMD_INIT:
            // Send ACK to host, go to CONNECTED state if successful.
            sendInitAck(&state);
            // Reset timers 
            channel_timer = bootloader_timer = 0;
            break;

          // Host starts a firmware update.
          case CMD_UPDATE_START:
            if (state == CONNECTED) {
              // Initiate firmware updates, go to RECEIVING_FIRMWARE state
              // if successful.
              startFirmwareUpdate(&state, &bytes_total, &bytes_received, 
                                     &firmware_number);
            }

            #ifdef DEBUG_LED_
            P0 = state;
            #endif 
            break;

          // Write message containing one hex record.
          case CMD_WRITE:
            if (state == RECEIVING_FIRMWARE) {
              writeHexRecord(&state, &bytes_received); 
            }

            #ifdef DEBUG_LED_
            P0 = 0x40;
            #endif
            break;

          // Firmware update has been completed.
          case CMD_UPDATE_COMPLETE:
            CE_LOW();
            // Check that every byte is received.
            if (bytes_received == bytes_total) {
              // Mark firmware as successfully installed. 
              hal_flash_byte_write(FW_INSTALLED, 0x01);
              hal_flash_byte_write(FW_NUMBER, firmware_number); 
              state = CONNECTED;
              send(CMD_ACK);
            } else {
              send(CMD_NACK);
            }

            if (!send_success) {
              state = ERROR;
            }

            #ifdef DEBUG_LED_
            P0 = 0x10;
            #endif
            break;

          // Host request data from flash at specified address.
          case CMD_READ:
            readHexRecord();

            #ifdef DEBUG_LED_
            P0 = 0x20;
            #endif
            break;

          // Host sends ping to check connections with device.
          case CMD_PING:
            if (state != LISTENING) {
              send(CMD_PONG);
            }

            #ifdef DEBUG_LED_
            P0 = 0x80;
            #endif
            break;

          // Host sends disconnect
          case CMD_EXIT:
            state = LISTENING;
            break;

          // These commands should no be received.
          case CMD_NO_CMD:
          default:
            state = ERROR;
            break;
        }
        // Clear command
        cmd = CMD_NO_CMD;
      }

    // RF interrupt bit not set
    } else if (state == LISTENING) {
      // Will listen to one channel for 'a while' before changing.
      channel_timer++;
      if (channel_timer > CHANNEL_TIMEOUT) {
        channel_timer = 0;
        // Go to next channel
        ch_i = (ch_i+1)%3;
        hal_nrf_set_rf_channel(default_channels[ch_i]);

        #ifdef DEBUG_LED_
        P0 = ch_i;
        #endif

        // After changing channels and being in the LISTENING state
        // for 'a while', boot loader loop will check if there is firmware
        // installed, and if so end the while(running) loop.
        bootloader_timer++;
        if (bootloader_timer > BOOTLOADER_TIMEOUT) {
          bootloader_timer = 0;
          running = (hal_flash_byte_read(FW_INSTALLED) == 0x01) ? false : true;
        }
      }

    // While connected must receive something or connection times out.
    // Connection timer reset when packet received.
    } else if (state == CONNECTED) {
      connection_timer++;
      if (connection_timer > CONNECTION_TIMEOUT) {
        state = LISTENING;
      }
    }
	} 

  resetRF();

  #ifdef DEBUG_LED_
  // Default value for P0DIR
  P0 = 0x00;
  P0DIR = 0xFF;
  #endif

  EA = ea_default;
  RF = rf_default;

  // Reads address of firmware's reset vector.
  temp_data[0] = hal_flash_byte_read(FW_RESET_ADDR_H);
  temp_data[1] = hal_flash_byte_read(FW_RESET_ADDR_L);
	firmware = (firmware_start)(((uint16_t)temp_data[0]<<8) | (temp_data[1]));
	
  // Jump to firmware. Goodbye!
	firmware();
}
Exemple #13
0
void main(void) {
	int i = 0; // local counter
	int total_pkt_count = 1;
	int addr_width = 5;
	int customized_plen = 0;
	int pipe_num = 6;
	epl_rf_en_auto_ack_t auto_ack = 0;
	//int mode = 1;		// 1 for sender mode, 2 for dumper mode

	// new params
	unsigned char pipe_source;		// used to store pipe_source number
	unsigned char ACKbuf[] = "ACK";
	unsigned char temp_buf[34]; // used for dumper to get RF packets from RX FIFO
	unsigned char temp_addr[5];
	unsigned char data_length = 0;
	unsigned char dynpd_pipe;
	unsigned char addr_buf[5];
	// set pin direction
	P0EXP = 0x00;
	P0ALT = 0x00;
	P0DIR = 0x00;

	// uart init
	epl_uart_init(UART_BAUD_57K6);
	//init usb connection
	usb_init(); // Initialize USB
	EA = 1; // Enable global IRQ
	//Start RF tx mode
	epl_rf_en_quick_init(cfg);

	//Clear TX FIFO
	hal_nrf_write_reg(FLUSH_TX, 0);
	hal_nrf_write_reg(FLUSH_RX, 0);

	hal_nrf_lock_unlock();
	hal_nrf_enable_dynamic_pl();

	LED_Blink(20);
	epl_uart_putstr("start!");
	while (1) {
		usb_recv_packet();
		switch (ubuf[1]) {

		case EPL_SENDER_MODE:
			customized_plen = 0;
			for (i = 0; i < PLOAD_LEN; i++)
				packet[i] = i + 9;
			hal_nrf_close_pipe(HAL_NRF_PIPE1);
			hal_nrf_close_pipe(HAL_NRF_PIPE2);
			hal_nrf_close_pipe(HAL_NRF_PIPE3);
			hal_nrf_close_pipe(HAL_NRF_PIPE4);
			hal_nrf_close_pipe(HAL_NRF_PIPE5);
			break;

		case EPL_DUMPER_MODE:

			hal_nrf_close_pipe(HAL_NRF_PIPE1);
			hal_nrf_close_pipe(HAL_NRF_PIPE2);
			hal_nrf_close_pipe(HAL_NRF_PIPE3);
			hal_nrf_close_pipe(HAL_NRF_PIPE4);
			hal_nrf_close_pipe(HAL_NRF_PIPE5);
			break;

		case EPL_OUTPUT_POWER: 			//Host:set_output_power
			hal_nrf_set_output_power(ubuf[2]);
			epl_uart_putstr("EPL_OUTPUT_POWER\n");
			usb_send_packet(ACKbuf, 3);
			epl_uart_putstr("EPL_OUTPUT_POWER     END\n");
			break;

		case EPL_CHANNEL:
			hal_nrf_set_rf_channel(ubuf[2]);
			usb_send_packet(ACKbuf, 3);
			break;

		case EPL_DATARATE:
			hal_nrf_set_datarate(ubuf[2]);
			usb_send_packet(ACKbuf, 3);
			break;

		case EPL_ADDR_WIDTH:
			addr_width = (int) ubuf[2];
			hal_nrf_set_address_width(ubuf[2]);
			usb_send_packet(ACKbuf, 3);
			break;

		case EPL_AUTOACK_P0:
			auto_ack = ubuf[2];
			usb_send_packet(ACKbuf, 3);
			break;

		case EPL_AUTOACK_P1:
			auto_ack = ubuf[2];
			usb_send_packet(ACKbuf, 3);
			break;

		case EPL_AUTOACK_P2:
			auto_ack = ubuf[2];
			usb_send_packet(ACKbuf, 3);
			break;

		case EPL_AUTOACK_P3:
			auto_ack = ubuf[2];
			usb_send_packet(ACKbuf, 3);
			break;

		case EPL_AUTOACK_P4:
			auto_ack = ubuf[2];
			usb_send_packet(ACKbuf, 3);
			break;

		case EPL_AUTOACK_P5:
			auto_ack = ubuf[2];
			usb_send_packet(ACKbuf, 3);
			break;

		case EPL_DATA_LENGTH_P0:
			data_length = (int) ubuf[2];
			epl_rf_en_rcv_pipe_config(HAL_NRF_PIPE0, temp_addr, data_length, auto_ack);
			usb_send_packet(ACKbuf, 3);
			break;

		case EPL_DATA_LENGTH_P1:
			data_length = (int) ubuf[2];
			epl_rf_en_rcv_pipe_config(HAL_NRF_PIPE1, temp_addr, data_length, auto_ack);
			usb_send_packet(ACKbuf, 3);
			break;

		case EPL_DATA_LENGTH_P2:
			data_length = (int) ubuf[2];
			epl_rf_en_rcv_pipe_config(HAL_NRF_PIPE2, temp_addr, data_length, auto_ack);
			usb_send_packet(ACKbuf, 3);
			break;

		case EPL_DATA_LENGTH_P3:
			data_length = (int) ubuf[2];
			epl_rf_en_rcv_pipe_config(HAL_NRF_PIPE3, temp_addr, data_length, auto_ack);
			usb_send_packet(ACKbuf, 3);
			break;


		case EPL_DATA_LENGTH_P4:
			data_length = (int) ubuf[2];
			epl_rf_en_rcv_pipe_config(HAL_NRF_PIPE4, temp_addr, data_length, auto_ack);
			usb_send_packet(ACKbuf, 3);
			break;

		case EPL_DATA_LENGTH_P5:
			data_length = (int) ubuf[2];
			epl_rf_en_rcv_pipe_config(HAL_NRF_PIPE5, temp_addr, data_length, auto_ack);
			usb_send_packet(ACKbuf, 3);
			break;

		case EPL_CRC_MODE:
			if (ubuf[2] == 0)
				hal_nrf_set_crc_mode(HAL_NRF_CRC_OFF);
			else if (ubuf[2] == 2)
				hal_nrf_set_crc_mode(HAL_NRF_CRC_8BIT);
			else if (ubuf[2] == 3)
				hal_nrf_set_crc_mode(HAL_NRF_CRC_16BIT);
			else
				;

			usb_send_packet(ACKbuf, 3);
			break;

		case EPL_RX_ADDR_P0:
			for (i = 0; i < addr_width; i++) {
				temp_addr[i] = ubuf[i + 2];
			}
			hal_nrf_set_address(HAL_NRF_PIPE0, temp_addr);
			epl_rf_en_set_dst_addr(temp_addr);

			usb_send_packet(ACKbuf, 3);
			break;

		case EPL_RX_ADDR_P1:
			for (i = 0; i < addr_width; i++) {
				temp_addr[i] = ubuf[i + 2];
			}
			usb_send_packet(ACKbuf, 3);
			break;
		case EPL_RX_ADDR_P2:
			for (i = 0; i < addr_width; i++) {
				temp_addr[i] = ubuf[i + 2];
			}
			usb_send_packet(ACKbuf, 3);
			break;
		case EPL_RX_ADDR_P3:
			for (i = 0; i < addr_width; i++) {
				temp_addr[i] = ubuf[i + 2];
			}
			usb_send_packet(ACKbuf, 3);
			break;
		case EPL_RX_ADDR_P4:
			for (i = 0; i < addr_width; i++) {
				temp_addr[i] = ubuf[i + 2];
			}
			usb_send_packet(ACKbuf, 3);
			break;
		case EPL_RX_ADDR_P5:
			for (i = 0; i < addr_width; i++) {
				temp_addr[i] = ubuf[i + 2];
			}
			usb_send_packet(ACKbuf, 3);
			break;

		case EPL_USER_PLOAD:
			if (ubuf[2] == USRS_PLOAD) {
				customized_plen = (int) ubuf[3];

				for (i = 0; i < customized_plen; i++) {
					packet[i] = ubuf[i + 4];
				}
			} else {
				customized_plen = 0;

				for (i = 0; i < PLOAD_LEN; i++) {
					packet[i] = i + 9;
				}
			}

			usb_send_packet(ACKbuf, 3);
			break;

		case EPL_NEW_COUNTER:
			total_pkt_count = 1;
			usb_send_packet(ACKbuf, 3);
			break;

		/*20110221 celine*/
		case EPL_DYNAMIC_PD:
			dynpd_pipe = (int)ubuf[2];
			if ((int)ubuf[3] == 01){
				//hal_nrf_setup_dyn_pl(dynpd_pipe);
				hal_nrf_write_reg(DYNPD, (1<<dynpd_pipe) | hal_nrf_read_reg(DYNPD));
			} else {
				//hal_nrf_lock_unlock();
				//hal_nrf_enable_dynamic_pl();
				hal_nrf_write_reg(DYNPD, ~(1<<dynpd_pipe) & hal_nrf_read_reg(DYNPD));
			}

			usb_send_packet(ACKbuf, 3);
			break;
		/**/
		case EPL_RUN_SENDER:
			epl_rf_en_enter_tx_mode();
			// clear Tx irq
			hal_nrf_clear_irq_flag(HAL_NRF_TX_DS);
			hal_nrf_clear_irq_flag(HAL_NRF_MAX_RT);

			if (ubuf[2] == AUTO_PLOAD) {
				epl_uart_putstr("\nauto pload\r\n");
				packet[0] = total_pkt_count++;
				epl_rf_en_send(packet, data_length);

			} else {
				epl_uart_putstr("\nusrs pload\r\n");
				epl_rf_en_send(packet, customized_plen);
			}
			LED_Blink(10);

			array_cp(temp_buf, ACKbuf, 3);
			temp_buf[3] = hal_nrf_read_reg(OBSERVE_TX) & 0x0F;
			usb_send_packet(temp_buf, 4);
			epl_rf_en_enter_rx_mode();
			break;

		case EPL_RUN_DUMPER:
			hal_nrf_clear_irq_flag(HAL_NRF_RX_DR);
			hal_nrf_flush_rx();
			epl_rf_en_enter_rx_mode();
			while (1) {
				if (ubuf[1] == 0xf5) { // Host wants to terminate
					epl_uart_putstr("Terminate !\r\n");
					break;
				}else if (hal_nrf_rx_fifo_empty() == 0) { // Rx_fifo is not empty
					LED0_Toggle();
					pipe_source = hal_nrf_get_rx_data_source();
					hal_nrf_read_rx_pload(temp_buf);

					// pending the data source on last byte
					temp_buf[32] = pipe_source;
					if(hal_nrf_read_reg(DYNPD)>>(int)pipe_source)
						temp_buf[33] = hal_nrf_read_reg(RD_RX_PLOAD_W);
					else
						temp_buf[33] = hal_nrf_read_reg(RX_PW_P0+pipe_source);
//					epl_uart_putstr("temp_buf[33] = ");
//					epl_uart_puthex(temp_buf[33]);
//					epl_uart_putstr("\r\n");
					usb_send_packet(temp_buf, 34);

					if((hal_nrf_read_reg(STATUS))&0x10){
						hal_nrf_write_reg(FLUSH_TX, 0);
					}
					LED0_Toggle();
				}
			}
			break;

		case EPL_SHOW_CONFIG:
			epl_uart_putstr("\r\n0. CONFIG = ");
			epl_uart_puthex(hal_nrf_read_reg(CONFIG));
			epl_uart_putstr("\r\n1. RF_CH = ");
			epl_uart_puthex(hal_nrf_read_reg(RF_CH));
			epl_uart_putstr("\r\n2. EN_AA = ");
			epl_uart_puthex(hal_nrf_read_reg(EN_AA));
			epl_uart_putstr("\r\n3. EN_RXADDR = ");
			epl_uart_puthex(hal_nrf_read_reg(EN_RXADDR));
			epl_uart_putstr("\r\n4. TX_ADDR = ");
			hal_nrf_read_multibyte_reg(HAL_NRF_TX, addr_buf);
			epl_uart_puthex(addr_buf[0]);
			epl_uart_puthex(addr_buf[1]);
			epl_uart_puthex(addr_buf[2]);
			epl_uart_puthex(addr_buf[3]);
			epl_uart_puthex(addr_buf[4]);
			epl_uart_putstr("\r\n4. RX_ADDR_PO = ");
			hal_nrf_read_multibyte_reg(HAL_NRF_PIPE0, addr_buf);
			epl_uart_puthex(addr_buf[0]);
			epl_uart_puthex(addr_buf[1]);
			epl_uart_puthex(addr_buf[2]);
			epl_uart_puthex(addr_buf[3]);
			epl_uart_puthex(addr_buf[4]);
			epl_uart_putstr("\r\n   RX_ADDR_P1 = ");
			hal_nrf_read_multibyte_reg(HAL_NRF_PIPE1, addr_buf);
			epl_uart_puthex(addr_buf[0]);
			epl_uart_puthex(addr_buf[1]);
			epl_uart_puthex(addr_buf[2]);
			epl_uart_puthex(addr_buf[3]);
			epl_uart_puthex(addr_buf[4]);
			epl_uart_putstr("\r\n   RX_ADDR_P2 = ");
			epl_uart_puthex(hal_nrf_read_reg(RX_ADDR_P2));
			epl_uart_putstr("\r\n   RX_ADDR_P3 = ");
			epl_uart_puthex(hal_nrf_read_reg(RX_ADDR_P3));
			epl_uart_putstr("\r\n   RX_ADDR_P4 = ");
			epl_uart_puthex(hal_nrf_read_reg(RX_ADDR_P4));
			epl_uart_putstr("\r\n   RX_ADDR_P5 = ");
			epl_uart_puthex(hal_nrf_read_reg(RX_ADDR_P5));
			epl_uart_putstr("\r\n5. RX_PW_P0 = ");
			epl_uart_puthex(hal_nrf_read_reg(RX_PW_P0));
			epl_uart_putstr("\r\n   RX_PW_P1 = ");
			epl_uart_puthex(hal_nrf_read_reg(RX_PW_P1));
			epl_uart_putstr("\r\n   RX_PW_P2 = ");
			epl_uart_puthex(hal_nrf_read_reg(RX_PW_P2));
			epl_uart_putstr("\r\n   RX_PW_P3 = ");
			epl_uart_puthex(hal_nrf_read_reg(RX_PW_P3));
			epl_uart_putstr("\r\n   RX_PW_P4 = ");
			epl_uart_puthex(hal_nrf_read_reg(RX_PW_P4));
			epl_uart_putstr("\r\n   RX_PW_P5 = ");
			epl_uart_puthex(hal_nrf_read_reg(RX_PW_P5));
			epl_uart_putstr("\r\n6. RF_SETUP = ");
			epl_uart_puthex(hal_nrf_read_reg(RF_SETUP));
			epl_uart_putstr("\r\n7. STATUS = ");
			epl_uart_puthex(hal_nrf_read_reg(STATUS));
			epl_uart_putstr("\r\n8 .DYNPD = ");
			epl_uart_puthex(hal_nrf_read_reg(DYNPD));
			epl_uart_putstr("\r\n9. FEATURE = ");
			epl_uart_puthex(hal_nrf_read_reg(FEATURE));
		default:
			break;
		}// end switch case
Exemple #14
0
int main(void)
{
    int ch;
    bool_t eol=FALSE, spi_ini=FALSE, gpio_ini=FALSE;

    spi_hndl_t spi_h;
    gpio_hndl_t gpio_h;

    if (gpio_init(&gpio_h, gpio_drv_io)!=LREC_SUCCESS) goto finish;
    else gpio_ini=TRUE;

    gpio_direction_output(&gpio_h, GPIO_CE, 0);

    if (spi_init(&spi_h, 0, SPI_CS, SPI_MODE_0, FALSE, 8,
        SPI_USE_DEF, SPI_USE_DEF, SPI_USE_DEF)!=LREC_SUCCESS) goto finish;
    else spi_ini=TRUE;

    signal(SIGINT, term_handler);
    signal(SIGTERM, term_handler);

    errno = 0;
    hal_nrf_set_spi_hndl(&spi_h);

    hal_nrf_set_power_mode(HAL_NRF_PWR_UP);
    usleep(1500);

    NRF_EXEC(hal_nrf_set_operation_mode(HAL_NRF_PRX));

    for (ch=0; ch<128 && !scan_finish; ch++)
    {
        int i;

        if (eol) printf("\n");

        NRF_EXEC(hal_nrf_set_rf_channel(ch));
        assert(hal_nrf_get_rf_channel()==ch);

        chip_enable();

        for (i=0; i<500; i++) {
            if (hal_nrf_get_carrier_detect()) break;
            usleep(1000);
        }

        if (i<500) {
            if (!eol) printf("\n");
            printf("Carrier detected on channel %d", ch);
            eol=TRUE;
        } else {
            printf(".");
            fflush(stdout);
            eol = (!((ch+1)%10) ? TRUE : FALSE);
        }

        chip_disable();
    }
    if (!eol) printf("\n");

finish:
    if (errno==ECOMM) printf("SPI communication error\n");
    if (spi_ini) spi_free(&spi_h);
    if (gpio_ini) gpio_free(&gpio_h);

    return 0;
}