?? radio.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 + -