main.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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();
  31. led_on(0);
  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("Initializing digital-potmeters\r\n");
  47. pot_init();
  48. printf("Initializing DAC...\r\n");
  49. dac_init();
  50. printf("Activating I2C bus...\r\n");
  51. i2c1_setup();
  52. i2c_display_init();
  53. printf("Display initialized.\r\n");
  54. printf("Displaying splash screen...\r\n");
  55. ui_init();
  56. printf("UI initialized.\r\n");
  57. //djhero_init();
  58. timer_init();
  59. printf("System up and running.\r\n\n\n");
  60. }
  61. static void (*process_input_callback)(uint8_t press, int hold) = NULL;
  62. static void (*keepalive_callback)(void) = NULL;
  63. void set_input_callback(void (*cb)(uint8_t press, int hold))
  64. {
  65. process_input_callback = cb;
  66. }
  67. void clear_input_callback(void)
  68. {
  69. process_input_callback = NULL;
  70. }
  71. void set_keepalive(void (*cb)(void))
  72. {
  73. keepalive_callback = cb;
  74. }
  75. void clear_keepalive(void)
  76. {
  77. keepalive_callback = NULL;
  78. }
  79. void process_button(uint8_t press, int hold)
  80. {
  81. static uint32_t last_b_time = 0;
  82. if (jiffies - last_b_time < 100)
  83. return;
  84. last_b_time = jiffies;
  85. if (process_input_callback)
  86. process_input_callback(press, hold);
  87. else
  88. ui_button_press(press, hold);
  89. }
  90. extern void gettime(uint32_t *s, uint32_t *us);
  91. int main(void) {
  92. int hb = 1;
  93. int fs_on = 0;
  94. int ret;
  95. char uc;
  96. volatile uint32_t poll_time = 0;
  97. uint32_t bstatus = 0;
  98. const uint32_t hb_len = 20;
  99. const uint32_t hb_pulse = 5;
  100. bootlevel_0();
  101. bootlevel_1();
  102. //pin_exti_start_read();
  103. while(1) {
  104. hb = 1;
  105. led_on(LED);
  106. poll_time = jiffies;
  107. if (!keepalive_callback)
  108. ui_keepalive(hb_len);
  109. else
  110. keepalive_callback();
  111. do {
  112. WFI();
  113. ret = button_poll(process_button);
  114. uc = uart_read();
  115. if (uc)
  116. process_button(uc, 0);
  117. if (jiffies - poll_time > hb_pulse) {
  118. if (hb) {
  119. uint32_t s, us;
  120. hb = 0;
  121. led_off(LED);
  122. // gettime(&s, &us);
  123. // printf("TIME: %u.%05u\r\n", s, us);
  124. }
  125. }
  126. } while ((jiffies - poll_time) < hb_len);
  127. }
  128. return 0;
  129. }