/* * CommunicationTask.c * * Created on: 8 áàôø× 2018 * Author: shlomo */ #include "include.h" #include "drivers/Uart_Comm/uart.h" #include "communicationTask.h" static void (*callback)(char* buffer, size_t length); Mailbox_Handle CommunicationRxMsgQ = NULL; Mailbox_Handle CommunicationTxMsgQ = NULL; typedef struct CommRxMessage{ uint16_t messageId; uint16_t msgSize; }CommRxMessageStruc; typedef struct CommTxMessage{ uint16_t messageId; uint16_t msgSize; char *Buff; }CommTxMessageStruc; int CommType = isUART; struct serialBuffer { char *buffer; size_t used; size_t size; } typedef SerialBuffer; char Buffer[2000]; SerialBuffer inBuffer; uint32_t initArray(size_t initialSize) { SerialBuffer *a = &inBuffer; if (initialSize >= 2000) return 0; a->buffer = Buffer; a->used = 0; a->size = initialSize; return initialSize; } void insertArray(char element) { SerialBuffer *a = &inBuffer; // a->used is the number of used entries, because a->array[a->used++] updates a->used only *after* the array has been accessed. // Therefore a->used can go up to a->size a->buffer[a->used++] = element; } void freeArray(void) { SerialBuffer *a = &inBuffer; a->used = a->size = 0; } Mailbox_Handle CommunicationRxMsgQ; uint32_t CommunicationTaskInit(void) { USBCDCD_init(); Init_U0(); return OK; } uint32_t CommTxMsgCounter = 0; uint32_t CommRxMsgCounter = 0; uint32_t CommunicationTaskMessageReceived(uint16_t msgSize) { CommRxMessageStruc Message; CommRxMsgCounter++; Message.messageId = 1; Message.msgSize = msgSize; if (CommunicationRxMsgQ != NULL) /*retcode =*/ Mailbox_post(CommunicationRxMsgQ , &Message, BIOS_NO_WAIT); return OK; } void RegisterReceiveCallback(void (*callback_ptr)(char* buffer, size_t length)) { callback = callback_ptr; } /****************************************************************************** * ======== communicationTask ======== * Task for this function is created statically. See the project's .cfg file. * this communication task is created statically in system initialization, in blocking mode * over one of the chosen ommunication methods (USB or Blutooth). ******************************************************************************/ void communicationTask(UArg arg0, UArg arg1) { //uint32_t ui32RxCount; CommRxMessageStruc Message; initArray(1); //ui32RxCount = 0; CommunicationRxMsgQ = Mailbox_create(sizeof(CommRxMessageStruc), 5, NULL,NULL); /* Block while the device is NOT connected to the USB */ // Semaphore_pend(initConnectionSem, BIOS_WAIT_FOREVER); while(1) { Mailbox_pend(CommunicationRxMsgQ , &Message, BIOS_WAIT_FOREVER); switch (Message.messageId) { case 1: //ui32RxCount += Message.msgSize; if (callback != NULL) { callback(inBuffer.buffer,inBuffer.used); } freeArray(); initArray( 1); break; default: break; } } } uint32_t CommunicationTaskSendMessage(char* buffer,size_t length) { CommTxMessageStruc Message; CommTxMsgCounter++; Message.messageId = 1; Message.msgSize = length; Message.Buff = buffer; if (CommunicationTxMsgQ != NULL) /*retcode =*/ Mailbox_post(CommunicationTxMsgQ , &Message, BIOS_NO_WAIT); return OK; } int32_t SetCommunicationPath(bool UARTorUSB) { CommType = UARTorUSB; return OK; } /****************************************************************************** * ======== communicationTask ======== * Task for this function is created statically. See the project's .cfg file. * this communication task is created statically in system initialization, in blocking mode * over one of the chosen ommunication methods (USB or Blutooth). ******************************************************************************/ void communicationTxTask(UArg arg0, UArg arg1) { //uint32_t ui32RxCount; CommTxMessageStruc Message; /* typedef struct CommRxMessage{ uint16_t messageId; uint16_t msgSize; char *Buff; }CommTxMessageStruc; */ initArray(1); #ifdef USE_USB SetCommunicationPath(isUSB); #else SetCommunicationPath(isUART); #endif //ui32RxCount = 0; CommunicationTxMsgQ = Mailbox_create(sizeof(CommTxMessageStruc), 10, NULL,NULL); /* Block while the device is NOT connected to the USB */ // Semaphore_pend(initConnectionSem, BIOS_WAIT_FOREVER); while(1) { Mailbox_pend(CommunicationTxMsgQ , &Message, BIOS_WAIT_FOREVER); switch (Message.messageId) { case 1: if (CommType == isUSB) USBCDCD_sendData(Message.Buff, Message.msgSize,10); else if (CommType == isUART) Uart_Tx(Message.Buff, Message.msgSize); free (Message.Buff); break; default: break; } } }