main.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 <time.h>
  21. #include <sys/thread.h>
  22. #include <sys/timer.h>
  23. #include <sys/version.h>
  24. #include <dev/irqreg.h>
  25. // Note: Please keep the includes in alphabetical order! - Jordy
  26. #include "alarm.h"
  27. #include "contentparser.h"
  28. #include "display.h"
  29. #include "displayHandler.h"
  30. #include "keyboard.h"
  31. #include "led.h"
  32. #include "log.h"
  33. #include "mp3stream.h"
  34. #include "network.h"
  35. #include "ntp.h"
  36. #include "portio.h"
  37. #include "rtc.h"
  38. #include "spidrv.h"
  39. #include "system.h"
  40. #include "typedefs.h"
  41. #include "uart0driver.h"
  42. #include "watchdog.h"
  43. /*-------------------------------------------------------------------------*/
  44. /* local routines (prototyping) */
  45. /*-------------------------------------------------------------------------*/
  46. static void SysMainBeatInterrupt(void*);
  47. static void SysControlMainBeat(u_char);
  48. /*-------------------------------------------------------------------------*/
  49. /* Stack check variables placed in .noinit section */
  50. /*-------------------------------------------------------------------------*/
  51. /*!
  52. * \addtogroup System
  53. */
  54. /*@{*/
  55. /*-------------------------------------------------------------------------*/
  56. /* start of code */
  57. /*-------------------------------------------------------------------------*/
  58. /*!
  59. * \brief ISR MainBeat Timer Interrupt (Timer 2 for Mega128, Timer 0 for Mega256).
  60. *
  61. * This routine is automatically called during system
  62. * initialization.
  63. *
  64. * resolution of this Timer ISR is 4,448 msecs
  65. *
  66. * \param *p not used (might be used to pass parms from the ISR)
  67. */
  68. static void SysMainBeatInterrupt(void *p)
  69. {
  70. /*
  71. * scan for valid keys
  72. */
  73. KbScan();
  74. if(KbGetKey() != KEY_NO_KEY){
  75. LcdBackLight(LCD_BACKLIGHT_ON);
  76. }
  77. }
  78. /*!
  79. * \brief Initialise Digital IO
  80. * init inputs to '0', outputs to '1' (DDRxn='0' or '1')
  81. *
  82. * Pull-ups are enabled when the pin is set to input (DDRxn='0') and then a '1'
  83. * is written to the pin (PORTxn='1')
  84. */
  85. void SysInitIO(void)
  86. {
  87. /*
  88. * Port B: VS1011, MMC CS/WP, SPI
  89. * output: all, except b3 (SPI Master In)
  90. * input: SPI Master In
  91. * pull-up: none
  92. */
  93. outp(0xF7, DDRB);
  94. /*
  95. * Port C: Address bus
  96. */
  97. /*
  98. * Port D: LCD_data, Keypad Col 2 & Col 3, SDA & SCL (TWI)
  99. * output: Keyboard colums 2 & 3
  100. * input: LCD_data, SDA, SCL (TWI)
  101. * pull-up: LCD_data, SDA & SCL
  102. */
  103. outp(0x0C, DDRD);
  104. outp((inp(PORTD) & 0x0C) | 0xF3, PORTD);
  105. /*
  106. * Port E: CS Flash, VS1011 (DREQ), RTL8019, LCD BL/Enable, IR, USB Rx/Tx
  107. * output: CS Flash, LCD BL/Enable, USB Tx
  108. * input: VS1011 (DREQ), RTL8019, IR
  109. * pull-up: USB Rx
  110. */
  111. outp(0x8E, DDRE);
  112. outp((inp(PORTE) & 0x8E) | 0x01, PORTE);
  113. /*
  114. * Port F: Keyboard_Rows, JTAG-connector, LED, LCD RS/RW, MCC-detect
  115. * output: LCD RS/RW, LED
  116. * input: Keyboard_Rows, MCC-detect
  117. * pull-up: Keyboard_Rows, MCC-detect
  118. * note: Key row 0 & 1 are shared with JTAG TCK/TMS. Cannot be used concurrent
  119. */
  120. #ifndef USE_JTAG
  121. sbi(JTAG_REG, JTD); // disable JTAG interface to be able to use all key-rows
  122. sbi(JTAG_REG, JTD); // do it 2 times - according to requirements ATMEGA128 datasheet: see page 256
  123. #endif //USE_JTAG
  124. outp(0x0E, DDRF);
  125. outp((inp(PORTF) & 0x0E) | 0xF1, PORTF);
  126. /*
  127. * Port G: Keyboard_cols, Bus_control
  128. * output: Keyboard_cols
  129. * input: Bus Control (internal control)
  130. * pull-up: none
  131. */
  132. outp(0x18, DDRG);
  133. }
  134. /*!
  135. * \brief Starts or stops the 4.44 msec mainbeat of the system
  136. * \param OnOff indicates if the mainbeat needs to start or to stop
  137. */
  138. static void SysControlMainBeat(u_char OnOff)
  139. {
  140. int nError = 0;
  141. if (OnOff==ON)
  142. {
  143. nError = NutRegisterIrqHandler(&OVERFLOW_SIGNAL, SysMainBeatInterrupt, NULL);
  144. if (nError == 0)
  145. {
  146. init_8_bit_timer();
  147. }
  148. }
  149. else
  150. {
  151. // disable overflow interrupt
  152. disable_8_bit_timer_ovfl_int();
  153. }
  154. }
  155. /*-------------------------------------------------------------------------*/
  156. /* global variable definitions */
  157. /*-------------------------------------------------------------------------*/
  158. bool isAlarmSyncing = false;
  159. bool initialized = false;
  160. bool running = false;
  161. /*-------------------------------------------------------------------------*/
  162. /* local variable definitions */
  163. /*-------------------------------------------------------------------------*/
  164. /*-------------------------------------------------------------------------*/
  165. /* Thread init */
  166. /*-------------------------------------------------------------------------*/
  167. THREAD(StartupInit, arg)
  168. {
  169. NutThreadSetPriority(5);
  170. NetworkInit();
  171. initialized = true;
  172. NutThreadExit();
  173. }
  174. THREAD(AlarmSync, arg)
  175. {
  176. NutThreadSetPriority(50);
  177. while(initialized == false){
  178. NutSleep(1000);
  179. }
  180. NtpSync();
  181. int dayCounter;
  182. dayCounter = 0;
  183. for(;;)
  184. {
  185. if((initialized == true) && (hasNetworkConnection() == true))
  186. {
  187. isAlarmSyncing = true;
  188. char url[49];
  189. sprintf(url, "/getAlarmen.php?radiomac=%s&tz=%d", getMacAdress(), getTimeZone());
  190. httpGet(url, parseAlarmJson);
  191. char url2[43];
  192. sprintf(url2, "/getTwitch.php?radiomac=%s", getMacAdress());
  193. httpGet(url2, parseTwitch);
  194. isAlarmSyncing = false;
  195. //Command que (Telegram) sync
  196. sprintf(url, "%s%s", "/getCommands.php?radiomac=", getMacAdress());
  197. httpGet(url, parseCommandQue);
  198. }
  199. if(dayCounter > 28800 && (hasNetworkConnection() == true))
  200. {
  201. NtpSync();
  202. dayCounter = 0;
  203. }
  204. dayCounter++;
  205. NutSleep(3000);
  206. }
  207. NutThreadExit();
  208. }
  209. /*-------------------------------------------------------------------------*/
  210. /* Global functions */
  211. /*-------------------------------------------------------------------------*/
  212. int timer(time_t start){
  213. time_t diff = time(0) - start;
  214. return diff;
  215. }
  216. long timerStruct(struct _tm s){
  217. struct _tm ct;
  218. X12RtcGetClock(&ct);
  219. long stime = (s.tm_hour * 3600) + (s.tm_min * 60) + s.tm_sec;
  220. long ctime = (ct.tm_hour * 3600) + (ct.tm_min * 60) + ct.tm_sec;
  221. return ctime - stime;
  222. }
  223. int checkOffPressed(){
  224. if (KbGetKey() != KEY_UNDEFINED){
  225. LcdBackLight(LCD_BACKLIGHT_ON);
  226. return 1;
  227. } else {
  228. return 0;
  229. }
  230. }
  231. int main(void)
  232. {
  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. X12Init();
  246. VsPlayerInit();
  247. NtpInit();
  248. NutThreadCreate("BackgroundThread", StartupInit, NULL, 1024);
  249. NutThreadCreate("BackgroundThread", AlarmSync, NULL, 2500);
  250. //NutThreadCreate("BackgroundThread", NTPSync, NULL, 700);
  251. /** Quick fix for turning off the display after 10 seconds boot */
  252. KbInit();
  253. SysControlMainBeat(ON); // enable 4.4 msecs heartbeat interrupt
  254. /*
  255. * Increase our priority so we can feed the watchdog.
  256. */
  257. NutThreadSetPriority(1);
  258. /* Enable global interrupts */
  259. sei();
  260. LcdBackLight(LCD_BACKLIGHT_OFF);
  261. X12RtcGetClock(&timeCheck);
  262. X12RtcGetClock(&start);
  263. for (;;)
  264. {
  265. //printf("running = %d, time = %d\n", running, timerStruct(start));
  266. if (timerStruct(start) < 0){
  267. X12RtcGetClock(&start);
  268. }
  269. if (timerStruct(timeCheck) < 0){
  270. X12RtcGetClock(&timeCheck);
  271. }
  272. //Check if a button is pressed
  273. if (checkOffPressed() == 1){
  274. X12RtcGetClock(&start);
  275. running = true;
  276. LcdBackLight(LCD_BACKLIGHT_ON);
  277. }
  278. //If escape is pressed, stop displaying custom message
  279. if(KbGetKey() == KEY_ESC){
  280. setDisplayingCustomMessage(false);
  281. }
  282. //Check if background LED is on, and compare to timer
  283. if (running == true){
  284. if (timerStruct(start) >= 10){
  285. running = false;
  286. LcdBackLight(LCD_BACKLIGHT_OFF);
  287. }
  288. }
  289. if(KbGetKey() == KEY_DOWN)
  290. {
  291. NutSleep(150);
  292. X12RtcGetClock(&timeCheck);
  293. u_char newVolume = volumeDown();
  294. displayVolume((int)newVolume);
  295. }
  296. else if(KbGetKey() == KEY_UP)
  297. {
  298. NutSleep(150);
  299. X12RtcGetClock(&timeCheck);
  300. u_char newVolume = volumeUp();
  301. displayVolume((int)newVolume);
  302. }
  303. else if(timerStruct(timeCheck) >= 5 && checkAlarms() == 1)
  304. {
  305. for (idx = 0; idx < 5; idx++){
  306. if (getState(idx) == 1){
  307. displayAlarm(0,1,idx);
  308. if (KbGetKey() == KEY_ESC){
  309. //NutDelay(50);
  310. handleAlarm(idx);
  311. //NutDelay(50);
  312. LcdBackLight(LCD_BACKLIGHT_OFF);
  313. } else if (KbGetKey() == KEY_01 || KbGetKey() == KEY_02 || KbGetKey() == KEY_03 || KbGetKey() == KEY_04 || KbGetKey() == KEY_05 || KbGetKey() == KEY_ALT){
  314. setSnooze(idx);
  315. LcdBackLight(LCD_BACKLIGHT_OFF);
  316. killPlayerThread();
  317. }
  318. }
  319. }
  320. }else if(isDisplayingCustomMessage() == true){
  321. if(timerStruct(timeCheck) >= 5)
  322. {
  323. setDisplayingCustomMessage(false);
  324. LcdBackLight(LCD_BACKLIGHT_OFF);
  325. }
  326. }
  327. else if (timerStruct(timeCheck) >= 5){
  328. displayTime(0);
  329. displayDate(1);
  330. }
  331. WatchDogRestart();
  332. }
  333. return(0);
  334. }