-
Notifications
You must be signed in to change notification settings - Fork 14
/
hw_config.c
429 lines (354 loc) · 11.3 KB
/
hw_config.c
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/**
******************************************************************************
* @file hw_config.c
* @author MCD Application Team
* @version V4.0.0
* @date 21-January-2013
* @brief Hardware Configuration & Setup
******************************************************************************
* @attention
*
* <h2><center>© COPYRIGHT 2013 STMicroelectronics</center></h2>
*
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.st.com/software_license_agreement_liberty_v2
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************
*/
#include "stm32_it.h"
#include "usb_lib.h"
#include "usb_prop.h"
#include "usb_desc.h"
#include "hw_config.h"
#include "usb_pwr.h"
USART_InitTypeDef USART_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
uint8_t USART_Rx_Buffer [USART_RX_DATA_SIZE];
uint32_t USART_Rx_ptr_in = 0;
uint32_t USART_Rx_ptr_out = 0;
uint32_t USART_Rx_length = 0;
uint8_t USB_Tx_State = 0;
extern LINE_CODING linecoding;
typedef enum
{
COM1 = 0,
COM2 = 1
} COM_TypeDef;
#define COMn 2
USART_TypeDef* COM_USART[COMn] = {EVAL_COM1, EVAL_COM1};
GPIO_TypeDef* COM_TX_PORT[COMn] = {EVAL_COM1_TX_GPIO_PORT, EVAL_COM1_TX_GPIO_PORT};
GPIO_TypeDef* COM_RX_PORT[COMn] = {EVAL_COM1_RX_GPIO_PORT, EVAL_COM1_RX_GPIO_PORT};
const uint32_t COM_USART_CLK[COMn] = {EVAL_COM1_CLK, EVAL_COM1_CLK};
const uint32_t COM_TX_PORT_CLK[COMn] = {EVAL_COM1_TX_GPIO_CLK, EVAL_COM1_TX_GPIO_CLK};
const uint32_t COM_RX_PORT_CLK[COMn] = {EVAL_COM1_RX_GPIO_CLK, EVAL_COM1_RX_GPIO_CLK};
const uint16_t COM_TX_PIN[COMn] = {EVAL_COM1_TX_PIN, EVAL_COM1_TX_PIN};
const uint16_t COM_RX_PIN[COMn] = {EVAL_COM1_RX_PIN, EVAL_COM1_RX_PIN};
static void intToUnicode(uint32_t value, uint8_t *buffer, uint8_t length)
{
uint8_t i = 0;
for (i = 0; i < length; i++)
{
if ((value >> 28) < 10)
{
buffer[i << 1] = (value >> 28) + '0';
}
else
{
buffer[i << 1] = (value >> 28) + 'A' - 10;
}
value = value << 4;
buffer[(i << 1) + 1] = 0;
}
}
void STM_EVAL_COMInit(COM_TypeDef COM, USART_InitTypeDef* USART_InitStruct)
{
if (COM != COM1)
return;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIO clock */
RCC_APB2PeriphClockCmd(COM_TX_PORT_CLK[COM] | COM_RX_PORT_CLK[COM] | RCC_APB2Periph_AFIO, ENABLE);
if (COM == COM1)
{
RCC_APB2PeriphClockCmd(COM_USART_CLK[COM], ENABLE);
}
else
{
/* Enable the USART2 Pins Software Remapping */
//GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
//RCC_APB1PeriphClockCmd(COM_USART_CLK[COM], ENABLE);
}
/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = COM_TX_PIN[COM];
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(COM_TX_PORT[COM], &GPIO_InitStructure);
/* Configure USART Rx as input floating */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = COM_RX_PIN[COM];
GPIO_Init(COM_RX_PORT[COM], &GPIO_InitStructure);
/* USART configuration */
USART_Init(COM_USART[COM], USART_InitStruct);
/* Enable USART */
USART_Cmd(COM_USART[COM], ENABLE);
}
void configureUsbSystem(void)
{
// Configure the EXTI line 18 connected internally to the USB IP
EXTI_ClearITPendingBit(EXTI_Line18);
EXTI_InitStructure.EXTI_Line = EXTI_Line18;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);
}
void Enter_LowPowerMode(void)
{
bDeviceState = SUSPENDED;
}
void Leave_LowPowerMode(void)
{
DEVICE_INFO *pInfo = &Device_Info;
if (pInfo->Current_Configuration != 0)
{
bDeviceState = CONFIGURED;
}
else
{
bDeviceState = ATTACHED;
}
SystemInit();
}
void configureUsbInterrupts(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
// 2 bit for pre-emption priority, 2 bits for subpriority
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// Enable the USB Wake-up interrupt
NVIC_InitStructure.NVIC_IRQChannel = USBWakeUp_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_Init(&NVIC_InitStructure);
// Enable USART Interrupt
NVIC_InitStructure.NVIC_IRQChannel = EVAL_COM1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_Init(&NVIC_InitStructure);
}
void configureUsbCable(FunctionalState state)
{
#if !defined(USE_STM32_NO_EVAL)
if (state != DISABLE)
{
GPIO_ResetBits(USB_DISCONNECT, USB_DISCONNECT_PIN);
}
else
{
GPIO_SetBits(USB_DISCONNECT, USB_DISCONNECT_PIN);
}
#endif /* STM32L1XX_MD */
}
/*******************************************************************************
* Function Name : setUsartDefaultSettings.
* Description : configure the EVAL_COM1 with default values.
* Input : None.
* Return : None.
*******************************************************************************/
void setUsartDefaultSettings(void)
{
/* EVAL_COM1 default configuration */
/* EVAL_COM1 configured as follow:
- BaudRate = 9600 baud
- Word Length = 8 Bits
- One Stop Bit
- Parity Odd
- Hardware flow control disabled
- Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = 921600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
/* Configure and enable the USART */
STM_EVAL_COMInit(COM1, &USART_InitStructure);
/* Enable the USART Receive interrupt */
USART_ITConfig(EVAL_COM1, USART_IT_RXNE, ENABLE);
}
bool USART_Config(void)
{
/* set the Stop bit*/
switch (linecoding.format)
{
case 0:
USART_InitStructure.USART_StopBits = USART_StopBits_1;
break;
case 1:
USART_InitStructure.USART_StopBits = USART_StopBits_1_5;
break;
case 2:
USART_InitStructure.USART_StopBits = USART_StopBits_2;
break;
default :
{
setUsartDefaultSettings();
return (FALSE);
}
}
/* set the parity bit*/
switch (linecoding.paritytype)
{
case 0:
USART_InitStructure.USART_Parity = USART_Parity_No;
break;
case 1:
USART_InitStructure.USART_Parity = USART_Parity_Even;
break;
case 2:
USART_InitStructure.USART_Parity = USART_Parity_Odd;
break;
default :
{
setUsartDefaultSettings();
return (FALSE);
}
}
/*set the data type : only 8bits and 9bits is supported */
switch (linecoding.datatype)
{
case 0x07:
/* With this configuration a parity (Even or Odd) should be set */
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
break;
case 0x08:
if (USART_InitStructure.USART_Parity == USART_Parity_No)
{
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
}
else
{
USART_InitStructure.USART_WordLength = USART_WordLength_9b;
}
break;
default :
{
setUsartDefaultSettings();
return (FALSE);
}
}
USART_InitStructure.USART_BaudRate = linecoding.bitrate;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
/* Configure and enable the USART */
STM_EVAL_COMInit(COM1, &USART_InitStructure);
return (TRUE);
}
/*******************************************************************************
* Function Name : USB_To_USART_Send_Data.
* Description : send the received data from USB to the UART 0.
* Input : data_buffer: data address.
Nb_bytes: number of bytes to send.
* Return : none.
*******************************************************************************/
void USB_To_USART_Send_Data(uint8_t* data_buffer, uint8_t Nb_bytes)
{
uint32_t i;
for (i = 0; i < Nb_bytes; i++)
{
USART_SendData(EVAL_COM1, *(data_buffer + i));
while(USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TXE) == RESET);
}
}
/*******************************************************************************
* Function Name : Handle_USBAsynchXfer.
* Description : send data to USB.
* Input : None.
* Return : none.
*******************************************************************************/
void Handle_USBAsynchXfer (void)
{
uint16_t USB_Tx_ptr;
uint16_t USB_Tx_length;
if(USB_Tx_State != 1)
{
if (USART_Rx_ptr_out == USART_RX_DATA_SIZE)
{
USART_Rx_ptr_out = 0;
}
if(USART_Rx_ptr_out == USART_Rx_ptr_in)
{
USB_Tx_State = 0;
return;
}
if(USART_Rx_ptr_out > USART_Rx_ptr_in) /* rollback */
{
USART_Rx_length = USART_RX_DATA_SIZE - USART_Rx_ptr_out;
}
else
{
USART_Rx_length = USART_Rx_ptr_in - USART_Rx_ptr_out;
}
if (USART_Rx_length > VIRTUAL_COM_PORT_DATA_SIZE)
{
USB_Tx_ptr = USART_Rx_ptr_out;
USB_Tx_length = VIRTUAL_COM_PORT_DATA_SIZE;
USART_Rx_ptr_out += VIRTUAL_COM_PORT_DATA_SIZE;
USART_Rx_length -= VIRTUAL_COM_PORT_DATA_SIZE;
}
else
{
USB_Tx_ptr = USART_Rx_ptr_out;
USB_Tx_length = USART_Rx_length;
USART_Rx_ptr_out += USART_Rx_length;
USART_Rx_length = 0;
}
USB_Tx_State = 1;
UserToPMABufferCopy(&USART_Rx_Buffer[USB_Tx_ptr], ENDP2_TXADDR, USB_Tx_length);
SetEPTxCount(ENDP2, USB_Tx_length);
SetEPTxValid(ENDP2);
}
}
void USART_To_USB_Send_Data(void)
{
//USART_ReceiveData(EVAL_COM1); return;
if (linecoding.datatype == 7)
{
USART_Rx_Buffer[USART_Rx_ptr_in] = USART_ReceiveData(EVAL_COM1) & 0x7F;
}
else if ((linecoding.datatype == 8) || 1)
{
USART_Rx_Buffer[USART_Rx_ptr_in] = USART_ReceiveData(EVAL_COM1);
}
USART_Rx_ptr_in++;
/* To avoid buffer overflow */
if(USART_Rx_ptr_in == USART_RX_DATA_SIZE)
{
USART_Rx_ptr_in = 0;
}
}
void fillSerialNumber(void)
{
uint32_t serialNumber0, serialNumber1, serialNumber2;
serialNumber0 = *(uint32_t*) ID1;
serialNumber1 = *(uint32_t*) ID2;
serialNumber2 = *(uint32_t*) ID3;
serialNumber0 += serialNumber2;
if (serialNumber0 != 0)
{
intToUnicode(serialNumber0, &vcpStringSerial[2], 8);
intToUnicode(serialNumber1, &vcpStringSerial[18], 4);
}
}