<?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>TWI Bluetooth transmittion problem.</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/112142/twi-bluetooth-transmittion-problem</link><description>Hi everyone, 
 I&amp;#39;m trying to send updated gyro and acceleration values from MPU6050 continuously by BLE function from nrf52832. I&amp;#39;m currently using the nrf52 development kit board to run this code. However, I&amp;#39;m getting a NRF_ERROR_DATA_SIZE. 
 This is</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 23 Jul 2024 06:23:39 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/112142/twi-bluetooth-transmittion-problem" /><item><title>RE: TWI Bluetooth transmittion problem.</title><link>https://devzone.nordicsemi.com/thread/495126?ContentTypeID=1</link><pubDate>Tue, 23 Jul 2024 06:23:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:40845a92-921a-4c9c-9f71-60c93bdc8aa9</guid><dc:creator>Simonr</dc:creator><description>&lt;p&gt;Hi&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve taken over this case while Edvin is out of office and will try to help you out.&lt;/p&gt;
&lt;p&gt;First off, t&lt;span&gt;he error code&amp;nbsp;&lt;/span&gt;&lt;code dir="ltr"&gt;0xC&lt;/code&gt;&lt;span&gt;&amp;nbsp;corresponds to&amp;nbsp;&lt;/span&gt;&lt;code dir="ltr"&gt;NRF_ERROR_BUSY&lt;/code&gt;&lt;span&gt;, which means that the function&amp;nbsp;&lt;/span&gt;&lt;code dir="ltr"&gt;sd_ble_gatts_hvx&lt;/code&gt;&lt;span&gt;&amp;nbsp;is unable to complete because the SoftDevice is busy with another operation. So you likely have to wait for the SoftDevice to be ready before calling the characteristics update function in your application.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;The device doesn&amp;#39;t show an error code regarding what&amp;#39;s going wrong on the connection side, but I&amp;#39;m guessing it&amp;#39;s related as well. I think you&amp;#39;re trying to do &amp;quot;everything&amp;quot; at once without waiting for the SoftDevice to be ready for it. Please check out our other samples (ble_app_uart for example) to see how it&amp;#39;s done there, with breaks, wait loops, and return codes, etc. and implement that on your end as well.&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;Simon&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: TWI Bluetooth transmittion problem.</title><link>https://devzone.nordicsemi.com/thread/495092?ContentTypeID=1</link><pubDate>Mon, 22 Jul 2024 17:13:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6ad68b73-3e12-47fb-a05b-cd1234846455</guid><dc:creator>Joseph</dc:creator><description>&lt;p&gt;Hi AHaug,&lt;/p&gt;
&lt;p&gt;Would I be able to get a help faster? I need this code to work soon:(&lt;/p&gt;
&lt;p&gt;If there&amp;#39;s any possible way that I can get help, please let me know.&lt;/p&gt;
&lt;p&gt;Thank you!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: TWI Bluetooth transmittion problem.</title><link>https://devzone.nordicsemi.com/thread/494340?ContentTypeID=1</link><pubDate>Wed, 17 Jul 2024 11:50:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f100fb71-604a-4ab0-bf26-54974268e278</guid><dc:creator>AHaug</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Edvin is out on vacation for a few more weeks, so this is just a heads up to let you know that it might take some time before we can follow up on this. We&amp;#39;ll let you know more about a status/ETA next week.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thank you for your patience.&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;br /&gt;Andreas&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: TWI Bluetooth transmittion problem.</title><link>https://devzone.nordicsemi.com/thread/493775?ContentTypeID=1</link><pubDate>Sun, 14 Jul 2024 21:51:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:762dcadf-94df-4544-813a-024f553478a9</guid><dc:creator>Joseph</dc:creator><description>&lt;p&gt;Hi Edvin,&lt;/p&gt;
&lt;p&gt;I made changes to my code. I followed your idea and I&amp;#39;m using GATT to update the data that I want to send out when the Bluetooth connection is made. I&amp;#39;m still not sure what you mean by the defined latency.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;This is my current code but it seems like I&amp;#39;m not able to read the data that&amp;#39;s being updated through Wireshark. Also, I&amp;#39;m not able to connect to the advertising ble device with nRF Connect on my phone. It&amp;#39;s keep saying that it&amp;#39;s connecting. After few seconds, I get an error in the terminal saying this:&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/_A4C26CD0B0B9F7C0_-2024_2D00_07_2D00_14-165012.png" /&gt;&lt;/p&gt;
&lt;p&gt;This is my current code:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;lt;stdbool.h&amp;gt;
#include &amp;lt;stdint.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &amp;quot;nordic_common.h&amp;quot;
#include &amp;quot;nrf.h&amp;quot;
#include &amp;quot;app_error.h&amp;quot;
#include &amp;quot;ble.h&amp;quot;
#include &amp;quot;ble_hci.h&amp;quot;
#include &amp;quot;ble_srv_common.h&amp;quot;
#include &amp;quot;ble_advdata.h&amp;quot;
#include &amp;quot;ble_advertising.h&amp;quot;
#include &amp;quot;ble_conn_params.h&amp;quot;
#include &amp;quot;nrf_sdh.h&amp;quot;
#include &amp;quot;nrf_sdh_soc.h&amp;quot;
#include &amp;quot;nrf_sdh_ble.h&amp;quot;
#include &amp;quot;nrf_ble_gatt.h&amp;quot;
#include &amp;quot;nrf_ble_qwr.h&amp;quot;
#include &amp;quot;app_timer.h&amp;quot;
#include &amp;quot;nrf_pwr_mgmt.h&amp;quot;
#include &amp;quot;app_util_platform.h&amp;quot;
#include &amp;quot;nrf_drv_twi.h&amp;quot;
#include &amp;quot;nrf_delay.h&amp;quot;
#include &amp;quot;nrf_log.h&amp;quot;
#include &amp;quot;nrf_log_ctrl.h&amp;quot;
#include &amp;quot;nrf_log_default_backends.h&amp;quot;
#include &amp;quot;mpu6050.h&amp;quot;
#include &amp;lt;math.h&amp;gt;

#define DEVICE_NAME                     &amp;quot;MPU&amp;quot;
#define APP_ADV_INTERVAL                300
#define APP_ADV_DURATION                0
#define APP_BLE_CONN_CFG_TAG            1
#define APP_BLE_OBSERVER_PRIO           3

#define TWI_SCL_PIN                     25
#define TWI_SDA_PIN                     24

#define gyroSENSITIVITY                 131.0f
#define G_CONSTANT                      9.8f
#define accSENSITIVITY                  16384.0f

#define BLE_UUID_OUR_BASE_UUID          {0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x4A, 0xC3, 0x93, \
                                         0xE0, 0xA9, 0xE5, 0x0E, 0x24, 0xE7, 0xB5, 0x00} // 128-bit base UUID
#define BLE_UUID_OUR_SERVICE            0x1400
#define BLE_UUID_OUR_CHARACTERISTIC     0x1401

BLE_ADVERTISING_DEF(m_advertising);
NRF_BLE_GATT_DEF(m_gatt);
NRF_BLE_QWR_DEF(m_qwr);

static uint8_t m_adv_data[24];
static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID;
static ble_gatts_char_handles_t m_char_handles;
static uint16_t m_service_handle;

static void advertising_start(void);
void update_characteristic_value(void);
void mpu6050_data_update(void);

void twi_init(void) {
    ret_code_t err_code;

    const nrf_drv_twi_config_t twi_config = {
        .scl                = TWI_SCL_PIN,
        .sda                = TWI_SDA_PIN,
        .frequency          = NRF_TWI_FREQ_400K,
        .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
        .clear_bus_init     = false
    };

    err_code = nrf_drv_twi_init(&amp;amp;m_twi, &amp;amp;twi_config, NULL, NULL);
    APP_ERROR_CHECK(err_code);

    nrf_drv_twi_enable(&amp;amp;m_twi);
}

static void on_adv_evt(ble_adv_evt_t ble_adv_evt) {
    switch (ble_adv_evt) {
        case BLE_ADV_EVT_FAST:
            NRF_LOG_INFO(&amp;quot;Fast advertising.&amp;quot;);
            break;
        case BLE_ADV_EVT_IDLE:
            advertising_start();
            break;
        default:
            break;
    }
}

static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context) {
    ret_code_t err_code;

    switch (p_ble_evt-&amp;gt;header.evt_id) {
        case BLE_GAP_EVT_DISCONNECTED:
            NRF_LOG_INFO(&amp;quot;Disconnected.&amp;quot;);
            m_conn_handle = BLE_CONN_HANDLE_INVALID;
            advertising_start();
            break;
        case BLE_GAP_EVT_CONNECTED:
            NRF_LOG_INFO(&amp;quot;Connected.&amp;quot;);
            m_conn_handle = p_ble_evt-&amp;gt;evt.gap_evt.conn_handle;
            break;
        default:
            break;
    }
}

static void gap_params_init(void) {
    ret_code_t err_code;
    ble_gap_conn_params_t gap_conn_params;
    ble_gap_conn_sec_mode_t sec_mode;

    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&amp;amp;sec_mode);

    err_code = sd_ble_gap_device_name_set(&amp;amp;sec_mode, (const uint8_t *)DEVICE_NAME, strlen(DEVICE_NAME));
    APP_ERROR_CHECK(err_code);

    memset(&amp;amp;gap_conn_params, 0, sizeof(gap_conn_params));

    gap_conn_params.min_conn_interval = MSEC_TO_UNITS(100, UNIT_1_25_MS);
    gap_conn_params.max_conn_interval = MSEC_TO_UNITS(200, UNIT_1_25_MS);
    gap_conn_params.slave_latency     = 0;
    gap_conn_params.conn_sup_timeout  = MSEC_TO_UNITS(4000, UNIT_10_MS);

    err_code = sd_ble_gap_ppcp_set(&amp;amp;gap_conn_params);
    APP_ERROR_CHECK(err_code);
}

static void advertising_init(void) {
    ret_code_t err_code;
    ble_advdata_t advdata;

    memset(&amp;amp;advdata, 0, sizeof(advdata));

    advdata.name_type               = BLE_ADVDATA_FULL_NAME;
    advdata.include_appearance      = false;
    advdata.flags                   = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;

    ble_advertising_init_t init;
    memset(&amp;amp;init, 0, sizeof(init));

    init.advdata = advdata;
    init.config.ble_adv_fast_enabled  = true;
    init.config.ble_adv_fast_interval = APP_ADV_INTERVAL;
    init.config.ble_adv_fast_timeout  = APP_ADV_DURATION;
    init.evt_handler = on_adv_evt;

    err_code = ble_advertising_init(&amp;amp;m_advertising, &amp;amp;init);
    APP_ERROR_CHECK(err_code);

    ble_advertising_conn_cfg_tag_set(&amp;amp;m_advertising, APP_BLE_CONN_CFG_TAG);
}

static void services_init(void) {
    ret_code_t err_code;
    ble_uuid_t service_uuid;
    ble_uuid128_t base_uuid = BLE_UUID_OUR_BASE_UUID;
    service_uuid.uuid = BLE_UUID_OUR_SERVICE;

    err_code = sd_ble_uuid_vs_add(&amp;amp;base_uuid, &amp;amp;service_uuid.type);
    APP_ERROR_CHECK(err_code);

    err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &amp;amp;service_uuid, &amp;amp;m_service_handle);
    APP_ERROR_CHECK(err_code);

    // Add a characteristic to the service
    ble_gatts_char_md_t char_md;
    memset(&amp;amp;char_md, 0, sizeof(char_md));
    char_md.char_props.read = 1;
    char_md.char_props.write = 1;
    char_md.char_props.notify = 1;

    ble_uuid_t char_uuid;
    char_uuid.uuid = BLE_UUID_OUR_CHARACTERISTIC;
    char_uuid.type = service_uuid.type;

    ble_gatts_attr_md_t attr_md;
    memset(&amp;amp;attr_md, 0, sizeof(attr_md));
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&amp;amp;attr_md.read_perm);
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&amp;amp;attr_md.write_perm);
    attr_md.vloc = BLE_GATTS_VLOC_STACK;

    ble_gatts_attr_t attr_char_value;
    memset(&amp;amp;attr_char_value, 0, sizeof(attr_char_value));
    attr_char_value.p_uuid = &amp;amp;char_uuid;
    attr_char_value.p_attr_md = &amp;amp;attr_md;
    attr_char_value.init_len = sizeof(uint8_t) * 20;
    attr_char_value.init_offs = 0;
    attr_char_value.max_len = sizeof(uint8_t) * 20;

    err_code = sd_ble_gatts_characteristic_add(m_service_handle, &amp;amp;char_md, &amp;amp;attr_char_value, &amp;amp;m_char_handles);
    APP_ERROR_CHECK(err_code);
}

static void ble_stack_init(void) {
    ret_code_t err_code = nrf_sdh_enable_request();
    APP_ERROR_CHECK(err_code);

    uint32_t ram_start = 0x20001800;
    err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &amp;amp;ram_start);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_sdh_ble_enable(&amp;amp;ram_start);
    APP_ERROR_CHECK(err_code);

    NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
}

static void power_management_init(void) {
    ret_code_t err_code = nrf_pwr_mgmt_init();
    APP_ERROR_CHECK(err_code);
}

static void idle_state_handle(void) {
    if (NRF_LOG_PROCESS() == false) {
        nrf_pwr_mgmt_run();
    }
}

static void advertising_start(void) {
    ret_code_t err_code;
    err_code = ble_advertising_start(&amp;amp;m_advertising, BLE_ADV_MODE_FAST);
    APP_ERROR_CHECK(err_code);
}

void mpu6050_data_update(void) {
    int16_t accel_data[3], gyro_data[3];
    float ax, ay, az, gx, gy, gz;

    read_mpu6050_data(accel_data, gyro_data);

    ax = ((float)accel_data[0] / accSENSITIVITY) * G_CONSTANT;
    ay = ((float)accel_data[1] / accSENSITIVITY) * G_CONSTANT;
    az = ((float)accel_data[2] / accSENSITIVITY) * G_CONSTANT;
    gx = ((float)gyro_data[0] / gyroSENSITIVITY) * (M_PI / 180);
    gy = ((float)gyro_data[1] / gyroSENSITIVITY) * (M_PI / 180);
    gz = ((float)gyro_data[2] / gyroSENSITIVITY) * (M_PI / 180);

    memcpy(&amp;amp;m_adv_data[0],  &amp;amp;ax, 4);
    memcpy(&amp;amp;m_adv_data[4],  &amp;amp;ay, 4);
    memcpy(&amp;amp;m_adv_data[8],  &amp;amp;az, 4);
    memcpy(&amp;amp;m_adv_data[12], &amp;amp;gx, 4);
    memcpy(&amp;amp;m_adv_data[16], &amp;amp;gy, 4);
    memcpy(&amp;amp;m_adv_data[20], &amp;amp;gz, 4);

    NRF_LOG_RAW_INFO(&amp;quot;m_adv_data[] = &amp;quot;);
    for (int i = 0; i &amp;lt; sizeof(m_adv_data); i++) {
        NRF_LOG_RAW_INFO(&amp;quot;%02x:&amp;quot;, m_adv_data[i]);
    }
    NRF_LOG_RAW_INFO(&amp;quot;\r\n&amp;quot;);

    update_characteristic_value();
}

void update_characteristic_value(void) {
    if (m_conn_handle != BLE_CONN_HANDLE_INVALID) {
        uint16_t len = sizeof(m_adv_data);
        ble_gatts_hvx_params_t hvx_params;
        memset(&amp;amp;hvx_params, 0, sizeof(hvx_params));

        hvx_params.handle = m_char_handles.value_handle;
        hvx_params.type = BLE_GATT_HVX_NOTIFICATION;
        hvx_params.offset = 0;
        hvx_params.p_len = &amp;amp;len;
        hvx_params.p_data = m_adv_data;

        ret_code_t err_code = sd_ble_gatts_hvx(m_conn_handle, &amp;amp;hvx_params);
        if (err_code != NRF_SUCCESS) {
            NRF_LOG_ERROR(&amp;quot;sd_ble_gatts_hvx failed: 0x%x&amp;quot;, err_code);
        }
    }
}

int main(void) {
    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();

    NRF_LOG_INFO(&amp;quot;MPU6050 BLE example started.&amp;quot;);
    NRF_LOG_FLUSH();

    twi_init();
    mpu6050_init();

    power_management_init();
    ble_stack_init();
    gap_params_init();
    advertising_init();
    services_init();

    advertising_start();

    while (true) {
        mpu6050_data_update();

        nrf_delay_ms(1000);
        idle_state_handle();
    }
}
&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: TWI Bluetooth transmittion problem.</title><link>https://devzone.nordicsemi.com/thread/491696?ContentTypeID=1</link><pubDate>Mon, 01 Jul 2024 21:32:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7fc750f4-edf9-44a7-8e5f-34e3f88ce382</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;I see. Yes. Using a connection instead of advertising allows you to get more data through. I suggest that you check out the ble_app_uart and ble_app_uart_c examples from the SDK. Instead of sending the data through your advertisements, you can just create an array uint8_t my_data[24], and update this array, and send it using ble_nus_data_send().&lt;/p&gt;
&lt;p&gt;You should be able to get a decent throughput using this method, so you can send the data many times per second. You will however have a defined latency. Something called &amp;quot;the connection interval&amp;quot;. The connection interval is the amount of times that pass between every time the devices communicate. So if you queue up a packet, it will be sent the next connection interval. So if you are lucky, the queued packet will be sent less than 1ms after it is queued up, and if you are unlucky, you queue it up directly after the devices has communicated, meaning it is almost an entire connection interval until the next time it will be able to send the packet.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I think the default values should be quite sufficient, but let me know if you need it to be shorter, and you struggle to do so. By default you should see a connection interval of around 30ms. I don&amp;#39;t know how frequent you intend to send the data?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: TWI Bluetooth transmittion problem.</title><link>https://devzone.nordicsemi.com/thread/491447?ContentTypeID=1</link><pubDate>Sun, 30 Jun 2024 02:24:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bb3085d6-3ddd-4848-a984-4e3ae45c044f</guid><dc:creator>Joseph</dc:creator><description>&lt;p&gt;Hi Edvin,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thank you so much for your help:)&amp;nbsp;&lt;/p&gt;
&lt;p&gt;What I&amp;#39;m trying to do with the acceleration and gyro is to use that data to predict some linear motion.&lt;/p&gt;
&lt;p&gt;Therefore, I can&amp;#39;t receive acceleration and gyro values at different time. I also want to able to receive those data as soon as possible so that I have more accurate linear motion prediction.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m able to use the code you modified and get the updated ACC and gyro values through BLE but the interval of those data is pretty long.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I&amp;#39;m using other dongle(NRF52840) to fetch the BLE transmitted from NRF52. I&amp;#39;m using Wireshark to get the data.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I think I might be better of modifying the code so that I can connect to the dongle by Bluetooth and transmit data with no restrictions. However, I&amp;#39;m not sure how to code this..&lt;/p&gt;
&lt;p&gt;Would you be able to guide me please? Thank you so much for your help!!!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: TWI Bluetooth transmittion problem.</title><link>https://devzone.nordicsemi.com/thread/491422?ContentTypeID=1</link><pubDate>Fri, 28 Jun 2024 22:11:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a1f1c804-10c0-4243-9983-e46eea644c12</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Oh, I forgot to add the main.c file that I ended up with, where I split up the advertisement packet into advdata and srdata. Both in advertising_init() and in&amp;nbsp;update_advertising_data().&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/68108.main.c"&gt;devzone.nordicsemi.com/.../68108.main.c&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: TWI Bluetooth transmittion problem.</title><link>https://devzone.nordicsemi.com/thread/491421?ContentTypeID=1</link><pubDate>Fri, 28 Jun 2024 22:10:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9d38567d-c564-4091-9c90-06a2bea40407</guid><dc:creator>Edvin</dc:creator><description>[quote user="Sangwoo Suk"] I&amp;#39;m planning to gather those data through BLE.&amp;nbsp;[/quote]
&lt;p&gt;What do you plan to use to gather those data? Another nRF board? A mobile phone? A computer? What will be the scanner?&lt;/p&gt;
[quote user="Sangwoo Suk"]How can I store them so that I can transmit 3 gyro and 3 acceleration values all together with no ram problem?[/quote]
&lt;p&gt;The issue starts when you try to copy ax, ay, az, gx, gy, gz into the &amp;quot;scaled_value&amp;quot; parameter. I suggest you just copy the values of ax, ay, az, gx, gy, gz into your m_adv_data[] buffer:&lt;/p&gt;
&lt;p&gt;Now, I don&amp;#39;t have the gyroscope that you are using, so I will only change the main() function. For general c-functions, I usually google the name of it, and &amp;quot;c&amp;quot;, and click the first hit. So googling &amp;quot;memcpy c&amp;quot; lead me &lt;a href="https://www.tutorialspoint.com/c_standard_library/c_function_memcpy.htm"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;So I ended up with something like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;
void process_logs(void)
{
    while (NRF_LOG_PROCESS())
    {
        // do nothing.
    }
}

int main(void) {
    int16_t accel_data[3], gyro_data[3];
    float ax, ay, az, gx, gy, gz;

    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();

    NRF_LOG_INFO(&amp;quot;MPU6050 BLE example started.&amp;quot;);
    NRF_LOG_FLUSH();

    twi_init();
    mpu6050_init();

    power_management_init();
    ble_stack_init();
    gap_params_init();
    advertising_init();

    advertising_start();

    while (true) {
        //read_mpu6050_data(accel_data, gyro_data);

        // Log raw data for debugging
        NRF_LOG_INFO(&amp;quot;Raw ACC: X=%d, Y=%d, Z=%d; Raw GYRO: X=%d, Y=%d, Z=%d&amp;quot;,
                     accel_data[0], accel_data[1], accel_data[2],
                     gyro_data[0], gyro_data[1], gyro_data[2]);

        ax = ((float)accel_data[0] / accSENSITIVITY) * G_CONSTANT;
        ay = ((float)accel_data[1] / accSENSITIVITY) * G_CONSTANT;
        az = ((float)accel_data[2] / accSENSITIVITY) * G_CONSTANT;
        gx = ((float)gyro_data[0] / gyroSENSITIVITY) * (M_PI / 180);
        gy = ((float)gyro_data[1] / gyroSENSITIVITY) * (M_PI / 180);
        gz = ((float)gyro_data[2] / gyroSENSITIVITY) * (M_PI / 180);

        memcpy(&amp;amp;m_adv_data[0],  &amp;amp;ax, 4); // since the size of a float is 4 bytes.
        memcpy(&amp;amp;m_adv_data[4],  &amp;amp;ay, 4);
        memcpy(&amp;amp;m_adv_data[8],  &amp;amp;az, 4);
        memcpy(&amp;amp;m_adv_data[12], &amp;amp;gx, 4);
        memcpy(&amp;amp;m_adv_data[16], &amp;amp;gy, 4);
        memcpy(&amp;amp;m_adv_data[20], &amp;amp;gz, 4);

        NRF_LOG_RAW_INFO(&amp;quot;m_adv_data[] = &amp;quot;);
        for (int i=0; i&amp;lt;sizeof(m_adv_data); i++)
        {
          NRF_LOG_RAW_INFO(&amp;quot;%02x:&amp;quot;, m_adv_data[i]);
        }
        NRF_LOG_RAW_INFO(&amp;quot;\r\n&amp;quot;);

        update_advertising_data();

        process_logs();

        nrf_delay_ms(1000);
        idle_state_handle();
    }
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;And actually testing this I realized that there was not room in the advertising packet for the entire 24 bytes of gyro data, in addition to the 3 bytes for the name (&amp;quot;MPU&amp;quot;), the 1 byte for the flags and the advertising element description byte and advertising element length byte for each of your 3 elements in the advertising packet. In addition, manufacturer specific data requires the company identifier (2 bytes) as the first two bytes of the manufacturer specific data. So in total we were looking at:&lt;/p&gt;
&lt;p&gt;[1B]flags_element, [1B] length of flag element, [1B] actual flags, &lt;span&gt;[1B] name_element,&amp;nbsp;[1B] name length,&amp;nbsp;[3B] actual name,&amp;nbsp;[1B] manufacturer specific data element,&amp;nbsp;[1B] manufacturer specific data length,&amp;nbsp;[2B] manufacturer identifier,&amp;nbsp;[24B] gyro/accel data&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;which adds up to 36 bytes, which is slightly higher than the 31 bytes that are the max length of an advertising packet. Luckily, it is quite easy to add scan response data, which is a whole second advertising packet that you can use. So I stripped away the manufacturer specific data, and added it to the scan response data. The manufacturer specific data now consists of 28 bytes, and your main advertising packet is 8 bytes, including flags and name.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Depending on your scanner, this may be a little extra work, because now the name and the actual sensor data are no longer on the same advertising packet. So when you are scanning, depending on what you use to scan, you may need to first take note of the device that has the correct name, and then check the next advertising packet from that device (coming from the same address as the device with the correct name).&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Another option than splitting up the advertising packet in an advertising packet and a scan response packet is to slim down the amount of data in the original advertising packet. Perhaps you can send the accellerometer data and the gyro data every other second, and add a byte at the start saying whether this is the accellerometer data or the gyroscope data.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;A 3rd option is to actually enter a connection with the scanner. This way you can send a lot longer packets (up to 247 bytes), so the size should not be a limit there. In addition this will have ACKing, so that you know that the connected device actually received the packet.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;But back to the example where the advertisements are split into two packets, the advertising packet and the scan response packet. W&lt;/span&gt;hen you scan you will not immediately see nice float numbers in the advertisement packet, because that is not how embedded programming works. You can only send bytes, and each float number consists of 4 bytes. Then it is up to the receiver to determine how to interpret these 4 bytes. If you write the program for the receiver as well, you would know that this packet contains 6*4 bytes, which makes up 6 floating point numbers. So in case you want to do the reversal operation in C, it would look something like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;float az;
memcpy(&amp;amp;az, &amp;amp;m_scan_data[8], 4); // not necessarily m_scan_data[8], but whereever the start of that float ends up in the scanned advertising packet). 8 bytes after the manufacturer ID.
NRF_LOG_INFO(&amp;quot;az: &amp;quot; NRF_LOG_FLOAT_MARKER, NRF_LOG_FLOAT(az));&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: TWI Bluetooth transmittion problem.</title><link>https://devzone.nordicsemi.com/thread/491411?ContentTypeID=1</link><pubDate>Fri, 28 Jun 2024 19:01:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e93fb5d2-9667-4f39-809c-483490b8809e</guid><dc:creator>Joseph</dc:creator><description>&lt;p&gt;I don&amp;#39;t understand how to use the debug function. Also, I don&amp;#39;t think I understand how you store the acceleration and gyro values too..&amp;nbsp;&lt;/p&gt;
&lt;p&gt;What I&amp;#39;m trying to do is to send out advertising data packet with gyro and acceleration values. I&amp;#39;m planning to gather those data through BLE.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;How can I store them so that I can transmit 3 gyro and 3 acceleration values all together with no ram problem?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: TWI Bluetooth transmittion problem.</title><link>https://devzone.nordicsemi.com/thread/491063?ContentTypeID=1</link><pubDate>Thu, 27 Jun 2024 10:18:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2e9396bd-4324-44ec-879e-c327422df46e</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Thank you for sharing the projects.&lt;/p&gt;
&lt;p&gt;Some few hints while developing:&lt;/p&gt;
&lt;p&gt;1: Open your project settings. For all project settings, select the &amp;quot;Common&amp;quot; option in the drop down menu. Then select the preprocessor definitions, and add &amp;quot;DEBUG&amp;quot; to the list:&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1719480470721v1.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;2: Then I also added a small function that will print out the log. call this directly before nrf_delay_ms(1000) in your main loop. Do not call it from anywhere else, as it may lead to weird behavior.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;void process_logs(void)
{
    while (NRF_LOG_PROCESS())
    {
        // Do nothing.
    }
}

int main(void)
{
    ...
    while (true)
    {
        ...
        process_logs();
        nrf_delay_ms(1000);
        idle_state_handle();
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;3: Then, you can also debug your application. You can set breakpoints, and add variables to watch, to monitor their value. Note that when you are using the softdevice, and set a breakpoint that is hit, then you can&amp;#39;t continue to step. You need to reset the application.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Using these three steps, I see that the casting that you are doing is not working as you expect it to. The&amp;nbsp;scaled_value parameter doesn&amp;#39;t get the value of ax*100. It is 0.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t know what you intend to do with these numbers. If you only plan to send them out and process it somewhere else, then you can just copy the values into your m_adv_data (memcpy()). If you need to find out what format you want them to be.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: TWI Bluetooth transmittion problem.</title><link>https://devzone.nordicsemi.com/thread/490712?ContentTypeID=1</link><pubDate>Tue, 25 Jun 2024 19:02:41 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2b41d25b-1f08-4aec-ba95-561d6111bf14</guid><dc:creator>Joseph</dc:creator><description>&lt;p&gt;I just tried printing the m_adv_data.&lt;/p&gt;
&lt;p&gt;But I&amp;#39;m getting this issue:&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/_A4C26CD0B0B9F7C0_-2024_2D00_06_2D00_25-135245.png" /&gt;&lt;/p&gt;
&lt;p&gt;Also, I have attached zip file here. My project is under example file and the folder name is called &amp;quot;ble_app_template_twi(use this). go into pca10040 afterwards.&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/3515.nRF5_5F00_SDK_5F00_17.1.0_5F00_ddde560.zip"&gt;devzone.nordicsemi.com/.../3515.nRF5_5F00_SDK_5F00_17.1.0_5F00_ddde560.zip&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: TWI Bluetooth transmittion problem.</title><link>https://devzone.nordicsemi.com/thread/490161?ContentTypeID=1</link><pubDate>Mon, 24 Jun 2024 07:52:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:58a0fa41-e27b-4280-8bd8-3a5b658e1bdf</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;If you want me to test any parts of your code, then you need to zip and upload the entire application folder that you are using (the folder containing the example and configuration files, such as the SDK\examples\ble_peripheral\&amp;lt;your application project&amp;gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;So what do you expect the values to look like? Have you tried printing the hexadecimal values that you are feeding into your advertising data?&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;NRF_LOG_RAW_INFO(&amp;quot;m_adv_data[] = &amp;quot;);
for (int i=0; i&amp;lt;sizeof(m_adv_data); i++)
{
    NRF_LOG_RAW_INFO(&amp;quot;%02x:&amp;quot;, m_adv_data[i]);
}
NRF_LOG_RAW_INFO(&amp;quot;\r\n&amp;quot;);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;BR,&lt;br /&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: TWI Bluetooth transmittion problem.</title><link>https://devzone.nordicsemi.com/thread/490059?ContentTypeID=1</link><pubDate>Fri, 21 Jun 2024 23:33:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:401f7e5a-6fdb-46c0-b41c-3b1a0a653d98</guid><dc:creator>Joseph</dc:creator><description>&lt;p&gt;Thank you so much for your help!&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I currently having a problem with sending ax,ay,az,gx,gy,gz values through BLE.&lt;/p&gt;
&lt;p&gt;This is what acceleration and gyro values that should be sent out by BLE:&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/_A4C26CD0B0B9F7C0_-2024_2D00_06_2D00_21-181837.png" /&gt;&lt;/p&gt;
&lt;p&gt;However, this is a snapshot of data that&amp;#39;s sent through BLE. I used Wireshark to get this data:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;Frame 723682: 63 bytes on wire (504 bits), 63 bytes captured (504 bits) on interface COM8-4.2, id 0
    Section number: 1
    Interface id: 0 (COM8-4.2)
    Encapsulation type: nRF Sniffer for Bluetooth LE (186)
    Arrival Time: Jun 21, 2024 18:27:41.048745000 중부 일광 절약 시간
    UTC Arrival Time: Jun 21, 2024 23:27:41.048745000 UTC
    Epoch Arrival Time: 1719012461.048745000
    [Time shift for this packet: 0.000000000 seconds]
    [Time delta from previous captured frame: 0.000755000 seconds]
    [Time delta from previous displayed frame: 0.000755000 seconds]
    [Time since reference or first frame: 3786.864048000 seconds]
    Frame Number: 723682
    Frame Length: 63 bytes (504 bits)
    Capture Length: 63 bytes (504 bits)
    [Frame is marked: False]
    [Frame is ignored: False]
    [Protocols in frame: nordic_ble:btle:btcommon]
nRF Sniffer for Bluetooth LE
    Board: 8
    Header Version: 3, Packet counter: 6013
        Length of payload: 56
        Protocol version: 3
        Packet counter: 6013
        Packet ID: 2
    Length of packet: 10
    Flags: 0x01
        .... ...1 = CRC: Ok
        .... ..0. = Reserved: 0
        .... .0.. = Reserved: 0
        .... 0... = Address Resolved: No
        .000 .... = PHY: LE 1M (0)
        0... .... = Reserved: 0
    Channel Index: 39
    RSSI: -51 dBm
    Event counter: 0
    Timestamp: 76326918&amp;#181;s
    [Packet time (start to end): 376&amp;#181;s]
    [Delta time (end to start): 579&amp;#181;s]
    [Delta time (start to start): 755&amp;#181;s]
Bluetooth Low Energy Link Layer
    Access Address: 0x8e89bed6
    Packet Header: 0x2560 (PDU Type: ADV_IND, ChSel: #2, TxAdd: Random)
    Advertising Address: cc:69:fe:c9:06:21 (cc:69:fe:c9:06:21)
    Advertising Data
        Flags
            Length: 2
            Type: Flags (0x01)
            000. .... = Reserved: 0x0
            ...0 .... = Simultaneous LE and BR/EDR to Same Device Capable (Host): false (0x0)
            .... 0... = Simultaneous LE and BR/EDR to Same Device Capable (Controller): false (0x0)
            .... .1.. = BR/EDR Not Supported: true (0x1)
            .... ..1. = LE General Discoverable Mode: true (0x1)
            .... ...0 = LE Limited Discoverable Mode: false (0x0)
        Manufacturer Specific
            Length: 23
            Type: Manufacturer Specific (0xff)
            Company ID: Nordic Semiconductor ASA (0x0059)
            Data: 0024ffbd03760000000000000000000000000000
                [Expert Info (Note/Undecoded): Undecoded]
        Device Name (shortened): MP
            Length: 3
            Type: Device Name (shortened) (0x08)
            Device Name: MP
    CRC: 0xca4899
&lt;/pre&gt;I don&amp;#39;t think I&amp;#39;m sending correct acceleration and gyro values through BLE.&lt;/p&gt;
&lt;p&gt;This is my current code:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;lt;stdbool.h&amp;gt;
#include &amp;lt;stdint.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &amp;quot;nordic_common.h&amp;quot;
#include &amp;quot;nrf.h&amp;quot;
#include &amp;quot;app_error.h&amp;quot;
#include &amp;quot;ble.h&amp;quot;
#include &amp;quot;ble_hci.h&amp;quot;
#include &amp;quot;ble_srv_common.h&amp;quot;
#include &amp;quot;ble_advdata.h&amp;quot;
#include &amp;quot;ble_advertising.h&amp;quot;
#include &amp;quot;ble_conn_params.h&amp;quot;
#include &amp;quot;nrf_sdh.h&amp;quot;
#include &amp;quot;nrf_sdh_soc.h&amp;quot;
#include &amp;quot;nrf_sdh_ble.h&amp;quot;
#include &amp;quot;app_timer.h&amp;quot;
#include &amp;quot;nrf_pwr_mgmt.h&amp;quot;
#include &amp;quot;app_util_platform.h&amp;quot;
#include &amp;quot;nrf_drv_twi.h&amp;quot;
#include &amp;quot;nrf_delay.h&amp;quot;
#include &amp;quot;nrf_log.h&amp;quot;
#include &amp;quot;nrf_log_ctrl.h&amp;quot;
#include &amp;quot;nrf_log_default_backends.h&amp;quot;
#include &amp;quot;mpu6050.h&amp;quot;
#include &amp;lt;math.h&amp;gt;  // Include this for M_PI(pi)

#define DEVICE_NAME                     &amp;quot;MPU&amp;quot;                          
#define APP_ADV_INTERVAL                300                             
#define APP_ADV_DURATION                0                               
#define APP_BLE_CONN_CFG_TAG            1                               

#define TWI_SCL_PIN                     25                              
#define TWI_SDA_PIN                     24                              

#define gyroSENSITIVITY                 131.0f
#define G_CONSTANT                      9.8f
#define accSENSITIVITY                  16384.0f

BLE_ADVERTISING_DEF(m_advertising);                                      

static uint8_t m_adv_data[20];                                            
static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID;                  
static char log_buffer[256];

static void advertising_start(void);

void twi_init(void) {
    ret_code_t err_code;

    const nrf_drv_twi_config_t twi_config = {
       .scl                = TWI_SCL_PIN,
       .sda                = TWI_SDA_PIN,
       .frequency          = NRF_TWI_FREQ_400K,
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
       .clear_bus_init     = false
    };

    err_code = nrf_drv_twi_init(&amp;amp;m_twi, &amp;amp;twi_config, NULL, NULL);
    APP_ERROR_CHECK(err_code);

    nrf_drv_twi_enable(&amp;amp;m_twi);
}

static void on_adv_evt(ble_adv_evt_t ble_adv_evt) {
    switch (ble_adv_evt) {
        case BLE_ADV_EVT_FAST:
            NRF_LOG_INFO(&amp;quot;Fast advertising.&amp;quot;);
            break;
        case BLE_ADV_EVT_IDLE:
            advertising_start();
            break;
        default:
            break;
    }
}

static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context) {
    switch (p_ble_evt-&amp;gt;header.evt_id) {
        case BLE_GAP_EVT_DISCONNECTED:
            NRF_LOG_INFO(&amp;quot;Disconnected.&amp;quot;);
            break;
        case BLE_GAP_EVT_CONNECTED:
            NRF_LOG_INFO(&amp;quot;Connected.&amp;quot;);
            m_conn_handle = p_ble_evt-&amp;gt;evt.gap_evt.conn_handle;
            break;
        default:
            break;
    }
}

static void gap_params_init(void) {
    ret_code_t err_code;
    ble_gap_conn_params_t gap_conn_params;
    ble_gap_conn_sec_mode_t sec_mode;

    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&amp;amp;sec_mode);

    err_code = sd_ble_gap_device_name_set(&amp;amp;sec_mode,
                                          (const uint8_t *)DEVICE_NAME,
                                          strlen(DEVICE_NAME));
    APP_ERROR_CHECK(err_code);

    memset(&amp;amp;gap_conn_params, 0, sizeof(gap_conn_params));

    gap_conn_params.min_conn_interval = MSEC_TO_UNITS(100, UNIT_1_25_MS);
    gap_conn_params.max_conn_interval = MSEC_TO_UNITS(200, UNIT_1_25_MS);
    gap_conn_params.slave_latency     = 0;
    gap_conn_params.conn_sup_timeout  = MSEC_TO_UNITS(4000, UNIT_10_MS);

    err_code = sd_ble_gap_ppcp_set(&amp;amp;gap_conn_params);
    APP_ERROR_CHECK(err_code);
}

static void advertising_init(void) {
    ret_code_t err_code;
    ble_advdata_t advdata;

    memset(&amp;amp;advdata, 0, sizeof(advdata));

    advdata.name_type               = BLE_ADVDATA_FULL_NAME;
    advdata.include_appearance      = false;
    advdata.flags                   = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;

    ble_advdata_manuf_data_t manuf_data;
    manuf_data.company_identifier = 0x0059; // Nordic Semiconductor company ID
    manuf_data.data.p_data = m_adv_data;
    manuf_data.data.size = sizeof(m_adv_data);

    advdata.p_manuf_specific_data = &amp;amp;manuf_data;

    ble_advertising_init_t init;
    memset(&amp;amp;init, 0, sizeof(init));

    init.advdata = advdata;
    init.config.ble_adv_fast_enabled  = true;
    init.config.ble_adv_fast_interval = APP_ADV_INTERVAL;
    init.config.ble_adv_fast_timeout  = APP_ADV_DURATION;
    init.evt_handler = on_adv_evt;

    err_code = ble_advertising_init(&amp;amp;m_advertising, &amp;amp;init);
    APP_ERROR_CHECK(err_code);

    ble_advertising_conn_cfg_tag_set(&amp;amp;m_advertising, APP_BLE_CONN_CFG_TAG);
}

static void ble_stack_init(void) {
    ret_code_t err_code = nrf_sdh_enable_request();
    APP_ERROR_CHECK(err_code);

    uint32_t ram_start = 0x20001800;  // Adjusted RAM start
    err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &amp;amp;ram_start);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_sdh_ble_enable(&amp;amp;ram_start);
    APP_ERROR_CHECK(err_code);
}

static void power_management_init(void) {
    ret_code_t err_code = nrf_pwr_mgmt_init();
    APP_ERROR_CHECK(err_code);
}

static void idle_state_handle(void) {
    if (NRF_LOG_PROCESS() == false) {
        nrf_pwr_mgmt_run();
    }
}

static void advertising_start(void) {
    ret_code_t err_code = ble_advertising_start(&amp;amp;m_advertising, BLE_ADV_MODE_FAST);
    APP_ERROR_CHECK(err_code);
}

void update_advertising_data(void) {
    ret_code_t err_code;
    ble_advdata_t advdata;

    ble_advdata_manuf_data_t manuf_data;
    manuf_data.company_identifier = 0x0059;
    manuf_data.data.p_data = m_adv_data;
    manuf_data.data.size = sizeof(m_adv_data);

    memset(&amp;amp;advdata, 0, sizeof(advdata));
    advdata.name_type               = BLE_ADVDATA_FULL_NAME;
    advdata.include_appearance      = false;
    advdata.flags                   = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
    advdata.p_manuf_specific_data   = &amp;amp;manuf_data;

    err_code = ble_advertising_advdata_update(&amp;amp;m_advertising, &amp;amp;advdata, NULL);
    APP_ERROR_CHECK(err_code);
}

int main(void) {
    int16_t accel_data[3], gyro_data[3];
    float ax, ay, az, gx, gy, gz;

    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();

    NRF_LOG_INFO(&amp;quot;MPU6050 BLE example started.&amp;quot;);
    NRF_LOG_FLUSH();

    twi_init();
    mpu6050_init();

    power_management_init();
    ble_stack_init();
    gap_params_init();
    advertising_init();

    advertising_start();

    while (true) {
        read_mpu6050_data(accel_data, gyro_data);

        // Log raw data for debugging
        NRF_LOG_INFO(&amp;quot;Raw ACC: X=%d, Y=%d, Z=%d; Raw GYRO: X=%d, Y=%d, Z=%d&amp;quot;,
                     accel_data[0], accel_data[1], accel_data[2],
                     gyro_data[0], gyro_data[1], gyro_data[2]);

        ax = ((float)accel_data[0] / accSENSITIVITY) * G_CONSTANT;
        ay = ((float)accel_data[1] / accSENSITIVITY) * G_CONSTANT;
        az = ((float)accel_data[2] / accSENSITIVITY) * G_CONSTANT;
        gx = ((float)gyro_data[0] / gyroSENSITIVITY) * (M_PI / 180);
        gy = ((float)gyro_data[1] / gyroSENSITIVITY) * (M_PI / 180);
        gz = ((float)gyro_data[2] / gyroSENSITIVITY) * (M_PI / 180);


        uint8_t offset = 0;
        // Directly update m_adv_data array with scaled values
        int16_t scaled_value;

        scaled_value = (int16_t)(ax*100);
        m_adv_data[offset++] = (scaled_value &amp;gt;&amp;gt; 8) &amp;amp; 0xFF;
        m_adv_data[offset++] = scaled_value &amp;amp; 0xFF;

        scaled_value = (int16_t)(ay*100);
        m_adv_data[offset++] = (scaled_value &amp;gt;&amp;gt; 8) &amp;amp; 0xFF;
        m_adv_data[offset++] = scaled_value &amp;amp; 0xFF;

        scaled_value = (int16_t)(az*100);
        m_adv_data[offset++] = (scaled_value &amp;gt;&amp;gt; 8) &amp;amp; 0xFF;
        m_adv_data[offset++] = scaled_value &amp;amp; 0xFF;

        scaled_value = (int16_t)(gx*100);
        m_adv_data[offset++] = (scaled_value &amp;gt;&amp;gt; 8) &amp;amp; 0xFF;
        m_adv_data[offset++] = scaled_value &amp;amp; 0xFF;

        scaled_value = (int16_t)(gy*100);
        m_adv_data[offset++] = (scaled_value &amp;gt;&amp;gt; 8) &amp;amp; 0xFF;
        m_adv_data[offset++] = scaled_value &amp;amp; 0xFF;

        scaled_value = (int16_t)(gz*100);
        m_adv_data[offset++] = (scaled_value &amp;gt;&amp;gt; 8) &amp;amp; 0xFF;
        m_adv_data[offset++] = scaled_value &amp;amp; 0xFF;

        update_advertising_data();

        nrf_delay_ms(1000);
        idle_state_handle();
    }
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Thank you for helping me!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: TWI Bluetooth transmittion problem.</title><link>https://devzone.nordicsemi.com/thread/489722?ContentTypeID=1</link><pubDate>Thu, 20 Jun 2024 11:58:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cd95d89c-08fc-4b68-92d6-f3d4aca27c13</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Unless you actually intend to do any calculations on the floating numbers, you probably don&amp;#39;t need to print them in the log. If the goal is to just send them over bluetooth either way. However, if you want to log them, please try to split up your problem. Try to strip out the accellerometer, and just try to print a floating number in the log. I got it working using:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#define ACCSENSITIVITY 16384.0f
#define GCONSTANT 9.81f

int main(void)
{
    ...
    
    float my_float = 32768.14;
    float ax = ((float)my_float / ACCSENSITIVITY * GCONSTANT);
    NRF_LOG_INFO(&amp;quot;floating number:&amp;quot; NRF_LOG_FLOAT_MARKER &amp;quot;\r\n&amp;quot;, NRF_LOG_FLOAT(ax));
    // Enter main loop.
    for (;;)
    {
        idle_state_handle();
    }&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Perhaps your log message is too long. Try printing only one at the time.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: TWI Bluetooth transmittion problem.</title><link>https://devzone.nordicsemi.com/thread/489595?ContentTypeID=1</link><pubDate>Wed, 19 Jun 2024 21:01:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5054d374-9d92-4f5a-b67b-96430a65590f</guid><dc:creator>Joseph</dc:creator><description>&lt;p&gt;I made a change and I get an error.&lt;/p&gt;
&lt;p&gt;This is the error:&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/_A4C26CD0B0B9F7C0_-2024_2D00_06_2D00_19-155957.png" /&gt;&lt;/p&gt;
&lt;p&gt;This is my current code:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#include &amp;lt;stdbool.h&amp;gt;
#include &amp;lt;stdint.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &amp;quot;nordic_common.h&amp;quot;
#include &amp;quot;nrf.h&amp;quot;
#include &amp;quot;app_error.h&amp;quot;
#include &amp;quot;ble.h&amp;quot;
#include &amp;quot;ble_hci.h&amp;quot;
#include &amp;quot;ble_srv_common.h&amp;quot;
#include &amp;quot;ble_advdata.h&amp;quot;
#include &amp;quot;ble_advertising.h&amp;quot;
#include &amp;quot;ble_conn_params.h&amp;quot;
#include &amp;quot;nrf_sdh.h&amp;quot;
#include &amp;quot;nrf_sdh_soc.h&amp;quot;
#include &amp;quot;nrf_sdh_ble.h&amp;quot;
#include &amp;quot;app_timer.h&amp;quot;
#include &amp;quot;nrf_pwr_mgmt.h&amp;quot;
#include &amp;quot;app_util_platform.h&amp;quot;
#include &amp;quot;nrf_drv_twi.h&amp;quot;
#include &amp;quot;nrf_delay.h&amp;quot;
#include &amp;quot;nrf_log.h&amp;quot;
#include &amp;quot;nrf_log_ctrl.h&amp;quot;
#include &amp;quot;nrf_log_default_backends.h&amp;quot;
#include &amp;quot;mpu6050.h&amp;quot;
#include &amp;lt;math.h&amp;gt;  // Include this for M_PI(pi)

#define DEVICE_NAME                     &amp;quot;MPU&amp;quot;                          
#define APP_ADV_INTERVAL                300                             
#define APP_ADV_DURATION                0                               
#define APP_BLE_CONN_CFG_TAG            1                               

#define TWI_SCL_PIN                     25                              
#define TWI_SDA_PIN                     24                              

#define gyroSENSITIVITY                 131.0f
#define G_CONSTANT                      9.81f
#define accSENSITIVITY                  16384.0f

BLE_ADVERTISING_DEF(m_advertising);                                      

static uint8_t m_adv_data[20];                                            
static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID;                  
static char log_buffer[256];

static void advertising_start(void);

void twi_init(void) {
    ret_code_t err_code;

    const nrf_drv_twi_config_t twi_config = {
       .scl                = TWI_SCL_PIN,
       .sda                = TWI_SDA_PIN,
       .frequency          = NRF_TWI_FREQ_400K,
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
       .clear_bus_init     = false
    };

    err_code = nrf_drv_twi_init(&amp;amp;m_twi, &amp;amp;twi_config, NULL, NULL);
    APP_ERROR_CHECK(err_code);

    nrf_drv_twi_enable(&amp;amp;m_twi);
}

static void on_adv_evt(ble_adv_evt_t ble_adv_evt) {
    switch (ble_adv_evt) {
        case BLE_ADV_EVT_FAST:
            NRF_LOG_INFO(&amp;quot;Fast advertising.&amp;quot;);
            break;
        case BLE_ADV_EVT_IDLE:
            advertising_start();
            break;
        default:
            break;
    }
}

static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context) {
    switch (p_ble_evt-&amp;gt;header.evt_id) {
        case BLE_GAP_EVT_DISCONNECTED:
            NRF_LOG_INFO(&amp;quot;Disconnected.&amp;quot;);
            break;
        case BLE_GAP_EVT_CONNECTED:
            NRF_LOG_INFO(&amp;quot;Connected.&amp;quot;);
            m_conn_handle = p_ble_evt-&amp;gt;evt.gap_evt.conn_handle;
            break;
        default:
            break;
    }
}

static void gap_params_init(void) {
    ret_code_t err_code;
    ble_gap_conn_params_t gap_conn_params;
    ble_gap_conn_sec_mode_t sec_mode;

    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&amp;amp;sec_mode);

    err_code = sd_ble_gap_device_name_set(&amp;amp;sec_mode,
                                          (const uint8_t *)DEVICE_NAME,
                                          strlen(DEVICE_NAME));
    APP_ERROR_CHECK(err_code);

    memset(&amp;amp;gap_conn_params, 0, sizeof(gap_conn_params));

    gap_conn_params.min_conn_interval = MSEC_TO_UNITS(100, UNIT_1_25_MS);
    gap_conn_params.max_conn_interval = MSEC_TO_UNITS(200, UNIT_1_25_MS);
    gap_conn_params.slave_latency     = 0;
    gap_conn_params.conn_sup_timeout  = MSEC_TO_UNITS(4000, UNIT_10_MS);

    err_code = sd_ble_gap_ppcp_set(&amp;amp;gap_conn_params);
    APP_ERROR_CHECK(err_code);
}

static void advertising_init(void) {
    ret_code_t err_code;
    ble_advdata_t advdata;

    memset(&amp;amp;advdata, 0, sizeof(advdata));

    advdata.name_type               = BLE_ADVDATA_FULL_NAME;
    advdata.include_appearance      = false;
    advdata.flags                   = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;

    ble_advdata_manuf_data_t manuf_data;
    manuf_data.company_identifier = 0x0059; // Nordic Semiconductor company ID
    manuf_data.data.p_data = m_adv_data;
    manuf_data.data.size = sizeof(m_adv_data);

    advdata.p_manuf_specific_data = &amp;amp;manuf_data;

    ble_advertising_init_t init;
    memset(&amp;amp;init, 0, sizeof(init));

    init.advdata = advdata;
    init.config.ble_adv_fast_enabled  = true;
    init.config.ble_adv_fast_interval = APP_ADV_INTERVAL;
    init.config.ble_adv_fast_timeout  = APP_ADV_DURATION;
    init.evt_handler = on_adv_evt;

    err_code = ble_advertising_init(&amp;amp;m_advertising, &amp;amp;init);
    APP_ERROR_CHECK(err_code);

    ble_advertising_conn_cfg_tag_set(&amp;amp;m_advertising, APP_BLE_CONN_CFG_TAG);
}

static void ble_stack_init(void) {
    ret_code_t err_code = nrf_sdh_enable_request();
    APP_ERROR_CHECK(err_code);

    uint32_t ram_start = 0x20001800;  // Adjusted RAM start
    err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &amp;amp;ram_start);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_sdh_ble_enable(&amp;amp;ram_start);
    APP_ERROR_CHECK(err_code);
}

static void power_management_init(void) {
    ret_code_t err_code = nrf_pwr_mgmt_init();
    APP_ERROR_CHECK(err_code);
}

static void idle_state_handle(void) {
    if (NRF_LOG_PROCESS() == false) {
        nrf_pwr_mgmt_run();
    }
}

static void advertising_start(void) {
    ret_code_t err_code = ble_advertising_start(&amp;amp;m_advertising, BLE_ADV_MODE_FAST);
    APP_ERROR_CHECK(err_code);
}

// Function to pack scaled float data into an array as int16_t
void pack_scaled_data(uint8_t *buffer, float value, uint8_t *offset) {
    int16_t scaled_value = (int16_t)(value * 10);  // Scale to 1 decimal place
    buffer[*offset] = (scaled_value &amp;gt;&amp;gt; 8) &amp;amp; 0xFF;
    buffer[*offset + 1] = scaled_value &amp;amp; 0xFF;
    *offset += 2;
}

void update_advertising_data(void) {
    ret_code_t err_code;
    ble_advdata_t advdata;

    ble_advdata_manuf_data_t manuf_data;
    manuf_data.company_identifier = 0x0059;
    manuf_data.data.p_data = m_adv_data;
    manuf_data.data.size = sizeof(m_adv_data);

    memset(&amp;amp;advdata, 0, sizeof(advdata));
    advdata.name_type               = BLE_ADVDATA_FULL_NAME;
    advdata.include_appearance      = false;
    advdata.flags                   = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
    advdata.p_manuf_specific_data   = &amp;amp;manuf_data;

    err_code = ble_advertising_advdata_update(&amp;amp;m_advertising, &amp;amp;advdata, NULL);
    APP_ERROR_CHECK(err_code);
}

int main(void) {
    int16_t accel_data[3], gyro_data[3];
    float ax, ay, az, gx, gy, gz;

    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();

    NRF_LOG_INFO(&amp;quot;MPU6050 BLE example started.&amp;quot;);
    NRF_LOG_FLUSH();

    twi_init();
    mpu6050_init();

    power_management_init();
    ble_stack_init();
    gap_params_init();
    advertising_init();

    advertising_start();

    while (true) {
        read_mpu6050_data(accel_data, gyro_data);

        // Log raw data for debugging
        NRF_LOG_INFO(&amp;quot;Raw ACC: X=%d, Y=%d, Z=%d; Raw GYRO: X=%d, Y=%d, Z=%d&amp;quot;,
                     accel_data[0], accel_data[1], accel_data[2],
                     gyro_data[0], gyro_data[1], gyro_data[2]);

        ax = ((float)accel_data[0] / accSENSITIVITY) * G_CONSTANT;
        ay = ((float)accel_data[1] / accSENSITIVITY) * G_CONSTANT;
        az = ((float)accel_data[2] / accSENSITIVITY) * G_CONSTANT;
        gx = ((float)gyro_data[0] / gyroSENSITIVITY) * (M_PI / 180);
        gy = ((float)gyro_data[1] / gyroSENSITIVITY) * (M_PI / 180);
        gz = ((float)gyro_data[2] / gyroSENSITIVITY) * (M_PI / 180);

        // Log calculated values using NRF_LOG_FLOAT_MARKER and NRF_LOG_FLOAT
        NRF_LOG_INFO(&amp;quot;Calculated ACC: X=&amp;quot; NRF_LOG_FLOAT_MARKER &amp;quot;, Y=&amp;quot; NRF_LOG_FLOAT_MARKER &amp;quot;, Z=&amp;quot; NRF_LOG_FLOAT_MARKER, NRF_LOG_FLOAT(ax), NRF_LOG_FLOAT(ay), NRF_LOG_FLOAT(az));
        NRF_LOG_INFO(&amp;quot;Calculated GYRO: X=&amp;quot; NRF_LOG_FLOAT_MARKER &amp;quot;, Y=&amp;quot; NRF_LOG_FLOAT_MARKER &amp;quot;, Z=&amp;quot; NRF_LOG_FLOAT_MARKER, NRF_LOG_FLOAT(gx), NRF_LOG_FLOAT(gy), NRF_LOG_FLOAT(gz));


        uint8_t offset = 0;
        // Pack acceleration data
        pack_scaled_data(m_adv_data, ax, &amp;amp;offset);
        pack_scaled_data(m_adv_data, ay, &amp;amp;offset);
        pack_scaled_data(m_adv_data, az, &amp;amp;offset);
        // Pack gyroscope data
        pack_scaled_data(m_adv_data, gx, &amp;amp;offset);
        pack_scaled_data(m_adv_data, gy, &amp;amp;offset);
        pack_scaled_data(m_adv_data, gz, &amp;amp;offset);

        update_advertising_data();

        nrf_delay_ms(1000);
        idle_state_handle();
    }
}
&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: TWI Bluetooth transmittion problem.</title><link>https://devzone.nordicsemi.com/thread/489537?ContentTypeID=1</link><pubDate>Wed, 19 Jun 2024 13:23:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6d860cd7-ee6f-4061-bb74-e1fbd87b357c</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;The way float numbers are handled on embedded devices is often a little different. And also in the NRF_LOG module.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Try printing them using this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;NRF_LOG_INFO(&amp;quot;My float number: &amp;quot; NRF_LOG_FLOAT_MARKER &amp;quot;\r\n&amp;quot;, NRF_LOG_FLOAT(ax));&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Does that work?&lt;/p&gt;
&lt;p&gt;BR,&lt;br /&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: TWI Bluetooth transmittion problem.</title><link>https://devzone.nordicsemi.com/thread/489382?ContentTypeID=1</link><pubDate>Tue, 18 Jun 2024 19:31:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8542b358-24b7-44ce-b0e0-22f23432aeb9</guid><dc:creator>Joseph</dc:creator><description>&lt;p&gt;I made m_adv_data as 20 byte array. It seems like I&amp;#39;m not getting the data issue.&lt;/p&gt;
&lt;p&gt;However, I&amp;#39;m getting another issue. I&amp;#39;m not able to output ACC and gyro values through LOG. I know that my sensor is working properly. I think there&amp;#39;s something wrong with the code. This is the error:&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:129px;max-width:320px;" alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/640x258/__key/communityserver-discussions-components-files/4/_A4C26CD0B0B9F7C0_-2024_2D00_06_2D00_18-150604.png" /&gt;&lt;/p&gt;
&lt;p&gt;This is my current code:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#include &amp;lt;stdbool.h&amp;gt;
#include &amp;lt;stdint.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &amp;quot;nordic_common.h&amp;quot;
#include &amp;quot;nrf.h&amp;quot;
#include &amp;quot;app_error.h&amp;quot;
#include &amp;quot;ble.h&amp;quot;
#include &amp;quot;ble_hci.h&amp;quot;
#include &amp;quot;ble_srv_common.h&amp;quot;
#include &amp;quot;ble_advdata.h&amp;quot;
#include &amp;quot;ble_advertising.h&amp;quot;
#include &amp;quot;ble_conn_params.h&amp;quot;
#include &amp;quot;nrf_sdh.h&amp;quot;
#include &amp;quot;nrf_sdh_soc.h&amp;quot;
#include &amp;quot;nrf_sdh_ble.h&amp;quot;
#include &amp;quot;app_timer.h&amp;quot;
#include &amp;quot;nrf_pwr_mgmt.h&amp;quot;
#include &amp;quot;app_util_platform.h&amp;quot;
#include &amp;quot;nrf_drv_twi.h&amp;quot;
#include &amp;quot;nrf_delay.h&amp;quot;
#include &amp;quot;nrf_log.h&amp;quot;
#include &amp;quot;nrf_log_ctrl.h&amp;quot;
#include &amp;quot;nrf_log_default_backends.h&amp;quot;
#include &amp;quot;mpu6050.h&amp;quot;
#include &amp;lt;math.h&amp;gt;  // Include this for M_PI(pi)

#define DEVICE_NAME                     &amp;quot;MPU&amp;quot;                          
#define APP_ADV_INTERVAL                300                             
#define APP_ADV_DURATION                0                               
#define APP_BLE_CONN_CFG_TAG            1                               

#define TWI_SCL_PIN                     25                              
#define TWI_SDA_PIN                     24                              

#define gyroSENSITIVITY                 131.0f
#define G_CONSTANT                      9.81f
#define accSENSITIVITY                  16384.0f

BLE_ADVERTISING_DEF(m_advertising);                                      

static uint8_t m_adv_data[20];                                            
static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID;                  
static char log_buffer[256];

static void advertising_start(void);

void twi_init(void) {
    ret_code_t err_code;

    const nrf_drv_twi_config_t twi_config = {
       .scl                = TWI_SCL_PIN,
       .sda                = TWI_SDA_PIN,
       .frequency          = NRF_TWI_FREQ_400K,
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
       .clear_bus_init     = false
    };

    err_code = nrf_drv_twi_init(&amp;amp;m_twi, &amp;amp;twi_config, NULL, NULL);
    APP_ERROR_CHECK(err_code);

    nrf_drv_twi_enable(&amp;amp;m_twi);
}

static void on_adv_evt(ble_adv_evt_t ble_adv_evt) {
    switch (ble_adv_evt) {
        case BLE_ADV_EVT_FAST:
            NRF_LOG_INFO(&amp;quot;Fast advertising.&amp;quot;);
            break;
        case BLE_ADV_EVT_IDLE:
            advertising_start();
            break;
        default:
            break;
    }
}

static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context) {
    switch (p_ble_evt-&amp;gt;header.evt_id) {
        case BLE_GAP_EVT_DISCONNECTED:
            NRF_LOG_INFO(&amp;quot;Disconnected.&amp;quot;);
            break;
        case BLE_GAP_EVT_CONNECTED:
            NRF_LOG_INFO(&amp;quot;Connected.&amp;quot;);
            m_conn_handle = p_ble_evt-&amp;gt;evt.gap_evt.conn_handle;
            break;
        default:
            break;
    }
}

static void gap_params_init(void) {
    ret_code_t err_code;
    ble_gap_conn_params_t gap_conn_params;
    ble_gap_conn_sec_mode_t sec_mode;

    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&amp;amp;sec_mode);

    err_code = sd_ble_gap_device_name_set(&amp;amp;sec_mode,
                                          (const uint8_t *)DEVICE_NAME,
                                          strlen(DEVICE_NAME));
    APP_ERROR_CHECK(err_code);

    memset(&amp;amp;gap_conn_params, 0, sizeof(gap_conn_params));

    gap_conn_params.min_conn_interval = MSEC_TO_UNITS(100, UNIT_1_25_MS);
    gap_conn_params.max_conn_interval = MSEC_TO_UNITS(200, UNIT_1_25_MS);
    gap_conn_params.slave_latency     = 0;
    gap_conn_params.conn_sup_timeout  = MSEC_TO_UNITS(4000, UNIT_10_MS);

    err_code = sd_ble_gap_ppcp_set(&amp;amp;gap_conn_params);
    APP_ERROR_CHECK(err_code);
}

static void advertising_init(void) {
    ret_code_t err_code;
    ble_advdata_t advdata;

    memset(&amp;amp;advdata, 0, sizeof(advdata));

    advdata.name_type               = BLE_ADVDATA_FULL_NAME;
    advdata.include_appearance      = false;
    advdata.flags                   = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;

    ble_advdata_manuf_data_t manuf_data;
    manuf_data.company_identifier = 0x0059; // Nordic Semiconductor company ID
    manuf_data.data.p_data = m_adv_data;
    manuf_data.data.size = sizeof(m_adv_data);

    advdata.p_manuf_specific_data = &amp;amp;manuf_data;

    ble_advertising_init_t init;
    memset(&amp;amp;init, 0, sizeof(init));

    init.advdata = advdata;
    init.config.ble_adv_fast_enabled  = true;
    init.config.ble_adv_fast_interval = APP_ADV_INTERVAL;
    init.config.ble_adv_fast_timeout  = APP_ADV_DURATION;
    init.evt_handler = on_adv_evt;

    err_code = ble_advertising_init(&amp;amp;m_advertising, &amp;amp;init);
    APP_ERROR_CHECK(err_code);

    ble_advertising_conn_cfg_tag_set(&amp;amp;m_advertising, APP_BLE_CONN_CFG_TAG);
}

static void ble_stack_init(void) {
    ret_code_t err_code = nrf_sdh_enable_request();
    APP_ERROR_CHECK(err_code);

    uint32_t ram_start = 0x20001800;  // Adjusted RAM start
    err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &amp;amp;ram_start);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_sdh_ble_enable(&amp;amp;ram_start);
    APP_ERROR_CHECK(err_code);
}

static void power_management_init(void) {
    ret_code_t err_code = nrf_pwr_mgmt_init();
    APP_ERROR_CHECK(err_code);
}

static void idle_state_handle(void) {
    if (NRF_LOG_PROCESS() == false) {
        nrf_pwr_mgmt_run();
    }
}

static void advertising_start(void) {
    ret_code_t err_code = ble_advertising_start(&amp;amp;m_advertising, BLE_ADV_MODE_FAST);
    APP_ERROR_CHECK(err_code);
}

// Function to pack scaled float data into an array as int16_t
void pack_scaled_data(uint8_t *buffer, float value, uint8_t *offset) {
    int16_t scaled_value = (int16_t)(value * 10);  // Scale to 1 decimal place
    buffer[*offset] = (scaled_value &amp;gt;&amp;gt; 8) &amp;amp; 0xFF;
    buffer[*offset + 1] = scaled_value &amp;amp; 0xFF;
    *offset += 2;
}

void update_advertising_data(void) {
    ret_code_t err_code;
    ble_advdata_t advdata;

    ble_advdata_manuf_data_t manuf_data;
    manuf_data.company_identifier = 0x0059;
    manuf_data.data.p_data = m_adv_data;
    manuf_data.data.size = sizeof(m_adv_data);

    memset(&amp;amp;advdata, 0, sizeof(advdata));
    advdata.name_type               = BLE_ADVDATA_FULL_NAME;
    advdata.include_appearance      = false;
    advdata.flags                   = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
    advdata.p_manuf_specific_data   = &amp;amp;manuf_data;

    err_code = ble_advertising_advdata_update(&amp;amp;m_advertising, &amp;amp;advdata, NULL);
    APP_ERROR_CHECK(err_code);
}

int main(void) {
    int16_t accel_data[3], gyro_data[3];

    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();

    NRF_LOG_INFO(&amp;quot;MPU6050 BLE example started.&amp;quot;);
    NRF_LOG_FLUSH();

    twi_init();
    mpu6050_init();

    power_management_init();
    ble_stack_init();
    gap_params_init();
    advertising_init();

    advertising_start();

    while (true) {
        read_mpu6050_data(accel_data, gyro_data);

        float ax = ((float)accel_data[0] / accSENSITIVITY) * G_CONSTANT;
        float ay = ((float)accel_data[1] / accSENSITIVITY) * G_CONSTANT;
        float az = ((float)accel_data[2] / accSENSITIVITY) * G_CONSTANT;
        float gx = ((float)gyro_data[0] / gyroSENSITIVITY) * (M_PI / 180);
        float gy = ((float)gyro_data[1] / gyroSENSITIVITY) * (M_PI / 180);
        float gz = ((float)gyro_data[2] / gyroSENSITIVITY) * (M_PI / 180);

        snprintf(log_buffer, sizeof(log_buffer), &amp;quot;ACC: X=%0.1f, Y=%0.1f, Z=%0.1f; GYRO: X=%0.1f, Y=%0.1f, Z=%0.1f&amp;quot;,
                 ax, ay, az, gx, gy, gz);
        NRF_LOG_INFO(&amp;quot;%s&amp;quot;, NRF_LOG_PUSH(log_buffer));

        uint8_t offset = 0;
        // Pack acceleration data
        pack_scaled_data(m_adv_data, ax, &amp;amp;offset);
        pack_scaled_data(m_adv_data, ay, &amp;amp;offset);
        pack_scaled_data(m_adv_data, az, &amp;amp;offset);
        // Pack gyroscope data
        pack_scaled_data(m_adv_data, gx, &amp;amp;offset);
        pack_scaled_data(m_adv_data, gy, &amp;amp;offset);
        pack_scaled_data(m_adv_data, gz, &amp;amp;offset);

        update_advertising_data();

        nrf_delay_ms(1000);
        idle_state_handle();
    }
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: TWI Bluetooth transmittion problem.</title><link>https://devzone.nordicsemi.com/thread/489125?ContentTypeID=1</link><pubDate>Mon, 17 Jun 2024 13:37:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0554af9b-4112-4c04-8e43-f252d435d2fd</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;It is difficult to say exactly what the issue is caused by based on the file that you sent. Does the log say anything about where this error was coming from?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I do see, however, that you are setting your:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;    ble_advdata_manuf_data_t manuf_data;
    manuf_data.company_identifier = 0x0059; // Nordic Semiconductor company ID
    manuf_data.data.p_data = m_adv_data;
    manuf_data.data.size = sizeof(m_adv_data);
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;and the m_adv_data is a 31 byte array.&lt;/p&gt;
&lt;p&gt;It is correct that you have 31 bytes at your disposal, but two of these bytes will be the company identifier, one byte will be the manufacturer_specific data length.&lt;/p&gt;
&lt;p&gt;I believe the scan response packet will look like: &amp;lt;type&amp;gt;&amp;lt;length&amp;gt;&amp;lt;company_identifier&amp;gt;&amp;lt;payload&amp;gt;&lt;/p&gt;
&lt;p&gt;So 1 byte for type, 1 byte for length, 2 bytes for company_identifier, this leaves you with 31-1-1-2 = 27 bytes for manufacturer data payload. Try to reduce the m_adv_data array to this size, and keep reducing it if you still get the same error. Or even better, reduce it to the amount of bytes you actually read from your sensor. A float takes 4 bytes. So if you try to push 6 float numbers, that is 24 bytes.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>