-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperipherals.c
230 lines (187 loc) · 5.59 KB
/
peripherals.c
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
/*
* peripherals.c
*
* Author: Tuomas Vaherkoski <[email protected]>
*
* This file is part of variable-power-supply project.
*/
#include "peripherals.h"
#include "eventqueue.h"
#include "display.h"
#include <avr/interrupt.h>
#include <inttypes.h>
#include <avr/io.h>
#include <avr/eeprom.h>
/* PWM ---------------------------------------------------------------------- */
uint16_t voltage;
void init_voltage_pwm(void) {
// Waveform outputs
DDRB |= _BV(PB1);
/*
* WGM10 0 (TCCR1A)
* WGM11 1 (TCCR1A)
* WGM12 1 (TCCR1B)
* WGM13 1 (TCCR1B)
* Timer/Counter Mode of Operation = Fast PWM
* TOP = ICR1
* Update of OCR1x at BOTTOM
* TOV1 Flag Set on TOP
*/
TCCR1A |= _BV(COM1A1) | _BV(WGM11);
TCCR1B |= _BV(WGM12) | _BV(WGM13);
ICR1 = 1000;
set_voltage(read_eeprom_voltage());
// start
TCCR1B |= _BV(CS10);
}
void set_voltage(uint16_t set_voltage) {
unsigned int uplimit = 1060;
unsigned int downlimit = 125;
if(set_voltage > downlimit && set_voltage <= uplimit) {
OCR1A = set_voltage - 125;
voltage = set_voltage;
} else if (set_voltage > uplimit) {
OCR1A = uplimit - 125;
voltage = uplimit;
} else {
OCR1A = 0;
voltage = downlimit;
}
}
uint16_t* get_voltage() {
return &voltage;
}
/* ADC ---------------------------------------------------------------------- */
#define ADCREF11 1100
#define ADCREFVCC 5000
#define ADCREFINIT ADCREF11
volatile unsigned int adc_reference;
uint16_t display_current;
void init_adc(void) {
// 1.1V with external capacitor at AREF pin
// select ADC0
ADMUX |= _BV(REFS0) | _BV(REFS1);
// ADC-clk = 1MHz / 128 = 7812Hz
ADCSRA |= _BV(ADEN) | _BV(ADSC) | _BV(ADIE) |
_BV(ADPS0) | _BV(ADPS1) | _BV(ADPS2);
adc_reference = ADCREFINIT;
ADCSRA |= _BV(ADSC); // start new conversion
}
/*
* Gain = 13
* Rsense = 0.22ohm
* Vin = ADC * 1024 / Vref
* current = Vin / Gain / Rsense
*/
void current_handeler(uint16_t current) {
uint32_t scale = (uint32_t)current * adc_reference * 100 / 1024 / 13 / 22;
current = (uint16_t)scale;
if(current > *get_current_limit()) {
limit_current();
} else {
set_voltage(*get_voltage());
}
// TODO: tee fiksummin
// how often displayed current value is updated
// smaller value means faster update
static uint16_t display_update = 0;
if(display_update % 1000 == 0 && current > *get_current_limit()) {
status_led_toggle(LED_CURRENT);
}
if(display_update > 5000) {
display_current = current;
display_update = 0;
}
display_update++;
// select Vref of ADC
// 1.1V gives better resolution in lower currents (1mA)
// 5V reference gives resolution of 4mA
if(adc_reference < ADCREFVCC && current > 490) {
adc_reference = ADCREFVCC;
ADMUX &= ~(_BV(REFS1));
} else if ( adc_reference > ADCREF11 && current < 470) {
adc_reference = ADCREF11;
ADMUX |= _BV(REFS1);
}
}
/* ADC finished, result in ADC register */
ISR(ADC_vect) {
static float adc_reference_prev = ADCREFINIT;
// if reference changes, discard result
if(adc_reference_prev == adc_reference) {
evq_push(current_handeler, ADC);
} else {
adc_reference_prev = adc_reference;
}
ADCSRA |= _BV(ADSC); // start new conversion
}
uint16_t* get_current() {
return &display_current;
}
/* limits ------------------------------------------------------------------- */
uint16_t current_limit; // mA
void set_current_limit(uint16_t limit) {
if(limit >= 10 && limit < 3000) {
current_limit = limit;
} else if (limit < 10) {
current_limit = 10;
} else {
current_limit = 2999;
}
}
uint16_t* get_current_limit(void) {
return ¤t_limit;
}
void limit_current(void) {
// set PWM output => 0
OCR1A = 0;
}
/* EEPROM */
uint16_t EEMEM eeprom_voltage = 125;
uint16_t EEMEM eeprom_current_limit = 200;
void save_eeprom_current_limit(uint16_t current) {
eeprom_update_word(&eeprom_current_limit, *get_current_limit());
blink_led(LED_CURRENT, 200);
}
void save_eeprom_voltage(uint16_t voltage) {
eeprom_update_word(&eeprom_voltage, *get_voltage());
blink_led(LED_VOLTAGE, 200);
}
uint16_t read_eeprom_current_limit(void) {
return eeprom_read_word(&eeprom_current_limit);
}
uint16_t read_eeprom_voltage(void) {
return eeprom_read_word(&eeprom_voltage);
}
/* SPI ---------------------------------------------------------------------- */
#define spi_begin() PORTC &= ~(_BV(PC5));
#define spi_end() PORTC |= (_BV(PC5));
volatile uint16_t spi_data_word;
/* MOSI - PB3
* SCK - PB5
* RCK - PC5
*/
void init_spi(void) {
// SS (PB2) pitää olla output SPI väylän oikean toiminnan varmistamiseksi
DDRB |= _BV(DDB2) | _BV(DDB3) | _BV(DDB5); // SS, MOSI, SCK | output
DDRC |= _BV(DDC5);
SPCR |= _BV(SPE) | _BV(SPIE) | _BV(MSTR) | _BV(CPOL) | _BV(DORD);
SPSR |= _BV(SPI2X);
spi_data_word = 0;
}
void spi_send_word(uint16_t word) {
loop_until_bit_is_clear(SPSR, SPIF);
spi_begin();
spi_data_word = word;
// LSB first
SPDR = (0x00FF & spi_data_word);
}
ISR(SPI_STC_vect) {
if (spi_data_word > 0) {
// there's still data, send MSB
SPDR = (spi_data_word >> 8);
spi_data_word = 0;
} else {
spi_end(); // done
}
}