main.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*! \mainpage SIR firmware documentation
  2. *
  3. * \section intro Introduction
  4. * A collection of HTML-files has been generated using the documentation in the sourcefiles to
  5. * allow the developer to browse through the technical documentation of this project.
  6. * \par
  7. * \note these HTML files are automatically generated (using DoxyGen) and all modifications in the
  8. * documentation should be done via the sourcefiles.
  9. */
  10. /*! \file
  11. * COPYRIGHT (C) STREAMIT BV 2010
  12. * \date 19 december 2003
  13. */
  14. #define LOG_MODULE LOG_MAIN_MODULE
  15. /*--------------------------------------------------------------------------*/
  16. /* Include files */
  17. /*--------------------------------------------------------------------------*/
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <time.h>
  21. #include <sys/thread.h>
  22. #include <sys/timer.h>
  23. #include <sys/version.h>
  24. #include <dev/irqreg.h>
  25. // Jordy: Please keep this in alphabetical order!
  26. #include "display.h"
  27. #include "displayHandler.h"
  28. #include "eeprom.h"
  29. #include "flash.h"
  30. #include "keyboard.h"
  31. #include "led.h"
  32. #include "log.h"
  33. #include "mmc.h"
  34. #include "network.h"
  35. #include "ntp.h"
  36. #include "portio.h"
  37. #include "remcon.h"
  38. #include "rtc.h"
  39. #include "spidrv.h"
  40. #include "system.h"
  41. #include "uart0driver.h"
  42. #include "watchdog.h"
  43. /*-------------------------------------------------------------------------*/
  44. /* global variable definitions */
  45. /*-------------------------------------------------------------------------*/
  46. /*-------------------------------------------------------------------------*/
  47. /* local variable definitions */
  48. /*-------------------------------------------------------------------------*/
  49. /*-------------------------------------------------------------------------*/
  50. /* local routines (prototyping) */
  51. /*-------------------------------------------------------------------------*/
  52. static void SysMainBeatInterrupt(void*);
  53. static void SysControlMainBeat(u_char);
  54. /*-------------------------------------------------------------------------*/
  55. /* Stack check variables placed in .noinit section */
  56. /*-------------------------------------------------------------------------*/
  57. /*!
  58. * \addtogroup System
  59. */
  60. /*@{*/
  61. /*-------------------------------------------------------------------------*/
  62. /* start of code */
  63. /*-------------------------------------------------------------------------*/
  64. /* ����������������������������������������������������������������������� */
  65. /*!
  66. * \brief ISR MainBeat Timer Interrupt (Timer 2 for Mega128, Timer 0 for Mega256).
  67. *
  68. * This routine is automatically called during system
  69. * initialization.
  70. *
  71. * resolution of this Timer ISR is 4,448 msecs
  72. *
  73. * \param *p not used (might be used to pass parms from the ISR)
  74. */
  75. /* ����������������������������������������������������������������������� */
  76. static void SysMainBeatInterrupt(void *p)
  77. {
  78. /*
  79. * scan for valid keys AND check if a MMCard is inserted or removed
  80. */
  81. KbScan();
  82. CardCheckCard();
  83. }
  84. /* ����������������������������������������������������������������������� */
  85. /*!
  86. * \brief Initialise Digital IO
  87. * init inputs to '0', outputs to '1' (DDRxn='0' or '1')
  88. *
  89. * Pull-ups are enabled when the pin is set to input (DDRxn='0') and then a '1'
  90. * is written to the pin (PORTxn='1')
  91. */
  92. /* ����������������������������������������������������������������������� */
  93. void SysInitIO(void)
  94. {
  95. /*
  96. * Port B: VS1011, MMC CS/WP, SPI
  97. * output: all, except b3 (SPI Master In)
  98. * input: SPI Master In
  99. * pull-up: none
  100. */
  101. outp(0xF7, DDRB);
  102. /*
  103. * Port C: Address bus
  104. */
  105. /*
  106. * Port D: LCD_data, Keypad Col 2 & Col 3, SDA & SCL (TWI)
  107. * output: Keyboard colums 2 & 3
  108. * input: LCD_data, SDA, SCL (TWI)
  109. * pull-up: LCD_data, SDA & SCL
  110. */
  111. outp(0x0C, DDRD);
  112. outp((inp(PORTD) & 0x0C) | 0xF3, PORTD);
  113. /*
  114. * Port E: CS Flash, VS1011 (DREQ), RTL8019, LCD BL/Enable, IR, USB Rx/Tx
  115. * output: CS Flash, LCD BL/Enable, USB Tx
  116. * input: VS1011 (DREQ), RTL8019, IR
  117. * pull-up: USB Rx
  118. */
  119. outp(0x8E, DDRE);
  120. outp((inp(PORTE) & 0x8E) | 0x01, PORTE);
  121. /*
  122. * Port F: Keyboard_Rows, JTAG-connector, LED, LCD RS/RW, MCC-detect
  123. * output: LCD RS/RW, LED
  124. * input: Keyboard_Rows, MCC-detect
  125. * pull-up: Keyboard_Rows, MCC-detect
  126. * note: Key row 0 & 1 are shared with JTAG TCK/TMS. Cannot be used concurrent
  127. */
  128. #ifndef USE_JTAG
  129. sbi(JTAG_REG, JTD); // disable JTAG interface to be able to use all key-rows
  130. sbi(JTAG_REG, JTD); // do it 2 times - according to requirements ATMEGA128 datasheet: see page 256
  131. #endif //USE_JTAG
  132. outp(0x0E, DDRF);
  133. outp((inp(PORTF) & 0x0E) | 0xF1, PORTF);
  134. /*
  135. * Port G: Keyboard_cols, Bus_control
  136. * output: Keyboard_cols
  137. * input: Bus Control (internal control)
  138. * pull-up: none
  139. */
  140. outp(0x18, DDRG);
  141. }
  142. /* ����������������������������������������������������������������������� */
  143. /*!
  144. * \brief Starts or stops the 4.44 msec mainbeat of the system
  145. * \param OnOff indicates if the mainbeat needs to start or to stop
  146. */
  147. /* ����������������������������������������������������������������������� */
  148. static void SysControlMainBeat(u_char OnOff)
  149. {
  150. int nError = 0;
  151. if (OnOff==ON)
  152. {
  153. nError = NutRegisterIrqHandler(&OVERFLOW_SIGNAL, SysMainBeatInterrupt, NULL);
  154. if (nError == 0)
  155. {
  156. init_8_bit_timer();
  157. }
  158. }
  159. else
  160. {
  161. // disable overflow interrupt
  162. disable_8_bit_timer_ovfl_int();
  163. }
  164. }
  165. int timer(time_t start){
  166. time_t diff = time(0) - start;
  167. return diff;
  168. }
  169. int checkOffPressed(){
  170. if (KbScan() < -1){
  171. LcdBackLight(LCD_BACKLIGHT_ON);
  172. return 1;
  173. } else {
  174. return 0;
  175. }
  176. }
  177. /* ����������������������������������������������������������������������� */
  178. /*!
  179. * \brief Main entry of the SIR firmware
  180. *
  181. * All the initialisations before entering the for(;;) loop are done BEFORE
  182. * the first key is ever pressed. So when entering the Setup (POWER + VOLMIN) some
  183. * initialisatons need to be done again when leaving the Setup because new values
  184. * might be current now
  185. *
  186. * \return \b never returns
  187. */
  188. /* ����������������������������������������������������������������������� */
  189. THREAD(StartupInit, arg)
  190. {
  191. NetworkInit();
  192. NtpSync();
  193. NutThreadExit();
  194. }
  195. int main(void)
  196. {
  197. time_t start;
  198. int running = 0;
  199. /*
  200. * First disable the watchdog
  201. */
  202. WatchDogDisable();
  203. NutDelay(100);
  204. SysInitIO();
  205. SPIinit();
  206. LedInit();
  207. LcdLowLevelInit();
  208. Uart0DriverInit();
  209. Uart0DriverStart();
  210. LogInit();
  211. CardInit();
  212. X12Init();
  213. LcdBackLight(LCD_BACKLIGHT_ON);
  214. NtpInit();
  215. NutThreadCreate("BackgroundThread", StartupInit, NULL, 512);
  216. /** Quick fix for turning off the display after 10 seconds boot */
  217. start = time(0);
  218. running = 1;
  219. /*
  220. * Kroeske: sources in rtc.c en rtc.h
  221. */
  222. if (At45dbInit()==AT45DB041B)
  223. {
  224. // ......
  225. }
  226. RcInit();
  227. KbInit();
  228. SysControlMainBeat(ON); // enable 4.4 msecs hartbeat interrupt
  229. /*
  230. * Increase our priority so we can feed the watchdog.
  231. */
  232. NutThreadSetPriority(1);
  233. /* Enable global interrupts */
  234. sei();
  235. for (;;)
  236. {
  237. //Check if a button is pressed
  238. if (checkOffPressed() == 1){
  239. start = time(0);
  240. running = 1;
  241. }
  242. //Check if background LED is on, and compare to timer
  243. if (running == 1){
  244. if (timer(start) >= 10){
  245. running = 0;
  246. LcdBackLight(LCD_BACKLIGHT_OFF);
  247. }
  248. }
  249. displayDate(0);
  250. displayTime(1);
  251. WatchDogRestart();
  252. }
  253. return(0); // never reached, but 'main()' returns a non-void, so.....
  254. }