Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-dev by
common/InterruptManager.cpp@129:8a86d1aafce0, 2016-05-16 (annotated)
- Committer:
- mbed_official
- Date:
- Mon May 16 12:00:12 2016 +0100
- Revision:
- 129:8a86d1aafce0
- Parent:
- 0:9b334a45a8ff
- Child:
- 144:ef7eb2e8f9f7
Synchronized with git revision 1d1f7ab1330d44cc5697c682173f7090a7d6b1cb
Full URL: https://github.com/mbedmicro/mbed/commit/1d1f7ab1330d44cc5697c682173f7090a7d6b1cb/
* [STM32F4] Get PCLK1 clock and set initial CAN frequency
CAN bus opperates on APB1 peripheral clock due to that we need to get PCLK1 freq
in *can_frequency()* function to properly calculate CAN speed and reconfigure
BS1, BS2, SJW bits.
Also to fully communicate with other ST platform we set the initical CAN
frequency to 100kb/s to be able to work with the slowest platform which supports
CAN, which is NUCLEO_F303K8 (APB1 is 32MHz).
Change-Id: I10af3aa8d715dd61c9d1b216ef813193449fecbd
* [STM32F4] Fix for CAN2 interrupt index
CAN2 interrupt index was wrong leading to not properly registering interrupt.
Having this fix allow us to pass MBED_30 test.
Change-Id: I33f9ca7c81286f7746a8f8352619e213bdf9756a
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| bogdanm | 0:9b334a45a8ff | 1 | #include "cmsis.h" |
| bogdanm | 0:9b334a45a8ff | 2 | #if defined(NVIC_NUM_VECTORS) |
| bogdanm | 0:9b334a45a8ff | 3 | |
| bogdanm | 0:9b334a45a8ff | 4 | #include "InterruptManager.h" |
| bogdanm | 0:9b334a45a8ff | 5 | #include <string.h> |
| bogdanm | 0:9b334a45a8ff | 6 | |
| bogdanm | 0:9b334a45a8ff | 7 | #define CHAIN_INITIAL_SIZE 4 |
| bogdanm | 0:9b334a45a8ff | 8 | |
| bogdanm | 0:9b334a45a8ff | 9 | namespace mbed { |
| bogdanm | 0:9b334a45a8ff | 10 | |
| bogdanm | 0:9b334a45a8ff | 11 | typedef void (*pvoidf)(void); |
| bogdanm | 0:9b334a45a8ff | 12 | |
| bogdanm | 0:9b334a45a8ff | 13 | InterruptManager* InterruptManager::_instance = (InterruptManager*)NULL; |
| bogdanm | 0:9b334a45a8ff | 14 | |
| bogdanm | 0:9b334a45a8ff | 15 | InterruptManager* InterruptManager::get() { |
| bogdanm | 0:9b334a45a8ff | 16 | if (NULL == _instance) |
| bogdanm | 0:9b334a45a8ff | 17 | _instance = new InterruptManager(); |
| bogdanm | 0:9b334a45a8ff | 18 | return _instance; |
| bogdanm | 0:9b334a45a8ff | 19 | } |
| bogdanm | 0:9b334a45a8ff | 20 | |
| bogdanm | 0:9b334a45a8ff | 21 | InterruptManager::InterruptManager() { |
| bogdanm | 0:9b334a45a8ff | 22 | memset(_chains, 0, NVIC_NUM_VECTORS * sizeof(CallChain*)); |
| bogdanm | 0:9b334a45a8ff | 23 | } |
| bogdanm | 0:9b334a45a8ff | 24 | |
| bogdanm | 0:9b334a45a8ff | 25 | void InterruptManager::destroy() { |
| bogdanm | 0:9b334a45a8ff | 26 | // Not a good idea to call this unless NO interrupt at all |
| bogdanm | 0:9b334a45a8ff | 27 | // is under the control of the handler; otherwise, a system crash |
| bogdanm | 0:9b334a45a8ff | 28 | // is very likely to occur |
| bogdanm | 0:9b334a45a8ff | 29 | if (NULL != _instance) { |
| bogdanm | 0:9b334a45a8ff | 30 | delete _instance; |
| bogdanm | 0:9b334a45a8ff | 31 | _instance = (InterruptManager*)NULL; |
| bogdanm | 0:9b334a45a8ff | 32 | } |
| bogdanm | 0:9b334a45a8ff | 33 | } |
| bogdanm | 0:9b334a45a8ff | 34 | |
| bogdanm | 0:9b334a45a8ff | 35 | InterruptManager::~InterruptManager() { |
| bogdanm | 0:9b334a45a8ff | 36 | for(int i = 0; i < NVIC_NUM_VECTORS; i++) |
| bogdanm | 0:9b334a45a8ff | 37 | if (NULL != _chains[i]) |
| bogdanm | 0:9b334a45a8ff | 38 | delete _chains[i]; |
| bogdanm | 0:9b334a45a8ff | 39 | } |
| bogdanm | 0:9b334a45a8ff | 40 | |
| bogdanm | 0:9b334a45a8ff | 41 | bool InterruptManager::must_replace_vector(IRQn_Type irq) { |
| bogdanm | 0:9b334a45a8ff | 42 | int irq_pos = get_irq_index(irq); |
| bogdanm | 0:9b334a45a8ff | 43 | |
| bogdanm | 0:9b334a45a8ff | 44 | if (NULL == _chains[irq_pos]) { |
| bogdanm | 0:9b334a45a8ff | 45 | _chains[irq_pos] = new CallChain(CHAIN_INITIAL_SIZE); |
| bogdanm | 0:9b334a45a8ff | 46 | _chains[irq_pos]->add((pvoidf)NVIC_GetVector(irq)); |
| bogdanm | 0:9b334a45a8ff | 47 | return true; |
| bogdanm | 0:9b334a45a8ff | 48 | } |
| bogdanm | 0:9b334a45a8ff | 49 | return false; |
| bogdanm | 0:9b334a45a8ff | 50 | } |
| bogdanm | 0:9b334a45a8ff | 51 | |
| bogdanm | 0:9b334a45a8ff | 52 | pFunctionPointer_t InterruptManager::add_common(void (*function)(void), IRQn_Type irq, bool front) { |
| bogdanm | 0:9b334a45a8ff | 53 | int irq_pos = get_irq_index(irq); |
| bogdanm | 0:9b334a45a8ff | 54 | bool change = must_replace_vector(irq); |
| bogdanm | 0:9b334a45a8ff | 55 | |
| bogdanm | 0:9b334a45a8ff | 56 | pFunctionPointer_t pf = front ? _chains[irq_pos]->add_front(function) : _chains[irq_pos]->add(function); |
| bogdanm | 0:9b334a45a8ff | 57 | if (change) |
| bogdanm | 0:9b334a45a8ff | 58 | NVIC_SetVector(irq, (uint32_t)&InterruptManager::static_irq_helper); |
| bogdanm | 0:9b334a45a8ff | 59 | return pf; |
| bogdanm | 0:9b334a45a8ff | 60 | } |
| bogdanm | 0:9b334a45a8ff | 61 | |
| bogdanm | 0:9b334a45a8ff | 62 | bool InterruptManager::remove_handler(pFunctionPointer_t handler, IRQn_Type irq) { |
| bogdanm | 0:9b334a45a8ff | 63 | int irq_pos = get_irq_index(irq); |
| bogdanm | 0:9b334a45a8ff | 64 | |
| bogdanm | 0:9b334a45a8ff | 65 | if (NULL == _chains[irq_pos]) |
| bogdanm | 0:9b334a45a8ff | 66 | return false; |
| bogdanm | 0:9b334a45a8ff | 67 | if (!_chains[irq_pos]->remove(handler)) |
| bogdanm | 0:9b334a45a8ff | 68 | return false; |
| bogdanm | 0:9b334a45a8ff | 69 | // If there's a single function left in the chain, swith the interrupt vector |
| bogdanm | 0:9b334a45a8ff | 70 | // to call that function directly. This way we save both time and space. |
| bogdanm | 0:9b334a45a8ff | 71 | if (_chains[irq_pos]->size() == 1 && NULL != _chains[irq_pos]->get(0)->get_function()) { |
| bogdanm | 0:9b334a45a8ff | 72 | NVIC_SetVector(irq, (uint32_t)_chains[irq_pos]->get(0)->get_function()); |
| bogdanm | 0:9b334a45a8ff | 73 | delete _chains[irq_pos]; |
| bogdanm | 0:9b334a45a8ff | 74 | _chains[irq_pos] = (CallChain*) NULL; |
| bogdanm | 0:9b334a45a8ff | 75 | } |
| bogdanm | 0:9b334a45a8ff | 76 | return true; |
| bogdanm | 0:9b334a45a8ff | 77 | } |
| bogdanm | 0:9b334a45a8ff | 78 | |
| bogdanm | 0:9b334a45a8ff | 79 | void InterruptManager::irq_helper() { |
| bogdanm | 0:9b334a45a8ff | 80 | _chains[__get_IPSR()]->call(); |
| bogdanm | 0:9b334a45a8ff | 81 | } |
| bogdanm | 0:9b334a45a8ff | 82 | |
| bogdanm | 0:9b334a45a8ff | 83 | int InterruptManager::get_irq_index(IRQn_Type irq) { |
| bogdanm | 0:9b334a45a8ff | 84 | return (int)irq + NVIC_USER_IRQ_OFFSET; |
| bogdanm | 0:9b334a45a8ff | 85 | } |
| bogdanm | 0:9b334a45a8ff | 86 | |
| bogdanm | 0:9b334a45a8ff | 87 | void InterruptManager::static_irq_helper() { |
| bogdanm | 0:9b334a45a8ff | 88 | InterruptManager::get()->irq_helper(); |
| bogdanm | 0:9b334a45a8ff | 89 | } |
| bogdanm | 0:9b334a45a8ff | 90 | |
| bogdanm | 0:9b334a45a8ff | 91 | } // namespace mbed |
| bogdanm | 0:9b334a45a8ff | 92 | |
| bogdanm | 0:9b334a45a8ff | 93 | #endif |
