main.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* LED blink project for the STM32L031 */
  2. #include "stm32l031xx.h"
  3. #include "display.h"
  4. #include "delay.h"
  5. /*=======================================================================*/
  6. /* global variables */
  7. volatile unsigned long SysTickCount = 0;
  8. /*=======================================================================*/
  9. void __attribute__ ((interrupt, used)) SysTick_Handler(void)
  10. {
  11. SysTickCount++;
  12. }
  13. void setHSIClock()
  14. {
  15. /* test if the current clock source is something else than HSI */
  16. if ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI)
  17. {
  18. /* enable HSI */
  19. RCC->CR |= RCC_CR_HSION;
  20. /* wait until HSI becomes ready */
  21. while ( (RCC->CR & RCC_CR_HSIRDY) == 0 )
  22. ;
  23. /* enable the HSI "divide by 4" bit */
  24. RCC->CR |= (uint32_t)(RCC_CR_HSIDIVEN);
  25. /* wait until the "divide by 4" flag is enabled */
  26. while((RCC->CR & RCC_CR_HSIDIVF) == 0)
  27. ;
  28. /* then use the HSI clock */
  29. RCC->CFGR = (RCC->CFGR & (uint32_t) (~RCC_CFGR_SW)) | RCC_CFGR_SW_HSI;
  30. /* wait until HSI clock is used */
  31. while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI)
  32. ;
  33. }
  34. /* disable PLL */
  35. RCC->CR &= (uint32_t)(~RCC_CR_PLLON);
  36. /* wait until PLL is inactive */
  37. while((RCC->CR & RCC_CR_PLLRDY) != 0)
  38. ;
  39. /* set latency to 1 wait state */
  40. FLASH->ACR |= FLASH_ACR_LATENCY;
  41. /* At this point the HSI runs with 4 MHz */
  42. /* Multiply by 16 device by 2 --> 32 MHz */
  43. RCC->CFGR = (RCC->CFGR & (~(RCC_CFGR_PLLMUL| RCC_CFGR_PLLDIV ))) | (RCC_CFGR_PLLMUL16 | RCC_CFGR_PLLDIV2);
  44. /* enable PLL */
  45. RCC->CR |= RCC_CR_PLLON;
  46. /* wait until the PLL is ready */
  47. while ((RCC->CR & RCC_CR_PLLRDY) == 0)
  48. ;
  49. /* use the PLL has clock source */
  50. RCC->CFGR |= (uint32_t) (RCC_CFGR_SW_PLL);
  51. /* wait until the PLL source is active */
  52. while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL)
  53. ;
  54. }
  55. int main()
  56. {
  57. setHSIClock();
  58. SystemCoreClockUpdate(); /* Update SystemCoreClock() */
  59. //SystemCoreClock = 32000000UL;
  60. SysTick->LOAD = (SystemCoreClock/1000)*50 - 1; /* 50ms task */
  61. SysTick->VAL = 0;
  62. SysTick->CTRL = 7; /* enable, generate interrupt (SysTick_Handler), do not divide by 2 */
  63. RCC->IOPENR |= RCC_IOPENR_IOPAEN; /* Enable clock for GPIO Port A */
  64. __NOP();
  65. __NOP();
  66. GPIOA->MODER &= ~GPIO_MODER_MODE13; /* clear mode for PA13 */
  67. GPIOA->MODER |= GPIO_MODER_MODE13_0; /* Output mode for PA13 */
  68. GPIOA->OTYPER &= ~GPIO_OTYPER_OT_13; /* Push/Pull for PA13 */
  69. GPIOA->OSPEEDR &= ~GPIO_OSPEEDER_OSPEED13; /* low speed for PA13 */
  70. GPIOA->PUPDR &= ~GPIO_PUPDR_PUPD13; /* no pullup/pulldown for PA13 */
  71. GPIOA->BSRR = GPIO_BSRR_BR_13; /* atomic clr PA13 */
  72. GPIOA->BSRR = GPIO_BSRR_BS_13; /* atomic set PA13 */
  73. display_Init();
  74. display_Write("STM32L031\n");
  75. display_WriteUnsigned(SystemCoreClock);
  76. display_Write(" Hz\n");
  77. /* real time clock enable */
  78. RCC->APB1ENR |= RCC_APB1ENR_PWREN; /* enable power interface */
  79. PWR->CR |= PWR_CR_DBP; /* activate write access to RCC->CSR */
  80. /* externel 32K clock source */
  81. RCC->CSR |= RCC_CSR_LSEBYP; /* bypass oscillator */
  82. /* externel 32K oscillator */
  83. //RCC->CSR &= ~RCC_CSR_LSEBYP; /* no bypass oscillator */
  84. //RCC->CSR &= ~RCC_CSR_LSEDRV_Msk /* lowest drive */
  85. //RCC->CSR |= RCC_CSR_LSEDRV_0; /* medium low drive */
  86. RCC->CSR |= RCC_CSR_LSEON; /* enable low speed external clock */
  87. delay_micro_seconds(100000*5); /* LSE requires between 100ms to 200ms */
  88. if ( RCC->CSR & RCC_CSR_LSERDY )
  89. display_Write("32K Clock Ready\n");
  90. else
  91. display_Write("32K Clock Error\n");
  92. RCC->CSR &= ~RCC_CSR_RTCSEL_Msk; /* no clock selection for RTC */
  93. RCC->CSR |= RCC_CSR_RTCSEL_LSE; /* select LSE */
  94. RCC->CSR |= RCC_CSR_RTCEN; /* enable RTC */
  95. RTC->WPR = 0x0ca; /* disable RTC write protection */
  96. RTC->WPR = 0x053;
  97. RTC->ISR = RTC_ISR_INIT; /* request RTC stop */
  98. while((RTC->ISR & RTC_ISR_INITF)!=RTC_ISR_INITF) /* wait for stop */
  99. ;
  100. RTC->PRER = 0x07f00ff;
  101. RTC->TR = 0;
  102. RTC->ISR =~ RTC_ISR_INIT; /* start RTC */
  103. RTC->WPR = 0; /* enable RTC write protection */
  104. RTC->WPR = 0;
  105. //PWR->CR &= ~PWR_CR_DBP; /* disable write access to RCC->CSR */
  106. for(;;)
  107. {
  108. delay_micro_seconds(500000);
  109. GPIOA->BSRR = GPIO_BSRR_BS_13; /* atomic set PA13 */
  110. delay_micro_seconds(500000);
  111. GPIOA->BSRR = GPIO_BSRR_BR_13; /* atomic clr PA13 */
  112. display_WriteUnsigned(RTC->TR);
  113. display_Write("\n");
  114. }
  115. }