Development mbed library for MAX32630FTHR
Dependents: blinky_max32630fthr
drivers/CAN.cpp@3:1198227e6421, 2016-12-16 (annotated)
- Committer:
- switches
- Date:
- Fri Dec 16 16:27:57 2016 +0000
- Revision:
- 3:1198227e6421
- Parent:
- 0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
switches | 0:5c4d7b2438d3 | 1 | /* mbed Microcontroller Library |
switches | 0:5c4d7b2438d3 | 2 | * Copyright (c) 2006-2013 ARM Limited |
switches | 0:5c4d7b2438d3 | 3 | * |
switches | 0:5c4d7b2438d3 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
switches | 0:5c4d7b2438d3 | 5 | * you may not use this file except in compliance with the License. |
switches | 0:5c4d7b2438d3 | 6 | * You may obtain a copy of the License at |
switches | 0:5c4d7b2438d3 | 7 | * |
switches | 0:5c4d7b2438d3 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
switches | 0:5c4d7b2438d3 | 9 | * |
switches | 0:5c4d7b2438d3 | 10 | * Unless required by applicable law or agreed to in writing, software |
switches | 0:5c4d7b2438d3 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
switches | 0:5c4d7b2438d3 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
switches | 0:5c4d7b2438d3 | 13 | * See the License for the specific language governing permissions and |
switches | 0:5c4d7b2438d3 | 14 | * limitations under the License. |
switches | 0:5c4d7b2438d3 | 15 | */ |
switches | 0:5c4d7b2438d3 | 16 | #include "drivers/CAN.h" |
switches | 0:5c4d7b2438d3 | 17 | |
switches | 0:5c4d7b2438d3 | 18 | #if DEVICE_CAN |
switches | 0:5c4d7b2438d3 | 19 | |
switches | 0:5c4d7b2438d3 | 20 | #include "cmsis.h" |
switches | 0:5c4d7b2438d3 | 21 | |
switches | 0:5c4d7b2438d3 | 22 | namespace mbed { |
switches | 0:5c4d7b2438d3 | 23 | |
switches | 0:5c4d7b2438d3 | 24 | static void donothing() {} |
switches | 0:5c4d7b2438d3 | 25 | |
switches | 0:5c4d7b2438d3 | 26 | CAN::CAN(PinName rd, PinName td) : _can(), _irq() { |
switches | 0:5c4d7b2438d3 | 27 | // No lock needed in constructor |
switches | 0:5c4d7b2438d3 | 28 | |
switches | 0:5c4d7b2438d3 | 29 | for (int i = 0; i < sizeof _irq / sizeof _irq[0]; i++) { |
switches | 0:5c4d7b2438d3 | 30 | _irq[i].attach(donothing); |
switches | 0:5c4d7b2438d3 | 31 | } |
switches | 0:5c4d7b2438d3 | 32 | |
switches | 0:5c4d7b2438d3 | 33 | can_init(&_can, rd, td); |
switches | 0:5c4d7b2438d3 | 34 | can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this); |
switches | 0:5c4d7b2438d3 | 35 | } |
switches | 0:5c4d7b2438d3 | 36 | |
switches | 0:5c4d7b2438d3 | 37 | CAN::~CAN() { |
switches | 0:5c4d7b2438d3 | 38 | // No lock needed in destructor |
switches | 0:5c4d7b2438d3 | 39 | can_irq_free(&_can); |
switches | 0:5c4d7b2438d3 | 40 | can_free(&_can); |
switches | 0:5c4d7b2438d3 | 41 | } |
switches | 0:5c4d7b2438d3 | 42 | |
switches | 0:5c4d7b2438d3 | 43 | int CAN::frequency(int f) { |
switches | 0:5c4d7b2438d3 | 44 | lock(); |
switches | 0:5c4d7b2438d3 | 45 | int ret = can_frequency(&_can, f); |
switches | 0:5c4d7b2438d3 | 46 | unlock(); |
switches | 0:5c4d7b2438d3 | 47 | return ret; |
switches | 0:5c4d7b2438d3 | 48 | } |
switches | 0:5c4d7b2438d3 | 49 | |
switches | 0:5c4d7b2438d3 | 50 | int CAN::write(CANMessage msg) { |
switches | 0:5c4d7b2438d3 | 51 | lock(); |
switches | 0:5c4d7b2438d3 | 52 | int ret = can_write(&_can, msg, 0); |
switches | 0:5c4d7b2438d3 | 53 | unlock(); |
switches | 0:5c4d7b2438d3 | 54 | return ret; |
switches | 0:5c4d7b2438d3 | 55 | } |
switches | 0:5c4d7b2438d3 | 56 | |
switches | 0:5c4d7b2438d3 | 57 | int CAN::read(CANMessage &msg, int handle) { |
switches | 0:5c4d7b2438d3 | 58 | lock(); |
switches | 0:5c4d7b2438d3 | 59 | int ret = can_read(&_can, &msg, handle); |
switches | 0:5c4d7b2438d3 | 60 | unlock(); |
switches | 0:5c4d7b2438d3 | 61 | return ret; |
switches | 0:5c4d7b2438d3 | 62 | } |
switches | 0:5c4d7b2438d3 | 63 | |
switches | 0:5c4d7b2438d3 | 64 | void CAN::reset() { |
switches | 0:5c4d7b2438d3 | 65 | lock(); |
switches | 0:5c4d7b2438d3 | 66 | can_reset(&_can); |
switches | 0:5c4d7b2438d3 | 67 | unlock(); |
switches | 0:5c4d7b2438d3 | 68 | } |
switches | 0:5c4d7b2438d3 | 69 | |
switches | 0:5c4d7b2438d3 | 70 | unsigned char CAN::rderror() { |
switches | 0:5c4d7b2438d3 | 71 | lock(); |
switches | 0:5c4d7b2438d3 | 72 | int ret = can_rderror(&_can); |
switches | 0:5c4d7b2438d3 | 73 | unlock(); |
switches | 0:5c4d7b2438d3 | 74 | return ret; |
switches | 0:5c4d7b2438d3 | 75 | } |
switches | 0:5c4d7b2438d3 | 76 | |
switches | 0:5c4d7b2438d3 | 77 | unsigned char CAN::tderror() { |
switches | 0:5c4d7b2438d3 | 78 | lock(); |
switches | 0:5c4d7b2438d3 | 79 | int ret = can_tderror(&_can); |
switches | 0:5c4d7b2438d3 | 80 | unlock(); |
switches | 0:5c4d7b2438d3 | 81 | return ret; |
switches | 0:5c4d7b2438d3 | 82 | } |
switches | 0:5c4d7b2438d3 | 83 | |
switches | 0:5c4d7b2438d3 | 84 | void CAN::monitor(bool silent) { |
switches | 0:5c4d7b2438d3 | 85 | lock(); |
switches | 0:5c4d7b2438d3 | 86 | can_monitor(&_can, (silent) ? 1 : 0); |
switches | 0:5c4d7b2438d3 | 87 | unlock(); |
switches | 0:5c4d7b2438d3 | 88 | } |
switches | 0:5c4d7b2438d3 | 89 | |
switches | 0:5c4d7b2438d3 | 90 | int CAN::mode(Mode mode) { |
switches | 0:5c4d7b2438d3 | 91 | lock(); |
switches | 0:5c4d7b2438d3 | 92 | int ret = can_mode(&_can, (CanMode)mode); |
switches | 0:5c4d7b2438d3 | 93 | unlock(); |
switches | 0:5c4d7b2438d3 | 94 | return ret; |
switches | 0:5c4d7b2438d3 | 95 | } |
switches | 0:5c4d7b2438d3 | 96 | |
switches | 0:5c4d7b2438d3 | 97 | int CAN::filter(unsigned int id, unsigned int mask, CANFormat format, int handle) { |
switches | 0:5c4d7b2438d3 | 98 | lock(); |
switches | 0:5c4d7b2438d3 | 99 | int ret = can_filter(&_can, id, mask, format, handle); |
switches | 0:5c4d7b2438d3 | 100 | unlock(); |
switches | 0:5c4d7b2438d3 | 101 | return ret; |
switches | 0:5c4d7b2438d3 | 102 | } |
switches | 0:5c4d7b2438d3 | 103 | |
switches | 0:5c4d7b2438d3 | 104 | void CAN::attach(Callback<void()> func, IrqType type) { |
switches | 0:5c4d7b2438d3 | 105 | lock(); |
switches | 0:5c4d7b2438d3 | 106 | if (func) { |
switches | 0:5c4d7b2438d3 | 107 | _irq[(CanIrqType)type].attach(func); |
switches | 0:5c4d7b2438d3 | 108 | can_irq_set(&_can, (CanIrqType)type, 1); |
switches | 0:5c4d7b2438d3 | 109 | } else { |
switches | 0:5c4d7b2438d3 | 110 | _irq[(CanIrqType)type].attach(donothing); |
switches | 0:5c4d7b2438d3 | 111 | can_irq_set(&_can, (CanIrqType)type, 0); |
switches | 0:5c4d7b2438d3 | 112 | } |
switches | 0:5c4d7b2438d3 | 113 | unlock(); |
switches | 0:5c4d7b2438d3 | 114 | } |
switches | 0:5c4d7b2438d3 | 115 | |
switches | 0:5c4d7b2438d3 | 116 | void CAN::_irq_handler(uint32_t id, CanIrqType type) { |
switches | 0:5c4d7b2438d3 | 117 | CAN *handler = (CAN*)id; |
switches | 0:5c4d7b2438d3 | 118 | handler->_irq[type].call(); |
switches | 0:5c4d7b2438d3 | 119 | } |
switches | 0:5c4d7b2438d3 | 120 | |
switches | 0:5c4d7b2438d3 | 121 | void CAN::lock() { |
switches | 0:5c4d7b2438d3 | 122 | _mutex.lock(); |
switches | 0:5c4d7b2438d3 | 123 | } |
switches | 0:5c4d7b2438d3 | 124 | |
switches | 0:5c4d7b2438d3 | 125 | void CAN::unlock() { |
switches | 0:5c4d7b2438d3 | 126 | _mutex.unlock(); |
switches | 0:5c4d7b2438d3 | 127 | } |
switches | 0:5c4d7b2438d3 | 128 | |
switches | 0:5c4d7b2438d3 | 129 | } // namespace mbed |
switches | 0:5c4d7b2438d3 | 130 | |
switches | 0:5c4d7b2438d3 | 131 | #endif |