I'm currently working on a project related to CAN communication using the CANOpenNode standard with an STM32 microcontroller. I've consulted various GitHub repositories and watched videos, but I'm unable to successfully transmit any TPDO packets, even the simplest ones. Would you be able to review my code and help identify any potential errors? Thank you so much.
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "CO_app_STM32.h"
#include "OD.h"
#include "CANopen.h"
#include "../CANopenNode/301/CO_SDOclient.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
/* USER CODE BEGIN Callback 0 */
/* USER CODE END Callback 0 */
if (htim->Instance == TIM1)
{
HAL_IncTick();
}
/* USER CODE BEGIN Callback 1 */
// Handle CANOpen app interrupts
if (htim == canopenNodeSTM32->timerHandle) {
canopen_app_interrupt();
}
/* USER CODE END Callback 1 */
}
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
CAN_HandleTypeDef hcan;
TIM_HandleTypeDef htim1;
UART_HandleTypeDef huart1;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_CAN_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_TIM1_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_CAN_Init();
MX_USART1_UART_Init();
MX_TIM1_Init();
/* USER CODE BEGIN 2 */
CANopenNodeSTM32 canOpenNodeSTM32;
canOpenNodeSTM32.CANHandle = &hcan;
canOpenNodeSTM32.HWInitFunction = MX_CAN_Init;
canOpenNodeSTM32.baudrate = 125;
canOpenNodeSTM32.desiredNodeID = 32;
canOpenNodeSTM32.timerHandle = &htim1;
canopen_app_init(&canOpenNodeSTM32);
OD_PERSIST_COMM.x6000_current_data=0xAB;
OD_PERSIST_COMM.x6001_voltage_data=0xAB;
OD_PERSIST_COMM.x6002_charger_mode=0xAB;
OD_PERSIST_COMM.x6003_timereq_data=0xAB;
CO_TPDOsendRequest(&canOpenNodeSTM32.canOpenStack->TPDO[0]);// this is the TPDO sending funciotn, am i right ?
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
HAL_Delay(100);
uint8_t receive_data[4];
receive_data[0]=OD_PERSIST_COMM.x6000_current_data;
receive_data[1]=OD_PERSIST_COMM.x6001_voltage_data;
receive_data[2]=OD_PERSIST_COMM.x6002_charger_mode;
receive_data[3]=OD_PERSIST_COMM.x6003_timereq_data;
HAL_UART_Transmit(&huart1, receive_data, 4, 100); // transmit data receive from CAN to UART
canopen_app_process();
}
/* USER CODE END 3 */
}