summaryrefslogtreecommitdiff
path: root/main.c
blob: 2040036455a82334e439ff90f8201e34d23f31b8 (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
/*
 *  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 "def.h"
#include "hardware.h"

static void InitPorts(void);
static void InitADC10(void);

static void GlobalInit(void);
static void start_conversion(void);
static void get_conversion_result(void);

volatile u16 Timer1;
volatile u16 SW1State;
volatile u16 SW2State;

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

#ifdef CAN_WAIT_TENSION

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

#else

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

#endif

enum {
  STOP         = 10,
  WAIT_VREF    = 15,
  WAIT_START   = 20,
  WAIT_ATX_OK  = 30,
  WAIT_V1P0    = 40,
  WAIT_V1P2    = 50,
  WAIT_V1P8    = 60,
  WAIT_RSMRST  = 70,
  CK410_VTT_GD = 80,
  CPU_RUN      = 90,
  WAIT_STOP    = 100,
};

int main(void)
{
  u16 state;

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

  __disable_interrupt();

  InitPorts();
  GlobalInit();
  InitADC10();

  __enable_interrupt();

  Timer1 = 30; // to allow Vref to settle
  state = WAIT_VREF;
  while (1) {
    switch (state) {
    case WAIT_VREF:
      if (Timer1 == 0) {
        start_conversion();
        state = WAIT_START;
      }
      break;

    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)) {
        // assertion: V1P2_CORE_EN_N is low but high impedance here
        // TO CHECK
        SetBit(P1DIR, 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 < 20) {
        SetBit(P2DIR, IMCH_RSMRST_N);
        ClrBit(P2OUT, IMCH_RSMRST_N);
        state = WAIT_V1P8;
      }
      break;

    case WAIT_V1P8:
      if (SW1State || TENSION_EXPIRED)
        state = STOP;
      if (TENSION_WAIT(bV1P8_DDR && V1P8_PGOOD)) {
        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 = 300;
        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 secondes
        state = STOP;
      break;

    case STOP:
      InitPorts();
      Timer1 = 10000; // Disable any other Power up for 10s.
      // We are very careful at first but we know no reason why this should
      // not work with an extremely lower timer. Under some conditions ATX
      // imposes debouncing up to at least 100 ms. I guess a good compromise
      // would be somewhere between 0.5 to 1s.
      ClrBit(P2OUT, RED_LED_N); // To show no restart is possible for now
      state = STOP + 1;
      break;
    case STOP + 1:
      InitPorts();
      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--;
}

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 InitADC10(void)
{
  // Ports are already initiated with correct values

  // ADC10SHT <- what kind of conversion time do we need?

  ADC10CTL0 = SREF_1 | ADC10SHT_2 | REFON | REF2_5V | ADC10ON | ADC10IE;
  // wait 30ms here

  ADC10AE0 = BIT5 | BIT6;
  ADC10AE1 = BIT4 | BIT5 | BIT6 | BIT7; // A12 A13 A14 A15
}

enum tensions {
  iVCC3,
  iV1P8_PGOOD,
  iV1P0,
  iV1P2,
  iV1P8_DDR,
  iV2P5,
  iTEMP
};

u16 inch[] = {
  [iVCC3]       = INCH_5,
  [iV1P8_PGOOD] = INCH_6,
  [iV1P0]       = INCH_12,
  [iV1P2]       = INCH_13,
  [iV1P8_DDR]   = INCH_14,
  [iV2P5]       = INCH_15,
  [iTEMP]       = INCH_10,
};

static unsigned cidx;

static void start_conversion(void)
{
  ADC10CTL1 = ADC10CTL1 & ~INCH_15 | inch[cidx];
  ADC10CTL0 |= ENC | ADC10SC;
}

#define check_tension(t, min, max) \
  case i ## t: \
    b ## t = ADC10MEM < min || ADC10MEM > max; \
    break;

static void get_conversion_result(void)
{
  switch (cidx) {
    /* See the ADC10 notes and InfosPowerSeq.ods for these values */
#ifdef BRIDGE_5_PERCENT
  check_tension(V1P0    , 180, 233)
  check_tension(V1P2    , 209, 288)
  check_tension(V1P8_DDR, 314, 432)
  check_tension(V2P5    , 435, 600)
  check_tension(VCC3    , 575, 792)
#else
  check_tension(V1P0    , 187, 224)
  check_tension(V1P2    , 218, 277)
  check_tension(V1P8_DDR, 327, 415)
  check_tension(V2P5    , 454, 577)
  check_tension(VCC3    , 599, 762)
#endif

  case iV1P8_PGOOD:
    bV1P8_PGOOD = ADC10MEM > 368;
    break;
  case iTEMP:
    break;
  }

  cidx = (cidx + 1) % ARRAY_SIZE(inch);
}

#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void)
{
  get_conversion_result();
  start_conversion();
}

static void GlobalInit(void)
{
  DCOCTL = CALDCO_12MHZ;
  BCSCTL1 = CALBC1_12MHZ;
  BCSCTL2 |= DIVS_3; // DIVS_3 => 1/8; SMCLK = 12/8 = 1.5Mhz

  // Set timer A so that Timer_A is called every 1.00266 ms (nominal)
  TACTL = TASSEL_2 + ID_3 + TACLR + TAIE + MC_1; // SMCLK + divise 8 + reset +
                                                 // enable intterupt + UP

  // 188/187500.0 = 0.001002666666666666
  TACCR0 = 188;
}

// vim: et:sw=2:sts=2