main.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. Example for the STM32L031 Eval Board with 128x64 OLED at PA13/PA14
  3. LED: PA1 / AF2: TIM2_CH2
  4. VarRes: PA5 / ADC CH5
  5. ch0 PA0 pin 6
  6. ch1 PA1 pin 7
  7. ch2 PA2 pin 8
  8. ch3 PA3 pin 9
  9. ch4 PA4 pin 10
  10. ch5 PA5 pin 11
  11. ch6 PA6 pin 12
  12. ch7 PA7 pin 13
  13. ch8 PB0 -
  14. ch9 PB1 pin 14
  15. ch 0..15: GPIO
  16. ch 16: ???
  17. ch 17: vref (bandgap)
  18. ch18: temperature sensor
  19. */
  20. #include <stdio.h>
  21. #include "stm32l031xx.h"
  22. #include "delay.h"
  23. #include "u8x8.h"
  24. /*=======================================================================*/
  25. /* external functions */
  26. uint8_t u8x8_gpio_and_delay_stm32l0(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  27. /*=======================================================================*/
  28. /* global variables */
  29. u8x8_t u8x8; // u8x8 object
  30. uint8_t u8x8_x, u8x8_y; // current position on the screen
  31. volatile unsigned long SysTickCount = 0;
  32. /*=======================================================================*/
  33. void __attribute__ ((interrupt, used)) SysTick_Handler(void)
  34. {
  35. SysTickCount++;
  36. }
  37. void setHSIClock()
  38. {
  39. /* test if the current clock source is something else than HSI */
  40. if ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI)
  41. {
  42. /* enable HSI */
  43. RCC->CR |= RCC_CR_HSION;
  44. /* wait until HSI becomes ready */
  45. while ( (RCC->CR & RCC_CR_HSIRDY) == 0 )
  46. ;
  47. /* enable the HSI "divide by 4" bit */
  48. RCC->CR |= (uint32_t)(RCC_CR_HSIDIVEN);
  49. /* wait until the "divide by 4" flag is enabled */
  50. while((RCC->CR & RCC_CR_HSIDIVF) == 0)
  51. ;
  52. /* then use the HSI clock */
  53. RCC->CFGR = (RCC->CFGR & (uint32_t) (~RCC_CFGR_SW)) | RCC_CFGR_SW_HSI;
  54. /* wait until HSI clock is used */
  55. while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI)
  56. ;
  57. }
  58. /* disable PLL */
  59. RCC->CR &= (uint32_t)(~RCC_CR_PLLON);
  60. /* wait until PLL is inactive */
  61. while((RCC->CR & RCC_CR_PLLRDY) != 0)
  62. ;
  63. /* set latency to 1 wait state */
  64. FLASH->ACR |= FLASH_ACR_LATENCY;
  65. /* At this point the HSI runs with 4 MHz */
  66. /* Multiply by 16 device by 2 --> 32 MHz */
  67. RCC->CFGR = (RCC->CFGR & (~(RCC_CFGR_PLLMUL| RCC_CFGR_PLLDIV ))) | (RCC_CFGR_PLLMUL16 | RCC_CFGR_PLLDIV2);
  68. /* enable PLL */
  69. RCC->CR |= RCC_CR_PLLON;
  70. /* wait until the PLL is ready */
  71. while ((RCC->CR & RCC_CR_PLLRDY) == 0)
  72. ;
  73. /* use the PLL has clock source */
  74. RCC->CFGR |= (uint32_t) (RCC_CFGR_SW_PLL);
  75. /* wait until the PLL source is active */
  76. while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL)
  77. ;
  78. SystemCoreClockUpdate(); /* Update SystemCoreClock global variable */
  79. }
  80. /*
  81. Enable several power regions: PWR, GPIOA
  82. This must be executed after each reset.
  83. */
  84. void startUp(void)
  85. {
  86. RCC->IOPENR |= RCC_IOPENR_IOPAEN; /* Enable clock for GPIO Port A */
  87. RCC->APB1ENR |= RCC_APB1ENR_PWREN; /* enable power interface (PWR) */
  88. PWR->CR |= PWR_CR_DBP; /* activate write access to RCC->CSR and RTC */
  89. SysTick->LOAD = (SystemCoreClock/1000)*50 - 1; /* 50ms task */
  90. SysTick->VAL = 0;
  91. SysTick->CTRL = 7; /* enable, generate interrupt (SysTick_Handler), do not divide by 2 */
  92. }
  93. /*=======================================================================*/
  94. /* u8x8 display procedures */
  95. void initDisplay(void)
  96. {
  97. u8x8_Setup(&u8x8, u8x8_d_ssd1306_128x64_noname, u8x8_cad_ssd13xx_i2c, u8x8_byte_sw_i2c, u8x8_gpio_and_delay_stm32l0);
  98. u8x8_InitDisplay(&u8x8);
  99. u8x8_ClearDisplay(&u8x8);
  100. u8x8_SetPowerSave(&u8x8, 0);
  101. u8x8_SetFont(&u8x8, u8x8_font_amstrad_cpc_extended_r);
  102. u8x8_x = 0;
  103. u8x8_y = 0;
  104. }
  105. void outChar(uint8_t c)
  106. {
  107. if ( u8x8_x >= u8x8_GetCols(&u8x8) )
  108. {
  109. u8x8_x = 0;
  110. u8x8_y++;
  111. }
  112. u8x8_DrawGlyph(&u8x8, u8x8_x, u8x8_y, c);
  113. u8x8_x++;
  114. }
  115. void outStr(const char *s)
  116. {
  117. while( *s )
  118. outChar(*s++);
  119. }
  120. void outHexHalfByte(uint8_t b)
  121. {
  122. b &= 0x0f;
  123. if ( b < 10 )
  124. outChar(b+'0');
  125. else
  126. outChar(b+'a'-10);
  127. }
  128. void outHex8(uint8_t b)
  129. {
  130. outHexHalfByte(b >> 4);
  131. outHexHalfByte(b);
  132. }
  133. void outHex16(uint16_t v)
  134. {
  135. outHex8(v>>8);
  136. outHex8(v);
  137. }
  138. void outHex32(uint32_t v)
  139. {
  140. outHex16(v>>16);
  141. outHex16(v);
  142. }
  143. void setRow(uint8_t r)
  144. {
  145. u8x8_x = 0;
  146. u8x8_y = r;
  147. }
  148. /*=======================================================================*/
  149. /*
  150. ADC defaults:
  151. - Clock source: ADCCLK (HSI16) (ADC_CFGR2)
  152. - ADC clock prescaler: divide by 1 (ADC_CCR)
  153. - software enabled start
  154. - right alignment
  155. - 12 Bit resolution
  156. - No interrupts enabled
  157. Calibration:
  158. better ignore ADC_ISR_EOCAL and use the ADC_CR_ADCAL flag only.
  159. otherwise some extra NOPs are required after calibration
  160. */
  161. void initADC(uint8_t ch)
  162. {
  163. /* ADC Clock Enable */
  164. RCC->APB2ENR |= RCC_APB2ENR_ADCEN; /* enable ADC clock */
  165. __NOP(); /* let us wait for some time */
  166. __NOP(); /* let us wait for some time */
  167. /* ADC Reset */
  168. RCC->APB2RSTR |= RCC_APB2RSTR_ADCRST;
  169. __NOP(); /* let us wait for some time */
  170. __NOP(); /* let us wait for some time */
  171. RCC->APB2RSTR &= ~RCC_APB2RSTR_ADCRST;
  172. __NOP(); /* let us wait for some time */
  173. __NOP(); /* let us wait for some time */
  174. /* CALIBRATION */
  175. ADC1->CR |= ADC_CR_ADCAL; /* start calibration */
  176. while ((ADC1->CR & ADC_CR_ADCAL) != 0) /* wait for clibration finished */
  177. {
  178. }
  179. /* ENABLE ADC */
  180. ADC1->ISR |= ADC_ISR_ADRDY; /* clear ready flag */
  181. ADC1->CR |= ADC_CR_ADEN; /* enable ADC */
  182. while ((ADC1->ISR & ADC_ISR_ADRDY) == 0) /* wait for ADC */
  183. {
  184. }
  185. /* CONFIGURE ADC */
  186. //ADC1->CFGR1 &= ~ADC_CFGR1_EXTEN; /* software enabled conversion start */
  187. //ADC1->CFGR1 &= ~ADC_CFGR1_ALIGN; /* right alignment */
  188. //ADC1->CFGR1 &= ~ADC_CFGR1_RES; /* 12 bit resolution */
  189. ADC1->CFGR1 |= ADC_CFGR1_CONT; /* continues mode */
  190. ADC1->CFGR2 |= ADC_CFGR2_OVSR_0; /* 011 oversampling ration x16 */
  191. ADC1->CFGR2 |= ADC_CFGR2_OVSR_1;
  192. ADC1->CFGR2 |= ADC_CFGR2_OVSS_2; /* shift 4 bits (because of x16 oversampling) */
  193. //ADC1->CFGR2 |= ADC_CFGR2_OVSR_1; /* 110 oversampling ration x128 */
  194. //ADC1->CFGR2 |= ADC_CFGR2_OVSR_2;
  195. //ADC1->CFGR2 |= ADC_CFGR2_OVSS_0; /* shift 7 bits (because of x128 oversampling) */
  196. //ADC1->CFGR2 |= ADC_CFGR2_OVSS_1; /* shift 7 bits (because of x128 oversampling) */
  197. //ADC1->CFGR2 |= ADC_CFGR2_OVSS_2; /* shift 7 bits (because of x128 oversampling) */
  198. ADC1->CFGR2 |= ADC_CFGR2_OVSE; /* enable oversampling */
  199. ADC1->CHSELR = 1<<ch; /* Select channel */
  200. //ADC1->SMPR |= ADC_SMPR_SMP_0 | ADC_SMPR_SMP_1 | ADC_SMPR_SMP_2; /* Select a sampling mode of 111 (very slow)*/
  201. /* START CONVERSION */
  202. ADC1->CR |= ADC_CR_ADSTART; /* start the ADC conversion */
  203. while ((ADC1->ISR & ADC_ISR_EOC) == 0) /* wait end of first conversion */
  204. {
  205. }
  206. //data is available in ADC1->DR;
  207. }
  208. /*=======================================================================*/
  209. void initTIM(void)
  210. {
  211. /* enable clock for TIM2 */
  212. RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
  213. /*enable clock for GPIOA */
  214. RCC->IOPENR |= RCC_IOPENR_IOPAEN; /* Enable clock for GPIO Port A */
  215. __NOP(); /* extra delay for clock stabilization required? */
  216. __NOP();
  217. /* prescalar for AHB and APB1 */
  218. /* reselt defaults for HPRE and PPRE1: no clock division */
  219. // RCC->CFGR &= ~RCC_CFGR_HPRE;
  220. // RCC->CFGR |= RCC_CFGR_HPRE_DIV1;
  221. // RCC->CFGR &= ~RCC_CFGR_PPRE1;
  222. // RCC->CFGR |= RCC_CFGR_PPRE1_DIV1;
  223. /* configure GPIOA PA1 for TIM2 */
  224. GPIOA->MODER &= ~GPIO_MODER_MODE1; /* clear mode for PA9 */
  225. GPIOA->MODER |= GPIO_MODER_MODE1_1; /* alt fn */
  226. GPIOA->OTYPER &= ~GPIO_OTYPER_OT_1; /* push-pull */
  227. GPIOA->AFR[0] &= ~(15<<4); /* Clear Alternate Function PA1 */
  228. GPIOA->AFR[0] |= 2<<4; /* AF2 Alternate Function PA1 */
  229. /* TIM2 configure */
  230. /* disable all interrupts */
  231. //TIM2->DIER = 0; /* 0 is reset default value */
  232. /* clear everything, including the "Update disable" flag, so that updates */
  233. /* are generated */
  234. // TIM2->CR1 = 0; /* 0 is reset default value */
  235. //TIM2->CR1 |= TIM_CR1_ARPE; // ARR is not modified so constant update is ok
  236. /* Update request by manual UG bit setting or slave controller */
  237. /* both is not required here */
  238. /* so, update request by couter over/underflow remains */
  239. //TIM2->CR1 |= TIM_CR1_URS; /* only udf/ovf generae events */
  240. TIM2->ARR = 4096; /* total cycle count */
  241. TIM2->CCR2 = 1024; /* duty cycle */
  242. //TIM2->CCMR1 &= ~TIM_CCMR1_OC2CE; /* disable clear output compare 2 **/
  243. TIM2->CCMR1 |= TIM_CCMR1_OC2M; /* all 3 bits set: PWM Mode 2 */
  244. //TIM2->CCMR1 &= ~TIM_CCMR1_OC1M_0; /* 110: PWM Mode 1 */
  245. TIM2->CCMR1 |= TIM_CCMR1_OC2PE; /* preload enable CCR2 is preloaded*/
  246. // TIM2->CCMR1 &= ~TIM_CCMR1_OC2FE; /* fast disable (reset default) */
  247. // TIM2->CCMR1 &= ~TIM_CCMR1_CC2S; /* configure cc2 as output (this is reset default) */
  248. //TIM2->EGR |= TIM_EGR_CC2G; /* capture event cc2 */
  249. TIM2->CCER |= TIM_CCER_CC2E; /* set output enable */
  250. //TIM2->CCER |= TIM_CCER_CC2P; /* polarity 0: normal (reset default) / 1: inverted*/
  251. TIM2->CR1 |= TIM_CR1_CEN; /* counter enable */
  252. }
  253. /*=======================================================================*/
  254. void main()
  255. {
  256. uint16_t adc_value;
  257. setHSIClock(); /* enable 32 MHz Clock */
  258. startUp(); /* enable systick irq and several power regions */
  259. initDisplay(); /* aktivate display */
  260. initADC(5); /* channel 5 */
  261. RCC->IOPENR |= RCC_IOPENR_IOPAEN; /* Enable clock for GPIO Port A */
  262. __NOP();
  263. __NOP();
  264. GPIOA->MODER &= ~GPIO_MODER_MODE1; /* clear mode for PA1 */
  265. GPIOA->MODER |= GPIO_MODER_MODE1_0; /* Output mode for PA1 */
  266. GPIOA->OTYPER &= ~GPIO_OTYPER_OT_1; /* no Push/Pull for PA1 */
  267. GPIOA->OSPEEDR &= ~GPIO_OSPEEDER_OSPEED1; /* low speed for PA1 */
  268. GPIOA->PUPDR &= ~GPIO_PUPDR_PUPD1; /* no pullup/pulldown for PA1 */
  269. GPIOA->BSRR = GPIO_BSRR_BS_1; /* atomic set PA1 */
  270. initTIM();
  271. setRow(0); outStr("ADC Test");
  272. setRow(2); outStr("ch5 pin11: ");
  273. for(;;)
  274. {
  275. adc_value = ADC1->DR;
  276. TIM2->CCR2 = adc_value;
  277. setRow(3); outHex16(adc_value);
  278. }
  279. }