RZT Flexible Software Package Documentation  Release v3.0.0

 
SMCI (r_sci_smci)

Functions

fsp_err_t R_SCI_SMCI_Open (smci_ctrl_t *const p_ctrl, smci_cfg_t const *const p_cfg)
 
fsp_err_t R_SCI_SMCI_Write (smci_ctrl_t *const p_ctrl, uint8_t const *const p_src, uint32_t const bytes)
 
fsp_err_t R_SCI_SMCI_Read (smci_ctrl_t *const p_ctrl, uint8_t *const p_dest, uint32_t const bytes)
 
fsp_err_t R_SCI_SMCI_TransferModeSet (smci_ctrl_t *const p_ctrl, smci_transfer_mode_t const *const p_transfer_mode_params)
 
fsp_err_t R_SCI_SMCI_BaudCalculate (smci_speed_params_t const *const p_speed_params, uint32_t baud_rate_error_x_1000, sci_smci_clock_source_t clock_source, void *const p_baud_setting)
 
fsp_err_t R_SCI_SMCI_BaudSet (smci_ctrl_t *const p_ctrl, void const *const p_baud_setting)
 
fsp_err_t R_SCI_SMCI_StatusGet (smci_ctrl_t *const p_ctrl, smci_status_t *const p_status)
 
fsp_err_t R_SCI_SMCI_ClockControl (smci_ctrl_t *const p_ctrl, bool clock_enable)
 
fsp_err_t R_SCI_SMCI_CallbackSet (smci_ctrl_t *const p_ctrl, void(*p_callback)(smci_callback_args_t *), void const *const p_context, smci_callback_args_t *const p_callback_memory)
 
fsp_err_t R_SCI_SMCI_Close (smci_ctrl_t *const p_ctrl)
 

Detailed Description

Driver for the SCI peripheral on RZ microprocessor. This module implements the SMCI Interface.

Overview

Features

The SCI SMCI module supports the following features:

Configuration

Build Time Configurations for r_sci_smci

The following build time configurations are defined in fsp_cfg/r_sci_smci_cfg.h:

ConfigurationOptionsDefaultDescription
Parameter Checking
  • Default (BSP)
  • Enabled
  • Disabled
Default (BSP) If selected code for parameter checking is included in the build.
Multiplex Interrupt
  • Enabled
  • Disabled
Disabled Enable multiplex interrupt for a single driver.

Configurations for Connectivity > SMCI (r_sci_smci)

This module can be added to the Stacks tab via New Stack > Connectivity > SMCI (r_sci_smci).

ConfigurationOptionsDefaultDescription
General > NameName must be a valid C symbolg_smci0 Module name.
General > ChannelValue must be a non-negative integer0 Select the SCI channel.
Baud > Baud RateValue must be an integer greater than 09600 Enter the desired baud rate.

If the requested baud rate cannot be achieved, the settings with the smallest percent error are used. The theoretical calculated baud rate and percent error are printed in a comment in the generated baud_setting_t structure.
Baud > Clock conversion integerRefer to the RZT Configuration tool for available options.F=372 fmax=5MHz D=1 Clock rate conversion integer is one of transmission parameters defined by ISO7816-3 as F and also decodes to f-max of the output clock.
Baud > Max Error (%)Must be a valid non negative integer with a maximum configurable value of 205 Maximum percent error allowed during baud calculation. This is used by the algorithm to determine whether or not to consider using less accurate alternative register settings.

NOTE: The baud calculation does not show an error in the tool if this percent error was not achieved. The calculated percent error is recorded in a comment in the generated baud_setting_t structure.
Baud > Clock Source
  • SCInASYNCCLK
  • PCLKM
SCInASYNCCLK Select whether the SCInASYNCCLK or peripheral clock (PCLKM) is used for generating the SCK frequency.
Interrupts > CallbackName must be a valid C symboluser_smci_callback A user callback function can be provided. If this callback function is provided, it will be called from the interrupt service routine (ISR).
Interrupts > Receive Interrupt PriorityMCU Specific OptionsSelect the receive interrupt priority.
Interrupts > Transmit Data Empty Interrupt PriorityMCU Specific OptionsSelect the transmit interrupt priority.
Interrupts > Error Interrupt PriorityMCU Specific OptionsSelect the error interrupt priority.

Clock Configuration

The clock source for the baud-rate clock generator is the PCLKSCIn (SCInASYNCCLK or PCLKM). It is scaled by the CCR2.CKS bits to acheive the requested baud rate. This is done in R_SCI_SMCI_BaudSet routine.

Pin Configuration

This module uses TXDx and RXDx to communicate to external devices. TXDx and RXDx need to be tied together and pulled up to VCC externally via a pullup resistor and connected to the DATA line of a connected card.

The SmartCard clock signal is generated by the SMCI module on the SCKx pin. The clock frequency produced by the module is (baudrate * F) / D. The ISO specification defines the valid maximum frequency range as 4MHz to 20MHz; only certain combinations of Fi, Di and f(max) are allowed.

When writing the application for the driver, the application developer must also allocate a software controlled Reset line via a GPIO. This will allow the reliable receipt of the ATR message. Optionally, VCC and VPP can also be controlled by a GPIO output, so that cold starts can be forced.

Usage Notes

The SMCI module is compliant to ISO7816-3. SMCI is a half duplex interface. Direct convention in T0 mode is the default. The driver supports both direct and indirect (inverted) modes of transmission. It also supports GSM mode in which the output clock can be enabled and disabled while the interface is still active. If it is known that the device connected is a device in inverse convention, the convention can be changed with smci_api_t::transferModeSet() after calling open.

The MCU creates the clock for the attached SMCI device based on the initial baudrate setting. Upon reset the SmartCard device advertises the Di and Fi parameters as well as the max clock speed it can handle. The Di and Fi parameters dictate at what rate the SmartCard device can sample the data line as a function of the supplied clock generated by the RZ microprocessor. Only certain combinations of D and F are supported by the SMCI on SCI module. Only combinations where the ratio of S=F/D corresponds to a value of S of 32, 64, 93, 128, 186, 256, 372, or 512 (for example see Table "Base clock settings in smart card interface mode" in the RZ microprocessor manual).

The baud rate (1/ETU) can be changed while the device is open to allow for speed negotiation based on the attached device's capabilities.

The SMCI module does not contain a FIFO, and as such the receipt of multi-byte data has to be handled by interrupt initiated callback. The application developer must develop their callback so that the receipt of data is handled sufficiently without receiver overrun. If the read routine is called with a length=0, every receive interrupt will initiate a call to the user's callback. If the read is called with a non-zero length... the interrupt will fill the user's read buffer and initiate the callback after the last byte is complete. In many cases, the user can send an event from their callback so that the reading routine can wait for the event with a timeout. If a timeout occurs, the user can return the read state machine to an idle state by calling the read routine with a length of 0.

Limitations

Examples

SCI SMCI Example

uint8_t g_dest[TRANSFER_LENGTH];
uint8_t g_src[TRANSFER_LENGTH];
uint8_t g_out_of_band_received[TRANSFER_LENGTH];
uint32_t g_transfer_complete = 0;
uint32_t g_receive_complete = 0;
uint32_t g_out_of_band_index = 0;
void r_sci_smci_basic_example (void)
{
/* Initialize g_src to known data */
for (uint32_t i = 0; i < TRANSFER_LENGTH; i++)
{
g_src[i] = (uint8_t) ('A' + (i % 26));
}
/* Open the smci instance with initial configuration. */
fsp_err_t err = R_SCI_SMCI_Open(&g_smci0_ctrl, &g_smci0_cfg);
assert(FSP_SUCCESS == err);
/* Need to have clock on in order to receive or transmit*/
R_SCI_SMCI_ClockControl(&g_smci0_ctrl, true);
err = R_SCI_SMCI_Read(&g_smci0_ctrl, g_dest, TRANSFER_LENGTH);
assert(FSP_SUCCESS == err);
err = R_SCI_SMCI_Write(&g_smci0_ctrl, g_src, TRANSFER_LENGTH);
assert(FSP_SUCCESS == err);
while (!g_transfer_complete)
{
}
while (!g_receive_complete)
{
}
}
void user_smci_callback (smci_callback_args_t * p_args)
{
/* Handle the SMCI event */
switch (p_args->event)
{
/* Received a character */
{
/* Only put the next character in the receive buffer if there is space for it */
if (sizeof(g_out_of_band_received) > g_out_of_band_index)
{
/* Write either the next one or two bytes depending on the receive data size */
g_out_of_band_received[g_out_of_band_index++] = p_args->data;
}
break;
}
/* Receive complete */
{
g_receive_complete = 1;
break;
}
/* Transmit complete */
{
g_transfer_complete = 1;
break;
}
default:
{
}
}
}

SCI SMCI Baud Set Example

#define SCI_SMCI_BAUDRATE_28800 (28800)
#define SCI_SMCI_BAUDRATE_ERROR_PERCENT_5 (5000)
void r_sci_smci_baud_example (void)
{
smci_speed_params_t speed_settings;
speed_settings.baudrate = SCI_SMCI_BAUDRATE_28800;
fsp_err_t err = R_SCI_SMCI_BaudCalculate(&speed_settings,
SCI_SMCI_BAUDRATE_ERROR_PERCENT_5,
SCI_SMCI_CLOCK_SOURCE_SCI0ASYNCCLK,
&baud_setting);
assert(FSP_SUCCESS == err);
err = R_SCI_SMCI_BaudSet(&g_smci0_ctrl, (void *) &baud_setting);
assert(FSP_SUCCESS == err);
}

Data Structures

struct  sci_smci_instance_ctrl_t
 
struct  sci_smci_baud_setting_t
 
struct  sci_smci_extended_cfg_t
 

Enumerations

enum  sci_smci_clock_source_t
 

Data Structure Documentation

◆ sci_smci_instance_ctrl_t

struct sci_smci_instance_ctrl_t

SMCI instance control block.

◆ sci_smci_baud_setting_t

struct sci_smci_baud_setting_t

Register settings to achieve a desired baud rate in Smart Card mode.

◆ sci_smci_extended_cfg_t

struct sci_smci_extended_cfg_t

SMCI on SCI device Configuration.

Data Fields
sci_smci_baud_setting_t * p_smci_baud_setting Register settings for a desired baud rate.
sci_smci_clock_source_t clock_source

Clock source to generate SCK can either be selected as PCLKM or SCInASYNCCLK.

Enumeration Type Documentation

◆ sci_smci_clock_source_t

Source clock selection options for SCI.

Function Documentation

◆ R_SCI_SMCI_Open()

fsp_err_t R_SCI_SMCI_Open ( smci_ctrl_t *const  p_ctrl,
smci_cfg_t const *const  p_cfg 
)

Configures the Smart Card Interface driver based on the input configurations. The interface stays in the clock-off state without enabling reception at the end of this function. ISO7816-3 default communication parameters are used to initialize SMCI port speed and parameters, as the ATR message is always sent in that format. Only if Inverse convention is expected should the transfer mode be changed after reset. Implements smci_api_t::open

Parameters
[in,out]p_ctrlPointer to SMCI control block that is to be opened
[in]p_cfgPointer to the config structure that shall be used to set parameters of the SMCI baud calculations needed to be have done and set into p_cfg->p_extend->p_smci_baud_setting
Return values
FSP_SUCCESSChannel opened successfully.
FSP_ERR_ASSERTIONPointer to SMCI control block or configuration structure is NULL.
FSP_ERR_IP_CHANNEL_NOT_PRESENTThe requested channel does not exist on this MCU.
FSP_ERR_ALREADY_OPENControl block has already been opened or channel is being used by another instance. Call close() then open() to reconfigure.

◆ R_SCI_SMCI_Write()

fsp_err_t R_SCI_SMCI_Write ( smci_ctrl_t *const  p_ctrl,
uint8_t const *const  p_src,
uint32_t const  bytes 
)

Transmits user specified number of bytes from the source buffer pointer. Implements smci_api_t::write

Parameters
[in,out]p_ctrlPointer to SMCI control block
[in]p_srcPointer to buffer that will be written out
[in]bytesNumber of bytes to be transferred
Return values
FSP_SUCCESSData transmission started successfully.
FSP_ERR_ASSERTIONPointer to SMCI control block is NULL.
FSP_ERR_NOT_OPENThe control block has not been opened.
FSP_ERR_IN_USEA SMCI transmission is in progress.

◆ R_SCI_SMCI_Read()

fsp_err_t R_SCI_SMCI_Read ( smci_ctrl_t *const  p_ctrl,
uint8_t *const  p_dest,
uint32_t const  bytes 
)

Receives user specified number of bytes into destination buffer pointer. Receiving is done at the ISR level as there is no FIFO. If 0 is passed in as the length, reception will always invoke the user callback. Implements smci_api_t::read

Parameters
[in,out]p_ctrlPointer to SMCI control block
[in,out]p_destPointer to the buffer top be read into
[in]bytesNumber of bytes to copy from the SMCI receive register
Return values
FSP_SUCCESSData reception started successfully.
FSP_ERR_ASSERTIONPointer to SMCI control block or read buffer is NULL.
FSP_ERR_NOT_OPENThe control block has not been opened.
FSP_ERR_IN_USEA previous read operation is still in progress.

◆ R_SCI_SMCI_TransferModeSet()

fsp_err_t R_SCI_SMCI_TransferModeSet ( smci_ctrl_t *const  p_ctrl,
smci_transfer_mode_t const *const  p_transfer_mode_params 
)

Updates the settings of block transfer mode and data transfer convention. The CCR1 and CCR3 registers will be set according to the input arguments of protocol type, data convention type, and mode. Implements smci_api_t::transferModeSet

Parameters
[in,out]p_ctrlPointer to SMCI control block that is to be modified
[in]p_transfer_mode_paramsPointer to SMCI settings like protocol, convention, and gsm_mode
Warning
This terminates any in-progress transmission and reception.
Return values
FSP_SUCCESSTransfer mode and data transfer direction was successfully changed.
FSP_ERR_IN_USEUnable to change transfer mode as device has clock off or is actively RX or TX.
FSP_ERR_ASSERTIONNull pointer was passed as a parameter.
FSP_ERR_NOT_OPENThe control block has not been opened.

◆ R_SCI_SMCI_BaudCalculate()

fsp_err_t R_SCI_SMCI_BaudCalculate ( smci_speed_params_t const *const  p_speed_params,
uint32_t  baud_rate_error_x_1000,
sci_smci_clock_source_t  clock_source,
void *const  p_baud_setting 
)

Calculates baud rate register settings. Evaluates and determines the best possible settings set to the baud rate related registers. And then updates the SCI registers.

Parameters
[in]p_speed_paramsStructure including speed defining params, baud, F, and D.
[in]baud_rate_error_x_1000<baud_rate_percent_error> x 1000 required for module to function. Absolute max baud_rate_error is 20000 (20%) according to the ISO spec.
[in]clock_sourceClock source (PCLKM or SCInASYNCCLK) used for bit rate calculation.
[out]p_baud_settingBaud setting information stored here if successful.
Return values
FSP_SUCCESSBaud rate setting calculation successful.
FSP_ERR_ASSERTIONp_speed_params or p_baud is a null pointer.
FSP_ERR_INVALID_ARGUMENTBaud rate is '0', freq is '0', or error in calculated baud rate is larger than 20%.

◆ R_SCI_SMCI_BaudSet()

fsp_err_t R_SCI_SMCI_BaudSet ( smci_ctrl_t *const  p_ctrl,
void const *const  p_baud_setting 
)

Updates the baud rate and clock output. p_baud_setting is a pointer to a sci_smci_baud_setting_t structure that needs to have already been filled by R_SCI_SMCI_BaudCalculate. Implements smci_api_t::baudSet

Warning
This terminates any in-progress transmission.
Parameters
[in,out]p_ctrlPointer to SMCI control block that is to be modified
[in]p_baud_settingPointer to baud setting information to be written to the SMCI hardware registers
Return values
FSP_SUCCESSBaud rate was successfully changed.
FSP_ERR_ASSERTIONPointer to SMCI control block or p_baud_setting is NULL.
FSP_ERR_NOT_OPENThe control block has not been opened.
FSP_ERR_INVALID_ARGUMENTThe p_baud_setting does not seem to be set correctly.

◆ R_SCI_SMCI_StatusGet()

fsp_err_t R_SCI_SMCI_StatusGet ( smci_ctrl_t *const  p_ctrl,
smci_status_t *const  p_status 
)

Provides the state of the driver and the # of bytes received since read was called. Implements smci_api_t::statusGet

Parameters
[in]p_ctrlPointer to SMCI control block of this SMCI instance
[out]p_statusPointer structure that will be filled in with status info
Return values
FSP_SUCCESSInformation stored in provided p_status.
FSP_ERR_ASSERTIONPointer to SMCI control block, or status structure is NULL.
FSP_ERR_NOT_OPENThe control block has not been opened.

◆ R_SCI_SMCI_ClockControl()

fsp_err_t R_SCI_SMCI_ClockControl ( smci_ctrl_t *const  p_ctrl,
bool  clock_enable 
)

Enable or disable the clock signal that is provided by interface the baud rate. When the clock is enabled, reception is enabled at the end of this function. See section "Clock Output Control" in Smart Card Interface Mode in the RZ microprocessor manual or the relevant section for the MCU being used. Implements smci_api_t::clockControl

Warning
This terminates any in-progress transmission and reception.
Parameters
[in,out]p_ctrlPointer to SMCI control block
[in]clock_enabletrue=Enable or false=disable the Smart Card Interface clock
Return values
FSP_SUCCESSClock output setting was successfully changed.
FSP_ERR_ASSERTIONPointer to SMCI control block is NULL.
FSP_ERR_NOT_OPENThe control block has not been opened.
FSP_ERR_INVALID_MODEClock cannot be disabled if GSM mode isnt active.

◆ R_SCI_SMCI_CallbackSet()

fsp_err_t R_SCI_SMCI_CallbackSet ( smci_ctrl_t *const  p_ctrl,
void(*)(smci_callback_args_t *)  p_callback,
void const *const  p_context,
smci_callback_args_t *const  p_callback_memory 
)

Updates the user callback and has option of providing memory for callback structure. Implements smci_api_t::callbackSet

Return values
FSP_SUCCESSCallback updated successfully.
FSP_ERR_ASSERTIONA required pointer is NULL.
FSP_ERR_NOT_OPENThe control block has not been opened.

◆ R_SCI_SMCI_Close()

fsp_err_t R_SCI_SMCI_Close ( smci_ctrl_t *const  p_ctrl)

Aborts any in progress transfers. Disables interrupts, receiver, and transmitter. Implements smci_api_t::close

Parameters
[in]p_ctrlPointer to SMCI control block that is requested to close
Return values
FSP_SUCCESSChannel successfully closed.
FSP_ERR_ASSERTIONPointer to SMCI control block is NULL.
FSP_ERR_NOT_OPENThe control block has not been opened.