How to use the Uart in LIN Mode in Kinetis K20 Family

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to use the Uart in LIN Mode in Kinetis K20 Family

2,718 Views
frankmuenzner
Contributor I

Hi,

I'm tying to read LIN data in a K20 Family device.

The Final device should be a data logger for logging of LIN data and serial data to Write this data to SD card

My system hardware setting:

Uart 1 - LIN data from test device  - rx line connected LIN Bus with 10k resistor.

Uart 3 - serial data from test device

Spi connection to Micro SD Card Adapter

I have read the reference manual and have done the following steps but i cant read the LIN data .

Is their any Application note or code example.

Has someone a good tip?

which is the correct way to initialise the Uart and to read the LIN data by ISR routine?

0 Kudos
5 Replies

993 Views
Kan_Li
NXP TechSupport
NXP TechSupport

Hi Frank,

I will contact our local AE team to check if any sample code available for your reference, and would you please share your code for a review? Which IDE you are using? Please kindly help to clarify! Thanks for your patience!

Have a great day!

B.R

Kan

0 Kudos

993 Views
frankmuenzner
Contributor I

Hello Kan,

Thanks for your reply.

Im using the Arduino IDE with the teenduino Plugin.

I use it because i have a Teensy 3.1

The teensy is microcontroller board based on MK20DX256VLH7 (Cortex-M4

base)

Because of the special IDE i think it is'nt useful if i share my code.

If i have a small sample program i can easy port.

For test if i can catch LIN messages i make a test program only with the

functionality to print out the LIN data.

The Lin protocol in my application is v1.3 / the speed is 19200.

It would be really nice if you can give me some informations.

My Serial initialisation routine is:

void serial_begin(uint32_t divisor)

{

ledVal = 0;

digitalWrite(LEDPIN, ledVal);

SIM_SCGC4 |= SIM_SCGC4_UART0; // turn on clock, TODO: use

bitband

rx_buffer_head = 0;

rx_buffer_tail = 0;

tx_buffer_head = 0;

tx_buffer_tail = 0;

transmitting = 0;

CORE_PIN0_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE |

PORT_PCR_MUX(3);

CORE_PIN1_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT

PCRMUX(3);

UART0_BDH = (divisor >> 13) & 0x1F;

UART0_BDL = (divisor >> 5) & 0xFF;

UART0_C4 = divisor & 0x1F;

//UART0_C1 = 0;

UART0_C1 = UART_C1_ILT;

UART0_TWFIFO = 2; // tx watermark, causes S1_TDRE to set

UART0_RWFIFO = 4; // rx watermark, causes S1_RDRF to set

UART0_PFIFO = UART_PFIFO_TXFE | UART_PFIFO_RXFE;

UART0_C2 = C2_TX_INACTIVE;

// #if UART_MODE == LIN_MODE

UART0_BDH = UART_BDH_LBKDIE |((divisor >> 13) & 0x1F);

UART0_BDL = (divisor >> 5) & 0xFF;

UART0_S2 = UART0_S2 | UART_S2_BRK13 | UART_S2_LBKDE;

// #endif

NVIC_SET_PRIORITY(IRQ_UART0_STATUS, IRQ_PRIORITY);

NVIC_ENABLE_IRQ(IRQ_UART0_STATUS);

}

my Interrupt service routine:

// status interrupt combines

// Transmit data below watermark UART_S1_TDRE

// Transmit complete UART_S1_TC

// Idle line UART_S1_IDLE

// Receive data above watermark UART_S1_RDRF

// LIN break detect UART_S2_LBKDIF

// RxD pin active edge UART_S2_RXEDGIF

void uart0_status_isr(void)

{

uint32_t head, newhead, tail, n;

uint8_t avail, c, buffer;

if (UART0_S2 & UART_S2_LBKDIF)

{

__disable_irq();

// if (UART_S1 & UART_S1_OR) avail =

UART0_S1;

buffer = UART0_D;

digitalWrite(OTHERPIN, ledVal ^= 1);

avail = UART0_S1;

UART0_S1 = avail;

avail = UART0_S1;

// (void)(UART0_S1 == 0); //

Dummy read of the SCIS1 register to clear flags

// (void)(UART0_D == 0); //

Dummy read of the SCID register to clear flags

if (UART0_S2 & UART_S2_LBKDIF &

UART_S2_RXEDGIF)

{

UART0_S2 = UART0_S2 |

UART_S2_LBKDIF |UART_S2_RXEDGIF ;

lin_break_count

=lin_break_count+20;

}

else UART0_S2 = (UART0_S2 |

UART_S2_LBKDIF);

lin_break_count++;

avail = UART0_RCFIFO;

if (avail == 0) {

(void)(UART0_S1 == 0);

// Dummy read of the SCIS1 register to clear flags

(void)(UART0_D == 0);

// Dummy read of the SCID register to clear flags

c = UART0_D;

UART0_CFIFO = UART_CFIFO_RXFLUSH;

__enable_irq();

}

else {

__enable_irq();

head = rx_buffer_head;

tail = rx_buffer_tail;

do {

n = UART0_D;

if (use9Bits && (UART0_C3

& 0x80)) n |= 0x100;

newhead = head + 1;

if (newhead >=

RX_BUFFER_SIZE) newhead = 0;

if (newhead != tail) {

head = newhead;

rx_buffer[head]

=

n;

}

} while (--avail > 0);

rx_buffer_head = head;

__enable_irq();

}

/* if (lin_break_count < 10000)

{

lin_break_count++;

// UART0_S2 = UART0_S2 |

UART_S2_LBKDIF ; // to clear the LIN Flag write it

c = UART0_S1;

c = c + UART0_S2;

c = c + UART0_C1;

c = c + UART0_C2;

c = c + UART0_C3;

c = c + UART0_D;

lin_break_count = UART0_RCFIFO

+c;

}

else lin_break_count = 0;

*/

}

if (UART0_S1 & (UART_S1_RDRF | UART_S1_IDLE)) {

__disable_irq();

avail = UART0_RCFIFO;

if (avail == 0) {

// The only way to clear the IDLE interrupt flag

is

// to read the data register. But reading with no

// data causes a FIFO underrun, which causes the

// FIFO to return corrupted data.

c = UART0_D;

UART0_CFIFO = UART_CFIFO_RXFLUSH;

__enable_irq();

} else {

__enable_irq();

head = rx_buffer_head;

tail = rx_buffer_tail;

do {

n = UART0_D;

if (use9Bits && (UART0_C3 & 0x80)) n |=

0x100;

newhead = head + 1;

if (newhead >= RX_BUFFER_SIZE) newhead =

0;

if (newhead != tail) {

head = newhead;

rx_buffer[head] = n;

}

} while (--avail > 0);

rx_buffer_head = head;

}

}

c = UART0_C2;

if ((c & UART_C2_TIE) && (UART0_S1 & UART_S1_TDRE)) {

head = tx_buffer_head;

tail = tx_buffer_tail;

do {

if (tail == head) break;

if (++tail >= TX_BUFFER_SIZE) tail = 0;

avail = UART0_S1;

n = tx_buffer[tail];

if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n

& 0x100) >> 2);

UART0_D = n;

} while (UART0_TCFIFO < 8);

tx_buffer_tail = tail;

if (UART0_S1 & UART_S1_TDRE) UART0_C2 = C2_TX_COMPLETING;

}

if ((c & UART_C2_TCIE) && (UART0_S1 & UART_S1_TC)) {

transmitting = 0;

if (transmit_pin) *transmit_pin = 0;

UART0_C2 = C2_TX_INACTIVE;

}

lin_break_count = buffer;

}

as you some of the code is greyed out - because i test different settings

to get it working.

I hope you can support me.

B.R:

Frank

Mit freundlichen Grüßen / best regards

Frank Münzner

E.G.O. Elektro-Gerätebau GmbH

Innovation / Research & Developement

Rote-Tor-Straße 14

75038 Oberderdingen

Deutschland

Fon +49 7045 45-68088

Fax +49 7045 9615-68088

E-Mail: Frank.Muenzner@egoproducts.com

Internet: http://www.egoproducts.com

Geschäftsführer: Dr. Johannes Haupt (Vors.), Dr. Karlheinz Hörsting

Vorsitzender des Aufsichts- u. Verwaltungsrates: Armin Sohler

Sitz: Oberderdingen, Handelsregister Mannheim HRB 240 044

Von: Kan_Li <admin@community.freescale.com>

An: Frank Muenzner <frank.muenzner@egoproducts.com>,

Datum: 10.07.2014 04:34

Betreff: Re: - How to use the Uart in

LIN Mode in Kinetis K20 Family

How to use the Uart in LIN Mode in Kinetis K20 Family

reply from Kan_Li in Kinetis Microcontrollers - View the full discussion

Hi Frank,

I will contact our local AE team to check if any sample code available for

your reference, and would you please share your code for a review? Which

IDE you are using? Please kindly help to clarify! Thanks for your

patience!

Have a great day!

B.R

Kan

Reply to this message by replying to this email, or go to the message on

Freescale Community

Start a new discussion in Kinetis Microcontrollers by email or at

Freescale Community

Following How to use the Uart in LIN Mode in Kinetis K20 Family in these

streams: Inbox

0 Kudos

993 Views
Kan_Li
NXP TechSupport
NXP TechSupport

Hi Frank,

Our AE team provided the UART validation code for your reference, which is for 120MHz part, but should be general across all K serials because UART modules are the same. Please kindly refer to attachment for more details.

Hope that helps,

B.R

Kan !

993 Views
frankmuenzner
Contributor I

Hello Kan,

thanks for yourt answer - i will test if it i get the code - I can see no

attacement.Could it be that you forget to attach the attachment.

Many many thanks for your help.

B.R.

Frank

Mit freundlichen Grüßen / best regards

Frank Münzner

E.G.O. Elektro-Gerätebau GmbH

Innovation / Research & Developement

Rote-Tor-Straße 14

75038 Oberderdingen

Deutschland

Fon +49 7045 45-68088

Fax +49 7045 9615-68088

E-Mail: Frank.Muenzner@egoproducts.com

Internet: http://www.egoproducts.com

Geschäftsführer: Dr. Johannes Haupt (Vors.), Dr. Karlheinz Hörsting

Vorsitzender des Aufsichts- u. Verwaltungsrates: Armin Sohler

Sitz: Oberderdingen, Handelsregister Mannheim HRB 240 044

Von: Kan_Li <admin@community.freescale.com>

An: Frank Muenzner <frank.muenzner@egoproducts.com>,

Datum: 11.07.2014 05:51

Betreff: Re: - Re: How to use the Uart

in LIN Mode in Kinetis K20 Family

Re: How to use the Uart in LIN Mode in Kinetis K20 Family

reply from Kan_Li in Kinetis Microcontrollers - View the full discussion

Hi Frank,

Our AE team provided the UART validation code for your reference, which is

for 120MHz part, but should be general across all K serials because UART

modules are the same. Please kindly refer to attachment for more details.

Hope that helps,

B.R

Kan !

Reply to this message by replying to this email, or go to the message on

Freescale Community

Start a new discussion in Kinetis Microcontrollers by email or at

Freescale Community

Following Re: How to use the Uart in LIN Mode in Kinetis K20 Family in

these streams: Inbox

0 Kudos

993 Views
Kan_Li
NXP TechSupport
NXP TechSupport

Hi Frank,

I put it in the community, please kindly log on and fetch it in the thread.

Thanks for your patience!

B.R

Kan

0 Kudos