ESP8266 ILI9341 display support code with printf sources, wire-frame viewer and custom fonts  1.0
ESP8266ILI9341DisplayProject
timer_hal.c
Go to the documentation of this file.
1 
25 #include "user_config.h"
26 
27 #include "lib/time.h"
28 #include "lib/timer.h"
29 
30 // =============================================
32 #ifdef ESP8266
33 static ETSTimer task_1ms;
34 
39 {
40  os_timer_disarm(&task_1ms);
41 }
42 
46 void enable_system_task()
47 {
48  os_timer_arm(&task_1ms, 1, 1);
49 }
50 
54 {
55  os_timer_disarm(&task_1ms);
56  os_timer_setfn(&task_1ms, ( os_timer_func_t *) execute_timers, NULL );
57 }
58 #endif // ifdef ESP8266
59 // =============================================
60 
61 // =============================================
63 #ifdef AVR
64 /*< We can read a high resolution hardware timer */
66 #define HAVE_HIRES_TIMER
67 
69 #define TIMER1_PRESCALE 1L
70 
72 #define TIMER1_COUNTS_PER_TIC (F_CPU/TIMER1_PRESCALE/SYSTEM_TASK_HZ)
73 
75 #define TIMER1_COUNTER_RES (SYSTEM_TASK_TIC_NS/TIMER1_COUNTS_PER_TIC)
76 
77 #if TIMER1_COUNTS_PER_TIC >= 65535L
78 #error TIMER1_COUNTS_PER_TIC too big -- increase TIMER1 Prescale
79 #endif
80 
81 #define TIMER1_PRE_1 (1 << CS10) /*< 1 Prescale */
82 #define TIMER1_PRE_8 (1 << CS11) /*< 8 Prescale */
83 #define TIMER1_PRE_64 ((1 << CS11) | ( 1 << CS10)) /*< 64 Prescale */
84 #define TIMER1_PRE_256 (1 << CS12) /*< 256 Prescale */
85 #define TIMER1_PRE_1024 ((1 << CS12) | ( 1 << CS10)) /*< 1024 Prescape */
86 
92 {
93  cli();
94 }
95 
99 void enable_system_task()
100 {
101  sei();
102 }
103 
118 {
119  cli();
120  TCCR1B=(1<<WGM12) | TIMER1_PRE_1; // No Prescale
121  TCCR1A=0;
122  OCR1A=(TIMER1_COUNTS_PER_TIC-1); // 0 .. count
123  TIMSK1 |= (1<<OCIE1A); //Enable the Output Compare A interrupt
124  sei();
125 }
126 
131 ISR(TIMER1_COMPA_vect)
132 {
133  execute_timers();
134 }
135 
136 #ifdef HAVE_HIRES_TIMER
137 MEMSPACE
147 int clock_gettime(clockid_t clk_id, struct timespec *ts)
148 {
149  uint16_t count1,count2; // must be same size as timer register
150  uint32_t offset = 0;
151  uint8_t pendingf = 0;
152  int errorf = 0;
153 
154  // disable interrupts
155  cli();
156 
157  count1 = TCNT1;
158 
159  ts->tv_sec = __clock.tv_sec;
160  ts->tv_nsec = __clock.tv_nsec;
161 
162  count2 = TCNT1;
163 
164  if( TIFR1 & (1<<OCF1A) )
165  pendingf = 1;
166 
167  if (count2 < count1)
168  {
170  if( !pendingf )
171  errorf = -1; // counter overflow and NO pending is an error!
172  offset = TIMER1_COUNTS_PER_TIC; // overflow
173  }
174  else
175  {
176  if( pendingf )
177  offset = TIMER1_COUNTS_PER_TIC; // overflow
178  }
179  offset += count2;
180 
181  // enable interrupts
182  sei();
183 
184  offset *= TIMER1_COUNTER_RES;
185 
186  ts->tv_nsec += offset;
187 
188  if (ts->tv_nsec >= 1000000000L)
189  {
190  ts->tv_nsec -= 1000000000L;
191  ts->tv_sec++;
192  }
193  return(errorf);
194 }
195 #endif // ifdef HAVE_HIRES_TIMER
196 
197 #endif // ifdef AVR
198 // =============================================
199 
void execute_timers()
Execute all user timers at SYSTEM_HZ rate. Called by system task.
Definition: timer.c:261
unsigned short uint16_t
Definition: send.c:18
Master include file for project Includes all project includes and defines here.
Common Linux/POSIX time functions.
MEMSPACE void install_timers_isr(void)
uint16_t clockid_t
type of clockid_t.
Definition: time.h:38
unsigned int uint32_t
Definition: send.c:19
time_t tv_sec
Definition: time.h:90
timer functions
volatile ts_t __clock
System Clock Time.
Definition: timer.c:38
long tv_nsec
Definition: time.h:91
#define NULL
Definition: cpu.h:55
MEMSPACE void enable_system_task(void)
#define MEMSPACE
Definition: cpu.h:25
POSIX timespec.
Definition: time.h:88
#define L(x)
Definition: cal_dex.c:54
MEMSPACE void disable_system_task(void)
unsigned char uint8_t
Definition: send.c:17
MEMSPACE int clock_gettime(clockid_t clk_id, struct timespec *ts)
Generic clock_gettime() function WITHOUT high resolution.
Definition: timer.c:377