STSW-STLKT01: Projects/SensorTile/Applications/DataLog/Src/usbd_cdc_interface.c Source File

STSW-STLKT01

usbd_cdc_interface.c
Go to the documentation of this file.
1 
48 /* Includes ------------------------------------------------------------------*/
49 #include "usbd_cdc_interface.h"
50 #include "main.h"
51 
52 /* Private typedef -----------------------------------------------------------*/
53 /* Private define ------------------------------------------------------------*/
54 #define APP_RX_DATA_SIZE 2048
55 #define APP_TX_DATA_SIZE 2048
56 
57 /* Private macro -------------------------------------------------------------*/
58 /* Private variables ---------------------------------------------------------*/
59 USBD_CDC_LineCodingTypeDef LineCoding =
60  {
61  115200, /* baud rate*/
62  0x00, /* stop bits-1*/
63  0x00, /* parity - none*/
64  0x08 /* nb. of bits 8*/
65  };
66 
67 uint8_t UserRxBuffer[APP_RX_DATA_SIZE];/* Received Data over USB are stored in this buffer */
68 uint8_t UserTxBuffer[APP_TX_DATA_SIZE];/* Received Data over UART (CDC interface) are stored in this buffer */
69 uint32_t BuffLength;
70 uint32_t UserTxBufPtrIn = 0;/* Increment this pointer or roll it back to
71  start address when data are received over USART */
72 uint32_t UserTxBufPtrOut = 0; /* Increment this pointer or roll it back to
73  start address when data are sent over USB */
74 
75 volatile uint8_t USB_RxBuffer[USB_RxBufferDim];
76 volatile uint16_t USB_RxBufferStart_idx = 0;
77 
78 /* TIM handler declaration */
79 TIM_HandleTypeDef TimHandle;
80 /* USB handler declaration */
81 extern USBD_HandleTypeDef USBD_Device;
82 
83 /* Private function prototypes -----------------------------------------------*/
84 static int8_t CDC_Itf_Init (void);
85 static int8_t CDC_Itf_DeInit (void);
86 static int8_t CDC_Itf_Control (uint8_t cmd, uint8_t* pbuf, uint16_t length);
87 static int8_t CDC_Itf_Receive (uint8_t* pbuf, uint32_t *Len);
88 
89 static void Error_Handler(void);
90 static void TIM_Config(void);
91 
92 USBD_CDC_ItfTypeDef USBD_CDC_fops =
93 {
98 };
99 
100 /* Private functions ---------------------------------------------------------*/
101 
108 static int8_t CDC_Itf_Init(void)
109 {
110  /*##-2- Enable TIM peripherals Clock #######################################*/
111  TIMx_CLK_ENABLE();
112 
113  /*##-3- Configure the NVIC for TIMx ########################################*/
114  /* Set Interrupt Group Priority */
115  HAL_NVIC_SetPriority(TIMx_IRQn, 0x6, 0);
116 
117  /* Enable the TIMx global Interrupt */
118  HAL_NVIC_EnableIRQ(TIMx_IRQn);
119 
120  /*##-3- Configure the TIM Base generation #################################*/
121  TIM_Config();
122 
123  /*##-4- Start the TIM Base generation in interrupt mode ####################*/
124  /* Start Channel1 */
125  if(HAL_TIM_Base_Start_IT(&TimHandle) != HAL_OK)
126  {
127  /* Starting Error */
128  Error_Handler();
129  }
130 
131  /*##-5- Set Application Buffers ############################################*/
132  USBD_CDC_SetTxBuffer(&USBD_Device, UserTxBuffer, 0);
133  USBD_CDC_SetRxBuffer(&USBD_Device, UserRxBuffer);
134 
135  return (USBD_OK);
136 }
137 
144 static int8_t CDC_Itf_DeInit(void)
145 {
146  return (USBD_OK);
147 }
148 
149 
158 static int8_t CDC_Itf_Control (uint8_t cmd, uint8_t* pbuf, uint16_t length)
159 {
160  switch (cmd)
161  {
162  case CDC_SEND_ENCAPSULATED_COMMAND:
163  /* Add your code here */
164  break;
165 
166  case CDC_GET_ENCAPSULATED_RESPONSE:
167  /* Add your code here */
168  break;
169 
170  case CDC_SET_COMM_FEATURE:
171  /* Add your code here */
172  break;
173 
174  case CDC_GET_COMM_FEATURE:
175  /* Add your code here */
176  break;
177 
178  case CDC_CLEAR_COMM_FEATURE:
179  /* Add your code here */
180  break;
181 
182  case CDC_SET_LINE_CODING:
183  LineCoding.bitrate = (uint32_t)(pbuf[0] | (pbuf[1] << 8) |\
184  (pbuf[2] << 16) | (pbuf[3] << 24));
185  LineCoding.format = pbuf[4];
186  LineCoding.paritytype = pbuf[5];
187  LineCoding.datatype = pbuf[6];
188 
189  /* Set the new configuration */
190 // ComPort_Config();
191  break;
192 
193  case CDC_GET_LINE_CODING:
194  pbuf[0] = (uint8_t)(LineCoding.bitrate);
195  pbuf[1] = (uint8_t)(LineCoding.bitrate >> 8);
196  pbuf[2] = (uint8_t)(LineCoding.bitrate >> 16);
197  pbuf[3] = (uint8_t)(LineCoding.bitrate >> 24);
198  pbuf[4] = LineCoding.format;
199  pbuf[5] = LineCoding.paritytype;
200  pbuf[6] = LineCoding.datatype;
201  break;
202 
203  case CDC_SET_CONTROL_LINE_STATE:
204  /* Add your code here */
205  break;
206 
207  case CDC_SEND_BREAK:
208  /* Add your code here */
209  break;
210 
211  default:
212  break;
213  }
214 
215  return (USBD_OK);
216 }
217 
224 uint8_t CDC_Fill_Buffer(uint8_t* Buf, uint32_t TotalLen)
225 {
226  uint16_t i;
227 
228  for (i = 0; i < TotalLen; i++)
229  {
230  UserTxBuffer[UserTxBufPtrIn] = Buf[i];
231  UserTxBufPtrIn = (UserTxBufPtrIn + 1) % APP_RX_DATA_SIZE;
232  }
233  return (USBD_OK);
234 }
235 
241 void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
242 {
243  uint32_t buffptr;
244  uint32_t buffsize;
245 
246  if(UserTxBufPtrOut != UserTxBufPtrIn)
247  {
248  if(UserTxBufPtrOut > UserTxBufPtrIn) /* Rollback */
249  {
250  buffsize = APP_RX_DATA_SIZE - UserTxBufPtrOut;
251  }
252  else
253  {
254  buffsize = UserTxBufPtrIn - UserTxBufPtrOut;
255  }
256 
257  buffptr = UserTxBufPtrOut;
258 
259  USBD_CDC_SetTxBuffer(&USBD_Device, (uint8_t*)&UserTxBuffer[buffptr], buffsize);
260 
261  if(USBD_CDC_TransmitPacket(&USBD_Device) == USBD_OK)
262  {
263  UserTxBufPtrOut += buffsize;
264  if (UserTxBufPtrOut == APP_RX_DATA_SIZE)
265  {
266  UserTxBufPtrOut = 0;
267  }
268  }
269  }
270 }
271 
272 
281 static int8_t CDC_Itf_Receive(uint8_t* Buf, uint32_t *Len)
282 {
283 // uint16_t numByteToCopy;
284 // if(((USB_RxBufferStart_idx) + (uint16_t)*Len) > USB_RxBufferDim)
285 // {
286 // numByteToCopy = USB_RxBufferDim - (USB_RxBufferStart_idx);
287 // uint16_t Buf_idx = 0;
288 // memcpy((uint8_t*)&USB_RxBuffer[USB_RxBufferStart_idx], (uint8_t*)&Buf[Buf_idx], numByteToCopy);
289 // USB_RxBufferStart_idx = 0;
290 // Buf_idx = numByteToCopy;
291 // numByteToCopy = (uint16_t)(*Len - numByteToCopy);
292 // memcpy((uint8_t*)&USB_RxBuffer[USB_RxBufferStart_idx], (uint8_t*)&Buf[Buf_idx], numByteToCopy);
293 // USB_RxBufferStart_idx = numByteToCopy;
294 // }
295 // else
296 // {
297 // numByteToCopy = (uint16_t) * Len;
298 // memcpy((uint8_t*)&USB_RxBuffer[USB_RxBufferStart_idx], (uint8_t*)&Buf[0], numByteToCopy);
299 // USB_RxBufferStart_idx = USB_RxBufferStart_idx + numByteToCopy;
300 // if(USB_RxBufferStart_idx == USB_RxBufferDim)
301 // USB_RxBufferStart_idx = 0;
302 // }
303 //
304 // /* Initiate next USB packet transfer */
305 // USBD_CDC_ReceivePacket(&USBD_Device);
306  return (USBD_OK);
307 }
308 
309 
315 static void TIM_Config(void)
316 {
317  /* Set TIMx instance */
318  TimHandle.Instance = TIMx;
319 
320  /* Initialize TIM3 peripheral as follow:
321  + Period = 10000 - 1
322  + Prescaler = ((SystemCoreClock/2)/10000) - 1
323  + ClockDivision = 0
324  + Counter direction = Up
325  */
326  TimHandle.Init.Period = (CDC_POLLING_INTERVAL*1000) - 1;
327  TimHandle.Init.Prescaler = 80-1;
328  TimHandle.Init.ClockDivision = 0;
329  TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
330  if(HAL_TIM_Base_Init(&TimHandle) != HAL_OK)
331  {
332  /* Initialization Error */
333  Error_Handler();
334  }
335 }
336 
337 
343 static void Error_Handler(void)
344 {
345  /* Add your own code here */
346 }
347 
348 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
uint8_t USBD_CDC_SetTxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff, uint16_t length)
USBD_CDC_SetTxBuffer.
Definition: usbd_cdc.c:810
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
TIM period elapsed callback.
uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev)
USBD_CDC_DataOut Data received on non-control Out endpoint.
Definition: usbd_cdc.c:846
uint8_t USBD_CDC_SetRxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff)
USBD_CDC_SetRxBuffer.
Definition: usbd_cdc.c:829
static int8_t CDC_Itf_DeInit(void)
CDC_Itf_DeInit DeInitializes the CDC media low layer.
static int8_t CDC_Itf_Control(uint8_t cmd, uint8_t *pbuf, uint16_t length)
CDC_Itf_Control Manage the CDC class requests.
static void TIM_Config(void)
TIM_Config: Configure TIMx timer.
static int8_t CDC_Itf_Init(void)
CDC_Itf_Init Initializes the CDC media low layer.
uint8_t CDC_Fill_Buffer(uint8_t *Buf, uint32_t TotalLen)
Fill the usb tx buffer.
static void Error_Handler(void)
This function is executed in case of error occurrence.
Header for usbd_cdc_interface.c file.
static int8_t CDC_Itf_Receive(uint8_t *pbuf, uint32_t *Len)
CDC_Itf_DataRx Data received over USB OUT endpoint are sent over CDC interface through this function...
Generated by   doxygen 1.8.13