main.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. /*-------------------------------------------------------------------------*/
  159. /* local variable definitions */
  160. /*-------------------------------------------------------------------------*/
  161. /*-------------------------------------------------------------------------*/
  162. /* Thread init */
  163. /*-------------------------------------------------------------------------*/
  164. THREAD(StartupInit, arg)
  165. {
  166. isAlarmSyncing = 1;
  167. NetworkInit();
  168. NtpSync();
  169. char* content = httpGet("/getAlarmen.php?radioid=DE370");
  170. parseAlarmJson(content);
  171. isAlarmSyncing = 0;
  172. free(content);
  173. //playStream("145.58.53.152", 80, "/3fm-bb-mp3");
  174. NutThreadExit();
  175. }
  176. /*-------------------------------------------------------------------------*/
  177. /* Global functions */
  178. /*-------------------------------------------------------------------------*/
  179. int timer(time_t start){
  180. time_t diff = time(0) - start;
  181. return diff;
  182. }
  183. int checkOffPressed(){
  184. if (KbScan() < -1){
  185. LcdBackLight(LCD_BACKLIGHT_ON);
  186. return 1;
  187. } else {
  188. return 0;
  189. }
  190. }
  191. int main(void)
  192. {
  193. time_t start;
  194. int running = 0;
  195. WatchDogDisable();
  196. NutDelay(100);
  197. SysInitIO();
  198. SPIinit();
  199. LedInit();
  200. LcdLowLevelInit();
  201. Uart0DriverInit();
  202. Uart0DriverStart();
  203. LogInit();
  204. CardInit();
  205. X12Init();
  206. VsPlayerInit();
  207. LcdBackLight(LCD_BACKLIGHT_ON);
  208. NtpInit();
  209. NutThreadCreate("BackgroundThread", StartupInit, NULL, 1024);
  210. /** Quick fix for turning off the display after 10 seconds boot */
  211. start = time(0);
  212. running = 1;
  213. RcInit();
  214. KbInit();
  215. SysControlMainBeat(ON); // enable 4.4 msecs heartbeat interrupt
  216. /*
  217. * Increase our priority so we can feed the watchdog.
  218. */
  219. NutThreadSetPriority(1);
  220. /* Enable global interrupts */
  221. sei();
  222. for (;;)
  223. {
  224. //Check if a button is pressed
  225. if (checkOffPressed() == 1){
  226. start = time(0);
  227. running = 1;
  228. LcdBacklightKnipperen(startLCD);
  229. }
  230. //Check if background LED is on, and compare to timer
  231. if (running == 1){
  232. if (timer(start) >= 10){
  233. running = 0;
  234. LcdBackLight(LCD_BACKLIGHT_OFF);
  235. }
  236. }
  237. if(!isAlarmSyncing && X12RtcGetStatus(5) > 0)
  238. {
  239. displayAlarm(0,1);
  240. if (KbScan() < -1 || checkTime() == 1){
  241. handleAlarm();
  242. LcdBackLight(LCD_BACKLIGHT_OFF);
  243. }
  244. }
  245. else {
  246. displayTime(0);
  247. displayDate(1);
  248. }
  249. WatchDogRestart();
  250. NutSleep(1000);
  251. }
  252. return(0);
  253. }