Skip to content

Commit

Permalink
Repo setup (#2)
Browse files Browse the repository at this point in the history
* Testing YAML

* Install libncurses

* Install libncurses

* everything

* can hw initial draft. Comments r soon to come

* Master tasks setup

* added interrupts, can comments, notify, flash library

* Formatting

* mastker tasks implementation

* Updated UART

* Updated master tasks to use 1000hz 10 hz and 1hz

* FWXVI

---------

Co-authored-by: vagrant <vagrant@midsunbox>
  • Loading branch information
Akashem06 and vagrant authored Nov 24, 2024
1 parent 57991e1 commit e7d92ab
Show file tree
Hide file tree
Showing 67 changed files with 1,538 additions and 380 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
107 changes: 107 additions & 0 deletions can/inc/can.h
Original file line number Diff line number Diff line change
@@ -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 <stdbool.h>
#include <stddef.h>
#include <stdint.h>

/* 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();
35 changes: 15 additions & 20 deletions can/inc/can_hw.h
Original file line number Diff line number Diff line change
@@ -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 */
Expand All @@ -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"
Expand All @@ -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;
Expand All @@ -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
*/
Expand All @@ -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);
10 changes: 5 additions & 5 deletions can/inc/can_msg.h
Original file line number Diff line number Diff line change
@@ -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 */
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions can/inc/can_queue.h
Original file line number Diff line number Diff line change
@@ -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 */
Expand Down
52 changes: 35 additions & 17 deletions can/src/arm/can_hw.c
Original file line number Diff line number Diff line change
@@ -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 */
Expand Down Expand Up @@ -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 },
Expand All @@ -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,
Expand All @@ -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);

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

Expand Down Expand Up @@ -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? */
}
Loading

0 comments on commit e7d92ab

Please sign in to comment.