main.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * (c) danielinux 2019
  3. * GPLv.2
  4. *
  5. * See LICENSE for details
  6. */
  7. #include <string.h>
  8. #include <stdio.h>
  9. #include "system.h"
  10. #include "led.h"
  11. #include "display.h"
  12. #include "spi_flash.h"
  13. #include "uart.h"
  14. #include "systick.h"
  15. #include "ui.h"
  16. #include "button.h"
  17. #include "adc.h"
  18. extern uint32_t cpu_freq;
  19. #define BLINK_INTERVAL 500
  20. static int sdcard_detected = 0;
  21. static void bootlevel_0(void)
  22. {
  23. clock_pll_on();
  24. uart2_setup(115200, 8, 0, 1);
  25. systick_enable();
  26. WFI();
  27. printf("Console on USART2 Enabled.\r\n");
  28. printf("\r\n=====================\r\n");
  29. printf("Entering bootlevel 0.\r\n");
  30. //led_setup(LED);
  31. //led_on(LED);
  32. printf("System clock started.\r\n");
  33. button_init();
  34. //pin_exti_init();
  35. printf("Buttons initialized.\r\n");
  36. }
  37. extern void timer_init(void);
  38. static void bootlevel_1(void)
  39. {
  40. uint16_t spiflash_vendor;
  41. int sdcard_present;
  42. printf("=====================\r\n");
  43. printf("Entering bootlevel 1.\r\n");
  44. printf("Initializing ADC...\r\n");
  45. adc_init();
  46. printf("Activating I2C bus...\r\n");
  47. i2c_display_init();
  48. printf("Display initialized.\r\n");
  49. printf("Displaying splash screen...\r\n");
  50. ui_init();
  51. printf("UI initialized.\r\n");
  52. timer_init();
  53. printf("System up and running.\r\n\n\n");
  54. }
  55. static void (*process_input_callback)(uint8_t press, int hold) = NULL;
  56. static void (*keepalive_callback)(void) = NULL;
  57. void set_input_callback(void (*cb)(uint8_t press, int hold))
  58. {
  59. process_input_callback = cb;
  60. }
  61. void clear_input_callback(void)
  62. {
  63. process_input_callback = NULL;
  64. }
  65. void set_keepalive(void (*cb)(void))
  66. {
  67. keepalive_callback = cb;
  68. }
  69. void clear_keepalive(void)
  70. {
  71. keepalive_callback = NULL;
  72. }
  73. void process_button(uint8_t press, int hold)
  74. {
  75. if (process_input_callback)
  76. process_input_callback(press, hold);
  77. else
  78. ui_button_press(press, hold);
  79. }
  80. extern void gettime(uint32_t *s, uint32_t *us);
  81. int main(void) {
  82. int hb = 1;
  83. int fs_on = 0;
  84. int ret;
  85. volatile uint32_t poll_time = 0;
  86. uint32_t bstatus = 0;
  87. const uint32_t hb_len = 400;
  88. const uint32_t hb_pulse = 5;
  89. bootlevel_0();
  90. bootlevel_1();
  91. //pin_exti_start_read();
  92. while(1) {
  93. hb = 1;
  94. //led_on(LED);
  95. poll_time = jiffies;
  96. if (!keepalive_callback)
  97. ui_keepalive(hb_len);
  98. else
  99. keepalive_callback();
  100. do {
  101. WFI();
  102. ret = button_poll(process_button);
  103. if (jiffies - poll_time > hb_pulse) {
  104. if (hb) {
  105. uint32_t s, us;
  106. hb = 0;
  107. //led_off(LED);
  108. // gettime(&s, &us);
  109. // printf("TIME: %u.%05u\r\n", s, us);
  110. }
  111. }
  112. } while ((jiffies - poll_time) < hb_len);
  113. }
  114. return 0;
  115. }