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.

RTOS/LAUNCHXL-CC2640R2: Creating/Adding source(.c) and header(.h) file in TI RTOS

Part Number: LAUNCHXL-CC2640R2
Other Parts Discussed in Thread: BLE-STACK

Tool/software: TI-RTOS

Hello,

I used Project Zero for Reference and now finished with my firmware,

Now I want to make my code modular so was trying to add a source (.c) and header (.h) file, 

I have used  I2C and PWM in my code so now i am willing make these i2c and pwm part modular which is currently a part of project_zero.c

However, when i did so and complied the project there are no errors nor any warning,

But when i run the code on CC2640R2-Launch XL my code is not working properply whereas before making the code modular it was working fine,

I tried adding these source(.c) and header(.h) file in profile, application, startup folder but still the issue remains the same.

I have attached my source(.c) and header(.h) header file below please check and help me if i am wrong anywhere,

Also if there are any other steps to add source(.c) and header(.h) file please guide me with those steps.

/*
 * sensor.c
 *
 *  Created on: Mar 1, 2018
 *      Author: uvpatil
 */

/* XDCtools Header files */
#include <xdc/std.h>
#include <xdc/cfg/global.h>
#include <xdc/runtime/Error.h>
#include <xdc/runtime/System.h>

/* BIOS Header files */
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Task.h>

#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <unistd.h>
#include <stdbool.h>
/* Driver Header files */
#include <ti/drivers/GPIO.h>
#include <ti/drivers/I2C.h>
#include <ti/drivers/PIN.h>
#include <ti/display/Display.h>
//#include <ti/drivers/PWM.h>
/* Example/Board Header files */
#include "Board.h"

#include <icall.h>
#include "hiddev.h"

#include "battery.h"
#include "sensor.h"


//I2C parameter Declaration
unsigned int    i;
uint16_t        ReadBuffer[2];
uint8_t         txBuffer[1];
uint8_t         rxBuffer[2];
I2C_Handle      i2c;
I2C_Params      i2cParams;
I2C_Transaction i2cTransaction;
uint16_t        temp;
uint8_t         txBuffer1[2];
uint8_t         rxBuffer1[2];

extern uint32_t ui32SenseLevel;
extern uint32_t ui32LowBattery;
/*********************************************************************
 *                        I2C
 *********************************************************************/
/*********************************************************************
 * @fn      Initaccelerometer
 *
 * @brief   Initialise I2C parameters
 *
 * @param   None
 *
 * @return  None
 */

void Initaccelerometer()
{
    GPIO_init();
    I2C_init();
    AccelWrite();
    AccelRead();
    ui32LowBattery=BATTERY_STATUS;

}


/*********************************************************************
 * @fn      AccelWrite
 *
 * @brief   perform Write using I2C
 *
 * @param   None
 *
 * @return  None
 */

void AccelWrite()
{

    /* Create I2C for usage */
    I2C_Params_init(&i2cParams);
    i2cParams.bitRate = I2C_400kHz;
    i2c = I2C_open(Board_I2C0, &i2cParams);
    if (i2c == NULL) {
        Display_printf(display, 0, 0, "Error Initializing I2C\n");
        while (1);
    }
    else {
        Display_printf(display, 0, 0, "I2C Initialized!\n");
    }

    /* Write to the Accelerometer Control register 1 to enable only accelerometer of the sensor */
    txBuffer1[0] = 0x20;
    txBuffer1[1] = 0x27;
    i2cTransaction.slaveAddress = 0x1D;
    i2cTransaction.writeBuf = txBuffer1;
    i2cTransaction.writeCount = 2;
    i2cTransaction.readBuf = rxBuffer1;
    i2cTransaction.readCount = 2;

    if (I2C_transfer(i2c, &i2cTransaction))
    {
        /* Extract written data */
        temp = rxBuffer1[0];
    }

    /* Write to the sensor Control register to enable active low interrupt from the sensor */
    txBuffer1[0] = 0x24;
    txBuffer1[1] = 0x00;
    i2cTransaction.slaveAddress = 0x1D;
    i2cTransaction.writeBuf = txBuffer1;
    i2cTransaction.writeCount = 2;
    i2cTransaction.readBuf = rxBuffer1;
    i2cTransaction.readCount = 2;

    if (I2C_transfer(i2c, &i2cTransaction))
    {
        /* Extract written data */
        temp = rxBuffer1[0];
    }


    /* Write to the Accelerometer Control register 3 to generate the Interrupt on INT1 pin */
    txBuffer1[0] = 0x22;
    txBuffer1[1] = 0xA0;
    i2cTransaction.slaveAddress = 0x1D;
    i2cTransaction.writeBuf = txBuffer1;
    i2cTransaction.writeCount = 2;
    i2cTransaction.readBuf = rxBuffer1;
    i2cTransaction.readCount = 2;

    if (I2C_transfer(i2c, &i2cTransaction))
    {
        /* Extract written data */
        temp = rxBuffer1[0];
    }

    /* Write to the Activity Threshold register to set the activity Threshold */
    txBuffer1[0] = 0x1E;
    txBuffer1[1] = ui32SenseLevel;
    i2cTransaction.slaveAddress = 0x1D;
    i2cTransaction.writeBuf = txBuffer1;
    i2cTransaction.writeCount = 2;
    i2cTransaction.readBuf = rxBuffer1;
    i2cTransaction.readCount = 2;

    if (I2C_transfer(i2c, &i2cTransaction))
    {
        /* Extract written data */
        temp = rxBuffer1[0];
    }

    /* Write to the Activity duration register to Set the activity Duration */
    txBuffer1[0] = 0x1F;
    txBuffer1[1] = 0x00;
    i2cTransaction.slaveAddress = 0x1D;
    i2cTransaction.writeBuf = txBuffer1;
    i2cTransaction.writeCount = 2;
    i2cTransaction.readBuf = rxBuffer1;
    i2cTransaction.readCount = 2;

    if (I2C_transfer(i2c, &i2cTransaction))
    {
        /* Extract written data */
        temp = rxBuffer1[0];
    }

    /* Write to the Interrupt Control register to enable Interrupt */
    txBuffer1[0] = 0x30;
    txBuffer1[1] = 0x2A;
    i2cTransaction.slaveAddress = 0x1D;
    i2cTransaction.writeBuf = txBuffer1;
    i2cTransaction.writeCount = 2;
    i2cTransaction.readBuf = rxBuffer1;
    i2cTransaction.readCount = 2;

    if (I2C_transfer(i2c, &i2cTransaction))
    {
        /* Extract written data */
        temp = rxBuffer1[0];
    }
    I2C_close(i2c);
}

/*********************************************************************
 * @fn      AccelRead
 *
 * @brief   perform Read using I2C
 *
 * @param   None
 *
 * @return  None
 */

void AccelRead()
{

    /* Create I2C for usage */
    I2C_Params_init(&i2cParams);
    i2cParams.bitRate = I2C_400kHz;
    i2c = I2C_open(Board_I2C0, &i2cParams);
    if (i2c == NULL) {
        Display_printf(display, 0, 0, "Error Initializing I2C\n");
        while (1);
    }
    else {
        Display_printf(display, 0, 0, "I2C Initialized!\n");
    }

    /* Point to the Activity Threshold register and read its data */
    txBuffer[0] = 0x0F;
    i2cTransaction.slaveAddress = 0x1D;
    i2cTransaction.writeBuf = txBuffer;
    i2cTransaction.writeCount = 1;
    i2cTransaction.readBuf = rxBuffer;
    i2cTransaction.readCount = 2;


    if (I2C_transfer(i2c, &i2cTransaction)) {
        /* Extract  the received data*/
        ReadBuffer[0] = rxBuffer[0];
    }

    I2C_close(i2c);
}

sensor.h

Thanks and Regards

Utkarsh

  • Hi Project Zero is from older SDK versions. I suggest you simple peripheral from SDK v1.40 or SDKv1.50 as base. I have added several code files to simple peripheral and did not encounter problems.
  • Hello,

    Thank you For replying,

    I understand Project zero is older version but now my all firmware is ready just need to make it Modular,

    And changing all my firmware according simple peripheral would be difficult and i also have less time,

    So Please help me if there any other way so that i can make the code modular using same firmware,

    Also while creating source(.c) and header(.h) files in TI RTOS are there any different steps to be followed which i am might be missing, then please help me with it.

    Thanks and Regards
    Utkarsh
  • Are you sure you application code is including "sensor.h" and is using your code? Simply adding your *.h and *.c files does not magically include and use your code, your application has to.
  • Hello,

    I do understand *.h and *.c file doesn't get magically added, but i have already added sensor.h file in my application code,

    Also i have checked that the functions from sensor.c file are linked with my application code,

    i have checked it by adding a break point in sensor.c file so when i run the code the compiler goes to the break point in sensor.c file when the functions from it are called in the project zero.c file,

    But the issue is that some part of the code is not working properly which are working fine if remove the modular part of the code,

    And also the device gets disconnected from the BLE Scanner application and functionality also doesn't works properly.

    I have attached the images where the code is going to Break point in sensor.c file 

    So how can resolve this issue

    Thanks and Regards

    Utkarsh

  • This sounds more like your code is not doing what you expect it to do. Have you tried debugging your application to verify your application isn't crashing and your sensor code works as expected?
  • Thank You for replying,

    I have verified but the application is working fine because i have checked with BLE scanner also and before making the code modular application runs perfectly,

    Also regarding sensor code, i am not able to verify it because as i mentioned in last mail that after some the device gets disconnected after some time 

    And also i have observed that when i start running the code i am expected to receive data from the launchpad,

    Now while receiving the data i am only able to receive half of the data other data(Checked on BLE scanner Andriod Application),

    However before making the code modular things where working totally fine,

    I also deleted and redesigned the *.h and & *.c file but the issue remains the same,

    Now can anyone Help me in this case that how i can make my code modular.

    Thanks and Regards 

    Utkarsh

  • Utkarsh,

    For me to understand why your code is not working you need to tell me what your code does. To quickly answer your question about how to make your code modular, making new *.h and *.c files and including them in your code is the way to go.

    When you are saying your application worked before adding the sensor.h and sensor.c files indicates your code is causing a bug with the application. I highly suggest to go over your code again, possibly only run you sensor code without the BLE application to verify it works as expected.

    You also say you expect to receive data from the LaunchPad, but only getting half of it. Where are you expecting this data? UART? BLE?
  • Thank You For replying,

    My aim is to exchange sensor data from launchpad to Bluetooth application using BLE, that is I want to send sensor data to BLE application (i.e., BLE Scanner) and change sensor configuration based on commands(data) received from BLE application, also I have a PWM controlled LED indication

    To my hardware side i have a sensor connected using I2C to Launchpad that is connected to BLE scanner via BLE,

    Initially my hardware sends data to the BLE application and if any change in sensor values accordingly the data is also updated.

    Similarly Through BLE Scanner user can send some commands to the Hardware and according to these commands the sensor configuration are updated

    This is the way my code works

    Now the exact issue issues what i am facing is

    1) For Modular Code (Sensor controlling code(i2c communication) is made in form of sensor.h and sensor.c files) :
    When the code is made Modular the (Hardware)device is visible on BLE scanner it also gets connected when i try to connect,
    Once I get connected I expect some initial data on all the characteristics but on some characteristics the initial data is not received on BLE scanner, whereas on other characteristics the data is received.
    Also once this happens after some time(few seconds) the connection is also lost,

    2)For Non Modular Code(Sensor controlling code(i2c communication) is part of same code of project_zero.c) :
    All the functionality of the Hardware with BLE communication on BLE Scanner is working absolutely Fine no issue at all

    So please help me to fix this bug

    Thanks and Regards
    UTkarsh
  • If you are only getting some of your characteristics on your phone then your services are not setup correctly. Go over and double check how your characteristics are setup. Take a look at the Bluetooth Developer Studio or the Custom Service SimpleLink Academy to make sure your characteristics are correct.

    You could also use HEAPMGR to debug the BLE-stack to see what is causing the stack to fail. A guide for how to use HEAPMGR can be found in the BLE-stack Users Guide by searching for HEAPMGR.

  • Thank You for replying and help,

    Thank you for suggestion about stack size, As the issue  is resolved when i increased my stack size declared for project zero 

    Thanks and Regards

    Utkarsh