guihal.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. guihal.c
  3. */
  4. #include "stm32l031xx.h"
  5. #include "gui.h"
  6. /*============================================*/
  7. /* reset */
  8. void do_reset(void)
  9. {
  10. /* clear the display first, to provide some visual feedback to the user */
  11. u8g2_ClearDisplay(gui_menu.u8g2);
  12. __disable_irq();
  13. /* Deactivate tamper so that the uC can be programmed via UART */
  14. /* This is required, because RX pin is shared with Tamp2 */
  15. RTC->WPR = 0x0ca; /* disable RTC write protection */
  16. RTC->WPR = 0x053;
  17. EXTI->IMR &= ~EXTI_IMR_IM19; /* disable tamper irq */
  18. RTC->TAMPCR = 0; /* clear tamper detection */
  19. RTC->WPR = 0; /* disable RTC write protection */
  20. RTC->WPR = 0;
  21. /* execute reset */
  22. NVIC_SystemReset();
  23. }
  24. /*============================================*/
  25. /* load & store from/to permanent memory */
  26. /* 5x 32bit */
  27. void store_gui_data(uint32_t *data)
  28. {
  29. RTC->BKP0R = data[0];
  30. RTC->BKP1R = data[1];
  31. RTC->BKP2R = data[2];
  32. RTC->BKP3R = data[3];
  33. RTC->BKP4R = data[4];
  34. }
  35. void load_gui_data(uint32_t *data)
  36. {
  37. int i;
  38. for( i = 0; i < GUI_ALARM_CNT; i++ )
  39. data[i] = 0;
  40. data[0] = RTC->BKP0R;
  41. data[1] = RTC->BKP1R;
  42. data[2] = RTC->BKP2R;
  43. data[3] = RTC->BKP3R;
  44. data[4] = RTC->BKP4R;
  45. }
  46. /*============================================*/
  47. /* input */
  48. int is_por_reset(void)
  49. {
  50. return 1;
  51. }
  52. int is_button_reset(void)
  53. {
  54. return 0;
  55. }
  56. uint32_t get_boot_status_register(void)
  57. {
  58. /* POR: 00001100 (POR=1, PIN=1), reset pin has RC network connected */
  59. /* Boot via NVIC: 00011100, compared to POR, the SW Reset flag is also set */
  60. return RCC->CSR >> 24;
  61. }
  62. extern volatile unsigned long PWR_CSR_Backup;
  63. uint32_t get_pwr_status_register(void)
  64. {
  65. /*
  66. Bit 1 SBF: Standby flag
  67. This bit is set by hardware and cleared only by a POR/PDR (power-on reset/power-down
  68. reset) or by setting the CSBF bit in the PWR power control register (PWR_CR)
  69. 0: Device has not been in Standby mode
  70. 1: Device has been in Standby mode
  71. Bit 0 WUF: Wakeup flag
  72. This bit is set by hardware and cleared by a system reset or by setting the CWUF bit in the
  73. PWR power control register (PWR_CR)
  74. 0: No wakeup event occurred
  75. 1: A wakeup event was received from the WKUP pin or from the RTC
  76. POR (voltage off-on): 00
  77. Reset Button: 00
  78. WUF Reset: 11
  79. */
  80. return PWR_CSR_Backup & 255;
  81. }
  82. extern volatile unsigned long ResetReason;
  83. uint32_t get_reset_reason(void)
  84. {
  85. return ResetReason;
  86. }
  87. extern volatile unsigned long RTCWUCount;
  88. uint32_t get_wakeup_count(void)
  89. {
  90. return RTCWUCount;
  91. }
  92. int is_dst_by_date(uint8_t region);
  93. uint32_t get_dst_by_date(void)
  94. {
  95. return is_dst_by_date(1);
  96. }
  97. uint32_t get_dst_by_RTC(void)
  98. {
  99. uint32_t dst_state = 0;
  100. if ( RTC->CR & RTC_CR_BCK ) /* BKP flag in the CR register */
  101. dst_state = 1;
  102. return dst_state;
  103. }
  104. /*============================================*/
  105. /* output */
  106. void SetAlarmSequence(const uint8_t *alarm_sequence);
  107. extern const uint8_t ASeqTrippleBeep[];
  108. void enable_alarm(void)
  109. {
  110. SetAlarmSequence(ASeqTrippleBeep);
  111. }
  112. void disable_alarm(void)
  113. {
  114. SetAlarmSequence(NULL);
  115. }
  116. void set_time(uint8_t ht, uint8_t ho, uint8_t mt, uint8_t mo, uint8_t st, uint8_t so)
  117. {
  118. uint32_t v;
  119. v = ht;
  120. v <<= 4,
  121. v |= ho;
  122. v <<= 4;
  123. v |= mt;
  124. v <<= 4;
  125. v |= mo;
  126. v <<= 4;
  127. v |= st;
  128. v <<= 4;
  129. v |= so;
  130. __disable_irq();
  131. RTC->WPR = 0x0ca; /* disable RTC write protection */
  132. RTC->WPR = 0x053;
  133. RTC->ISR = RTC_ISR_INIT; /* request RTC stop */
  134. while((RTC->ISR & RTC_ISR_INITF)!=RTC_ISR_INITF) /* wait for stop */
  135. ;
  136. RTC->PRER = 0x07f00ff; /* 1 Hz clock */
  137. RTC->TR = v;
  138. RTC->ISR =~ RTC_ISR_INIT; /* start RTC */
  139. RTC->WPR = 0; /* enable RTC write protection */
  140. RTC->WPR = 0;
  141. __enable_irq();
  142. }
  143. /*
  144. weekday: 0 = monday
  145. */
  146. void set_date(uint8_t yt, uint8_t yo, uint8_t mt, uint8_t mo, uint8_t dayt, uint8_t dayo, uint8_t weekday)
  147. {
  148. uint32_t v;
  149. v = yt;
  150. v <<= 4,
  151. v |= yo;
  152. v <<= 3;
  153. v |= weekday+1; /* monday starts with 1 on the STM32L0 */
  154. v <<= 1;
  155. v |= mt;
  156. v <<= 4;
  157. v |= mo;
  158. v <<= 4;
  159. v |= dayt;
  160. v <<= 4;
  161. v |= dayo;
  162. __disable_irq();
  163. RTC->WPR = 0x0ca; /* disable RTC write protection */
  164. RTC->WPR = 0x053;
  165. RTC->ISR = RTC_ISR_INIT; /* request RTC stop */
  166. while((RTC->ISR & RTC_ISR_INITF)!=RTC_ISR_INITF) /* wait for stop */
  167. ;
  168. RTC->PRER = 0x07f00ff; /* 1 Hz clock */
  169. RTC->DR = v;
  170. RTC->ISR =~ RTC_ISR_INIT; /* start RTC */
  171. RTC->WPR = 0; /* enable RTC write protection */
  172. RTC->WPR = 0;
  173. __enable_irq();
  174. }
  175. /* value 1..7, 0 is default (do not set) */
  176. void set_contrast(void)
  177. {
  178. uint8_t v = gui_data.contrast;
  179. if ( v > 0 )
  180. {
  181. v = v * 36;
  182. u8g2_SetContrast(gui_menu.u8g2, v);
  183. }
  184. }