<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>SPI BLE receive wrong values via UART and BLE</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/56291/spi-ble-receive-wrong-values-via-uart-and-ble</link><description>Hey guys, 
 I&amp;#39;m working with a Laird BL652-DVK (nrF 52832 - chip), Nordic SDK v.14.2.0 and SES (Segger Embedded Studio v.4.30). My goal is to send datas from sensor (accelerometer LIS3DH) via bluetooth to my phone. I get via spi the datas from sensor</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 10 Jan 2020 08:07:22 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/56291/spi-ble-receive-wrong-values-via-uart-and-ble" /><item><title>RE: SPI BLE receive wrong values via UART and BLE</title><link>https://devzone.nordicsemi.com/thread/228484?ContentTypeID=1</link><pubDate>Fri, 10 Jan 2020 08:07:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:73dacf83-f950-441a-8d18-8ac52dc50ad2</guid><dc:creator>bjorn-spockeli</dc:creator><description>&lt;p&gt;Hi Christoph,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If you have are using a modified ble_app_uart example then you&amp;#39;re using notifications to send the sensor data to the phone. Notifications can be enabled or disabled by the central device(phone) writing to the CCCD of the characteristic on the slave device. When the CCCD is written to you will get enter the on_write handler in the NUS code. Here the&amp;nbsp;is_notification_enabled flag set to true, see below.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static void on_write(ble_nus_t * p_nus, ble_evt_t const * p_ble_evt)
{
    ble_gatts_evt_write_t const * p_evt_write = &amp;amp;p_ble_evt-&amp;gt;evt.gatts_evt.params.write;
    ble_nus_evt_t evt;
    evt.p_nus = p_nus;
    if (   (p_evt_write-&amp;gt;handle == p_nus-&amp;gt;tx_handles.cccd_handle)
        &amp;amp;&amp;amp; (p_evt_write-&amp;gt;len == 2))
    {
        if (ble_srv_is_notification_enabled(p_evt_write-&amp;gt;data))
        {
            p_nus-&amp;gt;is_notification_enabled = true;
            evt.type = BLE_NUS_EVT_COMM_STARTED;
        }
        else
        {
            p_nus-&amp;gt;is_notification_enabled = false;
            evt.type = BLE_NUS_EVT_COMM_STOPPED;
        }
        p_nus-&amp;gt;data_handler(&amp;amp;evt);
    }
    else if (   (p_evt_write-&amp;gt;handle == p_nus-&amp;gt;rx_handles.value_handle)
             &amp;amp;&amp;amp; (p_nus-&amp;gt;data_handler != NULL))
    {
        evt.params.rx_data.p_data = p_evt_write-&amp;gt;data;
        evt.params.rx_data.length = p_evt_write-&amp;gt;len;
        evt.type = BLE_NUS_EVT_RX_DATA;
        p_nus-&amp;gt;data_handler(&amp;amp;evt);
    }
    else
    {
        // Do Nothing. This event is not relevant for this service.
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;This flag is checked in&amp;nbsp;ble_nus_string_send, which I assume that you use to send the sensor data.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;uint32_t ble_nus_string_send(ble_nus_t * p_nus, uint8_t * p_string, uint16_t * p_length)
{
    ble_gatts_hvx_params_t hvx_params;

    VERIFY_PARAM_NOT_NULL(p_nus);

    if ((p_nus-&amp;gt;conn_handle == BLE_CONN_HANDLE_INVALID) || (!p_nus-&amp;gt;is_notification_enabled))
    {
        return NRF_ERROR_INVALID_STATE;
    }

    if (*p_length &amp;gt; BLE_NUS_MAX_DATA_LEN)
    {
        return NRF_ERROR_INVALID_PARAM;
    }

    memset(&amp;amp;hvx_params, 0, sizeof(hvx_params));

    hvx_params.handle = p_nus-&amp;gt;tx_handles.value_handle;
    hvx_params.p_data = p_string;
    hvx_params.p_len  = p_length;
    hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;

    return sd_ble_gatts_hvx(p_nus-&amp;gt;conn_handle, &amp;amp;hvx_params);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;As you can see&amp;nbsp;ble_nus_string_send will return NRF_ERROR_INVALID_STATE if is_notification_enabled is false. So you could simply set&amp;nbsp;&lt;span&gt;is_notification_enabled&amp;nbsp;to false when you receive the&lt;/span&gt;&amp;nbsp;&lt;span&gt;stop command from nRF Connect on your phone. Alternativly, you can use a timer to trigger the sensor data transmission and use the start/stop signals to start/stop this timer that will call&amp;nbsp;ble_nus_string_send in its timeout handler.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Best regards&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Bjørn&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI BLE receive wrong values via UART and BLE</title><link>https://devzone.nordicsemi.com/thread/228382?ContentTypeID=1</link><pubDate>Thu, 09 Jan 2020 14:16:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fc0a8ee4-3533-4a71-9150-4e08d7b9e7c7</guid><dc:creator>Christoph_I</dc:creator><description>&lt;p&gt;Hey bjorn-spockeli,&lt;/p&gt;
&lt;p&gt;I want something similar to the uart example (../examples/peripheral/uart). Here we see that we close the connection when we send &amp;#39;q&amp;#39; or &amp;#39;Q&amp;#39; via uart. (code below)&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;while (true)
    {
        uint8_t cr;
        while (app_uart_get(&amp;amp;cr) != NRF_SUCCESS);
        while (app_uart_put(cr) != NRF_SUCCESS);

        if (cr == &amp;#39;q&amp;#39; || cr == &amp;#39;Q&amp;#39;)
        {
            printf(&amp;quot; \r\nExit!\r\n&amp;quot;);

            while (true)
            {
                // Do nothing.
            }
        }
    }&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I want nearly the same for my modified ble_app_uart. But I don&amp;#39;t want to send the quit command &amp;#39;q&amp;#39; or &amp;#39;Q&amp;#39; via UART, but via ble. When I press on my phone (nRF Connect) &amp;#39;s&amp;#39; (for start), the nRF sends the sensor data to my phone via ble. When I press &amp;#39;q&amp;#39; the nRF should stop sending sensor data. And when I press &amp;#39;s&amp;#39; again the nRF send datas again. (pseudocode and photos below)&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;//initializing
....

uint8_t read_data[2];
uint16_t length = sizeof(read_data);
uint8_t start_stop_command;

start_stop_command = register_which_get_data_via_ble;   //which register I have to read? Or which variable?


while(1):
    switch(start_stop_command)
    {
        case(&amp;#39;s&amp;#39; or &amp;#39;S&amp;#39;):
            //read WHO_AM_I register from lis3dh (example)
            nrf_drv_spi_transfer(&amp;amp;spi, LIS3DH_WHO_AM_I_REG, sizeof(LIS3DH_WHO_AM_I_REG), read_data, sizeof(read_data));     
            while(!spi_xfer_done);
            
            //send WHO_AM_I value (data) via ble to my phone
            ble_nus_string_send(&amp;amp;m_nus, DecArrAxis, &amp;amp;length);
            
            //if pressed &amp;#39;q&amp;#39; or &amp;#39;Q&amp;#39;: break
            if(register_which_get_data_via_ble == &amp;#39;q&amp;#39; or register_which_get_data_via_ble == &amp;#39;Q&amp;#39;)
            {
                break;
            }
        
        case(&amp;#39;q&amp;#39; or &amp;#39;Q&amp;#39;):
            //do nothing
            if(register_which_get_data_via_ble == &amp;#39;s&amp;#39; or register_which_get_data_via_ble == &amp;#39;S&amp;#39;)
            {
                break;
            }
    }
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;start command: (app: nRF Connect on my phone)&lt;/p&gt;
&lt;p&gt;RX Characteristics --&amp;gt; Value: s&lt;/p&gt;
&lt;p&gt;stop command:(app: nRF Connect on my phone)&lt;/p&gt;
&lt;p&gt;RX Characteristics --&amp;gt; Value: q&lt;/p&gt;
&lt;p&gt;I hope I could explain clearly and precisely.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI BLE receive wrong values via UART and BLE</title><link>https://devzone.nordicsemi.com/thread/228357?ContentTypeID=1</link><pubDate>Thu, 09 Jan 2020 12:58:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:83838310-ef05-48eb-83a8-842ee374257c</guid><dc:creator>bjorn-spockeli</dc:creator><description>&lt;p&gt;Could you elaborate on this? Is it the phone that should not send any thing to the nRF before the nRF has sent the sensor data?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI BLE receive wrong values via UART and BLE</title><link>https://devzone.nordicsemi.com/thread/228151?ContentTypeID=1</link><pubDate>Wed, 08 Jan 2020 13:30:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f6e72114-64f9-402c-828c-00dbac2eb951</guid><dc:creator>Christoph_I</dc:creator><description>&lt;p&gt;And I have another question: Is there a possibility to query the bluetooth RX buffer? Because I want to implement a send command for sending the sensor datas via ble to my phone. Before the send command it should be not allowed to send anything.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI BLE receive wrong values via UART and BLE</title><link>https://devzone.nordicsemi.com/thread/228124?ContentTypeID=1</link><pubDate>Wed, 08 Jan 2020 12:35:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:764ede3c-f358-464a-ade3-b7bd49274387</guid><dc:creator>Christoph_I</dc:creator><description>&lt;p&gt;Thanks for your great input cbd, you know a lot! But I prefer not to change something in Nordic code so I programmed a function to convert decimal into ASCII. The code is below:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;void AxisDec_to_ASCII(uint8_t dec)    
{
    static uint8_t DecArray[3];		
    uint8_t hundred = dec / 100;
    dec -= hundred*100;
    uint8_t ten = dec / 10;	
    dec -= ten * 10;
    uint8_t one = dec;	
	
    DecArray[0] = (hundred+48);	
    DecArray[1] = (ten+48);		
    DecArray[2] = (one+48);		

    uint16_t length = sizeof(DecArray);
    ble_nus_string_send(&amp;amp;m_nus, DecArray, &amp;amp;length);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;And it works. It convert the datas from dec to hex. What do you think about my function? Any suggestions?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI BLE receive wrong values via UART and BLE</title><link>https://devzone.nordicsemi.com/thread/228103?ContentTypeID=1</link><pubDate>Wed, 08 Jan 2020 10:46:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5c4639b9-fc91-48fb-97e1-e6a01a2a5c57</guid><dc:creator>cbd</dc:creator><description>&lt;p&gt;You can send non-text (or ASCII) if you wish, as ultimately all data is binary anyway.&lt;/p&gt;
&lt;p&gt;Points to bear in mind:&lt;/p&gt;
&lt;p&gt;0x00 (or null) is a valid value and as it acts as a string terminator you will need to replace any string handling operators with array handling ones e.g. strlen to determine size of data to send will no longer work.&lt;/p&gt;
&lt;p&gt;The Nordic NUS code should support it as :&lt;pre class="ui-code" data-mode="c_cpp"&gt;err_code = ble_nus_data_send(&amp;amp;m_nus, data_array, &amp;amp;length, m_conn_handle);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;treats the data as binary anyway.&lt;/p&gt;
&lt;p&gt;The NUS code may require certain terminators such as &amp;#39;\n&amp;#39; or &amp;#39;\r&amp;#39; to determine end of text - so you&amp;#39;ll need to check that.&lt;/p&gt;
&lt;p&gt;nRF Connect will be expecting to handle the Nordic UART Service data as text. It probably wont handle the data correctly for display, particularly non-printable characters.&lt;/p&gt;
&lt;p&gt;You could use the Nordic code as is, but change the service UUID (change the nus_base_uuid ) in this section&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;// Add a custom base UUID.
    err_code = sd_ble_uuid_vs_add(&amp;amp;nus_base_uuid, &amp;amp;p_nus-&amp;gt;uuid_type);
    VERIFY_SUCCESS(err_code);

    ble_uuid.type = p_nus-&amp;gt;uuid_type;
    ble_uuid.uuid = BLE_UUID_NUS_SERVICE;

    // Add the service.
    err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
                                        &amp;amp;ble_uuid,
                                        &amp;amp;p_nus-&amp;gt;service_handle);&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI BLE receive wrong values via UART and BLE</title><link>https://devzone.nordicsemi.com/thread/228075?ContentTypeID=1</link><pubDate>Wed, 08 Jan 2020 09:56:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:12a832a3-209b-4438-868f-f67d48d9bf56</guid><dc:creator>Christoph_I</dc:creator><description>&lt;p&gt;Hey cbd,&lt;/p&gt;
&lt;p&gt;1)&lt;/p&gt;
[quote userid="72220" url="~/f/nordic-q-a/56291/spi-ble-receive-wrong-values-via-uart-and-ble/227931"][/quote]
&lt;p&gt;&amp;gt;&amp;gt;&amp;gt; I&amp;#39;ve not looked at your code yet, but points that spring to mind: 0x33 (hex) = 51 decimal = ASCII character &amp;#39;3&amp;#39;.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If your characteristic is defined as having a string type then nRF Connect will automatically convert it - displaying 3 for 0x33.&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&amp;lt;&amp;lt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;You are right, thank you. I forgot such things like ASCII and exact displaying. Also the second printf format wasn&amp;#39;t the right, thanks for your attention!&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;2)&lt;/p&gt;
[quote userid="72220" url="~/f/nordic-q-a/56291/spi-ble-receive-wrong-values-via-uart-and-ble/227931"]occur before you try to read the spi first time?[/quote]
&lt;p&gt;&amp;nbsp;In my modified periphal example &lt;em&gt;spim&lt;/em&gt; I have the first printf(...WHO_AM_I...) before &lt;em&gt;lis_init()&lt;/em&gt; and this worked... Now I tried out the 1st printf(...WHO_AM_I...) after &lt;em&gt;lis_init()&lt;/em&gt; because of your suggestion and now I get 0x33. I don&amp;#39;t know where is difference between &lt;em&gt;spim&lt;/em&gt; and &lt;em&gt;ble_app_uart &lt;/em&gt;but thank you cbd!&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;3)&lt;/p&gt;
&lt;p&gt;I have a further question: to send datas via bluetooth to my phone/pc I need the function &lt;em&gt;ble_nus_string_send()&lt;/em&gt; and this function send the data as string. Do you know if there are possibilities to send the datas as hex or decimal values? Or is there only the possibility for me to convert the values ​​manually (by an own function, e.g. convert 0x33 in string/ASCII &amp;quot;33&amp;quot; and give &amp;quot;33&amp;quot; to &lt;em&gt;ble_nus_string_send()&lt;/em&gt; )?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI BLE receive wrong values via UART and BLE</title><link>https://devzone.nordicsemi.com/thread/227931?ContentTypeID=1</link><pubDate>Tue, 07 Jan 2020 14:31:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ac6c2110-1dae-4dff-96c5-7d22200fcb5c</guid><dc:creator>cbd</dc:creator><description>&lt;p&gt;I&amp;#39;ve not looked at your code yet, but points that spring to mind: 0x33 (hex) = 51 decimal = ASCII character &amp;#39;3&amp;#39;.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If your characteristic is defined as having a string type then nRF Connect will automatically convert it - displaying 3 for 0x33.&lt;/p&gt;
&lt;p&gt;&amp;hellip;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Looking at your code in main() your first printf statement formats the value as hex:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;printf(&amp;quot;WHOAMI: %x\r\n&amp;quot;, read_data[1]);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;whereas your second printf formats the value as decimal:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;printf(&amp;quot;WHOAMI: %d\r\n&amp;quot;, read_data[1]);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;...&lt;/p&gt;
&lt;p&gt;I can&amp;#39;t see why your first character out is 0x24 (&amp;#39;$&amp;#39;), perhaps you can tell dynamically e.g. in debug.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;...&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Ah shouldn&amp;#39;t this line&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt; lis_init();&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;occur before you try to read the spi first time?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>