2f7fb5754bcbc557f4d218787e0e5f1dd6f1624b
[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 #else
36 struct timeval tv1;
37 int sock_fd = -1;
38 int fd = -1;
39 int gargc;
40 char **gargv;
41 #endif
42 uint8_t bt;
43
44 //Specifics
45 #ifdef STM32F767xx
46 void _exit(int i){
47 while(1);
48 (void)i;
49 }
50
51 void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
52 {
53 uartf=1;
54 }
55 #else
56 void killHandler(int i)
57 {
58 printf("%i caught, Bye...\n", i);
59 exit(1);
60 }
61
62 void usage(FILE *o, char *arg0){
63 fprintf(o, "Usage: %s [opts]\n\nOptions\n"
64 "-p PORT Custom port number, default: 8123\n" , arg0);
65 }
66 #endif
67
68 //Interface things
69 long millis() {
70 #ifdef STM32F767xx
71 return HAL_GetTick();
72 #else
73 if (gettimeofday(&tv1, NULL) == -1)
74 pdie("gettimeofday");
75 return tv1.tv_sec*1000 + tv1.tv_usec/1000;
76 #endif
77 }
78
79 bool input_available(){
80 #ifdef STM32F767xx
81 return false;
82 #else
83 struct timeval tv;
84 fd_set fds;
85 tv.tv_sec = 0;
86 tv.tv_usec = 0;
87 FD_ZERO(&fds);
88 FD_SET(fd, &fds);
89 if (select(fd+1, &fds, NULL, NULL, &tv) == -1)
90 pdie("select");
91 return FD_ISSET(fd, &fds);
92 #endif
93 }
94
95 uint8_t read_byte()
96 {
97 #ifdef STM32F767xx
98 HAL_UART_Receive(&huart3, &bt, 1, 1000);
99 return 0;
100 #else
101 read(fd, &bt, 1);
102 return bt;
103 #endif
104 }
105
106 void write_byte(uint8_t b)
107 {
108 #ifdef STM32F767xx
109 HAL_UART_Transmit_DMA(&huart3, &b, 1);
110 #else
111 write(fd, &b, 1);
112 #endif
113 }
114
115 void delay(long ms)
116 {
117 #ifdef STM32F767xx
118 HAL_Delay(ms);
119 #else
120 usleep(ms*1000);
121 #endif
122 }
123
124 void setup()
125 {
126 #ifdef STM32F767xx
127 #else
128 int port = 8123, opti = 1;
129 //Register signal handler
130 if(signal(SIGINT, killHandler) == SIG_ERR){
131 die("Couldn't register signal handler...\n");
132 }
133 if(signal(SIGTERM, killHandler) == SIG_ERR){
134 die("Couldn't register signal handler...\n");
135 }
136 //Command line arguments
137 while(opti < gargc){
138 if(strcmp((*gargv)+opti, "-h") == 0){
139 usage(stdout, gargv[0]);
140 exit(EXIT_SUCCESS);
141 } else if(strcmp(gargv[opti], "-p") == 0 && opti+1<gargc){
142 port = atoi(gargv[++opti]);
143 if(port < 1)
144 die("Port numbers are > 1\n");
145 } else {
146 usage(stderr, gargv[0]);
147 exit(EXIT_FAILURE);
148 }
149 opti++;
150 }
151
152 //Open file descriptors
153 struct sockaddr_in sa;
154
155 memset(&sa, 0, sizeof(sa));
156 sa.sin_family = AF_INET;
157 sa.sin_addr.s_addr = INADDR_ANY;
158 sa.sin_port = htons(port);
159
160 if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
161 pdie("socket");
162 if(bind(sock_fd, (struct sockaddr*)&sa, sizeof(sa)) == -1)
163 pdie("bind");
164 if(listen(sock_fd, 10) == -1)
165 pdie("listen");
166
167 printf("Listening on %d\n", port);
168 fflush(stdout);
169 if((fd = accept(sock_fd, (struct sockaddr*)NULL, NULL)) == -1)
170 pdie("accept");
171 #endif
172 }