main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. /* ����������������������������������������������������������������������� */
  162. /*!
  163. * \brief Main entry of the SIR firmware
  164. *
  165. * All the initialisations before entering the for(;;) loop are done BEFORE
  166. * the first key is ever pressed. So when entering the Setup (POWER + VOLMIN) some
  167. * initialisatons need to be done again when leaving the Setup because new values
  168. * might be current now
  169. *
  170. * \return \b never returns
  171. */
  172. /* ����������������������������������������������������������������������� */
  173. int main(void)
  174. {
  175. int i;
  176. /*
  177. * Kroeske: time struct uit nut/os time.h (http://www.ethernut.de/api/time_8h-source.html)
  178. *
  179. */
  180. tm gmt;
  181. /*
  182. * Kroeske: Ook kan 'struct _tm gmt' Zie bovenstaande link
  183. */
  184. /*
  185. * First disable the watchdog
  186. */
  187. WatchDogDisable();
  188. NutDelay(100);
  189. SysInitIO();
  190. SPIinit();
  191. LedInit();
  192. LcdLowLevelInit();
  193. Uart0DriverInit();
  194. Uart0DriverStart();
  195. LogInit();
  196. LogMsg_P(LOG_INFO, PSTR("Hello World"));
  197. CardInit();
  198. NetworkInit();
  199. /*
  200. * Kroeske: sources in rtc.c en rtc.h
  201. */
  202. X12Init();
  203. if (X12RtcGetClock(&gmt) == 0)
  204. {
  205. LogMsg_P(LOG_INFO, PSTR("RTC time [%02d:%02d:%02d]"), gmt.tm_hour, gmt.tm_min, gmt.tm_sec );
  206. }
  207. if (At45dbInit()==AT45DB041B)
  208. {
  209. // ......
  210. }
  211. RcInit();
  212. KbInit();
  213. SysControlMainBeat(ON); // enable 4.4 msecs hartbeat interrupt
  214. /*
  215. * Increase our priority so we can feed the watchdog.
  216. */
  217. NutThreadSetPriority(1);
  218. /* Enable global interrupts */
  219. sei();
  220. /**
  221. for (;;)
  222. {
  223. NutSleep(100);
  224. if( !((t++)%15) )
  225. {
  226. LogMsg_P(LOG_INFO, PSTR("Yes!, I'm alive ... [%d]"),t);
  227. LedControl(LED_TOGGLE);
  228. if( x )
  229. {
  230. LcdBackLight(LCD_BACKLIGHT_ON);
  231. x = 0;
  232. }
  233. else
  234. {
  235. LcdBackLight(LCD_BACKLIGHT_OFF);
  236. x = 1;
  237. }
  238. }
  239. WatchDogRestart();
  240. }
  241. **/
  242. LedControl(LED_ON);
  243. LcdBackLight(LCD_BACKLIGHT_OFF);
  244. char string[1000];
  245. strcpy(string, "RADIO TEST");
  246. LcdBackLight(LCD_BACKLIGHT_ON);
  247. LcdChar('test');
  248. for(;;){
  249. u_char x = KbGetKey();
  250. if(x == KEY_OK){
  251. LcdBackLight(LCD_BACKLIGHT_ON);
  252. //NutSleep(3000); // dit weer terug zetten als je opdracht 1 wil tonen.
  253. }
  254. if(x == KEY_ESC){ // dit uit commenten als je opdracht 1 wil tonen.
  255. LcdBackLight(LCD_BACKLIGHT_OFF); // ^
  256. } // ^
  257. if(x == KEY_ALT){
  258. for(i = 0; i < strlen(string); i++) {
  259. LcdChar(string[i]);
  260. }
  261. }
  262. if(x == KEY_POWER){
  263. }
  264. }
  265. return(0); // never reached, but 'main()' returns a non-void, so.....
  266. }
  267. /* ---------- end of module ------------------------------------------------ */
  268. /*@}*/