update
[mTask.git] / int / nucleo-f767-blinky / src / interface.c
index 2f7fb57..70e3326 100644 (file)
@@ -5,8 +5,18 @@
 
 #ifdef STM32F767xx
 #include "stm32f7xx_hal.h"
-#include "gpio.h"
 #include "usart.h"
+#include "gpio.h"
+
+#define SET_LED_RED GPIOB->BSRR = GPIO_PIN_14
+#define RESET_LED_RED GPIOB->BSRR = GPIO_PIN_14 << 16
+
+#define SET_LED_BLUE GPIOB->BSRR = GPIO_PIN_7
+#define RESET_LED_BLUE GPIOB->BSRR = GPIO_PIN_7 << 16
+
+#define SET_LED_GREEN GPIOB->BSRR = GPIO_PIN_0
+#define RESET_LED_GREEN GPIOB->BSRR = GPIO_PIN_0 << 16
+
 #else
 #include <stdio.h>
 #include <netdb.h>
 
 #include "interface.h"
 
-#define SET_LED_RED GPIOB->BSRR = GPIO_PIN_14
-#define RESET_LED_RED GPIOB->BSRR = GPIO_PIN_14 << 16
-
-#define SET_LED_BLUE GPIOB->BSRR = GPIO_PIN_7
-#define RESET_LED_BLUE GPIOB->BSRR = GPIO_PIN_7 << 16
-
-#define SET_LED_GREEN GPIOB->BSRR = GPIO_PIN_0
-#define RESET_LED_GREEN GPIOB->BSRR = GPIO_PIN_0 << 16
-
 //Globals
 #ifdef STM32F767xx
 volatile char uartf = 0;
+char buf[128];
 #else
 struct timeval tv1;
 int sock_fd = -1;
@@ -78,7 +80,9 @@ long millis() {
 
 bool input_available(){
 #ifdef STM32F767xx
-       return false;
+       if(HAL_UART_Receive(&huart3, &bt, 1, 1) == HAL_TIMEOUT)
+               return false;
+       return true;
 #else
        struct timeval tv;
        fd_set fds;
@@ -88,7 +92,10 @@ bool input_available(){
        FD_SET(fd, &fds);
        if (select(fd+1, &fds, NULL, NULL, &tv) == -1)
                pdie("select");
-       return FD_ISSET(fd, &fds);
+       if(!FD_ISSET(fd, &fds))
+               return false;
+       read(fd, &bt, 1);
+       return true;
 #endif
 }
 
@@ -96,7 +103,7 @@ uint8_t read_byte()
 {
 #ifdef STM32F767xx
        HAL_UART_Receive(&huart3, &bt, 1, 1000);
-       return 0;
+       return bt;
 #else
        read(fd, &bt, 1);
        return bt;
@@ -112,6 +119,61 @@ void write_byte(uint8_t b)
 #endif
 }
 
+void write_dpin(uint8_t i, bool b)
+{
+#ifdef STM32F767xx
+#else
+       debug("dwrite %d: %d", i, b);
+#endif
+       (void) i;
+       (void) b;
+}
+
+bool read_dpin(uint8_t i)
+{
+#ifdef STM32F767xx
+       return false;
+#else
+       debug("dread %d", i);
+       return false;
+#endif
+       (void) i;
+}
+
+void write_apin(uint8_t i, uint8_t a)
+{
+#ifdef STM32F767xx
+       if(i == 1){
+               SET_LED_RED;
+               RESET_LED_BLUE;
+               RESET_LED_GREEN;
+       } else if(i == 2){
+               RESET_LED_RED;
+               SET_LED_BLUE;
+               RESET_LED_GREEN;
+       } else if(i == 3){
+               RESET_LED_RED;
+               RESET_LED_BLUE;
+               SET_LED_GREEN;
+       }
+#else
+       debug("awrite %d: %d", i, a);
+#endif
+       (void) a;
+       (void) i;
+}
+
+uint8_t read_apin(uint8_t i)
+{
+#ifdef STM32F767xx
+       return 0;
+#else
+       debug("aread %d", i);
+       return 0;
+#endif
+       (void) i;
+}
+
 void delay(long ms)
 {
 #ifdef STM32F767xx
@@ -128,10 +190,10 @@ void setup()
        int port = 8123, opti = 1;
        //Register signal handler
        if(signal(SIGINT, killHandler) == SIG_ERR){
-               die("Couldn't register signal handler...\n");
+               die("Couldn't register signal handler...");
        }
        if(signal(SIGTERM, killHandler) == SIG_ERR){
-               die("Couldn't register signal handler...\n");
+               die("Couldn't register signal handler...");
        }
        //Command line arguments
        while(opti < gargc){
@@ -141,7 +203,7 @@ void setup()
                } else if(strcmp(gargv[opti], "-p") == 0 && opti+1<gargc){
                        port = atoi(gargv[++opti]);
                        if(port < 1)
-                               die("Port numbers are > 1\n");
+                               die("Port numbers are > 1");
                } else {
                        usage(stderr, gargv[0]);
                        exit(EXIT_FAILURE);