main.c 9.3 KB

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