main.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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* content = httpGet("/getAlarmen.php?radioid=DE370");
  195. parseAlarmJson(content);
  196. free(content);
  197. isAlarmSyncing = 0;
  198. }
  199. NutSleep(3000);
  200. }
  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. int checkOffPressed(){
  211. if (KbGetKey() == KEY_POWER){
  212. LcdBackLight(LCD_BACKLIGHT_ON);
  213. return 1;
  214. } else {
  215. return 0;
  216. }
  217. }
  218. int main(void)
  219. {
  220. initialized = 0;
  221. int VOL2;
  222. time_t start;
  223. int idx = 0;
  224. int running;
  225. WatchDogDisable();
  226. NutDelay(100);
  227. SysInitIO();
  228. SPIinit();
  229. LedInit();
  230. LcdLowLevelInit();
  231. Uart0DriverInit();
  232. Uart0DriverStart();
  233. LogInit();
  234. CardInit();
  235. X12Init();
  236. VsPlayerInit();
  237. LcdBackLight(LCD_BACKLIGHT_ON);
  238. NtpInit();
  239. NutThreadCreate("BackgroundThread", StartupInit, NULL, 1024);
  240. NutThreadCreate("BackgroundThread", AlarmSync, NULL, 2500);
  241. NutThreadCreate("BackgroundThread", NTPSync, NULL, 700);
  242. /** Quick fix for turning off the display after 10 seconds boot */
  243. RcInit();
  244. KbInit();
  245. SysControlMainBeat(ON); // enable 4.4 msecs heartbeat interrupt
  246. /*
  247. * Increase our priority so we can feed the watchdog.
  248. */
  249. NutThreadSetPriority(1);
  250. /* Enable global interrupts */
  251. sei();
  252. /*struct _tm tm;
  253. tm = GetRTCTime();
  254. tm.tm_sec += 10;
  255. setAlarm(tm," test1234 ", "0.0.0.0","", 8001,1,0,0);
  256. tm.tm_sec +=20;
  257. setAlarm(tm," test5678 ", "0.0.0.0","", 8001,1,0,1);*/
  258. /* if(hasNetworkConnection() == true){
  259. playStream("145.58.53.152", 80, "/3fm-bb-mp3");
  260. }*/
  261. start = time(0) - 10;
  262. unsigned char VOL = 64;
  263. running = 1;
  264. for (;;)
  265. {
  266. //Check if a button is pressed
  267. if (checkOffPressed() == 1){
  268. start = time(0);
  269. running = 1;
  270. LcdBacklightKnipperen(startLCD);
  271. }
  272. //Check if background LED is on, and compare to timer
  273. if (running == 1){
  274. if (timer(start) >= 10){
  275. running = 0;
  276. LcdBackLight(LCD_BACKLIGHT_OFF);
  277. }
  278. }
  279. VOL = VOL2;
  280. if(KbGetKey() == KEY_DOWN)
  281. {
  282. NutSleep(150);
  283. start = time(0);
  284. if(VOL > 8){
  285. VOL -= 8;
  286. VsSetVolume (128-VOL, 128-VOL);
  287. displayVolume(VOL/8);
  288. }
  289. }
  290. else if(KbGetKey() == KEY_UP)
  291. {
  292. NutSleep(150);
  293. start = time(0);
  294. if(VOL < 128) {
  295. VOL += 8;
  296. VsSetVolume(128-VOL, 128-VOL);
  297. displayVolume(VOL/8);
  298. }
  299. }
  300. else if(timer(start) >= 5 && checkAlarms() == 1)
  301. {
  302. for (idx = 0; idx < 2; idx++){
  303. if (getState(idx) == 1){
  304. displayAlarm(0,1,idx);
  305. if (KbGetKey() == KEY_ESC){
  306. NutDelay(50);
  307. handleAlarm(idx);
  308. NutDelay(50);
  309. LcdBackLight(LCD_BACKLIGHT_OFF);
  310. stopStream();
  311. }
  312. }
  313. }
  314. }
  315. else if (timer(start) >= 5){
  316. displayTime(0);
  317. displayDate(1);
  318. }
  319. VOL2 = VOL;
  320. WatchDogRestart();
  321. }
  322. return(0);
  323. }