![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K82F/drivers/fsl_tsi_v4.c@3:f3764f852aa8, 2018-10-11 (annotated)
- Committer:
- kadonotakashi
- Date:
- Thu Oct 11 02:27:46 2018 +0000
- Revision:
- 3:f3764f852aa8
- Parent:
- 0:8fdf9a60065b
Nucreo 446 + SSD1331 test version;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kadonotakashi | 0:8fdf9a60065b | 1 | /* |
kadonotakashi | 0:8fdf9a60065b | 2 | * Copyright (c) 2014 - 2015, Freescale Semiconductor, Inc. |
kadonotakashi | 0:8fdf9a60065b | 3 | * All rights reserved. |
kadonotakashi | 0:8fdf9a60065b | 4 | * |
kadonotakashi | 0:8fdf9a60065b | 5 | * Redistribution and use in source and binary forms, with or without modification, |
kadonotakashi | 0:8fdf9a60065b | 6 | * are permitted provided that the following conditions are met: |
kadonotakashi | 0:8fdf9a60065b | 7 | * |
kadonotakashi | 0:8fdf9a60065b | 8 | * o Redistributions of source code must retain the above copyright notice, this list |
kadonotakashi | 0:8fdf9a60065b | 9 | * of conditions and the following disclaimer. |
kadonotakashi | 0:8fdf9a60065b | 10 | * |
kadonotakashi | 0:8fdf9a60065b | 11 | * o Redistributions in binary form must reproduce the above copyright notice, this |
kadonotakashi | 0:8fdf9a60065b | 12 | * list of conditions and the following disclaimer in the documentation and/or |
kadonotakashi | 0:8fdf9a60065b | 13 | * other materials provided with the distribution. |
kadonotakashi | 0:8fdf9a60065b | 14 | * |
kadonotakashi | 0:8fdf9a60065b | 15 | * o Neither the name of Freescale Semiconductor, Inc. nor the names of its |
kadonotakashi | 0:8fdf9a60065b | 16 | * contributors may be used to endorse or promote products derived from this |
kadonotakashi | 0:8fdf9a60065b | 17 | * software without specific prior written permission. |
kadonotakashi | 0:8fdf9a60065b | 18 | * |
kadonotakashi | 0:8fdf9a60065b | 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
kadonotakashi | 0:8fdf9a60065b | 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
kadonotakashi | 0:8fdf9a60065b | 21 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
kadonotakashi | 0:8fdf9a60065b | 22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
kadonotakashi | 0:8fdf9a60065b | 23 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
kadonotakashi | 0:8fdf9a60065b | 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
kadonotakashi | 0:8fdf9a60065b | 25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
kadonotakashi | 0:8fdf9a60065b | 26 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
kadonotakashi | 0:8fdf9a60065b | 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
kadonotakashi | 0:8fdf9a60065b | 28 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
kadonotakashi | 0:8fdf9a60065b | 29 | */ |
kadonotakashi | 0:8fdf9a60065b | 30 | #include "fsl_tsi_v4.h" |
kadonotakashi | 0:8fdf9a60065b | 31 | |
kadonotakashi | 0:8fdf9a60065b | 32 | void TSI_Init(TSI_Type *base, const tsi_config_t *config) |
kadonotakashi | 0:8fdf9a60065b | 33 | { |
kadonotakashi | 0:8fdf9a60065b | 34 | assert(config != NULL); |
kadonotakashi | 0:8fdf9a60065b | 35 | |
kadonotakashi | 0:8fdf9a60065b | 36 | bool is_module_enabled = false; |
kadonotakashi | 0:8fdf9a60065b | 37 | bool is_int_enabled = false; |
kadonotakashi | 0:8fdf9a60065b | 38 | |
kadonotakashi | 0:8fdf9a60065b | 39 | #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) |
kadonotakashi | 0:8fdf9a60065b | 40 | CLOCK_EnableClock(kCLOCK_Tsi0); |
kadonotakashi | 0:8fdf9a60065b | 41 | #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */ |
kadonotakashi | 0:8fdf9a60065b | 42 | if (base->GENCS & TSI_GENCS_TSIEN_MASK) |
kadonotakashi | 0:8fdf9a60065b | 43 | { |
kadonotakashi | 0:8fdf9a60065b | 44 | is_module_enabled = true; |
kadonotakashi | 0:8fdf9a60065b | 45 | TSI_EnableModule(base, false); |
kadonotakashi | 0:8fdf9a60065b | 46 | } |
kadonotakashi | 0:8fdf9a60065b | 47 | if (base->GENCS & TSI_GENCS_TSIIEN_MASK) |
kadonotakashi | 0:8fdf9a60065b | 48 | { |
kadonotakashi | 0:8fdf9a60065b | 49 | is_int_enabled = true; |
kadonotakashi | 0:8fdf9a60065b | 50 | TSI_DisableInterrupts(base, kTSI_GlobalInterruptEnable); |
kadonotakashi | 0:8fdf9a60065b | 51 | } |
kadonotakashi | 0:8fdf9a60065b | 52 | |
kadonotakashi | 0:8fdf9a60065b | 53 | if(config->mode == kTSI_AnalogModeSel_Capacitive) |
kadonotakashi | 0:8fdf9a60065b | 54 | { |
kadonotakashi | 0:8fdf9a60065b | 55 | TSI_SetHighThreshold(base, config->thresh); |
kadonotakashi | 0:8fdf9a60065b | 56 | TSI_SetLowThreshold(base, config->thresl); |
kadonotakashi | 0:8fdf9a60065b | 57 | TSI_SetElectrodeOSCPrescaler(base, config->prescaler); |
kadonotakashi | 0:8fdf9a60065b | 58 | TSI_SetReferenceChargeCurrent(base, config->refchrg); |
kadonotakashi | 0:8fdf9a60065b | 59 | TSI_SetElectrodeChargeCurrent(base, config->extchrg); |
kadonotakashi | 0:8fdf9a60065b | 60 | TSI_SetNumberOfScans(base, config->nscn); |
kadonotakashi | 0:8fdf9a60065b | 61 | TSI_SetAnalogMode(base, config->mode); |
kadonotakashi | 0:8fdf9a60065b | 62 | TSI_SetOscVoltageRails(base, config->dvolt); |
kadonotakashi | 0:8fdf9a60065b | 63 | } |
kadonotakashi | 0:8fdf9a60065b | 64 | else /* For noise modes */ |
kadonotakashi | 0:8fdf9a60065b | 65 | { |
kadonotakashi | 0:8fdf9a60065b | 66 | TSI_SetHighThreshold(base, config->thresh); |
kadonotakashi | 0:8fdf9a60065b | 67 | TSI_SetLowThreshold(base, config->thresl); |
kadonotakashi | 0:8fdf9a60065b | 68 | TSI_SetElectrodeOSCPrescaler(base, config->prescaler); |
kadonotakashi | 0:8fdf9a60065b | 69 | TSI_SetReferenceChargeCurrent(base, config->refchrg); |
kadonotakashi | 0:8fdf9a60065b | 70 | TSI_SetNumberOfScans(base, config->nscn); |
kadonotakashi | 0:8fdf9a60065b | 71 | TSI_SetAnalogMode(base, config->mode); |
kadonotakashi | 0:8fdf9a60065b | 72 | TSI_SetOscVoltageRails(base, config->dvolt); |
kadonotakashi | 0:8fdf9a60065b | 73 | TSI_SetElectrodeSeriesResistor(base, config->resistor); |
kadonotakashi | 0:8fdf9a60065b | 74 | TSI_SetFilterBits(base, config->filter); |
kadonotakashi | 0:8fdf9a60065b | 75 | } |
kadonotakashi | 0:8fdf9a60065b | 76 | |
kadonotakashi | 0:8fdf9a60065b | 77 | if (is_module_enabled) |
kadonotakashi | 0:8fdf9a60065b | 78 | { |
kadonotakashi | 0:8fdf9a60065b | 79 | TSI_EnableModule(base, true); |
kadonotakashi | 0:8fdf9a60065b | 80 | } |
kadonotakashi | 0:8fdf9a60065b | 81 | if (is_int_enabled) |
kadonotakashi | 0:8fdf9a60065b | 82 | { |
kadonotakashi | 0:8fdf9a60065b | 83 | TSI_EnableInterrupts(base, kTSI_GlobalInterruptEnable); |
kadonotakashi | 0:8fdf9a60065b | 84 | } |
kadonotakashi | 0:8fdf9a60065b | 85 | } |
kadonotakashi | 0:8fdf9a60065b | 86 | |
kadonotakashi | 0:8fdf9a60065b | 87 | void TSI_Deinit(TSI_Type *base) |
kadonotakashi | 0:8fdf9a60065b | 88 | { |
kadonotakashi | 0:8fdf9a60065b | 89 | base->GENCS = 0U; |
kadonotakashi | 0:8fdf9a60065b | 90 | base->DATA = 0U; |
kadonotakashi | 0:8fdf9a60065b | 91 | base->TSHD = 0U; |
kadonotakashi | 0:8fdf9a60065b | 92 | #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) |
kadonotakashi | 0:8fdf9a60065b | 93 | CLOCK_DisableClock(kCLOCK_Tsi0); |
kadonotakashi | 0:8fdf9a60065b | 94 | #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */ |
kadonotakashi | 0:8fdf9a60065b | 95 | } |
kadonotakashi | 0:8fdf9a60065b | 96 | |
kadonotakashi | 0:8fdf9a60065b | 97 | void TSI_GetNormalModeDefaultConfig(tsi_config_t *userConfig) |
kadonotakashi | 0:8fdf9a60065b | 98 | { |
kadonotakashi | 0:8fdf9a60065b | 99 | userConfig->thresh = 0U; |
kadonotakashi | 0:8fdf9a60065b | 100 | userConfig->thresl = 0U; |
kadonotakashi | 0:8fdf9a60065b | 101 | userConfig->prescaler = kTSI_ElecOscPrescaler_2div; |
kadonotakashi | 0:8fdf9a60065b | 102 | userConfig->extchrg = kTSI_ExtOscChargeCurrent_500nA; |
kadonotakashi | 0:8fdf9a60065b | 103 | userConfig->refchrg = kTSI_RefOscChargeCurrent_4uA; |
kadonotakashi | 0:8fdf9a60065b | 104 | userConfig->nscn = kTSI_ConsecutiveScansNumber_5time; |
kadonotakashi | 0:8fdf9a60065b | 105 | userConfig->mode = kTSI_AnalogModeSel_Capacitive; |
kadonotakashi | 0:8fdf9a60065b | 106 | userConfig->dvolt = kTSI_OscVolRailsOption_0; |
kadonotakashi | 0:8fdf9a60065b | 107 | } |
kadonotakashi | 0:8fdf9a60065b | 108 | |
kadonotakashi | 0:8fdf9a60065b | 109 | void TSI_GetLowPowerModeDefaultConfig(tsi_config_t *userConfig) |
kadonotakashi | 0:8fdf9a60065b | 110 | { |
kadonotakashi | 0:8fdf9a60065b | 111 | userConfig->thresh = 400U; |
kadonotakashi | 0:8fdf9a60065b | 112 | userConfig->thresl = 0U; |
kadonotakashi | 0:8fdf9a60065b | 113 | userConfig->prescaler = kTSI_ElecOscPrescaler_2div; |
kadonotakashi | 0:8fdf9a60065b | 114 | userConfig->extchrg = kTSI_ExtOscChargeCurrent_500nA; |
kadonotakashi | 0:8fdf9a60065b | 115 | userConfig->refchrg = kTSI_RefOscChargeCurrent_4uA; |
kadonotakashi | 0:8fdf9a60065b | 116 | userConfig->nscn = kTSI_ConsecutiveScansNumber_5time; |
kadonotakashi | 0:8fdf9a60065b | 117 | userConfig->mode = kTSI_AnalogModeSel_Capacitive; |
kadonotakashi | 0:8fdf9a60065b | 118 | userConfig->dvolt = kTSI_OscVolRailsOption_0; |
kadonotakashi | 0:8fdf9a60065b | 119 | } |
kadonotakashi | 0:8fdf9a60065b | 120 | |
kadonotakashi | 0:8fdf9a60065b | 121 | void TSI_Calibrate(TSI_Type *base, tsi_calibration_data_t *calBuff) |
kadonotakashi | 0:8fdf9a60065b | 122 | { |
kadonotakashi | 0:8fdf9a60065b | 123 | assert(calBuff != NULL); |
kadonotakashi | 0:8fdf9a60065b | 124 | |
kadonotakashi | 0:8fdf9a60065b | 125 | uint8_t i = 0U; |
kadonotakashi | 0:8fdf9a60065b | 126 | bool is_int_enabled = false; |
kadonotakashi | 0:8fdf9a60065b | 127 | |
kadonotakashi | 0:8fdf9a60065b | 128 | if (base->GENCS & TSI_GENCS_TSIIEN_MASK) |
kadonotakashi | 0:8fdf9a60065b | 129 | { |
kadonotakashi | 0:8fdf9a60065b | 130 | is_int_enabled = true; |
kadonotakashi | 0:8fdf9a60065b | 131 | TSI_DisableInterrupts(base, kTSI_GlobalInterruptEnable); |
kadonotakashi | 0:8fdf9a60065b | 132 | } |
kadonotakashi | 0:8fdf9a60065b | 133 | for (i = 0U; i < FSL_FEATURE_TSI_CHANNEL_COUNT; i++) |
kadonotakashi | 0:8fdf9a60065b | 134 | { |
kadonotakashi | 0:8fdf9a60065b | 135 | TSI_SetMeasuredChannelNumber(base, i); |
kadonotakashi | 0:8fdf9a60065b | 136 | TSI_StartSoftwareTrigger(base); |
kadonotakashi | 0:8fdf9a60065b | 137 | while (!(TSI_GetStatusFlags(base) & kTSI_EndOfScanFlag)) |
kadonotakashi | 0:8fdf9a60065b | 138 | { |
kadonotakashi | 0:8fdf9a60065b | 139 | } |
kadonotakashi | 0:8fdf9a60065b | 140 | calBuff->calibratedData[i] = TSI_GetCounter(base); |
kadonotakashi | 0:8fdf9a60065b | 141 | TSI_ClearStatusFlags(base, kTSI_EndOfScanFlag); |
kadonotakashi | 0:8fdf9a60065b | 142 | } |
kadonotakashi | 0:8fdf9a60065b | 143 | if (is_int_enabled) |
kadonotakashi | 0:8fdf9a60065b | 144 | { |
kadonotakashi | 0:8fdf9a60065b | 145 | TSI_EnableInterrupts(base, kTSI_GlobalInterruptEnable); |
kadonotakashi | 0:8fdf9a60065b | 146 | } |
kadonotakashi | 0:8fdf9a60065b | 147 | } |
kadonotakashi | 0:8fdf9a60065b | 148 | |
kadonotakashi | 0:8fdf9a60065b | 149 | void TSI_EnableInterrupts(TSI_Type *base, uint32_t mask) |
kadonotakashi | 0:8fdf9a60065b | 150 | { |
kadonotakashi | 0:8fdf9a60065b | 151 | uint32_t regValue = base->GENCS & (~ALL_FLAGS_MASK); |
kadonotakashi | 0:8fdf9a60065b | 152 | |
kadonotakashi | 0:8fdf9a60065b | 153 | if (mask & kTSI_GlobalInterruptEnable) |
kadonotakashi | 0:8fdf9a60065b | 154 | { |
kadonotakashi | 0:8fdf9a60065b | 155 | regValue |= TSI_GENCS_TSIIEN_MASK; |
kadonotakashi | 0:8fdf9a60065b | 156 | } |
kadonotakashi | 0:8fdf9a60065b | 157 | if (mask & kTSI_OutOfRangeInterruptEnable) |
kadonotakashi | 0:8fdf9a60065b | 158 | { |
kadonotakashi | 0:8fdf9a60065b | 159 | regValue &= (~TSI_GENCS_ESOR_MASK); |
kadonotakashi | 0:8fdf9a60065b | 160 | } |
kadonotakashi | 0:8fdf9a60065b | 161 | if (mask & kTSI_EndOfScanInterruptEnable) |
kadonotakashi | 0:8fdf9a60065b | 162 | { |
kadonotakashi | 0:8fdf9a60065b | 163 | regValue |= TSI_GENCS_ESOR_MASK; |
kadonotakashi | 0:8fdf9a60065b | 164 | } |
kadonotakashi | 0:8fdf9a60065b | 165 | |
kadonotakashi | 0:8fdf9a60065b | 166 | base->GENCS = regValue; /* write value to register */ |
kadonotakashi | 0:8fdf9a60065b | 167 | } |
kadonotakashi | 0:8fdf9a60065b | 168 | |
kadonotakashi | 0:8fdf9a60065b | 169 | void TSI_DisableInterrupts(TSI_Type *base, uint32_t mask) |
kadonotakashi | 0:8fdf9a60065b | 170 | { |
kadonotakashi | 0:8fdf9a60065b | 171 | uint32_t regValue = base->GENCS & (~ALL_FLAGS_MASK); |
kadonotakashi | 0:8fdf9a60065b | 172 | |
kadonotakashi | 0:8fdf9a60065b | 173 | if (mask & kTSI_GlobalInterruptEnable) |
kadonotakashi | 0:8fdf9a60065b | 174 | { |
kadonotakashi | 0:8fdf9a60065b | 175 | regValue &= (~TSI_GENCS_TSIIEN_MASK); |
kadonotakashi | 0:8fdf9a60065b | 176 | } |
kadonotakashi | 0:8fdf9a60065b | 177 | if (mask & kTSI_OutOfRangeInterruptEnable) |
kadonotakashi | 0:8fdf9a60065b | 178 | { |
kadonotakashi | 0:8fdf9a60065b | 179 | regValue |= TSI_GENCS_ESOR_MASK; |
kadonotakashi | 0:8fdf9a60065b | 180 | } |
kadonotakashi | 0:8fdf9a60065b | 181 | if (mask & kTSI_EndOfScanInterruptEnable) |
kadonotakashi | 0:8fdf9a60065b | 182 | { |
kadonotakashi | 0:8fdf9a60065b | 183 | regValue &= (~TSI_GENCS_ESOR_MASK); |
kadonotakashi | 0:8fdf9a60065b | 184 | } |
kadonotakashi | 0:8fdf9a60065b | 185 | |
kadonotakashi | 0:8fdf9a60065b | 186 | base->GENCS = regValue; /* write value to register */ |
kadonotakashi | 0:8fdf9a60065b | 187 | } |
kadonotakashi | 0:8fdf9a60065b | 188 | |
kadonotakashi | 0:8fdf9a60065b | 189 | void TSI_ClearStatusFlags(TSI_Type *base, uint32_t mask) |
kadonotakashi | 0:8fdf9a60065b | 190 | { |
kadonotakashi | 0:8fdf9a60065b | 191 | uint32_t regValue = base->GENCS & (~ALL_FLAGS_MASK); |
kadonotakashi | 0:8fdf9a60065b | 192 | |
kadonotakashi | 0:8fdf9a60065b | 193 | if (mask & kTSI_EndOfScanFlag) |
kadonotakashi | 0:8fdf9a60065b | 194 | { |
kadonotakashi | 0:8fdf9a60065b | 195 | regValue |= TSI_GENCS_EOSF_MASK; |
kadonotakashi | 0:8fdf9a60065b | 196 | } |
kadonotakashi | 0:8fdf9a60065b | 197 | if (mask & kTSI_OutOfRangeFlag) |
kadonotakashi | 0:8fdf9a60065b | 198 | { |
kadonotakashi | 0:8fdf9a60065b | 199 | regValue |= TSI_GENCS_OUTRGF_MASK; |
kadonotakashi | 0:8fdf9a60065b | 200 | } |
kadonotakashi | 0:8fdf9a60065b | 201 | |
kadonotakashi | 0:8fdf9a60065b | 202 | base->GENCS = regValue; /* write value to register */ |
kadonotakashi | 0:8fdf9a60065b | 203 | } |