/**@brief Initializes controllable device. Sets up channel but does not open it. */ void phc_init(const asc_ant_params_t * const p_ant_params) { uint32_t err_code; if(m_state == PHONE_OFF) { m_channel_number = p_ant_params->channel_number; m_device_number = *(p_ant_params->p_device_number); //Assign the slave channel err_code = sd_ant_channel_assign(m_channel_number, CHANNEL_TYPE_MASTER, p_ant_params->network_number, ASC_ANT_EXT_ASSIGNMENT); APP_ERROR_CHECK(err_code); //Assign channel id err_code = sd_ant_channel_id_set (m_channel_number, m_device_number, p_ant_params->device_type , p_ant_params->tx_type); APP_ERROR_CHECK(err_code); //Assign channel frequency err_code = sd_ant_channel_radio_freq_set (m_channel_number, p_ant_params->rf_frequency); APP_ERROR_CHECK(err_code); //Assign channel message period err_code = sd_ant_channel_period_set (m_channel_number, asc_get_counts_from_period_enum(p_ant_params->channel_period)); APP_ERROR_CHECK(err_code); //Set the hub's transmission power err_code = sd_ant_channel_radio_tx_power_set (m_channel_number, p_ant_params->tx_power, 0); APP_ERROR_CHECK(err_code); } }
// Public Functions void ascm_init(const asc_ant_params_t * const ant_parameters) { uint32_t err_code; m_channel_number = ant_parameters->channel_number; m_channel_period = ant_parameters->channel_period; deviceregistry_setup(pm_device_registry); err_code = sd_ant_channel_assign(m_channel_number, CHANNEL_TYPE_SHARED_MASTER, ant_parameters->network_number, ASC_ANT_EXT_ASSIGNMENT); APP_ERROR_CHECK(err_code); //Assign slave channel id. err_code = sd_ant_channel_id_set(m_channel_number, *(ant_parameters->p_device_number), ant_parameters->device_type, ant_parameters->tx_type); APP_ERROR_CHECK(err_code); //Assign slave channel frequency. err_code = sd_ant_channel_radio_freq_set(m_channel_number, ant_parameters->rf_frequency); APP_ERROR_CHECK(err_code); //Assign slave channel message period. err_code = sd_ant_channel_period_set(m_channel_number, asc_get_counts_from_period_enum(m_channel_period)); APP_ERROR_CHECK(err_code); //Set the transmission power. err_code = sd_ant_channel_radio_tx_power_set(m_channel_number, ant_parameters->tx_power, ASC_ANT_CUSTOM_PWR); APP_ERROR_CHECK(err_code); }
/**@brief Function for setting up the ANT module to be ready for TX broadcast. */ static void ant_channel_tx_broadcast_setup(void) { uint32_t err_code; uint8_t m_network_key[] = ANT_NETWORK_KEY; uint8_t m_broadcast_data[] = BROADCAST_PAYLOAD; // Set Network Key. err_code = sd_ant_network_address_set(ANT_NETWORK_NUMBER, (uint8_t*)m_network_key); APP_ERROR_CHECK(err_code); // Assign Channel err_code = sd_ant_channel_assign(ANT_CHANNEL_NUMBER, CHANNEL_TYPE_MASTER, ANT_NETWORK_NUMBER, ANT_EXT_ASSIGN); APP_ERROR_CHECK(err_code); // Set Channel ID. err_code = sd_ant_channel_id_set(ANT_CHANNEL_NUMBER, ANT_DEV_NUM, ANT_DEV_TYPE, ANT_TRANS_TYPE); APP_ERROR_CHECK(err_code); // Set Channel Period err_code = sd_ant_channel_period_set(ANT_CHANNEL_NUMBER, ANT_CHANNEL_PERIOD); APP_ERROR_CHECK(err_code); // Set RF Frequency err_code = sd_ant_channel_radio_freq_set(ANT_CHANNEL_NUMBER, ANT_RF_FREQUENCY); APP_ERROR_CHECK(err_code); //Set Tx Power err_code = sd_ant_channel_radio_tx_power_set(ANT_CHANNEL_NUMBER, RADIO_TX_POWER_LVL_3, ANT_TRANSMIT_POWER); APP_ERROR_CHECK(err_code); // Setup broadcast payload err_code = sd_ant_broadcast_message_tx(ANT_CHANNEL_NUMBER, BROADCAST_DATA_BUFFER_SIZE, m_broadcast_data); if(err_code != NRF_ANT_ERROR_CHANNEL_IN_WRONG_STATE) { APP_ERROR_CHECK(err_code); } // Open channel err_code = sd_ant_channel_open(ANT_CHANNEL_NUMBER); APP_ERROR_CHECK(err_code); }
/**@brief Sets the state machine to the specified state. */ static void ascmm_set_state(ascmm_states_t new_state) { uint32_t err_code; if(new_state != m_state) { switch (new_state) { case ASCMM_OFF: { //Close the channel err_code = sd_ant_channel_close(m_discovery_channel_number); APP_ERROR_CHECK(err_code); break; } case DISCOVERY: { if (m_state != ASCMM_OFF) { asc_event_set(&m_asc_event_flags, EVENT_ASC_DEVICE_IN_WRONG_STATE); return; } m_state = new_state; //Open the discovery channel err_code = sd_ant_channel_open(m_discovery_channel_number); break; } case CONNECTED: { if (m_state != DISCOVERY) { asc_event_set(&m_asc_event_flags, EVENT_ASC_DEVICE_IN_WRONG_STATE); return; } m_state = new_state; //Close the channel err_code = sd_ant_channel_close(m_discovery_channel_number); APP_ERROR_CHECK(err_code); //Assign the connection channel err_code = sd_ant_channel_assign(m_ant_connected_parameters.channel_number, m_ant_connected_parameters.channel_type, m_ant_connected_parameters.network_number, 0); APP_ERROR_CHECK(err_code); //Set channel id err_code = sd_ant_channel_id_set (m_ant_connected_parameters.channel_number, m_connection_device_number, m_ant_connected_parameters.device_type, m_ant_connected_parameters.tx_type); APP_ERROR_CHECK(err_code); //Set radio frequency err_code = sd_ant_channel_radio_freq_set (m_ant_connected_parameters.channel_number, m_ant_connected_parameters.rf_frequency); APP_ERROR_CHECK(err_code); if(m_ant_connected_parameters.channel_type == CHANNEL_TYPE_SLAVE) { //Configure search timeout for slave channel err_code = sd_ant_channel_rx_search_timeout_set(m_ant_connected_parameters.channel_number, ASCMM_SEARCH_TIMEOUT); APP_ERROR_CHECK(err_code); err_code = sd_ant_channel_low_priority_rx_search_timeout_set(m_ant_connected_parameters.channel_number, ASCMM_LP_SEARCH_TIMEOUT); APP_ERROR_CHECK(err_code); } else { // Configure Tx power for master chanel err_code = sd_ant_channel_radio_tx_power_set(m_ant_connected_parameters.channel_number, m_ant_connected_parameters.tx_power, 0); APP_ERROR_CHECK(err_code); } err_code = sd_ant_channel_open(m_ant_connected_parameters.channel_number); APP_ERROR_CHECK(err_code); break; } default: { //if the new state was not recognized, return to avoid setting the state changed event asc_event_set(&m_asc_event_flags, EVENT_ASC_DEVICE_IN_WRONG_STATE); return; } } asc_event_set(&m_asc_event_flags, EVENT_ASC_STATE_CHANGED); } //lint --e{438} }
// Public Functions void ascmm_init(const asc_ant_params_t * const p_ant_discovery_parameters, const asc_ant_params_t * const p_ant_connection_parameters, uint16_t device_number) { uint32_t err_code; m_discovery_channel_number = p_ant_discovery_parameters->channel_number; m_device_number = device_number; //Assign the slave channel err_code = sd_ant_channel_assign(m_discovery_channel_number, p_ant_discovery_parameters->channel_type, p_ant_discovery_parameters->network_number, EXT_PARAM_ALWAYS_SEARCH); APP_ERROR_CHECK(err_code); //Assign slave channel id. err_code = sd_ant_channel_id_set(m_discovery_channel_number, ANT_WILDCARD_DEVICE_NUMBER, p_ant_discovery_parameters->device_type, p_ant_discovery_parameters->tx_type); APP_ERROR_CHECK(err_code); //Assign slave channel frequency. err_code = sd_ant_channel_radio_freq_set(m_discovery_channel_number, p_ant_discovery_parameters->rf_frequency); APP_ERROR_CHECK(err_code); //Set the transmission power. err_code = sd_ant_channel_radio_tx_power_set(m_discovery_channel_number, p_ant_discovery_parameters->tx_power, ASC_ANT_CUSTOM_PWR); APP_ERROR_CHECK(err_code); //Configure search timeouts err_code = sd_ant_channel_rx_search_timeout_set(m_discovery_channel_number, DISCOVERY_SEARCH_TIMEOUT); APP_ERROR_CHECK(err_code); err_code = sd_ant_channel_low_priority_rx_search_timeout_set(m_discovery_channel_number, DISCOVERY_LP_SEARCH_TIMEOUT); APP_ERROR_CHECK(err_code); // Configure proximity search // @todo: JP added trailing 0 parameter err_code = sd_ant_prox_search_set(m_discovery_channel_number, PROXIMITY_THRESHOLD_2, 0); APP_ERROR_CHECK(err_code); //Enable extended messages err_code = sd_ant_lib_config_set(ANT_LIB_CONFIG_MESG_OUT_INC_DEVICE_ID); APP_ERROR_CHECK(err_code); //Initialize the connection parameters struct for later use //Many of these values are just defaults for now. //Ex. channel type will be determined before switching to the connected state) m_ant_connected_parameters.channel_number = p_ant_connection_parameters->channel_number; m_ant_connected_parameters.network_number = p_ant_connection_parameters->network_number; m_ant_connected_parameters.p_device_number = &(m_connection_device_number); //This will change over the course of discovery m_ant_connected_parameters.device_type = p_ant_connection_parameters->device_type; m_ant_connected_parameters.tx_type = p_ant_connection_parameters->tx_type; m_ant_connected_parameters.channel_period = p_ant_connection_parameters->channel_period; m_ant_connected_parameters.channel_type = p_ant_connection_parameters->channel_type; m_ant_connected_parameters.rf_frequency = p_ant_connection_parameters->rf_frequency; m_ant_connected_parameters.tx_power = p_ant_connection_parameters->tx_power; }
void ant_message_types_master_setup(void) { uint32_t err_code; ant_channel_config_t channel_config = { .channel_number = ANT_CHANNEL_NUMBER, .channel_type = CHANNEL_TYPE_MASTER, .ext_assign = EXT_ASSIGN, .rf_freq = RF_FREQUENCY, .transmission_type = CHAN_ID_TRANS_TYPE, .device_type = CHAN_ID_DEV_TYPE, .device_number = (uint16_t) (NRF_FICR->DEVICEID[0]), .channel_period = CHANNEL_PERIOD, .network_number = ANT_CHANNEL_DEFAULT_NETWORK, }; err_code = ant_channel_init(&channel_config); APP_ERROR_CHECK(err_code); //Set Tx Power err_code = sd_ant_channel_radio_tx_power_set(ANT_CHANNEL_NUMBER, RADIO_TX_POWER_LVL_3, ANT_CUSTOM_TRANSMIT_POWER); APP_ERROR_CHECK(err_code); // Open channel. err_code = sd_ant_channel_open(ANT_CHANNEL_NUMBER); APP_ERROR_CHECK(err_code); // Write counter value to last byte of the broadcast data. // The last byte is chosen to get the data more visible in the end of an printout // on the recieving end. memset(m_tx_buffer, 0, BROADCAST_DATA_BUFFER_SIZE); m_tx_buffer[BROADCAST_DATA_BUFFER_SIZE - 1] = m_counter; // Configure the initial payload of the broadcast data err_code = sd_ant_broadcast_message_tx(ANT_CHANNEL_NUMBER, BROADCAST_DATA_BUFFER_SIZE, m_tx_buffer); APP_ERROR_CHECK(err_code); //Set state to broadcasting state_message_types = BROADCAST; } void ant_message_types_master_bsp_evt_handler(bsp_event_t evt) { switch (evt) { case BSP_EVENT_KEY_0: state_message_types = BROADCAST; break; case BSP_EVENT_KEY_1: state_message_types = ACKNOWLEDGED; break; case BSP_EVENT_KEY_2: state_message_types = BURST; break; default: break; // no implementation needed } } void ant_message_types_master_event_handler(ant_evt_t * p_ant_evt) { uint32_t err_code; uint32_t led_output = LED_BROADCAST; switch (p_ant_evt->event) { // ANT broadcast/Acknowledged/Burst Success // Send the next message according to the current state and increment the counter. case EVENT_TX: // Intentional fall through case EVENT_TRANSFER_TX_COMPLETED: // Intentional fall through case EVENT_TRANSFER_TX_FAILED: LEDS_OFF(LEDS_MASK); m_tx_buffer[BROADCAST_DATA_BUFFER_SIZE - 1] = m_counter; if(state_message_types == BROADCAST) { // Send as broadcast err_code = sd_ant_broadcast_message_tx(ANT_CHANNEL_NUMBER, BROADCAST_DATA_BUFFER_SIZE, m_tx_buffer); APP_ERROR_CHECK(err_code); led_output = LED_BROADCAST; } else if(state_message_types == ACKNOWLEDGED) { // Send as acknowledged err_code = sd_ant_acknowledge_message_tx(ANT_CHANNEL_NUMBER, BROADCAST_DATA_BUFFER_SIZE, m_tx_buffer); APP_ERROR_CHECK(err_code); led_output = LED_ACKNOWLEDGED; } else if(state_message_types == BURST) { // If this is a new message, populate the burst buffer // with new dummy data. Otherwise, will retry sending the // same content. if(p_ant_evt->event != EVENT_TRANSFER_TX_FAILED) { for(uint32_t i = 0; i < BURST_BLOCK_SIZE; i++) { m_burst_data[i] = m_counter; m_counter++; } } // Queue a Burst Transfer. Since this is a small burst, queue entire burst. err_code = sd_ant_burst_handler_request(ANT_CHANNEL_NUMBER, BURST_BLOCK_SIZE, m_burst_data, (BURST_SEGMENT_START | BURST_SEGMENT_END)); APP_ERROR_CHECK(err_code); led_output = LED_BURST; } // Activate LED for 20ms LEDS_ON(led_output); nrf_delay_ms(20); LEDS_OFF(led_output); m_counter++; break; case TRANSFER_IN_PROGRESS: //Intentional fall through case TRANSFER_SEQUENCE_NUMBER_ERROR: //Intentional fall through case TRANSFER_IN_ERROR: //Intentional fall through case TRANSFER_BUSY: // Ignore these events; will retry burst transfer when we get the EVENT_TRANSFER_TX_FAILED event. break; default: break; } }