main.c 9.6 KB

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