micro-common-internal.c
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include PLATFORM_HEADER
00012 #include "error.h"
00013 #include "hal/micro/micro-common.h"
00014 #include "hal/micro/cortexm3/micro-common.h"
00015 #include "hal/micro/cortexm3/mfg-token.h"
00016
00017 #define HAL_STANDALONE
00018 #ifdef HAL_STANDALONE
00019
00020 #define AUXADC_REG (0xC0u)
00021 #define DUMMY 0
00022
00023 #define ADC_6MHZ_CLOCK 0
00024 #define ADC_1MHZ_CLOCK 1
00025
00026 #define ADC_SAMPLE_CLOCKS_32 0
00027 #define ADC_SAMPLE_CLOCKS_64 1
00028 #define ADC_SAMPLE_CLOCKS_128 2
00029 #define ADC_SAMPLE_CLOCKS_256 3
00030 #define ADC_SAMPLE_CLOCKS_512 4
00031 #define ADC_SAMPLE_CLOCKS_1024 5
00032 #define ADC_SAMPLE_CLOCKS_2048 6
00033 #define ADC_SAMPLE_CLOCKS_4096 7
00034
00035 #define CAL_ADC_CHANNEL_VDD_4 0x00 //VDD_PADS/4
00036 #define CAL_ADC_CHANNEL_VREG_2 0x01 //VREG_OUT/2
00037 #define CAL_ADC_CHANNEL_TEMP 0x02
00038 #define CAL_ADC_CHANNEL_GND 0x03
00039 #define CAL_ADC_CHANNEL_VREF 0x04
00040 #define CAL_ADC_CHANNEL_I 0x06
00041 #define CAL_ADC_CHANNEL_Q 0x07
00042 #define CAL_ADC_CHANNEL_ATEST_A 0x09
00043
00044 void stCalibrateVref(void)
00045 {
00046
00047
00048
00049 extern int16u stmRadioTxPowerMode;
00050 tokTypeMfgAnalogueTrimBoth biasTrim;
00051
00052 halCommonGetMfgToken(&biasTrim, TOKEN_MFG_ANALOG_TRIM_BOTH);
00053
00054 if(biasTrim.auxadc == 0xFFFF) {
00055 assert(FALSE);
00056 } else {
00057
00058 int16u temp_value;
00059 int16u mask = 0xFFFF;
00060
00061
00062
00063 while (SCR_BUSY_REG) ;
00064
00065 SCR_ADDR_REG = AUXADC_REG ;
00066
00067
00068 SCR_CTRL_REG = SCR_CTRL_SCR_READ_MASK;
00069 SCR_CTRL_REG = 0;
00070
00071
00072 while (SCR_BUSY_REG) ;
00073
00074 temp_value = SCR_READ_REG & ~mask;
00075 temp_value |= biasTrim.auxadc & mask;
00076
00077 SCR_WRITE_REG = temp_value;
00078
00079
00080 SCR_CTRL_REG = SCR_CTRL_SCR_WRITE_MASK;
00081 SCR_CTRL_REG = 0;
00082
00083 while (SCR_BUSY_REG) ;
00084
00085 }
00086 }
00087
00088
00089 void calDisableAdc(void) {
00090
00091 CAL_ADC_CONFIG &= ~CAL_ADC_CONFIG_CAL_ADC_EN;
00092 }
00093
00094
00095
00096
00097
00098
00099 StStatus calStartAdcConversion(int8u dummy1,
00100 int8u dummy2,
00101 int8u channel,
00102 int8u rate,
00103 int8u clock) {
00104
00105 INT_MGMTCFG &= ~INT_MGMTCALADC;
00106
00107 ATOMIC(
00108
00109 CAL_ADC_CONFIG =((CAL_ADC_CONFIG_CAL_ADC_EN) |
00110 (channel << CAL_ADC_CONFIG_CAL_ADC_MUX_BIT) |
00111 (rate << CAL_ADC_CONFIG_CAL_ADC_RATE_BIT) |
00112 (clock << CAL_ADC_CONFIG_CAL_ADC_CLKSEL_BIT) );
00113
00114
00115
00116 INT_MGMTFLAG = INT_MGMTCALADC;
00117 )
00118 return ST_SUCCESS;
00119 }
00120
00121
00122 StStatus calReadAdcBlocking(int8u dummy,
00123 int16u *value) {
00124
00125 while ( ! (INT_MGMTFLAG & INT_MGMTCALADC) );
00126
00127 INT_MGMTFLAG = INT_MGMTCALADC;
00128
00129 *value = (int16u)CAL_ADC_DATA;
00130 return ST_SUCCESS;
00131 }
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141 int16u stMeasureVddFast(void)
00142 {
00143 int16u value;
00144 int32u Ngnd;
00145 int32u Nreg;
00146 int32u Nvdd;
00147 tokTypeMfgRegVoltage1V8 vregOutTok;
00148 halCommonGetMfgToken(&vregOutTok, TOKEN_MFG_1V8_REG_VOLTAGE);
00149
00150
00151 calStartAdcConversion(DUMMY,
00152 DUMMY,
00153 CAL_ADC_CHANNEL_GND,
00154 ADC_SAMPLE_CLOCKS_128,
00155 ADC_6MHZ_CLOCK);
00156 calReadAdcBlocking(DUMMY, &value);
00157 Ngnd = (int32u)value;
00158
00159
00160 calStartAdcConversion(DUMMY,
00161 DUMMY,
00162 CAL_ADC_CHANNEL_VREG_2,
00163 ADC_SAMPLE_CLOCKS_128,
00164 ADC_6MHZ_CLOCK);
00165 calReadAdcBlocking(DUMMY, &value);
00166 Nreg = (int32u)value;
00167
00168
00169 calStartAdcConversion(DUMMY,
00170 DUMMY,
00171 CAL_ADC_CHANNEL_VDD_4,
00172 ADC_SAMPLE_CLOCKS_128,
00173 ADC_6MHZ_CLOCK);
00174 calReadAdcBlocking(DUMMY, &value);
00175 Nvdd = (int32u)value;
00176
00177 calDisableAdc();
00178
00179
00180
00181
00182
00183
00184 if(vregOutTok == 0xFFFF) {
00185 vregOutTok = 1800;
00186 } else {
00187 vregOutTok /= 10;
00188 }
00189 return ((((((Nvdd-Ngnd)<<16)/(Nreg-Ngnd))*vregOutTok)*2)>>16);
00190 }
00191 #endif
00192
00193 void halCommonCalibratePads(void)
00194 {
00195 if(stMeasureVddFast() < 2700) {
00196 GPIO_DBGCFG |= GPIO_DBGCFGRSVD;
00197 } else {
00198 GPIO_DBGCFG &= ~GPIO_DBGCFGRSVD;
00199 }
00200 }
00201
00202
00203 void halInternalSetRegTrim(boolean boostMode)
00204 {
00205 tokTypeMfgRegTrim regTrim;
00206 int8u trim1V2;
00207 int8u trim1V8;
00208
00209 halCommonGetMfgToken(®Trim, TOKEN_MFG_REG_TRIM);
00210
00211
00212
00213 trim1V8 = regTrim.regTrim1V8;
00214 trim1V2 = regTrim.regTrim1V2;
00215
00216
00217
00218 if((trim1V2 == 0xFF) && (trim1V8 == 0xFF)) {
00219 trim1V8 = 4;
00220 trim1V2 = 0;
00221 }
00222
00223
00224 if(boostMode) {
00225 trim1V8 += 2;
00226 }
00227
00228
00229
00230 if(trim1V8>7) {
00231 trim1V8 = 7;
00232 }
00233 if(trim1V2>7) {
00234 trim1V2 = 7;
00235 }
00236
00237 VREG_REG = ( (trim1V8<<VREG_VREG_1V8_TRIM_BIT) |
00238 (trim1V2<<VREG_VREG_1V2_TRIM_BIT) );
00239 }
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251 void halCommonDelayMicroseconds(int16u us)
00252 {
00253 int32u beginTime = ReadRegister(MAC_TIMER);
00254
00255
00256
00257
00258 if((OSC24M_CTRL&OSC24M_CTRL_OSC24M_SEL)!=OSC24M_CTRL_OSC24M_SEL) {
00259 us >>= 1;
00260 }
00261
00262
00263 if(us<=2) {
00264 return;
00265 }
00266
00267
00268
00269 MAC_TIMER_CTRL |= MAC_TIMER_CTRL_MAC_TIMER_EN;
00270
00271
00272
00273
00274
00275 while( ((MAC_TIMER-beginTime)&MAC_TIMER_MAC_TIMER_MASK) < us ) {
00276 ;
00277 }
00278 }
00279
00280
00281
00282
00283
00284
00285 void halCommonDelayMilliseconds(int16u ms)
00286 {
00287 if(ms==0) {
00288 return;
00289 }
00290
00291 while(ms-->0) {
00292 halCommonDelayMicroseconds(1000);
00293 }
00294 }