diff options
Diffstat (limited to 'src/common/GPIO')
-rw-r--r-- | src/common/GPIO/analog.c | 69 | ||||
-rw-r--r-- | src/common/GPIO/analog.h | 50 | ||||
-rw-r--r-- | src/common/GPIO/i2c.h | 53 | ||||
-rw-r--r-- | src/common/GPIO/leds.h | 9 | ||||
-rw-r--r-- | src/common/GPIO/pio.h | 47 | ||||
-rw-r--r-- | src/common/GPIO/temperature.c | 62 | ||||
-rw-r--r-- | src/common/GPIO/temperature.h | 40 | ||||
-rw-r--r-- | src/common/GPIO/usart.c | 171 | ||||
-rw-r--r-- | src/common/GPIO/usart.h | 78 |
9 files changed, 579 insertions, 0 deletions
diff --git a/src/common/GPIO/analog.c b/src/common/GPIO/analog.c new file mode 100644 index 0000000..b6f75a6 --- /dev/null +++ b/src/common/GPIO/analog.c @@ -0,0 +1,69 @@ +/* + * 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 "GPIO/analog.h" + +#define SUPPLY_HISTORY_LEN 10 + +static float supply_history[SUPPLY_HISTORY_LEN]; +static int supply_history_ix = 0; +static int supply_history_ready = 0; +static int last_qrp = 0; + +// Return 1 if analog supply is too low +int analog_supply_too_low(void) +{ + + // Hysteresis: + // Lower voltage 12.5V + // High voltage 13V + const float measure = analog_measure_12v(); + + supply_history[supply_history_ix] = measure; + supply_history_ix++; + if (supply_history_ix >= SUPPLY_HISTORY_LEN) { + supply_history_ix = 0; + supply_history_ready = 1; + } + + if (supply_history_ready) { + float sum = 0.0f; + for (int i = 0; i < SUPPLY_HISTORY_LEN; i++) { + sum += supply_history[i]; + } + int above_lower_limit = (sum > 12.5f * SUPPLY_HISTORY_LEN); + int above_upper_limit = (sum > 13.0f * SUPPLY_HISTORY_LEN); + + if (!above_lower_limit) { + last_qrp = 1; + } + else if (above_upper_limit) { + last_qrp = 0; + } + + return last_qrp; + } + + return 0; +} diff --git a/src/common/GPIO/analog.h b/src/common/GPIO/analog.h new file mode 100644 index 0000000..ddc19ac --- /dev/null +++ b/src/common/GPIO/analog.h @@ -0,0 +1,50 @@ +/* + * 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. +*/ + +#pragma once +#include <stdint.h> + +void analog_init(void); + +/* Measure the 12V supply voltage, in 0.5V increments. + * Returns 0.0f in case of error + * + * Warning, do not run from interrupt context! + */ +float analog_measure_12v(void); + +/* Measure SWR, and return voltages in mV. + * Returns 0 in case of error, 1 in case of success + * + * Warning, do not run from interrupt context! + */ +int analog_measure_swr(int *forward_mv, int* reflected_mv); + +/* Keep an average of measurements, and decide if the repeater should enter + * QRP. Returns 1 if low power must be activated + * + * Warning, do not run from interrupt context! + */ +int analog_supply_too_low(void); + diff --git a/src/common/GPIO/i2c.h b/src/common/GPIO/i2c.h new file mode 100644 index 0000000..69a4ad2 --- /dev/null +++ b/src/common/GPIO/i2c.h @@ -0,0 +1,53 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015 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. +*/ + +#ifndef __I2C_H_ +#define __I2C_H_ + +#include <stdint.h> + +/* Initialise I2C on the board for both the audio codec and the GPS receiver */ +void i2c_init(void); + +/* Do an I2C write, return 1 on success, 0 on failure */ +int i2c_write(uint8_t device, const uint8_t *txbuf, int len); + +/* Do an I2C read into rxbuf. + * Returns number of bytes received, or 0 in case of failure + */ +int i2c_read(uint8_t device, uint8_t *rxbuf, int len); + +/* Do an I2C write for the address, and then a read into rxbuf. + * Returns number of bytes received, or 0 in case of failure + */ +int i2c_read_from(uint8_t device, uint8_t address, uint8_t *rxbuf, int len); + +/* Start an I2C transaction, keeping exclusive access to I2C */ +void i2c_transaction_start(void); + +/* End an I2C transaction, unlocking the exclusive access to I2C */ +void i2c_transaction_end(void); + +#endif // __I2C_H_ + diff --git a/src/common/GPIO/leds.h b/src/common/GPIO/leds.h new file mode 100644 index 0000000..70c57d2 --- /dev/null +++ b/src/common/GPIO/leds.h @@ -0,0 +1,9 @@ +#pragma once + +#define LED_GREEN 1 +#define LED_ORANGE 2 +#define LED_RED 3 +#define LED_BLUE 4 + +void leds_turn_off(int); +void leds_turn_on(int); diff --git a/src/common/GPIO/pio.h b/src/common/GPIO/pio.h new file mode 100644 index 0000000..e118fc8 --- /dev/null +++ b/src/common/GPIO/pio.h @@ -0,0 +1,47 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015 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. +*/ + +#ifndef _PIO_H_ +#define _PIO_H_ + +#include <stddef.h> +#include <stdint.h> +#include "Core/fsm.h" + +void pio_init(void); + +void pio_set_tx(int on); +void pio_set_mod_off(int mod_off); +void pio_set_qrp(int on); +void pio_set_gps_epps(int on); +void pio_set_fax(int on); +void pio_set_det_1750(int on); +void pio_set_sq2(int on); + +void pio_set_fsm_signals(struct fsm_input_signals_t* sig); + +int pio_read_button(void); + +#endif // _PIO_H_ + diff --git a/src/common/GPIO/temperature.c b/src/common/GPIO/temperature.c new file mode 100644 index 0000000..f9e43cb --- /dev/null +++ b/src/common/GPIO/temperature.c @@ -0,0 +1,62 @@ +/* + * 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 "FreeRTOS.h" +#include "FreeRTOSConfig.h" +#include "task.h" +#include "Core/common.h" +#include "GPIO/temperature.h" + + +float _temperature_last_value; +int _temperature_valid; + + +void temperature_task(void *pvParameters); + + +void temperature_init() { + + xTaskCreate( + temperature_task, + "TaskTemperature", + 4*configMINIMAL_STACK_SIZE, + (void*) NULL, + tskIDLE_PRIORITY + 2UL, + NULL); +} + +// Return the current temperature +float temperature_get() { + if (_temperature_valid) { + return _temperature_last_value; + } else { + return 0.0f; + } +} + +// Return 1 if the temperature is valid +int temperature_valid() { + return _temperature_valid; +} diff --git a/src/common/GPIO/temperature.h b/src/common/GPIO/temperature.h new file mode 100644 index 0000000..f49cc26 --- /dev/null +++ b/src/common/GPIO/temperature.h @@ -0,0 +1,40 @@ +/* + * 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. +*/ + +#pragma once +#include <stdint.h> + +extern float _temperature_last_value; +extern int _temperature_valid; + +// Setup communication and temperature +void temperature_init(void); + +// Return 1 if the temperature is valid +int temperature_valid(void); + +// Get current temperature +float temperature_get(void); + +void temperature_task(void *); diff --git a/src/common/GPIO/usart.c b/src/common/GPIO/usart.c new file mode 100644 index 0000000..8bcd80a --- /dev/null +++ b/src/common/GPIO/usart.c @@ -0,0 +1,171 @@ +/* + * 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 <stdarg.h> +#include <string.h> +#include <inttypes.h> +#include "Core/common.h" +#include "GPIO/usart.h" +#include "GPIO/analog.h" +#include "FreeRTOS.h" +#include "task.h" +#include "queue.h" + +// The ISR writes into this buffer +static char nmea_sentence[MAX_NMEA_SENTENCE_LEN]; +static int nmea_sentence_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; + +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(); + +} + +void usart_gps_puts(const char* str) { + vTaskSuspendAll(); + usart_puts(USART3, str); + xTaskResumeAll(); +} + +#define MAX_MSG_LEN 80 +static char usart_debug_message[MAX_MSG_LEN]; + +void usart_debug_timestamp() { + // Don't call printf here, to reduce stack usage + uint64_t now = timestamp_now(); + if (now == 0) { + usart_puts(USART2, "[0] "); + } + else { + char ts_str[64]; + int i = 63; + + ts_str[i--] = '\0'; + ts_str[i--] = ' '; + ts_str[i--] = ']'; + + while (now > 0 && i >= 0) { + ts_str[i--] = '0' + (now % 10); + now /= 10; + } + ts_str[i] = '['; + + usart_puts(USART2, &ts_str[i]); + } +} + +void usart_debug(const char *format, ...) { + va_list list; + va_start(list, format); + vsnprintf(usart_debug_message, MAX_MSG_LEN-1, format, list); + + vTaskSuspendAll(); + usart_debug_timestamp(); + usart_puts(USART2, usart_debug_message); + xTaskResumeAll(); + + va_end(list); +} + +void usart_debug_puts(const char* str) { +#ifdef SIMULATOR + fprintf(stderr, "DEBUG: %s", str); +#endif + vTaskSuspendAll(); + usart_debug_timestamp(); + usart_puts(USART2, str); + xTaskResumeAll(); +} + +void usart_debug_puts_header(const char* hdr, const char* str) { + vTaskSuspendAll(); + usart_debug_timestamp(); + usart_puts(USART2, hdr); + usart_puts(USART2, str); + usart_puts(USART2, "\r\n"); + xTaskResumeAll(); +} + +int usart_get_nmea_sentence(char* nmea) { + return xQueueReceive(usart_nmea_queue, nmea, portMAX_DELAY); +} + + +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_process_char(char c) { +// Warning: running in interrupt context + usart_debug("Unknown command %c\r\n", c); +} + +void usart_gps_process_char(char c) +{ + BaseType_t require_context_switch = pdFALSE; + + if (nmea_sentence_last_written == 0) { + if (c == '$') { + // Likely new start of sentence + nmea_sentence[nmea_sentence_last_written] = c; + nmea_sentence_last_written++; + } + } + else if (nmea_sentence_last_written < MAX_NMEA_SENTENCE_LEN) { + nmea_sentence[nmea_sentence_last_written] = c; + nmea_sentence_last_written++; + + if (c == '\n') { + int success = xQueueSendToBackFromISR( + usart_nmea_queue, + nmea_sentence, + &require_context_switch); + + if (success == pdFALSE) { + trigger_fault(FAULT_SOURCE_USART); + } + + usart_clear_nmea_buffer(); + } + } + else { + // Buffer overrun without a meaningful NMEA message. + usart_clear_nmea_buffer(); + } + + portYIELD_FROM_ISR(require_context_switch); +} diff --git a/src/common/GPIO/usart.h b/src/common/GPIO/usart.h new file mode 100644 index 0000000..681d86f --- /dev/null +++ b/src/common/GPIO/usart.h @@ -0,0 +1,78 @@ +/* + * 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. +*/ + +/* 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_ + +#ifdef STM32F4XX +# include <stm32f4xx_usart.h> +#else +# define USART_TypeDef int +# define USART2 ((USART_TypeDef*)2) +# define USART3 ((USART_TypeDef*)3) +#endif + +#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); + +// Take GPS out of RESET +void usart_gps_remove_reset(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); +void usart_debug_puts_header(const char* hdr, const char* str); + +// Get a MAX_NMEA_SENTENCE_LEN sized NMEA sentence +// Return 1 on success +int usart_get_nmea_sentence(char* nmea); + +void usart_debug_timestamp(void); + +void usart_gps_specific_init(void); + +void usart_process_char(char); +void usart_gps_process_char(char); + +void usart_puts(USART_TypeDef*, const char*); + +#endif //__USART_H_ + |