Update to new build system.
[clinton/Smoothieware.git] / mbed / src / cpp / CAN.cpp
1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "CAN.h"
17
18 #if DEVICE_CAN
19
20 #include "cmsis.h"
21
22 namespace mbed {
23
24 CAN::CAN(PinName rd, PinName td) {
25 can_init(&_can, rd, td);
26 }
27
28 CAN::~CAN() {
29 can_free(&_can);
30 }
31
32 int CAN::frequency(int f) {
33 return can_frequency(&_can, f);
34 }
35
36 int CAN::write(CANMessage msg) {
37 return can_write(&_can, msg, 0);
38 }
39
40 int CAN::read(CANMessage &msg) {
41 return can_read(&_can, &msg);
42 }
43
44 void CAN::reset() {
45 can_reset(&_can);
46 }
47
48 unsigned char CAN::rderror() {
49 return can_rderror(&_can);
50 }
51
52 unsigned char CAN::tderror() {
53 return can_tderror(&_can);
54 }
55
56 void CAN::monitor(bool silent) {
57 can_monitor(&_can, (silent) ? 1 : 0);
58 }
59
60 static FunctionPointer* can_obj[2] = { NULL };
61
62 // Have to check that the CAN block is active before reading the Interrupt
63 // Control Register, or the mbed hangs
64 void can_irq(void) {
65 uint32_t icr;
66
67 if(LPC_SC->PCONP & (1 << 13)) {
68 icr = LPC_CAN1->ICR;
69
70 if(icr && (can_obj[0] != NULL)) {
71 can_obj[0]->call();
72 }
73 }
74
75 if(LPC_SC->PCONP & (1 << 14)) {
76 icr = LPC_CAN2->ICR;
77 if(icr && (can_obj[1] != NULL)) {
78 can_obj[1]->call();
79 }
80 }
81
82 }
83
84 void CAN::setup_interrupt(void) {
85 switch ((int)_can.dev) {
86 case CAN_1: can_obj[0] = &_rxirq; break;
87 case CAN_2: can_obj[1] = &_rxirq; break;
88 }
89 _can.dev->MOD |= 1;
90 _can.dev->IER |= 1;
91 _can.dev->MOD &= ~1;
92 NVIC_SetVector(CAN_IRQn, (uint32_t) &can_irq);
93 NVIC_EnableIRQ(CAN_IRQn);
94 }
95
96 void CAN::remove_interrupt(void) {
97 switch ((int)_can.dev) {
98 case CAN_1: can_obj[0] = NULL; break;
99 case CAN_2: can_obj[1] = NULL; break;
100 }
101
102 _can.dev->IER &= ~(1);
103 if ((can_obj[0] == NULL) && (can_obj[1] == NULL)) {
104 NVIC_DisableIRQ(CAN_IRQn);
105 }
106 }
107
108 void CAN::attach(void (*fptr)(void)) {
109 if (fptr != NULL) {
110 _rxirq.attach(fptr);
111 setup_interrupt();
112 } else {
113 remove_interrupt();
114 }
115 }
116
117 } // namespace mbed
118
119 #endif