| 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
ccan.c
Go to the documentation of this file.
1 /*
2 * @brief CCAN example
3 * This example show how to the CCAN in 3 mode : Polling, Interrupt and DMA
4 *
5 * @note
6 * Copyright(C) NXP Semiconductors, 2012
7 * All rights reserved.
8 *
9 * @par
10 * Software that is described herein is for illustrative purposes only
11 * which provides customers with programming information regarding the
12 * LPC products. This software is supplied "AS IS" without any warranties of
13 * any kind, and NXP Semiconductors and its licensor disclaim any and
14 * all warranties, express or implied, including all implied warranties of
15 * merchantability, fitness for a particular purpose and non-infringement of
16 * intellectual property rights. NXP Semiconductors assumes no responsibility
17 * or liability for the use of the software, conveys no license or rights under any
18 * patent, copyright, mask work right, or any other intellectual property rights in
19 * or to any products. NXP Semiconductors reserves the right to make changes
20 * in the software without notification. NXP Semiconductors also makes no
21 * representation or warranty that such application will be suitable for the
22 * specified use without further testing or modification.
23 *
24 * @par
25 * Permission to use, copy, modify, and distribute this software and its
26 * documentation is hereby granted, under NXP Semiconductors' and its
27 * licensor's relevant copyrights in the software, without fee, provided that it
28 * is used in conjunction with NXP Semiconductors microcontrollers. This
29 * copyright, permission, and disclaimer notice must appear in all copies of
30 * this code.
31 */
32
33 #define CCAN_TX_MSG_ID (0x200)
34 #define CCAN_RX_MSG_ID (0x100)
35 #define CCAN_TX_MSG_REMOTE_ID (0x300)
36
37 // FIXME - needs formatting to correct standards
38
39 #include "board.h"
40 static char WelcomeMenu[] = "\n\rHello NXP Semiconductors \r\r"
41 "CCAN DEMO : Use C_CAN to transmit and receive Message from CAN Analyzer\r\n"
42 "CCAN bit rate : 500kBit/s\r\n";
43
44 uint8_t msg_received_counter = 0;
45
46 void CAN0_IRQHandler(void)
47 {
48 message_object msg_buf;
49 uint32_t can_int, can_stat, i;
50 while ( (can_int = Chip_CCAN_GetIntID(LPC_C_CAN0)) != 0 ) {
51 if (can_int == CCAN_STATUS_INT) {
52 can_stat = Chip_CCAN_GetStatus(LPC_C_CAN0);
53 // TODO with error or TXOK, RXOK
54 if (can_stat & CCAN_STAT_EPASS) {
55 DEBUGOUT("Passive error\r\n");
56 return;
57 }
58 if (can_stat & CCAN_STAT_EWARN) {
59 DEBUGOUT("Warning!!!\r\n");
60 return;
61 }
62 if (can_stat & CCAN_STAT_BOFF) {
63 DEBUGOUT("CAN bus is off\r\n");
64 return;
65 }
66 Chip_CCAN_ClearStatus(LPC_C_CAN0, CCAN_STAT_TXOK);
67 Chip_CCAN_ClearStatus(LPC_C_CAN0, CCAN_STAT_RXOK);
68 }
69 else if ((1 <= can_int) && (can_int <= 0x20)) {
70 // Process msg num canint
71 Chip_CCAN_GetMsgObject(LPC_C_CAN0, can_int, &msg_buf);
72 switch (msg_buf.id) {
73 case CCAN_RX_MSG_ID:
74 Chip_CCAN_ClearIntPend(LPC_C_CAN0, can_int, CCAN_RX_DIR);
75 DEBUGOUT("Msg ID :%x\r\n", msg_buf.id);
76 DEBUGOUT("Msg data :");
77 for (i = 0; i < msg_buf.dlc; i++)
78 DEBUGOUT("%x ", msg_buf.data[i]);
79 DEBUGOUT("\nFeed back...\r\n");
80 msg_buf.id += 1;
81 Chip_CCAN_Send(LPC_C_CAN0, 0, &msg_buf);
82 break;
83
84 case CCAN_TX_MSG_ID:
85 Chip_CCAN_ClearIntPend(LPC_C_CAN0, can_int, CCAN_TX_DIR);
86 break;
87
88 case CCAN_TX_MSG_REMOTE_ID:
89 msg_received_counter++;
90 if (msg_received_counter == 5) {
91 DEBUGOUT("Remote transmit total is 5. Delete remote ID\r\n");
92 Chip_CCAN_DeleteReceiveID(LPC_C_CAN0, CCAN_TX_MSG_REMOTE_ID);
93 }
94 break;
95
96 default:
97 break;
98 }
99 }
100 }
101 }
102
103 int main(void)
104 {
105 message_object send_obj;
106 Board_Init();
107 DEBUGOUT(WelcomeMenu);
108 /* Set CCAN peripheral clock under 100Mhz for working stable */
109 Chip_Clock_SetBaseClock(CLK_BASE_APB3, CLKIN_IDIVC, true, false);
110 Chip_CCAN_Init(LPC_C_CAN0);
111 Chip_CCAN_SetBitRate(LPC_C_CAN0, 500000);
112 Chip_CCAN_IntEnable(LPC_C_CAN0, ENABLE);
113
114 send_obj.id = CCAN_TX_MSG_ID;
115 send_obj.dlc = 4;
116 send_obj.data[0] = 'A';
117 send_obj.data[1] = 'B';
118 send_obj.data[2] = 'C';
119 send_obj.data[3] = 'D';
120 Chip_CCAN_Send(LPC_C_CAN0, 0, &send_obj);
121
122 send_obj.id = CCAN_TX_MSG_REMOTE_ID;
123 send_obj.data[0] = 'E';
124 send_obj.data[1] = 'F';
125 send_obj.data[2] = 'G';
126 send_obj.data[3] = 'H';
127 Chip_CCAN_Send(LPC_C_CAN0, 1, &send_obj);
128
129 Chip_CCAN_AddReceiveID(LPC_C_CAN0, CCAN_RX_MSG_ID);
130
131 NVIC_EnableIRQ(C_CAN0_IRQn);
132
133 while (1);
134 }