main.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*!
  2. \file main.c
  3. \brief CMP watchdog window
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-06-06, V1.0.0, firmware for GD32F3x0
  8. */
  9. #include "gd32f3x0.h"
  10. #include <stdio.h>
  11. #include "gd32f3x0_eval.h"
  12. #include "main.h"
  13. uint32_t delayms;
  14. void led_config(void);
  15. void deepsleep_mode_config(void);
  16. void cmp_config(void);
  17. void delay_ms(uint32_t time);
  18. /*!
  19. \brief main function
  20. \param[in] none
  21. \param[out] none
  22. \retval none
  23. */
  24. int main(void)
  25. {
  26. SysTick_Config(72000000/1000);
  27. /* configure leds */
  28. led_config();
  29. /* configure CMP0 and CMP1 */
  30. cmp_config();
  31. /* configure CMP0 and CMP1 */
  32. check_state();
  33. while(1)
  34. {
  35. /* input voltage is over the thresholds: higher and lower thresholds */
  36. if(STATE_OVER_THRESHOLD == check_state()){
  37. gd_eval_led_on(LED1);
  38. gd_eval_led_off(LED2);
  39. gd_eval_led_on(LED3);
  40. gd_eval_led_off(LED4);
  41. }
  42. /* input voltage is within the thresholds: higher and lower thresholds */
  43. if(STATE_WITHIN_THRESHOLD == check_state()){
  44. delay_ms(500);
  45. if(STATE_WITHIN_THRESHOLD == check_state()){
  46. gd_eval_led_off(LED1);
  47. gd_eval_led_off(LED2);
  48. gd_eval_led_off(LED3);
  49. gd_eval_led_off(LED4);
  50. /* enter deepsleep mode */
  51. deepsleep_mode_config();
  52. }
  53. }
  54. /* input voltage is under the thresholds: higher and lower thresholds */
  55. if(STATE_UNDER_THRESHOLD == check_state()){
  56. gd_eval_led_off(LED1);
  57. gd_eval_led_on(LED2);
  58. gd_eval_led_off(LED3);
  59. gd_eval_led_on(LED4);
  60. }
  61. }
  62. }
  63. /*!
  64. \brief configure comparator
  65. \param[in] none
  66. \param[out] none
  67. \retval none
  68. */
  69. void cmp_config(void)
  70. {
  71. /* enable GPIOA clock */
  72. rcu_periph_clock_enable(RCU_GPIOA);
  73. /* configure PA1 as comparator input */
  74. gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_1);
  75. gpio_mode_set(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_PULLUP, GPIO_PIN_1);
  76. /* enable comparator clock */
  77. rcu_periph_clock_enable(RCU_CFGCMP);
  78. /* configure comparator channel0 */
  79. cmp_mode_init(CMP0, CMP_LOWSPEED, CMP_VREFINT, CMP_HYSTERESIS_HIGH);
  80. cmp_output_init(CMP0, CMP_OUTPUT_NONE, CMP_OUTPUT_POLARITY_NOINVERTED);
  81. /* configure comparator channel1 */
  82. cmp_mode_init(CMP1, CMP_LOWSPEED, CMP_1_2VREFINT, CMP_HYSTERESIS_HIGH);
  83. cmp_output_init(CMP1, CMP_OUTPUT_NONE, CMP_OUTPUT_POLARITY_NOINVERTED);
  84. /* configure exti line */
  85. exti_init(EXTI_21, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
  86. exti_init(EXTI_22, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
  87. /* configure ADC_CMP nvic */
  88. nvic_irq_enable(ADC_CMP_IRQn, 0, 0);
  89. /* enable comparator window */
  90. cmp_window_enable();
  91. /* enable comparator channels */
  92. cmp_enable(CMP0);
  93. cmp_enable(CMP1);
  94. }
  95. /*!
  96. \brief configure deepsleep mode
  97. \param[in] none
  98. \param[out] none
  99. \retval none
  100. */
  101. void deepsleep_mode_config(void)
  102. {
  103. /* enable pmu clock */
  104. rcu_periph_clock_enable(RCU_PMU);
  105. /* enter to deepsleep mode */
  106. pmu_to_deepsleepmode(PMU_LDO_LOWPOWER, WFI_CMD);
  107. }
  108. /*!
  109. \brief check comparator output state
  110. \param[in] none
  111. \param[out] none
  112. \retval cmp_state
  113. */
  114. cmp_state_enum check_state(void)
  115. {
  116. cmp_state_enum state;
  117. /* check if cmp0 output level is high and cmp1 output level is high */
  118. if ((cmp_output_level_get(CMP0) == CMP_OUTPUTLEVEL_HIGH)
  119. && (cmp_output_level_get(CMP1) == CMP_OUTPUTLEVEL_HIGH)){
  120. state = STATE_OVER_THRESHOLD;
  121. }
  122. /* check if cmp0 output level is low and cmp1 output level is high */
  123. if ((cmp_output_level_get(CMP0) == CMP_OUTPUTLEVEL_LOW)
  124. && (cmp_output_level_get(CMP1) == CMP_OUTPUTLEVEL_HIGH)){
  125. state = STATE_WITHIN_THRESHOLD;
  126. }
  127. /* check if cmp0 output level is low and cmp1 output level is low */
  128. if ((cmp_output_level_get(CMP0) == CMP_OUTPUTLEVEL_LOW)
  129. && (cmp_output_level_get(CMP1) == CMP_OUTPUTLEVEL_LOW)){
  130. state = STATE_UNDER_THRESHOLD;
  131. }
  132. return state;
  133. }
  134. /*!
  135. \brief delay function
  136. \param[in] none
  137. \param[out] none
  138. \retval none
  139. */
  140. void delay_ms(uint32_t time)
  141. {
  142. delayms = time;
  143. while(delayms);
  144. }
  145. /*!
  146. \brief configure the leds
  147. \param[in] none
  148. \param[out] none
  149. \retval none
  150. */
  151. void led_config(void)
  152. {
  153. gd_eval_led_init (LED1);
  154. gd_eval_led_init (LED2);
  155. gd_eval_led_init (LED3);
  156. gd_eval_led_init (LED4);
  157. }