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

Porting ble_app_uart into twi_master_with_slave

Hi,

I am using the nRF52 with code from SDK 9.2, as well as the S132 softdevice. I basically ported code from the ble_uart_app into the twi_example to send data over bluetooth.

I tried to port the ble_uart functions from the main file, as well as the libraries it used. Once I finished porting the libraries, the code can build just fine, but the UART does not output any printfs. More importantly, the nordic does not begin advertisement.

I included my main function code just in case:

int main(void)
{
	uart_config();
  	twi_init(); 
    printf("\nTWI Sensor Example\r\n");
	//BT
	uint32_t err_code;
    bool erase_bonds;
    uint8_t buffer[BLE_NUS_MAX_DATA_LEN] = {0};
		
    // Initialize.
    APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS, APP_TIMER_OP_QUEUE_SIZE, false);
    buttons_leds_init(&erase_bonds);
    ble_stack_init();
    gap_params_init();
    services_init();
    advertising_init();
    conn_params_init();
	err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
    APP_ERROR_CHECK(err_code);
	//BT end
}
  • well for a start - there's no infinite loop at the end of that main() routine. Usually you'd have

    while( 1 )
        power_manage() 
    

    or _wfe() or similar

    Secondly - are you even getting to ble_advertising_start(), use the debugger.

    Thirdly- what is err_code at the end, is it zero or are you just going to the error handler.

  • hello,

    1. I do have a infinite while loop where I am taking data. I removed power manage, because I am not using interrupts to wake the device up every time I get a sample from the sensor. Is power manage absolutely necessary?

    2/3. I have been debugging through UART. Since UART messages are not working, I cannot see the err_code value.

  • well you should have something to stop main() just exiting. Currently you start advertising and if that's your real code, as soon as that's done, you're off the end of main and depending on what you're building with you could run into a loop, which would work, or a load of shutdown code, which wouldn't.

    If you have an infinite loop elsewhere then either

    1. You're in it already - ie does twi_init() start the infinite loop, in which case you're never going to get to the rest of the code or

    2. Your infinite loop is triggered later by a timer or connection and you've already left main() before you get there.

    just putting while(1) __wfe() at the end of main is a good idea.

    If you have a debugger, use it. If you don't, then you're just going to have to start commenting things out piece by piece until you find out where you are. I never know why people debug with UART, too hard.

  • Okay, I took what you said and used a debugger. The code stops working at ble_stack_init(). It does not get to err_code. Also, sorry, here is the full code.

    int main(void)
    {
    	uart_config(); 
    
    	printf("\nTWI Sensor Example\r\n");
    	//BT
    	uint32_t err_code;
    	bool erase_bonds;
    	uint8_t buffer[BLE_NUS_MAX_DATA_LEN] = {0};
    	twi_init(); //not BT
    	printf("TWI");
    	MC3610 MC; //not BT
    	MC.start(); //not BT
    
    	// Initialize.
    	APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS, APP_TIMER_OP_QUEUE_SIZE, false);
    	buttons_leds_init(&erase_bonds);
    	ble_stack_init();
    	gap_params_init();
    	services_init();
    	advertising_init();
    	conn_params_init();
    	err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
    	APP_ERROR_CHECK(err_code);
    	//BT end
    
    	#ifdef TEST_MODE //TEST_MODE must be defined in the options for IDE
    	jazz_unit_test_mcube();
    	#endif
    
    	while(1) {
    		mc3610_acc_t rawData = MC.readRawAccel();
    		printf("%*.4f,%*.4f,%*.4f\r\n",6,rawData.XAxis_g,6,rawData.YAxis_g,6,rawData.ZAxis_g); 
    		sprintf((char*)buffer,"%.4f,%.4f,%.4f\r\n", rawData.XAxis_g, rawData.YAxis_g, rawData.ZAxis_g);
    		
    		if(m_conn_handle != BLE_CONN_HANDLE_INVALID)
    		{
    			err_code = ble_nus_string_send(&m_nus, buffer, sizeof(buffer));
    			if (err_code != NRF_ERROR_INVALID_STATE)
    			{
    				APP_ERROR_CHECK(err_code);
    			}
    		}
    
    		power_manage(); //puts it in sleep mode. can be woken with interrupt
    
    		nrf_delay_ms(10);
    
    	}
    }
    
  • Judepinto24, Please do not delete the thread with many comments. It will be useful for others to see your discussion. Did you mean to mark it as completed?

1 2