Chameleon-Mini
System.h
1 /*
2  * System.h
3  *
4  * Created on: 10.02.2013
5  * Author: skuser
6  */
7 
8 #ifndef SYSTEM_H
9 #define SYSTEM_H
10 
11 #include <avr/io.h>
12 #include <avr/pgmspace.h>
13 #include <avr/interrupt.h>
14 #include <stddef.h>
15 #include <stdbool.h>
16 #include "Common.h"
17 
18 #define F_RTC 1000
19 #define SYSTEM_MILLISECONDS_TO_RTC_CYCLES(x) \
20  ( (uint16_t) ( (double) F_RTC * x / 1E3 + 0.5) )
21 
22 #define SYSTEM_TICK_WIDTH 7 /* Bits */
23 #define SYSTEM_TICK_PERIOD (1<<7)
24 #define SYSTEM_TICK_MS (SYSTEM_TICK_PERIOD)
25 #define SYSTEM_TICK_FREQ (1000 / SYSTEM_TICK_PERIOD)
26 
27 #define SYSTEM_SMODE_PSAVE SLEEP_SMODE_PSAVE_gc
28 #define SYSTEM_SMODE_IDLE SLEEP_SMODE_IDLE_gc
29 
30 /* Use GPIORE and GPIORF as global tick register */
31 #define SYSTEM_TICK_REGISTER (*((volatile uint16_t*) &GPIORE))
32 
33 void SystemInit(void);
34 void SystemReset(void);
35 void SystemEnterBootloader(void);
36 void SystemStartUSBClock(void);
37 void SystemStopUSBClock(void);
38 void SystemInterruptInit(void);
39 INLINE bool SystemTick100ms(void);
40 
41 INLINE bool SystemTick100ms(void) {
42  if (RTC.INTFLAGS & RTC_COMPIF_bm) {
43  while (RTC.STATUS & RTC_SYNCBUSY_bm)
44  ;
45 
46  RTC.INTFLAGS = RTC_COMPIF_bm;
47  return true;
48  }
49 
50  return false;
51 }
52 
53 INLINE void SystemTickClearFlag(void) {
54  while (RTC.STATUS & RTC_SYNCBUSY_bm)
55  ;
56 
57  RTC.INTFLAGS = RTC_COMPIF_bm;
58 }
59 
60 INLINE uint16_t SystemGetSysTick(void) {
61  return SYSTEM_TICK_REGISTER | RTC.CNT;
62 }
63 
64 #endif /* SYSTEM_H */