main.c 8.9 KB

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