main.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. /* global variable definitions */
  62. /*-------------------------------------------------------------------------*/
  63. bool isAlarmSyncing = false;
  64. bool initialized = false;
  65. bool running = false;
  66. /*!
  67. * \brief ISR MainBeat Timer Interrupt (Timer 2 for Mega128, Timer 0 for Mega256).
  68. *
  69. * This routine is automatically called during system
  70. * initialization.
  71. *
  72. * resolution of this Timer ISR is 4,448 msecs
  73. *
  74. * \param *p not used (might be used to pass parms from the ISR)
  75. */
  76. static void SysMainBeatInterrupt(void *p)
  77. {
  78. KbScan();
  79. }
  80. /*!
  81. * \brief Initialise Digital IO
  82. * init inputs to '0', outputs to '1' (DDRxn='0' or '1')
  83. *
  84. * Pull-ups are enabled when the pin is set to input (DDRxn='0') and then a '1'
  85. * is written to the pin (PORTxn='1')
  86. */
  87. void SysInitIO(void)
  88. {
  89. /*
  90. * Port B: VS1011, MMC CS/WP, SPI
  91. * output: all, except b3 (SPI Master In)
  92. * input: SPI Master In
  93. * pull-up: none
  94. */
  95. outp(0xF7, DDRB);
  96. /*
  97. * Port C: Address bus
  98. */
  99. /*
  100. * Port D: LCD_data, Keypad Col 2 & Col 3, SDA & SCL (TWI)
  101. * output: Keyboard colums 2 & 3
  102. * input: LCD_data, SDA, SCL (TWI)
  103. * pull-up: LCD_data, SDA & SCL
  104. */
  105. outp(0x0C, DDRD);
  106. outp((inp(PORTD) & 0x0C) | 0xF3, PORTD);
  107. /*
  108. * Port E: CS Flash, VS1011 (DREQ), RTL8019, LCD BL/Enable, IR, USB Rx/Tx
  109. * output: CS Flash, LCD BL/Enable, USB Tx
  110. * input: VS1011 (DREQ), RTL8019, IR
  111. * pull-up: USB Rx
  112. */
  113. outp(0x8E, DDRE);
  114. outp((inp(PORTE) & 0x8E) | 0x01, PORTE);
  115. /*
  116. * Port F: Keyboard_Rows, JTAG-connector, LED, LCD RS/RW, MCC-detect
  117. * output: LCD RS/RW, LED
  118. * input: Keyboard_Rows, MCC-detect
  119. * pull-up: Keyboard_Rows, MCC-detect
  120. * note: Key row 0 & 1 are shared with JTAG TCK/TMS. Cannot be used concurrent
  121. */
  122. #ifndef USE_JTAG
  123. sbi(JTAG_REG, JTD); // disable JTAG interface to be able to use all key-rows
  124. sbi(JTAG_REG, JTD); // do it 2 times - according to requirements ATMEGA128 datasheet: see page 256
  125. #endif //USE_JTAG
  126. outp(0x0E, DDRF);
  127. outp((inp(PORTF) & 0x0E) | 0xF1, PORTF);
  128. /*
  129. * Port G: Keyboard_cols, Bus_control
  130. * output: Keyboard_cols
  131. * input: Bus Control (internal control)
  132. * pull-up: none
  133. */
  134. outp(0x18, DDRG);
  135. }
  136. /*!
  137. * \brief Starts or stops the 4.44 msec mainbeat of the system
  138. * \param OnOff indicates if the mainbeat needs to start or to stop
  139. */
  140. static void SysControlMainBeat(u_char OnOff)
  141. {
  142. int nError = 0;
  143. if (OnOff==ON)
  144. {
  145. nError = NutRegisterIrqHandler(&OVERFLOW_SIGNAL, SysMainBeatInterrupt, NULL);
  146. if (nError == 0)
  147. {
  148. init_8_bit_timer();
  149. }
  150. }
  151. else
  152. {
  153. // disable overflow interrupt
  154. disable_8_bit_timer_ovfl_int();
  155. }
  156. }
  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(AlarmCheck, arg)
  171. {
  172. NutThreadSetPriority(100);
  173. for(;;){
  174. if(checkAlarms() == 1){
  175. setCurrentDisplay(DISPLAY_Alarm, 1000);
  176. }
  177. NutSleep(1000);
  178. }
  179. }
  180. THREAD(AlarmSync, arg)
  181. {
  182. NutThreadSetPriority(50);
  183. while(initialized == false){
  184. NutSleep(1000);
  185. }
  186. int dayCounter = 0;
  187. int counter = 0;
  188. while(!NtpSync() && counter < 10)
  189. {
  190. NutSleep(1000);
  191. counter++;
  192. }
  193. counter = 0;
  194. for(;;)
  195. {
  196. if((initialized == true) && (hasNetworkConnection() == true))
  197. {
  198. checkSleep();
  199. isAlarmSyncing = true;
  200. char url[49];
  201. sprintf(url, "/getAlarmen.php?radiomac=%s&tz=%d", getMacAdress(), getTimeZone());
  202. httpGet(url, parseAlarmJson);
  203. isAlarmSyncing = false;
  204. char url2[43];
  205. sprintf(url2, "/getTwitch.php?radiomac=%s", getMacAdress());
  206. httpGet(url2, parseTwitch);
  207. char url3[43];
  208. sprintf(url3,"/getTwitter.php?radiomac=%s", getMacAdress());
  209. httpGet(url3,TwitterParser);
  210. //Command que (Telegram) sync
  211. sprintf(url, "%s%s", "/getCommands.php?radiomac=", getMacAdress());
  212. httpGet(url, parseCommandQue);
  213. }
  214. while(dayCounter > 28800 && (hasNetworkConnection() == true))
  215. {
  216. while(!NtpSync() && counter < 10)
  217. {
  218. NutSleep(1000);
  219. counter++;
  220. }
  221. dayCounter = 28800;
  222. counter = 0;
  223. }
  224. dayCounter++;
  225. NutSleep(3000);
  226. }
  227. NutThreadExit();
  228. }
  229. /*-------------------------------------------------------------------------*/
  230. /* Global functions */
  231. /*-------------------------------------------------------------------------*/
  232. int timer(time_t start){
  233. time_t diff = time(0) - start;
  234. return diff;
  235. }
  236. int main(void)
  237. {
  238. struct _tm timeCheck;
  239. WatchDogDisable();
  240. NutDelay(100);
  241. SysInitIO();
  242. SPIinit();
  243. LedInit();
  244. LcdLowLevelInit();
  245. Uart0DriverInit();
  246. Uart0DriverStart();
  247. LogInit();
  248. X12Init();
  249. VsPlayerInit();
  250. NtpInit();
  251. NutThreadCreate("BackgroundThread", StartupInit, NULL, 1024);
  252. NutThreadCreate("BackgroundThread", AlarmSync, NULL, 2500);
  253. NutThreadCreate("BackgroundThread", AlarmCheck, NULL, 256);
  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_ON);
  263. setCurrentDisplay(DISPLAY_DateTime, 5);
  264. X12RtcGetClock(&timeCheck);
  265. printf("Welcome to Saltyradio.\nI'm using mac address: %s\n\n\n", getMacAdress());
  266. for (;;)
  267. {
  268. //Key detecten
  269. if(KbGetKey() != KEY_UNDEFINED){
  270. //Backlight aanzetten.
  271. LcdBackLight(LCD_BACKLIGHT_ON);
  272. if(getCurrentDisplay() == DISPLAY_Alarm){
  273. if(KbGetKey() == KEY_01 || KbGetKey() == KEY_02 || KbGetKey() == KEY_03 || KbGetKey() == KEY_04 || KbGetKey() == KEY_05 || KbGetKey() == KEY_ALT){
  274. setSnooze(getRunningAlarmID());
  275. killPlayerThread();
  276. setCurrentDisplay(DISPLAY_DateTime, 2);
  277. }else if(KbGetKey() == KEY_ESC){
  278. handleAlarm(getRunningAlarmID());
  279. killPlayerThread();
  280. setCurrentDisplay(DISPLAY_DateTime, 5);
  281. }
  282. }else{
  283. if(KbGetKey() == KEY_DOWN){
  284. setCurrentDisplay(DISPLAY_Volume, 5);
  285. volumeDown();
  286. }else if(KbGetKey() == KEY_UP){
  287. setCurrentDisplay(DISPLAY_Volume, 5);
  288. volumeUp();
  289. }else if(KbGetKey() == KEY_LEFT){
  290. setCurrentDisplay(DISPLAY_Twitter,20);
  291. }else{
  292. setCurrentDisplay(DISPLAY_DateTime, 5);
  293. }
  294. if(KbGetKey() == KEY_01){
  295. setSleep();
  296. }
  297. if(KbGetKey() == KEY_02){
  298. changeChanel();
  299. }
  300. }
  301. }
  302. refreshScreen();
  303. WatchDogRestart();
  304. NutSleep(100);
  305. }
  306. return(0);
  307. }