main.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Xytronic LF-1600
  3. * Open Source firmware
  4. *
  5. * Copyright (c) 2015-2017 Michael Buesch <m@bues.ch>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. #include "main.h"
  22. #include "measure.h"
  23. #include "measure_current.h"
  24. #include "measure_temp.h"
  25. #include "controller_current.h"
  26. #include "controller_temp.h"
  27. #include "pwm_current.h"
  28. #include "timer.h"
  29. #include "util.h"
  30. #include "debug_uart.h"
  31. #include "display.h"
  32. #include "buttons.h"
  33. #include "menu.h"
  34. #include "settings.h"
  35. #include "presets.h"
  36. #include <avr/io.h>
  37. #include <avr/wdt.h>
  38. /* Configure unused package pins. */
  39. static void unused_pins_init(void)
  40. {
  41. /* Configure unused pins as inputs with pull up
  42. * to get a well defined level on the input driver.
  43. */
  44. /* PB5 */
  45. DDRB &= (uint8_t)~(1u << DDB5);
  46. PORTB |= 1u << PB5;
  47. /* PB6 */
  48. DDRB &= (uint8_t)~(1u << DDB6);
  49. PORTB |= 1u << PB6;
  50. /* PC0 */
  51. DDRC &= (uint8_t)~(1u << DDC0);
  52. PORTC |= 1u << PC0;
  53. }
  54. /* Early boot routine. Runs before main().
  55. * This function does not have a stack frame.
  56. */
  57. void early_init(void) __attribute__((naked, section(".init3"), used));
  58. void early_init(void)
  59. {
  60. /* Reconfigure watchdog. */
  61. MCUSR = 0;
  62. wdt_enable(WDTO_2S);
  63. }
  64. /* One iteration of the main loop. */
  65. #ifndef SIMULATOR
  66. static
  67. #endif
  68. void main_loop_once(void)
  69. {
  70. wdt_reset();
  71. /* Handle measurement results (if any).
  72. * This will also run the controllers, if needed.
  73. */
  74. measure_work();
  75. /* Handle user interface events. */
  76. menu_work();
  77. display_work();
  78. buttons_work();
  79. settings_work();
  80. }
  81. #ifndef RUN_MAIN_LOOP
  82. # define RUN_MAIN_LOOP 1
  83. #endif
  84. /* Main entry point. */
  85. int main(void) _mainfunc;
  86. int main(void)
  87. {
  88. /* Disable all interrupt sources. */
  89. TIMSK0 = 0u;
  90. TIMSK1 = 0u;
  91. /* Initialize basic system functions. */
  92. timer_init();
  93. buttons_init();
  94. debug_uart_init();
  95. settings_init();
  96. display_init();
  97. /* Initialize the measurement subsystem */
  98. measure_init();
  99. meascurr_init();
  100. meastemp_init();
  101. /* Initialize the controllers. */
  102. contrcurr_init();
  103. presets_init();
  104. contrtemp_init();
  105. /* Initialize the current actuator. */
  106. pwmcurr_init();
  107. /* Initialize the user interface. */
  108. menu_init();
  109. /* Configure unused package pins. */
  110. unused_pins_init();
  111. /* Startup measurements to get everything going. */
  112. measure_start();
  113. wdt_enable(WDTO_250MS);
  114. irq_enable();
  115. while (RUN_MAIN_LOOP)
  116. main_loop_once();
  117. return 0;
  118. }