Mike J. Chen | 6c92951 | 2011-08-15 11:59:47 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef __CLOCK_RECOVERY_H__ |
| 18 | #define __CLOCK_RECOVERY_H__ |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <common_time/ICommonClock.h> |
| 22 | #include <utils/LinearTransform.h> |
| 23 | #include <utils/threads.h> |
| 24 | |
| 25 | #ifdef TIME_SERVICE_DEBUG |
| 26 | #include "diag_thread.h" |
| 27 | #endif |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | class CommonClock; |
| 32 | class LocalClock; |
| 33 | |
| 34 | class ClockRecoveryLoop { |
| 35 | public: |
| 36 | ClockRecoveryLoop(LocalClock* local_clock, CommonClock* common_clock); |
| 37 | ~ClockRecoveryLoop(); |
| 38 | |
| 39 | void reset(bool position, bool frequency); |
| 40 | bool pushDisciplineEvent(int64_t local_time, |
| 41 | int64_t nominal_common_time, |
| 42 | int64_t data_point_rtt); |
| 43 | int32_t getLastErrorEstimate(); |
| 44 | |
| 45 | private: |
| 46 | typedef struct { |
| 47 | // Limits for the correction factor supplied to set_counter_slew_rate. |
| 48 | // The controller will always clamp its output to the range expressed by |
| 49 | // correction_(min|max) |
| 50 | int32_t correction_min; |
| 51 | int32_t correction_max; |
| 52 | |
| 53 | // Limits for the internal integration accumulator in the PID |
| 54 | // controller. The value of the accumulator is scaled by gain_I to |
| 55 | // produce the integral component of the PID controller output. |
| 56 | // Platforms can use these limits to prevent windup in the system |
| 57 | // if/when the correction factor needs to be driven to saturation for |
| 58 | // extended periods of time. |
| 59 | int32_t integrated_delta_min; |
| 60 | int32_t integrated_delta_max; |
| 61 | |
| 62 | // Gain for the P, I and D components of the controller. |
| 63 | LinearTransform gain_P; |
| 64 | LinearTransform gain_I; |
| 65 | LinearTransform gain_D; |
| 66 | } PIDParams; |
| 67 | |
| 68 | typedef struct { |
| 69 | int64_t local_time; |
| 70 | int64_t observed_common_time; |
| 71 | int64_t nominal_common_time; |
| 72 | int64_t rtt; |
| 73 | bool point_used; |
| 74 | } DisciplineDataPoint; |
| 75 | |
| 76 | static uint32_t findMinRTTNdx(DisciplineDataPoint* data, uint32_t count); |
| 77 | |
| 78 | void computePIDParams(); |
| 79 | void reset_l(bool position, bool frequency); |
| 80 | static int32_t doGainScale(const LinearTransform& gain, int32_t val); |
| 81 | void applySlew(); |
| 82 | |
| 83 | // The local clock HW abstraction we use as the basis for common time. |
| 84 | LocalClock* local_clock_; |
| 85 | bool local_clock_can_slew_; |
| 86 | |
| 87 | // The common clock we end up controlling along with the lock used to |
| 88 | // serialize operations. |
| 89 | CommonClock* common_clock_; |
| 90 | Mutex lock_; |
| 91 | |
| 92 | // The parameters computed to be used for the PID Controller. |
| 93 | PIDParams pid_params_; |
| 94 | |
| 95 | // The maximum allowed error (as indicated by a pushDisciplineEvent) before |
| 96 | // we panic. |
| 97 | int32_t panic_thresh_; |
| 98 | |
| 99 | // parameters maintained while running and reset during a reset |
| 100 | // of the frequency correction. |
| 101 | bool last_delta_valid_; |
| 102 | int32_t last_delta_; |
| 103 | int32_t integrated_error_; |
| 104 | int32_t correction_cur_; |
| 105 | |
| 106 | // State kept for filtering the discipline data. |
| 107 | static const uint32_t kFilterSize = 6; |
| 108 | DisciplineDataPoint filter_data_[kFilterSize]; |
| 109 | uint32_t filter_wr_; |
| 110 | bool filter_full_; |
| 111 | |
| 112 | static const uint32_t kStartupFilterSize = 4; |
| 113 | DisciplineDataPoint startup_filter_data_[kStartupFilterSize]; |
| 114 | uint32_t startup_filter_wr_; |
| 115 | |
| 116 | #ifdef TIME_SERVICE_DEBUG |
| 117 | sp<DiagThread> diag_thread_; |
| 118 | #endif |
| 119 | }; |
| 120 | |
| 121 | } // namespace android |
| 122 | |
| 123 | #endif // __CLOCK_RECOVERY_H__ |