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

📄 radio.c

📁 非常全的nrf2401设计资料
💻 C
字号:
/* Copyright (c) 2007 Nordic Semiconductor. All Rights Reserved.
 *
 * The information contained herein is confidential property of Nordic Semiconductor. The use,
 * copying, transfer or disclosure of such information is prohibited except by express written
 * agreement with Nordic Semiconductor.
 *
 * $LastChangedRevision: 2059 $
 */

 /** @file
 * Radio functions for the nRF24LU1 example application.
 *
 * This file handles all radio communication for the example application, i.e. 
 * radio_init, radio_send_packet and radio_interrupt function.
 * @author Runar Kjellhaug
 *
 */

#include "hal_nrf.h"
#include "system.h"
#include "radio.h"

code const uint8_t address[HAL_NRF_AW_5BYTES] = {0x22,0x33,0x44,0x55,0x01};
uint8_t pload[1];

extern bool device_op_mode;                       // select between PRX & PTX device
uint8_t radio_status;                             // global radio status byte; 
                                                  // ready, busy, TX_DS, MAX_RT or RX_DR
// Local prototype
void nrf_spi_init(void);

void radio_init(void)
{
  nrf_spi_init();                                 // init radio's SPI interface

  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, with autoack

  hal_nrf_set_crc_mode(HAL_NRF_CRC_16BIT);        // operates in 16bits CRC mode
  hal_nrf_set_auto_retr(3, 250);                  // 250 祍 delay, 3 retransmits

  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);    // Pipe0 used for auto ACK

  // device_op_mode initialized in system_init()
  if(device_op_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(HAL_NRF_PIPE0, 1); // pipe0 expect 1 byte payload
  }
  
  hal_nrf_set_rf_channel(40);                     // operating on static channel: 40 (2440MHz)
  hal_nrf_set_power_mode(HAL_NRF_PWR_UP);         // power up device
  
  RF = 1;                                         // enable RF interrupt
  radio_status = RF_IDLE;                         // radio now ready, i.e. RF_IDLE
}

void nrf_spi_init(void)
{
  RFCKEN = 1;                                     // enable L01 clock
  RFCTL = 0x10;                                   // L01 SPI speed = max (CK/2) & SPI enable
}

void radio_send_packet(uint8_t command)
{
  pload[0] = command;                         // create message
  hal_nrf_write_tx_pload(pload, 1);           // load message into radio
  
  CE_PULSE();                                 // send packet

  radio_status = RF_BUSY;                     // trans. in progress; RF_BUSY
}

void radio_irq(void) interrupt RF_READY_INT_VECT  
{
  switch(hal_nrf_get_clear_irq_flags())
  {
    case (1<<HAL_NRF_MAX_RT):                     // max retries reach LED2_flash
      hal_nrf_flush_tx();                         // simply flush tx fifo, avoid fifo jamming
      radio_status = HAL_NRF_MAX_RT;
      break;
    
    case (1<<HAL_NRF_TX_DS):                      // packet sent, LED1_flash      
      radio_status = HAL_NRF_TX_DS;
      break;
    
    case (1<<HAL_NRF_RX_DR):                      // packet received, LED1_flash
      hal_nrf_read_rx_pload(pload);
      
      radio_status = HAL_NRF_RX_DR;
      break;
      
    default:
      break;    
  }
}

⌨️ 快捷键说明

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