main.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #include "pot.h"
  19. #include "settings.h"
  20. #include "drums.h"
  21. #include "drone.h"
  22. #include "uart.h"
  23. #include "timer.h"
  24. extern uint32_t cpu_freq;
  25. #define BLINK_INTERVAL 500
  26. static int sdcard_detected = 0;
  27. static void bootlevel_0(void)
  28. {
  29. clock_pll_on();
  30. uart2_setup(115200, 8, 0, 1);
  31. systick_enable();
  32. WFI();
  33. settings_load();
  34. printf("Console on USART2 Enabled.\r\n");
  35. printf("\r\n=====================\r\n");
  36. printf("Entering bootlevel 0.\r\n");
  37. led_setup();
  38. led_on(0);
  39. printf("System clock started.\r\n");
  40. button_init();
  41. //pin_exti_init();
  42. printf("Buttons initialized.\r\n");
  43. }
  44. static void bootlevel_1(void)
  45. {
  46. uint16_t spiflash_vendor;
  47. int sdcard_present;
  48. printf("=====================\r\n");
  49. printf("Entering bootlevel 1.\r\n");
  50. printf("Initializing ADC...\r\n");
  51. adc_init();
  52. printf("Initializing digital-potmeters\r\n");
  53. pot_init();
  54. printf("Initializing DAC...\r\n");
  55. dac_init();
  56. printf("Activating I2C bus...\r\n");
  57. i2c1_setup();
  58. i2c_display_init();
  59. printf("Display initialized.\r\n");
  60. printf("Displaying splash screen...\r\n");
  61. ui_init();
  62. printf("UI initialized.\r\n");
  63. timer_init();
  64. drums_init();
  65. drone_init();
  66. drums_start();
  67. printf("System up and running.\r\n\n\n");
  68. }
  69. static void (*process_input_callback)(uint8_t press, int hold) = NULL;
  70. static void (*keepalive_callback)(void) = NULL;
  71. void set_input_callback(void (*cb)(uint8_t press, int hold))
  72. {
  73. process_input_callback = cb;
  74. }
  75. void clear_input_callback(void)
  76. {
  77. process_input_callback = NULL;
  78. }
  79. void set_keepalive(void (*cb)(void))
  80. {
  81. keepalive_callback = cb;
  82. }
  83. void clear_keepalive(void)
  84. {
  85. keepalive_callback = NULL;
  86. }
  87. void process_button(uint8_t press, int hold)
  88. {
  89. static uint32_t last_b_time = 0;
  90. if (jiffies - last_b_time < 100)
  91. return;
  92. last_b_time = jiffies;
  93. if (process_input_callback)
  94. process_input_callback(press, hold);
  95. else
  96. ui_button_press(press, hold);
  97. }
  98. extern void gettime(uint32_t *s, uint32_t *us);
  99. int main(void) {
  100. int hb = 1;
  101. int fs_on = 0;
  102. int ret;
  103. char uc;
  104. volatile uint32_t poll_time = 0;
  105. uint32_t bstatus = 0;
  106. const uint32_t hb_len = 20;
  107. const uint32_t hb_pulse = 5;
  108. bootlevel_0();
  109. bootlevel_1();
  110. //pin_exti_start_read();
  111. while(1) {
  112. hb = 1;
  113. led_on(LED);
  114. poll_time = jiffies;
  115. timer_poll();
  116. if (!keepalive_callback)
  117. ui_keepalive(hb_len);
  118. else
  119. keepalive_callback();
  120. do {
  121. WFI();
  122. ret = button_poll(process_button);
  123. uc = uart_read();
  124. if (uc)
  125. process_button(uc, 0);
  126. if (jiffies - poll_time > hb_pulse) {
  127. if (hb) {
  128. uint32_t s, us;
  129. hb = 0;
  130. led_off(LED);
  131. // gettime(&s, &us);
  132. // printf("TIME: %u.%05u\r\n", s, us);
  133. }
  134. }
  135. } while ((jiffies - poll_time) < hb_len);
  136. }
  137. return 0;
  138. }