main.c 7.7 KB

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