DanielMartensson/Open-SAE-J1939

STM32 Issues Compiling and Debugging

Ra13fael opened this issue · 2 comments

I am using STM32IDE and the STM32L432KC MCU, I am trying to use the J1939 protocol stack to read and talk with a battery. But I am having issues when I am trying to initialize the CAN and my thread crashes on STM32_PLC_CAN_GET_ID_Data.
Am I not using the library correctly??

/* USER CODE BEGIN Header /
/
*



/* USER CODE END Header /
/
Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------/
/
USER CODE BEGIN Includes */

#include "Hardware.h"
#include <stdlib.h>
#include <stdio.h>

#include "ISO_11783/ISO_11783-7_Application_Layer/Application_Layer.h"
#include "Open_SAE_J1939/Open_SAE_J1939.h"
#include "SAE_J1939/SAE_J1939-71_Application_Layer/Application_Layer.h"
#include "SAE_J1939/SAE_J1939-73_Diagnostics_Layer/Diagnostics_Layer.h"
#include "SAE_J1939/SAE_J1939-81_Network_Management_Layer/Network_Management_Layer.h"

/* USER CODE END Includes */

/* Private variables ---------------------------------------------------------*/
CAN_HandleTypeDef hcan1;

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_CAN1_Init(void);

/* USER CODE */
void STM32_PLC_Start_CAN(CAN_HandleTypeDef hcan, J1939 j1939);
void STM32_PLC_CAN_Get_ID_Data(uint32_t
ID, uint8_t data[], bool
is_new_message);
static CAN_HandleTypeDef *can_handler;
static J1939 *j1939_handler;
HAL_StatusTypeDef STM32_PLC_CAN_Transmit(uint8_t TxData[], CAN_TxHeaderTypeDef *TxHeader);
static void Create_CAN_Filter(CAN_HandleTypeDef *hcan);
static void Create_CAN_Interrupt(CAN_HandleTypeDef hcan);
/
USER CODE END */

/**

  • @brief The application entry point.
  • @RetVal int
    */
    int main(void)
    {

/* MCU Configuration--------------------------------------------------------*/

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();

/* Configure the system clock /
SystemClock_Config();
/
Initialize all configured peripherals */
MX_GPIO_Init();
MX_CAN1_Init();

/* USER CODE BEGIN WHILE /
/
Create our J1939 structure with two ECU */
J1939 j1939_1 = {0};
J1939 j1939_2 = {0};

/* Important to sent all non-address to 0xFF - Else we cannot use ECU address 0x0 */
for(uint8_t i = 0; i < 255; i++){
	j1939_1.other_ECU_address[i] = 0xFF;
	j1939_2.other_ECU_address[i] = 0xFF;
}
STM32_PLC_Start_CAN(&hcan1, &j1939_1);
//Open_SAE_J1939_Startup_ECU(&j1939_1);
/* Set the ECU address */
j1939_1.information_this_ECU.this_ECU_address = 0x80;												/* From 0 to 253 because 254 = error address and 255 = broadcast address */
j1939_2.information_this_ECU.this_ECU_address = 0xf8;

/* This is important because length_of_each_field is not a SAE J1939 standard. The software need to know how large field it is from the beginning */
j1939_2.from_other_ecu_identifications.ecu_identification.length_of_each_field = 30;

/* Set the ECU Identification */
j1939_1.information_this_ECU.this_identifications.ecu_identification.length_of_each_field = 30;
char ecu_part_number[20] = "ABC-1100P-XXXX10";
char ecu_serial_number[20] = "1-200-K-10M";
char ecu_location[20] = "Under bridge";
char ecu_type[20] = "Model G";
for(uint8_t i = 0; i < 20; i++){
	j1939_1.information_this_ECU.this_identifications.ecu_identification.ecu_part_number[i] = (uint8_t) ecu_part_number[i];
	j1939_1.information_this_ECU.this_identifications.ecu_identification.ecu_serial_number[i] = (uint8_t) ecu_serial_number[i];
	j1939_1.information_this_ECU.this_identifications.ecu_identification.ecu_location[i] = (uint8_t) ecu_location[i];
	j1939_1.information_this_ECU.this_identifications.ecu_identification.ecu_type[i] = (uint8_t) ecu_type[i];
}

/* Request Software Identification from ECU 2 to ECU 1 */
SAE_J1939_Send_Request(&j1939_2, 0x80, PGN_ECU_IDENTIFICATION);

/* Response request from ECU 1 perspective - Don't worry, in real CAN applications you don't need this mess. */
//Open_SAE_J1939_Listen_For_Messages(&j1939_1);
Open_SAE_J1939_Listen_For_Messages(&j1939_2);
//Open_SAE_J1939_Listen_For_Messages(&j1939_1);

/* Read response request from ECU 1 to ECU 2 */
for(uint8_t i = 0; i < 15; i++)
	Open_SAE_J1939_Listen_For_Messages(&j1939_2);

/* Display what ECU 2 got */
printf("Length of each field = %i\nECU part number = %s\nECU serial number = %s\nECU location = %s\nECU type = %s\nFrom ECU address 0x%X"
		,j1939_2.from_other_ecu_identifications.ecu_identification.length_of_each_field
		,j1939_2.from_other_ecu_identifications.ecu_identification.ecu_part_number
		,j1939_2.from_other_ecu_identifications.ecu_identification.ecu_serial_number
		,j1939_2.from_other_ecu_identifications.ecu_identification.ecu_location
		,j1939_2.from_other_ecu_identifications.ecu_identification.ecu_type
		,j1939_2.from_other_ecu_identifications.ecu_identification.from_ecu_address);

return 0;

}

/**

  • @brief System Clock Configuration
  • @RetVal None
    */
    void SystemClock_Config(void)
    {
    RCC_OscInitTypeDef RCC_OscInitStruct = {0};
    RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

/** Configure the main internal regulator output voltage
/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/
* Configure LSE Drive Capability
/
HAL_PWR_EnableBkUpAccess();
__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
/
* Initializes the RCC Oscillators according to the specified parameters

  • in the RCC_OscInitTypeDef structure.
    /
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE|RCC_OSCILLATORTYPE_MSI;
    RCC_OscInitStruct.LSEState = RCC_LSE_ON;
    RCC_OscInitStruct.MSIState = RCC_MSI_ON;
    RCC_OscInitStruct.MSICalibrationValue = 0;
    RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
    if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
    {
    Error_Handler();
    }
    /
    * Initializes the CPU, AHB and APB buses clocks
    */
    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
    |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
/** Enable MSI Auto calibration
*/
HAL_RCCEx_EnableMSIPLLMode();
}

/**

  • @brief CAN1 Initialization Function
  • @param None
  • @RetVal None
    */
    static void MX_CAN1_Init(void)
    {

/* USER CODE BEGIN CAN1_Init 0 */

/* USER CODE END CAN1_Init 0 */

/* USER CODE BEGIN CAN1_Init 1 */

/* USER CODE END CAN1_Init 1 /
hcan1.Instance = CAN1;
hcan1.Init.Prescaler = 1;
hcan1.Init.Mode = CAN_MODE_NORMAL;
hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan1.Init.TimeSeg1 = CAN_BS1_13TQ;
hcan1.Init.TimeSeg2 = CAN_BS2_2TQ;
hcan1.Init.TimeTriggeredMode = DISABLE;
hcan1.Init.AutoBusOff = DISABLE;
hcan1.Init.AutoWakeUp = DISABLE;
hcan1.Init.AutoRetransmission = DISABLE;
hcan1.Init.ReceiveFifoLocked = DISABLE;
hcan1.Init.TransmitFifoPriority = DISABLE;
if (HAL_CAN_Init(&hcan1) != HAL_OK)
{
Error_Handler();
}
/
USER CODE BEGIN CAN1_Init 2 */

/* USER CODE END CAN1_Init 2 */

}

/**

  • @brief GPIO Initialization Function
  • @param None
  • @RetVal None
    */
    static void MX_GPIO_Init(void)
    {

/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();

}

/* USER CODE BEGIN 4 */
void STM32_PLC_Start_CAN(CAN_HandleTypeDef *hcan, J1939 *j1939) {
can_handler = hcan;
j1939_handler = j1939;
Create_CAN_Filter(hcan);
if (HAL_CAN_Start(hcan) != HAL_OK)
Error_Handler();
Create_CAN_Interrupt(hcan);
}

HAL_StatusTypeDef STM32_PLC_CAN_Transmit(uint8_t TxData[], CAN_TxHeaderTypeDef *TxHeader) {
uint32_t TxMailbox;
return HAL_CAN_AddTxMessage(can_handler, TxHeader, TxData, &TxMailbox);
}

/* Returns true if the message data is new /
void STM32_PLC_CAN_Get_ID_Data(uint32_t
ID, uint8_t data[], bool* is_new_message) {
CAN_RxHeaderTypeDef RxHeader = {0};
uint8_t RxData[8] = {0};
HAL_StatusTypeDef status = HAL_CAN_GetRxMessage(can_handler, CAN_RX_FIFO0, &RxHeader, RxData);
if (status != HAL_OK)
Error_Handler();

/* Check the length of the data */
if(RxHeader.DLC == 0){
	*is_new_message = false;
	return;
}

/* Read ID */
if(RxHeader.IDE == CAN_ID_STD)
	*ID = RxHeader.StdId;
else
	*ID = RxHeader.ExtId;

/* Read data */
memcpy(data, RxData, 8);

/* Leave the function by saying that the message is new */
*is_new_message = true;

}

/* Interrupt handler that read message */
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan) {
Open_SAE_J1939_Listen_For_Messages(j1939_handler);
}

static void Create_CAN_Interrupt(CAN_HandleTypeDef hcan) {
/
Don't forget to check NVIC in CAN -> NVIC Settings -> CAN RX0 interrupt */
if (HAL_CAN_ActivateNotification(hcan, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
Error_Handler();
}

static void Create_CAN_Filter(CAN_HandleTypeDef *hcan) {
CAN_FilterTypeDef sFilterConfig;
sFilterConfig.FilterBank = 0;
sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
sFilterConfig.FilterIdHigh = 0x0000;
sFilterConfig.FilterIdLow = 0x0000;
sFilterConfig.FilterMaskIdHigh = 0x0000;
sFilterConfig.FilterMaskIdLow = 0x0000;
sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.SlaveStartFilterBank = 14;

if (HAL_CAN_ConfigFilter(hcan, &sFilterConfig) != HAL_OK)
	Error_Handler();

}

/* USER CODE END 4 */

/**

  • @brief This function is executed in case of error occurrence.
  • @RetVal None
    /
    void Error_Handler(void)
    {
    /
    USER CODE BEGIN Error_Handler_Debug /
    /
    User can add his own implementation to report the HAL error return state /
    __disable_irq();
    while (1)
    {
    }
    /
    USER CODE END Error_Handler_Debug */
    }

#ifdef USE_FULL_ASSERT
/**

  • @brief Reports the name of the source file and the source line number
  •     where the assert_param error has occurred.
    
  • @param file: pointer to the source file name
  • @param line: assert_param error line source number
  • @RetVal None
    */
    void assert_failed(uint8_t file, uint32_t line)
    {
    /
    USER CODE BEGIN 6 /
    /
    User can add his own implementation to report the file name and line number,
    ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) /
    /
    USER CODE END 6 /
    }
    #endif /
    USE_FULL_ASSERT */

Yes. You are using it totaly wrong!

Let's say that you STM32 board is your ECU (Eletrical Controller Unit) and you want to implement Open SAE J1939, then you should use this example: https://github.com/DanielMartensson/Open-SAE-J1939/blob/main/Src/Examples/Open%20SAE%20J1939/Startup.txt.
Use that, then you use the functions you want to communicate with other ECU:s. SAE 1939 is a standard, but in Open SAE J1939 Library, there are also another ISO library for hydraulical valves....if you want to use. Sonceboze(Swiz) Valves are using that standard.

By the way! What commando do you want to use?