ESP8266 ILI9341 display support code with printf sources, wire-frame viewer and custom fonts  1.0
ESP8266ILI9341DisplayProject
user_task.c
Go to the documentation of this file.
1 /*
2  user_task.c - was main.cpp - platform initialization and context switching
3  emulation
4 
5  Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
6  This file is part of the esp8266 core for Arduino environment.
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Lesser General Public
10  License as published by the Free Software Foundation; either
11  version 2.1 of the License, or (at your option) any later version.
12 
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22 
23 #include "user_config.h"
24 
25 //This may be used to change user task stack size:
26 // Can override stack in cont.h by defining it here
27 // FIXME DEBUG
28 #ifndef CONT_STACKSIZE
29 #define CONT_STACKSIZE (1024*4)
30 #endif
31 
32 
33 #include "user_task.h"
34 
35 
36 #define LOOP_TASK_PRIORITY 0
37 #define LOOP_QUEUE_SIZE 1
38 
39 #define OPTIMISTIC_YIELD_TIME_US 16000
40 
41 struct rst_info resetInfo;
42 
43 int atexit(void (*func)())
44 {
45  return 0;
46 }
47 
48 
49 extern void loop();
50 extern void setup();
51 
52 cont_t g_cont __attribute__ ((aligned (16)));
53 static os_event_t g_loop_queue[LOOP_QUEUE_SIZE];
54 
56 
57 void abort()
58 {
59  printf("\nABORT!\n");
60  do
61  {
62  *((int*)0) = 0;
63  } while(true);
64 }
65 
66 
67 void esp_yield()
68 {
69 // FIXME DEBUG
71 
72  if (cont_can_yield(&g_cont))
73  {
74  cont_yield(&g_cont);
75  }
76 }
77 
78 
80 {
81  system_os_post(LOOP_TASK_PRIORITY, 0, 0);
82 }
83 
84 
85 //void __yield()
86 void yield()
87 {
88  if (cont_can_yield(&g_cont))
89  {
90  esp_schedule();
91  esp_yield();
92  }
93  else
94  {
95  abort();
96  }
97 }
98 
99 //void yield(void) __attribute__ ((weak, alias("__yield")));
100 
101 
102 void optimistic_yield(uint32_t interval_us)
103 {
104  if (cont_can_yield(&g_cont) &&
105  (system_get_time() - g_micros_at_task_start) > interval_us)
106  {
107  yield();
108  }
109 }
110 
111 
112 bool setup_done = false;
114 {
115  extern void loop(void);
116  extern void web_task();
117 
118  if(!setup_done)
119  {
120  setup();
121  setup_done = true;
122  }
123 // FIXME DEBUG
124  REG_SET_BIT(0x3ff00014, BIT(0));
125  hspi_waitReady();
126 
127 #ifdef WEBSERVER
128  web_task();
129 #endif
130 
131 // USER TASK
132  loop();
133  esp_schedule();
134 }
135 
136 
137 static void loop_task(os_event_t *events)
138 {
139  g_micros_at_task_start = system_get_time();
140  cont_run(&g_cont, &loop_wrapper);
141  if(cont_check(&g_cont) != 0)
142  {
143  printf("\nsketch stack overflow detected\n");
144  abort();
145  }
146 }
147 
148 
149 void init_done()
150 {
151 printf("\nInit Done with Yield support!\n");
152 printf("=============================\n");
153  // disable os_printf at this time
154  //system_set_os_print(0);
155 
156  esp_schedule();
157 }
158 
159 
160 void user_init(void)
161 {
162 
163  if(!setup_done)
164  {
165  setup();
166  setup_done = true;
167  }
168 
169  cont_init(&g_cont);
170 
171  system_os_task(loop_task,
172  LOOP_TASK_PRIORITY, g_loop_queue,
174 
175  printf("loop_task installed\n");
176 
177  system_init_done_cb(&init_done);
178 }
Master include file for project Includes all project includes and defines here.
void loop()
Definition: user_main.c:533
void cont_yield(cont_t *)
cont_t g_cont __attribute__((aligned(16)))
#define LOOP_QUEUE_SIZE
Definition: user_task.c:37
void loop_wrapper()
Definition: user_task.c:113
unsigned int uint32_t
Definition: send.c:19
uint32_t g_micros_at_task_start
Definition: user_task.c:55
static os_event_t g_loop_queue[LOOP_QUEUE_SIZE]
Definition: user_task.c:53
struct rst_info resetInfo
Definition: user_task.c:41
int cont_check(cont_t *cont)
Definition: cont_util.c:36
bool cont_can_yield(cont_t *cont)
Definition: cont_util.c:44
static void loop_task(os_event_t *events)
Definition: user_task.c:137
void optimistic_yield(uint32_t interval_us)
Definition: user_task.c:102
void abort()
Definition: user_task.c:57
void hspi_waitReady(void)
HSPI Ready wait.
Definition: hspi.c:159
void yield()
Definition: user_task.c:86
int atexit(void(*func)())
Definition: user_task.c:43
void esp_yield()
Definition: user_task.c:67
void init_done()
Definition: user_task.c:149
void user_init(void)
Definition: user_task.c:160
void cont_run(cont_t *, void(*pfn)(void))
bool setup_done
Definition: user_task.c:112
MEMSPACE int printf(const char *format,...)
void setup()
main() Initialize user task
Definition: user_main.c:971
Definition: cont.h:30
void cont_init(cont_t *)
Definition: cont_util.c:27
#define LOOP_TASK_PRIORITY
Definition: user_task.c:36
void esp_schedule()
Definition: user_task.c:79
MEMSPACE void web_task()
Process ALL incoming HTTP requests.
Definition: web.c:2441