main.c 3.3 KB

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