Modified version of the mbed library for use with the Nucleo boards.

Dependents:   EEPROMWrite Full-Project

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Tue Nov 20 17:24:08 2012 +0000
Revision:
0:fd0d7bdfcdc2
Child:
2:143cac498751
mbed sources

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:fd0d7bdfcdc2 1 /* mbed Microcontroller Library
mbed_official 0:fd0d7bdfcdc2 2 * Copyright (c) 2006-2012 ARM Limited
mbed_official 0:fd0d7bdfcdc2 3 *
mbed_official 0:fd0d7bdfcdc2 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
mbed_official 0:fd0d7bdfcdc2 5 * of this software and associated documentation files (the "Software"), to deal
mbed_official 0:fd0d7bdfcdc2 6 * in the Software without restriction, including without limitation the rights
mbed_official 0:fd0d7bdfcdc2 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
mbed_official 0:fd0d7bdfcdc2 8 * copies of the Software, and to permit persons to whom the Software is
mbed_official 0:fd0d7bdfcdc2 9 * furnished to do so, subject to the following conditions:
mbed_official 0:fd0d7bdfcdc2 10 *
mbed_official 0:fd0d7bdfcdc2 11 * The above copyright notice and this permission notice shall be included in
mbed_official 0:fd0d7bdfcdc2 12 * all copies or substantial portions of the Software.
mbed_official 0:fd0d7bdfcdc2 13 *
mbed_official 0:fd0d7bdfcdc2 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
mbed_official 0:fd0d7bdfcdc2 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
mbed_official 0:fd0d7bdfcdc2 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
mbed_official 0:fd0d7bdfcdc2 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
mbed_official 0:fd0d7bdfcdc2 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mbed_official 0:fd0d7bdfcdc2 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
mbed_official 0:fd0d7bdfcdc2 20 * SOFTWARE.
mbed_official 0:fd0d7bdfcdc2 21 */
mbed_official 0:fd0d7bdfcdc2 22 #include "CAN.h"
mbed_official 0:fd0d7bdfcdc2 23
mbed_official 0:fd0d7bdfcdc2 24 #if DEVICE_CAN
mbed_official 0:fd0d7bdfcdc2 25
mbed_official 0:fd0d7bdfcdc2 26 #include "cmsis.h"
mbed_official 0:fd0d7bdfcdc2 27
mbed_official 0:fd0d7bdfcdc2 28 namespace mbed {
mbed_official 0:fd0d7bdfcdc2 29
mbed_official 0:fd0d7bdfcdc2 30 CAN::CAN(PinName rd, PinName td) {
mbed_official 0:fd0d7bdfcdc2 31 can_init(&_can, rd, td);
mbed_official 0:fd0d7bdfcdc2 32 }
mbed_official 0:fd0d7bdfcdc2 33
mbed_official 0:fd0d7bdfcdc2 34 CAN::~CAN() {
mbed_official 0:fd0d7bdfcdc2 35 can_free(&_can);
mbed_official 0:fd0d7bdfcdc2 36 }
mbed_official 0:fd0d7bdfcdc2 37
mbed_official 0:fd0d7bdfcdc2 38 int CAN::frequency(int f) {
mbed_official 0:fd0d7bdfcdc2 39 return can_frequency(&_can, f);
mbed_official 0:fd0d7bdfcdc2 40 }
mbed_official 0:fd0d7bdfcdc2 41
mbed_official 0:fd0d7bdfcdc2 42 int CAN::write(CANMessage msg) {
mbed_official 0:fd0d7bdfcdc2 43 return can_write(&_can, msg, 0);
mbed_official 0:fd0d7bdfcdc2 44 }
mbed_official 0:fd0d7bdfcdc2 45
mbed_official 0:fd0d7bdfcdc2 46 int CAN::read(CANMessage &msg) {
mbed_official 0:fd0d7bdfcdc2 47 return can_read(&_can, &msg);
mbed_official 0:fd0d7bdfcdc2 48 }
mbed_official 0:fd0d7bdfcdc2 49
mbed_official 0:fd0d7bdfcdc2 50 void CAN::reset() {
mbed_official 0:fd0d7bdfcdc2 51 can_reset(&_can);
mbed_official 0:fd0d7bdfcdc2 52 }
mbed_official 0:fd0d7bdfcdc2 53
mbed_official 0:fd0d7bdfcdc2 54 unsigned char CAN::rderror() {
mbed_official 0:fd0d7bdfcdc2 55 return can_rderror(&_can);
mbed_official 0:fd0d7bdfcdc2 56 }
mbed_official 0:fd0d7bdfcdc2 57
mbed_official 0:fd0d7bdfcdc2 58 unsigned char CAN::tderror() {
mbed_official 0:fd0d7bdfcdc2 59 return can_tderror(&_can);
mbed_official 0:fd0d7bdfcdc2 60 }
mbed_official 0:fd0d7bdfcdc2 61
mbed_official 0:fd0d7bdfcdc2 62 void CAN::monitor(bool silent) {
mbed_official 0:fd0d7bdfcdc2 63 can_monitor(&_can, (silent) ? 1 : 0);
mbed_official 0:fd0d7bdfcdc2 64 }
mbed_official 0:fd0d7bdfcdc2 65
mbed_official 0:fd0d7bdfcdc2 66 static FunctionPointer* can_obj[2] = { NULL };
mbed_official 0:fd0d7bdfcdc2 67
mbed_official 0:fd0d7bdfcdc2 68 // Have to check that the CAN block is active before reading the Interrupt
mbed_official 0:fd0d7bdfcdc2 69 // Control Register, or the mbed hangs
mbed_official 0:fd0d7bdfcdc2 70 void can_irq(void) {
mbed_official 0:fd0d7bdfcdc2 71 uint32_t icr;
mbed_official 0:fd0d7bdfcdc2 72
mbed_official 0:fd0d7bdfcdc2 73 if(LPC_SC->PCONP & (1 << 13)) {
mbed_official 0:fd0d7bdfcdc2 74 icr = LPC_CAN1->ICR;
mbed_official 0:fd0d7bdfcdc2 75
mbed_official 0:fd0d7bdfcdc2 76 if(icr && (can_obj[0] != NULL)) {
mbed_official 0:fd0d7bdfcdc2 77 can_obj[0]->call();
mbed_official 0:fd0d7bdfcdc2 78 }
mbed_official 0:fd0d7bdfcdc2 79 }
mbed_official 0:fd0d7bdfcdc2 80
mbed_official 0:fd0d7bdfcdc2 81 if(LPC_SC->PCONP & (1 << 14)) {
mbed_official 0:fd0d7bdfcdc2 82 icr = LPC_CAN2->ICR;
mbed_official 0:fd0d7bdfcdc2 83 if(icr && (can_obj[1] != NULL)) {
mbed_official 0:fd0d7bdfcdc2 84 can_obj[1]->call();
mbed_official 0:fd0d7bdfcdc2 85 }
mbed_official 0:fd0d7bdfcdc2 86 }
mbed_official 0:fd0d7bdfcdc2 87
mbed_official 0:fd0d7bdfcdc2 88 }
mbed_official 0:fd0d7bdfcdc2 89
mbed_official 0:fd0d7bdfcdc2 90 void CAN::setup_interrupt(void) {
mbed_official 0:fd0d7bdfcdc2 91 switch ((int)_can.dev) {
mbed_official 0:fd0d7bdfcdc2 92 case CAN_1: can_obj[0] = &_rxirq; break;
mbed_official 0:fd0d7bdfcdc2 93 case CAN_2: can_obj[1] = &_rxirq; break;
mbed_official 0:fd0d7bdfcdc2 94 }
mbed_official 0:fd0d7bdfcdc2 95 _can.dev->MOD |= 1;
mbed_official 0:fd0d7bdfcdc2 96 _can.dev->IER |= 1;
mbed_official 0:fd0d7bdfcdc2 97 _can.dev->MOD &= ~1;
mbed_official 0:fd0d7bdfcdc2 98 NVIC_SetVector(CAN_IRQn, (uint32_t) &can_irq);
mbed_official 0:fd0d7bdfcdc2 99 NVIC_EnableIRQ(CAN_IRQn);
mbed_official 0:fd0d7bdfcdc2 100 }
mbed_official 0:fd0d7bdfcdc2 101
mbed_official 0:fd0d7bdfcdc2 102 void CAN::remove_interrupt(void) {
mbed_official 0:fd0d7bdfcdc2 103 switch ((int)_can.dev) {
mbed_official 0:fd0d7bdfcdc2 104 case CAN_1: can_obj[0] = NULL; break;
mbed_official 0:fd0d7bdfcdc2 105 case CAN_2: can_obj[1] = NULL; break;
mbed_official 0:fd0d7bdfcdc2 106 }
mbed_official 0:fd0d7bdfcdc2 107
mbed_official 0:fd0d7bdfcdc2 108 _can.dev->IER &= ~(1);
mbed_official 0:fd0d7bdfcdc2 109 if ((can_obj[0] == NULL) && (can_obj[1] == NULL)) {
mbed_official 0:fd0d7bdfcdc2 110 NVIC_DisableIRQ(CAN_IRQn);
mbed_official 0:fd0d7bdfcdc2 111 }
mbed_official 0:fd0d7bdfcdc2 112 }
mbed_official 0:fd0d7bdfcdc2 113
mbed_official 0:fd0d7bdfcdc2 114 void CAN::attach(void (*fptr)(void)) {
mbed_official 0:fd0d7bdfcdc2 115 if (fptr != NULL) {
mbed_official 0:fd0d7bdfcdc2 116 _rxirq.attach(fptr);
mbed_official 0:fd0d7bdfcdc2 117 setup_interrupt();
mbed_official 0:fd0d7bdfcdc2 118 } else {
mbed_official 0:fd0d7bdfcdc2 119 remove_interrupt();
mbed_official 0:fd0d7bdfcdc2 120 }
mbed_official 0:fd0d7bdfcdc2 121 }
mbed_official 0:fd0d7bdfcdc2 122
mbed_official 0:fd0d7bdfcdc2 123 } // namespace mbed
mbed_official 0:fd0d7bdfcdc2 124
mbed_official 0:fd0d7bdfcdc2 125 #endif