main.c 7.7 KB

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