Fork of mbed-dev with the NUCLEO-L152RE target modified for use with the STM32L151RB (128 kB flash, 16 kB RAM).
Fork of mbed-dev by
To use this, remove the default "mbed" library and import this one instead. Target must be NUCLEO_L152RE.
common/CAN.cpp@28:a132a70c6c08, 2016-02-11 (annotated)
- Committer:
- Jim Paris
- Date:
- Thu Feb 11 15:22:33 2016 -0500
- Revision:
- 28:a132a70c6c08
- Parent:
- 0:9b334a45a8ff
Merge to get rid of old heads
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 0:9b334a45a8ff | 1 | /* mbed Microcontroller Library |
bogdanm | 0:9b334a45a8ff | 2 | * Copyright (c) 2006-2013 ARM Limited |
bogdanm | 0:9b334a45a8ff | 3 | * |
bogdanm | 0:9b334a45a8ff | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
bogdanm | 0:9b334a45a8ff | 5 | * you may not use this file except in compliance with the License. |
bogdanm | 0:9b334a45a8ff | 6 | * You may obtain a copy of the License at |
bogdanm | 0:9b334a45a8ff | 7 | * |
bogdanm | 0:9b334a45a8ff | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bogdanm | 0:9b334a45a8ff | 9 | * |
bogdanm | 0:9b334a45a8ff | 10 | * Unless required by applicable law or agreed to in writing, software |
bogdanm | 0:9b334a45a8ff | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
bogdanm | 0:9b334a45a8ff | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
bogdanm | 0:9b334a45a8ff | 13 | * See the License for the specific language governing permissions and |
bogdanm | 0:9b334a45a8ff | 14 | * limitations under the License. |
bogdanm | 0:9b334a45a8ff | 15 | */ |
bogdanm | 0:9b334a45a8ff | 16 | #include "CAN.h" |
bogdanm | 0:9b334a45a8ff | 17 | |
bogdanm | 0:9b334a45a8ff | 18 | #if DEVICE_CAN |
bogdanm | 0:9b334a45a8ff | 19 | |
bogdanm | 0:9b334a45a8ff | 20 | #include "cmsis.h" |
bogdanm | 0:9b334a45a8ff | 21 | |
bogdanm | 0:9b334a45a8ff | 22 | namespace mbed { |
bogdanm | 0:9b334a45a8ff | 23 | |
bogdanm | 0:9b334a45a8ff | 24 | CAN::CAN(PinName rd, PinName td) : _can(), _irq() { |
bogdanm | 0:9b334a45a8ff | 25 | can_init(&_can, rd, td); |
bogdanm | 0:9b334a45a8ff | 26 | can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this); |
bogdanm | 0:9b334a45a8ff | 27 | } |
bogdanm | 0:9b334a45a8ff | 28 | |
bogdanm | 0:9b334a45a8ff | 29 | CAN::~CAN() { |
bogdanm | 0:9b334a45a8ff | 30 | can_irq_free(&_can); |
bogdanm | 0:9b334a45a8ff | 31 | can_free(&_can); |
bogdanm | 0:9b334a45a8ff | 32 | } |
bogdanm | 0:9b334a45a8ff | 33 | |
bogdanm | 0:9b334a45a8ff | 34 | int CAN::frequency(int f) { |
bogdanm | 0:9b334a45a8ff | 35 | return can_frequency(&_can, f); |
bogdanm | 0:9b334a45a8ff | 36 | } |
bogdanm | 0:9b334a45a8ff | 37 | |
bogdanm | 0:9b334a45a8ff | 38 | int CAN::write(CANMessage msg) { |
bogdanm | 0:9b334a45a8ff | 39 | return can_write(&_can, msg, 0); |
bogdanm | 0:9b334a45a8ff | 40 | } |
bogdanm | 0:9b334a45a8ff | 41 | |
bogdanm | 0:9b334a45a8ff | 42 | int CAN::read(CANMessage &msg, int handle) { |
bogdanm | 0:9b334a45a8ff | 43 | return can_read(&_can, &msg, handle); |
bogdanm | 0:9b334a45a8ff | 44 | } |
bogdanm | 0:9b334a45a8ff | 45 | |
bogdanm | 0:9b334a45a8ff | 46 | void CAN::reset() { |
bogdanm | 0:9b334a45a8ff | 47 | can_reset(&_can); |
bogdanm | 0:9b334a45a8ff | 48 | } |
bogdanm | 0:9b334a45a8ff | 49 | |
bogdanm | 0:9b334a45a8ff | 50 | unsigned char CAN::rderror() { |
bogdanm | 0:9b334a45a8ff | 51 | return can_rderror(&_can); |
bogdanm | 0:9b334a45a8ff | 52 | } |
bogdanm | 0:9b334a45a8ff | 53 | |
bogdanm | 0:9b334a45a8ff | 54 | unsigned char CAN::tderror() { |
bogdanm | 0:9b334a45a8ff | 55 | return can_tderror(&_can); |
bogdanm | 0:9b334a45a8ff | 56 | } |
bogdanm | 0:9b334a45a8ff | 57 | |
bogdanm | 0:9b334a45a8ff | 58 | void CAN::monitor(bool silent) { |
bogdanm | 0:9b334a45a8ff | 59 | can_monitor(&_can, (silent) ? 1 : 0); |
bogdanm | 0:9b334a45a8ff | 60 | } |
bogdanm | 0:9b334a45a8ff | 61 | |
bogdanm | 0:9b334a45a8ff | 62 | int CAN::mode(Mode mode) { |
bogdanm | 0:9b334a45a8ff | 63 | return can_mode(&_can, (CanMode)mode); |
bogdanm | 0:9b334a45a8ff | 64 | } |
bogdanm | 0:9b334a45a8ff | 65 | |
bogdanm | 0:9b334a45a8ff | 66 | int CAN::filter(unsigned int id, unsigned int mask, CANFormat format, int handle) { |
bogdanm | 0:9b334a45a8ff | 67 | return can_filter(&_can, id, mask, format, handle); |
bogdanm | 0:9b334a45a8ff | 68 | } |
bogdanm | 0:9b334a45a8ff | 69 | |
bogdanm | 0:9b334a45a8ff | 70 | void CAN::attach(void (*fptr)(void), IrqType type) { |
bogdanm | 0:9b334a45a8ff | 71 | if (fptr) { |
bogdanm | 0:9b334a45a8ff | 72 | _irq[(CanIrqType)type].attach(fptr); |
bogdanm | 0:9b334a45a8ff | 73 | can_irq_set(&_can, (CanIrqType)type, 1); |
bogdanm | 0:9b334a45a8ff | 74 | } else { |
bogdanm | 0:9b334a45a8ff | 75 | can_irq_set(&_can, (CanIrqType)type, 0); |
bogdanm | 0:9b334a45a8ff | 76 | } |
bogdanm | 0:9b334a45a8ff | 77 | } |
bogdanm | 0:9b334a45a8ff | 78 | |
bogdanm | 0:9b334a45a8ff | 79 | void CAN::_irq_handler(uint32_t id, CanIrqType type) { |
bogdanm | 0:9b334a45a8ff | 80 | CAN *handler = (CAN*)id; |
bogdanm | 0:9b334a45a8ff | 81 | handler->_irq[type].call(); |
bogdanm | 0:9b334a45a8ff | 82 | } |
bogdanm | 0:9b334a45a8ff | 83 | |
bogdanm | 0:9b334a45a8ff | 84 | } // namespace mbed |
bogdanm | 0:9b334a45a8ff | 85 | |
bogdanm | 0:9b334a45a8ff | 86 | #endif |