/** * @brief Set whether or not the output should be connected to the connector * @param Connection: Can be any value of CANConnection * @retval SUCCES: Everything went ok * @retval ERROR: Something went wrong */ ErrorStatus can2SetConnection(CANConnection Connection) { RelayStatus relayStatus = RelayStatus_NotEnoughTimePassed; if (Connection == CANConnection_Connected) relayStatus = RELAY_SetState(&switchRelay, RelayState_On); else if (Connection == CANConnection_Disconnected) relayStatus = RELAY_SetState(&switchRelay, RelayState_Off); /* Make sure the relay was set or reset correctly */ if (relayStatus == RelayStatus_Ok) { ErrorStatus errorStatus = ERROR; if (Connection == CANConnection_Connected) errorStatus = prvEnableCan2Interface(); else errorStatus = prvDisableCan2Interface(); /* Make sure the interface was enabled correctly */ if (errorStatus == SUCCESS) { prvCurrentSettings.connection = Connection; return SUCCESS; } else { RELAY_SetState(&switchRelay, RelayState_Off); goto error; } } error: return ERROR; }
/** * @brief Enables the CAN2 interface with the current settings * @param None * @retval None */ static ErrorStatus prvEnableCan2Interface() { /*##-1- Enable peripheral Clocks ###########################################*/ /* CAN2 Peripheral clock enable */ __CAN1_CLK_ENABLE(); /* IMPORTANT!!! As CAN2 is a slave device we need to enable the clock for CAN1 as well */ __CAN2_CLK_ENABLE(); /*##-2- Configure the NVIC #################################################*/ /* NVIC configuration for CAN2 Reception complete interrupt */ HAL_NVIC_SetPriority(CAN2_RX0_IRQn, configLIBRARY_LOWEST_INTERRUPT_PRIORITY, 0); HAL_NVIC_EnableIRQ(CAN2_RX0_IRQn); HAL_NVIC_SetPriority(CAN2_TX_IRQn, configLIBRARY_LOWEST_INTERRUPT_PRIORITY, 0); HAL_NVIC_EnableIRQ(CAN2_TX_IRQn); /*##-3- Configure the CAN peripheral #######################################*/ if (HAL_CAN_Init(&CAN_Handle) != HAL_OK) { /* Initialization Error */ goto error; } /*##-4- Configure the CAN Filter ###########################################*/ if (HAL_CAN_ConfigFilter(&CAN_Handle, &CAN_Filter) != HAL_OK) { /* Filter configuration Error */ goto error; } /*##-5- Configure Transmission process #####################################*/ CAN_Handle.pTxMsg->StdId = 0x321; CAN_Handle.pTxMsg->ExtId = 0x01; CAN_Handle.pTxMsg->RTR = CAN_RTR_DATA; CAN_Handle.pTxMsg->IDE = CAN_ID_STD; CAN_Handle.pTxMsg->DLC = 2; /*##-6- Start the Reception process and enable reception interrupt #########*/ if (HAL_CAN_Receive_IT(&CAN_Handle, CAN_FIFO1) != HAL_OK) { /* Reception Error */ goto error; } return SUCCESS; error: /* Something went wrong so disable */ prvDisableCan2Interface(); return ERROR; }
/** * @brief Restart the CAN * @param None * @retval None */ void can2Restart() { prvDisableCan2Interface(); prvEnableCan2Interface(); }