Advertisement
Guest User

Untitled

a guest
Feb 25th, 2016
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.60 KB | None | 0 0
  1. #define STM32_CLOCKSOURCE_DEFAULT_TIMEOUT 100000
  2.  
  3. #define stm32_rccEnableHSI() do { STM32_RCC->CR |= RCC_CR_HSION; } while (0)
  4. #define stm32_rccDisableHSI() do { STM32_RCC->CR &= ~RCC_CR_HSION; } while (0)
  5. #define stm32_rccEnableHSE() do { STM32_RCC->CR |= RCC_CR_HSEON; } while (0)
  6. #define stm32_rccDisableHSE() do { STM32_RCC->CR &= ~RCC_CR_HSEON; } while (0)
  7. #define stm32_rccEnablePLL() do { STM32_RCC->CR |= RCC_CR_PLLON; } while (0)
  8. #define stm32_rccDisablePLL() do { STM32_RCC->CR &= ~RCC_CR_PLLON; } while (0)
  9. #define stm32_rccEnableLSI() do { STM32_RCC->CSR |= RCC_CSR_LSION; } while (0)
  10. #define stm32_rccDisableLSI() do { STM32_RCC->CSR &= ~RCC_CSR_LSION; } while (0)
  11. #define stm32_rccEnableLSE() do { STM32_RCC->BDCR |= RCC_BDCR_LSEON; } while (0)
  12. #define stm32_rccDisableLSE() do { STM32_RCC->BDCR |= ~RCC_BDCR_LSEON; } while (0)
  13.  
  14. #define stm32_rccHSIReady() (STM32_RCC->CR & RCC_CR_HSIRDY)
  15. #define stm32_rccHSEReady() (STM32_RCC->CR & RCC_CR_HSERDY)
  16. #define stm32_rccPLLReady() (STM32_RCC->CR & RCC_CR_PLLRDY)
  17. #define stm32_rccLSIReady() (STM32_RCC->CSR & RCC_CSR_LSIRDY)
  18. #define stm32_rccLSEReady() (STM32_RCC->BDCR & RCC_BDCR_LSERDY)
  19.  
  20. #define stm32_rccSelectHSI() do { STM32_RCC->CFGR = (STM32_RCC->CFGR & ~(RCC_CFGR_SW0 | RCC_CFGR_SW1)) | 0; } while (0)
  21. #define stm32_rccSelectHSE() do { STM32_RCC->CFGR = (STM32_RCC->CFGR & ~(RCC_CFGR_SW0 | RCC_CFGR_SW1)) | 1; } while (0)
  22. #define stm32_rccSelectPLL() do { STM32_RCC->CFGR = (STM32_RCC->CFGR & ~(RCC_CFGR_SW0 | RCC_CFGR_SW1)) | 2; } while (0)
  23.  
  24. #define stm32_rccHSISelected() ((STM32_RCC->CFGR & (RCC_CFGR_SW0 | RCC_CFGR_SW1)) == 0)
  25. #define stm32_rccHSESelected() ((STM32_RCC->CFGR & (RCC_CFGR_SW0 | RCC_CFGR_SW1)) == 1)
  26. #define stm32_rccPLLSelected() ((STM32_RCC->CFGR & (RCC_CFGR_SW0 | RCC_CFGR_SW1)) == 2)
  27.  
  28. void stm32_rccSetAHBPrescaler(STM32AHBPrescaler psc) {
  29.     STM32_RCC->CFGR = (STM32_RCC->CFGR & ~RCC_CFGR_HPRE_MASK) | (psc << RCC_CFGR_HPRE_OFFSET);
  30. }
  31.  
  32. void stm32_rccSetAPB1Prescaler(STM32APBPrescaler psc) {
  33.     STM32_RCC->CFGR = (STM32_RCC->CFGR & ~RCC_CFGR_PPRE1_MASK) | (psc << RCC_CFGR_PPRE1_OFFSET);
  34. }
  35.  
  36. void stm32_rccSetAPB2Prescaler(STM32APBPrescaler psc) {
  37.     STM32_RCC->CFGR = (STM32_RCC->CFGR & ~RCC_CFGR_PPRE2_MASK) | (psc << RCC_CFGR_PPRE2_OFFSET);
  38. }
  39.  
  40. void stm32_rccSetADCPrescaler(STM32ADCPrescaler psc) {
  41.     STM32_RCC->CFGR = (STM32_RCC->CFGR & ~RCC_CFGR_ADCPRE_MASK) | (psc << RCC_CFGR_ADCPRE_OFFSET);
  42. }
  43.  
  44. bool stm32_rccWaitHSIReady(uint32_t timeout) {
  45.     while ((stm32_rccHSIReady() == 0) && (timeout--));
  46.     return timeout > 0;
  47. }
  48.  
  49. bool stm32_rccWaitHSEReady(uint32_t timeout) {
  50.     while ((stm32_rccHSEReady() == 0) && (timeout--));
  51.     return timeout > 0;
  52. }
  53.  
  54. bool stm32_rccWaitPLLReady(uint32_t timeout) {
  55.     while ((stm32_rccPLLReady() == 0) && (timeout--));
  56.     return timeout > 0;
  57. }
  58.  
  59. bool stm32_rccWaitLSIReady(uint32_t timeout) {
  60.     while ((stm32_rccLSIReady() == 0) && (timeout--));
  61.     return timeout > 0;
  62. }
  63.  
  64. bool stm32_rccWaitLSEReady(uint32_t timeout) {
  65.     while ((stm32_rccLSEReady() == 0) && (timeout--));
  66.     return timeout > 0;
  67. }
  68.  
  69. bool stm32_rccWaitHSISelected(uint32_t timeout) {
  70.     while ((stm32_rccHSISelected() == 0) && (timeout--));
  71.     return timeout > 0;
  72. }
  73.  
  74. bool stm32_rccWaitHSESelected(uint32_t timeout) {
  75.     while ((stm32_rccHSESelected() == 0) && (timeout--));
  76.     return timeout > 0;
  77. }
  78.  
  79. bool stm32_rccWaitPLLSelected(uint32_t timeout) {
  80.     while ((stm32_rccPLLSelected() == 0) && (timeout--));
  81.     return timeout > 0;
  82. }
  83.  
  84. void stm32_rccSetBusPrescalers(uint32_t sysClk, STM32AHBPrescaler ahbPsc, STM32APBPrescaler apb1Psc, STM32APBPrescaler apb2Psc) {
  85.     stm32_rccSetAHBPrescaler(ahbPsc);
  86.     stm32_rccSetAPB1Prescaler(apb1Psc);
  87.     stm32_rccSetAPB2Prescaler(apb2Psc);
  88.     if (ahbPsc == 0) {
  89.         _stm32_rccAHBFrequency = sysClk;
  90.     } else if (ahbPsc <= 11) {
  91.         _stm32_rccAHBFrequency = sysClk / (1 << (ahbPsc - 7));
  92.     } else {
  93.         _stm32_rccAHBFrequency = sysClk / (1 << (ahbPsc - 6));
  94.     }
  95.     if (apb1Psc == 0) {
  96.         _stm32_rccAPB1Frequency = _stm32_rccAHBFrequency;
  97.     } else {
  98.         _stm32_rccAPB1Frequency = _stm32_rccAHBFrequency / (1 << (apb1Psc - 3));
  99.     }
  100.     if (apb2Psc == 0) {
  101.         _stm32_rccAPB2Frequency = _stm32_rccAHBFrequency;
  102.     } else {
  103.         _stm32_rccAPB2Frequency = _stm32_rccAHBFrequency / (1 << (apb2Psc - 3));
  104.     }
  105. }
  106.  
  107. void stm32_rccAutoSetPrescalers(uint32_t sysClk) {
  108.     if (sysClk <= 36000000) {
  109.         stm32_rccSetBusPrescalers(sysClk, RCC_AHB_NODIV, RCC_APB_NODIV, RCC_APB_NODIV);
  110.     } else {
  111.         stm32_rccSetBusPrescalers(sysClk, RCC_AHB_NODIV, RCC_APB_DIV2, RCC_APB_NODIV);
  112.     }
  113.     stm32_rccSetADCPrescaler((sysClk - 1) / 28000000);
  114.     FLASH_ACR = (FLASH_ACR & ~FLASH_ACR_LATENCY_MASK) | FLASH_ACR_PRFTBE | ((sysClk - 1) / 24000000);
  115. }
  116.  
  117. void stm32_rccUseHSI(void) {
  118.     stm32_rccEnableHSI();
  119.     stm32_rccWaitHSIReady(STM32_CLOCKSOURCE_DEFAULT_TIMEOUT);
  120.     stm32_rccSelectHSI();
  121.     stm32_rccWaitHSISelected(STM32_CLOCKSOURCE_DEFAULT_TIMEOUT);
  122.     stm32_rccAutoSetPrescalers(8000000);
  123. }
  124.  
  125. bool stm32_rccUseHSE(uint32_t clk) {
  126.     stm32_rccEnableHSE();
  127.     if (stm32_rccWaitHSEReady(STM32_CLOCKSOURCE_DEFAULT_TIMEOUT)) {
  128.         stm32_rccSelectHSE();
  129.         if (stm32_rccWaitHSESelected(STM32_CLOCKSOURCE_DEFAULT_TIMEOUT)) {
  130.             stm32_rccAutoSetPrescalers(clk);
  131.             return true;
  132.         }
  133.         stm32_rccUseHSI();
  134.     }
  135.     stm32_rccDisableHSE();
  136.     return false;
  137. }
  138.  
  139. bool stm32_rccSetupPLL(uint32_t inClk, uint32_t outClk, bool extClk) {
  140.     assert(outClk >= inClk);
  141.     uint32_t cfg = extClk ? RCC_CFGR_PLLSRC : 0;
  142.     uint32_t mul = outClk / inClk;
  143.     if (!extClk) {
  144.         mul *= 2;
  145.     }
  146.     if (mul && (mul <= 15)) {
  147.         if (mul < 8) {
  148.             cfg |= RCC_CFGR_PLLXTPRE;
  149.             mul *= 2;
  150.         }
  151.         cfg |= (mul - 2) << RCC_CFGR_PLLMUL_OFFSET;
  152.         if (outClk <= 48000000) {
  153.             cfg |= RCC_CFGR_OTGFSPRE;
  154.         }
  155.         STM32_RCC->CFGR = (STM32_RCC->CFGR & ~(RCC_CFGR_PLLMUL_MASK | RCC_CFGR_PLLXTPRE | RCC_CFGR_OTGFSPRE)) | cfg;
  156.         return true;
  157.     }
  158.     return false;
  159. }
  160.  
  161. bool stm32_rccUsePLL(uint32_t clk) {
  162.     stm32_rccEnablePLL();
  163.     if (stm32_rccWaitPLLReady(STM32_CLOCKSOURCE_DEFAULT_TIMEOUT)) {
  164.         stm32_rccAutoSetPrescalers(clk);
  165.         stm32_rccSelectPLL();
  166.         if (stm32_rccWaitPLLSelected(STM32_CLOCKSOURCE_DEFAULT_TIMEOUT)) {
  167.             return true;
  168.         }
  169.         stm32_rccUseHSI();
  170.     }
  171.     stm32_rccDisablePLL();
  172.     return false;
  173. }
  174.  
  175. bool stm32_rccUsePLLFromHSI(uint32_t clk) {
  176.     stm32_rccUseHSI();
  177.     if (stm32_rccSetupPLL(8000000, clk, false)) {
  178.         if (stm32_rccUsePLL(clk)) {
  179.             return true;
  180.         }
  181.     }
  182.     return false;
  183. }
  184.  
  185. bool stm32_rccUsePLLFromHSE(uint32_t clk, uint32_t hseClk) {
  186.     stm32_rccUseHSI();
  187.     stm32_rccEnableHSE();
  188.     if (stm32_rccWaitHSEReady(STM32_CLOCKSOURCE_DEFAULT_TIMEOUT)) {
  189.         if (stm32_rccSetupPLL(hseClk, clk, true)) {
  190.             if (stm32_rccUsePLL(clk)) {
  191.                 stm32_rccDisableHSI();
  192.                 return true;
  193.             }
  194.         }
  195.     }
  196.     stm32_rccDisableHSE();
  197.     return false;
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement