diff --git a/firmware/app/structs/eps2_data.c b/firmware/app/structs/eps2_data.c index 85ffbb6..522b640 100644 --- a/firmware/app/structs/eps2_data.c +++ b/firmware/app/structs/eps2_data.c @@ -35,6 +35,7 @@ */ #include +#include #include "eps2_data.h" @@ -64,8 +65,9 @@ int eps_buffer_write(uint8_t id, uint32_t *value) { switch(id) { - case EPS2_PARAM_ID_TIME_COUNTER: - eps_data_buff.time_counter_ms = *value; + case EPS2_PARAM_ID_TIMESTAMP: + eps_data_buff.timestamp = *value; + system_set_time(*value); break; case EPS2_PARAM_ID_MCU_TEMP: eps_data_buff.eps_mcu_temp_kelvin = *value; @@ -226,7 +228,7 @@ int eps_buffer_read(uint8_t id, uint32_t *value) #if CONFIG_SET_DUMMY_EPS == 1 switch(id) { - case EPS2_PARAM_ID_TIME_COUNTER: + case EPS2_PARAM_ID_TIMESTAMP: *value = 0; break; case EPS2_PARAM_ID_MCU_TEMP: @@ -384,8 +386,8 @@ int eps_buffer_read(uint8_t id, uint32_t *value) switch(id) { - case EPS2_PARAM_ID_TIME_COUNTER: - *value = eps_data_buff.time_counter_ms; + case EPS2_PARAM_ID_TIMESTAMP: + *value = eps_data_buff.timestamp; break; case EPS2_PARAM_ID_MCU_TEMP: *value = eps_data_buff.eps_mcu_temp_kelvin; diff --git a/firmware/app/structs/eps2_data.h b/firmware/app/structs/eps2_data.h index 87bdf58..b84e933 100644 --- a/firmware/app/structs/eps2_data.h +++ b/firmware/app/structs/eps2_data.h @@ -48,7 +48,7 @@ */ typedef enum { - EPS2_PARAM_ID_TIME_COUNTER = 0, + EPS2_PARAM_ID_TIMESTAMP = 0, EPS2_PARAM_ID_MCU_TEMP = 1, EPS2_PARAM_ID_EPS_CURRENT = 2, EPS2_PARAM_ID_LAST_RESET_CAUSE = 3, @@ -109,7 +109,7 @@ typedef struct /** * EPS MCU related data. */ - uint32_t time_counter_ms; /**< Time counter in milliseconds. */ + uint32_t timestamp; /**< Time counter in milliseconds. */ uint8_t last_reset_cause; /**< EPS MCU last reset cause */ uint16_t reset_counter; /**< EPS MCU reset counter */ uint16_t eps_mcu_temp_kelvin; /**< EPS MCU temperature in kelvin. */ diff --git a/firmware/app/tasks/read_sensors.c b/firmware/app/tasks/read_sensors.c index 47a6587..2f24e11 100644 --- a/firmware/app/tasks/read_sensors.c +++ b/firmware/app/tasks/read_sensors.c @@ -36,6 +36,7 @@ */ #include +#include #include #include @@ -482,10 +483,9 @@ void vTaskReadSensors(void) #endif } - /* Data timestamp */ - uint32_t time_counter = (uint32_t)xTaskGetTickCount(); - - eps_buffer_write(EPS2_PARAM_ID_TIME_COUNTER, &time_counter); + /* Update timestamp */ + uint32_t time_counter = system_get_time(); + eps_buffer_write(EPS2_PARAM_ID_TIMESTAMP, &time_counter); vTaskDelayUntil(&last_cycle, pdMS_TO_TICKS(TASK_READ_SENSORS_PERIOD_MS)); } diff --git a/firmware/app/tasks/startup.c b/firmware/app/tasks/startup.c index daedb35..d4aa0f1 100644 --- a/firmware/app/tasks/startup.c +++ b/firmware/app/tasks/startup.c @@ -140,7 +140,7 @@ void vTaskStartup(void *pvParameters) #if CONFIG_DEV_MEDIA_ENABLED == 1 /* Internal non-volatile memory initialization */ - if (media_init() != 0) + if (media_init(MEDIA_INT_FLASH) != 0) { error_counter++; } diff --git a/firmware/app/tasks/time_control.c b/firmware/app/tasks/time_control.c index 8a7c0bc..5c04133 100644 --- a/firmware/app/tasks/time_control.c +++ b/firmware/app/tasks/time_control.c @@ -1,7 +1,7 @@ /* * time_control.h * - * Copyright The EPS 2.0 Contributors. + * Copyright (C) 2021, SpaceLab. * * This file is part of EPS 2.0. * @@ -25,7 +25,7 @@ * * \author Gabriel Mariano Marcelino * - * \version 0.2.37 + * \version 0.1.10 * * \date 2020/08/09 * @@ -35,13 +35,13 @@ #include #include -/* #include */ +#include #include #include "time_control.h" #include "startup.h" -#define TIME_CONTROL_MEDIA MEDIA_NOR +#define TIME_CONTROL_MEDIA MEDIA_INT_FLASH #define TIME_CONTROL_SAVE_PERIOD_SEC 60 #define TIME_CONTROL_MEM_ID 0x12U #define TIME_CONTROL_CRC8_INITIAL_VAL 0x00U /* CRC8-CCITT initial value. */ @@ -120,6 +120,13 @@ void vTaskTimeControl(void) sys_log_print_event_from_module(SYS_LOG_ERROR, TASK_TIME_CONTROL_NAME, "Error saving the system time!"); sys_log_new_line(); } + else + { + sys_log_print_event_from_module(SYS_LOG_INFO, TASK_TIME_CONTROL_NAME, "Saving system time (epoch): "); + sys_log_print_uint(sys_tm); + sys_log_print_msg(" sec"); + sys_log_new_line(); + } } vTaskDelayUntil(&last_cycle, pdMS_TO_TICKS(TASK_TIME_CONTROL_PERIOD_MS)); @@ -131,7 +138,7 @@ static int time_control_load_sys_time(sys_time_t *tm) int err = -1; uint8_t buf[6] = {0}; -/* + if (media_read(TIME_CONTROL_MEDIA, CONFIG_MEM_ADR_SYS_TIME, buf, 6U) == 0) { if ((buf[0] == TIME_CONTROL_MEM_ID) && (time_control_crc8(buf, 5U) == buf[5])) @@ -154,7 +161,7 @@ static int time_control_load_sys_time(sys_time_t *tm) sys_log_print_event_from_module(SYS_LOG_ERROR, TASK_TIME_CONTROL_NAME, "Error reading the system time from the non-volatile memory!"); sys_log_new_line(); } -*/ + return err; } @@ -170,15 +177,18 @@ static int time_control_save_sys_time(sys_time_t tm) buf[3] = ((uint32_t)tm >> 8) & 0xFFU; buf[4] = (uint32_t)tm & 0xFFU; buf[5] = time_control_crc8(buf, 5U); -/* - if (media_write(TIME_CONTROL_MEDIA, CONFIG_MEM_ADR_SYS_TIME, buf, 6U) != 0) + + if (media_erase(TIME_CONTROL_MEDIA, FLASH_SEG_A_ADR) == 0) { - sys_log_print_event_from_module(SYS_LOG_ERROR, TASK_TIME_CONTROL_NAME, "Error writing the system time to the non-volatile memory!"); - sys_log_new_line(); + if (media_write(TIME_CONTROL_MEDIA, CONFIG_MEM_ADR_SYS_TIME, buf, 6U) != 0) + { + sys_log_print_event_from_module(SYS_LOG_ERROR, TASK_TIME_CONTROL_NAME, "Error writing the system time to the non-volatile memory!"); + sys_log_new_line(); - err = -1; + err = -1; + } } -*/ + return err; } diff --git a/firmware/app/tasks/time_control.h b/firmware/app/tasks/time_control.h index 0eb26ef..794a054 100644 --- a/firmware/app/tasks/time_control.h +++ b/firmware/app/tasks/time_control.h @@ -1,7 +1,7 @@ /* * time_control.c * - * Copyright The EPS 2.0 Contributors. + * Copyright (C) 2021, SpaceLab. * * This file is part of EPS 2.0. * @@ -25,7 +25,7 @@ * * \author Gabriel Mariano Marcelino * - * \version 0.2.37 + * \version 0.1.10 * * \date 2020/08/09 * diff --git a/firmware/config/config.h b/firmware/config/config.h index 407d30c..8d4a1e1 100644 --- a/firmware/config/config.h +++ b/firmware/config/config.h @@ -78,6 +78,9 @@ /* Drivers */ #define CONFIG_DRIVERS_DEBUG_ENABLED 0 +/* Memory adresses */ +#define CONFIG_MEM_ADR_SYS_TIME 0 + #define MAX_BATTERY_CHARGE 2450 /* [mAh] */ #define BAT_MONITOR_CHARGE_VALUE (uint16_t)(MAX_BATTERY_CHARGE/0.625) /* 0.625 is a conversion factor for the battery monitor */ #define RESET_BATTERY_TO_INITIAL_STATE 0 diff --git a/firmware/devices/media/media.c b/firmware/devices/media/media.c index 95dc9b8..194ad2d 100644 --- a/firmware/devices/media/media.c +++ b/firmware/devices/media/media.c @@ -1,101 +1,160 @@ /* * media.c - * - * Copyright (C) 2020, SpaceLab. - * + * + * Copyright The EPS 2.0 Contributors. + * * This file is part of EPS 2.0. - * + * * EPS 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. - * + * * EPS 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 EPS 2.0. If not, see . - * + * along with EPS 2.0. If not, see . + * */ /** * \brief Media device implementation. - * - * \author Augusto Cezar Boldori Vassoler - * - * \version 0.1.0 - * - * \date 02/15/2021 - * + * + * \author Gabriel Mariano Marcelino + * + * \version 0.1.19 + * + * \date 2020/07/21 + * * \addtogroup media * \{ */ +#include #include #include #include "media.h" -int media_init() +int media_init(media_t med) { + int err = -1; - sys_log_print_event_from_module(SYS_LOG_INFO, MEDIA_MODULE_NAME, "Initializing Media device."); - sys_log_new_line(); - - int err = flash_init(); - - if (err != 0) + switch(med) { - sys_log_print_event_from_module(SYS_LOG_ERROR, MEDIA_MODULE_NAME, "Error initializing Media device!"); - sys_log_new_line(); + case MEDIA_INT_FLASH: + err = flash_init(); + + break; + default: + sys_log_print_event_from_module(SYS_LOG_ERROR, MEDIA_MODULE_NAME, "Invalid storage media to initialize!"); + sys_log_new_line(); + + break; } - return err; - } -int media_write(uint32_t adr, uint32_t *data, uint16_t len) +int media_write(media_t med, uint32_t adr, uint8_t *data, uint16_t len) { - /* Address index */ - adr += FLASH_SEG_A_ADR; + int err = -1; - uint16_t i = 0; - for(i=0; i. - * + * */ /** * \brief Media device definition. - * - * \author Augusto Cezar Boldori Vassoler - * - * \version 0.1.0 - * - * \date 02/15/2021 - * + * + * \author Gabriel Mariano Marcelino + * + * \version 0.1.19 + * + * \date 2020/04/21 + * * \defgroup media Media * \ingroup devices * \{ @@ -39,6 +39,8 @@ #include +#include + #define MEDIA_MODULE_NAME "Media" /** @@ -47,24 +49,40 @@ typedef enum { MEDIA_INT_FLASH=0, /**< Internal flash memory. */ - MEDIA_NOR /**< NOR flash memory. */ -} media_types_e; +} media_t; /** - * \brief Media type. + * \brief Erase operation types. */ -typedef uint8_t media_t; +typedef enum +{ + MEDIA_ERASE_DIE=0, /**< Erases the whole die. */ + MEDIA_ERASE_SECTOR, /**< Erases a sector. */ + MEDIA_ERASE_SUB_SECTOR /**< Erases a sub-sector. */ +} media_erase_t; /** * \brief Media initialization. * + * \param[in] med is the storage media to initiailize. It can be: + * \parblock + * -\b MEDIA_INT_FLASH + * . + * \endparblock + * * \return The status/error code. */ -int media_init(); +int media_init(media_t med); /** * \brief Writes data into a given address of a media device. * + * \param[in] med is the storage media to write. It can be: + * \parblock + * -\b MEDIA_INT_FLASH + * . + * \endparblock + * * \param[in] adr is the address to write data. * * \param[in] data is an array of bytes to write. @@ -73,11 +91,17 @@ int media_init(); * * \return The status/error code. */ -int media_write(uint32_t adr, uint32_t *data, uint16_t len); +int media_write(media_t med, uint32_t adr, uint8_t *data, uint16_t len); /** * \brief Reads data from a given address of a media device. * + * \param[in] med is the storage media to read. It can be: + * \parblock + * -\b MEDIA_INT_FLASH + * . + * \endparblock + * * \param[in] adr is the address to read. * * \param[in,out] data is a pointer to store the read data. @@ -86,16 +110,30 @@ int media_write(uint32_t adr, uint32_t *data, uint16_t len); * * \return The status/error code. */ -int media_read(uint32_t adr, uint32_t *data, uint16_t len); +int media_read(media_t med, uint32_t adr, uint8_t *data, uint16_t len); /** * \brief Erases a memory region from a media device. * - * \param[in] adr is the address to erase. + * \param[in] med is the storage media to erase. It can be: + * \parblock + * -\b MEDIA_INT_FLASH + * . + * \endparblock + * + * \param[in] type is the erase operation type. It can be: + * \parblock + * -\b MEDIA_ERASE_DIE + * -\b MEDIA_ERASE_SECTOR + * -\b MEDIA_ERASE_SUB_SECTOR + * . + * \endparblock + * + * \param[in] sector is the sector number to erase. * * \return The status/error code. */ -int media_erase(uint32_t adr); +int media_erase(media_t med, uint32_t sector); #endif /* MEDIA_H_ */ diff --git a/firmware/system/sys_log/sys_log.c b/firmware/system/sys_log/sys_log.c index 651ec07..025e51b 100644 --- a/firmware/system/sys_log/sys_log.c +++ b/firmware/system/sys_log/sys_log.c @@ -42,6 +42,7 @@ #include "config/config.h" +#include "system/system.h" #include "sys_log.h" #include "sys_log_config.h" @@ -374,7 +375,7 @@ void sys_log_print_system_time() sys_log_set_color(SYS_LOG_SYSTEM_TIME_COLOR); sys_log_print_msg("[ "); - sys_log_print_uint(xTaskGetTickCount()); /* System time in milliseconds */ + sys_log_print_uint(system_get_time()); /* System time in milliseconds */ sys_log_print_msg(" ]"); sys_log_reset_color(); diff --git a/firmware/system/system.c b/firmware/system/system.c index f1173d7..5a33b1c 100644 --- a/firmware/system/system.c +++ b/firmware/system/system.c @@ -36,6 +36,8 @@ #include #include +#include + #include "system.h" sys_time_t sys_time = 0; @@ -54,12 +56,16 @@ uint8_t system_get_reset_cause(void) void system_set_time(sys_time_t tm) { + portENTER_CRITICAL(); sys_time = tm; + portEXIT_CRITICAL(); } void system_increment_time(void) { + portENTER_CRITICAL(); sys_time++; + portEXIT_CRITICAL(); } sys_time_t system_get_time(void) diff --git a/firmware/tests/devices/media_test.c b/firmware/tests/devices/media_test.c index 44a5909..0cab72e 100644 --- a/firmware/tests/devices/media_test.c +++ b/firmware/tests/devices/media_test.c @@ -1,35 +1,35 @@ /* * media_test.c - * + * * Copyright (C) 2021, SpaceLab. - * - * This file is part of EPS 2.0. - * - * EPS 2.0 is free software: you can redistribute it and/or modify + * + * 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. - * - * EPS 2.0 is distributed in the hope that it will be useful, + * + * 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 EPS 2.0. If not, see . - * + * along with TTC 2.0. If not, see . + * */ /** - * \brief Unit test of the Media Sensor device - * - * \author Lucas Zacchi de Medeiros - * - * \version 0.1.0 - * - * \date 2021/08/23 - * - * \defgroup media_test Media Sensor + * \brief Unit test of the media device. + * + * \author Gabriel Mariano Marcelino + * + * \version 0.1.3 + * + * \date 2021/08/07 + * + * \defgroup media_unit_test Media * \ingroup tests * \{ */ @@ -46,46 +46,22 @@ static void media_init_test(void **state) { - will_return(__wrap_flash_init, 0); - assert_return_code(media_init(), 0); } static void media_write_test(void **state) { - uint32_t adr = 0; - uint32_t data[8] = {0, 1, 2, 3, 4, 5, 6, 7}; - uint16_t len = 4; - - for (uint16_t i = 0; i < len; i += 4) - { - expect_value(__wrap_flash_write_long, data, data[i]); - } - - assert_return_code(media_write(adr, data, len), 0); } static void media_read_test(void **state) { - uint32_t adr = 0; - uint32_t data[8] = {0, 1, 2, 3, 4, 5, 6, 7}; - uint32_t *data_ptr = data; - uint16_t len = 4; - - for (uint16_t i = 0; i < len; i += 4) - { - will_return(__wrap_flash_read_long, adr - FLASH_SEG_A_ADR); - } - - assert_return_code(media_read(adr, data_ptr, len), 0); } static void media_erase_test(void **state) { - uint32_t address = 0; - - expect_value(__wrap_flash_write_single, data, 0xFF); +} - assert_return_code(media_erase(address), 0); +static void media_get_info_test(void **state) +{ } int main(void) @@ -94,7 +70,11 @@ int main(void) cmocka_unit_test(media_init_test), cmocka_unit_test(media_write_test), cmocka_unit_test(media_read_test), - cmocka_unit_test(media_erase_test)}; + cmocka_unit_test(media_erase_test), + cmocka_unit_test(media_get_info_test), + }; return cmocka_run_group_tests(media_tests, NULL, NULL); } + +/** \} End of media_test group */