Skip to content

Commit

Permalink
wip: finish tx/rx functions but it does not really work
Browse files Browse the repository at this point in the history
  • Loading branch information
everedero committed Dec 5, 2023
1 parent 99ba370 commit b483f2b
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 21 deletions.
24 changes: 24 additions & 0 deletions app/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,38 @@ LOG_MODULE_REGISTER(main, CONFIG_APP_LOG_LEVEL);
#error "whoops, node label radio0 not found"
#endif

//#define IS_TX

int main(void)
{
static const struct device *nrf24 = DEVICE_DT_GET(DT_NODELABEL(radio0));
uint8_t data_len = 8;
int i = 0;
uint8_t buffer[] = {0xab, 0xcd, 0xef, 0xaa, 0xbb, 0xcc, 0xdd, 0xee};

if (!device_is_ready(nrf24)) {
LOG_ERR("Sensor not ready");
return 0;
}
LOG_INF("Device ready");
#ifdef IS_TX
LOG_INF("I am TX!");
#else
LOG_INF("I am RX");
#endif

#ifdef IS_TX
nrf24_write(nrf24, buffer, data_len);
LOG_INF("Wrote stuff");
#else
data_len = 1;
for (i=0; i<data_len; i++)
{
nrf24_read(nrf24, &buffer[i], 1);
LOG_INF("Read: 0x%x", buffer[i]);
}
LOG_HEXDUMP_INF(buffer, data_len, "Received: ");
#endif
return 0;
}

88 changes: 75 additions & 13 deletions drivers/nrf24l01/nrf24l01.c
Original file line number Diff line number Diff line change
Expand Up @@ -463,30 +463,87 @@ void nrf24l01_start_listening(const struct device *dev)
nrf24l01_set_register_bit(dev, NRF_CONFIG, RX_DR, true);
nrf24l01_set_register_bit(dev, NRF_CONFIG, TX_DS, true);
nrf24l01_set_register_bit(dev, NRF_CONFIG, MAX_RT, true);
// Set CE high for RX
nrf24l01_toggle_ce(dev, HIGH);
// Restore the pipe0 adddress, if exists
/*
if (data->rx_datapipe0_address[0] > 0){
nrf24l01_write_register_len(dev, RX_ADDR_P0, pipe0_reading_address, addr_width);
} else {
nrf24_closeReadingPipe(spi, spi_cfg, 0);
}*/

if (nrf24l01_get_register_bit(dev, FEATURE, EN_ACK_PAY)) // If ACK payload enabled
if (nrf24l01_get_register_bit(dev, FEATURE, EN_ACK_PAY))
{
nrf24l01_cmd_register(dev, FLUSH_TX);
}
}
void nrf24l01_stop_listening(const struct device *dev)
{
const uint8_t tx_delay=250;
// Set CE low
nrf24l01_toggle_ce(dev, LOW);

k_sleep(K_MSEC(tx_delay));

if (nrf24l01_get_register_bit(dev, FEATURE, EN_ACK_PAY)) {
k_sleep(K_MSEC(tx_delay));
nrf24l01_cmd_register(dev, FLUSH_TX);
}
nrf24l01_set_register_bit(dev, NRF_CONFIG, PRIM_RX, false);

// for 3 pins solution TX mode is only left with additonal powerDown/powerUp cycle
// if (three_pins) {
// nrf24_powerDown(spi, spi_cfg);
// }
// nrf24_powerUp(spi, spi_cfg);

nrf24l01_toggle_reading_pipe(dev, 0, true);

}

/* API functions */

static int nrf24l01_read(const struct device *dev, uint8_t *buffer)
static int nrf24l01_read(const struct device *dev, uint8_t *buffer, uint8_t data_len)
{
return(0);
uint8_t pipe_num = 0;
static uint8_t got_byte = 0;

nrf24l01_start_listening(dev);
while (!nrf24l01_is_rx_data_available(dev, &pipe_num))
{
k_msleep(1);
}
LOG_INF("Read found on pipe %d", pipe_num);
nrf24l01_read_payload(dev, buffer, data_len);
// Acking
got_byte += 1;
LOG_DBG("Got byte %d", got_byte);
nrf24l01_write_ack_payload(dev, &got_byte, 1, pipe_num);

return 0;
}

static int nrf24l01_write(const struct device *dev, uint8_t *buffer)
static int nrf24l01_write(const struct device *dev, uint8_t *buffer, uint8_t data_len)
{
return(0);
uint8_t status;
nrf24l01_stop_listening(dev);

// Like startFastWrite
nrf24l01_write_tx_payload(dev, buffer, data_len);
nrf24l01_toggle_ce(dev, HIGH);
// Wait for status bits TX_DS or MAX_RT to be asserted
while( !(nrf24l01_get_register_bit(dev, NRF_STATUS, TX_DS) |
nrf24l01_get_register_bit(dev, NRF_STATUS, MAX_RT)))
{
k_msleep(1);
}
nrf24l01_toggle_ce(dev, LOW);

status = nrf24l01_set_register_bit(dev, NRF_STATUS, RX_DR, true);
nrf24l01_set_register_bit(dev, NRF_STATUS, TX_DS, true);
nrf24l01_set_register_bit(dev, NRF_STATUS, MAX_RT, true);

// Max retries exceeded, flush TX
if ( status & BIT(MAX_RT) ) {
nrf24l01_cmd_register(dev, FLUSH_TX);
return -EIO;
}

return 0;
}

static const struct nrf24_api nrf24l01_api = {
Expand Down Expand Up @@ -602,11 +659,16 @@ static int nrf24l01_init(const struct device *dev)
ret = nrf24l01_read_payload(dev, buf, data_len);
LOG_DBG("Read payload: 0x%x", ret);
LOG_HEXDUMP_DBG(buf, data_len, "Buffer");
nrf24l01_print_status(ret);
nrf24l01_print_status(ret);*/

nrf24l01_configure_pipes(dev);
nrf24l01_configure_ack(dev);

// Starting chip
nrf24l01_cmd_register(dev, FLUSH_TX);
nrf24l01_cmd_register(dev, FLUSH_RX);
nrf24l01_radio_power_up(dev);

return 0;
}

Expand Down
16 changes: 8 additions & 8 deletions include/app/drivers/nrf24.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

/** @cond INTERNAL_HIDDEN */

typedef int (*nrf24_read_t)(const struct device *dev, uint8_t *buffer);
typedef int (*nrf24_write_t)(const struct device *dev, uint8_t *buffer);
typedef int (*nrf24_read_t)(const struct device *dev, uint8_t *buffer, uint8_t data_len);
typedef int (*nrf24_write_t)(const struct device *dev, uint8_t *buffer, uint8_t data_len);

__subsystem struct nrf24_api {
nrf24_read_t read;
Expand All @@ -26,14 +26,14 @@ __subsystem struct nrf24_api {
* @retval 0 On success.
* @retval -errno Other negative errno in case of failure.
*/
__syscall int nrf24_read(const struct device *dev, uint8_t *buffer);
__syscall int nrf24_read(const struct device *dev, uint8_t *buffer, uint8_t data_len);

static inline int z_impl_nrf24_read(const struct device *dev, uint8_t *buffer)
static inline int z_impl_nrf24_read(const struct device *dev, uint8_t *buffer, uint8_t data_len)
{
const struct nrf24_api *api =
(const struct nrf24_api *)dev->api;

return api->read(dev, buffer);
return api->read(dev, buffer, data_len);
}

/**
Expand All @@ -45,14 +45,14 @@ static inline int z_impl_nrf24_read(const struct device *dev, uint8_t *buffer)
* @retval 0 On success.
* @retval -errno Other negative errno in case of failure.
*/
__syscall int nrf24_write(const struct device *dev, uint8_t *buffer);
__syscall int nrf24_write(const struct device *dev, uint8_t *buffer, uint8_t data_len);

static inline int z_impl_nrf24_write(const struct device *dev, uint8_t *buffer)
static inline int z_impl_nrf24_write(const struct device *dev, uint8_t *buffer, uint8_t data_len)
{
const struct nrf24_api *api =
(const struct nrf24_api *)dev->api;

return api->write(dev, buffer);
return api->write(dev, buffer, data_len);
}

#include <syscalls/nrf24.h>
Expand Down

0 comments on commit b483f2b

Please sign in to comment.