This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC2541: How to configure Negative values in SimpleBLEPeripheral example code

Part Number: CC2541

Hello,

I am using CC2541 controller and exploring SimpleBLEPeripheral example code.

As part of simpleGATTprofile.c I mapped simpleProfileChar1 to "Upper Temperature Threshold". This value will be always a positive value.

simpleProfileChar2 I want to map to "Lower Temperature Threshold". This value will be always a negative value.

For this I changed variable from uint8 to int8.

static int8 simpleProfileChar2 = 0;

My queries are:

1)  But I am getting compile warning. Whether we can store 'Negative values'.

2) How to handle different formats in SimpleGATTprofile.c

https://www.bluetooth.com/specifications/assigned-numbers/format-types

Thanks & Regards

Vishnu Beema

  • Hi Vishnu,
    What is the compile error?
  • Hi,

    Its not error, but it's a warning. Below is the warning.

    Warning[Pe144]: a value of type "int8 *" cannot be used to initialize an entity of type "uint8 *" C:\Texas Instruments\BLE-CC254x-1.4.2.2\Projects\ble\Profiles\SimpleProfile\CC254x\simpleGATTprofile.c 249

    Irrespective of that, I am trying to understand, on how to work on Negative values and other formats.

    Thanks & Regards

    Vishnu Beema

  • Hi Vishnu,

    In the attribute table, what you are providing is a pointer to your data storage. This pointer must be cast as uint8_t because that's what is expected there by the stack.

    How you actually store the data and how you cast/parse the data in the application is irrelevant to the GATT Server. Essentially, what is sent over the air is considered as an array of unsigned char by the stack.

    The warning is just because you have a mismatch of types in your casting.

    Best regards,
    Aslak
  • Hi Aslak,

    Thank you for your inputs.
    If over the air is considered as an array of unsigned char then how to Read / Write Negative values.
    Please let me know your inputs with respect to CC2541 SimpleBLEPheripheral example code.
    In this code how to handle Negative variables.

    Thanks & Regards
    Vishnu Beema
  • Hi Vishnu,

    Sorry I'm not able to give you sample code that does what you want in the project you want.

    Are you aware of how memory works in C? Basically whatever variable you have, what type it is and what size it is, it is addressable as an array of bytes.

    Let's say there's a function that expects a "buffer", which in the API is normalized to uint8_t* or sometimes void*;
    void MyBufferEatingFunction(uint8_t *buf, uint16_t bufLen);

    Now it happens that what you have is an array of signed integers;
    int32_t myNegativeNumbers[5] = { -1, -2, -400, -33, -50 };

    You use this array as you normally use an array of this type. Everything is nice and everyone is happy.

    Now, someone has given you a transmit function that requires an array of consecutive bytes like above. Luckily that is just another way of looking at the array of negative numbers because everything after all is just byte-addressable memory.

    If you want to be explicit, you then declare a pointer that has this way of looking at your array;
    uint8_t *myCharBuf = (uint8_t *)myNegativeNumbers;

    These two (myCharBuf and myNegativeNumbers) now point to the same memory, but "looks" at it differently.

    Let's call the function;
    MyBufferEatingFunction(myCharBuf, sizeof(myNegativeNumbers));

    Notice that this gives no warnings because we explicitly cast the memory that we give to the function. The following would produce a warning;
    MyBufferEatingFunction(myNegativeNumbers, sizeof(myNegativeNumbers));

    Because you have in fact given it an unexpected type (int32_t[] instead of uint8_t*). It would work however, but the type-checking gives a warning. However since you know what you are doing, you cast the memory explicitly to tell the compiler that you do in fact know what you are doing.

    In your case, the stack APIs are expecting uint8_t pointers, but you are giving it something else.

    I would direct your attention to SimpleLink Academy, and the tutorial on custom profiles. The functionality is very similar to CC2541 although the guide is for CC2640R2. dev.ti.com/.../

    If you have other questions about C programming I would suggest a forum targeting programming questions.

    Best regards,
    Aslak
  • Hello Aslak,

    Thank you for your detailed explanation.

    I know by typecasting it will avoid warning. But my worry is, whether is it right way to do.

    Just to crosscheck I did changes in all Read, Write, Set, Get and simpleProfileChangeCB(). With typecasting I am able to read, modify Negative values.

    So based on format, I need to do typecasting in all relevant functions (Read, Write, Set, Get etc...).

    Thanks & Regards

    Vishnu Beema