main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. /* return current system time in milliseconds */
  38. unsigned long getUpTime(void)
  39. {
  40. unsigned long sys_tick_cycle = SysTick->LOAD+1;
  41. unsigned long millis_per_sys_tick_irq;
  42. /*
  43. the simple approach
  44. millis_per_sys_tick_irq = (sys_tick_cycle*1000UL)/SystemCoreClock;
  45. may overflow for large values of SysTick->LOAD. Instead this is better because SystemCoreClock is always
  46. very large:
  47. millis_per_sys_tick_irq = sys_tick_cycle/(SystemCoreClock/1000);
  48. */
  49. millis_per_sys_tick_irq = sys_tick_cycle/(SystemCoreClock/1000);
  50. return millis_per_sys_tick_irq * SysTickCount;
  51. }
  52. void setHSIClock()
  53. {
  54. /* test if the current clock source is something else than HSI */
  55. if ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI)
  56. {
  57. /* enable HSI */
  58. RCC->CR |= RCC_CR_HSION;
  59. /* wait until HSI becomes ready */
  60. while ( (RCC->CR & RCC_CR_HSIRDY) == 0 )
  61. ;
  62. /* enable the HSI "divide by 4" bit */
  63. RCC->CR |= (uint32_t)(RCC_CR_HSIDIVEN);
  64. /* wait until the "divide by 4" flag is enabled */
  65. while((RCC->CR & RCC_CR_HSIDIVF) == 0)
  66. ;
  67. /* then use the HSI clock */
  68. RCC->CFGR = (RCC->CFGR & (uint32_t) (~RCC_CFGR_SW)) | RCC_CFGR_SW_HSI;
  69. /* wait until HSI clock is used */
  70. while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI)
  71. ;
  72. }
  73. /* disable PLL */
  74. RCC->CR &= (uint32_t)(~RCC_CR_PLLON);
  75. /* wait until PLL is inactive */
  76. while((RCC->CR & RCC_CR_PLLRDY) != 0)
  77. ;
  78. /* set latency to 1 wait state */
  79. FLASH->ACR |= FLASH_ACR_LATENCY;
  80. /* At this point the HSI runs with 4 MHz */
  81. /* Multiply by 16 device by 2 --> 32 MHz */
  82. RCC->CFGR = (RCC->CFGR & (~(RCC_CFGR_PLLMUL| RCC_CFGR_PLLDIV ))) | (RCC_CFGR_PLLMUL16 | RCC_CFGR_PLLDIV2);
  83. /* enable PLL */
  84. RCC->CR |= RCC_CR_PLLON;
  85. /* wait until the PLL is ready */
  86. while ((RCC->CR & RCC_CR_PLLRDY) == 0)
  87. ;
  88. /* use the PLL has clock source */
  89. RCC->CFGR |= (uint32_t) (RCC_CFGR_SW_PLL);
  90. /* wait until the PLL source is active */
  91. while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL)
  92. ;
  93. SystemCoreClockUpdate(); /* Update SystemCoreClock global variable */
  94. }
  95. /*
  96. Enable several power regions: PWR, GPIOA
  97. This must be executed after each reset.
  98. */
  99. void startUp(void)
  100. {
  101. RCC->IOPENR |= RCC_IOPENR_IOPAEN; /* Enable clock for GPIO Port A */
  102. RCC->APB1ENR |= RCC_APB1ENR_PWREN; /* enable power interface (PWR) */
  103. PWR->CR |= PWR_CR_DBP; /* activate write access to RCC->CSR and RTC */
  104. SysTick->LOAD = (SystemCoreClock/1000)*50 - 1; /* 50ms task */
  105. SysTick->VAL = 0;
  106. SysTick->CTRL = 7; /* enable, generate interrupt (SysTick_Handler), do not divide by 2 */
  107. }
  108. /*=======================================================================*/
  109. /* u8x8 display procedures */
  110. void initDisplay(void)
  111. {
  112. u8x8_Setup(&u8x8, u8x8_d_ssd1306_128x64_noname, u8x8_cad_ssd13xx_i2c, u8x8_byte_sw_i2c, u8x8_gpio_and_delay_stm32l0);
  113. u8x8_InitDisplay(&u8x8);
  114. u8x8_ClearDisplay(&u8x8);
  115. u8x8_SetPowerSave(&u8x8, 0);
  116. u8x8_SetFont(&u8x8, u8x8_font_amstrad_cpc_extended_r);
  117. u8x8_x = 0;
  118. u8x8_y = 0;
  119. }
  120. void outChar(uint8_t c)
  121. {
  122. if ( u8x8_x >= u8x8_GetCols(&u8x8) )
  123. {
  124. u8x8_x = 0;
  125. u8x8_y++;
  126. }
  127. u8x8_DrawGlyph(&u8x8, u8x8_x, u8x8_y, c);
  128. u8x8_x++;
  129. }
  130. void outStr(const char *s)
  131. {
  132. while( *s )
  133. outChar(*s++);
  134. }
  135. void outHexHalfByte(uint8_t b)
  136. {
  137. b &= 0x0f;
  138. if ( b < 10 )
  139. outChar(b+'0');
  140. else
  141. outChar(b+'a'-10);
  142. }
  143. void outHex8(uint8_t b)
  144. {
  145. outHexHalfByte(b >> 4);
  146. outHexHalfByte(b);
  147. }
  148. void outHex16(uint16_t v)
  149. {
  150. outHex8(v>>8);
  151. outHex8(v);
  152. }
  153. void outDec16(uint16_t v)
  154. {
  155. outStr(u8x8_u16toa(v, 5));
  156. }
  157. void outHex32(uint32_t v)
  158. {
  159. outHex16(v>>16);
  160. outHex16(v);
  161. }
  162. void setRow(uint8_t r)
  163. {
  164. u8x8_x = 0;
  165. u8x8_y = r;
  166. }
  167. /*=======================================================================*/
  168. /*
  169. ADC defaults:
  170. - Clock source: ADCCLK (HSI16) (ADC_CFGR2)
  171. - ADC clock prescaler: divide by 1 (ADC_CCR)
  172. - software enabled start
  173. - right alignment
  174. - 12 Bit resolution
  175. - No interrupts enabled
  176. - 1.5 clock cycles sampling time (fastest)
  177. Calibration:
  178. better ignore ADC_ISR_EOCAL and use the ADC_CR_ADCAL flag only.
  179. otherwise some extra NOPs are required after calibration
  180. Time
  181. (1.5 + 12.5) / 4 MHz = 3.5us --> 286KHz
  182. x16 oversampling: 56us --> 17.9KHz
  183. */
  184. void initADC(uint8_t ch)
  185. {
  186. /* ADC Clock Enable */
  187. RCC->APB2ENR |= RCC_APB2ENR_ADCEN; /* enable ADC clock */
  188. __NOP(); __NOP(); /* extra delay for clock stabilization required? */
  189. /* ADC Reset */
  190. RCC->APB2RSTR |= RCC_APB2RSTR_ADCRST;
  191. __NOP(); __NOP(); /* let us wait for some time */
  192. RCC->APB2RSTR &= ~RCC_APB2RSTR_ADCRST;
  193. __NOP(); __NOP(); /* let us wait for some time */
  194. /* CALIBRATION */
  195. ADC1->CR |= ADC_CR_ADCAL; /* start calibration */
  196. while ((ADC1->CR & ADC_CR_ADCAL) != 0) /* wait for clibration finished */
  197. {
  198. }
  199. /* ENABLE ADC */
  200. ADC1->ISR |= ADC_ISR_ADRDY; /* clear ready flag */
  201. ADC1->CR |= ADC_CR_ADEN; /* enable ADC */
  202. while ((ADC1->ISR & ADC_ISR_ADRDY) == 0) /* wait for ADC */
  203. {
  204. }
  205. /* CONFIGURE ADC */
  206. ADC1->CFGR1 |= ADC_CFGR1_CONT; /* continues mode */
  207. ADC1->CFGR2 |= ADC_CFGR2_OVSR_0; /* 011 oversampling ration x16 */
  208. ADC1->CFGR2 |= ADC_CFGR2_OVSR_1;
  209. ADC1->CFGR2 |= ADC_CFGR2_OVSS_2; /* shift 4 bits (because of x16 oversampling) */
  210. ADC1->CFGR2 |= ADC_CFGR2_OVSE; /* enable oversampling */
  211. ADC1->CHSELR = 1<<ch; /* Select channel */
  212. //ADC1->SMPR |= ADC_SMPR_SMP_0 | ADC_SMPR_SMP_1 | ADC_SMPR_SMP_2; /* Select a sampling mode of 111 (very slow)*/
  213. /* START CONVERSION */
  214. ADC1->CR |= ADC_CR_ADSTART; /* start the ADC conversion */
  215. while ((ADC1->ISR & ADC_ISR_EOC) == 0) /* wait end of first conversion */
  216. {
  217. }
  218. //data is available in ADC1->DR;
  219. }
  220. /*=======================================================================*/
  221. void initTIM(void)
  222. {
  223. /* enable clock for TIM2 */
  224. RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
  225. //RCC->CFGR |= RCC_CFGR_PPRE1_2;
  226. //RCC->CFGR |= RCC_CFGR_PPRE1_1;
  227. //RCC->CFGR |= RCC_CFGR_PPRE1_0;
  228. /*cenable clock for GPIOA */
  229. RCC->IOPENR |= RCC_IOPENR_IOPAEN; /* Enable clock for GPIO Port A */
  230. __NOP(); __NOP(); /* extra delay for clock stabilization required? */
  231. /* configure GPIOA PA1 for TIM2 */
  232. GPIOA->MODER &= ~GPIO_MODER_MODE1; /* clear mode for PA1 */
  233. GPIOA->MODER |= GPIO_MODER_MODE1_1; /* alt fn */
  234. GPIOA->OTYPER &= ~GPIO_OTYPER_OT_1; /* push-pull */
  235. GPIOA->AFR[0] &= ~(15<<4); /* Clear Alternate Function PA1 */
  236. GPIOA->AFR[0] |= 2<<4; /* AF2 Alternate Function PA1 */
  237. /* TIM2 configure */
  238. /* disable all interrupts */
  239. //TIM2->DIER = 0; /* 0 is reset default value */
  240. /* clear everything, including the "Update disable" flag, so that updates */
  241. /* are generated */
  242. // TIM2->CR1 = 0; /* 0 is reset default value */
  243. //TIM2->CR1 |= TIM_CR1_ARPE; // ARR is not modified so constant update is ok
  244. /* Update request by manual UG bit setting or slave controller */
  245. /* both is not required here */
  246. /* so, update request by couter over/underflow remains */
  247. //TIM2->CR1 |= TIM_CR1_URS; /* only udf/ovf generae events */
  248. TIM2->ARR = 4096; /* total cycle count */
  249. TIM2->CCR2 = 1024; /* duty cycle */
  250. //TIM2->CCMR1 &= ~TIM_CCMR1_OC2CE; /* disable clear output compare 2 **/
  251. TIM2->CCMR1 |= TIM_CCMR1_OC2M; /* all 3 bits set: PWM Mode 2 */
  252. //TIM2->CCMR1 &= ~TIM_CCMR1_OC1M_0; /* 110: PWM Mode 1 */
  253. TIM2->CCMR1 |= TIM_CCMR1_OC2PE; /* preload enable CCR2 is preloaded*/
  254. // TIM2->CCMR1 &= ~TIM_CCMR1_OC2FE; /* fast disable (reset default) */
  255. // TIM2->CCMR1 &= ~TIM_CCMR1_CC2S; /* configure cc2 as output (this is reset default) */
  256. //TIM2->EGR |= TIM_EGR_CC2G; /* capture event cc2 */
  257. TIM2->CCER |= TIM_CCER_CC2E; /* set output enable */
  258. //TIM2->CCER |= TIM_CCER_CC2P; /* polarity 0: normal (reset default) / 1: inverted*/
  259. TIM2->CR1 |= TIM_CR1_CEN; /* counter enable */
  260. }
  261. /*
  262. copy from ADC1->DR to TIM2->CCR2
  263. ADC DMA requests can be used with DMA Channel 1
  264. */
  265. void initDMA()
  266. {
  267. RCC->AHBENR |= RCC_AHBENR_DMAEN; /* enable DMA clock */
  268. __NOP(); __NOP(); /* extra delay for clock stabilization required? */
  269. /* defaults:
  270. - 8 Bit access
  271. - read from peripheral
  272. - none-circular mode
  273. - no increment mode
  274. */
  275. DMA1_Channel1->CCR |= DMA_CCR_MSIZE_0; /* 16 bit access */
  276. DMA1_Channel1->CCR |= DMA_CCR_PSIZE_0; /* 16 bit access */
  277. DMA1_Channel1->CCR |= DMA_CCR_CIRC; /* circular mode */
  278. DMA1_Channel1->CNDTR = 1; /* one data, then repeat (circular mode) */
  279. DMA1_Channel1->CPAR = (uint32_t)&(ADC1->DR); /* source value */
  280. DMA1_Channel1->CMAR = (uint32_t)&(TIM2->CCR2); /* destination register */
  281. DMA1_CSELR->CSELR &= ~DMA_CSELR_C1S; /* 0000: select ADC for DMA CH 1 (this is reset default) */
  282. DMA1_Channel1->CCR |= DMA_CCR_EN; /* enable */
  283. ADC1->CFGR1 |= ADC_CFGR1_DMACFG; /* never stop DMA requests */
  284. ADC1->CFGR1 |= ADC_CFGR1_DMAEN; /* enable DMA requests for ADC */
  285. }
  286. /*=======================================================================*/
  287. void main()
  288. {
  289. uint32_t start, diff;
  290. setHSIClock(); /* enable 32 MHz Clock */
  291. startUp(); /* enable systick irq and several power regions */
  292. initDisplay(); /* aktivate display */
  293. /* setup ADC controlled PWM */
  294. initADC(5); /* read from channel 5 (pin 11) */
  295. initTIM();
  296. initDMA();
  297. /* rest of the code just shows the current ADC value on the OLED */
  298. setRow(0); outStr("ADC DMA TIM Test");
  299. setRow(2); outStr("ch5 pin11: ");
  300. setRow(5); outStr("cycle: ");
  301. for(;;)
  302. {
  303. setRow(3); outHex16(ADC1->DR);
  304. TIM2->SR &= ~TIM_SR_CC2IF; /* clear irq flag */
  305. while ( (TIM2->SR & TIM_SR_CC2IF) == 0 )
  306. ;
  307. start = SysTick->VAL;
  308. TIM2->SR &= ~TIM_SR_CC2IF; /* clear irq flag */
  309. while ( (TIM2->SR & TIM_SR_CC2IF) == 0 )
  310. ;
  311. diff = start-SysTick->VAL;
  312. setRow(6); outHex32(diff);
  313. }
  314. }