main.c 9.5 KB

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