sidestep
authorMart Lubbers <mart@martlubbers.net>
Sat, 21 Jan 2017 10:25:15 +0000 (11:25 +0100)
committerMart Lubbers <mart@martlubbers.net>
Sat, 21 Jan 2017 10:25:15 +0000 (11:25 +0100)
clean-platform
int/nucleo-f767-blinky/Makefile
int/nucleo-f767-blinky/cube/nucleo-f767-blinky/Src/usart.c
int/nucleo-f767-blinky/src/Makefile
int/nucleo-f767-blinky/src/interface.c
int/nucleo-f767-blinky/src/interface.h
int/nucleo-f767-blinky/src/interface_linux.c [new file with mode: 0644]
int/nucleo-f767-blinky/src/main.c
int/nucleo-f767-blinky/src/serial.h [new file with mode: 0644]

index e79aa3e..d43071c 160000 (submodule)
@@ -1 +1 @@
-Subproject commit e79aa3e3f1704c704efc67655e6588a185578de1
+Subproject commit d43071ccf6d724eb075c93cfa37a5216a5cd990c
index d5e3f74..8b4f425 100644 (file)
@@ -41,7 +41,7 @@ include $(COMDIR)/pj/oocd-stm32f7xx.mk
 OCD = $(OCD-nucleo)
 
 #add sources  
-SRCS_C := $(wildcard $(SRCDIR)/*.c) 
+SRCS_C := $(filter-out src/interface_linux.c,$(wildcard $(SRCDIR)/*.c))
 SRCS_S := $(wildcard $(SRCDIR)/*.s) 
 
 #include common project makefile
index 73e6a18..2dfca4b 100644 (file)
@@ -51,7 +51,7 @@ void MX_USART3_UART_Init(void)
 {\r
 \r
   huart3.Instance = USART3;\r
-  huart3.Init.BaudRate = 115200;\r
+  huart3.Init.BaudRate = 9600;\r
   huart3.Init.WordLength = UART_WORDLENGTH_8B;\r
   huart3.Init.StopBits = UART_STOPBITS_1;\r
   huart3.Init.Parity = UART_PARITY_NONE;\r
index 17dd912..65d780d 100644 (file)
@@ -4,9 +4,9 @@ OBJS:=interpret.o sds.o task.o main.o interface.o
 
 all: mTaskSymbols.h $(PROG)
 
-%.o: %.cpp
-       gcc $(CFLAGS) -c $< -o $@
-       
+interface.o: interface_linux.c
+       gcc $(CFLAGS) -c $< -o $@ 
+
 $(PROG): $(OBJS)
        gcc $(LDFLAGS) -o $@ $(OBJS)
        
index 70e3326..260919c 100644 (file)
@@ -3,10 +3,10 @@
 #include <stdlib.h>
 #include <string.h>
 
-#ifdef STM32F767xx
 #include "stm32f7xx_hal.h"
 #include "usart.h"
 #include "gpio.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_GREEN GPIOB->BSRR = GPIO_PIN_0
 #define RESET_LED_GREEN GPIOB->BSRR = GPIO_PIN_0 << 16
 
-#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 "interface.h"
+#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;
@@ -52,97 +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
-       if(HAL_UART_Receive(&huart3, &bt, 1, 1) == HAL_TIMEOUT)
-               return false;
-       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");
-       if(!FD_ISSET(fd, &fds))
-               return false;
-       read(fd, &bt, 1);
-       return true;
-#endif
+bool input_available()
+{
+       return readpos != writepos;
 }
 
 uint8_t read_byte()
 {
-#ifdef STM32F767xx
-       HAL_UART_Receive(&huart3, &bt, 1, 1000);
-       return bt;
-#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", 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
+       return 0;
        (void) i;
 }
 
 void write_apin(uint8_t i, uint8_t a)
 {
-#ifdef STM32F767xx
        if(i == 1){
                SET_LED_RED;
                RESET_LED_BLUE;
@@ -156,79 +99,27 @@ void write_apin(uint8_t i, uint8_t a)
                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;
 }
 
+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...");
-       }
-       if(signal(SIGTERM, killHandler) == SIG_ERR){
-               die("Couldn't register signal handler...");
-       }
-       //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");
-               } 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');
 }
index 8b29921..a01905e 100644 (file)
 #include "stm32f7xx_hal.h"
 #include "gpio.h"
 #include "usart.h"
+#include "serial.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
+
 #endif
 
 #ifdef STM32F767xx
-extern volatile char uartf;
 extern char buf[128];
 #else
 extern int gargc;
 extern char **gargv;
 #endif
-extern uint8_t bt;
 
 uint8_t read_byte();
 void write_byte(uint8_t b);
@@ -38,10 +47,13 @@ void setup();
 
 #define read16() 256*read_byte() + read_byte()
 #ifdef STM32F767xx
-#define debug(s, ...) {\
-               sprintf(buf, "m" s "\r\n", ##__VA_ARGS__);\
-               HAL_UART_Transmit(&huart3, (uint8_t*)buf, strlen(buf), 1000);\
-       }
+#define debug(s, ...) ;
+//#define debug(s, ...) {\
+//             sprintf(buf, "m" s "\n", ##__VA_ARGS__);\
+//             for(int _i = 0; _i<strlen(buf); _i++){\
+//                     write_byte(buf[_i]);\
+//             }\
+//     }
 #define pdie(s) ;
 #define die(s, ...) ;
 #else
diff --git a/int/nucleo-f767-blinky/src/interface_linux.c b/int/nucleo-f767-blinky/src/interface_linux.c
new file mode 100644 (file)
index 0000000..548730c
--- /dev/null
@@ -0,0 +1,145 @@
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#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>
+
+#include "interface.h"
+struct timeval tv1;
+int sock_fd = -1;
+int fd = -1;
+int gargc;
+char **gargv;
+
+uint8_t bt;
+
+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);
+}
+
+long millis() {
+       if (gettimeofday(&tv1, NULL) == -1)
+               pdie("gettimeofday");
+       return tv1.tv_sec*1000 + tv1.tv_usec/1000;
+}
+
+bool input_available(){
+       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");
+       if(!FD_ISSET(fd, &fds))
+               return false;
+       read(fd, &bt, 1);
+       return true;
+}
+
+uint8_t read_byte()
+{
+       read(fd, &bt, 1);
+       return bt;
+}
+
+void write_byte(uint8_t b)
+{
+       write(fd, &b, 1);
+}
+
+void write_dpin(uint8_t i, bool b)
+{
+       debug("dwrite %d: %d", i, b);
+       (void) i;
+       (void) b;
+}
+
+bool read_dpin(uint8_t i)
+{
+       debug("dread %d", i);
+       return false;
+       (void) i;
+}
+
+void write_apin(uint8_t i, uint8_t a)
+{
+       debug("awrite %d: %d", i, a);
+       (void) a;
+       (void) i;
+}
+
+uint8_t read_apin(uint8_t i)
+{
+       debug("aread %d", i);
+       return 0;
+       (void) i;
+}
+
+void delay(long ms)
+{
+       usleep(ms*1000);
+}
+
+void setup()
+{
+       int port = 8123, opti = 1;
+       //Register signal handler
+       if(signal(SIGINT, killHandler) == SIG_ERR){
+               die("Couldn't register signal handler...");
+       }
+       if(signal(SIGTERM, killHandler) == SIG_ERR){
+               die("Couldn't register signal handler...");
+       }
+       //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");
+               } 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");
+}
index b00f73a..42c483e 100644 (file)
@@ -33,8 +33,9 @@ void read_message()
 {
        //Find next task
        if(input_available()){
-               //debug("Receiving input: %c\n", c);
-               switch(bt){
+               uint8_t c = read_byte();
+               debug("Receiving input: %c\n", c);
+               switch(c){
                case MSG_SDS_SPEC:
                        debug("Receiving an sds");
                        sds_register();
@@ -57,7 +58,7 @@ void read_message()
                case '\n':
                        break;
                default:
-                       debug("Unknown message: %X", bt);
+                       debug("Unknown message: %X", c);
                }
        }
 }
@@ -91,29 +92,40 @@ void loop()
 }
 
 #ifdef STM32F767xx
-char s[128] = "";
 int main1(void){
-       uint8_t bt;
-       HAL_UART_Receive(&huart3, &bt, 1, 100000);
 #else
 int main(int argc, char *argv[]){
        gargc = argc;
        gargv = argv;
 #endif
-       debug("booting up");
 
        //Initialize systems
        setup();
        sds_init();
        task_init();
 
-       write_byte('\n');
+       //debug("booting up");
 
+       int i = 0;
        while(true){
                //Check for new tasks
 //             debug("loop\r\n");
                loop();
                delay(100);
+               i = (i + 1) % 3;
+               if(i == 0){
+                       SET_LED_RED;
+                       RESET_LED_BLUE;
+                       RESET_LED_GREEN;
+               } else if(i == 1){
+                       RESET_LED_RED;
+                       SET_LED_BLUE;
+                       RESET_LED_GREEN;
+               } else if(i == 2){
+                       RESET_LED_RED;
+                       RESET_LED_BLUE;
+                       SET_LED_GREEN;
+               }
        }
        return 0;
 }
diff --git a/int/nucleo-f767-blinky/src/serial.h b/int/nucleo-f767-blinky/src/serial.h
new file mode 100644 (file)
index 0000000..99a7c38
--- /dev/null
@@ -0,0 +1,12 @@
+#ifndef SERIAL_H
+#define SERIAL_H
+
+#include <stdint.h>
+
+extern volatile char uartf;
+
+void init();
+bool input_av();
+uint8_t read_byte();
+void write_byte(uint8_t b);
+#endif