main.c 9.9 KB

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