summaryrefslogtreecommitdiff
path: root/digital/zigbit/bitcloud/stack/Components/HAL/drivers/USBClasses/VCP/src/vcpCdcProtocol.c
blob: 6ea6355749df3761db510e80efa3b4de39e8c81d (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/****************************************************************************//**
  \file vcpCdcProtocol.h

  \brief Implementation of communication device protocol command.

  \author
    Atmel Corporation: http://www.atmel.com \n
    Support email: avr@atmel.com

  Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
  Licensed under Atmel's Limited License Agreement (BitCloudTM).

  \internal
  History:
    11/09/08 A. Khromykh - Created
*******************************************************************************/
/******************************************************************************
                   Includes section
******************************************************************************/
#include <vcpCdcProtocol.h>
#include <usbEnumeration.h>
#include <usart.h>
#include <usb.h>

/******************************************************************************
                   Define(s) section
******************************************************************************/
// virtual uart speed
#define VU_1200   0x4B0
#define VU_2400   0x960
#define VU_4800   0x12C0
#define VU_9600   0x2580
#define VU_19200  0x4B00
#define VU_38400  0x9600
#define VU_57600  0xE100
#define VU_115200 0x1C200

// char size
#define VU_1STOPBIT    0
#define VU_1d5STOPBITS 1
#define VU_2STOPBITS   2

// parity
#define VU_NONE  0
#define VU_ODD   1
#define VU_EVEN  2
#define VU_MARK  3
#define VU_SPACE 4

// data bits number
#define VU_5DATABITS  5
#define VU_6DATABITS  6
#define VU_7DATABITS  7
#define VU_8DATABITS  8
#define VU_16DATABITS 16

/******************************************************************************
                   External global variables section
******************************************************************************/
extern HAL_UsartDescriptor_t *vcpPointDescrip;

/******************************************************************************
                   Global variables section
******************************************************************************/
UsbCdcRequest_t  request;
UsbCdcResponse_t response;

/******************************************************************************
                   Implementations section
******************************************************************************/
/******************************************************************************
Get baud rate meaning for cdc response.

Parameters:
  baudRate - virtual uart baudRate
******************************************************************************/
void vcpGetBaudRate(uint32_t baudRate)
{
  (void)baudRate;
  response.getLineCoding.dwDTERate = VU_115200;
}

/******************************************************************************
Get number of stop bits meaning for cdc response.

Parameters:
  stopBits - virtual uart stop bits
******************************************************************************/
void vcpGetStopBits(uint8_t stopBits)
{
  (void)stopBits;
  response.getLineCoding.bCharFormat = VU_1STOPBIT;
}

/******************************************************************************
Get parity meaning for cdc response.

Parameters:
  parity - virtual uart parity
******************************************************************************/
void vcpGetParity(uint8_t parity)
{
  (void)parity;
  response.getLineCoding.bParityType = VU_NONE;
}

/******************************************************************************
Get data length meaning for cdc response.

Parameters:
  dataLength - virtual uart data length
******************************************************************************/
void vcpGetDataLength(uint8_t dataLength)
{
  (void)dataLength;
  response.getLineCoding.bDataBits = VU_8DATABITS;
}

/******************************************************************************
Get virtual uart data and send answer to host.
******************************************************************************/
void vcpResponseGetLineCoding(void)
{
  vcpGetBaudRate(vcpPointDescrip->baudrate);
  vcpGetStopBits(vcpPointDescrip->stopbits);
  vcpGetParity(vcpPointDescrip->parity);
  vcpGetDataLength(vcpPointDescrip->dataLength);

  HAL_UsbWrite(0, (void *)&response, sizeof(GetLineCodingResponse_t), 0, 0);
}

/******************************************************************************
Set baud rate meaning to virtual port.

Parameters:
  baudRate - virtual uart baud rate
******************************************************************************/
void vcpSetBaudRate(uint32_t baudRate)
{
  (void)baudRate;
}

/******************************************************************************
Set number stop bits to virtual port.

Parameters:
  stopBits - virtual uart stop bits
******************************************************************************/
void vcpSetStopBits(uint8_t stopBits)
{
  (void)stopBits;
}

/******************************************************************************
Set parity meaning to virtual port.

Parameters:
  parity - virtual uart parity
******************************************************************************/
void vcpSetParity(uint8_t parity)
{
  (void)parity;
}

/******************************************************************************
Set data length to virtual port.

Parameters:
  dataLength - virtual uart data length
******************************************************************************/
void vcpSetDataLength(uint8_t dataLength)
{
  (void)dataLength;
}

/******************************************************************************
Set virtual uart data and send response to host.
******************************************************************************/
void vcpResponseSetLineCoding(void)
{
  vcpSetBaudRate(response.getLineCoding.dwDTERate);
  vcpSetStopBits(response.getLineCoding.bCharFormat);
  vcpSetParity(response.getLineCoding.bParityType);
  vcpSetDataLength(response.getLineCoding.bDataBits);

#if defined(AT91SAM7X256) || defined(AT91SAM3S4C)
  sendZLP();
#endif
}

/******************************************************************************
communication device request handler

Parameters:
  data - pointer to host's request
******************************************************************************/
void vcpRequestHandler(uint8_t *data)
{
  UsbCdcRequest_t *pRequest = NULL;

  pRequest = (UsbCdcRequest_t *)data;
  if (NULL == pRequest)
    return;

  // Check request code
  switch (pRequest->request.bRequest)
  {
    case SET_LINE_CODING:
        HAL_UsbRead(0, (void *)&response, sizeof(GetLineCodingResponse_t), (TransferCallback_t)vcpResponseSetLineCoding, 0);
      break;
    case GET_LINE_CODING:
        vcpResponseGetLineCoding();
      break;
    case SET_CONTROL_LINE_STATE:
        //vcpReadDataFromSetControlLineState(pRequest->wValue); // possible in the future
        #if defined(AT91SAM7X256) || defined(AT91SAM3S4C)
          sendZLP();
        #endif
      break;
    default:
        HAL_Stall(0);
      break;
  }
}

//eof vcpCdcProtocol.c