2 ******************************************************************************
4 * @author MCD Application Team
6 * @date 21-January-2013
7 * @brief Hardware Configuration & Setup
8 ******************************************************************************
11 * <h2><center>© COPYRIGHT 2013 STMicroelectronics</center></h2>
13 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
14 * You may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at:
17 * http://www.st.com/software_license_agreement_liberty_v2
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
25 ******************************************************************************
28 /* Includes ------------------------------------------------------------------*/
35 #include "hw_config.h"
39 #include "drivers/system.h"
40 #include "drivers/nvic.h"
42 #include "build_config.h"
45 /* Private typedef -----------------------------------------------------------*/
46 /* Private define ------------------------------------------------------------*/
47 /* Private macro -------------------------------------------------------------*/
48 /* Private variables ---------------------------------------------------------*/
49 ErrorStatus HSEStartUpStatus
;
50 EXTI_InitTypeDef EXTI_InitStructure
;
51 __IO
uint32_t packetSent
; // HJI
52 extern __IO
uint32_t receiveLength
; // HJI
54 uint8_t receiveBuffer
[64]; // HJI
55 uint32_t sendLength
; // HJI
56 static void IntToUnicode(uint32_t value
, uint8_t *pbuf
, uint8_t len
);
57 /* Extern variables ----------------------------------------------------------*/
59 /* Private function prototypes -----------------------------------------------*/
60 /* Private functions ---------------------------------------------------------*/
61 /*******************************************************************************
62 * Function Name : Set_System
63 * Description : Configures Main system clocks & power
66 *******************************************************************************/
69 #if !defined(STM32L1XX_MD) && !defined(STM32L1XX_HD) && !defined(STM32L1XX_MD_PLUS)
70 GPIO_InitTypeDef GPIO_InitStructure
;
71 #endif /* STM32L1XX_MD && STM32L1XX_XD */
73 #if defined(USB_USE_EXTERNAL_PULLUP)
74 GPIO_InitTypeDef GPIO_InitStructure
;
75 #endif /* USB_USE_EXTERNAL_PULLUP */
77 /*!< At this stage the microcontroller clock setting is already configured,
78 this is done through SystemInit() function which is called from startup
79 file (startup_stm32f10x_xx.s) before to branch to application main.
80 To reconfigure the default setting of SystemInit() function, refer to
81 system_stm32f10x.c file
83 #if defined(STM32L1XX_MD) || defined(STM32L1XX_HD)|| defined(STM32L1XX_MD_PLUS) || defined(STM32F37X) || defined(STM32F303xC)
84 /* Enable the SYSCFG module clock */
85 RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG
, ENABLE
);
86 #endif /* STM32L1XX_XD */
88 /*Pull down PA12 to create USB Disconnect Pulse*/ // HJI
89 #if defined(STM32F303xC) // HJI
90 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA
, ENABLE
); // HJI
92 GPIO_InitStructure
.GPIO_Pin
= GPIO_Pin_12
; // HJI
93 GPIO_InitStructure
.GPIO_Speed
= GPIO_Speed_50MHz
; // HJI
94 GPIO_InitStructure
.GPIO_Mode
= GPIO_Mode_OUT
; // HJI
95 GPIO_InitStructure
.GPIO_OType
= GPIO_OType_OD
; // HJI
96 GPIO_InitStructure
.GPIO_PuPd
= GPIO_PuPd_NOPULL
; // HJI
98 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA
, ENABLE
); // HJI
100 GPIO_InitStructure
.GPIO_Pin
= GPIO_Pin_12
;// HJI
101 GPIO_InitStructure
.GPIO_Speed
= GPIO_Speed_50MHz
;// HJI
102 GPIO_InitStructure
.GPIO_Mode
= GPIO_Mode_Out_OD
;// HJI
105 GPIO_Init(GPIOA
, &GPIO_InitStructure
); // HJI
107 GPIO_ResetBits(GPIOA
, GPIO_Pin_12
); // HJI
111 GPIO_SetBits(GPIOA
, GPIO_Pin_12
); // HJI
113 #if defined(STM32F37X) || defined(STM32F303xC)
115 /*Set PA11,12 as IN - USB_DM,DP*/
116 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA
, ENABLE
);
117 GPIO_InitStructure
.GPIO_Pin
= GPIO_Pin_11
| GPIO_Pin_12
;
118 GPIO_InitStructure
.GPIO_Speed
= GPIO_Speed_50MHz
;
119 GPIO_InitStructure
.GPIO_Mode
= GPIO_Mode_AF
;
120 GPIO_InitStructure
.GPIO_OType
= GPIO_OType_PP
;
121 GPIO_InitStructure
.GPIO_PuPd
= GPIO_PuPd_NOPULL
;
122 GPIO_Init(GPIOA
, &GPIO_InitStructure
);
124 /*SET PA11,12 for USB: USB_DM,DP*/
125 GPIO_PinAFConfig(GPIOA
, GPIO_PinSource11
, GPIO_AF_14
);
126 GPIO_PinAFConfig(GPIOA
, GPIO_PinSource12
, GPIO_AF_14
);
128 #endif /* STM32F37X && STM32F303xC)*/
129 #if defined(STM32F10X)
130 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA
, ENABLE
);
131 GPIO_InitStructure
.GPIO_Pin
= GPIO_Pin_11
| GPIO_Pin_12
;
132 GPIO_InitStructure
.GPIO_Speed
= GPIO_Speed_50MHz
;
133 GPIO_InitStructure
.GPIO_Mode
= GPIO_Mode_AF_OD
;
134 GPIO_Init(GPIOA
, &GPIO_InitStructure
);
137 /* Configure the EXTI line 18 connected internally to the USB IP */
138 EXTI_ClearITPendingBit(EXTI_Line18
);
139 EXTI_InitStructure
.EXTI_Line
= EXTI_Line18
;
140 EXTI_InitStructure
.EXTI_Trigger
= EXTI_Trigger_Rising
;
141 EXTI_InitStructure
.EXTI_LineCmd
= ENABLE
;
142 EXTI_Init(&EXTI_InitStructure
);
145 /*******************************************************************************
146 * Function Name : Set_USBClock
147 * Description : Configures USB Clock input (48MHz)
150 *******************************************************************************/
151 void Set_USBClock(void)
153 /* Select USBCLK source */
154 RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5
);
156 /* Enable the USB clock */
157 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB
, ENABLE
);
160 /*******************************************************************************
161 * Function Name : Enter_LowPowerMode
162 * Description : Power-off system clocks and power while entering suspend mode
165 *******************************************************************************/
166 void Enter_LowPowerMode(void)
168 /* Set the device state to suspend */
169 bDeviceState
= SUSPENDED
;
172 /*******************************************************************************
173 * Function Name : Leave_LowPowerMode
174 * Description : Restores system clocks and power while exiting suspend mode
177 *******************************************************************************/
178 void Leave_LowPowerMode(void)
180 DEVICE_INFO
*pInfo
= &Device_Info
;
182 /* Set the device state to the correct state */
183 if (pInfo
->Current_Configuration
!= 0) {
184 /* Device configured */
185 bDeviceState
= CONFIGURED
;
187 bDeviceState
= ATTACHED
;
189 /*Enable SystemCoreClock*/
193 /*******************************************************************************
194 * Function Name : USB_Interrupts_Config
195 * Description : Configures the USB interrupts
198 *******************************************************************************/
199 void USB_Interrupts_Config(void)
201 NVIC_InitTypeDef NVIC_InitStructure
;
203 /* 2 bit for pre-emption priority, 2 bits for subpriority */
204 NVIC_PriorityGroupConfig(NVIC_PRIORITY_GROUPING
); // is this really neccesary?
206 /* Enable the USB interrupt */
207 NVIC_InitStructure
.NVIC_IRQChannel
= USB_LP_CAN1_RX0_IRQn
;
208 NVIC_InitStructure
.NVIC_IRQChannelPreemptionPriority
= NVIC_PRIORITY_BASE(NVIC_PRIO_USB
);
209 NVIC_InitStructure
.NVIC_IRQChannelSubPriority
= NVIC_PRIORITY_SUB(NVIC_PRIO_USB
);
210 NVIC_InitStructure
.NVIC_IRQChannelCmd
= ENABLE
;
211 NVIC_Init(&NVIC_InitStructure
);
213 /* Enable the USB Wake-up interrupt */
214 NVIC_InitStructure
.NVIC_IRQChannel
= USBWakeUp_IRQn
;
215 NVIC_InitStructure
.NVIC_IRQChannelPreemptionPriority
= NVIC_PRIORITY_BASE(NVIC_PRIO_USB_WUP
);
216 NVIC_InitStructure
.NVIC_IRQChannelSubPriority
= NVIC_PRIORITY_SUB(NVIC_PRIO_USB_WUP
);
217 NVIC_Init(&NVIC_InitStructure
);
220 /*******************************************************************************
221 * Function Name : USB_Cable_Config
222 * Description : Software Connection/Disconnection of USB Cable
225 *******************************************************************************/
226 void USB_Cable_Config(FunctionalState NewState
)
231 /*******************************************************************************
232 * Function Name : Get_SerialNum.
233 * Description : Create the serial number string descriptor.
237 *******************************************************************************/
238 void Get_SerialNum(void)
240 uint32_t Device_Serial0
, Device_Serial1
, Device_Serial2
;
242 Device_Serial0
= *(uint32_t*)ID1
;
243 Device_Serial1
= *(uint32_t*)ID2
;
244 Device_Serial2
= *(uint32_t*)ID3
;
246 Device_Serial0
+= Device_Serial2
;
248 if (Device_Serial0
!= 0) {
249 IntToUnicode(Device_Serial0
, &Virtual_Com_Port_StringSerial
[2], 8);
250 IntToUnicode(Device_Serial1
, &Virtual_Com_Port_StringSerial
[18], 4);
254 /*******************************************************************************
255 * Function Name : HexToChar.
256 * Description : Convert Hex 32Bits value into char.
260 *******************************************************************************/
261 static void IntToUnicode(uint32_t value
, uint8_t *pbuf
, uint8_t len
)
265 for (idx
= 0; idx
< len
; idx
++) {
266 if (((value
>> 28)) < 0xA) {
267 pbuf
[2 * idx
] = (value
>> 28) + '0';
269 pbuf
[2 * idx
] = (value
>> 28) + 'A' - 10;
274 pbuf
[2 * idx
+ 1] = 0;
278 /*******************************************************************************
279 * Function Name : Send DATA .
280 * Description : send the data received from the STM32 to the PC through USB
284 *******************************************************************************/
285 uint32_t CDC_Send_DATA(uint8_t *ptrBuffer
, uint8_t sendLength
)
287 /* Last transmission hasn't finished, abort */
292 // We can only put 64 bytes in the buffer
293 if (sendLength
> 64 / 2) {
297 // Try to load some bytes if we can
299 UserToPMABufferCopy(ptrBuffer
, ENDP1_TXADDR
, sendLength
);
300 SetEPTxCount(ENDP1
, sendLength
);
301 packetSent
+= sendLength
;
308 /*******************************************************************************
309 * Function Name : Receive DATA .
310 * Description : receive the data from the PC to STM32 and send it through USB
314 *******************************************************************************/
315 uint32_t CDC_Receive_DATA(uint8_t* recvBuf
, uint32_t len
)
317 static uint8_t offset
= 0;
320 if (len
> receiveLength
) {
324 for (i
= 0; i
< len
; i
++) {
325 recvBuf
[i
] = (uint8_t)(receiveBuffer
[i
+ offset
]);
328 receiveLength
-= len
;
331 /* re-enable the rx endpoint which we had set to receive 0 bytes */
332 if (receiveLength
== 0) {
333 SetEPRxCount(ENDP3
, 64);
334 SetEPRxStatus(ENDP3
, EP_RX_VALID
);
341 /*******************************************************************************
342 * Function Name : usbIsConfigured.
343 * Description : Determines if USB VCP is configured or not
346 * Return : True if configured.
347 *******************************************************************************/
348 uint8_t usbIsConfigured(void)
350 return (bDeviceState
== CONFIGURED
);
353 /*******************************************************************************
354 * Function Name : usbIsConnected.
355 * Description : Determines if USB VCP is connected ot not
358 * Return : True if connected.
359 *******************************************************************************/
360 uint8_t usbIsConnected(void)
362 return (bDeviceState
!= UNCONNECTED
);
365 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/