Serial Port Profile with CC2650 and CC2640R2 (part 2)
Modified to transfer ADC values up to PC
Step 1:
Include ACD.h library
#include <ti/drivers/ADC.h>
Step 2:
Insert period event in order to update ADC read values from the potentiometer
#define SSSS_ICALL_EVT ICALL_MSG_EVENT_ID // Event_Id_31
#define SSSS_QUEUE_EVT UTIL_QUEUE_EVENT_ID // Event_Id_30
#define SSSS_PERIODIC_EVT Event_Id_00 //SOLUTION
// Bitwise OR of all events to pend on
#define SSSS_ALL_EVENTS (SSSS_ICALL_EVT | \
SSSS_QUEUE_EVT | \
SSSS_PERIODIC_EVT)
Declare handle period
// Clock instances for internal periodic events.
static Clock_Struct periodicClock;
and initialized it in SimpleSerialSocketServer_init()
Util_constructClock(&periodicClock, SimplePeripheral_clockHandler,
SBP_PERIODIC_EVT_PERIOD, 0, false, SSSS_PERIODIC_EVT);
Step 3: declare ADC handle, variables and Initialized ADC
static ADC_Handle adc; // my add
static ADC_Params params;// my add
char *adcStr = NULL; // adc had 12 bit
uint16_t adcValue; //my add
uint8_t adcReady = 0; // make to separate with send text from PC via UART
Next, in SimpleSerialSocketServer_init() function we initialize ADC
// ADC initialize
ADC_init();
ADC_Params_init(¶ms);
adc = ADC_open(Board_ADC0, ¶ms);
if (adc == NULL) {
//Log_error0("Error initializing ADC channel 0\n");
PIN_setOutputValue(ledPinHandle, Board_RLED, 1); //indicate mistake
Task_exit();;
}
Step 4:
Add process for Periodic Event in SimpleSerialSocketServer_taskFxn(UArg a0, UArg a1)
if (events & SSSS_PERIODIC_EVT)
{
Util_startClock(&periodicClock);
ADC_convert(adc, &adcValue);
adcStr = Byte2String(&adcValue);
adcReady = 1; //notify to send adcValue
SimpleSerialSocketServer_enqueueMsg(SSSS_OUTGOING_DATA, NULL, NULL, 6 );//sizeof(adcStr) 6 chars = 4 digits and two chars '\n and \r'
}
Here we add a Byte2String function to change the value from uint_16 to string with assume data has 12 bit so the data string will have 4 digits and 2 characters which are '\n' and '\r'
Step 4.2 declare extern function Byte2String
extern char *Byte2String(uint16_t *adcValue);
and add the Byte2String function into util.c
char *Byte2String(uint16_t *adcValue)
{
uint16_t num, div;
char hex[] = "0123456789"; //char
static char str[4 + 2]; //More character for \n\r
char *pStr = str;
//*pStr++ = '0';
//*pStr++ = 'x';
// Start from end of addr
// pAddr += B_ADDR_LEN;
num = *adcValue;
div = 1000;
while(div > 0)
{
*pStr++ = hex[num/div];
num = num%div;
div = div/10;
}
*pStr++ = '\n';
*pStr++ = '\r';
pStr = NULL;
return str;
}
Step 5: add Util_startClock to start clock function when two devices connected in function SimpleSerialSocketServer_processStateChangeEvt(gaprole_States_t newState)
..
case GAPROLE_CONNECTED:
{
uint8_t numActive = 0;
Util_startClock(&periodicClock); // SOLUTION
numActive = linkDB_NumActive();
Modified to transfer ADC values up to PC
Step 1:
Include ACD.h library
#include <ti/drivers/ADC.h>
Step 2:
Insert period event in order to update ADC read values from the potentiometer
#define SSSS_ICALL_EVT ICALL_MSG_EVENT_ID // Event_Id_31
#define SSSS_QUEUE_EVT UTIL_QUEUE_EVENT_ID // Event_Id_30
#define SSSS_PERIODIC_EVT Event_Id_00 //SOLUTION
// Bitwise OR of all events to pend on
#define SSSS_ALL_EVENTS (SSSS_ICALL_EVT | \
SSSS_QUEUE_EVT | \
SSSS_PERIODIC_EVT)
Declare handle period
// Clock instances for internal periodic events.
static Clock_Struct periodicClock;
and initialized it in SimpleSerialSocketServer_init()
Util_constructClock(&periodicClock, SimplePeripheral_clockHandler,
SBP_PERIODIC_EVT_PERIOD, 0, false, SSSS_PERIODIC_EVT);
Step 3: declare ADC handle, variables and Initialized ADC
static ADC_Handle adc; // my add
static ADC_Params params;// my add
char *adcStr = NULL; // adc had 12 bit
uint16_t adcValue; //my add
uint8_t adcReady = 0; // make to separate with send text from PC via UART
Next, in SimpleSerialSocketServer_init() function we initialize ADC
// ADC initialize
ADC_init();
ADC_Params_init(¶ms);
adc = ADC_open(Board_ADC0, ¶ms);
if (adc == NULL) {
//Log_error0("Error initializing ADC channel 0\n");
PIN_setOutputValue(ledPinHandle, Board_RLED, 1); //indicate mistake
Task_exit();;
}
Step 4:
Add process for Periodic Event in SimpleSerialSocketServer_taskFxn(UArg a0, UArg a1)
if (events & SSSS_PERIODIC_EVT)
{
Util_startClock(&periodicClock);
ADC_convert(adc, &adcValue);
adcStr = Byte2String(&adcValue);
adcReady = 1; //notify to send adcValue
SimpleSerialSocketServer_enqueueMsg(SSSS_OUTGOING_DATA, NULL, NULL, 6 );//sizeof(adcStr) 6 chars = 4 digits and two chars '\n and \r'
}
Here we add a Byte2String function to change the value from uint_16 to string with assume data has 12 bit so the data string will have 4 digits and 2 characters which are '\n' and '\r'
Step 4.2 declare extern function Byte2String
extern char *Byte2String(uint16_t *adcValue);
and add the Byte2String function into util.c
char *Byte2String(uint16_t *adcValue)
{
uint16_t num, div;
char hex[] = "0123456789"; //char
static char str[4 + 2]; //More character for \n\r
char *pStr = str;
//*pStr++ = '0';
//*pStr++ = 'x';
// Start from end of addr
// pAddr += B_ADDR_LEN;
num = *adcValue;
div = 1000;
while(div > 0)
{
*pStr++ = hex[num/div];
num = num%div;
div = div/10;
}
*pStr++ = '\n';
*pStr++ = '\r';
pStr = NULL;
return str;
}
Step 5: add Util_startClock to start clock function when two devices connected in function SimpleSerialSocketServer_processStateChangeEvt(gaprole_States_t newState)
..
case GAPROLE_CONNECTED:
{
uint8_t numActive = 0;
Util_startClock(&periodicClock); // SOLUTION
numActive = linkDB_NumActive();
..
case GAPROLE_WAITING:
{
Util_stopClock(&periodicClock); //SOLUTION
SimpleSerialSocketServer_freeAttRsp(bleNotConnected);
..
Comments
Post a Comment