From 4b29843d692f778c602f8f04088245672a47208f Mon Sep 17 00:00:00 2001 From: Werner Johansson Date: Tue, 23 Dec 2014 01:17:23 -0800 Subject: [PATCH] Temperature-controlled system fan speed --- src/io.c | 2 +- src/main.c | 2 + src/sched.h | 2 + src/systemfan.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++ src/systemfan.h | 6 +++ src/t962.h | 2 +- 6 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 src/systemfan.c create mode 100644 src/systemfan.h diff --git a/src/io.c b/src/io.c index e8af3ea..209accb 100644 --- a/src/io.c +++ b/src/io.c @@ -67,7 +67,7 @@ void IO_Init(void) { FIO0MASK = 0b01001101000000100000010001100000; // Mask out all unknown/unused pins FIO1MASK = 0b11111111000000001111111111111111; // Only LCD D0-D7 - FIO0DIR = 0b10000000011011000011101100000001; // Default output pins + FIO0DIR = 0b10000010011011000011101100000001; // Default output pins FIO1DIR = 0b00000000000000000000000000000000; FIO0PIN = 0x00; // Turn LED on and make PWM outputs active when in GPIO mode (to help 100% duty cycle issue) diff --git a/src/main.c b/src/main.c index 0c597b8..d025789 100644 --- a/src/main.c +++ b/src/main.c @@ -35,6 +35,7 @@ #include "nvstorage.h" #include "version.h" #include "max31855.h" +#include "systemfan.h" extern uint8_t logobmp[]; extern uint8_t stopbmp[]; @@ -195,6 +196,7 @@ int main(void) { OneWire_Init(); SPI_TC_Init(); Reflow_Init(); + SystemFan_Init(); Sched_SetWorkfunc( MAIN_WORK, Main_Work ); Sched_SetState( MAIN_WORK, 1, TICKS_SECS( 2 ) ); // Enable in 2 seconds diff --git a/src/sched.h b/src/sched.h index d7fb987..2d85d79 100644 --- a/src/sched.h +++ b/src/sched.h @@ -20,12 +20,14 @@ typedef enum eTask { SLEEP_WORK=0, BUZZER_WORK, KEYPAD_WORK, + SYSFANPWM_WORK, MAIN_WORK, ADC_WORK, ONEWIRE_WORK, SPI_TC_WORK, UI_WORK, REFLOW_WORK, + SYSFANSENSE_WORK, NV_WORK, SCHED_NUM_ITEMS // Last value } Task_t; diff --git a/src/systemfan.c b/src/systemfan.c new file mode 100644 index 0000000..85f7214 --- /dev/null +++ b/src/systemfan.c @@ -0,0 +1,99 @@ +/* + * systemfan.c - Temperature controlled system fan handling for T-962 reflow controller + * + * Copyright (C) 2014 Werner Johansson, wj@unifiedengineering.se + * + * 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 . + */ + +/* + * This utilizes the (previously unused) ADO testpoint that is connected to + * GPIO0.25 to drive a N-ch/NPN transistor to control the (noisy) system fan. + * + * System fan connector pin 1 = GND, pin 2 = ~10VDC + * + * Disconnect fan from GND, connect this wire to transistor drain/collector. Also + * connect the anode of a catch diode here, cathode to pin 2 (~10VDC). + * Transistor source/emitter connects to ground, gate connects directly to ADO testpoint. + * If using a bipolar transistor connect base through a 4k7 resistor to ADO testpoint. + * + * Example transistors that works: 2N7000 (N-ch FET) or BC547B (bipolar NPN) + */ + +#include "LPC214x.h" +#include +#include +#include "systemfan.h" +#include "sched.h" +#include "reflow.h" + +#define SYSFAN_PWM_PERIOD (TICKS_MS( 10 )) + +uint32_t syspwmval = 0; + +static int32_t SystemFanPWM_Work( void ) { + static uint8_t state = 0; + int32_t retval; + + if( state ) { + FIO0CLR = (syspwmval != SYSFAN_PWM_PERIOD) ? (1<<25) : 0; // SysFan off + retval = syspwmval ? (SYSFAN_PWM_PERIOD - syspwmval) : -1; + } else { + FIO0SET = syspwmval ? (1<<25) : 0; // SysFan on + retval = (syspwmval != SYSFAN_PWM_PERIOD) ? syspwmval : -1; + } + state ^= 1; + return retval; +} + +static int32_t SystemFanSense_Work( void ) { + //static uint8_t lastsysfanspeed = 0; + uint8_t sysfanspeed = 0; + + if( Reflow_IsTempSensorValid( TC_COLD_JUNCTION ) ) { + float systemp = Reflow_GetTempSensor( TC_COLD_JUNCTION ); + + // Sort this out with something better at some point + if( systemp > 50.0f ) { + sysfanspeed = 0xff; + } else if( systemp > 45.0f ) { + sysfanspeed = 0xc0; + } else if( systemp > 42.0f ) { + sysfanspeed = 0x80; + } else if( systemp > 40.0f ) { + sysfanspeed = 0x50; + } + } else { + sysfanspeed = 0xff; // No sensor, run at full speed as a precaution + } + + uint32_t temp = SYSFAN_PWM_PERIOD >> 8; + temp *= sysfanspeed; + if( sysfanspeed == 0xff ) temp = SYSFAN_PWM_PERIOD; // Make sure we reach 100% duty cycle + syspwmval = temp; + + Sched_SetState( SYSFANPWM_WORK, 2, 0 ); + //lastsysfanspeed = sysfanspeed; + return TICKS_SECS( 5 ); +} + +void SystemFan_Init( void ) { + printf("\n%s",__FUNCTION__); + Sched_SetWorkfunc( SYSFANPWM_WORK, SystemFanPWM_Work ); + Sched_SetWorkfunc( SYSFANSENSE_WORK, SystemFanSense_Work ); + + syspwmval = SYSFAN_PWM_PERIOD; // Turn on fan briefly at boot to indicate that it actually works + Sched_SetState( SYSFANPWM_WORK, 2, 0 ); // Enable PWM task + Sched_SetState( SYSFANSENSE_WORK, 1, TICKS_SECS( 2 ) ); // Enable Sense task +} diff --git a/src/systemfan.h b/src/systemfan.h new file mode 100644 index 0000000..ef2c503 --- /dev/null +++ b/src/systemfan.h @@ -0,0 +1,6 @@ +#ifndef SYSTEMFAN_H_ +#define SYSTEMFAN_H_ + +void SystemFan_Init( void ); + +#endif /* SYSTEMFAN_H_ */ diff --git a/src/t962.h b/src/t962.h index e79fb3a..4b77716 100644 --- a/src/t962.h +++ b/src/t962.h @@ -33,7 +33,7 @@ * 0.22 LCD RS output (0=cmd, 1=data) * 0.23 F1 button (active low, with pullup) VBUS input as alt function on LPC214x * 0.24 (No pin) - * 0.25 AD0.4 accessible on test point marked ADO - DAC output as alt function + * 0.25 AD0.4 accessible on test point marked ADO - DAC output as alt function (now used for sysfan control) * 0.26 ? USB D+ on LPC214x-series of chips * 0.27 ? USB D- on LPC214x-series of chips * 0.28 Temperature sensor input 1 (AD0.1), this was the left-hand one in our oven