tag:blogger.com,1999:blog-52098570818561573352024-03-13T22:22:09.639-04:00Embedded Software DevelopmentHoanhttp://www.blogger.com/profile/13256219944725692395noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-5209857081856157335.post-32172302285960325242018-02-23T22:10:00.002-05:002020-04-21T20:13:38.125-04:00<h2>
<div style="text-align: center;">
<span style="font-size: x-large;">Bluetooth LE with Nordic nRF51 & nRF52 series<br />
The easy way!<br />
Part 3</span><br />
<br /></div>
</h2>
<h3>
<div style="text-align: center;">
Custom service<br />
(UART over BLE) <br />
<br /></div>
</h3>
<br />
<br />
We have looked at how to advertise in previous post. Now, lets see how simple it is to create a BLE custom service. For this purpose, we'll create an UART to BLE firmware that sends whatever it received on UART RX to BLE and whatever that is received from BLE to UART TX. <br />
<br />
First thing first, lets start by initializing the UART interface. As mentioned before the concept of EHAL is very simple. Everything is initialized with a Config data first then just use. <br />
<br />
The UART interface configuration starts by defining I/O pins to be used for the UART.<br />
<br />
<div style="color: #4f76cb; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
/// UART pins definitions</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #931a68;">static</span> <span style="color: #006141;">IOPINCFG</span> s_UartPins[] = {</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
{UART_RX_PORT, UART_RX_PIN, UART_RX_PINOP, <span style="color: #0326cc;">IOPINDIR_INPUT</span>, <span style="color: #0326cc;">IOPINRES_NONE</span>, <span style="color: #0326cc;">IOPINTYPE_NORMAL</span>},<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #4e9072;">// RX</span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
{UART_TX_PORT, UART_TX_PIN, UART_TX_PINOP, <span style="color: #0326cc;">IOPINDIR_OUTPUT</span>, <span style="color: #0326cc;">IOPINRES_NONE</span>, <span style="color: #0326cc;">IOPINTYPE_NORMAL</span>},<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #4e9072;">// TX</span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
{UART_CTS_PORT, UART_CTS_PIN, UART_CTS_PINOP, <span style="color: #0326cc;">IOPINDIR_INPUT</span>, <span style="color: #0326cc;">IOPINRES_NONE</span>, <span style="color: #0326cc;">IOPINTYPE_NORMAL</span>},<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #4e9072;">// CTS</span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
{UART_RTS_PORT, UART_RTS_PIN, UART_RTS_PINOP, <span style="color: #0326cc;">IOPINDIR_OUTPUT</span>, <span style="color: #0326cc;">IOPINRES_NONE</span>, <span style="color: #0326cc;">IOPINTYPE_NORMAL</span>},<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #4e9072;">// RTS</span></div>
<div style="color: #4f76cb; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
};</div>
<div>
<br /></div>
<br />
Then the configuration data and the UART interface instance.<br />
<br />
<div style="color: #4f76cb; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
/// UART configuration</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #931a68;">const</span> <span style="color: #006141;">UARTCFG</span> g_UartCfg = {</div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> 0,<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// Device number zero based</div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> s_UartPins,<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// UART assigned pins</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #931a68;">sizeof</span><span style="color: black;">(s_UartPins) / </span><span style="color: #931a68;">sizeof</span><span style="color: black;">(</span><span style="color: #006141;">IOPINCFG</span><span style="color: black;">),</span><span style="color: #4e9072;">// Total number of UART pins used</span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
1000000,<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #4e9072;">// </span><span style="color: #4e9072; text-decoration: underline;">Baudrate</span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
8,<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #4e9072;">// Data bits</span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #0326cc;">UART_PARITY_NONE<span style="color: black;">,<span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #4e9072;">// Parity</span></span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
1,<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #4e9072;">// Stop bit</span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #0326cc;">UART_FLWCTRL_HW<span style="color: black;">,<span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #4e9072;">// Flow control</span></span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #931a68;">true</span><span style="color: black;">,<span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #4e9072;">// Interrupt mode</span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #0326cc;">APP_IRQ_PRIORITY_LOW</span><span style="color: black;">,<span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #4e9072;">// Interrupt priority</span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
nRFUartEvthandler,<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #4e9072;">// UART event handler</span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #931a68;">true</span><span style="color: black;">,<span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #4e9072;">// Blocking FIFO</span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
};</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 15px;">
<br /></div>
<div style="color: #4f76cb; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
/// UART object instance</div>
<div style="color: #4f76cb; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #006141; text-decoration: underline;">UART</span> g_Uart;</div>
<div>
<br /></div>
Initialize the interface :<br />
<br />
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
g_Uart.Init(g_UartCfg);</div>
<br />
Using UART :<br />
<br />
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
g_Uart.printf(<span style="color: #3933ff;">"Hello\r\n"</span>);</div>
<div>
<br /></div>
<br />
Now the BLE configuration for custom service.<br />
<br />
<div style="color: #4f76cb; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
/// Characteristic definitions</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #006141;">BLESRVC_CHAR</span> g_UartChars[] = {</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
{</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="white-space: pre;"> </span><span style="color: #4e9072;">// Read characteristic</span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>BLUEIO_UUID_UART_RX_CHAR,</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>20,</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>BLESVC_CHAR_PROP_READ | BLESVC_CHAR_PROP_NOTIFY | BLESVC_CHAR_PROP_VARLEN,</div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span>s_RxCharDescString,<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// char UTF-8 description string</div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: black; text-decoration: underline;">NULL</span><span style="color: black;">,<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// Callback for write char, set to NULL for read char</div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: black; text-decoration: underline;">NULL</span><span style="color: black;">,<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// Callback on set notification</div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: black; text-decoration: underline;">NULL</span><span style="color: black;">,<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// <span style="text-decoration: underline;">Tx</span> completed callback</div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: black; text-decoration: underline;">NULL</span><span style="color: black;">,<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// pointer to char default values</div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span>0,<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// Default value length in bytes</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
},</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
{</div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span>// Write characteristic</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>BLUEIO_UUID_UART_TX_CHAR,<span style="color: #4e9072;">// char UUID</span></div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span>20,<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// char max data length</div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span>BLESVC_CHAR_PROP_WRITEWORESP,</span>// char properties define by BLUEIOSVC_CHAR_PROP_...</div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span>s_TxCharDescString,<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// char UTF-8 description string</div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span>UartTxSrvcCallback,<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// Callback for write char, set to NULL for read char</div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: black; text-decoration: underline;">NULL</span><span style="color: black;">,<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// Callback on set notification</div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: black; text-decoration: underline;">NULL</span><span style="color: black;">,<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// <span style="text-decoration: underline;">Tx</span> completed callback</div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: black; text-decoration: underline;">NULL</span><span style="color: black;">,<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// pointer to char default values</div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span>0<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// Default value length in bytes</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
},</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
};</div>
<br />
<div style="color: #4f76cb; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
/// Service definition</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #931a68;">const</span> <span style="color: #006141;">BLESRVC_CFG</span> s_UartSrvcCfg = {</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #0326cc;">BLESRVC_SECTYPE_NONE</span><span style="color: black;">,<span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #4e9072;">// Secure or Open service/char</span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
BLUEIO_UUID_BASE, <span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #4e9072;">// Base UUID</span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
BLUEIO_UUID_UART_SERVICE,<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #4e9072;">// Service UUID</span></div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> 2, <span class="Apple-tab-span" style="white-space: pre;"> </span></span>// Total number of characteristics for the service</div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> g_UartChars,<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// Pointer a an array of characteristic</div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> g_LWrBuffer,<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// pointer to user long write buffer</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #931a68;">sizeof</span><span style="color: black;">(g_LWrBuffer)<span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #4e9072;">// long write buffer size</span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
};</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 15px;">
<br /></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #006141;">BLESRVC</span> g_UartBleSrvc;</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 15px;">
<br /></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #931a68;">const</span> <span style="color: #006141;">BLEAPP_DEVDESC</span> s_UartBleDevDesc {</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
MODEL_NAME, <span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #4e9072;">// Model name</span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
MANUFACTURER_NAME,<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #4e9072;">// Manufacturer name</span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #3933ff;">""</span><span style="color: black;">,<span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #4e9072;">// Serial number string</span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #3933ff;">"0.0"</span><span style="color: black;">,<span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #4e9072;">// Firmware version string</span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #3933ff;">"0.0"</span><span style="color: black;">,<span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #4e9072;">// Hardware version string</span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
};</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 15px;">
<br /></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #931a68;">const</span> <span style="color: #006141;">BLEAPP_CFG</span> s_BleAppCfg = {</div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> { </span>// Clock <span style="text-decoration: underline;">config</span> nrf_clock_lf_cfg_t</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #931a68;">#ifdef</span> IMM_NRF51822</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="white-space: pre;"> </span>NRF_CLOCK_LF_SRC_RC,<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #4e9072;">// Source RC</span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>1, 1, 0</div>
<div style="color: #931a68; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
#else</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>NRF_CLOCK_LF_SRC_XTAL,<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #4e9072;">// Source 32KHz XTAL</span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>0, 0, NRF_CLOCK_LF_ACCURACY_20_PPM</div>
<div style="color: #931a68; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
#endif</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 15px;">
},</div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> 0, <span class="Apple-tab-span" style="white-space: pre;"> </span></span>// Number of central link</div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> 1, <span class="Apple-tab-span" style="white-space: pre;"> </span></span>// Number of peripheral link</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #0326cc;">BLEAPP_MODE_APPSCHED<span style="color: black;">,<span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #4e9072;">// Use scheduler</span></span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
DEVICE_NAME,<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #4e9072;">// Device name</span></div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> ISYST_BLUETOOTH_ID,<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// PnP <span style="text-decoration: underline;">Bluetooth</span>/USB vendor id</div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> 1,<span class="Apple-tab-span" style="white-space: pre;"> </span>/</span>/ PnP Product ID</div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> 0,<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// <span style="text-decoration: underline;">Pnp</span> <span style="text-decoration: underline;">prod</span> version</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #931a68;">true</span><span style="color: black;">,<span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #4e9072;">// Enable device information service (DIS)</span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
&s_UartBleDevDesc,</div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> g_ManData,<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// Manufacture specific data to advertise</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #931a68;">sizeof</span><span style="color: black;">(g_ManData),<span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #4e9072;">// Length of manufacture specific data</span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #0326cc;">BLEAPP_SECTYPE_STATICKEY_MITM</span><span style="color: black;">,</span><span style="color: #4e9072;">//BLEAPP_SECTYPE_NONE, // Secure connection type</span></div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> BLEAPP_SECEXCHG_NONE,<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// Security key exchange</div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> </span><span style="color: black; text-decoration: underline;">NULL</span><span style="color: black;">, <span class="Apple-tab-span" style="white-space: pre;"> </span></span>// Service <span style="text-decoration: underline;">uuids</span> to advertise</div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> 0, <span class="Apple-tab-span" style="white-space: pre;"> </span></span>// Total number of <span style="text-decoration: underline;">uuids</span></div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> APP_ADV_INTERVAL,<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// Advertising interval in <span style="text-decoration: underline;">msec</span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
APP_ADV_TIMEOUT_IN_SECONDS,<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #4e9072;">// Advertising timeout in </span><span style="color: #4e9072; text-decoration: underline;">sec</span></div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> 0,<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// Slow advertising interval, if > 0, <span style="text-decoration: underline;">fallback</span> to</div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span>// slow interval on <span style="text-decoration: underline;">adv</span> timeout and advertise until connected</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
MIN_CONN_INTERVAL,</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
MAX_CONN_INTERVAL,</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
BLUEIO_CONNECT_LED_PORT,<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #4e9072;">// Led port </span><span style="color: #4e9072; text-decoration: underline;">nuber</span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
BLUEIO_CONNECT_LED_PIN,<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #4e9072;">// Led pin number</span></div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> 0,<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// <span style="text-decoration: underline;">Tx</span> power</div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> </span><span style="color: black; text-decoration: underline;">NULL</span><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span>// RTOS <span style="text-decoration: underline;">Softdevice</span> handler</div>
<br />
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
};</div>
<div>
<br /></div>
<br />
In order to handle the UART receive and send it to BLE, we need to hookup the UART event handler.<br />
<br />
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #931a68;">void</span> UartRxChedHandler(<span style="color: #931a68;">void</span> * p_event_data, <span style="color: #006141;">uint16_t</span> event_size)</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
{</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #006141;">uint8_t</span> buff[128];</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 15px;">
<br /></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #931a68;">int</span> l = g_Uart.<span style="text-decoration: underline;">Rx</span>(buff, 128);</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #931a68;">if</span> (l > 0)</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
{</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="text-decoration: underline;">BleSrvcCharNotify</span>(&g_UartBleSrvc, 0, buff, l);</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
}</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
}</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 15px;">
<br /></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #931a68;">int</span> nRFUartEvthandler(<span style="color: #006141;">UARTDEV</span> *pDev, <span style="color: #006141;">UART_EVT</span> EvtId, <span style="color: #006141;">uint8_t</span> *pBuffer, <span style="color: #931a68;">int</span> BufferLen)</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
{</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #931a68;">int</span> cnt = 0;</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #006141;">uint8_t</span> buff[20];</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 15px;">
<span style="color: #931a68;"><br /></span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 15px;">
<span style="color: #931a68;">switch</span> (EvtId)</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
{</div>
<div style="color: #0326cc; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #931a68;">case</span><span style="color: black;"> </span>UART_EVT_RXTIMEOUT<span style="color: black;">:</span></div>
<div style="color: #0326cc; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #931a68;">case</span><span style="color: black;"> </span>UART_EVT_RXDATA<span style="color: black;">:</span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> <span style="text-decoration: underline;">app_sched_event_put</span>(<span style="text-decoration: underline;">NULL</span>, 0, UartRxChedHandler);</div>
<div style="color: #931a68; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span> </span>break<span style="color: black;">;</span></div>
<div style="color: #0326cc; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #931a68;">case</span><span style="color: black;"> </span>UART_EVT_TXREADY<span style="color: black;">:</span></div>
<div style="color: #931a68; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span> </span>break<span style="color: black;">;</span></div>
<div style="color: #0326cc; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #931a68;">case</span><span style="color: black;"> </span>UART_EVT_LINESTATE<span style="color: black;">:</span></div>
<div style="color: #931a68; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span> </span>break<span style="color: black;">;</span></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
}</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 15px;">
<br /></div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #931a68;">return</span> cnt;</div>
<br />
<br />
<div>
</div>
<br />
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: monaco; font-size: 11px; font-stretch: normal; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;">
<div style="margin: 0px;">
}</div>
</div>
<br />
Handling received date from BLE and transmit to UART Tx.<br />
<br />
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #931a68;">void</span> UartTxSrvcCallback(<span style="color: #006141;">BLESRVC</span> *pBlueIOSvc, <span style="color: #006141;">uint8_t</span> *pData, <span style="color: #931a68;">int</span> Offset, <span style="color: #931a68;">int</span> Len)</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
{</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
g_Uart.<span style="text-decoration: underline;">Tx</span>(pData, Len);</div>
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
}</div>
<br />
<div style="font-family: Monaco; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 15px;">
<br /></div>
Those the main parts of this example. Full <a href="https://github.com/IOsonata/IOsonata/blob/master/ARM/Nordic/exemples/UartBleDemo.cpp">source code is located here</a> as part of the <a href="https://github.com/IOsonata/IOsonata">IOsonata library</a>.<br />
<br />
<a href="https://embeddedsoftdev.blogspot.ca/2017/12/bluetooth-le-with-nordic-nrf51-nrf52.html">Part 1</a> <a href="https://embeddedsoftdev.blogspot.ca/2018/01/bluetooth-le-with-nordic-nrf51-nrf52.html">Part 2</a><br />
<br />Hoanhttp://www.blogger.com/profile/13256219944725692395noreply@blogger.com0tag:blogger.com,1999:blog-5209857081856157335.post-29907323771220869102018-01-02T20:38:00.001-05:002020-08-14T21:29:34.292-04:00<h2>
<div style="text-align: center;">
<span style="font-size: x-large;">Bluetooth LE with Nordic nRF51 & nRF52 series<br />
The easy way!<br />
Part 2<br />
</span></div>
</h2>
<br />
<br />
<h3>
<div style="text-align: center;">
<h3>
Advertising environmental sensor data<br />
(Temperature, Humidity, Pressure)<br />
with Bosch BME280 & BME680 </h3>
</div>
</h3>
<br />
In previous post, we advertise over Bluetooth only an incremental counter. Lets do something more interesting this time by advertising environmental sensor data, temperature, pressure and humidity. Most digital sensors can be communicated using either I2C or SPI interface. EHAL provides a simple way to us those interface. Similar to the BLE interface, the I2C & SPI also make use to configuration data structure and an Init function.<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<iframe allowfullscreen="" class="YOUTUBE-iframe-video" data-thumbnail-src="https://i.ytimg.com/vi/Hl9jAOQTsLI/0.jpg" frameborder="0" height="266" src="https://www.youtube.com/embed/Hl9jAOQTsLI?feature=player_embedded" width="320"></iframe></div><div class="separator" style="clear: both; text-align: center;">avail at <a href="https://www.crowdsupply.com/i-syst/blyst-nano">https://www.crowdsupply.com/i-syst/blyst-nano</a></div>
<br />
<h3>
The hardware </h3>
<br />
The environmental sensors used in this example are the BME280 and BME680 from Bosch.<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://1.bp.blogspot.com/-rAv9tTM-rNY/XgZ8b52uTqI/AAAAAAAAAiA/1AcnV6J4gzYX5dI_liSt2Ich4epdj8pwgCLcBGAsYHQ/s1600/BLUEIO-TAG-EVIM_Features_Top.png" style="margin-left: auto; margin-right: auto;"><img border="0" data-original-height="655" data-original-width="1021" height="256" src="https://1.bp.blogspot.com/-rAv9tTM-rNY/XgZ8b52uTqI/AAAAAAAAAiA/1AcnV6J4gzYX5dI_liSt2Ich4epdj8pwgCLcBGAsYHQ/s400/BLUEIO-TAG-EVIM_Features_Top.png" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Bluetooth 5 Sensor Board<br />
avail at <a href="https://www.crowdsupply.com/i-syst/blyst-nano">https://www.crowdsupply.com/i-syst/blyst-nano</a></td></tr>
</tbody></table>
<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://2.bp.blogspot.com/-39O0FaGihHU/WkuDJjv_8mI/AAAAAAAAAZg/hqevIb8frhI0v6Gw7TCLwwYvPQzDsHJWACLcBGAs/s1600/IBK-BME680_PinOut_640.png" style="margin-left: auto; margin-right: auto;"><img border="0" data-original-height="495" data-original-width="640" height="305" src="https://2.bp.blogspot.com/-39O0FaGihHU/WkuDJjv_8mI/AAAAAAAAAZg/hqevIb8frhI0v6Gw7TCLwwYvPQzDsHJWACLcBGAs/s400/IBK-BME680_PinOut_640.png" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">The <a href="https://www.tindie.com/products/hnhoan/bme680-temperature-pressure-humidity-gas-sensor/">BME680 breakout board available on Tindie</a><br />
this board support both I2C & SPI interface.</td></tr>
</tbody></table>
<br />
<br />
<h3>
Configuring I2C for the <a href="https://www.tindie.com/products/hnhoan/bme680-temperature-pressure-humidity-gas-sensor/">BME680 breakout board</a></h3>
<br />
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
// Configure I2C interface</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #931a68;">static</span> <span style="color: #931a68;">const</span> <span style="color: #006141;">I2CCFG</span> s_I2cCfg = {</div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span style="white-space: pre;"> </span>0,<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// I2C device number</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>{<br />
<span style="color: #4e9072;"> // I2C pins in which the device is connected to.</span></div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 15px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>{<span style="text-decoration: underline;">I2C0_SDA_PORT</span>, <span style="text-decoration: underline;">I2C0_SDA_PIN</span>, <span style="text-decoration: underline;">I2C0_SDA_PINOP</span>, <span style="color: #0326cc;">IOPINDIR_BI</span>, <span style="color: #0326cc;">IOPINRES_NONE</span>, <span style="color: #0326cc;">IOPINTYPE_NORMAL</span>},</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>{<span style="text-decoration: underline;">I2C0_SCL_PORT</span>, <span style="text-decoration: underline;">I2C0_SCL_PIN</span>, <span style="text-decoration: underline;">I2C0_SCL_PINOP</span>, <span style="color: #0326cc;">IOPINDIR_OUTPUT</span>, <span style="color: #0326cc;">IOPINRES_NONE</span>, <span style="color: #0326cc;">IOPINTYPE_NORMAL</span>},</div>
<div style="color: #931a68; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>},</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>100000,<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #4e9072;">// Rate in Hz</span></div>
<div style="color: #0326cc; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span>I2CMODE_MASTER<span style="color: black;">,</span></div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span>0,<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// Slave address</div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span>5,<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// Retry</div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span>7,<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// Interrupt <span style="text-decoration: underline;">prio</span></div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span>NULL<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// Event callback</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
};</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 15px;">
<br /></div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
// I2C interface instance</div>
<br />
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #006141; text-decoration: underline;">I2C</span> g_I2c;</div>
<div>
<br /></div>
Initialize the I2C interface.<br />
<br />
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
g_I2c.Init(s_I2cCfg);</div>
<div>
<br /></div>
The I2C is now ready to use. Now lets try to use it to read the Bosch BME680 device ID directly.<br />
<br />
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> </span><span style="color: #006141;">uint8_t</span><span style="color: black;"> regaddr = 0xD0;<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// BME680 device ID register address</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #006141;">uint8_t</span> d;</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 15px;">
<br /></div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> g_I2c.</span><span style="color: black; text-decoration: underline;">Read</span><span style="color: black;">(0x76, </span>// BME680 I2C Device address</div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> <span class="Apple-tab-span" style="white-space: pre;"> </span> &regaddr, 1,<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// register address to read</div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span> &d, 1<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// buffer to return data</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> );</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<br /></div>
<span face="" style="color: black; font-family: monaco; font-size: 11px;"> </span><span face="" style="color: black; font-family: monaco; font-size: 11px; text-decoration: underline;">printf</span><span face="" style="color: black; font-family: monaco; font-size: 11px;">(</span><span face="" style="color: #3933ff; font-family: monaco; font-size: 11px;">"BME680 device id = 0x%02x\r\n"</span><span face="" style="color: black; font-family: monaco; font-size: 11px;">, d);</span><br />
<div>
<span style="color: black;"><br />
</span></div>
<div>
If the device is connected correctly, the variable d should contains the value 0x61 (97 decimal). </div>
<div>
<br />
<h3>
Configuring SPI</h3>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
// <span style="text-decoration: underline;">Motsai</span> <span style="text-decoration: underline;">Neblina</span> V2 module uses SPI interface</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<br /></div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #931a68;">static</span> <span style="color: #931a68;">const</span> <span style="color: #006141;">IOPINCFG</span> gsSpiBoschPin[] = {</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
{SPI_SCK_PORT, SPI_SCK_PIN, SPI_SCK_PINOP,</div>
<div style="color: #0326cc; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> </span>IOPINDIR_OUTPUT<span style="color: black;">, </span>IOPINRES_NONE<span style="color: black;">, </span>IOPINTYPE_NORMAL<span style="color: black;">},</span></div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
{SPI_MISO_PORT, SPI_MISO_PIN, SPI_MISO_PINOP,</div>
<div style="color: #0326cc; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> </span>IOPINDIR_INPUT<span style="color: black;">, </span>IOPINRES_NONE<span style="color: black;">, </span>IOPINTYPE_NORMAL<span style="color: black;">},</span></div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
{SPI_MOSI_PORT, SPI_MOSI_PIN, SPI_MOSI_PINOP,</div>
<div style="color: #0326cc; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> </span>IOPINDIR_OUTPUT<span style="color: black;">, </span>IOPINRES_NONE<span style="color: black;">, </span>IOPINTYPE_NORMAL<span style="color: black;">},</span></div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
{<span style="text-decoration: underline;">SPI_BME280_CS_PORT</span>, <span style="text-decoration: underline;">SPI_BME280_CS_PIN</span>, <span style="text-decoration: underline;">SPI_BME280_CS_PINOP</span>,</div>
<div style="color: #0326cc; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> </span>IOPINDIR_OUTPUT<span style="color: black;">, </span>IOPINRES_PULLUP<span style="color: black;">, </span>IOPINTYPE_NORMAL<span style="color: black;">},</span></div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
};</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 15px;">
<br /></div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #931a68;">static</span> <span style="color: #931a68;">const</span> <span style="color: #006141;">SPICFG</span> s_SpiCfg = {</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
SPI_DEVNO,</div>
<div style="color: #0326cc; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> </span>SPIMODE_MASTER<span style="color: black;">,</span></div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
gsSpiBoschPin,</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #931a68;">sizeof</span>( gsSpiBoschPin ) / <span style="color: #931a68;">sizeof</span>( <span style="color: #006141;">IOPINCFG</span> ),</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
8000000, <span style="color: #4e9072;">// Speed in Hz</span></div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
8, <span style="color: #4e9072;">// Data Size</span></div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> 5, </span>// Max retries</div>
<div style="color: #0326cc; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> </span>SPIDATABIT_MSB<span style="color: black;">,</span></div>
<div style="color: #0326cc; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> </span>SPIDATAPHASE_SECOND_CLK<span style="color: black;">, </span><span style="color: #4e9072;">// Data phase</span></div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> </span><span style="color: #0326cc;">SPICLKPOL_LOW</span><span style="color: black;">, </span>// clock polarity</div>
<div style="color: #0326cc; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> </span>SPICSEL_AUTO<span style="color: black;">,</span></div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> 6, </span>//APP_IRQ_PRIORITY_LOW, // Interrupt priority</div>
<div style="color: #931a68; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> </span>nullptr</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
};</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 15px;">
<br /></div>
<br />
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #006141; text-decoration: underline;">SPI</span> g_Spi;</div>
<div>
<br /></div>
Initialize the SPI interface<br />
<br />
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #4e9072;">// Initialize I2C</span></div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
g_Spi.Init(s_SpiCfg);</div>
<div>
<br /></div>
The SPI is now ready to use. Now lets try to use it to read the Bosch BME280 device ID directly.<br />
<br />
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> </span><span style="color: #006141;">uint8_t</span><span style="color: black;"> regaddr = 0xD0;<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// BME280 device ID register address</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #006141;">uint8_t</span> d;</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 15px;">
<br /></div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> g_Spi.</span><span style="color: black; text-decoration: underline;">Read</span><span style="color: black;">(0x76, </span>// BME280 I2C Device address</div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> <span class="Apple-tab-span" style="white-space: pre;"> </span> &regaddr, 1,<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// register address to read</div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span> &d, 1<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// buffer to return data</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> );</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<br /></div>
<br />
<div>
</div>
<br />
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: -webkit-standard; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; margin: 0px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;">
<span face="" style="color: black; font-family: monaco; font-size: 11px;"> </span><span face="" style="color: black; font-family: monaco; font-size: 11px; text-decoration: underline;">printf</span><span face="" style="color: black; font-family: monaco; font-size: 11px;">(</span><span face="" style="color: #3933ff; font-family: monaco; font-size: 11px;">"BME280 device id = 0x%02x\r\n"</span><span face="" style="color: black; font-family: monaco; font-size: 11px;">, d);</span></div>
<br />
If the device is connected correctly, the variable d should contains the value 0x60 (96 decimal).<br />
<br />
Luckily we don't need to manually reading data from the sensor that way. The EHAL already have driver written for both the BME280 & the BM680. Here is how to initialize the driver and read sensor data.<br />
<br />
<br /></div>
<div>
<h3>
Configuring sensor drivers </h3>
</div>
<div>
<br /></div>
<div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
// Configure environmental sensor<br />
<br />
// Configure BME680 for I2C</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #931a68;">static</span> <span style="color: #006141;">TPHSENSOR_CFG</span> s_Bme680SensorCfg = {</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="white-space: pre;"> </span>BME680_I2C_DEV_ADDR0, <span style="color: #4e9072;">// I2C device address</span></div>
<div style="color: #931a68; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #0326cc;">SENSOR_OPMODE_SINGLE</span><span style="color: black;">,</span></div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span>100,<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// Sampling frequency in Hz</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>1,</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>1,</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>1,</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>1</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
};</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 15px;">
<br /></div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
// BME680 Environmental sensor instance</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #006141;">TphgBme680</span> g_Bme680Sensor;<br />
<br />
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: -webkit-standard; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;">
</div>
<br />
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: -webkit-standard; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;">
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<div style="margin: 0px;">
<span style="color: #931a68;"><br />
</span></div>
<div style="margin: 0px;">
<span style="color: #931a68;"><span style="color: #4e9072;">// Configure BME280 for SPI</span></span></div>
<div style="margin: 0px;">
<span style="color: #931a68;">static</span> <span style="color: #006141;">TPHSENSOR_CFG</span> s_Bme280SensorCfg = {</div>
</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<div style="margin: 0px;">
<span style="white-space: pre;"> 0</span>, <span style="color: #4e9072;">// SPI chip select index</span></div>
</div>
<div style="color: #931a68; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<div style="margin: 0px;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #0326cc;">SENSOR_OPMODE_SINGLE</span><span style="color: black;">,</span></div>
</div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<div style="margin: 0px;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span>100,<span class="Apple-tab-span" style="white-space: pre;"> </span></span>// Sampling frequency in Hz</div>
</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<div style="margin: 0px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>1,</div>
</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<div style="margin: 0px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>1,</div>
</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<div style="margin: 0px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>1,</div>
</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<div style="margin: 0px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>1</div>
</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<div style="margin: 0px;">
};</div>
</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 15px;">
<div style="margin: 0px;">
<br /></div>
</div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<div style="margin: 0px;">
// BME280 Environmental sensor instance</div>
</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<div style="margin: 0px;">
<span style="color: #006141;">TphBme280</span> g_Bme280Sensor;</div>
</div>
</div>
</div>
</div>
<div>
<br /></div>
<div>
Initialization and reading sensor data</div>
<div>
<br /></div>
<div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
// Intitialize BM680 using I2C interface</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
g_Bme680Sensor.<span style="text-decoration: underline;">Init</span>(s_Bme680SensorCfg, &g_I2c, NULL);</div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> </span><br />
<div style="color: black; font-stretch: normal; line-height: normal;">
<div style="color: #4e9072; font-stretch: normal; line-height: normal;">
<div style="font-stretch: normal; line-height: normal;">
// or<br />
// intitialize BM680 using SPI interface</div>
<div style="color: black; font-stretch: normal; line-height: normal;">
g_Bme680Sensor.<span style="text-decoration: underline;">Init</span>(s_Bme680SensorCfg, &g_Spi, NULL);</div>
<div style="font-stretch: normal; line-height: normal;">
</div>
<br />
// Intitialize BM280 using SPI interface</div>
<div style="font-stretch: normal; line-height: normal;">
</div>
g_Bme280Sensor.<span style="text-decoration: underline;">Init</span>(s_Bme280SensorCfg, &g_Spi, NULL);</div>
<div style="font-stretch: normal; line-height: normal;">
</div>
<br /></div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> </span>// Update sensor data</div>
<div style="color: #006141; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> </span>TPHSENSOR_DATA<span style="color: black;"> tphdata;</span></div>
<div style="color: #006141; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<br /></div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
g_Bme680Sensor.<span face="" style="font-family: monaco; text-decoration: underline;">StartSampling</span><span face="" style="font-family: monaco;">();</span><br />
g_Bme680Sensor.<span style="text-decoration: underline;">Read</span>(tphdata);</div>
</div>
<div>
<br />
<span face="" style="font-family: monaco; font-size: 11px;"> </span><span face="" style="font-family: monaco; font-size: 11px;">g_Bme280Sensor</span><span face="" style="font-family: monaco; font-size: 11px;">.</span><span face="" style="font-family: monaco; font-size: 11px; text-decoration: underline;">StartSampling</span><span face="" style="font-family: monaco; font-size: 11px;">();</span><br />
<span face="" style="font-family: monaco; font-size: 11px;"> g_Bme280Sensor.</span><span face="" style="font-family: monaco; font-size: 11px; text-decoration: underline;">Read</span><span face="" style="font-family: monaco; font-size: 11px;">(tphdata);</span></div>
<div>
<br />
<br />
<h3>
Integrating into BLE advertising data</h3>
In the previous article, we advertising only a 32bits count. Let replace the configuration now to advertise the sensor data instead<br />
<br />
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="text-decoration: underline;">BLEADV_MANDATA</span> g_AdvData</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<br /></div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #931a68;">const</span> <span style="color: #006141;">BLEAPP_CFG</span> s_BleAppCfg = {</div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> { </span>// Clock <span style="text-decoration: underline;">config</span> nrf_clock_lf_cfg_t</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #931a68;">#ifdef</span> IMM_NRF51822</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
NRF_CLOCK_LF_SRC_RC,<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #4e9072;">// Source RC</span></div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
1, 1, 0</div>
<div style="color: #931a68; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
#else</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
0, 0, NRF_CLOCK_LF_ACCURACY_20_PPM</div>
<div style="color: #931a68; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
#endif</div>
<div style="color: #931a68; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
},</div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> 0, <span class="Apple-tab-span" style="white-space: pre;"> </span></span>// Number of central link</div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> 0, <span class="Apple-tab-span" style="white-space: pre;"> </span></span>// Number of peripheral link</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #0326cc;">BLEAPP_MODE_NOCONNECT</span><span style="color: black;">, </span><span style="color: #4e9072;">// </span><span style="color: #4e9072; text-decoration: underline;">Connectionless</span><span style="color: #4e9072;"> beacon type</span></div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
DEVICE_NAME, <span style="color: #4e9072;">// Device name</span></div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> ISYST_BLUETOOTH_ID, </span>// PnP <span style="text-decoration: underline;">Bluetooth</span>/USB vendor id</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
1, <span style="color: #4e9072;">// PnP Product ID</span></div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> 0,<span class="Apple-tab-span" style="white-space: pre;"> </span> </span>// <span style="text-decoration: underline;">Pnp</span> <span style="text-decoration: underline;">prod</span> version</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #931a68;">false</span><span style="color: black;">, </span><span style="color: #4e9072;">// Enable device information service (DIS)</span></div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> NULL, </span>// Pointer device info descriptor</div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> <b>(</b></span><b><span style="color: #006141;">uint8_t</span><span style="color: black;">*)&</span><span style="color: black;"> g_AdvData</span></b><span style="color: black;">, </span>// Manufacture specific data to advertise</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<b><span style="color: #931a68;">sizeof</span><span style="color: black;">(</span>g_AdvData</b><span style="color: black;"><b>)</b>, </span><span style="color: #4e9072;">// Length of manufacture specific data</span></div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #0326cc;">BLEAPP_SECTYPE_NONE</span><span style="color: black;">, </span><span style="color: #4e9072;">// Secure connection type</span></div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
BLEAPP_SECEXCHG_NONE, <span style="color: #4e9072;">// Security key exchange</span></div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> NULL, <span class="Apple-tab-span" style="white-space: pre;"> </span> </span>// Service <span style="text-decoration: underline;">uuids</span> to advertise</div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> 0, <span class="Apple-tab-span" style="white-space: pre;"> </span> </span>// Total number of <span style="text-decoration: underline;">uuids</span></div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> APP_ADV_INTERVAL, </span>// Advertising interval in <span style="text-decoration: underline;">msec</span></div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
APP_ADV_TIMEOUT_IN_SECONDS,<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #4e9072;">// Advertising timeout in </span><span style="color: #4e9072; text-decoration: underline;">sec</span></div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> 100, </span>// Slow advertising interval, if > 0, <span style="text-decoration: underline;">fallback</span> to</div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span> </span>// slow interval on <span style="text-decoration: underline;">adv</span> timeout and advertise until connected</div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> 0,<span class="Apple-tab-span" style="white-space: pre;"> </span> </span>// <span style="text-decoration: underline;">Min</span>. connection interval</div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> 0,<span class="Apple-tab-span" style="white-space: pre;"> </span> </span>// Max. connection interval</div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> -1, </span>// Led port <span style="text-decoration: underline;">nuber</span></div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> -1, </span>// Led pin number</div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> 0, <span class="Apple-tab-span" style="white-space: pre;"> </span> </span>// <span style="text-decoration: underline;">Tx</span> power</div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> NULL </span>// RTOS <span style="text-decoration: underline;">Softdevice</span> handler</div>
<br />
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
};</div>
<div>
</div>
<br />
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #931a68;">void</span> ReadPTHData()</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
{</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #006141;">TPHSENSOR_DATA</span> data;</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 15px;">
<br /></div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
g_TphSensor.<span style="text-decoration: underline;">Read</span>(data);</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
g_TphSensor.<span style="text-decoration: underline;">StartSampling</span>();</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 15px;">
<br /></div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
g_AdvData.<span style="text-decoration: underline;">Type</span> = <span style="text-decoration: underline;">BLEADV_MANDATA_TYPE_TPH</span>;</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 15px;">
<br /></div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #4e9072;">// NOTE : M0 does not access unaligned data</span></div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #4e9072;">// use local 4 bytes align stack variable then <span style="text-decoration: underline;">mem</span> copy</span></div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #4e9072;">// skip <span style="text-decoration: underline;">timestamp</span> as advertising pack is limited in size</span></div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="text-decoration: underline;">memcpy</span>(g_AdvData.Data, ((<span style="color: #006141;">uint8_t</span>*)&data) + 4, <span style="color: #931a68;">sizeof</span>(<span style="text-decoration: underline;">BLEADV_MANDATA_TPHSENSOR</span>));</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 15px;">
<br /></div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #4e9072;">// Update advertisement data</span></div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="text-decoration: underline;">BleAppAdvManDataSet</span>(g_AdvDataBuff, <span style="color: #931a68;">sizeof</span>(g_AdvDataBuff));</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 15px;">
}</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal; min-height: 15px;">
<br /></div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #931a68;">void</span> BlePeriphEvtUserHandler(<span style="color: #006141;">ble_evt_t</span> * p_ble_evt)</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
{</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: #931a68;">if</span> (p_ble_evt-><span style="color: #0326cc;">header</span>.<span style="color: #0326cc;">evt_id</span> == <span style="color: #0326cc;">BLE_GAP_EVT_TIMEOUT</span>)</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
{</div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> <span class="Apple-tab-span" style="white-space: pre;"> </span></span>// Update environmental sensor data every time advertisement timeout</div>
<div style="color: #4e9072; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="color: black;"> <span class="Apple-tab-span" style="white-space: pre;"> </span></span>// for re-advertisement</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
<span style="white-space: pre;"> </span>ReadPTHData();</div>
<div style="font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
}</div>
<div style="color: #931a68; font-family: monaco; font-size: 11px; font-stretch: normal; line-height: normal;">
}</div>
<div>
<br /></div>
Source code at :<a href="https://github.com/IOsonata/IOsonata/blob/master/ARM/Nordic/exemples/TPHSensorTag.cpp">https://github.com/IOsonata/IOsonata/blob/master/ARM/Nordic/exemples/TPHSensorTag.cpp</a></div>
<div>
Complete Eclipse based project is on github: <a href="https://github.com/IOsonata/IOsonata/tree/master/ARM/Nordic/nRF52/nRF52832/lib/Eclipse">nRF52 Project</a>, <a href="https://github.com/IOsonata/IOsonata/tree/master/ARM/Nordic/nRF51/exemples/TPHSensorTag/Eclipse">nRF51 Project</a>. Same code works for both <a href="https://github.com/IOsonata/IOsonata/tree/master/ARM/Nordic/exemples">nRF51 & nRF52 series including nRF52840</a>. </div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<span style="color: black;"><a href="https://embeddedsoftdev.blogspot.ca/2017/12/bluetooth-le-with-nordic-nrf51-nrf52.html">Part 1</a> <a href="https://embeddedsoftdev.blogspot.ca/2018/02/bluetooth-le-with-nordic-nrf51-nrf52.html">Part 3</a></span></div>
<div>
<span style="color: black;"><br />
</span></div>
<div>
<span style="color: black;"><br />
</span></div>
<div>
<span style="color: black;"><br />
</span></div>
<div>
<span style="color: black;"><br />
</span></div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />Hoanhttp://www.blogger.com/profile/13256219944725692395noreply@blogger.com1tag:blogger.com,1999:blog-5209857081856157335.post-23989328198643059862017-12-18T02:03:00.002-05:002020-07-04T18:05:05.003-04:00<h2 style="text-align: center;">
<span style="font-size: x-large;">Bluetooth LE with Nordic nRF51 & nRF52 Series</span></h2>
<h2 style="text-align: center;">
<span style="font-size: x-large;">The easy way! </span></h2>
<h3 style="text-align: center;">
<span style="font-size: large;">Part 1</span><span style="font-size: x-large;"> </span></h3>
<br />
Writing BLE firmware using the Nordic nRF51 & nRF52 SDK is not an easy task. The learning curve is quite steep for a beginner. The <a href="https://github.com/IOsonata/IOsonata">IOsonata</a> library encapsulates all that difficulties by providing a simple way to write BLE application firmware. <br />
<br />
<h3>
Prerequisite </h3>
<h4>
</h4>
<h4>
Software </h4>
<br />
The <a href="https://github.com/IOsonata/IOsonata">IOsonata</a> library development environment is based on Eclipse & GCC compiler. All projects are eclipse's native. They can be imported directly into eclipse. However the source are organized in a specific folder tree. Follow the blog posts to setup the environment, for <a href="http://embeddedsoftdev.blogspot.ca/p/eclipse.html">Eclipse setup</a>, for <a href="http://embeddedsoftdev.blogspot.ca/p/h.html">IOsonata setup</a>.<br />
<br />
<h4>
Hardware </h4>
<br />
Off course there are hardware needed to write Bluetooth firmware. Since this is about Nordic nRF5x series, any boards or module based on that chip can be used. An other tool required is a compatible debug JTag tool to be able to flash and debug the code. The <a href="http://embeddedsoftdev.blogspot.ca/p/idap-link.html">IDAP-Link</a> debug JTag supports flashing of the nRF5x series. The <a href="https://www.tindie.com/products/hnhoan/bluetooth-5-nrf52832-arm-m4f-dev-board-30-io/">IDK-BLYST-NANO</a> and the <a href="https://www.tindie.com/products/hnhoan/cmsis-dap-arm-debug-jtagswd-microsd-drag-and-drop/">IDAP-Link</a> would make a great BLE development kit.<br />
<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><br />
<iframe allowfullscreen="" frameborder="0" height="360" src="https://player.vimeo.com/video/290182585" width="640"></iframe></td></tr>
<tr><td style="text-align: center;"><br />
<a href="https://www.crowdsupply.com/i-syst/blyst-nano"><span style="font-size: small;"><b>nRF52832 finger tip sized module available at CrowSupply</b></span></a></td></tr>
</tbody></table>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><br />
<a href="https://2.bp.blogspot.com/-3Tg6mUDS1QY/WjdcIdj8HaI/AAAAAAAAAY4/0nXTjg5bGY4sIjGy-zYkeilsRkrG8O4zQCLcBGAs/s1600/IMG_0014.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img alt="" border="0" data-original-height="761" data-original-width="761" height="200" src="https://2.bp.blogspot.com/-3Tg6mUDS1QY/WjdcIdj8HaI/AAAAAAAAAY4/0nXTjg5bGY4sIjGy-zYkeilsRkrG8O4zQCLcBGAs/s200/IMG_0014.jpg" title="IMM-NRF52832" width="200" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;"><a href="https://www.tindie.com/products/hnhoan/nrf52832-ant-bluetooth-5-arm-cortex-m4f-30-io/">IMM-NRF52832</a></td></tr>
</tbody></table>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://1.bp.blogspot.com/-_Udzpmpl4x0/WjdcRKb39mI/AAAAAAAAAY8/ILUvdzZO6jQvXElTtyq78FfaSCafVFWLgCLcBGAs/s1600/IMG_0012.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" data-original-height="1505" data-original-width="1505" height="200" src="https://1.bp.blogspot.com/-_Udzpmpl4x0/WjdcRKb39mI/AAAAAAAAAY8/ILUvdzZO6jQvXElTtyq78FfaSCafVFWLgCLcBGAs/s200/IMG_0012.jpg" width="200" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;"><a href="https://www.tindie.com/products/hnhoan/cmsis-dap-arm-debug-jtagswd-microsd-drag-and-drop/">IDAP-Link</a></td></tr>
</tbody></table>
<br />
<br />
<h3>
Some basics</h3>
<br />
Lets start with some basic about Bluetooth LE devices. We are not attempting to go into the details of BLE specs here but merely some need to know in order to decide the right path to application. There are 2 main category of BLE devices. The Peripheral acts as BLE server. This is usually the sensors, the watch, the tracker, the locks, etc... The other type of devices are the Central. It is the one that connects to the Peripheral to get data from. The Central devices are mostly the computers, the smart phones and tablets. Further, Nordic provide the BLE stack for their nRF51 & nRF52 series SoC in the form of binary called Softdevice. This Softdevice support both Peripheral and Central mode. It has to be flashed with the firmware app in order to use BLE. <br />
<br />
<h3>
BLE Peripheral Firmware </h3>
<br />
The BLE Peripheral device always start by advertises its presence so the Central device can see and connect to it or not. The Peripheral device can be an advertiser only not allowing Central device to connect to it. This mode is often called connectionless or beacon. It is the simplest form if BLE Peripheral device. Here is how we can write the firmware of such device.<br />
<br />
<br />
<h4>
Device configuration </h4>
<br />
The IOsonata made thing simple and flexible to use any devices in an embedded system. Only two steps are required prior to use the device. First, declaring the device configuration by filling a data structure of that device. This data structure usually contains configuration for all operating mode of a BLE device. Lets see how to define the device configuration structure for a BLE advertiser peripheral device.<br />
<br />
<br />
<br />
<br />
<blockquote class="tr_bq">
<span style="font-family: "courier new" , "courier" , monospace;">#include "istddef.h" </span><br />
<span style="font-family: "courier new" , "courier" , monospace;">#include "ble_app.h" </span><br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">#define DEVICE_NAME "Advertiser"</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">#define APP_ADV_INTERVAL MSEC_TO_UNITS(100, UNIT_0_625_MS)</span><br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">const BLEAPP_CFG s_BleAppCfg = {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> { // Clock config nrf_clock_lf_cfg_t</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">#ifdef IMM_NRF51822</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> NRF_CLOCK_LF_SRC_RC, // Source RC</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> 1, 1, 0</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">#else</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> NRF_CLOCK_LF_SRC_XTAL, // Source 32KHz XTAL</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> 0, 0, NRF_CLOCK_LF_ACCURACY_20_PPM </span><br />
<span style="font-family: "courier new" , "courier" , monospace;">#endif</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> },</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> 0, // Number of central link</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> 0, // Number of peripheral link</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> BLEAPP_MODE_NOCONNECT, // Connectionless beacon type</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> DEVICE_NAME, // Device name</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> ISYST_BLUETOOTH_ID, // PnP Bluetooth/USB vendor id</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> 1, // PnP Product ID</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> 0,<span class="Apple-tab-span" style="white-space: pre;"> </span>// Pnp prod version</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> false,<span style="white-space: pre;"> </span>// Enable device information service (DIS)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> NULL,</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> NULL, // Manufacture specific data to advertise</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> 0, // Length of manufacture specific data</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> BLEAPP_SECTYPE_NONE, // Secure connection type</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> BLEAPP_SECEXCHG_NONE, // Security key exchange</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> NULL, <span class="Apple-tab-span" style="white-space: pre;"> </span>// Service uuids to advertise</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> 0, // Total number of uuids</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> APP_ADV_INTERVAL, // Advertising interval in msec</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> 0, // Advertising timeout in sec, 0 for never</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> 0, // Slow advertising interval, if > 0, fallback to</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> // slow interval on adv timeout and advertise until connected</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> 0,</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> 0,</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> -1, // Led port nuber</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> -1, // Led pin number</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> 0, // Tx power</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> NULL // RTOS Softdevice handler</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">};</span></blockquote>
<br />
<div>
</div>
<div>
<h4>
The code </h4>
<br />
Now that the type of BLE Peripheral device has been defined. Lets bring it to live with the main code. All that is needed is to call 2 functions.<br />
<br />
<br />
<blockquote class="tr_bq">
<span style="font-family: "courier new" , "courier" , monospace;">int main()<br />
{<br />
BleAppInit((const BLEAPP_CFG *)&s_BleAppCfg, true);<br />
BleAppRun();</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "courier new" , "courier" , monospace;"> return 0;<br />
}</span></blockquote>
<div>
<br /></div>
<br /></div>
That is all the code you need to write to create a BLE Advertiser firmware. Nordic NRF-Connect App can be used to see the advertisement.<br />
<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://1.bp.blogspot.com/-aYm6bbB7Fq4/Wjm55LFcjPI/AAAAAAAAAZM/NlvORLtxQt4hlWACY0wfKbYlTSRkRhzoACLcBGAs/s1600/IMG_3224.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" data-original-height="1334" data-original-width="750" height="400" src="https://1.bp.blogspot.com/-aYm6bbB7Fq4/Wjm55LFcjPI/AAAAAAAAAZM/NlvORLtxQt4hlWACY0wfKbYlTSRkRhzoACLcBGAs/s400/IMG_3224.png" width="223" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Use NRF-Connect App to view advertisement data</td></tr>
</tbody></table>
<br />
<br />
<br />
Now we can see our device shows up on the scan. That's about it. What is of interest is that in the advertisement packet there is a little private data that we can use to send data. Lets try to send a 32bit counter with the advertisement packet to make our Advertise more interesting. The counter will count every time the advertisement times out.<br />
<br />
In order to send the count we need to add it to the config so that the library would know. <br />
<br />
<br />
<blockquote class="tr_bq">
<span style="font-family: "courier new" , "courier" , monospace;"><b>#define APP_ADV_TIMEOUT 1 // Advertisement timeout in sec</b></span><br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;"><b>uint32_t g_AdvCounter = 0;</b></span><br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">const BLEAPP_CFG s_BleAppCfg = {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> { // Clock config nrf_clock_lf_cfg_t</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">#ifdef IMM_NRF51822</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> NRF_CLOCK_LF_SRC_RC, // Source RC</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> 1, 1, 0</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">#else</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> NRF_CLOCK_LF_SRC_XTAL, // Source 32KHz XTAL</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> 0, 0, NRF_CLOCK_LF_ACCURACY_20_PPM </span><br />
<span style="font-family: "courier new" , "courier" , monospace;">#endif</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> },</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> 0, // Number of central link</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> 0, // Number of peripheral link</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> BLEAPP_MODE_NOCONNECT, // Connectionless beacon type</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> DEVICE_NAME, // Device name</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> ISYST_BLUETOOTH_ID, // PnP Bluetooth/USB vendor id</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> 1, // PnP Product ID</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> 0,<span class="Apple-tab-span" style="white-space: pre;"> </span>// Pnp prod version</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> false,<span style="white-space: pre;"> </span>// Enable device information service (DIS)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> NULL,</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> <b>(uint8_t*)&g_AdvCounter</b>,// Manufacture specific data to advertise</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> <b>sizeof(g_AdvCounter)</b>, // Length of manufacture specific data</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> BLEAPP_SECTYPE_NONE, // Secure connection type</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> BLEAPP_SECEXCHG_NONE, // Security key exchange</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> NULL, <span class="Apple-tab-span" style="white-space: pre;"> </span>// Service uuids to advertise</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> 0, // Total number of uuids</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> APP_ADV_INTERVAL, // Advertising interval in msec</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> <b>APP_ADV_TIMEOUT</b>, // Advertising timeout in sec, 0 for never</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> 0, // Slow advertising interval, if > 0, fallback to</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> // slow interval on adv timeout and advertise until connected</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> 0,</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> 0,</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> -1, // Led port nuber</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> -1, // Led pin number</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> 0, // Tx power</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> NULL // RTOS Softdevice handler</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">};</span></blockquote>
<br />
<br />
In addition to the config, we need to catch the advertisement timeout to update the counter. This is accomplished by overloading the function<br />
<br />
<blockquote class="tr_bq">
<span style="font-family: "courier new" , "courier" , monospace;">void BlePeriphEvtUserHandler(ble_evt_t * p_ble_evt)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">{</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> if (p_ble_evt->header.evt_id == BLE_GAP_EVT_TIMEOUT)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> // Update counter and advertisement data</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> g_AdvCnt++;</span><br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;"> BleAppAdvManDataSet((uint8_t*)&g_AdvCnt, sizeof(g_AdvCnt));</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">}</span></blockquote>
<br />
Complete Eclipse based project is on github: <a href="https://github.com/I-SYST/EHAL/tree/master/ARM/Nordic/nRF52/exemples/BleAdvertiser">nRF52 Project</a>, <a href="https://github.com/I-SYST/EHAL/tree/master/ARM/Nordic/nRF51/exemples/BleAdvertiser">nRF51 Project</a>. Same code works for both <a href="https://github.com/I-SYST/EHAL/tree/master/ARM/Nordic/exemples">nRF51 & nRF52 series including nRF52840</a>. <br />
<br />
<br />
<a href="https://embeddedsoftdev.blogspot.ca/2018/01/bluetooth-le-with-nordic-nrf51-nrf52.html">Part 2</a> <a href="https://embeddedsoftdev.blogspot.ca/2018/02/bluetooth-le-with-nordic-nrf51-nrf52.html">Part 3</a> <br />
<br />
<br />
<br />
<br />
<br />
<br />Hoanhttp://www.blogger.com/profile/13256219944725692395noreply@blogger.com3tag:blogger.com,1999:blog-5209857081856157335.post-32939175696220530142014-04-16T20:56:00.001-04:002015-07-11T09:11:33.123-04:00WelcomeWelcome to my blog pages. This blog attempts to provide technical informations related to embedded software development. <br />
<br />
<b>Eclipse</b> page contains information and links to setup Eclipse based C/C++ embedded development environment for OSX, Linux & Windows<br />
<br />
<b>IDAP-Link</b> page is all about the CMSIS-DAP debug JTag and gang programming.<br />
<br />
<b>Arduino</b> page contains projects for Arduino. Currently LED matrix multi-display project.<br />
<br />
<b>EHAL</b> page is my Open Source Embedded Hardware Abstraction Library (EHAL). This is an attempts to provide multi-platform embedded software development with generic code re-use in C++.<br />
<br />
<b>nRF51/nRF52</b> page is all about Nordic nRF51 and nRF52 series Bluetooth LE & ANT development<br />
<br />
<b>LPC17xx/LPC11xx</b> page is about NXP LPC series ARM micro-controller<br />
<br />
<b>CC3200</b> page is about the TI ARM Cortex-M4 with embedded Wifi<br />
<br />
<br />
<br />
<br />Hoanhttp://www.blogger.com/profile/13256219944725692395noreply@blogger.com0