Skip to content

Commit d88cd50

Browse files
authored
Merge pull request #169 from spacelab-ufsc/fix_obdh_crc
Fix obdh crc
2 parents 0fb1c46 + fc8b2cc commit d88cd50

7 files changed

Lines changed: 117 additions & 7 deletions

File tree

firmware/app/tasks/antenna_deployment.c

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,45 @@
3737
#include <system/sys_log/sys_log.h>
3838

3939
#include <devices/antenna/antenna.h>
40+
#include <devices/media/media.h>
41+
#include <drivers/flash/flash.h>
4042

4143
#include <structs/ttc_data.h>
4244

45+
#include <libs/crc/crc.h>
46+
4347
#include "antenna_deployment.h"
4448

49+
/**
50+
* \brief Saves deployment state to internal flash memory.
51+
*
52+
* \param[in] ttc is a pointer to ttc data buffer.
53+
*
54+
* \return The status/error code.
55+
*/
56+
static int save_deployment_state_to_flash(ttc_data_t *ttc);
57+
58+
/**
59+
* \brief Loads deployment state from internal flash memory.
60+
*
61+
* \param[in] ttc is a pointer to ttc data buffer.
62+
*
63+
* \return The status/error code.
64+
*/
65+
static int load_deployment_state_from_flash(ttc_data_t *ttc);
66+
4567
xTaskHandle xTaskAntennaDeploymentHandle;
4668

4769
void vTaskAntennaDeployment(void)
4870
{
71+
vTaskDelay(pdMS_TO_TICKS(TASK_ANTENNA_DEPLOYMENT_TIMEOUT_MS));
72+
73+
if (load_deployment_state_from_flash(&ttc_data_buf) != 0)
74+
{
75+
sys_log_print_event_from_module(SYS_LOG_ERROR, TASK_ANTENNA_DEPLOYMENT_NAME, "Failed to load deployment state from internal flash!");
76+
sys_log_new_line();
77+
}
78+
4979
/* Initial hibernation */
5080
ttc_data_buf.ant_deploy_hib_count = 0;
5181

@@ -57,12 +87,22 @@ void vTaskAntennaDeployment(void)
5787

5888
for(i = initial_hib_time_counter; i < CONFIG_ANTENNA_DEPLOYMENT_HIBERNATION_MIN; i++)
5989
{
90+
uint32_t hib_dur = CONFIG_ANTENNA_DEPLOYMENT_HIBERNATION_MIN - (uint32_t)i;
91+
sys_log_print_event_from_module(SYS_LOG_WARNING, TASK_ANTENNA_DEPLOYMENT_NAME, "Antenna deployment will happen in ");
92+
sys_log_print_uint(hib_dur);
93+
sys_log_print_msg(" minutes!");
94+
sys_log_new_line();
95+
6096
vTaskDelay(pdMS_TO_TICKS(60000U));
6197

6298
ttc_data_buf.ant_deploy_hib_count++;
99+
100+
(void)save_deployment_state_to_flash(&ttc_data_buf);
63101
}
64102

65103
ttc_data_buf.ant_deploy_hib_exec = true;
104+
105+
(void)save_deployment_state_to_flash(&ttc_data_buf);
66106
}
67107
else
68108
{
@@ -71,7 +111,7 @@ void vTaskAntennaDeployment(void)
71111
}
72112

73113
/* Antenna deployment */
74-
if (ttc_data_buf.ant_deploy_count< CONFIG_ANTENNA_DEPLOYMENT_ATTEMPTS)
114+
if (ttc_data_buf.ant_deploy_count < CONFIG_ANTENNA_DEPLOYMENT_ATTEMPTS)
75115
{
76116
sys_log_print_event_from_module(SYS_LOG_INFO, TASK_ANTENNA_DEPLOYMENT_NAME, "Antenna deployment attempt number ");
77117
sys_log_print_uint(ttc_data_buf.ant_deploy_count + 1U);
@@ -89,6 +129,8 @@ void vTaskAntennaDeployment(void)
89129
ttc_data_buf.ant_deploy_count++;
90130

91131
ttc_data_buf.ant_deploy_exec = true;
132+
133+
(void)save_deployment_state_to_flash(&ttc_data_buf);
92134
}
93135
else
94136
{
@@ -102,4 +144,46 @@ void vTaskAntennaDeployment(void)
102144
vTaskSuspend(xTaskAntennaDeploymentHandle);
103145
}
104146

147+
static int save_deployment_state_to_flash(ttc_data_t *ttc)
148+
{
149+
int err = -1;
150+
uint8_t buf[5] = {0};
151+
152+
if (media_erase(MEDIA_INT_FLASH, FLASH_SEG_C_ADR) == 0)
153+
{
154+
buf[0] = ttc->ant_deploy_hib_count;
155+
buf[1] = ttc->ant_deploy_exec;
156+
buf[2] = ttc->ant_deploy_count;
157+
buf[3] = ttc->ant_deploy_hib_exec;
158+
buf[4] = crc8_get_val(buf, 4U);
159+
160+
err = media_write(MEDIA_INT_FLASH, 0U, FLASH_SEG_C_ADR, buf, 5U);
161+
}
162+
163+
return err;
164+
}
165+
166+
static int load_deployment_state_from_flash(ttc_data_t *ttc)
167+
{
168+
int err = -1;
169+
170+
uint8_t buf[5] = {0};
171+
172+
/* Getting the previous reset count parameter */
173+
if (media_read(MEDIA_INT_FLASH, 0U, FLASH_SEG_C_ADR, buf, 5U) == 0)
174+
{
175+
if (buf[4] == crc8_get_val(buf, 4U))
176+
{
177+
ttc->ant_deploy_hib_count = buf[0];
178+
ttc->ant_deploy_exec = buf[1];
179+
ttc->ant_deploy_count = buf[2];
180+
ttc->ant_deploy_hib_exec = buf[3];
181+
182+
err = 0;
183+
}
184+
}
185+
186+
return err;
187+
}
188+
105189
/** \} End of antenna_deployment group */

firmware/app/tasks/antenna_deployment.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@
3939

4040
#include <FreeRTOS.h>
4141
#include <task.h>
42+
#include <stdint.h>
4243

4344
#define TASK_ANTENNA_DEPLOYMENT_NAME "Antenna Deployment" /**< Task name. */
44-
#define TASK_ANTENNA_DEPLOYMENT_STACK_SIZE 150 /**< Stack size in bytes. */
45+
#define TASK_ANTENNA_DEPLOYMENT_STACK_SIZE 240 /**< Stack size in bytes. */
4546
#define TASK_ANTENNA_DEPLOYMENT_PRIORITY 6 /**< Task priority. */
47+
#define TASK_ANTENNA_DEPLOYMENT_TIMEOUT_MS 5000 /**< Wait time to initialize the task in milliseconds. */
4648

4749
/**
4850
* \brief Antenna deployment handle.

firmware/app/tasks/read_sensors.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ void vTaskReadSensors(void)
7777
ttc_data_buf.current = (uint16_t) pwr_sensor_buf.current;
7878
ttc_data_buf.voltage = (uint16_t) pwr_sensor_buf.bus_voltage;
7979
ttc_data_buf.power = (uint16_t) pwr_sensor_buf.power;
80+
81+
sys_log_print_event_from_module(SYS_LOG_INFO, TASK_READ_SENSORS_NAME, "Current uC current: ");
82+
sys_log_print_uint((uint32_t)pwr_sensor_buf.current);
83+
sys_log_print_msg(" mA");
84+
sys_log_new_line();
85+
86+
sys_log_print_event_from_module(SYS_LOG_INFO, TASK_READ_SENSORS_NAME, "Current uC voltage: ");
87+
sys_log_print_uint((uint32_t)pwr_sensor_buf.bus_voltage);
88+
sys_log_print_msg(" mV");
89+
sys_log_new_line();
8090
}
8191

8292
/* Radio current, voltage and power*/
@@ -85,6 +95,20 @@ void vTaskReadSensors(void)
8595
ttc_data_buf.radio.current = (uint16_t) pwr_sensor_buf.current;
8696
ttc_data_buf.radio.voltage = (uint16_t) pwr_sensor_buf.bus_voltage;
8797
ttc_data_buf.radio.power = (uint16_t) pwr_sensor_buf.power;
98+
99+
ttc_data_buf.current = (uint16_t) pwr_sensor_buf.current;
100+
ttc_data_buf.voltage = (uint16_t) pwr_sensor_buf.bus_voltage;
101+
ttc_data_buf.power = (uint16_t) pwr_sensor_buf.power;
102+
103+
sys_log_print_event_from_module(SYS_LOG_INFO, TASK_READ_SENSORS_NAME, "Current radio current: ");
104+
sys_log_print_uint((uint32_t)pwr_sensor_buf.current);
105+
sys_log_print_msg(" mA");
106+
sys_log_new_line();
107+
108+
sys_log_print_event_from_module(SYS_LOG_INFO, TASK_READ_SENSORS_NAME, "Current radio voltage: ");
109+
sys_log_print_uint((uint32_t)pwr_sensor_buf.bus_voltage);
110+
sys_log_print_msg(" mV");
111+
sys_log_new_line();
88112
}
89113

90114
/* Radio temperature */

firmware/devices/media/media.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ int media_erase(media_t med, uint32_t sector)
151151
case MEDIA_INT_FLASH:
152152
if (flash_mutex_take() == 0)
153153
{
154-
if ((sector == FLASH_SEG_A_ADR) || (sector == FLASH_SEG_B_ADR))
154+
if ((sector == FLASH_SEG_A_ADR) || (sector == FLASH_SEG_B_ADR) || (sector == FLASH_SEG_C_ADR))
155155
{
156156
flash_erase((uintptr_t)sector);
157157
err = flash_mutex_give();

firmware/devices/obdh/obdh.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ int obdh_read_request(obdh_request_t *obdh_request)
8686
spi_slave_dma_change_transfer_size(OBDH_TRANSFER_SIZE);
8787
}
8888

89-
if ((err != -1) && (crc8_get_val(request, OBDH_TRANSFER_SIZE) != request[OBDH_TRANSFER_SIZE - 1U]))
89+
if ((err != -1) && (crc8_get_val(request, OBDH_TRANSFER_SIZE - 1U) != request[OBDH_TRANSFER_SIZE - 1U]))
9090
{
9191
sys_log_print_event_from_module(SYS_LOG_ERROR, OBDH_MODULE_NAME, "Received invalid CRC!");
9292
sys_log_new_line();

firmware/drivers/isis_antenna/isis_antenna_i2c.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
#define ISIS_ANTENNA_I2C_CLOCK_HZ 100000UL
4646
#define ISIS_ANTENNA_I2C_EN_PIN GPIO_PIN_69
4747
#define ISIS_ANTENNA_I2C_RDY_PIN GPIO_PIN_62
48-
#define ISIS_ANTENNA_I2C_SLAVE_ADDRESS ISIS_ANTENNA_I2C_SLAVE_ADDRESS_UC_A
48+
#define ISIS_ANTENNA_I2C_SLAVE_ADDRESS ISIS_ANTENNA_I2C_SLAVE_ADDRESS_UC_B
4949

5050
#define ISIS_ANTENNA_I2C_OP_ATTEMPTS 10U
5151

firmware/tests/drivers/isis_antenna_test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*
2626
* \author Gabriel Mariano Marcelino <gabriel.mm8@gmail.com>
2727
*
28-
* \version 0.4.1
28+
* \version 1.0.1
2929
*
3030
* \date 2021/09/01
3131
*
@@ -49,7 +49,7 @@
4949

5050
#define ISIS_ANTENNA_IIC_PORT I2C_PORT_2
5151
#define ISIS_ANTENNA_IIC_CLOCK_HZ 100000UL
52-
#define ISIS_ANTENNA_IIC_ADR 0x31
52+
#define ISIS_ANTENNA_IIC_ADR 0x32
5353
#define ISIS_ANTENNA_IIC_EN_PIN GPIO_PIN_69
5454
#define ISIS_ANTENNA_IIC_RDY_PIN GPIO_PIN_62
5555

0 commit comments

Comments
 (0)