summaryrefslogtreecommitdiffstats
path: root/boiler-monster/stm32/app/pressure.c
blob: f719e4d001e325e70d729c92b3745181bb13ab31 (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
#include "project.h"

#define PRESSURE      GPIO0
#define PRESSURE_PORT     GPIOA
#define PRESSURE_CHANNEL  ADC_CHANNEL0
#define VREF_CHANNEL ADC_CHANNEL17

#define N_SAMPLES 1000

#define T do { printf("%s:%d\r\n",__FILE__,__LINE__); } while (0)

static int pressure;

struct avg {
	int acc;
	unsigned n;
};

static void avg_reset(struct avg *a)
{
a->acc=0;
a->n=0;
return;
}
static void avg_add(struct avg *a, int v)
{
a->acc+=v;
a->n++;
}
inline static unsigned avg_n(struct avg *a)
{
return a->n;
}

static int avg_get(struct avg *a)
{
if (!a->n) return 0;
return (a->acc) / (a->n);
}


uint16_t pressure_ch (void)
{
  return pressure;
}


static void pressure_calculate (int v,int r)
{
  /* r is 1.25 volts, transducer is 0.5V -> 0 psi 4.5V -> 100psi  */
  /* 100psi is 6.8947573 bar, and we want 256ths of bar */


  if (!r) {
    pressure = 0;
    //  return;
  } else {
    pressure = ((v * 552) / r) - 221;

    if (pressure < 0) pressure = 0;
  }

  printf ("QP: %d %d %d\r\n", v, r, (pressure * 100) / 256);
}



void pressure_tick(void)
{
static unsigned state;
static unsigned backoff=MS_TO_TICKS(1000);
static unsigned timeout;

static struct avg vref_avg;
static struct avg pressure_avg;


timeout++;

if (backoff) {
	backoff--;
	return;
}


switch (state) {
case 0:
  adc_off (ADC1);
  adc_power_on (ADC1);
  backoff=MS_TO_TICKS(5);
  state++;
  break;
case 1:
  ADC_CR2 (ADC1) |= ADC_CR2_RSTCAL;
  backoff=MS_TO_TICKS(2);
  state++;
  break;
case 2:
  if (ADC_CR2(ADC1) & ADC_CR2_CAL)
	state=0;
  ADC_CR2 (ADC1) |= ADC_CR2_CAL;
  backoff=MS_TO_TICKS(2);
  state++;
  break;
case 3:
  if (ADC_CR2(ADC1) & ADC_CR2_CAL)
	state=0;
  state++;

  avg_reset(&vref_avg);
  avg_reset(&pressure_avg);
  break;
case 4:
  if (adc_convert_start(VREF_CHANNEL))
	state=0;
  else {
  timeout=0;
  state++;
  }
  break;
case 5:
  if (adc_convert_done()) {
  avg_add(&vref_avg,adc_convert_get());
  state++;
  } else if (timeout > MS_TO_TICKS(5)) 
	state=0;
  break;
case 6:
  if (adc_convert_start(PRESSURE_CHANNEL))
	state=0;
  else {
  timeout=0;
  state++;
  }
  break;
case 7:
  if (adc_convert_done()) {
  avg_add(&pressure_avg,adc_convert_get());

  if (avg_n(&pressure_avg)<N_SAMPLES)
	state=4;
  else
	state++;
  } else if (timeout > MS_TO_TICKS(5)) 
	state=0;
  break;
case 8:
  pressure_calculate(avg_get(&pressure_avg),avg_get(&vref_avg));
  state=0;
  break;
}

}



void pressure_init (void)
{
  MAP_ANALOG (PRESSURE);
}