sidestep
[mTask.git] / int / nucleo-f767-blinky / src / interface.c
index 96077b0..260919c 100644 (file)
@@ -3,21 +3,9 @@
 #include <stdlib.h>
 #include <string.h>
 
-#ifdef STM32F767xx
 #include "stm32f7xx_hal.h"
-#include "gpio.h"
 #include "usart.h"
-#else
-#include <stdio.h>
-#include <netdb.h>
-#include <netinet/in.h>
-#include <signal.h>
-#include <sys/socket.h>
-#include <sys/time.h>
-#include <sys/types.h>
-#include <unistd.h>
-#endif
-
+#include "gpio.h"
 #include "interface.h"
 
 #define SET_LED_RED GPIOB->BSRR = GPIO_PIN_14
 #define SET_LED_GREEN GPIOB->BSRR = GPIO_PIN_0
 #define RESET_LED_GREEN GPIOB->BSRR = GPIO_PIN_0 << 16
 
+
+#define BUFSIZE 1024
+
 //Globals
-#ifdef STM32F767xx
-volatile char uartf = 0;
 char buf[128];
-#else
-struct timeval tv1;
-int sock_fd = -1;
-int fd = -1;
-int gargc;
-char **gargv;
-#endif
-uint8_t bt;
-
-//Specifics
-#ifdef STM32F767xx
+volatile bool sending = false;
+uint8_t circbuf[BUFSIZE];
+uint16_t readpos, writepos;
+uint8_t readb;
+
 void _exit(int i){
        while(1);
        (void)i;
@@ -51,89 +34,58 @@ void _exit(int i){
 
 void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
 {
-       uartf=1;
-}
-#else
-void killHandler(int i)
-{
-       printf("%i caught, Bye...\n", i);
-       exit(1);
-}
-
-void usage(FILE *o, char *arg0){
-       fprintf(o, "Usage: %s [opts]\n\nOptions\n"
-               "-p PORT  Custom port number, default: 8123\n" , arg0);
+       sending = false;
 }
-#endif
 
-//Interface things
-long millis() {
-#ifdef STM32F767xx
-       return HAL_GetTick();
-#else
-       if (gettimeofday(&tv1, NULL) == -1)
-               pdie("gettimeofday");
-       return tv1.tv_sec*1000 + tv1.tv_usec/1000;
-#endif
+int i = 0;
+void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
+{
+       if(writepos == BUFSIZE){
+               writepos = 0;
+       }
+       circbuf[writepos++] = readb;
+       HAL_UART_Receive_DMA(&huart3, &readb, 1);
 }
 
-bool input_available(){
-#ifdef STM32F767xx
-       return true;
-#else
-       struct timeval tv;
-       fd_set fds;
-       tv.tv_sec = 0;
-       tv.tv_usec = 0;
-       FD_ZERO(&fds);
-       FD_SET(fd, &fds);
-       if (select(fd+1, &fds, NULL, NULL, &tv) == -1)
-               pdie("select");
-       return FD_ISSET(fd, &fds);
-#endif
+bool input_available()
+{
+       return readpos != writepos;
 }
 
 uint8_t read_byte()
 {
-#ifdef STM32F767xx
-       HAL_UART_Receive(&huart3, &bt, 1, 1000);
-       return 0;
-#else
-       read(fd, &bt, 1);
-       return bt;
-#endif
+       while(sending){
+               HAL_Delay(5);
+       }
+       if(readpos == BUFSIZE){
+               readpos = 0;
+       }
+       return circbuf[readpos++];
 }
 
 void write_byte(uint8_t b)
 {
-#ifdef STM32F767xx
-       HAL_UART_Transmit_DMA(&huart3, &b, 1);
-#else
-       write(fd, &b, 1);
-#endif
+       while(sending){
+               HAL_Delay(5);
+       }
+       sending = true;
+       HAL_UART_Transmit_IT(&huart3, &b, 1);
 }
 
 void write_dpin(uint8_t i, bool b)
 {
-#ifdef STM32F767xx
-#else
-       debug("dwrite %d: %d\n", i, b);
-#endif
+       (void) i;
+       (void) b;
 }
 
 bool read_dpin(uint8_t i)
 {
-#ifdef STM32F767xx
-       return false;
-#else
-       debug("dread %d\n", i);
-       return false;
-#endif
+       return 0;
+       (void) i;
 }
 
 void write_apin(uint8_t i, uint8_t a)
 {
-#ifdef STM32F767xx
        if(i == 1){
                SET_LED_RED;
                RESET_LED_BLUE;
@@ -147,76 +99,27 @@ void write_apin(uint8_t i, uint8_t a)
                RESET_LED_BLUE;
                SET_LED_GREEN;
        }
-#else
-       debug("awrite %d: %d\n", i, a);
-#endif
 }
 
 uint8_t read_apin(uint8_t i)
 {
-#ifdef STM32F767xx
-       return 0;
-#else
-       debug("aread %d\n", i);
        return 0;
-#endif
+       (void) i;
+}
+
+long millis(){
+       return HAL_GetTick();
 }
 
 void delay(long ms)
 {
-#ifdef STM32F767xx
        HAL_Delay(ms);
-#else
-       usleep(ms*1000);
-#endif
 }
 
 void setup()
 {
-#ifdef STM32F767xx
-#else
-       int port = 8123, opti = 1;
-       //Register signal handler
-       if(signal(SIGINT, killHandler) == SIG_ERR){
-               die("Couldn't register signal handler...\n");
-       }
-       if(signal(SIGTERM, killHandler) == SIG_ERR){
-               die("Couldn't register signal handler...\n");
-       }
-       //Command line arguments
-       while(opti < gargc){
-               if(strcmp((*gargv)+opti, "-h") == 0){
-                       usage(stdout, gargv[0]);
-                       exit(EXIT_SUCCESS);
-               } else if(strcmp(gargv[opti], "-p") == 0 && opti+1<gargc){
-                       port = atoi(gargv[++opti]);
-                       if(port < 1)
-                               die("Port numbers are > 1\n");
-               } else {
-                       usage(stderr, gargv[0]);
-                       exit(EXIT_FAILURE);
-               }
-               opti++;
-       }
-
-       //Open file descriptors
-       struct sockaddr_in sa;
-
-       memset(&sa, 0, sizeof(sa));
-       sa.sin_family = AF_INET;
-       sa.sin_addr.s_addr = INADDR_ANY;
-       sa.sin_port = htons(port);
-
-       if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
-               pdie("socket");
-       if(bind(sock_fd, (struct sockaddr*)&sa, sizeof(sa)) == -1)
-               pdie("bind");
-       if(listen(sock_fd, 10) == -1)
-               pdie("listen");
-
-       printf("Listening on %d\n", port);
-       fflush(stdout);
-       if((fd = accept(sock_fd, (struct sockaddr*)NULL, NULL)) == -1)
-               pdie("accept");
-#endif
+       readpos = 0;
+       writepos = 0;
+       HAL_UART_Receive_DMA(&huart3, &readb, 1);
+       write_byte('b');
 }