main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /* LED blink project for the STM32L031 */
  2. #include "stm32l031xx.h"
  3. #include "delay.h"
  4. #include "u8x8.h"
  5. /*=======================================================================*/
  6. /* external functions */
  7. uint8_t u8x8_gpio_and_delay_stm32l0(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  8. /*=======================================================================*/
  9. /* global variables */
  10. u8x8_t u8x8;
  11. volatile unsigned long SysTickCount = 0;
  12. /*=======================================================================*/
  13. void __attribute__ ((interrupt, used)) SysTick_Handler(void)
  14. {
  15. SysTickCount++;
  16. }
  17. void setHSIClock()
  18. {
  19. /* test if the current clock source is something else than HSI */
  20. if ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI)
  21. {
  22. /* enable HSI */
  23. RCC->CR |= RCC_CR_HSION;
  24. /* wait until HSI becomes ready */
  25. while ( (RCC->CR & RCC_CR_HSIRDY) == 0 )
  26. ;
  27. /* enable the HSI "divide by 4" bit */
  28. RCC->CR |= (uint32_t)(RCC_CR_HSIDIVEN);
  29. /* wait until the "divide by 4" flag is enabled */
  30. while((RCC->CR & RCC_CR_HSIDIVF) == 0)
  31. ;
  32. /* then use the HSI clock */
  33. RCC->CFGR = (RCC->CFGR & (uint32_t) (~RCC_CFGR_SW)) | RCC_CFGR_SW_HSI;
  34. /* wait until HSI clock is used */
  35. while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI)
  36. ;
  37. }
  38. /* disable PLL */
  39. RCC->CR &= (uint32_t)(~RCC_CR_PLLON);
  40. /* wait until PLL is inactive */
  41. while((RCC->CR & RCC_CR_PLLRDY) != 0)
  42. ;
  43. /* set latency to 1 wait state */
  44. FLASH->ACR |= FLASH_ACR_LATENCY;
  45. /* At this point the HSI runs with 4 MHz */
  46. /* Multiply by 16 device by 2 --> 32 MHz */
  47. RCC->CFGR = (RCC->CFGR & (~(RCC_CFGR_PLLMUL| RCC_CFGR_PLLDIV ))) | (RCC_CFGR_PLLMUL16 | RCC_CFGR_PLLDIV2);
  48. /* enable PLL */
  49. RCC->CR |= RCC_CR_PLLON;
  50. /* wait until the PLL is ready */
  51. while ((RCC->CR & RCC_CR_PLLRDY) == 0)
  52. ;
  53. /* use the PLL has clock source */
  54. RCC->CFGR |= (uint32_t) (RCC_CFGR_SW_PLL);
  55. /* wait until the PLL source is active */
  56. while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL)
  57. ;
  58. }
  59. /*
  60. Enable several power regions: PWR, GPIOA
  61. Enable write access to RTC
  62. This must be executed after each reset.
  63. */
  64. void startUp(void)
  65. {
  66. RCC->IOPENR |= RCC_IOPENR_IOPAEN; /* Enable clock for GPIO Port A */
  67. RCC->APB1ENR |= RCC_APB1ENR_PWREN; /* enable power interface */
  68. PWR->CR |= PWR_CR_DBP; /* activate write access to RCC->CSR and RTC */
  69. //PWR_CSR_Backup = PWR->CSR; /* create a backup of the original PWR_CSR register for later analysis */
  70. PWR->CR |= PWR_CR_CSBF; /* clear the standby flag in the PWR_CSR register, but luckily we have a copy */
  71. PWR->CR |= PWR_CR_CWUF; /* also clear the WUF flag in PWR_CSR */
  72. /* PA0, TAMP2, button input */
  73. GPIOA->MODER &= ~GPIO_MODER_MODE0; /* clear mode for PA0 */
  74. GPIOA->PUPDR &= ~GPIO_PUPDR_PUPD0; /* no pullup/pulldown for PA0 */
  75. GPIOA->PUPDR |= GPIO_PUPDR_PUPD0_0; /* pullup for PA0 */
  76. /* PA2, TAMP3, button input */
  77. GPIOA->MODER &= ~GPIO_MODER_MODE2; /* clear mode for PA2 */
  78. GPIOA->PUPDR &= ~GPIO_PUPDR_PUPD2; /* no pullup/pulldown for PA2 */
  79. GPIOA->PUPDR |= GPIO_PUPDR_PUPD2_0; /* pullup for PA2 */
  80. }
  81. /* write access must be activated before calling this function: PWR->CR |= PWR_CR_DBP; */
  82. unsigned int initRTC(void)
  83. {
  84. unsigned int r = 0;
  85. /* real time clock enable */
  86. //enableRCCRTCWrite();
  87. __disable_irq();
  88. RTC->WPR = 0x0ca; /* disable RTC write protection */
  89. RTC->WPR = 0x053;
  90. /* try externel 32K clock source */
  91. RCC->CSR |= RCC_CSR_LSEBYP; /* bypass oscillator */
  92. RCC->CSR |= RCC_CSR_LSEON; /* enable low speed external clock */
  93. delay_micro_seconds(100000*5); /* LSE requires between 100ms to 200ms */
  94. if ( RCC->CSR & RCC_CSR_LSERDY )
  95. {
  96. r = 1;
  97. }
  98. else
  99. {
  100. RCC->CSR &= ~RCC_CSR_LSEON; /* disable external clock */
  101. /* try externel 32K oscillator */
  102. RCC->CSR &= ~RCC_CSR_LSEBYP; /* no bypass oscillator */
  103. RCC->CSR &= ~RCC_CSR_LSEDRV_Msk; /* lowest drive */
  104. RCC->CSR |= RCC_CSR_LSEDRV_0; /* medium low drive */
  105. RCC->CSR |= RCC_CSR_LSEON; /* enable low speed external clock */
  106. delay_micro_seconds(100000*6); /* LSE requires between 200ms and 400ms */
  107. if ( RCC->CSR & RCC_CSR_LSERDY )
  108. {
  109. r = 2;
  110. }
  111. }
  112. if ( r > 0 )
  113. {
  114. RCC->CSR &= ~RCC_CSR_RTCSEL_Msk; /* no clock selection for RTC */
  115. RCC->CSR |= RCC_CSR_RTCSEL_LSE; /* select LSE */
  116. RCC->CSR |= RCC_CSR_RTCEN; /* enable RTC */
  117. RTC->ISR = RTC_ISR_INIT; /* request RTC stop */
  118. while((RTC->ISR & RTC_ISR_INITF)!=RTC_ISR_INITF) /* wait for stop */
  119. ;
  120. RTC->PRER = 0x07f00ff; /* 1 Hz clock */
  121. RTC->TR = 0;
  122. RTC->ISR =~ RTC_ISR_INIT; /* start RTC */
  123. }
  124. RTC->WPR = 0; /* enable RTC write protection */
  125. RTC->WPR = 0;
  126. __enable_irq();
  127. return r;
  128. }
  129. void printBits(uint8_t y, uint16_t val)
  130. {
  131. int i;
  132. for( i = 0; i < 16; i++ )
  133. {
  134. u8x8_DrawGlyph(&u8x8, i, y, val & (1<<(15-i)) ? '1' : '0' );
  135. }
  136. }
  137. /*
  138. ch 0..15: GPIO
  139. ch 16: ???
  140. ch 17: vref (bandgap)
  141. ch18: temperature sensor
  142. returns 12 bit result, right aligned
  143. */
  144. uint16_t readADC(uint8_t ch)
  145. {
  146. uint32_t data;
  147. uint32_t i;
  148. __disable_irq();
  149. /* ADC RESET */
  150. RCC->APB2ENR |= RCC_APB2ENR_ADCEN; /* enable ADC clock */
  151. __NOP(); /* let us wait for some time */
  152. __NOP(); /* let us wait for some time */
  153. RCC->APB2RSTR |= RCC_APB2RSTR_ADCRST;
  154. __NOP(); /* let us wait for some time */
  155. __NOP(); /* let us wait for some time */
  156. RCC->APB2RSTR &= ~RCC_APB2RSTR_ADCRST;
  157. __NOP(); /* let us wait for some time */
  158. __NOP(); /* let us wait for some time */
  159. /* Enable some basic parts */
  160. ADC1->IER = 0; /* do not allow any interrupts */
  161. ADC1->CFGR2 &= ~ADC_CFGR2_CKMODE; /* select HSI16 clock */
  162. ADC1->CR |= ADC_CR_ADVREGEN; /* enable ADC voltage regulator, probably not required, because this is automatically activated */
  163. ADC->CCR |= ADC_CCR_VREFEN; /* Wake-up the VREFINT */
  164. ADC->CCR |= ADC_CCR_TSEN; /* Wake-up the temperature sensor */
  165. __NOP(); /* let us wait for some time */
  166. __NOP(); /* let us wait for some time */
  167. /* CALIBRATION */
  168. if ((ADC1->CR & ADC_CR_ADEN) != 0) /* clear ADEN flag if required */
  169. {
  170. ADC1->CR &= (uint32_t)(~ADC_CR_ADEN);
  171. }
  172. ADC1->CR |= ADC_CR_ADCAL; /* start calibration */
  173. while ((ADC1->ISR & ADC_ISR_EOCAL) == 0) /* wait for clibration finished */
  174. {
  175. }
  176. ADC1->ISR |= ADC_ISR_EOCAL; /* clear the status flag, by writing 1 to it */
  177. __NOP(); /* not sure why, but some nop's are required here, at least 4 of them */
  178. __NOP();
  179. __NOP();
  180. __NOP();
  181. __NOP();
  182. __NOP();
  183. /* ENABLE ADC */
  184. ADC1->ISR |= ADC_ISR_ADRDY; /* clear ready flag */
  185. ADC1->CR |= ADC_CR_ADEN; /* enable ADC */
  186. while ((ADC1->ISR & ADC_ISR_ADRDY) == 0) /* wait for ADC */
  187. {
  188. }
  189. //printBits(5, ADC1->ISR );
  190. //printBits(6, ADC1->CR );
  191. /* CONFIGURE ADC */
  192. ADC1->CFGR1 &= ~ADC_CFGR1_EXTEN; /* software enabled conversion start */
  193. ADC1->CFGR1 &= ~ADC_CFGR1_ALIGN; /* right alignment */
  194. ADC1->CFGR1 &= ~ADC_CFGR1_RES; /* 12 bit resolution */
  195. ADC1->CHSELR = 1<<ch; /* Select channel */
  196. ADC1->SMPR |= ADC_SMPR_SMP_0 | ADC_SMPR_SMP_1 | ADC_SMPR_SMP_2; /* Select a sampling mode of 111 (very slow)*/
  197. /* DO CONVERSION */
  198. data = 0;
  199. for( i = 0; i < 8; i++ )
  200. {
  201. ADC1->CR |= ADC_CR_ADSTART; /* start the ADC conversion */
  202. while ((ADC1->ISR & ADC_ISR_EOC) == 0) /* wait end of conversion */
  203. {
  204. }
  205. data += ADC1->DR; /* get ADC result and clear the ISR_EOC flag */
  206. }
  207. data >>= 3;
  208. /* DISABLE ADC */
  209. /* at this point the end of sampling and end of sequence bits are also set in ISR registr */
  210. if ( (ADC1->CR & ADC_CR_ADEN) != 0 )
  211. {
  212. ADC1->CR |= ADC_CR_ADDIS; /* disable ADC... maybe better execute a reset */
  213. while ((ADC1->CR & ADC_CR_ADEN) != 0) /* wait for ADC disable, ADEN is also cleared */
  214. {
  215. }
  216. }
  217. /* DISABLE OTHER PARTS, INCLUDING CLOCK */
  218. ADC->CCR &= ~ADC_CCR_VREFEN; /* disable VREFINT */
  219. ADC->CCR &= ~ADC_CCR_TSEN; /* disable temperature sensor */
  220. ADC1->CR &= ~ADC_CR_ADVREGEN; /* disable ADC voltage regulator */
  221. RCC->APB2ENR &= ~RCC_APB2ENR_ADCEN; /* disable ADC clock */
  222. __enable_irq();
  223. return data;
  224. }
  225. uint16_t getTemperature(void)
  226. {
  227. int16_t y1, y2,x1, x2, t;
  228. int16_t y;
  229. y1 = 30;
  230. x1 = *(uint16_t *)(0x1FF8007A); // 30 degree with 3.0V
  231. x1 *=30;
  232. x1 /=33;
  233. y2 = 110; // AN3964: 110 degree, Datasheet: 130 degree
  234. x2 = *(uint16_t *)(0x1FF8007E); // 130 degree with 3.0V
  235. x2 *=30;
  236. x2 /=33;
  237. t = readADC(18);
  238. y = ( (y2 - y1) * ( t - x1) ) / (x2 - x1) + y1;
  239. u8x8_DrawString(&u8x8, 0,6, u8x8_u16toa((y2 - y1)/(x2 - x1), 5));
  240. u8x8_DrawString(&u8x8, 7,6, u8x8_u16toa(t, 5));
  241. u8x8_DrawString(&u8x8, 13,6, u8x8_u16toa(y, 3));
  242. return y;
  243. }
  244. int main()
  245. {
  246. unsigned int rtcState;
  247. setHSIClock();
  248. SystemCoreClockUpdate(); /* Update SystemCoreClock() */
  249. //SystemCoreClock = 32000000UL;
  250. RCC->IOPENR |= RCC_IOPENR_IOPAEN; /* Enable clock for GPIO Port A */
  251. __NOP();
  252. __NOP();
  253. GPIOA->MODER &= ~GPIO_MODER_MODE13; /* clear mode for PA13 */
  254. GPIOA->MODER |= GPIO_MODER_MODE13_0; /* Output mode for PA13 */
  255. GPIOA->OTYPER &= ~GPIO_OTYPER_OT_13; /* Push/Pull for PA13 */
  256. GPIOA->OSPEEDR &= ~GPIO_OSPEEDER_OSPEED13; /* low speed for PA13 */
  257. GPIOA->PUPDR &= ~GPIO_PUPDR_PUPD13; /* no pullup/pulldown for PA13 */
  258. GPIOA->BSRR = GPIO_BSRR_BR_13; /* atomic clr PA13 */
  259. GPIOA->BSRR = GPIO_BSRR_BS_13; /* atomic set PA13 */
  260. SysTick->LOAD = (SystemCoreClock/1000)*50 - 1; /* 50ms task */
  261. SysTick->VAL = 0;
  262. SysTick->CTRL = 7; /* enable, generate interrupt (SysTick_Handler), do not divide by 2 */
  263. /* setup display */
  264. //u8g2_Setup_ssd1306_i2c_128x64_noname_2(&u8g2, U8G2_R0, u8x8_byte_sw_i2c, u8x8_gpio_and_delay_stm32l0);
  265. //u8g2_InitDisplay(&u8g2);
  266. //u8g2_SetPowerSave(&u8g2, 0);
  267. startUp();
  268. rtcState = initRTC();
  269. u8x8_Setup(&u8x8, u8x8_d_ssd1306_128x64_noname, u8x8_cad_ssd13xx_i2c, u8x8_byte_sw_i2c, u8x8_gpio_and_delay_stm32l0);
  270. u8x8_InitDisplay(&u8x8);
  271. u8x8_ClearDisplay(&u8x8);
  272. u8x8_SetPowerSave(&u8x8, 0);
  273. u8x8_SetFont(&u8x8, u8x8_font_amstrad_cpc_extended_r);
  274. u8x8_DrawString(&u8x8, 0,0, "Hello World!");
  275. u8x8_DrawGlyph(&u8x8, 0,1, rtcState+'0');
  276. u8x8_DrawString(&u8x8, 0,2, "Vref:");
  277. u8x8_DrawString(&u8x8, 7,2, u8x8_u16toa(readADC(17), 4));
  278. u8x8_DrawString(&u8x8, 0,3, "Temp:");
  279. u8x8_DrawString(&u8x8, 7,3, u8x8_u16toa(readADC(18), 4));
  280. u8x8_DrawString(&u8x8, 13,3, u8x8_u16toa(getTemperature(), 3));
  281. u8x8_DrawString(&u8x8, 0,4, "c30:");
  282. u8x8_DrawString(&u8x8, 7,4, u8x8_u16toa(*(uint16_t *)(0x1FF8007A), 4));
  283. u8x8_DrawString(&u8x8, 0,5, "c130:");
  284. u8x8_DrawString(&u8x8, 7,5, u8x8_u16toa(*(uint16_t *)(0x1FF8007E), 4));
  285. for(;;)
  286. {
  287. u8x8_DrawString(&u8x8, 0,2, "Vref:");
  288. u8x8_DrawString(&u8x8, 7,2, u8x8_u16toa(readADC(17), 4));
  289. u8x8_DrawString(&u8x8, 0,3, "Temp:");
  290. u8x8_DrawString(&u8x8, 7,3, u8x8_u16toa(readADC(18), 4));
  291. u8x8_DrawString(&u8x8, 13,3, u8x8_u16toa(getTemperature(), 3));
  292. u8x8_DrawString(&u8x8, 0,4, "c30:");
  293. u8x8_DrawString(&u8x8, 7,4, u8x8_u16toa(*(uint16_t *)(0x1FF8007A), 4));
  294. u8x8_DrawString(&u8x8, 0,5, "c130:");
  295. u8x8_DrawString(&u8x8, 7,5, u8x8_u16toa(*(uint16_t *)(0x1FF8007E), 4));
  296. delay_micro_seconds(500000);
  297. GPIOA->BSRR = GPIO_BSRR_BS_13; /* atomic set PA13 */
  298. delay_micro_seconds(500000);
  299. GPIOA->BSRR = GPIO_BSRR_BR_13; /* atomic clr PA13 */
  300. }
  301. }