main.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. void displayDate(){
  161. struct _tm gmt;
  162. gmt = GetRTCTime();
  163. char str[13];
  164. sprintf(str, " %02d/%02d/%04d", gmt.tm_mday, gmt.tm_mon, gmt.tm_year+1900);
  165. LcdArrayLineOne(str,13);
  166. }
  167. void displayTime(){
  168. struct _tm gmt;
  169. gmt = GetRTCTime();
  170. char str[12];
  171. sprintf(str, " %02d:%02d:%02d", gmt.tm_hour, gmt.tm_min, gmt.tm_sec);
  172. LcdArrayLineTwo(str,12);
  173. }
  174. int timer(time_t start){
  175. time_t diff = time(0) - start;
  176. return diff;
  177. }
  178. int checkOffPressed(){
  179. if (KbScan() < -1){
  180. LcdBackLight(LCD_BACKLIGHT_ON);
  181. return 1;
  182. } else {
  183. return 0;
  184. }
  185. }
  186. /* ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ */
  187. /*!
  188. * \brief Main entry of the SIR firmware
  189. *
  190. * All the initialisations before entering the for(;;) loop are done BEFORE
  191. * the first key is ever pressed. So when entering the Setup (POWER + VOLMIN) some
  192. * initialisatons need to be done again when leaving the Setup because new values
  193. * might be current now
  194. *
  195. * \return \b never returns
  196. */
  197. /* ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ */
  198. int main(void)
  199. {
  200. time_t start;
  201. int running = 0;
  202. /*
  203. * Kroeske: time struct uit nut/os time.h (http://www.ethernut.de/api/time_8h-source.html)
  204. *
  205. */
  206. struct _tm gmt;
  207. /*
  208. * Kroeske: Ook kan 'struct _tm gmt' Zie bovenstaande link
  209. */
  210. /*
  211. * First disable the watchdog
  212. */
  213. WatchDogDisable();
  214. NutDelay(100);
  215. SysInitIO();
  216. SPIinit();
  217. LedInit();
  218. LcdLowLevelInit();
  219. Uart0DriverInit();
  220. Uart0DriverStart();
  221. LogInit();
  222. CardInit();
  223. /*
  224. * Kroeske: sources in rtc.c en rtc.h
  225. */
  226. X12Init();
  227. gmt = GetRTCTime();
  228. LogMsg_P(LOG_INFO, PSTR("RTC time [%02d:%02d:%02d]"), gmt.tm_hour, gmt.tm_min, gmt.tm_sec );
  229. if (At45dbInit()==AT45DB041B)
  230. {
  231. // ......
  232. }
  233. RcInit();
  234. KbInit();
  235. SysControlMainBeat(ON); // enable 4.4 msecs hartbeat interrupt
  236. /*
  237. * Increase our priority so we can feed the watchdog.
  238. */
  239. NutThreadSetPriority(1);
  240. /* Enable global interrupts */
  241. sei();
  242. for (;;)
  243. {
  244. //Check if a button is pressed
  245. if (checkOffPressed() == 1){
  246. start = time(0);
  247. running = 1;
  248. }
  249. //Check if background LED is on, and compare to timer
  250. if (running == 1){
  251. if (timer(start) >= 10){
  252. running = 0;
  253. LcdBackLight(LCD_BACKLIGHT_OFF);
  254. }
  255. }
  256. displayTime();
  257. displayDate();
  258. WatchDogRestart();
  259. }
  260. return(0); // never reached, but 'main()' returns a non-void, so.....
  261. }