iLLD_TC27xD  1.0
IfxAsclin_Lin.c
Go to the documentation of this file.
1 /**
2  * \file IfxAsclin_Lin.c
3  * \brief ASCLIN LIN details
4  *
5  * \version iLLD_1_0_0_11_0
6  * \copyright Copyright (c) 2013 Infineon Technologies AG. All rights reserved.
7  *
8  *
9  * IMPORTANT NOTICE
10  *
11  *
12  * Infineon Technologies AG (Infineon) is supplying this file for use
13  * exclusively with Infineon's microcontroller products. This file can be freely
14  * distributed within development tools that are supporting such microcontroller
15  * products.
16  *
17  * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
18  * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
20  * INFINEON SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,
21  * OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
22  *
23  */
24 
25 /******************************************************************************/
26 /*----------------------------------Includes----------------------------------*/
27 /******************************************************************************/
28 
29 #include "IfxAsclin_Lin.h"
30 
31 /******************************************************************************/
32 /*-------------------------Function Implementations---------------------------*/
33 /******************************************************************************/
34 
36 {
37  Ifx_ASCLIN *asclinSFR = asclin->asclin; /* getting the pointer to ASCLIN registers from module handler*/
38 
39  /* check Rx header end acknowledgement */
40  if (IfxAsclin_getRxHeaderEndFlagStatus(asclinSFR)) /* RHE flag*/
41  {
43  asclin->acknowledgmentFlags.rxHeaderEnd = 1;
44  }
45 
46  /* check for Rx header error flags */
47  if (IfxAsclin_getHeaderTimeoutFlagStatus(asclinSFR)) /* HT flag*/
48  {
50  asclin->errorFlagsStatus.headerTimeout = 1;
51  }
52 
53  if (IfxAsclin_getCollisionDetectionErrorFlagStatus(asclinSFR)) /* CE flag*/
54  {
57  }
58 
59  if (IfxAsclin_getFrameErrorFlagStatus(asclinSFR)) /* FE flag*/
60  {
62  asclin->errorFlagsStatus.frameError = 1;
63  }
64 
65  if (IfxAsclin_getLinAutoBaudDetectionErrorFlagStatus(asclinSFR)) /* LA flag*/
66  {
69  }
70 
71  if (IfxAsclin_getLinParityErrorFlagStatus(asclinSFR)) /* LP flag*/
72  {
74  asclin->errorFlagsStatus.linParityError = 1;
75  }
76 }
77 
78 
80 {
81  Ifx_ASCLIN *asclinSFR = asclin->asclin; /* getting the pointer to ASCLIN registers from module handler*/
82 
83  /* check Rx response end acknowledgement*/
85  {
88  }
89 
90  /* check for Rx response error flags */
91  if (IfxAsclin_getRxFifoOverflowFlagStatus(asclinSFR)) /* RFO flag*/
92  {
94  asclin->errorFlagsStatus.rxFifoOverflow = 1;
95  }
96 
97  if (IfxAsclin_getCollisionDetectionErrorFlagStatus(asclinSFR)) /* CE flag*/
98  {
101  }
102 
103  if (IfxAsclin_getFrameErrorFlagStatus(asclinSFR)) /* FE flag*/
104  {
106  asclin->errorFlagsStatus.frameError = 1;
107  }
108 
109  if (IfxAsclin_getLinChecksumErrorFlagStatus(asclinSFR)) /* LC flag*/
110  {
113  }
114 
115  if (IfxAsclin_getResponseTimeoutFlagStatus(asclinSFR)) /* RT flag*/
116  {
118  asclin->errorFlagsStatus.responseTimeout = 1;
119  }
120 
121  if ((asclin->linMode == IfxAsclin_LinMode_slave) && (IfxAsclin_getBreakDetectedFlagStatus(asclinSFR))) /* BD flag only in slave mode*/
122  {
124  asclin->errorFlagsStatus.breakDetected = 1;
125  }
126 }
127 
128 
130 {
131  Ifx_ASCLIN *asclinSFR = asclin->asclin; /* getting the pointer to ASCLIN registers from module handler*/
132 
133  /* check Tx header end acknowledgement */
134  if (IfxAsclin_getTxHeaderEndFlagStatus(asclinSFR)) /* THE flag*/
135  {
137  asclin->acknowledgmentFlags.txHeaderEnd = 1;
138  }
139 
140  /* check for Tx header error flags */
141  if (IfxAsclin_getHeaderTimeoutFlagStatus(asclinSFR)) /* HT flag*/
142  {
144  asclin->errorFlagsStatus.headerTimeout = 1;
145  }
146 
147  if (IfxAsclin_getCollisionDetectionErrorFlagStatus(asclinSFR)) /* CE flag*/
148  {
151  }
152 
153  if (IfxAsclin_getFrameErrorFlagStatus(asclinSFR)) /* FE flag*/
154  {
156  asclin->errorFlagsStatus.frameError = 1;
157  }
158 
159  if (IfxAsclin_getLinParityErrorFlagStatus(asclinSFR)) /* LP flag*/
160  {
162  asclin->errorFlagsStatus.linParityError = 1;
163  }
164 }
165 
166 
168 {
169  Ifx_ASCLIN *asclinSFR = asclin->asclin; /* getting the pointer to ASCLIN registers from module handler*/
170 
171  /* check Tx response end acknowledgement */
172  if (IfxAsclin_getTxResponseEndFlagStatus(asclinSFR)) /* TRE flag*/
173  {
176  }
177 
178  /* check for Tx response error flags */
179  if (IfxAsclin_getTxFifoOverflowFlagStatus(asclinSFR)) /* TFO flag*/
180  {
182  asclin->errorFlagsStatus.txFifoOverflow = 1;
183  }
184 
185  if (IfxAsclin_getCollisionDetectionErrorFlagStatus(asclinSFR)) /* CE flag*/
186  {
189  }
190 
191  if (IfxAsclin_getResponseTimeoutFlagStatus(asclinSFR)) /* RT flag*/
192  {
194  asclin->errorFlagsStatus.responseTimeout = 1;
195  }
196 
197  if ((asclin->linMode == IfxAsclin_LinMode_slave) && (IfxAsclin_getBreakDetectedFlagStatus(asclinSFR))) /* BD flag only in slave mode*/
198  {
200  asclin->errorFlagsStatus.breakDetected = 1;
201  }
202 }
203 
204 
206 {
207  /*clear acknowledgement flags status */
208  asclin->acknowledgmentFlags.txHeaderEnd = 0;
209  asclin->acknowledgmentFlags.rxHeaderEnd = 0;
212 
213  /* clear error flags status*/
214  asclin->errorFlagsStatus.headerTimeout = 0;
216  asclin->errorFlagsStatus.frameError = 0;
217  asclin->errorFlagsStatus.responseTimeout = 0;
218  asclin->errorFlagsStatus.linParityError = 0;
219  asclin->errorFlagsStatus.breakDetected = 0;
222  asclin->errorFlagsStatus.txFifoOverflow = 0;
223  asclin->errorFlagsStatus.rxFifoOverflow = 0;
224 }
225 
226 
228 {
229  Ifx_ASCLIN *asclinSFR = asclin->asclin; /* getting the pointer to ASCLIN registers from module handler*/
231  IfxScuWdt_clearCpuEndinit(psw); /* clearing the endinit protection*/
232  IfxAsclin_setDisableModuleRequest(asclinSFR); /* disabling the module*/
233  IfxScuWdt_setCpuEndinit(psw); /* setting the endinit protection back on*/
234 }
235 
236 
238 {
239  Ifx_ASCLIN *asclinSFR = asclin->asclin; /* getting the pointer to ASCLIN registers from module handler*/
240  IfxAsclin_enableRxFifoInlet(asclinSFR, FALSE); /* disable Rx FIFO*/
242  IfxAsclin_clearAllFlags(asclinSFR); /* clear all flags*/
243 }
244 
245 
247 {
248  Ifx_ASCLIN *asclinSFR = config->asclin; /* pointer to ASCLIN registers */
250 
251  asclin->asclin = asclinSFR; /* adding register pointer to module handler*/
252  IfxAsclin_enableModule(asclinSFR); /* enabling the module*/
253 
254  /* mode initialisation */
255  IfxAsclin_setClockSource(asclinSFR, IfxAsclin_ClockSource_noClock); /* disabling the clock*/
256  IfxAsclin_setFrameMode(asclinSFR, IfxAsclin_FrameMode_initialise); /* setting the module in Initialise mode*/
257  IfxAsclin_setClockSource(asclinSFR, config->clockSource); /* setting the clock source*/
258 
259  /* lin mode initialisation */
260  IfxAsclin_setClockSource(asclinSFR, IfxAsclin_ClockSource_noClock); /* disabling the clock*/
261  IfxAsclin_setFrameMode(asclinSFR, config->frameMode); /* setting the module in Lin mode*/
262  IfxAsclin_setLinMode(asclinSFR, config->linMode); /* configuring lin mode of operation (master/slave)*/
263  IfxAsclin_setRxInput(asclinSFR, config->alti); /* selecting the Rx(alternate) input pin*/
264  IfxAsclin_setPrescaler(asclinSFR, config->btc.prescaler); /* setting the prescaler*/
265  IfxAsclin_setClockSource(asclinSFR, config->clockSource); /* setting the clock source*/
266 
267  /* baudrate generation in both modes */
268  status = (IfxAsclin_Status)IfxAsclin_setBitTiming(asclinSFR, config->brg.baudrate,
269  config->btc.oversampling,
270  config->bsc.samplePointPosition,
271  config->bsc.medianFilter); /* setting the baudrate bit fields to generate the required baudrate*/
272 
273  IfxAsclin_setClockSource(asclinSFR, IfxAsclin_ClockSource_noClock); /* disabling the clock again*/
274 
275  /* auto baud rate detection in slave mode */
276  if (config->linMode == IfxAsclin_LinMode_slave)
277  {
278  IfxAsclin_enableAutoBaudrateDetection(asclinSFR, config->brd.abd); /* setting the auto baudrate detection (enable/disable)*/
279  IfxAsclin_setBrdUpperlimt(asclinSFR, config->brd.upperLimit); /* setting the upper limit for abd*/
280  IfxAsclin_setBrdLowerlimt(asclinSFR, config->brd.lowerLimit); /* setting the lower limit for abd*/
281  }
282 
283  /* frame control initialisation */
284  IfxAsclin_enableParity(asclinSFR, config->frame.parityEnable); /* setting parity (enable/disable)*/
285  IfxAsclin_setParityType(asclinSFR, config->frame.parityType); /* setting parity type (odd/even)*/
286  IfxAsclin_setStopBit(asclinSFR, config->frame.stopBit); /* setting the stop bit*/
287  IfxAsclin_enableCollisionDetection(asclinSFR, config->frame.collisionDetectionEnable); /* setting collision detection (enable/disable)*/
288  IfxAsclin_setShiftDirection(asclinSFR, config->frame.shiftDir); /* setting the shift direction*/
289 
290  /* data control initialisation */
291  IfxAsclin_setDataLength(asclinSFR, config->data.dataLength); /* setting the number of bytes to be transfered*/
292  IfxAsclin_setHeaderResponseSelect(asclinSFR, config->data.headerOnly); /* setting the header only or header and response mode*/
293  IfxAsclin_setLinResponseTimeoutMode(asclinSFR, config->data.responseTimeoutMode); /* setting the response timeout mode (frame/response)*/
294  IfxAsclin_setChecksumMode(asclinSFR, config->data.checksum); /* setting the checksum mode (classic/enhanced)*/
295  IfxAsclin_setLinResponseTimeoutThreshold(asclinSFR, config->data.responseTimeout); /* setting the lin response timeout threshold*/
296 
297  /* lin control initialisation */
298  IfxAsclin_enableHardwareChecksum(asclinSFR, config->lin.csEnable); /* setting the hardware checksum (enable/disable)*/
299  IfxAsclin_setChecksumInjection(asclinSFR, config->lin.csi); /* setting checksum injection*/
300 
301  /* master specific initialisation */
302  if (config->linMode == IfxAsclin_LinMode_master)
303  {
304  IfxAsclin_setIdleDelay(asclinSFR, config->frame.idleDelay); /* setting idle delay for master*/
305  IfxAsclin_setLeadDelay(asclinSFR, config->frame.leadDelay); /* setting lead delay for master mode*/
306  IfxAsclin_setLinBreakLength(asclinSFR, config->lin.breakLength); /* setting the break generation length*/
307  IfxAsclin_setLinHeaderTimeout(asclinSFR, config->lin.headerTimeout); /* setting the header timeout value for master*/
308  asclin->receiveIdEnable = config->receiveIdEnable; /* echoing the selection of receiving Id in its own Rx FIFO*/
309  }
310 
311  /* slave specific initialisation */
312  if (config->linMode == IfxAsclin_LinMode_slave)
313  {
314  IfxAsclin_setIdleDelay(asclinSFR, config->frame.idleDelay); /* setting idle delay for slave */
315  IfxAsclin_setLeadDelay(asclinSFR, config->frame.leadDelay); /* setting lead delay for slave mode */
316  IfxAsclin_setLinBreakLength(asclinSFR, config->lin.breakLength); /* setting the break detection length */
317  IfxAsclin_setLinHeaderTimeout(asclinSFR, config->lin.headerTimeout); /* setting the header timeout value for slave*/
318  }
319 
320  /* fifo control initialisation */
321  IfxAsclin_setTxFifoInletWidth(asclinSFR, config->fifo.inWidth); /* setting Tx FIFO inlet width*/
322  IfxAsclin_setRxFifoOutletWidth(asclinSFR, config->fifo.outWidth); /* setting Rx FIFO outlet width*/
323  IfxAsclin_setRxBufferMode(asclinSFR, config->fifo.buffMode); /* setting Rx FIFO mode*/
324  IfxAsclin_enableTxFifoOutlet(asclinSFR, FALSE); /* disabling Rx FIFO for recieving*/
325  IfxAsclin_enableRxFifoInlet(asclinSFR, FALSE); /* disabling Tx FIFO for transmitting*/
326  IfxAsclin_flushRxFifo(asclinSFR); /* flushing Rx FIFO*/
327  IfxAsclin_flushTxFifo(asclinSFR); /* flushing Tx FIFO*/
328 
329  IfxAsclin_disableAllFlags(asclinSFR); /* disable all flags*/
330  IfxAsclin_clearAllFlags(asclinSFR); /* clear all flags*/
331 
332  /* Pin mapping*/
333  const IfxAsclin_Lin_Pins *pins = config->pins;
334 
335  if (pins != NULL_PTR)
336  {
337  IfxAsclin_Rx_In *rx = pins->rx;
338 
339  if (rx != NULL_PTR)
340  {
341  IfxAsclin_initRxPin(rx, pins->rxMode);
342  }
343 
344  IfxAsclin_Tx_Out *tx = pins->tx;
345 
346  if (tx != NULL_PTR)
347  {
348  IfxAsclin_initTxPin(tx, pins->txMode, pins->pinDriver);
349  }
350  }
351 
352  IfxAsclin_setClockSource(asclinSFR, config->clockSource); /* setting the clock source*/
353 
354  asclin->linMode = config->linMode; /* echoing the mode of operation to module handler*/
355 
356  return status;
357 }
358 
359 
360 void IfxAsclin_Lin_initModuleConfig(IfxAsclin_Lin_Config *config, Ifx_ASCLIN *asclin)
361 {
362  config->asclin = asclin;
363  config->frameMode = IfxAsclin_FrameMode_lin; /* LIN mode*/
364  config->linMode = IfxAsclin_LinMode_master; /* Lin Master mode*/
365  config->clockSource = IfxAsclin_ClockSource_kernelClock; /* kernel clock, fclc*/
366  config->alti = IfxAsclin_RxInputSelect_0; /* alternate input 0;*/
367 
368  /* Default values for baudrate */
369  config->brg.baudrate = 19200.0; /* default baudrate (the fractional dividier setup will be calculated in initModule)*/
370  /* Default Values for Bit Timings */
371  config->btc.prescaler = 4; /* default prescaler*/
372  config->btc.oversampling = IfxAsclin_OversamplingFactor_16; /* default oversampling factor*/
373  /* Default Values for Bit sampling */
374  config->bsc.filterDepth = 24U; /* digital glitch filter depth 24*/
375  config->bsc.medianFilter = IfxAsclin_SamplesPerBit_one; /* one sample per bit*/
376  config->bsc.samplePointPosition = IfxAsclin_SamplePointPosition_3; /* sample point position at 3*/
377  /* Default Values for Frame Control */
378  config->frame.parityEnable = FALSE; /* disable parity*/
379  config->frame.parityType = IfxAsclin_ParityType_even; /* even parity (if parity enabled) */
380  config->frame.stopBit = IfxAsclin_StopBit_1; /* one stop bit */
381  config->frame.shiftDir = IfxAsclin_ShiftDirection_lsbFirst; /* shift diection LSB first */
383  /* Default Values for Data Control */
384  config->data.dataLength = IfxAsclin_DataLength_8; /* number of bytes of the response */
385  config->data.headerOnly = IfxAsclin_HeaderResponseSelect_headerAndResponse; /* header and response */
387  config->data.checksum = IfxAsclin_Checksum_enhanced; /* enhanced checksum */
388  config->data.responseTimeout = 255;
389  /* Default Values for fifo Control */
390  config->fifo.outWidth = IfxAsclin_RxFifoOutletWidth_1; /* 8-bit wide read */
391  config->fifo.inWidth = IfxAsclin_TxFifoInletWidth_1; /* 8-bit wide write */
392  config->fifo.buffMode = IfxAsclin_ReceiveBufferMode_rxFifo; /* RxFIFO */
393  /* Default Values for Lin Control */
394  config->lin.csEnable = TRUE; /* checksum enable */
395  config->lin.csi = IfxAsclin_ChecksumInjection_written; /* 8-bit wide write */
396 
397  /* configuration for master mode */
398  {
399  config->receiveIdEnable = FALSE; /* receive Id in Rx Fifo after sending */
400  config->frame.idleDelay = IfxAsclin_IdleDelay_2; /* idle dalay 2 bits */
401  config->frame.leadDelay = IfxAsclin_LeadDelay_1; /*lead delay in master 1 bit */
402  /* Default Values for Lin Control */
403  config->lin.breakLength = 13; /* break generation length 13 */
404  config->lin.headerTimeout = 56; /* header timeout value in master 56 */
405  }
406 
407  config->pins = NULL_PTR; /* pins to null pointer*/
408 }
409 
410 
412 {
413  Ifx_ASCLIN *asclinSFR = asclin->asclin; /* getting the pointer to ASCLIN registers from module handler*/
414  IfxAsclin_clearAllFlags(asclinSFR); /* clearing all flags*/
415  IfxAsclin_flushRxFifo(asclinSFR); /* flushing Rx FIFO*/
416  IfxAsclin_enableTxFifoOutlet(asclinSFR, FALSE); /* enabling Tx FIFO for transmitting*/
417  IfxAsclin_enableRxFifoInlet(asclinSFR, TRUE); /* enabling Rx FIFO for receiving*/
418  IfxAsclin_Lin_waitForReceivedHeader(asclin); /* waiting until receive header end acknowledgemnet*/
419  IfxAsclin_read8(asclinSFR, id, 1U); /*reading the ID byte*/
420 }
421 
422 
424 {
425  Ifx_ASCLIN *asclinSFR = asclin->asclin; /* getting the pointer to ASCLIN registers from module handler*/
426  IfxAsclin_setDataLength(asclinSFR, length - 1); /* set number of bytes to be received*/
427  IfxAsclin_clearAllFlags(asclinSFR); /* clear all flags */
428  IfxAsclin_flushRxFifo(asclinSFR); /* flushing Rx FIFO */
429  IfxAsclin_enableTxFifoOutlet(asclinSFR, FALSE); /* enable Tx FIFO for transmitting */
430  IfxAsclin_enableRxFifoInlet(asclinSFR, TRUE); /* enable Rx FIFO for receiving */
431  IfxAsclin_Lin_waitForReceivedResponse(asclin); /* wait until receive response end acknowledgemnet or an error is occured*/
432  IfxAsclin_read8(asclinSFR, data, length); /* read the data bytes; */
433 }
434 
435 
437 {
438  Ifx_ASCLIN *asclinSFR = asclin->asclin; /* getting the pointer to ASCLIN registers from module handler*/
439  IfxAsclin_clearAllFlags(asclinSFR); /* clearing all flags*/
440 
441  if (asclin->receiveIdEnable) /* if receiving the sent id byte into its own Rx FIFO is selected*/
442  {
443  IfxAsclin_flushRxFifo(asclinSFR); /* flushing Rx FIFO*/
444  IfxAsclin_enableRxFifoInlet(asclinSFR, TRUE); /* enabling Rx FIFO */
445  }
446  else
447  {
448  IfxAsclin_enableRxFifoInlet(asclinSFR, FALSE); /* disabling Rx FIFO */
449  }
450 
451  IfxAsclin_flushTxFifo(asclinSFR); /* flushing Tx FIFO */
452  IfxAsclin_enableTxFifoOutlet(asclinSFR, TRUE); /* enabling Tx FIFO for transmitting*/
453  IfxAsclin_write8(asclinSFR, id, 1U); /* writing the ID byte */
454  IfxAsclin_setTransmitHeaderRequestFlag(asclinSFR); /* setting THRQS flag */
455  IfxAsclin_Lin_waitForTransmittedHeader(asclin); /* waiting until transmit header end acknowledgemnet */
456 }
457 
458 
460 {
461  Ifx_ASCLIN *asclinSFR = asclin->asclin; /* getting the pointer to ASCLIN registers from module handler*/
462  IfxAsclin_setDataLength(asclinSFR, length - 1); /* set number of bytes to be transfered */
463  IfxAsclin_clearAllFlags(asclinSFR); /* clear all flags */
464  IfxAsclin_flushTxFifo(asclinSFR); /* flushing Tx FIFO */
465  IfxAsclin_enableRxFifoInlet(asclinSFR, FALSE); /* disable Rx FIFO */
466  IfxAsclin_enableTxFifoOutlet(asclinSFR, TRUE); /* enable Tx FIFO for transmitting */
467  IfxAsclin_write8(asclinSFR, data, length); /* write the data bytes; */
468  IfxAsclin_setTransmitResponseRequestFlag(asclinSFR); /* set TRRQS flag */
469  IfxAsclin_Lin_waitForTransmittedResponse(asclin); /* wait until transmit response end acknowledgemnet */
470 }
471 
472 
474 {
475  IfxAsclin_Lin_clearFlagsStatus(asclin); /* clear all aknowledgement and error flags status */
476 
477  /* wait until receive header end acknowledgemnet has been detected */
478  while (asclin->acknowledgmentFlags.rxHeaderEnd != 1)
479  {
481  }
482 }
483 
484 
486 {
487  IfxAsclin_Lin_clearFlagsStatus(asclin); /* clear all aknowledgement and error flags status */
488 
489  /* wait until receive response end acknowledgemnet has been detected */
490  while (asclin->acknowledgmentFlags.rxResponseEnd != 1)
491  {
493  }
494 }
495 
496 
498 {
499  IfxAsclin_Lin_clearFlagsStatus(asclin); /* clear all aknowledgement and error flags status */
500 
501  /* wait until transmit header end acknowledgemnet has been detected */
502  while (asclin->acknowledgmentFlags.txHeaderEnd != 1)
503  {
505  }
506 }
507 
508 
510 {
511  IfxAsclin_Lin_clearFlagsStatus(asclin); /* clear all aknowledgement and error flags status */
512 
513  /* wait until transmit response end acknowledgemnet has been detected */
514  while (asclin->acknowledgmentFlags.txResponseEnd != 1)
515  {
517  }
518 }