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 when a byte is send via the uart on the AVR
36 static void thermistor_in_hook(struct avr_irq_t * irq, uint32_t value, void * param)
38 thermistor_p p = (thermistor_p)param;
39 avr_adc_mux_t v = *((avr_adc_mux_t*)&value);
41 // printf("%s(%2d/%2d)\n", __func__, p->adc_mux_number, v.src);
43 if (v.src != p->adc_mux_number)
47 for (int ei = 0; ei < p->table_entries; ei++, t += 2) {
48 if (t[1] < p->current) {
49 printf("%s(%2d) %.2f matches %3dC is %d adc\n", __func__, v.src,
50 p->current, t[1], t[0] / p->oversampling);
51 avr_raise_irq(p->irq + IRQ_TERM_ADC_VALUE_OUT, t[0] / p->oversampling);
55 printf("%s(%d) temperature out of range (%.2f), we're screwed\n",
56 __func__, p->adc_mux_number, p->current);
59 static const char * irq_names[IRQ_TERM_COUNT] = {
60 [IRQ_TERM_ADC_TRIGGER_IN] = "8<thermistor.trigger",
61 [IRQ_TERM_TEMP_VALUE_OUT] = "16>thermistor.out",
75 p->irq = avr_alloc_irq(&avr->irq_pool, 0, IRQ_TERM_COUNT, irq_names);
76 avr_irq_register_notify(p->irq + IRQ_TERM_ADC_TRIGGER_IN, thermistor_in_hook, p);
78 p->oversampling = oversampling;
80 p->table_entries = table_entries;
81 p->adc_mux_number = adc_mux_number;
82 p->current = p->target = start_temp;
84 avr_irq_t * src = avr_io_getirq(p->avr, AVR_IOCTL_ADC_GETIRQ, ADC_IRQ_OUT_TRIGGER);
85 avr_irq_t * dst = avr_io_getirq(p->avr, AVR_IOCTL_ADC_GETIRQ, adc_mux_number);
87 avr_connect_irq(src, p->irq + IRQ_TERM_ADC_TRIGGER_IN);
88 avr_connect_irq(p->irq + IRQ_TERM_ADC_VALUE_OUT, dst);
90 printf("%s on ADC %d start %.2f\n", __func__, adc_mux_number, p->current);