/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : hal_can.h
  * @brief          : CAN hardware abstraction layer header file
  ******************************************************************************
  */
/* USER CODE END Header */

#ifndef HAL_CAN_H
#define HAL_CAN_H

#include <stdint.h>
#include <stddef.h>
#include "hal.h"

/**
 * @brief CAN instance identifier definitions
 */
typedef enum {
    HAL_CAN_INSTANCE_1 = 0,
    HAL_CAN_INSTANCE_2,
    HAL_CAN_INSTANCE_MAX
} hal_can_instance_t;

/**
 * @brief CAN mode definitions
 */
typedef enum {
    HAL_CAN_MODE_NORMAL = 0,
    HAL_CAN_MODE_LOOPBACK,
    HAL_CAN_MODE_SILENT,
    HAL_CAN_MODE_SILENT_LOOPBACK
} hal_can_mode_t;

/**
 * @brief CAN filter mode definitions
 */
typedef enum {
    HAL_CAN_FILTER_MODE_IDMASK = 0,
    HAL_CAN_FILTER_MODE_IDLIST
} hal_can_filter_mode_t;

/**
 * @brief CAN filter scale definitions
 */
typedef enum {
    HAL_CAN_FILTER_SCALE_16BIT = 0,
    HAL_CAN_FILTER_SCALE_32BIT
} hal_can_filter_scale_t;

/**
 * @brief CAN filter fifo assignment definitions
 */
typedef enum {
    HAL_CAN_FILTER_FIFO0 = 0,
    HAL_CAN_FILTER_FIFO1
} hal_can_filter_fifo_t;

/**
 * @brief CAN frame format definitions
 */
typedef enum {
    HAL_CAN_FRAME_STANDARD = 0,
    HAL_CAN_FRAME_EXTENDED
} hal_can_frame_format_t;

/**
 * @brief CAN frame type definitions
 */
typedef enum {
    HAL_CAN_FRAME_DATA = 0,
    HAL_CAN_FRAME_REMOTE
} hal_can_frame_type_t;

/**
 * @brief CAN filter configuration structure
 */
typedef struct {
    uint8_t filter_id;
    hal_can_filter_mode_t mode;
    hal_can_filter_scale_t scale;
    hal_can_filter_fifo_t fifo_assignment;
    uint32_t filter_mask_id_high;
    uint32_t filter_mask_id_low;
    uint32_t filter_id_high;
    uint32_t filter_id_low;
    uint8_t filter_enable;
} hal_can_filter_config_t;

/**
 * @brief CAN configuration structure
 */
typedef struct {
    hal_can_instance_t instance;
    hal_can_mode_t mode;
    uint32_t prescaler;
    uint8_t sync_jump_width;
    uint8_t time_segment1;
    uint8_t time_segment2;
    uint8_t filter_count;
    const hal_can_filter_config_t* filters;
} hal_can_config_t;

/**
 * @brief CAN message structure
 */
typedef struct {
    hal_can_frame_format_t format;
    hal_can_frame_type_t type;
    uint32_t id;
    uint8_t dlc;
    uint8_t data[8];
} hal_can_message_t;

/**
 * @brief Initialize CAN hardware
 * @param config: CAN configuration structure
 * @retval HAL status code
 */
hal_ret_t hal_can_init(const hal_can_config_t *config);

/**
 * @brief Deinitialize CAN hardware
 * @param instance: CAN instance identifier
 * @retval HAL status code
 */
hal_ret_t hal_can_deinit(hal_can_instance_t instance);

/**
 * @brief Start CAN peripheral
 * @param instance: CAN instance identifier
 * @retval HAL status code
 */
hal_ret_t hal_can_start(hal_can_instance_t instance);

/**
 * @brief Stop CAN peripheral
 * @param instance: CAN instance identifier
 * @retval HAL status code
 */
hal_ret_t hal_can_stop(hal_can_instance_t instance);

/**
 * @brief Send CAN message
 * @param instance: CAN instance identifier
 * @param message: Pointer to CAN message structure
 * @param timeout: Timeout in milliseconds
 * @retval HAL status code
 */
hal_ret_t hal_can_send(hal_can_instance_t instance, const hal_can_message_t *message, uint32_t timeout);

/**
 * @brief Receive CAN message
 * @param instance: CAN instance identifier
 * @param fifo: FIFO number (0 or 1)
 * @param message: Pointer to CAN message structure
 * @param timeout: Timeout in milliseconds
 * @retval HAL status code
 */
hal_ret_t hal_can_receive(hal_can_instance_t instance, uint8_t fifo, hal_can_message_t *message, uint32_t timeout);

/**
 * @brief Get CAN receive FIFO message pending count
 * @param instance: CAN instance identifier
 * @param fifo: FIFO number (0 or 1)
 * @param count: Pointer to store pending message count
 * @retval HAL status code
 */
hal_ret_t hal_can_get_pending_count(hal_can_instance_t instance, uint8_t fifo, uint8_t *count);

/**
 * @brief Configure CAN filter
 * @param instance: CAN instance identifier
 * @param filter_config: Pointer to CAN filter configuration structure
 * @retval HAL status code
 */
hal_ret_t hal_can_configure_filter(hal_can_instance_t instance, const hal_can_filter_config_t *filter_config);

#endif /* HAL_CAN_H */