Other Parts Discussed in Thread: SYSCONFIG, CC2652RB,
Tool/software: Code Composer Studio
Hello,
I have to receive incoming data from uart via arduino and store the incoming data into a sd card attached with cc2652 launchpad. In this problem i am trying to use mutex example code with two tasks and a semaphore to switch tasks. I have put uartecho code in one task and sdraw code in other task. please see my code below. I am getting errors. Is th apporach right?
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
/* Driver Header files */
#include <ti/drivers/GPIO.h>
#include <ti/drivers/SD.h>
#include <ti/display/Display.h>
#include <ti/drivers/UART.h>
/* Driver configuration */
#include "ti_drivers_config.h"
#define WRITEENABLE 0
/* Size of data buffers */
#define BUFFER_SIZE 1024
/* Starting sector to write/read to */
#define STARTINGSECTOR 0
/* Bytes per Megabyte */
#define BYTESPERMEGABYTE 1048576
static uint8_t writeBuffer[BUFFER_SIZE];
static uint8_t readBuffer[BUFFER_SIZE];
#if (WRITEENABLE)
static uint8_t emptyBuffer[BUFFER_SIZE] = {0};
#endif
/* XDC module Headers */
#include <xdc/std.h>
#include <xdc/runtime/System.h>
/* BIOS module Headers */
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Clock.h>
#include <ti/sysbios/knl/Task.h>
#include <ti/sysbios/knl/Semaphore.h>
#include <ti/drivers/Board.h>
#define TASKSTACKSIZE 512
Void task1Fxn(UArg arg0, UArg arg1);
Void task2Fxn(UArg arg0, UArg arg1);
Int resource = 0;
Int finishCount = 0;
UInt32 sleepTickCount;
Task_Struct task1Struct, task2Struct;
Char task1Stack[TASKSTACKSIZE], task2Stack[TASKSTACKSIZE];
Semaphore_Struct semStruct;
Semaphore_Handle semHandle;
/*
* ======== main ========
*/
int main()
{
/* Construct BIOS objects */
Task_Params taskParams;
Semaphore_Params semParams;
Display_init();
GPIO_init();
SD_init();
/* Call driver init functions */
Board_init();
GPIO_setConfig(CONFIG_GPIO_LED_0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
/* Construct writer/reader Task threads */
Task_Params_init(&taskParams);
taskParams.stackSize = TASKSTACKSIZE;
taskParams.stack = &task1Stack;
taskParams.priority = 1;
Task_construct(&task1Struct, (Task_FuncPtr)task1Fxn, &taskParams, NULL);
taskParams.stack = &task2Stack;
taskParams.priority = 2;
Task_construct(&task2Struct, (Task_FuncPtr)task2Fxn, &taskParams, NULL);
/* Construct a Semaphore object to be use as a resource lock, inital count 1 */
Semaphore_Params_init(&semParams);
Semaphore_construct(&semStruct, 1, &semParams);
/* Obtain instance handle */
semHandle = Semaphore_handle(&semStruct);
/* We want to sleep for 10000 microseconds */
sleepTickCount = 10000 / Clock_tickPeriod;
BIOS_start(); /* Does not return */
return(0);
}
/*
* ======== task1Fxn ========
*/
Void task1Fxn(UArg arg0, UArg arg1)
{
UInt32 time;
for (;;) {
SD_Handle sdHandle;
Display_Handle display;
uint32_t cardCapacity;
uint32_t totalSectors;
uint32_t sectorSize;
uint32_t sectors;
uint32_t i;
int16_t result;
Display_init();
GPIO_init();
SD_init();
System_printf("Running task1 function\n");
if (Semaphore_getCount(semHandle) == 0) {
System_printf("Sem blocked in task1\n");
}
/* Get access to resource */
Semaphore_pend(semHandle, BIOS_WAIT_FOREVER);
/* Do work by waiting for 2 system ticks to pass */
time = Clock_getTicks();
while (Clock_getTicks() <= (time + 1)) {
;
}
/* Do work on locked resource */
resource += 1;
/* Unlock resource */
Semaphore_post(semHandle);
Task_sleep(sleepTickCount);
/* Open the display for output */
display = Display_open(Display_Type_UART, NULL);
if (display == NULL) {
/* Failed to open display driver */
while (1);
}
/* Initialize the writeBuffer with data */
for (i = 0; i < BUFFER_SIZE; i++) {
writeBuffer[i] = i & 0xFF;
}
/* Mount and register the SD Card */
sdHandle = SD_open(CONFIG_SD_0, NULL);
if (sdHandle == NULL) {
Display_printf(display, 0, 0, "Error starting the SD card!");
while (1);
}
result = SD_initialize(sdHandle);
if (result != SD_STATUS_SUCCESS) {
Display_printf(display, 0, 0, "Error initializing the SD card!");
while (1);
}
/* Turn on user LED */
GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_ON);
Display_printf(display, 0, 0, "\n\rStarting the SD example...\n");
/* Read capacity information from the SD card */
totalSectors = SD_getNumSectors(sdHandle);
sectorSize = SD_getSectorSize(sdHandle);
cardCapacity = (totalSectors / BYTESPERMEGABYTE) * sectorSize;
/* Display capacity information */
Display_printf(display, 0, 0, "======== SD Card Information ========");
Display_printf(display, 0, 0, "Sectors:\t\t%u", totalSectors);
Display_printf(display, 0, 0, "Sector Size:\t\t%u bytes", sectorSize);
Display_printf(display, 0, 0, "Card Capacity:\t\t%u MB", cardCapacity);
Display_printf(display, 0, 0, "=====================================\n");
/* Calculate number of sectors taken up by the array by rounding up */
sectors = (BUFFER_SIZE + sectorSize - 1) / sectorSize;
#if (WRITEENABLE)
Display_printf(display, 0, 0, "Writing %u bytes...", BUFFER_SIZE);
result = SD_write(sdHandle, writeBuffer, STARTINGSECTOR, sectors);
if (result != SD_STATUS_SUCCESS) {
Display_printf(display, 0, 0, "Error writing to the SD card!");
while (1);
}
#else
Display_printf(display, 0, 0, "Run the example with WRITEENABLE "
"= 1 to enable writes to the SD card!");
#endif
Display_printf(display, 0, 0, "Reading %u bytes...", BUFFER_SIZE);
result = SD_read(sdHandle, readBuffer, STARTINGSECTOR, sectors);
if (result != SD_STATUS_SUCCESS) {
Display_printf(display, 0, 0, "Error reading from the SD card!");
while (1);
}
/* Compare data read from the SD card with expected values */
Display_printf(display, 0, 0, "\nComparing read and write data...");
if (memcmp(readBuffer, writeBuffer, BUFFER_SIZE)) {
#if (WRITEENABLE)
Display_printf(display, 0, 0,
"Data read does not match write data!");
while (1);
#else
Display_printf(display, 0, 0,
"WRITEENABLE == 0, therefore data mismatch occurred!");
#endif
}
else {
Display_printf(display, 0, 0,
"Data matches!");
}
#if (WRITEENABLE)
/* Clear any previously written data overwriting data with 0x0 */
Display_printf(display, 0, 0, "Erasing data written...");
result = SD_write(sdHandle, emptyBuffer, STARTINGSECTOR, sectors);
if (result != SD_STATUS_SUCCESS) {
Display_printf(display, 0, 0, "Error erasing the SD card!");
while (1);
}
#endif
Display_printf(display, 0, 0, "Done!");
SD_close(sdHandle);
}
}
}
/*
* ======== task2Fxn ========
*/
Void task2Fxn(UArg arg0, UArg arg1)
{
for (;;) {
System_printf("Running task2 function\n");
char input;
const char echoPrompt[] = "Echoing characters:\r\n";
UART_Handle uart;
UART_Params uartParams;
/* Call driver init functions */
GPIO_init();
UART_init();
/* Configure the LED pin */
GPIO_setConfig(CONFIG_GPIO_LED_0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
/* Turn on user LED */
GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_ON);
/* Create a UART with data processing off. */
UART_Params_init(&uartParams);
uartParams.writeDataMode = UART_DATA_BINARY;
uartParams.readDataMode = UART_DATA_BINARY;
uartParams.readReturnMode = UART_RETURN_FULL;
uartParams.baudRate = 115200;
uart = UART_open(CONFIG_UART_0, &uartParams);
if (uart == NULL) {
/* UART_open() failed */
while (1);
}
UART_write(uart, echoPrompt, sizeof(echoPrompt));
/* Loop forever echoing */
while (1) {
UART_read(uart, &input, 1);
UART_write(uart, &input, 1);
}
if (Semaphore_getCount(semHandle) == 0) {
System_printf("Sem blocked in task2\n");
}
/* Get access to resource */
Semaphore_pend(semHandle, BIOS_WAIT_FOREVER);
/* Do work on locked resource */
resource += 1;
/* Unlock resource */
Semaphore_post(semHandle);
Task_sleep(sleepTickCount);
finishCount++;
if (finishCount == 5) {
System_printf("Calling BIOS_exit from task2\n");
BIOS_exit(0);
}
}
}

