Skip to content

Commit

Permalink
Merge pull request #199 from spacelab-ufsc/work/update-time-counter-task
Browse files Browse the repository at this point in the history
Work/update time counter task
  • Loading branch information
joaoclaudioeb authored Sep 24, 2024
2 parents 5f11065 + 1b67d06 commit 760f3af
Show file tree
Hide file tree
Showing 12 changed files with 241 additions and 142 deletions.
12 changes: 7 additions & 5 deletions firmware/app/structs/eps2_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
*/

#include <system/sys_log/sys_log.h>
#include <system/system.h>

#include "eps2_data.h"

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions firmware/app/structs/eps2_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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. */
Expand Down
8 changes: 4 additions & 4 deletions firmware/app/tasks/read_sensors.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
*/

#include <system/sys_log/sys_log.h>
#include <system/system.h>

#include <devices/current_sensor/current_sensor.h>
#include <devices/voltage_sensor/voltage_sensor.h>
Expand Down Expand Up @@ -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));
}
Expand Down
2 changes: 1 addition & 1 deletion firmware/app/tasks/startup.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
Expand Down
34 changes: 22 additions & 12 deletions firmware/app/tasks/time_control.c
Original file line number Diff line number Diff line change
@@ -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.
*
Expand All @@ -25,7 +25,7 @@
*
* \author Gabriel Mariano Marcelino <gabriel.mm8@gmail.com>
*
* \version 0.2.37
* \version 0.1.10
*
* \date 2020/08/09
*
Expand All @@ -35,13 +35,13 @@

#include <system/system.h>
#include <system/sys_log/sys_log.h>
/* #include <devices/media/media.h> */
#include <devices/media/media.h>
#include <config/config.h>

#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. */
Expand Down Expand Up @@ -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));
Expand All @@ -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]))
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions firmware/app/tasks/time_control.h
Original file line number Diff line number Diff line change
@@ -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.
*
Expand All @@ -25,7 +25,7 @@
*
* \author Gabriel Mariano Marcelino <gabriel.mm8@gmail.com>
*
* \version 0.2.37
* \version 0.1.10
*
* \date 2020/08/09
*
Expand Down
3 changes: 3 additions & 0 deletions firmware/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 760f3af

Please sign in to comment.