This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Multilink central + NUS client

Hello

I have a 3 custom PCBs with nRF52832 (1 central and 2 peripherals) and 1 nRF51 dongle (peripheral) arrangement where the two peripheral nRF52832 boards (custom PCBs) send their data to the central nRF52832, which then sends this data along with its own data to the dongle. For this purpose, I have merged the NUS client example with Multilink central program.

I am attaching the relevant main.c codes (for central device) below for your review. The UART receiver codes in dongle works just fine. I tried it with other example UART codes, connects well.

I am using SDK 14.2.0. I have no compilation errors but my central board does not connect with the dongle.Please let me know what changes I have to make

/************************** 1) BLE DEFINES **************************/

#define APP_BLE_CONN_CFG_TAG 1 /**< A tag that refers to the BLE stack configuration we set with @ref sd_ble_cfg_set. Default tag is @ref BLE_CONN_CFG_TAG_DEFAULT. */
#define APP_BLE_OBSERVER_PRIO 3 /**< Application's BLE observer priority. You shoulnd't need to modify this value. */

#define UART_TX_BUF_SIZE 1024 /**< UART TX buffer size. */
#define UART_RX_BUF_SIZE 256 /**< UART RX buffer size. */

#define NUS_SERVICE_UUID_TYPE BLE_UUID_TYPE_VENDOR_BEGIN /**< UUID type for the Nordic UART Service (vendor specific). */

#define SCAN_INTERVAL 0x00A0 /**< Determines scan interval in units of 0.625 millisecond. */
#define SCAN_WINDOW 0x0050 /**< Determines scan window in units of 0.625 millisecond. */
#define SCAN_TIMEOUT 0x0000 /**< Timout when scanning. 0x0000 disables timeout. */

#define MIN_CONNECTION_INTERVAL MSEC_TO_UNITS(7.5, UNIT_1_25_MS) /**< Determines minimum connection interval in millisecond. */
#define MAX_CONNECTION_INTERVAL MSEC_TO_UNITS(10.0, UNIT_1_25_MS) /**< Determines maximum connection interval in millisecond. */
#define SLAVE_LATENCY 0 /**< Determines slave latency in counts of connection events. */
#define SUPERVISION_TIMEOUT MSEC_TO_UNITS(4000, UNIT_10_MS) /**< Determines supervision time-out in units of 10 millisecond. */

#define UUID16_SIZE 2 /**< Size of 16 bit UUID */
#define UUID32_SIZE 4 /**< Size of 32 bit UUID */
#define UUID128_SIZE 16 /**< Size of 128 bit UUID */

#define ECHOBACK_BLE_UART_DATA 0 /**< Echo the UART data that is received over the Nordic UART Service back to the sender. */

/**************** 4) BLE Private Variables ***************/
/* Changes made */

BLE_NUS_C_ARRAY_DEF(m_ble_nus_c, NRF_SDH_BLE_CENTRAL_LINK_COUNT); /**< BLE NUS service client instance. */
NRF_BLE_GATT_DEF(m_gatt); /**< GATT module instance. */
BLE_DB_DISCOVERY_ARRAY_DEF(m_db_disc, NRF_SDH_BLE_CENTRAL_LINK_COUNT); /**< Database discovery module instances. */

static uint16_t m_ble_nus_max_data_len = BLE_GATT_ATT_MTU_DEFAULT - OPCODE_LENGTH - HANDLE_LENGTH; 
/**************** 12) DB discovery events handling ****************/
/* changed */

/**@brief Function for handling database discovery events.
*
* @details This function is callback function to handle events from the database discovery module.
* Depending on the UUIDs that are discovered, this function should forward the events
* to their respective services.
*
* @param[in] p_event Pointer to the database discovery event.
*/
static void db_disc_handler(ble_db_discovery_evt_t * p_evt)
{
// ble_nus_c_on_db_disc_evt(&m_ble_nus_c, p_evt);
ble_nus_c_on_db_disc_evt(&m_ble_nus_c[p_evt->conn_handle], p_evt);

}



/**************** 14) app_uart events ****************/
/* Changed */

/**@brief Function for handling app_uart events.
*
* @details This function will receive a single character from the app_uart module and append it to
* a string. The string will be be sent over BLE when the last character received was a
* 'new line' '\n' (hex 0x0A) or if the string has reached the maximum data length.
*/
void uart_event_handle(app_uart_evt_t * p_event)
{
static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
static uint16_t index = 0;
uint32_t ret_val;

switch (p_event->evt_type)
{
/**@snippet [Handling data from UART] */
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");
NRF_LOG_HEXDUMP_DEBUG(data_array, index);

do
{

for (uint32_t i = 0; i < NRF_SDH_BLE_CENTRAL_LINK_COUNT; i++)
{
ret_val = ble_nus_c_string_send(&m_ble_nus_c[i], data_array, index);
if ( (ret_val != NRF_ERROR_INVALID_STATE) && (ret_val != NRF_ERROR_BUSY) )
{
APP_ERROR_CHECK(ret_val);
}
}

} while (ret_val == NRF_ERROR_BUSY);

index = 0;
}
break;

/**@snippet [Handling data from UART] */
case APP_UART_COMMUNICATION_ERROR:
NRF_LOG_ERROR("Communication error occurred while handling UART.");
APP_ERROR_HANDLER(p_event->data.error_communication);
break;

case APP_UART_FIFO_ERROR:
NRF_LOG_ERROR("Error occurred in FIFO module used by UART.");
APP_ERROR_HANDLER(p_event->data.error_code);
break;

default:
break;
}
}


/**************** 14) NUS Client events ****************/
/* No changes */

/**@brief Callback handling NUS Client events.
 *
 * @details This function is called to notify the application of NUS client events.
 *
 * @param[in]   p_ble_nus_c   NUS Client Handle. This identifies the NUS client
 * @param[in]   p_ble_nus_evt Pointer to the NUS Client event.
 */

/**@snippet [Handling events from the ble_nus_c module] */
static void ble_nus_c_evt_handler(ble_nus_c_t * p_ble_nus_c, ble_nus_c_evt_t const * p_ble_nus_evt)
{
	ret_code_t err_code;

	switch (p_ble_nus_evt->evt_type)
	{
		case BLE_NUS_C_EVT_DISCOVERY_COMPLETE:
			NRF_LOG_INFO("Discovery complete.");
			err_code = ble_nus_c_handles_assign(&m_ble_nus_c[m_ble_nus_c->conn_handle], p_ble_nus_evt->conn_handle, &p_ble_nus_evt->handles);
			APP_ERROR_CHECK(err_code);

			err_code = ble_nus_c_tx_notif_enable(p_ble_nus_c);
			APP_ERROR_CHECK(err_code);
			NRF_LOG_INFO("Connected to device with Nordic UART Service.");
		
			// Connection to peripheral should be fully established by now, can start the ADC finally
			saadc_sampling_event_enable();
		  printf("\r\nConnection established! ADC started.\r\n");
			break;

		case BLE_NUS_C_EVT_NUS_TX_EVT:
			ble_nus_chars_received_uart_print(p_ble_nus_evt->p_data, p_ble_nus_evt->data_len);
			break;

		case BLE_NUS_C_EVT_DISCONNECTED:
			NRF_LOG_INFO("Disconnected.");
			scan_start();
			break;
	}
}

/**************** 16) BLE events ****************/
/* Changed */

/**@brief Function for handling BLE events.
 *
 * @param[in]   p_ble_evt   Bluetooth stack event.
 * @param[in]   p_context   Unused.
 */
static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
{
	ret_code_t            err_code;
	ble_gap_evt_t const * p_gap_evt = &p_ble_evt->evt.gap_evt;

	switch (p_ble_evt->header.evt_id)
	{
		case BLE_GAP_EVT_ADV_REPORT:
		{
			ble_gap_evt_adv_report_t const * p_adv_report = &p_gap_evt->params.adv_report;

			if (is_uuid_present(&m_nus_uuid, p_adv_report))
			{
				err_code = sd_ble_gap_connect(&p_adv_report->peer_addr,
											  &m_scan_params,
											  &m_connection_param,
											  APP_BLE_CONN_CFG_TAG);

				if (err_code == NRF_SUCCESS)
				{
					// scan is automatically stopped by the connect
					err_code = bsp_indication_set(BSP_INDICATE_IDLE);
					APP_ERROR_CHECK(err_code);
					NRF_LOG_INFO("Connecting to target %02x%02x%02x%02x%02x%02x",
							 p_adv_report->peer_addr.addr[0],
							 p_adv_report->peer_addr.addr[1],
							 p_adv_report->peer_addr.addr[2],
							 p_adv_report->peer_addr.addr[3],
							 p_adv_report->peer_addr.addr[4],
							 p_adv_report->peer_addr.addr[5]
							 );
				}
			}
		}break; // BLE_GAP_EVT_ADV_REPORT
		case BLE_GAP_EVT_CONNECTED:
		{
      APP_ERROR_CHECK_BOOL(p_gap_evt->conn_handle < NRF_SDH_BLE_CENTRAL_LINK_COUNT);
			err_code = ble_nus_c_handles_assign(&m_ble_nus_c[p_gap_evt->conn_handle], p_ble_evt->evt.gap_evt.conn_handle, NULL);
			APP_ERROR_CHECK(err_code);
			err_code = ble_db_discovery_start(&m_db_disc[p_gap_evt->conn_handle], p_ble_evt->evt.gap_evt.conn_handle);

			//bsp_board_led_on(CENTRAL_CONNECTED_LED);
      if (ble_conn_state_n_centrals() == NRF_SDH_BLE_CENTRAL_LINK_COUNT)
      {
			err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
			APP_ERROR_CHECK(err_code);

			}
      else
      {
				scan_start();
      }

		
		 /*case BLE_GAP_EVT_CONNECTED:
        {
            NRF_LOG_INFO("Connection 0x%x established, starting DB discovery.",
                         p_gap_evt->conn_handle);
            APP_ERROR_CHECK_BOOL(p_gap_evt->conn_handle < NRF_SDH_BLE_CENTRAL_LINK_COUNT);
            err_code = ble_nus_c_handles_assign(&m_ble_nus_c[p_gap_evt->conn_handle],
                                                p_gap_evt->conn_handle,
                                                NULL);
            APP_ERROR_CHECK(err_code);
			
						// start discovery of services. The NUS Client waits for a discovery result
					
						for (uint32_t i = 0; i < NRF_SDH_BLE_CENTRAL_LINK_COUNT; i++)
						{
							err_code = ble_db_discovery_start(&m_db_disc[i], p_ble_evt->evt.gap_evt.conn_handle);
							if (err_code != NRF_ERROR_BUSY)
							{
                APP_ERROR_CHECK(err_code);
							}
						}
						

            // Check if we should be looking for more peripherals to connect to.
            if (ble_conn_state_n_centrals() < NRF_SDH_BLE_CENTRAL_LINK_COUNT)
						{
							scan_start();
						}
						if (ble_conn_state_n_centrals() == NRF_SDH_BLE_CENTRAL_LINK_COUNT)
            {
                bsp_board_led_off(CENTRAL_SCANNING_LED);
            }
            else
            {
                // Resume scanning.
                bsp_board_led_on(CENTRAL_SCANNING_LED);
                scan_start();
            } Commented by Sapna */ 
						
        } break; // BLE_GAP_EVT_CONNECTED
		
		case BLE_GAP_EVT_TIMEOUT:
			if (p_gap_evt->params.timeout.src == BLE_GAP_TIMEOUT_SRC_SCAN)
			{
				NRF_LOG_INFO("Scan timed out.");
				scan_start();
			}
			else if (p_gap_evt->params.timeout.src == BLE_GAP_TIMEOUT_SRC_CONN)
			{
				NRF_LOG_INFO("Connection Request timed out.");
			}
			break;

		case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
			// Pairing not supported
			err_code = sd_ble_gap_sec_params_reply(p_ble_evt->evt.gap_evt.conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL);
			APP_ERROR_CHECK(err_code);
			break;

		case BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST:
			// Accepting parameters requested by peer.
			err_code = sd_ble_gap_conn_param_update(p_gap_evt->conn_handle,
													&p_gap_evt->params.conn_param_update_request.conn_params);
			APP_ERROR_CHECK(err_code);
			break;

#if defined(S132)
		case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
		{
			NRF_LOG_DEBUG("PHY update request.");
			ble_gap_phys_t const phys =
			{
				.rx_phys = BLE_GAP_PHY_AUTO,
				.tx_phys = BLE_GAP_PHY_AUTO,
			};
			err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys);
			APP_ERROR_CHECK(err_code);
		} break;
#endif

		case BLE_GATTC_EVT_TIMEOUT:
			// Disconnect on GATT Client timeout event.
			NRF_LOG_DEBUG("GATT Client Timeout.");
			err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle,
											 BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
			APP_ERROR_CHECK(err_code);
			break;

		case BLE_GATTS_EVT_TIMEOUT:
			// Disconnect on GATT Server timeout event.
			NRF_LOG_DEBUG("GATT Server Timeout.");
			err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle,
											 BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
			APP_ERROR_CHECK(err_code);
			break;

		default:
			break;
	}
}
/**************** 21) NUS client ****************/
/* Changes made */

/**@brief Function for initializing the NUS Client. */
static void nus_c_init(void)
{
	ret_code_t       err_code;
	ble_nus_c_init_t init;

	init.evt_handler = ble_nus_c_evt_handler;

	for (uint32_t i = 0; i < NRF_SDH_BLE_CENTRAL_LINK_COUNT; i++)
    {
        	err_code = ble_nus_c_init(&m_ble_nus_c[i], &init);
					APP_ERROR_CHECK(err_code);
    }
}

Parents Reply Children
No Data
Related