main.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. int VOL2;
  158. time_t start;
  159. time_t startVolumeTime;
  160. /*-------------------------------------------------------------------------*/
  161. /* local variable definitions */
  162. /*-------------------------------------------------------------------------*/
  163. /*-------------------------------------------------------------------------*/
  164. /* Thread init */
  165. /*-------------------------------------------------------------------------*/
  166. THREAD(StartupInit, arg)
  167. {
  168. NetworkInit();
  169. NtpSync();
  170. start = time(0);
  171. startVolumeTime = time(0);
  172. initialized = 1;
  173. NutThreadExit();
  174. }
  175. THREAD(Alarmsync, arg)
  176. {
  177. for(;;)
  178. {
  179. if(initialized && (hasNetworkConnection() == true))
  180. {
  181. isAlarmSyncing = 1;
  182. char* content = httpGet("/getAlarmen.php?radioid=DE370");
  183. parseAlarmJson(content);
  184. free(content);
  185. isAlarmSyncing = 0;
  186. }
  187. NutSleep(30000);
  188. }
  189. NutThreadExit();
  190. }
  191. /*-------------------------------------------------------------------------*/
  192. /* Global functions */
  193. /*-------------------------------------------------------------------------*/
  194. int timer(time_t start){
  195. time_t diff = time(0) - start;
  196. return diff;
  197. }
  198. int checkOffPressed(){
  199. if (KbGetKey() > 1){
  200. LcdBackLight(LCD_BACKLIGHT_ON);
  201. return 1;
  202. } else {
  203. return 0;
  204. }
  205. }
  206. int main(void)
  207. {
  208. initialized = 0;
  209. int idx = 0;
  210. int running = 0;
  211. WatchDogDisable();
  212. NutDelay(100);
  213. SysInitIO();
  214. SPIinit();
  215. LedInit();
  216. LcdLowLevelInit();
  217. Uart0DriverInit();
  218. Uart0DriverStart();
  219. LogInit();
  220. CardInit();
  221. X12Init();
  222. LcdBackLight(LCD_BACKLIGHT_ON);
  223. NtpInit();
  224. NutThreadCreate("BackgroundThread", StartupInit, NULL, 1024);
  225. NutThreadCreate("BackgroundThread", Alarmsync, NULL, 1024);
  226. /** Quick fix for turning off the display after 10 seconds boot */
  227. RcInit();
  228. KbInit();
  229. SysControlMainBeat(ON); // enable 4.4 msecs heartbeat interrupt
  230. /*
  231. * Increase our priority so we can feed the watchdog.
  232. */
  233. NutThreadSetPriority(1);
  234. /* Enable global interrupts */
  235. sei();
  236. unsigned char VOL = 64;
  237. displayDate(1);
  238. displayTime(0);
  239. struct _tm tm;
  240. tm = GetRTCTime();
  241. tm.tm_sec += 10;
  242. setAlarm(tm," test1234 ", "0.0.0.0", 8001,1,0,0);
  243. tm.tm_sec +=20;
  244. setAlarm(tm," test5678 ", "0.0.0.0", 8001,1,0,1);
  245. start = time(0);
  246. startVolumeTime = time(0);
  247. running = 1;
  248. for (;;)
  249. {
  250. //Check if a button is pressed
  251. if (checkOffPressed() == 1){
  252. start = time(0);
  253. running = 1;
  254. LcdBacklightKnipperen(startLCD);
  255. }
  256. //Check if background LED is on, and compare to timer
  257. if (running == 1){
  258. if (timer(start) >= 10){
  259. running = 0;
  260. LcdBackLight(LCD_BACKLIGHT_OFF);
  261. }
  262. }
  263. VOL = VOL2;
  264. if(KbGetKey() == KEY_DOWN)
  265. {
  266. NutSleep(150);
  267. startVolumeTime = time(0);
  268. if(VOL > 1){
  269. VOL -= 8;
  270. VsSetVolume (128-VOL, 128-VOL);
  271. displayVolume(VOL/8);
  272. }
  273. }
  274. else if(KbGetKey() == KEY_UP)
  275. {
  276. NutSleep(150);
  277. startVolumeTime = time(0);
  278. if(VOL < 128) {
  279. VOL += 8;
  280. VsSetVolume(128-VOL, 128-VOL);
  281. displayVolume(VOL/8);
  282. }
  283. }
  284. else if(timer(startVolumeTime) >= 5 && checkAlarms() == 1)
  285. {
  286. for (idx = 0; idx < 3; idx++){
  287. if (getState(idx) == 1){
  288. displayAlarm(0,1,idx);
  289. if (KbGetKey() == KEY_ESC){
  290. NutDelay(50);
  291. handleAlarm(idx);
  292. NutDelay(50);
  293. LcdBackLight(LCD_BACKLIGHT_OFF);
  294. }
  295. }
  296. }
  297. }
  298. else if (timer(startVolumeTime) >= 5){
  299. displayTime(0);
  300. displayDate(1);
  301. }
  302. printf("%d", timer(startVolumeTime));
  303. VOL2 = VOL;
  304. WatchDogRestart();
  305. }
  306. return(0);
  307. }