main.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*
  2. tim_scope for DC motor
  3. Example for the STM32L031 Eval Board with 128x64 OLED at PA13/PA14
  4. Single Mosfet Shield
  5. MOSFET: PA1 / AF2: TIM2_CH2
  6. VarRes: PA5 / ADC CH5
  7. Voltage sense: PA6 / ADC CH6
  8. */
  9. #include <stdio.h>
  10. #include "stm32l031xx.h"
  11. #include "delay.h"
  12. #include "u8g2.h"
  13. /*=======================================================================*/
  14. /* external functions */
  15. uint8_t u8x8_gpio_and_delay_stm32l0(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  16. /*=======================================================================*/
  17. /* global variables */
  18. u8g2_t u8g2; // u8g2 object
  19. uint8_t u8g2_x, u8g2_y; // current position on the screen
  20. volatile unsigned long SysTickCount = 0;
  21. /*=======================================================================*/
  22. void __attribute__ ((interrupt, used)) SysTick_Handler(void)
  23. {
  24. SysTickCount++;
  25. }
  26. void setHSIClock()
  27. {
  28. /* test if the current clock source is something else than HSI */
  29. if ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI)
  30. {
  31. /* enable HSI */
  32. RCC->CR |= RCC_CR_HSION;
  33. /* wait until HSI becomes ready */
  34. while ( (RCC->CR & RCC_CR_HSIRDY) == 0 )
  35. ;
  36. /* enable the HSI "divide by 4" bit */
  37. RCC->CR |= (uint32_t)(RCC_CR_HSIDIVEN);
  38. /* wait until the "divide by 4" flag is enabled */
  39. while((RCC->CR & RCC_CR_HSIDIVF) == 0)
  40. ;
  41. /* then use the HSI clock */
  42. RCC->CFGR = (RCC->CFGR & (uint32_t) (~RCC_CFGR_SW)) | RCC_CFGR_SW_HSI;
  43. /* wait until HSI clock is used */
  44. while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI)
  45. ;
  46. }
  47. /* disable PLL */
  48. RCC->CR &= (uint32_t)(~RCC_CR_PLLON);
  49. /* wait until PLL is inactive */
  50. while((RCC->CR & RCC_CR_PLLRDY) != 0)
  51. ;
  52. /* set latency to 1 wait state */
  53. FLASH->ACR |= FLASH_ACR_LATENCY;
  54. /* At this point the HSI runs with 4 MHz */
  55. /* Multiply by 16 device by 2 --> 32 MHz */
  56. RCC->CFGR = (RCC->CFGR & (~(RCC_CFGR_PLLMUL| RCC_CFGR_PLLDIV ))) | (RCC_CFGR_PLLMUL16 | RCC_CFGR_PLLDIV2);
  57. /* enable PLL */
  58. RCC->CR |= RCC_CR_PLLON;
  59. /* wait until the PLL is ready */
  60. while ((RCC->CR & RCC_CR_PLLRDY) == 0)
  61. ;
  62. /* use the PLL has clock source */
  63. RCC->CFGR |= (uint32_t) (RCC_CFGR_SW_PLL);
  64. /* wait until the PLL source is active */
  65. while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL)
  66. ;
  67. SystemCoreClockUpdate(); /* Update SystemCoreClock global variable */
  68. }
  69. /*
  70. Enable several power regions: PWR, GPIOA
  71. This must be executed after each reset.
  72. */
  73. void startUp(void)
  74. {
  75. RCC->IOPENR |= RCC_IOPENR_IOPAEN; /* Enable clock for GPIO Port A */
  76. RCC->APB1ENR |= RCC_APB1ENR_PWREN; /* enable power interface (PWR) */
  77. PWR->CR |= PWR_CR_DBP; /* activate write access to RCC->CSR and RTC */
  78. SysTick->LOAD = (SystemCoreClock/1000)*50 - 1; /* 50ms task */
  79. SysTick->VAL = 0;
  80. SysTick->CTRL = 7; /* enable, generate interrupt (SysTick_Handler), do not divide by 2 */
  81. }
  82. /*=======================================================================*/
  83. /* u8x8 display procedures */
  84. void initDisplay(void)
  85. {
  86. /* setup display */
  87. u8g2_Setup_ssd1306_i2c_128x64_noname_f(&u8g2, U8G2_R0, u8x8_byte_sw_i2c, u8x8_gpio_and_delay_stm32l0);
  88. u8g2_InitDisplay(&u8g2);
  89. u8g2_SetPowerSave(&u8g2, 0);
  90. u8g2_SetFont(&u8g2, u8g2_font_6x12_tf);
  91. u8g2_ClearBuffer(&u8g2);
  92. u8g2_DrawStr(&u8g2, 0,12, "STM32L031");
  93. u8g2_DrawStr(&u8g2, 0,24, u8x8_u8toa(SystemCoreClock/1000000, 2));
  94. u8g2_DrawStr(&u8g2, 20,24, "MHz");
  95. u8g2_SendBuffer(&u8g2);
  96. u8g2_x = 0;
  97. u8g2_y = 0;
  98. }
  99. void outChar(uint8_t c)
  100. {
  101. u8g2_x+=u8g2_DrawGlyph(&u8g2, u8g2_x, u8g2_y, c);
  102. }
  103. void outStr(const char *s)
  104. {
  105. while( *s )
  106. outChar(*s++);
  107. }
  108. void outHexHalfByte(uint8_t b)
  109. {
  110. b &= 0x0f;
  111. if ( b < 10 )
  112. outChar(b+'0');
  113. else
  114. outChar(b+'a'-10);
  115. }
  116. void outHex8(uint8_t b)
  117. {
  118. outHexHalfByte(b >> 4);
  119. outHexHalfByte(b);
  120. }
  121. void outHex16(uint16_t v)
  122. {
  123. outHex8(v>>8);
  124. outHex8(v);
  125. }
  126. void outHex32(uint32_t v)
  127. {
  128. outHex16(v>>16);
  129. outHex16(v);
  130. }
  131. void setRow(uint8_t r)
  132. {
  133. u8g2_x = 0;
  134. u8g2_y = r;
  135. }
  136. /*=======================================================================*/
  137. /* STOP ANY ADC CONVERSION */
  138. void stopADC(void)
  139. {
  140. ADC1->CR |= ADC_CR_ADSTP;
  141. while(ADC1->CR & ADC_CR_ADSTP)
  142. ;
  143. }
  144. /* CONFIGURATION with ADEN=0 */
  145. /* required to change the configuration of the ADC */
  146. void disableADC(void)
  147. {
  148. /* Check for the ADEN flag. */
  149. /* Setting ADDIS will fail if the ADC is alread disabled: The while loop will not terminate */
  150. if ((ADC1->CR & ADC_CR_ADEN) != 0)
  151. {
  152. /* is this correct? i think we must use the disable flag here */
  153. ADC1->CR |= ADC_CR_ADDIS;
  154. while(ADC1->CR & ADC_CR_ADDIS)
  155. ;
  156. }
  157. }
  158. /* ENABLE ADC (but do not start) */
  159. /* after the ADC is enabled, it must not be reconfigured */
  160. void enableADC(void)
  161. {
  162. ADC1->ISR |= ADC_ISR_ADRDY; /* clear ready flag */
  163. ADC1->CR |= ADC_CR_ADEN; /* enable ADC */
  164. while ((ADC1->ISR & ADC_ISR_ADRDY) == 0) /* wait for ADC */
  165. {
  166. }
  167. }
  168. void initADC(void)
  169. {
  170. //__disable_irq();
  171. /* ADC Clock Enable */
  172. RCC->APB2ENR |= RCC_APB2ENR_ADCEN; /* enable ADC clock */
  173. __NOP(); /* let us wait for some time */
  174. __NOP(); /* let us wait for some time */
  175. /* ADC Reset */
  176. RCC->APB2RSTR |= RCC_APB2RSTR_ADCRST;
  177. __NOP(); /* let us wait for some time */
  178. __NOP(); /* let us wait for some time */
  179. RCC->APB2RSTR &= ~RCC_APB2RSTR_ADCRST;
  180. __NOP(); /* let us wait for some time */
  181. __NOP(); /* let us wait for some time */
  182. /* ADC Basic Setup */
  183. ADC1->IER = 0; /* do not allow any interrupts */
  184. ADC1->CFGR2 &= ~ADC_CFGR2_CKMODE; /* select HSI16 clock */
  185. ADC1->CFGR1 = ADC_CFGR1_RES_1; /* 8 bit resolution */
  186. ADC1->CR |= ADC_CR_ADVREGEN; /* enable ADC voltage regulator, probably not required, because this is automatically activated */
  187. ADC->CCR |= ADC_CCR_VREFEN; /* Wake-up the VREFINT */
  188. ADC->CCR |= ADC_CCR_TSEN; /* Wake-up the temperature sensor */
  189. __NOP(); /* let us wait for some time */
  190. __NOP(); /* let us wait for some time */
  191. /* CALIBRATION */
  192. if ((ADC1->CR & ADC_CR_ADEN) != 0) /* clear ADEN flag if required */
  193. {
  194. /* is this correct? i think we must use the disable flag here */
  195. ADC1->CR &= (uint32_t)(~ADC_CR_ADEN);
  196. }
  197. ADC1->CR |= ADC_CR_ADCAL; /* start calibration */
  198. while ((ADC1->ISR & ADC_ISR_EOCAL) == 0) /* wait for clibration finished */
  199. {
  200. }
  201. ADC1->ISR |= ADC_ISR_EOCAL; /* clear the status flag, by writing 1 to it */
  202. __NOP(); /* not sure why, but some nop's are required here, at least 4 of them */
  203. __NOP();
  204. __NOP();
  205. __NOP();
  206. __NOP();
  207. __NOP();
  208. /* CONFIGURATION with ADEN=0 */
  209. disableADC();
  210. //ADC1->CFGR1 &= ~ADC_CFGR1_RES; /* 12 bit resolution */
  211. ADC1->CFGR1 = ADC_CFGR1_RES_1; /* 8 bit resolution */
  212. enableADC();
  213. }
  214. /*
  215. ch0 PA0 pin 6
  216. ch1 PA1 pin 7
  217. ch2 PA2 pin 8
  218. ch3 PA3 pin 9
  219. ch4 PA4 pin 10
  220. ch5 PA5 pin 11
  221. ch6 PA6 pin 12
  222. ch7 PA7 pin 13
  223. ch8 PB0 -
  224. ch9 PB1 pin 14
  225. ch 0..15: GPIO
  226. ch 16: ???
  227. ch 17: vref (bandgap)
  228. ch18: temperature sensor
  229. returns 12 bit result, right aligned
  230. */
  231. uint16_t getADC(uint8_t ch)
  232. {
  233. //uint32_t i;
  234. stopADC();
  235. disableADC();
  236. /* CONFIGURE ADC */
  237. //ADC1->CFGR1 &= ~ADC_CFGR1_EXTEN; /* software enabled conversion start */
  238. //ADC1->CFGR1 &= ~ADC_CFGR1_ALIGN; /* right alignment */
  239. ADC1->CFGR1 = ADC_CFGR1_RES_1; /* 8 bit resolution */
  240. //ADC1->SMPR |= ADC_SMPR_SMP_0 | ADC_SMPR_SMP_1 | ADC_SMPR_SMP_2; /* Select a sampling mode of 111 (very slow)*/
  241. ADC1->SMPR = 0;
  242. enableADC();
  243. ADC1->CHSELR = 1<<ch; /* Select channel (can be done also if ADC is enabled) */
  244. /* DO CONVERSION */
  245. ADC1->CR |= ADC_CR_ADSTART; /* start the ADC conversion */
  246. while ((ADC1->ISR & ADC_ISR_EOC) == 0) /* wait end of conversion */
  247. {
  248. }
  249. return ADC1->DR;
  250. }
  251. void scanADC(uint8_t ch, uint16_t cnt, uint8_t *buf)
  252. {
  253. stopADC();
  254. disableADC();
  255. RCC->AHBENR |= RCC_AHBENR_DMAEN; /* enable DMA clock */
  256. __NOP(); __NOP(); /* extra delay for clock stabilization required? */
  257. /* disable and reset to defaults */
  258. DMA1_Channel1->CCR = 0;
  259. /* defaults:
  260. - 8 Bit access --> ok
  261. - read from peripheral --> ok
  262. - none-circular mode --> ok
  263. - no increment mode --> will be changed below
  264. */
  265. DMA1_Channel1->CNDTR = cnt; /* buffer size */
  266. DMA1_Channel1->CPAR = (uint32_t)&(ADC1->DR); /* source value */
  267. DMA1_Channel1->CMAR = (uint32_t)buf; /* destination memory */
  268. DMA1_CSELR->CSELR &= ~DMA_CSELR_C1S; /* 0000: select ADC for DMA CH 1 (this is reset default) */
  269. DMA1_Channel1->CCR |= DMA_CCR_MINC; /* increment memory */
  270. DMA1_Channel1->CCR |= DMA_CCR_EN; /* enable */
  271. /*
  272. detect rising edge on external trigger (ADC_CFGR1_EXTEN_0)
  273. recive trigger from TIM2 (ADC_CFGR1_EXTSEL_1)
  274. 8 Bit resolution (ADC_CFGR1_RES_1)
  275. Use DMA one shot mode and enable DMA (ADC_CFGR1_DMAEN)
  276. Once DMA is finished, it will disable continues mode (ADC_CFGR1_CONT)
  277. */
  278. ADC1->CFGR1 = ADC_CFGR1_EXTEN_0 /* rising edge */
  279. | ADC_CFGR1_EXTSEL_1 /* TIM2 */
  280. | ADC_CFGR1_RES_1 /* 8 Bit resolution */
  281. | ADC_CFGR1_CONT /* continues mode */
  282. | ADC_CFGR1_DMAEN; /* enable generation of DMA requests */
  283. //ADC1->SMPR |= ADC_SMPR_SMP_0 | ADC_SMPR_SMP_1 | ADC_SMPR_SMP_2;
  284. //ADC1->SMPR = ADC_SMPR_SMP_1 ;
  285. ADC1->SMPR = ADC_SMPR_SMP_0 | ADC_SMPR_SMP_1 ;
  286. /*
  287. 12.5 + 8.5 = 21 ADC Cycles pre ADC sampling
  288. 4 MHz / 21 cycle / 256 = 744 Hz
  289. */
  290. enableADC();
  291. /* conversion will be started automatically with rising edge of TIM2, yet ADSTART is still required */
  292. ADC1->CR |= ADC_CR_ADSTART; /* start the ADC conversion */
  293. /* wait until DMA is completed */
  294. while ( DMA1_Channel1->CNDTR > 0 )
  295. ;
  296. }
  297. /*
  298. special values:
  299. -1 Can not find level
  300. 255 no rotation
  301. 0..254 BMEF level, speed is k*(255-getBEMFLevel())
  302. */
  303. int getBEMFLevel(uint16_t cnt, uint8_t *buf, uint16_t start)
  304. {
  305. return -1;
  306. }
  307. /*=======================================================================*/
  308. void initTIM(uint16_t tim_cycle)
  309. {
  310. /* enable clock for TIM2 */
  311. RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
  312. /*enable clock for GPIOA */
  313. RCC->IOPENR |= RCC_IOPENR_IOPAEN; /* Enable clock for GPIO Port A */
  314. __NOP(); /* extra delay for clock stabilization required? */
  315. __NOP();
  316. /* prescalar for AHB and APB1 */
  317. /* reselt defaults for HPRE and PPRE1: no clock division */
  318. // RCC->CFGR &= ~RCC_CFGR_HPRE;
  319. // RCC->CFGR |= RCC_CFGR_HPRE_DIV1;
  320. // RCC->CFGR &= ~RCC_CFGR_PPRE1;
  321. // RCC->CFGR |= RCC_CFGR_PPRE1_DIV1;
  322. /* configure GPIOA PA1 for TIM2 */
  323. GPIOA->MODER &= ~GPIO_MODER_MODE1; /* clear mode for PA9 */
  324. GPIOA->MODER |= GPIO_MODER_MODE1_1; /* alt fn */
  325. GPIOA->OTYPER &= ~GPIO_OTYPER_OT_1; /* push-pull */
  326. GPIOA->AFR[0] &= ~(15<<4); /* Clear Alternate Function PA1 */
  327. GPIOA->AFR[0] |= 2<<4; /* AF2 Alternate Function PA1 */
  328. /* TIM2 configure */
  329. /* disable all interrupts */
  330. //TIM2->DIER = 0; /* 0 is reset default value */
  331. /* clear everything, including the "Update disable" flag, so that updates */
  332. /* are generated */
  333. // TIM2->CR1 = 0; /* 0 is reset default value */
  334. //TIM2->CR1 |= TIM_CR1_ARPE; // ARR is not modified so constant update is ok
  335. /* Update request by manual UG bit setting or slave controller */
  336. /* both is not required here */
  337. /* so, update request by couter over/underflow remains */
  338. //TIM2->CR1 |= TIM_CR1_URS; /* only udf/ovf generae events */
  339. TIM2->CR2 |= TIM_CR2_MMS_1; /* Update event for TRGO */
  340. TIM2->ARR = 5355; /* total cycle count */
  341. TIM2->CCR2 = 1024; /* duty cycle */
  342. //TIM2->CCMR1 &= ~TIM_CCMR1_OC2CE; /* disable clear output compare 2 **/
  343. TIM2->CCMR1 |= TIM_CCMR1_OC2M; /* all 3 bits set: PWM Mode 2 */
  344. //TIM2->CCMR1 &= ~TIM_CCMR1_OC1M_0; /* 110: PWM Mode 1 */
  345. TIM2->CCMR1 |= TIM_CCMR1_OC2PE; /* preload enable CCR2 is preloaded*/
  346. // TIM2->CCMR1 &= ~TIM_CCMR1_OC2FE; /* fast disable (reset default) */
  347. // TIM2->CCMR1 &= ~TIM_CCMR1_CC2S; /* configure cc2 as output (this is reset default) */
  348. //TIM2->EGR |= TIM_EGR_CC2G; /* capture event cc2 */
  349. TIM2->CCER |= TIM_CCER_CC2E; /* set output enable */
  350. //TIM2->CCER |= TIM_CCER_CC2P; /* polarity 0: normal (reset default) / 1: inverted*/
  351. TIM2->PSC = 7; /* divide by 8 */
  352. TIM2->CR1 |= TIM_CR1_CEN; /* counter enable */
  353. /*
  354. TIM2 cycle:
  355. 32000000Hz / 5355 / 8 = 747 Hz
  356. */
  357. }
  358. /*=======================================================================*/
  359. #define BUF_MUL 2
  360. #define TIM_CYCLE_TIME 5355
  361. #define TIM_CYCLE_UPPER_SKIP 100
  362. #define TIM_CYCLE_LOWER_SKIP 400
  363. uint8_t adc_buf[128*BUF_MUL];
  364. void main()
  365. {
  366. uint16_t adc_value;
  367. uint16_t tim_duty;
  368. uint16_t zero_pos;
  369. uint16_t i;
  370. u8g2_uint_t y, yy;
  371. setHSIClock(); /* enable 32 MHz Clock */
  372. startUp(); /* enable systick irq and several power regions */
  373. initDisplay(); /* aktivate display */
  374. initADC();
  375. RCC->IOPENR |= RCC_IOPENR_IOPAEN; /* Enable clock for GPIO Port A */
  376. __NOP();
  377. __NOP();
  378. GPIOA->MODER &= ~GPIO_MODER_MODE1; /* clear mode for PA1 */
  379. GPIOA->MODER |= GPIO_MODER_MODE1_0; /* Output mode for PA1 */
  380. GPIOA->OTYPER &= ~GPIO_OTYPER_OT_1; /* no Push/Pull for PA1 */
  381. GPIOA->OSPEEDR &= ~GPIO_OSPEEDER_OSPEED1; /* low speed for PA1 */
  382. GPIOA->PUPDR &= ~GPIO_PUPDR_PUPD1; /* no pullup/pulldown for PA1 */
  383. GPIOA->BSRR = GPIO_BSRR_BS_1; /* atomic set PA1 */
  384. initTIM(TIM_CYCLE_TIME);
  385. for(;;)
  386. {
  387. u8g2_ClearBuffer(&u8g2);
  388. adc_value = getADC(5);
  389. tim_duty = ((uint32_t)adc_value*((uint32_t)TIM_CYCLE_TIME-TIM_CYCLE_UPPER_SKIP-TIM_CYCLE_LOWER_SKIP))>>8;
  390. tim_duty += TIM_CYCLE_LOWER_SKIP;
  391. TIM2->CCR2 = tim_duty;
  392. TIM2->SR &= ~TIM_SR_UIF;
  393. while( (TIM2->SR & TIM_SR_UIF) == 0 )
  394. ;
  395. yy = 30;
  396. for( i = 0; i < 128; i++ )
  397. {
  398. y = 30-(getADC(6)>>3);
  399. u8g2_DrawPixel(&u8g2, i, y);
  400. if ( y < yy )
  401. u8g2_DrawVLine(&u8g2, i, y, yy-y+1);
  402. else
  403. u8g2_DrawVLine(&u8g2, i, yy, y-yy+1);
  404. yy = y;
  405. }
  406. for( i = 0; i < 128*BUF_MUL; i++ )
  407. adc_buf[i] = i;
  408. scanADC(6, 128*BUF_MUL, adc_buf);
  409. yy = 60;
  410. zero_pos = ((uint32_t)tim_duty * (uint32_t)256) / (uint32_t)TIM_CYCLE_TIME;
  411. zero_pos +=4;
  412. zero_pos += (256-zero_pos)>>6;
  413. setRow(10); outHex16(adc_value);
  414. outStr(" "); outHex8(adc_buf[0]); outStr(" "); outHex8(adc_buf[1]); outStr(" "); outHex8(adc_buf[2]);
  415. outStr("|"); outHex8(adc_buf[zero_pos/2]); outStr("|"); outHex8(adc_buf[zero_pos]);
  416. u8g2_DrawVLine(&u8g2, zero_pos/2, yy-7, 15);
  417. u8g2_DrawVLine(&u8g2, zero_pos/4, yy-7, 15);
  418. for( i = 0; i < 128; i++ )
  419. {
  420. y = 60-(adc_buf[i*BUF_MUL]>>3);
  421. u8g2_DrawPixel(&u8g2, i, y);
  422. if ( y < yy )
  423. u8g2_DrawVLine(&u8g2, i, y, yy-y+1);
  424. else
  425. u8g2_DrawVLine(&u8g2, i, yy, y-yy+1);
  426. yy = y;
  427. }
  428. u8g2_SendBuffer(&u8g2);
  429. }
  430. }