diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2020-04-16 20:46:12 +0200 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2020-04-20 10:19:14 +0200 |
commit | d1f674cc2c05412ea9fa0de21cab777711c6331a (patch) | |
tree | 20a0c5ab27eb781bff288e28c37c90741aae97aa /src/common | |
parent | 2706d31fb899f79b1e7d95b3b3c6c7f154841173 (diff) | |
download | glutte-o-matic-d1f674cc2c05412ea9fa0de21cab777711c6331a.tar.gz glutte-o-matic-d1f674cc2c05412ea9fa0de21cab777711c6331a.tar.bz2 glutte-o-matic-d1f674cc2c05412ea9fa0de21cab777711c6331a.zip |
Add echo functionality for coulomb counter
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/Core/main.c | 40 | ||||
-rw-r--r-- | src/common/GPIO/usart.c | 64 | ||||
-rw-r--r-- | src/common/GPIO/usart.h | 11 |
3 files changed, 102 insertions, 13 deletions
diff --git a/src/common/Core/main.c b/src/common/Core/main.c index 54cee12..1321517 100644 --- a/src/common/Core/main.c +++ b/src/common/Core/main.c @@ -1,7 +1,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2019 Matthias P. Braendli, Maximilien Cuony + * Copyright (c) 2020 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 @@ -25,6 +25,7 @@ #include <stdio.h> #include <stdlib.h> #include <stdint.h> +#include <string.h> #include <math.h> /* Kernel includes. */ @@ -85,6 +86,7 @@ void init(void); static void detect_button_press(void *pvParameters); static void exercise_fsm(void *pvParameters); static void nf_analyse(void *pvParameters); +static void read_in_coulomb_counter(void *pvParameters); static void gps_monit_task(void *pvParameters); static void launcher_task(void *pvParameters); @@ -233,6 +235,20 @@ static void launcher_task(void __attribute__ ((unused))*pvParameters) trigger_fault(FAULT_SOURCE_MAIN); } + usart_debug_puts("TaskCC init\r\n"); + + xTaskCreate( + read_in_coulomb_counter, + "TaskCC", + 2*configMINIMAL_STACK_SIZE, + (void*) NULL, + tskIDLE_PRIORITY + 3UL, + &task_handle); + + if (!task_handle) { + trigger_fault(FAULT_SOURCE_MAIN); + } + usart_debug_puts("Init done.\r\n"); int last_qrp_from_supply = 0; @@ -698,6 +714,28 @@ static void nf_analyse(void __attribute__ ((unused))*pvParameters) } } +static char ccounter_msg[MAX_CCOUNTER_SENTENCE_LEN]; +static void read_in_coulomb_counter(void __attribute__ ((unused))*pvParameters) +{ + while (1) { + int ok = usart_get_ccounter_msg(ccounter_msg); + if (ok) { + size_t len = strlen(ccounter_msg); + /* Ignore if \n follows \r or not, as that should never happen, and in any case + * we don't want to send the \r or whatever could come after + */ + if (len > 2 && ccounter_msg[len-2] == '\r') { + ccounter_msg[len-2] = '\0'; + } + usart_debug_puts_header("CC:", ccounter_msg); + } + else { + usart_debug_puts("WARNING: Read from ccounter queue failed\r\n"); + vTaskDelay(5000 / portTICK_PERIOD_MS); + } + } +} + #if configGENERATE_RUN_TIME_STATS #include "stm32f4xx_conf.h" #include "stm32f4xx_tim.h" diff --git a/src/common/GPIO/usart.c b/src/common/GPIO/usart.c index 8bcd80a..6796142 100644 --- a/src/common/GPIO/usart.c +++ b/src/common/GPIO/usart.c @@ -1,7 +1,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2016 Matthias P. Braendli, Maximilien Cuony + * Copyright (c) 2020 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 @@ -33,23 +33,32 @@ #include "task.h" #include "queue.h" -// The ISR writes into this buffer +// The ISRs write into these buffer, for NMEA input and Coulomb counter static char nmea_sentence[MAX_NMEA_SENTENCE_LEN]; static int nmea_sentence_last_written = 0; - +static char ccounter_msg[MAX_CCOUNTER_SENTENCE_LEN]; +static int ccounter_msg_last_written = 0; // Once a completed NMEA sentence is received in the ISR, // it is appended to this queue static QueueHandle_t usart_nmea_queue; +// Once a full line (ending in \r\n) is received on USART2, +// it is appended to this queue +static QueueHandle_t usart_ccounter_queue; + void usart_gps_init() { usart_nmea_queue = xQueueCreate(15, MAX_NMEA_SENTENCE_LEN); if (usart_nmea_queue == 0) { while(1); /* fatal error */ } - usart_gps_specific_init(); + usart_ccounter_queue = xQueueCreate(5, MAX_CCOUNTER_SENTENCE_LEN); + if (usart_ccounter_queue == 0) { + while(1); /* fatal error */ + } + usart_gps_specific_init(); } void usart_gps_puts(const char* str) { @@ -117,25 +126,60 @@ void usart_debug_puts_header(const char* hdr, const char* str) { xTaskResumeAll(); } -int usart_get_nmea_sentence(char* nmea) { +int usart_get_nmea_sentence(char *nmea) { return xQueueReceive(usart_nmea_queue, nmea, portMAX_DELAY); } +int usart_get_ccounter_msg(char *msg) { + return xQueueReceive(usart_ccounter_queue, msg, portMAX_DELAY); +} -static void usart_clear_nmea_buffer(void) { - for (int i = 0; i < MAX_NMEA_SENTENCE_LEN; i++) { - nmea_sentence[i] = '\0'; +static void usart_clear_ccounter_buffer(void) { + for (int i = 0; i < MAX_CCOUNTER_SENTENCE_LEN; i++) { + ccounter_msg[i] = '\0'; } - nmea_sentence_last_written = 0; + ccounter_msg_last_written = 0; } void usart_process_char(char c) { // Warning: running in interrupt context - usart_debug("Unknown command %c\r\n", c); + BaseType_t require_context_switch = pdFALSE; + + if (ccounter_msg_last_written < MAX_CCOUNTER_SENTENCE_LEN) { + ccounter_msg[ccounter_msg_last_written] = c; + ccounter_msg_last_written++; + + if (c == '\n') { + int success = xQueueSendToBackFromISR( + usart_ccounter_queue, + ccounter_msg, + &require_context_switch); + + if (success == pdFALSE) { + trigger_fault(FAULT_SOURCE_USART); + } + + usart_clear_ccounter_buffer(); + } + } + else { + // Buffer overrun without a meaningful message. + usart_clear_ccounter_buffer(); + } + + portYIELD_FROM_ISR(require_context_switch); +} + +static void usart_clear_nmea_buffer(void) { + for (int i = 0; i < MAX_NMEA_SENTENCE_LEN; i++) { + nmea_sentence[i] = '\0'; + } + nmea_sentence_last_written = 0; } void usart_gps_process_char(char c) { +// Warning: running in interrupt context BaseType_t require_context_switch = pdFALSE; if (nmea_sentence_last_written == 0) { diff --git a/src/common/GPIO/usart.h b/src/common/GPIO/usart.h index 681d86f..8d16b10 100644 --- a/src/common/GPIO/usart.h +++ b/src/common/GPIO/usart.h @@ -1,7 +1,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2016 Matthias P. Braendli, Maximilien Cuony + * Copyright (c) 2020 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 @@ -25,7 +25,8 @@ /* 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. + * It also handles the debug USART 2 to send messages to the PC and + * receiv measurements from the Glutte-batteries coulomb counter. */ #ifndef __USART_H_ @@ -41,6 +42,8 @@ #define MAX_NMEA_SENTENCE_LEN 256 +#define MAX_CCOUNTER_SENTENCE_LEN 64 + // Initialise USART2 for PC debugging void usart_init(void); @@ -74,5 +77,9 @@ void usart_gps_process_char(char); void usart_puts(USART_TypeDef*, const char*); +// Get a MAX_CCOUNTER_SENTENCE_LEN sized message from Coulomb counter +// Return 1 on success +int usart_get_ccounter_msg(char *msg); + #endif //__USART_H_ |