| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- #pragma once
- #include <stdbool.h>
- #include <stdint.h>
- #include <furi_hal_spi.h>
- #include <momentum/momentum.h>
- #ifdef __cplusplus
- extern "C" {
- #endif
- #define R_REGISTER 0x00
- #define W_REGISTER 0x20
- #define REGISTER_MASK 0x1F
- #define ACTIVATE 0x50
- #define R_RX_PL_WID 0x60
- #define R_RX_PAYLOAD 0x61
- #define W_TX_PAYLOAD 0xA0
- #define W_TX_PAYLOAD_NOACK 0xB0
- #define W_ACK_PAYLOAD 0xA8
- #define FLUSH_TX 0xE1
- #define FLUSH_RX 0xE2
- #define REUSE_TX_PL 0xE3
- #define RF24_NOP 0xFF
- #define REG_CONFIG 0x00
- #define REG_EN_AA 0x01
- #define REG_EN_RXADDR 0x02
- #define REG_SETUP_AW 0x03
- #define REG_SETUP_RETR 0x04
- #define REG_DYNPD 0x1C
- #define REG_FEATURE 0x1D
- #define REG_RF_SETUP 0x06
- #define REG_STATUS 0x07
- #define REG_RX_ADDR_P0 0x0A
- #define REG_RX_ADDR_P1 0x0B
- #define REG_RX_ADDR_P2 0x0C
- #define REG_RX_ADDR_P3 0x0D
- #define REG_RX_ADDR_P4 0x0E
- #define REG_RX_ADDR_P5 0x0F
- #define REG_RF_CH 0x05
- #define REG_TX_ADDR 0x10
- #define REG_FIFO_STATUS 0x17
- #define RX_PW_P0 0x11
- #define RX_PW_P1 0x12
- #define RX_PW_P2 0x13
- #define RX_PW_P3 0x14
- #define RX_PW_P4 0x15
- #define RX_PW_P5 0x16
- #define RX_DR 0x40
- #define TX_DS 0x20
- #define MAX_RT 0x10
- #define nrf24_TIMEOUT 500
- #define nrf24_CE_PIN &gpio_ext_pb2
- #define nrf24_HANDLE \
- (momentum_settings.spi_nrf24_handle == SpiDefault ? &furi_hal_spi_bus_handle_external : \
- &furi_hal_spi_bus_handle_external_extra)
- /* Low level API */
- /** Write device register
- *
- * @param handle - pointer to FuriHalSpiHandle
- * @param reg - register
- * @param data - data to write
- *
- * @return device status
- */
- uint8_t nrf24_write_reg(const FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t data);
- /** Write buffer to device register
- *
- * @param handle - pointer to FuriHalSpiHandle
- * @param reg - register
- * @param data - data to write
- * @param size - size of data to write
- *
- * @return device status
- */
- uint8_t nrf24_write_buf_reg(const FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t* data, uint8_t size);
- /** Read device register
- *
- * @param handle - pointer to FuriHalSpiHandle
- * @param reg - register
- * @param[out] data - pointer to data
- *
- * @return device status
- */
- uint8_t nrf24_read_reg(const FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t* data, uint8_t size);
- /** Power up the radio for operation
- *
- * @param handle - pointer to FuriHalSpiHandle
- *
- * @return device status
- */
- uint8_t nrf24_power_up(const FuriHalSpiBusHandle* handle);
- /** Power down the radio
- *
- * @param handle - pointer to FuriHalSpiHandle
- *
- * @return device status
- */
- uint8_t nrf24_set_idle(const FuriHalSpiBusHandle* handle);
- /** Sets the radio to RX mode
- *
- * @param handle - pointer to FuriHalSpiHandle
- *
- * @return device status
- */
- uint8_t nrf24_set_rx_mode(const FuriHalSpiBusHandle* handle);
- /** Sets the radio to TX mode
- *
- * @param handle - pointer to FuriHalSpiHandle
- *
- * @return device status
- */
- uint8_t nrf24_set_tx_mode(const FuriHalSpiBusHandle* handle);
- /*=============================================================================================================*/
- /* High level API */
- /** Must call this before using any other nrf24 API
- *
- */
- void nrf24_init();
- /** Must call this when we end using nrf24 device
- *
- */
- void nrf24_deinit();
- /** Send flush rx command
- *
- * @param handle - pointer to FuriHalSpiHandle
- *
- * @return device status
- */
- uint8_t nrf24_flush_rx(const FuriHalSpiBusHandle* handle);
- /** Send flush tx command
- *
- * @param handle - pointer to FuriHalSpiHandle
- *
- * @return device status
- */
- uint8_t nrf24_flush_tx(const FuriHalSpiBusHandle* handle);
- /** Gets the RX packet length in data pipe 0
- *
- * @param handle - pointer to FuriHalSpiHandle
- * pipe - pipe index (0..5)
- * @return packet length in data pipe 0
- */
- uint8_t nrf24_get_packetlen(const FuriHalSpiBusHandle* handle, uint8_t pipe);
- /** Sets the RX packet length in data pipe 0
- *
- * @param handle - pointer to FuriHalSpiHandle
- * @param len - length to set
- *
- * @return device status
- */
- uint8_t nrf24_set_packetlen(const FuriHalSpiBusHandle* handle, uint8_t len);
- /** Gets configured length of MAC address
- *
- * @param handle - pointer to FuriHalSpiHandle
- *
- * @return MAC address length
- */
- uint8_t nrf24_get_maclen(const FuriHalSpiBusHandle* handle);
- /** Sets configured length of MAC address
- *
- * @param handle - pointer to FuriHalSpiHandle
- * @param maclen - length to set MAC address to, must be greater than 1 and less than 6
- *
- * @return MAC address length
- */
- uint8_t nrf24_set_maclen(const FuriHalSpiBusHandle* handle, uint8_t maclen);
- /** Gets the current status flags from the STATUS register
- *
- * @param handle - pointer to FuriHalSpiHandle
- *
- * @return status flags
- */
- uint8_t nrf24_status(const FuriHalSpiBusHandle* handle);
- /** Gets the current transfer rate
- *
- * @param handle - pointer to FuriHalSpiHandle
- *
- * @return transfer rate in bps
- */
- uint32_t nrf24_get_rate(const FuriHalSpiBusHandle* handle);
- /** Sets the transfer rate
- *
- * @param handle - pointer to FuriHalSpiHandle
- * @param rate - the transfer rate in bps
- *
- * @return device status
- */
- uint8_t nrf24_set_rate(const FuriHalSpiBusHandle* handle, uint32_t rate);
- /** Gets the current channel
- * In nrf24, the channel number is multiplied times 1MHz and added to 2400MHz to get the frequency
- *
- * @param handle - pointer to FuriHalSpiHandle
- *
- * @return channel
- */
- uint8_t nrf24_get_chan(const FuriHalSpiBusHandle* handle);
- /** Sets the channel
- *
- * @param handle - pointer to FuriHalSpiHandle
- * @param frequency - the frequency in hertz
- *
- * @return device status
- */
- uint8_t nrf24_set_chan(const FuriHalSpiBusHandle* handle, uint8_t chan);
- /** Gets the source mac address
- *
- * @param handle - pointer to FuriHalSpiHandle
- * @param[out] mac - the source mac address
- *
- * @return device status
- */
- uint8_t nrf24_get_src_mac(const FuriHalSpiBusHandle* handle, uint8_t* mac);
- /** Sets the source mac address
- *
- * @param handle - pointer to FuriHalSpiHandle
- * @param mac - the mac address to set
- * @param size - the size of the mac address (2 to 5)
- *
- * @return device status
- */
- uint8_t nrf24_set_src_mac(const FuriHalSpiBusHandle* handle, uint8_t* mac, uint8_t size);
- /** Gets the dest mac address
- *
- * @param handle - pointer to FuriHalSpiHandle
- * @param[out] mac - the source mac address
- *
- * @return device status
- */
- uint8_t nrf24_get_dst_mac(const FuriHalSpiBusHandle* handle, uint8_t* mac);
- /** Sets the dest mac address
- *
- * @param handle - pointer to FuriHalSpiHandle
- * @param mac - the mac address to set
- * @param size - the size of the mac address (2 to 5)
- *
- * @return device status
- */
- uint8_t nrf24_set_dst_mac(const FuriHalSpiBusHandle* handle, uint8_t* mac, uint8_t size);
- /** Reads RX packet
- *
- * @param handle - pointer to FuriHalSpiHandle
- * @param[out] packet - the packet contents
- * @param[out] ret_packetsize - size of the received packet
- * @param packet_size: >1 - size, 1 - packet length is determined by RX_PW_P0 register, 0 - it is determined by dynamic payload length command
- *
- * @return device status
- */
- uint8_t
- nrf24_rxpacket(const FuriHalSpiBusHandle* handle, uint8_t* packet, uint8_t* ret_packetsize, uint8_t packet_size_flag);
- /** Sends TX packet
- *
- * @param handle - pointer to FuriHalSpiHandle
- * @param packet - the packet contents
- * @param size - packet size
- * @param ack - boolean to determine whether an ACK is required for the packet or not
- *
- * @return device status
- */
- uint8_t nrf24_txpacket(const FuriHalSpiBusHandle* handle, uint8_t* payload, uint8_t size, bool ack);
- /** Configure the radio
- * This is not comprehensive, but covers a lot of the common configuration options that may be changed
- * @param handle - pointer to FuriHalSpiHandle
- * @param rate - transfer rate in Mbps (1 or 2)
- * @param srcmac - source mac address
- * @param dstmac - destination mac address
- * @param maclen - length of mac address
- * @param channel - channel to tune to
- * @param noack - if true, disable auto-acknowledge
- * @param disable_aa - if true, disable ShockBurst
- *
- */
- void nrf24_configure(
- const FuriHalSpiBusHandle* handle,
- uint8_t rate,
- uint8_t* srcmac,
- uint8_t* dstmac,
- uint8_t maclen,
- uint8_t channel,
- bool noack,
- bool disable_aa);
- // Set mac address (MSB first), Return: Status
- uint8_t nrf24_set_mac(uint8_t mac_addr, uint8_t* mac, uint8_t mlen);
- /** Configures the radio for "promiscuous mode" and primes it for rx
- * This is not an actual mode of the nrf24, but this function exploits a few bugs in the chip that allows it to act as if it were.
- * See http://travisgoodspeed.blogspot.com/2011/02/promiscuity-is-nrf24l01s-duty.html for details.
- * @param handle - pointer to FuriHalSpiHandle
- * @param channel - channel to tune to
- * @param rate - transfer rate in Mbps (1 or 2)
- */
- void nrf24_init_promisc_mode(const FuriHalSpiBusHandle* handle, uint8_t channel, uint8_t rate);
- /** Listens for a packet and returns first possible address sniffed
- * Call this only after calling nrf24_init_promisc_mode
- * @param handle - pointer to FuriHalSpiHandle
- * @param maclen - length of target mac address
- * @param[out] addresses - sniffed address
- *
- * @return success
- */
- bool nrf24_sniff_address(const FuriHalSpiBusHandle* handle, uint8_t maclen, uint8_t* address);
- /** Sends ping packet on each channel for designated tx mac looking for ack
- *
- * @param handle - pointer to FuriHalSpiHandle
- * @param srcmac - source address
- * @param dstmac - destination address
- * @param maclen - length of address
- * @param rate - transfer rate in Mbps (1 or 2)
- * @param min_channel - channel to start with
- * @param max_channel - channel to end at
- * @param autoinit - if true, automatically configure radio for this channel
- *
- * @return channel that the address is listening on, if this value is above the max_channel param, it failed
- */
- uint8_t nrf24_find_channel(
- const FuriHalSpiBusHandle* handle,
- uint8_t* srcmac,
- uint8_t* dstmac,
- uint8_t maclen,
- uint8_t rate,
- uint8_t min_channel,
- uint8_t max_channel,
- bool autoinit);
- /** Converts 64 bit value into uint8_t array
- * @param val - 64-bit integer
- * @param[out] out - bytes out
- * @param bigendian - if true, convert as big endian, otherwise little endian
- */
- void int64_to_bytes(uint64_t val, uint8_t* out, bool bigendian);
- /** Converts 32 bit value into uint8_t array
- * @param val - 32-bit integer
- * @param[out] out - bytes out
- * @param bigendian - if true, convert as big endian, otherwise little endian
- */
- void int32_to_bytes(uint32_t val, uint8_t* out, bool bigendian);
- /** Converts uint8_t array into 32 bit value
- * @param bytes - uint8_t array
- * @param bigendian - if true, convert as big endian, otherwise little endian
- *
- * @return 32-bit value
- */
- uint32_t bytes_to_int32(uint8_t* bytes, bool bigendian);
- #ifdef __cplusplus
- }
- #endif
|