/*
 * AMS_CAN.h
 *
 *  Created on: Mar 19, 2022
 *      Author: jasper
 */

#ifndef INC_AMS_CAN_H_
#define INC_AMS_CAN_H_

#include "main.h"

#include "stm32f4xx_hal.h"
#include "stm32f4xx_hal_can.h"
#include "stm32f4xx_hal_def.h"

#include <stdint.h>

#define CAN_ID_SLAVE_ERROR 0x001
#define CAN_ID_CLOCK_SYNC 0x002
#define CAN_ID_MASTER_HEARTBEAT 0x010
#define CAN_ID_AMS_SLAVE_HEARTBEAT_BASE 0x600
#define CAN_HEARTBEAT_TX_TIMEOUT 10 /* ms */

extern CAN_HandleTypeDef* ams_can_handle;

void ams_can_init(CAN_HandleTypeDef* ams, CAN_HandleTypeDef* car);

void ams_can_handle_ams_msg(CAN_RxHeaderTypeDef* header, uint8_t* data);

void ams_can_send_heartbeat();
/**
 * @brief Send an AMS Error via CAN.
 *
 * @param error_code The kind of error
 * @param transmission_timeout How long to wait for the transmission to complete
 * after starting it (in ms). Set to 0 for no wait.
 */
void ams_can_send_error(AMS_ErrorCode error_code,
                        uint32_t transmission_timeout);

HAL_StatusTypeDef ams_can_wait_for_free_mailboxes(CAN_HandleTypeDef* handle,
                                                  int num_mailboxes,
                                                  uint32_t timeout);

#endif /* INC_AMS_CAN_H_ */