LPCOpen Platform  v1.03
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ccan_rom.c
Go to the documentation of this file.
1 /*
2  * @brief CCAN on-chip driver example
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 #include "board.h"
32 
60 /*****************************************************************************
61  * Private types/enumerations/variables
62  ****************************************************************************/
63 
64 #define TEST_CCAN_BAUD_RATE 500000
65 
67 
68 /*****************************************************************************
69  * Public types/enumerations/variables
70  ****************************************************************************/
71 
72 /*****************************************************************************
73  * Private functions
74  ****************************************************************************/
75 void baudrateCalculate(uint32_t baud_rate, uint32_t *can_api_timing_cfg)
76 {
77  uint32_t pClk, div, quanta, segs, seg1, seg2, clk_per_bit, can_sjw;
78  Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_CAN);
80 
81  clk_per_bit = pClk / baud_rate;
82 
83  for (div = 0; div <= 15; div++) {
84  for (quanta = 1; quanta <= 32; quanta++) {
85  for (segs = 3; segs <= 17; segs++) {
86  if (clk_per_bit == (segs * quanta * (div + 1))) {
87  segs -= 3;
88  seg1 = segs / 2;
89  seg2 = segs - seg1;
90  can_sjw = seg1 > 3 ? 3 : seg1;
91  can_api_timing_cfg[0] = div;
92  can_api_timing_cfg[1] =
93  ((quanta - 1) & 0x3F) | (can_sjw & 0x03) << 6 | (seg1 & 0x0F) << 8 | (seg2 & 0x07) << 12;
94  return;
95  }
96  }
97  }
98  }
99 }
100 
101 /* CAN receive callback */
102 /* Function is executed by the Callback handler after
103  a CAN message has been received */
104 void CAN_rx(uint8_t msg_obj_num) {
105  /* Determine which CAN message has been received */
106  msg_obj.msgobj = msg_obj_num;
107  /* Now load up the msg_obj structure with the CAN message */
108  LPC_CCAN_API->can_receive(&msg_obj);
109  if (msg_obj_num == 1) {
110  /* Simply transmit CAN frame (echo) with with ID +0x100 via buffer 2 */
111  msg_obj.msgobj = 2;
112  msg_obj.mode_id += 0x100;
113  LPC_CCAN_API->can_transmit(&msg_obj);
114  }
115 }
116 
117 /* CAN transmit callback */
118 /* Function is executed by the Callback handler after
119  a CAN message has been transmitted */
120 void CAN_tx(uint8_t msg_obj_num) {}
121 
122 /* CAN error callback */
123 /* Function is executed by the Callback handler after
124  an error has occured on the CAN bus */
125 void CAN_error(uint32_t error_info) {}
126 
133 void CAN_IRQHandler(void) {
134  LPC_CCAN_API->isr();
135 }
136 
137 /*****************************************************************************
138  * Public functions
139  ****************************************************************************/
140 
145 int main(void)
146 {
147  uint32_t CanApiClkInitTable[2];
148  /* Publish CAN Callback Functions */
149  CCAN_CALLBACKS_T callbacks = {
150  CAN_rx,
151  CAN_tx,
152  CAN_error,
153  NULL,
154  NULL,
155  NULL,
156  NULL,
157  NULL,
158  };
159  Board_Init();
160  baudrateCalculate(TEST_CCAN_BAUD_RATE, CanApiClkInitTable);
161 
162  LPC_CCAN_API->init_can(&CanApiClkInitTable[0], TRUE);
163  /* Configure the CAN callback functions */
164  LPC_CCAN_API->config_calb(&callbacks);
165  /* Enable the CAN Interrupt */
166  NVIC_EnableIRQ(CAN_IRQn);
167 
168  /* Send a simple one time CAN message */
169  msg_obj.msgobj = 0;
170  msg_obj.mode_id = 0x345;
171  msg_obj.mask = 0x0;
172  msg_obj.dlc = 4;
173  msg_obj.data[0] = 'T'; // 0x54
174  msg_obj.data[1] = 'E'; // 0x45
175  msg_obj.data[2] = 'S'; // 0x53
176  msg_obj.data[3] = 'T'; // 0x54
177  LPC_CCAN_API->can_transmit(&msg_obj);
178 
179  /* Configure message object 1 to receive all 11-bit messages 0x400-0x4FF */
180  msg_obj.msgobj = 1;
181  msg_obj.mode_id = 0x400;
182  msg_obj.mask = 0x700;
183  LPC_CCAN_API->config_rxmsgobj(&msg_obj);
184 
185  while (1) {
186  __WFI(); /* Go to Sleep */
187  }
188 }
189