main.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 "vs10xx.h"
  43. #include "watchdog.h"
  44. /*-------------------------------------------------------------------------*/
  45. /* local routines (prototyping) */
  46. /*-------------------------------------------------------------------------*/
  47. static void SysMainBeatInterrupt(void*);
  48. static void SysControlMainBeat(u_char);
  49. /*-------------------------------------------------------------------------*/
  50. /* Stack check variables placed in .noinit section */
  51. /*-------------------------------------------------------------------------*/
  52. /*!
  53. * \addtogroup System
  54. */
  55. /*@{*/
  56. /*-------------------------------------------------------------------------*/
  57. /* start of code */
  58. /*-------------------------------------------------------------------------*/
  59. /*-------------------------------------------------------------------------*/
  60. /* global variable definitions */
  61. /*-------------------------------------------------------------------------*/
  62. bool isAlarmSyncing = false;
  63. bool initialized = false;
  64. bool running = false;
  65. /*!
  66. * \brief ISR MainBeat Timer Interrupt (Timer 2 for Mega128, Timer 0 for Mega256).
  67. *
  68. * This routine is automatically called during system
  69. * initialization.
  70. *
  71. * resolution of this Timer ISR is 4,448 msecs
  72. *
  73. * \param *p not used (might be used to pass parms from the ISR)
  74. */
  75. static void SysMainBeatInterrupt(void *p)
  76. {
  77. KbScan();
  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. /* local variable definitions */
  158. /*-------------------------------------------------------------------------*/
  159. /*-------------------------------------------------------------------------*/
  160. /* Thread init */
  161. /*-------------------------------------------------------------------------*/
  162. THREAD(StartupInit, arg)
  163. {
  164. NutThreadSetPriority(5);
  165. NetworkInit();
  166. initialized = true;
  167. NutThreadExit();
  168. }
  169. THREAD(AlarmCheck, arg)
  170. {
  171. NutThreadSetPriority(100);
  172. for(;;){
  173. if(checkAlarms() == 1){
  174. setCurrentDisplay(DISPLAY_Alarm, 1000);
  175. }
  176. NutSleep(1000);
  177. }
  178. }
  179. THREAD(AlarmSync, arg)
  180. {
  181. NutThreadSetPriority(50);
  182. while(initialized == false){
  183. NutSleep(1000);
  184. }
  185. NtpSync();
  186. int dayCounter;
  187. dayCounter = 0;
  188. for(;;)
  189. {
  190. if((initialized == true) && (hasNetworkConnection() == true))
  191. {
  192. isAlarmSyncing = true;
  193. char url[49];
  194. sprintf(url, "/getAlarmen.php?radiomac=%s&tz=%d", getMacAdress(), getTimeZone());
  195. httpGet(url, parseAlarmJson);
  196. char url2[43];
  197. sprintf(url2, "/getTwitch.php?radiomac=%s", getMacAdress());
  198. httpGet(url2, parseTwitch);
  199. char url3[43];
  200. sprintf(url3,"/getTwitter.php?radiomac=%s", getMacAdress());
  201. httpGet(url3,TwitterParser);
  202. isAlarmSyncing = false;
  203. //Command que (Telegram) sync
  204. sprintf(url, "%s%s", "/getCommands.php?radiomac=", getMacAdress());
  205. httpGet(url, parseCommandQue);
  206. }
  207. if(dayCounter > 28800 && (hasNetworkConnection() == true))
  208. {
  209. NtpSync();
  210. dayCounter = 0;
  211. }
  212. dayCounter++;
  213. NutSleep(3000);
  214. }
  215. NutThreadExit();
  216. }
  217. /*-------------------------------------------------------------------------*/
  218. /* Global functions */
  219. /*-------------------------------------------------------------------------*/
  220. int timer(time_t start){
  221. time_t diff = time(0) - start;
  222. return diff;
  223. }
  224. int main(void)
  225. {
  226. struct _tm timeCheck;
  227. WatchDogDisable();
  228. NutDelay(100);
  229. SysInitIO();
  230. SPIinit();
  231. LedInit();
  232. LcdLowLevelInit();
  233. Uart0DriverInit();
  234. Uart0DriverStart();
  235. LogInit();
  236. X12Init();
  237. VsPlayerInit();
  238. NtpInit();
  239. NutThreadCreate("BackgroundThread", StartupInit, NULL, 1024);
  240. NutThreadCreate("BackgroundThread", AlarmSync, NULL, 2500);
  241. NutThreadCreate("BackgroundThread", AlarmCheck, NULL, 256);
  242. KbInit();
  243. SysControlMainBeat(ON); // enable 4.4 msecs heartbeat interrupt
  244. /*
  245. * Increase our priority so we can feed the watchdog.
  246. */
  247. NutThreadSetPriority(1);
  248. /* Enable global interrupts */
  249. sei();
  250. LcdBackLight(LCD_BACKLIGHT_ON);
  251. setCurrentDisplay(DISPLAY_DateTime, 5);
  252. X12RtcGetClock(&timeCheck);
  253. for (;;)
  254. {
  255. //Key detecten
  256. if(KbGetKey() != KEY_UNDEFINED){
  257. //Backlight aanzetten.
  258. LcdBackLight(LCD_BACKLIGHT_ON);
  259. if(getCurrentDisplay() == DISPLAY_Alarm){
  260. if(KbGetKey() == KEY_01 || KbGetKey() == KEY_02 || KbGetKey() == KEY_03 || KbGetKey() == KEY_04 || KbGetKey() == KEY_05 || KbGetKey() == KEY_ALT){
  261. setSnooze(getRunningAlarmID());
  262. killPlayerThread();
  263. setCurrentDisplay(DISPLAY_DateTime, 2);
  264. }else if(KbGetKey() == KEY_ESC){
  265. handleAlarm(getRunningAlarmID());
  266. killPlayerThread();
  267. setCurrentDisplay(DISPLAY_DateTime, 5);
  268. }
  269. }else{
  270. if(KbGetKey() == KEY_DOWN){
  271. setCurrentDisplay(DISPLAY_Volume, 5);
  272. volumeDown();
  273. }else if(KbGetKey() == KEY_UP){
  274. setCurrentDisplay(DISPLAY_Volume, 5);
  275. volumeUp();
  276. }else if(KbGetKey() == KEY_LEFT){
  277. setCurrentDisplay(DISPLAY_Twitter,20);
  278. }else{
  279. setCurrentDisplay(DISPLAY_DateTime, 5);
  280. }
  281. }
  282. }
  283. refreshScreen();
  284. WatchDogRestart();
  285. NutSleep(100);
  286. }
  287. return(0);
  288. }