aboutsummaryrefslogtreecommitdiffstats
path: root/src/common
diff options
context:
space:
mode:
authorMaximilien Cuony <maximilien@theglu.org>2016-05-31 21:58:17 +0200
committerMaximilien Cuony <maximilien@theglu.org>2016-05-31 21:58:17 +0200
commit9069fc127e4f73041fbd1f66e4506fcf12418315 (patch)
tree902aa478c6e9d47cde89eaa996b5f7f662070518 /src/common
parent4803231e214fbd19eab4ba2289583859ee07183f (diff)
downloadglutte-o-matic-9069fc127e4f73041fbd1f66e4506fcf12418315.tar.gz
glutte-o-matic-9069fc127e4f73041fbd1f66e4506fcf12418315.tar.bz2
glutte-o-matic-9069fc127e4f73041fbd1f66e4506fcf12418315.zip
Simuator - Work in progress
Diffstat (limited to 'src/common')
-rw-r--r--src/common/includes/Core/common.h61
-rw-r--r--src/common/includes/Core/usart.h57
-rw-r--r--src/common/src/Core/FreeRTOSConfig.h164
-rw-r--r--src/common/src/Core/common.c231
-rw-r--r--src/common/src/Core/main.c399
5 files changed, 912 insertions, 0 deletions
diff --git a/src/common/includes/Core/common.h b/src/common/includes/Core/common.h
new file mode 100644
index 0000000..b483dfb
--- /dev/null
+++ b/src/common/includes/Core/common.h
@@ -0,0 +1,61 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Matthias P. Braendli
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+*/
+
+/* A set of common routines for internal timekeeping and a LFSR to generate
+ * a random number
+ */
+
+#pragma once
+
+#include <stdint.h>
+#include <time.h>
+
+/* Feature defines */
+#define ENABLE_PSK31 0
+
+#define FLOAT_PI 3.1415926535897932384f
+
+void common_init(void);
+
+// Return the current timestamp in milliseconds. Timestamps are monotonic, and not
+// wall clock time.
+uint64_t timestamp_now(void);
+
+// Calculate local time from GPS time, including daylight saving time
+// Return 1 on success, 0 on failure
+// A call to this function will invalidate the information inside 'time'
+// regardless of return value.
+int local_time(struct tm *time);
+
+// Return either 0 or 1, somewhat randomly
+int random_bool(void);
+
+// Fault handling mechanism
+#define FAULT_SOURCE_MAIN 1
+#define FAULT_SOURCE_GPS 2
+#define FAULT_SOURCE_I2C 3
+#define FAULT_SOURCE_USART 4
+void trigger_fault(int source);
+
+void hard_fault_handler_extra();
diff --git a/src/common/includes/Core/usart.h b/src/common/includes/Core/usart.h
new file mode 100644
index 0000000..9d9b59c
--- /dev/null
+++ b/src/common/includes/Core/usart.h
@@ -0,0 +1,57 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Matthias P. Braendli
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+*/
+
+/* This handles the USART 3 to the GPS receiver, and fills a queue of
+ * NMEA messages.
+ *
+ * It also handles the debug USART 2 and allows sending messages to the PC.
+ */
+
+#ifndef __USART_H_
+#define __USART_H_
+
+#define MAX_NMEA_SENTENCE_LEN 256
+
+// Initialise USART2 for PC debugging
+void usart_init(void);
+
+// Initialise USART3 for GPS and NMEA queue
+// Needs running scheduler
+void usart_gps_init(void);
+
+// Send the str to the GPS receiver
+void usart_gps_puts(const char* str);
+
+// a printf to send data to the PC
+void usart_debug(const char *format, ...);
+
+// Send a string to the PC
+void usart_debug_puts(const char* str);
+
+// Get a MAX_NMEA_SENTENCE_LEN sized NMEA sentence
+// Return 1 on success
+int usart_get_nmea_sentence(char* nmea);
+
+#endif //__USART_H_
+
diff --git a/src/common/src/Core/FreeRTOSConfig.h b/src/common/src/Core/FreeRTOSConfig.h
new file mode 100644
index 0000000..b58e2a2
--- /dev/null
+++ b/src/common/src/Core/FreeRTOSConfig.h
@@ -0,0 +1,164 @@
+/*
+ FreeRTOS V8.0.0:rc2 - Copyright (C) 2014 Real Time Engineers Ltd.
+ All rights reserved
+
+ VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
+
+ ***************************************************************************
+ * *
+ * FreeRTOS provides completely free yet professionally developed, *
+ * robust, strictly quality controlled, supported, and cross *
+ * platform software that has become a de facto standard. *
+ * *
+ * Help yourself get started quickly and support the FreeRTOS *
+ * project by purchasing a FreeRTOS tutorial book, reference *
+ * manual, or both from: http://www.FreeRTOS.org/Documentation *
+ * *
+ * Thank you! *
+ * *
+ ***************************************************************************
+
+ This file is part of the FreeRTOS distribution.
+
+ FreeRTOS is free software; you can redistribute it and/or modify it under
+ the terms of the GNU General Public License (version 2) as published by the
+ Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
+
+ >>! NOTE: The modification to the GPL is included to allow you to distribute
+ >>! a combined work that includes FreeRTOS without being obliged to provide
+ >>! the source code for proprietary components outside of the FreeRTOS
+ >>! kernel.
+
+ FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ FOR A PARTICULAR PURPOSE. Full license text is available from the following
+ link: http://www.freertos.org/a00114.html
+
+ 1 tab == 4 spaces!
+
+ ***************************************************************************
+ * *
+ * Having a problem? Start by reading the FAQ "My application does *
+ * not run, what could be wrong?" *
+ * *
+ * http://www.FreeRTOS.org/FAQHelp.html *
+ * *
+ ***************************************************************************
+
+ http://www.FreeRTOS.org - Documentation, books, training, latest versions,
+ license and Real Time Engineers Ltd. contact details.
+
+ http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
+ including FreeRTOS+Trace - an indispensable productivity tool, a DOS
+ compatible FAT file system, and our tiny thread aware UDP/IP stack.
+
+ http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
+ Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
+ licenses offer ticketed support, indemnification and middleware.
+
+ http://www.SafeRTOS.com - High Integrity Systems also provide a safety
+ engineered and independently SIL3 certified version for use in safety and
+ mission critical applications that require provable dependability.
+
+ 1 tab == 4 spaces!
+*/
+
+
+#ifndef FREERTOS_CONFIG_H
+#define FREERTOS_CONFIG_H
+
+/*-----------------------------------------------------------
+ * Application specific definitions.
+ *
+ * These definitions should be adjusted for your particular hardware and
+ * application requirements.
+ *
+ * THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE
+ * FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE.
+ *
+ * See http://www.freertos.org/a00110.html.
+ *----------------------------------------------------------*/
+
+/* Ensure stdint is only used by the compiler, and not the assembler. */
+//#ifdef __ICCARM__
+ #include <stdint.h>
+ extern uint32_t SystemCoreClock;
+//#endif
+
+#define configUSE_PREEMPTION 1
+#define configUSE_IDLE_HOOK 0 // Default: 1
+#define configUSE_TICK_HOOK 0 // Default: 1
+#define configCPU_CLOCK_HZ ( SystemCoreClock )
+#define configTICK_RATE_HZ ( ( portTickType ) 250 )
+#define configMAX_PRIORITIES ( 5 )
+#define configMINIMAL_STACK_SIZE ( ( unsigned short ) 130 )
+#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 75 * 1024 ) )
+#define configMAX_TASK_NAME_LEN ( 10 )
+#define configUSE_TRACE_FACILITY 1
+#define configUSE_16_BIT_TICKS 0
+#define configIDLE_SHOULD_YIELD 1
+#define configUSE_TICKLESS_IDLE 1
+#define configUSE_MUTEXES 1
+#define configQUEUE_REGISTRY_SIZE 8
+#define configUSE_RECURSIVE_MUTEXES 1
+#define configUSE_MALLOC_FAILED_HOOK 0 // Default: 1
+#define configUSE_APPLICATION_TASK_TAG 0
+#define configUSE_COUNTING_SEMAPHORES 1
+#define configGENERATE_RUN_TIME_STATS 0
+
+/* Co-routine definitions. */
+#define configUSE_CO_ROUTINES 0
+#define configMAX_CO_ROUTINE_PRIORITIES ( 2 )
+
+/* Software timer definitions. */
+#define configUSE_TIMERS 1
+#define configTIMER_TASK_PRIORITY ( 2 )
+#define configTIMER_QUEUE_LENGTH 10
+#define configTIMER_TASK_STACK_DEPTH ( configMINIMAL_STACK_SIZE * 2 )
+
+/* Set the following definitions to 1 to include the API function, or zero
+to exclude the API function. */
+#define INCLUDE_vTaskPrioritySet 1
+#define INCLUDE_uxTaskPriorityGet 1
+#define INCLUDE_vTaskDelete 1
+#define INCLUDE_vTaskCleanUpResources 1
+#define INCLUDE_vTaskSuspend 1
+#define INCLUDE_vTaskDelayUntil 1
+#define INCLUDE_vTaskDelay 1
+
+/* Cortex-M specific definitions. */
+#ifdef __NVIC_PRIO_BITS
+ /* __BVIC_PRIO_BITS will be specified when CMSIS is being used. */
+ #define configPRIO_BITS __NVIC_PRIO_BITS
+#else
+ #define configPRIO_BITS 4 /* 15 priority levels */
+#endif
+
+/* The lowest interrupt priority that can be used in a call to a "set priority"
+function. */
+#define configLIBRARY_LOWEST_INTERRUPT_PRIORITY 0xf
+
+/* The highest interrupt priority that can be used by any interrupt service
+routine that makes calls to interrupt safe FreeRTOS API functions. DO NOT CALL
+INTERRUPT SAFE FREERTOS API FUNCTIONS FROM ANY INTERRUPT THAT HAS A HIGHER
+PRIORITY THAN THIS! (higher priorities are lower numeric values. */
+#define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY 5
+
+/* Interrupt priorities used by the kernel port layer itself. These are generic
+to all Cortex-M ports, and do not rely on any particular library functions. */
+#define configKERNEL_INTERRUPT_PRIORITY ( configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
+/* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!!
+See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */
+#define configMAX_SYSCALL_INTERRUPT_PRIORITY ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
+
+/* Normal assert() semantics without relying on the provision of an assert.h
+header file. */
+#define configASSERT( x ) if( ( x ) == 0 ) { taskDISABLE_INTERRUPTS(); for( ;; ); }
+
+/* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS
+standard names. */
+#define vPortSVCHandler SVC_Handler
+#define xPortPendSVHandler PendSV_Handler
+#define xPortSysTickHandler SysTick_Handler
+
+#endif /* FREERTOS_CONFIG_H */
diff --git a/src/common/src/Core/common.c b/src/common/src/Core/common.c
new file mode 100644
index 0000000..2eee88f
--- /dev/null
+++ b/src/common/src/Core/common.c
@@ -0,0 +1,231 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Matthias P. Braendli
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+*/
+
+#include "Core/common.h"
+#include "Core/usart.h"
+#include "FreeRTOS.h"
+#include "timers.h"
+/* #include "Core/gps.h" */
+#include <time.h>
+
+static uint64_t common_timestamp = 0; // milliseconds since startup
+static TimerHandle_t common_timer;
+
+// The LFSR is used as random number generator
+static const uint16_t lfsr_start_state = 0x12ABu;
+static uint16_t lfsr;
+
+static void common_increase_timestamp(TimerHandle_t t);
+
+int find_last_sunday(const struct tm* time)
+{
+ struct tm t = *time;
+
+ // the last sunday can never be before the 20th
+ t.tm_mday = 20;
+
+ int last_sunday = 1;
+
+ while (t.tm_mon == time->tm_mon) {
+ t.tm_mday++;
+ if (mktime(&t) == (time_t)-1) {
+ // TODO error
+ return -1;
+ }
+
+ const int sunday = 0;
+ if (t.tm_wday == sunday) {
+ last_sunday = t.tm_mday;
+ }
+ }
+
+ return last_sunday;
+}
+
+/* Calculate if we are in daylight saving time.
+ * return 0 if false
+ * 1 if true
+ * -1 in case of error
+ */
+static int is_dst(const struct tm *time)
+{
+ /* DST from 01:00 UTC on last Sunday in March
+ * to 01:00 UTC on last Sunday in October
+ */
+ const int march = 2;
+ const int october = 9;
+ if (time->tm_mon < march) {
+ return 0;
+ }
+ else if (time->tm_mon == march) {
+ int last_sunday = find_last_sunday(time);
+ if (last_sunday == -1) return -1;
+
+ if (time->tm_mday < last_sunday) {
+ return 0;
+ }
+ else if (time->tm_mday == last_sunday) {
+ return (time->tm_hour < 1) ? 0 : 1;
+ }
+ else {
+ return 1;
+ }
+ }
+ else if (time->tm_mon > march && time->tm_mon < october) {
+ return 1;
+ }
+ else if (time->tm_mon == october) {
+ int last_sunday = find_last_sunday(time);
+ if (last_sunday == -1) return -1;
+
+ if (time->tm_mday < last_sunday) {
+ return 1;
+ }
+ else if (time->tm_mday == last_sunday) {
+ return (time->tm_hour < 1) ? 1 : 0;
+ }
+ else {
+ return 0;
+ }
+ }
+ else {
+ return 0;
+ }
+}
+
+int local_time(struct tm *time)
+{
+ const int local_time_offset=1; // hours
+
+ int valid = 0; // TODO gps_utctime(time);
+
+ if (valid) {
+ time->tm_hour += local_time_offset;
+
+ if (is_dst(time)) {
+ time->tm_hour++;
+ time->tm_isdst = 1;
+ }
+
+ // Let mktime fix the struct tm *time
+ if (mktime(time) == (time_t)-1) {
+ // TODO inform about failure
+ valid = 0;
+ }
+ }
+
+ return valid;
+}
+
+
+void common_init(void)
+{
+ common_timer = xTimerCreate("Timer",
+ portTICK_PERIOD_MS,
+ pdTRUE, // Auto-reload
+ NULL, // No unique id
+ common_increase_timestamp
+ );
+
+ xTimerStart(common_timer, 0);
+
+ lfsr = lfsr_start_state;
+}
+
+static void common_increase_timestamp(TimerHandle_t t)
+{
+ common_timestamp++;
+}
+
+uint64_t timestamp_now(void)
+{
+ return common_timestamp;
+}
+
+
+// Return either 0 or 1, somewhat randomly
+int random_bool(void)
+{
+ uint16_t bit;
+
+ /* taps: 16 14 13 11; feedback polynomial: x^16 + x^14 + x^13 + x^11 + 1 */
+ bit = ((lfsr >> 0) ^ (lfsr >> 2) ^ (lfsr >> 3) ^ (lfsr >> 5) ) & 1;
+ lfsr = (lfsr >> 1) | (bit << 15);
+
+ return bit;
+}
+
+// For the debugger
+static int faultsource = 0;
+void trigger_fault(int source)
+{
+ usart_debug("Fatal: %d", source);
+
+ __disable_irq();
+
+ faultsource = source;
+
+ while (1) {}
+}
+
+void hard_fault_handler_c(uint32_t *hardfault_args)
+{
+ uint32_t stacked_r0;
+ uint32_t stacked_r1;
+ uint32_t stacked_r2;
+ uint32_t stacked_r3;
+ uint32_t stacked_r12;
+ uint32_t stacked_lr;
+ uint32_t stacked_pc;
+ uint32_t stacked_psr;
+
+ stacked_r0 = hardfault_args[0];
+ stacked_r1 = hardfault_args[1];
+ stacked_r2 = hardfault_args[2];
+ stacked_r3 = hardfault_args[3];
+
+ stacked_r12 = hardfault_args[4];
+ stacked_lr = hardfault_args[5];
+ stacked_pc = hardfault_args[6];
+ stacked_psr = hardfault_args[7];
+
+ usart_debug_puts("\n\n[Hard fault handler - all numbers in hex]\n");
+ usart_debug("R0 = %x\n", stacked_r0);
+ usart_debug("R1 = %x\n", stacked_r1);
+ usart_debug("R2 = %x\n", stacked_r2);
+ usart_debug("R3 = %x\n", stacked_r3);
+ usart_debug("R12 = %x\n", stacked_r12);
+ usart_debug("LR [R14] = %x subroutine call return address\n", stacked_lr);
+ usart_debug("PC [R15] = %x program counter\n", stacked_pc);
+ usart_debug("PSR = %x\n", stacked_psr);
+ usart_debug("BFAR = %x\n", (*((volatile unsigned long *)(0xE000ED38))));
+ usart_debug("CFSR = %x\n", (*((volatile unsigned long *)(0xE000ED28))));
+ usart_debug("HFSR = %x\n", (*((volatile unsigned long *)(0xE000ED2C))));
+ usart_debug("DFSR = %x\n", (*((volatile unsigned long *)(0xE000ED30))));
+ usart_debug("AFSR = %x\n", (*((volatile unsigned long *)(0xE000ED3C))));
+
+ hard_fault_handler_extra();
+
+ while (1);
+}
diff --git a/src/common/src/Core/main.c b/src/common/src/Core/main.c
new file mode 100644
index 0000000..ac41355
--- /dev/null
+++ b/src/common/src/Core/main.c
@@ -0,0 +1,399 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Matthias P. Braendli, Maximilien Cuony
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <math.h>
+
+/* Kernel includes. */
+#include "FreeRTOS.h"
+#include "task.h"
+#include "timers.h"
+#include "semphr.h"
+
+/* Includes */
+/* #include "audio.h" */
+/* #include "cw.h" */
+/* #include "pio.h" */
+/* #include "i2c.h" */
+/* #include "gps.h" */
+/* #include "fsm.h" */
+/* #include "common.h" */
+#include "Core/usart.h"
+/* #include "delay.h" */
+/* #include "temperature.h" */
+#include "vc.h"
+
+
+// Private variables
+static int tm_trigger_button = 0;
+static int tm_trigger = 0;
+
+// Private function prototypes
+void init();
+
+// Tasks
+static void detect_button_press(void *pvParameters);
+static void exercise_fsm(void *pvParameters);
+static void gps_monit_task(void *pvParameters);
+static void launcher_task(void *pvParameters);
+
+// Audio callback function
+static void audio_callback(void* context, int select_buffer);
+
+void vApplicationStackOverflowHook( TaskHandle_t xTask,
+ signed char *pcTaskName )
+{
+ usart_debug("TASK OVERFLOW %s\r\n", pcTaskName);
+ while (1) {};
+}
+
+int main(void) {
+ init();
+ /* delay_init(); */
+ /* usart_init(); */
+ usart_debug_puts("\r\n******* glutt-o-matique version " GIT_VERSION " *******\r\n");
+ /* */
+ /* if (RCC_GetFlagStatus(RCC_FLAG_IWDGRST) != RESET) */
+ /* { */
+ /* usart_debug_puts("WARNING: A IWDG Reset occured!\r\n"); */
+ /* } */
+ /* RCC_ClearFlag(); */
+ /* */
+ /* TaskHandle_t task_handle; */
+ /* xTaskCreate( */
+ /* launcher_task, */
+ /* "Launcher", */
+ /* configMINIMAL_STACK_SIZE, */
+ /* (void*) NULL, */
+ /* tskIDLE_PRIORITY + 2UL, */
+ /* &task_handle); */
+ /* */
+ /* if (!task_handle) { */
+ /* trigger_fault(FAULT_SOURCE_MAIN); */
+ /* } */
+ /* */
+ /* #<{(| Start the RTOS Scheduler |)}># */
+ /* vTaskStartScheduler(); */
+ /* */
+ /* #<{(| HALT |)}># */
+ while(1);
+}
+
+// Launcher task is here to make sure the scheduler is
+// already running when calling the init functions.
+static void launcher_task(void *pvParameters)
+{
+ /* usart_debug_puts("CW init\r\n"); */
+ /* cw_psk31_init(16000); */
+ /* */
+ /* usart_debug_puts("PIO init\r\n"); */
+ /* pio_init(); */
+ /* */
+ /* usart_debug_puts("I2C init\r\n"); */
+ /* i2c_init(); */
+ /* */
+ /* usart_debug_puts("common init\r\n"); */
+ /* common_init(); */
+ /* */
+ /* usart_debug_puts("GPS init\r\n"); */
+ /* gps_init(); */
+ /* */
+ /* usart_debug_puts("DS18B20 init\r\n"); */
+ /* temperature_init(); */
+ /* */
+ /* usart_debug_puts("TaskButton init\r\n"); */
+ /* */
+ /* TaskHandle_t task_handle; */
+ /* xTaskCreate( */
+ /* detect_button_press, */
+ /* "TaskButton", */
+ /* 4*configMINIMAL_STACK_SIZE, */
+ /* (void*) NULL, */
+ /* tskIDLE_PRIORITY + 2UL, */
+ /* &task_handle); */
+ /* */
+ /* if (!task_handle) { */
+ /* trigger_fault(FAULT_SOURCE_MAIN); */
+ /* } */
+ /* */
+ /* usart_debug_puts("TaskFSM init\r\n"); */
+ /* */
+ /* xTaskCreate( */
+ /* exercise_fsm, */
+ /* "TaskFSM", */
+ /* 4*configMINIMAL_STACK_SIZE, */
+ /* (void*) NULL, */
+ /* tskIDLE_PRIORITY + 2UL, */
+ /* &task_handle); */
+ /* */
+ /* if (!task_handle) { */
+ /* trigger_fault(FAULT_SOURCE_MAIN); */
+ /* } */
+ /* */
+ /* usart_debug_puts("TaskGPS init\r\n"); */
+ /* */
+ /* xTaskCreate( */
+ /* gps_monit_task, */
+ /* "TaskGPSMonit", */
+ /* 4*configMINIMAL_STACK_SIZE, */
+ /* (void*) NULL, */
+ /* tskIDLE_PRIORITY + 2UL, */
+ /* &task_handle); */
+ /* */
+ /* if (!task_handle) { */
+ /* trigger_fault(FAULT_SOURCE_MAIN); */
+ /* } */
+ /* */
+ /* usart_debug_puts("Audio init\r\n"); */
+ /* */
+ /* InitializeAudio(Audio16000HzSettings); */
+ /* */
+ /* usart_debug_puts("Audio set volume\r\n"); */
+ /* SetAudioVolume(210); */
+ /* */
+ /* usart_debug_puts("Audio set callback\r\n"); */
+ /* PlayAudioWithCallback(audio_callback, NULL); */
+ /* */
+ /* // By default, let's the audio off to save power */
+ /* AudioOff(); */
+ /* */
+ /* usart_debug_puts("Init done.\r\n"); */
+ /* */
+ /* #<{(| We are done now, suspend this task */
+ /* * With FreeDOS' heap_1.c, we cannot delete it. */
+ /* * See freertos.org -> More Advanced... -> Memory Management */
+ /* * for more info. */
+ /* |)}># */
+ /* while (1) { */
+ /* vTaskSuspend(NULL); */
+ /* } */
+}
+
+static void detect_button_press(void *pvParameters)
+{
+ /* int pin_high_count = 0; */
+ /* int last_pin_high_count = 0; */
+ /* const int pin_high_thresh = 10; */
+ /* while (1) { */
+ /* if (GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0) == Bit_SET) { */
+ /* if (pin_high_count < pin_high_thresh) { */
+ /* pin_high_count++; */
+ /* } */
+ /* } */
+ /* else { */
+ /* if (pin_high_count > 0) { */
+ /* pin_high_count--; */
+ /* } */
+ /* } */
+ /* */
+ /* vTaskDelay(10 / portTICK_RATE_MS); #<{(| Debounce Delay |)}># */
+ /* */
+ /* if (pin_high_count == pin_high_thresh && */
+ /* last_pin_high_count != pin_high_count) { */
+ /* tm_trigger_button = 1; */
+ /* usart_debug_puts("Bouton bleu\r\n"); */
+ /* */
+ /* if (temperature_valid()) { */
+ /* */
+ /* float temp = temperature_get(); */
+ /* */
+ /* usart_debug("Temperature %f\r\n", temp); */
+ /* */
+ /* } else { */
+ /* usart_debug_puts("No temp\r\n"); */
+ /* } */
+ /* } */
+ /* else if (pin_high_count == 0 && */
+ /* last_pin_high_count != pin_high_count) { */
+ /* tm_trigger_button = 0; */
+ /* } */
+ /* */
+ /* last_pin_high_count = pin_high_count; */
+ /* } */
+}
+
+static void audio_callback(void* context, int select_buffer)
+{
+ /* static int16_t audio_buffer0[AUDIO_BUF_LEN]; */
+ /* static int16_t audio_buffer1[AUDIO_BUF_LEN]; */
+ /* int16_t *samples; */
+ /* */
+ /* if (select_buffer == 0) { */
+ /* samples = audio_buffer0; */
+ /* GPIO_ResetBits(GPIOD, GPIOD_BOARD_LED_RED); */
+ /* select_buffer = 1; */
+ /* } else { */
+ /* samples = audio_buffer1; */
+ /* GPIO_SetBits(GPIOD, GPIOD_BOARD_LED_RED); */
+ /* select_buffer = 0; */
+ /* } */
+ /* */
+ /* size_t samples_len = cw_psk31_fill_buffer(samples, AUDIO_BUF_LEN); */
+ /* */
+ /* if (samples_len == 0) { */
+ /* for (int i = 0; i < AUDIO_BUF_LEN; i++) { */
+ /* samples[i] = 0; */
+ /* } */
+ /* */
+ /* samples_len = AUDIO_BUF_LEN; */
+ /* } */
+ /* */
+ /* ProvideAudioBufferWithoutBlocking(samples, samples_len); */
+}
+
+/* static struct tm gps_time; */
+static void gps_monit_task(void *pvParameters) {
+/* GPIO_SetBits(GPIOD, GPIOD_BOARD_LED_BLUE); */
+/* */
+/* int t_gps_print_latch = 0; */
+/* */
+/* while (1) { */
+/* struct tm time; */
+/* int time_valid = local_time(&time); */
+/* */
+/* if (time_valid) { */
+/* if (time.tm_sec % 4 >= 2) { */
+/* GPIO_SetBits(GPIOD, GPIOD_BOARD_LED_BLUE); */
+/* } */
+/* else { */
+/* GPIO_ResetBits(GPIOD, GPIOD_BOARD_LED_BLUE); */
+/* } */
+/* */
+/* // Even hours: tm_trigger=1, odd hours: tm_trigger=0 */
+/* tm_trigger = (time.tm_hour + 1) % 2; */
+/* } */
+/* */
+/* gps_utctime(&gps_time); */
+/* */
+/* if (gps_time.tm_sec % 30 == 0 && t_gps_print_latch == 0) { */
+/* usart_debug("T_GPS %04d-%02d-%02d %02d:%02d:%02d\r\n", */
+/* gps_time.tm_year, gps_time.tm_mon, gps_time.tm_mday, */
+/* gps_time.tm_hour, gps_time.tm_min, gps_time.tm_sec); */
+/* */
+/* usart_debug("TIME %04d-%02d-%02d %02d:%02d:%02d\r\n", */
+/* time.tm_year, time.tm_mon, time.tm_mday, */
+/* time.tm_hour, time.tm_min, time.tm_sec); */
+/* */
+/* t_gps_print_latch = 1; */
+/* } */
+/* if (gps_time.tm_sec % 30 > 0) { */
+/* t_gps_print_latch = 0; */
+/* } */
+/* */
+/* vTaskDelay(100 / portTICK_RATE_MS); */
+/* */
+/* // Reload watchdog */
+/* IWDG_ReloadCounter(); */
+/* } */
+}
+
+/* static struct fsm_input_signals_t fsm_input; */
+static void exercise_fsm(void *pvParameters)
+{
+ /* int cw_last_trigger = 0; */
+ /* int last_tm_trigger = 0; */
+ /* int last_tm_trigger_button = 0; */
+ /* */
+ /* int last_sq = 0; */
+ /* int last_1750 = 0; */
+ /* int last_qrp = 0; */
+ /* int last_cw_done = 0; */
+ /* */
+ /* fsm_input.humidity = 0; */
+ /* fsm_input.temp = 15; */
+ /* fsm_input.swr_high = 0; */
+ /* fsm_input.sstv_mode = 0; */
+ /* fsm_input.wind_generator_ok = 1; */
+ /* while (1) { */
+ /* vTaskDelay(10 / portTICK_RATE_MS); */
+ /* */
+ /* pio_set_fsm_signals(&fsm_input); */
+ /* */
+ /* if (last_sq != fsm_input.sq) { */
+ /* last_sq = fsm_input.sq; */
+ /* usart_debug("In SQ %d\r\n", last_sq); */
+ /* } */
+ /* if (last_1750 != fsm_input.tone_1750) { */
+ /* last_1750 = fsm_input.tone_1750; */
+ /* usart_debug("In 1750 %d\r\n", last_1750); */
+ /* } */
+ /* if (last_qrp != fsm_input.qrp) { */
+ /* last_qrp = fsm_input.qrp; */
+ /* usart_debug("In QRP %d\r\n", last_qrp); */
+ /* } */
+ /* */
+ /* */
+ /* if (tm_trigger_button == 1 && last_tm_trigger_button == 0) { */
+ /* fsm_input.start_tm = 1; */
+ /* } */
+ /* last_tm_trigger_button = tm_trigger_button; */
+ /* */
+ /* if (tm_trigger == 1 && last_tm_trigger == 0) { */
+ /* fsm_input.start_tm = 1; */
+ /* } */
+ /* last_tm_trigger = tm_trigger; */
+ /* */
+ /* int cw_done = !cw_psk31_busy(); */
+ /* if (last_cw_done != cw_done) { */
+ /* usart_debug("In CW done %d\r\n", cw_done); */
+ /* last_cw_done = cw_done; */
+ /* */
+ /* fsm_input.cw_psk31_done = cw_done; */
+ /* } */
+ /* else { */
+ /* fsm_input.cw_psk31_done = 0; */
+ /* } */
+ /* */
+ /* if (fsm_input.cw_psk31_done) { */
+ /* GPIO_ResetBits(GPIOD, GPIOD_BOARD_LED_ORANGE); */
+ /* } */
+ /* else { */
+ /* GPIO_SetBits(GPIOD, GPIOD_BOARD_LED_ORANGE); */
+ /* } */
+ /* */
+ /* fsm_update_inputs(&fsm_input); */
+ /* fsm_update(); */
+ /* */
+ /* struct fsm_output_signals_t fsm_out; */
+ /* fsm_get_outputs(&fsm_out); */
+ /* */
+ /* pio_set_tx(fsm_out.tx_on); */
+ /* pio_set_mod_off(!fsm_out.modulation); */
+ /* pio_set_qrp(fsm_out.qrp); // TODO move out of FSM */
+ /* */
+ /* // Add message to CW generator only on rising edge of trigger */
+ /* if (fsm_out.cw_psk31_trigger && !cw_last_trigger) { */
+ /* cw_psk31_push_message(fsm_out.msg, fsm_out.cw_dit_duration, fsm_out.msg_frequency); */
+ /* */
+ /* usart_debug_puts("Out CW trigger\r\n"); */
+ /* } */
+ /* cw_last_trigger = fsm_out.cw_psk31_trigger; */
+ /* */
+ /* if (fsm_out.ack_start_tm) { */
+ /* fsm_input.start_tm = 0; */
+ /* } */
+ /* } */
+}