![]() |
RA Flexible Software Package Documentation
Release v5.9.0
|
|
Driver for the SCI peripheral on RA MCUs. This module implements the LIN Interface.
The Simple LIN on SCI HAL module supports Local Interface Network (LIN) transactions in master or slave mode. A callback must be provided, and is invoked when a transmission, reception, or other event has completed or occurred. The callback arguments contain information about the transaction status, bytes transferred and a pointer to the user defined context.
In this document, the terminology 'Start Frame' refers to the LIN header (Break Field, Sync Field, and Protected Identifier Field), and the terminology 'Information Frame' refers to the LIN Data Field.
Configuration | Options | Default | Description |
---|---|---|---|
Parameter Checking |
| Default (BSP) | If selected code for parameter checking is included in the build. |
Checksum Support |
| Enabled | When set to 'Disabled', code for checksum generation and validation is excluded from the build. This setting is applied globally to the project. Disable only when checksum generation and validation is not required for any LIN instance. |
Bus Conflict Detection Support |
| Disabled | When set to 'Disabled', code for bus conflict detection is excluded from the build. This setting is applied globally to the project. Enable when at least one LIN instance is using bus conflict detection. |
Configuration | Options | Default | Description |
---|---|---|---|
General | |||
Name | Name must be a valid C symbol | g_lin0 | Module name. |
SCI Channel | MCU Specific Options | Select the Serial Communications Interface channel. | |
Mode |
| Master | Select the LIN operating mode (master or slave). |
Extra | |||
Digital Filter Clock Select |
| Disabled | Enable the digital filter on RXDXn pin. The digital filter block in SCI consists of three-stage flipflop circuits. |
Bus Conflict Clock Divider |
| 2 | Select the base clock divider for the sampling clock of the bus conflict detection circuit. |
Baud | |||
Baud Rate | Value must be an integer greater than 0 | 19200 | 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 sci_lin_extended_cfg_t structure. |
Base Clock Cycles Per Bit Period |
| 16 | Select number of SCI base clock cycles per single bit period at the configured baud rate. When 16 base clock cycles is selected, the period of the SCI base clock is 1/16 of a single bit period. When 8 base clock cycles is selected, the period of the SCI base clock is 1/8 of a single bit period. |
Framing | |||
Framing > ID Filter (Slave Mode) | |||
Compare Data Mask | Value must be an integer between 0 and 255. | 0 | Select the bit mask to be applied before comparing the received PID to the selected compare data. This setting specifies which bits of the selected compare data must match. Set to 0 to disable the filter (allow all frame identifiers). |
Priority Compare Data | Value must be an integer between 0 and 255. | 0 | Select the priority compare filter data. |
Secondary Compare Data | Value must be an integer between 0 and 255. | 0 | Select the secondary compare filter data. |
Compare Data Select |
| Priority | Select the compare data to use. If 'both' is selected, the priority compare data is checked before the secondary compare data. |
Priority Interrupt Bit Enable |
| Disabled | Select whether to enable the Priority Interrupt Bit filter. When enabled, regardless of the Filter Data Select setting value, the bit specified by the Priority Interrupt Bit setting is compared with the corresponding bit in the Priority Compare Data Filter and if it matches, the identifier is allowed through the filter. |
Priority Interrupt Bit | Value must be an integer between bit 0 (LSb) and bit 7 (MSb). | 0 | Select the priority interrupt bit (0-7) of the received PID to compare with the corresponding bit in the Priority Compare Data Filter. |
Break Field Bits/Break Detection Threshold (bits) | Value must be an integer. | 13 | For master break field transmission, this configures the dominant period of the break field (in bits). For slave break field reception, this configures the break detection threshold in bits. Must be 13 bits or greater for master mode and 11 bits or greater for slave mode. |
Break Delimiter Bits | Value must be an integer between 1 and 255. | 1 | This configures the period of the break delimiter (in bits).The break delimiter shall be at least one nominal bit time long. |
LIN Timer Divider |
| 1 | Set the LIN timer divider. The LIN timer is used for break field transmission and detection. Higher dividers make possible transmission/detection of more break field bits, but are less precise. |
Interrupts | |||
Callback | Name must be a valid C symbol | sci_lin0_callback | A user callback function must be provided. It will be called from the interrupt service routine (ISR). |
Receive Interrupt Priority | MCU Specific Options | Select the receive interrupt priority. | |
Transmit Data Empty Interrupt Priority | MCU Specific Options | Select the transmit interrupt priority. | |
Transmit End Interrupt Priority | MCU Specific Options | Select the transmit end interrupt priority. | |
Receive Error Interrupt Priority | MCU Specific Options | Select the receive error interrupt priority. | |
Break Field Low Width Interrupt Priority (SCIX0) | MCU Specific Options | Select the break field low width interrupt priority. | |
Control Field 0/1 Match Interrupt Priority (SCIX1) | MCU Specific Options | Select the Control field 0,1 and Priority bit detection interrupt priority. | |
Bus Collision Detection Interrupt Priority (SCIX2) | MCU Specific Options | Select the bus collision detection interrupt priority, or set to 'Disabled' to disable bus collision detection. |
Peripheral clock for SCI LIN is PCLKA.
This module uses TXD and RXD to communicate to external devices. Connect TXD and RXD to an on-board or external LIN transceiver for LIN bus communication.
An external pull up resistor is required on TXD in both master and slave mode.
When the Bus Collision Detection Interrupt (SCIX2) is enabled, the TXDn pin output and the RXDn pin input are sampled by the selected bus conflict detection clock. When a mismatch occurs three times in a row, an SCIX2 interrupt is generated and transmission is stopped. This function only works during transmission.
Consideration of the specific application's LIN bus characteristics should be taken into account when enabling this function. If the bus collision sampling clock is too fast, the expected propagation delay through the transceiver may be erroneously detected as a bus collision (false positive). If the bus collision sampling clock is too slow, collisions may be missed (false negative).
A LIN transceiver is required to detect bus collisions. The propagation delay through the transceiver varies depending on individual transceiver design.
When Bus Conflict Detection is enabled, set the Bus Conflict Clock Divider to divide down the clock at least slow enough to avoid false positive collision detections caused by the worst case transceiver propagation delay. The Baud|Base Clock Cycles Per Bit Period setting can be used to adjust the SCI Base Clock frequency if needed.
Example:
The SCI base clock frequency can be calculated by the following formula:
sci_base_clock_frequency = 1 / [ (1/baud_rate)/cycles_per_bit_period ]
The following relationship must be satisfied to avoid false positives:
2 *[ 1e6 / (sci_base_clock_frequency/bus_conflict_clock_divider) ] > max_propagation_delay_us
Suppose:
Then:
Bus Conflict Divider Selection Process:
If no possible divider is found with 16 cycles, Baud|Base Clock Cycles Per Bit Period can be reduced to 8, which will reduce the SCI Base Clock by half compared to 16.
The SCI performs optional hardware PID filtering in slave mode. When enabled, only start frames with PIDs that pass through the filter are received and passed to the user callback. Other start frames are ignored.
An initial configuration of the filter settings can be applied in the properties view by selecting Framing -> ID Filter and choosing the desired settings. See sci_lin_id_filter_setting_t for the available filter settings, as well as the examples in this document. By default, filtering is disabled and all PIDs can be received.
The filter can be updated at runtime by using using R_SCI_LIN_IdFilterSet. The current filter settings can be viewed with R_SCI_LIN_IdFilterGet.
SCI data reception is disabled until a break field is detected (slave mode) or a call to R_SCI_LIN_InformationFrameRead() is made (master and slave mode).
The start frame is detected in slave mode only. Start frame reception is interrupt driven, and is enabled after a successful call to R_SCI_LIN_Open() in slave mode.
When the LIN break field is detected by the slave node, start frame reception begins without action from the application. When start frame reception completes, the user callback is called.
If the slave node needs to receive the information frame data, the slave node must call R_SCI_LIN_InformationFrameRead() before the first information data is received in order to receive the data. The user callback is called when reception is completed. It is permitted to call R_SCI_LIN_InformationFrameRead() from the callback context.
If the slave node needs to publish a response to the start frame, the slave node must call R_SCI_LIN_InformationFrameWrite() and provide the response data and the PID of the received header in the lin_transfer_params_t. The user callback is called when transmission is complete.
The start frame is not detected in master mode. Information frame reception is enabled only after a call to R_SCI_LIN_InformationFrameRead().
The start frame is transmitted by calling R_SCI_LIN_StartFrameWrite(). The user callback is called when transmission is complete.
If the master node needs to receive the information frame data response, call R_SCI_LIN_InformationFrameRead() after start frame transmission has completed and before the first information frame data is received. It is permitted to call R_SCI_LIN_InformationFrameRead() from the callback context.
If the master node needs to publish a response to its own start frame, the master node must call R_SCI_LIN_InformationFrameWrite() only after start frame transmission has completed, and provide the response data and the PID of the transmitted header in the lin_transfer_params_t. The user callback is called when transmission is complete.
The application is responsible for managing timeouts in case R_SCI_LIN_InformationFrameRead() is called, but the data is not received in the expected time period. If a timeout occurs, R_SCI_LIN_CommunicationAbort() may be called to cancel a pending read.
When an error occurs, reception is stopped and the user callback is called with the relevant error code.
The following demonstrates a basic example of frame reception for a LIN master.
The following demonstrates a basic example of frame transmission for a LIN master.
The following demonstrates a basic example of start frame reception and information frame response for a LIN slave.
The following example demonstrates a basic example of frame ID filtering.
The following example demonstrates an advanced example of frame ID filtering.
Data Structures | |
struct | sci_lin_timer_setting_t |
struct | sci_lin_baud_setting_t |
struct | sci_lin_id_filter_setting_t |
struct | sci_lin_baud_params_t |
struct | sci_lin_extended_cfg_t |
struct | sci_lin_instance_ctrl_t |
Enumerations | |
enum | sci_lin_priority_interrupt_bit_t |
enum | sci_lin_compare_data_select_t |
enum | sci_lin_timer_divider_t |
enum | sci_lin_digital_filter_clock_t |
enum | sci_lin_bus_conflict_clock_t |
enum | sci_lin_base_clock_t |
struct sci_lin_timer_setting_t |
Register settings for configuring the LIN timer
struct sci_lin_baud_setting_t |
Register settings for achieving a desired baud rate.
Data Fields | ||
---|---|---|
sci_lin_timer_setting_t | timer_setting | Break field timer settings associated with this baud rate. |
union sci_lin_baud_setting_t | __unnamed__ | |
uint8_t | brr | Bit Rate Register setting. |
uint8_t | cks: 2 | CKS value to get divisor. |
struct sci_lin_id_filter_setting_t |
Parameters for configuring the ID filter settings
Data Fields | ||
---|---|---|
uint8_t | compare_data_mask |
Bit mask applied before comparing the received PID to the compare data. Selects which bits of the selected compare data must match. |
uint8_t | primary_compare_data |
Primary compare data |
uint8_t | secondary_compare_data |
Secondary compare data |
uint8_t | priority_interrupt_bit_select: 3 |
Specify ONE of bits 0 to 7 of Control Field 1 as the priority interrupt bit. 0 is bit 0 of the PID, 1 is bit 1 of the PID, and so on. |
uint8_t | priority_interrupt_enable: 1 |
Set to 1 to enable the priority interrupt bit filter specified by priority_interrupt_bit_select, 0 to disable. When this bit is 1, regardless of the compare_data_select setting value, the bit specified by priority_interrupt_bit_select is compared with the Primary comparison data for Control Field 1 (primary_compare_data). |
uint8_t | compare_data_select: 2 |
Select the compare data for Control Field 1 (priority, secondary, or both). See sci_lin_compare_data_select_t |
uint8_t | __pad0__: 2 |
struct sci_lin_baud_params_t |
Parameters for baud and timer setting calculation
Data Fields | ||
---|---|---|
uint32_t | baudrate | Desired baudrate. |
uint16_t | break_bits | Master mode: Number of break field bits to transmit. Slave mode: Number of break field threshold bits. |
uint8_t | delimiter_bits | Break delimeter length setting (at least 1 nominal bit time long). |
sci_lin_base_clock_t | base_clock_cycles_per_bit | Number of base clock cycles for 1-bit period. |
struct sci_lin_extended_cfg_t |
SCI LIN extended configuration
Data Fields | ||
---|---|---|
sci_lin_digital_filter_clock_t | digital_filter_clock | Digital filter setting. See sci_lin_digital_filter_clock_t. |
sci_lin_bus_conflict_clock_t | bus_conflict_clock | Bus conflict detection clock setting. See sci_lin_bus_conflict_clock_t. |
uint16_t | break_bits | Master mode: Number of break field bits to transmit. Slave mode: Number of break field threshold bits. |
uint8_t | delimiter_bits | Break delimeter length setting (at least 1 nominal bit time long). |
sci_lin_baud_setting_t | baud_setting | Register settings for a desired baud rate. |
sci_lin_id_filter_setting_t | filter_setting | ID filter setting. |
IRQn_Type | scix0_irq | Break field low width IRQ number. |
IRQn_Type | scix1_irq | Control field 0-1 match / Priority interrupt bit detect IRQ number. |
IRQn_Type | scix2_irq | Bus collision detect IRQ number. |
IRQn_Type | rxi_irq | Receive interrupt IRQ number. |
IRQn_Type | txi_irq | Transmit interrupt IRQ number. |
IRQn_Type | tei_irq | Transmit end interrupt IRQ number. |
IRQn_Type | eri_irq | Error interrupt IRQ number. |
uint8_t | scix0_ipl | Break field low width interrupt priority. |
uint8_t | scix1_ipl | Control field 0-1 match / Priority interrupt bit detect interrupt priority. |
uint8_t | scix2_ipl | Bus collision detect interrupt priority. |
uint8_t | rxi_ipl | Receive interrupt priority. |
uint8_t | txi_ipl | Transmit interrupt priority. |
uint8_t | tei_ipl | Transmit end interrupt priority. |
uint8_t | eri_ipl | Error interrupt priority. |
struct sci_lin_instance_ctrl_t |
LIN Instance Control Block. DO NOT INITIALIZE.
Compare Data Select options for ID filtering.
LIN timer divider selection.
Digital filter configuration.
Bus conflict detection clock selection. The period of the SCI base clock is 1/16 of a single bit period when the SCIn.SEMR.ABCS is 0, and 1/8 of a single bit period when the SCIn.SEMR.ABCS is 1.
enum sci_lin_base_clock_t |
Asynchronous Mode Base Clock Select. The period of the SCI base clock is 1/16 of a single bit period when the SCIn.SEMR.ABCS is 0, and 1/8 of a single bit period when the SCIn.SEMR.ABCS is 1.
Enumerator | |
---|---|
SCI_LIN_BASE_CLOCK_16 | Select 16 base clock cycles for 1-bit period. |
SCI_LIN_BASE_CLOCK_8 | Select 8 base clock cycles for 1-bit period. |
fsp_err_t R_SCI_LIN_Open | ( | lin_ctrl_t *const | p_api_ctrl, |
lin_cfg_t const *const | p_cfg | ||
) |
Configures the LIN driver channel based on the input configuration.
Implements lin_api_t::open.
Example:
FSP_SUCCESS | Channel opened successfully. |
FSP_ERR_ASSERTION | Pointer to LIN control block or configuration structure is NULL. |
FSP_ERR_INVALID_CHANNEL | The requested channel does not exist on this MCU or the channel does not support LIN mode. |
FSP_ERR_ALREADY_OPEN | Control block has already been opened or channel is being used by another instance. Call close() then open() to reconfigure. |
FSP_ERR_INVALID_MODE | Setting not supported for selected mode |
fsp_err_t R_SCI_LIN_StartFrameWrite | ( | lin_ctrl_t *const | p_api_ctrl, |
uint8_t const | id | ||
) |
Begins non-blocking transmission of a LIN start frame (break, sync and protected identifier).
On successful start frame transmission, the callback is called with event lin_event_t::LIN_EVENT_TX_START_FRAME_COMPLETE.
Implements lin_api_t::startFrameWrite.
Example:
FSP_SUCCESS | Start frame transmission started successfully. |
FSP_ERR_ASSERTION | A required pointer is NULL. |
FSP_ERR_NOT_OPEN | The control block has not been opened. |
FSP_ERR_INVALID_ARGUMENT | ID out of range (unprotected ID must be less than 64) |
FSP_ERR_INVALID_MODE | Function called by slave node (not supported for slave nodes) |
FSP_ERR_IN_USE | A transmission or reception is currently in progress. Call R_SCI_LIN_CommunicationAbort to cancel it if desired, or wait for the current transfer operation to complete before starting a new one. |
fsp_err_t R_SCI_LIN_InformationFrameWrite | ( | lin_ctrl_t *const | p_api_ctrl, |
const lin_transfer_params_t *const | p_transfer_params | ||
) |
Begins non-blocking transmission of a LIN information frame.
On successful information frame transmission, the callback is called with event lin_event_t::LIN_EVENT_TX_INFORMATION_FRAME_COMPLETE.
Implements lin_api_t::informationFrameWrite.
Example:
FSP_SUCCESS | Data transmission started successfully. |
FSP_ERR_NOT_OPEN | The control block has not been opened. |
FSP_ERR_ASSERTION | Pointer to LIN control block, transfer parameters, or tx/rx buffer is NULL, or 0 bytes length provided |
FSP_ERR_IN_USE | A transmission or reception is currently in progress. Call R_SCI_LIN_CommunicationAbort to cancel it if desired, or wait for the current transfer operation to complete before starting a new one. |
fsp_err_t R_SCI_LIN_InformationFrameRead | ( | lin_ctrl_t *const | p_api_ctrl, |
lin_transfer_params_t *const | p_transfer_params | ||
) |
Begins non-blocking information frame reception to receive user specified number of bytes into destination buffer pointer.
The checksum type specifies the checksum type used for validation. If a non-standard algorithm is used, or the application prefers to validate the checksum outside the driver, or the application prefers to skip checksum validation, specify lin_checksum_type_t::LIN_CHECKSUM_TYPE_NONE. If checksum validation is skipped, the lin_checksum_type_t::LIN_EVENT_ERR_INVALID_CHECKSUM event is not possible. When lin_checksum_type_t::LIN_CHECKSUM_TYPE_NONE is used, the number of bytes specified in the receive buffer length will be received (the driver will not expect to receive an additional 1 checksum byte), so if a non-standard checksum is used, sufficient space must be allocated in the write buffer and accounted for in the provided length.
On successful information frame reception, the callback is called with event lin_event_t::LIN_EVENT_RX_INFORMATION_FRAME_COMPLETE.
Implements lin_api_t::informationFrameRead.
Example:
FSP_SUCCESS | Data reception started successfully. |
FSP_ERR_NOT_OPEN | The control block has not been opened. |
FSP_ERR_ASSERTION | Pointer to LIN control block, transfer parameters, or tx/rx buffer is NULL, or 0 bytes length provided |
FSP_ERR_IN_USE | A transmission or reception is currently in progress. Call R_SCI_LIN_CommunicationAbort to cancel it if desired, or wait for the current transfer operation to complete before starting a new one. |
FSP_ERR_INVALID_CALL | Data reception is not possible because a header frame has not been received yet (slave mode only). |
fsp_err_t R_SCI_LIN_CommunicationAbort | ( | lin_ctrl_t *const | p_api_ctrl | ) |
Cancels in progress information frame read or write, or start frame read or write. Break field reception cannot be cancelled. For slave nodes, reception of a new start frame reception is still enabled after a call to this function.
Implements lin_api_t::communicationAbort.
FSP_SUCCESS | Data transfer aborted successfully or no transfer was in progress. |
FSP_ERR_NOT_OPEN | The control block has not been opened. |
FSP_ERR_ASSERTION | Pointer to LIN control block is NULL. |
fsp_err_t R_SCI_LIN_CallbackSet | ( | lin_ctrl_t *const | p_api_ctrl, |
void(*)(lin_callback_args_t *) | p_callback, | ||
void const *const | p_context, | ||
lin_callback_args_t *const | p_callback_memory | ||
) |
Updates the user callback and has option of providing memory for callback structure.
Implements lin_api_t::callbackSet.
FSP_SUCCESS | Callback updated successfully. |
FSP_ERR_ASSERTION | Pointer to LIN control block or callback is NULL. |
FSP_ERR_NOT_OPEN | The control block has not been opened. |
FSP_ERR_NO_CALLBACK_MEMORY | p_callback is non-secure and p_callback_memory is either secure or NULL. |
fsp_err_t R_SCI_LIN_BaudCalculate | ( | sci_lin_baud_params_t const *const | p_baud_params, |
sci_lin_baud_setting_t *const | p_baud_setting | ||
) |
Calculates baud rate and LIN timer (TCNT, TPRE and TCSS) register settings. This function evaluates and determines the most accurate settings for the baud rate and timer related registers.
The LIN timer setting is used for break field transmission/detection. Because the timer setting is specified in terms of bits, and the duration of a bit varies depending on baud rate, the baud rate register settings and timer register settings are related. The smallest possible LIN timer divider which can achieve the desired break field bits setting at the configured baud rate is selected to provide the highest measurement accuracy.
The baud rate cannot be updated at runtime with this function. This function is provided to ease configuration of the initial baud settings.
[in] | p_baud_params | Parameters required to calculate the baud rate |
[out] | p_baud_setting | If calculation succeeds, contains computed values to achieve requested baud rate. If calculation fails, the input structure is not modified. |
FSP_SUCCESS | Register settings updated in provided p_baud_setting |
FSP_ERR_ASSERTION | p_baud_setting was NULL |
FSP_ERR_INVALID_ARGUMENT | Cannot achieve combination of break field bits and baudrate with provided settings or p_baud_params->baudrate was 0 or p_baud_params->break_bits was 0 |
fsp_err_t R_SCI_LIN_IdFilterSet | ( | lin_ctrl_t *const | p_api_ctrl, |
sci_lin_id_filter_setting_t const *const | p_config | ||
) |
Set the the ID filter settings for filtering control field 1 (PID byte).
NOTE: Setting the ID filter will abort any in-progress LIN start frame reception, as the ID filter settings cannot be changed during reception of the start frame. The next start frame will be received with the new settings.
[in] | p_api_ctrl | Pointer to the LIN control block. |
[in] | p_config | The ID filter settings to apply |
FSP_SUCCESS | ID filter updated successfully. |
FSP_ERR_ASSERTION | Pointer to LIN control block or p_config is NULL. |
FSP_ERR_INVALID_MODE | Function called by master node (not supported for master nodes) |
FSP_ERR_NOT_OPEN | The control block has not been opened. |
fsp_err_t R_SCI_LIN_IdFilterGet | ( | lin_ctrl_t *const | p_api_ctrl, |
sci_lin_id_filter_setting_t *const | p_config | ||
) |
Returns the currently configured ID filter settings.
[in] | p_api_ctrl | Pointer to the LIN control block. |
[out] | p_config | The current ID filter settings |
FSP_SUCCESS | ID filter updated successfully. |
FSP_ERR_ASSERTION | Pointer to LIN control block or p_config is NULL. |
FSP_ERR_INVALID_MODE | Function called by master node (not supported for master nodes) |
FSP_ERR_NOT_OPEN | The control block has not been opened. |
fsp_err_t R_SCI_LIN_Close | ( | lin_ctrl_t *const | p_api_ctrl | ) |
Closes the LIN driver.
Implements lin_api_t::close.
Example:
FSP_SUCCESS | Channel successfully closed. |
FSP_ERR_ASSERTION | Pointer to LIN control block is NULL. |
FSP_ERR_NOT_OPEN | The control block has not been opened. |
fsp_err_t R_SCI_LIN_WakeupSend | ( | lin_ctrl_t *const | p_api_ctrl | ) |
Send wakeup is not supported on the SCI LIN driver.
Implements lin_api_t::wakeupSend.
FSP_ERR_UNSUPPORTED | Function not supported in this implementation. |
fsp_err_t R_SCI_LIN_SleepEnter | ( | lin_ctrl_t *const | p_api_ctrl | ) |
Sleep is not supported on the SCI LIN driver.
Implements lin_api_t::sleepEnter.
FSP_ERR_UNSUPPORTED | Function not supported in this implementation. |
fsp_err_t R_SCI_LIN_SleepExit | ( | lin_ctrl_t *const | p_api_ctrl | ) |
Sleep is not supported on the SCI LIN driver.
Implements lin_api_t::sleepExit.
FSP_ERR_UNSUPPORTED | Function not supported in this implementation. |