11 #include PLATFORM_HEADER
28 #define SLOWRC_PERIOD_SETTLE_TIME 4250
38 #define SLOWRC_PERIOD_SAMPLES 8
59 #define CLK1K_NUMERATOR 384000000
68 stSerialPrintf(ST_ASSERT_SERIAL_PORT,
"halInternalCalibrateSlowRc:\r\n");
79 CLKRC_TUNE = CLKRC_TUNE_RESET;
80 CLK1K_CAL = CLK1K_CAL_RESET;
85 stSerialPrintf(ST_ASSERT_SERIAL_PORT,
86 "period: %u, ", CLK_PERIOD);
87 stSerialPrintf(ST_ASSERT_SERIAL_PORT,
"%u Hz\r\n",
88 ((int16u)(((int32u)192000000)/((int32u)CLK_PERIOD))));
103 delta = (19200+400) - period;
113 stSerialPrintf(ST_ASSERT_SERIAL_PORT,
"TUNE steps delta: %d\r\n",
120 stSerialPrintf(ST_ASSERT_SERIAL_PORT,
121 "period: %u, ", CLK_PERIOD);
122 stSerialPrintf(ST_ASSERT_SERIAL_PORT,
"%u Hz\r\n",
123 ((int16u)(((int32u)192000000)/((int32u)CLK_PERIOD))));
133 for(i=0;i<SLOWRC_PERIOD_SAMPLES;i++) {
135 average += CLK_PERIOD;
138 average = (average+(SLOWRC_PERIOD_SAMPLES/2))/SLOWRC_PERIOD_SAMPLES;
140 stSerialPrintf(ST_ASSERT_SERIAL_PORT,
"average: %u, %u Hz\r\n",
141 ((int16u)average), ((int16u)(((int32u)192000000)/((int32u)average))));
145 CLK1K_CAL = (int16u)(CLK1K_NUMERATOR/average);
147 stSerialPrintf(ST_ASSERT_SERIAL_PORT,
"CLK1K_CAL=%2X\r\n",CLK1K_CAL);
152 stSerialPrintf(ST_ASSERT_SERIAL_PORT,
"DONE\r\n");
162 #define FASTRC_PERIOD_SETTLE_TIME 128
170 int32s newTune = -16;
173 stSerialPrintf(ST_ASSERT_SERIAL_PORT,
"halInternalCalibrateFastRc:\r\n");
184 OSCHF_TUNE = newTune;
188 stSerialPrintf(ST_ASSERT_SERIAL_PORT,
189 "period: %u, ", CLK_PERIOD);
190 stSerialPrintf(ST_ASSERT_SERIAL_PORT,
"%u kHz\r\n",
191 ((int16u)((((int32u)3072000000)/((int32u)CLK_PERIOD))/1000)));
199 for(;newTune<16;newTune++) {
201 OSCHF_TUNE = newTune;
205 if(CLK_PERIOD>=256) {
211 stSerialPrintf(ST_ASSERT_SERIAL_PORT,
212 "period: %u, ", CLK_PERIOD);
213 stSerialPrintf(ST_ASSERT_SERIAL_PORT,
"%u kHz\r\n",
214 ((int16u)((((int32u)3072000000)/((int32u)CLK_PERIOD))/1000)));
261 #define OSC24M_BIASTRIM_OFFSET (0x2)
262 #define OSC24M_BIASTRIM_MIN (0+OSC24M_BIASTRIM_OFFSET)
263 #define OSC24M_BIASTRIM_MAX OSC24M_BIASTRIM_OSC24M_BIAS_TRIM_MASK
264 #define OSC24M_BIASTRIM_MSB (1 << (OSC24M_BIASTRIM_OSC24M_BIAS_TRIM_BITS-1))
265 #define OSC24M_BIASTRIM_UNINIT (0xFFFF)
266 tokTypeMfgOsc24mBiasTrim biasTrim=OSC24M_BIASTRIM_UNINIT;
308 if((OSC24M_COMP & OSC24M_HI) == OSC24M_HI) {
313 if((OSC24M_COMP & OSC24M_LO) != OSC24M_LO) {
316 if(biasTrim < (OSC24M_BIASTRIM_MAX - OSC24M_BIASTRIM_OFFSET)) {
317 biasTrim += OSC24M_BIASTRIM_OFFSET;
319 biasTrim = OSC24M_BIASTRIM_MAX;
324 if(biasTrim<OSC24M_BIASTRIM_OFFSET) {
325 biasTrim = OSC24M_BIASTRIM_OFFSET;
328 OSC24M_BIASTRIM = biasTrim;
331 static boolean setBiasCheckLow(
void)
333 OSC24M_BIASTRIM = biasTrim;
335 return ((OSC24M_COMP & OSC24M_LO) == OSC24M_LO);
345 OSC24M_CTRL = OSC24M_CTRL_OSC24M_EN;
350 bit = (OSC24M_BIASTRIM_MSB << 1);
355 if(setBiasCheckLow()) {
361 if((OSC24M_COMP & OSC24M_LO) != OSC24M_LO) {
366 if(biasTrim < (OSC24M_BIASTRIM_MAX - OSC24M_BIASTRIM_OFFSET)) {
367 biasTrim += OSC24M_BIASTRIM_OFFSET;
369 biasTrim = OSC24M_BIASTRIM_MAX;
381 static void halInternalConfigXtal24MhzFlashAccess(
void)
385 #
if defined(CORTEXM3_STM32W108)
386 FLASH_ACCESS = (FLASH_ACCESS_PREFETCH_EN |
387 (1<<FLASH_ACCESS_CODE_LATENCY_BIT));
401 boolean setTrimOneLastTime =
FALSE;
409 if(biasTrim == OSC24M_BIASTRIM_UNINIT) {
410 halCommonGetMfgToken(&biasTrim, TOKEN_MFG_OSC24M_BIAS_TRIM);
411 if(biasTrim == 0xFFFF) {
418 OSC24M_CTRL = OSC24M_CTRL_OSC24M_EN;
423 loSet = setBiasCheckLow();
424 hiSet = (OSC24M_COMP & OSC24M_HI) == OSC24M_HI;
434 if(biasTrim>OSC24M_BIASTRIM_MIN) {
436 setTrimOneLastTime =
TRUE;
441 }
while( (hiSet || !loSet) &&
442 (biasTrim<OSC24M_BIASTRIM_MAX) &&
443 (biasTrim>OSC24M_BIASTRIM_MIN) );
449 if(!loSet || (biasTrim<OSC24M_BIASTRIM_OFFSET)){
450 if(biasTrim < (OSC24M_BIASTRIM_MAX - OSC24M_BIASTRIM_OFFSET)) {
451 biasTrim += OSC24M_BIASTRIM_OFFSET;
453 biasTrim = OSC24M_BIASTRIM_MAX;
455 setTrimOneLastTime =
TRUE;
458 if(setTrimOneLastTime) {
464 OSC24M_CTRL |= OSC24M_CTRL_OSC24M_SEL;
470 CPU_CLKSEL = CPU_CLKSEL_FIELD;
472 halInternalConfigXtal24MhzFlashAccess();