Part Number: CC2640R2F
Hello All,
I am very new to C and embedded C programming. I am trying to learn Bluetooth communication. From these resources:
At the same time, I am also going through some examples to understand how those things are implemented. That brings me to this project repository:
https://github.com/ti-simplelink/ble_examples
Here I am trying to understand the “heart_rate” examples. Right now I am only interested to figure out how it is sending the measured simulated data to Bluetooth.
So, from the main.c file, I can see that from this function:
HeartRate_createTask();
We are running all the heartrate-related measurements. So, I opened up that function and it seemed to me all the notifications is sent from this function:
HeartRate_measNotify(void)
I am very new to C and embedded C programming so I tried to understand that section and tried to write comments for each line. I am not completely sure whether I have done a good job or not. But I would greatly appreciate it if you can read through my comments.
static void HeartRate_measNotify(void)
{
attHandleValueNoti_t heartRateMeas; // its a stracture type with three variables and here I am declaring a variable (heartRateMeas) of that type
heartRateMeas.pValue = GATT_bm_alloc(gapConnHandle, ATT_HANDLE_VALUE_NOTI, //allocation the memory location for pValue
HEARTRATE_MEAS_LEN,
NULL);
if (heartRateMeas.pValue != NULL) // if the MCU was able to allocate 9 (HEARTRATE_MEAS_LEN) consecutive memory location then it will enter this "if" statement
{
uint8_t *p = heartRateMeas.pValue; // saves the address of the first location of the pointer variable "pvalue" in pointer "p".
// The first location address of the allocated memory
uint8_t flags = heartRateflags[flagsIdx]; // save the value of the first variable of the "heartRateflags" array in the "flag" variable
// Build heart rate measurement structure from simulated values.
*p++ = flags; // save the value of "flag" in the first location of the allocated memory and then increment "p"
//so that the next variable can be saved in the 2nd location of the allocated memory.
*p++ = heartRateBpm; // save the value of "heartRateBpm" in the second location of the allocated memory and then increment "p"
//so that the next variable can be saved in the 3rd location of the allocated memory.
// if (flags & HEARTRATE_FLAGS_FORMAT_UINT16)
// {
// // Additional byte for 16 bit format.
// *p++ = 0;
// }
// if (flags & HEARTRATE_FLAGS_ENERGY_EXP)
// {
// *p++ = LO_UINT16(heartRateEnergyLvl);
// *p++ = HI_UINT16(heartRateEnergyLvl);
// }
// if (flags & HEARTRATE_FLAGS_RR)
// {
*p++ = LO_UINT16(heartRateRrInterval); // same procedure as before
*p++ = HI_UINT16(heartRateRrInterval);
*p++ = LO_UINT16(heartRateRrInterval2);
*p++ = HI_UINT16(heartRateRrInterval2);
// }
heartRateMeas.len = (uint8) (p - heartRateMeas.pValue); // in same cases, all the "if" statement may not be executed so the whole allocated memory (HEARTRATE_MEAS_LEN),
//may not have been used. Thats why save the actual length of the allocated memory.
// Send notification.
if (HeartRate_MeasNotify(gapConnHandle, &heartRateMeas) != SUCCESS) // it takes the ........ and the address of the "heartRateMeas" ?????
{
GATT_bm_free((gattMsg_t*) &heartRateMeas, ATT_HANDLE_VALUE_NOTI); // free ups the allocated memory
}
// Update simulated values.
// heartRateEnergyLvl += HEARTRATE_ENERGY_INCREMENT;
// if (++heartRateBpm == HEARTRATE_BPM_MAX)
// {
// heartRateBpm = HEARTRATE_BPM_DEFAULT;
// }
// heartRateBpm =55;
heartRateRrInterval = heartRateRrInterval2 = HEARTRATE_BPM2RR( //Why??
heartRateBpm);
}
}
I want to send multiple sensor data. Please correct me if I wrong, if I have understood it correctly then I just need to use this method to pass all the data to Bluetooth as long as I don’t go over 512 bytes (dev.ti.com/.../ble_01_custom_profile.html):
*p++ = LO_UINT16(heartRateRrInterval); *p++ = HI_UINT16(heartRateRrInterval); *p++ = LO_UINT16(heartRateRrInterval2); *p++ = HI_UINT16(heartRateRrInterval2);
But I still need to verify that idea. And still not sure in which section of the Handler or UUID it would show up. The main issue is that I am unable to understand this section of the code:
if (HeartRate_MeasNotify(gapConnHandle, &heartRateMeas) != SUCCESS)
I can understand that we are passing the “heartRateMeas” structure address to this function but from there when I directly went to the actual function I got completely lost. We have passed the address of the structure as “pNoti”. Then in this line:
return GATT_Notification(connHandle, pNoti, FALSE);
We sent the notification. Presumably the heart rate data (BPM value). But if I open up the declaration of that function, it actually does not go anywhere, just jumps back and forth between different declarations.
(icall_directAPI(ICALL_SERVICE_CLASS_BLE, (uint32_t) IDX_GATT_Notification , ##__VA_ARGS__))
I would really appreciate it if someone can explain to me how are we actually sending the data to Bluetooth? And what is actually happing here in this function or declaration?
Also, in the “heartrateservice.c” file there is a section:
pNoti->handle = heartRateAttrTbl[HEARTRATE_MEAS_VALUE_POS].handle;
What is this section of the code doing? As the handler value is zero why are we passing that zero value to PNoti (pNoti->handle)?
That is a lot of questions for a single post . I would appreciate any help that I would get.
Thanks.








