PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/hw_config.c

https://github.com/mwalecki/ptbldc
C | 381 lines | 201 code | 54 blank | 126 comment | 27 complexity | 3e06e9494358e1b52fb544ceb52ba3a3 MD5 | raw file
  1. /**
  2. ******************************************************************************
  3. * @file hw_config.c
  4. * @author MCD Application Team
  5. * @version V3.4.0
  6. * @date 29-June-2012
  7. * @brief Hardware Configuration & Setup
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  12. *
  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:
  16. *
  17. * http://www.st.com/software_license_agreement_liberty_v2
  18. *
  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.
  24. *
  25. ******************************************************************************
  26. */
  27. /* Includes ------------------------------------------------------------------*/
  28. #include "stm32_it.h"
  29. #include "usb_lib.h"
  30. #include "usb_prop.h"
  31. #include "usb_desc.h"
  32. #include "hw_config.h"
  33. #include "usb_pwr.h"
  34. #include "usb.h"
  35. /* Private typedef -----------------------------------------------------------*/
  36. /* Private define ------------------------------------------------------------*/
  37. /* Private macro -------------------------------------------------------------*/
  38. /* Private variables ---------------------------------------------------------*/
  39. ErrorStatus HSEStartUpStatus;
  40. USART_InitTypeDef USART_InitStructure;
  41. uint8_t USART_Rx_Buffer [USART_RX_DATA_SIZE];
  42. uint32_t USART_Rx_ptr_in = 0;
  43. uint32_t USART_Rx_ptr_out = 0;
  44. uint32_t USART_Rx_length = 0;
  45. uint8_t USB_Tx_State = 0;
  46. static void IntToUnicode (uint32_t value , uint8_t *pbuf , uint8_t len);
  47. /* Extern variables ----------------------------------------------------------*/
  48. extern LINE_CODING linecoding;
  49. /* Private function prototypes -----------------------------------------------*/
  50. /* Private functions ---------------------------------------------------------*/
  51. /*******************************************************************************
  52. * Function Name : Set_System
  53. * Description : Configures Main system clocks & power
  54. * Input : None.
  55. * Return : None.
  56. *******************************************************************************/
  57. void Set_System(void)
  58. {
  59. }
  60. /*******************************************************************************
  61. * Function Name : Set_USBClock
  62. * Description : Configures USB Clock input (48MHz)
  63. * Input : None.
  64. * Return : None.
  65. *******************************************************************************/
  66. void Set_USBClock(void)
  67. {
  68. #if defined(STM32L1XX_MD) || defined(STM32L1XX_HD) || defined(STM32L1XX_MD_PLUS)
  69. /* Enable USB clock */
  70. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);
  71. #elif defined(STM32F10X_CL)
  72. /* Select USBCLK source */
  73. RCC_OTGFSCLKConfig(RCC_OTGFSCLKSource_PLLVCO_Div3);
  74. /* Enable the USB clock */
  75. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_OTG_FS, ENABLE) ;
  76. #else
  77. /* Select USBCLK source */
  78. RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
  79. /* Enable the USB clock */
  80. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);
  81. #endif /* STM32F10X_CL */
  82. }
  83. /*******************************************************************************
  84. * Function Name : Enter_LowPowerMode
  85. * Description : Power-off system clocks and power while entering suspend mode
  86. * Input : None.
  87. * Return : None.
  88. *******************************************************************************/
  89. void Enter_LowPowerMode(void)
  90. {
  91. /* Set the device state to suspend */
  92. bDeviceState = SUSPENDED;
  93. }
  94. /*******************************************************************************
  95. * Function Name : Leave_LowPowerMode
  96. * Description : Restores system clocks and power while exiting suspend mode
  97. * Input : None.
  98. * Return : None.
  99. *******************************************************************************/
  100. void Leave_LowPowerMode(void)
  101. {
  102. DEVICE_INFO *pInfo = &Device_Info;
  103. /* Set the device state to the correct state */
  104. if (pInfo->Current_Configuration != 0)
  105. {
  106. /* Device configured */
  107. bDeviceState = CONFIGURED;
  108. }
  109. else
  110. {
  111. bDeviceState = ATTACHED;
  112. }
  113. }
  114. /*******************************************************************************
  115. * Function Name : USB_Interrupts_Config
  116. * Description : Configures the USB interrupts
  117. * Input : None.
  118. * Return : None.
  119. *******************************************************************************/
  120. void USB_Interrupts_Config(void)
  121. {
  122. NVIC_InitTypeDef NVIC_InitStructure;
  123. // Interrupts settings in interrupts.c
  124. return;
  125. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  126. #if defined(STM32L1XX_MD) || defined(STM32L1XX_HD) || defined(STM32L1XX_MD_PLUS)
  127. NVIC_InitStructure.NVIC_IRQChannel = USB_LP_IRQn;
  128. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  129. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  130. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  131. NVIC_Init(&NVIC_InitStructure);
  132. #elif defined(STM32F10X_CL)
  133. /* Enable the USB Interrupts */
  134. NVIC_InitStructure.NVIC_IRQChannel = OTG_FS_IRQn;
  135. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  136. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  137. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  138. NVIC_Init(&NVIC_InitStructure);
  139. #else
  140. NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
  141. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  142. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  143. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  144. NVIC_Init(&NVIC_InitStructure);
  145. #endif /* STM32L1XX_XD */
  146. }
  147. /*******************************************************************************
  148. * Function Name : USB_Cable_Config
  149. * Description : Software Connection/Disconnection of USB Cable
  150. * Input : None.
  151. * Return : Status
  152. *******************************************************************************/
  153. void USB_Cable_Config (FunctionalState NewState)
  154. {
  155. #ifdef USB_CONNECT_PORT
  156. if (NewState != DISABLE)
  157. {
  158. GPIO_SetBits(USB_CONNECT_PORT, USB_CONNECT_PIN);
  159. }
  160. else
  161. {
  162. GPIO_ResetBits(USB_CONNECT_PORT, USB_CONNECT_PIN);
  163. }
  164. #endif
  165. }
  166. void USART_Config_Default(void)
  167. {
  168. }
  169. bool USART_Config(void)
  170. {
  171. return TRUE;
  172. }
  173. /*******************************************************************************
  174. * Function Name : USB_To_USART_Send_Data.
  175. * Description : send the received data from USB to the UART 0.
  176. * Input : data_buffer: data address.
  177. Nb_bytes: number of bytes to send.
  178. * Return : none.
  179. *******************************************************************************/
  180. void USB_To_USART_Send_Data(uint8_t* data_buffer, uint8_t Nb_bytes)
  181. {
  182. uint32_t i;
  183. for (i = 0; i < Nb_bytes; i++)
  184. {
  185. // USART_SendData(EVAL_COM1, *(data_buffer + i));
  186. // while(USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TXE) == RESET);
  187. }
  188. }
  189. /*******************************************************************************
  190. * Function Name : Handle_USBAsynchXfer.
  191. * Description : send data to USB.
  192. * Input : None.
  193. * Return : none.
  194. *******************************************************************************/
  195. void Handle_USBAsynchXfer (void)
  196. {
  197. uint16_t USB_Tx_ptr;
  198. uint16_t USB_Tx_length;
  199. if(USB_Tx_State != 1)
  200. {
  201. if (USART_Rx_ptr_out == USART_RX_DATA_SIZE)
  202. {
  203. USART_Rx_ptr_out = 0;
  204. }
  205. if(USART_Rx_ptr_out == USART_Rx_ptr_in)
  206. {
  207. USB_Tx_State = 0;
  208. return;
  209. }
  210. if(USART_Rx_ptr_out > USART_Rx_ptr_in) /* rollback */
  211. {
  212. USART_Rx_length = USART_RX_DATA_SIZE - USART_Rx_ptr_out;
  213. }
  214. else
  215. {
  216. USART_Rx_length = USART_Rx_ptr_in - USART_Rx_ptr_out;
  217. }
  218. if (USART_Rx_length > VIRTUAL_COM_PORT_DATA_SIZE)
  219. {
  220. USB_Tx_ptr = USART_Rx_ptr_out;
  221. USB_Tx_length = VIRTUAL_COM_PORT_DATA_SIZE;
  222. USART_Rx_ptr_out += VIRTUAL_COM_PORT_DATA_SIZE;
  223. USART_Rx_length -= VIRTUAL_COM_PORT_DATA_SIZE;
  224. }
  225. else
  226. {
  227. USB_Tx_ptr = USART_Rx_ptr_out;
  228. USB_Tx_length = USART_Rx_length;
  229. USART_Rx_ptr_out += USART_Rx_length;
  230. USART_Rx_length = 0;
  231. }
  232. USB_Tx_State = 1;
  233. #ifdef USE_STM3210C_EVAL
  234. USB_SIL_Write(EP1_IN, &USART_Rx_Buffer[USB_Tx_ptr], USB_Tx_length);
  235. #else
  236. UserToPMABufferCopy(&USART_Rx_Buffer[USB_Tx_ptr], ENDP1_TXADDR, USB_Tx_length);
  237. SetEPTxCount(ENDP1, USB_Tx_length);
  238. SetEPTxValid(ENDP1);
  239. #endif /* USE_STM3210C_EVAL */
  240. }
  241. }
  242. /*******************************************************************************
  243. * Function Name : UART_To_USB_Send_Data.
  244. * Description : send the received data from UART 0 to USB.
  245. * Input : None.
  246. * Return : none.
  247. *******************************************************************************/
  248. void USART_To_USB_Send_Data(void)
  249. {
  250. // USART_Rx_Buffer[USART_Rx_ptr_in] = USART_ReceiveData(EVAL_COM1);
  251. USART_Rx_ptr_in++;
  252. /* To avoid buffer overflow */
  253. if(USART_Rx_ptr_in == USART_RX_DATA_SIZE)
  254. {
  255. USART_Rx_ptr_in = 0;
  256. }
  257. }
  258. /*******************************************************************************
  259. * Function Name : Get_SerialNum.
  260. * Description : Create the serial number string descriptor.
  261. * Input : None.
  262. * Output : None.
  263. * Return : None.
  264. *******************************************************************************/
  265. void Get_SerialNum(void)
  266. {
  267. uint32_t Device_Serial0, Device_Serial1, Device_Serial2;
  268. #if defined(STM32L1XX_MD) || defined(STM32L1XX_HD) || defined(STM32L1XX_MD_PLUS)
  269. Device_Serial0 = *(uint32_t*)(0x1FF80050);
  270. Device_Serial1 = *(uint32_t*)(0x1FF80054);
  271. Device_Serial2 = *(uint32_t*)(0x1FF80064);
  272. #else
  273. Device_Serial0 = *(__IO uint32_t*)(0x1FFFF7E8);
  274. Device_Serial1 = *(__IO uint32_t*)(0x1FFFF7EC);
  275. Device_Serial2 = *(__IO uint32_t*)(0x1FFFF7F0);
  276. #endif /* STM32L1XX_XD */
  277. Device_Serial0 += Device_Serial2;
  278. if (Device_Serial0 != 0)
  279. {
  280. IntToUnicode (Device_Serial0, &Virtual_Com_Port_StringSerial[2] , 8);
  281. IntToUnicode (Device_Serial1, &Virtual_Com_Port_StringSerial[18], 4);
  282. }
  283. }
  284. /*******************************************************************************
  285. * Function Name : HexToChar.
  286. * Description : Convert Hex 32Bits value into char.
  287. * Input : None.
  288. * Output : None.
  289. * Return : None.
  290. *******************************************************************************/
  291. static void IntToUnicode (uint32_t value , uint8_t *pbuf , uint8_t len)
  292. {
  293. uint8_t idx = 0;
  294. for( idx = 0 ; idx < len ; idx ++)
  295. {
  296. if( ((value >> 28)) < 0xA )
  297. {
  298. pbuf[ 2* idx] = (value >> 28) + '0';
  299. }
  300. else
  301. {
  302. pbuf[2* idx] = (value >> 28) + 'A' - 10;
  303. }
  304. value = value << 4;
  305. pbuf[ 2* idx + 1] = 0;
  306. }
  307. }
  308. #ifdef STM32F10X_CL
  309. /*******************************************************************************
  310. * Function Name : USB_OTG_BSP_uDelay.
  311. * Description : provide delay (usec).
  312. * Input : None.
  313. * Output : None.
  314. * Return : None.
  315. *******************************************************************************/
  316. void USB_OTG_BSP_uDelay (const uint32_t usec)
  317. {
  318. RCC_ClocksTypeDef RCC_Clocks;
  319. /* Configure HCLK clock as SysTick clock source */
  320. SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
  321. RCC_GetClocksFreq(&RCC_Clocks);
  322. SysTick_Config(usec * (RCC_Clocks.HCLK_Frequency / 1000000));
  323. SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk ;
  324. while (!(SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk));
  325. }
  326. #endif /* STM32F10X_CL */
  327. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/