add external libraries and update stm version
[mTask.git] / int / nucleo-f767-blinky / src / main.c
index 5ebac62..0be5c0f 100644 (file)
 #include "mTaskSymbols.h"
 #include "sds.h"
 #include "task.h"
-#include "misc.h"
+#include "interface.h"
 
 #define MSG_GET_TASK 't'
 #define MSG_DEL_TASK 'd'
 #define MSG_SDS_SPEC 's'
 #define MSG_SDS_UPD 'u'
 
-void _exit(int i){
-       while(1);
-       (void)i;
-}
-
-//Globals
-#ifdef STM32F767xx
-volatile char uartf = 0;
-#else
-struct timeval tv1;
-int sock_fd = -1;
-int fd = -1;
-int *argc;
-char **argv;
-#endif
-uint8_t bt;
-
-#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
-
-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
-}
-
-bool input_available(){
-#ifdef STM32F767xx
-       return false;
-#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
-}
-
-uint8_t read_byte()
-{
-#ifdef STM32F767xx
-       HAL_UART_Receive(&huart3, &bt, 1, 1000);
-       return 0;
-#else
-       read(fd, &bt, 1);
-       return bt;
-#endif
-}
-
-void write_byte(uint8_t b)
-{
-#ifdef STM32F767xx
-       HAL_UART_Transmit_DMA(&huart3, &b, 1);
-#else
-       write(fd, &b, 1);
-#endif
-}
-
-void delay(int ms)
-{
-#ifdef STM32F767xx
-       HAL_Delay(ms);
-#else
-       usleep(ms*1000);
-#endif
-}
-
-#ifndef STM32F767xx
-void killHandler(int i)
-{
-       printf("%i caught, Bye...\n", i);
-       exit(1);
-}
-#endif
-
-#ifdef STM32F767xx
-void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
-{
-       uartf=1;
-}
-#endif
-
 void read_message()
 {
        //Find next task
@@ -155,72 +59,15 @@ void read_message()
        }
 }
 
-void usage(FILE *o, char *arg0){
-       fprintf(o, "Usage: %s [opts]\n\nOptions\n"
-               "-p PORT  Custom port number, default: 8123\n" , arg0);
-}
-
-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 < *argc){
-               if(strcmp((*argv)+opti, "-h") == 0){
-                       usage(stdout, argv[0]);
-                       exit(EXIT_SUCCESS);
-               } else if(strcmp(argv[opti], "-p") == 0 && opti+1<*argc){
-                       port = atoi(argv[++opti]);
-                       if(port < 1)
-                               die("Port numbers are > 1\n");
-               } else {
-                       usage(stderr, argv[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
-
-       //Initialize systems
-       sds_init();
-       task_init();
-}
-
 void loop()
 {
        int ct;
        long cyclestart;
        struct task *curtask;
+
        if(input_available())
                read_message();
+
        //Run tasks
        cyclestart = millis();
        for(ct = 0; ct<MAXTASKS; ct++){
@@ -234,36 +81,35 @@ void loop()
 //                     debug("Task %d not scheduled\n", ct);
                        continue;
                }
-#ifdef DEBUG
-               printf("Current task to run: %d\n", ct);
-               getchar();
-#endif
+               debug("Current task to run: %d\n", ct);
                run_task(curtask);
+               curtask->lastrun = cyclestart;
+               write_byte('\n');
        }
-       write_byte('\n');
 }
 
 #ifdef STM32F767xx
-int main1(void)
+char s[128] = "";
+int main1(void){
 #else
-int main(int ac, char *av[])
-#endif
-{
-#ifndef STM32F767xx
-       argc = &ac;
-       argv = av;
+int main(int argc, char *argv[]){
+       gargc = argc;
+       gargv = argv;
 #endif
+       debug("booting up\r\n");
+
+       //Initialize systems
        setup();
+       sds_init();
+       task_init();
 
        write_byte('\n');
 
        while(true){
                //Check for new tasks
+               debug("loop\r\n");
                loop();
-               debug("Waiting for 500ms\n");
-               delay(500);
-               debug("done waiting\n");
-               delay(500);
+               delay(100);
        }
        return 0;
 }