From b19ee6f595a0cfa033d115fbeec6c367852fd65e Mon Sep 17 00:00:00 2001 From: Miguel Boing Date: Tue, 16 Jan 2024 16:46:35 -0300 Subject: [PATCH 001/103] firmware: drivers: si446x: Adding delay during configuration to avoid radio resets #145 --- firmware/drivers/si446x/si446x.c | 5 +++-- firmware/drivers/si446x/si446x_config.h | 20 ++++++++++---------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/firmware/drivers/si446x/si446x.c b/firmware/drivers/si446x/si446x.c index 5d986693..454276b0 100644 --- a/firmware/drivers/si446x/si446x.c +++ b/firmware/drivers/si446x/si446x.c @@ -108,12 +108,12 @@ void si446x_reg_config(void) /* Set RF parameter like frequency, data rate, etc. */ si446x_set_config(SI446X_CONFIGURATION_DATA, sizeof(SI446X_CONFIGURATION_DATA)); + uint8_t buf[2]; /* Frequency adjust (Tested manually) */ buf[0] = SI446X_XO_TUNE_REG_VALUE; si446x_set_properties(SI446X_PROPERTY_GLOBAL_XO_TUNE, buf, 1); - /* TX/RX shares 128 bytes FIFO */ buf[0] = 0x10; si446x_set_properties(SI446X_PROPERTY_GLOBAL_CONFIG, buf, 1); @@ -464,9 +464,10 @@ void si446x_set_config(const uint8_t *parameters, uint16_t para_len) cmd_len = parameters[pos++] - 1; /* Get command len */ cmd = parameters[pos++]; /* Get command */ memcpy(buffer, parameters + pos, cmd_len); /* Get parameters */ - + si446x_delay_ms(10); si446x_set_cmd(cmd, buffer, cmd_len); pos += cmd_len; + } } diff --git a/firmware/drivers/si446x/si446x_config.h b/firmware/drivers/si446x/si446x_config.h index 4e7933d9..6153a61b 100644 --- a/firmware/drivers/si446x/si446x_config.h +++ b/firmware/drivers/si446x/si446x_config.h @@ -1,34 +1,34 @@ /* * si446x_config.h - * + * * Copyright The TTC 2.0 Contributors. - * + * * This file is part of TTC 2.0. - * + * * TTC 2.0 is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. - * + * * TTC 2.0 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. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with TTC 2.0. If not, see . - * + * */ /** * \brief Si446x configuration. - * + * * \author Gabriel Mariano Marcelino - * + * * \version 0.1.23 - * + * * \date 2017/06/16 - * + * * \addtogroup si446x * \{ */ From ab7ad7dc5a3261ef7f39f2b4d19c5f955f4e5b71 Mon Sep 17 00:00:00 2001 From: Miguel Boing Date: Tue, 16 Jan 2024 16:47:55 -0300 Subject: [PATCH 002/103] firmware: devices: radio: Fixing radio_available function description #145 --- firmware/devices/radio/radio.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firmware/devices/radio/radio.h b/firmware/devices/radio/radio.h index 83ce7c58..5841277a 100644 --- a/firmware/devices/radio/radio.h +++ b/firmware/devices/radio/radio.h @@ -83,7 +83,7 @@ int radio_send(uint8_t *data, uint16_t len); int radio_recv(uint8_t *data, uint16_t len, uint32_t timeout_ms); /** - * \brief Verifies the number of availables bytes to receive. + * \brief Verifies if there are unread received bytes. * * \return The number of available bytes to receive (-1 on error). */ From 1be2e2f79a54dac7496ca55a96f63f40874ba4af Mon Sep 17 00:00:00 2001 From: Miguel Boing Date: Tue, 16 Jan 2024 16:49:14 -0300 Subject: [PATCH 003/103] firmware: app: tasks: Adding initial uplink manager task #145 --- firmware/app/tasks/uplink_manager.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/firmware/app/tasks/uplink_manager.c b/firmware/app/tasks/uplink_manager.c index b0f0161f..eb69d030 100644 --- a/firmware/app/tasks/uplink_manager.c +++ b/firmware/app/tasks/uplink_manager.c @@ -36,7 +36,7 @@ #include #include #include - +#include #include "uplink_manager.h" #include "startup.h" @@ -61,14 +61,23 @@ void vTaskUplinkManager(void) ttc_data_buf.up_buf.position_to_write = 0; uint16_t rx_size = 230; - uint8_t rx_packet[230]; - uint8_t pop_rx_packet[230]; + uint8_t rx_packet[230] = {0}; while(1) { TickType_t last_cycle = xTaskGetTickCount(); - /* TODO */ + if (radio_available() == 0) + { + radio_recv(rx_packet, 230, 100); + sys_log_print_event_from_module(SYS_LOG_INFO, TASK_UPLINK_MANAGER_NAME, "Received a new package:"); + sys_log_dump_hex(rx_packet, 230); + sys_log_new_line(); + + //ngham_decode(rx_packet); /* TODO */ + + uplink_add_packet(rx_packet, 220); + } vTaskDelayUntil(&last_cycle, pdMS_TO_TICKS(TASK_UPLINK_MANAGER_PERIOD_MS)); } From 30e109ecd052ac54fb8b06f75dbe53c28e54a354 Mon Sep 17 00:00:00 2001 From: miguelboing Date: Tue, 30 Jan 2024 05:20:12 -0300 Subject: [PATCH 004/103] firmware: config: Adding switching variable and vhf radio_configuration #147, #146 --- firmware/config/config.h | 4 + ...fig_Si4463.h => uhf_radio_config_Si4463.h} | 0 firmware/config/vhf_radio_config_Si4463.h | 632 ++++++++++++++++++ 3 files changed, 636 insertions(+) rename firmware/config/{radio_config_Si4463.h => uhf_radio_config_Si4463.h} (100%) create mode 100644 firmware/config/vhf_radio_config_Si4463.h diff --git a/firmware/config/config.h b/firmware/config/config.h index 394c9540..89d226ae 100644 --- a/firmware/config/config.h +++ b/firmware/config/config.h @@ -24,6 +24,7 @@ * \brief Configuration parameters definition. * * \author Gabriel Mariano Marcelino + * \author Miguel Boing * * \version 0.4.5 * @@ -36,6 +37,9 @@ #ifndef CONFIG_H_ #define CONFIG_H_ +/* Targets the radio module, it can be '0' or '1'*/ +#define RADIO_MODULE 1 + /* Tasks */ #define CONFIG_TASK_STARTUP_ENABLED 1 #define CONFIG_TASK_WATCHDOG_RESET_ENABLED 1 diff --git a/firmware/config/radio_config_Si4463.h b/firmware/config/uhf_radio_config_Si4463.h similarity index 100% rename from firmware/config/radio_config_Si4463.h rename to firmware/config/uhf_radio_config_Si4463.h diff --git a/firmware/config/vhf_radio_config_Si4463.h b/firmware/config/vhf_radio_config_Si4463.h new file mode 100644 index 00000000..5021815d --- /dev/null +++ b/firmware/config/vhf_radio_config_Si4463.h @@ -0,0 +1,632 @@ +/*! @file radio_config.h + * @brief This file contains the automatically generated + * configurations. + * + * @n WDS GUI Version: 3.2.11.0 + * @n Device: Si4463 Rev.: B1 + * + * @b COPYRIGHT + * @n Silicon Laboratories Confidential + * @n Copyright 2017 Silicon Laboratories, Inc. + * @n http://www.silabs.com + */ + +#ifndef RADIO_CONFIG_H_ +#define RADIO_CONFIG_H_ + +// USER DEFINED PARAMETERS +// Define your own parameters here + +// INPUT DATA +/* +// Crys_freq(Hz): 30000000 Crys_tol(ppm): 10 IF_mode: 2 High_perf_Ch_Fil: 1 OSRtune: 0 Ch_Fil_Bw_AFC: 0 ANT_DIV: 0 PM_pattern: 0 +// MOD_type: 3 Rsymb(sps): 4800 Fdev(Hz): 1200 RXBW(Hz): 150000 Manchester: 0 AFC_en: 0 Rsymb_error: 0.0 Chip-Version: 2 +// RF Freq.(MHz): 145.9 API_TC: 29 fhst: 0 inputBW: 0 BERT: 0 RAW_dout: 0 D_source: 0 Hi_pfm_div: 1 +// +// # RX IF frequency is -468750 Hz +// # WB filter 1 (BW = 9.54 kHz); NB-filter 1 (BW = 9.54 kHz) +// +// Modulation index: 0.5 +*/ + + +// CONFIGURATION PARAMETERS +#define RADIO_CONFIGURATION_DATA_RADIO_XO_FREQ 30000000L +#define RADIO_CONFIGURATION_DATA_CHANNEL_NUMBER 0x00 +#define RADIO_CONFIGURATION_DATA_RADIO_PACKET_LENGTH 0x07 +#define RADIO_CONFIGURATION_DATA_RADIO_STATE_AFTER_POWER_UP 0x03 +#define RADIO_CONFIGURATION_DATA_RADIO_DELAY_CNT_AFTER_RESET 0xF000 + + +// CONFIGURATION COMMANDS + +/* +// Command: RF_POWER_UP +// Description: Command to power-up the device and select the operational mode and functionality. +*/ +#define RF_POWER_UP 0x02, 0x01, 0x00, 0x01, 0xC9, 0xC3, 0x80 + +/* +// Command: RF_GPIO_PIN_CFG +// Description: Configures the GPIO pins. +*/ +#define RF_GPIO_PIN_CFG 0x13, 0x00, 0x63, 0x61, 0x60, 0x67, 0x4B, 0x00 + +/* +// Set properties: RF_GLOBAL_XO_TUNE_2 +// Number of properties: 2 +// Group ID: 0x00 +// Start ID: 0x00 +// Default values: 0x40, 0x00, +// Descriptions: +// GLOBAL_XO_TUNE - Configure the internal capacitor frequency tuning bank for the crystal oscillator. +// GLOBAL_CLK_CFG - Clock configuration options. +*/ +#define RF_GLOBAL_XO_TUNE_2 0x11, 0x00, 0x02, 0x00, 0x52, 0x00 + +/* +// Set properties: RF_GLOBAL_CONFIG_1 +// Number of properties: 1 +// Group ID: 0x00 +// Start ID: 0x03 +// Default values: 0x20, +// Descriptions: +// GLOBAL_CONFIG - Global configuration settings. +*/ +#define RF_GLOBAL_CONFIG_1 0x11, 0x00, 0x01, 0x03, 0x60 + +/* +// Set properties: RF_INT_CTL_ENABLE_1 +// Number of properties: 1 +// Group ID: 0x01 +// Start ID: 0x00 +// Default values: 0x04, +// Descriptions: +// INT_CTL_ENABLE - This property provides for global enabling of the three interrupt groups (Chip, Modem and Packet Handler) in order to generate HW interrupts at the NIRQ pin. +*/ +#define RF_INT_CTL_ENABLE_1 0x11, 0x01, 0x01, 0x00, 0x00 + +/* +// Set properties: RF_FRR_CTL_A_MODE_4 +// Number of properties: 4 +// Group ID: 0x02 +// Start ID: 0x00 +// Default values: 0x01, 0x02, 0x09, 0x00, +// Descriptions: +// FRR_CTL_A_MODE - Fast Response Register A Configuration. +// FRR_CTL_B_MODE - Fast Response Register B Configuration. +// FRR_CTL_C_MODE - Fast Response Register C Configuration. +// FRR_CTL_D_MODE - Fast Response Register D Configuration. +*/ +#define RF_FRR_CTL_A_MODE_4 0x11, 0x02, 0x04, 0x00, 0x01, 0x02, 0x03, 0x07 + +/* +// Set properties: RF_PREAMBLE_TX_LENGTH_9 +// Number of properties: 9 +// Group ID: 0x10 +// Start ID: 0x00 +// Default values: 0x08, 0x14, 0x00, 0x0F, 0x21, 0x00, 0x00, 0x00, 0x00, +// Descriptions: +// PREAMBLE_TX_LENGTH - Configure length of TX Preamble. +// PREAMBLE_CONFIG_STD_1 - Configuration of reception of a packet with a Standard Preamble pattern. +// PREAMBLE_CONFIG_NSTD - Configuration of transmission/reception of a packet with a Non-Standard Preamble pattern. +// PREAMBLE_CONFIG_STD_2 - Configuration of timeout periods during reception of a packet with Standard Preamble pattern. +// PREAMBLE_CONFIG - General configuration bits for the Preamble field. +// PREAMBLE_PATTERN_31_24 - Configuration of the bit values describing a Non-Standard Preamble pattern. +// PREAMBLE_PATTERN_23_16 - Configuration of the bit values describing a Non-Standard Preamble pattern. +// PREAMBLE_PATTERN_15_8 - Configuration of the bit values describing a Non-Standard Preamble pattern. +// PREAMBLE_PATTERN_7_0 - Configuration of the bit values describing a Non-Standard Preamble pattern. +*/ +#define RF_PREAMBLE_TX_LENGTH_9 0x11, 0x10, 0x09, 0x00, 0x04, 0x14, 0x00, 0x0F, 0x31, 0x00, 0x00, 0x00, 0x00 + +/* +// Set properties: RF_SYNC_CONFIG_5 +// Number of properties: 5 +// Group ID: 0x11 +// Start ID: 0x00 +// Default values: 0x01, 0x2D, 0xD4, 0x2D, 0xD4, +// Descriptions: +// SYNC_CONFIG - Sync Word configuration bits. +// SYNC_BITS_31_24 - Sync word. +// SYNC_BITS_23_16 - Sync word. +// SYNC_BITS_15_8 - Sync word. +// SYNC_BITS_7_0 - Sync word. +*/ +#define RF_SYNC_CONFIG_5 0x11, 0x11, 0x05, 0x00, 0x03, 0xBA, 0x67, 0x54, 0x7E + +/* +// Set properties: RF_PKT_CRC_CONFIG_7 +// Number of properties: 7 +// Group ID: 0x12 +// Start ID: 0x00 +// Default values: 0x00, 0x01, 0x08, 0xFF, 0xFF, 0x00, 0x00, +// Descriptions: +// PKT_CRC_CONFIG - Select a CRC polynomial and seed. +// PKT_WHT_POLY_15_8 - 16-bit polynomial value for the PN Generator (e.g., for Data Whitening) +// PKT_WHT_POLY_7_0 - 16-bit polynomial value for the PN Generator (e.g., for Data Whitening) +// PKT_WHT_SEED_15_8 - 16-bit seed value for the PN Generator (e.g., for Data Whitening) +// PKT_WHT_SEED_7_0 - 16-bit seed value for the PN Generator (e.g., for Data Whitening) +// PKT_WHT_BIT_NUM - Selects which bit of the LFSR (used to generate the PN / data whitening sequence) is used as the output bit for data scrambling. +// PKT_CONFIG1 - General configuration bits for transmission or reception of a packet. +*/ +#define RF_PKT_CRC_CONFIG_7 0x11, 0x12, 0x07, 0x00, 0x80, 0x01, 0x08, 0xFF, 0xFF, 0x00, 0x02 + +/* +// Set properties: RF_PKT_LEN_12 +// Number of properties: 12 +// Group ID: 0x12 +// Start ID: 0x08 +// Default values: 0x00, 0x00, 0x00, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +// Descriptions: +// PKT_LEN - Configuration bits for reception of a variable length packet. +// PKT_LEN_FIELD_SOURCE - Field number containing the received packet length byte(s). +// PKT_LEN_ADJUST - Provides for adjustment/offset of the received packet length value (in order to accommodate a variety of methods of defining total packet length). +// PKT_TX_THRESHOLD - TX FIFO almost empty threshold. +// PKT_RX_THRESHOLD - RX FIFO Almost Full threshold. +// PKT_FIELD_1_LENGTH_12_8 - Unsigned 13-bit Field 1 length value. +// PKT_FIELD_1_LENGTH_7_0 - Unsigned 13-bit Field 1 length value. +// PKT_FIELD_1_CONFIG - General data processing and packet configuration bits for Field 1. +// PKT_FIELD_1_CRC_CONFIG - Configuration of CRC control bits across Field 1. +// PKT_FIELD_2_LENGTH_12_8 - Unsigned 13-bit Field 2 length value. +// PKT_FIELD_2_LENGTH_7_0 - Unsigned 13-bit Field 2 length value. +// PKT_FIELD_2_CONFIG - General data processing and packet configuration bits for Field 2. +*/ +#define RF_PKT_LEN_12 0x11, 0x12, 0x0C, 0x08, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00, 0x32, 0x04, 0x00, 0x00, 0x00, 0x00 + +/* +// Set properties: RF_PKT_FIELD_2_CRC_CONFIG_12 +// Number of properties: 12 +// Group ID: 0x12 +// Start ID: 0x14 +// Default values: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +// Descriptions: +// PKT_FIELD_2_CRC_CONFIG - Configuration of CRC control bits across Field 2. +// PKT_FIELD_3_LENGTH_12_8 - Unsigned 13-bit Field 3 length value. +// PKT_FIELD_3_LENGTH_7_0 - Unsigned 13-bit Field 3 length value. +// PKT_FIELD_3_CONFIG - General data processing and packet configuration bits for Field 3. +// PKT_FIELD_3_CRC_CONFIG - Configuration of CRC control bits across Field 3. +// PKT_FIELD_4_LENGTH_12_8 - Unsigned 13-bit Field 4 length value. +// PKT_FIELD_4_LENGTH_7_0 - Unsigned 13-bit Field 4 length value. +// PKT_FIELD_4_CONFIG - General data processing and packet configuration bits for Field 4. +// PKT_FIELD_4_CRC_CONFIG - Configuration of CRC control bits across Field 4. +// PKT_FIELD_5_LENGTH_12_8 - Unsigned 13-bit Field 5 length value. +// PKT_FIELD_5_LENGTH_7_0 - Unsigned 13-bit Field 5 length value. +// PKT_FIELD_5_CONFIG - General data processing and packet configuration bits for Field 5. +*/ +#define RF_PKT_FIELD_2_CRC_CONFIG_12 0x11, 0x12, 0x0C, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + +/* +// Set properties: RF_PKT_FIELD_5_CRC_CONFIG_12 +// Number of properties: 12 +// Group ID: 0x12 +// Start ID: 0x20 +// Default values: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +// Descriptions: +// PKT_FIELD_5_CRC_CONFIG - Configuration of CRC control bits across Field 5. +// PKT_RX_FIELD_1_LENGTH_12_8 - Unsigned 13-bit RX Field 1 length value. +// PKT_RX_FIELD_1_LENGTH_7_0 - Unsigned 13-bit RX Field 1 length value. +// PKT_RX_FIELD_1_CONFIG - General data processing and packet configuration bits for RX Field 1. +// PKT_RX_FIELD_1_CRC_CONFIG - Configuration of CRC control bits across RX Field 1. +// PKT_RX_FIELD_2_LENGTH_12_8 - Unsigned 13-bit RX Field 2 length value. +// PKT_RX_FIELD_2_LENGTH_7_0 - Unsigned 13-bit RX Field 2 length value. +// PKT_RX_FIELD_2_CONFIG - General data processing and packet configuration bits for RX Field 2. +// PKT_RX_FIELD_2_CRC_CONFIG - Configuration of CRC control bits across RX Field 2. +// PKT_RX_FIELD_3_LENGTH_12_8 - Unsigned 13-bit RX Field 3 length value. +// PKT_RX_FIELD_3_LENGTH_7_0 - Unsigned 13-bit RX Field 3 length value. +// PKT_RX_FIELD_3_CONFIG - General data processing and packet configuration bits for RX Field 3. +*/ +#define RF_PKT_FIELD_5_CRC_CONFIG_12 0x11, 0x12, 0x0C, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + +/* +// Set properties: RF_PKT_RX_FIELD_3_CRC_CONFIG_9 +// Number of properties: 9 +// Group ID: 0x12 +// Start ID: 0x2C +// Default values: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +// Descriptions: +// PKT_RX_FIELD_3_CRC_CONFIG - Configuration of CRC control bits across RX Field 3. +// PKT_RX_FIELD_4_LENGTH_12_8 - Unsigned 13-bit RX Field 4 length value. +// PKT_RX_FIELD_4_LENGTH_7_0 - Unsigned 13-bit RX Field 4 length value. +// PKT_RX_FIELD_4_CONFIG - General data processing and packet configuration bits for RX Field 4. +// PKT_RX_FIELD_4_CRC_CONFIG - Configuration of CRC control bits across RX Field 4. +// PKT_RX_FIELD_5_LENGTH_12_8 - Unsigned 13-bit RX Field 5 length value. +// PKT_RX_FIELD_5_LENGTH_7_0 - Unsigned 13-bit RX Field 5 length value. +// PKT_RX_FIELD_5_CONFIG - General data processing and packet configuration bits for RX Field 5. +// PKT_RX_FIELD_5_CRC_CONFIG - Configuration of CRC control bits across RX Field 5. +*/ +#define RF_PKT_RX_FIELD_3_CRC_CONFIG_9 0x11, 0x12, 0x09, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + +/* +// Set properties: RF_MODEM_MOD_TYPE_12 +// Number of properties: 12 +// Group ID: 0x20 +// Start ID: 0x00 +// Default values: 0x02, 0x80, 0x07, 0x0F, 0x42, 0x40, 0x01, 0xC9, 0xC3, 0x80, 0x00, 0x06, +// Descriptions: +// MODEM_MOD_TYPE - Selects the type of modulation. In TX mode, additionally selects the source of the modulation. +// MODEM_MAP_CONTROL - Controls polarity and mapping of transmit and receive bits. +// MODEM_DSM_CTRL - Miscellaneous control bits for the Delta-Sigma Modulator (DSM) in the PLL Synthesizer. +// MODEM_DATA_RATE_2 - Unsigned 24-bit value used to determine the TX data rate +// MODEM_DATA_RATE_1 - Unsigned 24-bit value used to determine the TX data rate +// MODEM_DATA_RATE_0 - Unsigned 24-bit value used to determine the TX data rate +// MODEM_TX_NCO_MODE_3 - TX Gaussian filter oversampling ratio and Byte 3 of unsigned 26-bit TX Numerically Controlled Oscillator (NCO) modulus. +// MODEM_TX_NCO_MODE_2 - TX Gaussian filter oversampling ratio and Byte 3 of unsigned 26-bit TX Numerically Controlled Oscillator (NCO) modulus. +// MODEM_TX_NCO_MODE_1 - TX Gaussian filter oversampling ratio and Byte 3 of unsigned 26-bit TX Numerically Controlled Oscillator (NCO) modulus. +// MODEM_TX_NCO_MODE_0 - TX Gaussian filter oversampling ratio and Byte 3 of unsigned 26-bit TX Numerically Controlled Oscillator (NCO) modulus. +// MODEM_FREQ_DEV_2 - 17-bit unsigned TX frequency deviation word. +// MODEM_FREQ_DEV_1 - 17-bit unsigned TX frequency deviation word. +*/ +#define RF_MODEM_MOD_TYPE_12 0x11, 0x20, 0x0C, 0x00, 0x03, 0x00, 0x07, 0x02, 0xEE, 0x00, 0x05, 0xC9, 0xC3, 0x80, 0x00, 0x00 + +/* +// Set properties: RF_MODEM_FREQ_DEV_0_1 +// Number of properties: 1 +// Group ID: 0x20 +// Start ID: 0x0C +// Default values: 0xD3, +// Descriptions: +// MODEM_FREQ_DEV_0 - 17-bit unsigned TX frequency deviation word. +*/ +#define RF_MODEM_FREQ_DEV_0_1 0x11, 0x20, 0x01, 0x0C, 0xFC + +/* +// Set properties: RF_MODEM_TX_RAMP_DELAY_8 +// Number of properties: 8 +// Group ID: 0x20 +// Start ID: 0x18 +// Default values: 0x01, 0x00, 0x08, 0x03, 0xC0, 0x00, 0x10, 0x20, +// Descriptions: +// MODEM_TX_RAMP_DELAY - TX ramp-down delay setting. +// MODEM_MDM_CTRL - MDM control. +// MODEM_IF_CONTROL - Selects Fixed-IF, Scaled-IF, or Zero-IF mode of RX Modem operation. +// MODEM_IF_FREQ_2 - the IF frequency setting (an 18-bit signed number). +// MODEM_IF_FREQ_1 - the IF frequency setting (an 18-bit signed number). +// MODEM_IF_FREQ_0 - the IF frequency setting (an 18-bit signed number). +// MODEM_DECIMATION_CFG1 - Specifies three decimator ratios for the Cascaded Integrator Comb (CIC) filter. +// MODEM_DECIMATION_CFG0 - Specifies miscellaneous parameters and decimator ratios for the Cascaded Integrator Comb (CIC) filter. +*/ +#define RF_MODEM_TX_RAMP_DELAY_8 0x11, 0x20, 0x08, 0x18, 0x01, 0x00, 0x08, 0x02, 0x80, 0x00, 0xB0, 0x10 + +/* +// Set properties: RF_MODEM_BCR_OSR_1_9 +// Number of properties: 9 +// Group ID: 0x20 +// Start ID: 0x22 +// Default values: 0x00, 0x4B, 0x06, 0xD3, 0xA0, 0x06, 0xD3, 0x02, 0xC0, +// Descriptions: +// MODEM_BCR_OSR_1 - RX BCR/Slicer oversampling rate (12-bit unsigned number). +// MODEM_BCR_OSR_0 - RX BCR/Slicer oversampling rate (12-bit unsigned number). +// MODEM_BCR_NCO_OFFSET_2 - RX BCR NCO offset value (an unsigned 22-bit number). +// MODEM_BCR_NCO_OFFSET_1 - RX BCR NCO offset value (an unsigned 22-bit number). +// MODEM_BCR_NCO_OFFSET_0 - RX BCR NCO offset value (an unsigned 22-bit number). +// MODEM_BCR_GAIN_1 - The unsigned 11-bit RX BCR loop gain value. +// MODEM_BCR_GAIN_0 - The unsigned 11-bit RX BCR loop gain value. +// MODEM_BCR_GEAR - RX BCR loop gear control. +// MODEM_BCR_MISC1 - Miscellaneous control bits for the RX BCR loop. +*/ +#define RF_MODEM_BCR_OSR_1_9 0x11, 0x20, 0x09, 0x22, 0x00, 0x41, 0x07, 0xDD, 0x44, 0x07, 0xFF, 0x02, 0x00 + +/* +// Set properties: RF_MODEM_AFC_GEAR_7 +// Number of properties: 7 +// Group ID: 0x20 +// Start ID: 0x2C +// Default values: 0x00, 0x23, 0x83, 0x69, 0x00, 0x40, 0xA0, +// Descriptions: +// MODEM_AFC_GEAR - RX AFC loop gear control. +// MODEM_AFC_WAIT - RX AFC loop wait time control. +// MODEM_AFC_GAIN_1 - Sets the gain of the PLL-based AFC acquisition loop, and provides miscellaneous control bits for AFC functionality. +// MODEM_AFC_GAIN_0 - Sets the gain of the PLL-based AFC acquisition loop, and provides miscellaneous control bits for AFC functionality. +// MODEM_AFC_LIMITER_1 - Set the AFC limiter value. +// MODEM_AFC_LIMITER_0 - Set the AFC limiter value. +// MODEM_AFC_MISC - Specifies miscellaneous AFC control bits. +*/ +#define RF_MODEM_AFC_GEAR_7 0x11, 0x20, 0x07, 0x2C, 0x00, 0x12, 0x80, 0x7E, 0x01, 0xBF, 0xA0 + +/* +// Set properties: RF_MODEM_AGC_CONTROL_1 +// Number of properties: 1 +// Group ID: 0x20 +// Start ID: 0x35 +// Default values: 0xE0, +// Descriptions: +// MODEM_AGC_CONTROL - Miscellaneous control bits for the Automatic Gain Control (AGC) function in the RX Chain. +*/ +#define RF_MODEM_AGC_CONTROL_1 0x11, 0x20, 0x01, 0x35, 0xE2 + +/* +// Set properties: RF_MODEM_AGC_WINDOW_SIZE_9 +// Number of properties: 9 +// Group ID: 0x20 +// Start ID: 0x38 +// Default values: 0x11, 0x10, 0x10, 0x0B, 0x1C, 0x40, 0x00, 0x00, 0x2B, +// Descriptions: +// MODEM_AGC_WINDOW_SIZE - Specifies the size of the measurement and settling windows for the AGC algorithm. +// MODEM_AGC_RFPD_DECAY - Sets the decay time of the RF peak detectors. +// MODEM_AGC_IFPD_DECAY - Sets the decay time of the IF peak detectors. +// MODEM_FSK4_GAIN1 - Specifies the gain factor of the secondary branch in 4(G)FSK ISI-suppression. +// MODEM_FSK4_GAIN0 - Specifies the gain factor of the primary branch in 4(G)FSK ISI-suppression. +// MODEM_FSK4_TH1 - 16 bit 4(G)FSK slicer threshold. +// MODEM_FSK4_TH0 - 16 bit 4(G)FSK slicer threshold. +// MODEM_FSK4_MAP - 4(G)FSK symbol mapping code. +// MODEM_OOK_PDTC - Configures the attack and decay times of the OOK Peak Detector. +*/ +#define RF_MODEM_AGC_WINDOW_SIZE_9 0x11, 0x20, 0x09, 0x38, 0x11, 0x0E, 0x0E, 0x00, 0x1A, 0x20, 0x00, 0x00, 0x28 + +/* +// Set properties: RF_MODEM_OOK_CNT1_9 +// Number of properties: 9 +// Group ID: 0x20 +// Start ID: 0x42 +// Default values: 0xA4, 0x03, 0x56, 0x02, 0x00, 0xA3, 0x02, 0x80, 0xFF, +// Descriptions: +// MODEM_OOK_CNT1 - OOK control. +// MODEM_OOK_MISC - Selects the detector(s) used for demodulation of an OOK signal, or for demodulation of a (G)FSK signal when using the asynchronous demodulator. +// MODEM_RAW_SEARCH - Defines and controls the search period length for the Moving Average and Min-Max detectors. +// MODEM_RAW_CONTROL - Defines gain and enable controls for raw / nonstandard mode. +// MODEM_RAW_EYE_1 - 11 bit eye-open detector threshold. +// MODEM_RAW_EYE_0 - 11 bit eye-open detector threshold. +// MODEM_ANT_DIV_MODE - Antenna diversity mode settings. +// MODEM_ANT_DIV_CONTROL - Specifies controls for the Antenna Diversity algorithm. +// MODEM_RSSI_THRESH - Configures the RSSI threshold. +*/ +#define RF_MODEM_OOK_CNT1_9 0x11, 0x20, 0x09, 0x42, 0xA4, 0x03, 0xD6, 0x03, 0x00, 0x93, 0x01, 0x80, 0xFF + +/* +// Set properties: RF_MODEM_RSSI_CONTROL_1 +// Number of properties: 1 +// Group ID: 0x20 +// Start ID: 0x4C +// Default values: 0x01, +// Descriptions: +// MODEM_RSSI_CONTROL - Control of the averaging modes and latching time for reporting RSSI value(s). +*/ +#define RF_MODEM_RSSI_CONTROL_1 0x11, 0x20, 0x01, 0x4C, 0x00 + +/* +// Set properties: RF_MODEM_RSSI_COMP_1 +// Number of properties: 1 +// Group ID: 0x20 +// Start ID: 0x4E +// Default values: 0x32, +// Descriptions: +// MODEM_RSSI_COMP - RSSI compensation value. +*/ +#define RF_MODEM_RSSI_COMP_1 0x11, 0x20, 0x01, 0x4E, 0x40 + +/* +// Set properties: RF_MODEM_CLKGEN_BAND_1 +// Number of properties: 1 +// Group ID: 0x20 +// Start ID: 0x51 +// Default values: 0x08, +// Descriptions: +// MODEM_CLKGEN_BAND - Select PLL Synthesizer output divider ratio as a function of frequency band. +*/ +#define RF_MODEM_CLKGEN_BAND_1 0x11, 0x20, 0x01, 0x51, 0x0D + +/* +// Set properties: RF_MODEM_CHFLT_RX1_CHFLT_COE13_7_0_12 +// Number of properties: 12 +// Group ID: 0x21 +// Start ID: 0x00 +// Default values: 0xFF, 0xBA, 0x0F, 0x51, 0xCF, 0xA9, 0xC9, 0xFC, 0x1B, 0x1E, 0x0F, 0x01, +// Descriptions: +// MODEM_CHFLT_RX1_CHFLT_COE13_7_0 - Filter coefficients for the first set of RX filter coefficients. +// MODEM_CHFLT_RX1_CHFLT_COE12_7_0 - Filter coefficients for the first set of RX filter coefficients. +// MODEM_CHFLT_RX1_CHFLT_COE11_7_0 - Filter coefficients for the first set of RX filter coefficients. +// MODEM_CHFLT_RX1_CHFLT_COE10_7_0 - Filter coefficients for the first set of RX filter coefficients. +// MODEM_CHFLT_RX1_CHFLT_COE9_7_0 - Filter coefficients for the first set of RX filter coefficients. +// MODEM_CHFLT_RX1_CHFLT_COE8_7_0 - Filter coefficients for the first set of RX filter coefficients. +// MODEM_CHFLT_RX1_CHFLT_COE7_7_0 - Filter coefficients for the first set of RX filter coefficients. +// MODEM_CHFLT_RX1_CHFLT_COE6_7_0 - Filter coefficients for the first set of RX filter coefficients. +// MODEM_CHFLT_RX1_CHFLT_COE5_7_0 - Filter coefficients for the first set of RX filter coefficients. +// MODEM_CHFLT_RX1_CHFLT_COE4_7_0 - Filter coefficients for the first set of RX filter coefficients. +// MODEM_CHFLT_RX1_CHFLT_COE3_7_0 - Filter coefficients for the first set of RX filter coefficients. +// MODEM_CHFLT_RX1_CHFLT_COE2_7_0 - Filter coefficients for the first set of RX filter coefficients. +*/ +#define RF_MODEM_CHFLT_RX1_CHFLT_COE13_7_0_12 0x11, 0x21, 0x0C, 0x00, 0xFF, 0xBA, 0x0F, 0x51, 0xCF, 0xA9, 0xC9, 0xFC, 0x1B, 0x1E, 0x0F, 0x01 + +/* +// Set properties: RF_MODEM_CHFLT_RX1_CHFLT_COE1_7_0_12 +// Number of properties: 12 +// Group ID: 0x21 +// Start ID: 0x0C +// Default values: 0xFC, 0xFD, 0x15, 0xFF, 0x00, 0x0F, 0xFF, 0xC4, 0x30, 0x7F, 0xF5, 0xB5, +// Descriptions: +// MODEM_CHFLT_RX1_CHFLT_COE1_7_0 - Filter coefficients for the first set of RX filter coefficients. +// MODEM_CHFLT_RX1_CHFLT_COE0_7_0 - Filter coefficients for the first set of RX filter coefficients. +// MODEM_CHFLT_RX1_CHFLT_COEM0 - Filter coefficients for the first set of RX filter coefficients. +// MODEM_CHFLT_RX1_CHFLT_COEM1 - Filter coefficients for the first set of RX filter coefficients. +// MODEM_CHFLT_RX1_CHFLT_COEM2 - Filter coefficients for the first set of RX filter coefficients. +// MODEM_CHFLT_RX1_CHFLT_COEM3 - Filter coefficients for the first set of RX filter coefficients. +// MODEM_CHFLT_RX2_CHFLT_COE13_7_0 - Filter coefficients for the second set of RX filter coefficients. +// MODEM_CHFLT_RX2_CHFLT_COE12_7_0 - Filter coefficients for the second set of RX filter coefficients. +// MODEM_CHFLT_RX2_CHFLT_COE11_7_0 - Filter coefficients for the second set of RX filter coefficients. +// MODEM_CHFLT_RX2_CHFLT_COE10_7_0 - Filter coefficients for the second set of RX filter coefficients. +// MODEM_CHFLT_RX2_CHFLT_COE9_7_0 - Filter coefficients for the second set of RX filter coefficients. +// MODEM_CHFLT_RX2_CHFLT_COE8_7_0 - Filter coefficients for the second set of RX filter coefficients. +*/ +#define RF_MODEM_CHFLT_RX1_CHFLT_COE1_7_0_12 0x11, 0x21, 0x0C, 0x0C, 0xFC, 0xFD, 0x15, 0xFF, 0x00, 0x0F, 0xFF, 0xBA, 0x0F, 0x51, 0xCF, 0xA9 + +/* +// Set properties: RF_MODEM_CHFLT_RX2_CHFLT_COE7_7_0_12 +// Number of properties: 12 +// Group ID: 0x21 +// Start ID: 0x18 +// Default values: 0xB8, 0xDE, 0x05, 0x17, 0x16, 0x0C, 0x03, 0x00, 0x15, 0xFF, 0x00, 0x00, +// Descriptions: +// MODEM_CHFLT_RX2_CHFLT_COE7_7_0 - Filter coefficients for the second set of RX filter coefficients. +// MODEM_CHFLT_RX2_CHFLT_COE6_7_0 - Filter coefficients for the second set of RX filter coefficients. +// MODEM_CHFLT_RX2_CHFLT_COE5_7_0 - Filter coefficients for the second set of RX filter coefficients. +// MODEM_CHFLT_RX2_CHFLT_COE4_7_0 - Filter coefficients for the second set of RX filter coefficients. +// MODEM_CHFLT_RX2_CHFLT_COE3_7_0 - Filter coefficients for the second set of RX filter coefficients. +// MODEM_CHFLT_RX2_CHFLT_COE2_7_0 - Filter coefficients for the second set of RX filter coefficients. +// MODEM_CHFLT_RX2_CHFLT_COE1_7_0 - Filter coefficients for the second set of RX filter coefficients. +// MODEM_CHFLT_RX2_CHFLT_COE0_7_0 - Filter coefficients for the second set of RX filter coefficients. +// MODEM_CHFLT_RX2_CHFLT_COEM0 - Filter coefficients for the second set of RX filter coefficients. +// MODEM_CHFLT_RX2_CHFLT_COEM1 - Filter coefficients for the second set of RX filter coefficients. +// MODEM_CHFLT_RX2_CHFLT_COEM2 - Filter coefficients for the second set of RX filter coefficients. +// MODEM_CHFLT_RX2_CHFLT_COEM3 - Filter coefficients for the second set of RX filter coefficients. +*/ +#define RF_MODEM_CHFLT_RX2_CHFLT_COE7_7_0_12 0x11, 0x21, 0x0C, 0x18, 0xC9, 0xFC, 0x1B, 0x1E, 0x0F, 0x01, 0xFC, 0xFD, 0x15, 0xFF, 0x00, 0x0F + +/* +// Set properties: RF_PA_MODE_4 +// Number of properties: 4 +// Group ID: 0x22 +// Start ID: 0x00 +// Default values: 0x08, 0x7F, 0x00, 0x5D, +// Descriptions: +// PA_MODE - Selects the PA operating mode, and selects resolution of PA power adjustment (i.e., step size). +// PA_PWR_LVL - Configuration of PA output power level. +// PA_BIAS_CLKDUTY - Configuration of the PA Bias and duty cycle of the TX clock source. +// PA_TC - Configuration of PA ramping parameters. +*/ +#define RF_PA_MODE_4 0x11, 0x22, 0x04, 0x00, 0x08, 0x7F, 0x00, 0x3D + +/* +// Set properties: RF_SYNTH_PFDCP_CPFF_7 +// Number of properties: 7 +// Group ID: 0x23 +// Start ID: 0x00 +// Default values: 0x2C, 0x0E, 0x0B, 0x04, 0x0C, 0x73, 0x03, +// Descriptions: +// SYNTH_PFDCP_CPFF - Feed forward charge pump current selection. +// SYNTH_PFDCP_CPINT - Integration charge pump current selection. +// SYNTH_VCO_KV - Gain scaling factors (Kv) for the VCO tuning varactors on both the integrated-path and feed forward path. +// SYNTH_LPFILT3 - Value of resistor R2 in feed-forward path of loop filter. +// SYNTH_LPFILT2 - Value of capacitor C2 in feed-forward path of loop filter. +// SYNTH_LPFILT1 - Value of capacitors C1 and C3 in feed-forward path of loop filter. +// SYNTH_LPFILT0 - Bias current of the active amplifier in the feed-forward loop filter. +*/ +#define RF_SYNTH_PFDCP_CPFF_7 0x11, 0x23, 0x07, 0x00, 0x2C, 0x0E, 0x0B, 0x04, 0x0C, 0x73, 0x03 + +/* +// Set properties: RF_MATCH_VALUE_1_12 +// Number of properties: 12 +// Group ID: 0x30 +// Start ID: 0x00 +// Default values: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +// Descriptions: +// MATCH_VALUE_1 - Match value to be compared with the result of logically AND-ing (bit-wise) the Mask 1 value with the received Match 1 byte. +// MATCH_MASK_1 - Mask value to be logically AND-ed (bit-wise) with the Match 1 byte. +// MATCH_CTRL_1 - Enable for Packet Match functionality, and configuration of Match Byte 1. +// MATCH_VALUE_2 - Match value to be compared with the result of logically AND-ing (bit-wise) the Mask 2 value with the received Match 2 byte. +// MATCH_MASK_2 - Mask value to be logically AND-ed (bit-wise) with the Match 2 byte. +// MATCH_CTRL_2 - Configuration of Match Byte 2. +// MATCH_VALUE_3 - Match value to be compared with the result of logically AND-ing (bit-wise) the Mask 3 value with the received Match 3 byte. +// MATCH_MASK_3 - Mask value to be logically AND-ed (bit-wise) with the Match 3 byte. +// MATCH_CTRL_3 - Configuration of Match Byte 3. +// MATCH_VALUE_4 - Match value to be compared with the result of logically AND-ing (bit-wise) the Mask 4 value with the received Match 4 byte. +// MATCH_MASK_4 - Mask value to be logically AND-ed (bit-wise) with the Match 4 byte. +// MATCH_CTRL_4 - Configuration of Match Byte 4. +*/ +#define RF_MATCH_VALUE_1_12 0x11, 0x30, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + +/* +// Set properties: RF_FREQ_CONTROL_INTE_8 +// Number of properties: 8 +// Group ID: 0x40 +// Start ID: 0x00 +// Default values: 0x3C, 0x08, 0x00, 0x00, 0x00, 0x00, 0x20, 0xFF, +// Descriptions: +// FREQ_CONTROL_INTE - Frac-N PLL Synthesizer integer divide number. +// FREQ_CONTROL_FRAC_2 - Frac-N PLL fraction number. +// FREQ_CONTROL_FRAC_1 - Frac-N PLL fraction number. +// FREQ_CONTROL_FRAC_0 - Frac-N PLL fraction number. +// FREQ_CONTROL_CHANNEL_STEP_SIZE_1 - EZ Frequency Programming channel step size. +// FREQ_CONTROL_CHANNEL_STEP_SIZE_0 - EZ Frequency Programming channel step size. +// FREQ_CONTROL_W_SIZE - Set window gating period (in number of crystal reference clock cycles) for counting VCO frequency during calibration. +// FREQ_CONTROL_VCOCNT_RX_ADJ - Adjust target count for VCO calibration in RX mode. +*/ +#define RF_FREQ_CONTROL_INTE_8 0x11, 0x40, 0x08, 0x00, 0x39, 0x0A, 0xE1, 0x47, 0x00, 0x00, 0x20, 0xFA + + +// AUTOMATICALLY GENERATED CODE! +// DO NOT EDIT/MODIFY BELOW THIS LINE! +// -------------------------------------------- + +#ifndef FIRMWARE_LOAD_COMPILE +#define RADIO_CONFIGURATION_DATA_ARRAY { \ + 0x07, RF_POWER_UP, \ + 0x08, RF_GPIO_PIN_CFG, \ + 0x06, RF_GLOBAL_XO_TUNE_2, \ + 0x05, RF_GLOBAL_CONFIG_1, \ + 0x05, RF_INT_CTL_ENABLE_1, \ + 0x08, RF_FRR_CTL_A_MODE_4, \ + 0x0D, RF_PREAMBLE_TX_LENGTH_9, \ + 0x09, RF_SYNC_CONFIG_5, \ + 0x0B, RF_PKT_CRC_CONFIG_7, \ + 0x10, RF_PKT_LEN_12, \ + 0x10, RF_PKT_FIELD_2_CRC_CONFIG_12, \ + 0x10, RF_PKT_FIELD_5_CRC_CONFIG_12, \ + 0x0D, RF_PKT_RX_FIELD_3_CRC_CONFIG_9, \ + 0x10, RF_MODEM_MOD_TYPE_12, \ + 0x05, RF_MODEM_FREQ_DEV_0_1, \ + 0x0C, RF_MODEM_TX_RAMP_DELAY_8, \ + 0x0D, RF_MODEM_BCR_OSR_1_9, \ + 0x0B, RF_MODEM_AFC_GEAR_7, \ + 0x05, RF_MODEM_AGC_CONTROL_1, \ + 0x0D, RF_MODEM_AGC_WINDOW_SIZE_9, \ + 0x0D, RF_MODEM_OOK_CNT1_9, \ + 0x05, RF_MODEM_RSSI_CONTROL_1, \ + 0x05, RF_MODEM_RSSI_COMP_1, \ + 0x05, RF_MODEM_CLKGEN_BAND_1, \ + 0x10, RF_MODEM_CHFLT_RX1_CHFLT_COE13_7_0_12, \ + 0x10, RF_MODEM_CHFLT_RX1_CHFLT_COE1_7_0_12, \ + 0x10, RF_MODEM_CHFLT_RX2_CHFLT_COE7_7_0_12, \ + 0x08, RF_PA_MODE_4, \ + 0x0B, RF_SYNTH_PFDCP_CPFF_7, \ + 0x10, RF_MATCH_VALUE_1_12, \ + 0x0C, RF_FREQ_CONTROL_INTE_8, \ + 0x00 \ + } +#else +#define RADIO_CONFIGURATION_DATA_ARRAY { 0 } +#endif + +// DEFAULT VALUES FOR CONFIGURATION PARAMETERS +#define RADIO_CONFIGURATION_DATA_RADIO_XO_FREQ_DEFAULT 30000000L +#define RADIO_CONFIGURATION_DATA_CHANNEL_NUMBER_DEFAULT 0x00 +#define RADIO_CONFIGURATION_DATA_RADIO_PACKET_LENGTH_DEFAULT 0x10 +#define RADIO_CONFIGURATION_DATA_RADIO_STATE_AFTER_POWER_UP_DEFAULT 0x01 +#define RADIO_CONFIGURATION_DATA_RADIO_DELAY_CNT_AFTER_RESET_DEFAULT 0x1000 + +#define RADIO_CONFIGURATION_DATA_RADIO_PATCH_INCLUDED 0x00 +#define RADIO_CONFIGURATION_DATA_RADIO_PATCH_SIZE 0x00 +#define RADIO_CONFIGURATION_DATA_RADIO_PATCH { } + +#ifndef RADIO_CONFIGURATION_DATA_ARRAY +#error "This property must be defined!" +#endif + +#ifndef RADIO_CONFIGURATION_DATA_RADIO_XO_FREQ +#define RADIO_CONFIGURATION_DATA_RADIO_XO_FREQ RADIO_CONFIGURATION_DATA_RADIO_XO_FREQ_DEFAULT +#endif + +#ifndef RADIO_CONFIGURATION_DATA_CHANNEL_NUMBER +#define RADIO_CONFIGURATION_DATA_CHANNEL_NUMBER RADIO_CONFIGURATION_DATA_CHANNEL_NUMBER_DEFAULT +#endif + +#ifndef RADIO_CONFIGURATION_DATA_RADIO_PACKET_LENGTH +#define RADIO_CONFIGURATION_DATA_RADIO_PACKET_LENGTH RADIO_CONFIGURATION_DATA_RADIO_PACKET_LENGTH_DEFAULT +#endif + +#ifndef RADIO_CONFIGURATION_DATA_RADIO_STATE_AFTER_POWER_UP +#define RADIO_CONFIGURATION_DATA_RADIO_STATE_AFTER_POWER_UP RADIO_CONFIGURATION_DATA_RADIO_STATE_AFTER_POWER_UP_DEFAULT +#endif + +#ifndef RADIO_CONFIGURATION_DATA_RADIO_DELAY_CNT_AFTER_RESET +#define RADIO_CONFIGURATION_DATA_RADIO_DELAY_CNT_AFTER_RESET RADIO_CONFIGURATION_DATA_RADIO_DELAY_CNT_AFTER_RESET_DEFAULT +#endif + +#define RADIO_CONFIGURATION_DATA { \ + Radio_Configuration_Data_Array, \ + RADIO_CONFIGURATION_DATA_CHANNEL_NUMBER, \ + RADIO_CONFIGURATION_DATA_RADIO_PACKET_LENGTH, \ + RADIO_CONFIGURATION_DATA_RADIO_STATE_AFTER_POWER_UP, \ + RADIO_CONFIGURATION_DATA_RADIO_DELAY_CNT_AFTER_RESET \ + } + +#endif /* RADIO_CONFIG_H_ */ From a279d6f56a10f6b2dde2e05240532788f770683f Mon Sep 17 00:00:00 2001 From: miguelboing Date: Tue, 30 Jan 2024 05:23:43 -0300 Subject: [PATCH 005/103] firmware: si446x: Adding switch logic to si446x file #146 --- firmware/drivers/si446x/si446x.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/firmware/drivers/si446x/si446x.c b/firmware/drivers/si446x/si446x.c index 454276b0..8a0f2d9f 100644 --- a/firmware/drivers/si446x/si446x.c +++ b/firmware/drivers/si446x/si446x.c @@ -36,7 +36,18 @@ #include #include -#include + +#if defined(RADIO_MODULE) && (RADIO_MODULE == 0) +#include + +#elif defined(RADIO_MODULE) && (RADIO_MODULE == 1) +#include + +#else +sys_log_print_event_from_module(SYS_LOG_ERROR, "Radio Configuration", "Failed to determine target radio!"); +sys_log_new_line(); +#endif /* CONFIG_DEV_RADIO_ENABLED */ + #include #include "si446x.h" From 656b74b65e98af8ff570ed50d5df5db9df989d0c Mon Sep 17 00:00:00 2001 From: miguelboing Date: Tue, 30 Jan 2024 05:24:18 -0300 Subject: [PATCH 006/103] firmware: tests: drivers: Updating si446x test, resolves #146 --- firmware/tests/drivers/si446x_test.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/firmware/tests/drivers/si446x_test.c b/firmware/tests/drivers/si446x_test.c index 591a3e39..7668547a 100644 --- a/firmware/tests/drivers/si446x_test.c +++ b/firmware/tests/drivers/si446x_test.c @@ -44,7 +44,15 @@ #include #include -#include +#include + +#if defined(RADIO_MODULE) && (RADIO_MODULE == 0) +#include + +#elif defined(RADIO_MODULE) && (RADIO_MODULE == 1) +#include +#endif + #include #include #include From 38c2749d5a1ef3a15b81604a6d6314cb29095b54 Mon Sep 17 00:00:00 2001 From: miguelboing Date: Wed, 28 Feb 2024 08:28:40 -0300 Subject: [PATCH 007/103] firmware: app: tasks: Removing debug lines, resolves #149 --- firmware/app/tasks/obdh_server.c | 7 ------- 1 file changed, 7 deletions(-) diff --git a/firmware/app/tasks/obdh_server.c b/firmware/app/tasks/obdh_server.c index 703b661e..c9b4dee2 100644 --- a/firmware/app/tasks/obdh_server.c +++ b/firmware/app/tasks/obdh_server.c @@ -62,13 +62,6 @@ void vTaskObdhServer(void) obdh_response_t obdh_response = {0}; obdh_request.command = 0x00U; /* No command */ - uint8_t buffer1[7] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - uint8_t buffer2[7] = {0x22, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; - uint8_t buffer3[10] = {0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}; - - uplink_add_packet(buffer2, 6); - uplink_add_packet(buffer3, 10); - while(1) { TickType_t last_cycle = xTaskGetTickCount(); From 159ce6b0abdf84b9b2d1cedcf94dfd3d2bc02060 Mon Sep 17 00:00:00 2001 From: miguelboing Date: Wed, 28 Feb 2024 09:13:57 -0300 Subject: [PATCH 008/103] firmware: devices: radio: Changing last rx packet bytes to variable type #150 --- firmware/devices/radio/radio_data.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firmware/devices/radio/radio_data.h b/firmware/devices/radio/radio_data.h index 182ccbb8..a73836fa 100644 --- a/firmware/devices/radio/radio_data.h +++ b/firmware/devices/radio/radio_data.h @@ -55,7 +55,7 @@ typedef struct uint32_t tx_packet_counter; /**< Number of total packets sent */ uint8_t rx_fifo_counter; /**< Packets received in queue */ uint8_t tx_fifo_counter; /**< Packets in queue to be sent */ - uint16_t *last_rx_packet_bytes; /**< Number of available bytes in the last packet received */ + uint16_t last_rx_packet_bytes; /**< Number of available bytes in the last packet received */ } radio_data_t; #endif /* RADIO_DATA_H_ */ From 1ba4611341814c04a2fb71a7e8a57c792b6adaa8 Mon Sep 17 00:00:00 2001 From: miguelboing Date: Wed, 28 Feb 2024 09:15:43 -0300 Subject: [PATCH 009/103] firmware: devices: obdh: Updating variable last rx packet bytes tye #150 --- firmware/devices/obdh/obdh.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firmware/devices/obdh/obdh.c b/firmware/devices/obdh/obdh.c index ed7aa374..fd4eed9a 100644 --- a/firmware/devices/obdh/obdh.c +++ b/firmware/devices/obdh/obdh.c @@ -282,7 +282,7 @@ int obdh_write_response_param(ttc_data_t *ttc_data_buf, obdh_response_t *obdh_re break; case CMDPR_PARAM_N_BYTES_FIRST_AV_RX: - obdh_response->data.param_16 = *(ttc_data_buf->radio.last_rx_packet_bytes); + obdh_response->data.param_16 = ttc_data_buf->radio.last_rx_packet_bytes; break; default: From 0054a46df91a54ce122e01623943e3b388b4b8a6 Mon Sep 17 00:00:00 2001 From: miguelboing Date: Wed, 28 Feb 2024 09:49:47 -0300 Subject: [PATCH 010/103] firmware: app: structs: Improvements for packet handling #150 --- firmware/app/structs/ttc_data.c | 68 +++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 29 deletions(-) diff --git a/firmware/app/structs/ttc_data.c b/firmware/app/structs/ttc_data.c index 6003f30b..17329745 100644 --- a/firmware/app/structs/ttc_data.c +++ b/firmware/app/structs/ttc_data.c @@ -42,11 +42,11 @@ ttc_data_t ttc_data_buf; void downlink_add_packet(uint8_t *packet, uint16_t packet_size) { - uint16_t i = 0; + uint16_t i = 0U; ttc_data_buf.down_buf.packet_sizes[ttc_data_buf.down_buf.position_to_write] = packet_size; - for(i = 0; i = 5) + if (++ttc_data_buf.down_buf.position_to_write >= 5U) { - ttc_data_buf.down_buf.position_to_write = 0; + ttc_data_buf.down_buf.position_to_write = 0U; } } void downlink_pop_packet(uint8_t *packet, uint16_t *packet_size) { - uint16_t i = 0; + uint16_t i = 0U; - *packet_size = ttc_data_buf.down_buf.packet_sizes[ttc_data_buf.down_buf.position_to_read]; - - for(i = 0; i < ttc_data_buf.down_buf.packet_sizes[ttc_data_buf.down_buf.position_to_read]; i++) + if (ttc_data_buf.radio.tx_fifo_counter > 0U) { - packet[i] = ttc_data_buf.down_buf.packet_array[ttc_data_buf.down_buf.position_to_read][i]; - } + *packet_size = ttc_data_buf.down_buf.packet_sizes[ttc_data_buf.down_buf.position_to_read]; - ttc_data_buf.radio.tx_fifo_counter--; + for(i = 0U; i < ttc_data_buf.down_buf.packet_sizes[ttc_data_buf.down_buf.position_to_read]; i++) + { + packet[i] = ttc_data_buf.down_buf.packet_array[ttc_data_buf.down_buf.position_to_read][i]; + } - if (++ttc_data_buf.down_buf.position_to_read >= 5) - { - ttc_data_buf.down_buf.position_to_read = 0; + ttc_data_buf.radio.tx_fifo_counter--; + + if (++ttc_data_buf.down_buf.position_to_read >= 5U) + { + ttc_data_buf.down_buf.position_to_read = 0U; + } } } void uplink_add_packet(uint8_t *packet, uint16_t packet_size) { - uint16_t i = 0; + uint16_t i = 0U; ttc_data_buf.up_buf.packet_sizes[ttc_data_buf.up_buf.position_to_write] = packet_size; + if (ttc_data_buf.radio.rx_packet_counter == 0U) + { + ttc_data_buf.radio.last_rx_packet_bytes = packet_size; + } + for(i = 0; i < ttc_data_buf.up_buf.packet_sizes[ttc_data_buf.up_buf.position_to_write]; i++) { ttc_data_buf.up_buf.packet_array[ttc_data_buf.up_buf.position_to_write][i] = packet[i]; @@ -103,26 +111,28 @@ void uplink_pop_packet(uint8_t *packet, uint16_t *packet_size) { uint16_t i = 0; - *packet_size = ttc_data_buf.up_buf.packet_sizes[ttc_data_buf.up_buf.position_to_read]; - - for(i = 0; i < ttc_data_buf.up_buf.packet_sizes[ttc_data_buf.up_buf.position_to_read]; i++) + if (ttc_data_buf.radio.rx_fifo_counter > 0U) { - packet[i] = ttc_data_buf.up_buf.packet_array[ttc_data_buf.up_buf.position_to_read][i]; - ttc_data_buf.up_buf.packet_array[ttc_data_buf.up_buf.position_to_read][i] = 0xFF; /* Remove packet after a read */ - } + *packet_size = ttc_data_buf.up_buf.packet_sizes[ttc_data_buf.up_buf.position_to_read]; - ttc_data_buf.up_buf.packet_sizes[ttc_data_buf.up_buf.position_to_read] = 0xFF; /* 0xFF means that there is no package in this position */ + for(i = 0; i < ttc_data_buf.up_buf.packet_sizes[ttc_data_buf.up_buf.position_to_read]; i++) + { + packet[i] = ttc_data_buf.up_buf.packet_array[ttc_data_buf.up_buf.position_to_read][i]; + ttc_data_buf.up_buf.packet_array[ttc_data_buf.up_buf.position_to_read][i] = 0xFF; /* Remove packet after a read */ + } - ttc_data_buf.radio.rx_fifo_counter--; + ttc_data_buf.up_buf.packet_sizes[ttc_data_buf.up_buf.position_to_read] = 0x00; /* 0x00 means that there is no package in this position */ - if (++ttc_data_buf.up_buf.position_to_read >= 5) - { - ttc_data_buf.up_buf.position_to_read = 0; - } + ttc_data_buf.radio.rx_fifo_counter--; - /* Update rx packet bytes */ - ttc_data_buf.radio.last_rx_packet_bytes = &(ttc_data_buf.up_buf.packet_sizes[ttc_data_buf.up_buf.position_to_read]); + if (++ttc_data_buf.up_buf.position_to_read >= 5) + { + ttc_data_buf.up_buf.position_to_read = 0; + } + /* Update rx packet bytes */ + ttc_data_buf.radio.last_rx_packet_bytes = ttc_data_buf.up_buf.packet_sizes[ttc_data_buf.up_buf.position_to_read]; + } } /** \} End of ttc_data group */ From b437a72765a18fe3851af4b60b4bd7a2c6d22f7c Mon Sep 17 00:00:00 2001 From: miguelboing Date: Wed, 28 Feb 2024 09:51:33 -0300 Subject: [PATCH 011/103] firmware: app: tasks: Starting last rx packet as zero #150 --- firmware/app/tasks/uplink_manager.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/firmware/app/tasks/uplink_manager.c b/firmware/app/tasks/uplink_manager.c index eb69d030..7f4cbfb0 100644 --- a/firmware/app/tasks/uplink_manager.c +++ b/firmware/app/tasks/uplink_manager.c @@ -53,30 +53,29 @@ void vTaskUplinkManager(void) sys_log_print_event_from_module(SYS_LOG_INFO, TASK_UPLINK_MANAGER_NAME, "Initializing the Uplink Manager..."); sys_log_new_line(); - ttc_data_buf.radio.rx_fifo_counter = 0; - ttc_data_buf.radio.rx_packet_counter = 0; - ttc_data_buf.radio.last_rx_packet_bytes = &(ttc_data_buf.up_buf.packet_sizes); + ttc_data_buf.radio.rx_fifo_counter = 0U; + ttc_data_buf.radio.rx_packet_counter = 0U; + ttc_data_buf.radio.last_rx_packet_bytes = 0U; - ttc_data_buf.up_buf.position_to_read = 0; - ttc_data_buf.up_buf.position_to_write = 0; + ttc_data_buf.up_buf.position_to_read = 0U; + ttc_data_buf.up_buf.position_to_write = 0U; - uint16_t rx_size = 230; - uint8_t rx_packet[230] = {0}; + uint8_t rx_packet[230] = {0U}; while(1) { TickType_t last_cycle = xTaskGetTickCount(); - if (radio_available() == 0) + if (radio_available() == 0U) { - radio_recv(rx_packet, 230, 100); + radio_recv(rx_packet, 230U, 100U); sys_log_print_event_from_module(SYS_LOG_INFO, TASK_UPLINK_MANAGER_NAME, "Received a new package:"); - sys_log_dump_hex(rx_packet, 230); + sys_log_dump_hex(rx_packet, 230U); sys_log_new_line(); //ngham_decode(rx_packet); /* TODO */ - uplink_add_packet(rx_packet, 220); + uplink_add_packet(rx_packet, 220U); } vTaskDelayUntil(&last_cycle, pdMS_TO_TICKS(TASK_UPLINK_MANAGER_PERIOD_MS)); From c6fc63777eaedcb6d1b93da740cda513a0a4b132 Mon Sep 17 00:00:00 2001 From: miguelboing Date: Wed, 28 Feb 2024 18:20:58 -0300 Subject: [PATCH 012/103] firmware: drivers: si446x: Adding si446x mutex implementation #134 --- firmware/drivers/si446x/si446x.c | 3 ++ firmware/drivers/si446x/si446x.h | 3 ++ firmware/drivers/si446x/si446x_mutex.c | 50 ++++++++++++++++++-------- 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/firmware/drivers/si446x/si446x.c b/firmware/drivers/si446x/si446x.c index 8a0f2d9f..04ba3f23 100644 --- a/firmware/drivers/si446x/si446x.c +++ b/firmware/drivers/si446x/si446x.c @@ -65,6 +65,9 @@ int si446x_init(void) sys_log_new_line(); #endif /* CONFIG_DRIVERS_DEBUG_ENABLED */ + /* Create si446x mutex */ + si446x_mutex_create(); + si446x_gpio_init(); si446x_spi_init(); diff --git a/firmware/drivers/si446x/si446x.h b/firmware/drivers/si446x/si446x.h index a5c987a8..0524b000 100644 --- a/firmware/drivers/si446x/si446x.h +++ b/firmware/drivers/si446x/si446x.h @@ -39,6 +39,9 @@ #include #include +/* Mutex config. */ +#define SI446X_MUTEX_WAIT_TIME_MS 100 + /** * \brief Si446x modes. */ diff --git a/firmware/drivers/si446x/si446x_mutex.c b/firmware/drivers/si446x/si446x_mutex.c index b5d7363e..830c3dd8 100644 --- a/firmware/drivers/si446x/si446x_mutex.c +++ b/firmware/drivers/si446x/si446x_mutex.c @@ -24,10 +24,11 @@ * \brief Si446x mutex functions implementation. * * \author Gabriel Mariano Marcelino + * \author Miguel Boing * * \version 0.4.5 * - * \date 2023/04/13 + * \date 2024/02/28 * * \addtogroup si446x * \{ @@ -36,38 +37,59 @@ #include #include +#include + #include "si446x.h" #include "si446x_config.h" -static SemaphoreHandle_t si446x_semaphore = NULL; - -#define SI446X_MUTEX_WAIT_TIME_MS 100; +static SemaphoreHandle_t si446x_mutex = NULL; int si446x_mutex_create(void) { - return 0; + int err = 0; + + si446x_mutex = xSemaphoreCreateMutex(); + + if (si446x_mutex == NULL) + { + sys_log_print_event_from_module(SYS_LOG_ERROR, SI446X_MODULE_NAME, "Error creating a mutex!"); + sys_log_new_line(); + + err = -1; + } + + return err; } int si446x_mutex_take(void) { int err = -1; - //if (si446x_semaphore != NULL) - //{ + if (si446x_mutex != NULL) + { /* See if we can obtain the semaphore. If the semaphore is not */ - /* available wait SYS_LOG_MUTEX_WAIT_TIME_MS ms to see if it becomes free */ - // if (xSemaphoreTake(si446x_semaphore, pdMS_TO_TICKS(SI446X_MUTEX_WAIT_TIME_MS)) == pdTRUE) - //{ - // err = 0; - //} - //} + /* available wait SI446X_MUTEX_WAIT_TIME_MS ms to see if it becomes free */ + if (xSemaphoreTake(si446x_mutex, pdMS_TO_TICKS(SI446X_MUTEX_WAIT_TIME_MS)) == pdTRUE) + { + err = 0; + } + } return err; } int si446x_mutex_give(void) { - return 0; + int err = -1; + + if (si446x_mutex != NULL) + { + xSemaphoreGive(si446x_mutex); + + err = 0; + } + + return err; } /**< \} End of si446x group */ From bdb9a18953e406a993b8f618fbf5cfda3b312836 Mon Sep 17 00:00:00 2001 From: miguelboing Date: Wed, 28 Feb 2024 18:23:30 -0300 Subject: [PATCH 013/103] firmware: devices: radio: Using si446x mutex on radio functions, resolves #134 --- firmware/devices/radio/radio.c | 76 ++++++++++++++++++++++++---------- 1 file changed, 55 insertions(+), 21 deletions(-) diff --git a/firmware/devices/radio/radio.c b/firmware/devices/radio/radio.c index c2d70ef3..a8a13ae7 100644 --- a/firmware/devices/radio/radio.c +++ b/firmware/devices/radio/radio.c @@ -64,26 +64,36 @@ int radio_init(void) int radio_send(uint8_t *data, uint16_t len) { - sys_log_print_event_from_module(SYS_LOG_INFO, RADIO_MODULE_NAME, "Transmitting "); - sys_log_print_uint(len); - sys_log_print_msg(" byte(s)..."); - sys_log_new_line(); - int err = -1; - led_set(LED_DOWNLINK); - - if(si446x_tx_long_packet(data, len)) + if (si446x_mutex_take() == 0) { - led_clear(LED_DOWNLINK); + sys_log_print_event_from_module(SYS_LOG_INFO, RADIO_MODULE_NAME, "Transmitting "); + sys_log_print_uint(len); + sys_log_print_msg(" byte(s)..."); + sys_log_new_line(); - if(si446x_rx_init()) + led_set(LED_DOWNLINK); + + if(si446x_tx_long_packet(data, len)) { - err = 0; + led_clear(LED_DOWNLINK); + + if(si446x_rx_init()) + { + err = 0; + } } - } - led_clear(LED_DOWNLINK); + led_clear(LED_DOWNLINK); + + si446x_mutex_give(); + } + else + { + sys_log_print_event_from_module(SYS_LOG_ERROR, RADIO_MODULE_NAME, "Couldn't get mutex control."); + sys_log_new_line(); + } return err; } @@ -94,20 +104,30 @@ int radio_recv(uint8_t *data, uint16_t len, uint32_t timeout_ms) uint16_t i = 0; - for(i = 0; i < (timeout_ms/100); i++) + if (si446x_mutex_take() == 0) { - if (si446x_wait_nirq()) + for(i = 0; i < (timeout_ms/100); i++) { - res = (int)si446x_rx_packet(data, len); + if (si446x_wait_nirq()) + { + res = (int)si446x_rx_packet(data, len); - si446x_clear_interrupts(); + si446x_clear_interrupts(); - si446x_rx_init(); + si446x_rx_init(); - break; + break; + } + + vTaskDelay(pdMS_TO_TICKS(500)); } - vTaskDelay(pdMS_TO_TICKS(500)); + si446x_mutex_give(); + } + else + { + sys_log_print_event_from_module(SYS_LOG_ERROR, RADIO_MODULE_NAME, "Couldn't get mutex control."); + sys_log_new_line(); } return res; @@ -120,7 +140,21 @@ int radio_available(void) int radio_sleep(void) { - return (int)si446x_enter_standby_mode(); + int err = -1; + + if (si446x_mutex_take() == 0) + { + err = si446x_enter_standby_mode(); + + si446x_mutex_give(); + } + else + { + sys_log_print_event_from_module(SYS_LOG_ERROR, RADIO_MODULE_NAME, "Couldn't get mutex control."); + sys_log_new_line(); + } + + return err; } int radio_get_temperature(radio_temp_t *temp) From f0dea35ed09f489dc606237f722fb24ced9c8b48 Mon Sep 17 00:00:00 2001 From: miguelboing Date: Wed, 6 Mar 2024 16:52:32 -0300 Subject: [PATCH 014/103] firmware: drivers: spi_slave: DMA transfer size generalization #151 --- firmware/drivers/spi_slave/spi_slave.c | 27 +++++++++++--------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/firmware/drivers/spi_slave/spi_slave.c b/firmware/drivers/spi_slave/spi_slave.c index 4bdfcc08..e0dcb1f8 100644 --- a/firmware/drivers/spi_slave/spi_slave.c +++ b/firmware/drivers/spi_slave/spi_slave.c @@ -81,8 +81,10 @@ static int spi_read_isr_rx_buffer(spi_port_t port, uint8_t *data, uint16_t len); static int spi_slave_setup_gpio(spi_port_t port); -static uint8_t spi_slave_dma_tx_data[228] = {0}; -static uint8_t spi_slave_dma_rx_data[228] = {0}; +#define DMA_TRANSFER_SIZE 15 + +static uint8_t spi_slave_dma_tx_data[DMA_TRANSFER_SIZE] = {0}; +static uint8_t spi_slave_dma_rx_data[DMA_TRANSFER_SIZE] = {0}; static uint16_t spi_slave_dma_tx_position; static uint16_t spi_slave_dma_rx_position; @@ -100,7 +102,7 @@ int spi_slave_init(spi_port_t port, spi_config_t config) static DMA_initParam spi_slave_dma_param_tx = { .channelSelect = DMA_CHANNEL_0, .transferModeSelect = DMA_TRANSFER_REPEATED_SINGLE, - .transferSize = 228, + .transferSize = DMA_TRANSFER_SIZE, .triggerSourceSelect = DMA_TRIGGERSOURCE_13, .transferUnitSelect = DMA_SIZE_SRCBYTE_DSTBYTE, .triggerTypeSelect = DMA_TRIGGER_HIGH, @@ -109,7 +111,7 @@ int spi_slave_init(spi_port_t port, spi_config_t config) static DMA_initParam spi_slave_dma_param_rx = { .channelSelect = DMA_CHANNEL_1, .transferModeSelect = DMA_TRANSFER_REPEATED_SINGLE, - .transferSize = 228, + .transferSize = DMA_TRANSFER_SIZE, .triggerSourceSelect = DMA_TRIGGERSOURCE_12, .transferUnitSelect = DMA_SIZE_SRCBYTE_DSTBYTE, .triggerTypeSelect = DMA_TRIGGER_HIGH, @@ -310,8 +312,9 @@ int spi_slave_init(spi_port_t port, spi_config_t config) DMA_enableTransfers(DMA_CHANNEL_0); spi_slave_dma_rx_position = 0U; + spi_slave_dma_tx_position = 8U; - for(i = 0U; i < 228U; i++) + for(i = 0U; i < DMA_TRANSFER_SIZE; i++) { spi_slave_dma_rx_data[i] = 0xFFU; spi_slave_dma_tx_data[i] = 0xFFU; @@ -340,8 +343,6 @@ int spi_slave_init(spi_port_t port, spi_config_t config) DMA_enableTransfers(DMA_CHANNEL_1); - spi_slave_dma_tx_position = 8U; - __bis_SR_register(LPM4_bits + GIE); if (err == 0) @@ -373,9 +374,7 @@ void spi_slave_dma_write(uint8_t *data, uint16_t len) { spi_slave_dma_tx_data[spi_slave_dma_tx_position] = data[i]; - spi_slave_dma_tx_position++; - - if (spi_slave_dma_tx_position > 227U) /* Reset position after the end of the buffer */ + if (++spi_slave_dma_tx_position > (DMA_TRANSFER_SIZE - 1U)) /* Reset position after the end of the buffer */ { spi_slave_dma_tx_position = 0U; } @@ -383,9 +382,7 @@ void spi_slave_dma_write(uint8_t *data, uint16_t len) spi_slave_dma_tx_data[spi_slave_dma_tx_position] = 0x7EU; - spi_slave_dma_tx_position++; - - if (spi_slave_dma_tx_position > 227U) /* Reset position after the end of the buffer */ + if (++spi_slave_dma_tx_position > (DMA_TRANSFER_SIZE - 1U)) /* Reset position after the end of the buffer */ { spi_slave_dma_tx_position = 0U; } @@ -403,9 +400,7 @@ void spi_slave_dma_read(uint8_t *data, uint16_t len) spi_slave_dma_rx_data[spi_slave_dma_rx_position] = 0xFFU; /* Clear after read */ - spi_slave_dma_rx_position++; - - if (spi_slave_dma_rx_position > 227U) + if (++spi_slave_dma_rx_position > (DMA_TRANSFER_SIZE - 1U)) { spi_slave_dma_rx_position = 0U; /* Reset position after the end of the buffer */ } From f8da2770798881529470d9ea46680d6ce19bbe80 Mon Sep 17 00:00:00 2001 From: miguelboing Date: Mon, 11 Mar 2024 14:55:33 -0300 Subject: [PATCH 015/103] firmware: drivers: spi_slave: Change tranfer size functionality #151 --- firmware/drivers/spi_slave/spi_slave.c | 71 ++++++++++++-------------- firmware/drivers/spi_slave/spi_slave.h | 2 + 2 files changed, 34 insertions(+), 39 deletions(-) diff --git a/firmware/drivers/spi_slave/spi_slave.c b/firmware/drivers/spi_slave/spi_slave.c index e0dcb1f8..af2520d1 100644 --- a/firmware/drivers/spi_slave/spi_slave.c +++ b/firmware/drivers/spi_slave/spi_slave.c @@ -81,10 +81,11 @@ static int spi_read_isr_rx_buffer(spi_port_t port, uint8_t *data, uint16_t len); static int spi_slave_setup_gpio(spi_port_t port); -#define DMA_TRANSFER_SIZE 15 +#define DMA_TX_TRANSFER_SIZE 7 +#define DMA_RX_TRANSFER_SIZE 7 -static uint8_t spi_slave_dma_tx_data[DMA_TRANSFER_SIZE] = {0}; -static uint8_t spi_slave_dma_rx_data[DMA_TRANSFER_SIZE] = {0}; +static uint8_t spi_slave_dma_tx_data[70U] = {0U}; +static uint8_t spi_slave_dma_rx_data[70U] = {0U}; static uint16_t spi_slave_dma_tx_position; static uint16_t spi_slave_dma_rx_position; @@ -102,7 +103,7 @@ int spi_slave_init(spi_port_t port, spi_config_t config) static DMA_initParam spi_slave_dma_param_tx = { .channelSelect = DMA_CHANNEL_0, .transferModeSelect = DMA_TRANSFER_REPEATED_SINGLE, - .transferSize = DMA_TRANSFER_SIZE, + .transferSize = DMA_TX_TRANSFER_SIZE, .triggerSourceSelect = DMA_TRIGGERSOURCE_13, .transferUnitSelect = DMA_SIZE_SRCBYTE_DSTBYTE, .triggerTypeSelect = DMA_TRIGGER_HIGH, @@ -111,7 +112,7 @@ int spi_slave_init(spi_port_t port, spi_config_t config) static DMA_initParam spi_slave_dma_param_rx = { .channelSelect = DMA_CHANNEL_1, .transferModeSelect = DMA_TRANSFER_REPEATED_SINGLE, - .transferSize = DMA_TRANSFER_SIZE, + .transferSize = DMA_RX_TRANSFER_SIZE, .triggerSourceSelect = DMA_TRIGGERSOURCE_12, .transferUnitSelect = DMA_SIZE_SRCBYTE_DSTBYTE, .triggerTypeSelect = DMA_TRIGGER_HIGH, @@ -314,9 +315,13 @@ int spi_slave_init(spi_port_t port, spi_config_t config) spi_slave_dma_rx_position = 0U; spi_slave_dma_tx_position = 8U; - for(i = 0U; i < DMA_TRANSFER_SIZE; i++) + for(i = 0U; i < DMA_RX_TRANSFER_SIZE; i++) { spi_slave_dma_rx_data[i] = 0xFFU; + } + + for(i = 0U; i < DMA_TX_TRANSFER_SIZE; i++) + { spi_slave_dma_tx_data[i] = 0xFFU; } @@ -328,9 +333,6 @@ int spi_slave_init(spi_port_t port, spi_config_t config) spi_slave_dma_tx_data[5] = 0x00U; spi_slave_dma_tx_data[6] = 0x00U; - /* Next command preamble */ - spi_slave_dma_tx_data[7] = 0x7EU; - DMA_init(&spi_slave_dma_param_rx); DMA_setSrcAddress(DMA_CHANNEL_1, USCI_A_SPI_getReceiveBufferAddressForDMA(base_address), DMA_DIRECTION_UNCHANGED); @@ -372,50 +374,41 @@ void spi_slave_dma_write(uint8_t *data, uint16_t len) for(i = 0U; i < len; i++) { - spi_slave_dma_tx_data[spi_slave_dma_tx_position] = data[i]; - - if (++spi_slave_dma_tx_position > (DMA_TRANSFER_SIZE - 1U)) /* Reset position after the end of the buffer */ - { - spi_slave_dma_tx_position = 0U; - } + spi_slave_dma_tx_data[i] = data[i]; } +} - spi_slave_dma_tx_data[spi_slave_dma_tx_position] = 0x7EU; +void spi_slave_dma_read(uint8_t *data, uint16_t len) +{ + uint16_t i = 0U; - if (++spi_slave_dma_tx_position > (DMA_TRANSFER_SIZE - 1U)) /* Reset position after the end of the buffer */ + for(i = 0U; i < len; i++) { - spi_slave_dma_tx_position = 0U; + data[i] = spi_slave_dma_rx_data[i]; + spi_slave_dma_rx_data[i] = 0xFFU; /* Clear after read */ } } -void spi_slave_dma_read(uint8_t *data, uint16_t len) +void spi_slave_dma_change_transfer_size(uint16_t transfer_size) { - uint16_t i = 0U; + DMA_disableTransfers(DMA_CHANNEL_0); + DMA_disableTransfers(DMA_CHANNEL_1); - if ((spi_slave_dma_rx_data[spi_slave_dma_rx_position] != 0xFFU)) - { - for(i = 0U; i < len; i++) - { - data[i] = spi_slave_dma_rx_data[spi_slave_dma_rx_position]; + USCI_A_SPI_disable(USCI_A2_BASE); + USCI_A_SPI_enable(USCI_A2_BASE); - spi_slave_dma_rx_data[spi_slave_dma_rx_position] = 0xFFU; /* Clear after read */ + DMA_setSrcAddress(DMA_CHANNEL_0, (uint32_t)(uintptr_t)spi_slave_dma_tx_data, DMA_DIRECTION_INCREMENT); // cppcheck-suppress misra-c2012-11.4 + DMA_setDstAddress(DMA_CHANNEL_1, (uint32_t)(uintptr_t)spi_slave_dma_rx_data, DMA_DIRECTION_INCREMENT); // cppcheck-suppress misra-c2012-11.4 - if (++spi_slave_dma_rx_position > (DMA_TRANSFER_SIZE - 1U)) - { - spi_slave_dma_rx_position = 0U; /* Reset position after the end of the buffer */ - } - } - } + DMA_setTransferSize(DMA_CHANNEL_0, transfer_size); + DMA_setTransferSize(DMA_CHANNEL_1, transfer_size); + + DMA_enableTransfers(DMA_CHANNEL_0); + DMA_enableTransfers(DMA_CHANNEL_1); - else - { - for (i=0U; i < len; i++) - { - data[i] = 0xFFU; - } - } } + static int spi_slave_setup_gpio(spi_port_t port) { int err = 0; diff --git a/firmware/drivers/spi_slave/spi_slave.h b/firmware/drivers/spi_slave/spi_slave.h index cf84111e..6c650dc2 100644 --- a/firmware/drivers/spi_slave/spi_slave.h +++ b/firmware/drivers/spi_slave/spi_slave.h @@ -84,6 +84,8 @@ void spi_slave_dma_write(uint8_t *data, uint16_t len); */ void spi_slave_dma_read(uint8_t *data, uint16_t len); +void spi_slave_dma_change_transfer_size(uint16_t transfer_size); + /** * \brief Enables SPI Slave port interruption. * From 03d68630905850aba7b1b514c27548101eaae943 Mon Sep 17 00:00:00 2001 From: miguelboing Date: Mon, 11 Mar 2024 14:57:39 -0300 Subject: [PATCH 016/103] firmware: devices: obdh: Read parameter and packet updated #151 --- firmware/devices/obdh/obdh.c | 47 +++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/firmware/devices/obdh/obdh.c b/firmware/devices/obdh/obdh.c index fd4eed9a..7d8e6cc9 100644 --- a/firmware/devices/obdh/obdh.c +++ b/firmware/devices/obdh/obdh.c @@ -74,7 +74,7 @@ int obdh_read_request(obdh_request_t *obdh_request) uint8_t request[7] = {0}; spi_slave_dma_read(request, 7); - + /*TODO: Check here for preamble!*/ obdh_request->command = request[1]; if ((obdh_request->command != 0xFF) && (obdh_request->command != 0x00)) /* Received a request */ @@ -307,27 +307,28 @@ int obdh_flush_request(obdh_request_t *obdh_request) static int obdh_write_parameter(obdh_response_t *obdh_response) { int err = 0; - uint8_t response[6] = {0}; + uint8_t response[7] = {0}; - response[0] = obdh_response->command; - response[1] = obdh_response->parameter; + response[0] = 0x7EU; + response[1] = obdh_response->command; + response[2] = obdh_response->parameter; switch(cmdpr_param_size(obdh_response->parameter)) { case 1: - response[2] = obdh_response->data.param_8; + response[3] = obdh_response->data.param_8; break; case 2: - response[2] = (uint8_t)((obdh_response->data.param_16 >> 8) & 0xFFU); - response[3] = (uint8_t)(obdh_response->data.param_16 & 0xFFU); + response[3] = (uint8_t)((obdh_response->data.param_16 >> 8) & 0xFFU); + response[4] = (uint8_t)(obdh_response->data.param_16 & 0xFFU); break; case 4: - response[2] = (uint8_t)((obdh_response->data.param_32 >> 24) & 0xFFU); - response[3] = (uint8_t)((obdh_response->data.param_32 >> 16) & 0xFFU); - response[4] = (uint8_t)((obdh_response->data.param_32 >> 8) & 0xFFU); - response[5] = (uint8_t)(obdh_response->data.param_32 & 0xFFU); + response[3] = (uint8_t)((obdh_response->data.param_32 >> 24) & 0xFFU); + response[4] = (uint8_t)((obdh_response->data.param_32 >> 16) & 0xFFU); + response[5] = (uint8_t)((obdh_response->data.param_32 >> 8) & 0xFFU); + response[6] = (uint8_t)(obdh_response->data.param_32 & 0xFFU); break; default: @@ -340,7 +341,7 @@ static int obdh_write_parameter(obdh_response_t *obdh_response) if (err == 0) { - spi_slave_dma_write(response, 6); + spi_slave_dma_write(response, 7); } else { @@ -355,7 +356,9 @@ void obdh_write_read_bytes(uint16_t number_of_bytes) // cppcheck-suppress misra- uint8_t buffer[230]; uint8_t i; - for (i = 0U; i < 230U; i++) + buffer[0] = 0x7EU; + + for (i = 1U; i < number_of_bytes; i++) { buffer[i] = 0x00U; } @@ -366,14 +369,30 @@ void obdh_write_read_bytes(uint16_t number_of_bytes) // cppcheck-suppress misra- static int obdh_write_packet(obdh_response_t *obdh_response) { int err = 0; + uint16_t buffer_size; + buffer_size = obdh_response->data.data_packet.len + 2; + + uint8_t buffer[buffer_size]; - spi_slave_dma_write(obdh_response->data.data_packet.packet, obdh_response->data.data_packet.len); + buffer[0] = 0x7EU; + buffer[1] = 0x04U; + + for (uint16_t i = 2; i < buffer_size; i++) + { + buffer[i] = obdh_response->data.data_packet.packet[i-2]; + } + + spi_slave_dma_change_transfer_size(buffer_size); + + spi_slave_dma_write(buffer, buffer_size); sys_log_print_str("Packet:"); sys_log_dump_hex(obdh_response->data.data_packet.packet, obdh_response->data.data_packet.len); sys_log_new_line(); + vTaskDelay(pdMS_TO_TICKS(100)); + return err; } From ad010ef146ad3575b8b1ad211e7c49d97fa1d57e Mon Sep 17 00:00:00 2001 From: miguelboing Date: Mon, 11 Mar 2024 14:58:57 -0300 Subject: [PATCH 017/103] firmware: app: tasks: Fixing syncronization read parameter and packet commands #151 --- firmware/app/tasks/obdh_server.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/firmware/app/tasks/obdh_server.c b/firmware/app/tasks/obdh_server.c index c9b4dee2..303966d9 100644 --- a/firmware/app/tasks/obdh_server.c +++ b/firmware/app/tasks/obdh_server.c @@ -62,6 +62,9 @@ void vTaskObdhServer(void) obdh_response_t obdh_response = {0}; obdh_request.command = 0x00U; /* No command */ + uint8_t transmission_buffer[70U]; + uint8_t transmission_buffer_p; + while(1) { TickType_t last_cycle = xTaskGetTickCount(); @@ -84,7 +87,7 @@ void vTaskObdhServer(void) break; case CMDPR_CMD_WRITE_PARAM: - obdh_write_read_bytes(6); + obdh_write_read_bytes(7); sys_log_print_event_from_module(SYS_LOG_INFO, TASK_OBDH_SERVER_NAME, "TX is now "); @@ -110,7 +113,7 @@ void vTaskObdhServer(void) break; case CMDPR_CMD_TRANSMIT_PACKET: - obdh_write_read_bytes(6); + obdh_write_read_bytes(7); downlink_add_packet(obdh_request.data.data_packet.packet, (obdh_request.data.data_packet.len)+3); @@ -120,12 +123,30 @@ void vTaskObdhServer(void) uplink_pop_packet(obdh_response.data.data_packet.packet, &(obdh_response.data.data_packet.len)); - obdh_send_response(&obdh_response); + spi_slave_dma_change_transfer_size((obdh_response.data.data_packet.len + 2)); + + transmission_buffer[0] = 0x7EU; + transmission_buffer[1] = 0x04U; + + for (transmission_buffer_p = 0U; transmission_buffer_p < obdh_response.data.data_packet.len; transmission_buffer_p++) + { + transmission_buffer[transmission_buffer_p + 2] = obdh_response.data.data_packet.packet[transmission_buffer_p]; + } + + spi_slave_dma_write(transmission_buffer, (obdh_response.data.data_packet.len + 2)); + + vTaskDelay(pdMS_TO_TICKS(130)); + + spi_slave_dma_read(NULL, (obdh_response.data.data_packet.len + 2)); + + spi_slave_dma_change_transfer_size(7U); + obdh_write_read_bytes(7U); + break; case 0x00: /* Read mode */ - obdh_write_read_bytes(6); + obdh_write_read_bytes(7U); break; default: break; From d26172c649cab5d8f5d1593c52051eb384693b77 Mon Sep 17 00:00:00 2001 From: miguelboing Date: Tue, 12 Mar 2024 11:43:48 -0300 Subject: [PATCH 018/103] firmware: drivers: spi_slave: Removing unused position variables #151 --- firmware/drivers/spi_slave/spi_slave.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/firmware/drivers/spi_slave/spi_slave.c b/firmware/drivers/spi_slave/spi_slave.c index af2520d1..696ab4c7 100644 --- a/firmware/drivers/spi_slave/spi_slave.c +++ b/firmware/drivers/spi_slave/spi_slave.c @@ -87,9 +87,6 @@ static int spi_slave_setup_gpio(spi_port_t port); static uint8_t spi_slave_dma_tx_data[70U] = {0U}; static uint8_t spi_slave_dma_rx_data[70U] = {0U}; -static uint16_t spi_slave_dma_tx_position; -static uint16_t spi_slave_dma_rx_position; - int spi_slave_init(spi_port_t port, spi_config_t config) { int err = 0; @@ -312,9 +309,6 @@ int spi_slave_init(spi_port_t port, spi_config_t config) DMA_enableTransfers(DMA_CHANNEL_0); - spi_slave_dma_rx_position = 0U; - spi_slave_dma_tx_position = 8U; - for(i = 0U; i < DMA_RX_TRANSFER_SIZE; i++) { spi_slave_dma_rx_data[i] = 0xFFU; From 73d3f42d9b050ee66f591a326f66e204508ab907 Mon Sep 17 00:00:00 2001 From: miguelboing Date: Tue, 12 Mar 2024 11:45:05 -0300 Subject: [PATCH 019/103] firmware: devices: obdh: Updating write packet implementation #151 --- firmware/devices/obdh/obdh.c | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/firmware/devices/obdh/obdh.c b/firmware/devices/obdh/obdh.c index 7d8e6cc9..ace251e3 100644 --- a/firmware/devices/obdh/obdh.c +++ b/firmware/devices/obdh/obdh.c @@ -368,30 +368,33 @@ void obdh_write_read_bytes(uint16_t number_of_bytes) // cppcheck-suppress misra- static int obdh_write_packet(obdh_response_t *obdh_response) { - int err = 0; - uint16_t buffer_size; - buffer_size = obdh_response->data.data_packet.len + 2; + int err = -1; - uint8_t buffer[buffer_size]; + uint8_t transmission_buffer[70U]; + uint8_t transmission_buffer_p; - buffer[0] = 0x7EU; - buffer[1] = 0x04U; - - for (uint16_t i = 2; i < buffer_size; i++) + if((obdh_response->data.data_packet.len + 2) < 70U) { - buffer[i] = obdh_response->data.data_packet.packet[i-2]; - } + spi_slave_dma_change_transfer_size((obdh_response->data.data_packet.len + 2)); - spi_slave_dma_change_transfer_size(buffer_size); + transmission_buffer[0] = 0x7EU; + transmission_buffer[1] = 0x04U; - spi_slave_dma_write(buffer, buffer_size); + for (transmission_buffer_p = 0U; transmission_buffer_p < obdh_response->data.data_packet.len; transmission_buffer_p++) + { + transmission_buffer[transmission_buffer_p + 2] = obdh_response->data.data_packet.packet[transmission_buffer_p]; + } + + spi_slave_dma_write(transmission_buffer, (obdh_response->data.data_packet.len + 2)); - sys_log_print_str("Packet:"); + vTaskDelay(pdMS_TO_TICKS(130)); - sys_log_dump_hex(obdh_response->data.data_packet.packet, obdh_response->data.data_packet.len); - sys_log_new_line(); + spi_slave_dma_read(NULL, (obdh_response->data.data_packet.len + 2)); - vTaskDelay(pdMS_TO_TICKS(100)); + spi_slave_dma_change_transfer_size(7U); + + err = 0; + } return err; } From 6cea47aa4cabf07b6895b6b1eeda6b34d4070330 Mon Sep 17 00:00:00 2001 From: miguelboing Date: Tue, 12 Mar 2024 11:46:16 -0300 Subject: [PATCH 020/103] firmware: app: tasks: Improvements for obdh_server task #151 --- firmware/app/tasks/obdh_server.c | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/firmware/app/tasks/obdh_server.c b/firmware/app/tasks/obdh_server.c index 303966d9..286aba1f 100644 --- a/firmware/app/tasks/obdh_server.c +++ b/firmware/app/tasks/obdh_server.c @@ -62,9 +62,6 @@ void vTaskObdhServer(void) obdh_response_t obdh_response = {0}; obdh_request.command = 0x00U; /* No command */ - uint8_t transmission_buffer[70U]; - uint8_t transmission_buffer_p; - while(1) { TickType_t last_cycle = xTaskGetTickCount(); @@ -123,30 +120,15 @@ void vTaskObdhServer(void) uplink_pop_packet(obdh_response.data.data_packet.packet, &(obdh_response.data.data_packet.len)); - spi_slave_dma_change_transfer_size((obdh_response.data.data_packet.len + 2)); - - transmission_buffer[0] = 0x7EU; - transmission_buffer[1] = 0x04U; - - for (transmission_buffer_p = 0U; transmission_buffer_p < obdh_response.data.data_packet.len; transmission_buffer_p++) - { - transmission_buffer[transmission_buffer_p + 2] = obdh_response.data.data_packet.packet[transmission_buffer_p]; - } - - spi_slave_dma_write(transmission_buffer, (obdh_response.data.data_packet.len + 2)); - - vTaskDelay(pdMS_TO_TICKS(130)); - - spi_slave_dma_read(NULL, (obdh_response.data.data_packet.len + 2)); + obdh_send_response(&obdh_response); - spi_slave_dma_change_transfer_size(7U); obdh_write_read_bytes(7U); - break; case 0x00: /* Read mode */ obdh_write_read_bytes(7U); + break; default: break; From 258afeebd770d8e17b81e36f85f18e3b75c822e8 Mon Sep 17 00:00:00 2001 From: miguelboing Date: Wed, 13 Mar 2024 17:01:41 -0300 Subject: [PATCH 021/103] firmware: drivers: spi_slave: Incresing dma buffer size #151 --- firmware/drivers/spi_slave/spi_slave.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/firmware/drivers/spi_slave/spi_slave.c b/firmware/drivers/spi_slave/spi_slave.c index 696ab4c7..800202da 100644 --- a/firmware/drivers/spi_slave/spi_slave.c +++ b/firmware/drivers/spi_slave/spi_slave.c @@ -84,8 +84,8 @@ static int spi_slave_setup_gpio(spi_port_t port); #define DMA_TX_TRANSFER_SIZE 7 #define DMA_RX_TRANSFER_SIZE 7 -static uint8_t spi_slave_dma_tx_data[70U] = {0U}; -static uint8_t spi_slave_dma_rx_data[70U] = {0U}; +static uint8_t spi_slave_dma_tx_data[230U] = {0U}; +static uint8_t spi_slave_dma_rx_data[230U] = {0U}; int spi_slave_init(spi_port_t port, spi_config_t config) { From 403466c01919e77f517d99391be04d8af1bb6970 Mon Sep 17 00:00:00 2001 From: miguelboing Date: Wed, 13 Mar 2024 17:06:36 -0300 Subject: [PATCH 022/103] firmware: devices: obdh: Updating transmit command #151 --- firmware/devices/obdh/obdh.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/firmware/devices/obdh/obdh.c b/firmware/devices/obdh/obdh.c index ace251e3..0a63ab94 100644 --- a/firmware/devices/obdh/obdh.c +++ b/firmware/devices/obdh/obdh.c @@ -114,20 +114,26 @@ int obdh_read_request(obdh_request_t *obdh_request) obdh_request->data.data_packet.len = request[2]; - obdh_write_read_bytes(request[2]+2U); + spi_slave_dma_change_transfer_size((request[2] + 3U)); - vTaskDelay(pdMS_TO_TICKS(100)); + obdh_write_read_bytes((request[2] + 3U)); - spi_slave_dma_read(obdh_request->data.data_packet.packet, (obdh_request->data.data_packet.len)+3); + vTaskDelay(pdMS_TO_TICKS(130)); + + spi_slave_dma_read(obdh_request->data.data_packet.packet, (obdh_request->data.data_packet.len + 3U)); + + /* Removing protocol bytes */ + for (uint16_t i = 0; i < (uint16_t)(request[2]); i++) + { + obdh_request->data.data_packet.packet[i] = obdh_request->data.data_packet.packet[i+3U]; + } sys_log_print_event_from_module(SYS_LOG_INFO, OBDH_MODULE_NAME, "Transmit packet command received:"); sys_log_print_uint(obdh_request->data.data_packet.len); sys_log_print_msg(" bytes."); sys_log_new_line(); - sys_log_print_str("Packet: "); - sys_log_dump_hex(&(obdh_request->data.data_packet.packet[3]), obdh_request->data.data_packet.len); - sys_log_new_line(); + spi_slave_dma_change_transfer_size(7U); break; case CMDPR_CMD_READ_FIRST_PACKET: @@ -282,6 +288,8 @@ int obdh_write_response_param(ttc_data_t *ttc_data_buf, obdh_response_t *obdh_re break; case CMDPR_PARAM_N_BYTES_FIRST_AV_RX: + /* Update rx packet bytes */ + ttc_data_buf->radio.last_rx_packet_bytes = ttc_data_buf->up_buf.packet_sizes[ttc_data_buf->up_buf.position_to_read]; obdh_response->data.param_16 = ttc_data_buf->radio.last_rx_packet_bytes; break; From 91d1742b294cb8193516bf4a4c704fe8a22e4bb0 Mon Sep 17 00:00:00 2001 From: miguelboing Date: Wed, 13 Mar 2024 17:07:27 -0300 Subject: [PATCH 023/103] firmware: app: structs: Fixing last packet number of bytes logic #151 --- firmware/app/structs/ttc_data.c | 8 -------- 1 file changed, 8 deletions(-) diff --git a/firmware/app/structs/ttc_data.c b/firmware/app/structs/ttc_data.c index 17329745..69390892 100644 --- a/firmware/app/structs/ttc_data.c +++ b/firmware/app/structs/ttc_data.c @@ -88,11 +88,6 @@ void uplink_add_packet(uint8_t *packet, uint16_t packet_size) ttc_data_buf.up_buf.packet_sizes[ttc_data_buf.up_buf.position_to_write] = packet_size; - if (ttc_data_buf.radio.rx_packet_counter == 0U) - { - ttc_data_buf.radio.last_rx_packet_bytes = packet_size; - } - for(i = 0; i < ttc_data_buf.up_buf.packet_sizes[ttc_data_buf.up_buf.position_to_write]; i++) { ttc_data_buf.up_buf.packet_array[ttc_data_buf.up_buf.position_to_write][i] = packet[i]; @@ -129,9 +124,6 @@ void uplink_pop_packet(uint8_t *packet, uint16_t *packet_size) { ttc_data_buf.up_buf.position_to_read = 0; } - - /* Update rx packet bytes */ - ttc_data_buf.radio.last_rx_packet_bytes = ttc_data_buf.up_buf.packet_sizes[ttc_data_buf.up_buf.position_to_read]; } } From fd7ae569531b716ebd3a0ac14910ac07f3e94bf8 Mon Sep 17 00:00:00 2001 From: miguelboing Date: Wed, 13 Mar 2024 17:08:54 -0300 Subject: [PATCH 024/103] firmware: app: tasks: Updating bytes numbers from transmit command #151 --- firmware/app/tasks/obdh_server.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firmware/app/tasks/obdh_server.c b/firmware/app/tasks/obdh_server.c index 286aba1f..2684887d 100644 --- a/firmware/app/tasks/obdh_server.c +++ b/firmware/app/tasks/obdh_server.c @@ -112,7 +112,7 @@ void vTaskObdhServer(void) case CMDPR_CMD_TRANSMIT_PACKET: obdh_write_read_bytes(7); - downlink_add_packet(obdh_request.data.data_packet.packet, (obdh_request.data.data_packet.len)+3); + downlink_add_packet(obdh_request.data.data_packet.packet, obdh_request.data.data_packet.len); break; case CMDPR_CMD_READ_FIRST_PACKET: From 81c2e5c44c37cc7e646ee7e077aa0f684d912143 Mon Sep 17 00:00:00 2001 From: miguelboing Date: Tue, 26 Mar 2024 18:20:07 -0300 Subject: [PATCH 025/103] firmware: app: tasks: Fixing logic for tx enable #151 --- firmware/app/tasks/obdh_server.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/firmware/app/tasks/obdh_server.c b/firmware/app/tasks/obdh_server.c index 2684887d..8db71626 100644 --- a/firmware/app/tasks/obdh_server.c +++ b/firmware/app/tasks/obdh_server.c @@ -91,12 +91,12 @@ void vTaskObdhServer(void) switch(obdh_request.data.param_8) { case 0x00: - sys_log_print_msg("Turned on."); + sys_log_print_msg("Turned off."); ttc_data_buf.radio.tx_enable = obdh_request.data.param_8; break; case 0x01: - sys_log_print_msg("Turned off."); + sys_log_print_msg("Turned on."); ttc_data_buf.radio.tx_enable = obdh_request.data.param_8; break; From 5d25ca7109dfea3a0d9e56bdc13a3f34bf0ecd73 Mon Sep 17 00:00:00 2001 From: miguelboing Date: Tue, 26 Mar 2024 20:07:32 -0300 Subject: [PATCH 026/103] firmware: app: tasks: Task escalation improvements --- firmware/app/tasks/eps_server.c | 10 +--------- firmware/app/tasks/eps_server.h | 6 +++--- firmware/app/tasks/obdh_server.h | 2 +- firmware/app/tasks/uplink_manager.c | 2 +- 4 files changed, 6 insertions(+), 14 deletions(-) diff --git a/firmware/app/tasks/eps_server.c b/firmware/app/tasks/eps_server.c index 7aa4a716..75cb3a54 100644 --- a/firmware/app/tasks/eps_server.c +++ b/firmware/app/tasks/eps_server.c @@ -74,15 +74,7 @@ void vTaskEpsServer(void) sys_log_new_line(); sys_log_print_str("Packet: "); - - uint16_t i = 0; - - for(i = 0; i < eps_request.data.data_packet.len; i++) - { - sys_log_print_hex(eps_request.data.data_packet.packet[i]); - sys_log_print_str("|"); - } - + sys_log_dump_hex(eps_request.data.data_packet.packet, eps_request.data.data_packet.len); sys_log_new_line(); downlink_add_packet(eps_request.data.data_packet.packet, eps_request.data.data_packet.len); diff --git a/firmware/app/tasks/eps_server.h b/firmware/app/tasks/eps_server.h index a9fbf253..1604077d 100644 --- a/firmware/app/tasks/eps_server.h +++ b/firmware/app/tasks/eps_server.h @@ -41,9 +41,9 @@ #include #define TASK_EPS_SERVER_NAME "EPS Server" /**< Task name. */ -#define TASK_EPS_SERVER_STACK_SIZE 2000 /**< Stack size in bytes. */ -#define TASK_EPS_SERVER_PRIORITY 4 /**< Task priority. */ -#define TASK_EPS_SERVER_PERIOD_MS 250 /**< Task period in milliseconds. */ +#define TASK_EPS_SERVER_STACK_SIZE 1000 /**< Stack size in bytes. */ +#define TASK_EPS_SERVER_PRIORITY 3 /**< Task priority. */ +#define TASK_EPS_SERVER_PERIOD_MS 750 /**< Task period in milliseconds. */ #define TASK_EPS_SERVER_INITIAL_DELAY_MS 1000 /**< Delay, in milliseconds, before the first execution. */ #define TASK_EPS_SERVER_INIT_TIMEOUT_MS 10000 /**< Wait time to initialize the task in milliseconds. */ diff --git a/firmware/app/tasks/obdh_server.h b/firmware/app/tasks/obdh_server.h index 8659e6ea..aa06f470 100644 --- a/firmware/app/tasks/obdh_server.h +++ b/firmware/app/tasks/obdh_server.h @@ -41,7 +41,7 @@ #include #define TASK_OBDH_SERVER_NAME "OBDH Server" /**< Task name. */ -#define TASK_OBDH_SERVER_STACK_SIZE 4000 /**< Stack size in bytes. */ +#define TASK_OBDH_SERVER_STACK_SIZE 2000 /**< Stack size in bytes. */ #define TASK_OBDH_SERVER_PRIORITY 5 /**< Task priority. */ #define TASK_OBDH_SERVER_PERIOD_MS 100 /**< Task period in milliseconds. */ #define TASK_OBDH_SERVER_INITIAL_DELAY_MS 200 /**< Delay, in milliseconds, before the first execution. */ diff --git a/firmware/app/tasks/uplink_manager.c b/firmware/app/tasks/uplink_manager.c index 7f4cbfb0..ea02434a 100644 --- a/firmware/app/tasks/uplink_manager.c +++ b/firmware/app/tasks/uplink_manager.c @@ -68,7 +68,7 @@ void vTaskUplinkManager(void) if (radio_available() == 0U) { - radio_recv(rx_packet, 230U, 100U); + radio_recv(rx_packet, 80U, 100U); sys_log_print_event_from_module(SYS_LOG_INFO, TASK_UPLINK_MANAGER_NAME, "Received a new package:"); sys_log_dump_hex(rx_packet, 230U); sys_log_new_line(); From 578e3eb921f7d78c51a46d54e8a90b30d12c82a5 Mon Sep 17 00:00:00 2001 From: Miguel Boing Date: Sat, 20 Apr 2024 11:51:46 -0300 Subject: [PATCH 027/103] firmware: drivers: spi_slave: Compliance with misra-c, closes #151 --- firmware/drivers/spi_slave/spi_slave.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/firmware/drivers/spi_slave/spi_slave.c b/firmware/drivers/spi_slave/spi_slave.c index 800202da..c11b3f38 100644 --- a/firmware/drivers/spi_slave/spi_slave.c +++ b/firmware/drivers/spi_slave/spi_slave.c @@ -84,8 +84,8 @@ static int spi_slave_setup_gpio(spi_port_t port); #define DMA_TX_TRANSFER_SIZE 7 #define DMA_RX_TRANSFER_SIZE 7 -static uint8_t spi_slave_dma_tx_data[230U] = {0U}; -static uint8_t spi_slave_dma_rx_data[230U] = {0U}; +static uint8_t spi_slave_dma_tx_data[230U] = {0}; +static uint8_t spi_slave_dma_rx_data[230U] = {0}; int spi_slave_init(spi_port_t port, spi_config_t config) { @@ -309,12 +309,12 @@ int spi_slave_init(spi_port_t port, spi_config_t config) DMA_enableTransfers(DMA_CHANNEL_0); - for(i = 0U; i < DMA_RX_TRANSFER_SIZE; i++) + for(i = 0U; i < (uint8_t) DMA_RX_TRANSFER_SIZE; i++) { spi_slave_dma_rx_data[i] = 0xFFU; } - for(i = 0U; i < DMA_TX_TRANSFER_SIZE; i++) + for(i = 0U; i < (uint8_t) DMA_TX_TRANSFER_SIZE; i++) { spi_slave_dma_tx_data[i] = 0xFFU; } From 7158bc044297ca46472cbd158681732a67751ccb Mon Sep 17 00:00:00 2001 From: Miguel Boing Date: Sat, 20 Apr 2024 11:52:45 -0300 Subject: [PATCH 028/103] firmware: obdh: Compliance with misra-c, closes #150, closes #149 --- firmware/devices/obdh/obdh.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firmware/devices/obdh/obdh.c b/firmware/devices/obdh/obdh.c index 0a63ab94..c7d544ca 100644 --- a/firmware/devices/obdh/obdh.c +++ b/firmware/devices/obdh/obdh.c @@ -390,7 +390,7 @@ static int obdh_write_packet(obdh_response_t *obdh_response) for (transmission_buffer_p = 0U; transmission_buffer_p < obdh_response->data.data_packet.len; transmission_buffer_p++) { - transmission_buffer[transmission_buffer_p + 2] = obdh_response->data.data_packet.packet[transmission_buffer_p]; + transmission_buffer[transmission_buffer_p + 2U] = obdh_response->data.data_packet.packet[transmission_buffer_p]; } spi_slave_dma_write(transmission_buffer, (obdh_response->data.data_packet.len + 2)); From 93a8db8a3bdc0e723f3fa792f259c2715574fa27 Mon Sep 17 00:00:00 2001 From: Miguel Boing Date: Sat, 20 Apr 2024 12:02:53 -0300 Subject: [PATCH 029/103] firmware: app: tasks: Fixing system reset delay --- firmware/app/tasks/system_reset.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/firmware/app/tasks/system_reset.c b/firmware/app/tasks/system_reset.c index c1e1c283..c3384afd 100644 --- a/firmware/app/tasks/system_reset.c +++ b/firmware/app/tasks/system_reset.c @@ -38,13 +38,15 @@ #include "system_reset.h" +#define pdMS_TO_TICKS_LONG( xTimeInMs ) ( ( TickType_t ) ( ( ( uint64_t ) ( xTimeInMs ) * ( uint64_t ) configTICK_RATE_HZ ) / ( TickType_t ) 1000 ) ) + xTaskHandle xTaskSystemResetHandle; void vTaskSystemReset(void) { while(1) { - vTaskDelay(pdMS_TO_TICKS(TASK_SYSTEM_RESET_PERIOD_MS)); + vTaskDelay(pdMS_TO_TICKS_LONG(TASK_SYSTEM_RESET_PERIOD_MS)); sys_log_print_event_from_module(SYS_LOG_INFO, TASK_SYSTEM_RESET_NAME, "Restarting the system..."); sys_log_new_line(); From 4cb3e2672a7bd3fc7665a74ae37a3f75d44f171a Mon Sep 17 00:00:00 2001 From: Miguel Boing Date: Sat, 20 Apr 2024 17:52:36 -0300 Subject: [PATCH 030/103] firwmare: tests: mockups: drivers: Updating spi_slave and si446x mockups --- firmware/tests/mockups/drivers/si446x_wrap.c | 10 ++++++++++ firmware/tests/mockups/drivers/si446x_wrap.h | 4 ++++ firmware/tests/mockups/drivers/spi_slave_wrap.c | 7 ++++++- firmware/tests/mockups/drivers/spi_slave_wrap.h | 4 +++- 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/firmware/tests/mockups/drivers/si446x_wrap.c b/firmware/tests/mockups/drivers/si446x_wrap.c index 60edb272..2459d5d5 100644 --- a/firmware/tests/mockups/drivers/si446x_wrap.c +++ b/firmware/tests/mockups/drivers/si446x_wrap.c @@ -349,4 +349,14 @@ void __wrap_si446x_delay_us(uint32_t us) check_expected(us); } +int __wrap_si446x_mutex_take(void) +{ + return mock_type(int); +} + +int __wrap_si446x_mutex_give(void) +{ + return mock_type(int); +} + /** \} End of si446x_wrap group */ diff --git a/firmware/tests/mockups/drivers/si446x_wrap.h b/firmware/tests/mockups/drivers/si446x_wrap.h index 7f61f918..e1ba0561 100644 --- a/firmware/tests/mockups/drivers/si446x_wrap.h +++ b/firmware/tests/mockups/drivers/si446x_wrap.h @@ -128,6 +128,10 @@ void __wrap_si446x_delay_ms(uint16_t ms); void __wrap_si446x_delay_us(uint32_t us); +int __wrap_si446x_mutex_give(void); + +int __wrap_si446x_mutex_take(void); + #endif /* SI446X_WRAP_H_ */ /** \} End of si446x_wrap group */ diff --git a/firmware/tests/mockups/drivers/spi_slave_wrap.c b/firmware/tests/mockups/drivers/spi_slave_wrap.c index d5cc260a..661cf1d8 100644 --- a/firmware/tests/mockups/drivers/spi_slave_wrap.c +++ b/firmware/tests/mockups/drivers/spi_slave_wrap.c @@ -127,11 +127,16 @@ int __wrap_spi_slave_flush(spi_port_t port) return mock_type(int); } -uint16_t spi_slave_bytes_not_sent(spi_port_t port) +uint16_t __wrap_spi_slave_bytes_not_sent(spi_port_t port) { check_expected(port); return mock_type(uint16_t); } +void __wrap_spi_slave_dma_change_transfer_size(uint16_t transfer_size) +{ + check_expected(transfer_size); +} + /** \} End of spi_slave_wrap group */ diff --git a/firmware/tests/mockups/drivers/spi_slave_wrap.h b/firmware/tests/mockups/drivers/spi_slave_wrap.h index bc53b60d..76f979a4 100644 --- a/firmware/tests/mockups/drivers/spi_slave_wrap.h +++ b/firmware/tests/mockups/drivers/spi_slave_wrap.h @@ -58,7 +58,9 @@ int __wrap_spi_slave_write(spi_port_t port, uint8_t *data, uint16_t len); int __wrap_spi_slave_flush(spi_port_t port); -uint16_t spi_slave_bytes_not_sent(spi_port_t port); +uint16_t __wrap_spi_slave_bytes_not_sent(spi_port_t port); + +void __wrap_spi_slave_dma_change_transfer_size(uint16_t transfer_size); #endif /* SPI_WRAP_H_ */ From 77f8bd4f2ee125e36a8bf74beecc91f733e0eaf8 Mon Sep 17 00:00:00 2001 From: Miguel Boing Date: Sat, 20 Apr 2024 17:53:41 -0300 Subject: [PATCH 031/103] firmware: tests: devices: Updating Makefile wraps #151 --- firmware/tests/devices/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/firmware/tests/devices/Makefile b/firmware/tests/devices/Makefile index b3034f29..507ecc7b 100644 --- a/firmware/tests/devices/Makefile +++ b/firmware/tests/devices/Makefile @@ -22,7 +22,7 @@ TEMP_SENSOR_TEST_FLAGS=$(FLAGS),--wrap=adc_init,--wrap=adc_read,--wrap=adc_temp_ ANTENNA_TEST_FLAGS=$(FLAGS),--wrap=isis_antenna_init,--wrap=isis_antenna_get_data,--wrap=isis_antenna_arm,--wrap=isis_antenna_disarm,--wrap=isis_antenna_start_sequential_deploy,--wrap=isis_antenna_start_independent_deploy,--wrap=isis_antenna_read_deployment_status_code,--wrap=isis_antenna_read_deployment_status,--wrap=isis_antenna_get_antenna_status,--wrap=isis_antenna_get_antenna_timeout,--wrap=isis_antenna_get_burning,--wrap=isis_antenna_get_arming_status,--wrap=isis_antenna_get_raw_temperature,--wrap=isis_antenna_raw_to_temp_c,--wrap=isis_antenna_get_temperature_c,--wrap=isis_antenna_delay_s,--wrap=isis_antenna_delay_ms -RADIO_TEST_FLAGS=$(FLAGS),--wrap=si446x_init,--wrap=si446x_reg_config,--wrap=si446x_power_on_reset,--wrap=si446x_tx_packet,--wrap=si446x_tx_long_packet,--wrap=si446x_rx_packet,--wrap=si446x_rx_init,--wrap=si446x_check_device,--wrap=si446x_check_cts,--wrap=si446x_set_tx_power,--wrap=si446x_set_properties,--wrap=si446x_get_properties,--wrap=si446x_set_config,--wrap=si446x_set_preamble_len,--wrap=si446x_set_sync_word,--wrap=si446x_set_gpio_mode,--wrap=si446x_set_cmd,--wrap=si446x_get_cmd,--wrap=si446x_set_tx_interrupt,--wrap=si446x_set_rx_interrupt,--wrap=si446x_clear_interrupts,--wrap=si446x_write_tx_fifo,--wrap=si446x_read_rx_fifo,--wrap=si446x_fifo_reset,--wrap=si446x_enter_tx_mode,--wrap=si446x_enter_rx_mode,--wrap=si446x_enter_standby_mode,--wrap=si446x_wait_nirq,--wrap=si446x_wait_packet_sent,--wrap=si446x_wait_gpio1,--wrap=si446x_spi_init,--wrap=si446x_spi_enable,--wrap=si446x_spi_disable,--wrap=si446x_spi_write,--wrap=si446x_spi_read,--wrap=si446x_spi_transfer,--wrap=si446x_gpio_init,--wrap=si446x_gpio_set_pin,--wrap=si446x_gpio_clear_pin,--wrap=si446x_gpio_get_pin,--wrap=si446x_delay_s,--wrap=si446x_delay_ms,--wrap=si446x_delay_us,--wrap=led_set,--wrap=led_clear +RADIO_TEST_FLAGS=$(FLAGS),--wrap=si446x_init,--wrap=si446x_reg_config,--wrap=si446x_power_on_reset,--wrap=si446x_tx_packet,--wrap=si446x_tx_long_packet,--wrap=si446x_rx_packet,--wrap=si446x_rx_init,--wrap=si446x_check_device,--wrap=si446x_check_cts,--wrap=si446x_set_tx_power,--wrap=si446x_set_properties,--wrap=si446x_get_properties,--wrap=si446x_set_config,--wrap=si446x_set_preamble_len,--wrap=si446x_set_sync_word,--wrap=si446x_set_gpio_mode,--wrap=si446x_set_cmd,--wrap=si446x_get_cmd,--wrap=si446x_set_tx_interrupt,--wrap=si446x_set_rx_interrupt,--wrap=si446x_clear_interrupts,--wrap=si446x_write_tx_fifo,--wrap=si446x_read_rx_fifo,--wrap=si446x_fifo_reset,--wrap=si446x_enter_tx_mode,--wrap=si446x_enter_rx_mode,--wrap=si446x_enter_standby_mode,--wrap=si446x_wait_nirq,--wrap=si446x_wait_packet_sent,--wrap=si446x_wait_gpio1,--wrap=si446x_spi_init,--wrap=si446x_spi_enable,--wrap=si446x_spi_disable,--wrap=si446x_spi_write,--wrap=si446x_spi_read,--wrap=si446x_spi_transfer,--wrap=si446x_gpio_init,--wrap=si446x_gpio_set_pin,--wrap=si446x_gpio_clear_pin,--wrap=si446x_gpio_get_pin,--wrap=si446x_delay_s,--wrap=si446x_delay_ms,--wrap=si446x_delay_us,--wrap=led_set,--wrap=led_clear,--wrap=si446x_mutex_take,--wrap=si446x_mutex_give POWER_SENSOR_TEST_FLAGS=$(FLAGS),--wrap=ina22x_init,--wrap=ina22x_configuration,--wrap=ina22x_calibration,--wrap=ina22x_write_reg,--wrap=ina22x_read_reg,--wrap=ina22x_get_current_A,--wrap=ina22x_get_voltage_V,--wrap=ina22x_get_power_W,--wrap=ina22x_get_manufacturer_id,--wrap=ina22x_get_die_id @@ -30,7 +30,7 @@ LEDS_TEST_FLAGS=$(FLAGS),--wrap=gpio_init,--wrap=gpio_set_state,--wrap=gpio_get_ MEDIA_TEST_FLAGS=$(FLAGS),--wrap=flash_init,--wrap=flash_write,--wrap=flash_write_single,--wrap=flash_read_single,--wrap=flash_write_long,--wrap=flash_read_long,--wrap=flash_erase -OBDH_TEST_FLAGS=$(FLAGS),--wrap=spi_slave_init,--wrap=spi_slave_dma_write,--wrap=spi_slave_dma_read,--wrap=spi_slave_enable_isr,--wrap=spi_slave_disable_isr,--wrap=spi_slave_read_available,--wrap=spi_slave_read,--wrap=spi_slave_write,--wrap=spi_slave_flush,--wrap=spi_slave_bytes_not_sent +OBDH_TEST_FLAGS=$(FLAGS),--wrap=spi_slave_init,--wrap=spi_slave_dma_write,--wrap=spi_slave_dma_read,--wrap=spi_slave_enable_isr,--wrap=spi_slave_disable_isr,--wrap=spi_slave_read_available,--wrap=spi_slave_read,--wrap=spi_slave_write,--wrap=spi_slave_flush,--wrap=spi_slave_bytes_not_sent,--wrap=spi_slave_dma_change_transfer_size EPS_TEST_FLAGS=$(FLAGS),--wrap=uart_init,--wrap=uart_write,--wrap=uart_read,--wrap=uart_rx_enable,--wrap=uart_rx_disable,--wrap=uart_read_available,--wrap=uart_flush From fdfe40ee30997982c9dd5c5439dc7a55749f9c74 Mon Sep 17 00:00:00 2001 From: Miguel Boing Date: Sat, 20 Apr 2024 17:54:26 -0300 Subject: [PATCH 032/103] firmware: tests: devices: Updating obdh unitary test #151 --- firmware/tests/devices/obdh_test.c | 95 ++++++++++++++++++++++-------- 1 file changed, 69 insertions(+), 26 deletions(-) diff --git a/firmware/tests/devices/obdh_test.c b/firmware/tests/devices/obdh_test.c index c5f996cc..e917f6d1 100644 --- a/firmware/tests/devices/obdh_test.c +++ b/firmware/tests/devices/obdh_test.c @@ -56,7 +56,7 @@ void generate_random_request(uint8_t *request); void generate_random_packet(uint8_t *packet, uint16_t len); -void generate_random_response(uint8_t *response); +void generate_random_response(obdh_response_t *response); /* OBDH Configuration */ spi_config_t spi_config = {0U, SPI_MODE_0}; @@ -83,6 +83,9 @@ static void obdh_read_request_test(void **state) uint8_t request[7] = {0}; uint16_t i = 0; + /* obdh_write_read_bytes */ + uint8_t buffer[230]; + generate_random_request(request); obdh_request.command = request[1]; @@ -119,15 +122,16 @@ static void obdh_read_request_test(void **state) obdh_request.data.data_packet.len = request[2]; - /* obdh_write_read_bytes */ - uint8_t buffer[230]; + buffer[0] = 0x7EU; - for(i = 0U; i < 230; i++) + for(i = 1U; i < 230; i++) { buffer[i] = 0x00U; } - dummy = request[2] + 2U; + expect_value(__wrap_spi_slave_dma_change_transfer_size, transfer_size, request[2] + 3); + + dummy = request[2] + 3U; expect_memory(__wrap_spi_slave_dma_write, data, buffer, (int)dummy); expect_value(__wrap_spi_slave_dma_write, len, dummy); @@ -143,6 +147,8 @@ static void obdh_read_request_test(void **state) will_return(__wrap_spi_slave_dma_read, obdh_request.data.data_packet.packet[i]); } + expect_value(__wrap_spi_slave_dma_change_transfer_size, transfer_size, 7U); + break; case CMDPR_CMD_READ_FIRST_PACKET: obdh_request.data.data_packet.len = request[2]; @@ -168,22 +174,26 @@ static void obdh_send_response_test(void **state) obdh_response_t obdh_response; uint8_t response[7]; - generate_random_response(response); + uint8_t transmission_buffer[70U]; + uint8_t transmission_buffer_p; - response[1] = CMDPR_CMD_READ_PARAM; - response[2] = CMDPR_PARAM_FW_VER; + generate_random_response(&(obdh_response)); - obdh_response.command = response[1]; + printf("obdh_command: %d\n", obdh_response.command); + printf("obdh_param:%d\n", obdh_response.parameter); + + response[0] = 0x7EU; + response[1] = obdh_response.command; switch(obdh_response.command) { case CMDPR_CMD_READ_PARAM: - obdh_response.parameter = response[2]; + response[2] = obdh_response.parameter; switch(cmdpr_param_size(obdh_response.parameter)) { case 1: - obdh_response.data.param_8 = response[3]; + response[3] = obdh_response.data.param_8; for(i = 4; i < 7; i++) { @@ -213,12 +223,37 @@ static void obdh_send_response_test(void **state) if (err == 0) { - expect_memory(__wrap_spi_slave_dma_write, data, &response[1], 6); - expect_value(__wrap_spi_slave_dma_write, len, 6); + expect_memory(__wrap_spi_slave_dma_write, data, response, 7U); + expect_value(__wrap_spi_slave_dma_write, len, (uint16_t) 7); } break; case CMDPR_CMD_READ_FIRST_PACKET: + err = -1; + + if ((obdh_response.data.data_packet.len + 2) < 70U) + { + + expect_value(__wrap_spi_slave_dma_change_transfer_size, transfer_size, (obdh_response.data.data_packet.len + 2)); + + transmission_buffer[0] = 0x7EU; + transmission_buffer[1] = 0x04U; + + for (transmission_buffer_p = 0U; transmission_buffer_p < obdh_response.data.data_packet.len; transmission_buffer_p++) + { + transmission_buffer[transmission_buffer_p + 2U] = obdh_response.data.data_packet.packet[transmission_buffer_p]; + } + + expect_memory(__wrap_spi_slave_dma_write, data, transmission_buffer, (obdh_response.data.data_packet.len + 2)); + expect_value(__wrap_spi_slave_dma_write, len, obdh_response.data.data_packet.len + 2); + + expect_value(__wrap_spi_slave_dma_read, len, (obdh_response.data.data_packet.len + 2)); + + expect_value(__wrap_spi_slave_dma_change_transfer_size, transfer_size, 7U); + + err = 0; + } + break; default: err = -1; @@ -226,7 +261,7 @@ static void obdh_send_response_test(void **state) break; } - assert_int_equal(obdh_send_response(&(obdh_response)), err); + assert_int_equal((int) obdh_send_response(&(obdh_response)), err); } int main(void) @@ -271,27 +306,35 @@ void generate_random_packet(uint8_t *packet, uint16_t len) } } -void generate_random_response(uint8_t *response) +void generate_random_response(obdh_response_t *response) { - uint8_t i; - response[0] = 0xE7; uint8_t answer_commands[] = {CMDPR_CMD_READ_PARAM, CMDPR_CMD_READ_FIRST_PACKET}; - response[1] = answer_commands[rand()%2]; + response->command = answer_commands[rand()%2]; - if (response[1] == CMDPR_CMD_READ_PARAM) + if (response->command == CMDPR_CMD_READ_PARAM) { - response[2] = (uint8_t)(rand()%24); - /* Random values */ - for(i = 3; i < 7; i ++) + response->parameter = (uint8_t)(rand()%24); + + switch(cmdpr_param_size(response->parameter)) { - /* From 0x00 to 0xFF */ - response[i] = (uint8_t)(rand() % 0x100); + case 1: + response->data.param_8 = (uint8_t)(rand() % 0x100); + + break; + case 2: + response->data.param_16 = (uint16_t)(rand() % 0x10000); + + break; + case 4: + response->data.param_32 = (uint32_t)(rand() % 0x100000000); + + break; } } - else if(response[1] == CMDPR_CMD_READ_FIRST_PACKET) + else if(response->command == CMDPR_CMD_READ_FIRST_PACKET) { - response[2] = (uint8_t)(rand() % max_packet_size + 1); + response->data.data_packet.len = (uint8_t)(rand() % max_packet_size + 1); } } From c3369e42c743de26468a93176f3e74a2026dbd0c Mon Sep 17 00:00:00 2001 From: Miguel Boing Date: Sat, 20 Apr 2024 17:57:31 -0300 Subject: [PATCH 033/103] firmware: devices: obdh: Adding stddef reference #151 --- firmware/devices/obdh/obdh.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/firmware/devices/obdh/obdh.c b/firmware/devices/obdh/obdh.c index c7d544ca..0486bf98 100644 --- a/firmware/devices/obdh/obdh.c +++ b/firmware/devices/obdh/obdh.c @@ -33,6 +33,8 @@ * \{ */ +#include + #include #include From 219e6e74999db4f99ca074d5c363387c9c8188db Mon Sep 17 00:00:00 2001 From: Miguel Boing Date: Mon, 22 Apr 2024 17:24:04 -0300 Subject: [PATCH 034/103] firmware: Updating version number for modified files --- firmware/app/structs/ttc_data.c | 4 ++-- firmware/app/tasks/eps_server.c | 4 ++-- firmware/app/tasks/eps_server.h | 4 ++-- firmware/app/tasks/obdh_server.c | 4 ++-- firmware/app/tasks/obdh_server.h | 4 ++-- firmware/app/tasks/system_reset.c | 4 ++-- firmware/app/tasks/uplink_manager.c | 4 ++-- firmware/config/config.h | 4 ++-- firmware/devices/obdh/obdh.c | 4 ++-- firmware/devices/radio/radio.c | 4 ++-- firmware/devices/radio/radio.h | 4 ++-- firmware/drivers/si446x/si446x.c | 4 ++-- firmware/drivers/si446x/si446x_config.h | 4 ++-- firmware/drivers/si446x/si446x_mutex.c | 4 ++-- firmware/drivers/spi_slave/spi_slave.c | 4 ++-- firmware/drivers/spi_slave/spi_slave.h | 4 ++-- firmware/tests/devices/obdh_test.c | 4 ++-- firmware/tests/drivers/si446x_test.c | 4 ++-- firmware/tests/mockups/drivers/si446x_wrap.c | 4 ++-- firmware/tests/mockups/drivers/si446x_wrap.h | 4 ++-- firmware/tests/mockups/drivers/spi_slave_wrap.c | 4 ++-- firmware/tests/mockups/drivers/spi_slave_wrap.h | 4 ++-- 22 files changed, 44 insertions(+), 44 deletions(-) diff --git a/firmware/app/structs/ttc_data.c b/firmware/app/structs/ttc_data.c index 69390892..add09c9a 100644 --- a/firmware/app/structs/ttc_data.c +++ b/firmware/app/structs/ttc_data.c @@ -26,9 +26,9 @@ * \author Gabriel Mariano Marcelino * \author Miguel Boing * - * \version 0.4.5 + * \version 0.5.1 * - * \date 2021/04/14 + * \date 2024/04/22 * * \addtogroup ttc_data * \{ diff --git a/firmware/app/tasks/eps_server.c b/firmware/app/tasks/eps_server.c index 75cb3a54..27712d84 100644 --- a/firmware/app/tasks/eps_server.c +++ b/firmware/app/tasks/eps_server.c @@ -25,9 +25,9 @@ * * \author Miguel Boing * - * \version 0.4.3 + * \version 0.5.1 * - * \date 2023/03/03 + * \date 2024/04/22 * * \addtogroup eps_server * \{ diff --git a/firmware/app/tasks/eps_server.h b/firmware/app/tasks/eps_server.h index 1604077d..43c45e0b 100644 --- a/firmware/app/tasks/eps_server.h +++ b/firmware/app/tasks/eps_server.h @@ -25,9 +25,9 @@ * * \author Miguel Boing * - * \version 0.3.5 + * \version 0.5.1 * - * \date 2023/03/03 + * \date 2024/04/22 * * \defgroup eps EPS * \ingroup tasks diff --git a/firmware/app/tasks/obdh_server.c b/firmware/app/tasks/obdh_server.c index 8db71626..92490b7b 100644 --- a/firmware/app/tasks/obdh_server.c +++ b/firmware/app/tasks/obdh_server.c @@ -25,9 +25,9 @@ * * \author Miguel Boing * - * \version 0.4.5 + * \version 0.5.1 * - * \date 2023/03/03 + * \date 2024/04/22 * * \addtogroup obdh_server * \{ diff --git a/firmware/app/tasks/obdh_server.h b/firmware/app/tasks/obdh_server.h index aa06f470..e8808d6c 100644 --- a/firmware/app/tasks/obdh_server.h +++ b/firmware/app/tasks/obdh_server.h @@ -25,9 +25,9 @@ * * \author Miguel Boing * - * \version 0.4.5 + * \version 0.5.1 * - * \date 2023/03/03 + * \date 2024/04/22 * * \defgroup obdh OBDH * \ingroup tasks diff --git a/firmware/app/tasks/system_reset.c b/firmware/app/tasks/system_reset.c index c3384afd..1c6ef984 100644 --- a/firmware/app/tasks/system_reset.c +++ b/firmware/app/tasks/system_reset.c @@ -25,9 +25,9 @@ * * \author Gabriel Mariano Marcelino * - * \version 0.1.10 + * \version 0.5.1 * - * \date 2020/01/12 + * \date 2024/04/22 * * \addtogroup system_reset * \{ diff --git a/firmware/app/tasks/uplink_manager.c b/firmware/app/tasks/uplink_manager.c index ea02434a..d650391c 100644 --- a/firmware/app/tasks/uplink_manager.c +++ b/firmware/app/tasks/uplink_manager.c @@ -25,9 +25,9 @@ * * \author Miguel Boing * - * \version 0.4.5 + * \version 0.5.1 * - * \date 2023/04/03 + * \date 2024/04/22 * * \addtogroup uplink_manager * \{ diff --git a/firmware/config/config.h b/firmware/config/config.h index 89d226ae..5b387ef3 100644 --- a/firmware/config/config.h +++ b/firmware/config/config.h @@ -26,9 +26,9 @@ * \author Gabriel Mariano Marcelino * \author Miguel Boing * - * \version 0.4.5 + * \version 0.5.1 * - * \date 2019/10/26 + * \date 2024/04/22 * * \defgroup config Configuration * \{ diff --git a/firmware/devices/obdh/obdh.c b/firmware/devices/obdh/obdh.c index 0486bf98..eaa2c417 100644 --- a/firmware/devices/obdh/obdh.c +++ b/firmware/devices/obdh/obdh.c @@ -25,9 +25,9 @@ * * \author Miguel Boing * - * \version 0.4.5 + * \version 0.5.1 * - * \date 2023/02/12 + * \date 2024/04/22 * * \addtogroup obdh * \{ diff --git a/firmware/devices/radio/radio.c b/firmware/devices/radio/radio.c index a8a13ae7..d26d6e10 100644 --- a/firmware/devices/radio/radio.c +++ b/firmware/devices/radio/radio.c @@ -26,9 +26,9 @@ * \author Gabriel Mariano Marcelino * \author Miguel Boing * - * \version 0.4.5 + * \version 0.5.1 * - * \date 2019/10/27 + * \date 2024/04/22 * * \addtogroup radio * \{ diff --git a/firmware/devices/radio/radio.h b/firmware/devices/radio/radio.h index 5841277a..7306a3b6 100644 --- a/firmware/devices/radio/radio.h +++ b/firmware/devices/radio/radio.h @@ -25,9 +25,9 @@ * * \author Gabriel Mariano Marcelino * - * \version 0.1.23 + * \version 0.5.1 * - * \date 2019/10/27 + * \date 2024/04/22 * * \defgroup radio Radio * \ingroup devices diff --git a/firmware/drivers/si446x/si446x.c b/firmware/drivers/si446x/si446x.c index 04ba3f23..0dfae83b 100644 --- a/firmware/drivers/si446x/si446x.c +++ b/firmware/drivers/si446x/si446x.c @@ -25,9 +25,9 @@ * * \author Gabriel Mariano Marcelino * - * \version 0.2.10 + * \version 0.5.1 * - * \date 2017/06/01 + * \date 2024/04/22 * * \addtogroup si446x * \{ diff --git a/firmware/drivers/si446x/si446x_config.h b/firmware/drivers/si446x/si446x_config.h index 6153a61b..495334c5 100644 --- a/firmware/drivers/si446x/si446x_config.h +++ b/firmware/drivers/si446x/si446x_config.h @@ -25,9 +25,9 @@ * * \author Gabriel Mariano Marcelino * - * \version 0.1.23 + * \version 0.5.1 * - * \date 2017/06/16 + * \date 2024/04/22 * * \addtogroup si446x * \{ diff --git a/firmware/drivers/si446x/si446x_mutex.c b/firmware/drivers/si446x/si446x_mutex.c index 830c3dd8..feda9ce2 100644 --- a/firmware/drivers/si446x/si446x_mutex.c +++ b/firmware/drivers/si446x/si446x_mutex.c @@ -26,9 +26,9 @@ * \author Gabriel Mariano Marcelino * \author Miguel Boing * - * \version 0.4.5 + * \version 0.5.1 * - * \date 2024/02/28 + * \date 2024/04/22 * * \addtogroup si446x * \{ diff --git a/firmware/drivers/spi_slave/spi_slave.c b/firmware/drivers/spi_slave/spi_slave.c index c11b3f38..b0805179 100644 --- a/firmware/drivers/spi_slave/spi_slave.c +++ b/firmware/drivers/spi_slave/spi_slave.c @@ -25,9 +25,9 @@ * * \author Miguel Boing * - * \version 0.4.5 + * \version 0.5.1 * - * \date 2022/05/21 + * \date 2024/04/22 * * \addtogroup spi_slave * \{ diff --git a/firmware/drivers/spi_slave/spi_slave.h b/firmware/drivers/spi_slave/spi_slave.h index 6c650dc2..c1a0613b 100644 --- a/firmware/drivers/spi_slave/spi_slave.h +++ b/firmware/drivers/spi_slave/spi_slave.h @@ -25,9 +25,9 @@ * * \author Miguel Boing * - * \version 0.4.5 + * \version 0.5.1 * - * \date 2022/05/21 + * \date 2024/04/22 * * \defgroup spi_slave SPI_SLAVE * \ingroup drivers diff --git a/firmware/tests/devices/obdh_test.c b/firmware/tests/devices/obdh_test.c index e917f6d1..99ee335a 100644 --- a/firmware/tests/devices/obdh_test.c +++ b/firmware/tests/devices/obdh_test.c @@ -25,9 +25,9 @@ * * \author Miguel Boing * - * \version 0.4.5 + * \version 0.5.1 * - * \date 2023/02/17 + * \date 2024/04/22 * * \defgroup obdh_unit_test OBDH * \ingroup tests diff --git a/firmware/tests/drivers/si446x_test.c b/firmware/tests/drivers/si446x_test.c index 7668547a..9c21e3f4 100644 --- a/firmware/tests/drivers/si446x_test.c +++ b/firmware/tests/drivers/si446x_test.c @@ -25,9 +25,9 @@ * * \author Gabriel Mariano Marcelino * - * \version 0.1.23 + * \version 0.5.1 * - * \date 2021/09/04 + * \date 2024/04/22 * * \defgroup si446x_unit_test Si446x * \ingroup tests diff --git a/firmware/tests/mockups/drivers/si446x_wrap.c b/firmware/tests/mockups/drivers/si446x_wrap.c index 2459d5d5..9951d503 100644 --- a/firmware/tests/mockups/drivers/si446x_wrap.c +++ b/firmware/tests/mockups/drivers/si446x_wrap.c @@ -25,9 +25,9 @@ * * \author Gabriel Mariano Marcelino * - * \version 0.1.23 + * \version 0.5.1 * - * \date 2021/04/27 + * \date 2024/04/22 * * \addtogroup si446x_wrap * \{ diff --git a/firmware/tests/mockups/drivers/si446x_wrap.h b/firmware/tests/mockups/drivers/si446x_wrap.h index e1ba0561..59ed4a0f 100644 --- a/firmware/tests/mockups/drivers/si446x_wrap.h +++ b/firmware/tests/mockups/drivers/si446x_wrap.h @@ -25,9 +25,9 @@ * * \author Gabriel Mariano Marcelino * - * \version 0.1.23 + * \version 0.5.1 * - * \date 2021/04/27 + * \date 2024/04/22 * * \defgroup si446x_wrap Si446x Wrap * \ingroup tests diff --git a/firmware/tests/mockups/drivers/spi_slave_wrap.c b/firmware/tests/mockups/drivers/spi_slave_wrap.c index 661cf1d8..258163c2 100644 --- a/firmware/tests/mockups/drivers/spi_slave_wrap.c +++ b/firmware/tests/mockups/drivers/spi_slave_wrap.c @@ -25,9 +25,9 @@ * * \author Miguel Boing * - * \version 0.4.5 + * \version 0.5.1 * - * \date 2022/05/22 + * \date 2024/04/22 * * \addtogroup spi_slave_wrap * \{ diff --git a/firmware/tests/mockups/drivers/spi_slave_wrap.h b/firmware/tests/mockups/drivers/spi_slave_wrap.h index 76f979a4..cd1ef8cb 100644 --- a/firmware/tests/mockups/drivers/spi_slave_wrap.h +++ b/firmware/tests/mockups/drivers/spi_slave_wrap.h @@ -25,9 +25,9 @@ * * \author Miguel Boing * - * \version 0.4.5 + * \version 0.5.1 * - * \date 2021/08/25 + * \date 2024/04/22 * * \defgroup spi_slave_wrap SPI Slave Wrap * \ingroup tests From 5d2b1e3d6a088ee662ed8393029be219ac1aea3f Mon Sep 17 00:00:00 2001 From: miguelboing Date: Thu, 25 Apr 2024 11:06:37 -0300 Subject: [PATCH 035/103] firmware: drivers: si446x: Adding stddef header file #152 --- firmware/drivers/si446x/si446x_mutex.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/firmware/drivers/si446x/si446x_mutex.c b/firmware/drivers/si446x/si446x_mutex.c index feda9ce2..b136c2c6 100644 --- a/firmware/drivers/si446x/si446x_mutex.c +++ b/firmware/drivers/si446x/si446x_mutex.c @@ -34,6 +34,8 @@ * \{ */ +#include + #include #include From 277a700c139dfcf80f638c6d426ea7e95516b4cb Mon Sep 17 00:00:00 2001 From: miguelboing Date: Thu, 25 Apr 2024 11:11:36 -0300 Subject: [PATCH 036/103] firmware: tests: freertos_sim: Adding semphr mockup functions for si446x tests #152 --- firmware/tests/freertos_sim/semphr.c | 50 +++++++++++++++++++++++ firmware/tests/freertos_sim/semphr.h | 59 ++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 firmware/tests/freertos_sim/semphr.c create mode 100644 firmware/tests/freertos_sim/semphr.h diff --git a/firmware/tests/freertos_sim/semphr.c b/firmware/tests/freertos_sim/semphr.c new file mode 100644 index 00000000..d3c34872 --- /dev/null +++ b/firmware/tests/freertos_sim/semphr.c @@ -0,0 +1,50 @@ +/* + * semphr.c + * + * Copyright (C) 2021, SpaceLab. + * + * This file is part of TTC 2.0. + * + * TTC 2.0 it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * TTC 2.0 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. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with TTC 2.0. If not, see . + * + */ + +/** + * \brief FreeRTOS semphr simulation implementation. + * + * \author Miguel Boing + * + * \version 0.5.1 + * + * \date 2024/04/25 + * + * \addtogroup semphr_sim + * \{ + */ +#include "semphr.h" + +SemaphoreHandle_t xSemaphoreCreateMutex(void) +{ + return (SemaphoreHandle_t) 0x01; +} +BaseType_t xSemaphoreTake(SemaphoreHandle_t xSemaphore, TickType_t xBlockTime) +{ + return pdTRUE; +} + +BaseType_t xSemaphoreGive(SemaphoreHandle_t xSemaphore) +{ + return pdTRUE; +} + +/** \} End of semphr_sim group */ \ No newline at end of file diff --git a/firmware/tests/freertos_sim/semphr.h b/firmware/tests/freertos_sim/semphr.h new file mode 100644 index 00000000..18336f7b --- /dev/null +++ b/firmware/tests/freertos_sim/semphr.h @@ -0,0 +1,59 @@ +/* + * semphr.h + * + * Copyright (C) 2021, SpaceLab. + * + * This file is part of TTC 2.0. + * + * TTC 2.0 is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * TTC 2.0 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. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with TTC 2.0. If not, see . + * + */ + +/** + * \brief FreeRTOS semphr simulation definition. + * + * \author Miguel Boing + * + * \version 5.1.0 + * + * \date 2024/04/25 + * + * \defgroup semphr_sim FreeRTOS semphr + * \ingroup tests + * \{ + */ + +#ifndef SEMPHR_SIM_H_ +#define SEMPHR_SIM_H_ + +#include +#include "FreeRTOS.h" + +typedef enum +{ + pdFALSE=0x00, + pdTRUE=0x01, +}BaseType_t; + +typedef int* SemaphoreHandle_t; + +SemaphoreHandle_t xSemaphoreCreateMutex(void); + +BaseType_t xSemaphoreTake(SemaphoreHandle_t xSemaphore, TickType_t xBlockTime); + +BaseType_t xSemaphoreGive(SemaphoreHandle_t xSemaphore); + +#endif /* SEMPHR_SIM_H_ */ + +/** \} End of semphr_sim group */ \ No newline at end of file From 41d65757617d8f0f5f84f4d4514babba13ebab28 Mon Sep 17 00:00:00 2001 From: miguelboing Date: Thu, 25 Apr 2024 11:14:42 -0300 Subject: [PATCH 037/103] firmware: tests: drivers: Including semphr and mutex files to si446x rule #152 --- firmware/tests/drivers/Makefile | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/firmware/tests/drivers/Makefile b/firmware/tests/drivers/Makefile index 28e9206e..2b6fd073 100644 --- a/firmware/tests/drivers/Makefile +++ b/firmware/tests/drivers/Makefile @@ -34,8 +34,8 @@ isis_antenna_test: $(BUILD_DIR)/isis_antenna.o $(BUILD_DIR)/isis_antenna_delay.o $(CC) $(ISIS_ANTENNA_TEST_FLAGS) $(BUILD_DIR)/isis_antenna.o $(BUILD_DIR)/isis_antenna_delay.o $(BUILD_DIR)/isis_antenna_i2c.o $(BUILD_DIR)/isis_antenna_test.o $(BUILD_DIR)/sys_log_wrap.o $(BUILD_DIR)/tca4311a_wrap.o $(BUILD_DIR)/task.o -o $(BUILD_DIR)/$(TARGET_ISIS_ANTENNA) -lm -lcmocka .PHONY: si446x_test -si446x_test: $(BUILD_DIR)/si446x.o $(BUILD_DIR)/si446x_delay.o $(BUILD_DIR)/si446x_gpio.o $(BUILD_DIR)/si446x_spi.o $(BUILD_DIR)/si446x_test.o $(BUILD_DIR)/sys_log_wrap.o $(BUILD_DIR)/spi_wrap.o $(BUILD_DIR)/gpio_wrap.o $(BUILD_DIR)/task.o - $(CC) $(SI446X_TEST_FLAGS) $(BUILD_DIR)/si446x.o $(BUILD_DIR)/si446x_delay.o $(BUILD_DIR)/si446x_gpio.o $(BUILD_DIR)/si446x_spi.o $(BUILD_DIR)/si446x_test.o $(BUILD_DIR)/sys_log_wrap.o $(BUILD_DIR)/spi_wrap.o $(BUILD_DIR)/gpio_wrap.o $(BUILD_DIR)/task.o -o $(BUILD_DIR)/$(TARGET_SI446X) -lm -lcmocka +si446x_test: $(BUILD_DIR)/si446x.o $(BUILD_DIR)/si446x_delay.o $(BUILD_DIR)/si446x_gpio.o $(BUILD_DIR)/si446x_spi.o $(BUILD_DIR)/si446x_mutex.o $(BUILD_DIR)/si446x_test.o $(BUILD_DIR)/sys_log_wrap.o $(BUILD_DIR)/spi_wrap.o $(BUILD_DIR)/gpio_wrap.o $(BUILD_DIR)/task.o $(BUILD_DIR)/semphr.o + $(CC) $(SI446X_TEST_FLAGS) $(BUILD_DIR)/si446x.o $(BUILD_DIR)/si446x_delay.o $(BUILD_DIR)/si446x_gpio.o $(BUILD_DIR)/si446x_spi.o $(BUILD_DIR)/si446x_mutex.o $(BUILD_DIR)/si446x_test.o $(BUILD_DIR)/sys_log_wrap.o $(BUILD_DIR)/spi_wrap.o $(BUILD_DIR)/gpio_wrap.o $(BUILD_DIR)/task.o $(BUILD_DIR)/semphr.o -o $(BUILD_DIR)/$(TARGET_SI446X) -lm -lcmocka .PHONY: ina22x_test ina22x_test: $(BUILD_DIR)/ina22x.o $(BUILD_DIR)/ina22x_test.o $(BUILD_DIR)/sys_log_wrap.o $(BUILD_DIR)/i2c_wrap.o @@ -67,6 +67,9 @@ $(BUILD_DIR)/si446x_delay.o: ../../drivers/si446x/si446x_delay.c $(BUILD_DIR)/si446x_gpio.o: ../../drivers/si446x/si446x_gpio.c $(CC) $(SI446X_TEST_FLAGS) -c $< -o $@ +$(BUILD_DIR)/si446x_mutex.o: ../../drivers/si446x/si446x_mutex.c + $(CC) $(SI446X_TEST_FLAGS) -c $< -o $@ + $(BUILD_DIR)/si446x_spi.o: ../../drivers/si446x/si446x_spi.c $(CC) $(SI446X_TEST_FLAGS) -c $< -o $@ @@ -120,6 +123,9 @@ $(BUILD_DIR)/tca4311a_wrap.o: ../mockups/drivers/tca4311a_wrap.c $(BUILD_DIR)/task.o: ../freertos_sim/task.c $(CC) $(FLAGS) -c $< -o $@ +$(BUILD_DIR)/semphr.o: ../freertos_sim/semphr.c + $(CC) $(FLAGS) -c $< -o $@ + .PHONY: clean clean: rm $(BUILD_DIR)/$(TARGET_TCA4311A) $(BUILD_DIR)/$(TARGET_ISIS_ANTENNA) $(BUILD_DIR)/$(TARGET_SI446X) $(BUILD_DIR)/$(TARGET_INA22X) $(BUILD_DIR)/$(TARGET_TPS382X) $(BUILD_DIR)/*.o From 05ae89275684da3580731e275875253986ff21e1 Mon Sep 17 00:00:00 2001 From: miguelboing Date: Thu, 25 Apr 2024 11:32:01 -0300 Subject: [PATCH 038/103] firmware: tests: radio: Adding mutex implementation to radio test #152 --- firmware/tests/devices/radio_test.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/firmware/tests/devices/radio_test.c b/firmware/tests/devices/radio_test.c index f90d61c0..bd614bd9 100644 --- a/firmware/tests/devices/radio_test.c +++ b/firmware/tests/devices/radio_test.c @@ -25,9 +25,9 @@ * * \author Gabriel Mariano Marcelino * - * \version 0.2.12 + * \version 0.5.1 * - * \date 2021/02/21 + * \date 2024/04/25 * * \defgroup radio_unit_test Radio * \ingroup tests @@ -63,6 +63,8 @@ static void radio_send_test(void **state) uint8_t data[50] = {0}; uint16_t len = 50; + will_return(__wrap_si446x_mutex_take, 0); + expect_value(__wrap_led_set, l, LED_DOWNLINK); will_return(__wrap_led_set, 0); @@ -79,6 +81,8 @@ static void radio_send_test(void **state) expect_value(__wrap_led_clear, l, LED_DOWNLINK); will_return(__wrap_led_clear, 0); + will_return(__wrap_si446x_mutex_give, 0); + assert_return_code(radio_send(data, len), 0); } @@ -89,6 +93,8 @@ static void radio_recv_test(void **state) uint32_t timeout_ms = 100; uint16_t i; + will_return(__wrap_si446x_mutex_take, 0); + will_return(__wrap_si446x_wait_nirq, true); expect_value(__wrap_si446x_rx_packet, rx_buf, data); @@ -104,13 +110,19 @@ static void radio_recv_test(void **state) will_return(__wrap_si446x_rx_init, true); + will_return(__wrap_si446x_mutex_give, 0); + assert_return_code(radio_recv(data, len, timeout_ms), len); } static void radio_sleep_test(void **state) { + will_return(__wrap_si446x_mutex_take, 0); + will_return(__wrap_si446x_enter_standby_mode, 0); + will_return(__wrap_si446x_mutex_give, 0); + assert_return_code(radio_sleep(), 0); } From 2ab2ce60113a767f7ce6d552f83150e20f7e14ad Mon Sep 17 00:00:00 2001 From: miguelboing Date: Thu, 25 Apr 2024 11:52:39 -0300 Subject: [PATCH 039/103] firmware: app: tasks: Uplink manager changes for misra-c compliance #152 --- firmware/app/tasks/uplink_manager.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/firmware/app/tasks/uplink_manager.c b/firmware/app/tasks/uplink_manager.c index d650391c..31e9c87c 100644 --- a/firmware/app/tasks/uplink_manager.c +++ b/firmware/app/tasks/uplink_manager.c @@ -60,7 +60,7 @@ void vTaskUplinkManager(void) ttc_data_buf.up_buf.position_to_read = 0U; ttc_data_buf.up_buf.position_to_write = 0U; - uint8_t rx_packet[230] = {0U}; + uint8_t rx_packet[230] = {0}; while(1) { @@ -73,7 +73,7 @@ void vTaskUplinkManager(void) sys_log_dump_hex(rx_packet, 230U); sys_log_new_line(); - //ngham_decode(rx_packet); /* TODO */ + /*TODO ngham_decode(rx_packet);*/ uplink_add_packet(rx_packet, 220U); } From 14511ea643ea44e803cf8e1855410ca81dc1722e Mon Sep 17 00:00:00 2001 From: Gabriel Mariano Marcelino Date: Thu, 25 Apr 2024 13:26:58 -0300 Subject: [PATCH 040/103] firmware: Updating the firmware version --- firmware/version.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/firmware/version.h b/firmware/version.h index b0ced893..70ffccdf 100644 --- a/firmware/version.h +++ b/firmware/version.h @@ -25,7 +25,7 @@ * * \author Gabriel Mariano Marcelino * - * \version 0.5.0 + * \version 0.5.1 * * \date 2021/04/04 * @@ -36,7 +36,7 @@ #ifndef VERSION_H_ #define VERSION_H_ -#define FIRMWARE_VERSION "0.5.0" +#define FIRMWARE_VERSION "0.5.1" #define FIRMWARE_STATUS "Development" From d1d2e99c1a99f7ed55aa9ba4ac2b9bf1c651b362 Mon Sep 17 00:00:00 2001 From: Miguel Boing Date: Thu, 16 May 2024 10:15:07 -0300 Subject: [PATCH 041/103] firware: app: tasks: Fixing device id struct value #146 --- firmware/app/tasks/startup.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/firmware/app/tasks/startup.c b/firmware/app/tasks/startup.c index c68e1993..42998b86 100644 --- a/firmware/app/tasks/startup.c +++ b/firmware/app/tasks/startup.c @@ -92,8 +92,8 @@ void vTaskStartup(void) /* TTC parameters */ ttc_data_buf.hw_version = 0x04; - ttc_data_buf.fw_version = 0x00000400; - ttc_data_buf.device_id = 0xCC2B; + ttc_data_buf.fw_version = 0x00000405; + ttc_data_buf.device_id = 0xCC2A + RADIO_MODULE; #if defined(CONFIG_DEV_MEDIA_INT_ENABLED) && (CONFIG_DEV_MEDIA_INT_ENABLED == 1) /* Internal non-volatile memory initialization */ From 0518235ac7b717f4b11686eddb3e6500f04766f9 Mon Sep 17 00:00:00 2001 From: Miguel Boing Date: Mon, 3 Jun 2024 19:44:42 -0300 Subject: [PATCH 042/103] firmware: app: libs: Porting rsclib1.0 #136 --- firmware/app/libs/ngham-0.1/src/ngham.c | 2 +- firmware/app/libs/ngham-0.1/tests/test.c | 183 +++++-- firmware/app/libs/rsclib-0.1/src/rsc.c | 464 ---------------- .../.github/workflows/test.yml | 0 .../{rsclib-0.1 => rsclib-1.0}/.gitignore | 0 .../{rsclib-0.1 => rsclib-1.0}/CMakeLists.txt | 0 .../libs/{rsclib-0.1 => rsclib-1.0}/LICENSE | 0 .../libs/{rsclib-0.1 => rsclib-1.0}/README.md | 0 .../include/rsc/rsc.h | 27 +- firmware/app/libs/rsclib-1.0/src/rsc.c | 514 ++++++++++++++++++ .../tests/.gitignore | 0 .../tests/CMakeLists.txt | 0 .../tests/README.md | 0 .../{rsclib-0.1 => rsclib-1.0}/tests/test.c | 0 14 files changed, 671 insertions(+), 519 deletions(-) delete mode 100644 firmware/app/libs/rsclib-0.1/src/rsc.c rename firmware/app/libs/{rsclib-0.1 => rsclib-1.0}/.github/workflows/test.yml (100%) rename firmware/app/libs/{rsclib-0.1 => rsclib-1.0}/.gitignore (100%) rename firmware/app/libs/{rsclib-0.1 => rsclib-1.0}/CMakeLists.txt (100%) rename firmware/app/libs/{rsclib-0.1 => rsclib-1.0}/LICENSE (100%) rename firmware/app/libs/{rsclib-0.1 => rsclib-1.0}/README.md (100%) rename firmware/app/libs/{rsclib-0.1 => rsclib-1.0}/include/rsc/rsc.h (77%) create mode 100644 firmware/app/libs/rsclib-1.0/src/rsc.c rename firmware/app/libs/{rsclib-0.1 => rsclib-1.0}/tests/.gitignore (100%) rename firmware/app/libs/{rsclib-0.1 => rsclib-1.0}/tests/CMakeLists.txt (100%) rename firmware/app/libs/{rsclib-0.1 => rsclib-1.0}/tests/README.md (100%) rename firmware/app/libs/{rsclib-0.1 => rsclib-1.0}/tests/test.c (100%) diff --git a/firmware/app/libs/ngham-0.1/src/ngham.c b/firmware/app/libs/ngham-0.1/src/ngham.c index 6768e321..9ad8eea1 100755 --- a/firmware/app/libs/ngham-0.1/src/ngham.c +++ b/firmware/app/libs/ngham-0.1/src/ngham.c @@ -26,7 +26,7 @@ * \author Gabriel Mariano Marcelino * * \version 0.1.0 - * + * * \date 2023/03/12 * * \addtogroup ngham diff --git a/firmware/app/libs/ngham-0.1/tests/test.c b/firmware/app/libs/ngham-0.1/tests/test.c index 007f1092..3576157c 100644 --- a/firmware/app/libs/ngham-0.1/tests/test.c +++ b/firmware/app/libs/ngham-0.1/tests/test.c @@ -1,96 +1,197 @@ /* * test.c * - * Copyright The NGHam Contributors. + * Copyright The RSCLib Contributors. * - * This file is part of NGHam. + * This file is part of RSCLib. * - * NGHam is free software: you can redistribute it and/or modify + * RSCLib is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * - * NGHam is distributed in the hope that it will be useful, + * RSCLib 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. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License - * along with NGHam. If not, see . + * along with RSCLib. If not, see . * */ /** - * \brief NGHam library unit test. + * \brief Reed-Solomon C library unit test. * * \author Gabriel Mariano Marcelino + * \author Miguel Boing + * + * \version 1.0.1 * - * \version 0.1.0 - * - * \date 2023/03/12 + * \date 2024/04/30 * * \defgroup test Test - * \ingroup ngham + * \ingroup rsclib * \{ */ #include #include #include +#include +#include +#include +#include #include #include +#include #include +#include + +#include -#include +uint8_t random_value(uint8_t min, uint8_t max); -static void ngham_init_test(void **state) +static void rsc_init_test(void **state) { - assert_return_code(ngham_init(), 0); + reed_solomon_t rs16 = {0}; + + assert_return_code(rsc_init(8, 0x187, 112, 11, 16, 208, &rs16), 0); + + /* Expected results */ + uint8_t alpha_to[50] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x87, 0x89, 0x95, 0xAD, 0xDD, 0x3D, 0x7A, 0xF4}; + uint8_t index_of[50] = {0xFF, 0x00, 0x01, 0x63, 0x02, 0xC6, 0x64, 0x6A, 0x03, 0xCD, 0xC7, 0xBC, 0x65, 0x7E, 0x6B, 0x2A}; + uint8_t genpoly[50] = {0x7A, 0xF0, 0x12, 0xB4, 0xC7, 0xB5, 0xDD, 0x31, 0xEA, 0xE1, 0x3F, 0xC7, 0x8A, 0x28, 0x36, 0xC5}; + + assert_int_equal(rs16.mm, 8); + assert_int_equal(rs16.nn, 0xFF); + assert_memory_equal(rs16.alpha_to, alpha_to, 16); + assert_memory_equal(rs16.index_of, index_of, 16); + assert_memory_equal(rs16.genpoly, genpoly, 16); + assert_int_equal(rs16.nroots, 0x10); + assert_int_equal(rs16.fcr, 112); + assert_int_equal(rs16.prim, 0x0B); + assert_int_equal(rs16.iprim, 116); + assert_int_equal(rs16.pad, 208); } -static void ngham_encode_test(void **state) +static void rsc_encode_test(void **state) { - uint8_t data[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - uint16_t data_len = 10U; - uint8_t flags = 0U; + /* Expected result */ + uint8_t par_ref[32] = {219, 47, 89, 171, 224, 14, 4, 30, 113, 224, 22, 100, 152, 45, 160, 32}; - uint8_t pkt[300] = {0U}; - uint16_t pkt_len = UINT16_MAX; + reed_solomon_t rs16 = {0}; - assert_return_code(ngham_encode(data, data_len, flags, pkt, &pkt_len), 0); + rsc_init(8, 0x187, 112, 11, 16, 208, &rs16); - uint8_t expected_pkt[300] = {170, 170, 170, 170, 93, 230, 42, 126, 59, 73, 205, 237, 72, 15, 194, 153, 9, 117, 186, 137, 36, 154, 52, 142, 183, 70, 206, 90, 151, 125, 204, 50, 162, 191, 62, 10, 16, 241, 136, 148, 205, 234, 224, 253, 216, 143, 5, 252, 58, 82, 207, 117, 235, 194, 57, 208, 120, 236}; - uint16_t expected_pkt_len = 58U; + uint8_t data[32] = {0}; + uint8_t par[32] = {0}; + uint8_t par_len = 0; - assert_memory_equal((void*)pkt, (void*)expected_pkt, expected_pkt_len); - assert_int_equal(pkt_len, expected_pkt_len); -} + uint8_t i = 0; + for(i = 0; i < 32; i++) + { + data[i] = i; + } -static void ngham_decode_test(void **state) -{ -} + rsc_encode(&rs16, data, par, &par_len); -static void crc_ccitt_byte_test(void **state) -{ -// assert_return_code(crc_ccitt_byte(), ); + assert_int_equal(par_len, 16); + assert_memory_equal(par, par_ref, par_len); } -static void crc_ccitt_test(void **state) +static void rsc_decode_test(void **state) { -// assert_return_code(crc_ccitt(), ); -} + reed_solomon_t rs = {0}; + int rs_padding, rs_msg_size, rs_nroots; + + uint8_t data[255] = {0U}; + uint8_t par[255] = {0U}; + uint8_t par_len = 0U; + uint8_t pkt[300] = {0U}; + int err_pos[255] = {0U}; + int expected_err_pos[255] = {0U}; + int num_err = 0U; + int s_err = 0U; + int s_exp_err = 0U; + uint8_t rand_number_of_errors; + bool rand_error_pos_arr[255]; + uint8_t rand_error_position; + uint8_t rand_error_value; + + rs_nroots = (int) (random_value(1,2)) * 16; + + rs_msg_size = random_value(1,255 - (uint8_t)rs_nroots); + rs_padding = 255 - rs_msg_size - (uint8_t)rs_nroots; + + rsc_init(8, 0x187, 112, 11, rs_nroots, rs_padding, &rs); + + uint8_t i,j = 0; + for(i=0;i. - * - */ - -/** - * \brief Reed-Solomon C library implementation. - * - * \author Gabriel Mariano Marcelino - * - * \version 0.1.0 - * - * \date 2022/03/06 - * - * \addtogroup rsclib - * \{ - */ - -#include - -#include - -#define MIN(a, b) ((a) < (b) ? (a) : (b)) - -/** - * \brief Computes the modulo of a given number. - * - * \param[in] rs is the Reed-Solomon scheme used to compute the modulo. - * - * \param[in] x is the integer to compute the modulo. - * - * \return The computed modulo value of the given number. - */ -int modnn(reed_solomon_t *rs, int x); - -int rsc_init(int symsize, int gfpoly, int fcr, int prim, int nroots, int pad, reed_solomon_t *rs) -{ - int err = 0; - - int i, j, sr, root, iprim; - - /* Check parameter ranges */ - if ((symsize >= 0) && (symsize <= (8 * sizeof(uint8_t)))) - { - if ((fcr >= 0) && (fcr < (1 << symsize))) - { - if ((prim > 0) && (prim < (1 << symsize))) - { - if ((nroots >= 0) && (nroots < (1 << symsize))) - { - if ((pad >= 0) && (pad < ((1 << symsize) - 1 - nroots))) - { - rs->mm = symsize; - rs->nn = (1 << symsize) - 1; - rs->pad = pad; - - /* Generate Galois field lookup tables */ - rs->index_of[0] = rs->nn; /* log(zero) = -inf */ - rs->alpha_to[rs->nn] = 0; /* alpha**-inf = 0 */ - sr = 1; - for(i = 0; i < rs->nn; i++) - { - rs->index_of[sr] = i; - rs->alpha_to[i] = sr; - sr <<= 1; - if (sr & (1 << symsize)) - { - sr ^= gfpoly; - } - sr &= rs->nn; - } - if (sr == 1) - { - rs->fcr = fcr; - rs->prim = prim; - rs->nroots = nroots; - - /* Find prim-th root of 1, used in decoding */ - for(iprim = 1; (iprim % prim) != 0; iprim += rs->nn) - { - } - rs->iprim = iprim / prim; - - rs->genpoly[0] = 1; - for(i = 0, root = fcr * prim; i < nroots; i++, root += prim) - { - rs->genpoly[i + 1] = 1; - - /* Multiply rs->genpoly[] by @**(root + x) */ - for(j = i; j > 0; j--) - { - if (rs->genpoly[j] != 0) - { - rs->genpoly[j] = rs->genpoly[j - 1] ^ rs->alpha_to[modnn(rs, rs->index_of[rs->genpoly[j]] + root)]; - } - else - { - rs->genpoly[j] = rs->genpoly[j - 1]; - } - } - /* rs->genpoly[0] can never be zero */ - rs->genpoly[0] = rs->alpha_to[modnn(rs, rs->index_of[rs->genpoly[0]] + root)]; - } - /* convert rs->genpoly[] to index form for quicker encoding */ - for(i = 0; i <= nroots; i++) - { - rs->genpoly[i] = rs->index_of[rs->genpoly[i]]; - } - - err = 0; - } - else - { - /* field generator polynomial is not primitive! */ - memset(rs, 0, sizeof(reed_solomon_t)); - - err = -1; - } - } - else - { - err = 0; /* Too much padding */ - } - } - else - { - err = -1; /* Can't have more roots than symbol values! */ - } - } - else - { - err = -1; - } - } - else - { - err = -1; - } - } - else - { - err = -1; - } - - return err; -} - -void rsc_encode(reed_solomon_t *rs, uint8_t *data, uint8_t *parity, uint8_t *parity_len) -{ - int i = 0; - int j = 0; - int feedback = 0; - - memset(parity, 0, rs->nroots); - - for(i = 0; i < (rs->nn - rs->nroots - rs->pad); i++) - { - feedback = rs->index_of[data[i] ^ parity[0]]; - if (feedback != rs->nn) /* feedback term is non-zero */ - { - for(j = 0; j < rs->nroots; j++) - { - parity[j] ^= rs->alpha_to[modnn(rs, feedback + rs->genpoly[rs->nroots - j])]; - } - } - /* Shift */ - memmove(&parity[0], &parity[1], rs->nroots - 1); - if (feedback != rs->nn) - { - parity[rs->nroots - 1] = rs->alpha_to[modnn(rs, feedback + rs->genpoly[0])]; - } - else - { - parity[rs->nroots - 1] = 0U; - } - } - - *parity_len = rs->nroots; -} - -int rsc_decode(reed_solomon_t *rs, uint8_t *pkt, uint8_t *data, uint8_t *err_pos, uint8_t *num_err) -{ - int err = -1; - -// int deg_lambda = 0; -// int el = 0; -// int deg_omega = 0; -// int u = 0; -// int q = 0; -// int tmp = 0; -// int num1 = 0; -// int num2 = 0; -// int den = 0; -// int discr_r = 0; -// uint8_t lambda[32 + 1] = {0U}; -// uint8_t s[32] = {0U}; -// uint8_t b[32 + 1] = {0U}; -// uint8_t t[32 + 1] = {0U}; -// uint8_t omega[32 + 1] = {0U}; -// uint8_t root[32] = {0U}; -// uint8_t reg[32 + 1] = {0U}; -// uint8_t loc[32] = {0U}; -// int syn_error = 0; -// int count = 0; -// -// int i = 0; -// int j = 0; -// -// /* Form the syndromes; i.e., evaluate data(x) at roots of g(x) */ -// memset(s, data[0], rs->nroots); -// -// for(j = 1; j < (rs->nn - rs->pad); j++) -// { -// for(i = 0; i < rs->nroots; i++) -// { -// if (s[i] == 0) -// { -// s[i] = data[j]; -// } -// else -// { -// s[i] = data[j] ^ rs->alpha_to[modnn(rs, rs->index_of[s[i]] + (rs->fcr + i) * rs->prim)]; -// } -// } -// } -// -// /* Convert syndromes to index form, checking for nonzero condition */ -// syn_error = 0; -// for(i = 0; i < rs->nroots; i++) -// { -// syn_error |= s[i]; -// s[i] = rs->index_of[s[i]]; -// } -// -// if (not syn_error) -// { -// /* if syndrome is zero, data[] is a codeword and there are no errors to correct. So return data[] unmodified */ -// count = 0; -// } -// else -// { -// lambda[0] = 1; -// -// if (no_eras > 0) -// { -// /* Init lambda to be the erasure locator polynomial */ -// lambda[1] = rs->alpha_to[modnn(rs, rs->prim * (rs->nn - 1 - eras_pos[0]))] -// for(i = 1; i < no_eras; i++) -// { -// u = modnn(rs, rs->prim * (rs->nn - 1 - eras_pos[i])); -// for(j = i + 1; i > 0; i--) -// { -// tmp = rs->index_of[lambda[j - 1]]; -// if (tmp != rs->nn) -// { -// lambda[j] ^= rs->alpha_to[modnn(rs, u + tmp)]; -// } -// } -// } -// } -// -// for(i = 0; i < (rs->nroots + 1); i++) -// { -// b[i] = rs->index_of[lambda[i]]; -// } -// -// /* Begin Berlekamp-Massey algorithm to determine error+erasure locator polynomial */ -// r = no_eras + 1; -// el = no_eras; -// while(r <= rs->nroots) /* r is the step number */ -// { -// /* Compute discrepancy at the r-th step in poly-form */ -// discr_r = 0; -// for(i = 0; i < r; i++) -// { -// if ((lambda[i] != 0) && (s[r - i - 1] != rs->nn)) -// { -// discr_r ^= rs->alpha_to[modnn(rs, rs->index_of[lambda[i]] + s[r - i - 1])]; -// } -// } -// discr_r = rs->index_of[discr_r]; /* Index form */ -// if (discr_r == rs->nn) -// { -// /* 2 lines below: B(x) <-- x*B(x) */ -// b.insert(0, rs->nn) -// } -// else -// { -// /* 7 lines below: T(x) <-- lambda(x) - discr_r*x*b(x) */ -// t[0] = lambda[0]; -// for(i = 0; i < rs->nroots; i++) -// { -// if (b[i] != rs->nn) -// { -// t[i + 1] = lambda[i + 1] ^ rs->alpha_to[modnn(rs, discr_r + b[i])]; -// } -// else -// { -// t[i + 1] = lambda[i + 1]; -// } -// } -// if (2 * el <= r + no_eras - 1): -// { -// el = r + no_eras - el; -// /* 2 lines below: B(x) <-- inv(discr_r) * lambda(x) */ -// for(i = 0; i < (rs->nroots + 1); i++) -// { -// if (lambda[i] == 0) -// { -// b[i] = rs->nn; -// } -// else -// { -// b[i] = modnn(rs, rs->index_of[lambda[i]] - discr_r + rs->nn); -// } -// } -// } -// else -// { -// /* 2 lines below: B(x) <-- x*B(x) */ -// b.insert(0, rs->nn) -// } -// lambda = t[:rs->nroots + 1] -// } -// r++; -// } -// -// /* Convert lambda to index form and compute deg(lambda(x)) */ -// deg_lambda = 0; -// for(i = 0; i < (rs->nroots + 1); i++) -// { -// lambda[i] = rs->index_of[lambda[i]]; -// if (lambda[i] != rs->nn) -// { -// deg_lambda = i; -// } -// } -// /* Find roots of the error+erasure locator polynomial by Chien search */ -// reg[1:] = lambda[1:rs->nroots + 1] -// count = 0; /* Number of roots of _lambda(x) */ -// k = rs->iprim - 1; -// for(i = 1; i < (rs->nn + 1); i++) -// { -// q = 1; /* lambda[0] is always 0 */ -// for(j = deg_lambda; j > 0; j--) -// { -// if (reg[j] != rs->nn) -// { -// reg[j] = modnn(rs, reg[j] + j); -// q ^= rs->alpha_to[reg[j]]; -// } -// } -// if (q != 0) -// { -// k = modnn(rs, k + rs->iprim); -// continue; /* Not a root */ -// } -// /* store root (index-form) and error location number */ -// root[count] = i; -// loc[count] = k; -// /* If we've already found max possible roots, abort the search to save time */ -// count++; -// if (count == deg_lambda) -// { -// break; -// } -// k = modnn(rs, k + rs->iprim) -// } -// if (deg_lambda != count) -// { -// /* deg(lambda) unequal to number of roots => uncorrectable error detected */ -// count = -1 -// } -// else -// { -// /* Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo x**rs.nroots). in index form. Also find deg(omega) */ -// deg_omega = deg_lambda - 1; -// for(i = 0; i < (deg_omega + 1); i++) -// { -// tmp = 0; -// for(j = i; j > -1; j--) -// { -// if ((s[i - j] != rs->nn) && (lambda[j] != rs->nn)) -// { -// tmp ^= rs->alpha_to[modnn(rs, s[i - j] + lambda[j])]; -// } -// } -// omega[i] = rs->index_of[tmp]; -// } -// -// /* Compute error values in poly-form. num1 = omega(inv(X(l))), num2 = inv(X(l))**(rs.fcr-1) and den = lambda_pr(inv(X(l))) all in poly-form */ -// for(j = (count - 1); j > -1; j--) -// { -// num1 = 0; -// for(i = deg_omega; j > -1; j--) -// { -// if (omega[i] != rs->nn) -// { -// num1 ^= rs->alpha_to[modnn(rs, omega[i] + i * root[j])]; -// } -// } -// num2 = rs->alpha_to[modnn(rs, root[j] * (rs->fcr - 1) + rs->nn)]; -// den = 0; -// -// /* lambda[i+1] for i even is the formal derivative lambda_pr of lambda[i] */ -// for i in range(min(deg_lambda, rs->nroots - 1) & ~1, -1, -2): -// { -// if (lambda[i + 1] != rs->nn) -// { -// den ^= rs->alpha_to[modnn(lambda[i + 1] + i * root[j])]; -// } -// } -// /* Apply error to data */ -// if (num1 != 0 && loc[j] >= rs->pad): -// { -// data[loc[j] - rs->pad] = data[loc[j] - rs->pad] ^ rs->alpha_to[modnn(rs, rs->index_of[num1] + rs->index_of[num2] + rs->nn - rs->index_of[den])]; -// } -// } -// } -// } -// -// if (eras_pos != None) -// { -// for(i = 0; i < count; i++) -// { -// eras_pos[i] = loc[i]; -// } -// } -// -// err = count; -// -// return data, retval, eras_pos -// return err; -} - -int modnn(reed_solomon_t *rs, int x) -{ - while(x >= rs->nn) - { - x -= rs->nn; - x = (x >> rs->mm) + (x & rs->nn); - } - - return x; -} - -/**< \} End of rsclib group */ diff --git a/firmware/app/libs/rsclib-0.1/.github/workflows/test.yml b/firmware/app/libs/rsclib-1.0/.github/workflows/test.yml similarity index 100% rename from firmware/app/libs/rsclib-0.1/.github/workflows/test.yml rename to firmware/app/libs/rsclib-1.0/.github/workflows/test.yml diff --git a/firmware/app/libs/rsclib-0.1/.gitignore b/firmware/app/libs/rsclib-1.0/.gitignore similarity index 100% rename from firmware/app/libs/rsclib-0.1/.gitignore rename to firmware/app/libs/rsclib-1.0/.gitignore diff --git a/firmware/app/libs/rsclib-0.1/CMakeLists.txt b/firmware/app/libs/rsclib-1.0/CMakeLists.txt similarity index 100% rename from firmware/app/libs/rsclib-0.1/CMakeLists.txt rename to firmware/app/libs/rsclib-1.0/CMakeLists.txt diff --git a/firmware/app/libs/rsclib-0.1/LICENSE b/firmware/app/libs/rsclib-1.0/LICENSE similarity index 100% rename from firmware/app/libs/rsclib-0.1/LICENSE rename to firmware/app/libs/rsclib-1.0/LICENSE diff --git a/firmware/app/libs/rsclib-0.1/README.md b/firmware/app/libs/rsclib-1.0/README.md similarity index 100% rename from firmware/app/libs/rsclib-0.1/README.md rename to firmware/app/libs/rsclib-1.0/README.md diff --git a/firmware/app/libs/rsclib-0.1/include/rsc/rsc.h b/firmware/app/libs/rsclib-1.0/include/rsc/rsc.h similarity index 77% rename from firmware/app/libs/rsclib-0.1/include/rsc/rsc.h rename to firmware/app/libs/rsclib-1.0/include/rsc/rsc.h index 68edd00d..f9ca882c 100644 --- a/firmware/app/libs/rsclib-0.1/include/rsc/rsc.h +++ b/firmware/app/libs/rsclib-1.0/include/rsc/rsc.h @@ -24,10 +24,11 @@ * \brief Reed-Solomon C library definition. * * \author Gabriel Mariano Marcelino + * \author Miguel Boing + * + * \version 1.0.1 * - * \version 0.1.0 - * - * \date 2022/03/06 + * \date 2024/05/09 * * \defgroup rsclib Reed Solomon C Library * \{ @@ -38,23 +39,23 @@ #include -#define RSC_VERSION "v0.1.0" +#define RSC_VERSION "v1.0.0" /** * \brief Reed-Solomon codec control block. */ typedef struct { - int mm; /**< Bits per symbol */ - int nn; /**< Symbols per block (= (1 << mm) - 1) */ + uint32_t mm; /**< Bits per symbol */ + uint32_t nn; /**< Symbols per block (= (1 << mm) - 1) */ uint8_t alpha_to[256]; /**< log lookup table */ uint8_t index_of[256]; /**< Antilog lookup table */ uint8_t genpoly[64]; /**< Generator polynomial */ - int nroots; /**< Number of generator roots = number of parity symbols */ - int fcr; /**< First consecutive root, index form */ - int prim; /**< Primitive element, index form */ - int iprim; /**< prim-th root of 1, index form */ - int pad; /**< Padding bytes in shortened block */ + uint32_t nroots; /**< Number of generator roots = number of parity symbols */ + uint32_t fcr; /**< First consecutive root, index form */ + uint32_t prim; /**< Primitive element, index form */ + uint32_t iprim; /**< prim-th root of 1, index form */ + uint32_t pad; /**< Padding bytes in shortened block */ } reed_solomon_t; /** @@ -76,7 +77,7 @@ typedef struct * * \return The status/error code. */ -int rsc_init(int symsize, int gfpoly, int fcr, int prim, int nroots, int pad, reed_solomon_t *rs); +int rsc_init(uint32_t symsize, int gfpoly, uint32_t fcr, uint32_t prim, uint32_t nroots, uint32_t pad, reed_solomon_t *rs); /** * \brief Computes the parity data of a given byte sequence. @@ -108,7 +109,7 @@ void rsc_encode(reed_solomon_t *rs, uint8_t *data, uint8_t *parity, uint8_t *par * * \return The status/error code. */ -int rsc_decode(reed_solomon_t *rs, uint8_t *pkt, uint8_t *data, uint8_t *err_pos, uint8_t *num_err); +int rsc_decode(reed_solomon_t *rs, uint8_t *data, int *err_pos, int *num_err); #endif /* RSC_H_ */ diff --git a/firmware/app/libs/rsclib-1.0/src/rsc.c b/firmware/app/libs/rsclib-1.0/src/rsc.c new file mode 100644 index 00000000..585a8758 --- /dev/null +++ b/firmware/app/libs/rsclib-1.0/src/rsc.c @@ -0,0 +1,514 @@ +/* + * rsc.c + * + * Copyright The RSCLib Contributors. + * + * This file is part of RSCLib. + * + * RSCLib is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * RSCLib 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. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with RSCLib. If not, see . + * + */ + +/** + * \brief Reed-Solomon C library implementation. + * + * \author Gabriel Mariano Marcelino + * \author Miguel Boing + * + * \version 1.0.1 + * + * \date 2024/05/09 + * + * \addtogroup rsclib + * \{ + */ + +#include "rsc/rsc.h" +#include + +/** + * \brief Computes the modulo of a given number. + * + * \param[in] rs is the Reed-Solomon scheme used to compute the modulo. + * + * \param[in] x is the integer to compute the modulo. + * + * \return The computed modulo value of the given number. + */ +static int modnn(reed_solomon_t *rs, int num); + +static int min(int a, int b); + +int rsc_init(uint32_t symsize, int gfpoly, uint32_t fcr, uint32_t prim, uint32_t nroots, uint32_t pad, reed_solomon_t *rs) +{ + int err = 0; + + uint32_t i; + uint32_t j; + uint32_t sr; + uint32_t root; + uint32_t iprim; + + /* Check parameter ranges */ + if (symsize <= (8U * sizeof(uint8_t))) + { + if (fcr < (1UL << symsize)) + { + if ((prim > 0U) && ( prim < (1U << symsize))) + { + if (( nroots < (1U << symsize))) + { + if ((pad < ((1U << symsize) - 1U - nroots))) + { + rs->mm = symsize; + rs->nn = (1U << symsize) - 1U; + rs->pad = pad; + + /* Generate Galois field lookup tables */ + rs->index_of[0] = rs->nn; /* log(zero) = -inf */ + rs->alpha_to[rs->nn] = 0; /* alpha**-inf = 0 */ + sr = 1U; + for(i = 0; i < rs->nn; i++) + { + rs->index_of[sr] = i; + rs->alpha_to[i] = sr; + sr <<= 1; + if ((sr & (1U << symsize)) != 0U) + { + sr ^= gfpoly; + } + sr &= rs->nn; + } + if (sr == 1U) + { + rs->fcr = fcr; + rs->prim = prim; + rs->nroots = nroots; + + /* Find prim-th root of 1, used in decoding */ + for(iprim = 1U; (iprim % prim) != 0U; iprim += rs->nn) + { + } + rs->iprim = iprim / prim; + + rs->genpoly[0] = 1U; + + root = fcr * prim; + for(i = 0U; i < nroots; i++) + { + if (i != 0U) + { + root += prim; + } + + rs->genpoly[i + 1U] = 1U; + + /* Multiply rs->genpoly[] by @**(root + x) */ + for(j = i; j > 0U; j--) + { + if (rs->genpoly[j] != 0U) + { + rs->genpoly[j] = rs->genpoly[j - 1U] ^ rs->alpha_to[modnn(rs, rs->index_of[rs->genpoly[j]] + (uint32_t)root)]; + } + else + { + rs->genpoly[j] = rs->genpoly[j - 1U]; + } + } + /* rs->genpoly[0] can never be zero */ + rs->genpoly[0] = rs->alpha_to[modnn(rs, rs->index_of[rs->genpoly[0]] + (uint32_t)root)]; + } + /* convert rs->genpoly[] to index form for quicker encoding */ + for(i = 0U; i <= nroots; i++) + { + rs->genpoly[i] = rs->index_of[rs->genpoly[i]]; + } + + err = 0; + } + else + { + /* field generator polynomial is not primitive! */ + (void)memset(rs, 0, sizeof(reed_solomon_t)); + + err = -1; + } + } + else + { + err = 0; /* Too much padding */ + } + } + else + { + err = -1; /* Can't have more roots than symbol values! */ + } + } + else + { + err = -1; + } + } + else + { + err = -1; + } + } + else + { + err = -1; + } + + return err; +} + +void rsc_encode(reed_solomon_t *rs, uint8_t *data, uint8_t *parity, uint8_t *parity_len) +{ + uint32_t i = 0; + uint32_t j = 0; + uint32_t feedback = 0; + + (void)memset(parity, 0, rs->nroots); + + for(i = 0U; i < (rs->nn - rs->nroots - rs->pad); i++) + { + feedback = rs->index_of[data[i] ^ parity[0]]; + if (feedback != rs->nn) /* feedback term is non-zero */ + { + for(j = 0U; j < rs->nroots; j++) + { + parity[j] ^= rs->alpha_to[modnn(rs, feedback + rs->genpoly[rs->nroots - j])]; + } + } + /* Shift */ + (void)memmove(&parity[0], &parity[1], rs->nroots - 1U); + if (feedback != rs->nn) + { + parity[rs->nroots - 1U] = rs->alpha_to[modnn(rs, feedback + rs->genpoly[0])]; + } + else + { + parity[rs->nroots - 1U] = 0U; + } + } + + *parity_len = rs->nroots; +} + +int rsc_decode(reed_solomon_t *rs, uint8_t *data, int *err_pos, int *num_err) +{ + int err = -1; + + int deg_lambda = 0; + int el = 0; + int deg_omega = 0; + + int r = 0; + int i = 0; + int j = 0; + int k = 0; + + uint8_t u = 0; + uint8_t q = 0; + uint8_t tmp = 0; + uint8_t num1 = 0; + uint8_t num2 = 0; + uint8_t den = 0; + uint8_t discr_r = 0; + uint8_t lambda[33] = {0}; + uint8_t s[32] = {0}; + uint8_t b[33] = {0}; + uint8_t t[33] = {0}; + uint8_t omega[33] = {0}; + uint8_t root[32] = {0}; + uint8_t reg[33] = {0}; + uint8_t loc[32] = {0}; + + int syn_error = 0; + int count = 0; + + /* Form the syndromes; i.e., evaluate data(x) at roots of g(x) */ + for (i=0;i<(int)rs->nroots;i++) + { + s[i] = data[0]; + } + + for(j = 1; j < ((int)(rs->nn) - (int)(rs->pad)); j++) + { + for(i = 0; i < (int)rs->nroots; i++) + { + if (s[i] == 0U) + { + s[i] = data[j]; + } + else + { + s[i] = data[j] ^ rs->alpha_to[modnn(rs, ((int)(rs->index_of[s[i]]) + ((int)(rs->fcr) + i) * (int)(rs->prim)))]; + } + } + } + + for(i = 0; i < (int)rs->nroots; i++) + { + syn_error |= s[i]; + s[i] = rs->index_of[s[i]]; + + } + + + if (syn_error == 0) + { + /* if syndrome is zero, data[] is a codeword and there are no errors + to correct. So return data[] unmodified */ + count = 0; + err = 0; + } + else + { + (void)memset(&lambda[1],0,rs->nroots*sizeof(lambda[0])); + lambda[0] = 1U; + + if (*num_err > 0) + { + /* Init lambda to be the erasure locator polynomial */ + lambda[1] = rs->alpha_to[modnn(rs, rs->prim * (rs->nn - 1U - (uint32_t)(err_pos[0])))]; + for(i = 1U; i < *num_err; i++) + { + u = modnn(rs, rs->prim * (rs->nn - 1U - (uint32_t)(err_pos[i]))); + for(j = i + 1; j > 0; j--) + { + tmp = rs->index_of[lambda[j - 1]]; + if (tmp != rs->nn) + { + lambda[j] ^= rs->alpha_to[modnn(rs, u + tmp)]; + } + } + } + } + + for(i = 0; i < ((int)(rs->nroots) + 1); i++) + { + b[i] = rs->index_of[lambda[i]]; + } + + /* Begin Berlekamp-Massey algorithm to determine error+erasure locator polynomial */ + r = *num_err; + el = *num_err; + while(++r <= rs->nroots) /* r is the step number */ + { + /* Compute discrepancy at the r-th step in poly-form */ + discr_r = 0; + for(i = 0U; i < r; i++) + { + if ((lambda[i] != 0U) && ((uint32_t)(s[r - i - 1]) != rs->nn)) + { + discr_r ^= rs->alpha_to[modnn(rs, rs->index_of[lambda[i]] + s[r - i - 1])]; + } + } + discr_r = rs->index_of[discr_r]; /* Index form */ + if ((uint32_t)discr_r == rs->nn) + { + /* 2 lines below: B(x) <-- x*B(x) */ + (void)memmove(&b[1],b,rs->nroots*sizeof(b[0])); + b[0] = (uint8_t) rs->nn; + } + else + { + /* 7 lines below: T(x) <-- lambda(x) - discr_r*x*b(x) */ + t[0] = lambda[0]; + for(i = 0U; i < (int) rs->nroots; i++) + { + if (((uint32_t) b[i]) != rs->nn) + { + t[i + 1] = lambda[i + 1] ^ rs->alpha_to[modnn(rs, discr_r + b[i])]; + } + else + { + t[i + 1] = lambda[i + 1]; + } + } + if ((2 * el) <= (r + *num_err - 1)) + { + el = r + *num_err - el; + /* 2 lines below: B(x) <-- inv(discr_r) * lambda(x) */ + for(i = 0; i <= (int)rs->nroots; i++) + { + if (lambda[i] == 0U) + { + b[i] = (uint8_t)rs->nn; + } + else + { + b[i] = modnn(rs, rs->index_of[lambda[i]] - discr_r + rs->nn); + } + } + } + else + { + /* 2 lines below: B(x) <-- x*B(x) */ + (void)memmove(&b[1],b,(rs->nroots)*sizeof(b[0])); + b[0] = 255U; + } + + (void)memcpy(lambda,t,(rs->nroots)*sizeof(t[0])); + } + } + + /* Convert lambda to index form and compute deg(lambda(x)) */ + deg_lambda = 0; + + for(i = 0; i < ((int)(rs->nroots) + 1); i++) + { + lambda[i] = rs->index_of[lambda[i]]; + if (lambda[i] != rs->nn) + { + deg_lambda = i; + } + } + /* Find roots of the error+erasure locator polynomial by Chien search */ + (void)memcpy(®[1],&lambda[1],rs->nroots*sizeof(reg[0])); + count = 0; /* Number of roots of lambda(x) */ + k =(int)(rs->iprim) - 1; + for(i = 1; i <= (int)(rs->nn); i++) + { + if (i != 1) + { + k = modnn(rs, k + (int)(rs->iprim)); + } + + q = 1; /* lambda[0] is always 0 */ + for(j = deg_lambda; j > 0; j--) + { + if (reg[j] != rs->nn) + { + reg[j] = modnn(rs, reg[j] + (uint8_t)j); + q ^= rs->alpha_to[reg[j]]; + } + } + + if (q != 0U) + { + continue; /* Not a root */ + } + /* store root (index-form) and error location number */ + root[count] = i; + loc[count] = k; + /* If we've already found max possible roots, abort the search to save time */ + if (++count == deg_lambda) + { + break; + } + + } + if (deg_lambda != count) + { + /* deg(lambda) unequal to number of roots => uncorrectable error detected */ + count = -1; + err = -1; + } + else + { + /* Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo x**rs.nroots). in index form. Also find deg(omega) */ + deg_omega = deg_lambda - 1; + for(i = 0; i <= deg_omega; i++) + { + tmp = 0; + for(j = i; j >= 0; j--) + { + if ((s[i - j] != rs->nn) && (lambda[j] != rs->nn)) + { + tmp ^= rs->alpha_to[modnn(rs, s[i - j] + lambda[j])]; + } + } + omega[i] = rs->index_of[tmp]; + } + + /* Compute error values in poly-form. num1 = omega(inv(X(l))), num2 = inv(X(l))**(rs.fcr-1) and den = lambda_pr(inv(X(l))) all in poly-form */ + for(j = (count - 1); j >= 0; j--) + { + num1 = 0; + for(i = deg_omega; i >= 0; i--) + { + if (omega[i] != rs->nn) + { + num1 ^= rs->alpha_to[modnn(rs, (int)omega[i] + (i * (int)root[j]))]; + } + } + num2 = rs->alpha_to[modnn(rs, (int)(root[j]) * ((int)(rs->fcr) - 1) + (int)(rs->nn))]; + den = 0; + + /* lambda[i+1] for i even is the formal derivative lambda_pr of lambda[i] */ + for (i = min(deg_lambda, ((int)(rs->nroots)-1)) & ~1; i>=0; i -= 2) + { + if (lambda[i + 1] != rs->nn) + { + den ^= rs->alpha_to[modnn(rs, (int)lambda[i + 1] + (i * (int)root[j]))]; + } + } + /* Apply error to data */ + if ((num1 != 0U) && (loc[j] >= rs->pad)) + { + data[loc[j] - rs->pad] ^= rs->alpha_to[modnn(rs, rs->index_of[num1] + rs->index_of[num2] + rs->nn - rs->index_of[den])]; + } + } + err = 0; + } + } + + if (err_pos != NULL) + { + for(i = 0; i < count; i++) + { + err_pos[i] = loc[i]; + } + } + + if (num_err != NULL) + { + *num_err = count; + } + + return err; +} + +static int modnn(reed_solomon_t *rs, int num) +{ + uint32_t x; + x = (uint32_t)num; + + while(x >= rs->nn) + { + x -= rs->nn; + x = (x >> rs->mm) + (x & rs->nn); + } + + return (int)x; +} + +static int min(int a, int b) +{ + int min; + if (a <= b) + { + min = a; + } + else + { + min = b; + } + + return min; +} + +/**< \} End of rsclib group */ diff --git a/firmware/app/libs/rsclib-0.1/tests/.gitignore b/firmware/app/libs/rsclib-1.0/tests/.gitignore similarity index 100% rename from firmware/app/libs/rsclib-0.1/tests/.gitignore rename to firmware/app/libs/rsclib-1.0/tests/.gitignore diff --git a/firmware/app/libs/rsclib-0.1/tests/CMakeLists.txt b/firmware/app/libs/rsclib-1.0/tests/CMakeLists.txt similarity index 100% rename from firmware/app/libs/rsclib-0.1/tests/CMakeLists.txt rename to firmware/app/libs/rsclib-1.0/tests/CMakeLists.txt diff --git a/firmware/app/libs/rsclib-0.1/tests/README.md b/firmware/app/libs/rsclib-1.0/tests/README.md similarity index 100% rename from firmware/app/libs/rsclib-0.1/tests/README.md rename to firmware/app/libs/rsclib-1.0/tests/README.md diff --git a/firmware/app/libs/rsclib-0.1/tests/test.c b/firmware/app/libs/rsclib-1.0/tests/test.c similarity index 100% rename from firmware/app/libs/rsclib-0.1/tests/test.c rename to firmware/app/libs/rsclib-1.0/tests/test.c From 0378667aa92f60167389e5ccf5ad48bb795f4ae9 Mon Sep 17 00:00:00 2001 From: Miguel Boing Date: Mon, 3 Jun 2024 20:03:37 -0300 Subject: [PATCH 043/103] firmware: app: libs: Updating ngham library with decoder implementation #136 --- .../ngham-0.1/include/ngham/ngham_extension.h | 92 ----- .../ngham-0.1/include/ngham/ngham_packets.h | 69 ---- firmware/app/libs/ngham-0.1/src/ngham.c | 347 ---------------- .../app/libs/ngham-0.1/src/ngham_extension.c | 175 -------- .../app/libs/ngham-0.1/src/ngham_packets.c | 16 - .../.github/workflows/test.yml | 0 .../libs/{ngham-0.1 => ngham-1.0}/.gitignore | 0 .../{ngham-0.1 => ngham-1.0}/CMakeLists.txt | 0 .../app/libs/{ngham-0.1 => ngham-1.0}/LICENSE | 0 .../libs/{ngham-0.1 => ngham-1.0}/README.md | 0 .../include/ngham/ccsds_scrambler.h | 2 +- .../include/ngham/config.h | 0 .../include/ngham/crc_ccitt.h | 4 +- .../include/ngham/ngham.h | 25 +- .../ngham-1.0/include/ngham/ngham_packets.h | 57 +++ .../include/ngham/ngham_spp.h | 12 +- .../src/ccsds_scrambler.c | 4 +- .../{ngham-0.1 => ngham-1.0}/src/crc_ccitt.c | 0 firmware/app/libs/ngham-1.0/src/ngham.c | 388 ++++++++++++++++++ .../app/libs/ngham-1.0/src/ngham_packets.c | 16 + .../{ngham-0.1 => ngham-1.0}/src/ngham_spp.c | 0 .../tests/CMakeLists.txt | 0 .../{ngham-0.1 => ngham-1.0}/tests/Makefile | 0 .../{ngham-0.1 => ngham-1.0}/tests/README.md | 0 .../{ngham-0.1 => ngham-1.0}/tests/test.c | 0 25 files changed, 489 insertions(+), 718 deletions(-) delete mode 100644 firmware/app/libs/ngham-0.1/include/ngham/ngham_extension.h delete mode 100644 firmware/app/libs/ngham-0.1/include/ngham/ngham_packets.h delete mode 100755 firmware/app/libs/ngham-0.1/src/ngham.c delete mode 100644 firmware/app/libs/ngham-0.1/src/ngham_extension.c delete mode 100644 firmware/app/libs/ngham-0.1/src/ngham_packets.c rename firmware/app/libs/{ngham-0.1 => ngham-1.0}/.github/workflows/test.yml (100%) rename firmware/app/libs/{ngham-0.1 => ngham-1.0}/.gitignore (100%) rename firmware/app/libs/{ngham-0.1 => ngham-1.0}/CMakeLists.txt (100%) rename firmware/app/libs/{ngham-0.1 => ngham-1.0}/LICENSE (100%) rename firmware/app/libs/{ngham-0.1 => ngham-1.0}/README.md (100%) rename firmware/app/libs/{ngham-0.1 => ngham-1.0}/include/ngham/ccsds_scrambler.h (95%) rename firmware/app/libs/{ngham-0.1 => ngham-1.0}/include/ngham/config.h (100%) rename firmware/app/libs/{ngham-0.1 => ngham-1.0}/include/ngham/crc_ccitt.h (94%) rename firmware/app/libs/{ngham-0.1 => ngham-1.0}/include/ngham/ngham.h (76%) create mode 100644 firmware/app/libs/ngham-1.0/include/ngham/ngham_packets.h rename firmware/app/libs/{ngham-0.1 => ngham-1.0}/include/ngham/ngham_spp.h (69%) rename firmware/app/libs/{ngham-0.1 => ngham-1.0}/src/ccsds_scrambler.c (96%) rename firmware/app/libs/{ngham-0.1 => ngham-1.0}/src/crc_ccitt.c (100%) create mode 100755 firmware/app/libs/ngham-1.0/src/ngham.c create mode 100644 firmware/app/libs/ngham-1.0/src/ngham_packets.c rename firmware/app/libs/{ngham-0.1 => ngham-1.0}/src/ngham_spp.c (100%) rename firmware/app/libs/{ngham-0.1 => ngham-1.0}/tests/CMakeLists.txt (100%) rename firmware/app/libs/{ngham-0.1 => ngham-1.0}/tests/Makefile (100%) rename firmware/app/libs/{ngham-0.1 => ngham-1.0}/tests/README.md (100%) rename firmware/app/libs/{ngham-0.1 => ngham-1.0}/tests/test.c (100%) diff --git a/firmware/app/libs/ngham-0.1/include/ngham/ngham_extension.h b/firmware/app/libs/ngham-0.1/include/ngham/ngham_extension.h deleted file mode 100644 index 943c20eb..00000000 --- a/firmware/app/libs/ngham-0.1/include/ngham/ngham_extension.h +++ /dev/null @@ -1,92 +0,0 @@ -//**************************************************************// -// NGHam protocol - Jon Petter Skagmo, LA3JPA, 2014. // -// Licensed under LGPL. // -//**************************************************************// - -#ifndef NGHAM_EXTENSIONS_H -#define NGHAM_EXTENSIONS_H - -#include "stdint.h" -#include "ngham_packets.h" - -// Possible values for the type field. After type byte, length follows. -#define PKT_TYPE_DATA 0 -#define PKT_TYPE_ID 1 -#define PKT_TYPE_STAT 2 -#define PKT_TYPE_SIMPLEDIGI 3 -#define PKT_TYPE_POS 4 -#define PKT_TYPE_TOH 5 -#define PKT_TYPE_DEST 6 // Destination/receiver callsign -#define PKT_TYPE_CMD_REQ 7 // Command packet -#define PKT_TYPE_CMD_REPLY 8 // Command packet -#define PKT_TYPE_REQUEST 9 - -#define PKT_TYPES 10 -#define PKT_SIZE_VARIABLE 0xffff - -extern const char* PKT_TYPE_STRINGS[]; -extern const uint16_t PKT_TYPE_SIZES[]; - -// Additional NA-values -#define TEMP_NA 0xff -#define VOLT_NA 0xff -#define UINT8_NA 0xff -#define INT32_NA 0x7fffffff -#define COG_NA 0x7ff - -// Ensure compatibility when used with MinGW etc. -#ifdef _WIN32 -#define ATTRIBUTE_PACKED __attribute__ ((packed,gcc_struct)) -#else -#define ATTRIBUTE_PACKED __attribute__ ((packed)) -#endif - -// TOH packet -typedef struct ATTRIBUTE_PACKED{ - uint32_t toh_us; // Time of hour in microseconds - uint8_t toh_val; // Validity -}ngham_toh_t; - -// Statistics packet -typedef struct ATTRIBUTE_PACKED{ - uint16_t hw_ver; // 10b company, 6b product - uint16_t serial; // Serial nr. - uint16_t sw_ver; // 4b major, 4b minor, 8b build - uint32_t uptime_s; // Time in whole seconds since startup - uint8_t voltage; // Input voltage in desivolts (0-25.5) - int8_t temp; // System temp in deg. celsius (-128 to 127) - uint8_t signal; // Received signal strength in dBm - 200, -200 to 54 (0xff=N/A) - uint8_t noise; // Noise floor, same as above - uint16_t cntr_rx_ok; // Packets successfully received - uint16_t cntr_rx_fix; // Packets with corrected errors - uint16_t cntr_rx_err; // Packets with uncorrectable errors - uint16_t cntr_tx; // Packets sent -}ngham_stat_t; - -// Position packet -typedef struct ATTRIBUTE_PACKED{ - int32_t latitude; // In degrees * 10^7 - int32_t longitude; // In degrees * 10^7 - int32_t altitude; // In centimeters - unsigned int sog:20; // Hundreds of meters per second - unsigned int cog:12; // Tenths of degrees - uint8_t hdop; // In tenths -}ngham_pos_t; - -// Always first in a packet, except when resent by another station. -typedef struct ATTRIBUTE_PACKED{ - uint8_t call_ssid[6]; // 7 x 6 bit (SIXBIT DEC, which is ASCII-32 and limited to 0-64) empty characters padded with 0, 6 bit SSID - uint8_t sequence; // Wraps around from 255 to 0 -}ngham_id_t; - -typedef struct ATTRIBUTE_PACKED{ - uint8_t call_ssid[6]; // 7 x 6 bit (SIXBIT DEC, which is ASCII-32 and limited to 0-64) empty characters padded with 0, 6 bit SSID -}ngham_dest_t; - -uint8_t* ngh_ext_allocate_pkt(tx_pkt_t* p, uint8_t pkt_type, uint16_t data_len); -void ngh_ext_append_pkt(tx_pkt_t* p, uint8_t type, uint8_t* data, uint16_t size); -uint16_t ngh_ext_numpkts(uint8_t* d, uint16_t d_len); -uint8_t ngh_ext_encode_callsign(uint8_t* enc_callsign, char* callsign); -void ngh_ext_decode_callsign(char* callsign, uint8_t* enc_callsign); - -#endif \ No newline at end of file diff --git a/firmware/app/libs/ngham-0.1/include/ngham/ngham_packets.h b/firmware/app/libs/ngham-0.1/include/ngham/ngham_packets.h deleted file mode 100644 index fb1401ec..00000000 --- a/firmware/app/libs/ngham-0.1/include/ngham/ngham_packets.h +++ /dev/null @@ -1,69 +0,0 @@ -//**************************************************************// -// NGHam protocol - Jon Petter Skagmo, LA3JPA, 2014. // -// Licensed under LGPL. // -//**************************************************************// - -#ifndef NGHAM_PACKETS_H -#define NGHAM_PACKETS_H - -#include "stdint.h" - -// Possible packet conditions -#define PKT_CONDITION_OK 0 // Successfully received packet -#define PKT_CONDITION_FAIL 1 // Receiption failed after receiving the packet -#define PKT_CONDITION_PREFAIL 2 // Reception failed before receiving the whole packet - -// Packet priority in transmission -#define PKT_PRIORITY_NORMAL 0 -#define PKT_PRIORITY_FIRST_IN_SLOT 10 // Should be put first in the next time slot for timing purposes - -// Size of payload in packet struct -#define PKT_PL_SIZE 512 - -// Basic "not available"-values -#define RSSI_NA 0xff -#define TIMESTAMP_NA 0xffffffff - -// If the following flag is set in a packet, NGHam extensions are used and first byte is type -#define NGHAM_FLAG_TYPE_EXTENSION 0x01 - -#define SPP_PL_MAX 255 // Not a power of two, so no ring buffers! - -#define NGHAM_PL_MAX 220 - -// Ensure compatibility when used with MinGW etc. -#ifdef _WIN32 -#define ATTRIBUTE_PACKED __attribute__ ((packed,gcc_struct)) -#else -#define ATTRIBUTE_PACKED __attribute__ ((packed)) -#endif - -typedef struct ATTRIBUTE_PACKED{ - uint32_t timestamp_toh_us; // Time stamp of sync word detection - uint8_t noise; // Same as above - uint8_t rssi; // In dBm + 200 - uint8_t errors; // Recovered symbols - uint8_t ngham_flags; - uint8_t pl[PKT_PL_SIZE]; - uint16_t pl_len; -}rx_pkt_t; - -typedef struct ATTRIBUTE_PACKED{ - uint8_t priority; - uint8_t ngham_flags; - uint8_t pl[PKT_PL_SIZE]; - uint16_t pl_len; -}tx_pkt_t; - -// NGHam SPP header -typedef struct ATTRIBUTE_PACKED{ - uint8_t start; - uint16_t crc; - uint8_t pl_type; - uint8_t pl_len; -}ngh_spphdr_t; - -void rx_pkt_init(rx_pkt_t *p); -void tx_pkt_init(tx_pkt_t *p); - -#endif diff --git a/firmware/app/libs/ngham-0.1/src/ngham.c b/firmware/app/libs/ngham-0.1/src/ngham.c deleted file mode 100755 index 9ad8eea1..00000000 --- a/firmware/app/libs/ngham-0.1/src/ngham.c +++ /dev/null @@ -1,347 +0,0 @@ -/* - * ngham.c - * - * Copyright The NGHam Contributors. - * - * This file is part of NGHam. - * - * NGHam is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * NGHam 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. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with NGHam. If not, see . - * - */ - -/** - * \brief NGHam library implementation. - * - * \author Gabriel Mariano Marcelino - * - * \version 0.1.0 - * - * \date 2023/03/12 - * - * \addtogroup ngham - * \{ - */ - -#include /* For NULL etc. */ -#include /* For memcpy */ -#include /* For free */ - -#include - -#include -#include - -#include /* Pre-generated array from scrambling polynomial */ -#include - -/* There are seven different sizes. */ -/* Each size has a correlation tag for size, a total size, a maximum payload size and a parity data size. */ -#define NGH_SIZES 7 - -/* Decoder states */ -#define NGH_STATE_SIZE_TAG 0 -#define NGH_STATE_SIZE_TAG_2 1 -#define NGH_STATE_SIZE_TAG_3 2 -#define NGH_STATE_SIZE_KNOWN 3 -#define NGH_STATE_STATUS 4 -#define NGH_STATE_STATUS_2 5 - -/* Maximum number of errors in the size tag */ -#define NGH_SIZE_TAG_MAX_ERROR 6 - -uint8_t NGH_PL_SIZE[7] = {28U, 60U, 92U, 124U, 156U, 188U, 220U}; /* Actual payload */ -uint8_t NGH_PL_SIZE_FULL[7] = {31U, 63U, 95U, 127U, 159U, 191U, 223U}; /* Size with LEN, payload and CRC */ -uint8_t NGH_PL_PAR_SIZE[7] = {47U, 79U, 111U, 159U, 191U, 223U, 255U}; /* Size with RS parity added */ -uint8_t NGH_PAR_SIZE[7] = {16U, 16U, 16U, 32U, 32U, 32U, 32U}; - -/* The seven different size tag vectors */ -uint32_t NGH_SIZE_TAG[7] = { - 0b001110110100100111001101, - 0b010011011101101001010111, - 0b011101101001001110011010, - 0b100110111011010010101110, - 0b101000001111110101100011, - 0b110101100110111011111001, - 0b111011010010011100110100 -}; - -/* Preamble and synchronization vector */ -uint8_t NGH_PREAMBLE = 0xAAU; -uint8_t NGH_PREAMBLE_FOUR_LEVEL = 0xDDU; -uint8_t NGH_SYNC[4] = {0x5DU, 0xE6U, 0x2AU, 0x7EU}; -uint8_t NGH_SYNC_FOUR_LEVEL[8] = {0x77U, 0xF7U, 0xFDU, 0x7DU, 0x5DU, 0xDDU, 0x7FU, 0xFDU}; - -/* Reed Solomon control blocks for the different NGHAM sizes */ -reed_solomon_t rs[NGH_SIZES] = {0}; - -int decoder_state = NGH_STATE_SIZE_TAG; - -/** - * \brief Generates the Reed-Solomon tables for all packets sizes. - * - * Run only once - generates Reed Solomon tables for all 7 packet sizes. - * - * MM=8, genpoly=0x187, fcs=112, prim=11, nroots=32 or 16 - * - * \return The status/error code. - */ -int ngham_init_arrays(void); - -/** - * \brief Used to check if hamming distance in size tag is smaller than treshold. - * - * \param[in] x . - * - * \param[in] y . - * - * \return . - */ -uint8_t ngham_tag_check(uint32_t x, uint32_t y); - -int ngham_init(void) -{ - decoder_state = NGH_STATE_SIZE_TAG; - - return ngham_init_arrays(); -} - -int ngham_encode(uint8_t *data, uint16_t len, uint8_t flags, uint8_t *pkt, uint16_t *pkt_len) -{ - uint16_t j = 0U; - uint16_t crc = 0U; - uint8_t size_nr = 0U; - uint16_t d_len = 0U; - uint8_t codeword_start = 0U; - - /* Check size and find control block for smallest possible RS codeword */ - if ((len == 0) || (len > NGH_PL_SIZE[NGH_SIZES - 1])) - { - return -1; - } - - while(len > NGH_PL_SIZE[size_nr]) - { - size_nr++; - } - - /* Insert preamble, sync and size-tag */ - if (NGHAM_FOUR_LEVEL_MODULATION) - { - codeword_start = NGH_PREAMBLE_SIZE_FOUR_LEVEL + NGH_SYNC_SIZE_FOUR_LEVEL + NGH_SIZE_TAG_SIZE; - - for(j = 0; j < NGH_PREAMBLE_SIZE_FOUR_LEVEL; j++) - { - pkt[d_len++] = NGH_PREAMBLE_FOUR_LEVEL; - } - - for(j = 0; j < NGH_SYNC_SIZE_FOUR_LEVEL; j++) - { - pkt[d_len++] = NGH_SYNC_FOUR_LEVEL[j]; - } - } - else - { - codeword_start = NGH_PREAMBLE_SIZE + NGH_SYNC_SIZE + NGH_SIZE_TAG_SIZE; - - for(j = 0U; j < NGH_PREAMBLE_SIZE; j++) - { - pkt[d_len++] = NGH_PREAMBLE; - } - - for(j = 0U; j < NGH_SYNC_SIZE; j++) - { - pkt[d_len++] = NGH_SYNC[j]; - } - } - - pkt[d_len++] = (NGH_SIZE_TAG[size_nr] >> 16) & 0xFFU; - pkt[d_len++] = (NGH_SIZE_TAG[size_nr] >> 8) & 0xFFU; - pkt[d_len++] = NGH_SIZE_TAG[size_nr] & 0xFFU; - - /* Prepare content of codeword */ - pkt[d_len] = (NGH_PL_SIZE[size_nr] - len) & 0x1FU; /* Insert padding size */ - pkt[d_len] |= (flags << 5) & 0xE0U; /* Insert flags */ - d_len++; - for(j = 0; j < len; j++) - { - pkt[d_len++] = data[j]; /* Insert data */ - } - crc = crc_ccitt(&pkt[codeword_start], len + 1); /* Insert CRC */ - pkt[d_len++] = (crc >> 8) & 0xFFU; - pkt[d_len++] = crc & 0xFFU; - for(j = len + 3; j < NGH_PL_SIZE_FULL[size_nr]; j++) - { - pkt[d_len++] = 0; /* Insert padding */ - } - - /* Generate parity data */ - uint8_t par_len = UINT8_MAX; - rsc_encode(&rs[size_nr], &pkt[codeword_start], &pkt[d_len], &par_len); - d_len += NGH_PAR_SIZE[size_nr]; - - /* Scramble */ - for(j = 0; j < NGH_PL_PAR_SIZE[size_nr]; j++) - { - pkt[codeword_start + j] ^= ccsds_poly[j]; - } - - *pkt_len = d_len; - - return 0; -} - -void ngham_decode(uint8_t d) -{ -// static uint8_t size_nr; -// static uint32_t size_tag; -// static unsigned int length; -// /* This points to the address one lower than the payload! */ -// static uint8_t* buf = (uint8_t*)&rx_pkt.ngham_flags; -// -// switch(decoder_state) -// { -// case NGH_STATE_SIZE_TAG: -// size_tag = 0; -// ngham_action_reception_started(); -// case NGH_STATE_SIZE_TAG_2: -// size_tag <<= 8; -// size_tag |= d; -// decoder_state++; -// break; -// case NGH_STATE_SIZE_TAG_3: -// size_tag <<= 8; -// size_tag |= d; -// { -// for (size_nr = 0; size_nr < NGH_SIZES; size_nr++) -// { -// /* If tag is intact, set known size */ -// if (ngham_tag_check(size_tag, NGH_SIZE_TAG[size_nr])) -// { -// decoder_state = NGH_STATE_SIZE_KNOWN; -// length = 0; -// -// /* Set new packet size as soon as possible */ -//// ngham_action_set_packet_size(NGH_PL_PAR_SIZE[size_nr] + NGH_SIZE_TAG_SIZE); -// break; -// } -// } -// /* If size tag is not found, every size can theoretically be attempted */ -// if (decoder_state != NGH_STATE_SIZE_KNOWN) -// { -//// ngham_action_handle_packet(PKT_CONDITION_PREFAIL, NULL); -// decoder_state = NGH_STATE_SIZE_TAG; -// } -// } -// break; -// case NGH_STATE_SIZE_KNOWN: -// /* De-scramble byte and append to buffer */ -// buf[length] = d^ccsds_poly[length]; -// length++; -// -// /* Do whatever is necessary in this action */ -// if (length == NGHAM_BYTES_TILL_ACTION_HALFWAY) -// { -// ngham_action_reception_halfway(); -// } -// -// if (length == NGH_PL_PAR_SIZE[size_nr]) -// { -// int8_t errors; -// -// /* Set packet size back to a large value */ -//// ngham_action_set_packet_size(255); -// decoder_state = NGH_STATE_SIZE_TAG; -// -// /* Run Reed Solomon decoding, calculate packet length */ -// errors = decode_rs_char(&rs_cb[size_nr], buf, 0, 0); -// rx_pkt.pl_len = NGH_PL_SIZE[size_nr] - (buf[0] & NGH_PADDING_bm); -// -// /* Check if the packet is decodeable and then if CRC is OK */ -// if ((errors != -1) && -// (crc_ccitt(buf, rx_pkt.pl_len + 1) == ((buf[rx_pkt.pl_len + 1] << 8) | buf[rx_pkt.pl_len + 2])) ) -// { -// /* Copy remaining fields and pass on */ -// rx_pkt.errors = errors; -// rx_pkt.ngham_flags = (buf[0] & NGH_FLAGS_bm) >> NGH_FLAGS_bp; -// rx_pkt.noise = ngham_action_get_noise_floor(); -// rx_pkt.rssi = ngham_action_get_rssi(); -//// ngham_action_handle_packet(PKT_CONDITION_OK, &rx_pkt); -// } -// /* If packet decoding not was successful, count this as an error */ -// else -// { -//// ngham_action_handle_packet(PKT_CONDITION_FAIL, NULL); -// } -// } -// break; -// } -} - -int ngham_init_arrays(void) -{ - int err = 0; - int j = 0; - - for(j = 0; j < 3; j++) - { - if (rsc_init(8, 0x187, 112, 11, 16, (NGH_PL_PAR_SIZE[6] - NGH_PL_PAR_SIZE[j]), &rs[j]) != 0) - { - err = -1; - } - } - - for(; j < NGH_SIZES; j++) - { - if (rsc_init(8, 0x187, 112, 11, 32, (NGH_PL_PAR_SIZE[6] - NGH_PL_PAR_SIZE[j]), &rs[j]) != 0) - { - err = -1; - } - } - - return err; -} - -uint8_t ngham_tag_check(uint32_t x, uint32_t y) -{ - uint8_t j = 0U; - uint8_t distance = 0U; - uint32_t diff = x ^ y; - uint8_t res = 0; - - if (diff != 0U) - { - for(j = 0U; j < 24U; j++) - { - if (diff & 0x01U) - { - distance++; - if (distance > NGH_SIZE_TAG_MAX_ERROR) - { - res = 0; - break; - } - } - - diff >>= 1; - } - } - else - { - res = 1; - } - - return res; -} - -/**< \} End of ngham group */ diff --git a/firmware/app/libs/ngham-0.1/src/ngham_extension.c b/firmware/app/libs/ngham-0.1/src/ngham_extension.c deleted file mode 100644 index d4a6336b..00000000 --- a/firmware/app/libs/ngham-0.1/src/ngham_extension.c +++ /dev/null @@ -1,175 +0,0 @@ -//**************************************************************// -// NGHam protocol - Jon Petter Skagmo, LA3JPA, 2014. // -// Licensed under LGPL. // -//**************************************************************// - -#include "ngham_extension.h" - -#include "ngham_packets.h" -#include "stdint.h" -#include -#include - -const char* PKT_TYPE_STRINGS[] = { - "data", - "id", - "stat", - "sdigi", - "pos", - "toh", - "dest", - "cmd_req", - "cmd_reply", - "request" -}; - -const uint16_t PKT_TYPE_SIZES[] = { - PKT_SIZE_VARIABLE, - sizeof(ngham_id_t), - sizeof(ngham_stat_t), - 1, - sizeof(ngham_pos_t), - sizeof(ngham_toh_t), - sizeof(ngham_dest_t), - PKT_SIZE_VARIABLE, - PKT_SIZE_VARIABLE, - 1 -}; - -//void ngh_ext_readout_example(uint8_t* d, uint16_t d_len){ -// uint16_t start = 0; -// uint16_t j; -// // ngham_extract_numpkts does necessary length check of each field -// for (j=0; jsignal-200 -// } -// break; -// } -// start += d[start+1] + 2; // next start -// } -//} - -// Will set data type and length and increase tx_pkt length to fit data, -// then return pointer to beginning of data without actually copying any data -uint8_t* ngh_ext_allocate_pkt(tx_pkt_t* p, uint8_t pkt_type, uint16_t data_len){ - if ( (p->pl_len + 2 + data_len) > NGHAM_PL_MAX ) return NULL; - p->pl[p->pl_len] = pkt_type; - p->pl[p->pl_len+1] = data_len; - p->pl_len += 2 + data_len; - return p->pl+p->pl_len-data_len; -} - -// Append extension packet with given data, type and size to tx_pkt -void ngh_ext_append_pkt(tx_pkt_t* p, uint8_t type, uint8_t* data, uint16_t size){ - if ( (p->pl_len + 2 + size) > NGHAM_PL_MAX ) return; - p->pl[p->pl_len] = type; - p->pl[p->pl_len+1] = size; - memcpy(&p->pl[p->pl_len+2], data, size); - p->pl_len += 2 + size; -} - -// Returns number of sub packets and verifies them -uint16_t ngh_ext_numpkts(uint8_t* d, uint16_t d_len){ - // Go through all sub packets - uint16_t start, packets; - start = 0; - packets = 0; - - while( (d_len >= (start+2)) && (d_len >= (start+2+d[start+1])) ){ - // If PKT_TYPE is invalid valid or packet type does not have correct length - if ( (d[start]>PKT_TYPES) || - ((PKT_TYPE_SIZES[d[start]] != PKT_SIZE_VARIABLE) && - (PKT_TYPE_SIZES[d[start]] != d[start+1])) ){ - return 0; - } - packets++; - start += d[start+1] + 2; // next start - } - return packets; -} - -uint8_t ngh_ext_encode_callsign(uint8_t* enc_callsign, char* callsign){ - uint32_t temp; - uint8_t j, copy[7], ssid = 0; - - // Convert to DEC SIXBIT until length is 7, zero terminated, or dash (SSID start) - for (j=0; (j<7) && callsign[j] && (callsign[j]!='-'); j++){ - // Lowercase converted to uppercase - if((callsign[j] >= 0x61) && (callsign[j] <= 0x7a)) copy[j] = callsign[j]-64; - else copy[j] = callsign[j]-32; - } - if (j<7) copy[j] = 0; // Zero terminate if preliminary end - - // Get SSID, if any - if (callsign[j] == '-'){ - j++; - // First number - if ( (callsign[j] >= 0x30) && (callsign[j] <= 0x39) ) ssid += (callsign[j] - '0'); - else return 0; - j++; - // Second number - if ( (callsign[j] >= 0x30) && (callsign[j] <= 0x39) ){ - ssid *= 10; - ssid += (callsign[j] - '0'); - j++; - } - else if (callsign[j] != 0) return 0; - } - - temp = ((copy[0]<<18)&0xfc0000) | ((copy[1]<<12)&0x3f000) | ((copy[2]<<6)&0xfc0) | (copy[3]&0x3f); - enc_callsign[0] = (temp>>16) & 0xff; - enc_callsign[1] = (temp>>8) & 0xff; - enc_callsign[2] = temp & 0xff; - - temp = ((copy[4]<<18)&0xfc0000) | ((copy[5]<<12)&0x3f000) | ((copy[6]<<6)&0xfc0) | (ssid&0x3f); - enc_callsign[3] = (temp>>16) & 0xff; - enc_callsign[4] = (temp>>8) & 0xff; - enc_callsign[5] = temp & 0xff; - - return 1; -} - -// 11 long array (7 characters, dash, two decimals and termination) -// Destination "callsign", source "enc_callsign" -void ngh_ext_decode_callsign(char* callsign, uint8_t* enc_callsign){ - uint32_t temp; - uint8_t j, ssid; - - temp = ((enc_callsign[0]<<16)&0xff0000) | ((enc_callsign[1]<<8)&0xff00) | (enc_callsign[2]&0xff); - callsign[0] = (temp>>18) & 0x3f; - callsign[1] = (temp>>12) & 0x3f; - callsign[2] = (temp>>6) & 0x3f; - callsign[3] = temp & 0x3f; - - temp = ((enc_callsign[3]<<16)&0xff0000) | ((enc_callsign[4]<<8)&0xff00) | (enc_callsign[5]&0xff); - callsign[4] = (temp>>18) & 0x3f; - callsign[5] = (temp>>12) & 0x3f; - callsign[6] = (temp>>6) & 0x3f; - callsign[7] = 0; // Zero terminate (needed if max length) - - // Find end of callsign and convert from DEC SIXBIT - for (j=0; (j<7) && callsign[j]; j++) callsign[j] += 32; - - // If non-zero SSID - ssid = temp & 0x3f; - if (ssid){ - callsign[j++] = '-'; - if (ssid > 9){ - callsign[j++] = (ssid/10) + '0'; - ssid %= 10; - } - callsign[j++] = ssid + '0'; - callsign[j] = 0; - } -} \ No newline at end of file diff --git a/firmware/app/libs/ngham-0.1/src/ngham_packets.c b/firmware/app/libs/ngham-0.1/src/ngham_packets.c deleted file mode 100644 index ed853712..00000000 --- a/firmware/app/libs/ngham-0.1/src/ngham_packets.c +++ /dev/null @@ -1,16 +0,0 @@ -#include "ngham_packets.h" - -void rx_pkt_init(rx_pkt_t *p){ - p->pl_len = 0; - p->ngham_flags = 0; - p->rssi = RSSI_NA; - p->noise = RSSI_NA; - p->errors = 0; - p->timestamp_toh_us = TIMESTAMP_NA; -} - -void tx_pkt_init(tx_pkt_t *p){ - p->pl_len = 0; - p->ngham_flags = 0; - p->priority = PKT_PRIORITY_NORMAL; -} diff --git a/firmware/app/libs/ngham-0.1/.github/workflows/test.yml b/firmware/app/libs/ngham-1.0/.github/workflows/test.yml similarity index 100% rename from firmware/app/libs/ngham-0.1/.github/workflows/test.yml rename to firmware/app/libs/ngham-1.0/.github/workflows/test.yml diff --git a/firmware/app/libs/ngham-0.1/.gitignore b/firmware/app/libs/ngham-1.0/.gitignore similarity index 100% rename from firmware/app/libs/ngham-0.1/.gitignore rename to firmware/app/libs/ngham-1.0/.gitignore diff --git a/firmware/app/libs/ngham-0.1/CMakeLists.txt b/firmware/app/libs/ngham-1.0/CMakeLists.txt similarity index 100% rename from firmware/app/libs/ngham-0.1/CMakeLists.txt rename to firmware/app/libs/ngham-1.0/CMakeLists.txt diff --git a/firmware/app/libs/ngham-0.1/LICENSE b/firmware/app/libs/ngham-1.0/LICENSE similarity index 100% rename from firmware/app/libs/ngham-0.1/LICENSE rename to firmware/app/libs/ngham-1.0/LICENSE diff --git a/firmware/app/libs/ngham-0.1/README.md b/firmware/app/libs/ngham-1.0/README.md similarity index 100% rename from firmware/app/libs/ngham-0.1/README.md rename to firmware/app/libs/ngham-1.0/README.md diff --git a/firmware/app/libs/ngham-0.1/include/ngham/ccsds_scrambler.h b/firmware/app/libs/ngham-1.0/include/ngham/ccsds_scrambler.h similarity index 95% rename from firmware/app/libs/ngham-0.1/include/ngham/ccsds_scrambler.h rename to firmware/app/libs/ngham-1.0/include/ngham/ccsds_scrambler.h index aa4983d1..7e62235b 100755 --- a/firmware/app/libs/ngham-0.1/include/ngham/ccsds_scrambler.h +++ b/firmware/app/libs/ngham-1.0/include/ngham/ccsds_scrambler.h @@ -16,7 +16,7 @@ * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License - * along with NGHam. If not, see . + * along with NGHam. If not, see . * */ diff --git a/firmware/app/libs/ngham-0.1/include/ngham/config.h b/firmware/app/libs/ngham-1.0/include/ngham/config.h similarity index 100% rename from firmware/app/libs/ngham-0.1/include/ngham/config.h rename to firmware/app/libs/ngham-1.0/include/ngham/config.h diff --git a/firmware/app/libs/ngham-0.1/include/ngham/crc_ccitt.h b/firmware/app/libs/ngham-1.0/include/ngham/crc_ccitt.h similarity index 94% rename from firmware/app/libs/ngham-0.1/include/ngham/crc_ccitt.h rename to firmware/app/libs/ngham-1.0/include/ngham/crc_ccitt.h index 80cc8f8f..e4c8a51e 100755 --- a/firmware/app/libs/ngham-0.1/include/ngham/crc_ccitt.h +++ b/firmware/app/libs/ngham-1.0/include/ngham/crc_ccitt.h @@ -16,7 +16,7 @@ * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License - * along with NGHam. If not, see . + * along with NGHam. If not, see . * */ @@ -35,7 +35,7 @@ */ #ifndef CRC_CCITT_H_ -#define CRC_CCITT_H_ +#define CRC_CCITT_H_ #include diff --git a/firmware/app/libs/ngham-0.1/include/ngham/ngham.h b/firmware/app/libs/ngham-1.0/include/ngham/ngham.h similarity index 76% rename from firmware/app/libs/ngham-0.1/include/ngham/ngham.h rename to firmware/app/libs/ngham-1.0/include/ngham/ngham.h index a278a405..6204b646 100755 --- a/firmware/app/libs/ngham-0.1/include/ngham/ngham.h +++ b/firmware/app/libs/ngham-1.0/include/ngham/ngham.h @@ -16,7 +16,7 @@ * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License - * along with NGHam. If not, see . + * along with NGHam. If not, see . * */ @@ -24,10 +24,11 @@ * \brief NGHam library definition. * * \author Gabriel Mariano Marcelino + * \author Miguel Boing * - * \version 0.1.0 + * \version 1.0.0 * - * \date 2023/03/12 + * \date 2024/06/03 * * \defgroup ngham NGHam * \{ @@ -40,7 +41,7 @@ #include "ngham_packets.h" -#define NGHAM_VERSION "v0.1.0" +#define NGHAM_VERSION "v1.0.0" #define NGH_PREAMBLE_SIZE 4U #define NGH_SYNC_SIZE 4U @@ -81,13 +82,21 @@ int ngham_init(void); int ngham_encode(uint8_t *data, uint16_t len, uint8_t flags, uint8_t *pkt, uint16_t *pkt_len); /** - * \brief Decodes a given NGHam packet to data. + * \brief Encodes a given data into a NGHam packet. + * + * Received packets are passed to this function - max. length 512B. + * + * \param[in] pkt is the array of bytes to be decoded. * - * \param[in] d . + * \param[in] pkt_len is number of bytes of the packet. * - * \return None. + * \param[in,out] data is a pointer to store the decoded packet. + * + * \param[in,out] data_len is the number of bytes of the decoded packet. + * + * \return The status/error code. */ -void ngham_decode(uint8_t d); +int ngham_decode(uint8_t* pkt, uint16_t pkt_len, uint8_t* data, uint16_t* data_len); #endif /* NGHAM_H_ */ diff --git a/firmware/app/libs/ngham-1.0/include/ngham/ngham_packets.h b/firmware/app/libs/ngham-1.0/include/ngham/ngham_packets.h new file mode 100644 index 00000000..438b789d --- /dev/null +++ b/firmware/app/libs/ngham-1.0/include/ngham/ngham_packets.h @@ -0,0 +1,57 @@ +//**************************************************************// +// NGHam protocol - Jon Petter Skagmo, LA3JPA, 2014. // +// Licensed under LGPL. // +//**************************************************************// + +#ifndef NGHAM_PACKETS_H +#define NGHAM_PACKETS_H + +#include "stdint.h" + +// Packet priority in transmission +#define PKT_PRIORITY_NORMAL 0 +#define PKT_PRIORITY_FIRST_IN_SLOT 10 // Should be put first in the next time slot for timing purposes + +// Size of payload in packet struct +#define PKT_PL_SIZE 512 + +// Basic "not available"-values +#define RSSI_NA 0xff +#define TIMESTAMP_NA 0xffffffff + +// If the following flag is set in a packet, NGHam extensions are used and first byte is type +#define NGHAM_FLAG_TYPE_EXTENSION 0x01 + +#define SPP_PL_MAX 255 // Not a power of two, so no ring buffers! + +#define NGHAM_PL_MAX 220 + +// Ensure compatibility when used with MinGW etc. +#ifdef _WIN32 +#define ATTRIBUTE_PACKED __attribute__ ((packed,gcc_struct)) +#else +#define ATTRIBUTE_PACKED __attribute__ ((packed)) +#endif + +typedef struct ATTRIBUTE_PACKED{ + uint32_t timestamp_toh_us; // Time stamp of sync word detection + uint8_t noise; // Same as above + uint8_t rssi; // In dBm + 200 + uint8_t errors; // Recovered symbols + uint8_t ngham_flags; + uint8_t pl[PKT_PL_SIZE]; + uint16_t pl_len; +}rx_pkt_t; + +typedef struct ATTRIBUTE_PACKED{ + uint8_t priority; + uint8_t ngham_flags; + uint8_t pl[PKT_PL_SIZE]; + uint16_t pl_len; +}tx_pkt_t; + + +void rx_pkt_init(rx_pkt_t *p); +void tx_pkt_init(tx_pkt_t *p); + +#endif diff --git a/firmware/app/libs/ngham-0.1/include/ngham/ngham_spp.h b/firmware/app/libs/ngham-1.0/include/ngham/ngham_spp.h similarity index 69% rename from firmware/app/libs/ngham-0.1/include/ngham/ngham_spp.h rename to firmware/app/libs/ngham-1.0/include/ngham/ngham_spp.h index b839cccc..49711362 100644 --- a/firmware/app/libs/ngham-0.1/include/ngham/ngham_spp.h +++ b/firmware/app/libs/ngham-1.0/include/ngham/ngham_spp.h @@ -6,16 +6,16 @@ #include #include "ngham_packets.h" -#include "ngham_paths.h" +//#include "ngham_paths.h" #include PATH_NGHAM_PLATFORM_SPP -#define NGHAM_SPP_TYPE_RX 0x00 // Packet types -#define NGHAM_SPP_TYPE_TX 0x01 // Packet types -#define NGHAM_SPP_TYPE_LOCAL 0x02 -#define NGHAM_SPP_TYPE_CMD 0x03 +#define NGHAM_SPP_TYPE_RX 0x00 // Packet types +#define NGHAM_SPP_TYPE_TX 0x01 // Packet types +#define NGHAM_SPP_TYPE_LOCAL 0x02 +#define NGHAM_SPP_TYPE_CMD 0x03 void ngham_parse_byte(port_ctx_t* ctx, uint8_t c); void ngham_spp_fill_header(ngh_spphdr_t* hdr, uint8_t type, uint8_t* d, uint16_t d_len); void ngham_print_cmd(port_ctx_t* ctx, uint8_t* d, uint16_t d_len); void ngham_print_rx_pkt(rx_pkt_t* p); -void ngham_pack_tx_pkt_local(tx_pkt_t* p); \ No newline at end of file +void ngham_pack_tx_pkt_local(tx_pkt_t* p); diff --git a/firmware/app/libs/ngham-0.1/src/ccsds_scrambler.c b/firmware/app/libs/ngham-1.0/src/ccsds_scrambler.c similarity index 96% rename from firmware/app/libs/ngham-0.1/src/ccsds_scrambler.c rename to firmware/app/libs/ngham-1.0/src/ccsds_scrambler.c index c01fea4e..ec04e257 100755 --- a/firmware/app/libs/ngham-0.1/src/ccsds_scrambler.c +++ b/firmware/app/libs/ngham-1.0/src/ccsds_scrambler.c @@ -16,7 +16,7 @@ * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License - * along with NGHam. If not, see . + * along with NGHam. If not, see . * */ @@ -62,7 +62,7 @@ const uint8_t ccsds_poly[255] = {0xFF, 0x48, 0x0E, 0xC0, 0x9A, 0x0D, 0x70, 0xBC, uint8_t ccsds_poly_pos = 0U; -void ccsds_scrambler_init() +void ccsds_scrambler_init(void) { ccsds_poly_pos = 0U; } diff --git a/firmware/app/libs/ngham-0.1/src/crc_ccitt.c b/firmware/app/libs/ngham-1.0/src/crc_ccitt.c similarity index 100% rename from firmware/app/libs/ngham-0.1/src/crc_ccitt.c rename to firmware/app/libs/ngham-1.0/src/crc_ccitt.c diff --git a/firmware/app/libs/ngham-1.0/src/ngham.c b/firmware/app/libs/ngham-1.0/src/ngham.c new file mode 100755 index 00000000..051487a5 --- /dev/null +++ b/firmware/app/libs/ngham-1.0/src/ngham.c @@ -0,0 +1,388 @@ +/* + * ngham.c + * + * Copyright The NGHam Contributors. + * + * This file is part of NGHam. + * + * NGHam is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NGHam 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. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with NGHam. If not, see . + * + */ + +/** + * \brief NGHam library implementation. + * + * \author Gabriel Mariano Marcelino + * \author Miguel Boing + * + * \version 1.0.0 + * + * \date 2024/06/03 + * + * \addtogroup ngham + * \{ + */ + +#include /* For NULL etc. */ +#include /* For memcpy */ +#include /* For free */ +#include + +#include "ngham/ngham.h" +#include "ngham/config.h" + +#include "ngham/ccsds_scrambler.h" /* Pre-generated array from scrambling polynomial */ +#include "ngham/crc_ccitt.h" + +/* There are seven different sizes. */ +/* Each size has a correlation tag for size, a total size, a maximum payload size and a parity data size. */ +#define NGH_SIZES 7U + +/* Decoder states */ +#define NGH_STATE_SIZE_TAG 0 +#define NGH_STATE_SIZE_TAG_2 1 +#define NGH_STATE_SIZE_TAG_3 2 +#define NGH_STATE_SIZE_KNOWN 3 +#define NGH_STATE_STATUS 4 +#define NGH_STATE_STATUS_2 5 + +/* Maximum number of errors in the size tag */ +#define NGH_SIZE_TAG_MAX_ERROR 6U + +uint8_t NGH_PL_SIZE[7] = {28U, 60U, 92U, 124U, 156U, 188U, 220U}; /* Actual payload */ +uint8_t NGH_PL_SIZE_FULL[7] = {31U, 63U, 95U, 127U, 159U, 191U, 223U}; /* Size with LEN, payload and CRC */ +uint8_t NGH_PL_PAR_SIZE[7] = {47U, 79U, 111U, 159U, 191U, 223U, 255U}; /* Size with RS parity added */ +uint8_t NGH_PAR_SIZE[7] = {16U, 16U, 16U, 32U, 32U, 32U, 32U}; + +/* The seven different size tag vectors */ +uint32_t NGH_SIZE_TAG[7] = { + 0b001110110100100111001101, + 0b010011011101101001010111, + 0b011101101001001110011010, + 0b100110111011010010101110, + 0b101000001111110101100011, + 0b110101100110111011111001, + 0b111011010010011100110100 +}; + +/* Preamble and synchronization vector */ +uint8_t NGH_PREAMBLE = 0xAAU; +uint8_t NGH_PREAMBLE_FOUR_LEVEL = 0xDDU; +uint8_t NGH_SYNC[4] = {0x5DU, 0xE6U, 0x2AU, 0x7EU}; +uint8_t NGH_SYNC_FOUR_LEVEL[8] = {0x77U, 0xF7U, 0xFDU, 0x7DU, 0x5DU, 0xDDU, 0x7FU, 0xFDU}; + +/* Reed Solomon control blocks for the different NGHAM sizes */ +reed_solomon_t rs[NGH_SIZES] = {0}; + +int decoder_state = NGH_STATE_SIZE_TAG; + +rx_pkt_t rx_pkt; + +/** + * \brief Generates the Reed-Solomon tables for all packets sizes. + * + * Run only once - generates Reed Solomon tables for all 7 packet sizes. + * + * MM=8, genpoly=0x187, fcs=112, prim=11, nroots=32 or 16 + * + * \return The status/error code. + */ +static int ngham_init_arrays(void); + +/** + * \brief Used to check if hamming distance in size tag is smaller than threshold. + * + * \param[in] x . + * + * \param[in] y . + * + * \return . + */ +static uint8_t ngham_tag_check(uint32_t x, uint32_t y); + +static int ngham_decode_byte(uint8_t d, uint8_t* pkt); + +int ngham_init(void) +{ + decoder_state = NGH_STATE_SIZE_TAG; + + return ngham_init_arrays(); +} + +int ngham_encode(uint8_t *data, uint16_t len, uint8_t flags, uint8_t *pkt, uint16_t *pkt_len) +{ + int err = -1; + uint16_t j = 0U; + uint16_t crc = 0U; + uint8_t size_nr = 0U; + uint16_t d_len = 0U; + uint8_t codeword_start = 0U; + + /* Check size and find control block for smallest possible RS codeword */ + if (!((len == 0U) || (len > NGH_PL_SIZE[NGH_SIZES - 1U]))) + { + while(len > NGH_PL_SIZE[size_nr]) + { + size_nr++; + } + + /* Insert preamble, sync and size-tag */ + if (NGHAM_FOUR_LEVEL_MODULATION == 1) + { + codeword_start = NGH_PREAMBLE_SIZE_FOUR_LEVEL + NGH_SYNC_SIZE_FOUR_LEVEL + NGH_SIZE_TAG_SIZE; + + for(j = 0; j < NGH_PREAMBLE_SIZE_FOUR_LEVEL; j++) + { + pkt[d_len] = NGH_PREAMBLE_FOUR_LEVEL; + d_len++; + } + + for(j = 0; j < NGH_SYNC_SIZE_FOUR_LEVEL; j++) + { + pkt[d_len] = NGH_SYNC_FOUR_LEVEL[j]; + d_len++; + } + } + else + { + codeword_start = NGH_PREAMBLE_SIZE + NGH_SYNC_SIZE + NGH_SIZE_TAG_SIZE; + + for(j = 0U; j < NGH_PREAMBLE_SIZE; j++) + { + pkt[d_len] = NGH_PREAMBLE; + d_len++; + } + + for(j = 0U; j < NGH_SYNC_SIZE; j++) + { + pkt[d_len] = NGH_SYNC[j]; + d_len++; + } + } + + pkt[d_len] = (NGH_SIZE_TAG[size_nr] >> 16) & 0xFFU; + d_len++; + pkt[d_len] = (NGH_SIZE_TAG[size_nr] >> 8) & 0xFFU; + d_len++; + pkt[d_len] = NGH_SIZE_TAG[size_nr] & 0xFFU; + d_len++; + + /* Prepare content of codeword */ + pkt[d_len] = (NGH_PL_SIZE[size_nr] - len) & 0x1FU; /* Insert padding size */ + pkt[d_len] |= (flags << 5) & 0xE0U; /* Insert flags */ + d_len++; + for(j = 0; j < len; j++) + { + pkt[d_len] = data[j]; /* Insert data */ + d_len++; + } + crc = crc_ccitt(&pkt[codeword_start], len + 1U); /* Insert CRC */ + pkt[d_len] = (crc >> 8) & 0xFFU; + d_len++; + pkt[d_len] = crc & 0xFFU; + d_len++; + for(j = len + 3U; j < NGH_PL_SIZE_FULL[size_nr]; j++) + { + pkt[d_len] = 0; /* Insert padding */ + d_len++; + } + + /* Generate parity data */ + uint8_t par_len = UINT8_MAX; + rsc_encode(&rs[size_nr], &pkt[codeword_start], &pkt[d_len], &par_len); + d_len += NGH_PAR_SIZE[size_nr]; + + /* Scramble */ + for(j = 0; j < NGH_PL_PAR_SIZE[size_nr]; j++) + { + pkt[codeword_start + j] ^= ccsds_poly[j]; + } + + *pkt_len = d_len; + err = 0; + } + return err; +} + +int ngham_decode(uint8_t* pkt, uint16_t pkt_len, uint8_t* data, uint16_t* data_len) +{ + int err = -1; + uint16_t i = 0U; + static uint8_t preamb_sync_1[8] = {0xAA, 0xAA, 0xAA, 0xAA, 0x5D, 0xE6, 0x2A, 0x7E}; + static uint8_t preamb_sync_2[16] = {0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0x77, 0xF7, 0xFD, 0x7D, 0x5D, 0xDD, 0x7F, 0xFD}; + uint8_t offset = 0U; + + + /* Remove for preamble and sync*/ + if (memcmp(preamb_sync_1, pkt, 8U * sizeof(pkt[0])) == 0) + { + offset = 8U; + } + else if(memcmp(preamb_sync_2, pkt, 16U * sizeof(pkt[0])) == 0) + { + offset = 16U; + } + else + { + offset = 0U; + } + + rx_pkt_init(&rx_pkt); + + for (i=offset; i < pkt_len; i++) + { + if (ngham_decode_byte(pkt[i], &pkt[offset + 3U]) == 0) + { + err = 0; + break; + } + } + + (void)memcpy(data, rx_pkt.pl, rx_pkt.pl_len); + *data_len = rx_pkt.pl_len; + + return err; +} + +static int ngham_decode_byte(uint8_t d, uint8_t* buf) +{ + static uint8_t size_nr; + static uint32_t size_tag; + static unsigned int length; + int8_t errors; + uint32_t num_errors = 0U; + int err = -1; + uint16_t i; + + switch(decoder_state) + { + case NGH_STATE_SIZE_TAG: + size_tag = 0U; + + case NGH_STATE_SIZE_TAG_2: + size_tag <<= 8U; + size_tag |= d; + decoder_state++; + break; + case NGH_STATE_SIZE_TAG_3: + size_tag <<= 8U; + size_tag |= d; + { + for (size_nr = 0U; size_nr < NGH_SIZES; size_nr++) + { + /* If tag is intact, set known size */ + if (ngham_tag_check(size_tag, NGH_SIZE_TAG[size_nr]) == 1U) + { + decoder_state = NGH_STATE_SIZE_KNOWN; + length = 0U; + + break; + } + } + /* If size tag is not found, every size can theoretically be attempted */ + if (decoder_state != NGH_STATE_SIZE_KNOWN) + { + decoder_state = NGH_STATE_SIZE_TAG; + } + } + break; + case NGH_STATE_SIZE_KNOWN: + /* De-scramble byte and append to buffer */ + buf[length] = d^ccsds_poly[length]; + length++; + + if (length == NGH_PL_PAR_SIZE[size_nr]) + { + decoder_state = NGH_STATE_SIZE_TAG; + + errors = rsc_decode(&rs[size_nr], buf, 0UL, &num_errors); + rx_pkt.pl_len = NGH_PL_SIZE[size_nr] - (buf[0] & NGH_PADDING_bm); + + /* Check if the packet is decodeable and then if CRC is OK */ + if ((errors != -1) && (crc_ccitt(buf, rx_pkt.pl_len + 1U) == ((buf[rx_pkt.pl_len + 1U] << 8U) | buf[rx_pkt.pl_len + 2U])) ) + { + /* Copy remaining fields and pass on */ + rx_pkt.errors = errors; + rx_pkt.ngham_flags = (buf[0] & NGH_FLAGS_bm) >> NGH_FLAGS_bp; + (void)memcpy(rx_pkt.pl, &buf[1], rx_pkt.pl_len); + err = 0; + } + } + break; + default: + err = -1; + + break; + } + + return err; +} + +static int ngham_init_arrays(void) +{ + int err = 0; + uint16_t j = 0U; + + for(j = 0U; j < 3U; j++) + { + if (rsc_init(8, 0x187, 112, 11, 16, (NGH_PL_PAR_SIZE[6] - NGH_PL_PAR_SIZE[j]), &rs[j]) != 0) + { + err = -1; + } + } + + for(; j < NGH_SIZES; j++) + { + if (rsc_init(8, 0x187, 112, 11, 32, (NGH_PL_PAR_SIZE[6] - NGH_PL_PAR_SIZE[j]), &rs[j]) != 0) + { + err = -1; + } + } + + return err; +} + +static uint8_t ngham_tag_check(uint32_t x, uint32_t y) +{ + uint8_t j = 0U; + uint8_t distance = 0U; + uint32_t diff = x ^ y; + uint8_t res = 0; + + if (diff != 0U) + { + for(j = 0U; j < 24U; j++) + { + if ((diff & 0x01U) != 0U) + { + distance++; + if (distance > NGH_SIZE_TAG_MAX_ERROR) + { + res = 0; + break; + } + } + + diff >>= 1; + } + } + else + { + res = 1; + } + + return res; +} + +/**< \} End of ngham group */ diff --git a/firmware/app/libs/ngham-1.0/src/ngham_packets.c b/firmware/app/libs/ngham-1.0/src/ngham_packets.c new file mode 100644 index 00000000..bba33e47 --- /dev/null +++ b/firmware/app/libs/ngham-1.0/src/ngham_packets.c @@ -0,0 +1,16 @@ +#include "ngham/ngham_packets.h" + +void rx_pkt_init(rx_pkt_t *p){ + p->pl_len = 0; + p->ngham_flags = 0; + p->rssi = RSSI_NA; + p->noise = RSSI_NA; + p->errors = 0; + p->timestamp_toh_us = TIMESTAMP_NA; +} + +void tx_pkt_init(tx_pkt_t *p){ + p->pl_len = 0; + p->ngham_flags = 0; + p->priority = PKT_PRIORITY_NORMAL; +} diff --git a/firmware/app/libs/ngham-0.1/src/ngham_spp.c b/firmware/app/libs/ngham-1.0/src/ngham_spp.c similarity index 100% rename from firmware/app/libs/ngham-0.1/src/ngham_spp.c rename to firmware/app/libs/ngham-1.0/src/ngham_spp.c diff --git a/firmware/app/libs/ngham-0.1/tests/CMakeLists.txt b/firmware/app/libs/ngham-1.0/tests/CMakeLists.txt similarity index 100% rename from firmware/app/libs/ngham-0.1/tests/CMakeLists.txt rename to firmware/app/libs/ngham-1.0/tests/CMakeLists.txt diff --git a/firmware/app/libs/ngham-0.1/tests/Makefile b/firmware/app/libs/ngham-1.0/tests/Makefile similarity index 100% rename from firmware/app/libs/ngham-0.1/tests/Makefile rename to firmware/app/libs/ngham-1.0/tests/Makefile diff --git a/firmware/app/libs/ngham-0.1/tests/README.md b/firmware/app/libs/ngham-1.0/tests/README.md similarity index 100% rename from firmware/app/libs/ngham-0.1/tests/README.md rename to firmware/app/libs/ngham-1.0/tests/README.md diff --git a/firmware/app/libs/ngham-0.1/tests/test.c b/firmware/app/libs/ngham-1.0/tests/test.c similarity index 100% rename from firmware/app/libs/ngham-0.1/tests/test.c rename to firmware/app/libs/ngham-1.0/tests/test.c From 90f97c97eeb20bfae900b91949af9a14737bc13c Mon Sep 17 00:00:00 2001 From: Miguel Boing Date: Mon, 3 Jun 2024 20:23:30 -0300 Subject: [PATCH 044/103] firmware: Updating paths to ngham and rsc libs #136 --- firmware/.cproject | 68 +++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/firmware/.cproject b/firmware/.cproject index cf1dda38..e32c60c6 100644 --- a/firmware/.cproject +++ b/firmware/.cproject @@ -55,8 +55,8 @@ - - + + -