Skip to content

Commit

Permalink
another fix
Browse files Browse the repository at this point in the history
  • Loading branch information
KwiatPiotr committed Nov 19, 2020
1 parent 0a8a5d4 commit d388862
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 63 deletions.
22 changes: 4 additions & 18 deletions PUTM_EV_CAN_Translator_f105/Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "can_messeges_func.h"
#include <stdio.h>

/* USER CODE END Includes */

Expand Down Expand Up @@ -86,6 +85,7 @@ uint16_t engine_mode;
typedef void (*request_list_type)(CAN_TxHeaderTypeDef*, uint8_t**);

uint32_t tim2_counter;
uint32_t last_apps_timestamp;
uint32_t apps_timeout_counter;
uint32_t engine_timeout_counter;

Expand Down Expand Up @@ -308,6 +308,7 @@ int main(void)
HAL_Delay(10);
}


}
/* USER CODE END 3 */
}
Expand Down Expand Up @@ -575,18 +576,15 @@ static void MX_GPIO_Init(void)

/* USER CODE BEGIN 4 */
static void CAN_requests_Init(void){
request_list_type requests[8] = {
request_list_type requests[5] = {
&CAN_request_speed_command,
&CAN_request_power_command,
&CAN_request_igbt_temp_command,
&CAN_request_motor_temp_command,
&CAN_request_air_temp_command,
&CAN_request_status_command,
&CAN_request_N_max_command,
&CAN_request_speed_limit_command
};

for (int i = 0; i < 8; ++i){
for (int i = 0; i < 5; ++i){
CAN_TxHeaderTypeDef TxHeader;
uint8_t* TxData = NULL;

Expand All @@ -602,21 +600,10 @@ static void CAN_requests_Init(void){
HAL_Delay(3);
free(TxData);
}
printf("dupa\n");

HAL_Delay(10);
}

int _write(int32_t file, uint8_t *ptr, int32_t len)
{
int i=0;
for(i=0 ; i<len ; i++)
ITM_SendChar((*ptr++));

return len;

}

/* USER CODE END 4 */

/**
Expand All @@ -628,7 +615,6 @@ void Error_Handler(void)
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
HAL_GPIO_WritePin(GPIO_LED_3_GPIO_Port, GPIO_LED_3_Pin, 0);
//HAL_NVIC_SystemReset();
/* USER CODE END Error_Handler_Debug */
}

Expand Down
98 changes: 53 additions & 45 deletions PUTM_EV_CAN_Translator_f105/Core/Src/stm32f1xx_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
#define MAX_TIMEOUT_TICKS 50
#define MAX_TIMEOUT_TICKS 100
/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
Expand Down Expand Up @@ -85,6 +85,7 @@ extern uint16_t inverter_temp_air_raw;
extern uint32_t tim2_counter;
extern uint32_t apps_timeout_counter;
extern uint32_t engine_timeout_counter;
extern uint32_t last_apps_timestamp;

extern uint8_t inverter_stopped;

Expand Down Expand Up @@ -119,7 +120,12 @@ void NMI_Handler(void)
void HardFault_Handler(void)
{
/* USER CODE BEGIN HardFault_IRQn 0 */

HAL_GPIO_WritePin(GPIO_LED_1_GPIO_Port, GPIO_LED_1_Pin, 0);
HAL_GPIO_WritePin(GPIO_LED_2_GPIO_Port, GPIO_LED_2_Pin, 0);
HAL_GPIO_WritePin(GPIO_LED_3_GPIO_Port, GPIO_LED_3_Pin, 0);
HAL_GPIO_WritePin(GPIO_LED_4_GPIO_Port, GPIO_LED_4_Pin, 0);
HAL_GPIO_WritePin(GPIO_LED_5_GPIO_Port, GPIO_LED_5_Pin, 0);
HAL_GPIO_WritePin(GPIO_LED_6_GPIO_Port, GPIO_LED_6_Pin, 0);
/* USER CODE END HardFault_IRQn 0 */
while (1)
{
Expand Down Expand Up @@ -239,60 +245,62 @@ void SysTick_Handler(void)
void CAN1_RX0_IRQHandler(void)
{
/* USER CODE BEGIN CAN1_RX0_IRQn 0 */
if (HAL_CAN_GetRxMessage(&hcan1, CAN_RX_FIFO0, &RxHeader_CAN1, RxData_CAN1) == HAL_OK){
if(RxHeader_CAN1.StdId == 0x0A){
apps_timeout_counter = tim2_counter;

/* USER CODE END CAN1_RX0_IRQn 0 */
HAL_CAN_IRQHandler(&hcan1);
/* USER CODE BEGIN CAN1_RX0_IRQn 1 */
if (HAL_CAN_GetRxMessage(&hcan1, CAN_RX_FIFO0, &RxHeader_CAN1, RxData_CAN1) == HAL_OK){
if(RxHeader_CAN1.StdId == 0x0A){
apps_timeout_counter = tim2_counter;
if (last_apps_timestamp + 15 > tim2_counter){
return;
}

CAN_TxHeaderTypeDef TxHeader;
uint8_t* TxData = NULL;
CAN_TxHeaderTypeDef TxHeader;
uint8_t* TxData = NULL;

int16_t apps = ((int16_t)RxData_CAN1[1]) << 8;
apps = apps | ((int16_t)RxData_CAN1[0]);
int16_t apps = ((int16_t)RxData_CAN1[1]) << 8;
apps = apps | ((int16_t)RxData_CAN1[0]);

if (apps > 0){
apps = ((apps * 10 ) / 10); // TOMASZ TUTAJ
if (apps > 0){
apps = ((apps * 10 ) / 10); // TOMASZ TUTAJ

//HAL_GPIO_WritePin(GPIO_LED_4_GPIO_Port, GPIO_LED_4_Pin, 1);
}
else if (apps == 0 && inverter_RPM_to_send > 0){
apps = 0; // 0%
//apps = -10; // -2.5%
//apps = -50; // -5%
//apps = -1 * (inverter_RPM_to_send * 10 / 0x7fff);
}
else if (apps == 0 && inverter_RPM_to_send > 0){
apps = 0; // 0%
//apps = -10; // -2.5%
//apps = -50; // -5%
//apps = -1 * (inverter_RPM_to_send * 10 / 0x7fff);

//HAL_GPIO_WritePin(GPIO_LED_4_GPIO_Port, GPIO_LED_4_Pin, 0);
}
}

if (apps > 500){
emegrancy_stop(&hcan2);
HAL_GPIO_WritePin(GPIO_LED_5_GPIO_Port, GPIO_LED_5_Pin, 0);
HAL_GPIO_WritePin(GPIO_LED_6_GPIO_Port, GPIO_LED_6_Pin, 0);
apps = 0;
}
if (apps > 500){
emegrancy_stop(&hcan2);
HAL_GPIO_WritePin(GPIO_LED_5_GPIO_Port, GPIO_LED_5_Pin, 0);
HAL_GPIO_WritePin(GPIO_LED_6_GPIO_Port, GPIO_LED_6_Pin, 0);
apps = 0;
}

CAN_set_speed_command(&TxHeader, &TxData, apps);
//if (HAL_CAN_GetTxMailboxesFreeLevel(&hcan2) < 3){
CAN_set_speed_command(&TxHeader, &TxData, apps);

HAL_CAN_AbortTxRequest(&hcan2, TxMailbox2);
if (HAL_CAN_AddTxMessage(&hcan2, &TxHeader, TxData, &TxMailbox2) != HAL_OK)
{
Error_Handler();
HAL_CAN_AbortTxRequest(&hcan2, TxMailbox2);
if (HAL_CAN_AddTxMessage(&hcan2, &TxHeader, TxData, &TxMailbox2) != HAL_OK)
{
Error_Handler();
}
free(TxData);

last_apps_timestamp = tim2_counter;
}
//}
free(TxData);
}
else if(RxHeader_CAN1.StdId == 0x0C){
if(RxData_CAN1[3] != 0x00){
emegrancy_stop(&hcan2);
else if(RxHeader_CAN1.StdId == 0x0C){
if(RxData_CAN1[3] != 0x00){
emegrancy_stop(&hcan2);
}
}
}else{
Error_Handler();
}
}else{
Error_Handler();
}
/* USER CODE END CAN1_RX0_IRQn 0 */
HAL_CAN_IRQHandler(&hcan1);
/* USER CODE BEGIN CAN1_RX0_IRQn 1 */

/* USER CODE END CAN1_RX0_IRQn 1 */
}

Expand All @@ -306,7 +314,7 @@ void TIM2_IRQHandler(void)

if(apps_timeout_counter + MAX_TIMEOUT_TICKS <= tim2_counter){
emegrancy_stop(&hcan2); //message to engine that apps is not responding
HAL_GPIO_WritePin(GPIO_LED_1_GPIO_Port, GPIO_LED_1_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIO_LED_2_GPIO_Port, GPIO_LED_2_Pin, GPIO_PIN_RESET);
}

if(engine_timeout_counter + MAX_TIMEOUT_TICKS <= tim2_counter){
Expand Down

0 comments on commit d388862

Please sign in to comment.