diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 8c122b9..a9b5514 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -75,6 +75,10 @@ jobs: id: build-stm32 run: | scons --platform=arm --define="${DEFINES}" + - name: Build x86 + id: build-x86 + run: | + scons --platform=x86 --define="${DEFINES}" # - name: Build and test # id: build-test # run: | diff --git a/can/inc/can.h b/can/inc/can.h new file mode 100644 index 0000000..e2dd8b4 --- /dev/null +++ b/can/inc/can.h @@ -0,0 +1,107 @@ +#pragma once + +/************************************************************************************************ + * @file can.h + * + * @brief Header file for CAN Application code + * + * @date 2024-11-23 + * @author Midnight Sun Team #24 - MSXVI + ************************************************************************************************/ + +/* Standard library headers */ +#include +#include +#include + +/* Inter-component Headers */ +#include "status.h" + +/* Intra-component Headers */ +#include "can_hw.h" +#include "can_queue.h" + +typedef struct CanStorage { + volatile CanQueue rx_queue; + uint16_t device_id; +} CanStorage; + +/** + * @brief Initialize the CAN interface + * @param storage Pointer to the CAN storage + * @param settings Pointer to the CAN settings + * @return STATUS_CODE_OK if initialization succeeded + * STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect + * STATUS_CODE_INTERNAL_ERROR if initialization fails + */ +StatusCode can_init(CanStorage *storage, const CanSettings *settings); + +/** + * @brief Initialize the CAN interface + * @param rx_queue Pointer to the CAN RX queue + * @param settings Pointer to the CAN settings + * @return STATUS_CODE_OK if initialization succeeded + * STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect + * STATUS_CODE_INTERNAL_ERROR if HAL initialization fails + */ +StatusCode can_add_filter_in(); + +/** + * @brief Initialize the CAN interface + * @param rx_queue Pointer to the CAN RX queue + * @param settings Pointer to the CAN settings + * @return STATUS_CODE_OK if initialization succeeded + * STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect + * STATUS_CODE_INTERNAL_ERROR if HAL initialization fails + */ +StatusCode can_transmit(); + +/** + * @brief Initialize the CAN interface + * @param rx_queue Pointer to the CAN RX queue + * @param settings Pointer to the CAN settings + * @return STATUS_CODE_OK if initialization succeeded + * STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect + * STATUS_CODE_INTERNAL_ERROR if HAL initialization fails + */ +StatusCode can_receive(); + +/** + * @brief Initialize the CAN interface + * @param rx_queue Pointer to the CAN RX queue + * @param settings Pointer to the CAN settings + * @return STATUS_CODE_OK if initialization succeeded + * STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect + * STATUS_CODE_INTERNAL_ERROR if HAL initialization fails + */ +StatusCode cache_can_tx(); + +/** + * @brief Initialize the CAN interface + * @param rx_queue Pointer to the CAN RX queue + * @param settings Pointer to the CAN settings + * @return STATUS_CODE_OK if initialization succeeded + * STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect + * STATUS_CODE_INTERNAL_ERROR if HAL initialization fails + */ +StatusCode cache_can_rx(); + +/** + * @brief Initialize the CAN interface + * @param rx_queue Pointer to the CAN RX queue + * @param settings Pointer to the CAN settings + * @return STATUS_CODE_OK if initialization succeeded + * STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect + * STATUS_CODE_INTERNAL_ERROR if HAL initialization fails + */ +StatusCode clear_rx_cache(); + +/** + * @brief Initialize the CAN interface + * @param rx_queue Pointer to the CAN RX queue + * @param settings Pointer to the CAN settings + * @return STATUS_CODE_OK if initialization succeeded + * STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect + * STATUS_CODE_INTERNAL_ERROR if HAL initialization fails + */ +StatusCode clear_tx_cache(); diff --git a/can/inc/can_hw.h b/can/inc/can_hw.h index 187bf0d..fa544b2 100644 --- a/can/inc/can_hw.h +++ b/can/inc/can_hw.h @@ -1,12 +1,12 @@ #pragma once /************************************************************************************************ - * can_hw.h + * @file can_hw.h * - * Header file for CAN HW Interface + * @brief Header file for CAN HW Interface * - * Created: 2024-11-03 - * Midnight Sun Team #24 - MSXVI + * @date 2024-11-03 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ @@ -20,8 +20,6 @@ /* Intra-component Headers */ #include "can_queue.h" -#include "gpio.h" -#include "uart_mcu.h" #ifdef CAN_HW_DEV_USE_CAN0 #define CAN_HW_DEV_INTERFACE "can0" @@ -43,12 +41,6 @@ typedef enum { NUM_CAN_HW_BITRATES } CanHwBitrate; -typedef enum { - CAN_CONTINUOUS = 0, - CAN_ONE_SHOT_MODE, - NUM_CAN_MODES -} CanMode; - typedef struct CanSettings { uint16_t device_id; CanHwBitrate bitrate; @@ -70,9 +62,9 @@ StatusCode can_hw_init(const CanQueue* rx_queue, const CanSettings *settings); /** * @brief Sets a filter on the CAN interface - * @param mask - * @param filter - * @param extended + * @param mask Determines which bits in the received ID are considered during filtering + * @param filter Specifies the pattern the CAN ID must adhere to + * @param extended Boolean to use CAN extended ID feature * @return STATUS_CODE_OK if initialization succeeded * STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect */ @@ -92,16 +84,19 @@ CanHwBusStatus can_hw_bus_status(void); * @param extended Boolean to use CAN extended ID feature * @param data Pointer to the data to transmit * @param len Size of the data to transfer - * @return STATUS_CODE_OK if data is retrieved succesfully + * @return STATUS_CODE_OK if data is transmitted succesfully + * STATUS_CODE_RESOURCE_EXHAUSTED if CAN mailbox is full + * STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect */ StatusCode can_hw_transmit(uint32_t id, bool extended, const uint8_t *data, size_t len); /** * @brief Receives CAN data from the bus - * @param id - * @param extended + * @param id Pointer to store the CAN ID received + * @param extended Pointer to a flag to indicate CAN extended ID feature * @param data Pointer to a buffer to store data - * @param len Number of CAN messages to retrieve in 8-bytes - * @return + * @param len Pointer to the number of CAN messages received + * @return true if data is retrieved succesfully + * false if one of the parameters are incorrect or internal error occurred */ bool can_hw_receive(uint32_t *id, bool *extended, uint64_t *data, size_t *len); diff --git a/can/inc/can_msg.h b/can/inc/can_msg.h index 49e8b75..62fe218 100644 --- a/can/inc/can_msg.h +++ b/can/inc/can_msg.h @@ -1,12 +1,12 @@ #pragma once /************************************************************************************************ - * can_msg.h + * @file can_msg.h * - * CAN message definitions + * @brief CAN message definitions * - * Created: 2024-11-02 - * Midnight Sun Team #24 - MSXVI + * @date 2024-11-02 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ @@ -37,7 +37,7 @@ typedef union CanId { CanMessageId raw; struct { uint16_t source_id : 4; - uint16_t type : 1; // Currently unused + uint16_t type : 1; uint32_t msg_id : 24; }; } CanId; diff --git a/can/inc/can_queue.h b/can/inc/can_queue.h index 92244ed..e7cfa0e 100644 --- a/can/inc/can_queue.h +++ b/can/inc/can_queue.h @@ -1,12 +1,12 @@ #pragma once /************************************************************************************************ - * can_queue.h + * @file can_queue.h * - * Wrappers for CAN queues to reduce instruction jumps + * @brief Wrappers for CAN queues to reduce instruction jumps * - * Created: 2024-11-02 - * Midnight Sun Team #24 - MSXVI + * @date 2024-11-02 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ diff --git a/can/src/arm/can_hw.c b/can/src/arm/can_hw.c index f4c13d2..a3ddccd 100644 --- a/can/src/arm/can_hw.c +++ b/can/src/arm/can_hw.c @@ -1,10 +1,10 @@ /************************************************************************************************ - * can_hw.c + * @file can_hw.c * - * Source file for CAN HW Interface + * @brief Source file for CAN HW Interface * - * Created: 2024-11-03 - * Midnight Sun Team #24 - MSXVI + * @date 2024-11-03 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ @@ -50,7 +50,7 @@ typedef struct CanHwTiming { } CanHwTiming; /* Generated settings using http://www.bittiming.can-wiki.info/ */ -/* For 80 MHz APB1 clock */ +/* For 80 MHz APB1 clock. CAN Sampling occurs at ~87.5% of the frame */ static CanHwTiming s_timing[NUM_CAN_HW_BITRATES] = { [CAN_HW_BITRATE_125KBPS] = { .prescaler = 40, .bs1 = CAN_BS1_12TQ, .bs2 = CAN_BS2_1TQ }, [CAN_HW_BITRATE_250KBPS] = { .prescaler = 20, .bs1 = CAN_BS1_12TQ, .bs2 = CAN_BS2_1TQ }, @@ -70,7 +70,7 @@ static bool s_tx_full = false; static int s_can_filter_en = 0; static uint32_t can_filters[CAN_HW_NUM_FILTER_BANKS]; -static void prv_add_filter_in(uint8_t filter_num, uint32_t mask, uint32_t filter) { +static void s_add_filter_in(uint8_t filter_num, uint32_t mask, uint32_t filter) { CAN_FilterTypeDef filter_cfg = { .FilterBank = filter_num, .FilterMode = CAN_FILTERMODE_IDMASK, @@ -88,6 +88,10 @@ static void prv_add_filter_in(uint8_t filter_num, uint32_t mask, uint32_t filter } StatusCode can_hw_init(const CanQueue *rx_queue, const CanSettings *settings) { + if (rx_queue == NULL || settings == NULL) { + return STATUS_CODE_INVALID_ARGS; + } + gpio_init_pin_af(&settings->tx, GPIO_ALTFN_PUSH_PULL, GPIO_ALT9_CAN1); gpio_init_pin_af(&settings->rx, GPIO_ALTFN_PUSH_PULL, GPIO_ALT9_CAN1); @@ -104,15 +108,15 @@ StatusCode can_hw_init(const CanQueue *rx_queue, const CanSettings *settings) { s_can_handle.Instance = CAN_HW_BASE; s_can_handle.Init.Prescaler = s_timing[settings->bitrate].prescaler; s_can_handle.Init.Mode = can_mode; - s_can_handle.Init.SyncJumpWidth = CAN_SJW_1TQ; - s_can_handle.Init.TimeSeg1 = s_timing[settings->bitrate].bs1; - s_can_handle.Init.TimeSeg2 = s_timing[settings->bitrate].bs2; - s_can_handle.Init.TimeTriggeredMode = DISABLE; - s_can_handle.Init.AutoBusOff = ENABLE; - s_can_handle.Init.AutoWakeUp = DISABLE; - s_can_handle.Init.AutoRetransmission = ENABLE; - s_can_handle.Init.ReceiveFifoLocked = DISABLE; - s_can_handle.Init.TransmitFifoPriority = DISABLE; + s_can_handle.Init.SyncJumpWidth = CAN_SJW_1TQ; /* Maximum time quantum jumps for resynchronization of bus nodes */ + s_can_handle.Init.TimeSeg1 = s_timing[settings->bitrate].bs1; /* Time permitted before sample point (Prop + Phase seg) */ + s_can_handle.Init.TimeSeg2 = s_timing[settings->bitrate].bs2; /* Time permitted after sample point */ + s_can_handle.Init.TimeTriggeredMode = DISABLE; /* Traditional CAN behaviour based on priority and arbitration */ + s_can_handle.Init.AutoBusOff = ENABLE; /* Auto error handling. Turns bus off when many errors detected */ + s_can_handle.Init.AutoWakeUp = DISABLE; /* Node stays in sleep until explicitly woken */ + s_can_handle.Init.AutoRetransmission = DISABLE; /* We use one-shot transmission since auto retransmit can cause timing issues */ + s_can_handle.Init.ReceiveFifoLocked = DISABLE; /* Ensures latest data is always available */ + s_can_handle.Init.TransmitFifoPriority = DISABLE; /* Message priority is driven off ID rather than order in FIFO */ if (HAL_CAN_Init(&s_can_handle) != HAL_OK) { return STATUS_CODE_INTERNAL_ERROR; @@ -130,7 +134,7 @@ StatusCode can_hw_init(const CanQueue *rx_queue, const CanSettings *settings) { interrupt_nvic_enable(CAN1_SCE_IRQn, INTERRUPT_PRIORITY_HIGH); /* Allow all messages by default, but reset the filter count so it's overwritten on the first filter */ - prv_add_filter_in(0, 0, 0); + s_add_filter_in(0, 0, 0); s_num_filters = 0; /* Initialize CAN queue */ @@ -165,7 +169,7 @@ StatusCode can_hw_add_filter_in(uint32_t mask, uint32_t filter, bool extended) { uint32_t mask_val = (mask << offset) | (1 << 2); uint32_t filter_val = (filter << offset) | ((uint32_t)extended << 2); - prv_add_filter_in(s_num_filters, mask_val, filter_val); + s_add_filter_in(s_num_filters, mask_val, filter_val); s_num_filters++; return STATUS_CODE_OK; } @@ -183,6 +187,15 @@ CanHwBusStatus can_hw_bus_status(void) { } StatusCode can_hw_transmit(uint32_t id, bool extended, const uint8_t *data, size_t len) { + if (data == NULL) { + return STATUS_CODE_INVALID_ARGS; + } + + if (len > 8U) { + return STATUS_CODE_INVALID_ARGS; + } + + CAN_TxHeaderTypeDef tx_header = { .StdId = id, .ExtId = id, @@ -210,6 +223,10 @@ StatusCode can_hw_transmit(uint32_t id, bool extended, const uint8_t *data, size } bool can_hw_receive(uint32_t *id, bool *extended, uint64_t *data, size_t *len) { + if (id == NULL || extended == NULL || data == NULL || len == NULL) { + return false; + } + CAN_RxHeaderTypeDef rx_header; uint8_t rx_data[8]; @@ -291,4 +308,5 @@ void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan) { void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan) { /* TODO: Add error notifications/handling */ + /* Aryan - Maybe reinitialize bus? */ } diff --git a/can/src/can.c b/can/src/can.c new file mode 100644 index 0000000..d946631 --- /dev/null +++ b/can/src/can.c @@ -0,0 +1,61 @@ +/************************************************************************************************ + * @file can.c + * + * @brief Source file for CAN Application code + * + * @date 2024-11-23 + * @author Midnight Sun Team #24 - MSXVI + ************************************************************************************************/ + +/* Standard library headers */ +#include +#include + +/* Inter-component Headers */ +#include "FreeRTOS.h" +#include "semphr.h" +#include "log.h" + +/* Intra-component Headers */ +#include "can_hw.h" +#include "can.h" + +// rx_struct g_rx_struct; +// tx_struct g_tx_struct; + +static CanStorage *s_can_storage; + +static StaticSemaphore_t s_can_tx_mutex; +static SemaphoreHandle_t s_can_tx_handle; + +static StaticSemaphore_t s_can_rx_mutex; +static SemaphoreHandle_t s_can_rx_handle; + +StatusCode can_init(CanStorage *storage, const CanSettings *settings) +{ + // if (settings->device_id >= CAN_MSG_MAX_DEVICES) { + // return STATUS_CODE_INVALID_ARGS; + // } + + s_can_storage = storage; + + memset(s_can_storage, 0, sizeof(*s_can_storage)); + s_can_storage->device_id = settings->device_id; + + // memset(&g_tx_struct, 0, sizeof(g_tx_struct)); + // memset(&g_rx_struct, 0, sizeof(g_rx_struct)); + + status_ok_or_return(can_queue_init(&s_can_storage->rx_queue)); + + /* Initialize CAN HW interface */ + status_ok_or_return(can_hw_init(&s_can_storage->rx_queue, settings)); + + s_can_rx_handle = xSemaphoreCreateMutexStatic(&s_can_rx_mutex); + s_can_tx_handle = xSemaphoreCreateMutexStatic(&s_can_tx_mutex); + + if (s_can_rx_handle == NULL || s_can_tx_handle == NULL) { + return STATUS_CODE_INTERNAL_ERROR; + } + + return STATUS_CODE_OK; +} \ No newline at end of file diff --git a/can/src/x86/can_hw.c b/can/src/x86/can_hw.c index 551d8ef..56fa9f2 100644 --- a/can/src/x86/can_hw.c +++ b/can/src/x86/can_hw.c @@ -1,10 +1,10 @@ /************************************************************************************************ - * can_hw.c + * @file can_hw.c * - * Source file for CAN HW Interface + * @brief Source file for CAN HW Interface * - * Created: 2024-11-03 - * Midnight Sun Team #24 - MSXVI + * @date 2024-11-03 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ diff --git a/libraries/core/inc/stm32l4xx_hal_conf.h b/libraries/core/inc/stm32l4xx_hal_conf.h index 4af3171..217f483 100644 --- a/libraries/core/inc/stm32l4xx_hal_conf.h +++ b/libraries/core/inc/stm32l4xx_hal_conf.h @@ -85,7 +85,6 @@ #define HAL_UART_MODULE_ENABLED /*#define HAL_USART_MODULE_ENABLED */ /*#define HAL_WWDG_MODULE_ENABLED */ -/*#define HAL_EXTI_MODULE_ENABLED */ /*#define HAL_PSSI_MODULE_ENABLED */ #define HAL_GPIO_MODULE_ENABLED #define HAL_EXTI_MODULE_ENABLED diff --git a/libraries/master/.placeholder b/libraries/master/.placeholder deleted file mode 100644 index e69de29..0000000 diff --git a/libraries/master/config.json b/libraries/master/config.json new file mode 100644 index 0000000..dd959fe --- /dev/null +++ b/libraries/master/config.json @@ -0,0 +1,7 @@ +{ + "libs": [ + "FreeRTOS", + "ms-common" + ] +} + \ No newline at end of file diff --git a/libraries/master/inc/master_tasks.h b/libraries/master/inc/master_tasks.h new file mode 100644 index 0000000..a7a3d28 --- /dev/null +++ b/libraries/master/inc/master_tasks.h @@ -0,0 +1,40 @@ +#pragma once + +/************************************************************************************************ + * @file master_tasks.h + * + * @brief Header file for Master Tasks API. Supports 1KHz, 100Hz and 10Hz scheduling + * + * @date 2024-11-04 + * @author Midnight Sun Team #24 - MSXVI + ************************************************************************************************/ + +/* Standard library headers */ + +/* Inter-component Headers */ +#include "log.h" +#include "tasks.h" + +/* Intra-component Headers */ + +#ifndef MASTER_TASK_1000HZ_SIZE +#define MASTER_TASK_1000HZ_SIZE (TASK_STACK_256) +#endif + +#ifndef MASTER_TASK_10HZ_SIZE +#define MASTER_TASK_10HZ_SIZE (TASK_STACK_256) +#endif + +#ifndef MASTER_TASK_1HZ_SIZE +#define MASTER_TASK_1HZ_SIZE (TASK_STACK_256) +#endif + +void run_1000hz_cycle(); +void run_10hz_cycle(); +void run_1hz_cycle(); +void pre_loop_init(); + +StatusCode init_master_tasks(); +Task *get_1000hz_task(); +Task *get_10hz_task(); +Task *get_1hz_task(); diff --git a/libraries/master/src/master_tasks.c b/libraries/master/src/master_tasks.c new file mode 100644 index 0000000..4ef28a7 --- /dev/null +++ b/libraries/master/src/master_tasks.c @@ -0,0 +1,95 @@ +/************************************************************************************************ + * @file master_tasks.c + * + * @brief Source file for Master Tasks API. Supports 1KHz, 100Hz and 10Hz scheduling + * + * @date 2024-11-04 + * @author Midnight Sun Team #24 - MSXVI + ************************************************************************************************/ + +/* Standard library headers */ + +/* Inter-component Headers */ + +/* Intra-component Headers */ +#include "master_tasks.h" + +#define MASTER_1000HZ_TO_MS 1U +#define MASTER_10HZ_TO_MS 100U +#define MASTER_1HZ_TO_MS 1000U + +#define MASTER_TASKS_PRIORITY (configMAX_PRIORITIES - 1U) +#define MAX_CYCLES_OVER 5 + +static uint8_t s_cycles_over = 0; + +void master_no_op() {} + +void run_1000hz_cycle() __attribute__((weak, alias("master_no_op"))); +void run_10hz_cycle() __attribute__((weak, alias("master_no_op"))); +void run_1hz_cycle() __attribute__((weak, alias("master_no_op"))); +void pre_loop_init() __attribute__((weak, alias("master_no_op"))); + +void check_late_cycle(Task *task, BaseType_t delay) { + if (delay != pdTRUE) { + ++s_cycles_over; + LOG_WARN("%s out of sync!! Currently over by %u cycles\n", task->name, s_cycles_over); + } else { + if (s_cycles_over != 0) { + --s_cycles_over; + } + } + if (s_cycles_over > MAX_CYCLES_OVER) { + LOG_CRITICAL("%s out of sync!! Ending Scheduler\n", task->name); + vTaskEndScheduler(); + } +} + +TASK(master_task_1000hz, MASTER_TASK_1000HZ_SIZE) { + TickType_t xLastWakeTime = xTaskGetTickCount(); + while (true) { + run_1000hz_cycle(); + BaseType_t delay = xTaskDelayUntil(&xLastWakeTime, pdMS_TO_TICKS(MASTER_1000HZ_TO_MS)); + check_late_cycle(master_task_1000hz, delay); + } +} + +TASK(master_task_10hz, MASTER_TASK_10HZ_SIZE) { + TickType_t xLastWakeTime = xTaskGetTickCount(); + while (true) { + run_10hz_cycle(); + BaseType_t delay = xTaskDelayUntil(&xLastWakeTime, pdMS_TO_TICKS(MASTER_10HZ_TO_MS)); + check_late_cycle(master_task_10hz, delay); + } +} + +TASK(master_task_1hz, MASTER_TASK_1HZ_SIZE) { + TickType_t xLastWakeTime = xTaskGetTickCount(); + while (true) { + run_1hz_cycle(); + BaseType_t delay = xTaskDelayUntil(&xLastWakeTime, pdMS_TO_TICKS(MASTER_1HZ_TO_MS)); + check_late_cycle(master_task_1hz, delay); + } +} + +StatusCode init_master_tasks() { + s_cycles_over = 0; + tasks_init_task(master_task_1000hz, MASTER_TASKS_PRIORITY, NULL); + tasks_init_task(master_task_10hz, MASTER_TASKS_PRIORITY - 1U, NULL); + tasks_init_task(master_task_1hz, MASTER_TASKS_PRIORITY - 2U, NULL); + + pre_loop_init(); + return STATUS_CODE_OK; +} + +Task *get_1000hz_task() { + return master_task_1000hz; +} + +Task *get_10hz_task() { + return master_task_10hz; +} + +Task *get_1hz_task() { + return master_task_1hz; +} diff --git a/libraries/master/test/test_master_tasks.c b/libraries/master/test/test_master_tasks.c new file mode 100644 index 0000000..02c1182 --- /dev/null +++ b/libraries/master/test/test_master_tasks.c @@ -0,0 +1,27 @@ +/************************************************************************************************ + * @file test_master_tasks.h + * + * @brief Test file for Master Tasks API. Supports 1Hz, 10Hz and 1000Hz scheduling + * + * @date 2024-11-04 + * @author Midnight Sun Team #24 - MSXVI + ************************************************************************************************/ + +/* Standard library headers */ +#include + +/* Inter-component Headers */ +#include "delay.h" +#include "log.h" +#include "master_tasks.h" +#include "tasks.h" +#include "test_helpers.h" +#include "unity.h" + +/* Intra-component Headers */ + +void setup_test(void) { + log_init(); +} + +void teardown_test(void) {} diff --git a/libraries/ms-common/inc/arm/gpio_mcu.h b/libraries/ms-common/inc/arm/gpio_mcu.h index 28b654c..06d3691 100644 --- a/libraries/ms-common/inc/arm/gpio_mcu.h +++ b/libraries/ms-common/inc/arm/gpio_mcu.h @@ -1,12 +1,12 @@ #pragma once /************************************************************************************************ - * gpio_mcu.h + * @file gpio_mcu.h * - * Header file for MCU specific GPIO library + * @brief Header file for MCU specific GPIO library * - * Created: 2024-11-02 - * Midnight Sun Team #24 - MSXVI + * @date 2024-11-02 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ diff --git a/libraries/ms-common/inc/arm/pwm_mcu.h b/libraries/ms-common/inc/arm/pwm_mcu.h index 5dd39bb..61579db 100644 --- a/libraries/ms-common/inc/arm/pwm_mcu.h +++ b/libraries/ms-common/inc/arm/pwm_mcu.h @@ -1,12 +1,12 @@ #pragma once /************************************************************************************************ - * pwm_mcu.h + * @file pwm_mcu.h * - * Header file for MCU specific PWM library + * @brief Header file for MCU specific PWM library * - * Created: 2024-11-02 - * Midnight Sun Team #24 - MSXVI + * @date 2024-11-02 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ diff --git a/libraries/ms-common/inc/arm/uart_mcu.h b/libraries/ms-common/inc/arm/uart_mcu.h index 34288e7..4702352 100644 --- a/libraries/ms-common/inc/arm/uart_mcu.h +++ b/libraries/ms-common/inc/arm/uart_mcu.h @@ -1,12 +1,12 @@ #pragma once /************************************************************************************************ - * uart_mcu.h + * @file uart_mcu.h * - * Header file for MCU specific UART library + * @brief Header file for MCU specific UART library * - * Created: 2024-11-02 - * Midnight Sun Team #24 - MSXVI + * @date 2024-11-02 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ diff --git a/libraries/ms-common/inc/delay.h b/libraries/ms-common/inc/delay.h new file mode 100644 index 0000000..93e1057 --- /dev/null +++ b/libraries/ms-common/inc/delay.h @@ -0,0 +1,35 @@ +#pragma once + +/************************************************************************************************ + * @file delay.h + * + * @brief Header file for the delay library + * + * @date 2024-10-30 + * @author Midnight Sun Team #24 - MSXVI + ************************************************************************************************/ + +/* Standard library headers */ +#include + +/* Inter-component Headers */ + +/* Intra-component Headers */ + +/** + * @brief Blocking delay for some amount of time in milliseconds + * @param time_ms Amount of time to delay for + */ +void delay_ms(uint32_t time_ms); + +/** + * @brief Non-blocking delay for some amount of time in milliseconds + * @param time_ms Amount of time to delay for + */ +void non_blocking_delay_ms(uint32_t time_ms); + +/** + * @brief Blocking delay for some amount of time in seconds + * @param time_ms Amount of time to delay for + */ +#define delay_s(time) delay_ms((time)*1000) diff --git a/libraries/ms-common/inc/flash.h b/libraries/ms-common/inc/flash.h new file mode 100644 index 0000000..3c75782 --- /dev/null +++ b/libraries/ms-common/inc/flash.h @@ -0,0 +1,61 @@ +#pragma once + +/************************************************************************************************ + * @file flash.h + * + * @brief Flash Library Header file + * + * @date 2024-11-05 + * @author Midnight Sun Team #24 - MSXVI + ************************************************************************************************/ + +/* Standard library headers */ +#include + +/* Inter-component Headers */ + +/* Intra-component Headers */ +#include "status.h" + +#define FLASH_MEMORY_ALIGNMENT 4U + +#define FLASH_BASE_ADDR (0x08000000U) + +#define FLASH_PAGE_TO_ADDR(page) (FLASH_BASE_ADDR + ((page)*FLASH_PAGE_SIZE)) +#define FLASH_ADDR_TO_PAGE(addr) (((addr)-FLASH_BASE_ADDR) / FLASH_PAGE_SIZE) + +#define NUM_FLASH_PAGES 128U + +/** + * @brief Read from the flash memory and store data into a buffer + * @param address Memory address to read from. This value MUST be 4-byte aligned + * @param buffer Pointer to the buffer to store data + * @param buffer_len Length of the buffer. This value MUST be 4-byte aligned + * @return STATUS_CODE_OK if flash memory was read succesfully + * STATUS_CODE_OUT_OF_RANGE if address is out of range + * STATUS_CODE_INVALID_ARGS if address or read bytes is not aligned + */ +StatusCode flash_read(uintptr_t address, uint8_t *buffer, size_t buffer_len); + +/** + * @brief Write a buffer of data into the flash memory + * @details This does not rewrite the flash memory. Once written, the data must be cleared + * by erasing the entire page + * @param address Memory address to store the buffer. This value MUST be 4-byte aligned + * @param buffer Pointer to the buffer of data to store + * @param buffer_len Length of the buffer to write. This MUST also be 4-byte aligned + * @return STATUS_CODE_OK if flash memory was written succesfully + * STATUS_CODE_OUT_OF_RANGE if address is out of range + * STATUS_CODE_INTERNAL_ERROR if flash write failed + */ +StatusCode flash_write(uintptr_t address, uint8_t *buffer, size_t buffer_len); + +/** + * @brief Erase pages of flash memory + * @param start_page First page to erase + * @param num_pages Number of pages to erase + * @return STATUS_CODE_OK if flash memory was erased succesfully + * STATUS_CODE_INVALID_ARGS if incorrect arguments were passed in + * STATUS_CODE_INTERNAL_ERROR if flash erase failed + */ +StatusCode flash_erase(uint8_t start_page, uint8_t num_pages); diff --git a/libraries/ms-common/inc/fsm.h b/libraries/ms-common/inc/fsm.h index e3ac908..2eb03b0 100644 --- a/libraries/ms-common/inc/fsm.h +++ b/libraries/ms-common/inc/fsm.h @@ -1,12 +1,12 @@ #pragma once /************************************************************************************************ - * fsm.h + * @file fsm.h * - * Finite State Machine Library + * @brief Finite State Machine Library * - * Created: 2024-10-27 - * Midnight Sun Team #24 - MSXVI + * @date 2024-10-27 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ diff --git a/libraries/ms-common/inc/gpio.h b/libraries/ms-common/inc/gpio.h index 0e32b80..d749ce1 100644 --- a/libraries/ms-common/inc/gpio.h +++ b/libraries/ms-common/inc/gpio.h @@ -1,12 +1,12 @@ #pragma once /************************************************************************************************ - * gpio.h + * @file gpio.h * - * GPIO Library Header file + * @brief GPIO Library Header file * - * Created: 2024-11-02 - * Midnight Sun Team #24 - MSXVI + * @date 2024-11-02 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ diff --git a/libraries/ms-common/inc/gpio_interrupts.h b/libraries/ms-common/inc/gpio_interrupts.h new file mode 100644 index 0000000..c3c95ce --- /dev/null +++ b/libraries/ms-common/inc/gpio_interrupts.h @@ -0,0 +1,40 @@ +#pragma once + +/************************************************************************************************ + * @file gpio_interrupts.h + * + * @brief GPIO Interrupts Library Header file + * + * @date 2024-11-05 + * @author Midnight Sun Team #24 - MSXVI + ************************************************************************************************/ + +/* Standard library headers */ + +/* Inter-component Headers */ + +/* Intra-component Headers */ +#include "gpio.h" +#include "interrupts.h" +#include "notify.h" +#include "status.h" +#include "tasks.h" + +/** + * @brief Register a GPIO interrupt by passing in the pin address and task to notify + * @param address Pointer to the GPIO address + * @param settings Pointer to the interrupt settings + * @param event Event notification + * @param task Task to notify when interrupt fires + * @return STATUS_CODE_OK if interrupt initialization succeeded + * STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect + * STATUS_CODE_RESOURCE_EXHAUSTED if the pin is already being used + */ +StatusCode gpio_register_interrupt(const GpioAddress *address, const InterruptSettings *settings, + const Event event, const Task *task); + +/** + * @brief Software generated GPIO interrupt + * @param address Pointer to the GPIO address + */ +StatusCode gpio_trigger_interrupt(const GpioAddress *address); diff --git a/libraries/ms-common/inc/interrupts.h b/libraries/ms-common/inc/interrupts.h index 3285a81..8e6b765 100644 --- a/libraries/ms-common/inc/interrupts.h +++ b/libraries/ms-common/inc/interrupts.h @@ -1,12 +1,12 @@ #pragma once /************************************************************************************************ - * interrupts.h + * @file interrupts.h * - * Interrupts Library Header file + * @brief Interrupts Library Header file * - * Created: 2024-10-27 - * Midnight Sun Team #24 - MSXVI + * @date 2024-10-27 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ @@ -74,12 +74,12 @@ typedef struct InterruptSettings { * @details If called multiple times the subsequent attempts will clear everything * resulting in needing to re-initialize all interrupts */ -void stm32f10x_interrupt_init(void); +void interrupt_init(void); /** * @brief Enables the nested interrupt vector controller for a given channel - * @param irq_channel - * @param priority + * @param irq_channel Numeric ID of the interrupt channel from the NVIC + * @param priority Priority level of the interrupt * @return STATUS_CODE_OK if the channel is succesfully initialized * STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect */ @@ -87,8 +87,8 @@ StatusCode interrupt_nvic_enable(uint8_t irq_channel, InterruptPriority priority /** * @brief Enables an external interrupt line with the given settings - * @param address - * @param settings + * @param address Pointer to the GPIO address + * @param settings Pointer to the interrupt settings * @return STATUS_CODE_OK if the channel is succesfully initialized * STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect */ @@ -96,7 +96,7 @@ StatusCode interrupt_exti_enable(GpioAddress *address, const InterruptSettings * /** * @brief Triggers a software interrupt on a given external interrupt channel - * @param line + * @param line Numeric ID of the EXTI line (GPIO Pin number) * @return STATUS_CODE_OK if the channel is succesfully initialized * STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect */ @@ -104,8 +104,8 @@ StatusCode interrupt_exti_trigger(uint8_t line); /** * @brief Get the pending flag for an external interrupt - * @param line - * @param pending_bit + * @param line Numeric ID of the EXTI line (GPIO Pin number) + * @param pending_bit Pointer to a variable that is updated with the pending bit * @return STATUS_CODE_OK if the channel is succesfully initialized * STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect */ @@ -113,7 +113,7 @@ StatusCode interrupt_exti_get_pending(uint8_t line, uint8_t *pending_bit); /** * @brief Clears the pending flag for an external interrupt - * @param line + * @param line Numeric ID of the EXTI line (GPIO Pin number) * @return STATUS_CODE_OK if the channel is succesfully initialized * STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect */ @@ -121,8 +121,8 @@ StatusCode interrupt_exti_clear_pending(uint8_t line); /** * @brief Masks or clears the external interrupt on a given line - * @param line - * @param masked + * @param line Numeric ID of the EXTI line (GPIO Pin number) + * @param masked 0: Clears the interrupt 1: Mask the lines interrupt * @return STATUS_CODE_OK if the channel is succesfully initialized * STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect */ diff --git a/libraries/ms-common/inc/log.h b/libraries/ms-common/inc/log.h index 6aa5874..65aa9da 100644 --- a/libraries/ms-common/inc/log.h +++ b/libraries/ms-common/inc/log.h @@ -1,12 +1,12 @@ #pragma once /************************************************************************************************ - * log.h + * @file log.h * - * Header file for the logging library used to debug all modules + * @brief Header file for the logging library used to debug all modules * - * Created: 2024-11-02 - * Midnight Sun Team #24 - MSXVI + * @date 2024-11-02 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ diff --git a/libraries/ms-common/inc/mcu.h b/libraries/ms-common/inc/mcu.h index 349ac75..07a1410 100644 --- a/libraries/ms-common/inc/mcu.h +++ b/libraries/ms-common/inc/mcu.h @@ -1,12 +1,12 @@ #pragma once /************************************************************************************************ - * mcu.h + * @file mcu.h * - * Header file for MCU intialization + * @brief Header file for MCU intialization * - * Created: 2024-10-27 - * Midnight Sun Team #24 - MSXVI + * @date 2024-10-27 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ diff --git a/libraries/ms-common/inc/misc.h b/libraries/ms-common/inc/misc.h index d0d2717..06b4389 100644 --- a/libraries/ms-common/inc/misc.h +++ b/libraries/ms-common/inc/misc.h @@ -1,12 +1,12 @@ #pragma once /************************************************************************************************ - * misc.h + * @file misc.h * - * Common helper functions + * @brief Common helper functions * - * Created: 2024-10-27 - * Midnight Sun Team #24 - MSXVI + * @date 2024-10-27 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ diff --git a/libraries/ms-common/inc/mpu.h b/libraries/ms-common/inc/mpu.h index 27cf1d3..cd3a2ba 100644 --- a/libraries/ms-common/inc/mpu.h +++ b/libraries/ms-common/inc/mpu.h @@ -1,16 +1,17 @@ #pragma once /************************************************************************************************ - * mpu.h + * @file mpu.h * - * MPU Library Header file + * @brief MPU Library Header file * - * Created: 2024-11-03 - * Midnight Sun Team #24 - MSXVI + * @date 2024-11-03 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ #include +#include /* Inter-component Headers */ @@ -25,7 +26,8 @@ typedef enum { MPU_REGION_4, MPU_REGION_5, MPU_REGION_6, - MPU_REGION_7 + MPU_REGION_7, + NUM_MPU_REGIONS } MPURegionNumber; typedef enum { @@ -38,17 +40,17 @@ typedef enum { } MPUAccessPerm; typedef struct { - uint8_t enable; - uint8_t number; + bool enable; + MPURegionNumber number; uint32_t base_address; - uint8_t size; + uint32_t size; } MPURegion; typedef struct { - uint8_t access_permission; - uint8_t disable_exec; - uint8_t is_cacheable; - uint8_t is_bufferable; + MPUAccessPerm access_permission; + bool disable_code_exec; + bool is_cacheable; + bool is_bufferable; } MPURegionSettings; /** @@ -56,7 +58,7 @@ typedef struct { * @param region Pointer to which MPU region to configure * @param settings Pointer to MPU region configuration settings * @return STATUS_CODE_OK if MPU initialization succeeded - * @return STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect + * STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect */ StatusCode mpu_configure_region(MPURegion *region, MPURegionSettings *settings); @@ -64,15 +66,17 @@ StatusCode mpu_configure_region(MPURegion *region, MPURegionSettings *settings); * @brief Enables given region * @param region_number Number of MPU region to enable * @return STATUS_CODE_OK if mpu region was successfully enabled + * STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect */ -StatusCode mpu_enable_region(uint32_t region_number); +StatusCode mpu_enable_region(MPURegionNumber region_number); /** * @brief Disables given region * @param region_number Number of MPU region to disable * @return STATUS_CODE_OK if mpu region was successfully disabled + * STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect */ -StatusCode mpu_disable_region(uint32_t region_number); +StatusCode mpu_disable_region(MPURegionNumber region_number); /** * @brief Enables or Disables MPU with default memory access diff --git a/libraries/ms-common/inc/notify.h b/libraries/ms-common/inc/notify.h new file mode 100644 index 0000000..3d05d03 --- /dev/null +++ b/libraries/ms-common/inc/notify.h @@ -0,0 +1,77 @@ +#pragma once + +/************************************************************************************************ + * @file notify.h + * + * @brief Notify Library Header file + * + * @date 2024-11-05 + * @author Midnight Sun Team #24 - MSXVI + ************************************************************************************************/ + +/* Standard library headers */ + +/* Inter-component Headers */ +#include "FreeRTOS.h" + +/* Intra-component Headers */ +#include "status.h" +#include "tasks.h" + +/* Maximum block amount */ +#define BLOCK_INDEFINITELY UINT16_MAX + +/* Invalid event ID */ +#define INVALID_EVENT 32 + +/* 8 bit value that represents a bit in the notification */ +typedef uint8_t Event; + +/** + * @brief Get the highest priority event available and clears it + * @details Call this function in a while loop to clear all events + * @param notification Notification value that is cleared and returned + * @param event Event value that is updated to the highest priority + * @return STATUS_CODE_OK if all events are processed (notification = 0) + * STATUS_CODE_INCOPMLETE if some events are not cleared + */ +StatusCode event_from_notification(uint32_t *notification, Event *event); + +/** + * @brief Checks if the notification is available in the event + * @param notification Pointer to the notification to + * @return True if the notification exists. False if it does not + */ +bool notify_check_event(uint32_t *notification, Event event); + +/** + * @brief Get the current notification value for the calilng task without a timeout + * @param notification Pointer to a notification value that is updated upon success + * @return STATUS_CODE_OK if the value is retrieved succesfully + * STATUS_CODE_TIMEOUT if the value cannot be retrieved + */ +StatusCode notify_get(uint32_t *notification); + +/** + * @brief Get the current notification value for the calilng task with a maximum timeout + * @param notification Pointer to a notification value that is polled + * @param ms_to_wait Time in milliseconds to wait for a notification before timing out + * @return STATUS_CODE_OK if notification is received succesfully + * STATUS_CODE_TIMEOUT if a timeout has occurred + */ +StatusCode notify_wait(uint32_t *notification, uint32_t ms_to_wait); + +/** + * @brief Notify a specific task with an event + * @param task Pointer to the task to notify + * @param event Numeric value used to signal task + * @return STATUS_CODE_OK + */ +StatusCode notify(Task *task, Event event); + +/** + * @brief Sends an event notification from an ISR to a task + * @param task Pointer to the task to notify + * @param event Numeric value used to signal task + */ +void notify_from_isr(Task *task, Event event); diff --git a/libraries/ms-common/inc/queues.h b/libraries/ms-common/inc/queues.h index c37ba44..27d723f 100644 --- a/libraries/ms-common/inc/queues.h +++ b/libraries/ms-common/inc/queues.h @@ -1,12 +1,12 @@ #pragma once /************************************************************************************************ - * queues.h + * @file queues.h * - * Header file for the queue library + * @brief Header file for the queue library * - * Created: 2024-11-02 - * Midnight Sun Team #24 - MSXVI + * @date 2024-11-02 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ diff --git a/libraries/ms-common/inc/semaphore.h b/libraries/ms-common/inc/semaphore.h index 6f487ec..1b47359 100644 --- a/libraries/ms-common/inc/semaphore.h +++ b/libraries/ms-common/inc/semaphore.h @@ -1,12 +1,12 @@ #pragma once /************************************************************************************************ - * semaphore.h + * @file semaphore.h * - * Header file for the semaphore library + * @brief Header file for the semaphore library * - * Created: 2024-10-27 - * Midnight Sun Team #24 - MSXVI + * @date 2024-10-27 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ diff --git a/libraries/ms-common/inc/status.h b/libraries/ms-common/inc/status.h index b816a7a..de68bdf 100644 --- a/libraries/ms-common/inc/status.h +++ b/libraries/ms-common/inc/status.h @@ -1,12 +1,12 @@ #pragma once /************************************************************************************************ - * status.h + * @file status.h * - * Status Library for more verbose error handling + * @brief Status Library for more verbose error handling * - * Created: 2024-10-27 - * Midnight Sun Team #24 - MSXVI + * @date 2024-10-27 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ diff --git a/libraries/ms-common/inc/tasks.h b/libraries/ms-common/inc/tasks.h index 5c80a94..c50b5aa 100644 --- a/libraries/ms-common/inc/tasks.h +++ b/libraries/ms-common/inc/tasks.h @@ -1,12 +1,12 @@ #pragma once /************************************************************************************************ - * tasks.h + * @file tasks.h * - * Header file for the RTOS tasks wrapper + * @brief Header file for the RTOS tasks wrapper * - * Created: 2024-10-27 - * Midnight Sun Team #24 - MSXVI + * @date 2024-10-27 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ @@ -24,7 +24,7 @@ /** * @brief Define a task function. This should go in a source file (.c). * @details The generated function has the following signature: - * void _prv_your_task_function(void *context) + * void _s_your_task_function(void *context) * where context is the context pointer passed to tasks_init_task. * @param task_name is the name of your task, which should match any previous DECLARE_TASK * declarations. @@ -32,17 +32,17 @@ */ #define TASK(task_name, task_stack_size) \ /* forward declaration so we can reference it in the Task */ \ - static void _prv_task_impl_##task_name(void *); \ + static void _s_task_impl_##task_name(void *); \ static StackType_t _s_stack_##task_name[task_stack_size]; \ /* use a compound literal so users can use it as a pointer */ \ Task *task_name = &((Task){ \ - .task_func = _prv_task_impl_##task_name, \ + .task_func = _s_task_impl_##task_name, \ .name = #task_name, \ .stack = _s_stack_##task_name, \ .stack_size = task_stack_size, \ .handle = NULL, /* will be initialized by tasks_init_task */ \ }); \ - static void _prv_task_impl_##task_name(void *context) + static void _s_task_impl_##task_name(void *context) /** * @brief Maximum amount of RTOS tasks supported at a time diff --git a/libraries/ms-common/inc/test_helpers.h b/libraries/ms-common/inc/test_helpers.h index fc79916..a289877 100644 --- a/libraries/ms-common/inc/test_helpers.h +++ b/libraries/ms-common/inc/test_helpers.h @@ -1,12 +1,12 @@ #pragma once /************************************************************************************************ - * test_helpers.h + * @file test_helpers.h * - * Unity test framework helper library + * @brief Unity test framework helper library * - * Created: 2024-10-27 - * Midnight Sun Team #24 - MSXVI + * @date 2024-10-27 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ // Test helper functions. These should only ever be called within a file in the diff --git a/libraries/ms-common/inc/uart.h b/libraries/ms-common/inc/uart.h index cfb95cc..13e6eef 100644 --- a/libraries/ms-common/inc/uart.h +++ b/libraries/ms-common/inc/uart.h @@ -1,12 +1,12 @@ #pragma once /************************************************************************************************ - * uart.h + * @file uart.h * - * UART Library Header file + * @brief UART Library Header file * - * Created: 2024-11-02 - * Midnight Sun Team #24 - MSXVI + * @date 2024-11-02 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ @@ -18,7 +18,8 @@ #include "gpio.h" #include "uart_mcu.h" -#define UART_MAX_BUFFER_LEN 256 +#define UART_MAX_BUFFER_LEN 256U +#define UART_TIMEOUT_MS 10U typedef enum { UART_FLOW_CONTROL_NONE, @@ -41,6 +42,7 @@ typedef struct { * @return STATUS_CODE_OK if initialization succeeded * STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect * STATUS_CODE_INTERNAL_ERROR if HAL initialization fails + * STATUS_CODE_RESOURCE_EXHAUSTED if already initialized */ StatusCode uart_init(UartPort uart, UartSettings *settings); @@ -53,6 +55,8 @@ StatusCode uart_init(UartPort uart, UartSettings *settings); * @param len Length of data to receive * @return STATUS_CODE_OK if initialization succeeded * STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect + * STATUS_CODE_INTERNAL_ERROR if HAL receiving fails + * STATUS_CODE_TIMEOUT if receiving takes too long */ StatusCode uart_rx(UartPort uart, uint8_t *data, size_t len); @@ -65,5 +69,7 @@ StatusCode uart_rx(UartPort uart, uint8_t *data, size_t len); * @param len Length of data to receive * @return STATUS_CODE_OK if initialization succeeded * STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect + * STATUS_CODE_INTERNAL_ERROR if HAL transmission fails + * STATUS_CODE_TIMEOUT if transmission takes too long */ StatusCode uart_tx(UartPort uart, uint8_t *data, size_t len); diff --git a/libraries/ms-common/inc/x86/gpio_mcu.h b/libraries/ms-common/inc/x86/gpio_mcu.h index 2f67169..d739dc9 100644 --- a/libraries/ms-common/inc/x86/gpio_mcu.h +++ b/libraries/ms-common/inc/x86/gpio_mcu.h @@ -1,12 +1,12 @@ #pragma once /************************************************************************************************ - * gpio_mcu.h + * @file gpio_mcu.h * - * Header file for MCU specific GPIO library + * @brief Header file for MCU specific GPIO library * - * Created: 2024-11-02 - * Midnight Sun Team #24 - MSXVI + * @date 2024-11-02 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ diff --git a/libraries/ms-common/inc/x86/pwm_mcu.h b/libraries/ms-common/inc/x86/pwm_mcu.h index 5dd39bb..61579db 100644 --- a/libraries/ms-common/inc/x86/pwm_mcu.h +++ b/libraries/ms-common/inc/x86/pwm_mcu.h @@ -1,12 +1,12 @@ #pragma once /************************************************************************************************ - * pwm_mcu.h + * @file pwm_mcu.h * - * Header file for MCU specific PWM library + * @brief Header file for MCU specific PWM library * - * Created: 2024-11-02 - * Midnight Sun Team #24 - MSXVI + * @date 2024-11-02 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ diff --git a/libraries/ms-common/inc/x86/uart_mcu.h b/libraries/ms-common/inc/x86/uart_mcu.h index 34288e7..4702352 100644 --- a/libraries/ms-common/inc/x86/uart_mcu.h +++ b/libraries/ms-common/inc/x86/uart_mcu.h @@ -1,12 +1,12 @@ #pragma once /************************************************************************************************ - * uart_mcu.h + * @file uart_mcu.h * - * Header file for MCU specific UART library + * @brief Header file for MCU specific UART library * - * Created: 2024-11-02 - * Midnight Sun Team #24 - MSXVI + * @date 2024-11-02 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ diff --git a/libraries/ms-common/src/arm/flash.c b/libraries/ms-common/src/arm/flash.c new file mode 100644 index 0000000..092efe6 --- /dev/null +++ b/libraries/ms-common/src/arm/flash.c @@ -0,0 +1,106 @@ +/************************************************************************************************ + * @file flash.c + * + * @brief Flash Library Source file + * + * @date 2024-11-05 + * @author Midnight Sun Team #24 - MSXVI + ************************************************************************************************/ + +/* Standard library headers */ +#include + +/* Inter-component Headers */ +#include "stm32l433xx.h" +#include "stm32l4xx_hal_flash.h" + +/* Intra-component Headers */ +#include "flash.h" +#include "misc.h" + +/* Symbols from linker-scripts */ +extern uint32_t _flash_start; +extern uint32_t _flash_size; +extern uint32_t _flash_page_size; +extern uint32_t _bootloader_start; +extern uint32_t _application_start; +extern uint32_t _bootloader_size; +extern uint32_t _application_size; + +static StatusCode s_validate_address(uintptr_t address, size_t size) { + /* Check memory alignment */ + if (address % FLASH_MEMORY_ALIGNMENT != 0U || size % FLASH_MEMORY_ALIGNMENT != 0U) { + return STATUS_CODE_INVALID_ARGS; + } + + /* Ensure adderss is in range */ + uint32_t flash_end = (uint32_t)&_flash_start + (uint32_t)&_flash_size; + if (address < (uint32_t)&_flash_start || address + size > flash_end) { + return STATUS_CODE_OUT_OF_RANGE; + } + + return STATUS_CODE_OK; +} + +StatusCode flash_read(uintptr_t address, uint8_t *buffer, size_t buffer_len) { + if (buffer == NULL) { + return STATUS_CODE_INVALID_ARGS; + } + + status_ok_or_return(s_validate_address(address, buffer_len)); + + /* Direct memory read */ + memcpy(buffer, (void *)address, buffer_len); + return STATUS_CODE_OK; +} + +StatusCode flash_write(uintptr_t address, uint8_t *buffer, size_t buffer_len) { + if (buffer == NULL) { + return STATUS_CODE_INVALID_ARGS; + } + + status_ok_or_return(s_validate_address(address, buffer_len)); + + HAL_FLASH_Unlock(); + __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS); + + /* Program the flash double word by double word (64-bits) */ + for (size_t i = 0U; i < buffer_len; i += 8U) { + uint64_t data = 0U; + memcpy(&data, &buffer[i], MIN(8U, buffer_len - i)); + + if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, address + i, data) != HAL_OK) { + HAL_FLASH_Lock(); + return STATUS_CODE_INTERNAL_ERROR; + } + } + + HAL_FLASH_Lock(); + return STATUS_CODE_OK; +} + +StatusCode flash_erase(uint8_t start_page, uint8_t num_pages) { + if (start_page >= NUM_FLASH_PAGES || num_pages == 0U || + start_page + num_pages > NUM_FLASH_PAGES) { + return STATUS_CODE_INVALID_ARGS; + } + + FLASH_EraseInitTypeDef erase_init = { .TypeErase = FLASH_TYPEERASE_PAGES, + .Banks = FLASH_BANK_1, + .Page = start_page, + .NbPages = num_pages }; + + uint32_t page_error = 0U; + HAL_StatusTypeDef status; + + HAL_FLASH_Unlock(); + __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS); + status = HAL_FLASHEx_Erase(&erase_init, &page_error); + HAL_FLASH_Lock(); + + if (status != HAL_OK) { + return STATUS_CODE_INTERNAL_ERROR; + } + + return STATUS_CODE_OK; +} diff --git a/libraries/ms-common/src/arm/gpio.c b/libraries/ms-common/src/arm/gpio.c index fcc9340..0c302ea 100644 --- a/libraries/ms-common/src/arm/gpio.c +++ b/libraries/ms-common/src/arm/gpio.c @@ -1,10 +1,10 @@ /************************************************************************************************ - * gpio.c + * @file gpio.c * - * GPIO Library Source Code + * @brief GPIO Library Source Code * - * Created: 2024-11-02 - * Midnight Sun Team #24 - MSXVI + * @date 2024-11-02 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ @@ -18,6 +18,7 @@ /* Intra-component Headers */ #include "gpio.h" +#include "interrupts.h" #include "status.h" static const uint32_t s_gpio_mode_map[] = { @@ -130,6 +131,44 @@ StatusCode gpio_init_pin_af(const GpioAddress *address, const GpioMode pin_mode, return STATUS_CODE_OK; } +StatusCode gpio_it_init(const GpioAddress *address, const InterruptSettings *settings) { + if (address == NULL || settings == NULL) { + return STATUS_CODE_INVALID_ARGS; + } + taskENTER_CRITICAL(); + GPIO_InitTypeDef init = { 0 }; + init.Pin = 1U << (address->pin); + + switch (settings->edge) { + case INTERRUPT_EDGE_RISING: + init.Mode = GPIO_MODE_IT_RISING; + break; + case INTERRUPT_EDGE_FALLING: + init.Mode = GPIO_MODE_IT_FALLING; + break; + case INTERRUPT_EDGE_RISING_FALLING: + init.Mode = GPIO_MODE_IT_RISING_FALLING; + break; + default: + return STATUS_CODE_INVALID_ARGS; + } + + init.Pull = GPIO_NOPULL; + init.Speed = GPIO_SPEED_FREQ_HIGH; + GPIO_TypeDef *gpio_port = + (GPIO_TypeDef *)(AHB2PERIPH_BASE + (address->port * GPIO_ADDRESS_OFFSET)); + + /* Initialize the GPIO pin for interrupts */ + HAL_GPIO_Init(gpio_port, &init); + + /* Initialize the EXTI line to handle interrupts */ + interrupt_exti_enable(address, settings); + + taskEXIT_CRITICAL(); + + return STATUS_CODE_OK; +} + StatusCode gpio_set_state(const GpioAddress *address, GpioState state) { if (address == NULL) { return STATUS_CODE_INVALID_ARGS; diff --git a/libraries/ms-common/src/arm/gpio_interrupts.c b/libraries/ms-common/src/arm/gpio_interrupts.c new file mode 100644 index 0000000..c601e0d --- /dev/null +++ b/libraries/ms-common/src/arm/gpio_interrupts.c @@ -0,0 +1,97 @@ +/************************************************************************************************ + * @file gpio_interrupts.c + * + * @brief GPIO Interrupts Library Source Code + * + * @date 2024-11-05 + * @author Midnight Sun Team #24 - MSXVI + ************************************************************************************************/ + +/* Standard library headers */ + +/* Inter-component Headers */ + +/* Intra-component Headers */ +#include "gpio_interrupts.h" + +#include "log.h" + +typedef struct GpioInterrupt { + InterruptSettings settings; + GpioAddress address; + Event event; + Task *task; +} GpioInterrupt; + +static GpioInterrupt s_gpio_it_interrupts[GPIO_PINS_PER_PORT] = { 0 }; + +StatusCode gpio_register_interrupt(const GpioAddress *address, const InterruptSettings *settings, + const Event event, const Task *task) { + if (address->port >= NUM_GPIO_PORTS || address->pin >= GPIO_PINS_PER_PORT || + event >= INVALID_EVENT) { + return STATUS_CODE_INVALID_ARGS; + } else if (s_gpio_it_interrupts[address->pin].task != NULL) { + LOG_DEBUG("GPIO INTERRUPT INIT FAILED. Pin already being used"); + return STATUS_CODE_RESOURCE_EXHAUSTED; + } + + // Register exti channel and enable interrupt + status_ok_or_return(interrupt_exti_enable(address, settings)); + + s_gpio_it_interrupts[address->pin].address = *address; + s_gpio_it_interrupts[address->pin].settings = *settings; + s_gpio_it_interrupts[address->pin].event = event; + s_gpio_it_interrupts[address->pin].task = task; + + return STATUS_CODE_OK; +} + +StatusCode gpio_trigger_interrupt(const GpioAddress *address) { + if (address->port >= NUM_GPIO_PORTS || address->pin >= GPIO_PINS_PER_PORT) { + return STATUS_CODE_INVALID_ARGS; + } + + return interrupt_exti_trigger(address->pin); +} + +static void s_gpio_callbacks(uint8_t lower_bound, uint8_t upper_bound) { + uint8_t pending = 0; + for (int i = lower_bound; i <= upper_bound; i++) { + interrupt_exti_get_pending(i, &pending); + if (pending && s_gpio_it_interrupts[i].task != NULL) { + notify_from_isr(s_gpio_it_interrupts[i].task, s_gpio_it_interrupts[i].event); + } + interrupt_exti_clear_pending(i); + } +} + +/** + * @brief Interrupt handlers for EXTI 1-15 + */ +void EXTI0_IRQHandler(void) { + s_gpio_callbacks(0U, 0U); +} + +void EXTI1_IRQHandler(void) { + s_gpio_callbacks(1U, 1U); +} + +void EXTI2_IRQHandler(void) { + s_gpio_callbacks(2U, 2U); +} + +void EXTI3_IRQHandler(void) { + s_gpio_callbacks(3U, 3U); +} + +void EXTI4_IRQHandler(void) { + s_gpio_callbacks(4U, 4U); +} + +void EXTI9_5_IRQHandler(void) { + s_gpio_callbacks(5U, 9U); +} + +void EXTI15_10_IRQHandler(void) { + s_gpio_callbacks(10U, 15U); +} diff --git a/libraries/ms-common/src/arm/interrupts.c b/libraries/ms-common/src/arm/interrupts.c index b573c3a..5e3f254 100644 --- a/libraries/ms-common/src/arm/interrupts.c +++ b/libraries/ms-common/src/arm/interrupts.c @@ -1,22 +1,32 @@ /************************************************************************************************ - * interrupts.c + * @file interrupts.c * - * Interrupts Library Source file + * @brief Interrupts Library Source file * - * Created: 2024-11-03 - * Midnight Sun Team #24 - MSXVI + * @date 2024-11-03 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ +#include /* Inter-component Headers */ +#include "stm32l433xx.h" #include "stm32l4xx_hal_cortex.h" #include "stm32l4xx_hal_exti.h" /* Intra-component Headers */ #include "interrupts.h" -void interrupt_init(void) {} +static EXTI_HandleTypeDef s_exti_handles[16]; + +void interrupt_init(void) { + HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4); + + for (int i = 0; i < 16; i++) { + memset(&s_exti_handles[i], 0, sizeof(EXTI_HandleTypeDef)); + } +} StatusCode interrupt_nvic_enable(uint8_t irq_channel, InterruptPriority priority) { if (priority >= NUM_INTERRUPT_PRIORITIES || irq_channel >= NUM_STM32L433X_INTERRUPT_CHANNELS) { @@ -33,6 +43,34 @@ StatusCode interrupt_exti_enable(GpioAddress *address, const InterruptSettings * return STATUS_CODE_INVALID_ARGS; } + EXTI_ConfigTypeDef init = { 0 }; + + /* The line will correspond to the pin. This means A2 and B2 + * will have the same interrupt line + */ + init.Line = 1U << (address->pin); + init.Mode = EXTI_MODE_INTERRUPT; + + switch (settings->edge) { + case INTERRUPT_EDGE_RISING: + init.Trigger = EXTI_TRIGGER_RISING; + break; + case INTERRUPT_EDGE_FALLING: + init.Trigger = EXTI_TRIGGER_FALLING; + break; + case INTERRUPT_EDGE_RISING_FALLING: + init.Trigger = EXTI_TRIGGER_RISING_FALLING; + break; + default: + return STATUS_CODE_INVALID_ARGS; + } + + init.GPIOSel = address->port; + + if (HAL_EXTI_SetConfigLine(&s_exti_handles[address->pin], &init) != HAL_OK) { + return STATUS_CODE_INTERNAL_ERROR; + } + return STATUS_CODE_OK; } @@ -40,15 +78,15 @@ StatusCode interrupt_exti_trigger(uint8_t line) { if (line > NUM_STM32L433X_EXTI_LINES) { return STATUS_CODE_INVALID_ARGS; } - + HAL_EXTI_GenerateSWI(&s_exti_handles[line]); return STATUS_CODE_OK; } StatusCode interrupt_exti_get_pending(uint8_t line, uint8_t *pending_bit) { - if (line >= NUM_STM32L433X_INTERRUPT_CHANNELS) { + if (line >= NUM_STM32L433X_INTERRUPT_CHANNELS || pending_bit == NULL) { return STATUS_CODE_INVALID_ARGS; } - + *pending_bit = HAL_EXTI_GetPending(&s_exti_handles[line], EXTI_TRIGGER_RISING_FALLING); return STATUS_CODE_OK; } @@ -56,7 +94,7 @@ StatusCode interrupt_exti_clear_pending(uint8_t line) { if (line >= NUM_STM32L433X_INTERRUPT_CHANNELS) { return STATUS_CODE_INVALID_ARGS; } - + HAL_EXTI_ClearPending(&s_exti_handles[line], (EXTI_GPIO | EXTI_REG1 | EXTI_EVENT | line)); return STATUS_CODE_OK; } @@ -65,5 +103,11 @@ StatusCode interrupt_exti_mask_set(uint8_t line, bool masked) { return STATUS_CODE_INVALID_ARGS; } + if (masked) { + EXTI->IMR1 &= ~(1U << line); + } else { + EXTI->IMR1 |= (1U << line); + } + return STATUS_CODE_OK; } diff --git a/libraries/ms-common/src/arm/mcu.c b/libraries/ms-common/src/arm/mcu.c index 3d09c00..fbcdb88 100644 --- a/libraries/ms-common/src/arm/mcu.c +++ b/libraries/ms-common/src/arm/mcu.c @@ -1,10 +1,10 @@ /************************************************************************************************ - * mcu.c + * @file mcu.c * - * Source code for MCU intialization + * @brief Source code for MCU intialization * - * Created: 2024-10-27 - * Midnight Sun Team #24 - MSXVI + * @date 2024-10-27 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ diff --git a/libraries/ms-common/src/arm/mpu.c b/libraries/ms-common/src/arm/mpu.c index 5bb5ffc..2d7db40 100644 --- a/libraries/ms-common/src/arm/mpu.c +++ b/libraries/ms-common/src/arm/mpu.c @@ -1,10 +1,10 @@ /************************************************************************************************ - * mpu.c + * @file mpu.c * - * MPU Library Source code + * @brief MPU Library Source code * - * Created: 2024-11-03 - * Midnight Sun Team #24 - MSXVI + * @date 2024-11-03 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ @@ -15,8 +15,47 @@ /* Intra-component Headers */ #include "mpu.h" +#define MPU_REGION_MIN_SIZE 32U +#define IS_POWER_OF_TWO(x) (((x) != 0) && (((x) & ((x)-1U)) == 0)) + +static StatusCode s_validate_region_settings(MPURegion *region, MPURegionSettings *settings) { + /* Validate region number */ + if (region->number >= NUM_MPU_REGIONS) { + return STATUS_CODE_INVALID_ARGS; + } + + /* Validate alignment */ + if (region->base_address & (region->size - 1U)) { + return STATUS_CODE_INVALID_ARGS; + } + + /* Validate region size (must be power of 2 and >= 32 bytes) */ + if (!IS_POWER_OF_TWO(region->size) || region->size < MPU_REGION_MIN_SIZE) { + return STATUS_CODE_INVALID_ARGS; + } + + /* Validate access permissions */ + switch (settings->access_permission) { + case MPU_REGION_NO_ACCESS: + case MPU_REGION_PRIV_RW: + case MPU_REGION_PRIV_RW_URO: + case MPU_REGION_FULL_ACCESS: + case MPU_REGION_PRIV_RO: + case MPU_REGION_PRIV_RO_URO: + break; + default: + return STATUS_CODE_INVALID_ARGS; + } + + return STATUS_CODE_OK; +} + StatusCode mpu_configure_region(MPURegion *region, MPURegionSettings *settings) { - if ((region == NULL) || (settings == NULL)) return STATUS_CODE_INVALID_ARGS; + if ((region == NULL) || (settings == NULL)) { + return STATUS_CODE_INVALID_ARGS; + } + + status_ok_or_return(s_validate_region_settings(region, settings)); MPU_Region_InitTypeDef init; @@ -24,11 +63,11 @@ StatusCode mpu_configure_region(MPURegion *region, MPURegionSettings *settings) init.Number = region->number; init.BaseAddress = region->base_address; init.Size = region->size; - init.SubRegionDisable = 0x00; - init.TypeExtField = 0x00; + init.SubRegionDisable = 0x00U; + init.TypeExtField = 0x00U; init.AccessPermission = settings->access_permission; - init.DisableExec = settings->disable_exec; - init.IsShareable = 0x00; + init.DisableExec = settings->disable_code_exec; + init.IsShareable = 0x00U; init.IsCacheable = settings->is_cacheable; init.IsBufferable = settings->is_bufferable; @@ -38,13 +77,21 @@ StatusCode mpu_configure_region(MPURegion *region, MPURegionSettings *settings) return STATUS_CODE_OK; } -StatusCode mpu_enable_region(uint32_t region_number) { +StatusCode mpu_enable_region(MPURegionNumber region_number) { + if (region_number >= NUM_MPU_REGIONS) { + return STATUS_CODE_INVALID_ARGS; + } + HAL_MPU_EnableRegion(region_number); return STATUS_CODE_OK; } -StatusCode mpu_disable_region(uint32_t region_number) { +StatusCode mpu_disable_region(MPURegionNumber region_number) { + if (region_number >= NUM_MPU_REGIONS) { + return STATUS_CODE_INVALID_ARGS; + } + HAL_MPU_DisableRegion(region_number); return STATUS_CODE_OK; diff --git a/libraries/ms-common/src/arm/uart.c b/libraries/ms-common/src/arm/uart.c index 8503c77..0951d19 100644 --- a/libraries/ms-common/src/arm/uart.c +++ b/libraries/ms-common/src/arm/uart.c @@ -1,15 +1,17 @@ /************************************************************************************************ - * uart.c + * @file uart.c * - * UART Library Source Code + * @brief UART Library Source Code * - * Created: 2024-11-02 - * Midnight Sun Team #24 - MSXVI + * @date 2024-11-02 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ /* Inter-component Headers */ +#include "FreeRTOS.h" +#include "semphr.h" #include "stm32l433xx.h" #include "stm32l4xx_hal_conf.h" #include "stm32l4xx_hal_rcc.h" @@ -23,10 +25,10 @@ #include "uart.h" #include "uart_mcu.h" -static inline void prv_enable_usart1(void) { +static inline void s_enable_usart1(void) { __HAL_RCC_USART1_CLK_ENABLE(); } -static inline void prv_enable_usart2(void) { +static inline void s_enable_usart2(void) { __HAL_RCC_USART2_CLK_ENABLE(); } @@ -37,66 +39,79 @@ static const uint16_t s_uart_flow_control_map[] = { [UART_FLOW_CONTROL_RTS_CTS] = UART_HWCONTROL_RTS_CTS, }; -typedef struct UartPortQueue { - Queue rx_queue; - Queue tx_queue; - uint8_t rx_buf[UART_MAX_BUFFER_LEN]; - uint8_t tx_buf[UART_MAX_BUFFER_LEN]; -} UartPortQueue; - typedef struct { USART_TypeDef *base; void (*rcc_cmd)(void); uint8_t irq; + bool initialized; } UartPortData; static UartPortData s_port[] = { - [UART_PORT_1] = { .rcc_cmd = prv_enable_usart1, .irq = USART1_IRQn, .base = USART1 }, - [UART_PORT_2] = { .rcc_cmd = prv_enable_usart2, .irq = USART2_IRQn, .base = USART2 }, + [UART_PORT_1] = { .rcc_cmd = s_enable_usart1, + .irq = USART1_IRQn, + .base = USART1, + .initialized = false }, + [UART_PORT_2] = { .rcc_cmd = s_enable_usart2, + .irq = USART2_IRQn, + .base = USART2, + .initialized = false }, }; static UART_HandleTypeDef s_uart_handles[NUM_UART_PORTS]; -static UartPortQueue s_port_queues[NUM_UART_PORTS]; -static uint8_t rx_bytes[NUM_UART_PORTS]; -void USART1_IRQHandler(void) { - HAL_UART_IRQHandler(&s_uart_handles[UART_PORT_1]); -} +/* Mutex for port access */ +static StaticSemaphore_t s_uart_port_mutex[NUM_UART_PORTS]; +static SemaphoreHandle_t s_uart_port_handle[NUM_UART_PORTS]; -void USART2_IRQHandler(void) { - HAL_UART_IRQHandler(&s_uart_handles[UART_PORT_2]); -} +/* Semaphore to signal event complete */ +static StaticSemaphore_t s_uart_cmplt_sem[NUM_UART_PORTS]; +static SemaphoreHandle_t s_uart_cmplt_handle[NUM_UART_PORTS]; -/* Callback functions for HAL UART TX */ -void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) { - UartPort uart = NUM_UART_PORTS; - for (UartPort i = 0; i < NUM_UART_PORTS; i++) { - if (&s_uart_handles[i] == huart) { - uart = i; - } +/* Private helper for common TX/RX operations */ +static StatusCode s_uart_transfer(UartPort uart, uint8_t *data, size_t len, bool is_rx) { + if (data == NULL || uart >= NUM_UART_PORTS || len > UART_MAX_BUFFER_LEN) { + return STATUS_CODE_INVALID_ARGS; } - if (uart >= NUM_UART_PORTS) { - return; + if (!s_port[uart].initialized) { + return STATUS_CODE_UNINITIALIZED; } - uint8_t tx_byte; + /* Take the mutex for this uart port */ + if (xSemaphoreTake(s_uart_port_handle[uart], pdMS_TO_TICKS(UART_TIMEOUT_MS)) != pdTRUE) { + return STATUS_CODE_TIMEOUT; + } - if (xQueueReceiveFromISR(s_port_queues[uart].tx_queue.handle, &tx_byte, pdFALSE) == pdFALSE) { - /* If there is no more data */ - __HAL_UART_DISABLE_IT(huart, UART_IT_TXE); + HAL_StatusTypeDef status; + if (is_rx) { + status = HAL_UART_Receive_IT(&s_uart_handles[uart], data, len); } else { - HAL_UART_Transmit_IT(huart, &tx_byte, 1); + status = HAL_UART_Transmit_IT(&s_uart_handles[uart], data, len); } - __HAL_UART_CLEAR_IT(&s_uart_handles[uart], UART_IT_TXE); + + if (status != HAL_OK) { + xSemaphoreGive(s_uart_port_handle[uart]); + return STATUS_CODE_INTERNAL_ERROR; + } + + if (xSemaphoreTake(s_uart_cmplt_handle[uart], pdMS_TO_TICKS(UART_TIMEOUT_MS)) != pdTRUE) { + xSemaphoreGive(s_uart_port_handle[uart]); + return STATUS_CODE_TIMEOUT; + } + + xSemaphoreGive(s_uart_port_handle[uart]); + return STATUS_CODE_OK; } -/* Callback functions for HAL UART RX */ -void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { +/* Private helper to handle transfer complete */ +static void s_uart_transfer_complete_callback(UART_HandleTypeDef *huart, bool is_rx) { + BaseType_t higher_priority_task = pdFALSE; UartPort uart = NUM_UART_PORTS; + for (UartPort i = 0; i < NUM_UART_PORTS; i++) { if (&s_uart_handles[i] == huart) { uart = i; + break; } } @@ -104,17 +119,36 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { return; } - uint8_t *rx_byte = &rx_bytes[uart]; + __HAL_UART_CLEAR_IT(huart, is_rx ? UART_IT_RXNE : UART_IT_TXE); - HAL_UART_Receive_IT(huart, rx_byte, 1); - if (xQueueSendFromISR(s_port_queues[uart].rx_queue.handle, &rx_byte, pdFALSE) == errQUEUE_FULL) { - /* Drop oldest data if queue is full */ - uint8_t buf = 0; - xQueueReceiveFromISR(s_port_queues[uart].rx_queue.handle, &buf, pdFALSE); - xQueueSendFromISR(s_port_queues[uart].rx_queue.handle, rx_byte, pdFALSE); - } + xSemaphoreGiveFromISR(s_uart_cmplt_handle[uart], &higher_priority_task); + portYIELD_FROM_ISR(higher_priority_task); +} - __HAL_UART_CLEAR_IT(&s_uart_handles[uart], UART_IT_RXNE); +void USART1_IRQHandler(void) { + HAL_UART_IRQHandler(&s_uart_handles[UART_PORT_1]); +} + +void USART2_IRQHandler(void) { + HAL_UART_IRQHandler(&s_uart_handles[UART_PORT_2]); +} + +/* Callback functions for HAL UART TX */ +void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) { + s_uart_transfer_complete_callback(huart, false); +} + +/* Callback functions for HAL UART RX */ +void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { + s_uart_transfer_complete_callback(huart, true); +} + +StatusCode uart_rx(UartPort uart, uint8_t *data, size_t len) { + return s_uart_transfer(uart, data, len, true); +} + +StatusCode uart_tx(UartPort uart, uint8_t *data, size_t len) { + return s_uart_transfer(uart, data, len, false); } StatusCode uart_init(UartPort uart, UartSettings *settings) { @@ -126,15 +160,16 @@ StatusCode uart_init(UartPort uart, UartSettings *settings) { return STATUS_CODE_INVALID_ARGS; } - s_port_queues[uart].tx_queue.item_size = sizeof(uint8_t); - s_port_queues[uart].tx_queue.num_items = UART_MAX_BUFFER_LEN; - s_port_queues[uart].tx_queue.storage_buf = s_port_queues[uart].tx_buf; - queue_init(&s_port_queues[uart].tx_queue); + if (s_port[uart].initialized) { + return STATUS_CODE_RESOURCE_EXHAUSTED; + } - s_port_queues[uart].rx_queue.item_size = sizeof(uint8_t); - s_port_queues[uart].rx_queue.num_items = UART_MAX_BUFFER_LEN; - s_port_queues[uart].rx_queue.storage_buf = s_port_queues[uart].rx_buf; - queue_init(&s_port_queues[uart].rx_queue); + s_uart_port_handle[uart] = xSemaphoreCreateMutexStatic(&s_uart_port_mutex[uart]); + s_uart_cmplt_handle[uart] = xSemaphoreCreateBinaryStatic(&s_uart_cmplt_sem[uart]); + + if (s_uart_port_handle[uart] == NULL || s_uart_cmplt_handle[uart] == NULL) { + return STATUS_CODE_INTERNAL_ERROR; + } gpio_init_pin_af(&settings->tx, GPIO_ALTFN_PUSH_PULL, GPIO_ALT7_USART1); gpio_init_pin_af(&settings->rx, GPIO_ALTFN_PUSH_PULL, GPIO_ALT7_USART1); @@ -162,50 +197,7 @@ StatusCode uart_init(UartPort uart, UartSettings *settings) { /* Initialize interrupts */ interrupt_nvic_enable(s_port[uart].irq, INTERRUPT_PRIORITY_HIGH); - HAL_UART_Receive_IT(&s_uart_handles[uart], &rx_bytes[uart], 1); - - return STATUS_CODE_OK; -} - -StatusCode uart_rx(UartPort uart, uint8_t *data, size_t len) { - if (data == NULL || uart >= NUM_UART_PORTS) { - return STATUS_CODE_INVALID_ARGS; - } - - if (len > (size_t)queue_get_spaces_available(&s_port_queues[uart].tx_queue)) { - return STATUS_CODE_RESOURCE_EXHAUSTED; - } - - for (uint8_t i = 0; i < len; i++) { - if (queue_receive(&s_port_queues[uart].tx_queue, &data[i], 0) != STATUS_CODE_OK) { - return STATUS_CODE_INCOMPLETE; - } - } + s_port[uart].initialized = true; return STATUS_CODE_OK; } - -StatusCode uart_tx(UartPort uart, uint8_t *data, size_t len) { - if (data == NULL || uart >= NUM_UART_PORTS) { - return STATUS_CODE_INVALID_ARGS; - } - - if (len > (size_t)queue_get_spaces_available(&s_port_queues[uart].tx_queue)) { - return STATUS_CODE_RESOURCE_EXHAUSTED; - } - - StatusCode status = STATUS_CODE_OK; - - for (uint8_t i = 0; i < len; i++) { - if (queue_send(&s_port_queues[uart].tx_queue, &data[i], 0) != STATUS_CODE_OK) { - status = STATUS_CODE_INCOMPLETE; - break; - } - } - - if (status == STATUS_CODE_OK) { - __HAL_UART_ENABLE_IT(&s_uart_handles[uart], UART_IT_TXE); - } - - return status; -} diff --git a/libraries/ms-common/src/delay.c b/libraries/ms-common/src/delay.c new file mode 100644 index 0000000..f866ce2 --- /dev/null +++ b/libraries/ms-common/src/delay.c @@ -0,0 +1,31 @@ +/************************************************************************************************ + * @file delay.c + * + * @brief Source code for the delay library + * + * @date 2024-10-30 + * @author Midnight Sun Team #24 - MSXVI + ************************************************************************************************/ + +/* Standard library headers */ + +/* Inter-component Headers */ +#include "FreeRTOS.h" +#include "task.h" + +/* Intra-component Headers */ +#include "delay.h" +#include "log.h" + +void delay_ms(uint32_t time_ms) { + if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) { + LOG_WARN("Scheduler is not running."); + } + vTaskDelay(time_ms); +} + +void non_blocking_delay_ms(uint32_t time_ms) { + TickType_t time_ticks = pdMS_TO_TICKS(time_ms) + xTaskGetTickCount(); + while (xTaskGetTickCount() < time_ticks) { + } +} diff --git a/libraries/ms-common/src/fsm.c b/libraries/ms-common/src/fsm.c index 1a11f26..3e0d0c2 100644 --- a/libraries/ms-common/src/fsm.c +++ b/libraries/ms-common/src/fsm.c @@ -1,10 +1,10 @@ /************************************************************************************************ - * tasks.c + * @file tasks.c * - * Source code for the RTOS tasks wrapper + * @brief Source code for the RTOS tasks wrapper * - * Created: 2024-10-30 - * Midnight Sun Team #24 - MSXVI + * @date 2024-10-30 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ diff --git a/libraries/ms-common/src/log.c b/libraries/ms-common/src/log.c index 4c89ef8..18f190e 100644 --- a/libraries/ms-common/src/log.c +++ b/libraries/ms-common/src/log.c @@ -1,10 +1,10 @@ /************************************************************************************************ - * log.c + * @file log.c * - * Source code for the logging library used to debug all modules + * @brief Source code for the logging library used to debug all modules * - * Created: 2024-11-02 - * Midnight Sun Team #24 - MSXVI + * @date 2024-11-02 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ diff --git a/libraries/ms-common/src/notify.c b/libraries/ms-common/src/notify.c new file mode 100644 index 0000000..f18b98c --- /dev/null +++ b/libraries/ms-common/src/notify.c @@ -0,0 +1,88 @@ +/************************************************************************************************ + * @file notify.h + * + * @brief Notify Library Header file + * + * @date 2024-11-05 + * @author Midnight Sun Team #24 - MSXVI + ************************************************************************************************/ + +/* Standard library headers */ + +/* Inter-component Headers */ + +/* Intra-component Headers */ +#include "notify.h" + +#include "log.h" + +StatusCode event_from_notification(uint32_t *notification, Event *event) { + if (notification == NULL) { + *event = INVALID_EVENT; + return STATUS_CODE_INVALID_ARGS; + } + if (*notification == 0) { + *event = INVALID_EVENT; + return STATUS_CODE_OK; + } + /* Get index of first event */ + *event = 31 - __builtin_clz(*notification); + /* Clear bit */ + *notification = *notification & ~(1u << *event); + + return STATUS_CODE_INCOMPLETE; +} + +bool notify_check_event(uint32_t *notification, Event event) { + if (!notification || event >= INVALID_EVENT) { + LOG_DEBUG("Provided bad event\n"); + return false; + } + // Check event bit in notification + if ((1U << event) & *notification) { + *notification &= ~(1U << event); + return true; + } else { + return false; + } +} + +StatusCode notify_get(uint32_t *notification) { + return notify_wait(notification, 0U); +} + +StatusCode notify_wait(uint32_t *notification, uint32_t ms_to_wait) { + TickType_t ticks_to_wait = 0U; + if (ms_to_wait == BLOCK_INDEFINITELY) { + ticks_to_wait = portMAX_DELAY; + } else { + ticks_to_wait = pdMS_TO_TICKS(ms_to_wait); + } + /* Block until notification arrives */ + BaseType_t result = xTaskNotifyWait(0, UINT32_MAX, notification, ticks_to_wait); + if (result) { + return STATUS_CODE_OK; + } else { + return STATUS_CODE_TIMEOUT; + } +} + +StatusCode notify(Task *task, Event event) { + if (event >= INVALID_EVENT) { + return STATUS_CODE_INVALID_ARGS; + } + + BaseType_t result = xTaskNotify(task->handle, 1U << event, eSetBits); + + if (result) { + return STATUS_CODE_OK; + } else { + return STATUS_CODE_UNKNOWN; + } +} + +void notify_from_isr(Task *task, Event event) { + BaseType_t task_woken = 0U; + xTaskNotifyFromISR(task->handle, 1U << event, eSetBits, &task_woken); + portYIELD_FROM_ISR(task_woken); +} diff --git a/libraries/ms-common/src/queues.c b/libraries/ms-common/src/queues.c index bbc446c..b1cb415 100644 --- a/libraries/ms-common/src/queues.c +++ b/libraries/ms-common/src/queues.c @@ -1,10 +1,10 @@ /************************************************************************************************ - * queues.c + * @file queues.c * - * Source code for the queue library + * @brief Source code for the queue library * - * Created: 2024-11-02 - * Midnight Sun Team #24 - MSXVI + * @date 2024-11-02 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ diff --git a/libraries/ms-common/src/semaphore.c b/libraries/ms-common/src/semaphore.c index da43ee6..567b7dc 100644 --- a/libraries/ms-common/src/semaphore.c +++ b/libraries/ms-common/src/semaphore.c @@ -1,10 +1,10 @@ /************************************************************************************************ - * semaphore.c + * @file semaphore.c * - * Source code for the semaphore library + * @brief Source code for the semaphore library * - * Created: 2024-10-30 - * Midnight Sun Team #24 - MSXVI + * @date 2024-10-30 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ diff --git a/libraries/ms-common/src/tasks.c b/libraries/ms-common/src/tasks.c index 3ff53ae..88f2db8 100644 --- a/libraries/ms-common/src/tasks.c +++ b/libraries/ms-common/src/tasks.c @@ -1,10 +1,10 @@ /************************************************************************************************ - * tasks.c + * @file tasks.c * - * Source code for the RTOS tasks wrapper + * @brief Source code for the RTOS tasks wrapper * - * Created: 2024-10-30 - * Midnight Sun Team #24 - MSXVI + * @date 2024-10-30 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ @@ -22,7 +22,7 @@ static StaticSemaphore_t s_end_task_sem; static SemaphoreHandle_t s_end_task_handle = NULL; // Add any setup or teardown that needs to be done for every task here. -static void prv_task(void *params) { +static void s_task(void *params) { Task *task = params; if (task == NULL) { // guard just in case, error should have been caught previously LOG_CRITICAL("CRITICAL: Tried to start null task!\n"); @@ -65,7 +65,7 @@ StatusCode tasks_init_task(Task *task, TaskPriority priority, void *context) { } task->context = context; - task->handle = xTaskCreateStatic(prv_task, task->name, task->stack_size, task, priority, + task->handle = xTaskCreateStatic(s_task, task->name, task->stack_size, task, priority, task->stack, &task->tcb); if (task->handle == NULL) { LOG_CRITICAL("Failed to create Task %s\n", task->name); diff --git a/libraries/ms-common/src/x86/flash.c b/libraries/ms-common/src/x86/flash.c new file mode 100644 index 0000000..6069803 --- /dev/null +++ b/libraries/ms-common/src/x86/flash.c @@ -0,0 +1,14 @@ +/************************************************************************************************ + * @file flash.c + * + * @brief Flash Library Source file + * + * @date 2024-11-05 + * @author Midnight Sun Team #24 - MSXVI + ************************************************************************************************/ + +/* Standard library headers */ + +/* Inter-component Headers */ + +/* Intra-component Headers */ diff --git a/libraries/ms-common/src/x86/gpio.c b/libraries/ms-common/src/x86/gpio.c index 1572c1e..c27948e 100644 --- a/libraries/ms-common/src/x86/gpio.c +++ b/libraries/ms-common/src/x86/gpio.c @@ -1,16 +1,19 @@ /************************************************************************************************ - * gpio.c + * @file gpio.c * - * GPIO Library Source Code + * @brief GPIO Library Source Code * - * Created: 2024-11-02 - * Midnight Sun Team #24 - MSXVI + * @date 2024-11-02 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ /* Inter-component Headers */ +#include "FreeRTOS.h" +#include "log.h" #include "status.h" +#include "task.h" /* Intra-component Headers */ #include "gpio.h" @@ -18,26 +21,26 @@ static GpioMode s_gpio_pin_modes[GPIO_TOTAL_PINS]; static uint8_t s_gpio_pin_state[GPIO_TOTAL_PINS]; -static GpioMode s_gpio_alt_functions[GPIO_TOTAL_PINS]; +static GpioAlternateFunctions s_gpio_alt_functions[GPIO_TOTAL_PINS]; StatusCode gpio_init(void) { - for (uint32_t k = 0; k < GPIO_TOTAL_PINS; ++k) { - s_gpio_pin_state[k] = GPIO_STATE_LOW; - s_gpio_alt_functions[k] = GPIO_ALT_NONE; + for (uint32_t i = 0U; i < GPIO_TOTAL_PINS; ++i) { + s_gpio_pin_modes[i] = NUM_GPIO_MODES; + s_gpio_pin_state[i] = NUM_GPIO_STATES; + s_gpio_alt_functions[i] = GPIO_ALT_NONE; } return STATUS_CODE_OK; } StatusCode gpio_init_pin(const GpioAddress *address, const GpioMode pin_mode, GpioState init_state) { - taskENTER_CRITICAL(); - if (address->port >= NUM_GPIO_PORTS || address->pin >= GPIO_PINS_PER_PORT || pin_mode >= NUM_GPIO_MODES) { - taskEXIT_CRITICAL(); - return status_code(STATUS_CODE_INVALID_ARGS); + return STATUS_CODE_INVALID_ARGS; } + taskENTER_CRITICAL(); + uint32_t index = address->port * (uint32_t)GPIO_PINS_PER_PORT + address->pin; s_gpio_pin_modes[index] = pin_mode; @@ -50,13 +53,13 @@ StatusCode gpio_init_pin(const GpioAddress *address, const GpioMode pin_mode, StatusCode gpio_init_pin_af(const GpioAddress *address, const GpioMode pin_mode, GpioAlternateFunctions alt_func) { - taskENTER_CRITICAL(); if (address->port >= NUM_GPIO_PORTS || address->pin >= GPIO_PINS_PER_PORT || pin_mode >= NUM_GPIO_MODES) { - taskEXIT_CRITICAL(); - return status_code(STATUS_CODE_INVALID_ARGS); + return STATUS_CODE_INVALID_ARGS; } + taskENTER_CRITICAL(); + uint32_t index = address->port * (uint32_t)GPIO_PINS_PER_PORT + address->pin; s_gpio_pin_modes[index] = pin_mode; @@ -72,17 +75,18 @@ StatusCode gpio_init_pin_af(const GpioAddress *address, const GpioMode pin_mode, StatusCode gpio_set_state(const GpioAddress *address, GpioState state) { if (address->port >= NUM_GPIO_PORTS || address->pin >= GPIO_PINS_PER_PORT) { - taskEXIT_CRITICAL(); - return status_code(STATUS_CODE_INVALID_ARGS); + return STATUS_CODE_INVALID_ARGS; } + taskENTER_CRITICAL(); + uint32_t index = address->port * (uint32_t)GPIO_PINS_PER_PORT + address->pin; GpioMode mode = s_gpio_pin_modes[index]; if (mode != GPIO_OUTPUT_OPEN_DRAIN && mode != GPIO_OUTPUT_PUSH_PULL) { LOG_WARN("Attempting to set an input pin, check your configuration"); taskEXIT_CRITICAL(); - return status_code(STATUS_CODE_INVALID_ARGS); + return STATUS_CODE_INVALID_ARGS; } s_gpio_pin_state[index] = state; @@ -96,11 +100,10 @@ StatusCode gpio_set_state(const GpioAddress *address, GpioState state) { } StatusCode gpio_toggle_state(const GpioAddress *address) { - taskENTER_CRITICAL(); if (address->port >= NUM_GPIO_PORTS || address->pin >= GPIO_PINS_PER_PORT) { - taskEXIT_CRITICAL(); - return status_code(STATUS_CODE_INVALID_ARGS); + return STATUS_CODE_INVALID_ARGS; } + taskENTER_CRITICAL(); uint32_t index = address->port * (uint32_t)GPIO_PINS_PER_PORT + address->pin; @@ -119,16 +122,13 @@ StatusCode gpio_toggle_state(const GpioAddress *address) { } GpioState gpio_get_state(const GpioAddress *address) { - taskENTER_CRITCAL(); if (address->port >= NUM_GPIO_PORTS || address->pin >= GPIO_PINS_PER_PORT) { - taskEXIT_CRITICAL(); - return status_code(STATUS_CODE_INVALID_ARGS); - - if (address->port < NUM_GPIO_PORTS || address->pin < GPIO_PINS_PER_PORT) { - uint32_t index = address->port * (uint32_t)GPIO_PINS_PER_PORT + address->pin; - taskEXIT_CRITICAL(); - return s_gpio_pin_state[index]; - } - taskEXIT_CRITICAL(); return GPIO_STATE_LOW; } + + taskENTER_CRITICAL(); + uint32_t index = address->port * (uint32_t)GPIO_PINS_PER_PORT + address->pin; + taskEXIT_CRITICAL(); + + return s_gpio_pin_state[index]; +} diff --git a/libraries/ms-common/src/x86/gpio_interrupts.c b/libraries/ms-common/src/x86/gpio_interrupts.c new file mode 100644 index 0000000..676ba6e --- /dev/null +++ b/libraries/ms-common/src/x86/gpio_interrupts.c @@ -0,0 +1,24 @@ +/************************************************************************************************ + * @file gpio_interrupts.c + * + * @brief GPIO Interrupts Library Source file + * + * @date 2024-11-05 + * @author Midnight Sun Team #24 - MSXVI + ************************************************************************************************/ + +/* Standard library headers */ + +/* Inter-component Headers */ + +/* Intra-component Headers */ +#include "gpio_interrupts.h" + +StatusCode gpio_register_interrupt(const GpioAddress *address, const InterruptSettings *settings, + const Event event, const Task *task) { + return STATUS_CODE_UNIMPLEMENTED; +} + +StatusCode gpio_trigger_interrupt(const GpioAddress *address) { + return STATUS_CODE_UNIMPLEMENTED; +} diff --git a/libraries/ms-common/src/x86/interrupts.c b/libraries/ms-common/src/x86/interrupts.c index 80296aa..4a96e91 100644 --- a/libraries/ms-common/src/x86/interrupts.c +++ b/libraries/ms-common/src/x86/interrupts.c @@ -1,10 +1,10 @@ /************************************************************************************************ - * interrupts.c + * @file interrupts.c * - * Interrupts Library Source file + * @brief Interrupts Library Source file * - * Created: 2024-11-03 - * Midnight Sun Team #24 - MSXVI + * @date 2024-11-03 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ diff --git a/libraries/ms-common/src/x86/mcu.c b/libraries/ms-common/src/x86/mcu.c index 5f0cc56..a5218db 100644 --- a/libraries/ms-common/src/x86/mcu.c +++ b/libraries/ms-common/src/x86/mcu.c @@ -1,10 +1,10 @@ /************************************************************************************************ - * mcu.c + * @file mcu.c * - * Source code for MCU intialization + * @brief Source code for MCU intialization * - * Created: 2024-10-27 - * Midnight Sun Team #24 - MSXVI + * @date 2024-10-27 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ @@ -14,4 +14,6 @@ /* Intra-component Headers*/ #include "mcu.h" -void mcu_init(void) {} +StatusCode mcu_init(void) { + return STATUS_CODE_INCOMPLETE; +} diff --git a/libraries/ms-common/src/x86/mpu.c b/libraries/ms-common/src/x86/mpu.c index 66459ef..89339d0 100644 --- a/libraries/ms-common/src/x86/mpu.c +++ b/libraries/ms-common/src/x86/mpu.c @@ -1,10 +1,10 @@ /************************************************************************************************ - * mpu.c + * @file mpu.c * - * MPU Library Source code + * @brief MPU Library Source code * - * Created: 2024-11-03 - * Midnight Sun Team #24 - MSXVI + * @date 2024-11-03 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ @@ -12,3 +12,20 @@ /* Inter-component Headers */ /* Intra-component Headers */ +#include "mpu.h" + +StatusCode mpu_configure_region(MPURegion *region, MPURegionSettings *settings) { + return STATUS_CODE_UNIMPLEMENTED; +} + +StatusCode mpu_enable_region(MPURegionNumber region_number) { + return STATUS_CODE_UNIMPLEMENTED; +} + +StatusCode mpu_disable_region(MPURegionNumber region_number) { + return STATUS_CODE_UNIMPLEMENTED; +} + +StatusCode mpu_init(void) { + return STATUS_CODE_UNIMPLEMENTED; +} diff --git a/libraries/ms-common/src/x86/uart.c b/libraries/ms-common/src/x86/uart.c index 8545169..cf15c3a 100644 --- a/libraries/ms-common/src/x86/uart.c +++ b/libraries/ms-common/src/x86/uart.c @@ -1,10 +1,10 @@ /************************************************************************************************ - * uart.c + * @file uart.c * - * UART Library Source Code + * @brief UART Library Source Code * - * Created: 2024-11-02 - * Midnight Sun Team #24 - MSXVI + * @date 2024-11-02 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ diff --git a/libraries/ms-freertos/src/static_alloc.c b/libraries/ms-freertos/src/static_alloc.c index 5e1763f..cd0807e 100644 --- a/libraries/ms-freertos/src/static_alloc.c +++ b/libraries/ms-freertos/src/static_alloc.c @@ -1,10 +1,10 @@ /************************************************************************************************ - * static_alloc.c + * @file static_alloc.c * - * Static allocation helpers for FreeRTOS. + * @brief Static allocation helpers for FreeRTOS. * - * Created: 2024-10-27 - * Midnight Sun Team #24 - MSXVI + * @date 2024-10-27 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ diff --git a/projects/blinky/src/main.c b/projects/blinky/src/main.c index 9a988a0..dea375a 100644 --- a/projects/blinky/src/main.c +++ b/projects/blinky/src/main.c @@ -1,10 +1,10 @@ /************************************************************************************************ - * main.c + * @file main.c * - * Main file for Blinky + * @brief Main file for Blinky * - * Created: 2024-11-03 - * Midnight Sun Team #24 - MSXVI + * @date 2024-11-03 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ diff --git a/projects/can_communication/src/main.c b/projects/can_communication/src/main.c index b56ddbd..5eac76c 100644 --- a/projects/can_communication/src/main.c +++ b/projects/can_communication/src/main.c @@ -1,10 +1,10 @@ /************************************************************************************************ - * main.c + * @file main.c * - * Main file for CAN communication + * @brief Main file for CAN communication * - * Created: 2024-11-03 - * Midnight Sun Team #24 - MSXVI + * @date 2024-11-03 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ @@ -17,7 +17,7 @@ /* Intra-component Headers */ -TASK(CANCommunication, TASK_STACK_512) { +TASK(can_communication, TASK_STACK_512) { while (true) { LOG_DEBUG("BLINKY\n"); } @@ -28,7 +28,7 @@ int main() { tasks_init(); log_init(); - tasks_init_task(CANCommunication, TASK_PRIORITY(3U), NULL); + tasks_init_task(can_communication, TASK_PRIORITY(3U), NULL); tasks_start(); diff --git a/scons/new_target.scons b/scons/new_target.scons index 3396bd1..c8b3168 100644 --- a/scons/new_target.scons +++ b/scons/new_target.scons @@ -14,7 +14,8 @@ TARGET = VARS.get("TARGET") DEFAULT_CONFIG = { "libs": [ "FreeRTOS", - "ms-common" + "ms-common", + "master" ] } diff --git a/scons/template/main.c b/scons/template/main.c index f4ad680..c5cb680 100644 --- a/scons/template/main.c +++ b/scons/template/main.c @@ -1,10 +1,10 @@ /************************************************************************************************ - * main.c + * @file main.c * - * Main file for [PROJECT NAME] + * @brief Main file for [PROJECT NAME] * - * Created: [YYYY/MM/DD] - * Midnight Sun Team #24 - MSXVI + * @date [YYYY/MM/DD] + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */ @@ -14,23 +14,24 @@ #include "gpio.h" #include "log.h" #include "tasks.h" +#include "master_tasks.h" /* Intra-component Headers */ void pre_loop_init() {} -void run_fast_cycle() {} +void run_1000hz_cycle() {} -void run_medium_cycle() {} +void run_10hz_cycle() {} -void run_slow_cycle() {} +void run_1hz_cycle() {} int main() { mcu_init(); tasks_init(); log_init(); - init_master_task(); + init_master_tasks(); tasks_start(); diff --git a/smoke/master_tasks/README.md b/smoke/master_tasks/README.md new file mode 100644 index 0000000..1f4e4b4 --- /dev/null +++ b/smoke/master_tasks/README.md @@ -0,0 +1,14 @@ + +# master_tasks \ No newline at end of file diff --git a/smoke/master_tasks/config.json b/smoke/master_tasks/config.json new file mode 100644 index 0000000..ef1bba3 --- /dev/null +++ b/smoke/master_tasks/config.json @@ -0,0 +1,7 @@ +{ + "libs": [ + "FreeRTOS", + "ms-common", + "master" + ] +} \ No newline at end of file diff --git a/smoke/master_tasks/src/main.c b/smoke/master_tasks/src/main.c new file mode 100644 index 0000000..6eaed34 --- /dev/null +++ b/smoke/master_tasks/src/main.c @@ -0,0 +1,58 @@ +/************************************************************************************************ + * @file main.c + * + * @brief Main file for master tasks smoke + * + * @date 2024-11-13 + * @author Midnight Sun Team #24 - MSXVI + ************************************************************************************************/ + +/* Standard library headers */ + +/* Inter-component Headers */ +#include "gpio.h" +#include "log.h" +#include "master_tasks.h" +#include "mcu.h" +#include "tasks.h" + +/* Intra-component Headers */ + +TickType_t last_time_1000hz = 0U; +TickType_t last_time_10hz = 0U; +TickType_t last_time_1hz = 0U; + +void pre_loop_init() { + LOG_DEBUG("Pre-loop initialization\n"); +} + +void run_1000hz_cycle() { + TickType_t time_elapsed = xTaskGetTickCount() - last_time_1000hz; + last_time_1000hz = xTaskGetTickCount(); + LOG_DEBUG("Running 1000Hz cycle. Time elapsed between cycles: %ld\n", time_elapsed); +} + +void run_10hz_cycle() { + TickType_t time_elapsed = xTaskGetTickCount() - last_time_10hz; + last_time_10hz = xTaskGetTickCount(); + LOG_DEBUG("Running 10hz cycle. Time elapsed between cycles: %ld\n", time_elapsed); +} + +void run_1hz_cycle() { + TickType_t time_elapsed = xTaskGetTickCount() - last_time_1hz; + last_time_1hz = xTaskGetTickCount(); + LOG_DEBUG("Running 1hz cycle. Time elapsed between cycles: %ld\n", time_elapsed); +} + +int main() { + mcu_init(); + tasks_init(); + log_init(); + + init_master_tasks(); + + tasks_start(); + + LOG_DEBUG("exiting main?"); + return 0; +} diff --git a/smoke/rtos_sample/src/main.c b/smoke/rtos_sample/src/main.c index 51cc702..06456d7 100644 --- a/smoke/rtos_sample/src/main.c +++ b/smoke/rtos_sample/src/main.c @@ -1,10 +1,10 @@ /************************************************************************************************ - * main.c + * @file main.c * - * Main file for RTOS Sample. + * @brief Main file for RTOS Sample. * - * Created: 2024-10-27 - * Midnight Sun Team #24 - MSXVI + * @date 2024-10-27 + * @author Midnight Sun Team #24 - MSXVI ************************************************************************************************/ /* Standard library headers */