This is the quick start guide for the LIN module, with step-by-step instructions on how to configure and use the driver in a selection of use cases.
The use cases contain several code fragments. The code fragments in the steps for setup can be copied into a custom initialization function, while the steps for usage can be copied into, e.g., the main application function.
LIN use cases
LIN master use case - Setup LIN master.
In this use case, the LIN node is set as a master PUBLISH. A specific data will be sent via it.
Prerequisites
- General Purpose I/O Management (gpio)
- Universal Synchronous Asynchronous Receiver Transmitter (USART)
- Timer Counter (TC)
Code
The following definitions must be added to the project.
#define LIN_FRAME_ID_12 0x12
#define LIN_TRANSFER_BYTE 0x55
#define LIN_MASTER_NODE_NUM 0
#define TC_CHANNEL 0
#define TC_FREQ 20
An array for the transfer bytes must be added:
The Timer Counter are used to generate period cycle for the LIN master transfer. More information about how to configure and use TC can be found at sam_drivers_tc_group.
{
uint32_t ul_div;
uint32_t ul_tcclks;
static uint32_t ul_sysclk;
tc_init(TC0, 0, ul_tcclks | TC_CMR_CPCTRG);
NVIC_EnableIRQ((IRQn_Type)ID_TC0);
}
Add to application initialization:
{
lin_desc.uc_status = 0;
}
Workflow
- LIN master node ID:
#define LIN_FRAME_ID_12 0x12
- First data of the transfer:
#define LIN_TRANSFER_BYTE 0x55
- Mater node number:
#define LIN_MASTER_NODE_NUM 0
- Timer Counter channel number:
- Timer Counter frequency:
- Initialize system clock:
- Initialize board peripherals:
- Enable PMC for USART:
- Initialize LIN master, set the master with baudrate 9600:
- Set LIN master descriptor ID:
- Set LIN master descriptor transfer size:
- Set LIN master transfer type PUBLISHER:
- Set LIN master default transfer status:
- Set LIN master transfer data buffer:
- Setup the master descriptor:
- Initialize the Timer Counter:
- Start the Timer Counter:
Use case
Example code
Add to application C-file:
{
sizeof(lin_data_node));
}
Workflow
- The LIN master period is generated by TC interrupt service routine:
- Clear Timer Counter status:
- Set the first byte of the transfer:
- Start the transfer:
LIN slave use case - Setup LIN slave.
In this use case, the LIN node is set as a slave subscriber. An LED will be flash when a specific data received.
Prerequisites
- General Purpose I/O Management (gpio)
- Universal Synchronous Asynchronous Receiver Transmitter (USART)
- Timer Counter (TC)
Workflow
The following definitions must be added to the project.
#define LIN_FRAME_ID_12 0x12
#define LIN_SPECIFIC_BYTE 0x55
#define LIN_SLAVE_NODE_NUM 0
An array for the received byte must be added:
uint8_t lin_data_node[8];
The usart0 interrupt service handler must be created to receive the LIN frame.
Add to application initialization:
{
lin_desc.uc_status = 0;
NVIC_EnableIRQ(USART0_IRQn);
Workflow
- LIN slave node ID:
#define LIN_FRAME_ID_12 0x12
- Specific data to flash the LED:
#define LIN_SPECIFIC_BYTE 0x55
- Slave node number:
#define LIN_SLAVE_NODE_NUM 0
- Initialize system clock:
- Initialize board peripherals:
- Enable PMC for USART:
- Initialize LIN master, set the master with baudrate 9600:
- Set LIN slave descriptor ID:
- Set LIN master descriptor transfer size:
- Set LIN slave transfer type SUBSCRIBE:
- Set LIN master default transfer status:
- Set LIN slave transfer data buffer:
- Set LIN slave task handle:
- Setup the salve descriptor:
Use case
Example code
Add to application C-file:
{
if (uc_buf[0] == LIN_SPECIFIC_BYTE) {
}
}
Workflow
- The slave task is defined in the LIN descriptor.
- Check if received the spcific byte and flash the LED.
if (uc_buf[0] == LIN_SPECIFIC_BYTE) {
}