70e3326be3398b119171d3ff88cabd743cf70055
[mTask.git] / int / nucleo-f767-blinky / src / interface.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 "usart.h"
9 #include "gpio.h"
10
11 #define SET_LED_RED GPIOB->BSRR = GPIO_PIN_14
12 #define RESET_LED_RED GPIOB->BSRR = GPIO_PIN_14 << 16
13
14 #define SET_LED_BLUE GPIOB->BSRR = GPIO_PIN_7
15 #define RESET_LED_BLUE GPIOB->BSRR = GPIO_PIN_7 << 16
16
17 #define SET_LED_GREEN GPIOB->BSRR = GPIO_PIN_0
18 #define RESET_LED_GREEN GPIOB->BSRR = GPIO_PIN_0 << 16
19
20 #else
21 #include <stdio.h>
22 #include <netdb.h>
23 #include <netinet/in.h>
24 #include <signal.h>
25 #include <sys/socket.h>
26 #include <sys/time.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 #endif
30
31 #include "interface.h"
32
33 //Globals
34 #ifdef STM32F767xx
35 volatile char uartf = 0;
36 char buf[128];
37 #else
38 struct timeval tv1;
39 int sock_fd = -1;
40 int fd = -1;
41 int gargc;
42 char **gargv;
43 #endif
44 uint8_t bt;
45
46 //Specifics
47 #ifdef STM32F767xx
48 void _exit(int i){
49 while(1);
50 (void)i;
51 }
52
53 void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
54 {
55 uartf=1;
56 }
57 #else
58 void killHandler(int i)
59 {
60 printf("%i caught, Bye...\n", i);
61 exit(1);
62 }
63
64 void usage(FILE *o, char *arg0){
65 fprintf(o, "Usage: %s [opts]\n\nOptions\n"
66 "-p PORT Custom port number, default: 8123\n" , arg0);
67 }
68 #endif
69
70 //Interface things
71 long millis() {
72 #ifdef STM32F767xx
73 return HAL_GetTick();
74 #else
75 if (gettimeofday(&tv1, NULL) == -1)
76 pdie("gettimeofday");
77 return tv1.tv_sec*1000 + tv1.tv_usec/1000;
78 #endif
79 }
80
81 bool input_available(){
82 #ifdef STM32F767xx
83 if(HAL_UART_Receive(&huart3, &bt, 1, 1) == HAL_TIMEOUT)
84 return false;
85 return true;
86 #else
87 struct timeval tv;
88 fd_set fds;
89 tv.tv_sec = 0;
90 tv.tv_usec = 0;
91 FD_ZERO(&fds);
92 FD_SET(fd, &fds);
93 if (select(fd+1, &fds, NULL, NULL, &tv) == -1)
94 pdie("select");
95 if(!FD_ISSET(fd, &fds))
96 return false;
97 read(fd, &bt, 1);
98 return true;
99 #endif
100 }
101
102 uint8_t read_byte()
103 {
104 #ifdef STM32F767xx
105 HAL_UART_Receive(&huart3, &bt, 1, 1000);
106 return bt;
107 #else
108 read(fd, &bt, 1);
109 return bt;
110 #endif
111 }
112
113 void write_byte(uint8_t b)
114 {
115 #ifdef STM32F767xx
116 HAL_UART_Transmit_DMA(&huart3, &b, 1);
117 #else
118 write(fd, &b, 1);
119 #endif
120 }
121
122 void write_dpin(uint8_t i, bool b)
123 {
124 #ifdef STM32F767xx
125 #else
126 debug("dwrite %d: %d", i, b);
127 #endif
128 (void) i;
129 (void) b;
130 }
131
132 bool read_dpin(uint8_t i)
133 {
134 #ifdef STM32F767xx
135 return false;
136 #else
137 debug("dread %d", i);
138 return false;
139 #endif
140 (void) i;
141 }
142
143 void write_apin(uint8_t i, uint8_t a)
144 {
145 #ifdef STM32F767xx
146 if(i == 1){
147 SET_LED_RED;
148 RESET_LED_BLUE;
149 RESET_LED_GREEN;
150 } else if(i == 2){
151 RESET_LED_RED;
152 SET_LED_BLUE;
153 RESET_LED_GREEN;
154 } else if(i == 3){
155 RESET_LED_RED;
156 RESET_LED_BLUE;
157 SET_LED_GREEN;
158 }
159 #else
160 debug("awrite %d: %d", i, a);
161 #endif
162 (void) a;
163 (void) i;
164 }
165
166 uint8_t read_apin(uint8_t i)
167 {
168 #ifdef STM32F767xx
169 return 0;
170 #else
171 debug("aread %d", i);
172 return 0;
173 #endif
174 (void) i;
175 }
176
177 void delay(long ms)
178 {
179 #ifdef STM32F767xx
180 HAL_Delay(ms);
181 #else
182 usleep(ms*1000);
183 #endif
184 }
185
186 void setup()
187 {
188 #ifdef STM32F767xx
189 #else
190 int port = 8123, opti = 1;
191 //Register signal handler
192 if(signal(SIGINT, killHandler) == SIG_ERR){
193 die("Couldn't register signal handler...");
194 }
195 if(signal(SIGTERM, killHandler) == SIG_ERR){
196 die("Couldn't register signal handler...");
197 }
198 //Command line arguments
199 while(opti < gargc){
200 if(strcmp((*gargv)+opti, "-h") == 0){
201 usage(stdout, gargv[0]);
202 exit(EXIT_SUCCESS);
203 } else if(strcmp(gargv[opti], "-p") == 0 && opti+1<gargc){
204 port = atoi(gargv[++opti]);
205 if(port < 1)
206 die("Port numbers are > 1");
207 } else {
208 usage(stderr, gargv[0]);
209 exit(EXIT_FAILURE);
210 }
211 opti++;
212 }
213
214 //Open file descriptors
215 struct sockaddr_in sa;
216
217 memset(&sa, 0, sizeof(sa));
218 sa.sin_family = AF_INET;
219 sa.sin_addr.s_addr = INADDR_ANY;
220 sa.sin_port = htons(port);
221
222 if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
223 pdie("socket");
224 if(bind(sock_fd, (struct sockaddr*)&sa, sizeof(sa)) == -1)
225 pdie("bind");
226 if(listen(sock_fd, 10) == -1)
227 pdie("listen");
228
229 printf("Listening on %d\n", port);
230 fflush(stdout);
231 if((fd = accept(sock_fd, (struct sockaddr*)NULL, NULL)) == -1)
232 pdie("accept");
233 #endif
234 }