Subversion Repositories DashDisplay

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 mjames 1
/**
2
  ******************************************************************************
3
  * @file           : usbd_cdc_if.c
4
  * @brief          :
5
  ******************************************************************************
6
  * COPYRIGHT(c) 2016 STMicroelectronics
7
  *
8
  * Redistribution and use in source and binary forms, with or without modification,
9
  * are permitted provided that the following conditions are met:
10
  * 1. Redistributions of source code must retain the above copyright notice,
11
  * this list of conditions and the following disclaimer.
12
  * 2. Redistributions in binary form must reproduce the above copyright notice,
13
  * this list of conditions and the following disclaimer in the documentation
14
  * and/or other materials provided with the distribution.
15
  * 3. Neither the name of STMicroelectronics nor the names of its contributors
16
  * may be used to endorse or promote products derived from this software
17
  * without specific prior written permission.
18
  *
19
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
  *
30
  ******************************************************************************
31
*/
32
 
33
/* Includes ------------------------------------------------------------------*/
34
#include "usbd_cdc_if.h"
35
/* USER CODE BEGIN INCLUDE */
36
#include "serial.h"
37
/* USER CODE END INCLUDE */
38
 
39
/** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
40
  * @{
41
  */
42
 
43
/** @defgroup USBD_CDC
44
  * @brief usbd core module
45
  * @{
46
  */
47
 
48
/** @defgroup USBD_CDC_Private_TypesDefinitions
49
  * @{
50
  */
51
/* USER CODE BEGIN PRIVATE_TYPES */
52
/* USER CODE END PRIVATE_TYPES */
53
/**
54
  * @}
55
  */
56
 
57
/** @defgroup USBD_CDC_Private_Defines
58
  * @{
59
  */
60
/* USER CODE BEGIN PRIVATE_DEFINES */
61
/* Define size for the receive and transmit buffer over CDC */
62
/* It's up to user to redefine and/or remove those define */
63
#define APP_RX_DATA_SIZE  4
64
#define APP_TX_DATA_SIZE  4
65
/* USER CODE END PRIVATE_DEFINES */
66
/**
67
  * @}
68
  */
69
 
70
/** @defgroup USBD_CDC_Private_Macros
71
  * @{
72
  */
73
/* USER CODE BEGIN PRIVATE_MACRO */
74
/* USER CODE END PRIVATE_MACRO */
75
 
76
/**
77
  * @}
78
  */
79
 
80
/** @defgroup USBD_CDC_Private_Variables
81
  * @{
82
  */
83
/* Create buffer for reception and transmission           */
84
/* It's up to user to redefine and/or remove those define */
85
/* Received Data over USB are stored in this buffer       */
86
uint8_t UserRxBufferFS[APP_RX_DATA_SIZE];
87
 
88
/* Send Data over USB CDC are stored in this buffer       */
89
uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];
90
 
91
/* USB handler declaration */
92
/* Handle for USB Full Speed IP */
93
  USBD_HandleTypeDef  *hUsbDevice_0;
94
/* USER CODE BEGIN PRIVATE_VARIABLES */
95
/* USER CODE END PRIVATE_VARIABLES */
96
 
97
/**
98
  * @}
99
  */
100
 
101
/** @defgroup USBD_CDC_IF_Exported_Variables
102
  * @{
103
  */
104
  extern USBD_HandleTypeDef hUsbDeviceFS;
105
/* USER CODE BEGIN EXPORTED_VARIABLES */
106
/* USER CODE END EXPORTED_VARIABLES */
107
 
108
/**
109
  * @}
110
  */
111
 
112
/** @defgroup USBD_CDC_Private_FunctionPrototypes
113
  * @{
114
  */
115
static int8_t CDC_Init_FS     (void);
116
static int8_t CDC_DeInit_FS   (void);
117
static int8_t CDC_Control_FS  (uint8_t cmd, uint8_t* pbuf, uint16_t length);
118
static int8_t CDC_Receive_FS  (uint8_t* pbuf, uint32_t *Len);
119
 
120
/* USER CODE BEGIN PRIVATE_FUNCTIONS_DECLARATION */
121
/* USER CODE END PRIVATE_FUNCTIONS_DECLARATION */
122
 
123
/**
124
  * @}
125
  */
126
 
127
USBD_CDC_ItfTypeDef USBD_Interface_fops_FS =
128
{
129
  CDC_Init_FS,
130
  CDC_DeInit_FS,
131
  CDC_Control_FS,  
132
  CDC_Receive_FS
133
};
134
 
135
/* Private functions ---------------------------------------------------------*/
136
/**
137
  * @brief  CDC_Init_FS
138
  *         Initializes the CDC media low layer over the FS USB IP
139
  * @param  None
140
  * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
141
  */
142
static int8_t CDC_Init_FS(void)
143
{
144
  hUsbDevice_0 = &hUsbDeviceFS;
145
  /* USER CODE BEGIN 3 */
146
  /* Set Application Buffers */
147
  USBD_CDC_SetTxBuffer(hUsbDevice_0, UserTxBufferFS, 0);
148
  USBD_CDC_SetRxBuffer(hUsbDevice_0, UserRxBufferFS);
149
  return (USBD_OK);
150
  /* USER CODE END 3 */
151
}
152
 
153
/**
154
  * @brief  CDC_DeInit_FS
155
  *         DeInitializes the CDC media low layer
156
  * @param  None
157
  * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
158
  */
159
static int8_t CDC_DeInit_FS(void)
160
{
161
  /* USER CODE BEGIN 4 */
162
  return (USBD_OK);
163
  /* USER CODE END 4 */
164
}
165
 
166
/**
167
  * @brief  CDC_Control_FS
168
  *         Manage the CDC class requests
169
  * @param  cmd: Command code            
170
  * @param  pbuf: Buffer containing command data (request parameters)
171
  * @param  length: Number of data to be sent (in bytes)
172
  * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
173
  */
174
static int8_t CDC_Control_FS  (uint8_t cmd, uint8_t* pbuf, uint16_t length)
175
{
176
  /* USER CODE BEGIN 5 */
177
  switch (cmd)
178
  {
179
  case CDC_SEND_ENCAPSULATED_COMMAND:
180
 
181
    break;
182
 
183
  case CDC_GET_ENCAPSULATED_RESPONSE:
184
 
185
    break;
186
 
187
  case CDC_SET_COMM_FEATURE:
188
 
189
    break;
190
 
191
  case CDC_GET_COMM_FEATURE:
192
 
193
    break;
194
 
195
  case CDC_CLEAR_COMM_FEATURE:
196
 
197
    break;
198
 
199
  /*******************************************************************************/
200
  /* Line Coding Structure                                                       */
201
  /*-----------------------------------------------------------------------------*/
202
  /* Offset | Field       | Size | Value  | Description                          */
203
  /* 0      | dwDTERate   |   4  | Number |Data terminal rate, in bits per second*/
204
  /* 4      | bCharFormat |   1  | Number | Stop bits                            */
205
  /*                                        0 - 1 Stop bit                       */
206
  /*                                        1 - 1.5 Stop bits                    */
207
  /*                                        2 - 2 Stop bits                      */
208
  /* 5      | bParityType |  1   | Number | Parity                               */
209
  /*                                        0 - None                             */
210
  /*                                        1 - Odd                              */
211
  /*                                        2 - Even                             */
212
  /*                                        3 - Mark                             */
213
  /*                                        4 - Space                            */
214
  /* 6      | bDataBits  |   1   | Number Data bits (5, 6, 7, 8 or 16).          */
215
  /*******************************************************************************/
216
  case CDC_SET_LINE_CODING:  
217
  {
218
          uint32_t rate = *((uint32_t *)pbuf);
219
          if(rate > 0)
220
          {
221
                hirda3.Init.BaudRate = rate; // match apparent baud rate seen
222
            HAL_IRDA_Init(&hirda3);
223
 
224
          }
225
 
226
  }
227
    break;
228
 
229
  case CDC_GET_LINE_CODING:    
230
 
231
    break;
232
 
233
  case CDC_SET_CONTROL_LINE_STATE:
234
 
235
    break;
236
 
237
  case CDC_SEND_BREAK:
238
 
239
    break;    
240
 
241
  default:
242
    break;
243
  }
244
 
245
  return (USBD_OK);
246
  /* USER CODE END 5 */
247
}
248
 
249
/**
250
  * @brief  CDC_Receive_FS
251
  *         Data received over USB OUT endpoint are sent over CDC interface
252
  *         through this function.
253
  *          
254
  *         @note
255
  *         This function will block any OUT packet reception on USB endpoint
256
  *         untill exiting this function. If you exit this function before transfer
257
  *         is complete on CDC interface (ie. using DMA controller) it will result
258
  *         in receiving more data while previous ones are still not sent.
259
  *                
260
  * @param  Buf: Buffer of data to be received
261
  * @param  Len: Number of data received (in bytes)
262
  * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
263
  */
264
static int8_t CDC_Receive_FS (uint8_t* Buf, uint32_t *Len)
265
{
266
  /* USER CODE BEGIN 6 */
267
 
268
                int i = 0;
269
 
270
            for (i = 0; i < *Len; i++)
271
            {
272
                PutCharSerial(&uc3,Buf[i]);
273
            }
274
#if 0
275
            {
276
            UserTxBufferFS[i] = UserRxBufferFS[i];
277
            }
278
            USBD_CDC_SetTxBuffer(&hUsbDeviceFS, &UserTxBufferFS[0], *Len);
279
            USBD_CDC_TransmitPacket(&hUsbDeviceFS);
280
 
281
#endif
282
            USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &UserRxBufferFS[0]);
283
            USBD_CDC_ReceivePacket(&hUsbDeviceFS);
284
 
285
 
286
  return (USBD_OK);
287
  /* USER CODE END 6 */
288
}
289
 
290
/**
291
  * @brief  CDC_Transmit_FS
292
  *         Data send over USB IN endpoint are sent over CDC interface
293
  *         through this function.          
294
  *         @note
295
  *        
296
  *                
297
  * @param  Buf: Buffer of data to be send
298
  * @param  Len: Number of data to be send (in bytes)
299
  * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY
300
  */
301
uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)
302
{
303
  uint8_t result = USBD_OK;
304
  /* USER CODE BEGIN 7 */
305
  if(hUsbDevice_0)
306
  {
307
  USBD_CDC_SetTxBuffer(hUsbDevice_0, Buf, Len);  
308
  result = USBD_CDC_TransmitPacket(hUsbDevice_0);
309
  }
310
  else
311
  {
312
          result = USBD_BUSY;
313
  }
314
  /* USER CODE END 7 */
315
  return result;
316
}
317
 
318
/* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */
319
int16_t CDC_Poll_UART(void)
320
 {
321
        int16_t chars = SerialCharsReceived(&uc3);
322
        int16_t i;
323
        if (chars > APP_TX_DATA_SIZE)
324
                chars = APP_TX_DATA_SIZE;
325
 
326
        for (i = 0; i < chars; i++) {
327
                uint8_t c;
328
                c = GetCharSerial(&uc3);
329
 
330
                UserTxBufferFS[i] = c;
331
 
332
        }
333
 
334
        if(chars) /* do not send zero chars as this looks like end of transfer and will eventually kill the link */
335
        {
336
                CDC_Transmit_FS(UserTxBufferFS, chars);
337
        }
338
        return chars;
339
}
340
 
341
 
342
/* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */
343
 
344
/**
345
  * @}
346
  */
347
 
348
/**
349
  * @}
350
  */
351
 
352
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
353