static void uart_event_handler(void * context, nrf_libuarte_async_evt_t * p_event) { cli_libuarte_internal_t * p_internal = mp_internal; ret_code_t err_code = NRF_SUCCESS; size_t len; UNUSED_VARIABLE(err_code); switch (p_event->type) { case NRF_LIBUARTE_ASYNC_EVT_ERROR: NRF_LOG_WARNING("(evt) ERROR"); break; case NRF_LIBUARTE_ASYNC_EVT_RX_DATA: { len = (size_t)((uint32_t)p_event->data.rxtx.length & 0x0000FFFF); err_code = nrf_ringbuf_cpy_put(p_internal->p_rx_ringbuf, p_event->data.rxtx.p_data, &len); ASSERT(err_code == NRF_SUCCESS); if (len != p_event->data.rxtx.length) { NRF_LOG_WARNING("Data lost, no room in RX ringbuf"); } nrf_libuarte_async_rx_free(&libuarte, p_event->data.rxtx.p_data, p_event->data.rxtx.length); if (p_event->data.rxtx.length) { NRF_LOG_DEBUG("(evt) RXRDY length:%d", p_event->data.rxtx.length); NRF_LOG_HEXDUMP_DEBUG(p_event->data.rxtx.p_data, p_event->data.rxtx.length); p_internal->p_cb->handler(NRF_CLI_TRANSPORT_EVT_RX_RDY, p_internal->p_cb->p_context); } break; } case NRF_LIBUARTE_ASYNC_EVT_TX_DONE: err_code = nrf_ringbuf_free(p_internal->p_tx_ringbuf, p_event->data.rxtx.length); ASSERT(err_code == NRF_SUCCESS); uint8_t * p_data; len = 255; err_code = nrf_ringbuf_get(p_internal->p_tx_ringbuf, &p_data, &len, true); ASSERT(err_code == NRF_SUCCESS); if (len) { NRF_LOG_DEBUG("(evt) Started TX (%d).", len); err_code = nrf_libuarte_async_tx(&libuarte, p_data, len); ASSERT(err_code == NRF_SUCCESS); } else { m_uart_busy = false; } p_internal->p_cb->handler(NRF_CLI_TRANSPORT_EVT_TX_RDY, p_internal->p_cb->p_context); NRF_LOG_DEBUG("(evt) TX completed (%d)", p_event->data.rxtx.length); break; default: NRF_LOG_ERROR("(evt) Unknown event"); ASSERT(false); break; } }
/**@snippet [Handling the data received over BLE] */ static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length) { uint32_t err_code; NRF_LOG_DEBUG("Received data from BLE NUS. Writing data on UART.\r\n"); NRF_LOG_HEXDUMP_DEBUG(p_data, length); for (uint32_t i = 0; i < length; i++) { do { err_code = app_uart_put(p_data[i]); if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_BUSY)) { NRF_LOG_ERROR("Failed receiving NUS message. Error 0x%x. \r\n", err_code); APP_ERROR_CHECK(err_code); } } while (err_code == NRF_ERROR_BUSY); } if (p_data[length-1] == '\r') { while (app_uart_put('\n') == NRF_ERROR_BUSY); } }
void ndef_record_printout(uint32_t num, nfc_ndef_record_desc_t * const p_rec_desc) { NRF_LOG_INFO("NDEF record %d content:", num); NRF_LOG_INFO("TNF: %s",(uint32_t)tnf_strings[p_rec_desc->tnf]); if (p_rec_desc->p_id != NULL) { NRF_LOG_INFO("ID:"); NRF_LOG_HEXDUMP_INFO((uint8_t *)p_rec_desc->p_id, p_rec_desc->id_length); } if (p_rec_desc->p_type != NULL) { NRF_LOG_INFO("type:"); NRF_LOG_HEXDUMP_INFO((uint8_t *)p_rec_desc->p_type, p_rec_desc->type_length); } if (p_rec_desc->payload_constructor == (p_payload_constructor_t) nfc_ndef_bin_payload_memcopy) { nfc_ndef_bin_payload_desc_t * p_bin_pay_desc = p_rec_desc->p_payload_descriptor; if (p_bin_pay_desc->p_payload != NULL) { NRF_LOG_INFO("Payload length: %d bytes", p_bin_pay_desc->payload_length); NRF_LOG_HEXDUMP_DEBUG((uint8_t *)p_bin_pay_desc->p_payload, p_bin_pay_desc->payload_length); } else { NRF_LOG_INFO("No payload"); } } }
/**@snippet [Handling the data received over UART] */ void uart_event_handle(app_uart_evt_t * p_event) { static uint8_t data_array[BLE_NUS_MAX_DATA_LEN]; static uint8_t index = 0; uint32_t err_code; switch (p_event->evt_type) { case APP_UART_DATA_READY: UNUSED_VARIABLE(app_uart_get(&data_array[index])); index++; if ((data_array[index - 1] == '\n') || (index >= (m_ble_nus_max_data_len))) { NRF_LOG_DEBUG("Ready to send data over BLE NUS\r\n"); NRF_LOG_HEXDUMP_DEBUG(data_array, index); do { err_code = ble_nus_string_send(&m_nus, data_array, index); if ( (err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_ERROR_BUSY) ) { APP_ERROR_CHECK(err_code); } } while (err_code == NRF_ERROR_BUSY); index = 0; } break; case APP_UART_COMMUNICATION_ERROR: APP_ERROR_HANDLER(p_event->data.error_communication); break; case APP_UART_FIFO_ERROR: APP_ERROR_HANDLER(p_event->data.error_code); break; default: break; } }
void nrf_drv_rng_block_rand(uint8_t * p_buff, uint32_t length) { ASSERT(m_rng_cb.state == NRFX_DRV_STATE_INITIALIZED); while (length) { uint32_t len = MIN(length, RNG_CONFIG_POOL_SIZE); ret_code_t err_code; do { err_code = nrf_drv_rng_rand(p_buff, len); } while (err_code != NRF_SUCCESS); length -= len; p_buff += len; } NRF_LOG_DEBUG("Rand buffer data:"); NRF_LOG_HEXDUMP_DEBUG((uint8_t *)p_buff, length); }
ret_code_t nrf_drv_rng_rand(uint8_t * p_buff, uint8_t length) { ret_code_t err_code = NRF_SUCCESS; ASSERT(m_rng_cb.state == NRFX_DRV_STATE_INITIALIZED); #ifdef SOFTDEVICE_PRESENT do { bool sd_is_enabled; NRF_DRV_RNG_LOCK(); sd_is_enabled = NRF_DRV_RNG_SD_IS_ENABLED(); if (!sd_is_enabled) #endif // SOFTDEVICE_PRESENT { err_code = nrf_queue_read(&m_rand_pool, p_buff, (uint32_t)length); nrfx_rng_start(); } #ifdef SOFTDEVICE_PRESENT NRF_DRV_RNG_RELEASE(); if (sd_is_enabled) { err_code = sd_rand_application_vector_get(p_buff, length); if (err_code == NRF_ERROR_SOC_RAND_NOT_ENOUGH_VALUES) { err_code = NRF_ERROR_NOT_FOUND; } } } while (err_code == NRF_ERROR_SOFTDEVICE_NOT_ENABLED); #endif // SOFTDEVICE_PRESENT ASSERT((err_code == NRF_SUCCESS) || (err_code == NRF_ERROR_NOT_FOUND)); #if defined(RNG_CONFIG_RANDOM_NUMBER_LOG_ENABLED) && (RNG_CONFIG_RANDOM_NUMBER_LOG_ENABLED != 0) NRF_LOG_DEBUG("Rand buffer data:"); NRF_LOG_HEXDUMP_DEBUG((uint8_t *)p_buff, length); #endif // RNG_CONFIG_RANDOM_NUMBER_LOG_ENABLED NRF_LOG_WARNING("Function: %s, error code: %s.", (uint32_t)__func__, (uint32_t)NRF_LOG_ERROR_STRING_GET(err_code)); return err_code; }
/**@brief Function for executing the state entry action. */ static void spis_state_entry_action_execute(NRF_SPIS_Type * p_spis, spis_cb_t * p_cb) { nrf_drv_spis_event_t event; switch (p_cb->spi_state) { case SPIS_BUFFER_RESOURCE_REQUESTED: nrf_spis_task_trigger(p_spis, NRF_SPIS_TASK_ACQUIRE); break; case SPIS_BUFFER_RESOURCE_CONFIGURED: event.evt_type = NRF_DRV_SPIS_BUFFERS_SET_DONE; event.rx_amount = 0; event.tx_amount = 0; APP_ERROR_CHECK_BOOL(p_cb->handler != NULL); p_cb->handler(event); break; case SPIS_XFER_COMPLETED: event.evt_type = NRF_DRV_SPIS_XFER_DONE; event.rx_amount = nrf_spis_rx_amount_get(p_spis); event.tx_amount = nrf_spis_tx_amount_get(p_spis); NRF_LOG_INFO("Transfer rx_len:%d.\r\n", event.rx_amount); NRF_LOG_DEBUG("Rx data:\r\n"); NRF_LOG_HEXDUMP_DEBUG((uint8_t *)p_cb->rx_buffer, event.rx_amount * sizeof(p_cb->rx_buffer)); APP_ERROR_CHECK_BOOL(p_cb->handler != NULL); p_cb->handler(event); break; default: // No implementation required. break; } }
void I2S_IRQHandler(void) { uint32_t const * p_data_received = NULL; uint32_t * p_data_to_send = NULL; if (nrf_i2s_event_check(NRF_I2S, NRF_I2S_EVENT_TXPTRUPD)) { nrf_i2s_event_clear(NRF_I2S, NRF_I2S_EVENT_TXPTRUPD); NRF_LOG_DEBUG("Event: %s.", (uint32_t)EVT_TO_STR(NRF_I2S_EVENT_TXPTRUPD)); // If transmission is not enabled, but for some reason the TXPTRUPD // event has been generated, just ignore it. if (m_cb.p_tx_buffer != NULL) { uint32_t * p_tx_buffer_next; if (nrf_i2s_tx_buffer_get(NRF_I2S) == m_cb.p_tx_buffer) { p_tx_buffer_next = m_cb.p_tx_buffer + m_cb.buffer_half_size; } else { p_tx_buffer_next = m_cb.p_tx_buffer; } nrf_i2s_tx_buffer_set(NRF_I2S, p_tx_buffer_next); m_cb.tx_ready = true; // Now the part of the buffer that we've configured as "next" should // be filled by the application with proper data to be sent; // the peripheral is sending data from the other part of the buffer // (but it will finish soon...). p_data_to_send = p_tx_buffer_next; } } if (nrf_i2s_event_check(NRF_I2S, NRF_I2S_EVENT_RXPTRUPD)) { nrf_i2s_event_clear(NRF_I2S, NRF_I2S_EVENT_RXPTRUPD); NRF_LOG_DEBUG("Event: %s.", (uint32_t)EVT_TO_STR(NRF_I2S_EVENT_RXPTRUPD)); // If reception is not enabled, but for some reason the RXPTRUPD event // has been generated, just ignore it. if (m_cb.p_rx_buffer != NULL) { uint32_t * p_rx_buffer_next; if (nrf_i2s_rx_buffer_get(NRF_I2S) == m_cb.p_rx_buffer) { p_rx_buffer_next = m_cb.p_rx_buffer + m_cb.buffer_half_size; } else { p_rx_buffer_next = m_cb.p_rx_buffer; } nrf_i2s_rx_buffer_set(NRF_I2S, p_rx_buffer_next); m_cb.rx_ready = true; // The RXPTRUPD event is generated for the first time right after // the transfer is started. Since there is no data received yet at // this point we only update the buffer pointer (it is done above), // there is no callback to the application. // [for synchronized mode this has to be handled differently - // see below] if (m_cb.just_started && !m_cb.synchronized_mode) { m_cb.just_started = false; } else { // The RXPTRUPD event indicates that from now on the peripheral // will be filling the part of the buffer that was pointed at // the time the event has been generated, hence now we can let // the application process the data stored in the other part of // the buffer - the one that we've just set to be filled next. p_data_received = p_rx_buffer_next; } } } // Call the data handler passing received data to the application and/or // requesting data to be sent. if (!m_cb.synchronized_mode) { if ((p_data_received != NULL) || (p_data_to_send != NULL)) { if (p_data_received != NULL) { NRF_LOG_DEBUG("Rx data:"); NRF_LOG_HEXDUMP_DEBUG(p_data_received, m_cb.buffer_half_size * sizeof(p_data_received[0])); } m_cb.handler(p_data_received, p_data_to_send, m_cb.buffer_half_size); if (p_data_to_send != NULL) { NRF_LOG_DEBUG("Tx data:"); NRF_LOG_HEXDUMP_DEBUG(p_data_to_send, m_cb.buffer_half_size * sizeof(p_data_to_send[0])); } } } // In the synchronized mode wait until the events for both RX and TX occur. // And ignore the initial occurrences of these events, since they only // indicate that the transfer has started - no data is received yet at // that moment, so we have got nothing to pass to the application. else { if (m_cb.rx_ready && m_cb.tx_ready) { m_cb.rx_ready = false; m_cb.tx_ready = false; if (m_cb.just_started) { m_cb.just_started = false; } else { NRF_LOG_DEBUG("Rx data:"); NRF_LOG_HEXDUMP_DEBUG(nrf_i2s_rx_buffer_get(NRF_I2S), m_cb.buffer_half_size * sizeof(p_data_to_send[0])); m_cb.handler(nrf_i2s_rx_buffer_get(NRF_I2S), nrf_i2s_tx_buffer_get(NRF_I2S), m_cb.buffer_half_size); NRF_LOG_DEBUG("Tx data:"); NRF_LOG_HEXDUMP_DEBUG(nrf_i2s_tx_buffer_get(NRF_I2S), m_cb.buffer_half_size * sizeof(p_data_to_send[0])); } } } }