#include #include #include #include #include #include #include #include #include #include #include #include /* Driver configuration */ #include "ti_drivers_config.h" /* Driver configuration */ #include "ti_drivers_config.h" /* String conversion macro */ #define STR_(n) #n #define STR(n) STR_(n) /* Drive number used for FatFs */ #define DRIVE_NUM 0 const char inputfile[] = "fat:"STR(DRIVE_NUM)":input.txt"; const char outputfile[] = "fat:"STR(DRIVE_NUM)":output.txt"; char textarray[] = "Hello from shivam"; char fatfsPrefix[] = "fat"; /* XDC module Headers */ #include #include /* BIOS module Headers */ #include #include #include #include #include #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; /* Call driver init functions */ Board_init(); /* 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; UART_Handle handle; UART_Params params; char rxBuf; // Receive buffer GPIO_init(); UART_init(); GPIO_setConfig(CONFIG_GPIO_LED_0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW); for (;;) { System_printf("Running task1 function\n"); UART_Params_init(¶ms); params.baudRate = 115200; params.writeDataMode = UART_DATA_BINARY; // Open the UART and do the read handle = UART_open(CONFIG_UART_0, ¶ms); if (handle == NULL) { /* UART_open() failed */ while (1); } /* Turn on user LED to indicate successful initialization */ GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_ON); while(rxBuf!='\n') { UART_read(handle, &rxBuf, 1); UART_write(handle, &rxBuf, 1); System_printf(&rxBuf); } 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); } } /* * ======== task2Fxn ======== */ Void task2Fxn(UArg arg0, UArg arg1) { SDFatFS_Handle sdfatfsHandle; FILE *src; SDFatFS_init(); add_device(fatfsPrefix, _MSA, ffcio_open, ffcio_close, ffcio_read, ffcio_write, ffcio_lseek, ffcio_unlink, ffcio_rename); for (;;) { System_printf("Running task2 function\n"); sdfatfsHandle = SDFatFS_open(CONFIG_SDFatFS_0, DRIVE_NUM); src = fopen(inputfile, "r"); if (!src) { /* Open file for both reading and writing */ src = fopen(inputfile, "w+"); if (!src) { while (1); } fwrite(textarray, 1, strlen(textarray), src); fclose(src); SDFatFS_close(sdfatfsHandle); } 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); } } } int32_t fatfs_getFatTime(void) { /* * FatFs uses this API to get the current time in FatTime format. User's * must implement this function based on their system's timekeeping * mechanism. See FatFs documentation for details on FatTime format. */ /* Jan 1 2017 00:00:00 */ return (0x4A210000); }