main.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. boost converter project for the STM32L011x4
  3. STM32L011D3P D=14Pin 3=8KB P=TTSOP
  4. STM32L011D4P D=14Pin 4=16KB P=TTSOP
  5. STM32L011F3P F=20Pin 3=8KB P=TTSOP
  6. STM32L011F4P F=20Pin 4=16KB P=TTSOP
  7. TSSOP20:
  8. PB9/BOOT 1 20 PA14
  9. PC14 2 19 PA13
  10. PC15 3 18 PA10 TIM21_CH1, TIM2_CH3
  11. Reset 4 17 PA9 TIM21_CH2
  12. VDDA 5 16 VDD
  13. PA0 C1- 6 15 GND
  14. PA1 C1+ 7 14 PB1 ADC9
  15. PA2 C2- TX 8 13 PA7 C2+
  16. PA3 C2+ RX 9 12 PA6 ADC6
  17. PA4 C1- C2- 10 11 PA5 C1- C2-
  18. Comp 2:
  19. Input: REFINT /4
  20. Input: PA7 (C2+),
  21. Output: PA9 (TIM21_CH2)
  22. Comp 1:
  23. Input: REFINT
  24. Input: PA1 (C1+)
  25. Output: PA10 (TIM2_CH3)
  26. */
  27. #include "stm32l011xx.h"
  28. /*===============================================*/
  29. /*
  30. systick IRQ
  31. */
  32. volatile unsigned long SysTickCount = 0;
  33. void __attribute__ ((interrupt, used)) SysTick_Handler(void)
  34. {
  35. SysTickCount++;
  36. if ( SysTickCount & 1 )
  37. GPIOA->BSRR = GPIO_BSRR_BS_0; /* atomic set PA0 */
  38. else
  39. GPIOA->BSRR = GPIO_BSRR_BR_0; /* atomic clr PA0 */
  40. /*
  41. if ( COMP2->CSR & COMP_CSR_COMP2VALUE )
  42. GPIOA->BSRR = GPIO_BSRR_BS_0;
  43. else
  44. GPIOA->BSRR = GPIO_BSRR_BR_0;
  45. */
  46. }
  47. /*===============================================*/
  48. /*
  49. boost converter
  50. PA6=Output to N-MOS Gate
  51. PA7=Reference Input
  52. */
  53. void boost_converter(void)
  54. {
  55. /* assumes, that SYSCFG_CRGR3 is not locked */
  56. /* assumes, that COMP2 is not locked */
  57. /* setup internal reference (ca. 1.2V) */
  58. SYSCFG->CFGR3 |= SYSCFG_CFGR3_ENBUFLP_VREFINT_COMP; /* enable VREFINT during low power mode */
  59. while( (SYSCFG->CFGR3 & SYSCFG_CFGR3_VREFINT_RDYF) == 0 ) /* wait for VREFINT until it becomes ready */
  60. ;
  61. /* configure PA7 as COMP2 Plus input */
  62. /* GPIO has to be in input state without any pull up/down resistor: This is default, so nothing needs to be done here */
  63. /* configure PA9 as TIM21 output */
  64. GPIOA->AFR[1] &= ~GPIO_AFRH_AFRH1_Msk;
  65. GPIOA->AFR[1] |= 5<<GPIO_AFRH_AFRH1_Pos; /* AF5 selects TIM21 */
  66. GPIOA->MODER &= ~GPIO_MODER_MODE9; /* clear mode */
  67. GPIOA->MODER |= GPIO_MODER_MODE9_1; /* Alternate Function Mode */
  68. GPIOA->OTYPER &= ~GPIO_OTYPER_OT_9; /* no Push/Pull */
  69. GPIOA->OSPEEDR &= ~GPIO_OSPEEDER_OSPEED9; /* low speed */
  70. GPIOA->PUPDR &= ~GPIO_PUPDR_PUPD9; /* no pullup/pulldown */
  71. GPIOA->BSRR = GPIO_BSRR_BR_9; /* atomic clr */
  72. /* setup COMP2 */
  73. /*
  74. COMP2 has only one register: CSR
  75. Bit 31: Lock bit, will lock CSR until next reset if set to 1
  76. Bit 30: Output value, considering polarity
  77. Bit 15: Polarity, 0: Not inverted
  78. Bits 10:8: Plus input
  79. 000: PA3
  80. 001: PB4
  81. 010: PB5
  82. 011: PB6
  83. 100: PB7
  84. 101: PA7 (for category 1 devices only)
  85. Bits 6:4: Minus input
  86. 000: VREFINT
  87. 001: PA2
  88. 010: PA4
  89. 011: PA5
  90. 100: 1/4 VREFINT
  91. 101: 1/2 VREFINT
  92. 110: 3/4 VREFINT
  93. 111: PB
  94. Bit 3: Speed selection, 0: Low speed
  95. Bit 0: Enable
  96. */
  97. /* select PA7 as positive input --> 101, only available for the STM32L011 */
  98. COMP2->CSR &= ~(uint32_t)COMP_CSR_COMP2INPSEL;
  99. COMP2->CSR |= COMP_CSR_COMP2INPSEL_0 | COMP_CSR_COMP2INPSEL_2;
  100. /*
  101. 0.306V
  102. 20mA 15 Ohm 20.4mA
  103. 20mA 18 Ohm 17mA
  104. 350mA 1 Ohm 306mA --> 300mWatt
  105. */
  106. /* select 1/4 internal reference voltage --> 0.306 Volt -->100 */
  107. COMP2->CSR &= ~(uint32_t)COMP_CSR_COMP2INNSEL;
  108. COMP2->CSR |= COMP_CSR_COMP2INNSEL_2;
  109. /* invert polarity */
  110. //COMP2->CSR |= COMP_CSR_COMP2POLARITY;
  111. /* comparator enable */
  112. COMP2->CSR |= COMP_CSR_COMP2EN;
  113. /* setup PWM mode for TIM21 */
  114. /* configure output compare for channel 2 of TIM21 */
  115. TIM21->CCMR1 &= ~(uint32_t)TIM_CCMR1_CC2S;
  116. /* configure pwm mode 1 */
  117. TIM21->CCMR1 &= ~(uint32_t)TIM_CCMR1_OC2M; /* clear mode to 000 */
  118. TIM21->CCMR1 |= TIM_CCMR1_OC2M_2 | TIM_CCMR1_OC2M_1; /* pwm mode 1 (110) */
  119. TIM21->CCMR1 |= TIM_CCMR1_OC2PE; /* load modified CCR1 during update event only */
  120. TIM21->PSC = 0; /* run with max speed (2 MHz after reset) */
  121. TIM21->ARR = 20; /* period of 20 clocks (100KHz if sys clock is not modified) */
  122. TIM21->CCR2 = 5; /* a value between 0 and ARR, which defines the duty cycle */
  123. /* output the result of channel 2 to PA9 */
  124. TIM21->CCER |= TIM_CCER_CC2E;
  125. /* do not invert the output polarity (this is also the default value) */
  126. TIM21->CCER &= ~(uint32_t)TIM_CCER_CC2P;
  127. /* use up counter (this is also the default value) */
  128. TIM21->CR1 &= ~(uint32_t)TIM_CR1_DIR;
  129. /* do not use "one pulse mode" (continues mode, this is also the default value) */
  130. TIM21->CR1 &= ~(uint32_t)TIM_CR1_OPM;
  131. /* always generate an update event (this is also default) */
  132. TIM21->CR1 &= ~(uint32_t)TIM_CR1_UDIS;
  133. /* update event can be caused by UG bit and overflow (this is default) */
  134. TIM21->CR1 &= ~(uint32_t)TIM_CR1_URS;
  135. /* connect COMP2 with TIM21 */
  136. /* the following two bits are not documented in RM0377 */
  137. /* However, it is mentioned in "A.9.10 ETR configuration to clear OCxREF code example" */
  138. TIM21->CCMR1 |= TIM_CCMR1_OC2CE; /* enable clearing on OC1 for ETR clearing */
  139. TIM21->SMCR |= TIM_SMCR_OCCS; /* Select ETR as OCREF clear source (reserved bit = 1) */
  140. //TIM21->EGR |= TIM_EGR_UG;
  141. TIM21->OR &= ~TIM21_OR_ETR_RMP_Msk;
  142. TIM21->OR |= TIM21_OR_ETR_RMP_0; /* bit pattern 01: connect with COMP2 */
  143. /* enable the counter */
  144. TIM21->CR1 |= TIM_CR1_CEN;
  145. }
  146. int main()
  147. {
  148. RCC->IOPENR |= RCC_IOPENR_IOPAEN; /* Enable clock for GPIO Port A */
  149. RCC->APB2ENR |= RCC_APB2ENR_ADCEN;
  150. RCC->APB2ENR |= RCC_APB2ENR_TIM21EN;
  151. RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
  152. RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
  153. __NOP();
  154. __NOP();
  155. GPIOA->MODER &= ~GPIO_MODER_MODE0; /* clear mode for PA0 */
  156. GPIOA->MODER |= GPIO_MODER_MODE0_0; /* Output mode for PA0 */
  157. GPIOA->OTYPER &= ~GPIO_OTYPER_OT_0; /* no Push/Pull for PA0 */
  158. GPIOA->OSPEEDR &= ~GPIO_OSPEEDER_OSPEED0; /* low speed for PA0 */
  159. GPIOA->PUPDR &= ~GPIO_PUPDR_PUPD0; /* no pullup/pulldown for PA0 */
  160. GPIOA->BSRR = GPIO_BSRR_BR_0; /* atomic clr PA0 */
  161. boost_converter();
  162. SysTick->LOAD = 2000*500 - 1;
  163. SysTick->VAL = 0;
  164. SysTick->CTRL = 7; /* enable, generate interrupt (SysTick_Handler), do not divide by 2 */
  165. for(;;)
  166. ;
  167. }