From a5c6b47a3e645aa8ce326e00c80e8bf10d515215 Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Sat, 21 Jan 2017 11:25:15 +0100 Subject: [PATCH] sidestep --- clean-platform | 2 +- int/nucleo-f767-blinky/Makefile | 2 +- .../cube/nucleo-f767-blinky/Src/usart.c | 2 +- int/nucleo-f767-blinky/src/Makefile | 6 +- int/nucleo-f767-blinky/src/interface.c | 189 ++++-------------- int/nucleo-f767-blinky/src/interface.h | 24 ++- int/nucleo-f767-blinky/src/interface_linux.c | 145 ++++++++++++++ int/nucleo-f767-blinky/src/main.c | 28 ++- int/nucleo-f767-blinky/src/serial.h | 12 ++ 9 files changed, 241 insertions(+), 169 deletions(-) create mode 100644 int/nucleo-f767-blinky/src/interface_linux.c create mode 100644 int/nucleo-f767-blinky/src/serial.h diff --git a/clean-platform b/clean-platform index e79aa3e..d43071c 160000 --- a/clean-platform +++ b/clean-platform @@ -1 +1 @@ -Subproject commit e79aa3e3f1704c704efc67655e6588a185578de1 +Subproject commit d43071ccf6d724eb075c93cfa37a5216a5cd990c diff --git a/int/nucleo-f767-blinky/Makefile b/int/nucleo-f767-blinky/Makefile index d5e3f74..8b4f425 100644 --- a/int/nucleo-f767-blinky/Makefile +++ b/int/nucleo-f767-blinky/Makefile @@ -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 diff --git a/int/nucleo-f767-blinky/cube/nucleo-f767-blinky/Src/usart.c b/int/nucleo-f767-blinky/cube/nucleo-f767-blinky/Src/usart.c index 73e6a18..2dfca4b 100644 --- a/int/nucleo-f767-blinky/cube/nucleo-f767-blinky/Src/usart.c +++ b/int/nucleo-f767-blinky/cube/nucleo-f767-blinky/Src/usart.c @@ -51,7 +51,7 @@ void MX_USART3_UART_Init(void) { huart3.Instance = USART3; - huart3.Init.BaudRate = 115200; + huart3.Init.BaudRate = 9600; huart3.Init.WordLength = UART_WORDLENGTH_8B; huart3.Init.StopBits = UART_STOPBITS_1; huart3.Init.Parity = UART_PARITY_NONE; diff --git a/int/nucleo-f767-blinky/src/Makefile b/int/nucleo-f767-blinky/src/Makefile index 17dd912..65d780d 100644 --- a/int/nucleo-f767-blinky/src/Makefile +++ b/int/nucleo-f767-blinky/src/Makefile @@ -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) diff --git a/int/nucleo-f767-blinky/src/interface.c b/int/nucleo-f767-blinky/src/interface.c index 70e3326..260919c 100644 --- a/int/nucleo-f767-blinky/src/interface.c +++ b/int/nucleo-f767-blinky/src/interface.c @@ -3,10 +3,10 @@ #include #include -#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 @@ -17,34 +17,16 @@ #define SET_LED_GREEN GPIOB->BSRR = GPIO_PIN_0 #define RESET_LED_GREEN GPIOB->BSRR = GPIO_PIN_0 << 16 -#else -#include -#include -#include -#include -#include -#include -#include -#include -#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 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'); } diff --git a/int/nucleo-f767-blinky/src/interface.h b/int/nucleo-f767-blinky/src/interface.h index 8b29921..a01905e 100644 --- a/int/nucleo-f767-blinky/src/interface.h +++ b/int/nucleo-f767-blinky/src/interface.h @@ -10,16 +10,25 @@ #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 +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#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 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"); +} diff --git a/int/nucleo-f767-blinky/src/main.c b/int/nucleo-f767-blinky/src/main.c index b00f73a..42c483e 100644 --- a/int/nucleo-f767-blinky/src/main.c +++ b/int/nucleo-f767-blinky/src/main.c @@ -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 index 0000000..99a7c38 --- /dev/null +++ b/int/nucleo-f767-blinky/src/serial.h @@ -0,0 +1,12 @@ +#ifndef SERIAL_H +#define SERIAL_H + +#include + +extern volatile char uartf; + +void init(); +bool input_av(); +uint8_t read_byte(); +void write_byte(uint8_t b); +#endif -- 2.20.1