main.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. #include "httpstream.h"
  43. /*-------------------------------------------------------------------------*/
  44. /* local routines (prototyping) */
  45. /*-------------------------------------------------------------------------*/
  46. static void SysMainBeatInterrupt(void*);
  47. static void SysControlMainBeat(u_char);
  48. /*-------------------------------------------------------------------------*/
  49. /* Stack check variables placed in .noinit section */
  50. /*-------------------------------------------------------------------------*/
  51. /*!
  52. * \addtogroup System
  53. */
  54. /*@{*/
  55. /*-------------------------------------------------------------------------*/
  56. /* start of code */
  57. /*-------------------------------------------------------------------------*/
  58. /*!
  59. * \brief ISR MainBeat Timer Interrupt (Timer 2 for Mega128, Timer 0 for Mega256).
  60. *
  61. * This routine is automatically called during system
  62. * initialization.
  63. *
  64. * resolution of this Timer ISR is 4,448 msecs
  65. *
  66. * \param *p not used (might be used to pass parms from the ISR)
  67. */
  68. static void SysMainBeatInterrupt(void *p)
  69. {
  70. /*
  71. * scan for valid keys AND check if a MMCard is inserted or removed
  72. */
  73. KbScan();
  74. CardCheckCard();
  75. }
  76. /*!
  77. * \brief Initialise Digital IO
  78. * init inputs to '0', outputs to '1' (DDRxn='0' or '1')
  79. *
  80. * Pull-ups are enabled when the pin is set to input (DDRxn='0') and then a '1'
  81. * is written to the pin (PORTxn='1')
  82. */
  83. void SysInitIO(void)
  84. {
  85. /*
  86. * Port B: VS1011, MMC CS/WP, SPI
  87. * output: all, except b3 (SPI Master In)
  88. * input: SPI Master In
  89. * pull-up: none
  90. */
  91. outp(0xF7, DDRB);
  92. /*
  93. * Port C: Address bus
  94. */
  95. /*
  96. * Port D: LCD_data, Keypad Col 2 & Col 3, SDA & SCL (TWI)
  97. * output: Keyboard colums 2 & 3
  98. * input: LCD_data, SDA, SCL (TWI)
  99. * pull-up: LCD_data, SDA & SCL
  100. */
  101. outp(0x0C, DDRD);
  102. outp((inp(PORTD) & 0x0C) | 0xF3, PORTD);
  103. /*
  104. * Port E: CS Flash, VS1011 (DREQ), RTL8019, LCD BL/Enable, IR, USB Rx/Tx
  105. * output: CS Flash, LCD BL/Enable, USB Tx
  106. * input: VS1011 (DREQ), RTL8019, IR
  107. * pull-up: USB Rx
  108. */
  109. outp(0x8E, DDRE);
  110. outp((inp(PORTE) & 0x8E) | 0x01, PORTE);
  111. /*
  112. * Port F: Keyboard_Rows, JTAG-connector, LED, LCD RS/RW, MCC-detect
  113. * output: LCD RS/RW, LED
  114. * input: Keyboard_Rows, MCC-detect
  115. * pull-up: Keyboard_Rows, MCC-detect
  116. * note: Key row 0 & 1 are shared with JTAG TCK/TMS. Cannot be used concurrent
  117. */
  118. #ifndef USE_JTAG
  119. sbi(JTAG_REG, JTD); // disable JTAG interface to be able to use all key-rows
  120. sbi(JTAG_REG, JTD); // do it 2 times - according to requirements ATMEGA128 datasheet: see page 256
  121. #endif //USE_JTAG
  122. outp(0x0E, DDRF);
  123. outp((inp(PORTF) & 0x0E) | 0xF1, PORTF);
  124. /*
  125. * Port G: Keyboard_cols, Bus_control
  126. * output: Keyboard_cols
  127. * input: Bus Control (internal control)
  128. * pull-up: none
  129. */
  130. outp(0x18, DDRG);
  131. }
  132. /*!
  133. * \brief Starts or stops the 4.44 msec mainbeat of the system
  134. * \param OnOff indicates if the mainbeat needs to start or to stop
  135. */
  136. static void SysControlMainBeat(u_char OnOff)
  137. {
  138. int nError = 0;
  139. if (OnOff==ON)
  140. {
  141. nError = NutRegisterIrqHandler(&OVERFLOW_SIGNAL, SysMainBeatInterrupt, NULL);
  142. if (nError == 0)
  143. {
  144. init_8_bit_timer();
  145. }
  146. }
  147. else
  148. {
  149. // disable overflow interrupt
  150. disable_8_bit_timer_ovfl_int();
  151. }
  152. }
  153. /*-------------------------------------------------------------------------*/
  154. /* global variable definitions */
  155. /*-------------------------------------------------------------------------*/
  156. int isAlarmSyncing;
  157. /*-------------------------------------------------------------------------*/
  158. /* local variable definitions */
  159. /*-------------------------------------------------------------------------*/
  160. /*-------------------------------------------------------------------------*/
  161. /* Thread init */
  162. /*-------------------------------------------------------------------------*/
  163. THREAD(StartupInit, arg)
  164. {
  165. isAlarmSyncing = 1;
  166. NetworkInit();
  167. NtpSync();
  168. char* content = httpGet("/getAlarmen.php?radioid=DE370");
  169. parseAlarmJson(content);
  170. isAlarmSyncing = 0;
  171. free(content);
  172. playStream();
  173. NutThreadExit();
  174. }
  175. /*-------------------------------------------------------------------------*/
  176. /* Global functions */
  177. /*-------------------------------------------------------------------------*/
  178. int timer(time_t start){
  179. time_t diff = time(0) - start;
  180. return diff;
  181. }
  182. int checkOffPressed(){
  183. if (KbScan() < -1){
  184. LcdBackLight(LCD_BACKLIGHT_ON);
  185. return 1;
  186. } else {
  187. return 0;
  188. }
  189. }
  190. int main(void)
  191. {
  192. time_t start;
  193. int running = 0;
  194. WatchDogDisable();
  195. NutDelay(100);
  196. SysInitIO();
  197. SPIinit();
  198. LedInit();
  199. LcdLowLevelInit();
  200. Uart0DriverInit();
  201. Uart0DriverStart();
  202. LogInit();
  203. CardInit();
  204. X12Init();
  205. VsPlayerInit();
  206. LcdBackLight(LCD_BACKLIGHT_ON);
  207. NtpInit();
  208. NutThreadCreate("BackgroundThread", StartupInit, NULL, 1024);
  209. /** Quick fix for turning off the display after 10 seconds boot */
  210. start = time(0);
  211. running = 1;
  212. RcInit();
  213. KbInit();
  214. SysControlMainBeat(ON); // enable 4.4 msecs heartbeat interrupt
  215. /*
  216. * Increase our priority so we can feed the watchdog.
  217. */
  218. NutThreadSetPriority(1);
  219. /* Enable global interrupts */
  220. sei();
  221. for (;;)
  222. {
  223. //Check if a button is pressed
  224. if (checkOffPressed() == 1){
  225. start = time(0);
  226. running = 1;
  227. LcdBacklightKnipperen(startLCD);
  228. }
  229. //Check if background LED is on, and compare to timer
  230. if (running == 1){
  231. if (timer(start) >= 10){
  232. running = 0;
  233. LcdBackLight(LCD_BACKLIGHT_OFF);
  234. }
  235. }
  236. if(!isAlarmSyncing && X12RtcGetStatus(5) > 0)
  237. {
  238. displayAlarm(0,1);
  239. if (KbScan() < -1 || checkTime() == 1){
  240. handleAlarm();
  241. LcdBackLight(LCD_BACKLIGHT_OFF);
  242. }
  243. }
  244. else {
  245. displayTime(0);
  246. displayDate(1);
  247. }
  248. WatchDogRestart();
  249. NutSleep(1000);
  250. }
  251. return(0);
  252. }