diff --git a/Charger-FW.ioc b/Charger-FW.ioc index 7a07c82..8b31457 100644 --- a/Charger-FW.ioc +++ b/Charger-FW.ioc @@ -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 diff --git a/Core/Src/main.c b/Core/Src/main.c index 28faafe..c7c396d 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -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 @@ -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; @@ -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; @@ -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 @@ -298,7 +300,7 @@ 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; @@ -306,24 +308,24 @@ static void MX_I2C2_Init(void) 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 */ @@ -377,7 +379,7 @@ static void MX_USART2_UART_Init(void) /* USER CODE END USART2_Init 2 */ } - +#include /** * @brief GPIO Initialization Function * @param None @@ -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); } @@ -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) diff --git a/Makefile b/Makefile index b73fe51..622e071 100644 --- a/Makefile +++ b/Makefile @@ -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] ########################################################################################################################## # ------------------------------------------------