make workable for linux again
[mTask.git] / int / nucleo-f767-blinky / src / main.c
1 #include <stdbool.h>
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #ifdef STM32F767xx
7 #include "stm32f7xx_hal.h"
8 #include "gpio.h"
9 #include "usart.h"
10 #else
11 #include <stdio.h>
12 #include <netdb.h>
13 #include <netinet/in.h>
14 #include <signal.h>
15 #include <sys/socket.h>
16 #include <sys/time.h>
17 #include <sys/types.h>
18 #include <unistd.h>
19 #endif
20
21 #include "interpret.h"
22 #include "mTaskSymbols.h"
23 #include "sds.h"
24 #include "task.h"
25 #include "misc.h"
26
27 #define MSG_GET_TASK 't'
28 #define MSG_DEL_TASK 'd'
29 #define MSG_SDS_SPEC 's'
30 #define MSG_SDS_UPD 'u'
31
32 void _exit(int i){
33 while(1);
34 (void)i;
35 }
36
37 //Globals
38 #ifdef STM32F767xx
39 volatile char uartf = 0;
40 #else
41 struct timeval tv1;
42 int sock_fd = -1;
43 int fd = -1;
44 int *argc;
45 char **argv;
46 #endif
47 uint8_t bt;
48
49 #define SET_LED_RED GPIOB->BSRR = GPIO_PIN_14
50 #define RESET_LED_RED GPIOB->BSRR = GPIO_PIN_14 << 16
51
52 #define SET_LED_BLUE GPIOB->BSRR = GPIO_PIN_7
53 #define RESET_LED_BLUE GPIOB->BSRR = GPIO_PIN_7 << 16
54
55 #define SET_LED_GREEN GPIOB->BSRR = GPIO_PIN_0
56 #define RESET_LED_GREEN GPIOB->BSRR = GPIO_PIN_0 << 16
57
58 long millis() {
59 #ifdef STM32F767xx
60 return HAL_GetTick();
61 #else
62 if (gettimeofday(&tv1, NULL) == -1)
63 pdie("gettimeofday");
64 return tv1.tv_sec*1000 + tv1.tv_usec/1000;
65 #endif
66 }
67
68 bool input_available(){
69 #ifdef STM32F767xx
70 return false;
71 #else
72 struct timeval tv;
73 fd_set fds;
74 tv.tv_sec = 0;
75 tv.tv_usec = 0;
76 FD_ZERO(&fds);
77 FD_SET(fd, &fds);
78 if (select(fd+1, &fds, NULL, NULL, &tv) == -1)
79 pdie("select");
80 return FD_ISSET(fd, &fds);
81 #endif
82 }
83
84 uint8_t read_byte()
85 {
86 #ifdef STM32F767xx
87 HAL_UART_Receive(&huart3, &bt, 1, 1000);
88 return 0;
89 #else
90 read(fd, &bt, 1);
91 return bt;
92 #endif
93 }
94
95 void write_byte(uint8_t b)
96 {
97 #ifdef STM32F767xx
98 HAL_UART_Transmit_DMA(&huart3, &b, 1);
99 #else
100 write(fd, &b, 1);
101 #endif
102 }
103
104 void delay(int ms)
105 {
106 #ifdef STM32F767xx
107 HAL_Delay(ms);
108 #else
109 usleep(ms*1000);
110 #endif
111 }
112
113 #ifndef STM32F767xx
114 void killHandler(int i)
115 {
116 printf("%i caught, Bye...\n", i);
117 exit(1);
118 }
119 #endif
120
121 #ifdef STM32F767xx
122 void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
123 {
124 uartf=1;
125 }
126 #endif
127
128 void read_message()
129 {
130 //Find next task
131 uint8_t c = read_byte();
132 debug("Receiving input: %c\n", c);
133 switch(c){
134 case MSG_SDS_SPEC:
135 debug("Receiving an sds\n");
136 sds_register();
137 break;
138 case MSG_SDS_UPD:
139 debug("Receiving an sds\n");
140 //TODO do something with the return value
141 sds_update();
142 break;
143 case MSG_DEL_TASK:
144 debug("Receiving a delete task request\n");
145 task_delete();
146 break;
147 case MSG_GET_TASK:
148 debug("Receiving a task\n");
149 c = task_register();
150 break;
151 case '\n':
152 break;
153 default:
154 debug("Unknown message: %X\n", c);
155 }
156 }
157
158 void usage(FILE *o, char *arg0){
159 fprintf(o, "Usage: %s [opts]\n\nOptions\n"
160 "-p PORT Custom port number, default: 8123\n" , arg0);
161 }
162
163 void setup()
164 {
165 #ifdef STM32F767xx
166 #else
167 int port = 8123, opti = 1;
168 //Register signal handler
169 if(signal(SIGINT, killHandler) == SIG_ERR){
170 die("Couldn't register signal handler...\n");
171 }
172 if(signal(SIGTERM, killHandler) == SIG_ERR){
173 die("Couldn't register signal handler...\n");
174 }
175 //Command line arguments
176 while(opti < *argc){
177 if(strcmp((*argv)+opti, "-h") == 0){
178 usage(stdout, argv[0]);
179 exit(EXIT_SUCCESS);
180 } else if(strcmp(argv[opti], "-p") == 0 && opti+1<*argc){
181 port = atoi(argv[++opti]);
182 if(port < 1)
183 die("Port numbers are > 1\n");
184 } else {
185 usage(stderr, argv[0]);
186 exit(EXIT_FAILURE);
187 }
188 opti++;
189 }
190
191 //Open file descriptors
192 struct sockaddr_in sa;
193
194 memset(&sa, 0, sizeof(sa));
195 sa.sin_family = AF_INET;
196 sa.sin_addr.s_addr = INADDR_ANY;
197 sa.sin_port = htons(port);
198
199 if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
200 pdie("socket");
201 if(bind(sock_fd, (struct sockaddr*)&sa, sizeof(sa)) == -1)
202 pdie("bind");
203 if(listen(sock_fd, 10) == -1)
204 pdie("listen");
205
206 printf("Listening on %d\n", port);
207 fflush(stdout);
208 if((fd = accept(sock_fd, (struct sockaddr*)NULL, NULL)) == -1)
209 pdie("accept");
210 #endif
211
212 //Initialize systems
213 sds_init();
214 task_init();
215 }
216
217 void loop()
218 {
219 int ct;
220 long cyclestart;
221 struct task *curtask;
222 if(input_available())
223 read_message();
224 //Run tasks
225 cyclestart = millis();
226 for(ct = 0; ct<MAXTASKS; ct++){
227 //See whether the task is even in use
228 if((curtask = task_get(ct)) == NULL){
229 // debug("Task %d not implemented\n", ct);
230 continue;
231 }
232 //See whether the task interval has passed
233 if(cyclestart-curtask->lastrun < curtask->interval){
234 // debug("Task %d not scheduled\n", ct);
235 continue;
236 }
237 #ifdef DEBUG
238 printf("Current task to run: %d\n", ct);
239 getchar();
240 #endif
241 run_task(curtask);
242 }
243 write_byte('\n');
244 }
245
246 #ifdef STM32F767xx
247 int main1(void)
248 #else
249 int main(int ac, char *av[])
250 #endif
251 {
252 #ifndef STM32F767xx
253 argc = &ac;
254 argv = av;
255 #endif
256 setup();
257
258 write_byte('\n');
259
260 while(true){
261 //Check for new tasks
262 loop();
263 debug("Waiting for 500ms\n");
264 delay(500);
265 debug("done waiting\n");
266 delay(500);
267 }
268 return 0;
269 }