system_at32f4xx.c 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457
  1. /**
  2. ******************************************************************************
  3. * @file system_at32f4xx.c
  4. * @author Artery Technology
  5. * @version V1.0.0
  6. * @date 2019-05-27
  7. * @brief CMSIS Cortex-M4 system source file
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13. * TIME. AS A RESULT, ARTERYTEK SHALL NOT BE HELD LIABLE FOR ANY
  14. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17. *
  18. * <h2><center>&copy; COPYRIGHT 2018 ArteryTek</center></h2>
  19. ******************************************************************************
  20. */
  21. /** @addtogroup CMSIS
  22. * @{
  23. */
  24. /** @addtogroup at32f4xx_system
  25. * @{
  26. */
  27. /** @addtogroup at32f4xx_System_Private_Includes
  28. * @{
  29. */
  30. #include "at32f4xx.h"
  31. /**
  32. * @}
  33. */
  34. /** @addtogroup at32f4xx_System_Private_TypesDefinitions
  35. * @{
  36. */
  37. /**
  38. * @}
  39. */
  40. /** @addtogroup at32f4xx_System_Private_Defines
  41. * @{
  42. */
  43. /*!< Uncomment the line corresponding to the desired System clock (SYSCLK)
  44. frequency (after reset the HSI is used as SYSCLK source)
  45. IMPORTANT NOTE:
  46. ==============
  47. 1. After each device reset the HSI is used as System clock source.
  48. 2. Please make sure that the selected System clock doesn't exceed your device's
  49. maximum frequency.
  50. 3. If none of the define below is enabled, the HSI is used as System clock
  51. source.
  52. 4. The System clock configuration functions provided within this file assume that:
  53. - For at32f4xx devices, an external 8MHz crystal is used to drive the System clock.
  54. If you are using different crystal you have to adapt those functions accordingly.
  55. Clock (MHz)
  56. PLL from HSE or HSI
  57. SYSCLK HCLK PCLK2 PCLK1
  58. 24 24 24 24
  59. 36 36 36 36
  60. 48 48 48 24
  61. 56 56 56 28
  62. 72 72 72 36
  63. 96 96 48 48
  64. 108 108 54 54
  65. 120 120 60 60
  66. 144 144 72 72
  67. 150 150 75 75
  68. 168 168 84 84
  69. 176 176 88 88
  70. 192 192 96 96
  71. 200 200 100 100
  72. 224 224 112 112
  73. 240 240 120 120
  74. */
  75. #if defined (AT32F403xx) || defined (AT32F413xx) || \
  76. defined (AT32F415xx) || defined (AT32F403Axx)|| \
  77. defined (AT32F407xx)
  78. /* #define SYSCLK_FREQ_HSE HSE_VALUE */
  79. /* #define SYSCLK_FREQ_24MHz 24000000 */
  80. /* #define SYSCLK_FREQ_36MHz 36000000 */
  81. /* #define SYSCLK_FREQ_48MHz 48000000 */
  82. /* #define SYSCLK_FREQ_56MHz 56000000 */
  83. /* #define SYSCLK_FREQ_72MHz 72000000 */
  84. /* #define SYSCLK_FREQ_96MHz 96000000 */
  85. /* #define SYSCLK_FREQ_108MHz 108000000 */
  86. /* #define SYSCLK_FREQ_120MHz 120000000 */
  87. /* #define SYSCLK_FREQ_144MHz 144000000 */
  88. /* #define SYSCLK_FREQ_24MHz_HSI 24000000 */
  89. /* #define SYSCLK_FREQ_36MHz_HSI 36000000 */
  90. /* #define SYSCLK_FREQ_48MHz_HSI 48000000 */
  91. /* #define SYSCLK_FREQ_56MHz_HSI 56000000 */
  92. /* #define SYSCLK_FREQ_72MHz_HSI 72000000 */
  93. /* #define SYSCLK_FREQ_96MHz_HSI 96000000 */
  94. /* #define SYSCLK_FREQ_108MHz_HSI 108000000 */
  95. /* #define SYSCLK_FREQ_120MHz_HSI 120000000 */
  96. /* #define SYSCLK_FREQ_144MHz_HSI 144000000 */
  97. #endif
  98. #if defined (AT32F415xx)
  99. /* #define SYSCLK_FREQ_150MHz 150000000 */
  100. /* #define SYSCLK_FREQ_150MHz_HSI 150000000 */
  101. #endif
  102. #if defined (AT32F403xx) || defined (AT32F413xx) || \
  103. defined (AT32F403Axx)|| defined (AT32F407xx)
  104. /* #define SYSCLK_FREQ_168MHz 168000000 */
  105. /* #define SYSCLK_FREQ_176MHz 176000000 */
  106. /* #define SYSCLK_FREQ_192MHz 192000000 */
  107. /* #define SYSCLK_FREQ_200MHz 200000000 */
  108. /* #define SYSCLK_FREQ_168MHz_HSI 168000000 */
  109. /* #define SYSCLK_FREQ_176MHz_HSI 176000000 */
  110. /* #define SYSCLK_FREQ_192MHz_HSI 192000000 */
  111. /* #define SYSCLK_FREQ_200MHz_HSI 200000000 */
  112. #endif
  113. #if defined (AT32F403Axx)|| defined (AT32F407xx)
  114. /* #define SYSCLK_FREQ_224MHz 224000000 */
  115. #define SYSCLK_FREQ_240MHz 240000000
  116. /* #define SYSCLK_FREQ_224MHz_HSI 224000000 */
  117. /* #define SYSCLK_FREQ_240MHz_HSI 240000000 */
  118. #endif
  119. /*!< Uncomment the following line if you need to use external SRAM mounted
  120. (AT32 High density and XL-density devices) as data memory */
  121. /* #define DATA_IN_ExtSRAM */
  122. /*!< Uncomment the following line if you need to relocate your vector Table in
  123. Internal SRAM. */
  124. /* #define VECT_TAB_SRAM */
  125. #define VECT_TAB_OFFSET 0x0 /*!< Vector Table base offset field.
  126. This value must be a multiple of 0x200. */
  127. /**
  128. * @}
  129. */
  130. /** @addtogroup at32f4xx_System_Private_Macros
  131. * @{
  132. */
  133. /**
  134. * @}
  135. */
  136. /** @addtogroup at32f4xx_System_Private_Variables
  137. * @{
  138. */
  139. /*******************************************************************************
  140. * Clock Definitions
  141. *******************************************************************************/
  142. #ifdef SYSCLK_FREQ_HSE
  143. uint32_t SystemCoreClock = SYSCLK_FREQ_HSE; /*!< System Clock Frequency (Core Clock) */
  144. #elif defined SYSCLK_FREQ_24MHz
  145. uint32_t SystemCoreClock = SYSCLK_FREQ_24MHz; /*!< System Clock Frequency (Core Clock) */
  146. #elif defined SYSCLK_FREQ_36MHz
  147. uint32_t SystemCoreClock = SYSCLK_FREQ_36MHz; /*!< System Clock Frequency (Core Clock) */
  148. #elif defined SYSCLK_FREQ_48MHz
  149. uint32_t SystemCoreClock = SYSCLK_FREQ_48MHz; /*!< System Clock Frequency (Core Clock) */
  150. #elif defined SYSCLK_FREQ_56MHz
  151. uint32_t SystemCoreClock = SYSCLK_FREQ_56MHz; /*!< System Clock Frequency (Core Clock) */
  152. #elif defined SYSCLK_FREQ_72MHz
  153. uint32_t SystemCoreClock = SYSCLK_FREQ_72MHz; /*!< System Clock Frequency (Core Clock) */
  154. #elif defined SYSCLK_FREQ_96MHz
  155. uint32_t SystemCoreClock = SYSCLK_FREQ_96MHz; /*!< System Clock Frequency (Core Clock) */
  156. #elif defined SYSCLK_FREQ_108MHz
  157. uint32_t SystemCoreClock = SYSCLK_FREQ_108MHz; /*!< System Clock Frequency (Core Clock) */
  158. #elif defined SYSCLK_FREQ_120MHz
  159. uint32_t SystemCoreClock = SYSCLK_FREQ_120MHz; /*!< System Clock Frequency (Core Clock) */
  160. #elif defined SYSCLK_FREQ_144MHz
  161. uint32_t SystemCoreClock = SYSCLK_FREQ_144MHz; /*!< System Clock Frequency (Core Clock) */
  162. #elif defined SYSCLK_FREQ_150MHz
  163. uint32_t SystemCoreClock = SYSCLK_FREQ_150MHz; /*!< System Clock Frequency (Core Clock) */
  164. #elif defined SYSCLK_FREQ_168MHz
  165. uint32_t SystemCoreClock = SYSCLK_FREQ_168MHz; /*!< System Clock Frequency (Core Clock) */
  166. #elif defined SYSCLK_FREQ_176MHz
  167. uint32_t SystemCoreClock = SYSCLK_FREQ_176MHz; /*!< System Clock Frequency (Core Clock) */
  168. #elif defined SYSCLK_FREQ_192MHz
  169. uint32_t SystemCoreClock = SYSCLK_FREQ_192MHz; /*!< System Clock Frequency (Core Clock) */
  170. #elif defined SYSCLK_FREQ_200MHz
  171. uint32_t SystemCoreClock = SYSCLK_FREQ_200MHz; /*!< System Clock Frequency (Core Clock) */
  172. #elif defined SYSCLK_FREQ_224MHz
  173. uint32_t SystemCoreClock = SYSCLK_FREQ_224MHz; /*!< System Clock Frequency (Core Clock) */
  174. #elif defined SYSCLK_FREQ_240MHz
  175. uint32_t SystemCoreClock = SYSCLK_FREQ_240MHz; /*!< System Clock Frequency (Core Clock) */
  176. #elif defined SYSCLK_FREQ_24MHz_HSI
  177. uint32_t SystemCoreClock = SYSCLK_FREQ_24MHz_HSI; /*!< System Clock Frequency (Core Clock) */
  178. #elif defined SYSCLK_FREQ_36MHz_HSI
  179. uint32_t SystemCoreClock = SYSCLK_FREQ_36MHz_HSI; /*!< System Clock Frequency (Core Clock) */
  180. #elif defined SYSCLK_FREQ_48MHz_HSI
  181. uint32_t SystemCoreClock = SYSCLK_FREQ_48MHz_HSI; /*!< System Clock Frequency (Core Clock) */
  182. #elif defined SYSCLK_FREQ_56MHz_HSI
  183. uint32_t SystemCoreClock = SYSCLK_FREQ_56MHz_HSI; /*!< System Clock Frequency (Core Clock) */
  184. #elif defined SYSCLK_FREQ_72MHz_HSI
  185. uint32_t SystemCoreClock = SYSCLK_FREQ_72MHz_HSI; /*!< System Clock Frequency (Core Clock) */
  186. #elif defined SYSCLK_FREQ_96MHz_HSI
  187. uint32_t SystemCoreClock = SYSCLK_FREQ_96MHz_HSI; /*!< System Clock Frequency (Core Clock) */
  188. #elif defined SYSCLK_FREQ_108MHz_HSI
  189. uint32_t SystemCoreClock = SYSCLK_FREQ_108MHz_HSI; /*!< System Clock Frequency (Core Clock) */
  190. #elif defined SYSCLK_FREQ_120MHz_HSI
  191. uint32_t SystemCoreClock = SYSCLK_FREQ_120MHz_HSI; /*!< System Clock Frequency (Core Clock) */
  192. #elif defined SYSCLK_FREQ_144MHz_HSI
  193. uint32_t SystemCoreClock = SYSCLK_FREQ_144MHz_HSI; /*!< System Clock Frequency (Core Clock) */
  194. #elif defined SYSCLK_FREQ_150MHz_HSI
  195. uint32_t SystemCoreClock = SYSCLK_FREQ_150MHz_HSI; /*!< System Clock Frequency (Core Clock) */
  196. #elif defined SYSCLK_FREQ_168MHz_HSI
  197. uint32_t SystemCoreClock = SYSCLK_FREQ_168MHz_HSI; /*!< System Clock Frequency (Core Clock) */
  198. #elif defined SYSCLK_FREQ_176MHz_HSI
  199. uint32_t SystemCoreClock = SYSCLK_FREQ_176MHz_HSI; /*!< System Clock Frequency (Core Clock) */
  200. #elif defined SYSCLK_FREQ_192MHz_HSI
  201. uint32_t SystemCoreClock = SYSCLK_FREQ_192MHz_HSI; /*!< System Clock Frequency (Core Clock) */
  202. #elif defined SYSCLK_FREQ_200MHz_HSI
  203. uint32_t SystemCoreClock = SYSCLK_FREQ_200MHz_HSI; /*!< System Clock Frequency (Core Clock) */
  204. #elif defined SYSCLK_FREQ_224MHz_HSI
  205. uint32_t SystemCoreClock = SYSCLK_FREQ_224MHz_HSI; /*!< System Clock Frequency (Core Clock) */
  206. #elif defined SYSCLK_FREQ_240MHz_HSI
  207. uint32_t SystemCoreClock = SYSCLK_FREQ_240MHz_HSI; /*!< System Clock Frequency (Core Clock) */
  208. #else /*!< HSI Selected as System Clock source */
  209. #define SYSCLK_FREQ_HSI HSI_VALUE
  210. uint32_t SystemCoreClock = HSI_VALUE; /*!< System Clock Frequency (Core Clock) */
  211. #endif
  212. __I uint8_t AHBPscTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  213. /**
  214. * @}
  215. */
  216. /** @addtogroup at32f4xx_System_Private_FunctionPrototypes
  217. * @{
  218. */
  219. static void SetSysClock(void);
  220. #ifdef SYSCLK_FREQ_HSE
  221. static void SetSysClockToHSE(void);
  222. #elif defined SYSCLK_FREQ_24MHz
  223. static void SetSysClockTo24M(void);
  224. #elif defined SYSCLK_FREQ_36MHz
  225. static void SetSysClockTo36M(void);
  226. #elif defined SYSCLK_FREQ_48MHz
  227. static void SetSysClockTo48M(void);
  228. #elif defined SYSCLK_FREQ_56MHz
  229. static void SetSysClockTo56M(void);
  230. #elif defined SYSCLK_FREQ_72MHz
  231. static void SetSysClockTo72M(void);
  232. #elif defined SYSCLK_FREQ_96MHz
  233. static void SetSysClockTo96M(void);
  234. #elif defined SYSCLK_FREQ_108MHz
  235. static void SetSysClockTo108M(void);
  236. #elif defined SYSCLK_FREQ_120MHz
  237. static void SetSysClockTo120M(void);
  238. #elif defined SYSCLK_FREQ_144MHz
  239. static void SetSysClockTo144M(void);
  240. #elif defined SYSCLK_FREQ_150MHz
  241. static void SetSysClockTo150M(void);
  242. #elif defined SYSCLK_FREQ_168MHz
  243. static void SetSysClockTo168M(void);
  244. #elif defined SYSCLK_FREQ_176MHz
  245. static void SetSysClockTo176M(void);
  246. #elif defined SYSCLK_FREQ_192MHz
  247. static void SetSysClockTo192M(void);
  248. #elif defined SYSCLK_FREQ_200MHz
  249. static void SetSysClockTo200M(void);
  250. #elif defined SYSCLK_FREQ_224MHz
  251. static void SetSysClockTo224M(void);
  252. #elif defined SYSCLK_FREQ_240MHz
  253. static void SetSysClockTo240M(void);
  254. #elif defined SYSCLK_FREQ_24MHz_HSI
  255. static void SetSysClockTo24MHSI(void);
  256. #elif defined SYSCLK_FREQ_36MHz_HSI
  257. static void SetSysClockTo36MHSI(void);
  258. #elif defined SYSCLK_FREQ_48MHz_HSI
  259. static void SetSysClockTo48MHSI(void);
  260. #elif defined SYSCLK_FREQ_56MHz_HSI
  261. static void SetSysClockTo56MHSI(void);
  262. #elif defined SYSCLK_FREQ_72MHz_HSI
  263. static void SetSysClockTo72MHSI(void);
  264. #elif defined SYSCLK_FREQ_96MHz_HSI
  265. static void SetSysClockTo96MHSI(void);
  266. #elif defined SYSCLK_FREQ_108MHz_HSI
  267. static void SetSysClockTo108MHSI(void);
  268. #elif defined SYSCLK_FREQ_120MHz_HSI
  269. static void SetSysClockTo120MHSI(void);
  270. #elif defined SYSCLK_FREQ_144MHz_HSI
  271. static void SetSysClockTo144MHSI(void);
  272. #elif defined SYSCLK_FREQ_150MHz_HSI
  273. static void SetSysClockTo150MHSI(void);
  274. #elif defined SYSCLK_FREQ_168MHz_HSI
  275. static void SetSysClockTo168MHSI(void);
  276. #elif defined SYSCLK_FREQ_176MHz_HSI
  277. static void SetSysClockTo176MHSI(void);
  278. #elif defined SYSCLK_FREQ_192MHz_HSI
  279. static void SetSysClockTo192MHSI(void);
  280. #elif defined SYSCLK_FREQ_200MHz_HSI
  281. static void SetSysClockTo200MHSI(void);
  282. #elif defined SYSCLK_FREQ_224MHz_HSI
  283. static void SetSysClockTo224MHSI(void);
  284. #elif defined SYSCLK_FREQ_240MHz_HSI
  285. static void SetSysClockTo240MHSI(void);
  286. #endif
  287. #ifdef DATA_IN_ExtSRAM
  288. static void SystemInit_ExtMemCtrl(void);
  289. #endif /* DATA_IN_ExtSRAM */
  290. /**
  291. * @}
  292. */
  293. /** @addtogroup at32f4xx_System_Private_Functions
  294. * @{
  295. */
  296. /**
  297. * @brief Setup the microcontroller system
  298. * Initialize the Embedded Flash Interface, the PLL and update the
  299. * SystemCoreClock variable.
  300. * @note This function should be used only after reset.
  301. * @param None
  302. * @retval None
  303. */
  304. void SystemInit (void)
  305. {
  306. #if defined (AT32F415xx)
  307. /* Enable low power mode, 0x40007050[bit2] */
  308. RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_PWR, ENABLE);
  309. *(volatile uint8_t *)(0x40007050) |= (uint8_t)(0x1 << 2);
  310. RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_PWR, DISABLE);
  311. #endif
  312. #if defined (__FPU_USED) && (__FPU_USED == 1U)
  313. SCB->CPACR |= ((3U << 10U * 2U) | /* set CP10 Full Access */
  314. (3U << 11U * 2U) ); /* set CP11 Full Access */
  315. #endif
  316. /* Reset the RCC clock configuration to the default reset state(for debug purpose) */
  317. /* Set HSIEN bit */
  318. BIT_SET(RCC->CTRL, RCC_CTRL_HSIEN);
  319. /* Reset SW, AHBPSC, APB1PSC, APB2PSC, ADCPSC and CLKOUT bits */
  320. BIT_CLEAR(RCC->CFG, RCC_CFG_SYSCLKSEL | RCC_CFG_AHBPSC | \
  321. RCC_CFG_APB1PSC | RCC_CFG_APB2PSC | \
  322. RCC_CFG_ADCPSC | RCC_CFG_CLKOUT);
  323. /* Reset HSEEN, HSECFDEN and PLLEN bits */
  324. BIT_CLEAR(RCC->CTRL, RCC_CTRL_HSEEN | RCC_CTRL_HSECFDEN | \
  325. RCC_CTRL_PLLEN);
  326. /* Reset HSEBYPS bit */
  327. BIT_CLEAR(RCC->CTRL, RCC_CTRL_HSEBYPS);
  328. /* Reset PLLRC, PLLHSEPSC, PLLMUL, USBPSC and PLLRANGE bits */
  329. BIT_CLEAR(RCC->CFG, RCC_CFG_PLLRC | RCC_CFG_PLLHSEPSC | \
  330. RCC_CFG_PLLMULT | RCC_CFG_USBPSC | RCC_CFG_PLLRANGE);
  331. /* Reset USB768B, CLKOUT[3], HSICAL_KEY[7:0] */
  332. BIT_CLEAR(RCC->MISC, 0x010100FF);
  333. /* Disable all interrupts and clear pending bits */
  334. RCC->CLKINT = RCC_CLKINT_LSISTBLFC | RCC_CLKINT_LSESTBLFC | \
  335. RCC_CLKINT_HSISTBLFC | RCC_CLKINT_HSESTBLFC | \
  336. RCC_CLKINT_PLLSTBLFC | RCC_CLKINT_HSECFDFC;
  337. #ifdef DATA_IN_ExtSRAM
  338. SystemInit_ExtMemCtrl();
  339. #endif /* DATA_IN_ExtSRAM */
  340. /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */
  341. /* Configure the Flash Latency cycles and enable prefetch buffer */
  342. SetSysClock();
  343. #ifdef VECT_TAB_SRAM
  344. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
  345. #else
  346. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
  347. #endif
  348. }
  349. /**
  350. * @brief Update SystemCoreClock variable according to Clock Register Values.
  351. * The SystemCoreClock variable contains the core clock (HCLK), it can
  352. * be used by the user application to setup the SysTick timer or configure
  353. * other parameters.
  354. *
  355. * @note Each time the core clock (HCLK) changes, this function must be called
  356. * to update SystemCoreClock variable value. Otherwise, any configuration
  357. * based on this variable will be incorrect.
  358. *
  359. * @note - The system frequency computed by this function is not the real
  360. * frequency in the chip. It is calculated based on the predefined
  361. * constant and the selected clock source:
  362. *
  363. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  364. *
  365. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  366. *
  367. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  368. * or HSI_VALUE(*) multiplied by the PLL factors.
  369. *
  370. * (*) HSI_VALUE is a constant defined in at32f4xx.h file (default value
  371. * 8 MHz) but the real value may vary depending on the variations
  372. * in voltage and temperature.
  373. *
  374. * (**) HSE_VALUE is a constant defined in at32f4xx.h file (default value
  375. * 8 MHz or 25 MHz, depedning on the product used), user has to ensure
  376. * that HSE_VALUE is same as the real frequency of the crystal used.
  377. * Otherwise, this function may have wrong result.
  378. *
  379. * - The result of this function could be not correct when using fractional
  380. * value for HSE crystal.
  381. * @param None
  382. * @retval None
  383. */
  384. void SystemCoreClockUpdate (void)
  385. {
  386. uint32_t tmp = 0, pllmult = 0, pllrefclk = 0, tempcfg = 0;
  387. /* Get SYSCLK source -------------------------------------------------------*/
  388. tmp = RCC->CFG & RCC_CFG_SYSCLKSTS;
  389. switch (tmp)
  390. {
  391. case RCC_CFG_SYSCLKSTS_HSI: /* HSI used as system clock */
  392. SystemCoreClock = HSI_VALUE;
  393. break;
  394. case RCC_CFG_SYSCLKSTS_HSE: /* HSE used as system clock */
  395. SystemCoreClock = HSE_VALUE;
  396. break;
  397. case RCC_CFG_SYSCLKSTS_PLL: /* PLL used as system clock */
  398. /* Get PLL clock source and multiplication factor ----------------------*/
  399. pllrefclk = RCC->CFG & RCC_CFG_PLLRC;
  400. tempcfg = RCC->CFG;
  401. pllmult = RCC_GET_PLLMULT(tempcfg);
  402. if (pllrefclk == RCC_PLLRefClk_HSI_Div2)
  403. {
  404. /* HSI oscillator clock divided by 2 selected as PLL clock entry */
  405. SystemCoreClock = (HSI_VALUE >> 1) * pllmult;
  406. }
  407. else
  408. {
  409. /* HSE selected as PLL clock entry */
  410. if ((RCC->CFG & RCC_CFG_PLLHSEPSC) != (uint32_t)RESET)
  411. {
  412. /* HSE oscillator clock divided by 2 */
  413. SystemCoreClock = (HSE_VALUE >> 1) * pllmult;
  414. }
  415. else
  416. {
  417. SystemCoreClock = HSE_VALUE * pllmult;
  418. }
  419. }
  420. break;
  421. default:
  422. SystemCoreClock = HSI_VALUE;
  423. break;
  424. }
  425. /* Compute HCLK clock frequency ----------------*/
  426. /* Get HCLK prescaler */
  427. tmp = AHBPscTable[((RCC->CFG & RCC_CFG_AHBPSC) >> 4)];
  428. /* HCLK clock frequency */
  429. SystemCoreClock >>= tmp;
  430. }
  431. /**
  432. * @brief Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.
  433. * @param None
  434. * @retval None
  435. */
  436. static void SetSysClock(void)
  437. {
  438. #ifdef SYSCLK_FREQ_HSE
  439. SetSysClockToHSE();
  440. #elif defined SYSCLK_FREQ_24MHz
  441. SetSysClockTo24M();
  442. #elif defined SYSCLK_FREQ_36MHz
  443. SetSysClockTo36M();
  444. #elif defined SYSCLK_FREQ_48MHz
  445. SetSysClockTo48M();
  446. #elif defined SYSCLK_FREQ_56MHz
  447. SetSysClockTo56M();
  448. #elif defined SYSCLK_FREQ_72MHz
  449. SetSysClockTo72M();
  450. #elif defined SYSCLK_FREQ_96MHz
  451. SetSysClockTo96M();
  452. #elif defined SYSCLK_FREQ_108MHz
  453. SetSysClockTo108M();
  454. #elif defined SYSCLK_FREQ_120MHz
  455. SetSysClockTo120M();
  456. #elif defined SYSCLK_FREQ_144MHz
  457. SetSysClockTo144M();
  458. #elif defined SYSCLK_FREQ_150MHz
  459. SetSysClockTo150M();
  460. #elif defined SYSCLK_FREQ_168MHz
  461. SetSysClockTo168M();
  462. #elif defined SYSCLK_FREQ_176MHz
  463. SetSysClockTo176M();
  464. #elif defined SYSCLK_FREQ_192MHz
  465. SetSysClockTo192M();
  466. #elif defined SYSCLK_FREQ_200MHz
  467. SetSysClockTo200M();
  468. #elif defined SYSCLK_FREQ_224MHz
  469. SetSysClockTo224M();
  470. #elif defined SYSCLK_FREQ_240MHz
  471. SetSysClockTo240M();
  472. #elif defined SYSCLK_FREQ_24MHz_HSI
  473. SetSysClockTo24MHSI();
  474. #elif defined SYSCLK_FREQ_36MHz_HSI
  475. SetSysClockTo36MHSI();
  476. #elif defined SYSCLK_FREQ_48MHz_HSI
  477. SetSysClockTo48MHSI();
  478. #elif defined SYSCLK_FREQ_56MHz_HSI
  479. SetSysClockTo56MHSI();
  480. #elif defined SYSCLK_FREQ_72MHz_HSI
  481. SetSysClockTo72MHSI();
  482. #elif defined SYSCLK_FREQ_96MHz_HSI
  483. SetSysClockTo96MHSI();
  484. #elif defined SYSCLK_FREQ_108MHz_HSI
  485. SetSysClockTo108MHSI();
  486. #elif defined SYSCLK_FREQ_120MHz_HSI
  487. SetSysClockTo120MHSI();
  488. #elif defined SYSCLK_FREQ_144MHz_HSI
  489. SetSysClockTo144MHSI();
  490. #elif defined SYSCLK_FREQ_150MHz_HSI
  491. SetSysClockTo150MHSI();
  492. #elif defined SYSCLK_FREQ_168MHz_HSI
  493. SetSysClockTo168MHSI();
  494. #elif defined SYSCLK_FREQ_176MHz_HSI
  495. SetSysClockTo176MHSI();
  496. #elif defined SYSCLK_FREQ_192MHz_HSI
  497. SetSysClockTo192MHSI();
  498. #elif defined SYSCLK_FREQ_200MHz_HSI
  499. SetSysClockTo200MHSI();
  500. #elif defined SYSCLK_FREQ_224MHz_HSI
  501. SetSysClockTo224MHSI();
  502. #elif defined SYSCLK_FREQ_240MHz_HSI
  503. SetSysClockTo240MHSI();
  504. #endif
  505. /* If none of the define above is enabled, the HSI is used as System clock
  506. source (default after reset) */
  507. }
  508. /**
  509. * @brief Setup the external memory controller. Called in startup_at32f4xx.s
  510. * before jump to __main
  511. * @param None
  512. * @retval None
  513. */
  514. #ifdef DATA_IN_ExtSRAM
  515. /**
  516. * @brief Setup the external memory controller.
  517. * Called in startup_at32f4xx_xx.s/.c before jump to main.
  518. * This function configures the external SRAM mounted
  519. * (AT32 High density devices). This SRAM will be used as program
  520. * data memory (including heap and stack).
  521. * @param None
  522. * @retval None
  523. */
  524. void SystemInit_ExtMemCtrl(void)
  525. {
  526. /* Enable XMC clock */
  527. RCC->AHBEN = RCC_AHBEN_SRAMEN | RCC_AHBEN_FLASHEN | RCC_AHBEN_XMCEN;
  528. /* Enable GPIOD, GPIOE, GPIOF and GPIOG clocks */
  529. RCC->APB2EN = RCC_APB2EN_GPIODEN | RCC_APB2EN_GPIOEEN | RCC_APB2EN_GPIOFEN | RCC_APB2EN_GPIOGEN;
  530. /* --------------- SRAM Data lines, NOE and NWE configuration ---------------*/
  531. /*---------------- SRAM Address lines configuration -------------------------*/
  532. /*---------------- NOE and NWE configuration --------------------------------*/
  533. /*---------------- NE3 configuration ----------------------------------------*/
  534. /*---------------- NBL0, NBL1 configuration ---------------------------------*/
  535. GPIOD->CTRLL = 0x44BB44BB;
  536. GPIOD->CTRLH = 0xBBBBBBBB;
  537. GPIOE->CTRLL = 0xB44444BB;
  538. GPIOE->CTRLH = 0xBBBBBBBB;
  539. GPIOF->CTRLL = 0x44BBBBBB;
  540. GPIOF->CTRLH = 0xBBBB4444;
  541. GPIOG->CTRLL = 0x44BBBBBB;
  542. GPIOG->CTRLH = 0x44444B44;
  543. /*---------------- XMC Configuration ---------------------------------------*/
  544. /*---------------- Enable XMC Bank1_SRAM Bank ------------------------------*/
  545. XMC_Bank1->BK1CTRLR[4] = 0x00001011;
  546. XMC_Bank1->BK1CTRLR[5] = 0x00000200;
  547. }
  548. #endif /* DATA_IN_ExtSRAM */
  549. #ifndef SYSCLK_FREQ_HSI
  550. #ifdef AT32F403xx
  551. /**
  552. * @brief Delay to wait for HSE stable.
  553. * @note This function should be used before reading the HSESTBL flag.
  554. * @param None
  555. * @retval None
  556. */
  557. static void WaitHseStbl(uint32_t delay)
  558. {
  559. uint32_t i;
  560. for(i = 0; i < delay; i++)
  561. ;
  562. }
  563. #endif
  564. #endif /* SYSCLK_FREQ_HSI */
  565. #ifdef SYSCLK_FREQ_HSE
  566. /**
  567. * @brief Selects HSE as System clock source and configure HCLK, PCLK2
  568. * and PCLK1 prescalers.
  569. * @note This function should be used only after reset.
  570. * @param None
  571. * @retval None
  572. */
  573. static void SetSysClockToHSE(void)
  574. {
  575. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  576. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  577. /* Enable HSE */
  578. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSEEN);
  579. /* Wait till HSE is ready and if Time out is reached exit */
  580. do
  581. {
  582. HSEStatus = RCC->CTRL & RCC_CTRL_HSESTBL;
  583. StartUpCounter++;
  584. }
  585. while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  586. #ifdef AT32F403xx
  587. WaitHseStbl(HSE_STABLE_DELAY);
  588. #endif
  589. if ((RCC->CTRL & RCC_CTRL_HSESTBL) != RESET)
  590. {
  591. HSEStatus = (uint32_t)0x01;
  592. }
  593. else
  594. {
  595. HSEStatus = (uint32_t)0x00;
  596. }
  597. if (HSEStatus == (uint32_t)0x01)
  598. {
  599. #if defined (AT32F415xx)
  600. /* Enable Prefetch Buffer */
  601. FLASH->ACR |= FLASH_ACR_PRFTBE;
  602. /* Flash 1 wait state */
  603. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  604. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;
  605. #endif
  606. /* HCLK = SYSCLK */
  607. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  608. /* PCLK2 = HCLK */
  609. RCC->CFG &= 0xFFFFC7FF;
  610. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV1;
  611. /* PCLK1 = HCLK */
  612. RCC->CFG &= 0xFFFFF8FF;
  613. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV1;
  614. /* Select HSE as system clock source */
  615. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  616. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_HSE;
  617. /* Wait till HSE is used as system clock source */
  618. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != (uint32_t)0x04)
  619. {
  620. }
  621. }
  622. else
  623. {
  624. /* If HSE fails to start-up, the application will have wrong clock
  625. configuration. User can add here some code to deal with this error */
  626. }
  627. }
  628. #elif defined SYSCLK_FREQ_24MHz
  629. /**
  630. * @brief Sets System clock frequency to 24MHz and configure HCLK, PCLK2
  631. * and PCLK1 prescalers.
  632. * @note This function should be used only after reset.
  633. * @param None
  634. * @retval None
  635. */
  636. static void SetSysClockTo24M(void)
  637. {
  638. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  639. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  640. /* Enable HSE */
  641. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSEEN);
  642. /* Wait till HSE is ready and if Time out is reached exit */
  643. do
  644. {
  645. HSEStatus = RCC->CTRL & RCC_CTRL_HSESTBL;
  646. StartUpCounter++;
  647. }
  648. while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  649. #ifdef AT32F403xx
  650. WaitHseStbl(HSE_STABLE_DELAY);
  651. #endif
  652. if ((RCC->CTRL & RCC_CTRL_HSESTBL) != RESET)
  653. {
  654. HSEStatus = (uint32_t)0x01;
  655. }
  656. else
  657. {
  658. HSEStatus = (uint32_t)0x00;
  659. }
  660. if (HSEStatus == (uint32_t)0x01)
  661. {
  662. #if defined (AT32F415xx)
  663. /* Enable Prefetch Buffer */
  664. FLASH->ACR |= FLASH_ACR_PRFTBE;
  665. /* Flash 1 wait state */
  666. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  667. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;
  668. #endif
  669. /* HCLK = SYSCLK */
  670. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  671. /* PCLK2 = HCLK */
  672. RCC->CFG &= 0xFFFFC7FF;
  673. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV1;
  674. /* PCLK1 = HCLK */
  675. RCC->CFG &= 0xFFFFF8FF;
  676. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV1;
  677. /* PLL configuration: = (HSE / 2) * 6 = 24 MHz */
  678. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  679. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSE | RCC_CFG_PLLHSEPSC_HSE_DIV2 | RCC_CFG_PLLMULT6);
  680. /* Enable PLL */
  681. RCC->CTRL |= RCC_CTRL_PLLEN;
  682. /* Wait till PLL is ready */
  683. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  684. {
  685. }
  686. /* Select PLL as system clock source */
  687. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  688. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  689. /* Wait till PLL is used as system clock source */
  690. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  691. {
  692. }
  693. #ifdef AT32F403xx
  694. WaitHseStbl(PLL_STABLE_DELAY);
  695. #endif
  696. }
  697. else
  698. {
  699. /* If HSE fails to start-up, the application will have wrong clock
  700. configuration. User can add here some code to deal with this error */
  701. }
  702. }
  703. #elif defined SYSCLK_FREQ_36MHz
  704. /**
  705. * @brief Sets System clock frequency to 36MHz and configure HCLK, PCLK2
  706. * and PCLK1 prescalers.
  707. * @note This function should be used only after reset.
  708. * @param None
  709. * @retval None
  710. */
  711. static void SetSysClockTo36M(void)
  712. {
  713. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  714. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  715. /* Enable HSE */
  716. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSEEN);
  717. /* Wait till HSE is ready and if Time out is reached exit */
  718. do
  719. {
  720. HSEStatus = RCC->CTRL & RCC_CTRL_HSESTBL;
  721. StartUpCounter++;
  722. }
  723. while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  724. #ifdef AT32F403xx
  725. WaitHseStbl(HSE_STABLE_DELAY);
  726. #endif
  727. if ((RCC->CTRL & RCC_CTRL_HSESTBL) != RESET)
  728. {
  729. HSEStatus = (uint32_t)0x01;
  730. }
  731. else
  732. {
  733. HSEStatus = (uint32_t)0x00;
  734. }
  735. if (HSEStatus == (uint32_t)0x01)
  736. {
  737. #if defined (AT32F415xx)
  738. /* Enable Prefetch Buffer */
  739. FLASH->ACR |= FLASH_ACR_PRFTBE;
  740. /* Flash 1 wait state */
  741. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  742. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;
  743. #endif
  744. /* HCLK = SYSCLK */
  745. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  746. /* PCLK2 = HCLK */
  747. RCC->CFG &= 0xFFFFC7FF;
  748. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV1;
  749. /* PCLK1 = HCLK */
  750. RCC->CFG &= 0xFFFFF8FF;
  751. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV1;
  752. /* PLL configuration: PLLCLK = (HSE / 2) * 9 = 36 MHz */
  753. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  754. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSE | RCC_CFG_PLLHSEPSC_HSE_DIV2 | RCC_CFG_PLLMULT9);
  755. /* Enable PLL */
  756. RCC->CTRL |= RCC_CTRL_PLLEN;
  757. /* Wait till PLL is ready */
  758. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  759. {
  760. }
  761. /* Select PLL as system clock source */
  762. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  763. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  764. /* Wait till PLL is used as system clock source */
  765. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  766. {
  767. }
  768. #ifdef AT32F403xx
  769. WaitHseStbl(PLL_STABLE_DELAY);
  770. #endif
  771. }
  772. else
  773. {
  774. /* If HSE fails to start-up, the application will have wrong clock
  775. configuration. User can add here some code to deal with this error */
  776. }
  777. }
  778. #elif defined SYSCLK_FREQ_48MHz
  779. /**
  780. * @brief Sets System clock frequency to 48MHz and configure HCLK, PCLK2
  781. * and PCLK1 prescalers.
  782. * @note This function should be used only after reset.
  783. * @param None
  784. * @retval None
  785. */
  786. static void SetSysClockTo48M(void)
  787. {
  788. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  789. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  790. /* Enable HSE */
  791. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSEEN);
  792. /* Wait till HSE is ready and if Time out is reached exit */
  793. do
  794. {
  795. HSEStatus = RCC->CTRL & RCC_CTRL_HSESTBL;
  796. StartUpCounter++;
  797. }
  798. while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  799. #ifdef AT32F403xx
  800. WaitHseStbl(HSE_STABLE_DELAY);
  801. #endif
  802. if ((RCC->CTRL & RCC_CTRL_HSESTBL) != RESET)
  803. {
  804. HSEStatus = (uint32_t)0x01;
  805. }
  806. else
  807. {
  808. HSEStatus = (uint32_t)0x00;
  809. }
  810. if (HSEStatus == (uint32_t)0x01)
  811. {
  812. #if defined (AT32F415xx)
  813. /* Enable Prefetch Buffer */
  814. FLASH->ACR |= FLASH_ACR_PRFTBE;
  815. /* Flash 1 wait state */
  816. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  817. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;
  818. #endif
  819. /* HCLK = SYSCLK */
  820. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  821. /* PCLK2 = HCLK */
  822. RCC->CFG &= 0xFFFFC7FF;
  823. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV1;
  824. /* PCLK1 = HCLK/2 */
  825. RCC->CFG &= 0xFFFFF8FF;
  826. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV2;
  827. /* PLL configuration: PLLCLK = HSE * 6 = 48 MHz */
  828. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  829. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSE | RCC_CFG_PLLMULT6);
  830. /* Enable PLL */
  831. RCC->CTRL |= RCC_CTRL_PLLEN;
  832. /* Wait till PLL is ready */
  833. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  834. {
  835. }
  836. /* Select PLL as system clock source */
  837. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  838. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  839. /* Wait till PLL is used as system clock source */
  840. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  841. {
  842. }
  843. #ifdef AT32F403xx
  844. WaitHseStbl(PLL_STABLE_DELAY);
  845. #endif
  846. }
  847. else
  848. {
  849. /* If HSE fails to start-up, the application will have wrong clock
  850. configuration. User can add here some code to deal with this error */
  851. }
  852. }
  853. #elif defined SYSCLK_FREQ_56MHz
  854. /**
  855. * @brief Sets System clock frequency to 56MHz and configure HCLK, PCLK2
  856. * and PCLK1 prescalers.
  857. * @note This function should be used only after reset.
  858. * @param None
  859. * @retval None
  860. */
  861. static void SetSysClockTo56M(void)
  862. {
  863. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  864. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  865. /* Enable HSE */
  866. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSEEN);
  867. /* Wait till HSE is ready and if Time out is reached exit */
  868. do
  869. {
  870. HSEStatus = RCC->CTRL & RCC_CTRL_HSESTBL;
  871. StartUpCounter++;
  872. }
  873. while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  874. #ifdef AT32F403xx
  875. WaitHseStbl(HSE_STABLE_DELAY);
  876. #endif
  877. if ((RCC->CTRL & RCC_CTRL_HSESTBL) != RESET)
  878. {
  879. HSEStatus = (uint32_t)0x01;
  880. }
  881. else
  882. {
  883. HSEStatus = (uint32_t)0x00;
  884. }
  885. if (HSEStatus == (uint32_t)0x01)
  886. {
  887. #if defined (AT32F415xx)
  888. /* Enable Prefetch Buffer */
  889. FLASH->ACR |= FLASH_ACR_PRFTBE;
  890. /* Flash 1 wait state */
  891. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  892. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;
  893. #endif
  894. /* HCLK = SYSCLK */
  895. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  896. /* PCLK2 = HCLK */
  897. RCC->CFG &= 0xFFFFC7FF;
  898. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV1;
  899. /* PCLK1 = HCLK/2 */
  900. RCC->CFG &= 0xFFFFF8FF;
  901. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV2;
  902. /* PLL configuration: PLLCLK = HSE * 7 = 56 MHz */
  903. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  904. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSE | RCC_CFG_PLLMULT7);
  905. /* Enable PLL */
  906. RCC->CTRL |= RCC_CTRL_PLLEN;
  907. /* Wait till PLL is ready */
  908. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  909. {
  910. }
  911. /* Select PLL as system clock source */
  912. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  913. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  914. /* Wait till PLL is used as system clock source */
  915. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  916. {
  917. }
  918. #ifdef AT32F403xx
  919. WaitHseStbl(PLL_STABLE_DELAY);
  920. #endif
  921. }
  922. else
  923. {
  924. /* If HSE fails to start-up, the application will have wrong clock
  925. configuration. User can add here some code to deal with this error */
  926. }
  927. }
  928. #elif defined SYSCLK_FREQ_72MHz
  929. /**
  930. * @brief Sets System clock frequency to 72MHz and configure HCLK, PCLK2
  931. * and PCLK1 prescalers.
  932. * @note This function should be used only after reset.
  933. * @param None
  934. * @retval None
  935. */
  936. static void SetSysClockTo72M(void)
  937. {
  938. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  939. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  940. /* Enable HSE */
  941. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSEEN);
  942. /* Wait till HSE is ready and if Time out is reached exit */
  943. do
  944. {
  945. HSEStatus = RCC->CTRL & RCC_CTRL_HSESTBL;
  946. StartUpCounter++;
  947. }
  948. while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  949. #ifdef AT32F403xx
  950. WaitHseStbl(HSE_STABLE_DELAY);
  951. #endif
  952. if ((RCC->CTRL & RCC_CTRL_HSESTBL) != RESET)
  953. {
  954. HSEStatus = (uint32_t)0x01;
  955. }
  956. else
  957. {
  958. HSEStatus = (uint32_t)0x00;
  959. }
  960. if (HSEStatus == (uint32_t)0x01)
  961. {
  962. #if defined (AT32F415xx)
  963. /* Enable Prefetch Buffer */
  964. FLASH->ACR |= FLASH_ACR_PRFTBE;
  965. /* Flash 1 wait state */
  966. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  967. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;
  968. #endif
  969. /* HCLK = SYSCLK */
  970. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  971. /* PCLK2 = HCLK */
  972. RCC->CFG &= 0xFFFFC7FF;
  973. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV1;
  974. /* PCLK1 = HCLK/2 */
  975. RCC->CFG &= 0xFFFFF8FF;
  976. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV2;
  977. /* PLL configuration: PLLCLK = HSE * 9 = 72 MHz */
  978. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  979. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSE | RCC_CFG_PLLMULT9);
  980. /* Enable PLL */
  981. RCC->CTRL |= RCC_CTRL_PLLEN;
  982. /* Wait till PLL is ready */
  983. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  984. {
  985. }
  986. /* Select PLL as system clock source */
  987. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  988. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  989. /* Wait till PLL is used as system clock source */
  990. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  991. {
  992. }
  993. #ifdef AT32F403xx
  994. WaitHseStbl(PLL_STABLE_DELAY);
  995. #endif
  996. }
  997. else
  998. {
  999. /* If HSE fails to start-up, the application will have wrong clock
  1000. configuration. User can add here some code to deal with this error */
  1001. }
  1002. }
  1003. #elif defined SYSCLK_FREQ_96MHz
  1004. /**
  1005. * @brief Sets System clock frequency to 96MHz and configure HCLK, PCLK2
  1006. * and PCLK1 prescalers.
  1007. * @note This function should be used only after reset.
  1008. * @param None
  1009. * @retval None
  1010. */
  1011. static void SetSysClockTo96M(void)
  1012. {
  1013. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  1014. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  1015. /* Enable HSE */
  1016. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSEEN);
  1017. /* Wait till HSE is ready and if Time out is reached exit */
  1018. do
  1019. {
  1020. HSEStatus = RCC->CTRL & RCC_CTRL_HSESTBL;
  1021. StartUpCounter++;
  1022. }
  1023. while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  1024. #ifdef AT32F403xx
  1025. WaitHseStbl(HSE_STABLE_DELAY);
  1026. #endif
  1027. if ((RCC->CTRL & RCC_CTRL_HSESTBL) != RESET)
  1028. {
  1029. HSEStatus = (uint32_t)0x01;
  1030. }
  1031. else
  1032. {
  1033. HSEStatus = (uint32_t)0x00;
  1034. }
  1035. if (HSEStatus == (uint32_t)0x01)
  1036. {
  1037. #if defined (AT32F415xx)
  1038. /* Enable Prefetch Buffer */
  1039. FLASH->ACR |= FLASH_ACR_PRFTBE;
  1040. /* Flash 1 wait state */
  1041. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  1042. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;
  1043. #endif
  1044. /* HCLK = SYSCLK */
  1045. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  1046. /* PCLK2 = HCLK/2 */
  1047. RCC->CFG &= 0xFFFFC7FF;
  1048. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV2;
  1049. /* PCLK1 = HCLK/2 */
  1050. RCC->CFG &= 0xFFFFF8FF;
  1051. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV2;
  1052. /* PLL configuration: PLLCLK = HSE * 12 = 96 MHz */
  1053. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  1054. #if defined (AT32F415xx)
  1055. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSE | RCC_CFG_PLLMULT12);
  1056. #else
  1057. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSE | RCC_CFG_PLLMULT12 | RCC_CFG_PLLRANGE_GT72MHZ);
  1058. #endif
  1059. /* Enable PLL */
  1060. RCC->CTRL |= RCC_CTRL_PLLEN;
  1061. /* Wait till PLL is ready */
  1062. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  1063. {
  1064. }
  1065. /* Select PLL as system clock source */
  1066. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  1067. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  1068. /* Wait till PLL is used as system clock source */
  1069. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  1070. {
  1071. }
  1072. #ifdef AT32F403xx
  1073. WaitHseStbl(PLL_STABLE_DELAY);
  1074. #endif
  1075. }
  1076. else
  1077. {
  1078. /* If HSE fails to start-up, the application will have wrong clock
  1079. configuration. User can add here some code to deal with this error */
  1080. }
  1081. }
  1082. #elif defined SYSCLK_FREQ_108MHz
  1083. /**
  1084. * @brief Sets System clock frequency to 108MHz and configure HCLK, PCLK2
  1085. * and PCLK1 prescalers.
  1086. * @note This function should be used only after reset.
  1087. * @param None
  1088. * @retval None
  1089. */
  1090. static void SetSysClockTo108M(void)
  1091. {
  1092. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  1093. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  1094. /* Enable HSE */
  1095. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSEEN);
  1096. /* Wait till HSE is ready and if Time out is reached exit */
  1097. do
  1098. {
  1099. HSEStatus = RCC->CTRL & RCC_CTRL_HSESTBL;
  1100. StartUpCounter++;
  1101. }
  1102. while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  1103. #ifdef AT32F403xx
  1104. WaitHseStbl(HSE_STABLE_DELAY);
  1105. #endif
  1106. if ((RCC->CTRL & RCC_CTRL_HSESTBL) != RESET)
  1107. {
  1108. HSEStatus = (uint32_t)0x01;
  1109. }
  1110. else
  1111. {
  1112. HSEStatus = (uint32_t)0x00;
  1113. }
  1114. if (HSEStatus == (uint32_t)0x01)
  1115. {
  1116. #if defined (AT32F415xx)
  1117. /* Enable Prefetch Buffer */
  1118. FLASH->ACR |= FLASH_ACR_PRFTBE;
  1119. /* Flash 1 wait state */
  1120. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  1121. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_3;
  1122. #endif
  1123. /* HCLK = SYSCLK */
  1124. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  1125. /* PCLK2 = HCLK/2 */
  1126. RCC->CFG &= 0xFFFFC7FF;
  1127. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV2;
  1128. /* PCLK1 = HCLK/2 */
  1129. RCC->CFG &= 0xFFFFF8FF;
  1130. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV2;
  1131. /* PLL configuration: PLLCLK = (HSE/2) * 27 = 108 MHz */
  1132. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  1133. #if defined (AT32F415xx)
  1134. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSE | RCC_CFG_PLLHSEPSC_HSE_DIV2 | RCC_CFG_PLLMULT27);
  1135. #else
  1136. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSE | RCC_CFG_PLLHSEPSC_HSE_DIV2 | RCC_CFG_PLLMULT27 \
  1137. | RCC_CFG_PLLRANGE_GT72MHZ);
  1138. #endif
  1139. /* Enable PLL */
  1140. RCC->CTRL |= RCC_CTRL_PLLEN;
  1141. /* Wait till PLL is ready */
  1142. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  1143. {
  1144. }
  1145. /* Select PLL as system clock source */
  1146. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  1147. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  1148. /* Wait till PLL is used as system clock source */
  1149. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  1150. {
  1151. }
  1152. #ifdef AT32F403xx
  1153. WaitHseStbl(PLL_STABLE_DELAY);
  1154. #endif
  1155. }
  1156. else
  1157. {
  1158. /* If HSE fails to start-up, the application will have wrong clock
  1159. configuration. User can add here some code to deal with this error */
  1160. }
  1161. }
  1162. #elif defined SYSCLK_FREQ_120MHz
  1163. /**
  1164. * @brief Sets System clock frequency to 120MHz and configure HCLK, PCLK2
  1165. * and PCLK1 prescalers.
  1166. * @note This function should be used only after reset.
  1167. * @param None
  1168. * @retval None
  1169. */
  1170. static void SetSysClockTo120M(void)
  1171. {
  1172. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  1173. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  1174. /* Enable HSE */
  1175. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSEEN);
  1176. /* Wait till HSE is ready and if Time out is reached exit */
  1177. do
  1178. {
  1179. HSEStatus = RCC->CTRL & RCC_CTRL_HSESTBL;
  1180. StartUpCounter++;
  1181. }
  1182. while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  1183. #ifdef AT32F403xx
  1184. WaitHseStbl(HSE_STABLE_DELAY);
  1185. #endif
  1186. if ((RCC->CTRL & RCC_CTRL_HSESTBL) != RESET)
  1187. {
  1188. HSEStatus = (uint32_t)0x01;
  1189. }
  1190. else
  1191. {
  1192. HSEStatus = (uint32_t)0x00;
  1193. }
  1194. if (HSEStatus == (uint32_t)0x01)
  1195. {
  1196. #if defined (AT32F415xx)
  1197. /* Enable Prefetch Buffer */
  1198. FLASH->ACR |= FLASH_ACR_PRFTBE;
  1199. /* Flash 1 wait state */
  1200. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  1201. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_3;
  1202. #endif
  1203. /* HCLK = SYSCLK */
  1204. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  1205. /* PCLK2 = HCLK/2 */
  1206. RCC->CFG &= 0xFFFFC7FF;
  1207. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV2;
  1208. /* PCLK1 = HCLK/2 */
  1209. RCC->CFG &= 0xFFFFF8FF;
  1210. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV2;
  1211. /* PLL configuration: PLLCLK = HSE * 15 = 120 MHz */
  1212. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  1213. #if defined (AT32F415xx)
  1214. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSE | RCC_CFG_PLLMULT15);
  1215. #else
  1216. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSE | RCC_CFG_PLLMULT15 | RCC_CFG_PLLRANGE_GT72MHZ);
  1217. #endif
  1218. /* Enable PLL */
  1219. RCC->CTRL |= RCC_CTRL_PLLEN;
  1220. /* Wait till PLL is ready */
  1221. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  1222. {
  1223. }
  1224. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  1225. defined (AT32F407xx) || defined (AT32F415xx)
  1226. RCC_StepModeCmd(ENABLE);
  1227. #endif
  1228. /* Select PLL as system clock source */
  1229. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  1230. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  1231. /* Wait till PLL is used as system clock source */
  1232. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  1233. {
  1234. }
  1235. #ifdef AT32F403xx
  1236. WaitHseStbl(PLL_STABLE_DELAY);
  1237. #endif
  1238. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  1239. defined (AT32F407xx) || defined (AT32F415xx)
  1240. RCC_StepModeCmd(DISABLE);
  1241. #endif
  1242. }
  1243. else
  1244. {
  1245. /* If HSE fails to start-up, the application will have wrong clock
  1246. configuration. User can add here some code to deal with this error */
  1247. }
  1248. }
  1249. #elif defined SYSCLK_FREQ_144MHz
  1250. /**
  1251. * @brief Sets System clock frequency to 144MHz and configure HCLK, PCLK2
  1252. * and PCLK1 prescalers.
  1253. * @note This function should be used only after reset.
  1254. * @param None
  1255. * @retval None
  1256. */
  1257. static void SetSysClockTo144M(void)
  1258. {
  1259. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  1260. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  1261. /* Enable HSE */
  1262. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSEEN);
  1263. /* Wait till HSE is ready and if Time out is reached exit */
  1264. do
  1265. {
  1266. HSEStatus = RCC->CTRL & RCC_CTRL_HSESTBL;
  1267. StartUpCounter++;
  1268. }
  1269. while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  1270. #ifdef AT32F403xx
  1271. WaitHseStbl(HSE_STABLE_DELAY);
  1272. #endif
  1273. if ((RCC->CTRL & RCC_CTRL_HSESTBL) != RESET)
  1274. {
  1275. HSEStatus = (uint32_t)0x01;
  1276. }
  1277. else
  1278. {
  1279. HSEStatus = (uint32_t)0x00;
  1280. }
  1281. if (HSEStatus == (uint32_t)0x01)
  1282. {
  1283. #if defined (AT32F415xx)
  1284. /* Enable Prefetch Buffer */
  1285. FLASH->ACR |= FLASH_ACR_PRFTBE;
  1286. /* Flash 1 wait state */
  1287. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  1288. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_4;
  1289. #endif
  1290. /* HCLK = SYSCLK */
  1291. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  1292. /* PCLK2 = HCLK/2 */
  1293. RCC->CFG &= 0xFFFFC7FF;
  1294. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV2;
  1295. /* PCLK1 = HCLK/2 */
  1296. RCC->CFG &= 0xFFFFF8FF;
  1297. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV2;
  1298. /* PLL configuration: PLLCLK = HSE * 18 = 144 MHz */
  1299. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  1300. #if defined (AT32F415xx)
  1301. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSE | RCC_CFG_PLLMULT18);
  1302. #else
  1303. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSE | RCC_CFG_PLLMULT18 | RCC_CFG_PLLRANGE_GT72MHZ);
  1304. #endif
  1305. /* Enable PLL */
  1306. RCC->CTRL |= RCC_CTRL_PLLEN;
  1307. /* Wait till PLL is ready */
  1308. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  1309. {
  1310. }
  1311. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  1312. defined (AT32F407xx) || defined (AT32F415xx)
  1313. RCC_StepModeCmd(ENABLE);
  1314. #endif
  1315. /* Select PLL as system clock source */
  1316. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  1317. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  1318. /* Wait till PLL is used as system clock source */
  1319. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  1320. {
  1321. }
  1322. #ifdef AT32F403xx
  1323. WaitHseStbl(PLL_STABLE_DELAY);
  1324. #endif
  1325. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  1326. defined (AT32F407xx) || defined (AT32F415xx)
  1327. RCC_StepModeCmd(DISABLE);
  1328. #endif
  1329. }
  1330. else
  1331. {
  1332. /* If HSE fails to start-up, the application will have wrong clock
  1333. configuration. User can add here some code to deal with this error */
  1334. }
  1335. }
  1336. #elif defined SYSCLK_FREQ_150MHz
  1337. /**
  1338. * @brief Sets System clock frequency to 150MHz and configure HCLK, PCLK2
  1339. * and PCLK1 prescalers.
  1340. * @note This function should be used only after reset.
  1341. * @param None
  1342. * @retval None
  1343. */
  1344. static void SetSysClockTo150M(void)
  1345. {
  1346. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  1347. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  1348. /* Enable HSE */
  1349. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSEEN);
  1350. /* Wait till HSE is ready and if Time out is reached exit */
  1351. do
  1352. {
  1353. HSEStatus = RCC->CTRL & RCC_CTRL_HSESTBL;
  1354. StartUpCounter++;
  1355. }
  1356. while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  1357. #ifdef AT32F403xx
  1358. WaitHseStbl(HSE_STABLE_DELAY);
  1359. #endif
  1360. if ((RCC->CTRL & RCC_CTRL_HSESTBL) != RESET)
  1361. {
  1362. HSEStatus = (uint32_t)0x01;
  1363. }
  1364. else
  1365. {
  1366. HSEStatus = (uint32_t)0x00;
  1367. }
  1368. if (HSEStatus == (uint32_t)0x01)
  1369. {
  1370. #if defined (AT32F415xx)
  1371. /* Enable Prefetch Buffer */
  1372. FLASH->ACR |= FLASH_ACR_PRFTBE;
  1373. /* Flash 1 wait state */
  1374. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  1375. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_4;
  1376. #endif
  1377. /* HCLK = SYSCLK */
  1378. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  1379. /* PCLK2 = HCLK/2 */
  1380. RCC->CFG &= 0xFFFFC7FF;
  1381. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV2;
  1382. /* PCLK1 = HCLK/2 */
  1383. RCC->CFG &= 0xFFFFF8FF;
  1384. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV2;
  1385. /* PLL configuration: PLLCLK = (HSE * 75) / (1 * 4) = 150 MHz */
  1386. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  1387. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSE);
  1388. RCC_PLLconfig2(PLL_FREF_8M, 75, 1, PLL_FR_4);
  1389. /* Enable PLL */
  1390. RCC->CTRL |= RCC_CTRL_PLLEN;
  1391. /* Wait till PLL is ready */
  1392. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  1393. {
  1394. }
  1395. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  1396. defined (AT32F407xx) || defined (AT32F415xx)
  1397. RCC_StepModeCmd(ENABLE);
  1398. #endif
  1399. /* Select PLL as system clock source */
  1400. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  1401. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  1402. /* Wait till PLL is used as system clock source */
  1403. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  1404. {
  1405. }
  1406. #ifdef AT32F403xx
  1407. WaitHseStbl(PLL_STABLE_DELAY);
  1408. #endif
  1409. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  1410. defined (AT32F407xx) || defined (AT32F415xx)
  1411. RCC_StepModeCmd(DISABLE);
  1412. #endif
  1413. }
  1414. else
  1415. {
  1416. /* If HSE fails to start-up, the application will have wrong clock
  1417. configuration. User can add here some code to deal with this error */
  1418. }
  1419. }
  1420. #elif defined SYSCLK_FREQ_168MHz
  1421. /**
  1422. * @brief Sets System clock frequency to 168MHz and configure HCLK, PCLK2
  1423. * and PCLK1 prescalers.
  1424. * @note This function should be used only after reset.
  1425. * @param None
  1426. * @retval None
  1427. */
  1428. static void SetSysClockTo168M(void)
  1429. {
  1430. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  1431. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  1432. /* Enable HSE */
  1433. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSEEN);
  1434. /* Wait till HSE is ready and if Time out is reached exit */
  1435. do
  1436. {
  1437. HSEStatus = RCC->CTRL & RCC_CTRL_HSESTBL;
  1438. StartUpCounter++;
  1439. }
  1440. while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  1441. #ifdef AT32F403xx
  1442. WaitHseStbl(HSE_STABLE_DELAY);
  1443. #endif
  1444. if ((RCC->CTRL & RCC_CTRL_HSESTBL) != RESET)
  1445. {
  1446. HSEStatus = (uint32_t)0x01;
  1447. }
  1448. else
  1449. {
  1450. HSEStatus = (uint32_t)0x00;
  1451. }
  1452. if (HSEStatus == (uint32_t)0x01)
  1453. {
  1454. /* HCLK = SYSCLK */
  1455. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  1456. /* PCLK2 = HCLK/2 */
  1457. RCC->CFG &= 0xFFFFC7FF;
  1458. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV2;
  1459. /* PCLK1 = HCLK/2 */
  1460. RCC->CFG &= 0xFFFFF8FF;
  1461. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV2;
  1462. /* PLL configuration: PLLCLK = HSE * 21 = 168 MHz */
  1463. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  1464. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSE | RCC_CFG_PLLMULT21 | RCC_CFG_PLLRANGE_GT72MHZ);
  1465. /* Enable PLL */
  1466. RCC->CTRL |= RCC_CTRL_PLLEN;
  1467. /* Wait till PLL is ready */
  1468. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  1469. {
  1470. }
  1471. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  1472. defined (AT32F407xx)
  1473. RCC_StepModeCmd(ENABLE);
  1474. #endif
  1475. /* Select PLL as system clock source */
  1476. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  1477. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  1478. /* Wait till PLL is used as system clock source */
  1479. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  1480. {
  1481. }
  1482. #ifdef AT32F403xx
  1483. WaitHseStbl(PLL_STABLE_DELAY);
  1484. #endif
  1485. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  1486. defined (AT32F407xx)
  1487. RCC_StepModeCmd(DISABLE);
  1488. #endif
  1489. }
  1490. else
  1491. {
  1492. /* If HSE fails to start-up, the application will have wrong clock
  1493. configuration. User can add here some code to deal with this error */
  1494. }
  1495. }
  1496. #elif defined SYSCLK_FREQ_176MHz
  1497. /**
  1498. * @brief Sets System clock frequency to 176MHz and configure HCLK, PCLK2
  1499. * and PCLK1 prescalers.
  1500. * @note This function should be used only after reset.
  1501. * @param None
  1502. * @retval None
  1503. */
  1504. static void SetSysClockTo176M(void)
  1505. {
  1506. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  1507. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  1508. /* Enable HSE */
  1509. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSEEN);
  1510. /* Wait till HSE is ready and if Time out is reached exit */
  1511. do
  1512. {
  1513. HSEStatus = RCC->CTRL & RCC_CTRL_HSESTBL;
  1514. StartUpCounter++;
  1515. }
  1516. while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  1517. #ifdef AT32F403xx
  1518. WaitHseStbl(HSE_STABLE_DELAY);
  1519. #endif
  1520. if ((RCC->CTRL & RCC_CTRL_HSESTBL) != RESET)
  1521. {
  1522. HSEStatus = (uint32_t)0x01;
  1523. }
  1524. else
  1525. {
  1526. HSEStatus = (uint32_t)0x00;
  1527. }
  1528. if (HSEStatus == (uint32_t)0x01)
  1529. {
  1530. /* HCLK = SYSCLK */
  1531. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  1532. /* PCLK2 = HCLK/2 */
  1533. RCC->CFG &= 0xFFFFC7FF;
  1534. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV2;
  1535. /* PCLK1 = HCLK/2 */
  1536. RCC->CFG &= 0xFFFFF8FF;
  1537. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV2;
  1538. /* PLL configuration: PLLCLK = HSE * 22 = 176 MHz */
  1539. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  1540. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSE | RCC_CFG_PLLMULT22 | RCC_CFG_PLLRANGE_GT72MHZ);
  1541. /* Enable PLL */
  1542. RCC->CTRL |= RCC_CTRL_PLLEN;
  1543. /* Wait till PLL is ready */
  1544. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  1545. {
  1546. }
  1547. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  1548. defined (AT32F407xx)
  1549. RCC_StepModeCmd(ENABLE);
  1550. #endif
  1551. /* Select PLL as system clock source */
  1552. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  1553. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  1554. /* Wait till PLL is used as system clock source */
  1555. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  1556. {
  1557. }
  1558. #ifdef AT32F403xx
  1559. WaitHseStbl(PLL_STABLE_DELAY);
  1560. #endif
  1561. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  1562. defined (AT32F407xx)
  1563. RCC_StepModeCmd(DISABLE);
  1564. #endif
  1565. }
  1566. else
  1567. {
  1568. /* If HSE fails to start-up, the application will have wrong clock
  1569. configuration. User can add here some code to deal with this error */
  1570. }
  1571. }
  1572. #elif defined SYSCLK_FREQ_192MHz
  1573. /**
  1574. * @brief Sets System clock frequency to 192MHz and configure HCLK, PCLK2
  1575. * and PCLK1 prescalers.
  1576. * @note This function should be used only after reset.
  1577. * @param None
  1578. * @retval None
  1579. */
  1580. static void SetSysClockTo192M(void)
  1581. {
  1582. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  1583. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  1584. /* Enable HSE */
  1585. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSEEN);
  1586. /* Wait till HSE is ready and if Time out is reached exit */
  1587. do
  1588. {
  1589. HSEStatus = RCC->CTRL & RCC_CTRL_HSESTBL;
  1590. StartUpCounter++;
  1591. }
  1592. while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  1593. #ifdef AT32F403xx
  1594. WaitHseStbl(HSE_STABLE_DELAY);
  1595. #endif
  1596. if ((RCC->CTRL & RCC_CTRL_HSESTBL) != RESET)
  1597. {
  1598. HSEStatus = (uint32_t)0x01;
  1599. }
  1600. else
  1601. {
  1602. HSEStatus = (uint32_t)0x00;
  1603. }
  1604. if (HSEStatus == (uint32_t)0x01)
  1605. {
  1606. /* HCLK = SYSCLK */
  1607. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  1608. /* PCLK2 = HCLK/2 */
  1609. RCC->CFG &= 0xFFFFC7FF;
  1610. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV2;
  1611. /* PCLK1 = HCLK/2 */
  1612. RCC->CFG &= 0xFFFFF8FF;
  1613. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV2;
  1614. /* PLL configuration: PLLCLK = HSE * 24 = 192 MHz */
  1615. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  1616. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSE | RCC_CFG_PLLMULT24 | RCC_CFG_PLLRANGE_GT72MHZ);
  1617. /* Enable PLL */
  1618. RCC->CTRL |= RCC_CTRL_PLLEN;
  1619. /* Wait till PLL is ready */
  1620. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  1621. {
  1622. }
  1623. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  1624. defined (AT32F407xx)
  1625. RCC_StepModeCmd(ENABLE);
  1626. #endif
  1627. /* Select PLL as system clock source */
  1628. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  1629. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  1630. /* Wait till PLL is used as system clock source */
  1631. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  1632. {
  1633. }
  1634. #ifdef AT32F403xx
  1635. WaitHseStbl(PLL_STABLE_DELAY);
  1636. #endif
  1637. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  1638. defined (AT32F407xx)
  1639. RCC_StepModeCmd(DISABLE);
  1640. #endif
  1641. }
  1642. else
  1643. {
  1644. /* If HSE fails to start-up, the application will have wrong clock
  1645. configuration. User can add here some code to deal with this error */
  1646. }
  1647. }
  1648. #elif defined SYSCLK_FREQ_200MHz
  1649. /**
  1650. * @brief Sets System clock frequency to 200MHz and configure HCLK, PCLK2
  1651. * and PCLK1 prescalers.
  1652. * @note This function should be used only after reset.
  1653. * @param None
  1654. * @retval None
  1655. */
  1656. static void SetSysClockTo200M(void)
  1657. {
  1658. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  1659. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  1660. /* Enable HSE */
  1661. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSEEN);
  1662. /* Wait till HSE is ready and if Time out is reached exit */
  1663. do
  1664. {
  1665. HSEStatus = RCC->CTRL & RCC_CTRL_HSESTBL;
  1666. StartUpCounter++;
  1667. }
  1668. while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  1669. #ifdef AT32F403xx
  1670. WaitHseStbl(HSE_STABLE_DELAY);
  1671. #endif
  1672. if ((RCC->CTRL & RCC_CTRL_HSESTBL) != RESET)
  1673. {
  1674. HSEStatus = (uint32_t)0x01;
  1675. }
  1676. else
  1677. {
  1678. HSEStatus = (uint32_t)0x00;
  1679. }
  1680. if (HSEStatus == (uint32_t)0x01)
  1681. {
  1682. /* HCLK = SYSCLK */
  1683. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  1684. /* PCLK2 = HCLK/2 */
  1685. RCC->CFG &= 0xFFFFC7FF;
  1686. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV2;
  1687. /* PCLK1 = HCLK/2 */
  1688. RCC->CFG &= 0xFFFFF8FF;
  1689. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV2;
  1690. /* PLL configuration: PLLCLK = HSE * 25 = 200 MHz */
  1691. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  1692. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSE | RCC_CFG_PLLMULT25 | RCC_CFG_PLLRANGE_GT72MHZ);
  1693. /* Enable PLL */
  1694. RCC->CTRL |= RCC_CTRL_PLLEN;
  1695. /* Wait till PLL is ready */
  1696. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  1697. {
  1698. }
  1699. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  1700. defined (AT32F407xx)
  1701. RCC_StepModeCmd(ENABLE);
  1702. #endif
  1703. /* Select PLL as system clock source */
  1704. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  1705. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  1706. /* Wait till PLL is used as system clock source */
  1707. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  1708. {
  1709. }
  1710. #ifdef AT32F403xx
  1711. WaitHseStbl(PLL_STABLE_DELAY);
  1712. #endif
  1713. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  1714. defined (AT32F407xx)
  1715. RCC_StepModeCmd(DISABLE);
  1716. #endif
  1717. }
  1718. else
  1719. {
  1720. /* If HSE fails to start-up, the application will have wrong clock
  1721. configuration. User can add here some code to deal with this error */
  1722. }
  1723. }
  1724. #elif defined SYSCLK_FREQ_224MHz
  1725. /**
  1726. * @brief Sets System clock frequency to 224MHz and configure HCLK, PCLK2
  1727. * and PCLK1 prescalers.
  1728. * @note This function should be used only after reset.
  1729. * @param None
  1730. * @retval None
  1731. */
  1732. static void SetSysClockTo224M(void)
  1733. {
  1734. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  1735. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  1736. /* Enable HSE */
  1737. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSEEN);
  1738. /* Wait till HSE is ready and if Time out is reached exit */
  1739. do
  1740. {
  1741. HSEStatus = RCC->CTRL & RCC_CTRL_HSESTBL;
  1742. StartUpCounter++;
  1743. }
  1744. while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  1745. #ifdef AT32F403xx
  1746. WaitHseStbl(HSE_STABLE_DELAY);
  1747. #endif
  1748. if ((RCC->CTRL & RCC_CTRL_HSESTBL) != RESET)
  1749. {
  1750. HSEStatus = (uint32_t)0x01;
  1751. }
  1752. else
  1753. {
  1754. HSEStatus = (uint32_t)0x00;
  1755. }
  1756. if (HSEStatus == (uint32_t)0x01)
  1757. {
  1758. /* HCLK = SYSCLK */
  1759. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  1760. /* PCLK2 = HCLK/2 */
  1761. RCC->CFG &= 0xFFFFC7FF;
  1762. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV2;
  1763. /* PCLK1 = HCLK/2 */
  1764. RCC->CFG &= 0xFFFFF8FF;
  1765. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV2;
  1766. /* PLL configuration: PLLCLK = HSE * 28 = 224 MHz */
  1767. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  1768. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSE | RCC_CFG_PLLMULT28 | RCC_CFG_PLLRANGE_GT72MHZ);
  1769. /* Enable PLL */
  1770. RCC->CTRL |= RCC_CTRL_PLLEN;
  1771. /* Wait till PLL is ready */
  1772. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  1773. {
  1774. }
  1775. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  1776. defined (AT32F407xx)
  1777. RCC_StepModeCmd(ENABLE);
  1778. #endif
  1779. /* Select PLL as system clock source */
  1780. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  1781. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  1782. /* Wait till PLL is used as system clock source */
  1783. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  1784. {
  1785. }
  1786. #ifdef AT32F403xx
  1787. WaitHseStbl(PLL_STABLE_DELAY);
  1788. #endif
  1789. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  1790. defined (AT32F407xx)
  1791. RCC_StepModeCmd(DISABLE);
  1792. #endif
  1793. }
  1794. else
  1795. {
  1796. /* If HSE fails to start-up, the application will have wrong clock
  1797. configuration. User can add here some code to deal with this error */
  1798. }
  1799. }
  1800. #elif defined SYSCLK_FREQ_240MHz
  1801. /**
  1802. * @brief Sets System clock frequency to 240MHz and configure HCLK, PCLK2
  1803. * and PCLK1 prescalers.
  1804. * @note This function should be used only after reset.
  1805. * @param None
  1806. * @retval None
  1807. */
  1808. static void SetSysClockTo240M(void)
  1809. {
  1810. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  1811. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  1812. /* Enable HSE */
  1813. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSEEN);
  1814. /* Wait till HSE is ready and if Time out is reached exit */
  1815. do
  1816. {
  1817. HSEStatus = RCC->CTRL & RCC_CTRL_HSESTBL;
  1818. StartUpCounter++;
  1819. }
  1820. while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  1821. #ifdef AT32F403xx
  1822. WaitHseStbl(HSE_STABLE_DELAY);
  1823. #endif
  1824. if ((RCC->CTRL & RCC_CTRL_HSESTBL) != RESET)
  1825. {
  1826. HSEStatus = (uint32_t)0x01;
  1827. }
  1828. else
  1829. {
  1830. HSEStatus = (uint32_t)0x00;
  1831. }
  1832. if (HSEStatus == (uint32_t)0x01)
  1833. {
  1834. /* HCLK = SYSCLK */
  1835. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  1836. /* PCLK2 = HCLK/2 */
  1837. RCC->CFG &= 0xFFFFC7FF;
  1838. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV2;
  1839. /* PCLK1 = HCLK/2 */
  1840. RCC->CFG &= 0xFFFFF8FF;
  1841. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV2;
  1842. /* PLL configuration: PLLCLK = HSE * 30 = 240 MHz */
  1843. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  1844. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSE | RCC_CFG_PLLMULT30 | RCC_CFG_PLLRANGE_GT72MHZ);
  1845. /* Enable PLL */
  1846. RCC->CTRL |= RCC_CTRL_PLLEN;
  1847. /* Wait till PLL is ready */
  1848. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  1849. {
  1850. }
  1851. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  1852. defined (AT32F407xx)
  1853. RCC_StepModeCmd(ENABLE);
  1854. #endif
  1855. /* Select PLL as system clock source */
  1856. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  1857. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  1858. /* Wait till PLL is used as system clock source */
  1859. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  1860. {
  1861. }
  1862. #ifdef AT32F403xx
  1863. WaitHseStbl(PLL_STABLE_DELAY);
  1864. #endif
  1865. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  1866. defined (AT32F407xx)
  1867. RCC_StepModeCmd(DISABLE);
  1868. #endif
  1869. }
  1870. else
  1871. {
  1872. /* If HSE fails to start-up, the application will have wrong clock
  1873. configuration. User can add here some code to deal with this error */
  1874. }
  1875. }
  1876. #elif defined SYSCLK_FREQ_24MHz_HSI
  1877. /**
  1878. * @brief Sets System clock frequency to 24MHz from HSI and configure HCLK, PCLK2
  1879. * and PCLK1 prescalers.
  1880. * @note This function should be used only after reset.
  1881. * @param None
  1882. * @retval None
  1883. */
  1884. static void SetSysClockTo24MHSI(void)
  1885. {
  1886. __IO uint32_t StartUpCounter = 0, HSIStatus = 0;
  1887. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  1888. /* Enable HSI */
  1889. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSIEN);
  1890. /* Wait till HSI is ready and if Time out is reached exit */
  1891. do
  1892. {
  1893. HSIStatus = RCC->CTRL & RCC_CTRL_HSISTBL;
  1894. StartUpCounter++;
  1895. }
  1896. while((HSIStatus == 0) && (StartUpCounter != 0xFFFF));
  1897. if ((RCC->CTRL & RCC_CTRL_HSISTBL) != RESET)
  1898. {
  1899. HSIStatus = (uint32_t)0x01;
  1900. }
  1901. else
  1902. {
  1903. HSIStatus = (uint32_t)0x00;
  1904. }
  1905. if (HSIStatus == (uint32_t)0x01)
  1906. {
  1907. #if defined (AT32F415xx)
  1908. /* Enable Prefetch Buffer */
  1909. FLASH->ACR |= FLASH_ACR_PRFTBE;
  1910. /* Flash 1 wait state */
  1911. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  1912. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;
  1913. #endif
  1914. /* HCLK = SYSCLK */
  1915. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  1916. /* PCLK2 = HCLK */
  1917. RCC->CFG &= 0xFFFFC7FF;
  1918. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV1;
  1919. /* PCLK1 = HCLK */
  1920. RCC->CFG &= 0xFFFFF8FF;
  1921. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV1;
  1922. /* PLL configuration: PLLCLK = (HSI/2) * 6 = 24 MHz */
  1923. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  1924. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSI_DIV2 | RCC_CFG_PLLMULT6);
  1925. /* Enable PLL */
  1926. RCC->CTRL |= RCC_CTRL_PLLEN;
  1927. /* Wait till PLL is ready */
  1928. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  1929. {
  1930. }
  1931. /* Select PLL as system clock source */
  1932. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  1933. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  1934. /* Wait till PLL is used as system clock source */
  1935. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  1936. {
  1937. }
  1938. #ifdef AT32F403xx
  1939. WaitHseStbl(PLL_STABLE_DELAY);
  1940. #endif
  1941. }
  1942. }
  1943. #elif defined SYSCLK_FREQ_36MHz_HSI
  1944. /**
  1945. * @brief Sets System clock frequency to 36MHz from HSI and configure HCLK, PCLK2
  1946. * and PCLK1 prescalers.
  1947. * @note This function should be used only after reset.
  1948. * @param None
  1949. * @retval None
  1950. */
  1951. static void SetSysClockTo36MHSI(void)
  1952. {
  1953. __IO uint32_t StartUpCounter = 0, HSIStatus = 0;
  1954. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  1955. /* Enable HSI */
  1956. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSIEN);
  1957. /* Wait till HSI is ready and if Time out is reached exit */
  1958. do
  1959. {
  1960. HSIStatus = RCC->CTRL & RCC_CTRL_HSISTBL;
  1961. StartUpCounter++;
  1962. }
  1963. while((HSIStatus == 0) && (StartUpCounter != 0xFFFF));
  1964. if ((RCC->CTRL & RCC_CTRL_HSISTBL) != RESET)
  1965. {
  1966. HSIStatus = (uint32_t)0x01;
  1967. }
  1968. else
  1969. {
  1970. HSIStatus = (uint32_t)0x00;
  1971. }
  1972. if (HSIStatus == (uint32_t)0x01)
  1973. {
  1974. #if defined (AT32F415xx)
  1975. /* Enable Prefetch Buffer */
  1976. FLASH->ACR |= FLASH_ACR_PRFTBE;
  1977. /* Flash 1 wait state */
  1978. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  1979. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;
  1980. #endif
  1981. /* HCLK = SYSCLK */
  1982. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  1983. /* PCLK2 = HCLK */
  1984. RCC->CFG &= 0xFFFFC7FF;
  1985. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV1;
  1986. /* PCLK1 = HCLK */
  1987. RCC->CFG &= 0xFFFFF8FF;
  1988. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV1;
  1989. /* PLL configuration: PLLCLK = (HSI/2) * 9 = 36 MHz */
  1990. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  1991. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSI_DIV2 | RCC_CFG_PLLMULT9);
  1992. /* Enable PLL */
  1993. RCC->CTRL |= RCC_CTRL_PLLEN;
  1994. /* Wait till PLL is ready */
  1995. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  1996. {
  1997. }
  1998. /* Select PLL as system clock source */
  1999. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  2000. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  2001. /* Wait till PLL is used as system clock source */
  2002. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  2003. {
  2004. }
  2005. #ifdef AT32F403xx
  2006. WaitHseStbl(PLL_STABLE_DELAY);
  2007. #endif
  2008. }
  2009. }
  2010. #elif defined SYSCLK_FREQ_48MHz_HSI
  2011. /**
  2012. * @brief Sets System clock frequency to 48MHz from HSI and configure HCLK, PCLK2
  2013. * and PCLK1 prescalers.
  2014. * @note This function should be used only after reset.
  2015. * @param None
  2016. * @retval None
  2017. */
  2018. static void SetSysClockTo48MHSI(void)
  2019. {
  2020. __IO uint32_t StartUpCounter = 0, HSIStatus = 0;
  2021. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  2022. /* Enable HSI */
  2023. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSIEN);
  2024. /* Wait till HSI is ready and if Time out is reached exit */
  2025. do
  2026. {
  2027. HSIStatus = RCC->CTRL & RCC_CTRL_HSISTBL;
  2028. StartUpCounter++;
  2029. }
  2030. while((HSIStatus == 0) && (StartUpCounter != 0xFFFF));
  2031. if ((RCC->CTRL & RCC_CTRL_HSISTBL) != RESET)
  2032. {
  2033. HSIStatus = (uint32_t)0x01;
  2034. }
  2035. else
  2036. {
  2037. HSIStatus = (uint32_t)0x00;
  2038. }
  2039. if (HSIStatus == (uint32_t)0x01)
  2040. {
  2041. #if defined (AT32F415xx)
  2042. /* Enable Prefetch Buffer */
  2043. FLASH->ACR |= FLASH_ACR_PRFTBE;
  2044. /* Flash 1 wait state */
  2045. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  2046. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;
  2047. #endif
  2048. /* HCLK = SYSCLK */
  2049. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  2050. /* PCLK2 = HCLK */
  2051. RCC->CFG &= 0xFFFFC7FF;
  2052. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV1;
  2053. /* PCLK1 = HCLK/2 */
  2054. RCC->CFG &= 0xFFFFF8FF;
  2055. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV2;
  2056. /* PLL configuration: PLLCLK = (HSI/2) * 12 = 48 MHz */
  2057. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  2058. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSI_DIV2 | RCC_CFG_PLLMULT12);
  2059. /* Enable PLL */
  2060. RCC->CTRL |= RCC_CTRL_PLLEN;
  2061. /* Wait till PLL is ready */
  2062. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  2063. {
  2064. }
  2065. /* Select PLL as system clock source */
  2066. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  2067. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  2068. /* Wait till PLL is used as system clock source */
  2069. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  2070. {
  2071. }
  2072. #ifdef AT32F403xx
  2073. WaitHseStbl(PLL_STABLE_DELAY);
  2074. #endif
  2075. }
  2076. }
  2077. #elif defined SYSCLK_FREQ_56MHz_HSI
  2078. /**
  2079. * @brief Sets System clock frequency to 56MHz from HSI and configure HCLK, PCLK2
  2080. * and PCLK1 prescalers.
  2081. * @note This function should be used only after reset.
  2082. * @param None
  2083. * @retval None
  2084. */
  2085. static void SetSysClockTo56MHSI(void)
  2086. {
  2087. __IO uint32_t StartUpCounter = 0, HSIStatus = 0;
  2088. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  2089. /* Enable HSI */
  2090. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSIEN);
  2091. /* Wait till HSI is ready and if Time out is reached exit */
  2092. do
  2093. {
  2094. HSIStatus = RCC->CTRL & RCC_CTRL_HSISTBL;
  2095. StartUpCounter++;
  2096. }
  2097. while((HSIStatus == 0) && (StartUpCounter != 0xFFFF));
  2098. if ((RCC->CTRL & RCC_CTRL_HSISTBL) != RESET)
  2099. {
  2100. HSIStatus = (uint32_t)0x01;
  2101. }
  2102. else
  2103. {
  2104. HSIStatus = (uint32_t)0x00;
  2105. }
  2106. if (HSIStatus == (uint32_t)0x01)
  2107. {
  2108. #if defined (AT32F415xx)
  2109. /* Enable Prefetch Buffer */
  2110. FLASH->ACR |= FLASH_ACR_PRFTBE;
  2111. /* Flash 1 wait state */
  2112. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  2113. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;
  2114. #endif
  2115. /* HCLK = SYSCLK */
  2116. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  2117. /* PCLK2 = HCLK */
  2118. RCC->CFG &= 0xFFFFC7FF;
  2119. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV1;
  2120. /* PCLK1 = HCLK/2 */
  2121. RCC->CFG &= 0xFFFFF8FF;
  2122. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV2;
  2123. /* PLL configuration: PLLCLK = (HSI/2) * 14 = 56 MHz */
  2124. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  2125. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSI_DIV2 | RCC_CFG_PLLMULT14);
  2126. /* Enable PLL */
  2127. RCC->CTRL |= RCC_CTRL_PLLEN;
  2128. /* Wait till PLL is ready */
  2129. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  2130. {
  2131. }
  2132. /* Select PLL as system clock source */
  2133. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  2134. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  2135. /* Wait till PLL is used as system clock source */
  2136. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  2137. {
  2138. }
  2139. #ifdef AT32F403xx
  2140. WaitHseStbl(PLL_STABLE_DELAY);
  2141. #endif
  2142. }
  2143. }
  2144. #elif defined SYSCLK_FREQ_72MHz_HSI
  2145. /**
  2146. * @brief Sets System clock frequency to 72MHz from HSI and configure HCLK, PCLK2
  2147. * and PCLK1 prescalers.
  2148. * @note This function should be used only after reset.
  2149. * @param None
  2150. * @retval None
  2151. */
  2152. static void SetSysClockTo72MHSI(void)
  2153. {
  2154. __IO uint32_t StartUpCounter = 0, HSIStatus = 0;
  2155. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  2156. /* Enable HSI */
  2157. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSIEN);
  2158. /* Wait till HSI is ready and if Time out is reached exit */
  2159. do
  2160. {
  2161. HSIStatus = RCC->CTRL & RCC_CTRL_HSISTBL;
  2162. StartUpCounter++;
  2163. }
  2164. while((HSIStatus == 0) && (StartUpCounter != 0xFFFF));
  2165. if ((RCC->CTRL & RCC_CTRL_HSISTBL) != RESET)
  2166. {
  2167. HSIStatus = (uint32_t)0x01;
  2168. }
  2169. else
  2170. {
  2171. HSIStatus = (uint32_t)0x00;
  2172. }
  2173. if (HSIStatus == (uint32_t)0x01)
  2174. {
  2175. #if defined (AT32F415xx)
  2176. /* Enable Prefetch Buffer */
  2177. FLASH->ACR |= FLASH_ACR_PRFTBE;
  2178. /* Flash 1 wait state */
  2179. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  2180. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;
  2181. #endif
  2182. /* HCLK = SYSCLK */
  2183. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  2184. /* PCLK2 = HCLK */
  2185. RCC->CFG &= 0xFFFFC7FF;
  2186. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV1;
  2187. /* PCLK1 = HCLK/2 */
  2188. RCC->CFG &= 0xFFFFF8FF;
  2189. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV2;
  2190. /* PLL configuration: PLLCLK = (HSI/2) * 18 = 72 MHz */
  2191. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  2192. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSI_DIV2 | RCC_CFG_PLLMULT18);
  2193. /* Enable PLL */
  2194. RCC->CTRL |= RCC_CTRL_PLLEN;
  2195. /* Wait till PLL is ready */
  2196. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  2197. {
  2198. }
  2199. /* Select PLL as system clock source */
  2200. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  2201. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  2202. /* Wait till PLL is used as system clock source */
  2203. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  2204. {
  2205. }
  2206. #ifdef AT32F403xx
  2207. WaitHseStbl(PLL_STABLE_DELAY);
  2208. #endif
  2209. }
  2210. }
  2211. #elif defined SYSCLK_FREQ_96MHz_HSI
  2212. /**
  2213. * @brief Sets System clock frequency to 96MHz from HSI and configure HCLK, PCLK2
  2214. * and PCLK1 prescalers.
  2215. * @note This function should be used only after reset.
  2216. * @param None
  2217. * @retval None
  2218. */
  2219. static void SetSysClockTo96MHSI(void)
  2220. {
  2221. __IO uint32_t StartUpCounter = 0, HSIStatus = 0;
  2222. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  2223. /* Enable HSI */
  2224. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSIEN);
  2225. /* Wait till HSI is ready and if Time out is reached exit */
  2226. do
  2227. {
  2228. HSIStatus = RCC->CTRL & RCC_CTRL_HSISTBL;
  2229. StartUpCounter++;
  2230. }
  2231. while((HSIStatus == 0) && (StartUpCounter != 0xFFFF));
  2232. if ((RCC->CTRL & RCC_CTRL_HSISTBL) != RESET)
  2233. {
  2234. HSIStatus = (uint32_t)0x01;
  2235. }
  2236. else
  2237. {
  2238. HSIStatus = (uint32_t)0x00;
  2239. }
  2240. if (HSIStatus == (uint32_t)0x01)
  2241. {
  2242. #if defined (AT32F415xx)
  2243. /* Enable Prefetch Buffer */
  2244. FLASH->ACR |= FLASH_ACR_PRFTBE;
  2245. /* Flash 1 wait state */
  2246. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  2247. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;
  2248. #endif
  2249. /* HCLK = SYSCLK */
  2250. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  2251. /* PCLK2 = HCLK/2 */
  2252. RCC->CFG &= 0xFFFFC7FF;
  2253. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV2;
  2254. /* PCLK1 = HCLK/2 */
  2255. RCC->CFG &= 0xFFFFF8FF;
  2256. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV2;
  2257. /* PLL configuration: PLLCLK = (HSI/2) * 24 = 96 MHz */
  2258. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  2259. #if defined (AT32F415xx)
  2260. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSI_DIV2 | RCC_CFG_PLLMULT24);
  2261. #else
  2262. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSI_DIV2 | RCC_CFG_PLLMULT24 | RCC_CFG_PLLRANGE_GT72MHZ);
  2263. #endif
  2264. /* Enable PLL */
  2265. RCC->CTRL |= RCC_CTRL_PLLEN;
  2266. /* Wait till PLL is ready */
  2267. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  2268. {
  2269. }
  2270. /* Select PLL as system clock source */
  2271. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  2272. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  2273. /* Wait till PLL is used as system clock source */
  2274. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  2275. {
  2276. }
  2277. #ifdef AT32F403xx
  2278. WaitHseStbl(PLL_STABLE_DELAY);
  2279. #endif
  2280. }
  2281. }
  2282. #elif defined SYSCLK_FREQ_108MHz_HSI
  2283. /**
  2284. * @brief Sets System clock frequency to 108MHz from HSI and configure HCLK, PCLK2
  2285. * and PCLK1 prescalers.
  2286. * @note This function should be used only after reset.
  2287. * @param None
  2288. * @retval None
  2289. */
  2290. static void SetSysClockTo108MHSI(void)
  2291. {
  2292. __IO uint32_t StartUpCounter = 0, HSIStatus = 0;
  2293. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  2294. /* Enable HSI */
  2295. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSIEN);
  2296. /* Wait till HSI is ready and if Time out is reached exit */
  2297. do
  2298. {
  2299. HSIStatus = RCC->CTRL & RCC_CTRL_HSISTBL;
  2300. StartUpCounter++;
  2301. }
  2302. while((HSIStatus == 0) && (StartUpCounter != 0xFFFF));
  2303. if ((RCC->CTRL & RCC_CTRL_HSISTBL) != RESET)
  2304. {
  2305. HSIStatus = (uint32_t)0x01;
  2306. }
  2307. else
  2308. {
  2309. HSIStatus = (uint32_t)0x00;
  2310. }
  2311. if (HSIStatus == (uint32_t)0x01)
  2312. {
  2313. #if defined (AT32F415xx)
  2314. /* Enable Prefetch Buffer */
  2315. FLASH->ACR |= FLASH_ACR_PRFTBE;
  2316. /* Flash 1 wait state */
  2317. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  2318. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_3;
  2319. #endif
  2320. /* HCLK = SYSCLK */
  2321. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  2322. /* PCLK2 = HCLK/2 */
  2323. RCC->CFG &= 0xFFFFC7FF;
  2324. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV2;
  2325. /* PCLK1 = HCLK/2 */
  2326. RCC->CFG &= 0xFFFFF8FF;
  2327. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV2;
  2328. /* PLL configuration: PLLCLK = (HSI/2) * 27 = 108 MHz */
  2329. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  2330. #if defined (AT32F415xx)
  2331. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSI_DIV2 | RCC_CFG_PLLMULT27);
  2332. #else
  2333. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSI_DIV2 | RCC_CFG_PLLMULT27 | RCC_CFG_PLLRANGE_GT72MHZ);
  2334. #endif
  2335. /* Enable PLL */
  2336. RCC->CTRL |= RCC_CTRL_PLLEN;
  2337. /* Wait till PLL is ready */
  2338. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  2339. {
  2340. }
  2341. /* Select PLL as system clock source */
  2342. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  2343. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  2344. /* Wait till PLL is used as system clock source */
  2345. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  2346. {
  2347. }
  2348. #ifdef AT32F403xx
  2349. WaitHseStbl(PLL_STABLE_DELAY);
  2350. #endif
  2351. }
  2352. }
  2353. #elif defined SYSCLK_FREQ_120MHz_HSI
  2354. /**
  2355. * @brief Sets System clock frequency to 120MHz from HSI and configure HCLK, PCLK2
  2356. * and PCLK1 prescalers.
  2357. * @note This function should be used only after reset.
  2358. * @param None
  2359. * @retval None
  2360. */
  2361. static void SetSysClockTo120MHSI(void)
  2362. {
  2363. __IO uint32_t StartUpCounter = 0, HSIStatus = 0;
  2364. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  2365. /* Enable HSI */
  2366. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSIEN);
  2367. /* Wait till HSI is ready and if Time out is reached exit */
  2368. do
  2369. {
  2370. HSIStatus = RCC->CTRL & RCC_CTRL_HSISTBL;
  2371. StartUpCounter++;
  2372. }
  2373. while((HSIStatus == 0) && (StartUpCounter != 0xFFFF));
  2374. if ((RCC->CTRL & RCC_CTRL_HSISTBL) != RESET)
  2375. {
  2376. HSIStatus = (uint32_t)0x01;
  2377. }
  2378. else
  2379. {
  2380. HSIStatus = (uint32_t)0x00;
  2381. }
  2382. if (HSIStatus == (uint32_t)0x01)
  2383. {
  2384. #if defined (AT32F415xx)
  2385. /* Enable Prefetch Buffer */
  2386. FLASH->ACR |= FLASH_ACR_PRFTBE;
  2387. /* Flash 1 wait state */
  2388. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  2389. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_3;
  2390. #endif
  2391. /* HCLK = SYSCLK */
  2392. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  2393. /* PCLK2 = HCLK/2 */
  2394. RCC->CFG &= 0xFFFFC7FF;
  2395. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV2;
  2396. /* PCLK1 = HCLK/2 */
  2397. RCC->CFG &= 0xFFFFF8FF;
  2398. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV2;
  2399. /* PLL configuration: PLLCLK = (HSI/2) * 30 = 120 MHz */
  2400. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  2401. #if defined (AT32F415xx)
  2402. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSI_DIV2 | RCC_CFG_PLLMULT30);
  2403. #else
  2404. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSI_DIV2 | RCC_CFG_PLLMULT30 | RCC_CFG_PLLRANGE_GT72MHZ);
  2405. #endif
  2406. /* Enable PLL */
  2407. RCC->CTRL |= RCC_CTRL_PLLEN;
  2408. /* Wait till PLL is ready */
  2409. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  2410. {
  2411. }
  2412. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  2413. defined (AT32F407xx) || defined (AT32F415xx)
  2414. RCC_StepModeCmd(ENABLE);
  2415. #endif
  2416. /* Select PLL as system clock source */
  2417. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  2418. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  2419. /* Wait till PLL is used as system clock source */
  2420. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  2421. {
  2422. }
  2423. #ifdef AT32F403xx
  2424. WaitHseStbl(PLL_STABLE_DELAY);
  2425. #endif
  2426. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  2427. defined (AT32F407xx) || defined (AT32F415xx)
  2428. RCC_StepModeCmd(DISABLE);
  2429. #endif
  2430. }
  2431. }
  2432. #elif defined SYSCLK_FREQ_144MHz_HSI
  2433. /**
  2434. * @brief Sets System clock frequency to 144MHz from HSI and configure HCLK, PCLK2
  2435. * and PCLK1 prescalers.
  2436. * @note This function should be used only after reset.
  2437. * @param None
  2438. * @retval None
  2439. */
  2440. static void SetSysClockTo144MHSI(void)
  2441. {
  2442. __IO uint32_t StartUpCounter = 0, HSIStatus = 0;
  2443. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  2444. /* Enable HSI */
  2445. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSIEN);
  2446. /* Wait till HSI is ready and if Time out is reached exit */
  2447. do
  2448. {
  2449. HSIStatus = RCC->CTRL & RCC_CTRL_HSISTBL;
  2450. StartUpCounter++;
  2451. }
  2452. while((HSIStatus == 0) && (StartUpCounter != 0xFFFF));
  2453. if ((RCC->CTRL & RCC_CTRL_HSISTBL) != RESET)
  2454. {
  2455. HSIStatus = (uint32_t)0x01;
  2456. }
  2457. else
  2458. {
  2459. HSIStatus = (uint32_t)0x00;
  2460. }
  2461. if (HSIStatus == (uint32_t)0x01)
  2462. {
  2463. #if defined (AT32F415xx)
  2464. /* Enable Prefetch Buffer */
  2465. FLASH->ACR |= FLASH_ACR_PRFTBE;
  2466. /* Flash 1 wait state */
  2467. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  2468. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_4;
  2469. #endif
  2470. /* HCLK = SYSCLK */
  2471. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  2472. /* PCLK2 = HCLK/2 */
  2473. RCC->CFG &= 0xFFFFC7FF;
  2474. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV2;
  2475. /* PCLK1 = HCLK/2 */
  2476. RCC->CFG &= 0xFFFFF8FF;
  2477. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV2;
  2478. /* PLL configuration: PLLCLK = (HSI/2) * 36 = 144 MHz */
  2479. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  2480. #if defined (AT32F415xx)
  2481. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSI_DIV2 | RCC_CFG_PLLMULT36);
  2482. #else
  2483. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSI_DIV2 | RCC_CFG_PLLMULT36 | RCC_CFG_PLLRANGE_GT72MHZ);
  2484. #endif
  2485. /* Enable PLL */
  2486. RCC->CTRL |= RCC_CTRL_PLLEN;
  2487. /* Wait till PLL is ready */
  2488. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  2489. {
  2490. }
  2491. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  2492. defined (AT32F407xx) || defined (AT32F415xx)
  2493. RCC_StepModeCmd(ENABLE);
  2494. #endif
  2495. /* Select PLL as system clock source */
  2496. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  2497. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  2498. /* Wait till PLL is used as system clock source */
  2499. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  2500. {
  2501. }
  2502. #ifdef AT32F403xx
  2503. WaitHseStbl(PLL_STABLE_DELAY);
  2504. #endif
  2505. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  2506. defined (AT32F407xx) || defined (AT32F415xx)
  2507. RCC_StepModeCmd(DISABLE);
  2508. #endif
  2509. }
  2510. }
  2511. #elif defined SYSCLK_FREQ_150MHz_HSI
  2512. /**
  2513. * @brief Sets System clock frequency to 150MHz from HSI and configure HCLK, PCLK2
  2514. * and PCLK1 prescalers.
  2515. * @note This function should be used only after reset.
  2516. * @param None
  2517. * @retval None
  2518. */
  2519. static void SetSysClockTo150MHSI(void)
  2520. {
  2521. __IO uint32_t StartUpCounter = 0, HSIStatus = 0;
  2522. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  2523. /* Enable HSI */
  2524. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSIEN);
  2525. /* Wait till HSI is ready and if Time out is reached exit */
  2526. do
  2527. {
  2528. HSIStatus = RCC->CTRL & RCC_CTRL_HSISTBL;
  2529. StartUpCounter++;
  2530. }
  2531. while((HSIStatus == 0) && (StartUpCounter != 0xFFFF));
  2532. if ((RCC->CTRL & RCC_CTRL_HSISTBL) != RESET)
  2533. {
  2534. HSIStatus = (uint32_t)0x01;
  2535. }
  2536. else
  2537. {
  2538. HSIStatus = (uint32_t)0x00;
  2539. }
  2540. if (HSIStatus == (uint32_t)0x01)
  2541. {
  2542. #if defined (AT32F415xx)
  2543. /* Enable Prefetch Buffer */
  2544. FLASH->ACR |= FLASH_ACR_PRFTBE;
  2545. /* Flash 1 wait state */
  2546. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  2547. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_4;
  2548. #endif
  2549. /* HCLK = SYSCLK */
  2550. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  2551. /* PCLK2 = HCLK/2 */
  2552. RCC->CFG &= 0xFFFFC7FF;
  2553. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV2;
  2554. /* PCLK1 = HCLK/2 */
  2555. RCC->CFG &= 0xFFFFF8FF;
  2556. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV2;
  2557. /* PLL configuration: PLLCLK = ((HSI/2) * 150) / (1 * 4) = 150 MHz */
  2558. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  2559. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSI_DIV2);
  2560. RCC_PLLconfig2(PLL_FREF_4M, 150, 1, PLL_FR_4);
  2561. /* Enable PLL */
  2562. RCC->CTRL |= RCC_CTRL_PLLEN;
  2563. /* Wait till PLL is ready */
  2564. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  2565. {
  2566. }
  2567. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  2568. defined (AT32F407xx) || defined (AT32F415xx)
  2569. RCC_StepModeCmd(ENABLE);
  2570. #endif
  2571. /* Select PLL as system clock source */
  2572. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  2573. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  2574. /* Wait till PLL is used as system clock source */
  2575. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  2576. {
  2577. }
  2578. #ifdef AT32F403xx
  2579. WaitHseStbl(PLL_STABLE_DELAY);
  2580. #endif
  2581. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  2582. defined (AT32F407xx) || defined (AT32F415xx)
  2583. RCC_StepModeCmd(DISABLE);
  2584. #endif
  2585. }
  2586. }
  2587. #elif defined SYSCLK_FREQ_168MHz_HSI
  2588. /**
  2589. * @brief Sets System clock frequency to 168MHz from HSI and configure HCLK, PCLK2
  2590. * and PCLK1 prescalers.
  2591. * @note This function should be used only after reset.
  2592. * @param None
  2593. * @retval None
  2594. */
  2595. static void SetSysClockTo168MHSI(void)
  2596. {
  2597. __IO uint32_t StartUpCounter = 0, HSIStatus = 0;
  2598. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  2599. /* Enable HSI */
  2600. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSIEN);
  2601. /* Wait till HSI is ready and if Time out is reached exit */
  2602. do
  2603. {
  2604. HSIStatus = RCC->CTRL & RCC_CTRL_HSISTBL;
  2605. StartUpCounter++;
  2606. }
  2607. while((HSIStatus == 0) && (StartUpCounter != 0xFFFF));
  2608. if ((RCC->CTRL & RCC_CTRL_HSISTBL) != RESET)
  2609. {
  2610. HSIStatus = (uint32_t)0x01;
  2611. }
  2612. else
  2613. {
  2614. HSIStatus = (uint32_t)0x00;
  2615. }
  2616. if (HSIStatus == (uint32_t)0x01)
  2617. {
  2618. /* HCLK = SYSCLK */
  2619. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  2620. /* PCLK2 = HCLK/2 */
  2621. RCC->CFG &= 0xFFFFC7FF;
  2622. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV2;
  2623. /* PCLK1 = HCLK/2 */
  2624. RCC->CFG &= 0xFFFFF8FF;
  2625. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV2;
  2626. /* PLL configuration: PLLCLK = (HSI/2) * 42 = 168 MHz */
  2627. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  2628. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSI_DIV2 | RCC_CFG_PLLMULT42 | RCC_CFG_PLLRANGE_GT72MHZ);
  2629. /* Enable PLL */
  2630. RCC->CTRL |= RCC_CTRL_PLLEN;
  2631. /* Wait till PLL is ready */
  2632. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  2633. {
  2634. }
  2635. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  2636. defined (AT32F407xx)
  2637. RCC_StepModeCmd(ENABLE);
  2638. #endif
  2639. /* Select PLL as system clock source */
  2640. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  2641. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  2642. /* Wait till PLL is used as system clock source */
  2643. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  2644. {
  2645. }
  2646. #ifdef AT32F403xx
  2647. WaitHseStbl(PLL_STABLE_DELAY);
  2648. #endif
  2649. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  2650. defined (AT32F407xx)
  2651. RCC_StepModeCmd(DISABLE);
  2652. #endif
  2653. }
  2654. }
  2655. #elif defined SYSCLK_FREQ_176MHz_HSI
  2656. /**
  2657. * @brief Sets System clock frequency to 176MHz from HSI and configure HCLK, PCLK2
  2658. * and PCLK1 prescalers.
  2659. * @note This function should be used only after reset.
  2660. * @param None
  2661. * @retval None
  2662. */
  2663. static void SetSysClockTo176MHSI(void)
  2664. {
  2665. __IO uint32_t StartUpCounter = 0, HSIStatus = 0;
  2666. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  2667. /* Enable HSI */
  2668. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSIEN);
  2669. /* Wait till HSI is ready and if Time out is reached exit */
  2670. do
  2671. {
  2672. HSIStatus = RCC->CTRL & RCC_CTRL_HSISTBL;
  2673. StartUpCounter++;
  2674. }
  2675. while((HSIStatus == 0) && (StartUpCounter != 0xFFFF));
  2676. if ((RCC->CTRL & RCC_CTRL_HSISTBL) != RESET)
  2677. {
  2678. HSIStatus = (uint32_t)0x01;
  2679. }
  2680. else
  2681. {
  2682. HSIStatus = (uint32_t)0x00;
  2683. }
  2684. if (HSIStatus == (uint32_t)0x01)
  2685. {
  2686. /* HCLK = SYSCLK */
  2687. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  2688. /* PCLK2 = HCLK/2 */
  2689. RCC->CFG &= 0xFFFFC7FF;
  2690. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV2;
  2691. /* PCLK1 = HCLK/2 */
  2692. RCC->CFG &= 0xFFFFF8FF;
  2693. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV2;
  2694. /* PLL configuration: PLLCLK = (HSI/2) * 44 = 176 MHz */
  2695. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  2696. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSI_DIV2 | RCC_CFG_PLLMULT44 | RCC_CFG_PLLRANGE_GT72MHZ);
  2697. /* Enable PLL */
  2698. RCC->CTRL |= RCC_CTRL_PLLEN;
  2699. /* Wait till PLL is ready */
  2700. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  2701. {
  2702. }
  2703. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  2704. defined (AT32F407xx)
  2705. RCC_StepModeCmd(ENABLE);
  2706. #endif
  2707. /* Select PLL as system clock source */
  2708. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  2709. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  2710. /* Wait till PLL is used as system clock source */
  2711. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  2712. {
  2713. }
  2714. #ifdef AT32F403xx
  2715. WaitHseStbl(PLL_STABLE_DELAY);
  2716. #endif
  2717. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  2718. defined (AT32F407xx)
  2719. RCC_StepModeCmd(DISABLE);
  2720. #endif
  2721. }
  2722. }
  2723. #elif defined SYSCLK_FREQ_192MHz_HSI
  2724. /**
  2725. * @brief Sets System clock frequency to 192MHz from HSI and configure HCLK, PCLK2
  2726. * and PCLK1 prescalers.
  2727. * @note This function should be used only after reset.
  2728. * @param None
  2729. * @retval None
  2730. */
  2731. static void SetSysClockTo192MHSI(void)
  2732. {
  2733. __IO uint32_t StartUpCounter = 0, HSIStatus = 0;
  2734. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  2735. /* Enable HSI */
  2736. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSIEN);
  2737. /* Wait till HSI is ready and if Time out is reached exit */
  2738. do
  2739. {
  2740. HSIStatus = RCC->CTRL & RCC_CTRL_HSISTBL;
  2741. StartUpCounter++;
  2742. }
  2743. while((HSIStatus == 0) && (StartUpCounter != 0xFFFF));
  2744. if ((RCC->CTRL & RCC_CTRL_HSISTBL) != RESET)
  2745. {
  2746. HSIStatus = (uint32_t)0x01;
  2747. }
  2748. else
  2749. {
  2750. HSIStatus = (uint32_t)0x00;
  2751. }
  2752. if (HSIStatus == (uint32_t)0x01)
  2753. {
  2754. /* HCLK = SYSCLK */
  2755. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  2756. /* PCLK2 = HCLK/2 */
  2757. RCC->CFG &= 0xFFFFC7FF;
  2758. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV2;
  2759. /* PCLK1 = HCLK/2 */
  2760. RCC->CFG &= 0xFFFFF8FF;
  2761. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV2;
  2762. /* PLL configuration: PLLCLK = (HSI/2) * 48 = 192 MHz */
  2763. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  2764. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSI_DIV2 | RCC_CFG_PLLMULT48 | RCC_CFG_PLLRANGE_GT72MHZ);
  2765. /* Enable PLL */
  2766. RCC->CTRL |= RCC_CTRL_PLLEN;
  2767. /* Wait till PLL is ready */
  2768. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  2769. {
  2770. }
  2771. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  2772. defined (AT32F407xx)
  2773. RCC_StepModeCmd(ENABLE);
  2774. #endif
  2775. /* Select PLL as system clock source */
  2776. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  2777. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  2778. /* Wait till PLL is used as system clock source */
  2779. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  2780. {
  2781. }
  2782. #ifdef AT32F403xx
  2783. WaitHseStbl(PLL_STABLE_DELAY);
  2784. #endif
  2785. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  2786. defined (AT32F407xx)
  2787. RCC_StepModeCmd(DISABLE);
  2788. #endif
  2789. }
  2790. }
  2791. #elif defined SYSCLK_FREQ_200MHz_HSI
  2792. /**
  2793. * @brief Sets System clock frequency to 200MHz from HSI and configure HCLK, PCLK2
  2794. * and PCLK1 prescalers.
  2795. * @note This function should be used only after reset.
  2796. * @param None
  2797. * @retval None
  2798. */
  2799. static void SetSysClockTo200MHSI(void)
  2800. {
  2801. __IO uint32_t StartUpCounter = 0, HSIStatus = 0;
  2802. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  2803. /* Enable HSI */
  2804. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSIEN);
  2805. /* Wait till HSI is ready and if Time out is reached exit */
  2806. do
  2807. {
  2808. HSIStatus = RCC->CTRL & RCC_CTRL_HSISTBL;
  2809. StartUpCounter++;
  2810. }
  2811. while((HSIStatus == 0) && (StartUpCounter != 0xFFFF));
  2812. if ((RCC->CTRL & RCC_CTRL_HSISTBL) != RESET)
  2813. {
  2814. HSIStatus = (uint32_t)0x01;
  2815. }
  2816. else
  2817. {
  2818. HSIStatus = (uint32_t)0x00;
  2819. }
  2820. if (HSIStatus == (uint32_t)0x01)
  2821. {
  2822. /* HCLK = SYSCLK */
  2823. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  2824. /* PCLK2 = HCLK/2 */
  2825. RCC->CFG &= 0xFFFFC7FF;
  2826. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV2;
  2827. /* PCLK1 = HCLK/2 */
  2828. RCC->CFG &= 0xFFFFF8FF;
  2829. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV2;
  2830. /* PLL configuration: PLLCLK = (HSI/2) * 50 = 200 MHz */
  2831. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  2832. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSI_DIV2 | RCC_CFG_PLLMULT50 | RCC_CFG_PLLRANGE_GT72MHZ);
  2833. /* Enable PLL */
  2834. RCC->CTRL |= RCC_CTRL_PLLEN;
  2835. /* Wait till PLL is ready */
  2836. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  2837. {
  2838. }
  2839. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  2840. defined (AT32F407xx)
  2841. RCC_StepModeCmd(ENABLE);
  2842. #endif
  2843. /* Select PLL as system clock source */
  2844. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  2845. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  2846. /* Wait till PLL is used as system clock source */
  2847. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  2848. {
  2849. }
  2850. #ifdef AT32F403xx
  2851. WaitHseStbl(PLL_STABLE_DELAY);
  2852. #endif
  2853. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  2854. defined (AT32F407xx)
  2855. RCC_StepModeCmd(DISABLE);
  2856. #endif
  2857. }
  2858. }
  2859. #elif defined SYSCLK_FREQ_224MHz_HSI
  2860. /**
  2861. * @brief Sets System clock frequency to 224MHz from HSI and configure HCLK, PCLK2
  2862. * and PCLK1 prescalers.
  2863. * @note This function should be used only after reset.
  2864. * @param None
  2865. * @retval None
  2866. */
  2867. static void SetSysClockTo224MHSI(void)
  2868. {
  2869. __IO uint32_t StartUpCounter = 0, HSIStatus = 0;
  2870. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  2871. /* Enable HSI */
  2872. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSIEN);
  2873. /* Wait till HSI is ready and if Time out is reached exit */
  2874. do
  2875. {
  2876. HSIStatus = RCC->CTRL & RCC_CTRL_HSISTBL;
  2877. StartUpCounter++;
  2878. }
  2879. while((HSIStatus == 0) && (StartUpCounter != 0xFFFF));
  2880. if ((RCC->CTRL & RCC_CTRL_HSISTBL) != RESET)
  2881. {
  2882. HSIStatus = (uint32_t)0x01;
  2883. }
  2884. else
  2885. {
  2886. HSIStatus = (uint32_t)0x00;
  2887. }
  2888. if (HSIStatus == (uint32_t)0x01)
  2889. {
  2890. /* HCLK = SYSCLK */
  2891. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  2892. /* PCLK2 = HCLK/2 */
  2893. RCC->CFG &= 0xFFFFC7FF;
  2894. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV2;
  2895. /* PCLK1 = HCLK/2 */
  2896. RCC->CFG &= 0xFFFFF8FF;
  2897. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV2;
  2898. /* PLL configuration: PLLCLK = (HSI/2) * 56 = 224 MHz */
  2899. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  2900. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSI_DIV2 | RCC_CFG_PLLMULT56 | RCC_CFG_PLLRANGE_GT72MHZ);
  2901. /* Enable PLL */
  2902. RCC->CTRL |= RCC_CTRL_PLLEN;
  2903. /* Wait till PLL is ready */
  2904. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  2905. {
  2906. }
  2907. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  2908. defined (AT32F407xx)
  2909. RCC_StepModeCmd(ENABLE);
  2910. #endif
  2911. /* Select PLL as system clock source */
  2912. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  2913. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  2914. /* Wait till PLL is used as system clock source */
  2915. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  2916. {
  2917. }
  2918. #ifdef AT32F403xx
  2919. WaitHseStbl(PLL_STABLE_DELAY);
  2920. #endif
  2921. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  2922. defined (AT32F407xx)
  2923. RCC_StepModeCmd(DISABLE);
  2924. #endif
  2925. }
  2926. }
  2927. #elif defined SYSCLK_FREQ_240MHz_HSI
  2928. /**
  2929. * @brief Sets System clock frequency to 240MHz from HSI and configure HCLK, PCLK2
  2930. * and PCLK1 prescalers.
  2931. * @note This function should be used only after reset.
  2932. * @param None
  2933. * @retval None
  2934. */
  2935. static void SetSysClockTo240MHSI(void)
  2936. {
  2937. __IO uint32_t StartUpCounter = 0, HSIStatus = 0;
  2938. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  2939. /* Enable HSI */
  2940. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSIEN);
  2941. /* Wait till HSI is ready and if Time out is reached exit */
  2942. do
  2943. {
  2944. HSIStatus = RCC->CTRL & RCC_CTRL_HSISTBL;
  2945. StartUpCounter++;
  2946. }
  2947. while((HSIStatus == 0) && (StartUpCounter != 0xFFFF));
  2948. if ((RCC->CTRL & RCC_CTRL_HSISTBL) != RESET)
  2949. {
  2950. HSIStatus = (uint32_t)0x01;
  2951. }
  2952. else
  2953. {
  2954. HSIStatus = (uint32_t)0x00;
  2955. }
  2956. if (HSIStatus == (uint32_t)0x01)
  2957. {
  2958. /* HCLK = SYSCLK */
  2959. RCC->CFG |= (uint32_t)RCC_CFG_AHBPSC_DIV1;
  2960. /* PCLK2 = HCLK/2 */
  2961. RCC->CFG &= 0xFFFFC7FF;
  2962. RCC->CFG |= (uint32_t)RCC_CFG_APB2PSC_DIV2;
  2963. /* PCLK1 = HCLK/2 */
  2964. RCC->CFG &= 0xFFFFF8FF;
  2965. RCC->CFG |= (uint32_t)RCC_CFG_APB1PSC_DIV2;
  2966. /* PLL configuration: PLLCLK = (HSI/2) * 60 = 240 MHz */
  2967. RCC->CFG &= RCC_CFG_PLLCFG_MASK;
  2968. RCC->CFG |= (uint32_t)(RCC_CFG_PLLRC_HSI_DIV2 | RCC_CFG_PLLMULT60 | RCC_CFG_PLLRANGE_GT72MHZ);
  2969. /* Enable PLL */
  2970. RCC->CTRL |= RCC_CTRL_PLLEN;
  2971. /* Wait till PLL is ready */
  2972. while((RCC->CTRL & RCC_CTRL_PLLSTBL) == 0)
  2973. {
  2974. }
  2975. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  2976. defined (AT32F407xx)
  2977. RCC_StepModeCmd(ENABLE);
  2978. #endif
  2979. /* Select PLL as system clock source */
  2980. RCC->CFG &= (uint32_t)((uint32_t)~(RCC_CFG_SYSCLKSEL));
  2981. RCC->CFG |= (uint32_t)RCC_CFG_SYSCLKSEL_PLL;
  2982. /* Wait till PLL is used as system clock source */
  2983. while ((RCC->CFG & (uint32_t)RCC_CFG_SYSCLKSTS) != RCC_CFG_SYSCLKSTS_PLL)
  2984. {
  2985. }
  2986. #ifdef AT32F403xx
  2987. WaitHseStbl(PLL_STABLE_DELAY);
  2988. #endif
  2989. #if defined (AT32F413xx) || defined (AT32F403Axx)|| \
  2990. defined (AT32F407xx)
  2991. RCC_StepModeCmd(DISABLE);
  2992. #endif
  2993. }
  2994. }
  2995. #endif
  2996. /**
  2997. * @}
  2998. */
  2999. /**
  3000. * @}
  3001. */
  3002. /**
  3003. * @}
  3004. */
  3005. /******************* (C) COPYRIGHT 2018 ArteryTek *****END OF FILE****/