+++ /dev/null
-# Hey Emacs, this is a -*- makefile -*-
-
-# AVR-GCC Makefile template, derived from the WinAVR template (which
-# is public domain), believed to be neutral to any flavor of "make"
-# (GNU make, BSD make, SysV make)
-
-
-MCU = attiny441
-FORMAT = ihex
-TARGET = PowerSwitch
-SRC = $(TARGET).c
-ASRC =
-OPT = s
-
-# Name of this Makefile (used for "make depend").
-MAKEFILE = Makefile
-
-# Debugging format.
-# Native formats for AVR-GCC's -g are stabs [default], or dwarf-2.
-# AVR (extended) COFF requires stabs, plus an avr-objcopy run.
-DEBUG = stabs
-
-# Compiler flag to set the C Standard level.
-# c89 - "ANSI" C
-# gnu89 - c89 plus GCC extensions
-# c99 - ISO C99 standard (not yet fully implemented)
-# gnu99 - c99 plus GCC extensions
-CSTANDARD = -std=gnu99
-
-# Place -D or -U options here
-CDEFS =
-
-# Place -I options here
-CINCS =
-
-
-CDEBUG = -g$(DEBUG)
-CWARN = -Wall -Wstrict-prototypes
-CTUNING = -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
-#CEXTRA = -Wa,-adhlns=$(<:.c=.lst)
-CFLAGS = $(CDEBUG) $(CDEFS) $(CINCS) -O$(OPT) $(CWARN) $(CSTANDARD) $(CEXTRA)
-
-
-#ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs
-
-
-#Additional libraries.
-
-# Minimalistic printf version
-PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
-
-# Floating point printf version (requires MATH_LIB = -lm below)
-PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
-
-PRINTF_LIB =
-
-# Minimalistic scanf version
-SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
-
-# Floating point + %[ scanf version (requires MATH_LIB = -lm below)
-SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
-
-SCANF_LIB =
-
-MATH_LIB = -lm
-
-# External memory options
-
-# 64 KB of external RAM, starting after internal RAM (ATmega128!),
-# used for variables (.data/.bss) and heap (malloc()).
-#EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
-
-# 64 KB of external RAM, starting after internal RAM (ATmega128!),
-# only used for heap (malloc()).
-#EXTMEMOPTS = -Wl,--defsym=__heap_start=0x801100,--defsym=__heap_end=0x80ffff
-
-EXTMEMOPTS =
-
-#LDMAP = $(LDFLAGS) -Wl,-Map=$(TARGET).map,--cref
-LDFLAGS = $(EXTMEMOPTS) $(LDMAP) $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
-
-
-# Programming support using avrdude. Settings and variables.
-
-AVRDUDE_PROGRAMMER = usbtiny
-AVRDUDE_PORT = usb
-
-AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
-#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
-
-
-# Uncomment the following if you want avrdude's erase cycle counter.
-# Note that this counter needs to be initialized first using -Yn,
-# see avrdude manual.
-#AVRDUDE_ERASE_COUNTER = -y
-
-# Uncomment the following if you do /not/ wish a verification to be
-# performed after programming the device.
-#AVRDUDE_NO_VERIFY = -V
-
-# Increase verbosity level. Please use this when submitting bug
-# reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
-# to submit bug reports.
-#AVRDUDE_VERBOSE = -v -v
-
-AVRDUDE_BASIC = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
-AVRDUDE_FLAGS = $(AVRDUDE_BASIC) $(AVRDUDE_NO_VERIFY) $(AVRDUDE_VERBOSE) $(AVRDUDE_ERASE_COUNTER)
-
-
-CC = avr-gcc
-OBJCOPY = avr-objcopy
-OBJDUMP = avr-objdump
-SIZE = avr-size
-NM = avr-nm
-AVRDUDE = avrdude
-REMOVE = rm -f
-MV = mv -f
-
-# Define all object files.
-OBJ = $(SRC:.c=.o) $(ASRC:.S=.o)
-
-# Define all listing files.
-LST = $(ASRC:.S=.lst) $(SRC:.c=.lst)
-
-# Combine all necessary flags and optional flags.
-# Add target processor to flags.
-ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS)
-ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
-
-
-# Default target.
-all: build
-
-build: elf hex eep
-
-elf: $(TARGET).elf
-hex: $(TARGET).hex
-eep: $(TARGET).eep
-lss: $(TARGET).lss
-sym: $(TARGET).sym
-
-
-# Program the device.
-program: $(TARGET).hex $(TARGET).eep
- $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
-
-
-
-
-# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
-COFFCONVERT=$(OBJCOPY) --debugging \
---change-section-address .data-0x800000 \
---change-section-address .bss-0x800000 \
---change-section-address .noinit-0x800000 \
---change-section-address .eeprom-0x810000
-
-
-coff: $(TARGET).elf
- $(COFFCONVERT) -O coff-avr $(TARGET).elf $(TARGET).cof
-
-
-extcoff: $(TARGET).elf
- $(COFFCONVERT) -O coff-ext-avr $(TARGET).elf $(TARGET).cof
-
-
-.SUFFIXES: .elf .hex .eep .lss .sym
-
-.elf.hex:
- $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@
-
-.elf.eep:
- -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
- --change-section-lma .eeprom=0 -O $(FORMAT) $< $@
-
-# Create extended listing file from ELF output file.
-.elf.lss:
- $(OBJDUMP) -h -S $< > $@
-
-# Create a symbol table from ELF output file.
-.elf.sym:
- $(NM) -n $< > $@
-
-
-
-# Link: create ELF output file from object files.
-$(TARGET).elf: $(OBJ)
- $(CC) $(ALL_CFLAGS) $(OBJ) --output $@ $(LDFLAGS)
-
-
-# Compile: create object files from C source files.
-.c.o:
- $(CC) -c $(ALL_CFLAGS) $< -o $@
-
-
-# Compile: create assembler files from C source files.
-.c.s:
- $(CC) -S $(ALL_CFLAGS) $< -o $@
-
-
-# Assemble: create object files from assembler source files.
-.S.o:
- $(CC) -c $(ALL_ASFLAGS) $< -o $@
-
-
-
-# Target: clean project.
-clean:
- $(REMOVE) $(TARGET).hex $(TARGET).eep $(TARGET).cof $(TARGET).elf \
- $(TARGET).map $(TARGET).sym $(TARGET).lss \
- $(OBJ) $(LST) $(SRC:.c=.s) $(SRC:.c=.d)
-
-depend:
- if grep '^# DO NOT DELETE' $(MAKEFILE) >/dev/null; \
- then \
- sed -e '/^# DO NOT DELETE/,$$d' $(MAKEFILE) > \
- $(MAKEFILE).$$$$ && \
- $(MV) $(MAKEFILE).$$$$ $(MAKEFILE); \
- fi
- echo '# DO NOT DELETE THIS LINE -- make depend depends on it.' \
- >> $(MAKEFILE); \
- $(CC) -M -mmcu=$(MCU) $(CDEFS) $(CINCS) $(SRC) $(ASRC) >> $(MAKEFILE)
-
-.PHONY: all build elf hex eep lss sym program coff extcoff clean depend
+++ /dev/null
-#define F_CPU 6000000UL\r
-\r
-#include <avr/interrupt.h>\r
-#include <util/delay.h>\r
-#include <avr/io.h>\r
-#include <avr/eeprom.h>\r
-#include <string.h>\r
-\r
-#define FIRMWARE_VERSION 0x010\r
-\r
-//A0 Aref In\r
-//A1 Adc2 In (ADC1)\r
-//A2 Relay2 Out\r
-//A3 Relay1 Out\r
-//A4 RX In\r
-//A5 TX Out\r
-//A6 Switch1 In\r
-//A7 Adc1 In (ADC7)\r
-\r
-//B0 X --\r
-//B1 X --\r
-//B2 Switch2 In \r
-//B3 Reset --\r
-\r
-//all commands end in \n\r
-// cc -- channel address in hex \r
-//Scc(0|1|/) -- switch channel off/on/toggle\r
-//Scc? -- read channel status - 2nd char: 'e' in case of overcurrent, 3rd char: enable 1/0\r
-//Ccc? -- read current\r
-//Dcc? -- read averaged current\r
-//Lccxxx -- set current limit (hex)\r
-//Lcc? -- read current limit\r
-//Axxx -- answer - three hex digits or chars\r
-//Occ(0|1|2|3) -- switch ADC & set max scale: off|2.2Vref|4.1Vref|VCCref (one setting for both ports)\r
-//Pcc(0|1) -- Do a automatic power cycle (2 seconds long)\r
-//Rcc? -- read ADC measurement of internal 1.1V reference (one register)\r
-//Icc? -- Firmware Info\r
-\r
-#define STARTTX(i) txcnt = (i);UCSR1B |= (1<< UDRIE1)\r
-#define ISMYADDR() (rxbuf[1] == '0' && (rxbuf[2] == '0' || rxbuf[2] == '1') )\r
-\r
-volatile uint8_t settings_changed = 0;\r
-uint8_t output_enable = 0x3;\r
-uint8_t output_error = 0;\r
-uint8_t adc_enable = 1;\r
-uint16_t adc[2];\r
-uint16_t avgadc[2] = {0,0};\r
-uint8_t countdown[2] = {0,0};\r
-uint16_t adc_reference = 0;\r
-\r
-uint8_t rxcnt = 0, txcnt = 0, txpoint = 0;\r
-uint8_t rxbuf[7];\r
-uint8_t txbuf[7];\r
-uint16_t limit[2] = {0x0800,0x0800};\r
-\r
-\r
-uint8_t nib_to_hex(uint16_t in, uint8_t nib) {\r
- uint8_t t = (in >> (nib*4)) & 0xF;\r
- if (t <= 9) {return t + 0x30;}\r
- return t - 10 + 0x61;\r
- }\r
- \r
-uint8_t hex_to_int(uint8_t h) { //assumes valid number\r
- if (h < 0x40) return h-0x30;\r
- if (h < 0x50) return h-0x41+10;\r
- return h-0x61+10;\r
- }\r
- \r
-void sub2(uint8_t* c1, uint8_t* c2) {\r
- uint8_t b = hex_to_int(*c1)*16 + hex_to_int(*c2);\r
- b -= 2;\r
- *c1 = nib_to_hex(b,1);\r
- *c2 = nib_to_hex(b,0);\r
- }\r
-\r
-void forward_msg(uint8_t i) {\r
- sub2(&rxbuf[1],&rxbuf[2]);\r
- memcpy ((uint8_t*)txbuf,(uint8_t*)rxbuf,i);\r
- STARTTX(i);\r
- } \r
-\r
-uint8_t is_my_address(uint8_t s) {\r
- if (ISMYADDR()) {\r
- rxbuf[2] -= '0';\r
- return 1;\r
- }\r
- else {\r
- forward_msg(s);\r
- return 0;\r
- }\r
- }\r
- \r
-void send_answer_hex(uint16_t v) {\r
- txbuf[0]='A';\r
- txbuf[1]=nib_to_hex(v,2);\r
- txbuf[2]=nib_to_hex(v,1);\r
- txbuf[3]=nib_to_hex(v,0);\r
- txbuf[4]='\n';\r
- STARTTX(5);\r
- }\r
- \r
-void send_answer_chars(uint8_t a, uint8_t b, uint8_t c) {\r
- txbuf[0]='A';\r
- txbuf[1]=a;\r
- txbuf[2]=b;\r
- txbuf[3]=c;\r
- txbuf[4]='\n';\r
- STARTTX(5);\r
- }\r
-\r
- \r
-void switchoutput(uint8_t chan, int8_t to) {\r
- if(to == -1) {\r
- output_enable ^= (1<<chan);\r
- output_error &= ~(1<<chan);\r
- }\r
- else if (to == 0 || to == -2) {\r
- output_enable &= ~(1<<chan);\r
- }\r
- else if (to == 1) {\r
- output_enable |= (1<<chan);\r
- output_error &= ~(1<<chan);\r
- }\r
-\r
- if(to >= -1) {settings_changed |= 4;}\r
- \r
- uint8_t en1 = ((output_enable & 1) && !(output_error & 1))?1:0; //switched for open drain version\r
- uint8_t en2 = ((output_enable & 2) && !(output_error & 2))?1:0;\r
- \r
- PORTA &= ~((1<<2) | (1<<3));\r
- DDRA = (DDRA & ~(3<<2)) | ((en1&1)<<3) | ((en2&1)<<2);\r
- //PORTA = (PORTA & ~(3<<2)) | ((en1&1)<<3) | ((en2&1)<<2);\r
-\r
- }\r
-\r
-void correct_adc(uint8_t channel) {\r
- if(adc_enable == 1) return;\r
- if(adc_enable == 2) {\r
- adc[channel] = adc[channel]*2 - (adc[channel]>>3);\r
- }\r
- }\r
- \r
-ISR(TIMER0_OVF_vect) {\r
- static uint8_t dwncnt[2] = {0,0};\r
- if(!(PINA & 0x40)) {dwncnt[0]++;} else {dwncnt[0] = 0;}\r
- if(!(PINB & 0x04)) {dwncnt[1]++;} else {dwncnt[1] = 0;}\r
- \r
- \r
- for(uint8_t i=0; i<=1;i++) {\r
- if(dwncnt[i] == 50) {switchoutput(i,-1);} \r
- if(countdown[i] != 0) {\r
- if(countdown[i] == 1) {\r
- switchoutput(i,-1);\r
- }\r
- countdown[i]--;\r
- }\r
- }\r
- \r
- if(adc_enable) {ADCSRA |= (1<<ADSC);}\r
- asm volatile("wdr");\r
- } \r
- \r
-ISR(ADC_vect) {\r
- static uint8_t channel = 0;\r
- if(channel == 0) { \r
- adc_reference = ADC;\r
- ADMUXA = 7; \r
- ADCSRA |= (1<<ADSC); \r
- channel = 1;\r
- }\r
- else if(channel == 1) { \r
- adc[0] = ADC; \r
- correct_adc(0);\r
- ADMUXA = 1; \r
- ADCSRA |= (1<<ADSC); \r
- channel = 2;\r
- avgadc[0] -= avgadc[0]/8;\r
- avgadc[0] += adc[0];\r
- if(limit[0]*8<avgadc[0]) {\r
- output_error |= 1;\r
- switchoutput(0,-2);\r
- } \r
- }\r
- else if(channel == 2) { \r
- adc[1] = ADC; \r
- correct_adc(1);\r
- ADMUXA = 0xD;\r
- channel = 0;\r
- avgadc[1] -= avgadc[1]/8;\r
- avgadc[1] += adc[1];\r
- if(limit[1]*8<avgadc[1]) {\r
- output_error |= 2;\r
- switchoutput(1,-2);\r
- } \r
- }\r
- }\r
-\r
-ISR(USART1_UDRE_vect) {\r
- UDR1 = txbuf[txpoint++];\r
- if(--txcnt == 0 || txpoint > 7) { \r
- txpoint = 0;\r
- txcnt = 0;\r
- UCSR1B &= ~(1<< UDRIE1);\r
- }\r
- }\r
- \r
-ISR(USART1_RX_vect) {\r
- uint8_t buf = UDR1;\r
- if (rxcnt != 0 || (buf == 'S' || buf == 'A' || buf == 'L' || buf == 'C' || buf == 'I'\r
- || buf == 'D' || buf == 'O' || buf == 'P' || buf == 'R')) {\r
- rxbuf[rxcnt++] = buf;\r
- }\r
- if (buf == '\n' || buf == '\r') {\r
-\r
-\r
- if(rxcnt == 5) { //4 letter commands\r
-//Forward any incoming 4 letter answer \r
- if (rxbuf[0] == 'A') {\r
- memcpy ((uint8_t*)txbuf,(uint8_t*)rxbuf,5);\r
- STARTTX(5);\r
- }\r
- \r
-//Switch command \r
- if (rxbuf[0] == 'S' && is_my_address(5)) { \r
- if (rxbuf[3] != '?') {\r
- switchoutput(rxbuf[2],rxbuf[3]-'0');\r
- }\r
- send_answer_chars('0', (output_error & (1<<rxbuf[2]))?'e':'0', \r
- (output_enable & (1<<rxbuf[2]))?'1':'0'); \r
- }\r
-\r
-//Power cycle command\r
- if (rxbuf[0] == 'P' && is_my_address(5)) { \r
- if (rxbuf[3] != '?') {\r
- switchoutput(rxbuf[2],rxbuf[3]-'0');\r
- countdown[rxbuf[2]] = 200;\r
- }\r
- send_answer_chars('0', (output_error & (1<<rxbuf[2]))?'e':'0', \r
- (output_enable & (1<<rxbuf[2]))?'1':'0'); \r
- }\r
- \r
-//Read current command\r
- if (rxbuf[0] == 'C' && rxbuf[3] == '?' && is_my_address(5)) { \r
- if(adc_enable == 3) { send_answer_hex(adc[rxbuf[2]]+0x800); }\r
- else { send_answer_hex(adc[rxbuf[2]]); }\r
- }\r
- \r
-//Read average current command\r
- if (rxbuf[0] == 'D' && rxbuf[3] == '?' && is_my_address(5)) {\r
- if(adc_enable == 3) { send_answer_hex(avgadc[rxbuf[2]]/8+0x800); }\r
- else { send_answer_hex(avgadc[rxbuf[2]]/8); }\r
- }\r
-\r
-//Read firmware info\r
- if (rxbuf[0] == 'I' && rxbuf[3] == '?' && is_my_address(5)) {\r
- send_answer_hex(FIRMWARE_VERSION);\r
- }\r
- \r
-//Switch ADC\r
- if (rxbuf[0] == 'O' && is_my_address(5)) {\r
- if(rxbuf[3] != '?')\r
- adc_enable = rxbuf[3] - '0';\r
- send_answer_hex(adc_enable);\r
- settings_changed |= 8;\r
- if (adc_enable == 1) ADMUXB = (6 << REFS0); //2.2V reference, with capacitor\r
- if (adc_enable == 2) ADMUXB = (7 << REFS0); //4.096V as reference\r
- if (adc_enable == 3) ADMUXB = (0 << REFS0); //Vcc as reference\r
- }\r
- \r
-//Read current limit \r
- if (rxbuf[0] == 'L' && rxbuf[3] == '?' && is_my_address(5)) {\r
- send_answer_hex(limit[rxbuf[2]]); \r
- } \r
-\r
-//Read reference value\r
- if (rxbuf[0] == 'R' && rxbuf[3] == '?' && is_my_address(5)) {\r
- send_answer_hex(adc_reference); \r
- } \r
- }\r
- \r
- \r
-//Set current limit\r
- if (rxcnt == 7 && rxbuf[0] == 'L' && is_my_address(7)) {\r
- if(rxbuf[3] != '?') {\r
- uint16_t lim = hex_to_int(rxbuf[3])*256 + hex_to_int(rxbuf[4])*16 + hex_to_int(rxbuf[5]);\r
- limit[rxbuf[2]] = lim;\r
- }\r
- send_answer_hex(limit[rxbuf[2]]); \r
- settings_changed |= rxbuf[2]+1; \r
- }\r
- } \r
- \r
- if (rxcnt >= 7 || buf == '\n' || buf == '\r') { rxcnt = 0; } \r
- \r
-} \r
-\r
-\r
- \r
-__attribute__((naked)) int main(void) {\r
- // Configure ports\r
- \r
- PORTA = 0b00001100;\r
- PUEA = 0b01010000;\r
- DDRA = 0b00101100; \r
- \r
- DDRB = 0b00000000;\r
- PUEB = 0b00001100;\r
- PORTB = 0b00000000;\r
- \r
- output_enable = eeprom_read_byte((uint8_t*)0x24);\r
- switchoutput(0,-3); \r
- limit[0] = eeprom_read_byte((uint8_t*)0x21)<<8;\r
- limit[0] |= eeprom_read_byte((uint8_t*)0x20);\r
- limit[1] = eeprom_read_byte((uint8_t*)0x23)<<8;\r
- limit[1] |= eeprom_read_byte((uint8_t*)0x22); \r
- adc_enable = eeprom_read_byte((uint8_t*)0x25);\r
- \r
- //CCP = 0xD8; //allow writing of CLKPR\r
- CLKPR = (0 << CLKPS0); // no prescaler \r
- PRR = (1 << PRTWI) | (0 << PRUSART1) | (1 << PRUSART0) | (1 << PRSPI) \r
- |(1 << PRTIM2) | (1 << PRTIM1) | (0 << PRTIM0) | (0 << PRADC); \r
- \r
- TCCR0B = (4 << CS00); //prescaler 256 -> 6M/256*256 = 91.5 Hz\r
- TIMSK0 = (1 << TOIE0); //Overflow interrupt`\r
- \r
- ADMUXA = 7; //(1 (Out2),7 (Out1))\r
- if (adc_enable == 1) ADMUXB = (6 << REFS0); //2.2V reference, with capacitor\r
- if (adc_enable == 2) ADMUXB = (7 << REFS0); //4.096V reference\r
- if (adc_enable == 3) ADMUXB = (0 << REFS0); //Vcc reference\r
- ADCSRA = (1 << ADEN) | (1 << ADSC) | (1 << ADIE) | (6 << ADPS0); //enable, start, irq, /128\r
- ADCSRB = 0;\r
- DIDR0 = (1<<ADC1D) | (1<< ADC7D); //disable digital inputs\r
- \r
- UCSR1A = (1 << U2X1);\r
- UCSR1B = (1 << RXCIE1) | (0 << TXCIE1) | (0 << RXEN1) | (1 << TXEN1);\r
- UCSR1C = (3 << UCSZ10); //8 Bit\r
- UBRR1 = 0x0C; //57600\r
- \r
- _delay_ms(100);\r
- UCSR1B |= (1 << RXEN1);\r
-\r
- CCP = 0xD8;\r
- WDTCSR = (1<<WDE) | (5<<WDP0); //Watchdog at .5 seconds\r
- sei(); \r
- \r
- \r
- while(1) {\r
- _delay_ms(3000); \r
- if(settings_changed != 0) {\r
- if (settings_changed & 1) {\r
- settings_changed &= ~1;\r
- eeprom_update_byte((uint8_t*)0x20,limit[0]&0xFF);\r
- eeprom_update_byte((uint8_t*)0x21,(limit[0]>>8)&0xFF);\r
- }\r
- if (settings_changed & 2) {\r
- settings_changed &= ~2;\r
- eeprom_update_byte((uint8_t*)0x22,limit[1]&0xFF);\r
- eeprom_update_byte((uint8_t*)0x23,(limit[1]>>8)&0xFF);\r
- }\r
- if (settings_changed & 4) {\r
- settings_changed &= ~4;\r
- eeprom_update_byte((uint8_t*)0x24,output_enable);\r
- }\r
- if (settings_changed & 8) {\r
- settings_changed &= ~8;\r
- eeprom_update_byte((uint8_t*)0x25,adc_enable);\r
- }\r
- }\r
- }\r
- }
\ No newline at end of file