diff --git a/firmware/Core/Inc/rtos_tasks/rtos_tasks.h b/firmware/Core/Inc/rtos_tasks/rtos_tasks.h index 0dc6b6aa2..6b00b4739 100644 --- a/firmware/Core/Inc/rtos_tasks/rtos_tasks.h +++ b/firmware/Core/Inc/rtos_tasks/rtos_tasks.h @@ -14,5 +14,6 @@ void TASK_handle_uart_telecommands(void *argument); void TASK_execute_telecommands(void *argument); void TASK_monitor_freertos_memory(void *argument); +void TASK_bootup(void *argument); #endif // __INCLUDE_GUARD__RTOS_TASKS_H__ diff --git a/firmware/Core/Inc/startup_sequence/antenna_deploy_startup.h b/firmware/Core/Inc/startup_sequence/antenna_deploy_startup.h new file mode 100644 index 000000000..c01633ade --- /dev/null +++ b/firmware/Core/Inc/startup_sequence/antenna_deploy_startup.h @@ -0,0 +1,7 @@ +#ifndef __INCLUDE_GUARD__ANTENNA_DEPLOY_STARTUP_H__ +#define __INCLUDE_GUARD__ANTENNA_DEPLOY_STARTUP_H__ +#include +int16_t START_antenna_deploy(); + + +#endif // __INCLUDE_GUARD__ANTENNA_DEPLOY_STARTUP_H__ \ No newline at end of file diff --git a/firmware/Core/Src/main.c b/firmware/Core/Src/main.c index e8692eb45..aca3af3f6 100644 --- a/firmware/Core/Src/main.c +++ b/firmware/Core/Src/main.c @@ -32,6 +32,8 @@ #include "adcs_drivers/adcs_internal_drivers.h" #include "littlefs/flash_driver.h" +#include "startup_sequence/antenna_deploy_startup.h" + /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -121,6 +123,13 @@ const osThreadAttr_t TASK_time_sync_Attributes = { .priority = (osPriority_t) osPriorityNormal, //TODO: Figure out which priority makes sense for this task }; +osThreadId_t TASK_bootup_Handle; +const osThreadAttr_t TASK_bootup_Attributes = { + .name = "TASK_bootup", + .stack_size = 1024, //in bytes. 512 is tool small and will cause crashes when lfs_close() is called! + .priority = (osPriority_t) osPriorityNormal, //TODO: Figure out which priority makes sense for this task +}; + osThreadId_t TASK_monitor_freertos_memory_Handle; const osThreadAttr_t TASK_monitor_freertos_memory_Attributes = { .name = "TASK_monitor_freertos_memory", @@ -294,6 +303,8 @@ int main(void) TASK_service_eps_watchdog_Handle = osThreadNew(TASK_service_eps_watchdog, NULL, &TASK_service_eps_watchdog_Attributes); TASK_time_sync_Handle = osThreadNew(TASK_time_sync, NULL, &TASK_time_sync_Attributes); + + TASK_bootup_Handle = osThreadNew(TASK_bootup, NULL, &TASK_bootup_Attributes); /* USER CODE END RTOS_THREADS */ /* USER CODE BEGIN RTOS_EVENTS */ diff --git a/firmware/Core/Src/rtos_tasks/rtos_tasks.c b/firmware/Core/Src/rtos_tasks/rtos_tasks.c index 0e3abe8fc..3296ee963 100644 --- a/firmware/Core/Src/rtos_tasks/rtos_tasks.c +++ b/firmware/Core/Src/rtos_tasks/rtos_tasks.c @@ -12,6 +12,7 @@ #include "log/log.h" #include "config/configuration.h" #include "eps_drivers/eps_commands.h" +#include "startup_sequence/antenna_deploy_startup.h" #include "cmsis_os.h" @@ -222,3 +223,21 @@ void TASK_monitor_freertos_memory(void *argument) { } /* End Task's Main Loop */ } +/// @brief Completes all tasks which are nesssary after the obc is powered on(or rebooted) +/// @param argument +void TASK_bootup(void *argument) { + TASK_HELP_start_of_task(); + + uint8_t ant_deploy_complete = 0; + uint32_t delay_ms = 100; + while (1) { + //TODO: What should the delay be here? + osDelay(delay_ms); + delay_ms = 60000; + if (!ant_deploy_complete) { + if (START_antenna_deploy() == 0) { + ant_deploy_complete = 1; + } + } + } /* End Task's Main Loop */ +} diff --git a/firmware/Core/Src/startup_sequence/antenna_deploy_startup.c b/firmware/Core/Src/startup_sequence/antenna_deploy_startup.c new file mode 100644 index 000000000..58409f8d9 --- /dev/null +++ b/firmware/Core/Src/startup_sequence/antenna_deploy_startup.c @@ -0,0 +1,232 @@ +#include "startup_sequence/antenna_deploy_startup.h" +#include "littlefs/littlefs_helper.h" +#include "littlefs/lfs.h" +#include "log/log.h" +#include "antenna_deploy_drivers/ant_commands.h" +#include "antenna_deploy_drivers/ant_internal_drivers.h" +/// @brief Attempts to read to the file "lifecycle/deploy_antenna_on_boot_enabled.bool". +/// If 1 is stored in the file then it attempts to deploy the antenna. If the file did not +/// exist: then it creates the file, stores a 1 in it, and attempts to deploy. +/// @return 0 on success, <0 on failure +int16_t START_antenna_deploy() { + //TODO: remove after validation + LOG_message( + LOG_SYSTEM_LFS, + LOG_SEVERITY_NORMAL, + LOG_SINK_ALL, + "Starting Antenna Deploy" + ); + + uint8_t LFS_unmount_on_completion = 0; + if (!LFS_is_lfs_mounted) { + if (lfs_mount(&LFS_filesystem, &LFS_cfg) != 0) { + return -1; + } + LFS_unmount_on_completion = 1; + } + + // Create lifecycle directory if it doesn't exist + const int16_t mkdir_result = lfs_mkdir(&LFS_filesystem, "lifecycle"); + if(mkdir_result == LFS_ERR_EXIST) { + LOG_message( + LOG_SYSTEM_LFS, + LOG_SEVERITY_NORMAL, + LOG_SINK_ALL, + "lifecycle directory already exists. Skipping creation." + ); + } + + if(mkdir_result != LFS_ERR_EXIST && mkdir_result != 0) { + LOG_message( + LOG_SYSTEM_LFS, + LOG_SEVERITY_NORMAL, + LOG_SINK_ALL, + "Error %d creating lifecycle directory.", + mkdir_result + ); + return -2; + } + + //TODO: remove after validation + if(mkdir_result == 0) { + LOG_message( + LOG_SYSTEM_LFS, + LOG_SEVERITY_NORMAL, + LOG_SINK_ALL, + "lifecycle directory created." + ); + } + // At this point the lifecycle directory exists or has been created + + + // Create deploy_antenna_on_boot_enabled.bool file if it doesn't exist + lfs_file_t file; + int16_t open_result = lfs_file_opencfg(&LFS_filesystem, &file, "lifecycle/deploy_antenna_on_boot_enabled.bool", LFS_O_RDONLY, &LFS_file_cfg); + if ( open_result == LFS_ERR_NOENT) { + + // if file doesn't exist, create it and open for writing and write 1 + open_result = lfs_file_opencfg(&LFS_filesystem, &file, "lifecycle/deploy_antenna_on_boot_enabled.bool", LFS_O_WRONLY | LFS_O_CREAT, &LFS_file_cfg); + if(open_result != 0) { + LOG_message( + LOG_SYSTEM_LFS, + LOG_SEVERITY_WARNING, + LOG_SINK_ALL, + "Error %d opening/creating deploy_antenna_on_boot_enabled.bool file.", + open_result + ); + return open_result; + } + + uint8_t buff = 1; + int32_t write_result = lfs_file_write(&LFS_filesystem, &file, &buff, sizeof(buff)); + if(write_result < 0) { + LOG_message( + LOG_SYSTEM_LFS, + LOG_SEVERITY_WARNING, + LOG_SINK_ALL, + "Error %ld writing to newly created deploy_antenna_on_boot_enabled.bool file.", + write_result + ); + return write_result; + } + + //TODO: remove after validation + LOG_message( + LOG_SYSTEM_LFS, + LOG_SEVERITY_NORMAL, + LOG_SINK_ALL, + "file did not exist, created deploy_antenna_on_boot_enabled.bool file." + ); + + int32_t close_result = lfs_file_close(&LFS_filesystem, &file); + if(close_result != 0) { + LOG_message( + LOG_SYSTEM_LFS, + LOG_SEVERITY_WARNING, + LOG_SINK_ALL, + "Error %ld closing newly created deploy_antenna_on_boot_enabled.bool file.", + close_result + ); + return close_result; + } + + // the file has been created and 1 has been written to it, open for reading + open_result = lfs_file_opencfg(&LFS_filesystem, &file, "lifecycle/deploy_antenna_on_boot_enabled.bool", LFS_O_RDONLY, &LFS_file_cfg); + if (open_result != 0) { + LOG_message( + LOG_SYSTEM_LFS, + LOG_SEVERITY_WARNING, + LOG_SINK_ALL, + "Error %d opening/creating deploy_antenna_on_boot_enabled.bool file.", + open_result + ); + return -5 ; + } + LOG_message( + LOG_SYSTEM_LFS, + LOG_SEVERITY_NORMAL, + LOG_SINK_ALL, + "new file created, 1 written, opend for reading." + ); + } + + + //TODO: remove after validation + else if (open_result == 0) { + LOG_message( + LOG_SYSTEM_LFS, + LOG_SEVERITY_NORMAL, + LOG_SINK_ALL, + "deploy_antenna_on_boot_enabled.bool file opened." + ); + } + + // At this point the depoy_antenna_on_boot_enabled.bool file is open + uint8_t deploy_antenna_on_boot_enabled; + int32_t num_bytes_read = lfs_file_read(&LFS_filesystem, &file, &deploy_antenna_on_boot_enabled, sizeof(deploy_antenna_on_boot_enabled)); + if(num_bytes_read < 0) { + LOG_message( + LOG_SYSTEM_LFS, + LOG_SEVERITY_WARNING, + LOG_SINK_ALL, + "Error %ld reading deploy_antenna_on_boot_enabled.bool file.", + num_bytes_read + ); + return num_bytes_read; + } + //TODO: remove after validation + if(num_bytes_read == 0) { + LOG_message( + LOG_SYSTEM_LFS, + LOG_SEVERITY_NORMAL, + LOG_SINK_ALL, + "deploy_antenna_on_boot_enabled.bool file is empty." + ); + } + //TODO: remove after validation + if(num_bytes_read > 0) { + LOG_message( + LOG_SYSTEM_LFS, + LOG_SEVERITY_NORMAL, + LOG_SINK_ALL, + "read %ld bytes from deploy_antenna_on_boot_enabled.bool. Result: %d", + num_bytes_read, + deploy_antenna_on_boot_enabled + ); + } + //reading the file is done now, close the file and unmount lfs if needed + int16_t close_status = lfs_file_close(&LFS_filesystem, &file); + if( close_status != 0) { + LOG_message( + LOG_SYSTEM_LFS, + LOG_SEVERITY_ERROR, + LOG_SINK_ALL, + "Error %d closing deploy_antenna_on_boot_enabled.bool file.", + close_status + ); + return close_status; + } + + if(LFS_unmount_on_completion) { + int16_t unmount_status = lfs_unmount(&LFS_filesystem); + if (unmount_status != 0) { + LOG_message( + LOG_SYSTEM_LFS, + LOG_SEVERITY_ERROR, + LOG_SINK_ALL, + "Error %d unmounting lfs.", + unmount_status + ); + return unmount_status; + } + } + + if(deploy_antenna_on_boot_enabled == 1) { + //TODO: which mcu should be armed on the antenna deploy system? Error handling. How long to activate? + uint8_t arm_result = ANT_CMD_arm_antenna_system(ANT_I2C_BUS_A_MCU_A); + if (arm_result != 0) { + LOG_message( + LOG_SYSTEM_ANTENNA_DEPLOY, + LOG_SEVERITY_ERROR, + LOG_SINK_ALL, + "Error %d arming antenna deploy system.", + arm_result + ); + return arm_result; + } + + uint8_t deploy_result = ANT_CMD_start_automated_sequential_deployment(ANT_I2C_BUS_A_MCU_A, 7); + if (deploy_result != 0) { + LOG_message( + LOG_SYSTEM_ANTENNA_DEPLOY, + LOG_SEVERITY_ERROR, + LOG_SINK_ALL, + "Error %d deploying antennas.", + deploy_result + ); + return deploy_result; + } + } + + return 0; +} \ No newline at end of file