Skip to content

Commit

Permalink
ioc changes, can error prints
Browse files Browse the repository at this point in the history
  • Loading branch information
Sabramz committed Apr 25, 2024
1 parent 2945af9 commit 078db26
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 34 deletions.
11 changes: 6 additions & 5 deletions Charger-FW.ioc
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ CAD.pinconfig=Dual
CAD.provider=
FDCAN1.CalculateBaudRateNominal=500000
FDCAN1.CalculateTimeBitNominal=2000
FDCAN1.CalculateTimeQuantumNominal=250.0
FDCAN1.CalculateTimeQuantumNominal=125.0
FDCAN1.DataPrescaler=4
FDCAN1.DataTimeSeg1=3
FDCAN1.DataTimeSeg2=4
FDCAN1.IPParameters=CalculateTimeQuantumNominal,CalculateTimeBitNominal,CalculateBaudRateNominal,NominalPrescaler,NominalTimeSeg2,NominalTimeSeg1,DataPrescaler,DataTimeSeg1,DataTimeSeg2,Mode
FDCAN1.FrameFormat=FDCAN_FRAME_CLASSIC
FDCAN1.IPParameters=CalculateTimeQuantumNominal,CalculateTimeBitNominal,CalculateBaudRateNominal,NominalPrescaler,NominalTimeSeg2,NominalTimeSeg1,DataPrescaler,DataTimeSeg1,DataTimeSeg2,Mode,FrameFormat
FDCAN1.Mode=FDCAN_MODE_NORMAL
FDCAN1.NominalPrescaler=4
FDCAN1.NominalTimeSeg1=3
FDCAN1.NominalTimeSeg2=4
FDCAN1.NominalPrescaler=2
FDCAN1.NominalTimeSeg1=13
FDCAN1.NominalTimeSeg2=2
File.Version=6
GPIO.groupedBy=
I2C2.Analog_Filter=I2C_ANALOGFILTER_DISABLE
Expand Down
75 changes: 47 additions & 28 deletions Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ int main(void)
while (1)
{
/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */
float voltage = 10.0; // V
float current = 1.0; // A
Expand All @@ -146,9 +147,10 @@ int main(void)
uint8_t current_high = ((uint16_t) (current * 10)) >> 8;
uint8_t current_low = (uint8_t) (current * 10);
uint8_t control = 1; // 0 for start charging
uint8_t charger_msg[8] = {0, 0, 0, control, current_low, current_high, voltage_low, voltage_high};
//uint8_t charger_msg[8] = {voltage_high, voltage_low, current_high, current_low, control, 0, 0, 0};
//uint8_t charger_msg[8] = {0, 0, 0, control, current_low, current_high, voltage_low, voltage_high};
uint8_t charger_msg[8] = {voltage_high, voltage_low, current_high, current_low, control, 0, 0, 0};
HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan1, &TxHeader, charger_msg);
HAL_Delay(1);
// if (get_switch(gpio, SWITCH_PIN_1) == 0) {
// if (state_idx + 1 == display_state_count) {
// state_idx = 0;
Expand Down Expand Up @@ -242,10 +244,10 @@ static void MX_FDCAN1_Init(void)
hfdcan1.Init.AutoRetransmission = DISABLE;
hfdcan1.Init.TransmitPause = DISABLE;
hfdcan1.Init.ProtocolException = DISABLE;
hfdcan1.Init.NominalPrescaler = 4;
hfdcan1.Init.NominalPrescaler = 2;
hfdcan1.Init.NominalSyncJumpWidth = 1;
hfdcan1.Init.NominalTimeSeg1 = 3;
hfdcan1.Init.NominalTimeSeg2 = 4;
hfdcan1.Init.NominalTimeSeg1 = 13;
hfdcan1.Init.NominalTimeSeg2 = 2;
hfdcan1.Init.DataPrescaler = 4;
hfdcan1.Init.DataSyncJumpWidth = 1;
hfdcan1.Init.DataTimeSeg1 = 3;
Expand All @@ -269,7 +271,7 @@ static void MX_FDCAN1_Init(void)
}

// /* Prepare Tx Header */
TxHeader.Identifier = 0x680; // Determines ID of CAN message
TxHeader.Identifier = 0x1806E5F4; // Determines ID of CAN message
TxHeader.IdType = FDCAN_STANDARD_ID;
TxHeader.TxFrameType = FDCAN_DATA_FRAME;
TxHeader.DataLength = FDCAN_DLC_BYTES_8; // Specifies the number of data bytes to be transmitted
Expand Down Expand Up @@ -298,32 +300,32 @@ static void MX_I2C2_Init(void)

/* USER CODE END I2C2_Init 1 */
hi2c2.Instance = I2C2;
hi2c2.Init.Timing = 0x00303D5D;
hi2c2.Init.Timing = 0x00303DFD;
hi2c2.Init.OwnAddress1 = 0;
hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c2.Init.OwnAddress2 = 0;
hi2c2.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
// if (HAL_I2C_Init(&hi2c2) != HAL_OK)
// {
// Error_Handler();
// }
if (HAL_I2C_Init(&hi2c2) != HAL_OK)
{
Error_Handler();
}

/** Configure Analogue filter
*/
// if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
// {
// Error_Handler();
// }
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_DISABLE) != HAL_OK)
{
Error_Handler();
}

/** Configure Digital filter
*/
// if (HAL_I2CEx_ConfigDigitalFilter(&hi2c2, 0) != HAL_OK)
// {
// Error_Handler();
// }
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c2, 0) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN I2C2_Init 2 */

/* USER CODE END I2C2_Init 2 */
Expand Down Expand Up @@ -377,7 +379,7 @@ static void MX_USART2_UART_Init(void)
/* USER CODE END USART2_Init 2 */

}

#include <stdlib.h>
/**
* @brief GPIO Initialization Function
* @param None
Expand Down Expand Up @@ -443,13 +445,30 @@ void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs)
}
}
} else if (rx_header.Identifier == 0x18FF50E5) {
printf("Max allowable charging terminal voltage: %d\r\n", rx_data[0]); //((float) ((rx_data[1] << 8) | rx_data[0])) / 10.0);
printf("Max Allowable Charging Current: %d\r\n", rx_data[2]);
printf("Hardware Failure: %s\r\n", ((rx_data[5] & 0b00000001) > 0) ? "True" : "False");
printf("Overheating: %s\r\n", ((rx_data[5] & 0b00000010) > 0) ? "True" : "False");
printf("Input voltage fault: %s\r\n", ((rx_data[5] & 0b00000100) > 0) ? "True" : "False");
printf("Battery connection fault: %s\r\n", ((rx_data[5] & 0b00001000) > 1) ? "True" : "False");
printf("Communication timeout: %s\r\n", ((rx_data[5] & 0b00010000) > 1) ? "True" : "False");
printf("%d\r\n", rx_data[0]);
printf("%d\r\n", rx_data[1]);
printf("%d\r\n", rx_data[2]);
printf("%d\r\n", rx_data[3]);
printf("%X\r\n", rx_data[4]);
printf("%d\r\n", rx_data[5]);
printf("%d\r\n", rx_data[6]);
printf("%d\r\n", rx_data[7]);
FDCAN_ProtocolStatusTypeDef *protocol = malloc(sizeof(FDCAN_ProtocolStatusTypeDef));
if (HAL_FDCAN_GetProtocolStatus(hfdcan, protocol) != HAL_OK) {
Error_Handler();
}
printf("Error code: %ld\r\n", protocol->LastErrorCode);
printf("Data error code: %ld\r\n", protocol->DataLastErrorCode);
printf("Passive error code: %ld\r\n", protocol->ErrorPassive); // 0 active error, 1 passive
printf("Bus off : %ld\r\n\n", protocol->BusOff); // 0 for bus on, 1 bus off

// printf("Max allowable charging terminal voltage: %d\r\n", rx_data[0]); //((float) ((rx_data[1] << 8) | rx_data[0])) / 10.0);
// printf("Max Allowable Charging Current: %d\r\n", rx_data[2]);
// printf("Hardware Failure: %s\r\n", ((rx_data[5] & 0b00000001) > 0) ? "True" : "False");
// printf("Overheating: %s\r\n", ((rx_data[5] & 0b00000010) > 0) ? "True" : "False");
// printf("Input voltage fault: %s\r\n", ((rx_data[5] & 0b00000100) > 0) ? "True" : "False");
// printf("Battery connection fault: %s\r\n", ((rx_data[5] & 0b00001000) > 1) ? "True" : "False");
// printf("Communication timeout: %s\r\n", ((rx_data[5] & 0b00010000) > 1) ? "True" : "False");
} else {
printf("CAN msg %lu received \r\n", rx_header.Identifier);
}
Expand All @@ -464,7 +483,7 @@ void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs)
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
printf("Error Encountered!\n");
printf("Error Encountered!\r\n");
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
##########################################################################################################################
# File automatically-generated by tool: [projectgenerator] version: [4.3.0-B58] date: [Fri Apr 19 19:04:04 EDT 2024]
# File automatically-generated by tool: [projectgenerator] version: [4.3.0-B58] date: [Thu Apr 25 02:07:47 EDT 2024]
##########################################################################################################################

# ------------------------------------------------
Expand Down

0 comments on commit 078db26

Please sign in to comment.