Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
b81b58f
can.h first commit
alisha1697 Mar 9, 2025
d54680a
can.c first commit
alisha1697 Mar 9, 2025
5941dc4
Documentation of can.c
alisha1697 Mar 22, 2025
a931c17
Documented can.h
alisha1697 Mar 22, 2025
85bae8f
Documentation complete can.c
alisha1697 Mar 22, 2025
94448df
fixed frame range
alisha1697 Apr 5, 2025
9e0024c
filter works...too much
alisha1697 May 29, 2025
5cbe481
CANFD project file
alisha1697 May 31, 2025
ad68aae
Update can.c
alisha1697 Jun 10, 2025
fbabef5
Update can.h
alisha1697 Jun 10, 2025
1961fa5
fixed parsing in test_canfd.c
alisha1697 Jun 10, 2025
19d4454
Updated can.c with CAN_Receive and fixed definition error
alisha1697 Jun 11, 2025
aeb4e9c
Updated can.h
alisha1697 Jun 11, 2025
4aad34f
FDCAN_Serial_Testing Zip CUBEIDE project file
alisha1697 Jun 14, 2025
86e78e2
Updated rudder PID
JoshuaO956 Jul 16, 2025
9317a5a
Setup IOC and test code for encoder and motor
MichaelGreenough Jul 16, 2025
95d2ccf
Motor cal code
MichaelGreenough Jul 17, 2025
e553625
Motor deadband code
MichaelGreenough Jul 17, 2025
f7c84fd
Code cleanup
MichaelGreenough Jul 26, 2025
4f8333c
Added deadband calibration
MichaelGreenough Jul 26, 2025
81219f6
More tweaks, added serial controllable motor zeroing script
MichaelGreenough Jul 26, 2025
69b510e
Beginings of utils
MichaelGreenough Jul 26, 2025
9aba891
Working PID on the boat, just need CANBus integration
MichaelGreenough Jul 31, 2025
3cf4a3c
Configured ioc for CANFD
MichaelGreenough Aug 3, 2025
90cb1cb
Merge remote-tracking branch 'origin/alisha1697-can-library-1' into J…
MichaelGreenough Aug 3, 2025
bab6644
Working CAN (had to rebuild drivers)
MichaelGreenough Aug 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions projects/base-library/project/Core/Inc/can.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
/*
* can.h
* can.h
*
* Description: Provides declarations for variables and function prototypes related to CAN.
* Created on: Mar 8, 2025
* Author: Alisha
*
* Created on: April 4, 2024
* Author: Peter, Jordan, Katherine
* @brief Header file for FDCAN library.
*
* @details This file contains function prototypes and global variables for configuring,
* transmitting, and receiving FDCAN messages on the STM32.
*/
#ifndef SRC_CAN_H_
#define SRC_CAN_H_

/* Includes ------------------------------------------------------------------*/
#include "stm32u5xx_hal.h"
#include "stm32u5xx_hal_fdcan.h"
#include <stdint.h>
#include <stdlib.h>

/* Variables ------------------------------------------------------------------*/
extern FDCAN_HandleTypeDef hfdcan1; /* Handle for FDCAN1 */
extern uint8_t* RxData1; /* Pointer to receive buffer for FIFO0 (Standard ID)*/
extern uint8_t* RxData2; /* Pointer to receive buffer for FIFO1 (Extended ID)*/
extern uint16_t RxData1_Length; /* Length of data received in FIFO0 */
extern uint16_t RxData2_Length; /* Length of data received in FIFO1 */

/* Function prototypes ------------------------------------------------------------------*/
void CAN_SetRxBufferSize(uint16_t RxData1_Length, uint16_t RxData2_Length);
void CAN_Init(void);
HAL_StatusTypeDef CAN_Transmit(uint32_t Identifier, uint32_t IdType, uint32_t DataLength, uint8_t* DataBuffer);
void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs);
void HAL_FDCAN_RxFifo1Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo1ITs);
void CAN_PrintRxData(void);

#endif /* SRC_CAN_H_ */

/* Functions ------------------------------------------------------------------*/
224 changes: 220 additions & 4 deletions projects/base-library/project/Core/Src/can.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,230 @@
/*
* can.c
* can.c
*
* Description: Provides CAN message framing, parsing and other related features other than read/write.
* Created on: Mar 8, 2025
* Author: Alisha
*
* Created on: April 4, 2024
* Author: Peter, Jordan, Katherine
* @brief This file implements the function prototypes in can.h to initialize and handle FDCAN on an STM32.
*
* @details The implementation includes buffer management, initialization, transmission, and
* reception handling with callback functions for handling received messages.
*/

/* Includes ------------------------------------------------------------------*/
#include "can.h"
#include "main.h"
#include <stdio.h>

/* Variables ------------------------------------------------------------------*/
FDCAN_HandleTypeDef hfdcan1; /* Handle for FDCAN1 */
HAL_StatusTypeDef CanStartStatus; /* Status of FDCAN start operation */
uint8_t* RxData1 = NULL; /* Pointer to receive buffer for FIFO0 (Standard ID)*/
uint8_t* RxData2 = NULL; /* Pointer to receive buffer for FIFO1 (Extended ID)*/
uint16_t RxData1_BufferLength = 0; /* Length of data received in FIFO0 */
uint16_t RxData2_BufferLength = 0; /* Length of data received in FIFO1 */

/* Functions ------------------------------------------------------------------*/

/**
* @brief Allocates memory for FDCAN receive buffers.
* @param RxData1_Length: Length of buffer for FIFO0.
* @param RxData2_Length: Length of buffer for FIFO1.
*/
void CAN_SetRxBufferSize(uint16_t RxData1_Length, uint16_t RxData2_Length) {
if (RxData1 != NULL) {
free(RxData1);
}
if (RxData2 != NULL) {
free(RxData2);
}

RxData1_BufferLength = RxData1_Length;
RxData2_BufferLength = RxData2_Length;

RxData1 = (uint8_t* )malloc(RxData1_Length);
RxData2 = (uint8_t* )malloc(RxData2_Length);
if (RxData1 == NULL || RxData2 == NULL) {
Error_Handler();
}
}

/**
* @brief Initializes the FDCAN module.
* @details In order:
* Configures standard ID reception filter to Rx buffer
* Configures extended ID reception filter to Rx FIFO 1
* Configures global filter: Filter all remote frames with STD and EXT ID
* Reject non matching frames with STD ID and EXT ID
* Starts the FDCAN controller (continuous listening CAN bus)
* Activates Notifications
*/
void CAN_Init(void) {
/*##-1 Configures FDCAN meta data and controllers*/
FDCAN_FilterTypeDef sFilterConfig;
sFilterConfig.IdType = FDCAN_STANDARD_ID;
sFilterConfig.FilterIndex = 0;
sFilterConfig.FilterType = FDCAN_FILTER_RANGE;
sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;
sFilterConfig.FilterID1 = 0x000;
sFilterConfig.FilterID2 = 0x2FF;
if (HAL_FDCAN_ConfigFilter(&hfdcan1, &sFilterConfig) != HAL_OK)
{
Error_Handler();
}

sFilterConfig.IdType = FDCAN_EXTENDED_ID;
sFilterConfig.FilterIndex = 0;
sFilterConfig.FilterType = FDCAN_FILTER_RANGE_NO_EIDM;
sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO1;
sFilterConfig.FilterID1 = 0x1111111;
sFilterConfig.FilterID2 = 0x2222222;
if (HAL_FDCAN_ConfigFilter(&hfdcan1, &sFilterConfig) != HAL_OK)
{
Error_Handler();
}

if (HAL_FDCAN_ConfigGlobalFilter(&hfdcan1, FDCAN_ACCEPT_IN_RX_FIFO0, FDCAN_ACCEPT_IN_RX_FIFO0, FDCAN_FILTER_REMOTE, FDCAN_FILTER_REMOTE) != HAL_OK)
{
Error_Handler();
}

/*##-2 Start FDCAN controller (continuous listening CAN bus) ##############*/
CanStartStatus = HAL_FDCAN_Start(&hfdcan1);
if (CanStartStatus != HAL_OK)
{
Error_Handler();
}

if (HAL_FDCAN_ActivateNotification(&hfdcan1, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0) != HAL_OK)
{
Error_Handler();
}

if (HAL_FDCAN_ActivateNotification(&hfdcan1, FDCAN_IT_RX_FIFO1_NEW_MESSAGE, 0) != HAL_OK)
{
Error_Handler();
}
}

/**
* @brief Transmits a FDCAN message.
* @param Identifier: Message identifier (In-line with Filter used).
* @param IdType: Identifier type (FDCAN_STANDARD_ID or FDCAN_EXTENDED_ID).
* @param DataLength: Length of the TxData buffer in the form of FDCAN_data_length_code
* FDCAN_data_length_code (X bytes data field):
* FDCAN_DLC_BYTES_0, FDCAN_DLC_BYTES_1, FDCAN_DLC_BYTES_2,
* FDCAN_DLC_BYTES_3, FDCAN_DLC_BYTES_4, FDCAN_DLC_BYTES_5,
* FDCAN_DLC_BYTES_6, FDCAN_DLC_BYTES_7, FDCAN_DLC_BYTES_8,
* FDCAN_DLC_BYTES_12, FDCAN_DLC_BYTES_16, FDCAN_DLC_BYTES_20,
* FDCAN_DLC_BYTES_24, FDCAN_DLC_BYTES_32, FDCAN_DLC_BYTES_48,
* FDCAN_DLC_BYTES_64
* @param DataBuffer: Pointer to the TxData buffer.
* @return HAL_StatusTypeDef HAL_OK if successful, !HAL_OK otherwise.
*/
HAL_StatusTypeDef CAN_Transmit(uint32_t Identifier, uint32_t IdType, uint32_t DataLength, uint8_t* DataBuffer) {
FDCAN_TxHeaderTypeDef TxHeader;

TxHeader.Identifier = Identifier;
TxHeader.IdType = IdType;
TxHeader.TxFrameType = FDCAN_DATA_FRAME;
TxHeader.DataLength = DataLength;
TxHeader.ErrorStateIndicator = FDCAN_ESI_ACTIVE;
TxHeader.BitRateSwitch = FDCAN_BRS_ON;
TxHeader.FDFormat = FDCAN_FD_CAN;
TxHeader.TxEventFifoControl = FDCAN_STORE_TX_EVENTS;

return HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan1, &TxHeader, DataBuffer);
}

/**
* @brief FDCAN has multiple Callbacks. This section highlights the order of the callbacks
* which you are able to overwrite for specific purposes (like flashing an LED
* for debugging or changing the Tx data in any way).
* These can be overwritten from stm32u5xx_hal_fdcan.h
* @details When CAN_Transmit is called:
* 1. HAL_FDCAN_TxEventFifoCallback(): Triggered when a new entry is added to the Tx Event FIFO after a successful transmission.
* - Log transmission status, toggle an LED, or trigger another action upon successful transmission.
* 2. HAL_FDCAN_TxBufferCompleteCallback(): Triggered when a dedicated Tx buffer completes transmission.
* - This can be used to add functionality after the TxData has been fired.
* 3. HAL_FDCAN_RxFifo0Callback(): Triggered when a new message is received in Rx FIFO 0.
* 4. HAL_FDCAN_RxFifo1Callback(): Triggered when a new message is received in Rx FIFO 1.
* - Process received data, used for debugging (toggle an LED) or trigger other operations based on received messages.
* 5. HAL_FDCAN_AbortTxRequest(): Called if transmission is aborted before completion (can be triggered by user).
* - Handle retransmission, alert on transmission failure or manually abort for testing.
* 6. HAL_FDCAN_HighPriorityMessageCallback(): Triggered when a high-priority message or remote frame is received.
* - When processing of urgent messages
*/


/**
* @brief Callback function for handling messages received in FIFO0.
* @param hfdcan: Pointer to FDCAN handle.
* @param RxFifo0ITs: FIFO0 interrupt flags.
*/
void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs) {
if ((RxFifo0ITs & FDCAN_IT_RX_FIFO0_NEW_MESSAGE) != RESET) {
FDCAN_RxHeaderTypeDef RxHeader;
if (RxData1 == NULL) {
Error_Handler();
}
if (HAL_FDCAN_GetRxMessage(hfdcan, FDCAN_RX_FIFO0, &RxHeader, RxData1) != HAL_OK) {
Error_Handler();
}
//check actual length incoming against the buf len rather than stringcmp?
RxData1_BufferLength = RxHeader.DataLength;
}
/* added for debug */
//HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
}

/**
* @brief Callback function for handling messages received in FIFO1.
* @param hfdcan: Pointer to FDCAN handle.
* @param RxFifo1ITs: FIFO1 interrupt flags.
*/
void HAL_FDCAN_RxFifo1Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo1ITs) {
if ((RxFifo1ITs & FDCAN_IT_RX_FIFO1_NEW_MESSAGE) != RESET) {
FDCAN_RxHeaderTypeDef RxHeader;
if (RxData2 == NULL) {
Error_Handler();
}
if (HAL_FDCAN_GetRxMessage(hfdcan, FDCAN_RX_FIFO1, &RxHeader, RxData2) != HAL_OK) {
Error_Handler();
}
RxData2_BufferLength = RxHeader.DataLength;
}
}

/**
* @brief Prints the received RxDataX recived from FIFO0 and FIFO1.
*
* @details This function checks if there is received data in the Rx buffers
* If data is available, it prints it in hexadecimal format;
* otherwise, it indicates that no data has been received.
*
* @note This function assumes that RxData1 and RxData2 have been allocated and
* populated by the Rx FIFO callbacks.
*
* @retval None
*/
void CAN_PrintRxData(void) {
if (RxData1 != NULL && RxData1_BufferLength > 0) {
printf("FIFO0 Received: ");
for (uint16_t i = 0; i < RxData1_BufferLength; i++) {
printf("%02X ", RxData1[i]);
}
printf("\n");
} else {
printf("FIFO0: No data received.\n");
}

if (RxData2 != NULL && RxData2_BufferLength > 0) {
printf("FIFO1 Received: ");
for (uint16_t i = 0; i < RxData2_BufferLength; i++) {
printf("%02X ", RxData2[i]);
}
printf("\n");
} else {
printf("FIFO1: No data received.\n");
}
}
Binary file added projects/canfd/FDCAN_Serial.zip
Binary file not shown.
Loading