aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Communication/CommunicationTask.c
blob: 18a0153d5dfbc53460b3b3545c09d420a831b15c (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * 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;
        }
    }

}