main.c 11 KB

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