S32 SDK
lin_common.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * THIS SOFTWARE IS PROVIDED BY NXP "AS IS" AND ANY EXPRESSED OR
7  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
9  * IN NO EVENT SHALL NXP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
10  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
11  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
12  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
13  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
14  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
15  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
16  * THE POSSIBILITY OF SUCH DAMAGE.
17  */
18 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "lin_driver.h"
27 
28 /*******************************************************************************
29  * Definitions
30  ******************************************************************************/
31 
32 /*******************************************************************************
33  * Code
34  ******************************************************************************/
35 /*FUNCTION**********************************************************************
36  *
37  * Function Name : BIT
38  * Description : Return bit B in byte A
39  * This is not a public API as it is called by other API functions.
40  *
41  *END**************************************************************************/
42 static inline uint8_t BIT(uint8_t A,
43  uint8_t B)
44 {
45  return (uint8_t)((A >> B) & 0x01U);
46 }
47 
48 /*FUNCTION**********************************************************************
49  *
50  * Function Name : LIN_DRV_ProcessParity
51  * Description : Makes or checks parity bits. If action is checking parity, the function
52  * returns ID value if parity bits are correct or 0xFF if parity bits are incorrect. If action
53  * is making parity bits, then from input value of ID, the function returns PID.
54  * This is not a public API as it is called by other API functions.
55  *
56  * Implements : LIN_DRV_ProcessParity_Activity
57  *END**************************************************************************/
58 uint8_t LIN_DRV_ProcessParity(uint8_t PID,
59  uint8_t typeAction)
60 {
61  uint8_t parity;
62  uint8_t retVal;
63 
64  parity = (uint8_t)(((BIT(PID, 0U) ^ BIT(PID, 1U) ^ BIT(PID, 2U) ^ BIT(PID, 4U)) << 6U) |
65  ((0xFFU ^ (BIT(PID, 1U) ^ BIT(PID, 3U) ^ BIT(PID, 4U) ^ BIT(PID, 5U))) << 7U));
66 
67  /* Check if action is checking parity bits */
68  if (CHECK_PARITY == typeAction)
69  {
70  /* If parity bits are incorrect */
71  if ((PID & 0xC0U) != parity)
72  {
73  /* Return 0xFF if parity bits are incorrect */
74  retVal = 0xFFU;
75  }
76  /* If parity bits are correct */
77  else
78  {
79  /* Return ID if parity bits are correct */
80  retVal = (uint8_t)(PID & 0x3FU);
81  }
82  }
83  /* If action is making parity bits */
84  else
85  {
86  /* Return PID in case of making parity bits */
87  retVal = (uint8_t)(PID | parity);
88  }
89 
90  return retVal;
91 }
92 
93 /*FUNCTION**********************************************************************
94  *
95  * Function Name : LIN_DRV_MakeChecksumByte
96  * Description : Makes the checksum byte for a frame. For PID of identifiers,
97  * if PID is 0x3C (ID 0x3C) or 0x7D (ID 0x3D) or 0xFE (ID 0x3E) or 0xBF (ID 0x3F)
98  * apply classic checksum and apply enhanced checksum for other PID.
99  *
100  * Implements : LIN_DRV_MakeChecksumByte_Activity
101  *END**************************************************************************/
102 uint8_t LIN_DRV_MakeChecksumByte(const uint8_t * buffer,
103  uint8_t sizeBuffer,
104  uint8_t PID)
105 {
106  uint8_t length;
107  uint16_t checksum;
108 
109  /* For PID is 0x3C (ID 0x3C) or 0x7D (ID 0x3D) or 0xFE (ID 0x3E) or 0xBF (ID 0x3F)
110  * apply classic checksum and apply enhanced checksum for other PID */
111  if ((0x3CU != PID) && (0x7DU != PID) && (0xFEU != PID) && (0xBFU != PID))
112  {
113  /* For PID other than 0x3C, 0x7D, 0xFE and 0xBF: Add PID in checksum calculation */
114  checksum = PID;
115  }
116  else
117  {
118  /* For 0x3C, 0x7D, 0xFE and 0xBF: Do not add PID in checksum calculation */
119  checksum = 0U;
120  }
121 
122  for (length = sizeBuffer; 0U < length; length--)
123  {
124  checksum += *buffer;
125  buffer++;
126  /* Deal with the carry */
127  if (checksum > 0xFFU)
128  {
129  checksum -= 0xFFU;
130  }
131  }
132 
133  /* Return reversed checksum */
134  return (uint8_t)(~checksum);
135 }
136 
137 /*******************************************************************************
138  * EOF
139  ******************************************************************************/
uint8_t LIN_DRV_ProcessParity(uint8_t PID, uint8_t typeAction)
Makes or checks parity bits. If action is checking parity, the function returns ID value if parity bi...
Definition: lin_common.c:58
static uint8_t BIT(uint8_t A, uint8_t B)
Definition: lin_common.c:42
uint8_t LIN_DRV_MakeChecksumByte(const uint8_t *buffer, uint8_t sizeBuffer, uint8_t PID)
Makes the checksum byte for a frame.
Definition: lin_common.c:102
#define CHECK_PARITY
Definition: lin_driver.h:53