USB Serial application

Fork of USBSerial_HelloWorld by Samuel Mokrani

Committer:
Zaitsev
Date:
Sat Dec 16 10:26:48 2017 +0000
Revision:
11:b3f2a8bdac4d
Parent:
10:41552d038a69
A copy for D.S;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Zaitsev 10:41552d038a69 1 /* mbed Microcontroller Library
Zaitsev 10:41552d038a69 2 * Copyright (c) 2006-2016 ARM Limited
Zaitsev 10:41552d038a69 3 *
Zaitsev 10:41552d038a69 4 * Licensed under the Apache License, Version 2.0 (the "License");
Zaitsev 10:41552d038a69 5 * you may not use this file except in compliance with the License.
Zaitsev 10:41552d038a69 6 * You may obtain a copy of the License at
Zaitsev 10:41552d038a69 7 *
Zaitsev 10:41552d038a69 8 * http://www.apache.org/licenses/LICENSE-2.0
Zaitsev 10:41552d038a69 9 *
Zaitsev 10:41552d038a69 10 * Unless required by applicable law or agreed to in writing, software
Zaitsev 10:41552d038a69 11 * distributed under the License is distributed on an "AS IS" BASIS,
Zaitsev 10:41552d038a69 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Zaitsev 10:41552d038a69 13 * See the License for the specific language governing permissions and
Zaitsev 10:41552d038a69 14 * limitations under the License.
Zaitsev 10:41552d038a69 15 */
Zaitsev 10:41552d038a69 16 #include <stddef.h>
Zaitsev 10:41552d038a69 17 #include "us_ticker_api.h"
Zaitsev 10:41552d038a69 18 #include "PeripheralNames.h"
Zaitsev 10:41552d038a69 19 #include "hal_tick.h"
Zaitsev 10:41552d038a69 20
Zaitsev 10:41552d038a69 21 // A 16-bit timer is used
Zaitsev 10:41552d038a69 22 #if TIM_MST_16BIT
Zaitsev 10:41552d038a69 23
Zaitsev 10:41552d038a69 24 TIM_HandleTypeDef TimMasterHandle;
Zaitsev 10:41552d038a69 25
Zaitsev 10:41552d038a69 26 volatile uint32_t SlaveCounter = 0;
Zaitsev 10:41552d038a69 27 volatile uint32_t oc_int_part = 0;
Zaitsev 10:41552d038a69 28 volatile uint16_t oc_rem_part = 0;
Zaitsev 10:41552d038a69 29 volatile uint8_t tim_it_update; // TIM_IT_UPDATE event flag set in timer_irq_handler()
Zaitsev 10:41552d038a69 30 volatile uint32_t tim_it_counter = 0; // Time stamp to be updated by timer_irq_handler()
Zaitsev 10:41552d038a69 31
Zaitsev 10:41552d038a69 32 static int us_ticker_inited = 0;
Zaitsev 10:41552d038a69 33
Zaitsev 10:41552d038a69 34 void set_compare(uint16_t count)
Zaitsev 10:41552d038a69 35 {
Zaitsev 10:41552d038a69 36 TimMasterHandle.Instance = TIM_MST;
Zaitsev 10:41552d038a69 37 // Set new output compare value
Zaitsev 10:41552d038a69 38 __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_1, count);
Zaitsev 10:41552d038a69 39 // Enable IT
Zaitsev 10:41552d038a69 40 __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1);
Zaitsev 10:41552d038a69 41 }
Zaitsev 10:41552d038a69 42
Zaitsev 10:41552d038a69 43 void us_ticker_init(void)
Zaitsev 10:41552d038a69 44 {
Zaitsev 10:41552d038a69 45 if (us_ticker_inited) return;
Zaitsev 10:41552d038a69 46 us_ticker_inited = 1;
Zaitsev 10:41552d038a69 47
Zaitsev 10:41552d038a69 48 TimMasterHandle.Instance = TIM_MST;
Zaitsev 10:41552d038a69 49
Zaitsev 10:41552d038a69 50 HAL_InitTick(0); // The passed value is not used
Zaitsev 10:41552d038a69 51 }
Zaitsev 10:41552d038a69 52
Zaitsev 10:41552d038a69 53 uint32_t us_ticker_read()
Zaitsev 10:41552d038a69 54 {
Zaitsev 10:41552d038a69 55 uint32_t counter;
Zaitsev 10:41552d038a69 56
Zaitsev 10:41552d038a69 57 TimMasterHandle.Instance = TIM_MST;
Zaitsev 10:41552d038a69 58
Zaitsev 10:41552d038a69 59 if (!us_ticker_inited) us_ticker_init();
Zaitsev 10:41552d038a69 60
Zaitsev 10:41552d038a69 61 #if defined(TARGET_STM32L0)
Zaitsev 10:41552d038a69 62 uint16_t cntH_old, cntH, cntL;
Zaitsev 10:41552d038a69 63 do {
Zaitsev 10:41552d038a69 64 // For some reason on L0xx series we need to read and clear the
Zaitsev 10:41552d038a69 65 // overflow flag which give extra time to propelry handle possible
Zaitsev 10:41552d038a69 66 // hiccup after ~60s
Zaitsev 10:41552d038a69 67 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1OF) == SET) {
Zaitsev 10:41552d038a69 68 __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1OF);
Zaitsev 10:41552d038a69 69 }
Zaitsev 10:41552d038a69 70 cntH_old = SlaveCounter;
Zaitsev 10:41552d038a69 71 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_UPDATE) == SET) {
Zaitsev 10:41552d038a69 72 cntH_old += 1;
Zaitsev 10:41552d038a69 73 }
Zaitsev 10:41552d038a69 74 cntL = TIM_MST->CNT;
Zaitsev 10:41552d038a69 75 cntH = SlaveCounter;
Zaitsev 10:41552d038a69 76 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_UPDATE) == SET) {
Zaitsev 10:41552d038a69 77 cntH += 1;
Zaitsev 10:41552d038a69 78 }
Zaitsev 10:41552d038a69 79 } while(cntH_old != cntH);
Zaitsev 10:41552d038a69 80 // Glue the upper and lower part together to get a 32 bit timer
Zaitsev 10:41552d038a69 81 return (uint32_t)(cntH << 16 | cntL);
Zaitsev 10:41552d038a69 82 #else
Zaitsev 10:41552d038a69 83 tim_it_update = 0; // Clear TIM_IT_UPDATE event flag
Zaitsev 10:41552d038a69 84 counter = TIM_MST->CNT + (uint32_t)(SlaveCounter << 16); // Calculate new time stamp
Zaitsev 10:41552d038a69 85 if (tim_it_update == 1) {
Zaitsev 10:41552d038a69 86 return tim_it_counter; // In case of TIM_IT_UPDATE return the time stamp that was calculated in timer_irq_handler()
Zaitsev 10:41552d038a69 87 }
Zaitsev 10:41552d038a69 88 else {
Zaitsev 10:41552d038a69 89 return counter; // Otherwise return the time stamp calculated here
Zaitsev 10:41552d038a69 90 }
Zaitsev 10:41552d038a69 91 #endif
Zaitsev 10:41552d038a69 92 }
Zaitsev 10:41552d038a69 93
Zaitsev 10:41552d038a69 94 void us_ticker_set_interrupt(timestamp_t timestamp)
Zaitsev 10:41552d038a69 95 {
Zaitsev 10:41552d038a69 96 int delta = (int)((uint32_t)timestamp - us_ticker_read());
Zaitsev 10:41552d038a69 97
Zaitsev 10:41552d038a69 98 uint16_t cval = TIM_MST->CNT;
Zaitsev 10:41552d038a69 99
Zaitsev 10:41552d038a69 100 if (delta <= 0) { // This event was in the past
Zaitsev 10:41552d038a69 101 us_ticker_irq_handler();
Zaitsev 10:41552d038a69 102 } else {
Zaitsev 10:41552d038a69 103 oc_int_part = (uint32_t)(delta >> 16);
Zaitsev 10:41552d038a69 104 oc_rem_part = (uint16_t)(delta & 0xFFFF);
Zaitsev 10:41552d038a69 105 if (oc_rem_part <= (0xFFFF - cval)) {
Zaitsev 10:41552d038a69 106 set_compare(cval + oc_rem_part);
Zaitsev 10:41552d038a69 107 oc_rem_part = 0;
Zaitsev 10:41552d038a69 108 } else {
Zaitsev 10:41552d038a69 109 set_compare(0xFFFF);
Zaitsev 10:41552d038a69 110 oc_rem_part = oc_rem_part - (0xFFFF - cval);
Zaitsev 10:41552d038a69 111 }
Zaitsev 10:41552d038a69 112 }
Zaitsev 10:41552d038a69 113 }
Zaitsev 10:41552d038a69 114
Zaitsev 10:41552d038a69 115 void us_ticker_disable_interrupt(void)
Zaitsev 10:41552d038a69 116 {
Zaitsev 10:41552d038a69 117 TimMasterHandle.Instance = TIM_MST;
Zaitsev 10:41552d038a69 118 __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1);
Zaitsev 10:41552d038a69 119 }
Zaitsev 10:41552d038a69 120
Zaitsev 10:41552d038a69 121 void us_ticker_clear_interrupt(void)
Zaitsev 10:41552d038a69 122 {
Zaitsev 10:41552d038a69 123 TimMasterHandle.Instance = TIM_MST;
Zaitsev 10:41552d038a69 124 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) {
Zaitsev 10:41552d038a69 125 __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1);
Zaitsev 10:41552d038a69 126 }
Zaitsev 10:41552d038a69 127 }
Zaitsev 10:41552d038a69 128
Zaitsev 10:41552d038a69 129 #endif // TIM_MST_16BIT