进一步迭代优化统一管理

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 */

View File

@ -8,15 +8,343 @@
/* USER CODE END Header */
#include "hal.h"
#include <string.h>
/**
* @brief HAL module registry
*/
static hal_module_config_t hal_module_registry[HAL_MODULE_MAX];
static uint32_t hal_module_count = 0;
/**
* @brief HAL error context
*/
static hal_error_context_t hal_error_ctx = {
.callback = NULL,
.error_count = 0,
.enabled = 1
};
/**
* @brief HAL module initialization
* @retval HAL status code
*/
void hal_init(void) {
hal_ret_t hal_init(void) {
hal_ret_t status = HAL_RET_OK;
/* Clear module registry */
memset(hal_module_registry, 0, sizeof(hal_module_registry));
hal_module_count = 0;
/* Initialize error handling system */
status = hal_error_init();
if (status != HAL_RET_OK) {
return status;
}
/* Call architecture specific initialization */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F4
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
hal_stm32f1_init();
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
hal_stm32f4_init();
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
hal_stm32f7_init();
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
hal_stm32l4_init();
#else
#error "Unsupported HAL architecture"
return HAL_RET_ERROR;
#endif
return HAL_RET_OK;
}
/**
* @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) {
if (module_config == NULL) {
return HAL_RET_INVALID_PARAM;
}
if (module_config->type >= HAL_MODULE_MAX) {
return HAL_RET_INVALID_PARAM;
}
/* Check if module already registered */
if (hal_module_registry[module_config->type].ops.init != NULL) {
return HAL_RET_RESOURCE_LOCKED;
}
/* Register the module */
memcpy(&hal_module_registry[module_config->type], module_config, sizeof(hal_module_config_t));
hal_module_count++;
return HAL_RET_OK;
}
/**
* @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) {
if (module_type >= HAL_MODULE_MAX || handle == NULL) {
return HAL_RET_INVALID_PARAM;
}
if (hal_module_registry[module_type].ops.init == NULL) {
return HAL_RET_NOT_SUPPORTED;
}
/* Initialize handle if not already initialized */
if (handle->status == HAL_PERIPH_STATE_RESET) {
HAL_HANDLE_INIT(handle);
handle->status = HAL_PERIPH_STATE_READY;
}
return hal_module_registry[module_type].ops.init(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) {
if (module_type >= HAL_MODULE_MAX || handle == NULL) {
return HAL_RET_INVALID_PARAM;
}
if (hal_module_registry[module_type].ops.deinit == NULL) {
return HAL_RET_NOT_SUPPORTED;
}
hal_ret_t status = hal_module_registry[module_type].ops.deinit(handle);
if (status == HAL_RET_OK) {
handle->status = HAL_PERIPH_STATE_RESET;
}
return status;
}
/**
* @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) {
if (module_type >= HAL_MODULE_MAX || handle == NULL) {
return HAL_RET_INVALID_PARAM;
}
if (hal_module_registry[module_type].ops.configure == NULL) {
return HAL_RET_NOT_SUPPORTED;
}
return hal_module_registry[module_type].ops.configure(handle, 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) {
if (module_type >= HAL_MODULE_MAX || handle == NULL) {
return HAL_RET_INVALID_PARAM;
}
if (hal_module_registry[module_type].ops.control == NULL) {
return HAL_RET_NOT_SUPPORTED;
}
return hal_module_registry[module_type].ops.control(handle, cmd, 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) {
if (module_type >= HAL_MODULE_MAX || handle == NULL || status == NULL) {
return HAL_RET_INVALID_PARAM;
}
if (hal_module_registry[module_type].ops.get_status == NULL) {
return HAL_RET_NOT_SUPPORTED;
}
return hal_module_registry[module_type].ops.get_status(handle, 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) {
if (module_type >= HAL_MODULE_MAX) {
return NULL;
}
return hal_module_registry[module_type].default_config;
}
/**
* @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) {
if (major == NULL || minor == NULL || patch == NULL) {
return HAL_RET_INVALID_PARAM;
}
*major = HAL_VERSION_MAJOR;
*minor = HAL_VERSION_MINOR;
*patch = HAL_VERSION_PATCH;
return HAL_RET_OK;
}
/**
* @brief Get HAL version string
* @retval Version string
*/
const char* hal_get_version_string(void) {
return HAL_VERSION_STRING;
}
/**
* @brief Get HAL module count
* @retval Number of registered HAL modules
*/
uint32_t hal_get_module_count(void) {
return hal_module_count;
}
/**
* @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) {
if (module_type >= HAL_MODULE_MAX) {
return NULL;
}
if (hal_module_registry[module_type].ops.init == NULL) {
return NULL;
}
return &hal_module_registry[module_type];
}
/**
* @brief Initialize HAL error handling system
* @retval HAL status code
*/
hal_ret_t hal_error_init(void) {
memset(&hal_error_ctx.last_error, 0, sizeof(hal_error_t));
hal_error_ctx.error_count = 0;
hal_error_ctx.enabled = 1;
hal_error_ctx.callback = NULL;
return HAL_RET_OK;
}
/**
* @brief Deinitialize HAL error handling system
* @retval HAL status code
*/
hal_ret_t hal_error_deinit(void) {
hal_error_ctx.enabled = 0;
hal_error_ctx.callback = NULL;
return HAL_RET_OK;
}
/**
* @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) {
hal_error_ctx.callback = callback;
return HAL_RET_OK;
}
/**
* @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) {
if (error == NULL) {
return HAL_RET_INVALID_PARAM;
}
if (!hal_error_ctx.enabled) {
return HAL_RET_OK;
}
/* Copy error information */
memcpy(&hal_error_ctx.last_error, error, sizeof(hal_error_t));
hal_error_ctx.error_count++;
/* Call error callback if registered */
if (hal_error_ctx.callback != NULL) {
hal_error_ctx.callback(error);
}
return HAL_RET_OK;
}
/**
* @brief Get last error information
* @retval Pointer to last error information
*/
const hal_error_t* hal_error_get_last(void) {
return &hal_error_ctx.last_error;
}
/**
* @brief Get total error count
* @retval Total error count
*/
uint32_t hal_error_get_count(void) {
return hal_error_ctx.error_count;
}
/**
* @brief Clear error information
* @retval HAL status code
*/
hal_ret_t hal_error_clear(void) {
memset(&hal_error_ctx.last_error, 0, sizeof(hal_error_t));
hal_error_ctx.error_count = 0;
return HAL_RET_OK;
}
/**
* @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) {
hal_error_ctx.enabled = enable;
return HAL_RET_OK;
}

313
HAL/Src/hal_adc.c Normal file
View File

@ -0,0 +1,313 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : hal_adc.c
* @brief : ADC hardware abstraction layer source file
******************************************************************************
*/
/* USER CODE END Header */
#include "hal.h"
#include "hal_adc.h"
/**
* @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) {
if (config == NULL) {
return HAL_INVALID_PARAM;
}
if (config->instance >= HAL_ADC_INSTANCE_MAX) {
return HAL_INVALID_PARAM;
}
/* Call architecture specific ADC initialization */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
/* TODO: Implement STM32F1 ADC initialization */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
/* TODO: Implement STM32F4 ADC initialization */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
/* TODO: Implement STM32F7 ADC initialization */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
/* TODO: Implement STM32L4 ADC initialization */
return HAL_OK;
#else
#error "Unsupported HAL architecture"
return HAL_ERROR;
#endif
}
/**
* @brief Deinitialize ADC hardware
* @param instance: ADC instance identifier
* @retval HAL status code
*/
hal_ret_t hal_adc_deinit(hal_adc_instance_t instance) {
if (instance >= HAL_ADC_INSTANCE_MAX) {
return HAL_INVALID_PARAM;
}
/* Call architecture specific ADC deinitialization */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
/* TODO: Implement STM32F1 ADC deinitialization */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
/* TODO: Implement STM32F4 ADC deinitialization */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
/* TODO: Implement STM32F7 ADC deinitialization */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
/* TODO: Implement STM32L4 ADC deinitialization */
return HAL_OK;
#else
#error "Unsupported HAL architecture"
return HAL_ERROR;
#endif
}
/**
* @brief Start ADC conversion
* @param instance: ADC instance identifier
* @retval HAL status code
*/
hal_ret_t hal_adc_start(hal_adc_instance_t instance) {
if (instance >= HAL_ADC_INSTANCE_MAX) {
return HAL_INVALID_PARAM;
}
/* Call architecture specific ADC start implementation */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
/* TODO: Implement STM32F1 ADC start */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
/* TODO: Implement STM32F4 ADC start */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
/* TODO: Implement STM32F7 ADC start */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
/* TODO: Implement STM32L4 ADC start */
return HAL_OK;
#else
#error "Unsupported HAL architecture"
return HAL_ERROR;
#endif
}
/**
* @brief Stop ADC conversion
* @param instance: ADC instance identifier
* @retval HAL status code
*/
hal_ret_t hal_adc_stop(hal_adc_instance_t instance) {
if (instance >= HAL_ADC_INSTANCE_MAX) {
return HAL_INVALID_PARAM;
}
/* Call architecture specific ADC stop implementation */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
/* TODO: Implement STM32F1 ADC stop */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
/* TODO: Implement STM32F4 ADC stop */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
/* TODO: Implement STM32F7 ADC stop */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
/* TODO: Implement STM32L4 ADC stop */
return HAL_OK;
#else
#error "Unsupported HAL architecture"
return HAL_ERROR;
#endif
}
/**
* @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) {
if (value == NULL) {
return HAL_INVALID_PARAM;
}
if (instance >= HAL_ADC_INSTANCE_MAX || channel >= HAL_ADC_CHANNEL_MAX) {
return HAL_INVALID_PARAM;
}
/* Call architecture specific ADC get value implementation */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
/* TODO: Implement STM32F1 ADC get value */
*value = 0;
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
/* TODO: Implement STM32F4 ADC get value */
*value = 0;
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
/* TODO: Implement STM32F7 ADC get value */
*value = 0;
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
/* TODO: Implement STM32L4 ADC get value */
*value = 0;
return HAL_OK;
#else
#error "Unsupported HAL architecture"
return HAL_ERROR;
#endif
}
/**
* @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) {
if (values == NULL || length == 0) {
return HAL_INVALID_PARAM;
}
if (instance >= HAL_ADC_INSTANCE_MAX) {
return HAL_INVALID_PARAM;
}
/* Call architecture specific ADC get values implementation */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
/* TODO: Implement STM32F1 ADC get values */
for (uint8_t i = 0; i < length; i++) {
values[i] = 0;
}
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
/* TODO: Implement STM32F4 ADC get values */
for (uint8_t i = 0; i < length; i++) {
values[i] = 0;
}
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
/* TODO: Implement STM32F7 ADC get values */
for (uint8_t i = 0; i < length; i++) {
values[i] = 0;
}
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
/* TODO: Implement STM32L4 ADC get values */
for (uint8_t i = 0; i < length; i++) {
values[i] = 0;
}
return HAL_OK;
#else
#error "Unsupported HAL architecture"
return HAL_ERROR;
#endif
}
/**
* @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) {
if (config == NULL) {
return HAL_INVALID_PARAM;
}
if (instance >= HAL_ADC_INSTANCE_MAX || config->channel >= HAL_ADC_CHANNEL_MAX) {
return HAL_INVALID_PARAM;
}
/* Call architecture specific ADC channel configuration implementation */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
/* TODO: Implement STM32F1 ADC channel configuration */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
/* TODO: Implement STM32F4 ADC channel configuration */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
/* TODO: Implement STM32F7 ADC channel configuration */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
/* TODO: Implement STM32L4 ADC channel configuration */
return HAL_OK;
#else
#error "Unsupported HAL architecture"
return HAL_ERROR;
#endif
}
/**
* @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) {
if (instance >= HAL_ADC_INSTANCE_MAX || channel >= HAL_ADC_CHANNEL_MAX) {
return HAL_INVALID_PARAM;
}
/* Call architecture specific ADC channel enable implementation */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
/* TODO: Implement STM32F1 ADC channel enable */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
/* TODO: Implement STM32F4 ADC channel enable */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
/* TODO: Implement STM32F7 ADC channel enable */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
/* TODO: Implement STM32L4 ADC channel enable */
return HAL_OK;
#else
#error "Unsupported HAL architecture"
return HAL_ERROR;
#endif
}
/**
* @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) {
if (instance >= HAL_ADC_INSTANCE_MAX || channel >= HAL_ADC_CHANNEL_MAX) {
return HAL_INVALID_PARAM;
}
/* Call architecture specific ADC channel disable implementation */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
/* TODO: Implement STM32F1 ADC channel disable */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
/* TODO: Implement STM32F4 ADC channel disable */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
/* TODO: Implement STM32F7 ADC channel disable */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
/* TODO: Implement STM32L4 ADC channel disable */
return HAL_OK;
#else
#error "Unsupported HAL architecture"
return HAL_ERROR;
#endif
}

275
HAL/Src/hal_can.c Normal file
View File

@ -0,0 +1,275 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : hal_can.c
* @brief : CAN hardware abstraction layer source file
******************************************************************************
*/
/* USER CODE END Header */
#include "hal.h"
#include "hal_can.h"
/**
* @brief Initialize CAN hardware
* @param config: CAN configuration structure
* @retval HAL status code
*/
hal_status_t hal_can_init(const hal_can_config_t *config) {
if (config == NULL) {
return HAL_INVALID_PARAM;
}
if (config->instance >= HAL_CAN_INSTANCE_MAX) {
return HAL_INVALID_PARAM;
}
/* Call architecture specific CAN initialization */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
/* TODO: Implement STM32F1 CAN initialization */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
/* TODO: Implement STM32F4 CAN initialization */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
/* TODO: Implement STM32F7 CAN initialization */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
/* TODO: Implement STM32L4 CAN initialization */
return HAL_OK;
#else
#error "Unsupported HAL architecture"
return HAL_ERROR;
#endif
}
/**
* @brief Deinitialize CAN hardware
* @param instance: CAN instance identifier
* @retval HAL status code
*/
hal_status_t hal_can_deinit(hal_can_instance_t instance) {
if (instance >= HAL_CAN_INSTANCE_MAX) {
return HAL_INVALID_PARAM;
}
/* Call architecture specific CAN deinitialization */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
/* TODO: Implement STM32F1 CAN deinitialization */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
/* TODO: Implement STM32F4 CAN deinitialization */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
/* TODO: Implement STM32F7 CAN deinitialization */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
/* TODO: Implement STM32L4 CAN deinitialization */
return HAL_OK;
#else
#error "Unsupported HAL architecture"
return HAL_ERROR;
#endif
}
/**
* @brief Start CAN peripheral
* @param instance: CAN instance identifier
* @retval HAL status code
*/
hal_status_t hal_can_start(hal_can_instance_t instance) {
if (instance >= HAL_CAN_INSTANCE_MAX) {
return HAL_INVALID_PARAM;
}
/* Call architecture specific CAN start implementation */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
/* TODO: Implement STM32F1 CAN start */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
/* TODO: Implement STM32F4 CAN start */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
/* TODO: Implement STM32F7 CAN start */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
/* TODO: Implement STM32L4 CAN start */
return HAL_OK;
#else
#error "Unsupported HAL architecture"
return HAL_ERROR;
#endif
}
/**
* @brief Stop CAN peripheral
* @param instance: CAN instance identifier
* @retval HAL status code
*/
hal_status_t hal_can_stop(hal_can_instance_t instance) {
if (instance >= HAL_CAN_INSTANCE_MAX) {
return HAL_INVALID_PARAM;
}
/* Call architecture specific CAN stop implementation */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
/* TODO: Implement STM32F1 CAN stop */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
/* TODO: Implement STM32F4 CAN stop */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
/* TODO: Implement STM32F7 CAN stop */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
/* TODO: Implement STM32L4 CAN stop */
return HAL_OK;
#else
#error "Unsupported HAL architecture"
return HAL_ERROR;
#endif
}
/**
* @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_status_t hal_can_send(hal_can_instance_t instance, const hal_can_message_t *message, uint32_t timeout) {
if (message == NULL) {
return HAL_INVALID_PARAM;
}
if (instance >= HAL_CAN_INSTANCE_MAX) {
return HAL_INVALID_PARAM;
}
/* Call architecture specific CAN send implementation */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
/* TODO: Implement STM32F1 CAN send */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
/* TODO: Implement STM32F4 CAN send */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
/* TODO: Implement STM32F7 CAN send */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
/* TODO: Implement STM32L4 CAN send */
return HAL_OK;
#else
#error "Unsupported HAL architecture"
return HAL_ERROR;
#endif
}
/**
* @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_status_t hal_can_receive(hal_can_instance_t instance, uint8_t fifo, hal_can_message_t *message, uint32_t timeout) {
if (message == NULL || fifo > 1) {
return HAL_INVALID_PARAM;
}
if (instance >= HAL_CAN_INSTANCE_MAX) {
return HAL_INVALID_PARAM;
}
/* Call architecture specific CAN receive implementation */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
/* TODO: Implement STM32F1 CAN receive */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
/* TODO: Implement STM32F4 CAN receive */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
/* TODO: Implement STM32F7 CAN receive */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
/* TODO: Implement STM32L4 CAN receive */
return HAL_OK;
#else
#error "Unsupported HAL architecture"
return HAL_ERROR;
#endif
}
/**
* @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_status_t hal_can_get_pending_count(hal_can_instance_t instance, uint8_t fifo, uint8_t *count) {
if (count == NULL || fifo > 1) {
return HAL_INVALID_PARAM;
}
if (instance >= HAL_CAN_INSTANCE_MAX) {
return HAL_INVALID_PARAM;
}
/* Call architecture specific CAN get pending count implementation */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
/* TODO: Implement STM32F1 CAN get pending count */
*count = 0;
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
/* TODO: Implement STM32F4 CAN get pending count */
*count = 0;
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
/* TODO: Implement STM32F7 CAN get pending count */
*count = 0;
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
/* TODO: Implement STM32L4 CAN get pending count */
*count = 0;
return HAL_OK;
#else
#error "Unsupported HAL architecture"
return HAL_ERROR;
#endif
}
/**
* @brief Configure CAN filter
* @param instance: CAN instance identifier
* @param filter_config: Pointer to CAN filter configuration structure
* @retval HAL status code
*/
hal_status_t hal_can_configure_filter(hal_can_instance_t instance, const hal_can_filter_config_t *filter_config) {
if (filter_config == NULL) {
return HAL_INVALID_PARAM;
}
if (instance >= HAL_CAN_INSTANCE_MAX) {
return HAL_INVALID_PARAM;
}
/* Call architecture specific CAN filter configuration implementation */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
/* TODO: Implement STM32F1 CAN filter configuration */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
/* TODO: Implement STM32F4 CAN filter configuration */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
/* TODO: Implement STM32F7 CAN filter configuration */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
/* TODO: Implement STM32L4 CAN filter configuration */
return HAL_OK;
#else
#error "Unsupported HAL architecture"
return HAL_ERROR;
#endif
}

View File

@ -9,11 +9,13 @@
#include "hal.h"
#include "hal_gpio.h"
#include <stddef.h>
/**
* @brief Initialize GPIO hardware
* @retval HAL status code
*/
void hal_gpio_init(void) {
hal_ret_t hal_gpio_init(void) {
/* Call architecture specific GPIO initialization */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
hal_stm32f1_gpio_init();
@ -25,14 +27,21 @@ void hal_gpio_init(void) {
hal_stm32l4_gpio_init();
#else
#error "Unsupported HAL architecture"
return HAL_RET_ERROR;
#endif
return HAL_RET_OK;
}
/**
* @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) {
if (config == NULL) {
return HAL_RET_INVALID_PARAM;
}
/* Call architecture specific GPIO configuration */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
hal_stm32f1_gpio_configure_pin(config);
@ -44,7 +53,9 @@ void hal_gpio_configure_pin(const hal_gpio_config_t *config) {
hal_stm32l4_gpio_configure_pin(config);
#else
#error "Unsupported HAL architecture"
return HAL_RET_ERROR;
#endif
return HAL_RET_OK;
}
/**
@ -52,8 +63,14 @@ void hal_gpio_configure_pin(const hal_gpio_config_t *config) {
* @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) {
/* Validate parameters */
if (port >= HAL_GPIO_PORT_K + 1 || pin >= HAL_GPIO_PIN_ALL) {
return HAL_RET_INVALID_PARAM;
}
/* Call architecture specific GPIO write implementation */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
hal_stm32f1_gpio_write_pin(port, pin, state);
@ -65,15 +82,23 @@ void hal_gpio_write_pin(hal_gpio_port_t port, hal_gpio_pin_t pin, hal_gpio_pin_s
hal_stm32l4_gpio_write_pin(port, pin, state);
#else
#error "Unsupported HAL architecture"
return HAL_RET_ERROR;
#endif
return HAL_RET_OK;
}
/**
* @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) {
/* Validate parameters */
if (port >= HAL_GPIO_PORT_K + 1 || pin >= HAL_GPIO_PIN_ALL) {
return HAL_RET_INVALID_PARAM;
}
/* Call architecture specific GPIO toggle implementation */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
hal_stm32f1_gpio_toggle_pin(port, pin);
@ -85,7 +110,9 @@ void hal_gpio_toggle_pin(hal_gpio_port_t port, hal_gpio_pin_t pin) {
hal_stm32l4_gpio_toggle_pin(port, pin);
#else
#error "Unsupported HAL architecture"
return HAL_RET_ERROR;
#endif
return HAL_RET_OK;
}
/**

218
HAL/Src/hal_i2c.c Normal file
View File

@ -0,0 +1,218 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : hal_i2c.c
* @brief : I2C hardware abstraction layer source file
******************************************************************************
*/
/* USER CODE END Header */
#include "hal.h"
#include "hal_i2c.h"
/**
* @brief Initialize I2C hardware
* @param config: I2C configuration structure
* @retval HAL status code
*/
hal_status_t hal_i2c_init(const hal_i2c_config_t *config) {
if (config == NULL) {
return HAL_INVALID_PARAM;
}
if (config->instance >= HAL_I2C_INSTANCE_MAX) {
return HAL_INVALID_PARAM;
}
/* Call architecture specific I2C initialization */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
/* TODO: Implement STM32F1 I2C initialization */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
/* TODO: Implement STM32F4 I2C initialization */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
/* TODO: Implement STM32F7 I2C initialization */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
/* TODO: Implement STM32L4 I2C initialization */
return HAL_OK;
#else
#error "Unsupported HAL architecture"
return HAL_ERROR;
#endif
}
/**
* @brief Deinitialize I2C hardware
* @param instance: I2C instance identifier
* @retval HAL status code
*/
hal_status_t hal_i2c_deinit(hal_i2c_instance_t instance) {
if (instance >= HAL_I2C_INSTANCE_MAX) {
return HAL_INVALID_PARAM;
}
/* Call architecture specific I2C deinitialization */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
/* TODO: Implement STM32F1 I2C deinitialization */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
/* TODO: Implement STM32F4 I2C deinitialization */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
/* TODO: Implement STM32F7 I2C deinitialization */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
/* TODO: Implement STM32L4 I2C deinitialization */
return HAL_OK;
#else
#error "Unsupported HAL architecture"
return HAL_ERROR;
#endif
}
/**
* @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_status_t hal_i2c_master_transmit(hal_i2c_instance_t instance, uint16_t dev_address, const uint8_t *data, uint16_t length, uint32_t timeout) {
if (data == NULL || length == 0) {
return HAL_INVALID_PARAM;
}
if (instance >= HAL_I2C_INSTANCE_MAX) {
return HAL_INVALID_PARAM;
}
/* Call architecture specific I2C transmit implementation */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
/* TODO: Implement STM32F1 I2C transmit */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
/* TODO: Implement STM32F4 I2C transmit */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
/* TODO: Implement STM32F7 I2C transmit */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
/* TODO: Implement STM32L4 I2C transmit */
return HAL_OK;
#else
#error "Unsupported HAL architecture"
return HAL_ERROR;
#endif
}
/**
* @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_status_t hal_i2c_master_receive(hal_i2c_instance_t instance, uint16_t dev_address, uint8_t *data, uint16_t length, uint32_t timeout) {
if (data == NULL || length == 0) {
return HAL_INVALID_PARAM;
}
if (instance >= HAL_I2C_INSTANCE_MAX) {
return HAL_INVALID_PARAM;
}
/* Call architecture specific I2C receive implementation */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
/* TODO: Implement STM32F1 I2C receive */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
/* TODO: Implement STM32F4 I2C receive */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
/* TODO: Implement STM32F7 I2C receive */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
/* TODO: Implement STM32L4 I2C receive */
return HAL_OK;
#else
#error "Unsupported HAL architecture"
return HAL_ERROR;
#endif
}
/**
* @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_status_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) {
if (tx_data == NULL || rx_data == NULL || tx_length == 0 || rx_length == 0) {
return HAL_INVALID_PARAM;
}
if (instance >= HAL_I2C_INSTANCE_MAX) {
return HAL_INVALID_PARAM;
}
/* Call architecture specific I2C transmit and receive implementation */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
/* TODO: Implement STM32F1 I2C transmit and receive */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
/* TODO: Implement STM32F4 I2C transmit and receive */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
/* TODO: Implement STM32F7 I2C transmit and receive */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
/* TODO: Implement STM32L4 I2C transmit and receive */
return HAL_OK;
#else
#error "Unsupported HAL architecture"
return HAL_ERROR;
#endif
}
/**
* @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_status_t hal_i2c_is_device_ready(hal_i2c_instance_t instance, uint16_t dev_address, uint32_t trials, uint32_t timeout) {
if (instance >= HAL_I2C_INSTANCE_MAX) {
return HAL_INVALID_PARAM;
}
/* Call architecture specific I2C device ready check implementation */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
/* TODO: Implement STM32F1 I2C device ready check */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
/* TODO: Implement STM32F4 I2C device ready check */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
/* TODO: Implement STM32F7 I2C device ready check */
return HAL_OK;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
/* TODO: Implement STM32L4 I2C device ready check */
return HAL_OK;
#else
#error "Unsupported HAL architecture"
return HAL_ERROR;
#endif
}

View File

@ -15,42 +15,54 @@
* @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) {
if (config == NULL) {
return HAL_RET_INVALID_PARAM;
}
if (instance >= HAL_SPI_INSTANCE_MAX) {
return HAL_RET_INVALID_PARAM;
}
/* Call architecture specific SPI initialization */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
return hal_stm32f1_spi_init(instance, config);
return hal_stm32f1_spi_init(instance, config) ? HAL_RET_OK : HAL_RET_ERROR;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
return hal_stm32f4_spi_init(instance, config);
return hal_stm32f4_spi_init(instance, config) ? HAL_RET_OK : HAL_RET_ERROR;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
return hal_stm32f7_spi_init(instance, config);
return hal_stm32f7_spi_init(instance, config) ? HAL_OK : HAL_ERROR;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
return hal_stm32l4_spi_init(instance, config);
return hal_stm32l4_spi_init(instance, config) ? HAL_OK : HAL_ERROR;
#else
#error "Unsupported HAL architecture"
return false;
return HAL_RET_ERROR;
#endif
}
/**
* @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) {
if (instance >= HAL_SPI_INSTANCE_MAX) {
return HAL_RET_INVALID_PARAM;
}
/* Call architecture specific SPI deinitialization */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
return hal_stm32f1_spi_deinit(instance);
return hal_stm32f1_spi_deinit(instance) ? HAL_OK : HAL_ERROR;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
return hal_stm32f4_spi_deinit(instance);
return hal_stm32f4_spi_deinit(instance) ? HAL_RET_OK : HAL_RET_ERROR;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
return hal_stm32f7_spi_deinit(instance);
return hal_stm32f7_spi_deinit(instance) ? HAL_OK : HAL_ERROR;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
return hal_stm32l4_spi_deinit(instance);
return hal_stm32l4_spi_deinit(instance) ? HAL_OK : HAL_ERROR;
#else
#error "Unsupported HAL architecture"
return false;
return HAL_RET_ERROR;
#endif
}
@ -59,21 +71,29 @@ bool hal_spi_deinit(hal_spi_instance_t instance) {
* @param instance SPI instance identifier
* @param p_data Pointer to data buffer to transmit
* @param size Size of data to transmit
* @return true if transmission is successful, false otherwise
* @retval HAL status code
*/
bool hal_spi_transmit(hal_spi_instance_t instance, const uint8_t *p_data, uint16_t size) {
hal_ret_t hal_spi_transmit(hal_spi_instance_t instance, const uint8_t *p_data, uint16_t size) {
if (p_data == NULL || size == 0) {
return HAL_RET_INVALID_PARAM;
}
if (instance >= HAL_SPI_INSTANCE_MAX) {
return HAL_RET_INVALID_PARAM;
}
/* Call architecture specific SPI transmit implementation */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
return hal_stm32f1_spi_transmit(instance, p_data, size);
return hal_stm32f1_spi_transmit(instance, p_data, size) ? HAL_OK : HAL_ERROR;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
return hal_stm32f4_spi_transmit(instance, p_data, size);
return hal_stm32f4_spi_transmit(instance, p_data, size) ? HAL_RET_OK : HAL_RET_ERROR;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
return hal_stm32f7_spi_transmit(instance, p_data, size);
return hal_stm32f7_spi_transmit(instance, p_data, size) ? HAL_OK : HAL_ERROR;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
return hal_stm32l4_spi_transmit(instance, p_data, size);
return hal_stm32l4_spi_transmit(instance, p_data, size) ? HAL_OK : HAL_ERROR;
#else
#error "Unsupported HAL architecture"
return false;
return HAL_RET_ERROR;
#endif
}
@ -82,21 +102,29 @@ bool hal_spi_transmit(hal_spi_instance_t instance, const uint8_t *p_data, uint16
* @param instance SPI instance identifier
* @param p_data Pointer to data buffer to receive
* @param size Size of data to receive
* @return true if reception is successful, false otherwise
* @retval HAL status code
*/
bool hal_spi_receive(hal_spi_instance_t instance, uint8_t *p_data, uint16_t size) {
hal_ret_t hal_spi_receive(hal_spi_instance_t instance, uint8_t *p_data, uint16_t size) {
if (p_data == NULL || size == 0) {
return HAL_RET_INVALID_PARAM;
}
if (instance >= HAL_SPI_INSTANCE_MAX) {
return HAL_RET_INVALID_PARAM;
}
/* Call architecture specific SPI receive implementation */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
return hal_stm32f1_spi_receive(instance, p_data, size);
return hal_stm32f1_spi_receive(instance, p_data, size) ? HAL_OK : HAL_ERROR;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
return hal_stm32f4_spi_receive(instance, p_data, size);
return hal_stm32f4_spi_receive(instance, p_data, size) ? HAL_RET_OK : HAL_RET_ERROR;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
return hal_stm32f7_spi_receive(instance, p_data, size);
return hal_stm32f7_spi_receive(instance, p_data, size) ? HAL_OK : HAL_ERROR;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
return hal_stm32l4_spi_receive(instance, p_data, size);
return hal_stm32l4_spi_receive(instance, p_data, size) ? HAL_OK : HAL_ERROR;
#else
#error "Unsupported HAL architecture"
return false;
return HAL_RET_ERROR;
#endif
}
@ -106,20 +134,28 @@ bool hal_spi_receive(hal_spi_instance_t instance, uint8_t *p_data, uint16_t size
* @param p_tx_data Pointer to data buffer to transmit
* @param p_rx_data Pointer to data buffer to receive
* @param size Size of data to transmit/receive
* @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 *p_tx_data, uint8_t *p_rx_data, uint16_t size) {
hal_ret_t hal_spi_transmit_receive(hal_spi_instance_t instance, const uint8_t *p_tx_data, uint8_t *p_rx_data, uint16_t size) {
if (p_tx_data == NULL || p_rx_data == NULL || size == 0) {
return HAL_RET_INVALID_PARAM;
}
if (instance >= HAL_SPI_INSTANCE_MAX) {
return HAL_RET_INVALID_PARAM;
}
/* Call architecture specific SPI transmit and receive implementation */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
return hal_stm32f1_spi_transmit_receive(instance, p_tx_data, p_rx_data, size);
return hal_stm32f1_spi_transmit_receive(instance, p_tx_data, p_rx_data, size) ? HAL_OK : HAL_ERROR;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
return hal_stm32f4_spi_transmit_receive(instance, p_tx_data, p_rx_data, size);
return hal_stm32f4_spi_transmit_receive(instance, p_tx_data, p_rx_data, size) ? HAL_RET_OK : HAL_RET_ERROR;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
return hal_stm32f7_spi_transmit_receive(instance, p_tx_data, p_rx_data, size);
return hal_stm32f7_spi_transmit_receive(instance, p_tx_data, p_rx_data, size) ? HAL_OK : HAL_ERROR;
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
return hal_stm32l4_spi_transmit_receive(instance, p_tx_data, p_rx_data, size);
return hal_stm32l4_spi_transmit_receive(instance, p_tx_data, p_rx_data, size) ? HAL_OK : HAL_ERROR;
#else
#error "Unsupported HAL architecture"
return false;
return HAL_RET_ERROR;
#endif
}

View File

@ -12,8 +12,9 @@
/**
* @brief Initialize UART hardware
* @retval HAL status code
*/
void hal_uart_init(void) {
hal_ret_t hal_uart_init(void) {
/* Call architecture specific UART initialization */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
hal_stm32f1_uart_init();
@ -25,14 +26,25 @@ void hal_uart_init(void) {
hal_stm32l4_uart_init();
#else
#error "Unsupported HAL architecture"
return HAL_RET_ERROR;
#endif
return HAL_RET_OK;
}
/**
* @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) {
if (config == NULL) {
return HAL_RET_INVALID_PARAM;
}
if (config->instance >= HAL_UART_INSTANCE_MAX) {
return HAL_RET_INVALID_PARAM;
}
/* Call architecture specific UART configuration */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
hal_stm32f1_uart_config(config);
@ -44,7 +56,9 @@ void hal_uart_config(const hal_uart_config_t *config) {
hal_stm32l4_uart_config(config);
#else
#error "Unsupported HAL architecture"
return HAL_RET_ERROR;
#endif
return HAL_RET_OK;
}
/**
@ -52,8 +66,17 @@ void hal_uart_config(const hal_uart_config_t *config) {
* @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) {
if (data == NULL || length == 0) {
return HAL_RET_INVALID_PARAM;
}
if (instance >= HAL_UART_INSTANCE_MAX) {
return HAL_RET_INVALID_PARAM;
}
/* Call architecture specific UART send implementation */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
hal_stm32f1_uart_send(instance, data, length);
@ -65,7 +88,9 @@ void hal_uart_send(hal_uart_instance_t instance, const uint8_t *data, size_t len
hal_stm32l4_uart_send(instance, data, length);
#else
#error "Unsupported HAL architecture"
return HAL_RET_ERROR;
#endif
return HAL_RET_OK;
}
/**
@ -73,62 +98,92 @@ void hal_uart_send(hal_uart_instance_t instance, const uint8_t *data, size_t len
* @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) {
if (data == NULL || received == NULL || length == 0) {
return HAL_RET_INVALID_PARAM;
}
if (instance >= HAL_UART_INSTANCE_MAX) {
return HAL_RET_INVALID_PARAM;
}
/* Call architecture specific UART receive implementation */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
return hal_stm32f1_uart_receive(instance, data, length);
*received = hal_stm32f1_uart_receive(instance, data, length);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
return hal_stm32f4_uart_receive(instance, data, length);
*received = hal_stm32f4_uart_receive(instance, data, length);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
return hal_stm32f7_uart_receive(instance, data, length);
*received = hal_stm32f7_uart_receive(instance, data, length);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
return hal_stm32l4_uart_receive(instance, data, length);
*received = hal_stm32l4_uart_receive(instance, data, length);
#else
#error "Unsupported HAL architecture"
return 0;
return HAL_RET_ERROR;
#endif
return HAL_RET_OK;
}
/**
* @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) {
if (ready == NULL) {
return HAL_RET_INVALID_PARAM;
}
if (instance >= HAL_UART_INSTANCE_MAX) {
return HAL_RET_INVALID_PARAM;
}
/* Call architecture specific UART TX ready check */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
return hal_stm32f1_uart_is_tx_ready(instance);
*ready = hal_stm32f1_uart_is_tx_ready(instance);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
return hal_stm32f4_uart_is_tx_ready(instance);
*ready = hal_stm32f4_uart_is_tx_ready(instance);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
return hal_stm32f7_uart_is_tx_ready(instance);
*ready = hal_stm32f7_uart_is_tx_ready(instance);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
return hal_stm32l4_uart_is_tx_ready(instance);
*ready = hal_stm32l4_uart_is_tx_ready(instance);
#else
#error "Unsupported HAL architecture"
return 0;
return HAL_RET_ERROR;
#endif
return HAL_RET_OK;
}
/**
* @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) {
if (ready == NULL) {
return HAL_RET_INVALID_PARAM;
}
if (instance >= HAL_UART_INSTANCE_MAX) {
return HAL_RET_INVALID_PARAM;
}
/* Call architecture specific UART RX ready check */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
return hal_stm32f1_uart_is_rx_ready(instance);
*ready = hal_stm32f1_uart_is_rx_ready(instance);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
return hal_stm32f4_uart_is_rx_ready(instance);
*ready = hal_stm32f4_uart_is_rx_ready(instance);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
return hal_stm32f7_uart_is_rx_ready(instance);
*ready = hal_stm32f7_uart_is_rx_ready(instance);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
return hal_stm32l4_uart_is_rx_ready(instance);
*ready = hal_stm32l4_uart_is_rx_ready(instance);
#else
#error "Unsupported HAL architecture"
return 0;
return HAL_RET_ERROR;
#endif
return HAL_RET_OK;
}