进一步迭代优化统一管理

This commit is contained in:
冯佳
2026-01-23 14:35:51 +08:00
parent 988cc7ad4a
commit 075e8299cf
36 changed files with 6146 additions and 1590 deletions

View File

@ -14,9 +14,9 @@
#include "stm32f4xx_hal.h"
/* Include common HAL headers */
#include "../../hal_delay.h"
#include "../../hal_gpio.h"
#include "../../hal_uart.h"
#include "../../hal_delay.h"
/**
* @brief STM32F4 specific initialization

View File

@ -10,6 +10,14 @@
#ifndef HAL_H
#define HAL_H
#include <stdint.h>
/* HAL version definitions */
#define HAL_VERSION_MAJOR 1
#define HAL_VERSION_MINOR 0
#define HAL_VERSION_PATCH 0
#define HAL_VERSION_STRING "1.0.0"
/* Define supported architectures */
#define HAL_ARCH_STM32F1 0
#define HAL_ARCH_STM32F4 1
@ -21,27 +29,339 @@
#define HAL_TARGET_ARCH HAL_ARCH_STM32F4
#endif
/* Include architecture specific headers */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
#include "arch/stm32f1/hal_stm32f1.h"
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
#include "arch/stm32f4/hal_stm32f4.h"
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
#include "arch/stm32f7/hal_stm32f7.h"
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
#include "arch/stm32l4/hal_stm32l4.h"
#else
#error "Unsupported HAL architecture: " #HAL_TARGET_ARCH
#endif
/* Architecture specific functions will be included in implementation files */
/* Architecture compatibility check */
#if HAL_TARGET_ARCH < HAL_ARCH_STM32F1 || HAL_TARGET_ARCH > HAL_ARCH_STM32L4
#error "Invalid HAL architecture selection"
#endif
/**
* @brief HAL status return codes - renamed to avoid conflict with STM32 HAL
*/
typedef enum {
HAL_RET_OK = 0x00U, /*!< Operation completed successfully */
HAL_RET_ERROR = 0x01U, /*!< Generic operation failed */
HAL_RET_BUSY = 0x02U, /*!< Peripheral or resource busy */
HAL_RET_TIMEOUT = 0x03U, /*!< Operation timed out */
HAL_RET_INVALID_PARAM = 0x04U, /*!< Invalid parameter */
HAL_RET_NOT_SUPPORTED = 0x05U, /*!< Operation not supported */
HAL_RET_OUT_OF_MEMORY = 0x06U, /*!< Out of memory */
HAL_RET_INTERNAL_ERROR = 0x07U, /*!< Internal error */
HAL_RET_RESOURCE_LOCKED = 0x08U, /*!< Resource locked */
HAL_RET_RESOURCE_ERROR = 0x09U, /*!< Resource error */
HAL_RET_COMM_ERROR = 0x0AU, /*!< Communication error */
HAL_RET_CONFIG_ERROR = 0x0BU, /*!< Configuration error */
HAL_RET_INIT_ERROR = 0x0CU, /*!< Initialization error */
HAL_RET_DEINIT_ERROR = 0x0DU, /*!< Deinitialization error */
HAL_RET_IO_ERROR = 0x0EU, /*!< I/O operation error */
HAL_RET_OVERRUN_ERROR = 0x0FU, /*!< Overrun error */
HAL_RET_UNDERRUN_ERROR = 0x10U, /*!< Underrun error */
HAL_RET_PARITY_ERROR = 0x11U, /*!< Parity error */
HAL_RET_FRAMING_ERROR = 0x12U, /*!< Framing error */
HAL_RET_NOISE_ERROR = 0x13U /*!< Noise error */
} hal_ret_t;
/**
* @brief HAL error category definitions
*/
typedef enum {
HAL_ERR_CAT_COMMON = 0x00U, /*!< Common error category */
HAL_ERR_CAT_GPIO = 0x10U, /*!< GPIO error category */
HAL_ERR_CAT_UART = 0x20U, /*!< UART error category */
HAL_ERR_CAT_SPI = 0x30U, /*!< SPI error category */
HAL_ERR_CAT_I2C = 0x40U, /*!< I2C error category */
HAL_ERR_CAT_CAN = 0x50U, /*!< CAN error category */
HAL_ERR_CAT_ADC = 0x60U, /*!< ADC error category */
HAL_ERR_CAT_DMA = 0x70U, /*!< DMA error category */
HAL_ERR_CAT_RTC = 0x80U, /*!< RTC error category */
HAL_ERR_CAT_MODULE = 0x90U, /*!< Module management error category */
HAL_ERR_CAT_ARCH = 0xA0U, /*!< Architecture specific error category */
HAL_ERR_CAT_MAX = 0xFFU /*!< Maximum error category */
} hal_error_category_t;
/**
* @brief HAL extended error code structure
*/
typedef struct {
hal_ret_t status; /*!< HAL status code */
hal_error_category_t category; /*!< Error category */
uint32_t specific_error; /*!< Specific error code (category dependent) */
const char* file; /*!< File where error occurred */
uint32_t line; /*!< Line number where error occurred */
const char* function; /*!< Function where error occurred */
uint32_t timestamp; /*!< Error timestamp */
} hal_error_t;
/**
* @brief HAL error callback type
*/
typedef void (*hal_error_callback_t)(const hal_error_t* error);
/**
* @brief HAL error information context
*/
typedef struct {
hal_error_callback_t callback; /*!< Error callback function */
hal_error_t last_error; /*!< Last error information */
uint32_t error_count; /*!< Total error count */
uint8_t enabled; /*!< Error logging enabled flag */
} hal_error_context_t;
/**
* @brief HAL module types
*/
typedef enum {
HAL_MODULE_GPIO = 0,
HAL_MODULE_UART,
HAL_MODULE_SPI,
HAL_MODULE_I2C,
HAL_MODULE_CAN,
HAL_MODULE_ADC,
HAL_MODULE_DAC,
HAL_MODULE_TIMER,
HAL_MODULE_RTC,
HAL_MODULE_DMA,
HAL_MODULE_MAX
} hal_module_type_t;
/**
* @brief HAL handle structure - base for all HAL peripherals
*/
typedef struct {
void* instance;
void* config;
hal_ret_t status;
uint32_t lock;
void* user_data;
} hal_handle_t;
/**
* @brief HAL module operations structure - unified API for all modules
*/
typedef struct {
hal_ret_t (*init)(hal_handle_t* handle);
hal_ret_t (*deinit)(hal_handle_t* handle);
hal_ret_t (*configure)(hal_handle_t* handle, void* config);
hal_ret_t (*control)(hal_handle_t* handle, uint32_t cmd, void* param);
hal_ret_t (*get_status)(hal_handle_t* handle, uint32_t* status);
} hal_module_ops_t;
/**
* @brief HAL module configuration structure
*/
typedef struct {
hal_module_type_t type;
const char* name;
uint32_t version;
hal_module_ops_t ops;
void* default_config;
} hal_module_config_t;
/**
* @brief HAL initialization structure
*/
typedef struct {
uint32_t module_count;
const hal_module_config_t* modules;
uint32_t flags;
} hal_init_t;
/**
* @brief HAL flags definitions
*/
#define HAL_INIT_FLAG_DEBUG (1 << 0) /*!< Enable debug mode */
#define HAL_INIT_FLAG_NO_CACHE (1 << 1) /*!< Disable cache */
#define HAL_INIT_FLAG_SAFE_MODE (1 << 2) /*!< Enable safe mode */
/**
* @brief HAL peripheral state definitions
*/
typedef enum {
HAL_PERIPH_STATE_RESET = 0,
HAL_PERIPH_STATE_READY,
HAL_PERIPH_STATE_BUSY,
HAL_PERIPH_STATE_TIMEOUT,
HAL_PERIPH_STATE_ERROR
} hal_periph_state_t;
/**
* @brief HAL lock/unlock macros
*/
#define HAL_LOCK(handle) do { while((handle)->lock); (handle)->lock = 1; } while(0)
#define HAL_UNLOCK(handle) do { (handle)->lock = 0; } while(0)
/**
* @brief HAL handle initialization macro
*/
#define HAL_HANDLE_INIT(handle) \
do { \
memset((handle), 0, sizeof(hal_handle_t)); \
(handle)->status = HAL_RET_OK; \
(handle)->lock = 0; \
} while(0)
/**
* @brief HAL module registration macro
*/
#define HAL_MODULE_REGISTER(module) \
static const hal_module_config_t __hal_module_##module##_config = { \
.type = HAL_MODULE_##module, \
.name = #module, \
.version = 1, \
.ops = __hal_##module##_ops, \
.default_config = &__hal_##module##_default_config \
};
/**
* @brief HAL module registration at startup
*/
#define HAL_MODULE_REGISTER_STARTUP(module) \
HAL_MODULE_REGISTER(module) \
__attribute__((constructor)) static void __hal_register_##module##_at_startup(void) { \
hal_module_register(&__hal_module_##module##_config); \
}
/**
* @brief HAL module initialization
*/
void hal_init(void);
hal_ret_t hal_init(void);
/**
* @brief Register a HAL module
* @param module_config: Module configuration
* @retval HAL status code
*/
hal_ret_t hal_module_register(const hal_module_config_t* module_config);
/**
* @brief Initialize a specific HAL module
* @param module_type: Module type
* @param handle: HAL handle for the module
* @retval HAL status code
*/
hal_ret_t hal_module_init(hal_module_type_t module_type, hal_handle_t* handle);
/**
* @brief Deinitialize a specific HAL module
* @param module_type: Module type
* @param handle: HAL handle for the module
* @retval HAL status code
*/
hal_ret_t hal_module_deinit(hal_module_type_t module_type, hal_handle_t* handle);
/**
* @brief Configure a specific HAL module
* @param module_type: Module type
* @param handle: HAL handle for the module
* @param config: Module configuration data
* @retval HAL status code
*/
hal_ret_t hal_module_configure(hal_module_type_t module_type, hal_handle_t* handle, void* config);
/**
* @brief Control a specific HAL module
* @param module_type: Module type
* @param handle: HAL handle for the module
* @param cmd: Control command
* @param param: Control parameter
* @retval HAL status code
*/
hal_ret_t hal_module_control(hal_module_type_t module_type, hal_handle_t* handle, uint32_t cmd, void* param);
/**
* @brief Get status of a specific HAL module
* @param module_type: Module type
* @param handle: HAL handle for the module
* @param status: Pointer to store module status
* @retval HAL status code
*/
hal_ret_t hal_module_get_status(hal_module_type_t module_type, hal_handle_t* handle, uint32_t* status);
/**
* @brief Get default configuration for a module
* @param module_type: Module type
* @return Pointer to default configuration, or NULL if not available
*/
void* hal_module_get_default_config(hal_module_type_t module_type);
/**
* @brief Get HAL version information
* @param major: Pointer to store major version
* @param minor: Pointer to store minor version
* @param patch: Pointer to store patch version
* @retval HAL status code
*/
hal_ret_t hal_get_version(uint8_t* major, uint8_t* minor, uint8_t* patch);
/**
* @brief Get HAL version string
* @retval Version string
*/
const char* hal_get_version_string(void);
/**
* @brief Get HAL module count
* @retval Number of registered HAL modules
*/
uint32_t hal_get_module_count(void);
/**
* @brief Get HAL module configuration
* @param module_type: Module type
* @retval Pointer to module configuration, or NULL if not found
*/
const hal_module_config_t* hal_get_module_config(hal_module_type_t module_type);
/**
* @brief Initialize HAL error handling system
* @retval HAL status code
*/
hal_ret_t hal_error_init(void);
/**
* @brief Deinitialize HAL error handling system
* @retval HAL status code
*/
hal_ret_t hal_error_deinit(void);
/**
* @brief Register an error callback function
* @param callback: Error callback function
* @retval HAL status code
*/
hal_ret_t hal_error_register_callback(hal_error_callback_t callback);
/**
* @brief Record an error
* @param error: Pointer to error information
* @retval HAL status code
*/
hal_ret_t hal_error_record(const hal_error_t* error);
/**
* @brief Get last error information
* @retval Pointer to last error information
*/
const hal_error_t* hal_error_get_last(void);
/**
* @brief Get total error count
* @retval Total error count
*/
uint32_t hal_error_get_count(void);
/**
* @brief Clear error information
* @retval HAL status code
*/
hal_ret_t hal_error_clear(void);
/**
* @brief Enable/disable error logging
* @param enable: Enable flag (1 to enable, 0 to disable)
* @retval HAL status code
*/
hal_ret_t hal_error_enable(uint8_t enable);
#endif /* HAL_H */

175
HAL/Inc/hal_adc.h Normal file
View File

@ -0,0 +1,175 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : hal_adc.h
* @brief : ADC hardware abstraction layer header file
******************************************************************************
*/
/* USER CODE END Header */
#ifndef HAL_ADC_H
#define HAL_ADC_H
#include <stdint.h>
#include <stddef.h>
#include "hal.h"
/**
* @brief ADC instance identifier definitions
*/
typedef enum {
HAL_ADC_INSTANCE_1 = 0,
HAL_ADC_INSTANCE_2,
HAL_ADC_INSTANCE_3,
HAL_ADC_INSTANCE_MAX
} hal_adc_instance_t;
/**
* @brief ADC resolution definitions
*/
typedef enum {
HAL_ADC_RESOLUTION_12B = 0,
HAL_ADC_RESOLUTION_10B,
HAL_ADC_RESOLUTION_8B,
HAL_ADC_RESOLUTION_6B
} hal_adc_resolution_t;
/**
* @brief ADC channel definitions
*/
typedef enum {
HAL_ADC_CHANNEL_0 = 0,
HAL_ADC_CHANNEL_1,
HAL_ADC_CHANNEL_2,
HAL_ADC_CHANNEL_3,
HAL_ADC_CHANNEL_4,
HAL_ADC_CHANNEL_5,
HAL_ADC_CHANNEL_6,
HAL_ADC_CHANNEL_7,
HAL_ADC_CHANNEL_8,
HAL_ADC_CHANNEL_9,
HAL_ADC_CHANNEL_10,
HAL_ADC_CHANNEL_11,
HAL_ADC_CHANNEL_12,
HAL_ADC_CHANNEL_13,
HAL_ADC_CHANNEL_14,
HAL_ADC_CHANNEL_15,
HAL_ADC_CHANNEL_16,
HAL_ADC_CHANNEL_17,
HAL_ADC_CHANNEL_18,
HAL_ADC_CHANNEL_MAX
} hal_adc_channel_t;
/**
* @brief ADC sample time definitions
*/
typedef enum {
HAL_ADC_SAMPLETIME_3CYCLES = 0,
HAL_ADC_SAMPLETIME_15CYCLES,
HAL_ADC_SAMPLETIME_28CYCLES,
HAL_ADC_SAMPLETIME_56CYCLES,
HAL_ADC_SAMPLETIME_84CYCLES,
HAL_ADC_SAMPLETIME_112CYCLES,
HAL_ADC_SAMPLETIME_144CYCLES,
HAL_ADC_SAMPLETIME_480CYCLES
} hal_adc_sampletime_t;
/**
* @brief ADC channel configuration structure
*/
typedef struct {
hal_adc_channel_t channel;
hal_adc_sampletime_t sampletime;
uint8_t rank;
uint8_t enable;
} hal_adc_channel_config_t;
/**
* @brief ADC configuration structure
*/
typedef struct {
hal_adc_instance_t instance;
hal_adc_resolution_t resolution;
uint8_t scan_conversion_mode;
uint8_t continuous_conversion_mode;
uint8_t discontinuous_conversion_mode;
uint8_t discontinuous_number_of_conversions;
uint8_t nbr_of_conversions;
uint8_t channel_count;
const hal_adc_channel_config_t* channels;
uint32_t timeout;
} hal_adc_config_t;
/**
* @brief Initialize ADC hardware
* @param config: ADC configuration structure
* @retval HAL status code
*/
hal_ret_t hal_adc_init(const hal_adc_config_t *config);
/**
* @brief Deinitialize ADC hardware
* @param instance: ADC instance identifier
* @retval HAL status code
*/
hal_ret_t hal_adc_deinit(hal_adc_instance_t instance);
/**
* @brief Start ADC conversion
* @param instance: ADC instance identifier
* @retval HAL status code
*/
hal_ret_t hal_adc_start(hal_adc_instance_t instance);
/**
* @brief Stop ADC conversion
* @param instance: ADC instance identifier
* @retval HAL status code
*/
hal_ret_t hal_adc_stop(hal_adc_instance_t instance);
/**
* @brief Get ADC conversion value
* @param instance: ADC instance identifier
* @param channel: ADC channel
* @param value: Pointer to store conversion value
* @param timeout: Timeout in milliseconds
* @retval HAL status code
*/
hal_ret_t hal_adc_get_value(hal_adc_instance_t instance, hal_adc_channel_t channel, uint16_t *value, uint32_t timeout);
/**
* @brief Get multiple ADC conversion values
* @param instance: ADC instance identifier
* @param values: Pointer to store conversion values
* @param length: Number of values to get
* @param timeout: Timeout in milliseconds
* @retval HAL status code
*/
hal_ret_t hal_adc_get_values(hal_adc_instance_t instance, uint16_t *values, uint8_t length, uint32_t timeout);
/**
* @brief Configure ADC channel
* @param instance: ADC instance identifier
* @param config: ADC channel configuration structure
* @retval HAL status code
*/
hal_ret_t hal_adc_configure_channel(hal_adc_instance_t instance, const hal_adc_channel_config_t *config);
/**
* @brief Enable ADC channel
* @param instance: ADC instance identifier
* @param channel: ADC channel
* @retval HAL status code
*/
hal_ret_t hal_adc_enable_channel(hal_adc_instance_t instance, hal_adc_channel_t channel);
/**
* @brief Disable ADC channel
* @param instance: ADC instance identifier
* @param channel: ADC channel
* @retval HAL status code
*/
hal_ret_t hal_adc_disable_channel(hal_adc_instance_t instance, hal_adc_channel_t channel);
#endif /* HAL_ADC_H */

180
HAL/Inc/hal_can.h Normal file
View File

@ -0,0 +1,180 @@
/* 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 */

View File

@ -10,6 +10,8 @@
#ifndef HAL_GPIO_H
#define HAL_GPIO_H
#include "hal.h"
/**
* @brief GPIO pin state definitions
*/
@ -105,29 +107,33 @@ typedef struct {
/**
* @brief Initialize GPIO hardware
* @retval HAL status code
*/
void hal_gpio_init(void);
hal_ret_t hal_gpio_init(void);
/**
* @brief Configure GPIO pin
* @param config: GPIO configuration structure
* @retval HAL status code
*/
void hal_gpio_configure_pin(const hal_gpio_config_t *config);
hal_ret_t hal_gpio_configure_pin(const hal_gpio_config_t *config);
/**
* @brief Write GPIO pin state
* @param port: GPIO port
* @param pin: GPIO pin
* @param state: GPIO pin state
* @retval HAL status code
*/
void hal_gpio_write_pin(hal_gpio_port_t port, hal_gpio_pin_t pin, hal_gpio_pin_state_t state);
hal_ret_t hal_gpio_write_pin(hal_gpio_port_t port, hal_gpio_pin_t pin, hal_gpio_pin_state_t state);
/**
* @brief Toggle GPIO pin state
* @param port: GPIO port
* @param pin: GPIO pin
* @retval HAL status code
*/
void hal_gpio_toggle_pin(hal_gpio_port_t port, hal_gpio_pin_t pin);
hal_ret_t hal_gpio_toggle_pin(hal_gpio_port_t port, hal_gpio_pin_t pin);
/**
* @brief Read GPIO pin state

125
HAL/Inc/hal_i2c.h Normal file
View File

@ -0,0 +1,125 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : hal_i2c.h
* @brief : I2C hardware abstraction layer header file
******************************************************************************
*/
/* USER CODE END Header */
#ifndef HAL_I2C_H
#define HAL_I2C_H
#include <stdint.h>
#include <stddef.h>
#include "hal.h"
/**
* @brief I2C instance identifier definitions
*/
typedef enum {
HAL_I2C_INSTANCE_1 = 0,
HAL_I2C_INSTANCE_2,
HAL_I2C_INSTANCE_3,
HAL_I2C_INSTANCE_4,
HAL_I2C_INSTANCE_MAX
} hal_i2c_instance_t;
/**
* @brief I2C clock speed definitions
*/
typedef enum {
HAL_I2C_SPEED_STANDARD = 100000U, /*!< Standard speed (100 kHz) */
HAL_I2C_SPEED_FAST = 400000U, /*!< Fast speed (400 kHz) */
HAL_I2C_SPEED_FAST_PLUS = 1000000U, /*!< Fast plus speed (1 MHz) */
HAL_I2C_SPEED_HIGH = 3400000U /*!< High speed (3.4 MHz) */
} hal_i2c_speed_t;
/**
* @brief I2C address mode definitions
*/
typedef enum {
HAL_I2C_ADDRESS_7BIT = 0,
HAL_I2C_ADDRESS_10BIT
} hal_i2c_address_mode_t;
/**
* @brief I2C duty cycle definitions
*/
typedef enum {
HAL_I2C_DUTYCYCLE_2 = 0,
HAL_I2C_DUTYCYCLE_16_9
} hal_i2c_dutycycle_t;
/**
* @brief I2C configuration structure
*/
typedef struct {
hal_i2c_instance_t instance;
hal_i2c_speed_t speed;
hal_i2c_address_mode_t address_mode;
hal_i2c_dutycycle_t dutycycle;
uint16_t own_address1;
uint32_t timeout;
} hal_i2c_config_t;
/**
* @brief Initialize I2C hardware
* @param config: I2C configuration structure
* @retval HAL status code
*/
hal_ret_t hal_i2c_init(const hal_i2c_config_t *config);
/**
* @brief Deinitialize I2C hardware
* @param instance: I2C instance identifier
* @retval HAL status code
*/
hal_ret_t hal_i2c_deinit(hal_i2c_instance_t instance);
/**
* @brief Transmit data to I2C slave
* @param instance: I2C instance identifier
* @param dev_address: Slave device address
* @param data: Pointer to data buffer
* @param length: Data length in bytes
* @param timeout: Timeout in milliseconds
* @retval HAL status code
*/
hal_ret_t hal_i2c_master_transmit(hal_i2c_instance_t instance, uint16_t dev_address, const uint8_t *data, uint16_t length, uint32_t timeout);
/**
* @brief Receive data from I2C slave
* @param instance: I2C instance identifier
* @param dev_address: Slave device address
* @param data: Pointer to data buffer
* @param length: Data length to receive in bytes
* @param timeout: Timeout in milliseconds
* @retval HAL status code
*/
hal_ret_t hal_i2c_master_receive(hal_i2c_instance_t instance, uint16_t dev_address, uint8_t *data, uint16_t length, uint32_t timeout);
/**
* @brief Transmit and receive data from I2C slave
* @param instance: I2C instance identifier
* @param dev_address: Slave device address
* @param tx_data: Pointer to transmit data buffer
* @param tx_length: Transmit data length in bytes
* @param rx_data: Pointer to receive data buffer
* @param rx_length: Receive data length in bytes
* @param timeout: Timeout in milliseconds
* @retval HAL status code
*/
hal_ret_t hal_i2c_master_transmit_receive(hal_i2c_instance_t instance, uint16_t dev_address, const uint8_t *tx_data, uint16_t tx_length, uint8_t *rx_data, uint16_t rx_length, uint32_t timeout);
/**
* @brief Check if I2C bus is ready
* @param instance: I2C instance identifier
* @param dev_address: Slave device address
* @param trials: Number of trials
* @param timeout: Timeout in milliseconds
* @retval HAL status code
*/
hal_ret_t hal_i2c_is_device_ready(hal_i2c_instance_t instance, uint16_t dev_address, uint32_t trials, uint32_t timeout);
#endif /* HAL_I2C_H */

View File

@ -13,6 +13,7 @@
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "hal.h"
/**
* @brief SPI instance identifier definitions
@ -75,34 +76,34 @@ typedef struct {
* @brief Initialize SPI hardware
* @param instance SPI instance identifier
* @param config SPI configuration structure
* @return true if initialization is successful, false otherwise
* @retval HAL status code
*/
bool hal_spi_init(hal_spi_instance_t instance, const hal_spi_config_t *config);
hal_ret_t hal_spi_init(hal_spi_instance_t instance, const hal_spi_config_t *config);
/**
* @brief Deinitialize SPI hardware
* @param instance SPI instance identifier
* @return true if deinitialization is successful, false otherwise
* @retval HAL status code
*/
bool hal_spi_deinit(hal_spi_instance_t instance);
hal_ret_t hal_spi_deinit(hal_spi_instance_t instance);
/**
* @brief Transmit data over specific SPI instance
* @param instance: SPI instance identifier
* @param data: Pointer to data buffer
* @param size: Data length in bytes
* @return true if transmission is successful, false otherwise
* @retval HAL status code
*/
bool hal_spi_transmit(hal_spi_instance_t instance, const uint8_t *data, uint16_t size);
hal_ret_t hal_spi_transmit(hal_spi_instance_t instance, const uint8_t *data, uint16_t size);
/**
* @brief Receive data from specific SPI instance
* @param instance: SPI instance identifier
* @param data: Pointer to data buffer
* @param size: Data length to receive in bytes
* @return true if reception is successful, false otherwise
* @retval HAL status code
*/
bool hal_spi_receive(hal_spi_instance_t instance, uint8_t *data, uint16_t size);
hal_ret_t hal_spi_receive(hal_spi_instance_t instance, uint8_t *data, uint16_t size);
/**
* @brief Transmit and receive data simultaneously over specific SPI instance
@ -110,8 +111,8 @@ bool hal_spi_receive(hal_spi_instance_t instance, uint8_t *data, uint16_t size);
* @param tx_data: Pointer to transmit data buffer
* @param rx_data: Pointer to receive data buffer
* @param size: Data length in bytes
* @return true if transmission and reception are successful, false otherwise
* @retval HAL status code
*/
bool hal_spi_transmit_receive(hal_spi_instance_t instance, const uint8_t *tx_data, uint8_t *rx_data, uint16_t size);
hal_ret_t hal_spi_transmit_receive(hal_spi_instance_t instance, const uint8_t *tx_data, uint8_t *rx_data, uint16_t size);
#endif /* HAL_SPI_H */

View File

@ -12,6 +12,7 @@
#include <stdint.h>
#include <stddef.h>
#include "hal.h"
/**
* @brief UART parity definitions
@ -64,44 +65,50 @@ typedef struct {
/**
* @brief Initialize UART hardware
* @retval HAL status code
*/
void hal_uart_init(void);
hal_ret_t hal_uart_init(void);
/**
* @brief Configure UART parameters for specific instance
* @param config: UART configuration structure
* @retval HAL status code
*/
void hal_uart_config(const hal_uart_config_t *config);
hal_ret_t hal_uart_config(const hal_uart_config_t *config);
/**
* @brief Send data over specific UART instance
* @param instance: UART instance identifier
* @param data: Pointer to data buffer
* @param length: Data length in bytes
* @retval HAL status code
*/
void hal_uart_send(hal_uart_instance_t instance, const uint8_t *data, size_t length);
hal_ret_t hal_uart_send(hal_uart_instance_t instance, const uint8_t *data, size_t length);
/**
* @brief Receive data from specific UART instance
* @param instance: UART instance identifier
* @param data: Pointer to data buffer
* @param length: Data length to receive in bytes
* @retval Number of bytes received
* @param received: Pointer to store number of bytes received
* @retval HAL status code
*/
size_t hal_uart_receive(hal_uart_instance_t instance, uint8_t *data, size_t length);
hal_ret_t hal_uart_receive(hal_uart_instance_t instance, uint8_t *data, size_t length, size_t *received);
/**
* @brief Check if specific UART instance is ready to send
* @param instance: UART instance identifier
* @retval 1 if ready, 0 otherwise
* @param ready: Pointer to store ready status (1 if ready, 0 otherwise)
* @retval HAL status code
*/
uint8_t hal_uart_is_tx_ready(hal_uart_instance_t instance);
hal_ret_t hal_uart_is_tx_ready(hal_uart_instance_t instance, uint8_t *ready);
/**
* @brief Check if specific UART instance has data to receive
* @param instance: UART instance identifier
* @retval 1 if data available, 0 otherwise
* @param ready: Pointer to store ready status (1 if data available, 0 otherwise)
* @retval HAL status code
*/
uint8_t hal_uart_is_rx_ready(hal_uart_instance_t instance);
hal_ret_t hal_uart_is_rx_ready(hal_uart_instance_t instance, uint8_t *ready);
#endif /* HAL_UART_H */