Part Number:CC2640
Tool/software: TI-RTOS
I am working with the following code i have to clock tasks one for reading from SPI every 19.53 ms and the second reading from I2C every 1 second before i add the I2C the program was working probably after i added the I2C the progra
/*
* Copyright (c) 2015-2016, Texas Instruments Incorporated
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of Texas Instruments Incorporated nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* ======== uartecho.c ========
*/
/* XDCtools Header files */
#include <xdc/std.h>
#include <stdio.h>
#include <xdc/runtime/System.h>
/* BIOS Header files */
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Task.h>
/* TI-RTOS Header files */
#include <ti/drivers/PIN.h>
#include <ti/drivers/UART.h>
#include <ti/drivers/PWM.h>
#include <ti/drivers/SPI.h>
#include <ti/drivers/I2C.h>
#include <ti/sysbios/knl/Clock.h>
/* Example/Board Header files */
#include "Board.h"
#include <stdint.h>
#define TASKSTACKSIZE 768
Task_Struct task0Struct;
Char task0Stack[TASKSTACKSIZE];
UART_Handle uart;
UART_Params uartParams;
PWM_Handle pwm1;
PWM_Params params;
uint32_t pwmPeriod = 131080; // Period and duty in microseconds
uint16_t duty = 1000;
uint16_t dutyInc = 100;
PIN_Handle ledPinHandle;
static PIN_Handle GSRPinHandle;
void clk0Fxn(UArg arg0);
void GSRCallbackFxn(PIN_Handle handle, PIN_Id pinId);
Clock_Struct clk0Struct,clk1Struct;
Clock_Handle clk2Handle,clk1Handle;
/* Construct BIOS Objects */
static SPI_Handle ADChandle;
static SPI_Params ADCparams;
static SPI_Transaction spiTransaction;
static uint8_t transmitBuffer[24];
static uint8_t receiveBuffer[24];
static char SendBuff[25]="0123456789abcdefghi";
static uint16_t count=0;
static uint16_t HeartRate=0;
static float old_res=1000;
uint8_t txBuffer[10];
uint8_t rxBuffer[5];
I2C_Handle i2c;
I2C_Params i2cParams;
I2C_Transaction i2cTransaction;
/* Global memory storage for a PIN_Config table */
static PIN_State ledPinState;
static PIN_State GSRPinState;
//static uint32_t count=0;
Clock_Params clkParams;
/*
* Application LED pin configuration table:
* - All LEDs board LEDs are off.
*/
PIN_Config PinTable[] = {
Board_SPI_FLASH_CS | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
PIN_TERMINATE
};
PIN_Config GSRPinTable[] = {
Board_GSR_INT | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_DIS,
POST | PIN_INPUT_EN | PIN_PUSHPULL | PIN_IRQ_DIS,
PIN_TERMINATE
};
void SPIFxn(SPI_Handle handle, SPI_Transaction *transaction)
{
// HeartRate=50;// sem_post(&slaveSem);
}
void I2CFxn(I2C_Handle handle, I2C_Transaction *transaction, bool res)
{
HeartRate=res;//receiveBuffer[5];// sem_post(&slaveSem);
count++;
}
void GSRCallbackFxn(PIN_Handle handle, PIN_Id pinId) {
}
/*
* ======== clk0Fxn =======
*/
Void clk0Fxn(UArg arg0)
{
static float time;
static float volt;
static float new_res;
static char pi1=0;
// static char str[];
time = Clock_getTicks();
time/=100;
//PWM_stop(pwm1);
//PIN_setOutputValue(ledPinHandle, Board_SPI_FLASH_CS, 0);
pi1=PIN_getInputValue(Board_GSR_INT);
//pi2=PIN_getInputValue(POST);
SPI_transfer(ADChandle, &spiTransaction);
//PWM_start(pwm1);
volt=receiveBuffer[0]*0x10000+receiveBuffer[1]*0x100+receiveBuffer[2];
if (volt>0x7fffff) {
volt=-1*(16777216-volt);
};
volt=volt/((12842325.47-volt)/2000);
new_res=volt;
if (volt-old_res<-50) {
volt=old_res;
}
old_res=new_res;
//PIN_setOutputValue(ledPinHandle, Board_SPI_FLASH_CS, 1);
/*
count++;
if (count>=103){
I2C_transfer(i2c, &i2cTransaction);
count=0;
}
*/
sprintf(SendBuff,"%10.2f,%8.3f, %d\r",time,volt,count);
if (pi1==0) UART_write(uart, SendBuff, sizeof(SendBuff));
// PIN_setOutputValue(ledPinHandle, Board_SPI_FLASH_CS,!PIN_getInputValue(Board_SPI_FLASH_CS));
}
Void clk1Fxn(UArg arg0)
{
I2C_transfer(i2c, &i2cTransaction);
}
/*
* ======== echoFxn ========
* Task for this function is created statically. See the project's .cfg file.
*/
void echoFxn(UArg arg0, UArg arg1)
{
/* Call board init functions */
////////////////////////
UART_Params_init(&uartParams);
uartParams.writeDataMode = UART_DATA_BINARY;
uartParams.readDataMode = UART_DATA_BINARY;
uartParams.readReturnMode = UART_RETURN_FULL;
uartParams.readEcho = UART_ECHO_OFF;
uartParams.baudRate = 115200;
uart = UART_open(Board_UART0, &uartParams);
if (uart == NULL) {
System_abort("Error opening the UART");
}
//////////////////////
PWM_Params_init(¶ms);
params.dutyUnits = PWM_DUTY_FRACTION;
params.dutyValue =PWM_DUTY_FRACTION_MAX/2;
params.periodUnits = PWM_PERIOD_HZ;
params.periodValue = pwmPeriod;
pwm1 = PWM_open(Board_PWM3, ¶ms);
if (pwm1 == NULL) {
System_abort("Board_PWM0 did not open");
}
PWM_start(pwm1);
//////////////////
SPI_Params_init(&ADCparams);
ADCparams.bitRate=1000000;
ADCparams.dataSize=8;
//SPI_init();
ADCparams.transferCallbackFxn=SPIFxn;
ADCparams.transferMode=SPI_MODE_CALLBACK;
ADChandle = SPI_open(Board_SPI_MASTER, &ADCparams);
if (!ADChandle) {
Task_exit();
}
spiTransaction.rxBuf=(void *)receiveBuffer;
spiTransaction.txBuf=(void *)transmitBuffer;
spiTransaction.count=3;
//intiating the I2C
I2C_Params_init(&i2cParams);
//i2cParams.transferCallbackFxn=I2CFxn;
i2cParams.transferMode=I2C_MODE_BLOCKING;
i2cParams.bitRate = I2C_400kHz;
i2c = I2C_open(Board_I2C0, &i2cParams);
if (i2c == NULL) {
System_abort("Error Initializing I2C\n");
}
transmitBuffer[0] = 0;
transmitBuffer[1] = 3;
transmitBuffer[2] = 0x44;
transmitBuffer[3] = 1;
transmitBuffer[4] = 1;
i2cTransaction.slaveAddress = 0x44;
i2cTransaction.writeBuf = transmitBuffer;
i2cTransaction.writeCount = 5;
i2cTransaction.readBuf = receiveBuffer;
i2cTransaction.readCount = 5;
//intiating the clock function
////
Clock_Params_init(&clkParams);
clkParams.period = 19530/Clock_tickPeriod;
clkParams.startFlag = false;
// Construct a periodic Clock Instance
Clock_construct(&clk0Struct, (Clock_FuncPtr)clk0Fxn,
0, &clkParams);
clkParams.period = 2000000/Clock_tickPeriod;
clkParams.startFlag = false;
// Construct a periodic Clock Instance
Clock_construct(&clk1Struct, (Clock_FuncPtr)clk1Fxn,
0, &clkParams);
/* Open LED pins */
/////////
ledPinHandle = PIN_open(&ledPinState, PinTable);
if(!ledPinHandle) {
System_abort("Error initializing board LED pins\n");
}
//////
GSRPinHandle = PIN_open(&GSRPinState, GSRPinTable);
if(!GSRPinHandle) {
System_abort("Error initializing GSR pins\n");
}
//char input;
char echoPrompt[8]="ABCDE\r\n";
/* Create a UART with data processing off. */
Task_sleep(100);
PIN_setOutputValue(ledPinHandle, Board_SPI_FLASH_CS, 1);
Task_sleep(100);
PIN_setOutputValue(ledPinHandle, Board_SPI_FLASH_CS, 0);
Task_sleep(100);
PIN_setOutputValue(ledPinHandle, Board_SPI_FLASH_CS, 1);
Task_sleep(500);
UART_write(uart, echoPrompt, sizeof(echoPrompt));
while(!PIN_getInputValue(POST)){};
// send START command for PPG
I2C_transfer(i2c, &i2cTransaction);
I2C_close(i2c);
i2cParams.transferCallbackFxn=I2CFxn;
i2cParams.transferMode=I2C_MODE_CALLBACK;
i2cParams.bitRate = I2C_400kHz;
i2c = I2C_open(Board_I2C0, &i2cParams);
if (i2c == NULL) {
System_abort("Error Initializing I2C\n");
}
transmitBuffer[0] = 0;
transmitBuffer[1] = 4;
transmitBuffer[2] = 0x44;
transmitBuffer[3] = 2;
transmitBuffer[4] = 8;
transmitBuffer[5] = 0x20;
i2cTransaction.slaveAddress = 0x44;
i2cTransaction.writeBuf = transmitBuffer;
i2cTransaction.writeCount = 6;
i2cTransaction.readBuf = receiveBuffer;
i2cTransaction.readCount =6;
// send GET heart rate command for PPG
// set DATA for heart rate parameter for PPG
/*if (PIN_registerIntCb(GSRPinHandle, &GSRCallbackFxn) != 0) {
System_abort("Error registering button callback function");
}
PIN_setConfig(GSRPinHandle, PIN_BM_IRQ, Board_GSR_INT | PIN_IRQ_NEGEDGE);
*/
////
clk2Handle = Clock_handle(&clk0Struct);
Clock_start(clk2Handle);
clk1Handle = Clock_handle(&clk1Struct);
Clock_start(clk1Handle);
/* Loop forever echoing */
while (1) {
// I2C_transfer(i2c, &i2cTransaction);
//SPI_transfer(ADChandle, &spiTransaction);
// System_sprintf(echoPrompt,"%d \r\n",receiveBuffer[0]*0x100+receiveBuffer[1]*0x10+receiveBuffer[2]);
// UART_write(uart, echoPrompt, sizeof(echoPrompt));
//CPUdelay(8000*50);
}
}
/*
* ======== buttonCallbackFxn ========
* Pin interrupt Callback function board buttons configured in the pinTable.
* If Board_LED3 and Board_LED4 are defined, then we'll add them to the PIN
* callback function.
*/
/*
* ======== main ========
*/
int main(void)
{
Task_Params taskParams;
Board_initGeneral();
Board_initUART();
Board_initPWM();
Board_initSPI();
Board_initI2C();
/* Construct BIOS objects */
Task_Params_init(&taskParams);
taskParams.stackSize = TASKSTACKSIZE;
taskParams.stack = &task0Stack;
Task_construct(&task0Struct, (Task_FuncPtr)echoFxn, &taskParams, NULL);
/* Start BIOS */
BIOS_start();
return (0);
}
![]()
m stopped after many reading and hangs. after i debugged the program i found that it hangs in a function related to the sysbios as in the photo (endless loop)