main.c 10 KB

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