HAL SMARTCARD How to Use

group SMARTCARD_How_To_Use

How to use the SMARTCARD HAL module driver

The SMARTCARD HAL driver can be used as follows:

  1. Declare a hal_smartcard_handle_t handle structure, for example: hal_smartcard_handle_t hsmartcard;

  2. Initialize the SMARTCARDx driver with a USART HW instance by calling the HAL_SMARTCARD_Init(). The SMARTCARDx clock is enabled inside the HAL_SMARTCARD_Init() if USE_HAL_SMARTCARD_CLK_ENABLE_MODEL > HAL_CLK_ENABLE_NO.

  3. Configure the low-level hardware (GPIO, CLOCK, NVIC, etc.):

    • Enable the SMARTCARDx interface clock when USE_HAL_SMARTCARD_CLK_ENABLE_MODEL is set to HAL_CLK_ENABLE_NO.

    • Configure SMARTCARDx pins:

      • Enable the clock for the SMARTCARDx GPIOs

      • Configure SMARTCARDx pins as alternate-function open-drain

    • Configure the NVIC for interrupt processing:

      • Configure the SMARTCARDx interrupt priority

      • Enable the NVIC SMARTCARDx IRQ Channel

  4. Configure the communication baud rate, stop bit, first bit, parity mode, NACK, smartcard clock prescaler, source clock prescaler, clock polarity, clock phase, clock output enabling, guard time and auto retry count by calling HAL_SMARTCARD_SetConfig().

    Configure or enable advanced features:

    All these advanced configurations are optional. If not called, default values apply.

  5. For SMARTCARDx I/O operations, polling, interrupt, and DMA are available in this driver.

  6. Callback registration

    • When the compilation flag USE_HAL_SMARTCARD_REGISTER_CALLBACKS is set to 1, it allows the user to configure the driver callbacks dynamically via its own method:

  • To unregister a callback, register the default callback via the registration function.

  • By default, after the HAL_SMARTCARD_Init() and when the state is HAL_SMARTCARD_STATE_INIT, all callbacks are set to the corresponding default weak functions.

  • Callbacks can be registered in the handle global_state HAL_SMARTCARD_STATE_INIT and HAL_SMARTCARD_STATE_CONFIGURED.

  • When the compilation flag USE_HAL_SMARTCARD_REGISTER_CALLBACKS is set to 0 or not defined, the callback registration feature is not available and all callbacks are set to the corresponding weak functions.

Acquire/Release the HAL SMARTCARD handle

  • When the compilation flag USE_HAL_MUTEX is set to 1, a multi-threaded user application is allowed to acquire the SMARTCARD HAL handle to execute a transmit or receive process, or a sequence of transmit/receive operations. Release the SMARTCARD HAL handle when the process or sequence ends.

  • HAL acquire/release operations are based on the HAL OS abstraction layer (stm32_hal_os.c/.h osal):

  • When the compilation flag USE_HAL_MUTEX is set to 0 or not defined, HAL_SMARTCARD_AcquireBus() and HAL_SMARTCARD_ReleaseBus() are not available.

Note

In SMARTCARD mode, ETU (Elementary Time Unit) is equivalent to the baud period duration