main.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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) SaltyRadio 2016
  12. * \date 20-02-2016
  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 "typedefs.h"
  39. #include <time.h>
  40. #include "rtc.h"
  41. #include "alarm.h"
  42. #include "ntp.h"
  43. #include "httpstream.h"
  44. #include "contentparser.h"
  45. #include "gotosleep.h"
  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. * \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. 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. * \brief Initialise Digital IO
  81. * init inputs to '0', outputs to '1' (DDRxn='0' or '1')
  82. *
  83. * Pull-ups are enabled when the pin is set to input (DDRxn='0') and then a '1'
  84. * is written to the pin (PORTxn='1')
  85. */
  86. void SysInitIO(void)
  87. {
  88. /*
  89. * Port B: VS1011, MMC CS/WP, SPI
  90. * output: all, except b3 (SPI Master In)
  91. * input: SPI Master In
  92. * pull-up: none
  93. */
  94. outp(0xF7, DDRB);
  95. /*
  96. * Port C: Address bus
  97. */
  98. /*
  99. * Port D: LCD_data, Keypad Col 2 & Col 3, SDA & SCL (TWI)
  100. * output: Keyboard colums 2 & 3
  101. * input: LCD_data, SDA, SCL (TWI)
  102. * pull-up: LCD_data, SDA & SCL
  103. */
  104. outp(0x0C, DDRD);
  105. outp((inp(PORTD) & 0x0C) | 0xF3, PORTD);
  106. /*
  107. * Port E: CS Flash, VS1011 (DREQ), RTL8019, LCD BL/Enable, IR, USB Rx/Tx
  108. * output: CS Flash, LCD BL/Enable, USB Tx
  109. * input: VS1011 (DREQ), RTL8019, IR
  110. * pull-up: USB Rx
  111. */
  112. outp(0x8E, DDRE);
  113. outp((inp(PORTE) & 0x8E) | 0x01, PORTE);
  114. /*
  115. * Port F: Keyboard_Rows, JTAG-connector, LED, LCD RS/RW, MCC-detect
  116. * output: LCD RS/RW, LED
  117. * input: Keyboard_Rows, MCC-detect
  118. * pull-up: Keyboard_Rows, MCC-detect
  119. * note: Key row 0 & 1 are shared with JTAG TCK/TMS. Cannot be used concurrent
  120. */
  121. #ifndef USE_JTAG
  122. sbi(JTAG_REG, JTD); // disable JTAG interface to be able to use all key-rows
  123. sbi(JTAG_REG, JTD); // do it 2 times - according to requirements ATMEGA128 datasheet: see page 256
  124. #endif //USE_JTAG
  125. outp(0x0E, DDRF);
  126. outp((inp(PORTF) & 0x0E) | 0xF1, PORTF);
  127. /*
  128. * Port G: Keyboard_cols, Bus_control
  129. * output: Keyboard_cols
  130. * input: Bus Control (internal control)
  131. * pull-up: none
  132. */
  133. outp(0x18, DDRG);
  134. }
  135. /*!
  136. * \brief Starts or stops the 4.44 msec mainbeat of the system
  137. * \param OnOff indicates if the mainbeat needs to start or to stop
  138. */
  139. static void SysControlMainBeat(u_char OnOff)
  140. {
  141. int nError = 0;
  142. if (OnOff==ON)
  143. {
  144. nError = NutRegisterIrqHandler(&OVERFLOW_SIGNAL, SysMainBeatInterrupt, NULL);
  145. if (nError == 0)
  146. {
  147. init_8_bit_timer();
  148. }
  149. }
  150. else
  151. {
  152. // disable overflow interrupt
  153. disable_8_bit_timer_ovfl_int();
  154. }
  155. }
  156. /*-------------------------------------------------------------------------*/
  157. /* global variable definitions */
  158. /*-------------------------------------------------------------------------*/
  159. int isAlarmSyncing;
  160. int initialized;
  161. int running = 0;
  162. unsigned char VOL = 64;
  163. /*-------------------------------------------------------------------------*/
  164. /* local variable definitions */
  165. /*-------------------------------------------------------------------------*/
  166. /*-------------------------------------------------------------------------*/
  167. /* Thread init */
  168. /*-------------------------------------------------------------------------*/
  169. THREAD(StartupInit, arg)
  170. {
  171. NetworkInit();
  172. NtpSync();
  173. initialized = 1;
  174. NutThreadExit();
  175. }
  176. THREAD(NTPSync, arg)
  177. {
  178. for(;;)
  179. {
  180. if(initialized && (hasNetworkConnection() == true))
  181. {
  182. while(isAlarmSyncing)
  183. {
  184. NutSleep(2000);
  185. }
  186. NtpSync();
  187. }
  188. NutSleep(86400000);
  189. }
  190. }
  191. THREAD(AlarmSync, arg)
  192. {
  193. for(;;)
  194. {
  195. if(initialized && (hasNetworkConnection() == true))
  196. {
  197. isAlarmSyncing = 1;
  198. char url[43];
  199. sprintf(url, "%s%s", "/getAlarmen.php?radiomac=", getMacAdress());
  200. httpGet(url, parseAlarmJson);
  201. isAlarmSyncing = 0;
  202. }
  203. NutSleep(3000);
  204. }
  205. NutThreadExit();
  206. }
  207. /*-------------------------------------------------------------------------*/
  208. /* Global functions */
  209. /*-------------------------------------------------------------------------*/
  210. int timer(time_t start){
  211. time_t diff = time(0) - start;
  212. return diff;
  213. }
  214. long timerStruct(struct _tm s){
  215. struct _tm ct;
  216. X12RtcGetClock(&ct);
  217. long stime = (s.tm_hour * 3600) + (s.tm_min * 60) + s.tm_sec;
  218. long ctime = (ct.tm_hour * 3600) + (ct.tm_min * 60) + ct.tm_sec;
  219. return ctime - stime;
  220. }
  221. int checkOffPressed(){
  222. if (KbGetKey() != KEY_UNDEFINED){
  223. LcdBackLight(LCD_BACKLIGHT_ON);
  224. return 1;
  225. } else {
  226. return 0;
  227. }
  228. }
  229. int main(void)
  230. {
  231. initialized = 0;
  232. int VOL2 = 127;
  233. struct _tm timeCheck;
  234. struct _tm start;
  235. int idx = 0;
  236. WatchDogDisable();
  237. NutDelay(100);
  238. SysInitIO();
  239. SPIinit();
  240. LedInit();
  241. LcdLowLevelInit();
  242. Uart0DriverInit();
  243. Uart0DriverStart();
  244. LogInit();
  245. CardInit();
  246. X12Init();
  247. VsPlayerInit();
  248. NtpInit();
  249. NutThreadCreate("BackgroundThread", StartupInit, NULL, 1024);
  250. NutThreadCreate("BackgroundThread", AlarmSync, NULL, 2500);
  251. NutThreadCreate("BackgroundThread", NTPSync, NULL, 700);
  252. /** Quick fix for turning off the display after 10 seconds boot */
  253. RcInit();
  254. KbInit();
  255. SysControlMainBeat(ON); // enable 4.4 msecs heartbeat interrupt
  256. /*
  257. * Increase our priority so we can feed the watchdog.
  258. */
  259. NutThreadSetPriority(1);
  260. /* Enable global interrupts */
  261. sei();
  262. LcdBackLight(LCD_BACKLIGHT_OFF);
  263. X12RtcGetClock(&timeCheck);
  264. X12RtcGetClock(&start);
  265. for (;;)
  266. {
  267. //printf("running = %d, time = %d\n", running, timerStruct(start));
  268. if (timerStruct(start) < 0){
  269. X12RtcGetClock(&start);
  270. }
  271. if (timerStruct(timeCheck) < 0){
  272. X12RtcGetClock(&timeCheck);
  273. }
  274. //Check if a button is pressed
  275. if (checkOffPressed() == 1){
  276. X12RtcGetClock(&start);
  277. running = 1;
  278. LcdBackLight(LCD_BACKLIGHT_ON);
  279. }
  280. //Check if background LED is on, and compare to timer
  281. if (running == 1){
  282. if (timerStruct(start) >= 10 || running > 1){
  283. running = 0;
  284. LcdBackLight(LCD_BACKLIGHT_OFF);
  285. }
  286. }
  287. VOL = VOL2;
  288. if(KbGetKey() == KEY_DOWN)
  289. {
  290. NutSleep(150);
  291. X12RtcGetClock(&timeCheck);
  292. if(VOL > 8){
  293. VOL -= 8;
  294. VsSetVolume (127-VOL, 127-VOL);
  295. displayVolume(VOL/8);
  296. }
  297. }
  298. else if(KbGetKey() == KEY_UP)
  299. {
  300. NutSleep(150);
  301. X12RtcGetClock(&timeCheck);
  302. if(VOL < 128) {
  303. VOL += 8;
  304. VsSetVolume(128-VOL, 128-VOL);
  305. displayVolume(VOL/8);
  306. }
  307. }
  308. else if(KbGetKey() == KEY_LEFT)
  309. {
  310. NutSleep(150);
  311. VOL = 20;
  312. VsSetVolume(128-VOL, 128-VOL);
  313. X12RtcGetClock(&timeCheck);
  314. setSleep();
  315. }
  316. else if(KbGetKey() == KEY_RIGHT)
  317. {
  318. changeChanel();
  319. }
  320. else if(timerStruct(timeCheck) >= 5 && checkAlarms() == 1)
  321. {
  322. for (idx = 0; idx < 5; idx++){
  323. if (getState(idx) == 1){
  324. displayAlarm(0,1,idx);
  325. if (KbGetKey() == KEY_ESC){
  326. //NutDelay(50);
  327. handleAlarm(idx);
  328. //NutDelay(50);
  329. LcdBackLight(LCD_BACKLIGHT_OFF);
  330. stopStream();
  331. } else if (KbGetKey() == KEY_01 || KbGetKey() == KEY_02 || KbGetKey() == KEY_03 || KbGetKey() == KEY_04 || KbGetKey() == KEY_05 || KbGetKey() == KEY_ALT){
  332. setSnooze(idx);
  333. LcdBackLight(LCD_BACKLIGHT_OFF);
  334. stopStream();
  335. }
  336. }
  337. }
  338. }
  339. else if (timerStruct(timeCheck) >= 5){
  340. displayTime(0);
  341. displayDate(1);
  342. }
  343. checkSleep();
  344. VOL2 = VOL;
  345. WatchDogRestart();
  346. }
  347. return(0);
  348. }