main.c 9.6 KB

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