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/time.h"
40 #include "drivers/nvic.h"
42 #include "common/utils.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)
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 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA
, ENABLE
); // HJI
91 GPIO_InitStructure
.GPIO_Pin
= GPIO_Pin_12
; // HJI
92 GPIO_InitStructure
.GPIO_Speed
= GPIO_Speed_50MHz
; // HJI
93 GPIO_InitStructure
.GPIO_Mode
= GPIO_Mode_Out_OD
; // HJI
95 GPIO_Init(GPIOA
, &GPIO_InitStructure
); // HJI
97 GPIO_ResetBits(GPIOA
, GPIO_Pin_12
); // HJI
101 GPIO_SetBits(GPIOA
, GPIO_Pin_12
); // HJI
103 /* Configure the EXTI line 18 connected internally to the USB IP */
104 EXTI_ClearITPendingBit(EXTI_Line18
);
105 EXTI_InitStructure
.EXTI_Line
= EXTI_Line18
;
106 EXTI_InitStructure
.EXTI_Trigger
= EXTI_Trigger_Rising
;
107 EXTI_InitStructure
.EXTI_LineCmd
= ENABLE
;
108 EXTI_Init(&EXTI_InitStructure
);
111 /*******************************************************************************
112 * Function Name : Set_USBClock
113 * Description : Configures USB Clock input (48MHz)
116 *******************************************************************************/
117 void Set_USBClock(void)
119 RCC_ClocksTypeDef RCC_Clocks
;
120 RCC_GetClocksFreq(&RCC_Clocks
);
122 /* Select USBCLK source */
123 RCC_USBCLKConfig(RCC_Clocks
.SYSCLK_Frequency
== 72000000 ? RCC_USBCLKSource_PLLCLK_1Div5
: RCC_USBCLKSource_PLLCLK_Div1
);
125 /* Enable the USB clock */
126 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB
, ENABLE
);
129 /*******************************************************************************
130 * Function Name : Enter_LowPowerMode
131 * Description : Power-off system clocks and power while entering suspend mode
134 *******************************************************************************/
135 void Enter_LowPowerMode(void)
137 /* Set the device state to suspend */
138 bDeviceState
= SUSPENDED
;
141 /*******************************************************************************
142 * Function Name : Leave_LowPowerMode
143 * Description : Restores system clocks and power while exiting suspend mode
146 *******************************************************************************/
147 void Leave_LowPowerMode(void)
149 DEVICE_INFO
*pInfo
= &Device_Info
;
151 /* Set the device state to the correct state */
152 if (pInfo
->Current_Configuration
!= 0) {
153 /* Device configured */
154 bDeviceState
= CONFIGURED
;
156 bDeviceState
= ATTACHED
;
158 /*Enable SystemCoreClock*/
162 /*******************************************************************************
163 * Function Name : USB_Interrupts_Config
164 * Description : Configures the USB interrupts
167 *******************************************************************************/
168 void USB_Interrupts_Config(void)
170 NVIC_SetPriority(USB_LP_CAN1_RX0_IRQn
, NVIC_PRIO_USB
);
171 NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn
);
173 NVIC_SetPriority(USBWakeUp_IRQn
, NVIC_PRIO_USB
);
174 NVIC_EnableIRQ(USBWakeUp_IRQn
);
177 /*******************************************************************************
178 * Function Name : USB_Cable_Config
179 * Description : Software Connection/Disconnection of USB Cable
182 *******************************************************************************/
183 void USB_Cable_Config(FunctionalState NewState
)
188 /*******************************************************************************
189 * Function Name : Get_SerialNum.
190 * Description : Create the serial number string descriptor.
194 *******************************************************************************/
195 void Get_SerialNum(void)
197 uint32_t Device_Serial0
, Device_Serial1
, Device_Serial2
;
199 Device_Serial0
= *(uint32_t*)ID1
;
200 Device_Serial1
= *(uint32_t*)ID2
;
201 Device_Serial2
= *(uint32_t*)ID3
;
203 Device_Serial0
+= Device_Serial2
;
205 if (Device_Serial0
!= 0) {
206 IntToUnicode(Device_Serial0
, &Virtual_Com_Port_StringSerial
[2], 8);
207 IntToUnicode(Device_Serial1
, &Virtual_Com_Port_StringSerial
[18], 4);
211 /*******************************************************************************
212 * Function Name : HexToChar.
213 * Description : Convert Hex 32Bits value into char.
217 *******************************************************************************/
218 static void IntToUnicode(uint32_t value
, uint8_t *pbuf
, uint8_t len
)
222 for (idx
= 0; idx
< len
; idx
++) {
223 if (((value
>> 28)) < 0xA) {
224 pbuf
[2 * idx
] = (value
>> 28) + '0';
226 pbuf
[2 * idx
] = (value
>> 28) + 'A' - 10;
231 pbuf
[2 * idx
+ 1] = 0;
235 /*******************************************************************************
236 * Function Name : Send DATA .
237 * Description : send the data received from the STM32 to the PC through USB
241 *******************************************************************************/
242 uint32_t CDC_Send_DATA(const uint8_t *ptrBuffer
, uint32_t sendLength
)
244 /* Last transmission hasn't finished, abort */
249 // We can only put 64 bytes in the buffer
250 if (sendLength
> 64 / 2) {
254 // Try to load some bytes if we can
256 UserToPMABufferCopy(ptrBuffer
, ENDP1_TXADDR
, sendLength
);
257 SetEPTxCount(ENDP1
, sendLength
);
258 packetSent
+= sendLength
;
265 uint32_t CDC_Send_FreeBytes(void)
267 /* this driver is blocking, so the buffer is unlimited */
271 /*******************************************************************************
272 * Function Name : Receive DATA .
273 * Description : receive the data from the PC to STM32 and send it through USB
277 *******************************************************************************/
278 uint32_t CDC_Receive_DATA(uint8_t* recvBuf
, uint32_t len
)
280 static uint8_t offset
= 0;
283 if (len
> receiveLength
) {
287 for (i
= 0; i
< len
; i
++) {
288 recvBuf
[i
] = (uint8_t)(receiveBuffer
[i
+ offset
]);
291 receiveLength
-= len
;
294 /* re-enable the rx endpoint which we had set to receive 0 bytes */
295 if (receiveLength
== 0) {
296 SetEPRxCount(ENDP3
, 64);
297 SetEPRxStatus(ENDP3
, EP_RX_VALID
);
304 uint32_t CDC_Receive_BytesAvailable(void)
306 return receiveLength
;
309 /*******************************************************************************
310 * Function Name : usbIsConfigured.
311 * Description : Determines if USB VCP is configured or not
314 * Return : True if configured.
315 *******************************************************************************/
316 uint8_t usbIsConfigured(void)
318 return (bDeviceState
== CONFIGURED
);
321 /*******************************************************************************
322 * Function Name : usbIsConnected.
323 * Description : Determines if USB VCP is connected ot not
326 * Return : True if connected.
327 *******************************************************************************/
328 uint8_t usbIsConnected(void)
330 return (bDeviceState
!= UNCONNECTED
);
334 /*******************************************************************************
335 * Function Name : CDC_BaudRate.
336 * Description : Get the current baud rate
339 * Return : Baud rate in bps
340 *******************************************************************************/
341 uint32_t CDC_BaudRate(void)
343 return Virtual_Com_Port_GetBaudRate();
346 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/