mbed library sources

Fork of mbed-src by mbed official

Committer:
emilmont
Date:
Mon Feb 18 11:44:18 2013 +0000
Revision:
2:143cac498751
Parent:
0:fd0d7bdfcdc2
Update mbed sources to Rev 59

Who changed what in which revision?

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