# HG changeset patch # User Tero Koskinen # Date 1442860709 -10800 # Mon Sep 21 21:38:29 2015 +0300 # Node ID 2828facb12515221da8c57d4e51d673cd8be8c21 # Parent 3b9deff378cc87755d1436c480defa8f45046f94 Sending and receiving works. diff --git a/nrf24.c b/nrf24.c --- a/nrf24.c +++ b/nrf24.c @@ -50,6 +50,9 @@ #define RF24_SERCOM 0 +// #define LOG(...) +#define LOG + const struct rf24_settings rf24_defaults = { .data_received_interrupt = false, @@ -62,22 +65,31 @@ }; static CTL_SPI_DEVICE_t nrf24_device; +static nrf24_pin_state nrf24_ce_fn; +static nrf24_pin_state nrf24_cs_fn; static uint8_t rf24_addr_len = 0; +#if 0 static void rf24_cs(bool state) { gpio_set_state(D2X_PORT_A, 5, state); } - -void rf24_select(struct CTL_SPI_DEVICE_s *self, int state) +#endif +static void rf24_select(struct CTL_SPI_DEVICE_s *self, int state) { - gpio_set_state(D2X_PORT_A, 5, state); + if (nrf24_cs_fn) { + nrf24_cs_fn(state); + } } + static void rf24_ce(bool state) { - gpio_set_state(D2X_PORT_A, 17, state); + if (nrf24_ce_fn) { + nrf24_ce_fn(state); + } + // gpio_set_state(D2X_PORT_A, 17, state); } uint8_t rf24_read_register_array(uint8_t reg, uint8_t *buf, uint32_t len) @@ -132,7 +144,7 @@ CTL_STATUS_t transfer_status, send_status; uint32_t i; uint8_t data; - uint8_t command = RF24_R_REGISTER | ( RF24_REGISTER_MASK & reg ); + uint8_t command = RF24_W_REGISTER | ( RF24_REGISTER_MASK & reg ); ctl_spi_select_device(&nrf24_device); transfer_status = ctl_spi_exchange(&nrf24_device, &command, &data, 1); @@ -155,7 +167,7 @@ int transfer_status, write_status; uint32_t i; uint8_t data; - uint8_t command = RF24_R_REGISTER | ( RF24_REGISTER_MASK & reg ); + uint8_t command = RF24_W_REGISTER | ( RF24_REGISTER_MASK & reg ); ctl_spi_select_device(&nrf24_device); @@ -171,7 +183,7 @@ } ctl_spi_deselect_device(&nrf24_device); - debug_printf("rf24_write_register ok\n"); + LOG("rf24_write_register ok\n"); return data; } @@ -194,6 +206,11 @@ return rf_status; } +void rf24_set_status(uint8_t value) +{ + (void)rf24_write_register(RF24_STATUS_ADDR, value); +} + void rf24_set_channel(uint8_t ch) { uint8_t s; @@ -286,12 +303,14 @@ if (transfer_status != CTL_NO_ERROR) { debug_printf("rf24_get_rx_payload, transfer error: %d\n", transfer_status); + ctl_spi_deselect_device(&nrf24_device); return RF24_ERROR; } read_status = ctl_spi_read(&nrf24_device, payload, payload_len); if (read_status != CTL_NO_ERROR) { debug_printf("rf24_get_rx_payload, read error: %d\n", read_status); + ctl_spi_deselect_device(&nrf24_device); return RF24_ERROR; } ctl_spi_deselect_device(&nrf24_device); @@ -315,8 +334,9 @@ ctl_spi_select_device(&nrf24_device); transfer_status = ctl_spi_exchange(&nrf24_device, &command, &data, 1); if (transfer_status != CTL_NO_ERROR) { - debug_printf("rf24_set_tx_payload, transfer error: %d\n", transfer_status); - rf24_cs(false); + LOG("rf24_set_tx_payload, transfer error: %d\n", transfer_status); + + ctl_spi_deselect_device(&nrf24_device); return RF24_ERROR; } @@ -324,7 +344,7 @@ ctl_spi_deselect_device(&nrf24_device); if (send_status != CTL_NO_ERROR) { - debug_printf("rf24_set_tx_payload, send error: %d\n", send_status); + LOG("rf24_set_tx_payload, send error: %d\n", send_status); return RF24_ERROR; } @@ -351,6 +371,8 @@ conf = rf24_read_register(RF24_CONFIG_ADDR); + LOG("rf24_get_config: config val: 0x%02x\n", conf); + config->data_received_interrupt = (conf & MASK_RX_DR) == MASK_RX_DR; config->data_sent_interrupt = (conf & MASK_TX_DS) == MASK_TX_DS; config->max_retransmission_interrupt = (conf & MASK_MAX_RT) == MASK_MAX_RT; @@ -362,16 +384,22 @@ void rf24_set_config(const struct rf24_settings *config) { - uint8_t conf; + uint8_t conf = 0; - if (config->data_received_interrupt) conf |= MASK_RX_DR; - if (config->data_sent_interrupt) conf |= MASK_TX_DS; - if (config->max_retransmission_interrupt) conf |= MASK_MAX_RT; + if (config->rx_mode) { + rf24_stop_listening(); + } + + if (!config->data_received_interrupt) conf |= MASK_RX_DR; + if (!config->data_sent_interrupt) conf |= MASK_TX_DS; + if (!config->max_retransmission_interrupt) conf |= MASK_MAX_RT; if (config->enable_crc) conf |= EN_CRC; if (config->use_2_bit_crc) conf |= CRC0; if (config->power_up) conf |= PWR_UP; if (config->rx_mode) conf |= PRIM_RX; + LOG("rf24_set_config: config value: 0x%02x\n", conf); + (void)rf24_write_register(RF24_CONFIG_ADDR, conf); } @@ -382,7 +410,51 @@ rf24_ce(false); } -void rf24_init(const struct rf24_settings *config, CTL_SPI_BUS_t *spi_bus) +void rf24_start_listening(void) +{ + rf24_ce(true); +} + +void rf24_stop_listening(void) +{ + rf24_ce(false); +} + + +void rf24_flush_tx(void) +{ + CTL_STATUS_t transfer_status; + uint8_t data, command; + + command = FLUSH_TX; + ctl_spi_select_device(&nrf24_device); + transfer_status = ctl_spi_exchange(&nrf24_device, &command, &data, 1); + + if (transfer_status != CTL_NO_ERROR) { + LOG("rf24_flush_tx, transfer error: %d\n", transfer_status); + } + + ctl_spi_deselect_device(&nrf24_device); + +} + +void rf24_flush_rx(void) +{ + CTL_STATUS_t transfer_status; + uint8_t data, command; + + command = FLUSH_RX; + ctl_spi_select_device(&nrf24_device); + transfer_status = ctl_spi_exchange(&nrf24_device, &command, &data, 1); + + if (transfer_status != CTL_NO_ERROR) { + LOG("rf24_flush_tx, transfer error: %d\n", transfer_status); + } + + ctl_spi_deselect_device(&nrf24_device); +} + +void rf24_init(const struct rf24_settings *config, CTL_SPI_BUS_t *spi_bus, nrf24_pin_state select_fn, nrf24_pin_state ce_fn) { int status; uint8_t s; @@ -392,7 +464,11 @@ ctl_spi_attach_device(spi_bus, &nrf24_device); + nrf24_device.select = rf24_select; + nrf24_cs_fn = select_fn; + nrf24_ce_fn = ce_fn; + ctl_spi_set_protocol(&nrf24_device, CTL_SPI_MODE0, 8, 100000, 0xFF); // status = spi_enable(0, SPI_EDGE_FIRST_RISING); @@ -404,24 +480,29 @@ status = gpio_set_output(D2X_PORT_A, 5); rf24_ce(false); - rf24_cs(false); + select_fn(1); ctl_sleep(10); rf24_set_config(config); if (config->rx_mode) { - debug_printf("RX mode\n"); + LOG("RX mode\n"); } else { - debug_printf("TX mode\n"); + LOG("TX mode\n"); } s = rf24_read_register_array(0x0A, addr, 5); - debug_printf("rf24 default address: %02x %02x %02x %02x %02x\n", addr[0], addr[1], addr[2], addr[3], addr[4]); + LOG("rf24 default address: %02x %02x %02x %02x %02x\n", addr[0], addr[1], addr[2], addr[3], addr[4]); s = rf24_write_register(0x05, 76); - debug_printf("channel write status: %d\n", s); + LOG("channel write status: %d\n", s); s = rf24_read_register(0x05); - debug_printf("rf24 channel: %d\n", s); + LOG("rf24 channel: %d\n", s); } +void rf24_interrupt_handler(void) +{ + int i = 0; + i++; +} \ No newline at end of file diff --git a/nrf24.h b/nrf24.h --- a/nrf24.h +++ b/nrf24.h @@ -15,6 +15,8 @@ #define RF24_OK 0 #define RF24_ERROR -1 +typedef void (*nrf24_pin_state)(int state); + struct rf24_settings { bool data_received_interrupt; bool data_sent_interrupt; @@ -27,8 +29,10 @@ extern const struct rf24_settings rf24_defaults; -void rf24_init(const struct rf24_settings *config, CTL_SPI_BUS_t *spi_bus); +void rf24_init(const struct rf24_settings *config, CTL_SPI_BUS_t *spi_bus, nrf24_pin_state select_fn, nrf24_pin_state ce_fn); +void rf24_get_config(struct rf24_settings* config); uint8_t rf24_status(void); +void rf24_set_status(uint8_t value); void rf24_set_rx_addr(uint8_t pipe_no, const uint8_t *addr, uint8_t addr_len); void rf24_set_rx_payload_len(uint8_t pipe_no, uint8_t payload_len); int rf24_get_rx_payload(char *payload, uint8_t payload_max_len, uint8_t *len); @@ -36,5 +40,10 @@ void rf24_enable_pipes(uint8_t pipe_mask); void rf24_set_tx_addr(const uint8_t *addr, uint8_t addr_len); void rf24_send_payload(void); +void rf24_start_listening(void); +void rf24_stop_listening(void); +void rf24_flush_tx(void); +void rf24_flush_rx(void); +void rf24_interrupt_handler(void); #endif /* NRF24_H */ \ No newline at end of file