main.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * This notice applies to any and all portions of this file
  8. * that are not between comment pairs USER CODE BEGIN and
  9. * USER CODE END. Other portions of this file, whether
  10. * inserted by the user or by software development tools
  11. * are owned by their respective copyright owners.
  12. *
  13. * Copyright (c) 2019 STMicroelectronics International N.V.
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted, provided that the following conditions are met:
  18. *
  19. * 1. Redistribution of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * 3. Neither the name of STMicroelectronics nor the names of other
  25. * contributors to this software may be used to endorse or promote products
  26. * derived from this software without specific written permission.
  27. * 4. This software, including modifications and/or derivative works of this
  28. * software, must execute solely and exclusively on microcontroller or
  29. * microprocessor devices manufactured by or for STMicroelectronics.
  30. * 5. Redistribution and use of this software other than as permitted under
  31. * this license is void and will automatically terminate your rights under
  32. * this license.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  35. * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  37. * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  38. * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  39. * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  40. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  42. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  43. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  44. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  45. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. *
  47. ******************************************************************************
  48. */
  49. /* USER CODE END Header */
  50. /* Includes ------------------------------------------------------------------*/
  51. #define MAIN_C
  52. #include "core/main.h"
  53. #include "usb_device.h"
  54. #include "core/gpio.h"
  55. //#include "drivers/spi/spi.h"
  56. #include "drivers/flash/base/extmem_flash.h"
  57. #include "app/usbapp/usb_application_flash.h"
  58. #include "app/usbapp/usb_application_enumspy.h"
  59. #include "app/thermo/tsensor.h"
  60. #include "app/thermo/NTCtsensor.h"
  61. #include "app/control_table/control_table.h"
  62. #include "app/automat/automat.h"
  63. #include "app/led/led.h"
  64. #include "app/vbat/vbat.h"
  65. #include "app/nfm/nfm_base.h"
  66. #include "stm32l1xx_hal_i2c.h"
  67. #include "drivers/keycontrol/keycontrol.h"
  68. #include "drivers/adc/adc.h"
  69. #include "drivers/power_management/power_management.h"
  70. #include "app/nfm/nfm_base.h"
  71. #include "core/sleep_and_exti.h"
  72. #include "core_cm3.h" // SCB, SCB_SHCSR_USGFAULTENA
  73. #include "stm32l1xx_hal_conf.h" // TICK_INT_PRIORITY()
  74. #include "stm32l1xx_hal_cortex.h" // HAL_NVIC_SetPriority()
  75. #include "core/config.h"
  76. #include "core/config_pins.h"
  77. #include "core/debugpins.h"
  78. #include "core/csect.h"
  79. #include "my_assert.h"
  80. #include "static_assert.h"
  81. #if CONFIG_NOIRQ_SNPRINTF
  82. #include <stdarg.h> // va_list
  83. #endif
  84. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  85. #if CONFIG_STACKOVERFLOW_CONTROLSECTION
  86. // CSTACK overflow protection/notification section
  87. // Place specially prepared signature into the area before CSTACK block in RAM
  88. // to provide easy control of stack overflow.
  89. extern void size_cstack_protect; // Size of area: see .icf
  90. #pragma location=".cstacktail" // Protection section name
  91. // Fill the area with signature:
  92. static __root volatile uint32_t cstacktail[8] = {
  93. 0xEFBEADDE, 0xEFBEADDE, 0xEFBEADDE, 0xEFBEADDE, 0xEFBEADDE, 0xEFBEADDE, 0xEFBEADDE, 0xEFBEADDE
  94. };
  95. #endif
  96. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  97. #if DEBUG_TRACE
  98. static volatile int debugcnt = 0;
  99. static int getdebugcnt()
  100. {
  101. int r = 0;
  102. __DI__ r = debugcnt; debugcnt++; __EI__
  103. return r;
  104. }
  105. #define MAX_TRACEPOINTS 16
  106. static volatile int g_debug_tracepoints[ MAX_TRACEPOINTS ] = {0};
  107. void debug_trace( int idx )
  108. {
  109. if( idx < MAX_TRACEPOINTS )
  110. g_debug_tracepoints[idx] = getdebugcnt();
  111. }
  112. #endif
  113. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  114. static volatile bool xUSBPlugged = false;
  115. static volatile bool xUSBActive = false;
  116. static volatile bool xAutomatActive = false;
  117. //static volatile bool g_bHarmUpEnable = false;
  118. //static volatile uint32_t g_nHarmUpTimestamp = 0ul;
  119. #if CONFIG_REBOOT_FEATURE
  120. static volatile bool g_RebootRequest = false;
  121. #endif
  122. #if CONFIG_VBATMON
  123. static volatile uint32_t xVBatVoltage = 0;
  124. #endif
  125. #if CONFIG_USB
  126. static eNFMUSBInterface_t usbDeviceInterface =
  127. #if CONFIG_USB_OVERRIDE_IFACE_USBTMC
  128. eNFM_IfaceUSBTMC;
  129. #else
  130. eNFM_IfaceUSBVendor;
  131. #endif
  132. #endif
  133. void SystemClock_Config(void);
  134. void SystemClock_Config_Low(void);
  135. #if CONFIG_EXTREME_LOWPOWTEST
  136. void GPIO_Init_LowPowerTest(void)
  137. {
  138. /* GPIO Ports Clock Enable */
  139. __HAL_RCC_GPIOC_CLK_ENABLE();
  140. __HAL_RCC_GPIOH_CLK_ENABLE();
  141. __HAL_RCC_GPIOA_CLK_ENABLE();
  142. __HAL_RCC_GPIOB_CLK_ENABLE();
  143. GPIO_InitTypeDef GPIO_InitStruct = {0};
  144. /*Configure all GPIO pins of port C*/
  145. GPIO_InitStruct.Pin = GPIO_PIN_All;
  146. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  147. GPIO_InitStruct.Pull = GPIO_NOPULL;
  148. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  149. /*Configure all GPIO pins of port A ,except PA13 and PA14 (SWD)*/
  150. GPIO_InitStruct.Pin = GPIO_PIN_All & (~(GPIO_PIN_13|GPIO_PIN_14));
  151. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  152. GPIO_InitStruct.Pull = GPIO_NOPULL;
  153. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  154. /*Configure all GPIO pins of port B*/
  155. GPIO_InitStruct.Pin = GPIO_PIN_All;
  156. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  157. GPIO_InitStruct.Pull = GPIO_NOPULL;
  158. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  159. /* GPIO Ports Clock Enable */
  160. __HAL_RCC_GPIOC_CLK_DISABLE();
  161. __HAL_RCC_GPIOH_CLK_DISABLE();
  162. __HAL_RCC_GPIOA_CLK_DISABLE();
  163. __HAL_RCC_GPIOB_CLK_DISABLE();
  164. }
  165. #endif
  166. #if CONFIG_EXTREME_LOWPOWTEST
  167. static void main_extreme_low_power()
  168. {
  169. __HAL_RCC_COMP_CLK_ENABLE();
  170. __HAL_RCC_SYSCFG_CLK_ENABLE();
  171. __HAL_RCC_PWR_CLK_ENABLE();
  172. GPIO_Init_LowPowerTest();
  173. SystemClock_Config_Low();
  174. SleepManagerHandle.Init();
  175. while(1)
  176. SleepManagerHandle.Sleep();
  177. }
  178. #endif
  179. /**
  180. * @brief The application entry point.
  181. * @retval int
  182. */
  183. #if CONFIG_USB_DYNAMIC_CLOCK_ADOPT && CONFIG_USB
  184. static void SysConfig_USBConnected()
  185. {
  186. __DI__
  187. // need to disable interrupts to setup SystemTick correctly
  188. SystemClock_Config();
  189. #if CONFIG_SYSTICK == CONFIG_SYSTICK_NORMAL
  190. #if CONFIG_SYSTICK_BOOST_MULTIPLIER
  191. HAL_SYSTICK_Config( SystemCoreClock / 1000 / (CONFIG_SYSTICK_BOOST_MULTIPLIER) ); // CONFIG_SYSTICK_BOOST_MULTIPLIER times per 1ms
  192. HAL_NVIC_SetPriority( SysTick_IRQn, TICK_INT_PRIORITY, 0 );
  193. #else
  194. HAL_SYSTICK_Config( SystemCoreClock / 1000 ); // 1 times per 1ms
  195. HAL_NVIC_SetPriority( SysTick_IRQn, TICK_INT_PRIORITY, 0 );
  196. #endif
  197. #else
  198. HAL_SYSTICK_Config( SystemCoreClock / 1000 ); // 1 times per 1ms
  199. HAL_NVIC_SetPriority( SysTick_IRQn, TICK_INT_PRIORITY, 0 );
  200. #endif
  201. __EI__
  202. #if CONFIG_EXTMEM
  203. /* Reinitialize the external memory */
  204. ExtMemHandle.Init();
  205. #endif
  206. #if CONFIG_NFMBASECLASS
  207. #if CONFIG_USB_OVERRIDE_IFACE_USBTMC
  208. usbDeviceInterface = eNFM_IfaceUSBTMC;
  209. #else
  210. #if CONFIG_USB_OVERRIDE_IFACE_VENDOR
  211. usbDeviceInterface = eNFM_IfaceUSBVendor;
  212. #else
  213. if( ! NFMClass->methods.usbInterface.getInterface( &usbDeviceInterface ) )
  214. {
  215. usbDeviceInterface = eNFM_IfaceUSBVendor;
  216. }
  217. #endif
  218. #endif
  219. #else
  220. #if CONFIG_USB_OVERRIDE_IFACE_USBTMC
  221. usbDeviceInterface = eNFM_IfaceUSBTMC;
  222. #else
  223. usbDeviceInterface = eNFM_IfaceUSBVendor;
  224. #endif
  225. #endif
  226. USB_Device_Init( (eUSBDeviceInterface_t)usbDeviceInterface );
  227. }
  228. #endif
  229. #if CONFIG_USB_DYNAMIC_CLOCK_ADOPT && CONFIG_USB
  230. static void SysConfig_USBDisconnected()
  231. {
  232. USB_Device_DeInit();
  233. #if CONFIG_EXTMEM
  234. #if CONFIG_AUTOMAT_MODE == 0
  235. /* Deinitialize the external memory */
  236. ExtMemHandle.DeInit( true );
  237. #else
  238. // later...
  239. #endif
  240. #endif
  241. __DI__
  242. // need to disable interrupts to setup SystemTick correctly
  243. SystemClock_Config_Low();
  244. #if CONFIG_SYSTICK == CONFIG_SYSTICK_NORMAL
  245. #if CONFIG_SYSTICK_BOOST_MULTIPLIER
  246. HAL_SYSTICK_Config( SystemCoreClock / 1000 / (CONFIG_SYSTICK_BOOST_MULTIPLIER) ); // CONFIG_SYSTICK_BOOST_MULTIPLIER times per 1ms
  247. HAL_NVIC_SetPriority( SysTick_IRQn, TICK_INT_PRIORITY, 0 );
  248. #else
  249. HAL_SYSTICK_Config( SystemCoreClock / 1000 ); // 1 times per 1ms
  250. HAL_NVIC_SetPriority( SysTick_IRQn, TICK_INT_PRIORITY, 0 );
  251. #endif
  252. #else
  253. HAL_SYSTICK_Config( SystemCoreClock / 1000 ); // 1 times per 1ms
  254. HAL_NVIC_SetPriority( SysTick_IRQn, TICK_INT_PRIORITY, 0 );
  255. #endif
  256. __EI__
  257. }
  258. #endif
  259. #if CONFIG_USBIRQ_TEST
  260. void USBIRQ_Test()
  261. {
  262. #warning Òåñò îøèáêè ÷òåíèÿ ïàìÿòè ÷åðåç USB ïðè äëèòåëüíîì çàïðåùåíèè ïðåðûâàíèé USB
  263. HAL_NVIC_DisableIRQ( USB_LP_IRQn );
  264. for( size_t i = 0; i < 1024; ++i ) __NOP();
  265. HAL_NVIC_EnableIRQ( USB_LP_IRQn );
  266. }
  267. #endif
  268. //#if CONFIG_HARMUP_INTERVAL > 0
  269. //void harmup_init()
  270. //{
  271. // g_bHarmUpEnable = true;
  272. // g_nHarmUpTimestamp = HAL_GetTick();
  273. //
  274. // LEDHandle.SetHarmupStatus( !g_bHarmUpEnable );
  275. //}
  276. //
  277. //void harmup_serve()
  278. //{
  279. // if( g_bHarmUpEnable )
  280. // {
  281. // if( (HAL_GetTick() - g_nHarmUpTimestamp)/1000 > (CONFIG_HARMUP_INTERVAL) )
  282. // {
  283. // g_bHarmUpEnable = false;
  284. //
  285. // LEDHandle.SetHarmupStatus( !g_bHarmUpEnable );
  286. // }
  287. // }
  288. //}
  289. //#endif
  290. #if CONFIG_REBOOT_FEATURE
  291. void RebootRequest()
  292. {
  293. __DI__
  294. g_RebootRequest = true; // set global flag
  295. __EI__
  296. }
  297. #endif
  298. #if CONFIG_NOIRQ_SNPRINTF
  299. // @_snprintf
  300. // Limits the depth of stack usage during @snprintf by
  301. // disabling interrupts.
  302. int _snprintf( char * buffer, size_t szBuffer, const char * format, ... )
  303. {
  304. int rc;
  305. __DI__
  306. va_list args;
  307. va_start (args, format);
  308. rc = vsnprintf(buffer, szBuffer, format, args);
  309. va_end (args);
  310. __EI__
  311. return rc;
  312. }
  313. #endif
  314. int main(void)
  315. {
  316. // Validate the CSTACK protection area size
  317. my_assert( sizeof(cstacktail) == (size_t)(&size_cstack_protect) );
  318. /* MCU Configuration--------------------------------------------------------*/
  319. #if CONFIG_USB && CONFIG_AUTOMAT_MODE && CONFIG_AUTOMAT_MODE_USB_POWERBANK
  320. bool bEnumerateAwaiting = false;
  321. bool bUsbPowerBankConnected = false;
  322. #endif
  323. // Enable MemFault, BusFault and UsgFault handlers
  324. SCB->SHCSR |= SCB_SHCSR_USGFAULTENA;
  325. SCB->SHCSR |= SCB_SHCSR_BUSFAULTENA;
  326. SCB->SHCSR |= SCB_SHCSR_MEMFAULTENA;
  327. #if CONFIG_EXTREME_LOWPOWTEST
  328. main_extreme_low_power();
  329. #endif
  330. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  331. HAL_Init();
  332. // Initialize GPIO
  333. // Need init before SysClock and USB to determine the clock freq
  334. MX_GPIO_Init();
  335. #if CONFIG_ADC
  336. ADC1Handle.Init(eADCRes_12bit);
  337. #endif
  338. #if CONFIG_PM
  339. // Initialization and set of all pins except FPGA power control pin. It will be set separately after all init routine.
  340. PMHandle.Init();
  341. HAL_Wait_100us(500); // Çàäåðæêà 0.5 ñ
  342. #endif
  343. #if CONFIG_TABLE
  344. TableHandle.Init();
  345. #endif
  346. #if CONFIG_PM
  347. PMHandle.INPTRG_EN(false);
  348. #endif
  349. #if CONFIG_LEDS
  350. LEDHandle.Init();
  351. setLedColor(eLed_color_grn);
  352. #endif
  353. //DebugPins_Init();
  354. #if CONFIG_SLEEPMANAGER
  355. // Initialize Sleep/Wakeup manager
  356. // Need init before SysClock and USB to determine the clock freq
  357. SleepManagerHandle.Init();
  358. #endif
  359. // Initialize system clock
  360. #if CONFIG_USB_DYNAMIC_CLOCK_ADOPT && CONFIG_USB
  361. // First USB presense detecting...
  362. #if CONFIG_SLEEPMANAGER
  363. xUSBPlugged = SleepManagerHandle.GetUSBPlugged();
  364. #else
  365. xUSBPlugged = true;
  366. #endif
  367. __DI__
  368. // need to disable interrupts to setup SystemTick correctly
  369. if( xUSBPlugged )
  370. SystemClock_Config();
  371. else
  372. SystemClock_Config_Low();
  373. #if CONFIG_SYSTICK == CONFIG_SYSTICK_NORMAL
  374. #if CONFIG_SYSTICK_BOOST_MULTIPLIER
  375. HAL_SYSTICK_Config( SystemCoreClock / 1000 / (CONFIG_SYSTICK_BOOST_MULTIPLIER) ); // CONFIG_SYSTICK_BOOST_MULTIPLIER times per 1ms
  376. HAL_NVIC_SetPriority( SysTick_IRQn, TICK_INT_PRIORITY, 0 );
  377. #endif
  378. #endif
  379. __EI__
  380. #else
  381. __DI__
  382. // need to disable interrupts to setup SystemTick correctly
  383. /* Configure the system clock */
  384. #if (CONFIG_SYSCLOCK == CONFIG_SYSCLOCK_NORMAL) || (CONFIG_SYSCLOCK == CONFIG_SYSCLOCK_HIGH)
  385. SystemClock_Config();
  386. #endif
  387. #if CONFIG_SYSCLOCK == CONFIG_SYSCLOCK_LOW
  388. #if CONFIG_USB
  389. #warning Invalid clock while USB is used!
  390. #endif
  391. SystemClock_Config_Low();
  392. #endif
  393. //-----------------------
  394. #if CONFIG_USB //
  395. xUSBPlugged = true; //
  396. #endif //
  397. //-----------------------
  398. #if CONFIG_SYSTICK == CONFIG_SYSTICK_NORMAL
  399. #if CONFIG_SYSTICK_BOOST_MULTIPLIER
  400. HAL_SYSTICK_Config( SystemCoreClock / 1000 / (CONFIG_SYSTICK_BOOST_MULTIPLIER) ); // CONFIG_SYSTICK_BOOST_MULTIPLIER times per 1ms
  401. HAL_NVIC_SetPriority( SysTick_IRQn, TICK_INT_PRIORITY, 0 );
  402. #endif
  403. #endif
  404. __EI__
  405. #endif
  406. // Configure the system tick
  407. #if CONFIG_SYSTICK == CONFIG_SYSTICK_NORMAL
  408. #endif
  409. #if CONFIG_EXTMEM
  410. /* Initialize External Non-Volatile memory */
  411. ExtMemHandle.Init();
  412. #endif
  413. #if CONFIG_NFMBASECLASS
  414. nfmbase_init();
  415. #endif
  416. #if CONFIG_NFMBASECLASS && CONFIG_KEYSW
  417. #error The modes "CONFIG_NFMBASECLASS" and "CONFIG_KEYSW" are incompatible
  418. #endif
  419. #if CONFIG_SYSTICK == CONFIG_SYSTICK_LOW
  420. // power consumption debug
  421. HAL_SYSTICK_Config( 5000000 );
  422. HAL_NVIC_SetPriority( SysTick_IRQn, TICK_INT_PRIORITY, 0 );
  423. #endif
  424. #if CONFIG_USB_OVERRIDE_IFACE_USBTMC && CONFIG_USB_OVERRIDE_IFACE_VENDOR
  425. #error Invalid configuration: choose only one overriding interface.
  426. #endif
  427. #if !CONFIG_USB_USBTMC_ENABLE && CONFIG_USB_OVERRIDE_IFACE_USBTMC
  428. #error Invalid configuration: USBTMC is disabled and can not be overriding interface.
  429. #endif
  430. #if CONFIG_USB
  431. // Install USB Hook
  432. #if CONFIG_SLEEPMANAGER
  433. SleepManagerHandle.SetUSBWakeup( true );
  434. #endif
  435. #if CONFIG_USB_DYNAMIC_CLOCK_ADOPT
  436. if( xUSBPlugged )
  437. {
  438. #if CONFIG_NFMBASECLASS
  439. #if CONFIG_USB_OVERRIDE_IFACE_USBTMC
  440. usbDeviceInterface = eNFM_IfaceUSBTMC;
  441. #else
  442. #if CONFIG_USB_OVERRIDE_IFACE_VENDOR
  443. usbDeviceInterface = eNFM_IfaceVendor;
  444. #else
  445. if( ! NFMClass->methods.usbInterface.getInterface( &usbDeviceInterface ) )
  446. {
  447. usbDeviceInterface = eNFM_IfaceUSBVendor;
  448. }
  449. #endif
  450. #endif
  451. #else
  452. #if CONFIG_USB_OVERRIDE_IFACE_USBTMC
  453. usbDeviceInterface = eNFM_IfaceUSBTMC;
  454. #else
  455. usbDeviceInterface = eNFM_IfaceUSBVendor;
  456. #endif
  457. #endif
  458. // Initialize USB
  459. USB_Device_Init( (eUSBDeviceInterface_t)usbDeviceInterface );
  460. }
  461. #else
  462. #if CONFIG_NFMBASECLASS
  463. #if CONFIG_USB_OVERRIDE_IFACE_USBTMC
  464. usbDeviceInterface = eNFM_IfaceUSBTMC;
  465. NFMClass->methods.usbInterface.setInterface( usbDeviceInterface );
  466. #else
  467. #if CONFIG_USB_OVERRIDE_IFACE_VENDOR
  468. usbDeviceInterface = eNFM_IfaceUSBVendor;
  469. NFMClass->methods.usbInterface.setInterface( usbDeviceInterface );
  470. #else
  471. if( ! NFMClass->methods.usbInterface.getInterface( &usbDeviceInterface ) )
  472. {
  473. usbDeviceInterface = eNFM_IfaceUSBVendor;
  474. }
  475. #endif
  476. #endif
  477. #else
  478. #if CONFIG_USB_OVERRIDE_IFACE_USBTMC
  479. usbDeviceInterface = eNFM_IfaceUSBTMC;
  480. #else
  481. usbDeviceInterface = eNFM_IfaceUSBVendor;
  482. #endif
  483. #endif
  484. // Initialize USB once and forever
  485. USB_Device_Init( (eUSBDeviceInterface_t)usbDeviceInterface );
  486. #endif
  487. #endif
  488. #if CONFIG_AUTOMAT_MODE
  489. #if CONFIG_USB_DYNAMIC_CLOCK_ADOPT && CONFIG_USB
  490. if( !xUSBPlugged )
  491. {
  492. #endif
  493. #if CONFIG_SYSTICK_BOOST_MULTIPLIER
  494. xAutomatActive = NFMAutomatHandle.Init( CONFIG_SYSTICK_BOOST_MULTIPLIER );
  495. #else
  496. xAutomatActive = NFMAutomatHandle.Init( 1 );
  497. #endif
  498. #if CONFIG_LEDS
  499. LEDHandle.SetAutomatAlarm( !xAutomatActive );
  500. #endif
  501. #if CONFIG_USB_DYNAMIC_CLOCK_ADOPT && CONFIG_USB
  502. }
  503. #endif
  504. #endif
  505. #if CONFIG_AUTOMAT_MODE && CONFIG_AUTOMAT_MODE_USB_POWERBANK
  506. if( xUSBPlugged )
  507. {
  508. #warning Feature: Çàïðîøåíà ôóíêöèÿ ðàáîòû îò USB-Power-Bank, ò.å. ïèòàíèå íà ðàçúåì USB ïîäàíî, è ñîáûòèå Plugged ñðàáîòàëî, íî òðåáóåòñÿ ðàáîòàòü â ðåæèìå Automat åñëè íåò ýíóìåðàöèè â òå÷åíèå íåêîòîðîãî âðåìåíè.
  509. bEnumerateAwaiting = true; // mode: awaiting for enumerating
  510. UsbApplicationEnumSpyHandle.startEnumerateTimeout();
  511. }
  512. #endif
  513. #if CONFIG_LEDS
  514. LEDHandle.SetUSBConnectivity( xUSBPlugged );
  515. #endif
  516. #if CONFIG_NFMBASECLASS && CONFIG_NFMBASECLASS_TEST
  517. nfmbase_test();
  518. #endif
  519. #if CONFIG_SCPI_TEST
  520. extern void scpi_parser_test();
  521. extern void scpi_command_test();
  522. //scpi_parser_test();
  523. scpi_command_test();
  524. #endif
  525. #if CONFIG_HARMUP_INTERVAL > 0
  526. // initialize HarmUp procedure
  527. LEDHandle.harmup_init();
  528. #else
  529. LEDHandle.SetHarmupStatus( true );
  530. #endif
  531. #if CONFIG_REBOOT_FEATURE
  532. uint32_t l_RebootRequestStamp = 0;
  533. bool l_RebootRequest = false;
  534. #endif
  535. HAL_GPIO_WritePin( GPIOA, GPIO_PIN_3, GPIO_PIN_SET); //POWER ON FPGA BOARD
  536. while (1)
  537. {
  538. #if CONFIG_USB
  539. #if CONFIG_EXTMEM
  540. // serve USB application
  541. if( xUSBPlugged )
  542. {
  543. usb_application_flash_serve();
  544. }
  545. #endif
  546. #endif
  547. #if CONFIG_ADC && CONFIG_PM
  548. //PMHandle.PM_Serve();
  549. #endif
  550. #if CONFIG_SLEEPMANAGER && CONFIG_SLEEPMANAGER_EXTRASLEEP
  551. SleepManagerHandle.Sleep();
  552. #endif
  553. #if CONFIG_USB_DYNAMIC_CLOCK_ADOPT && CONFIG_USB
  554. #if CONFIG_SLEEPMANAGER
  555. SleepManagerHandle.Sleep();
  556. #endif
  557. #if CONFIG_USBIRQ_TEST
  558. USBIRQ_Test();
  559. #endif
  560. #if CONFIG_HARMUP_INTERVAL > 0
  561. // serve for HarmUp procedure
  562. LEDHandle.harmup_serve();
  563. #endif
  564. {
  565. #if CONFIG_SLEEPMANAGER
  566. bool temp__xUSBPlugged = SleepManagerHandle.GetUSBPlugged();
  567. xUSBActive = SleepManagerHandle.GetUSBActive();
  568. #else
  569. bool temp__xUSBPlugged = true;
  570. xUSBActive = true;
  571. #endif
  572. #if CONFIG_AUTOMAT_MODE && CONFIG_AUTOMAT_MODE_USB_POWERBANK
  573. if( !temp__xUSBPlugged && bUsbPowerBankConnected )
  574. {
  575. // When USB-unplug event detected -> reset @bUsbPowerBankConnected
  576. bUsbPowerBankConnected = false;
  577. }
  578. if( bEnumerateAwaiting && temp__xUSBPlugged )
  579. {
  580. // USB is plugged and the device waits for enumeration
  581. if( UsbApplicationEnumSpyHandle.checkEnumerateTimeout() )
  582. {
  583. bEnumerateAwaiting = false;
  584. // The host had not enumerate the device for a long time
  585. bUsbPowerBankConnected = true;
  586. }
  587. }
  588. #endif
  589. #if CONFIG_AUTOMAT_MODE && CONFIG_AUTOMAT_MODE_USB_POWERBANK
  590. // Emulate USB unplugged when USB-Power-Bank mode is active
  591. if( bUsbPowerBankConnected )
  592. {
  593. // Emulate USB unplugged
  594. temp__xUSBPlugged = false;
  595. }
  596. #endif
  597. #if CONFIG_REBOOT_FEATURE
  598. {
  599. __DI__
  600. if( g_RebootRequest )
  601. {
  602. l_RebootRequest = true; // set local flag
  603. #if CONFIG_REBOOT_FEATURE
  604. l_RebootRequestStamp = HAL_GetTick();
  605. #endif
  606. g_RebootRequest = false; // reset global flag
  607. }
  608. __EI__
  609. if( l_RebootRequest )
  610. {
  611. #if CONFIG_REBOOT_FEATURE
  612. if( HAL_GetTick() - l_RebootRequestStamp > CONFIG_REBOOT_FEATURE_DELAY )
  613. {
  614. #endif
  615. if( xUSBPlugged && xUSBActive )
  616. {
  617. temp__xUSBPlugged = false; // force reset PLUG indicator
  618. }
  619. #if CONFIG_REBOOT_FEATURE
  620. }
  621. #endif
  622. }
  623. }
  624. #endif
  625. if( xUSBPlugged != temp__xUSBPlugged )
  626. {
  627. if( temp__xUSBPlugged )
  628. {
  629. #if CONFIG_AUTOMAT_MODE
  630. NFMAutomatHandle.DeInit();
  631. xAutomatActive = false;
  632. #endif
  633. #if CONFIG_LEDS
  634. LEDHandle.SetAutomatAlarm( false );
  635. #endif
  636. SysConfig_USBConnected();
  637. #if CONFIG_USB && CONFIG_AUTOMAT_MODE && CONFIG_AUTOMAT_MODE_USB_POWERBANK
  638. #warning Feature: Çàïðîøåíà ôóíêöèÿ ðàáîòû îò USB-Power-Bank, ò.å. ïèòàíèå íà ðàçúåì USB ïîäàíî, è ñîáûòèå Plugged ñðàáîòàëî, íî òðåáóåòñÿ ðàáîòàòü â ðåæèìå Automat åñëè íåò ýíóìåðàöèè â òå÷åíèå íåêîòîðîãî âðåìåíè.
  639. bEnumerateAwaiting = true; // mode: awaiting for enumerating
  640. UsbApplicationEnumSpyHandle.startEnumerateTimeout();
  641. #endif
  642. }
  643. else
  644. {
  645. SysConfig_USBDisconnected();
  646. #if CONFIG_AUTOMAT_MODE && CONFIG_AUTOMAT_MODE_USB_POWERBANK
  647. UsbApplicationEnumSpyHandle.stopEnumerateTimeout();
  648. bEnumerateAwaiting = false;
  649. #endif
  650. #if CONFIG_AUTOMAT_MODE
  651. #if CONFIG_SYSTICK_BOOST_MULTIPLIER
  652. xAutomatActive = NFMAutomatHandle.Init( CONFIG_SYSTICK_BOOST_MULTIPLIER );
  653. #else
  654. xAutomatActive = NFMAutomatHandle.Init( 1 );
  655. #endif
  656. #if CONFIG_LEDS
  657. LEDHandle.SetAutomatAlarm( !xAutomatActive );
  658. #endif
  659. #endif
  660. #if CONFIG_EXTMEM
  661. #if CONFIG_AUTOMAT_MODE
  662. /* Deinitialize the external memory */
  663. ExtMemHandle.DeInit( true );
  664. #endif
  665. #endif
  666. }
  667. #if CONFIG_SYSTICK == CONFIG_SYSTICK_LOW
  668. // power consumption debug
  669. HAL_SYSTICK_Config( 5000000 );
  670. HAL_NVIC_SetPriority( SysTick_IRQn, TICK_INT_PRIORITY, 0 );
  671. #endif
  672. xUSBPlugged = temp__xUSBPlugged;
  673. #if CONFIG_LEDS
  674. LEDHandle.SetUSBConnectivity( xUSBPlugged );
  675. #endif
  676. }
  677. #if CONFIG_REBOOT_FEATURE
  678. if( l_RebootRequest )
  679. {
  680. #if CONFIG_REBOOT_FEATURE
  681. if( HAL_GetTick() - l_RebootRequestStamp > CONFIG_REBOOT_FEATURE_DELAY )
  682. {
  683. #endif
  684. __DI__
  685. #if CONFIG_EXTMEM
  686. /* Deinitialize External Non-Volatile memory */
  687. ExtMemHandle.DeInit( true );
  688. #endif
  689. // Deinitialize USB
  690. USB_Device_DeInit();
  691. HAL_NVIC_SystemReset();
  692. __EI__
  693. while(1);
  694. #if CONFIG_REBOOT_FEATURE
  695. }
  696. #endif
  697. }
  698. #endif
  699. }
  700. #else
  701. #if CONFIG_SLEEPMANAGER
  702. xUSBPlugged = SleepManagerHandle.GetUSBPlugged();
  703. xUSBActive = SleepManagerHandle.GetUSBActive();
  704. SleepManagerHandle.Sleep();
  705. #else
  706. // ACM2543: USB is always connected
  707. /*xUSBPlugged = false;*/
  708. /*xUSBActive = false;*/
  709. #endif
  710. #if CONFIG_HARMUP_INTERVAL > 0
  711. // serve for HarmUp procedure
  712. LEDHandle.harmup_serve();
  713. #endif
  714. #if CONFIG_REBOOT_FEATURE
  715. {
  716. __DI__
  717. if( g_RebootRequest )
  718. {
  719. l_RebootRequest = true; // set local flag
  720. #if CONFIG_REBOOT_FEATURE
  721. l_RebootRequestStamp = HAL_GetTick();
  722. #endif
  723. g_RebootRequest = false; // reset global flag
  724. }
  725. __EI__
  726. if( l_RebootRequest )
  727. {
  728. #if CONFIG_REBOOT_FEATURE
  729. if( HAL_GetTick() - l_RebootRequestStamp > CONFIG_REBOOT_FEATURE_DELAY )
  730. {
  731. #endif
  732. __DI__
  733. #if CONFIG_EXTMEM
  734. /* Deinitialize External Non-Volatile memory */
  735. ExtMemHandle.DeInit( true );
  736. #endif
  737. // Deinitialize USB
  738. USB_Device_DeInit();
  739. HAL_NVIC_SystemReset();
  740. __EI__
  741. while(1);
  742. #if CONFIG_REBOOT_FEATURE
  743. }
  744. #endif
  745. }
  746. }
  747. #endif
  748. #endif
  749. #if CONFIG_AUTOMAT_MODE
  750. if( !xUSBPlugged )
  751. {
  752. #if CONFIG_TSENSOR
  753. int8_t automat_temperature = THERMO_SENSOR_AVGTEMP_TO_INT( ThermoSensor.GetTempAVG() );
  754. #else
  755. int8_t automat_temperature = THERMO_SENSOR_AVGTEMP_TO_INT( THERMO_SENSOR_INVALID_TEMP_VALUE );
  756. #endif
  757. NFMAutomatHandle.TemperatureUpdate( automat_temperature, ThermoSensor.GetReady() );
  758. }
  759. #endif
  760. }
  761. }
  762. /**
  763. * @brief System Clock Configuration
  764. * @retval None
  765. */
  766. #if (CONFIG_SYSCLOCK == CONFIG_SYSCLOCK_NORMAL) || (CONFIG_SYSCLOCK == CONFIG_SYSCLOCK_HIGH) || CONFIG_USB_DYNAMIC_CLOCK_ADOPT
  767. void SystemClock_Config(void)
  768. {
  769. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  770. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  771. /**Configure the main internal regulator output voltage
  772. */
  773. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  774. /**Initializes the CPU, AHB and APB busses clocks
  775. */
  776. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  777. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  778. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  779. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  780. #if CONFIG_CRYSTAL == CONFIG_CRYSTAL_12MHZ
  781. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL8;
  782. #elif CONFIG_CRYSTAL == CONFIG_CRYSTAL_16MHZ
  783. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
  784. #elif CONFIG_CRYSTAL == CONFIG_CRYSTAL_8MHZ
  785. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
  786. #elif CONFIG_CRYSTAL == CONFIG_CRYSTAL_4MHZ
  787. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL24;
  788. #else
  789. #error Invalid Crystal Ocsillator Frequency specified
  790. #endif
  791. RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV3;
  792. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  793. {
  794. Error_Handler();
  795. }
  796. /**Initializes the CPU, AHB and APB busses clocks
  797. */
  798. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  799. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  800. #if CONFIG_SYSCLOCK == CONFIG_SYSCLOCK_HIGH
  801. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // two times greater than RCC_SYSCLKSOURCE_HSE (see PLL settings)
  802. #else
  803. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
  804. #endif
  805. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // System Clock Divider
  806. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  807. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  808. #if (PREFETCH_ENABLE == 0)
  809. __HAL_FLASH_PREFETCH_BUFFER_DISABLE();
  810. #endif /* PREFETCH_ENABLE */
  811. #if CONFIG_FLASH_64BIT_MANAGE
  812. // NOTE: Enabling 64-bit access must be performed before
  813. // changing the FLASH LATENCY and changing the CPU clock
  814. // Refer: RM0038, STM32L151CB User Manual, ch. 3.3.1. DocID15965
  815. __HAL_FLASH_ACC64_ENABLE(); // enable 64-bit flash access to enhance performance
  816. #endif
  817. #if CONFIG_SYSCLOCK == CONFIG_SYSCLOCK_HIGH
  818. // On High frequency it is required to add one cycle to flash access latency.
  819. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  820. {
  821. Error_Handler();
  822. }
  823. #else
  824. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  825. {
  826. Error_Handler();
  827. }
  828. #endif
  829. #if (PREFETCH_ENABLE != 0)
  830. __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
  831. #endif /* PREFETCH_ENABLE */
  832. //HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_NOCLOCK, RCC_MCODIV_1);
  833. /*
  834. { // Configure GPIO pin for MCO function
  835. GPIO_InitTypeDef GPIO_InitStruct = {0};
  836. GPIO_InitStruct.Pin = CONFIG_PIN__MCO;
  837. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  838. GPIO_InitStruct.Pull = GPIO_NOPULL;
  839. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  840. GPIO_InitStruct.Alternate = CONFIG_AF__MCO;
  841. HAL_GPIO_Init(CONFIG_PORT__MCO, &GPIO_InitStruct);
  842. }
  843. */
  844. }
  845. #endif
  846. #if CONFIG_VBATMON
  847. bool SystemClock_HSI( bool state )
  848. {
  849. bool bRet = false;
  850. {
  851. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  852. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  853. RCC_OscInitStruct.HSIState = ((state)?RCC_HSI_ON:RCC_HSI_OFF);
  854. RCC_OscInitStruct.HSICalibrationValue = 0;
  855. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  856. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  857. {
  858. Error_Handler();
  859. }
  860. else
  861. {
  862. bRet = true;
  863. }
  864. }
  865. return true;
  866. }
  867. #else
  868. bool SystemClock_HSI( bool state ){ (void)state; return false; }
  869. #endif
  870. #if (CONFIG_SYSCLOCK == CONFIG_SYSCLOCK_LOW) || CONFIG_USB_DYNAMIC_CLOCK_ADOPT || CONFIG_EXTREME_LOWPOWTEST
  871. void SystemClock_Config_Low(void)
  872. {
  873. {
  874. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  875. /**Configure the main internal regulator output voltage
  876. */
  877. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  878. /**Initializes the CPU, AHB and APB busses clocks
  879. */
  880. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  881. RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  882. RCC_OscInitStruct.MSICalibrationValue = 0;
  883. #if CONFIG_MSI_FREQ == CONFIG_MSI_FREQ_4MHZ
  884. RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
  885. #elif CONFIG_MSI_FREQ == CONFIG_MSI_FREQ_2MHZ
  886. RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5;
  887. #elif CONFIG_MSI_FREQ == CONFIG_MSI_FREQ_1MHZ
  888. RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_4;
  889. #else
  890. #warning MSI Frequency set to the default
  891. RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5; // 2MHz
  892. #endif
  893. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  894. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  895. {
  896. Error_Handler();
  897. }
  898. }
  899. __HAL_FLASH_PREFETCH_BUFFER_DISABLE();
  900. {
  901. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  902. /**Initializes the CPU, AHB and APB busses clocks
  903. */
  904. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  905. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  906. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
  907. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // System Clock Divider
  908. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; //RCC_HCLK_DIV1;
  909. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; //RCC_HCLK_DIV1;
  910. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) //FLASH_LATENCY_1?
  911. {
  912. Error_Handler();
  913. }
  914. }
  915. {
  916. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  917. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  918. RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
  919. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_OFF;
  920. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  921. {
  922. Error_Handler();
  923. }
  924. }
  925. #if CONFIG_FLASH_64BIT_MANAGE
  926. // Disable 64-bit FLASH access, reduce power consumption
  927. // NOTE: Disable 64-bit access must be performed after
  928. // changing the FLASH LATENCY and changing the CPU clock
  929. // Refer: RM0038, STM32L151CB User Manual, ch. 3.3.1. DocID15965
  930. __HAL_FLASH_ACC64_DISABLE(); // disable 64-bit flash access to enhance performance
  931. #endif
  932. //HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_NOCLOCK, RCC_MCODIV_1);
  933. // { // DeConfigure GPIO pin (MCO function)
  934. // GPIO_InitTypeDef GPIO_InitStruct = {0};
  935. // GPIO_InitStruct.Pin = CONFIG_PIN__MCO;
  936. // GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  937. // GPIO_InitStruct.Pull = GPIO_NOPULL;
  938. // GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  939. // HAL_GPIO_Init(CONFIG_PORT__MCO, &GPIO_InitStruct);
  940. // }
  941. }
  942. #endif
  943. /**
  944. * @brief This function is executed in case of error occurrence.
  945. * @retval None
  946. */
  947. void Error_Handler(void)
  948. {
  949. /* USER CODE BEGIN Error_Handler_Debug */
  950. /* User can add his own implementation to report the HAL error return state */
  951. /* USER CODE END Error_Handler_Debug */
  952. }
  953. void HAL_Wait_us( size_t n )
  954. {
  955. #warning need to debug. Sometimes the delay is wrong! especially for 1us
  956. HAL_Wait_1us( 10*n ); // need to debug. Sometimes the delay is wrong! especially for 1us
  957. }
  958. void HAL_Wait_10us( size_t n )
  959. {
  960. HAL_Wait_1us( 10*n );
  961. }
  962. void HAL_Wait_100us( size_t n )
  963. {
  964. HAL_Wait_1us( 100*n );
  965. }
  966. #pragma optimize=speed
  967. static void HAL_Wait_1us( size_t us )
  968. {
  969. int cycles = (1 + (HAL_RCC_GetHCLKFreq() >> 20));
  970. if( cycles <= 4 ) // up to 4MHZ clock
  971. {
  972. void sys_delayus_up4MHZ( int us, int cycles );
  973. sys_delayus_up4MHZ( us, cycles );
  974. }
  975. else
  976. if( cycles <= 8 ) // up to 8MHZ clock
  977. {
  978. void sys_delayus_up8MHZ( int us, int cycles );
  979. sys_delayus_up8MHZ( us, cycles );
  980. }
  981. else
  982. if( cycles <= 16 ) // up to 16MHZ clock
  983. {
  984. void sys_delayus_up16MHZ( int us, int cycles );
  985. sys_delayus_up16MHZ( us, cycles );
  986. }
  987. else
  988. {
  989. void sys_delayus_up96MHZ( int us, int cycles );
  990. sys_delayus_up96MHZ( us, cycles );
  991. }
  992. }
  993. #pragma optimize=none
  994. static void sys_delayus_up4MHZ( int us, int cycles )
  995. {
  996. int m = cycles * us;
  997. for( int i = cycles + cycles; i < m; ++i )
  998. asm("nop");
  999. }
  1000. #pragma optimize=none
  1001. static void sys_delayus_up8MHZ( int us, int cycles ) // 4...8MHz
  1002. {
  1003. for( int i = (cycles*us)>>2; i > (us>>1); --i );
  1004. }
  1005. #pragma optimize=none
  1006. static void sys_delayus_up16MHZ( int us, int cycles ) // 8...16MHz
  1007. {
  1008. for( int i = (cycles*us)>>2; i > (us>>1); --i );
  1009. }
  1010. #pragma optimize=none
  1011. static void sys_delayus_up96MHZ( int us, int cycles )
  1012. {
  1013. for( int i = (cycles*us)>>2; i > (us>>1); --i );
  1014. }
  1015. #ifdef USE_FULL_ASSERT
  1016. /**
  1017. * @brief Reports the name of the source file and the source line number
  1018. * where the assert_param error has occurred.
  1019. * @param file: pointer to the source file name
  1020. * @param line: assert_param error line source number
  1021. * @retval None
  1022. */
  1023. void assert_failed(uint8_t *file, uint32_t line)
  1024. {
  1025. /* User can add his own implementation to report the file name and line number,
  1026. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  1027. while(1)
  1028. {
  1029. HAL_NVIC_SystemReset();
  1030. }
  1031. }
  1032. #endif /* USE_FULL_ASSERT */
  1033. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/