| LPCOpen Platform v1.03 LPCOpen Platform for NXP LPC Microcontrollers

- Main Page
- Modules
- Data Structures
- Files
- Related Pages

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
can_17xx_40xx.c
Go to the documentation of this file.
1 /*
2 * @brief LPC17xx/40xx CAN driver
3 *
4 * @note
5 * Copyright(C) NXP Semiconductors, 2012
6 * All rights reserved.
7 *
8 * @par
9 * Software that is described herein is for illustrative purposes only
10 * which provides customers with programming information regarding the
11 * LPC products. This software is supplied "AS IS" without any warranties of
12 * any kind, and NXP Semiconductors and its licensor disclaim any and
13 * all warranties, express or implied, including all implied warranties of
14 * merchantability, fitness for a particular purpose and non-infringement of
15 * intellectual property rights. NXP Semiconductors assumes no responsibility
16 * or liability for the use of the software, conveys no license or rights under any
17 * patent, copyright, mask work right, or any other intellectual property rights in
18 * or to any products. NXP Semiconductors reserves the right to make changes
19 * in the software without notification. NXP Semiconductors also makes no
20 * representation or warranty that such application will be suitable for the
21 * specified use without further testing or modification.
22 *
23 * @par
24 * Permission to use, copy, modify, and distribute this software and its
25 * documentation is hereby granted, under NXP Semiconductors' and its
26 * licensor's relevant copyrights in the software, without fee, provided that it
27 * is used in conjunction with NXP Semiconductors microcontrollers. This
28 * copyright, permission, and disclaimer notice must appear in all copies of
29 * this code.
30 */
31
32 #include "chip.h"
33
34 /*****************************************************************************
35 * Private types/enumerations/variables
36 ****************************************************************************/
37
38 /*****************************************************************************
39 * Public types/enumerations/variables
40 ****************************************************************************/
41
42 /*****************************************************************************
43 * Private functions
44 ****************************************************************************/
45
46 /* Determine clock for CAN Peripheral */
47 static CHIP_SYSCTL_CLOCK_T Chip_CAN_DetermineClk(LPC_CAN_T *pCAN) {
48
49 CHIP_SYSCTL_CLOCK_T CanClk;
50
51 if (pCAN == LPC_CAN1) {
52 CanClk = SYSCTL_CLOCK_CAN1;
53 }
54 else {
55 CanClk = SYSCTL_CLOCK_CAN2;
56 }
57
58 return CanClk;
59 }
60
61 /* Get CAN Clock Rate */
62 static uint32_t Chip_CAN_GetClockRate(LPC_CAN_T *pCAN) {
63 #if !defined(CHIP_LPC175X_6X)
64 return Chip_Clock_GetPeripheralClockRate();
65 #else
66 if (pCAN == LPC_CAN1) {
67 return Chip_Clock_GetPeripheralClockRate(SYSCTL_PCLK_CAN1);
68 }
69 else {
70 return Chip_Clock_GetPeripheralClockRate(SYSCTL_PCLK_CAN2);
71 }
72 #endif
73 }
74
75 #if !defined(CHIP_LPC175X_6X)
76 /* Determine CAN peripheral for reset*/
77 static CHIP_SYSCTL_RESET_T Chip_CAN_DetermineReset(LPC_CAN_T *pCAN) {
78
79 CHIP_SYSCTL_RESET_T CanRst;
80
81 if (pCAN == LPC_CAN1) {
82 CanRst = SYSCTL_RESET_CAN1;
83 }
84 else {
85 CanRst = SYSCTL_RESET_CAN2;
86 }
87
88 return CanRst;
89 }
90
91 #endif /*#if !defined(CHIP_LPC175X_6X)*/
92
93 /*****************************************************************************
94 * Public functions
95 ****************************************************************************/
96
97 /* Initialize CAN Interface */
98 void Chip_CAN_Init(LPC_CAN_T *pCAN)
99 {
100 /* Enable CAN Clock */
101 Chip_Clock_EnablePeriphClock(Chip_CAN_DetermineClk(pCAN));
102 #if !defined(CHIP_LPC175X_6X)
103 Chip_SYSCTL_PeriphReset(Chip_CAN_DetermineReset(pCAN));
104 #endif
105
106 /* Initialize CAN Peripheral */
107 IP_CAN_Init(pCAN);
108
109 /* Initiialize Acceptance filter */
110 IP_CAN_AF_Init(LPC_CANAF, LPC_CANAF_RAM);
111 }
112
113 /* De-Initialize CAN Interface */
114 void Chip_CAN_DeInit(LPC_CAN_T *pCAN)
115 {
116 IP_CAN_DeInit(pCAN);
117 Chip_Clock_DisablePeriphClock(Chip_CAN_DetermineClk(pCAN));
118 }
119
120 /* Set CAN Bit Rate */
121 Status Chip_CAN_SetBitRate(LPC_CAN_T *pCAN, uint32_t BitRate)
122 {
123 IP_CAN_BUS_TIMING_T BusTiming;
124 uint32_t result = 0;
125 uint8_t NT, TSEG1 = 0, TSEG2 = 0;
126 uint32_t CANPclk = 0;
127 uint32_t BRP = 0;
128
129 CANPclk = Chip_CAN_GetClockRate(pCAN);
130 result = CANPclk / BitRate;
131
132 /* Calculate suitable nominal time value
133 * NT (nominal time) = (TSEG1 + TSEG2 + 3)
134 * NT <= 24
135 * TSEG1 >= 2*TSEG2
136 */
137 for (NT = 24; NT > 0; NT = NT - 2) {
138 if ((result % NT) == 0) {
139 BRP = result / NT - 1;
140
141 NT--;
142
143 TSEG2 = (NT / 3) - 1;
144
145 TSEG1 = NT - (NT / 3) - 1;
146
147 break;
148 }
149 }
150 if (NT == 0) {
151 return ERROR;
152 }
153
154 BusTiming.TESG1 = TSEG1;
155 BusTiming.TESG2 = TSEG2;
156 BusTiming.BRP = BRP;
157 BusTiming.SJW = 3;
158 BusTiming.SAM = 0;
159 IP_CAN_SetBusTiming(pCAN, &BusTiming);
160 return SUCCESS;
161 }
162
163 /* Get Free TxBuf */
164 CAN_BUFFER_ID Chip_CAN_GetFreeTxBuf(LPC_CAN_T *pCAN)
165 {
166 IP_CAN_BUFFER_ID_T TxBufID = CAN_BUFFER_1;
167
168 /* Select a free buffer */
169 for (TxBufID = (IP_CAN_BUFFER_ID_T) 0; TxBufID < CAN_BUFFER_LAST; TxBufID++) {
170 if (IP_CAN_GetStatus(pCAN) & CAN_SR_TBS(TxBufID)) {
171 break;
172 }
173 }
174
175 return TxBufID;
176 }