summaryrefslogtreecommitdiff
path: root/main.c
blob: a7179be2d958753948ba47b97d6f2c58f455a384 (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
/*
 *  XIOHV5.12 power sequence
 *  Copyright (C) 2012  Avencall
 *  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/>.
 */

#include <stdbool.h>

#include <io430.h>
#include <intrinsics.h>

#include <stdio.h>

#include "def.h"
#include "hardware.h"

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

volatile u16 TAVector;
volatile u16 Timer1;
volatile u16 Timer2;
volatile u16 SW1State;
volatile u16 SW2State;

#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

#define STOP             10
#define WAIT_START       20
#define WAIT_ATX_OK      30
#define WAIT_V1P0        40
#define WAIT_V1P2        50
#define WAIT_V1P8        60
#define WAIT_RSMRST      70
#define CK410_VTT_GD     80
#define STATE_SYS_PWR_OK 90
#define CPU_RUN          100
#define WAIT_STOP        110

#define RST_STATE        10
#define ON_STATE         20
#define RST_WAIT         30


// For UCOS16 = 1
// BRCLK        Baud Rate   UCBRx UCBRSx UCBRFx   Tx Error  Rx Error
// 12,000,000   115200          6      0      8     -1.8 0  -2.2 0.4
static void SerialInit(void)
{
  UCA0CTL1_bit.UCSWRST = 1;

  UCA0CTL0 = 0;
  UCA0CTL1 = 0xC1;  // UCSSEL=11b => SMCLK; (keep) UCSWRST = 1;
  UCA0BR0 = 6;
  UCA0BR1 = 0;
  UCA0MCTL = 0x81;  // UCOS16 = 1; UCBRSx = 0; UCBRFx = 8;
  UCA0STAT = 0;

  // Configure port for UART access
  // Pin 25 as UCA0TXD
  // Pin 26 as UCA0RXD
  P3SEL |= 0x30;

  UCA0CTL1_bit.UCSWRST = 0;
}


static void ll_putchar(unsigned char c)
{
  while (!IFG2_bit.UCA0TXIFG);
  UCA0TXBUF = c;
}


size_t __write(int handle, const unsigned char * buffer, size_t size)
{
  size_t nChars = 0;

  if (buffer == 0)
    return 0;

  for (; size != 0; --size) {
    ll_putchar(*buffer++);
    ++nChars;
  }

  return nChars;
}


int main(void)
{
  u16 state, resetState;

  WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer to prevent time out reset

  __disable_interrupt();

  InitPorts();
  GlobalInit();
  SerialInit();

  __enable_interrupt();

  state = WAIT_START;
  resetState = ON_STATE;
  while (1) {

    switch (resetState) { //////////////////////////////////////////////
    case ON_STATE:
      if (SW2State > 300) {
        resetState = RST_STATE;
      }
      break;

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

    case RST_WAIT:
      if (Timer2 == 0) {
         SetBit(P2OUT, SYS_RESET_N);
         if (SW2State == 0)
           resetState = ON_STATE;
      }
      break;
    }

    switch (state) { ///////////////////////////////////////////////////
    case WAIT_START:
      if (SW1State > 30)
        state = WAIT_START + 1;
      break;
    case WAIT_START + 1:
      if (SW1State == 0) {
        SetBit(P4OUT, CMDPWR); // Start Atx Power Supply
        Timer1 = 2000;
        state = WAIT_ATX_OK;
      }
      break;

    case WAIT_ATX_OK:
      if (SW1State || TENSION_EXPIRED)
        state = STOP;
      if ((P4IN & ATX_PWROK) && TENSION_WAIT(bV2P5 && bVCC3)) {
        ClrBit(P1OUT, V1P2_CORE_EN_N);
        Timer1 = 30;
        state = WAIT_V1P2;
      }
      break;

    case WAIT_V1P2:
      if (SW1State || TENSION_EXPIRED)
        state = STOP;
      if (TENSION_WAIT(bV1P2)) {
        Timer1 = 30;
        SetBit(P4OUT, V1P8_CMD);
        state = WAIT_RSMRST;
      }
      break;

    case WAIT_RSMRST:
      if (SW1State)
        state = STOP;
      if (Timer1 < 19) {
        SetBit(P2OUT, IMCH_RSMRST_N);
        state = WAIT_V1P8;
      }
      break;

    case WAIT_V1P8:
      if (SW1State || TENSION_EXPIRED)
        state = STOP;
      if (TENSION_WAIT(bV1P8)) {
        ClrBit(P2OUT, CPU_VCCP_EN_N);
        Timer1 = 30;
        state = WAIT_V1P0;
      }
      break;

    case WAIT_V1P0:
      if (SW1State || TENSION_EXPIRED)
        state = STOP;
      if (TENSION_WAIT(bV1P0)) {
        SetBit(P3OUT, VRMPWRGD);
        ClrBit(P2OUT, GREEN_LED_N);
        Timer1 = 3;
        state = CK410_VTT_GD;
      }
      break;

    case CK410_VTT_GD:
      if (Timer1 == 0) {
        SetBit(P2DIR, CK410_PWR_GD_N);
        ClrBit(P2OUT, CK410_PWR_GD_N);
        Timer1 = 105;
        state = STATE_SYS_PWR_OK;
      }
      break;

    case STATE_SYS_PWR_OK:
      if (Timer1 == 0) {
         SetBit(P3OUT, SYS_PWR_OK);
         Timer1 = 10;
         state = CPU_RUN;
      }
      break;

    case CPU_RUN:
      if (SW1State)
        state = STOP;
      if (Timer1 == 0) {
        // Start Tolapai: emulate the power button from its point of view
        // we don't really know where is the best place to do that so
        // we will try it here at first.
        SetBit(P2DIR, IMCH_PWRBTN_N);
        Timer1 = 200;
        state = CPU_RUN + 1;
      }
      break;
    case CPU_RUN + 1:
      if (SW1State)
        state = STOP;
      if (Timer1 == 0) {
        ClrBit(P2DIR, IMCH_PWRBTN_N);
        state = WAIT_STOP;
      }
      break;

    case WAIT_STOP:
      if (SW1State >= 6000) // Sw1 button pressed for more than 6 seconds
        state = STOP;
      break;

    case STOP:
      InitPorts();
      Timer1 = 500; // Disable any other Power up for 0.5 s.
      ClrBit(P2OUT, RED_LED_N); // To show no restart is possible for now
      state = STOP + 1;
      break;
    case STOP + 1:
      if (Timer1 == 0) {
        SetBit(P2OUT, RED_LED_N); // To show restart is possible now
        state = WAIT_START;
      }
      break;
    }
  }
}


#pragma vector = TIMERA1_VECTOR
__interrupt void Timer_A(void)
{
  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)
{
  DCOCTL = CALDCO_12MHZ;
  BCSCTL1 = CALBC1_12MHZ;

  TACTL = TASSEL_2 + ID_3 + TACLR + TAIE + MC_1; // SMCLK + div by 8 + reset +
                                                 // enable interrupt + UP

  // Calibrated DCO Frequencies - Tolerance Over Temperature 0C to 85C
  //
  //                                           Min Typ Max
  //             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

  // Timer_A called every:
  //
  // >>> 1539 / 1500000. * .975
  // 0.00100035                       1.000 ms min
  // >>> 1539 / 1500000.
  // 0.001026                         1.026 ms nom
  // >>> 1539 / 1500000. * 1.025
  // 0.00105165                       1.052 ms max

  TACCR0 = 1539;
}