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