4 Copyright 2008-2012 Michel Pollet <buserror@gmail.com>
6 This file is part of simavr.
8 simavr is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 simavr is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with simavr. If not, see <http://www.gnu.org/licenses/>.
31 #include "thermistor.h"
34 * called the ADC could use a new value
35 * The value returned is NOT in "ADC" mode, it's in millivolts
37 static void thermistor_in_hook(struct avr_irq_t * irq, uint32_t value, void * param)
39 thermistor_p p = (thermistor_p)param;
40 avr_adc_mux_t v = *((avr_adc_mux_t*)&value);
42 // printf("%s(%2d/%2d)\n", __func__, p->adc_mux_number, v.src);
44 if (v.src != p->adc_mux_number)
48 for (int ei = 0; ei < p->table_entries; ei++, t += 2) {
49 if (t[1] <= p->current) {
50 // printf("%s(%2d) %.2f matches %3dC is %d adc\n", __func__, v.src,
51 // p->current, t[1], t[0] / p->oversampling);
52 avr_raise_irq(p->irq + IRQ_TERM_ADC_VALUE_OUT,
53 ((t[0] / p->oversampling) * 5000) / 0x3ff);
57 printf("%s(%d) temperature out of range (%.2f), we're screwed\n",
58 __func__, p->adc_mux_number, p->current);
61 static const char * irq_names[IRQ_TERM_COUNT] = {
62 [IRQ_TERM_ADC_TRIGGER_IN] = "8<thermistor.trigger",
63 [IRQ_TERM_TEMP_VALUE_OUT] = "16>thermistor.out",
77 p->irq = avr_alloc_irq(&avr->irq_pool, 0, IRQ_TERM_COUNT, irq_names);
78 avr_irq_register_notify(p->irq + IRQ_TERM_ADC_TRIGGER_IN, thermistor_in_hook, p);
80 p->oversampling = oversampling;
82 p->table_entries = table_entries;
83 p->adc_mux_number = adc_mux_number;
84 p->current = p->target = start_temp;
86 avr_irq_t * src = avr_io_getirq(p->avr, AVR_IOCTL_ADC_GETIRQ, ADC_IRQ_OUT_TRIGGER);
87 avr_irq_t * dst = avr_io_getirq(p->avr, AVR_IOCTL_ADC_GETIRQ, adc_mux_number);
89 avr_connect_irq(src, p->irq + IRQ_TERM_ADC_TRIGGER_IN);
90 avr_connect_irq(p->irq + IRQ_TERM_ADC_VALUE_OUT, dst);
92 printf("%s on ADC %d start %.2f\n", __func__, adc_mux_number, p->current);