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