8-bit Microcontroller. Application Note. AVR286: LIN Firmware Base for LIN/UART Controller. LIN Features. 1. Atmel LIN/UART Controller



Similar documents
Application Note. 8-bit Microcontrollers. AVR270: USB Mouse Demonstration

AVR32701: AVR32AP7 USB Performance. 32-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR1309: Using the XMEGA SPI. 8-bit Microcontrollers. Application Note. Features. 1 Introduction SCK MOSI MISO SS

8-bit. Application Note. Microcontrollers. AVR282: USB Firmware Upgrade for AT90USB

AVR317: Using the Master SPI Mode of the USART module. 8-bit Microcontrollers. Application Note. Features. Introduction

AVR1900: Getting started with ATxmega128A1 on STK bit Microcontrollers. Application Note. 1 Introduction

AVR1510: Xplain training - XMEGA USART. 8-bit Microcontrollers. Application Note. Prerequisites. 1 Introduction

AVR1318: Using the XMEGA built-in AES accelerator. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR033: Getting Started with the CodeVisionAVR C Compiler. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

Using CryptoMemory in Full I 2 C Compliant Mode. Using CryptoMemory in Full I 2 C Compliant Mode AT88SC0104CA AT88SC0204CA AT88SC0404CA AT88SC0808CA

AVR115: Data Logging with Atmel File System on ATmega32U4. Microcontrollers. Application Note. 1 Introduction. Atmel

AVR1600: Using the XMEGA Quadrature Decoder. 8-bit Microcontrollers. Application Note. Features. 1 Introduction. Sensors

AVR319: Using the USI module for SPI communication. 8-bit Microcontrollers. Application Note. Features. Introduction

AVR305: Half Duplex Compact Software UART. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR1922: Xplain Board Controller Firmware. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR1301: Using the XMEGA DAC. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

APPLICATION NOTE. Atmel AVR134: Real Time Clock (RTC) Using the Asynchronous Timer. Atmel AVR 8-bit Microcontroller. Introduction.

Application Note. 8-bit Microcontrollers. AVR272: USB CDC Demonstration UART to USB Bridge

AVR287: USB Host HID and Mass Storage Demonstration. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

Atmel AVR4920: ASF - USB Device Stack - Compliance and Performance Figures. Atmel Microcontrollers. Application Note. Features.

AVR32138: How to optimize the ADC usage on AT32UC3A0/1, AT32UC3A3 and AT32UC3B0/1 series. 32-bit Microcontrollers. Application Note.

Atmel AVR4903: ASF - USB Device HID Mouse Application. Atmel Microcontrollers. Application Note. Features. 1 Introduction

AVR353: Voltage Reference Calibration and Voltage ADC Usage. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR106: C functions for reading and writing to Flash memory. 8-bit Microcontrollers. Application Note. Features. Introduction

Atmel AVR4921: ASF - USB Device Stack Differences between ASF V1 and V2. 8-bit Atmel Microcontrollers. Application Note. Features.

8-bit Microcontroller. Application Note. AVR134: Real-Time Clock (RTC) using the Asynchronous Timer. Features. Theory of Operation.

General Porting Considerations. Memory EEPROM XRAM

AVR134: Real Time Clock (RTC) using the Asynchronous Timer. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR32788: AVR 32 How to use the SSC in I2S mode. 32-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR2006: Design and characterization of the Radio Controller Board's 2.4GHz PCB Antenna. Application Note. Features.

AVR151: Setup and Use of the SPI. Introduction. Features. Atmel AVR 8-bit Microcontroller APPLICATION NOTE

AT91 ARM Thumb Microcontrollers. AT91SAM CAN Bootloader. AT91SAM CAN Bootloader User Notes. 1. Description. 2. Key Features

Application Note. 8-bit Microcontrollers. AVR293: USB Composite Device

AVR055: Using a 32kHz XTAL for run-time calibration of the internal RC. 8-bit Microcontrollers. Application Note. Features.

Application Note. C51 Bootloaders. C51 General Information about Bootloader and In System Programming. Overview. Abreviations

Application Note. 8-bit Microcontrollers. AVR280: USB Host CDC Demonstration. 1. Introduction

8-bit Microcontroller. Application Note. AVR415: RC5 IR Remote Control Transmitter. Features. Introduction. Figure 1.

8-bit RISC Microcontroller. Application Note. AVR910: In-System Programming

Atmel AVR4027: Tips and Tricks to Optimize Your C Code for 8-bit AVR Microcontrollers. 8-bit Atmel Microcontrollers. Application Note.

USER GUIDE EDBG. Description

3-output Laser Driver for HD-DVD/ Blu-ray/DVD/ CD-ROM ATR0885. Preliminary. Summary. Features. Applications. 1. Description

32-bit AVR UC3 Microcontrollers. 32-bit AtmelAVR Application Note. AVR32769: How to Compile the standalone AVR32 Software Framework in AVR32 Studio V2

AT91SAM ARM-based Flash MCU. Application Note

AT89C5131A Starter Kit... Software User Guide

AVR034: Mixing C and Assembly Code with IAR Embedded Workbench for AVR. 8-bit Microcontroller. Application Note. Features.

How To Use An Atmel Atmel Avr32848 Demo For Android (32Bit) With A Microcontroller (32B) And An Android Accessory (32D) On A Microcontroller (32Gb) On An Android Phone Or

AVR030: Getting Started with IAR Embedded Workbench for Atmel AVR. 8-bit Microcontrollers. Application Note. Features.

AVR106: C Functions for Reading and Writing to Flash Memory. Introduction. Features. AVR 8-bit Microcontrollers APPLICATION NOTE

8-bit RISC Microcontroller. Application Note. AVR155: Accessing an I 2 C LCD Display using the AVR 2-wire Serial Interface

AVR1321: Using the Atmel AVR XMEGA 32-bit Real Time Counter and Battery Backup System. 8-bit Microcontrollers. Application Note.

Application Note. USB Mass Storage Device Implementation. USB Microcontrollers. References. Abbreviations. Supported Controllers

AVR Timer/Counter. Prof Prabhat Ranjan DA-IICT, Gandhinagar

AVR245: Code Lock with 4x4 Keypad and I2C LCD. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

8-bit Microcontroller. Application Note. AVR105: Power Efficient High Endurance Parameter Storage in Flash Memory

AVR315: Using the TWI Module as I2C Master. Introduction. Features. AVR 8-bit Microcontrollers APPLICATION NOTE

AT89LP Flash Data Memory. Application Note. AT89LP Flash Data Memory API. 1. Introduction. 2. Theory of Operation. 2.1 Flash Memory Operation

APPLICATION NOTE. Atmel AVR443: Sensor-based Control of Three Phase Brushless DC Motor. Atmel AVR 8-bit Microcontrollers. Features.

Manchester Coding Basics. Application Note. Manchester Coding Basics. 1. Modulation

8051 Flash Microcontroller. Application Note. A Digital Thermometer Using the Atmel AT89LP2052 Microcontroller

APPLICATION NOTE Atmel AT02509: In House Unit with Bluetooth Low Energy Module Hardware User Guide 8-bit Atmel Microcontroller Features Description

AVR223: Digital Filters with AVR. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

APPLICATION NOTE. Atmel AT02985: User s Guide for USB-CAN Demo on SAM4E-EK. Atmel AVR 32-bit Microcontroller. Features. Description.

AVR2004: LC-Balun for AT86RF230. Application Note. Features. 1 Introduction

AVR311: Using the TWI Module as I2C Slave. Introduction. Features. AVR 8-bit Microcontrollers APPLICATION NOTE

AVR32100: Using the AVR32 USART. 32-bit Microcontrollers. Application Note. Features. 1 Introduction

Step Motor Controller. Application Note. AVR360: Step Motor Controller. Theory of Operation. Features. Introduction

AVR131: Using the AVR s High-speed PWM. Introduction. Features. AVR 8-bit Microcontrollers APPLICATION NOTE

PC Base Adapter Daughter Card UART GPIO. Figure 1. ToolStick Development Platform Block Diagram

HANDLING SUSPEND MODE ON A USB MOUSE

APPLICATION NOTE. Atmel AT04389: Connecting SAMD20E to the AT86RF233 Transceiver. Atmel SAMD20. Description. Features

8-bit Microcontroller. Application Note. AVR222: 8-point Moving Average Filter

AVR1003: Using the XMEGA Clock System. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

Develop a Dallas 1-Wire Master Using the Z8F1680 Series of MCUs

Application Note. Atmel ATSHA204 Authentication Modes. Prerequisites. Overview. Introduction

AVR32110: Using the AVR32 Timer/Counter. 32-bit Microcontrollers. Application Note. Features. 1 Introduction

8-bit Microcontroller. Application Note. AVR400: Low Cost A/D Converter

Application Note. Atmel CryptoAuthentication Product Uses. Atmel ATSHA204. Abstract. Overview

AVR241: Direct driving of LCD display using general IO. 8-bit Microcontrollers. Application Note. Features. Introduction AVR

Implementing SPI Master and Slave Functionality Using the Z8 Encore! F083A

APPLICATION NOTE. AT07175: SAM-BA Bootloader for SAM D21. Atmel SAM D21. Introduction. Features

Atmel AVR1017: XMEGA - USB Hardware Design Recommendations. 8-bit Atmel Microcontrollers. Application Note. Features.

AVR125: ADC of tinyavr in Single Ended Mode. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

8-bit RISC Microcontroller. Application Note. AVR236: CRC Check of Program Memory

Application Note. USB Microcontrollers. USB PC Drivers Based on Generic HID Class. 1. Introduction

AT88CK490 Evaluation Kit

Using XGATE to Implement LIN Communication on HCS12X Daniel Malik 8/16-Bit Products Division East Kilbride, Scotland

AVR ONE!... Quick-start Guide. EVK Windows 32104B AVR ONE! 02/10

Local Interconnect Network Training. Local Interconnect Network Training. Overview

In-Vehicle Networking

AVR442: PC Fan Control using ATtiny13. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

Serial Communications

AT91 ARM Thumb Microcontrollers. Application Note. Interfacing a PC Card to an AT91RM9200-DK. Introduction. Hardware Interface

AT15007: Differences between ATmega328/P and ATmega328PB. Introduction. Features. Atmel AVR 8-bit Microcontrollers APPLICATION NOTE

8-bit RISC Microcontroller. Application Note. AVR182: Zero Cross Detector

AN3998 Application note

APPLICATION NOTE. Atmel LF-RFID Kits Overview. Atmel LF-RFID Kit. LF-RFID Kit Introduction

AVR Butterfly Training. Atmel Norway, AVR Applications Group

256K (32K x 8) Battery-Voltage Parallel EEPROMs AT28BV256

8-bit Microcontroller. Application Note. AVR314: DTMF Generator

APPLICATION NOTE. Secure Personalization with Transport Key Authentication. ATSHA204A, ATECC108A, and ATECC508A. Introduction.

Transcription:

AVR286: LIN Firmware Base for LIN/UART Controller LIN Features The LIN (Local Interconnect Network) is a serial communications protocol which efficiently supports the control of mechatronics nodes in distributed automotive applications. The main properties of the LIN bus are: Single master with multiple slaves concept Low cost silicon implementation based on common UART communication Self synchronization with on-chip oscillator in slave node Deterministic signal transmission with signal propagation time computable in advance Low cost single-wire implementation Speed up to 20 Kbit/s. 8-bit Microcontroller Application Note 1. Atmel LIN/UART Controller The LIN/UART Controller is available on some AVR micro controllers such as ATmega32/64/M1/C1 or ATtiny167. 1.1 Features Hardware Implementation of LIN 2.x (LIN 1.x Compatibility) Small, CPU Efficient and Independent Master/Slave Routines Based on LIN Work Flow Concept of LIN 2.x Specification Automatic LIN Header Handling and Filtering of Irrelevant LIN Frames Automatic LIN Response Handling Extended LIN Error Detection and Signaling Hardware Frame Time-out Detection Break-in-data Support Capability Automatic Re-synchronization to Ensure Proper Frame Integrity Fully Flexible Extended Frames Support Capabilities 1.2 Controller Overview The LIN/UART Controller is designed to match as closely as possible the LIN software application structure. The LIN software application is developed as independent tasks, several slave tasks and one master task. The controller conforms to this partitioning. The only link between a master task and a slave task in a master node is the (interrupt) flag IDOK. For a master task, this event represents the end of this task, for the slave task it represents the beginning of this task. When the slave task is warned of an identifier presence, it has first to analyze it to know what to do with the response. Hardware flags identify the presence of one of the specific identifiers from 60 (0x3C) up to 63 (0x3F).

2. Firmware Base 2.1 Description This application note provides a firmware base (drivers) to use the LIN/UART Controller on the ATmega32/64/M1/C1 and ATtiny167. It assumes that readers are familiar with the device architecture and with the LIN/UART Controller. A minimum knowledge of LIN specification v1.x or v2.x (www.lin-subbus.org) is also required to understand the content of this document. This document is written for the software developers to help in the development of their applications or their LIN stack construction with the Atmel micro controllers. The drivers are designed to simplify the programming of the LIN/UART Controller. Some general information and tips are also discussed to help the developers to build quickly their applications. In addition, this document provides application examples to illustrate the usage of these drivers. 2.2 Limitations 2.3 Terminology 2.4 Coding Style Diagnostics frames: handled by LIN library, software placed above the drivers, Sleep & Wake-up: features application dependent, Configuration for LIN hardware test purposes: not included. ID: LIN Identifier Tx: Transmission Rx: Reception The coding style explained below are important to understand the firmware: Defined constants use caps characters. #define CONF_LINBRR 25 C-macro functions use the first character as cap #define Is_lin_header_ready() ((LINSIR & (1<<LIDOK))? TRUE: FALSE) Functions use small caps characters void lin_get_response (unsigned char *l_data) 2.5 Device-specific I/O Header File The firmware described in this document is based upon the device-specific I/O header file provided by the compiler: 1. AVR GCC: iom64m1.h, iom64c1.h, iom32m1.h or iom32c1.h, 2. IAR: iom64m1.h, iom64c1.h, iom32m1.h or iom32c1.h, 3.... In the device-specific I/O header file, the name, the address and the content (register-bit) of all the registers are defined. 2 AVR286

AVR286 3. Definitions First, an application will have to define the following values: #define FOSC 8000 Device frequency in KHz #define LIN_BAUDRATE 19200 LIN baudrate in bit/s 3.1 Bit Time The bit time definition CONF_LINBRR, setup the baud rate for communication. This definition is controlled by FOSC and LIN_BAUDRATE. At the end, only one CONF_LINBRR value is retained to program the LIN baud rate register of the LIN/UART Controller. #ifndef FOSC #error You must define FOSC #elif FOSC == 16000 if device frequency = 16 MHz #ifndef LIN_BAUDRATE #error You must define LIN_BAUDRATE #elif LIN_BAUDRATE == 19200 #define CONF_LINBRR 25 (0x19) 19.2 kbps, error = 0.2% #elif LIN_BAUDRATE == 9600 #define CONF_LINBRR 51 (0x33) 9.6 kbps, error= 0.2% #elif LIN_BAUDRATE == 4800 #define CONF_LINBRR 103 (0x67) 4.8 kbps, error= 0.2% #elif LIN_BAUDRATE == 2400 #define CONF_LINBRR 207 (0xCF) 2.4 kbps, error=-0.2% #else #error Not available LIN_BAUDRATE value #endif #elif FOSC == 8000 if device frequency = 8 MHz #ifndef LIN_BAUDRATE #error You must define LIN_BAUDRATE #elif LIN_BAUDRATE == 19200 #define CONF_LINBRR 12 (0x0C) 19.2 kbps, error = 0.2% #elif LIN_BAUDRATE == 9600 #define CONF_LINBRR 25 (0x19) 9.6 kbps, error= 0.2% #elif LIN_BAUDRATE == 4800 #define CONF_LINBRR 51 (0x33) 4.8 kbps, error= 0.2% #elif LIN_BAUDRATE == 2400 #define CONF_LINBRR 103 (0x67) 2.4 kbps, error=-0.2% #else #error Not available LIN_BAUDRATE value #endif #else #error Not available FOSC value #endif This method uses the conditional compilation of the C preprocessor. This method saves time and code bytes compared to a code line in C. It is very easy to add a new set of CONF_LINBRR values for other FOSC definitions (ex: FOSC == 4000). The CONF_LINBRR value is to calculate using the following formula: CONF_LINBRR = [round {(FOSC x 1000) / (32 x LIN_BAUDRATE)]-1 3.2 Configuration LIN protocols #define LIN_1X (1<<LIN13) #define LIN_2X (0<<LIN13) 3

LIN commands #define LIN_CMD_MASK ((1<<LCMD1) (1<<LCMD0)) #define LIN_RX_HEADER ((0<<LCMD1) (0<<LCMD0)) #define LIN_ABORT ((0<<LCMD1) (0<<LCMD0)) #define LIN_TX_HEADER ((0<<LCMD1) (1<<LCMD0)) #define LIN_RX_RESPONSE((1<<LCMD1) (0<<LCMD0)) #define LIN_TX_RESPONSE((1<<LCMD1) (1<<LCMD0)) LIN interrupt flags #define LIN_ERROR (1<<LERR ) #define LIN_IDOK (1<<LIDOK) #define LIN_RXOK (1<<LRXOK) #define LIN_TXOK (1<<LTXOK) LIN ID masks #define LIN_1X_ID_MASK ((1<<LID3) (1<<LID2) (1<<LID1) (1<<LID0)) #define LIN_1X_LEN_MASK((1<<LID5) (1<<LID4)) #define LIN_2X_ID_MASK ((1<<LID5) (1<<LID4) LIN_1X_ID_MASK ) Specific to ATmega32/64/M1/C1 Specific to ATtiny167 #define LIN_PORT_IN PIND #define LIN_PORT_IN PINA #define LIN_PORT_DIR DDRD #define LIN_PORT_DIR DDRA #define LIN_PORT_OUT PORTD #define LIN_PORT_OUT PORTA #define LIN_INPUT_PIN 4 #define LIN_INPUT_PIN 0 #define LIN_OUTPUT_PIN 3 #define LIN_OUTPUT_PIN 1 This method of definition tolerates changes of register address and allocation of register-bit. 4. C-macros C-macros are commonly used in C to define small snippets of code. During the preprocessing phase, each C-macro call is replaced, in-line, by the corresponding C-macro definition. If the C- macro has parameters, they are substituted into the C-macro body during expansion; thus, a C- macro can mimic a C-function. The usual reason for doing this is to avoid the overhead of a function call in simple cases, where the code is lightweight enough that function call overhead has a significant impact on performance. 4.1 C-macros for LIN 1.x Use LIN 1.x protocol #define Lin_1x_set_type() ( LINCR = LIN_1X ) #define Lin_1x_enable() ( LINCR = LIN_1X (1<<LENA) (0<<LCMD2) ) #define Lin_1x_set_id(id) { LINIDR &= ~LIN_1X_ID_MASK; \ LINIDR = id & LIN_1X_ID_MASK; #define Lin_1x_set_len(len) { LINIDR &= ~LIN_1X_LEN_MASK; \ LINIDR = (((len+4)<<2) & LIN_1X_LEN_MASK; #define Lin_get_len() ( LINDLR & (0x0F << LRXDL0) ) Lin_set_rx_len(len) - Automatic setting in LIN 1.x for response Lin_set_tx_len(len) - Automatic setting in LIN 1.x for response 4.2 C-macros for LIN 2.x Use LIN 2.x protocol #define Lin_2x_set_type() ( LINCR = LIN_2X ) #define Lin_2x_enable() ( LINCR = LIN_2X (1<<LENA) (0<<LCMD2) ) #define Lin_2x_set_id(id) { LINIDR &= ~LIN_2X_ID_MASK; \ LINIDR = id & LIN_2X_ID_MASK; Lin_2x_set_len(len) - Not available in LIN 2.1 Lin_get_len() ------- Not available in LIN 2.1 #define Lin_set_rx_len(len) ( LINDLR = len & (0x0F << LRXDL0) ) #define Lin_set_tx_len(len) ( LINDLR = len << LTXDL0 ) 4 AVR286

AVR286 4.3 Shared C-macros Use of any protocol #define Lin_set_type(ltype)(( ltype == LIN_2X )? \ Lin_2x_set_type(): Lin_1x_set_type() ) #define Lin_get_type() ( LINCR & (1<<LIN1X) ) #define Lin_set_len(len) ( LINDLR = (len<<ltxdl0) (len &(0x0F<<LRXDL0) ) Miscellaneous C-macros #define Lin_get_error_status()( LINERR ) #define Lin_set_baudrate(br) \ { LINBRRH = (unsigned char)(((unsigned short)br)>>8);\ LINBRRL = (unsigned char) ((unsigned short)br); #define Lin_sw_reset() ( LINCR = 1<<LSWRES ) #define Lin_full_reset() { Lin_sw_reset(); Lin_clear_enable_it(); \ LINBRRL = 0x00; LINBRRH = 0x00; Interrupt handling #define Lin_get_it() \ ( LINSIR & ((1<<LERR) (1<<LIDOK) (1<<LTXOK) (1<<LRXOK)) ) #define Lin_set_enable_it() \ ( LINENIR = (1<<LENERR) (1<<LENIDOK) (1<<LENTXOK) (1<<LENRXOK) ) #define Lin_clear_enable_it() \ ( LINENIR = (0<<LENERR) (0<<LENIDOK) (0<<LENTXOK) (0<<LENRXOK) ) #define Lin_clear_err_it() ( LINSIR = 1<<LERR ) #define Lin_clear_idok_it() ( LINSIR = 1<<LIDOK ) #define Lin_clear_txok_it() ( LINSIR = 1<<LTXOK ) #define Lin_clear_rxok_it() ( LINSIR = 1<<LRXOK ) LIN commands #define Lin_abort() (LINCR &= ~LIN_CMD_MASK) #define Lin_rx_header()(LINCR &= ~LIN_CMD_MASK) #define Lin_tx_header(){LINCR &= ~LIN_CMD_MASK; LINCR = LIN_TX_HEADER ; #define Lin_rx_response(){LINCR &= ~LIN_CMD_MASK; LINCR = LIN_RX_RESPONSE; #define Lin_tx_response(){LINCR &= ~LIN_CMD_MASK; LINCR = LIN_TX_RESPONSE; LIN checking #define Is_lin_header_ready() ( (LINSIR & (1<<LIDOK))? 1 : 0 ) #define Is_lin_rx_response_ready()( (LINSIR & (1<<LRXOK))? 1 : 0 ) #define Is_lin_tx_response_ready()( (LINSIR & (1<<LTXOK))? 1 : 0 ) ID & data handling #define Lin_get_id() ( LINIDR & LIN_2X_ID_MASK) #define Lin_clear_index() ( LINSEL = (0<<LAINC) (0x00<<LINDX0) ) #define Lin_get_data() ( LINDAT ) #define Lin_set_data(data) ( LINDAT = data ) 4.4 Remark on C-macro If a long C-macro (several lines of C-code) is too often used, it penalizes the size of generated code (each C-macro call is replaced, in-line, by the corresponding C-macro definition). Then, it will be appropriate to encapsulate it in a C-function. 5

5. C-functions 5.1 LIN/UART Controller Initialization This function initializes the LIN controller and, if needed, the LIN interrupts. Two arguments are passed to the function: 1. unsigned char l_type : By construction, l_type is either LIN_1X or LIN_2X 2. unsigned long b_rate : LIN Baud Rate Register (LINBRR) value The function returns: unsigned char : == 0 : Initialization failed, LIN type is not in accordance!= 0 : Initialization performed Warning: none 5.1.1 Prototype 5.1.2 Function extern unsigned char lin_init (unsigned char l_type, unsigned long b_rate); unsigned char lin_init (unsigned char l_type, unsigned long b_rate) { Pull-up on TxLIN & RxLIN (one by one to use bit-addressing) LIN_PORT_DIR &= ~(1<<LIN_INPUT_PIN ); LIN_PORT_DIR &= ~(1<<LIN_OUTPUT_PIN); LIN_PORT_OUT = (1<<LIN_INPUT_PIN ); LIN_PORT_OUT = (1<<LIN_OUTPUT_PIN); Lin_full_reset(); Lin_set_baudrate(b_rate); if (l_type == LIN_1X) { Lin_1x_enable(); else if (l_type == LIN_2X) { Lin_2x_enable(); else { return 0; If LIN is interrupt driven, enable the 2 following lines Lin_set_enable_it(); asm ("sei"); return 1; 5.1.3 Notes & Remarks The LIN/UART Controller allows user customization of the protocol. It is possible to remove the detection of the CHECKSUM field in received frames, the transmission of the CHECKSUM field or the frame time-out. If these features are insufficient, the LIN/UART Controller can also be initialized as a pure UART to allow maximum user freedom. It is then possible to keep the automatic features to manage the header and switch to manual mode (UART mode) to manage the response. A system mixing reception and transmission in a same response is also possible. 6 AVR286

AVR286 5.2 LIN Header Transmission This function commands the sending of the LIN header, MASTER task of MASTER node. Three arguments are passed to the function: 1. unsigned char l_type : By construction, l_type is either LIN_1X or LIN_2X 2. unsigned char l_id : LIN identifier value. In case of LIN_1X, the coded length is transported into the LIN identifier. 3. unsigned char l_len : True length (not coded), number of data bytes transported in the response. This information is not used in LIN_1X because it is coded in l_id. This function returns: Warning: unsigned char : none == 0 : Initialization failed, LIN type is not in accordance!= 0 : Header transmission of the header on-going 5.2.1 Prototype 5.2.2 Function extern unsigned char lin_tx_header \ (unsigned char l_type, unsigned char l_id, unsigned char l_len); unsigned char lin_tx_header \ (unsigned char l_type, unsigned char l_id, unsigned char l_len) { Lin_abort(); Useful if controller is still in 'lin_tx_response' or in 'lin_rx_response' mode. Note that when these modes are running, writing in LIN registers is disabled and the ID cannot be set in the controller. (c.f. Break-in-Data behavior ) if (l_type == LIN_1X) { Lin_1x_set_id(l_id); Lin_1x_set_len(l_len); else if (l_type == LIN_2X) { Lin_2x_set_id(l_id); No length transported in LIN 2.X else { return 0; Lin_tx_header(); return 1; Set command 5.2.3 Notes & Remarks It will be essential to verify that there will be no error during the transmission. 7

5.3 LIN Response Reception This function commands the reception of a LIN response, SLAVE task of MASTER or SLAVE node. Two arguments are passed to the function: 1. unsigned char l_type : By construction, l_type is either LIN_1X or LIN_2X 2. unsigned char l_len : True length (not coded), number of data bytes expected in the response. This information is not used in LIN_1X because it is coded in l_id. This function returns: unsigned char : == 0 : Initialization failed, LIN type is not in accordance!= 0 : Response reception on-going Warning: At the end of the reception, the LIN/UART Controller data buffer will need to be emptied (read) (c.f. Lin_get_response() function) 5.3.1 Prototype 5.3.2 Function extern unsigned char lin_rx_response \ (unsigned char l_type, unsigned char l_len); unsigned char lin_rx_response \ (unsigned char l_type, unsigned char l_len) { if (l_type == LIN_1X) { Lin_1x_set_type(); else if (l_type == LIN_2X) { Lin_2x_set_type(); Lin_set_rx_len(l_len); else { return 0; Lin_rx_response(); return 1; Change is necessary Change is necessary Set command 5.3.3 Notes & Remarks It will be essential to verify that there will be no error during the reception. 8 AVR286

AVR286 5.4 LIN Response Transmission This function commands the sending of a LIN response, SLAVE task of MASTER or SLAVE node. Three arguments are passed to the function: 1. unsigned char l_type : By construction, l_type is either LIN_1X or LIN_2X 2. unsigned char *l_data : Internal SRAM array pointer. This array contains the data bytes to transmit. 3. unsigned char l_len : True length (not coded), number of data bytes transported in the response. This information is not used in LIN_1X because it is coded in l_id. This function returns: Warning: unsigned char : none == 0 : Initialization failed, LIN type is not in accordance!= 0 : Response transmission on-going 5.4.1 Prototype 5.4.2 Function extern unsigned char lin_tx_response \ (unsigned char l_type, unsigned char *l_data, unsigned char l_len); unsigned char lin_tx_response \ (unsigned char l_type, unsigned char *l_data, unsigned char l_len) { unsigned char i; if (l_type == LIN_1X) { Lin_1x_set_type(); else if (l_type == LIN_2X) { Lin_2x_set_type(); Lin_set_tx_len(l_len); else { return 0; Lin_clear_index(); for (i = 0; i < l_len; i++) { Lin_set_data(*l_data++); Lin_tx_response(); return 1; Change is necessary Change is necessary Data processing Set command 5.4.3 Notes & Remarks It will be essential to verify that there will be no error during the transmission. 9

5.5 Read Data Received This function reads (empties) the reception data buffer when a LIN response had been received. This function is additional of the lin_rx_response() function. Only one argument is passed to the function: 1. unsigned char *l_data : Internal SRAM array pointer. This array will contain the data bytes received. This function returns nothing. Warning: none 5.5.1 Prototype 5.5.2 Function extern void lin_get_response (unsigned char *l_data); void lin_get_response (unsigned char *l_data) { unsigned char i, l_len; l_len = Lin_get_len(); Lin_clear_index(); for (i = 0; i < l_len; i++) { (*l_data++) = Lin_get_data(); 5.5.3 Notes & Remarks (none) 10 AVR286

AVR286 6. Firmware Packaging 6.1 lin_drv.h File 6.2 lin_drv.c File Two files are delivered. lin_drv.h file includes the config.h file where are defined the main clock frequency, the LIN baudrate used and the device-specific I/O header file. In lin_drv.h file, we will find: 1. Definitions: Bit Time definitions, Configuration definitions. 2. C-macros: LIN 1.x C-macros, LIN 2.x C-macros, Shared C-macros. 3. Prototypes of functions: lin_init() lin_tx_header(), lin_rx_response(), lin_tx_response(), lin_get_response(). lin_drv.c file includes the lin_drv.h file. In lin_drv.c file, we will only find the C-functions: 1. lin_init() 2. lin_tx_header(), 3. lin_rx_response(), 4. lin_tx_response(), 5. lin_get_response(). 11

7. LIN Communication Management This paragraph shows a LIN communication management based on a polling method. Management based on interrupts needs to build its own LIN message object descriptor. But many items discussed below can be reused if interrupt method is preferred. 7.1 Using LIN Status & Interrupt Register To manage LIN communication, only reading LIN Status & Interrupt Register (LINSIR) is necessay. Note that this register houses all the interrupt sources. Look at this register: 7 6 5 4 3 2 1 0 LIDST2 LIDST1 LIDST0 LBUSY LERR LIDOK LTXOK LRXOK LINSIR There can be only four events: 1. ID transmission completed / ID received: When the ID flag rises (LIDOK), this means two things (both are important): The LIN header phase has been performed, the header transmission was completed (MASTER node) or an header was received (SLAVE node), The LIN response phase must take place as soon as possible if the node is concerned with the received LIN ID. 2. Response reception completed: When the Rx flag rises (LRXOK), this means that the LIN response reception is now completed and there was no detected error. 3. Response transmission completed: When the Tx flag rises (LTXOK), this means that the LIN response transmission is now completed and there was no detected error. 4. Error: If the error flag rises (LERR), this root cause is contained in the LIN Error Register (LINERR). It is important to note when an error rises. The meaning of the error often depends on it. 7.2 LIN Slave Example ----------------------------------------------------------------------- T I T L E : lin_slave_example.c ----------------------------------------------------------------------- ----- I N C L U D E S #include "config.h" #include "lin_drv.h" ----- D E C L A R A T I O N S #define LIN_ID_0 0x12 #define LIN_ID_1 0x13 #define LEN_0 1 #define LEN_1 2 unsigned char lin_motor_contol[len_0]; unsigned char lin_node_status[len_1]; volatile unsigned char lin_slave_err_nb = 0; ----- F U N C T I O N S......................... lin_id_task function of "lin_slave_example.c" 12 AVR286

AVR286 The LIN ID received must be checked and compared to the ones that the node must manage. void lin_id_task (void) { switch (Lin_get_id()) { case LIN_ID_0: lin_rx_response(lin_2x, LEN_0); case LIN_ID_1: lin_tx_response(lin_2x, lin_node_status, LEN_1); default: ID: absent/refused......................... lin_rx_task function of "lin_slave_example.c" - Save response data - Update application signals. void lin_rx_task (void) { unsigned char l_temp; lin_get_response (lin_motor_contol); Update of the application signals l_temp = lin_motor_contol[0] & 0x03; if((l_temp == 0x01) (l_temp == 0x03)) { PORTB = 1<<PORT0; else { PORTB &= ~(1<<PORT0); if(l_temp == 0x02) { PORTB = 1<<PORT1; else { PORTB &= ~(1<<PORT1);......................... lin_tx_task function of "lin_slave_example.c" - Update buffer array with application signals to be ready for the next transmission void lin_tx_task (void) { Update of the application signals lin_node_status[0] = PINB & 0x03; lin_node_status[1] = lin_slave_err_nb;......................... lin_err_task function of "lin_slave_example.c" 13

- If LIN error, increment the node error number void lin_err_task (void) { Global variable update lin_slave_err_nb++;... M A I N ( )................ main function of "lin_slave_example.c" In a 'no end' loop, do: - if LIN_ID_0 (Rx response 1 Byte ). PORT-B.0 = DC motor command -> clockwise. PORT-B.1 = DC motor command -> counterclockwise - if LIN_ID_1 (Tx response 2 bytes). Byte[0] = motor status. Byte[1] = node error number int main (void) { Port B setting for motor control DDRB = 1<<PORTB0; DDRB = 1<<PORTB1; PORTB &= ~(1<<PORTB0); PORTB &= ~(1<<PORTB1); LIN Initialization lin_init((unsigned char)lin_2x, ((unsigned short)conf_linbrr)); No End Loop while (1) { switch (Lin_get_it()) { case LIN_IDOK: lin_id_task(); Lin_clear_idok_it(); case LIN_RXOK: lin_rx_task(); Lin_clear_rxok_it(); case LIN_TXOK: lin_tx_task(); Lin_clear_txok_it(); case LIN_ERROR: lin_err_task(); Lin_clear_err_it(); default: return 0; Program size (compiler option: -Os): Code = 816 Flash bytes, Data = 4 RAM bytes. 14 AVR286

AVR286 7.3 LIN Master Example A real time task must be added to send the headers. For instance the Timer0 will be used to send them. Else the program looks like the LIN slave example. ----------------------------------------------------------------------- T I T L E : lin_master_example.c ----------------------------------------------------------------------- ----- I N C L U D E S #include "config.h" #include "lin_drv.h" #include <avr/interrupt.h> Use AVR-GCC library ----- D E C L A R A T I O N S #define LIN_ID_0 0x12 #define LIN_ID_1 0x13 #define LEN_0 1 #define LEN_1 2 #define MAGIC_NUMBER 77 unsigned char lin_motor_command[len_0]; unsigned char lin_slave_node_status[len_1]; volatile unsigned char lin_master_err_nb = 0; volatile unsigned char rtc_tics = 0; ----- F U N C T I O N S......................... lin_id_task function of "lin_master_example.c" The LIN ID received must be checked and compared to the ones that the node must manage. void lin_id_task (void) { switch (Lin_get_id()) { case LIN_ID_0: lin_tx_response(lin_2x, lin_motor_command, LEN_0); case LIN_ID_1: lin_rx_response(lin_2x, LEN_1); default: ID: absent/refused......................... lin_rx_task function of "lin_master_example.c" - Save response data - Update application signals. void lin_rx_task (void) { 15

lin_get_response (lin_slave_node_status); If command OK and no error on slave node - PORTB[6] = 0 - else PORTB[6] = 1 if (lin_slave_node_status[0]!= lin_motor_command[0]) { PORTB = 1<<PORT6; else if (lin_slave_node_status[1]!= 0){ PORTB = 1<<PORT6; else { PORTB &= ~(1<<PORT6);......................... lin_tx_task function of "lin_master_example.c" - Update buffer array with application signals to be ready for the next transmission void lin_tx_task (void) { Update of the application signals (active-low switches) lin_motor_command[0] = (~PINB) & 0x03;......................... lin_err_task function of "lin_master_example.c" - If LIN error, increment the node error number void lin_err_task (void) { Global variable update lin_master_err_nb++;......................... OCR0A interrupt service routine of "lin_master_example.c" AVR-GCC declaration (default): ISR(TIMER0_COMPA_vect) IAR declaration: #pragma vector TIMER0_COMPA_vect interrupt void timer0_compa_vect(void) The appropriate LIN header is sent following the rtc_tic value ISR(TIMER0_COMPA_vect) { rtc_tics++; if((rtc_tics & 0x01) == 0x01) { lin_tx_header((unsigned char)lin_2x, (unsigned char)lin_id_0, 0); else { lin_tx_header((unsigned char)lin_2x, (unsigned char)lin_id_1, 0); 16 AVR286

AVR286... M A I N ( )................ main function of "lin_master_example.c" In a 'no end' loop, do: - if LIN_ID_0 (Tx response 1 Byte). PORT-B.0 = DC motor command -> clockwise. PORT-B.1 = DC motor command -> counterclockwise - if LIN_ID_1 (Rx response 2 bytes). Byte[0] = motor status. Byte[1] = slave node error number Timer0 ensures that each header is sent in 20ms intervals. int main (void) { Port B setting for motor control PORTB0 & PORTB1 switches active low with internal pull-up DDRB &= ~(1<<PORTB0); DDRB &= ~(1<<PORTB1); PORTB = 1<<PORTB0; PORTB = 1<<PORTB1; Port B setting for motor status flag DDRB = 1<<PORTB6; PORTB &= ~(1<<PORTB6); LIN initialization lin_init((unsigned char)lin_2x, ((unsigned short)conf_linbrr)); Timer0 & INT initialization (no output) Timer0 Reset TIMSK0 = 0; TCCR0B = 0; TCCR0A = 0; TCNT0 = 0; OCR0A = 0; TIFR0 = ((1<<OCF0A) (1<<TOV0)); No output, mode 2, 10 ms interrupt TCCR0A = 0x02; OCR0A = MAGIC_NUMBER; TCCR0B = 0x05; Timer0 compare A interrupt enable TIMSK0 = 1<<OCIE0A; asm ("sei"); No End Loop while (1) { switch (Lin_get_it()) { case LIN_IDOK: lin_id_task(); Lin_clear_idok_it(); case LIN_RXOK: lin_rx_task(); Lin_clear_rxok_it(); case LIN_TXOK: lin_tx_task(); Lin_clear_txok_it(); case LIN_ERROR: lin_err_task(); Lin_clear_err_it(); default: return 0; Program size (compiler option: -Os): Code = 946 Flash bytes, Data = 5 RAM bytes. 17

7.4 Configuration Header File ----------------------------------------------------------------------- config.h for "lin_slave_example.c" or "lin_master_example.c" ----------------------------------------------------------------------- ----- I N C L U D E S #include <avr/io.h> Use AVR-GCC library ----- D E C L A R A T I O N S Use an external crystal oscillator for "lin_master_example.c" #define FOSC 8000 In KHz #define LIN_BAUDRATE 19200 In bit/s 18 AVR286

Headquarters International Atmel Corporation 2325 Orchard Parkway San Jose, CA 95131 USA Tel: 1(408) 441-0311 Fax: 1(408) 487-2600 Atmel Asia Room 1219 Chinachem Golden Plaza 77 Mody Road Tsimshatsui East Kowloon Hong Kong Tel: (852) 2721-9778 Fax: (852) 2722-1369 Atmel Europe Le Krebs 8, Rue Jean-Pierre Timbaud BP 309 78054 Saint-Quentin-en- Yvelines Cedex France Tel: (33) 1-30-60-70-00 Fax: (33) 1-30-60-71-11 Atmel Japan 9F, Tonetsu Shinkawa Bldg. 1-24-8 Shinkawa Chuo-ku, Tokyo 104-0033 Japan Tel: (81) 3-3523-3551 Fax: (81) 3-3523-7581 Product Contact Web Site www.atmel.com Technical Support avr@atmel.com Sales Contact www.atmel.com/contacts Literature Requests www.atmel.com/literature Disclaimer: The information in this document is provided in connection with Atmel products. No license, express or implied, by estoppel or otherwise, to any intellectual property right is granted by this document or in connection with the sale of Atmel products. EXCEPT AS SET FORTH IN ATMEL S TERMS AND CONDI- TIONS OF SALE LOCATED ON ATMEL S WEB SITE, ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS, IMPLIED OR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, CONSEQUENTIAL, PUNITIVE, SPECIAL OR INCIDEN- TAL DAMAGES (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF PROFITS, BUSINESS INTERRUPTION, OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT, EVEN IF ATMEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of this document and reserves the right to make changes to specifications and product descriptions at any time without notice. Atmel does not make any commitment to update the information contained herein. Unless specifically provided otherwise, Atmel products are not suitable for, and shall not be used in, automotive applications. Atmel s products are not intended, authorized, or warranted for use as components in applications intended to support or sustain life. 2008 Atmel Corporation. All rights reserved. Atmel, logo and combinations thereof, AVR and others are registered trademarks or trademarks of Atmel Corporation or its subsidiaries. Other terms and product names may be trademarks of others.