summaryrefslogtreecommitdiff
path: root/main.c
blob: a6a06be18766cc2912d0b2b24e82a73bd42201ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
/*
 *  XIOHV5.12 power sequence
 *  Copyright (C) 2012, 2013  Avencall
 *
 *  main.c
 *  Authors:
 *    Jean-Marc Ouvrard
 *    Noe Rubinstein
 *    Guillaume Knispel
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT:
 *
 *  - FOR OFFICIAL BUILDS: *** mspgcc 20120406 ***
 *
 *  - We also target (non-official builds only, less tested):
 *      IAR C/C++ Compiler V5.40.2.30380/W32
 *      TI MSP430 Optimizing C/C++ Compiler v 4.1.1
 *        (primarily in COFF mode, should also build in ELF mode)
 *
 *  - Unpatched mspgcc 20120406 from Debian Wheezy (4.6.3~mspgcc-20120406-3)
 *    does not contain a fix for
 *    "Assignment to chained volatiles can produce invalid assignment"
 *    http://sourceforge.net/tracker/?func=detail&aid=3559978&group_id=42303&atid=432701
 *    => DO NOT CHAIN ASSIGN VOLATILES (like in: vol2 = vol1 = &something;)
 *
 *  - Maintain UNEXPECTED_ISR_LIST (used when building with IAR and TI)
 *
 *  - Do not build with TI Compiler with an optimization level superior to O1:
 *    it results in both very poor code for switches, and some safety related
 *    checks optimized out.
 *
 *  - More generally, you should use the provided IAR / TI CCS projects and
 *    mspgcc Makefile, and you should refrain from modifying the build options.
 *
 *  - Regarding TI Compiler:
 *    * in COFF mode, global and static declaration are handled differently
 *      depending on whether they are explicitly initialized to zero in the
 *      source code or not (in violation of the C standard). Use explicit = 0
 *      initializations when needed.
 *    * in ELF mode, the compiler find it funny to define plain ints as
 *      32-bits ones. Given that the MSP430 is a 16-bits computer, you should
 *      not use plain ints unless you are forced to or don't care much about
 *      the produced code.
 *
 *  - Some compiler supports C99, others C90 with extensions. Only use simple
 *    constructs common to both C versions and never depend on subtleties.
 *
 *  - The mspgcc build uses the -funsigned-char option so plain chars behave
 *    like they do by default with IAR / TI compilers.
 */

/* Random notes:
 *
 *  - We have decided to remove the reset button and to keep the power button
 *    (v5 has both, v6 will only have the power button)
 *
 *    We should make sure that the power button works more reliably especially
 *    considering the ACPI specification and interactions with the EP80579
 *    standard ACPI subsystem. We should also debounce the On -> Off transition
 *    of the power button.
 */


#include "compiler.h"
#include "config.h"

 MASK_MISRA
#include <stdint.h>
#include <stdbool.h>
#include <msp430f2274.h>
#include <intrinsics.h>
 UNMASK_MISRA

#include "def.h"

#include "main.h"
#include "hardware.h"
#include "serial.h"


    ////////////////////////////////////////////////////////////////////


// UNEXPECTED_ISR_LIST *must* be manually maintained for IAR and TI
// (UNEXPECTED_ISR_LIST will not be used during an mspgcc build)
// Note that this does not include the vectors that are undefined on this chip.
#define UNEXPECTED_ISR_LIST						\
    PORT1_VECTOR,PORT2_VECTOR,ADC10_VECTOR,USCIAB0TX_VECTOR,		\
    USCIAB0RX_VECTOR,TIMERA0_VECTOR,WDT_VECTOR,TIMERB1_VECTOR,		\
    TIMERB0_VECTOR,NMI_VECTOR


    ////////////////////////////////////////////////////////////////////


static void InitPorts(void);
static void GlobalInit(void);

volatile u16 SoftWD = 0x7fff ^ SOFTWD_KEY;

volatile u16 TAVector;
volatile u16 Timer1 = 0;
volatile u16 Timer2 = 0;
volatile u16 SW1State = 0;
volatile u16 SW2State = 0;
volatile u16 Timer_A_count = 0;


#ifdef CAN_WAIT_TENSION

//has to be coded on real board
volatile u8 bV1P0 = true;
volatile u8 bV1P2 = true;
volatile u8 bV1P8_DDR = true;
volatile u8 bV2P5 = true;
volatile u8 bVCC3 = true;

#define TENSION_EXPIRED (Timer1 == 0)
#define TENSION_WAIT(t) (t)

#else

#define TENSION_EXPIRED (0)
#define TENSION_WAIT(t) (Timer1 == 0)

#endif /* CAN_WAIT_TENSION */


    ////////////////////////////////////////////////////////////////////


enum machine_state {
	MACHINE_OFF			= SV(0),
	WAIT_START_CMDPWR		= SV(1),
	WAIT_ATX_START_V1P2		= SV(2),
	WAIT_V1P2_START_V1P8		= SV(3),
	DEASSERT_RSMRST_N		= SV(4),
	WAIT_V1P8_START_V1P0		= SV(5),
	WAIT_V1P0_ASSERT_VRMPWRGD	= SV(6),
	ASSERT_CK410_PWR_GD_N		= SV(7),
	ASSERT_SYS_PWR_OK		= SV(8),
	PRESS_PWRBTN			= SV(9),
	RELEASE_PWRBTN			= SV(10),
	MACHINE_RUNNING			= SV(11),
	STOP_INHIBIT			= SV(12),
	STOP_FINAL			= SV(13),
};
#define STATE_UPPER_LIMIT	STOP_FINAL

enum reset_state {
	RST_NO		= SV(0),
	RST_PRESSED	= SV(1),
	RST_WAIT	= SV(2),
};
#define RESET_UPPER_LIMIT	RST_WAIT


    ////////////////////////////////////////////////////////////////////


u8 s3n_st = 0;
#define S3N_COUNT_MASK		0x7fu
#define S3N_DEB_MASK		0x80u
#define S3N_ASSERTED		(!(s3n_st & S3N_DEB_MASK))
#define S3N_DEASSERTED		(s3n_st & S3N_DEB_MASK)

/* NOTE: S3N_DEASSERT_MS must be > 1 for proper debouncing in all conditions */
#define S3N_DEASSERT_MS	  MS(2)
#define S3N_ASSERT_COUNT  2

static inline void init_s3n_st(void)
{
	s3n_st = Timer_A_count & S3N_COUNT_MASK;
}

static void update_s3n_st(void)
{
	if (S3N_ASSERTED) {
		if (!(P4IN & SLP_S3_N))
			init_s3n_st();
		else if (((Timer_A_count - s3n_st) & S3N_COUNT_MASK)
			 >= S3N_DEASSERT_MS)
			s3n_st = S3N_DEB_MASK;
	} else /* S3N_DEASSERTED */ {
		if (P4IN & SLP_S3_N) {
			s3n_st = S3N_DEB_MASK;
		} else {
			s3n_st++;
			if (s3n_st >= (S3N_DEB_MASK | S3N_ASSERT_COUNT))
				init_s3n_st();
		}
	}
}


    ////////////////////////////////////////////////////////////////////


// It seems that TI compiler does not honor inlines from ISR
#define reboot()	do { WDTCTL = 0; } while (1)

/* Note that different bit extents are used in PF_KEY and _PF_MAX */
#define PF_KEY		0xE0
// autoboot after power failure max _PF_MAX+1 times (if power seq unsuccessful)
#define _PF_MAX		0x03

// powerfail_recover MUST BE EXPLICITLY INITIALIZED TO 0.
// (The "= 0" must be present in the source code.)
// Initializing to 0 ensures that the board won't be unconditionally started
// on an MSP power up -- this shall never be done unconditionally and at the
// moment we have no code to check the conditions for which this would be safe.
u8 powerfail_recover = 0;	// THIS IS SAFETY-RELATED.

static inline u8 PF_COUNT(u8 v)
{ return v ^ PF_KEY; }
static inline bool PF_VALID(u8 v)
{ return (v == 0) || (PF_COUNT(v) <= _PF_MAX); }
#define PF_MAX		PF_COUNT(_PF_MAX)


#define change_state(ns)					\
	do {							\
		trace_state(ns);				\
		state = (ns);					\
	} while (0)


 MASK_MISRA
int main(void)
 UNMASK_MISRA
{
	u16 state, resetState;

#ifndef WATCHDOG
	WDTCTL = WDTPW | WDTHOLD;
#endif /* WATCHDOG */

	__disable_interrupt();

	GlobalInit();
	InitPorts();
#ifdef TRACE_SERIAL
	SerialInit();
#endif /* TRACE_SERIAL */

	__enable_interrupt();

	state = MACHINE_OFF;
	resetState = RST_NO;

	while (1) {

    ////////////////////////////////////////////////////////////////////
#ifdef TRACE_SERIAL // debug behavior

		switch (in_range(resetState, RESET_UPPER_LIMIT)) {
		case RST_NO:
			if (SW2State > MS(100)) {
				dump_trace();
				resetState = RST_WAIT;
			}
			break;
		case RST_WAIT:
			if (SW2State == 0)
				resetState = RST_NO;
			break;
		}

#else               // normal behavior

		if (out_range(resetState, RESET_UPPER_LIMIT)) {
			Timer2 = 0;
			resetState = RST_WAIT;
		}

		switch (in_range(resetState, RESET_UPPER_LIMIT)) {
		case RST_NO:
			if (SW2State > MS(300)) {
				resetState = RST_PRESSED;
			}
			break;

		case RST_PRESSED:
			ClrBit(P2REN, SYS_RESET_N);
			SetBit(P2DIR, SYS_RESET_N);
			ClrBit(P2OUT, SYS_RESET_N);
			Timer2 = MS(150);
			resetState = RST_WAIT;
			break;

		case RST_WAIT:
			if (Timer2 == 0) {
				SetBit(P2OUT, SYS_RESET_N);
				ClrBit(P2DIR, SYS_RESET_N);
				SetBit(P2REN, SYS_RESET_N);
				if (SW2State == 0)
					resetState = RST_NO;
			}
			break;
		}
#endif /* TRACE_SERIAL */

    ////////////////////////////////////////////////////////////////////

#if defined(TRACE_SERIAL) && defined(TEST_LOOP_SPEED)
		{
			u16 new_timer = Timer_A_count;
			u16 delta = new_timer - ref_Timer_A_count;
			if (delta) {
				if (tl_state > -2)
					tl_state--;
				if (tl_state == -1)
					tl_num = 0;
				if (tl_state == -2)
					tl_delta = delta;
				ref_Timer_A_count = new_timer;
			}
			if (tl_state == -1)
				tl_num++;
		}
#endif

		trace_changes(state);

		if (out_range(state, STATE_UPPER_LIMIT))
			reboot();

#define Xcase(c)	case c: ResetSoftWD();

		switch (in_range(state, STATE_UPPER_LIMIT)) {

		Xcase(MACHINE_OFF)
#ifdef LOOP_REBOOT
			change_state(WAIT_START_CMDPWR);
#endif
			if (powerfail_recover) {
				if (!PF_VALID(powerfail_recover))
					reboot();
				if (PF_COUNT(powerfail_recover))
					powerfail_recover--;
				else
					powerfail_recover = 0;
				change_state(WAIT_START_CMDPWR);
			} else if (SW1State > MS(30))
				change_state(WAIT_START_CMDPWR);
			break;

		Xcase(WAIT_START_CMDPWR)
			if (SW1State == 0) {
				SetBit(P4OUT, CMDPWR);
				// Start Atx Power Supply
				Timer1 = MS(2000);
				change_state(WAIT_ATX_START_V1P2);
			}
			break;

		Xcase(WAIT_ATX_START_V1P2)
			/* WARNING: no timeout when not monitoring tensions */
			if (SW1State || TENSION_EXPIRED)
				change_state(STOP_INHIBIT);
			if ((P4IN & ATX_PWROK)
			    && TENSION_WAIT(bV2P5 && bVCC3)) {
				ClrBit(P1OUT, V1P2_CORE_EN_N);
				Timer1 = MS(30);
				change_state(WAIT_V1P2_START_V1P8);
			}
			break;

		Xcase(WAIT_V1P2_START_V1P8)
			if (SW1State || TENSION_EXPIRED)
				change_state(STOP_INHIBIT);
			if (TENSION_WAIT(bV1P2)) {
				Timer1 = MS(30);
				SetBit(P4OUT, V1P8_CMD);
				change_state(DEASSERT_RSMRST_N);
			}
			break;

		Xcase(DEASSERT_RSMRST_N)
			if (SW1State)
				change_state(STOP_INHIBIT);
			if (Timer1 < MS(19)) {
				SetBit(P2OUT, IMCH_RSMRST_N);
				change_state(WAIT_V1P8_START_V1P0);
			}
			break;

		Xcase(WAIT_V1P8_START_V1P0)
			if (SW1State || TENSION_EXPIRED)
				change_state(STOP_INHIBIT);
			if (TENSION_WAIT(bV1P8)) {
				ClrBit(P2OUT, CPU_VCCP_EN_N);
				Timer1 = MS(30);
				change_state(WAIT_V1P0_ASSERT_VRMPWRGD);
			}
			break;

		Xcase(WAIT_V1P0_ASSERT_VRMPWRGD)
			if (SW1State || TENSION_EXPIRED)
				change_state(STOP_INHIBIT);
			if (TENSION_WAIT(bV1P0)) {
				SetBit(P3OUT, VRMPWRGD);
				ClrBit(P2OUT, GREEN_LED_N);
				Timer1 = MS(3);
				change_state(ASSERT_CK410_PWR_GD_N);
			}
			break;

		Xcase(ASSERT_CK410_PWR_GD_N)
			if (!(P4IN & ATX_PWROK))
				change_state(STOP_INHIBIT);
			if (Timer1 == 0) {
				SetBit(P2DIR, CK410_PWR_GD_N);
				ClrBit(P2OUT, CK410_PWR_GD_N);
				Timer1 = MS(105);
				change_state(ASSERT_SYS_PWR_OK);
			}
			break;

		Xcase(ASSERT_SYS_PWR_OK)
			if (!(P4IN & ATX_PWROK))
				change_state(STOP_INHIBIT);
			if (Timer1 == 0) {
				SetBit(P3OUT, SYS_PWR_OK);
				Timer1 = MS(10);
				change_state(PRESS_PWRBTN);
			}
			break;

		Xcase(PRESS_PWRBTN)
			if (!(P4IN & ATX_PWROK))
				change_state(STOP_INHIBIT);
			if (Timer1 == 0) {
				// Start the EP80579 by driving IMCH_PWRBTN_N.
				// Doing it here seems to be reliable.
				// Testing has shown variations on S3N:
				// in some cases it is only released after
				// IMCH_PWRBTN_N has been driven, proving
				// that this is necessary.
				SetBit(P2DIR, IMCH_PWRBTN_N);
				Timer1 = MS(50);
				init_s3n_st();
				change_state(RELEASE_PWRBTN);
			}
			break;

		Xcase(RELEASE_PWRBTN)
			if (!(P4IN & ATX_PWROK))
				change_state(STOP_INHIBIT);
			update_s3n_st();
			if ((Timer1 == 0) || S3N_DEASSERTED) {
				ClrBit(P2DIR, IMCH_PWRBTN_N);
#ifdef LOOP_REBOOT
				Timer1 = LOOP_REBOOT;
#endif
				change_state(MACHINE_RUNNING);
			}
			break;

		Xcase(MACHINE_RUNNING)
#ifdef LOOP_REBOOT
			if (Timer1 == 0)
				change_state(STOP_INHIBIT);
#endif
			powerfail_recover = PF_MAX;
			update_s3n_st();
			if ((SW1State >= MS(3800)) // approx 4 seconds
			|| S3N_ASSERTED) {
				powerfail_recover = 0;
				change_state(STOP_INHIBIT);
			} else if (!(P4IN & ATX_PWROK)) {
				change_state(STOP_INHIBIT);
			}
			break;

		Xcase(STOP_INHIBIT)
			InitPorts();
			// Disable any other Power up for 3 s:
			Timer1 = MS(3000);
			ClrBit(P2OUT, RED_LED_N);
			change_state(STOP_FINAL);
			break;

		Xcase(STOP_FINAL)
			if (SW1State > 0)
				powerfail_recover = 0;
			if ((Timer1 == 0) && (SW1State == 0)) {
				// Restart is possible now: all leds off
				SetBit(P2OUT, RED_LED_N);
				change_state(MACHINE_OFF);
			}
			break;
		}
	}
}


#ifndef MY_GCC
#pragma vector = UNEXPECTED_ISR_LIST
#endif
__interrupt void _unexpected_(void)
{
	reboot();
}


#ifdef MY_GCC
void _endless_loop__(void)
{
	reboot();
}
#endif /* MY_GCC */


#pragma vector = TIMERA1_VECTOR
__interrupt void Timer_A(void)
{
#ifdef WATCHDOG
	if ((SoftWD ^ SOFTWD_KEY) > MAX_SOFTWD)
		reboot();
	else
		SoftWD--;

	/* ACLK (VLO) /64 => T belongs to [0.0032; 0.016] s */
	WDTCTL = WDTPW | WDTCNTCL | WDTSSEL | WDTIS1 | WDTIS0;
#endif /* WATCHDOG */

	Timer_A_count++;

	if (!(P1IN & START_SW1_N))
		SW1State++;
	else
		SW1State = 0;

	if (!(P1IN & RST_SW2_N))
		SW2State++;
	else
		SW2State = 0;

	if (Timer1)
		Timer1--;

	if (Timer2)
		Timer2--;

	TAVector = TAIV;
}


static void InitPorts(void)
{
	/* DIR: direction: 0 input 1 output
	 * SEL: function: 0 gpio
	 * REN: resistor enabled
	 * OUT: output when REN=0 and DIR=1
	 *      0 pull-down 1 pull-up if REN=1
	 * IES: Interrupt Edge Select
	 * IE:  Interrupt Enable
	 */

	P1DIR = P1DIR_INIT;
	P1SEL = P1SEL_INIT;
	P1REN = P1REN_INIT;
	P1OUT = P1OUT_INIT;
	P1IES = P1IES_INIT;
	P1IE = P1IE_INIT;

	P2OUT = P2OUT_INIT;
	P2SEL = P2SEL_INIT;
	P2REN = P2REN_INIT;
	P2DIR = P2DIR_INIT;
	P2IES = P2IES_INIT;
	P2IE = P2IE_INIT;

	P3OUT = P3OUT_INIT;
	P3SEL = P3SEL_INIT;
	P3DIR = P3DIR_INIT;

	P4OUT = P4OUT_INIT;
	P4SEL = P4SEL_INIT;
	P4DIR = P4DIR_INIT;
	P4REN = P4REN_INIT;
}


static void GlobalInit(void)
{
	/******** MCLK SMCLK ACLK Configuration ********/

	/* Freq selection procedure compliant with errata BCL12 at startup
	 * (potentially switching from RSEL < 12 to > 13)
	 * Note that we do not even need this workaround with an 8MHz freq
	 * for the DCO (RSEL == 13). However, we might return to a 12MHz freq
	 * on the v6.6 board, so this sequence shall not be changed.
	 */
	BCSCTL2 = DIVS_2 | DIVM_2; /* MCLK and SMCLK Divider: /4 */
	DCOCTL = 0;

	/* LFXT1 low freq (to allow for VLO), ACLK will be /1 */
	BCSCTL1 = CALBC1_8MHZ & ~(XTS | DIVA0 | DIVA1);
	DCOCTL = CALDCO_8MHZ;

	BCSCTL3 = LFXT1S_2 /* VLO */;

	/* Status here:
	 *    DCO: 8MHz nominal.
	 *    MCLK and SMCLK: DCO/4 = 2MHz nominal.
	 *    ACLK from VLOCLK: 4 -> 20kHz.
	 */

#ifdef WATCHDOG
	/******** Watchdog early configuration ********/

	                           /* ACLK   /8192 */
	WDTCTL = WDTPW + WDTCNTCL + WDTSSEL + WDTIS0;
	/* This initial WDT will expire in 0.4 to 2.1 s
	 * Note that it will be reconfigured to expire with shorter intervals
	 * starting from the next clearing.
	 */
#endif /* WATCHDOG */


	/********  Soft Watchdog ********/

	SoftWD = MAX_SOFTWD ^ SOFTWD_KEY;


	/********  Timer A configuration ********/

		// SMCLK | div by 1 | reset | enable interrupt | UP
	TACTL = TASSEL_2 | ID_0     | TACLR | TAIE             | MC_1; 

/* =======================================
 *
 * Calibrated DCO Frequencies - Tolerance Over Temperature 0C to 85C
 *                                              Min Typ Max
 *             BCSCTL1 = CALBC1_8MHZ,  2.2 V   7.76  8  8.4
 * fCAL(8MHz)  DCOCTL = CALDCO_8MHZ,    3 V    7.8   8  8.2   MHz
 *             Gating time: 5 ms       3.6 V   7.6   8  8.24
 *
 *             BCSCTL1 = CALBC1_12MHZ, 2.2 V   11.7 12 12.3
 * fCAL(12MHz) DCOCTL = CALDCO_12MHZ,   3 V    11.7 12 12.3   MHz
 *             Gating time: 5 ms       3.6 V   11.7 12 12.3
 *
 * Currently used values 7.6 -> 8.4 MHz = 8 MHz +/- 5%
 * Set Timer_A to 1 ms period min :
 * 
 * ==========================================

from math import ceil

def taccr0(min_freq, nom_freq, max_freq, divisor, min_target_period):
    min_div_freq = float(min_freq) / divisor
    nom_div_freq = float(nom_freq) / divisor
    max_div_freq = float(max_freq) / divisor
    count = ceil(max_div_freq * min_target_period)
    minp = (count / max_div_freq) * 1000.
    nomp = (count / nom_div_freq) * 1000.
    maxp = ((count + 1) / min_div_freq) * 1000.
    print "\t/""* (TA16 errata => + 1) *""/"
    print "\tTACCR0 =", int(count), "+ 1;\t" + \
         ("/""* T: min=%.3f nom=%.3f max=%.3f ms *""/") % (minp, nomp, maxp)

taccr0(7600000, 8000000, 8400000, 4, 0.001)

 * ========================================== */

/* !!!WARNING!!! CHECK FOR 1 MS PERIOD */
#if MS(100) != 100
# error change definition of MS in config.h
#endif
	/* (TA16 errata => + 1) */
	TACCR0 = 2100 + 1;	/* T: min=1.000 nom=1.050 max=1.106 ms */
}