Čeština / English
Login

SVN Repository / Prohlížení

Aktuální adresář: FITkit / trunk / mcu / libs / nrf24l01 /

nrf24l01_com.c

   1  /*******************************************************************************
   2     nrf24l01_com.h: NRF24L01 high-level API
   3     Copyright (C) 2010 Brno University of Technology,
   4                        Faculty of Information Technology
   5     Author(s): Zdenek Vasicek <vasicek AT fit.vutbr.cz>
   6  
   7     LICENSE TERMS
   8  
   9     Redistribution and use in source and binary forms, with or without
  10     modification, are permitted provided that the following conditions
  11     are met:
  12     1. Redistributions of source code must retain the above copyright
  13        notice, this list of conditions and the following disclaimer.
  14     2. Redistributions in binary form must reproduce the above copyright
  15        notice, this list of conditions and the following disclaimer in
  16        the documentation and/or other materials provided with the
  17        distribution.
  18     3. All advertising materials mentioning features or use of this software
  19        or firmware must display the following acknowledgement:
  20  
  21          This product includes software developed by the University of
  22          Technology, Faculty of Information Technology, Brno and its
  23          contributors.
  24  
  25     4. Neither the name of the Company nor the names of its contributors
  26        may be used to endorse or promote products derived from this
  27        software without specific prior written permission.
  28  
  29     This software or firmware is provided ``as is'', and any express or implied
  30     warranties, including, but not limited to, the implied warranties of
  31     merchantability and fitness for a particular purpose are disclaimed.
  32     In no event shall the company or contributors be liable for any
  33     direct, indirect, incidental, special, exemplary, or consequential
  34     damages (including, but not limited to, procurement of substitute
  35     goods or services; loss of use, data, or profits; or business
  36     interruption) however caused and on any theory of liability, whether
  37     in contract, strict liability, or tort (including negligence or
  38     otherwise) arising in any way out of the use of this software, even
  39     if advised of the possibility of such damage.
  40  
  41     $Id$
  42  
  43  
  44  *******************************************************************************/
  45  
  46  #include "fspi.h"
  47  #include "nrf24l01_spi.h"
  48  #include "nrf24l01_com.h"
  49  
  50  char tx_active;
  51  char reset_rx0addr;
  52  
  53  /**
  54   \brief Inicializace NRF24L01
  55   **/
  56  char NRF_init(){
  57    unsigned char status;
  58    char buf[32];
  59  
  60    tx_active = 0;
  61    reset_rx0addr = 0;
  62  
  63    // Discard transmission
  64    RF_CE_clr();
  65  
  66    delay_ms(200);
  67  
  68    // power down
  69    status = RF_reg_write(NRF_CONFIG, 0x00);
  70  
  71    // pockat min 1.5ms
  72    delay_ms(5);
  73  
  74    // Flush TX FIFO
  75    RF_cmd_write(FLUSH_TX, 0, 0);
  76    // Flush RX FIFO
  77    RF_cmd_write(FLUSH_RX, 0, 0);
  78  
  79    // Channel 1
  80    RF_reg_write(NRF_RF_CH, 0x01);
  81  
  82    // Write CONFIG register (addres - 0x00)
  83    // 00001010 - CRC enable, power-up, prim_RX
  84    RF_reg_write(NRF_CONFIG, 0x0B);
  85  
  86    // pockat min 1.5ms
  87    delay_ms(5);
  88  
  89    // Clear TX_DS bit in status register
  90    RF_reg_write(NRF_STATUS, TX_DS | MAX_RT | RX_DR);
  91    // Flush TX FIFO
  92    RF_cmd_write(FLUSH_TX, 0, 0);
  93    // Flush RX FIFO
  94    RF_cmd_write(FLUSH_RX, 0, 0);
  95  
  96    status = RF_reg_write(NRF_CONFIG, 0x0B);
  97    if (status == 0x0E) return 1;
  98  
  99    term_send_str("NRF_setup failed, status: "); term_send_hex(status); term_send_crlf();
 100  
 101    return 0;
 102  }
 103  
 104  void NRF_config(char channel, char payload_len, char recv_addr[5], char rst_rx0addr)
 105  {
 106     reset_rx0addr = rst_rx0addr;
 107  
 108     // Select RF channel
 109     RF_reg_write(NRF_RF_CH, channel);
 110  
 111     // Set payload length
 112  	RF_reg_write(NRF_RX_PW_P0, payload_len & 0xF);
 113  	RF_reg_write(NRF_RX_PW_P1, payload_len & 0xF);
 114  
 115     //Write RX_ADDR_P1 register -> Set receive address data Pipe1 -> address in RX_ADDRESS_P1 array
 116     RF_reg_write_buf(NRF_RX_ADDR_P1, &(recv_addr[0]), 5);
 117  
 118     //enable auto ack
 119     RF_reg_write(NRF_EN_AA, 0x2F);
 120     //enable retransmit (15x)
 121     RF_reg_write(NRF_SETUP_RETR, 0x0F);
 122  
 123     // Start receiver
 124     NRF_enableRx();
 125  
 126     // Flush RX FIFO
 127     RF_cmd_write(FLUSH_RX, 0, 0);
 128  }
 129  
 130  void NRF_enableRx()
 131  {
 132     // Start receiver
 133     tx_active = 0;
 134     RF_CE_clr();
 135     RF_reg_write(NRF_CONFIG, 0x0B);
 136     if (reset_rx0addr)
 137        RF_reg_write_bytes(NRF_RX_ADDR_P0, 0, 5);
 138     RF_CE_set();
 139     RF_reg_write(NRF_STATUS, (TX_DS | MAX_RT));
 140  }
 141  
 142  
 143  inline void NRF_setTXAddress(char tx_addr[5])
 144  {
 145     //Write RX_ADDR_P0 register -> Set receive address data Pipe0 -> address in RX_ADDRESS_P0 array
 146     RF_reg_write_buf(NRF_RX_ADDR_P0, &(tx_addr[0]), 5);
 147  
 148     //Write TX_ADDR register -> Transmit address. Used for a PTX device only. Address in TX_ADDRESS array
 149     RF_reg_write_buf(NRF_TX_ADDR, &(tx_addr[0]), 5);
 150  }
 151  
 152  
 153  inline char NRF_dataReady()
 154  {
 155     unsigned char status;
 156     status = RF_cmd_write(NRF_NOP, 0, 0);
 157     return (status & RX_DR);
 158  }
 159  
 160  
 161  inline char NRF_getByte()
 162  {
 163     char buf[16];
 164     //get payload
 165     RF_cmd_read(R_RX_PAYLOAD, &(buf[0]), 1);
 166     //reset status bit
 167     RF_reg_write(NRF_STATUS, RX_DR);
 168     return buf[0];
 169  }
 170  
 171  inline void NRF_sendByte(char byte)
 172  {
 173      unsigned char status; int t;
 174  
 175      status = RF_cmd_write(NRF_NOP, 0, 0);
 176  
 177      //cekani, az je odeslan predchozi paket
 178      while (tx_active) {
 179  	    if ((status & (TX_DS | MAX_RT)) != 0) {
 180  		    tx_active = 0;
 181  		    break;
 182  	    }
 183         status = RF_cmd_write(NRF_NOP, 0, 0);
 184      }
 185  
 186      RF_CE_clr();
 187  
 188      tx_active = 1;
 189  
 190      RF_reg_write(NRF_CONFIG, 0x0A);
 191      RF_cmd_write(FLUSH_TX, 0, 0);
 192  
 193      status = RF_cmd_write(W_TX_PAYLOAD, &byte, 1);
 194  
 195      //odeslat data
 196      RF_CE_set();
 197  }
 198  
 199  inline char NRF_isSending()
 200  {
 201     unsigned char status;
 202  
 203     if (tx_active) {
 204        status = RF_cmd_write(NRF_NOP, 0, 0);
 205        if ((status & (TX_DS | MAX_RT)) != 0) {
 206  			NRF_enableRx();
 207  			return 0;
 208  		}
 209        return 1;
 210  	}
 211  	return 0;
 212  }
 213  
 214  inline char NRF_isRxEmpty()
 215  {
 216     unsigned char regd;
 217  
 218     RF_reg_read(NRF_FIFO_STATUS, &regd, 1);
 219  	return (regd & 1);
 220  }
 221  
Zobrazeno: 597130x Naposledy: 27.1.2022 14:55:52