// 0 = success // 1 = init failed // 2 = set tx_power failed // 3 = enabled failed int RFduinoGZLL_begin(device_t device) { _device = device; if (! nrf_gzll_init(device == HOST ? NRF_GZLL_MODE_HOST : NRF_GZLL_MODE_DEVICE)) return 1; nrf_gzll_tx_power_t tx_power; if (RFduinoGZLL_tx_power_level <= -20) tx_power = NRF_GZLL_TX_POWER_N20_DBM; else if (RFduinoGZLL_tx_power_level <= -16) tx_power = NRF_GZLL_TX_POWER_N16_DBM; else if (RFduinoGZLL_tx_power_level <= -12) tx_power = NRF_GZLL_TX_POWER_N12_DBM; else if (RFduinoGZLL_tx_power_level <= -16) tx_power = NRF_GZLL_TX_POWER_N16_DBM; else if (RFduinoGZLL_tx_power_level <= -8) tx_power = NRF_GZLL_TX_POWER_N8_DBM; else if (RFduinoGZLL_tx_power_level <= -4) tx_power = NRF_GZLL_TX_POWER_N4_DBM; else if (RFduinoGZLL_tx_power_level <= 0) tx_power = NRF_GZLL_TX_POWER_0_DBM; else tx_power = NRF_GZLL_TX_POWER_4_DBM; if (! nrf_gzll_set_tx_power(tx_power)) return 2; if (! nrf_gzll_enable()) return 3; return 0; }
/**@brief Start Gazell functionality. */ void gzll_app_start(void) { bool gzll_call_ok; gzll_call_ok = nrf_gzll_init(NRF_GZLL_MODE_DEVICE); if (!gzll_call_ok) { GET_GAZELL_ERROR(); } gzll_call_ok = nrf_gzll_set_max_tx_attempts(MAX_TX_ATTEMPTS); if (!gzll_call_ok) { GET_GAZELL_ERROR(); } gzll_call_ok = nrf_gzll_enable(); if (!gzll_call_ok) { GET_GAZELL_ERROR(); } // Add a packet to the TX FIFO to start the data transfer. // From here on more data will be added through the Gazell callbacks gzll_packet[0] = DUMMY_PACKET; gzll_call_ok = nrf_gzll_add_packet_to_tx_fifo(0, gzll_packet, PAYLOAD_SIZE); if (!gzll_call_ok) { GET_GAZELL_ERROR(); } }
// 0 = success // 1 = init failed // 2 = set tx_power failed // 3 = enabled failed // 4 = set channel selection policy failed int RFduinoGZLL_begin(device_t device) { _device = device; if (! nrf_gzll_init(device == HOST ? NRF_GZLL_MODE_HOST : NRF_GZLL_MODE_DEVICE)) return 1; nrf_gzll_tx_power_t tx_power; if (RFduinoGZLL_tx_power_level <= -20) tx_power = NRF_GZLL_TX_POWER_N20_DBM; else if (RFduinoGZLL_tx_power_level <= -16) tx_power = NRF_GZLL_TX_POWER_N16_DBM; else if (RFduinoGZLL_tx_power_level <= -12) tx_power = NRF_GZLL_TX_POWER_N12_DBM; else if (RFduinoGZLL_tx_power_level <= -16) tx_power = NRF_GZLL_TX_POWER_N16_DBM; else if (RFduinoGZLL_tx_power_level <= -8) tx_power = NRF_GZLL_TX_POWER_N8_DBM; else if (RFduinoGZLL_tx_power_level <= -4) tx_power = NRF_GZLL_TX_POWER_N4_DBM; else if (RFduinoGZLL_tx_power_level <= 0) tx_power = NRF_GZLL_TX_POWER_0_DBM; else tx_power = NRF_GZLL_TX_POWER_4_DBM; if (! nrf_gzll_set_tx_power(tx_power)) return 2; if (! nrf_gzll_set_device_channel_selection_policy(NRF_GZLL_DEVICE_CHANNEL_SELECTION_POLICY_USE_CURRENT)) return 4; if (RFduinoGZLL_host_base_address ) { uint8_t msb = RFduinoGZLL_host_base_address >> 24; if (msb == 0x55 || msb == 0xAA) return 5; // msb of base address should not be alternating 0s and 1s if ( !nrf_gzll_set_base_address_0(RFduinoGZLL_host_base_address) ) return 5; } if (RFduinoGZLL_device_base_address) { uint8_t msb = RFduinoGZLL_device_base_address >> 24; if (msb == 0x55 || msb == 0xAA) return 6; // msb of base address should not be alternating 0s and 1s if ( !nrf_gzll_set_base_address_1(RFduinoGZLL_device_base_address) ) return 6; } if (! nrf_gzll_enable()) return 3; RFduinoGZLL_enabled = 1; return 0; }
void gzll_rx_start(void) { if(nrf_gzll_get_mode() != NRF_GZLL_MODE_HOST) { gzll_goto_idle(); (void)nrf_gzll_set_mode(NRF_GZLL_MODE_HOST); } if(!nrf_gzll_is_enabled()) { (void)nrf_gzll_enable(); } }
int main(void) { // Debug helper variables uint32_t length; // Data and acknowledgement payloads uint8_t payload[NRF_GZLL_CONST_MAX_PAYLOAD_LENGTH]; ///< Payload to send to Host. // Setup user interface (buttons and LEDs) ui_init(); // Initialize Gazell Link Layer (void)nrf_gzll_init(NRF_GZLL_MODE_HOST); (void)nrf_gzll_set_timeslot_period(NRF_GZLLDE_RXPERIOD_DIV_2); // Half RX period on nRF24Lxx device // Initialize Gazell Pairing Library gzp_init(); (void)nrf_gzll_set_rx_pipes_enabled(nrf_gzll_get_rx_pipes_enabled() | (1 << UNENCRYPTED_DATA_PIPE)); gzp_pairing_enable(true); (void)nrf_gzll_enable(); for(;;) { gzp_host_execute(); // If Host ID request received if(gzp_id_req_received()) { // Always grant request gzp_id_req_grant(); } length = NRF_GZLL_CONST_MAX_PAYLOAD_LENGTH; if( nrf_gzll_get_rx_fifo_packet_count(UNENCRYPTED_DATA_PIPE) ) { if(nrf_gzll_fetch_packet_from_rx_fifo(UNENCRYPTED_DATA_PIPE, payload, &length)) { output_present(payload[0]); } } else if (gzp_crypt_user_data_received()) { if(gzp_crypt_user_data_read(payload, (uint8_t*)&length)) { output_present(payload[0]); } } } }
bool gzp_update_radio_params(const uint8_t* system_address) { uint8_t i; uint8_t channels[NRF_GZLL_CONST_MAX_CHANNEL_TABLE_SIZE]; uint32_t channel_table_size; uint32_t pairing_base_address_32, system_address_32; bool update_ok = true; bool gzll_enabled_state; gzll_enabled_state = nrf_gzll_is_enabled(); // Configure "global" pairing address pairing_base_address_32 = (pairing_base_address[0]) + ((uint32_t)pairing_base_address[1] << 8) + ((uint32_t)pairing_base_address[2] << 16) + ((uint32_t)pairing_base_address[3] << 24) ; if(system_address != NULL) { system_address_32 = (system_address[0]) + ((uint32_t)system_address[1] << 8) + ((uint32_t)system_address[2] << 16) + ((uint32_t)system_address[3] << 24) ; } else { return false; } nrf_gzp_disable_gzll(); update_ok = update_ok && nrf_gzll_set_base_address_0(pairing_base_address_32); update_ok = update_ok && nrf_gzll_set_address_prefix_byte(GZP_PAIRING_PIPE, pairing_address_prefix_byte); update_ok = update_ok && nrf_gzll_set_base_address_1(system_address_32); // Configure address for pipe 1 - 5. Address byte set to equal pipe number. for(i = 1; i < NRF_GZLL_CONST_PIPE_COUNT; i++) { update_ok = update_ok && nrf_gzll_set_address_prefix_byte(i,i); } channel_table_size = nrf_gzll_get_channel_table_size(); gzp_generate_channels(&channels[0], system_address, channel_table_size); // Write generated channel subset to Gazell Link Layer update_ok = update_ok && nrf_gzll_set_channel_table(&channels[0], channel_table_size); if(gzll_enabled_state) { update_ok = update_ok && nrf_gzll_enable(); } return update_ok; }
int main() { uint8_t debug_led_output; // Setup user interface (buttons and LEDs) ui_init(); // Initialize Gazell init_ok = nrf_gzll_init(NRF_GZLL_MODE_DEVICE); // Attempt sending every packet up to 100 times init_ok &= nrf_gzll_set_max_tx_attempts(100); // Load data into TX queue data_payload[0] = input_get(); push_ok = nrf_gzll_add_packet_to_tx_fifo(PIPE_NUMBER, data_payload, TX_PAYLOAD_LENGTH); // Enable Gazell to start sending over the air enable_ok = nrf_gzll_enable(); while(1) { // Error handling debug_led_output = ((uint8_t)tx_success << 4) | ((uint8_t)pop_ok << 3) | ((uint8_t)push_ok << 2) | ((uint8_t)enable_ok << 1) | (uint8_t)init_ok; // If an error has occured if(debug_led_output != 0x1F) { output_present(0xFF); nrf_delay_us(1000000); // 1 second delay output_present(0xFF); nrf_delay_us(1000000); // 1 second delay output_present(0xFF); //nrf_gzll_get_error_code()); nrf_delay_us(1000000); // 1 second delay } // Optionally send the CPU to sleep while waiting for a callback. // __WFI(); } }
int main() { uint8_t debug_led_output; // Setup port directions nrf_gpio_port_dir_set(BUTTONS, NRF_GPIO_PORT_DIR_INPUT); nrf_gpio_port_dir_set(LEDS, NRF_GPIO_PORT_DIR_OUTPUT); // Initialize Gazell init_ok = nrf_gzll_init(NRF_GZLL_MODE_DEVICE); // Attempt sending every packet up to 100 times init_ok &= nrf_gzll_set_max_tx_attempts(100); // Load data into TX queue data_payload[0] = nrf_gpio_port_read(BUTTONS); push_ok = nrf_gzll_add_packet_to_tx_fifo(PIPE_NUMBER, data_payload, TX_PAYLOAD_LENGTH); // Enable Gazell to start sending over the air enable_ok = nrf_gzll_enable(); while(1) { // Error handling debug_led_output = ((uint8_t)tx_success << 4) | ((uint8_t)pop_ok << 3) | ((uint8_t)push_ok << 2) | ((uint8_t)enable_ok << 1) | (uint8_t)init_ok; // If an error has occured if(debug_led_output != 0x1F) { nrf_gpio_port_write(LEDS,0xFF); nrf_delay_us(1000000); // 1 second delay nrf_gpio_port_write(LEDS, debug_led_output); nrf_delay_us(1000000); // 1 second delay nrf_gpio_port_write(LEDS,0xF0 + (uint32_t)nrf_gzll_get_error_code()); nrf_delay_us(1000000); // 1 second delay } // Optionally send the CPU to sleep while waiting for a callback. // __WFI(); } }
static gzp_tx_rx_trans_result_t gzp_tx_rx_transaction(const uint8_t *tx_packet, uint8_t tx_length, uint8_t *rx_dst, uint32_t *rx_length, uint8_t pipe) { gzp_tx_rx_trans_result_t retval; uint8_t fetch_packet[GZP_CMD_FETCH_RESP_PAYLOAD_LENGTH]; bool tx_packet_success; bool fetch_success; uint32_t local_rx_length = GZP_MAX_ACK_PAYLOAD_LENGTH; uint32_t temp_lifetime; nrf_gzp_flush_rx_fifo(pipe); retval = GZP_TX_RX_FAILED_TO_SEND; (void)nrf_gzll_disable(); while(nrf_gzll_is_enabled()) {} temp_lifetime = nrf_gzll_get_sync_lifetime(); (void)nrf_gzll_set_sync_lifetime(GZP_TX_RX_TRANS_DELAY * 3); // 3 = RXPERIOD * 2 + margin (void)nrf_gzll_enable(); tx_packet_success = gzp_tx_packet(tx_packet, tx_length, pipe); if(tx_packet_success) { retval = GZP_TX_RX_NO_RESPONSE; nrf_gzp_flush_rx_fifo(pipe); fetch_packet[0] = (uint8_t)GZP_CMD_FETCH_RESP; gzp_tick_sleep_rx_periods(GZP_TX_RX_TRANS_DELAY); tx_packet_success = gzp_tx_packet(fetch_packet, GZP_CMD_FETCH_RESP_PAYLOAD_LENGTH, pipe); if(tx_packet_success) { if(nrf_gzll_get_rx_fifo_packet_count(pipe)) { local_rx_length = NRF_GZLL_CONST_MAX_PAYLOAD_LENGTH; fetch_success = nrf_gzll_fetch_packet_from_rx_fifo(pipe, rx_dst, &local_rx_length); } else { fetch_success = false; } if(fetch_success) { retval = GZP_TX_RX_SUCCESS; } else { //print_string("GZP_TX_FETCH_FAILED\r\n"); } } else { //print_string("GZP_TX_FETCH_NO_ACK\r\n"); } } (void)nrf_gzll_disable(); while(nrf_gzll_is_enabled()) {} (void)nrf_gzll_set_sync_lifetime(temp_lifetime); (void)nrf_gzll_enable(); return retval; }
bool gzp_address_req_send() { //lint -save -e514 Unusual use of a Boolean expression (gzll_update_ok &= ...) uint8_t i; bool retval = false; bool success; uint8_t address_req[GZP_CMD_HOST_ADDRESS_REQ_PAYLOAD_LENGTH]; uint8_t rx_payload[NRF_GZLL_CONST_MAX_PAYLOAD_LENGTH]; uint32_t rx_payload_length = NRF_GZLL_CONST_MAX_PAYLOAD_LENGTH; nrf_gzll_tx_power_t temp_power; uint32_t temp_max_tx_attempts; bool gzll_update_ok = true; // Store parameters that are temporarily changed temp_max_tx_attempts = nrf_gzll_get_max_tx_attempts(); temp_power = nrf_gzll_get_tx_power(); // Modify parameters nrf_gzp_disable_gzll(); gzll_update_ok &= nrf_gzll_set_max_tx_attempts(GZP_REQ_TX_TIMEOUT); gzll_update_ok &= nrf_gzll_set_tx_power(GZP_POWER); // Flush RX FIFO gzll_update_ok &= nrf_gzll_flush_rx_fifo(GZP_PAIRING_PIPE); gzll_update_ok &= nrf_gzll_enable(); // Build "request" packet address_req[0] = (uint8_t)GZP_CMD_HOST_ADDRESS_REQ; // Send a number of packets in order to broadcast that devices not within // close proximity must back off. for(i = 0; i < GZP_MAX_BACKOFF_PACKETS; i++) { success = gzp_tx_packet(address_req, GZP_CMD_HOST_ADDRESS_REQ_PAYLOAD_LENGTH, GZP_PAIRING_PIPE); if(success) { nrf_gzp_flush_rx_fifo(GZP_PAIRING_PIPE); } else { break; } } gzp_delay_rx_periods(GZP_TX_ACK_WAIT_TIMEOUT); // Send message for fetching pairing response from host. address_req[0] = (uint8_t)GZP_CMD_HOST_ADDRESS_FETCH; success = gzp_tx_packet(address_req, GZP_CMD_HOST_ADDRESS_REQ_PAYLOAD_LENGTH, GZP_PAIRING_PIPE); if(success && latest_tx_info.payload_received_in_ack) { // If pairing response received if(nrf_gzll_get_rx_fifo_packet_count(GZP_PAIRING_PIPE) > 0) { rx_payload_length = NRF_GZLL_CONST_MAX_PAYLOAD_LENGTH; //dummy placeholder if(nrf_gzll_fetch_packet_from_rx_fifo(GZP_PAIRING_PIPE, rx_payload, &rx_payload_length)) { if(rx_payload[0] == (uint8_t)GZP_CMD_HOST_ADDRESS_RESP) { memcpy(gzp_system_address, &rx_payload[GZP_CMD_HOST_ADDRESS_RESP_ADDRESS], GZP_SYSTEM_ADDRESS_WIDTH); gzll_update_ok &= gzp_update_radio_params(&rx_payload[GZP_CMD_HOST_ADDRESS_RESP_ADDRESS]); #ifndef GZP_NV_STORAGE_DISABLE (void)gzp_params_store(false); // "False" indicates that only "system address" part of DB element will be stored #endif retval = true; } } } } else { gzp_delay_rx_periods(GZP_NOT_PROXIMITY_BACKOFF_RX_TIMEOUT - GZP_TX_ACK_WAIT_TIMEOUT); } gzp_delay_rx_periods(GZP_STEP1_RX_TIMEOUT); // Clean-up and restore parameters temporarily modified nrf_gzp_disable_gzll(); gzll_update_ok &= nrf_gzll_flush_rx_fifo(GZP_PAIRING_PIPE); gzll_update_ok &= nrf_gzll_flush_tx_fifo(GZP_PAIRING_PIPE); gzll_update_ok &= nrf_gzll_set_max_tx_attempts(temp_max_tx_attempts); gzll_update_ok &= nrf_gzll_set_tx_power(temp_power); gzll_update_ok &= nrf_gzll_enable(); if(!gzll_update_ok) { /* The update of the Gazell parameters failed. Use nrf_gzll_get_error_code() to investigate the cause. */ } return retval; //lint -restore }
int main() { bool init_ok = false; //lint -save -e514 Unusual use of a boolean expression (use of &= assignment). // Configure input pins nrf_gpio_pin_dir_set(BUTTON_SEND_MOUSE_DATA, NRF_GPIO_PIN_DIR_INPUT); nrf_gpio_pin_dir_set(BUTTON_SEND_KEYBOARD_DATA, NRF_GPIO_PIN_DIR_INPUT); nrf_gpio_port_dir_set(LED_PORT, NRF_GPIO_PORT_DIR_OUTPUT); // Initialize and enable "mouse sensor" init_ok = mouse_sensor_init(MOUSE_SENSOR_SAMPLE_PERIOD_8_MS); mouse_sensor_enable(); // Initialize and enable Gazell init_ok &= nrf_gzll_init(NRF_GZLL_MODE_DEVICE); // Ensure Gazell parameters are configured. init_ok &= nrf_gzll_set_max_tx_attempts(150); init_ok &= nrf_gzll_set_device_channel_selection_policy(NRF_GZLLDE_DEVICE_CHANNEL_SELECTION_POLICY); init_ok &= nrf_gzll_set_timeslot_period(NRF_GZLLDE_RXPERIOD_DIV_2); init_ok &= nrf_gzll_set_sync_lifetime(0); // Asynchronous mode, more efficient for pairing. switch(gzp_get_pairing_status()) { case -2: host_id_received = false; system_addr_received = false; break; case -1: host_id_received = false; system_addr_received = true; break; default: host_id_received = true; system_addr_received = true; } gzp_init(); init_ok &= nrf_gzll_enable(); if(init_ok) { while(1) { // If BUTTON_SEND_MOUSE_DATA button is pressed. if(nrf_gpio_pin_read(BUTTON_SEND_MOUSE_DATA) == 0) { read_mouse_and_send(); } // If BUTTON_SEND_KEYBOARD_DATA button is pressed if(nrf_gpio_pin_read(BUTTON_SEND_KEYBOARD_DATA) == 0) { read_keyboard_and_send(); } /* CPU sleep. We will wake up from all enabled interrupts, which here are the internal Gazell interrupts and the "mouse sensor" internal timer interrupt. */ //__WFI(); } } else { /* The initialization failed. Use nrf_gzll_get_error_code() to investigate the cause. */ } //lint -restore }
int main() { bool init_ok = false; uint32_t err_code; //lint -save -e514 Unusual use of a boolean expression (use of &= assignment). const app_uart_comm_params_t comm_params = { RX_PIN_NUMBER, TX_PIN_NUMBER, RTS_PIN_NUMBER, CTS_PIN_NUMBER, APP_UART_FLOW_CONTROL_ENABLED, false, UART_BAUDRATE_BAUDRATE_Baud38400 }; APP_UART_FIFO_INIT(&comm_params, UART_RX_BUF_SIZE, UART_TX_BUF_SIZE, uart_error_handle, APP_IRQ_PRIORITY_LOW, err_code); (void)err_code; UNUSED_VARIABLE(bsp_init(BSP_INIT_BUTTONS,0,NULL)); printf("Desktop emulator example\n"); // Initialize and enable "mouse sensor" init_ok = mouse_sensor_init(MOUSE_SENSOR_SAMPLE_PERIOD_8_MS); mouse_sensor_enable(); // Initialize and enable Gazell init_ok &= nrf_gzll_init(NRF_GZLL_MODE_DEVICE); // Ensure Gazell parameters are configured. init_ok &= nrf_gzll_set_max_tx_attempts(150); init_ok &= nrf_gzll_set_device_channel_selection_policy(NRF_GZLLDE_DEVICE_CHANNEL_SELECTION_POLICY); init_ok &= nrf_gzll_set_timeslot_period(NRF_GZLLDE_RXPERIOD_DIV_2); init_ok &= nrf_gzll_set_sync_lifetime(0); // Asynchronous mode, more efficient for pairing. switch(gzp_get_pairing_status()) { case -2: host_id_received = false; system_addr_received = false; break; case -1: host_id_received = false; system_addr_received = true; break; default: host_id_received = true; system_addr_received = true; } gzp_init(); init_ok &= nrf_gzll_enable(); if(init_ok) { while(1) { // If BUTTON_SEND_MOUSE_DATA button is pressed. bool send_mouse_data_button_pressed; err_code = bsp_button_is_pressed(SEND_MOUSE_DATA_BUTTON_ID,&(send_mouse_data_button_pressed)); if( send_mouse_data_button_pressed) { read_mouse_and_send(); } else { //indicate mouse button not pressed } // If BUTTON_SEND_KEYBOARD_DATA button is pressed bool send_keyboard_data_button_pressed; err_code = bsp_button_is_pressed(SEND_KEYBOARD_DATA_BUTTON_ID,&(send_keyboard_data_button_pressed)); if(send_keyboard_data_button_pressed) { read_keyboard_and_send(); } else { //indicate keyboard button not pressed } error_report(); /* CPU sleep. We will wake up from all enabled interrupts, which here are the internal Gazell interrupts and the "mouse sensor" internal timer interrupt. */ //__WFI(); } } else { /* The initialization failed. Use nrf_gzll_get_error_code() to investigate the cause. */ } //lint -restore }