mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Fri Mar 21 11:45:09 2014 +0000
Revision:
130:1dec54e4aec3
Child:
240:9a7c54113eaf
Synchronized with git revision e5c9ff6781a4e277a5a454e5a0b037f76e31739d

Full URL: https://github.com/mbedmicro/mbed/commit/e5c9ff6781a4e277a5a454e5a0b037f76e31739d/

STM32F0-Discovery (STM32F051R8) initial port

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 130:1dec54e4aec3 1 /* mbed Microcontroller Library
mbed_official 130:1dec54e4aec3 2 * Copyright (c) 2014, STMicroelectronics
mbed_official 130:1dec54e4aec3 3 * All rights reserved.
mbed_official 130:1dec54e4aec3 4 *
mbed_official 130:1dec54e4aec3 5 * Redistribution and use in source and binary forms, with or without
mbed_official 130:1dec54e4aec3 6 * modification, are permitted provided that the following conditions are met:
mbed_official 130:1dec54e4aec3 7 *
mbed_official 130:1dec54e4aec3 8 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 130:1dec54e4aec3 9 * this list of conditions and the following disclaimer.
mbed_official 130:1dec54e4aec3 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 130:1dec54e4aec3 11 * this list of conditions and the following disclaimer in the documentation
mbed_official 130:1dec54e4aec3 12 * and/or other materials provided with the distribution.
mbed_official 130:1dec54e4aec3 13 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 130:1dec54e4aec3 14 * may be used to endorse or promote products derived from this software
mbed_official 130:1dec54e4aec3 15 * without specific prior written permission.
mbed_official 130:1dec54e4aec3 16 *
mbed_official 130:1dec54e4aec3 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 130:1dec54e4aec3 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 130:1dec54e4aec3 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 130:1dec54e4aec3 20 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 130:1dec54e4aec3 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 130:1dec54e4aec3 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 130:1dec54e4aec3 23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 130:1dec54e4aec3 24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 130:1dec54e4aec3 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 130:1dec54e4aec3 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 130:1dec54e4aec3 27 */
mbed_official 130:1dec54e4aec3 28 #include <stddef.h>
mbed_official 130:1dec54e4aec3 29 #include "us_ticker_api.h"
mbed_official 130:1dec54e4aec3 30 #include "PeripheralNames.h"
mbed_official 130:1dec54e4aec3 31
mbed_official 130:1dec54e4aec3 32 // Timer selection:
mbed_official 130:1dec54e4aec3 33 #define TIM_MST TIM1
mbed_official 130:1dec54e4aec3 34 #define TIM_MST_UP_IRQ TIM1_BRK_UP_TRG_COM_IRQn
mbed_official 130:1dec54e4aec3 35 #define TIM_MST_OC_IRQ TIM1_CC_IRQn
mbed_official 130:1dec54e4aec3 36 #define TIM_MST_RCC RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE)
mbed_official 130:1dec54e4aec3 37
mbed_official 130:1dec54e4aec3 38 static int us_ticker_inited = 0;
mbed_official 130:1dec54e4aec3 39 static volatile uint32_t SlaveCounter = 0;
mbed_official 130:1dec54e4aec3 40 static volatile uint32_t oc_int_part = 0;
mbed_official 130:1dec54e4aec3 41 static volatile uint16_t oc_rem_part = 0;
mbed_official 130:1dec54e4aec3 42
mbed_official 130:1dec54e4aec3 43 void set_compare(uint16_t count) {
mbed_official 130:1dec54e4aec3 44 // Set new output compare value
mbed_official 130:1dec54e4aec3 45 TIM_SetCompare1(TIM_MST, count);
mbed_official 130:1dec54e4aec3 46 // Enable IT
mbed_official 130:1dec54e4aec3 47 TIM_ITConfig(TIM_MST, TIM_IT_CC1, ENABLE);
mbed_official 130:1dec54e4aec3 48 }
mbed_official 130:1dec54e4aec3 49
mbed_official 130:1dec54e4aec3 50 // Used to increment the slave counter
mbed_official 130:1dec54e4aec3 51 static void tim_update_irq_handler(void) {
mbed_official 130:1dec54e4aec3 52 if (TIM_GetITStatus(TIM_MST, TIM_IT_Update) == SET) {
mbed_official 130:1dec54e4aec3 53 TIM_ClearITPendingBit(TIM_MST, TIM_IT_Update);
mbed_official 130:1dec54e4aec3 54 SlaveCounter++;
mbed_official 130:1dec54e4aec3 55 }
mbed_official 130:1dec54e4aec3 56 }
mbed_official 130:1dec54e4aec3 57
mbed_official 130:1dec54e4aec3 58 // Used by interrupt system
mbed_official 130:1dec54e4aec3 59 static void tim_oc_irq_handler(void) {
mbed_official 130:1dec54e4aec3 60 uint16_t cval = TIM_MST->CNT;
mbed_official 130:1dec54e4aec3 61
mbed_official 130:1dec54e4aec3 62 // Clear interrupt flag
mbed_official 130:1dec54e4aec3 63 if (TIM_GetITStatus(TIM_MST, TIM_IT_CC1) == SET) {
mbed_official 130:1dec54e4aec3 64 TIM_ClearITPendingBit(TIM_MST, TIM_IT_CC1);
mbed_official 130:1dec54e4aec3 65 }
mbed_official 130:1dec54e4aec3 66
mbed_official 130:1dec54e4aec3 67 if (oc_rem_part > 0) {
mbed_official 130:1dec54e4aec3 68 set_compare(oc_rem_part); // Finish the remaining time left
mbed_official 130:1dec54e4aec3 69 oc_rem_part = 0;
mbed_official 130:1dec54e4aec3 70 }
mbed_official 130:1dec54e4aec3 71 else {
mbed_official 130:1dec54e4aec3 72 if (oc_int_part > 0) {
mbed_official 130:1dec54e4aec3 73 set_compare(0xFFFF);
mbed_official 130:1dec54e4aec3 74 oc_rem_part = cval; // To finish the counter loop the next time
mbed_official 130:1dec54e4aec3 75 oc_int_part--;
mbed_official 130:1dec54e4aec3 76 }
mbed_official 130:1dec54e4aec3 77 else {
mbed_official 130:1dec54e4aec3 78 us_ticker_irq_handler();
mbed_official 130:1dec54e4aec3 79 }
mbed_official 130:1dec54e4aec3 80 }
mbed_official 130:1dec54e4aec3 81 }
mbed_official 130:1dec54e4aec3 82
mbed_official 130:1dec54e4aec3 83 void us_ticker_init(void) {
mbed_official 130:1dec54e4aec3 84 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
mbed_official 130:1dec54e4aec3 85
mbed_official 130:1dec54e4aec3 86 if (us_ticker_inited) return;
mbed_official 130:1dec54e4aec3 87 us_ticker_inited = 1;
mbed_official 130:1dec54e4aec3 88
mbed_official 130:1dec54e4aec3 89 // Enable Timer clock
mbed_official 130:1dec54e4aec3 90 TIM_MST_RCC;
mbed_official 130:1dec54e4aec3 91
mbed_official 130:1dec54e4aec3 92 // Configure time base
mbed_official 130:1dec54e4aec3 93 TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
mbed_official 130:1dec54e4aec3 94 TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
mbed_official 130:1dec54e4aec3 95 TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick
mbed_official 130:1dec54e4aec3 96 TIM_TimeBaseStructure.TIM_ClockDivision = 0;
mbed_official 130:1dec54e4aec3 97 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
mbed_official 130:1dec54e4aec3 98 TIM_TimeBaseInit(TIM_MST, &TIM_TimeBaseStructure);
mbed_official 130:1dec54e4aec3 99
mbed_official 130:1dec54e4aec3 100 // Configure interrupts
mbed_official 130:1dec54e4aec3 101 TIM_ITConfig(TIM_MST, TIM_IT_Update, ENABLE);
mbed_official 130:1dec54e4aec3 102
mbed_official 130:1dec54e4aec3 103 // Update interrupt used for 32-bit counter
mbed_official 130:1dec54e4aec3 104 NVIC_SetVector(TIM_MST_UP_IRQ, (uint32_t)tim_update_irq_handler);
mbed_official 130:1dec54e4aec3 105 NVIC_EnableIRQ(TIM_MST_UP_IRQ);
mbed_official 130:1dec54e4aec3 106
mbed_official 130:1dec54e4aec3 107 // Output compare interrupt used for timeout feature
mbed_official 130:1dec54e4aec3 108 NVIC_SetVector(TIM_MST_OC_IRQ, (uint32_t)tim_oc_irq_handler);
mbed_official 130:1dec54e4aec3 109 NVIC_EnableIRQ(TIM_MST_OC_IRQ);
mbed_official 130:1dec54e4aec3 110
mbed_official 130:1dec54e4aec3 111 // Enable timer
mbed_official 130:1dec54e4aec3 112 TIM_Cmd(TIM_MST, ENABLE);
mbed_official 130:1dec54e4aec3 113 }
mbed_official 130:1dec54e4aec3 114
mbed_official 130:1dec54e4aec3 115 uint32_t us_ticker_read() {
mbed_official 130:1dec54e4aec3 116 uint32_t counter, counter2;
mbed_official 130:1dec54e4aec3 117 if (!us_ticker_inited) us_ticker_init();
mbed_official 130:1dec54e4aec3 118 // A situation might appear when Master overflows right after Slave is read and before the
mbed_official 130:1dec54e4aec3 119 // new (overflowed) value of Master is read. Which would make the code below consider the
mbed_official 130:1dec54e4aec3 120 // previous (incorrect) value of Slave and the new value of Master, which would return a
mbed_official 130:1dec54e4aec3 121 // value in the past. Avoid this by computing consecutive values of the timer until they
mbed_official 130:1dec54e4aec3 122 // are properly ordered.
mbed_official 130:1dec54e4aec3 123 counter = (uint32_t)(SlaveCounter << 16);
mbed_official 130:1dec54e4aec3 124 counter += TIM_MST->CNT;
mbed_official 130:1dec54e4aec3 125 while (1) {
mbed_official 130:1dec54e4aec3 126 counter2 = (uint32_t)(SlaveCounter << 16);
mbed_official 130:1dec54e4aec3 127 counter2 += TIM_MST->CNT;
mbed_official 130:1dec54e4aec3 128 if (counter2 > counter) {
mbed_official 130:1dec54e4aec3 129 break;
mbed_official 130:1dec54e4aec3 130 }
mbed_official 130:1dec54e4aec3 131 counter = counter2;
mbed_official 130:1dec54e4aec3 132 }
mbed_official 130:1dec54e4aec3 133 return counter2;
mbed_official 130:1dec54e4aec3 134 }
mbed_official 130:1dec54e4aec3 135
mbed_official 130:1dec54e4aec3 136 void us_ticker_set_interrupt(unsigned int timestamp) {
mbed_official 130:1dec54e4aec3 137 int delta = (int)(timestamp - us_ticker_read());
mbed_official 130:1dec54e4aec3 138 uint16_t cval = TIM_MST->CNT;
mbed_official 130:1dec54e4aec3 139
mbed_official 130:1dec54e4aec3 140 if (delta <= 0) { // This event was in the past
mbed_official 130:1dec54e4aec3 141 us_ticker_irq_handler();
mbed_official 130:1dec54e4aec3 142 }
mbed_official 130:1dec54e4aec3 143 else {
mbed_official 130:1dec54e4aec3 144 oc_int_part = (uint32_t)(delta >> 16);
mbed_official 130:1dec54e4aec3 145 oc_rem_part = (uint16_t)(delta & 0xFFFF);
mbed_official 130:1dec54e4aec3 146 if (oc_rem_part <= (0xFFFF - cval)) {
mbed_official 130:1dec54e4aec3 147 set_compare(cval + oc_rem_part);
mbed_official 130:1dec54e4aec3 148 oc_rem_part = 0;
mbed_official 130:1dec54e4aec3 149 } else {
mbed_official 130:1dec54e4aec3 150 set_compare(0xFFFF);
mbed_official 130:1dec54e4aec3 151 oc_rem_part = oc_rem_part - (0xFFFF - cval);
mbed_official 130:1dec54e4aec3 152 }
mbed_official 130:1dec54e4aec3 153 }
mbed_official 130:1dec54e4aec3 154 }
mbed_official 130:1dec54e4aec3 155
mbed_official 130:1dec54e4aec3 156 void us_ticker_disable_interrupt(void) {
mbed_official 130:1dec54e4aec3 157 TIM_ITConfig(TIM_MST, TIM_IT_CC1, DISABLE);
mbed_official 130:1dec54e4aec3 158 }
mbed_official 130:1dec54e4aec3 159
mbed_official 130:1dec54e4aec3 160 void us_ticker_clear_interrupt(void) {
mbed_official 130:1dec54e4aec3 161 if (TIM_GetITStatus(TIM_MST, TIM_IT_CC1) == SET) {
mbed_official 130:1dec54e4aec3 162 TIM_ClearITPendingBit(TIM_MST, TIM_IT_CC1);
mbed_official 130:1dec54e4aec3 163 }
mbed_official 130:1dec54e4aec3 164 }