Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lypinator 0:bb348c97df44 1 /* mbed Microcontroller Library
lypinator 0:bb348c97df44 2 * Copyright (c) 2006-2013 ARM Limited
lypinator 0:bb348c97df44 3 *
lypinator 0:bb348c97df44 4 * Licensed under the Apache License, Version 2.0 (the "License");
lypinator 0:bb348c97df44 5 * you may not use this file except in compliance with the License.
lypinator 0:bb348c97df44 6 * You may obtain a copy of the License at
lypinator 0:bb348c97df44 7 *
lypinator 0:bb348c97df44 8 * http://www.apache.org/licenses/LICENSE-2.0
lypinator 0:bb348c97df44 9 *
lypinator 0:bb348c97df44 10 * Unless required by applicable law or agreed to in writing, software
lypinator 0:bb348c97df44 11 * distributed under the License is distributed on an "AS IS" BASIS,
lypinator 0:bb348c97df44 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
lypinator 0:bb348c97df44 13 * See the License for the specific language governing permissions and
lypinator 0:bb348c97df44 14 * limitations under the License.
lypinator 0:bb348c97df44 15 */
lypinator 0:bb348c97df44 16 #include "drivers/CAN.h"
lypinator 0:bb348c97df44 17
lypinator 0:bb348c97df44 18 #if DEVICE_CAN
lypinator 0:bb348c97df44 19
lypinator 0:bb348c97df44 20 #include "cmsis.h"
lypinator 0:bb348c97df44 21 #include "platform/mbed_power_mgmt.h"
lypinator 0:bb348c97df44 22
lypinator 0:bb348c97df44 23 namespace mbed {
lypinator 0:bb348c97df44 24
lypinator 0:bb348c97df44 25 CAN::CAN(PinName rd, PinName td) : _can(), _irq()
lypinator 0:bb348c97df44 26 {
lypinator 0:bb348c97df44 27 // No lock needed in constructor
lypinator 0:bb348c97df44 28
lypinator 0:bb348c97df44 29 for (size_t i = 0; i < sizeof _irq / sizeof _irq[0]; i++) {
lypinator 0:bb348c97df44 30 _irq[i] = NULL;
lypinator 0:bb348c97df44 31 }
lypinator 0:bb348c97df44 32
lypinator 0:bb348c97df44 33 can_init(&_can, rd, td);
lypinator 0:bb348c97df44 34 can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this);
lypinator 0:bb348c97df44 35 }
lypinator 0:bb348c97df44 36
lypinator 0:bb348c97df44 37 CAN::CAN(PinName rd, PinName td, int hz) : _can(), _irq()
lypinator 0:bb348c97df44 38 {
lypinator 0:bb348c97df44 39 // No lock needed in constructor
lypinator 0:bb348c97df44 40
lypinator 0:bb348c97df44 41 for (size_t i = 0; i < sizeof _irq / sizeof _irq[0]; i++) {
lypinator 0:bb348c97df44 42 _irq[i] = NULL;
lypinator 0:bb348c97df44 43 }
lypinator 0:bb348c97df44 44
lypinator 0:bb348c97df44 45 can_init_freq(&_can, rd, td, hz);
lypinator 0:bb348c97df44 46 can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this);
lypinator 0:bb348c97df44 47 }
lypinator 0:bb348c97df44 48
lypinator 0:bb348c97df44 49 CAN::~CAN()
lypinator 0:bb348c97df44 50 {
lypinator 0:bb348c97df44 51 // No lock needed in destructor
lypinator 0:bb348c97df44 52
lypinator 0:bb348c97df44 53 // Detaching interrupts releases the sleep lock if it was locked
lypinator 0:bb348c97df44 54 for (int irq = 0; irq < IrqCnt; irq++) {
lypinator 0:bb348c97df44 55 attach(NULL, (IrqType)irq);
lypinator 0:bb348c97df44 56 }
lypinator 0:bb348c97df44 57 can_irq_free(&_can);
lypinator 0:bb348c97df44 58 can_free(&_can);
lypinator 0:bb348c97df44 59 }
lypinator 0:bb348c97df44 60
lypinator 0:bb348c97df44 61 int CAN::frequency(int f)
lypinator 0:bb348c97df44 62 {
lypinator 0:bb348c97df44 63 lock();
lypinator 0:bb348c97df44 64 int ret = can_frequency(&_can, f);
lypinator 0:bb348c97df44 65 unlock();
lypinator 0:bb348c97df44 66 return ret;
lypinator 0:bb348c97df44 67 }
lypinator 0:bb348c97df44 68
lypinator 0:bb348c97df44 69 int CAN::write(CANMessage msg)
lypinator 0:bb348c97df44 70 {
lypinator 0:bb348c97df44 71 lock();
lypinator 0:bb348c97df44 72 int ret = can_write(&_can, msg, 0);
lypinator 0:bb348c97df44 73 unlock();
lypinator 0:bb348c97df44 74 return ret;
lypinator 0:bb348c97df44 75 }
lypinator 0:bb348c97df44 76
lypinator 0:bb348c97df44 77 int CAN::read(CANMessage &msg, int handle)
lypinator 0:bb348c97df44 78 {
lypinator 0:bb348c97df44 79 lock();
lypinator 0:bb348c97df44 80 int ret = can_read(&_can, &msg, handle);
lypinator 0:bb348c97df44 81 unlock();
lypinator 0:bb348c97df44 82 return ret;
lypinator 0:bb348c97df44 83 }
lypinator 0:bb348c97df44 84
lypinator 0:bb348c97df44 85 void CAN::reset()
lypinator 0:bb348c97df44 86 {
lypinator 0:bb348c97df44 87 lock();
lypinator 0:bb348c97df44 88 can_reset(&_can);
lypinator 0:bb348c97df44 89 unlock();
lypinator 0:bb348c97df44 90 }
lypinator 0:bb348c97df44 91
lypinator 0:bb348c97df44 92 unsigned char CAN::rderror()
lypinator 0:bb348c97df44 93 {
lypinator 0:bb348c97df44 94 lock();
lypinator 0:bb348c97df44 95 int ret = can_rderror(&_can);
lypinator 0:bb348c97df44 96 unlock();
lypinator 0:bb348c97df44 97 return ret;
lypinator 0:bb348c97df44 98 }
lypinator 0:bb348c97df44 99
lypinator 0:bb348c97df44 100 unsigned char CAN::tderror()
lypinator 0:bb348c97df44 101 {
lypinator 0:bb348c97df44 102 lock();
lypinator 0:bb348c97df44 103 int ret = can_tderror(&_can);
lypinator 0:bb348c97df44 104 unlock();
lypinator 0:bb348c97df44 105 return ret;
lypinator 0:bb348c97df44 106 }
lypinator 0:bb348c97df44 107
lypinator 0:bb348c97df44 108 void CAN::monitor(bool silent)
lypinator 0:bb348c97df44 109 {
lypinator 0:bb348c97df44 110 lock();
lypinator 0:bb348c97df44 111 can_monitor(&_can, (silent) ? 1 : 0);
lypinator 0:bb348c97df44 112 unlock();
lypinator 0:bb348c97df44 113 }
lypinator 0:bb348c97df44 114
lypinator 0:bb348c97df44 115 int CAN::mode(Mode mode)
lypinator 0:bb348c97df44 116 {
lypinator 0:bb348c97df44 117 lock();
lypinator 0:bb348c97df44 118 int ret = can_mode(&_can, (CanMode)mode);
lypinator 0:bb348c97df44 119 unlock();
lypinator 0:bb348c97df44 120 return ret;
lypinator 0:bb348c97df44 121 }
lypinator 0:bb348c97df44 122
lypinator 0:bb348c97df44 123 int CAN::filter(unsigned int id, unsigned int mask, CANFormat format, int handle)
lypinator 0:bb348c97df44 124 {
lypinator 0:bb348c97df44 125 lock();
lypinator 0:bb348c97df44 126 int ret = can_filter(&_can, id, mask, format, handle);
lypinator 0:bb348c97df44 127 unlock();
lypinator 0:bb348c97df44 128 return ret;
lypinator 0:bb348c97df44 129 }
lypinator 0:bb348c97df44 130
lypinator 0:bb348c97df44 131 void CAN::attach(Callback<void()> func, IrqType type)
lypinator 0:bb348c97df44 132 {
lypinator 0:bb348c97df44 133 lock();
lypinator 0:bb348c97df44 134 if (func) {
lypinator 0:bb348c97df44 135 // lock deep sleep only the first time
lypinator 0:bb348c97df44 136 if (!_irq[(CanIrqType)type]) {
lypinator 0:bb348c97df44 137 sleep_manager_lock_deep_sleep();
lypinator 0:bb348c97df44 138 }
lypinator 0:bb348c97df44 139 _irq[(CanIrqType)type] = func;
lypinator 0:bb348c97df44 140 can_irq_set(&_can, (CanIrqType)type, 1);
lypinator 0:bb348c97df44 141 } else {
lypinator 0:bb348c97df44 142 // unlock deep sleep only the first time
lypinator 0:bb348c97df44 143 if (_irq[(CanIrqType)type]) {
lypinator 0:bb348c97df44 144 sleep_manager_unlock_deep_sleep();
lypinator 0:bb348c97df44 145 }
lypinator 0:bb348c97df44 146 _irq[(CanIrqType)type] = NULL;
lypinator 0:bb348c97df44 147 can_irq_set(&_can, (CanIrqType)type, 0);
lypinator 0:bb348c97df44 148 }
lypinator 0:bb348c97df44 149 unlock();
lypinator 0:bb348c97df44 150 }
lypinator 0:bb348c97df44 151
lypinator 0:bb348c97df44 152 void CAN::_irq_handler(uint32_t id, CanIrqType type)
lypinator 0:bb348c97df44 153 {
lypinator 0:bb348c97df44 154 CAN *handler = (CAN *)id;
lypinator 0:bb348c97df44 155 if (handler->_irq[type]) {
lypinator 0:bb348c97df44 156 handler->_irq[type].call();
lypinator 0:bb348c97df44 157 }
lypinator 0:bb348c97df44 158 }
lypinator 0:bb348c97df44 159
lypinator 0:bb348c97df44 160 void CAN::lock()
lypinator 0:bb348c97df44 161 {
lypinator 0:bb348c97df44 162 _mutex.lock();
lypinator 0:bb348c97df44 163 }
lypinator 0:bb348c97df44 164
lypinator 0:bb348c97df44 165 void CAN::unlock()
lypinator 0:bb348c97df44 166 {
lypinator 0:bb348c97df44 167 _mutex.unlock();
lypinator 0:bb348c97df44 168 }
lypinator 0:bb348c97df44 169
lypinator 0:bb348c97df44 170 } // namespace mbed
lypinator 0:bb348c97df44 171
lypinator 0:bb348c97df44 172 #endif