mbed library sources for GR-PEACH rev.B.

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Mon Feb 03 09:30:05 2014 +0000
Revision:
84:f54042cbc282
Parent:
76:aeb1df146756
Child:
87:085cde657901
Synchronized with git revision bbbd8699601c42149ccf0c37bc42bb6856ccc4c6

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

[NUCLEO_L152RE/F030_R8] SPI, I2C, Ticker, PWM updates

Who changed what in which revision?

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