Statistics
| Branch: | Tag: | Revision:

amiro-blt / Target / Source / com.c @ 69661903

History | View | Annotate | Download (14.1 KB)

1
/************************************************************************************//**
2
* \file         Source\com.c
3
* \brief        Bootloader communication interface source file.
4
* \ingroup      Core
5
* \internal
6
*----------------------------------------------------------------------------------------
7
*                          C O P Y R I G H T
8
*----------------------------------------------------------------------------------------
9
*   Copyright (c) 2011  by Feaser    http://www.feaser.com    All rights reserved
10
*
11
*----------------------------------------------------------------------------------------
12
*                            L I C E N S E
13
*----------------------------------------------------------------------------------------
14
* This file is part of OpenBLT. OpenBLT is free software: you can redistribute it and/or
15
* modify it under the terms of the GNU General Public License as published by the Free
16
* Software Foundation, either version 3 of the License, or (at your option) any later
17
* version.
18
*
19
* OpenBLT is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
20
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
21
* PURPOSE. See the GNU General Public License for more details.
22
*
23
* You should have received a copy of the GNU General Public License along with OpenBLT.
24
* If not, see <http://www.gnu.org/licenses/>.
25
*
26
* A special exception to the GPL is included to allow you to distribute a combined work 
27
* that includes OpenBLT without being obliged to provide the source code for any 
28
* proprietary components. The exception text is included at the bottom of the license
29
* file <license.html>.
30
* 
31
* \endinternal
32
****************************************************************************************/
33

    
34
/****************************************************************************************
35
* Include files
36
****************************************************************************************/
37
#include "boot.h"                                /* bootloader generic header          */
38
#if (BOOT_COM_CAN_ENABLE > 0)
39
  #include "can.h"                                    /* can driver module             */
40
#endif
41
#if (BOOT_COM_UART_ENABLE > 0)
42
  #include "uart.h"                                   /* uart driver module            */
43
#endif
44
#if (BOOT_COM_USB_ENABLE > 0)
45
  #include "usb.h"                                    /* usb driver module             */
46
#endif
47
#if (BOOT_COM_NET_ENABLE > 0)
48
  #include "net.h"                                    /* tcp/ip driver module          */
49
#endif
50

    
51

    
52
#if (BOOT_COM_ENABLE > 0)
53
/****************************************************************************************
54
* Local data declarations
55
****************************************************************************************/
56
/** \brief Holds the communication interface of the currently active interface. */
57
static tComInterfaceId comActiveInterface = COM_IF_OTHER;
58
#if (BOOT_DEBUGGING_UART2_ENABLE > 0)
59
static void InitDebugData(void);
60
#endif
61

    
62
#if (BOOT_DEBUGGING_UART2_ENABLE > 0)
63
static blt_int8u debugDataRUart[8];
64
#endif
65

    
66
/************************************************************************************//**
67
** \brief     Initializes the communication module including the hardware needed for 
68
**            the communication.
69
** \return    none
70
**
71
****************************************************************************************/
72
void ComInit(void)
73
{
74
#if (BOOT_DEBUGGING_UART2_ENABLE > 0)
75
  InitDebugData();
76
#endif
77
  /* initialize the XCP communication protocol */
78
  XcpInit();
79
#if (BOOT_COM_CAN_ENABLE > 0)
80
  /* initialize the CAN controller */
81
  CanInit();
82
  /* set it as active */
83
  comActiveInterface = COM_IF_CAN;
84
#endif
85
#if (BOOT_COM_UART_ENABLE > 0)
86
  /* initialize the UART interface */
87
  UartInit();
88
  /* set it as active */
89
  comActiveInterface = COM_IF_UART;
90
#endif
91
#if (BOOT_COM_BLUETOOTH_UART_ENABLE > 0)
92
  /* initialize the UART interface */
93
  BluetoothUartInit();
94
  /* set it as active */
95
  comActiveInterface = COM_IF_BLUETOOTH;
96
#endif
97
#if (BOOT_COM_USB_ENABLE > 0)
98
  /* initialize the USB interface */
99
  UsbInit();
100
  /* set it as active */
101
  comActiveInterface = COM_IF_USB;
102
#endif
103
#if (BOOT_COM_NET_ENABLE > 0)
104
  /* initialize the TCP/IP interface */
105
  NetInit();
106
  /* set it as active */
107
  comActiveInterface = COM_IF_NET;
108
#endif
109
} /*** end of ComInit ***/
110

    
111
#if (BOOT_DEBUGGING_UART2_ENABLE > 0)
112
/************************************************************************************//**
113
** \brief     Initializes the debugging data.
114
** \return    none
115
**
116
****************************************************************************************/
117
static void InitDebugData(void) {
118
  debugDataRUart[0] = 'r';
119
  debugDataRUart[1] = 'U';
120
  debugDataRUart[2] = 'A';
121
  debugDataRUart[3] = 'R';
122
  debugDataRUart[4] = 'T';
123
  debugDataRUart[5] = ':';
124
  debugDataRUart[6] = ' ';
125
  debugDataRUart[7] = 10;
126
} /*** end of InitDebugData ***/
127
#endif
128

    
129

    
130
/************************************************************************************//**
131
** \brief     Updates the communication module by checking if new data was received 
132
**            and submitting the request to process newly received data.
133
** \return    none
134
**
135
****************************************************************************************/
136
void ComTask(void)
137
{
138
  blt_int16s messageLength;
139
  /* make xcpCtoReqPacket static for runtime efficiency */
140
  static unsigned char xcpCtoReqPacket[BOOT_COM_RX_MAX_DATA];
141
 
142
#if (BOOT_COM_CAN_ENABLE > 0)
143
  messageLength = (blt_int16s)CanReceivePacket(&xcpCtoReqPacket[0]);
144
  if (messageLength > 0)
145
  {
146
    /* make this the active interface */
147
    comActiveInterface = COM_IF_CAN;
148
    /* process packet */
149
  #if (BOOT_GATE_ENABLE > 0)
150
    XcpPacketReceived(&xcpCtoReqPacket[0], messageLength, BLT_FALSE);
151
  #else
152
    XcpPacketReceived(&xcpCtoReqPacket[0], messageLength);
153
  #endif /* BOOT_GATE_ENABLE > 0 */
154
  }
155
#endif
156
#if (BOOT_COM_UART_ENABLE > 0)
157
  messageLength = (blt_int16s)UartReceivePacket(&xcpCtoReqPacket[0]);
158
  if (messageLength > 0)
159
  {
160
    /* make this the active interface */
161
    comActiveInterface = COM_IF_UART;
162
#if (BOOT_DEBUGGING_UART2_ENABLE > 0)
163
    /* send debugging data */
164
    BuildData(&debugDataRUart, &xcpCtoReqPacket[0], messageLength);
165
#endif
166
    /* process packet */
167
  #if (BOOT_GATE_ENABLE > 0)
168
    XcpPacketReceived(&xcpCtoReqPacket[0], messageLength, BLT_FALSE);
169
  #else
170
    XcpPacketReceived(&xcpCtoReqPacket[0], messageLength);
171
  #endif /* BOOT_GATE_ENABLE > 0 */
172
  }
173
#endif
174
#if (BOOT_COM_BLUETOOTH_UART_ENABLE > 0)
175
  messageLength = (blt_int16s)BluetoothUartReceivePacket(&xcpCtoReqPacket[0]);
176
  if (messageLength > 0)
177
  {
178
    //UartTransmitPacket(&xcpCtoReqPacket[0], messageLength);
179
    //UartTransmitPacket("\n", 1);
180
    /* make this the active interface */
181
    comActiveInterface = COM_IF_BLUETOOTH;
182
    /* process packet */
183
  #if (BOOT_GATE_ENABLE > 0)
184
    XcpPacketReceived(&xcpCtoReqPacket[0], messageLength, BLT_FALSE);
185
  #else
186
    XcpPacketReceived(&xcpCtoReqPacket[0], messageLength);
187
  #endif /* BOOT_GATE_ENABLE > 0 */
188
//  } else {
189
//    UartTransmitPacket("-\n", 2);
190
  }
191
#endif
192
#if (BOOT_COM_USB_ENABLE > 0)
193
  messageLength = (blt_int16s)UsbReceivePacket(&xcpCtoReqPacket[0]);
194
  if (messageLength > 0)
195
  {
196
    /* make this the active interface */
197
    comActiveInterface = COM_IF_USB;
198
    /* process packet */
199
  #if (BOOT_GATE_ENABLE > 0)
200
    XcpPacketReceived(&xcpCtoReqPacket[0], messageLength, BLT_FALSE);
201
  #else
202
    XcpPacketReceived(&xcpCtoReqPacket[0], messageLength);
203
  #endif /* BOOT_GATE_ENABLE > 0 */
204
  }
205
#endif
206
/* net conversation not impemented with length yet! */
207

    
208
//#if (BOOT_COM_NET_ENABLE > 0)
209
//  if (NetReceivePacket(&xcpCtoReqPacket[0]) == BLT_TRUE)
210
//  {
211
//    /* make this the active interface */
212
//    comActiveInterface = COM_IF_NET;
213
//    /* process packet */
214
//    XcpPacketReceived(&xcpCtoReqPacket[0]);
215
//  }
216
//#endif
217

    
218
} /*** end of ComTask ***/
219

    
220

    
221
/************************************************************************************//**
222
** \brief     Releases the communication module.
223
** \return    none
224
**
225
****************************************************************************************/
226
void ComFree(void)
227
{
228
#if (BOOT_COM_USB_ENABLE > 0)
229
  /* disconnect the usb device from the usb host */
230
  UsbFree();
231
#endif
232
} /*** end of ComFree ***/
233

    
234

    
235
/************************************************************************************//**
236
** \brief     Transmits the packet using the xcp transport layer.
237
** \param     data Pointer to the byte buffer with packet data.
238
** \param     len  Number of data bytes that need to be transmitted.
239
** \return    none
240
**
241
****************************************************************************************/
242
void ComTransmitPacket(blt_int8u *data, blt_int16u len)
243
{
244
#if (BOOT_COM_CAN_ENABLE > 0)
245
  /* transmit the packet. note that len is limited to 8 in the plausibility check,
246
   * so cast is okay.
247
   */
248
  if (comActiveInterface == COM_IF_CAN)
249
  {
250
    CanTransmitPacket(data, (blt_int8u)len, 0);
251
  }
252
#endif
253
#if (BOOT_COM_UART_ENABLE > 0)
254
  /* transmit the packet. note that len is limited to 255 in the plausibility check,
255
   * so cast is okay.
256
   */
257
  if (comActiveInterface == COM_IF_UART)
258
  {
259
    UartTransmitPacket(data, (blt_int8u)len);
260
  }
261
#endif
262
#if (BOOT_COM_BLUETOOTH_UART_ENABLE > 0)
263
  /* transmit the packet. note that len is limited to 255 in the plausibility check,
264

265
   * so cast is okay.
266
   */
267
  if (comActiveInterface == COM_IF_BLUETOOTH)
268
  {
269
    BluetoothUartTransmitPacket(data, (blt_int8u)len);
270
  }
271
#endif
272
#if (BOOT_COM_USB_ENABLE > 0)
273
  /* transmit the packet */
274
  if (comActiveInterface == COM_IF_USB)
275
  {
276
    UsbTransmitPacket(data, len);
277
  }
278
#endif
279
#if (BOOT_COM_NET_ENABLE > 0)
280
  if (comActiveInterface == COM_IF_NET)
281
  {
282
    /* transmit the packet */
283
    NetTransmitPacket(data, len);
284
  }
285
#endif
286

    
287
  /* send signal that the packet was transmitted */
288
  XcpPacketTransmitted();
289
} /*** end of ComTransmitPacket ***/
290

    
291

    
292
/************************************************************************************//**
293
** \brief     Obtains the maximum number of bytes that can be received on the specified
294
**            communication interface.
295
** \return    Maximum number of bytes that can be received.
296
**
297
****************************************************************************************/
298
blt_int16u ComGetActiveInterfaceMaxRxLen(void)
299
{
300
  blt_int16u result;
301
  
302
  /* filter on communication interface identifier */
303
  switch (comActiveInterface)
304
  {
305
    case COM_IF_UART:
306
      result = BOOT_COM_UART_RX_MAX_DATA;
307
      break;
308

    
309
    case COM_IF_BLUETOOTH:
310
      result = BOOT_COM_UART_RX_MAX_DATA;
311
      break;
312

    
313
    case COM_IF_CAN:
314
      result = BOOT_COM_CAN_RX_MAX_DATA;
315
      break;
316

    
317
    case COM_IF_USB:
318
      result = BOOT_COM_USB_RX_MAX_DATA;
319
      break;
320

    
321
    case COM_IF_NET:
322
      result = BOOT_COM_NET_RX_MAX_DATA;
323
      break;
324
      
325
    default:
326
      result = BOOT_COM_RX_MAX_DATA;
327
      break;
328
  }
329
  
330
  return result;
331
} /*** end of ComGetActiveInterfaceMaxRxLen ***/
332

    
333

    
334
/************************************************************************************//**
335
** \brief     Obtains the maximum number of bytes that can be transmitted on the 
336
**            specified communication interface.
337
** \return    Maximum number of bytes that can be received.
338
**
339
****************************************************************************************/
340
blt_int16u ComGetActiveInterfaceMaxTxLen(void)
341
{
342
  blt_int16u result;
343
  
344
  /* filter on communication interface identifier */
345
  switch (comActiveInterface)
346
  {
347
    case COM_IF_UART:
348
      result = BOOT_COM_UART_TX_MAX_DATA;
349
      break;
350

    
351
    case COM_IF_BLUETOOTH:
352
      result = BOOT_COM_UART_TX_MAX_DATA;
353
      break;
354

    
355
    case COM_IF_CAN:
356
      result = BOOT_COM_CAN_TX_MAX_DATA;
357
      break;
358

    
359
    case COM_IF_USB:
360
      result = BOOT_COM_USB_TX_MAX_DATA;
361
      break;
362

    
363
    case COM_IF_NET:
364
      result = BOOT_COM_NET_TX_MAX_DATA;
365
      break;
366
      
367
    default:
368
      result = BOOT_COM_TX_MAX_DATA;
369
      break;
370
  }
371
  
372
  return result;
373
} /*** end of ComGetActiveInterfaceMaxTxLen ***/
374

    
375

    
376
/************************************************************************************//**
377
** \brief     This function obtains the XCP connection state.
378
** \return    BLT_TRUE when an XCP connection is established, BLT_FALSE otherwise.
379
**
380
****************************************************************************************/
381
blt_bool ComIsConnected(void)
382
{
383
  return XcpIsConnected();
384
} /*** end of ComIsConnected ***/
385

    
386

    
387
#if (BOOTLOADER_OF_MAIN_DEVICE > 0)
388
/************************************************************************************//**
389
** \brief     This function obtains the XCP connection state.
390
** \return    BLT_TRUE when an XCP connection to main was established, BLT_FALSE otherwise.
391
**
392
****************************************************************************************/
393
blt_bool ComWasConnectedToMain(void)
394
{
395
  return XcpWasConnectedToMain();
396
} /*** end of ComWasConnectedToMain ***/
397
#endif /* BOOTLOADER_OF_MAIN_DEVICE > 0 */
398

    
399

    
400
#endif /* BOOT_COM_ENABLE > 0 */
401

    
402

    
403

    
404

    
405

    
406

    
407
#if (BOOT_COM_UART_ENABLE > 0)
408

    
409
/************************************************************************************//**
410
** \brief     This function transmitts the packet direct with UART.
411
** \param     data Pointer to the byte buffer with packet data.
412
** \param     len  Number of data bytes that need to be transmitted.
413
** \return    none.
414
**
415
****************************************************************************************/
416
void ComTransmitPacketDirect(blt_int8u *data, blt_int8u len) {
417
#if (BOOT_COM_BLUETOOTH_UART_ENABLE > 0)
418
  if (comActiveInterface == COM_IF_UART) {
419
#endif
420
    UartTransmitPacket(data, len);
421
#if (BOOT_COM_BLUETOOTH_UART_ENABLE > 0)
422
  } else {
423
    BluetoothUartTransmitPacket(data, len);
424
  }
425
#endif
426
  XcpPacketTransmitted();
427
} /*** end of ComTransmitPacketDirect ***/
428

    
429
#endif
430

    
431
/*********************************** end of com.c **************************************/