aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Communication/CommunicationTask.c
blob: b6841ef7e594bc9403a71dd246e20a04684a8c4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * 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[4000];
SerialBuffer inBuffer;
uint32_t initArray(size_t initialSize) {
  SerialBuffer *a = &inBuffer;
  if (initialSize >= 4000) 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;
}

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;
        }
    }

}

bool CommunicationTaskSendMessage(char* buffer,size_t length)
{
    CommTxMessageStruc Message;
    CommTxMsgCounter++;

    Message.messageId = 1;
    Message.msgSize = length;
    Message.Buff = buffer;
    if (CommunicationTxMsgQ != NULL)
        return Mailbox_post(CommunicationTxMsgQ , &Message, BIOS_NO_WAIT);

    return false;
}
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 Communication methods (USB or Bluetooth).
 ******************************************************************************/
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;
        }
    }

}