main.c 11 KB

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