From 08d27100c208470e35437dc175ae066d22e0e57e Mon Sep 17 00:00:00 2001 From: Adrian Weber Date: Tue, 18 Feb 2020 14:17:04 +0100 Subject: [PATCH] Adding a Readout script; Bugfix and uncommenting for use with ProMicro (For Debugging) --- atmega32u4/dcdc_rich/Makefile | 2 +- atmega32u4/dcdc_rich/dcdc.pl | 269 ++++++++++++++++++++++++++++++++++ atmega32u4/dcdc_rich/main.c | 45 ++++-- 3 files changed, 306 insertions(+), 10 deletions(-) create mode 100755 atmega32u4/dcdc_rich/dcdc.pl diff --git a/atmega32u4/dcdc_rich/Makefile b/atmega32u4/dcdc_rich/Makefile index 8ee15d8..c5b29c2 100644 --- a/atmega32u4/dcdc_rich/Makefile +++ b/atmega32u4/dcdc_rich/Makefile @@ -11,7 +11,7 @@ TARGET = main SRC = $(TARGET).c usb_serial.c ASRC = OPT = 2 -PORT=/dev/ttyACM0 +PORT=/dev/ttyACM1 # Name of this Makefile (used for "make depend"). MAKEFILE = Makefile diff --git a/atmega32u4/dcdc_rich/dcdc.pl b/atmega32u4/dcdc_rich/dcdc.pl new file mode 100755 index 0000000..d1d5183 --- /dev/null +++ b/atmega32u4/dcdc_rich/dcdc.pl @@ -0,0 +1,269 @@ +#!/usr/bin/perl +# if ($ENV{'SERVER_SOFTWARE'} =~ /HTTPi/i) { +# print "HTTP/1.0 200 OK\n"; +# print "Content-type: text/html\r\n\r\n"; +# } +# else { +# use lib '..'; +# print "Content-type: text/html\n\n"; +# } + +use strict; +use warnings; +use Device::SerialPort; +use feature 'state'; +use URI::Escape; +use Data::Dumper; +use Time::HiRes qw( usleep); +use Getopt::Long; + +my $port; +my $help; +my $ser_dev; +my $isTrbNet = 0; +Getopt::Long::Configure(qw(gnu_getopt)); +GetOptions( + 'help|h' => \$help, + 'device|d=s' => \$ser_dev, + ) ; + +my $mode = 0; +my $ch; +my $uC; +my $reg; +my $rw; +my $val; +my $num = 0; +my $args = scalar @ARGV; + +# my $envstring = $ENV{'QUERY_STRING'}; +# +# +# my @new_command = split('&',$envstring); +# my $ser_dev = shift(@new_command); + +$ser_dev = "/dev/ttyUSB0" unless defined $ser_dev; + +sub PrintAnswer { + my ($s) = @_; + + print $s."\n"; + +} + +sub SendCmdShort { + my ($reg) = @_; + + if($args == 3 || $args == 4) { + # microcontroller number in chain + my $uC = $ARGV[1]; + if (substr($uC,0,2) eq "0x") {$uC = hex(substr($uC,2));} + if ($uC > $num) { + die "This microcontroller Number is not allowed! \n"; + } + + # channel number + my $ch = $ARGV[2]; + if (substr($ch,0,2) eq "0x") {$ch = hex(substr($ch,2));} + if ($ch >= 4) { + die "This channel does not exist\n"; + } + my $rw = "R"; + my $val = 0x0000; + my $cmd = sprintf("%s%02x0%01x%01x%04x",$rw,$uC,$ch,$reg,$val); + print $cmd."\n"; + PrintAnswer(Cmd($cmd)) #Answer without \n + } else { + die "Not all arguements were specified!\n"; + } + +} + +sub Cmd { + my ($c) = @_; + for my $i (0..0) { + $port->write($c."\n"); + my $a = ""; + for my $j (0..16) { + my ($l,$s) = $port->read(12); + $a .= $l; + if ($l < 1) {next;} + #print "DBG".(length $s)."\n"; + #print $l." ".$s."\n"; + #if ($s =~ /^\w[a-f0-9]{9}\n/) { return substr($s,0,10);} + if ($s =~ /^\w[a-f0-9]{0,11}/) { my $size = length $s; $s = substr($s,0,$size-1); return $s;} + usleep(10000); + } + usleep(50000); + #print '.'; + } + return ; +} + +if ($help || (defined $ARGV[0] && $ARGV[0] =~ /help/)) { + print "dcdc.pl [-d DEVICE] reg [uC [CHANNEL [OPERATION [REGISTER [VALUE]]]]]\n"; + print "dcdc.pl [-d DEVICE] CMD [uC [CHANNEL [VALUE]]]\n\n"; + print "CMD: s: set DCDC setting, g: get DCDC setting,\n"; + print " t: temperature, v: input voltage, c: input current;\n"; + print "uC: number of Microcontrolle rin chain. As Hex or Dec\n"; + print "CHANNEL: Channel number, hex or decimal\n"; + print "OPERATION: R = Read; W = write\n"; + print "REGISTER: Register to access (dec or hex)\n"; + print "VALUE: A 16 Bit value, 4 hex digits or decimal\n"; + exit; +} + + $port = new Device::SerialPort($ser_dev); + unless ($port) { + print "can't open serial interface $ser_dev\n"; + exit; + } + $port->user_msg('ON'); + $port->baudrate(57600); + $port->parity("none"); + $port->databits(8); + $port->stopbits(1); + $port->handshake("none"); + $port->read_char_time(0); + $port->read_const_time(50); + $port->write_settings; + + +#Scan for number of Microcontrolles. +$num = Cmd("S"); +$num = hex(substr($num,1)); +if ($num == 0){ die "Can not access a power switch\n";} + + + +# microcontroller number in chain +if($args > 1) { + if ($ARGV[0] eq "reg") {$mode = 1;} + if ($ARGV[0] eq "s") {$mode = 2;} + if ($ARGV[0] eq "g") {$mode = 3;} + if ($ARGV[0] eq "t") {$mode = 4;} + if ($ARGV[0] eq "v") {$mode = 5;} + if ($ARGV[0] eq "c") {$mode = 6;} + if ($ARGV[0] eq "i") {$mode = 7;} +} + +if ($mode == 1) { + + # direct register access + if($args == 6) { + + # microcontroller number in chain + $uC = $ARGV[1]; + if (substr($uC,0,2) eq "0x") {$uC = hex(substr($uC,2));} + if ($uC > $num) { + die "This microcontroller Number is not allowed! \n"; + } + + # channel number + $ch = $ARGV[2]; + if (substr($ch,0,2) eq "0x") {$ch = hex(substr($ch,2));} + if ($ch >= 4) { + die "This channel does not exist\n"; + } + + # operation R/W + $rw = $ARGV[3]; + if ($rw eq "r" || $rw eq "R") { + $rw = "R"; + } elsif ($rw eq "w" || $rw eq "W") { + $rw = "W"; + } else { + die "This register does not exist\n"; + } + + # Register + $reg = $ARGV[4]; + if (substr($reg,0,2) eq "0x") {$reg= hex(substr($reg,2));} + if ($reg >= 6) { + die "This register does not exist\n"; + } + + # Value + $val = $ARGV[5]; + if (substr($val,0,2) eq "0x") {$val= hex(substr($val,2));} + if ($val > 0xFFFF) { + die "The value is greater than 16 bit! Only 16 bit are allowed\n"; + } + my $cmd = sprintf("%s%02x0%01x%01x%04x",$rw,$uC,$ch,$reg,$val); + print $cmd."\n"; + PrintAnswer(Cmd($cmd)) #Answer without \n + } else { + + die "Not all arguements were specified!\n"; + } +} + +# Set settings of DCDC +if($mode == 2) { + if($args == 4) { + # microcontroller number in chain + $uC = $ARGV[1]; + if (substr($uC,0,2) eq "0x") {$uC = hex(substr($uC,2));} + if ($uC > $num) { + die "This microcontroller Number is not allowed! \n"; + } + + # channel number + $ch = $ARGV[2]; + if (substr($ch,0,2) eq "0x") {$ch = hex(substr($ch,2));} + if ($ch >= 4) { + die "This channel does not exist\n"; + } + + $rw = "W"; + $reg = 0x1; + $val = $ARGV[3]; + if (substr($val,0,2) eq "0x") {$val= hex(substr($val,2));} + if ($val > 0xFFFF) { + die "The value is greater than 16 bit! Only 16 bit are allowed\n"; + } + my $cmd = sprintf("%s%02x0%01x%01x%04x",$rw,$uC,$ch,$reg,$val); + print $cmd."\n"; + PrintAnswer(Cmd($cmd)) #Answer without \n + } else { + die "Not all arguements were specified!\nUSAGE: dcdc.pl s \n"; + } +} + +# Read Setting of DCDC +if($mode == 3) {SendCmdShort(0x1)} + +# Temperature +if($mode == 4) {SendCmdShort(0x4)} + +# Voltage +if($mode == 5) {SendCmdShort(0x2)} + +# Current +if($mode == 6) {SendCmdShort(0x3)} + +# Infos +if($mode == 7) {SendCmdShort(0x5)} + + + + + +# if($args <= 1) { +# print " Ch\t Curr.\t AvgCur\t Limit\n"; + +# for my $i (0..$num-1) { +# next if ($args >= 1 && $ch != $i); +# my $n = sprintf("%02x",$i); + +# my $s = Cmd("S$n?"); +# my $curr = Cmd("C$n?"); +# my $avg = Cmd("D$n?"); +# my $lim = Cmd("L$n?"); + +# if(($s & 0xf0) == 0xe0) {printf(" $n\tERR\t\t (%3i)\n",$lim&0x3FF);} +# if(($s & 0xff) == 0x00) {printf(" $n\t--- \t\t (%3i)\n",$lim&0x3FF);} +# if(($s & 0xff) == 0x01) {printf(" $n\t%3imA\t %3imA\t (%3i)\n",$curr&0x7ff,$avg&0x7ff,$lim&0x3FF);} +# } + +# } \ No newline at end of file diff --git a/atmega32u4/dcdc_rich/main.c b/atmega32u4/dcdc_rich/main.c index 6232261..91d3b4b 100644 --- a/atmega32u4/dcdc_rich/main.c +++ b/atmega32u4/dcdc_rich/main.c @@ -208,15 +208,41 @@ uint16_t read_ADC(void) { ***/ void getdata(uint8_t buf) { - if (rxcnt != 0 || (buf == 'W' || buf == 'R' || buf == 'A' )) { + if (rxcnt != 0 || (buf == 'W' || buf == 'R' || buf == 'A' || buf == 'S')) { rxbuf[rxcnt++] = buf; } if (buf == '\n' || buf == '\r') { //End of Command if (rxbuf[0] == 'A') { memcpy ((uint8_t*)txbuf,(uint8_t*)rxbuf,10); - txbuf[10] = 0; + txbuf[11] = 0; send_answer_buf(txbuf); + } else if (rxbuf[0] == 'S') { // Scann of chain, returns number of boards + txbuf[0] = 'S'; + uint8_t length = rxcnt - 2 ; // Length of Counter + //read current counter value + uint32_t cnt = 0; + if (length == 0){ + length = 1; + } else { + uint32_t base = 1; + for (uint8_t i = length; i > 0; i--){ + cnt += hex_to_int(rxbuf[i])*base; + base *= 16; + } + } + // inc counter value + cnt++; + //send to next uC + if ((cnt % 16) == 0) length++; + for (uint8_t i = length; i > 0; i--){ + txbuf[i] = nib_to_hex(cnt,length-i); + } + if (length > 8) length = 8; // skip to keep a clean ending of message + txbuf[length+1] = 10; + txbuf[length+2] = 0; + send_answer_buf(txbuf); + rxcnt = 0; } else if (is_my_address(10)){ // message is for this uC if (rxbuf[0] == 'W'){ //write @@ -274,9 +300,9 @@ void getdata(uint8_t buf) { send_answer_hex(&rxbuf[0],adc_temp); } - // get temperature + // get info if (hex_to_int(rxbuf[5]) == 5) { - uint16_t res = (FIRMWARE_VERSION<<4) & (0<<1) & (0<<0); + uint16_t res = ((FIRMWARE_VERSION<<4) | (0<<1) | (0<<0))&0xFFFF; send_answer_hex(&rxbuf[0],res); } } @@ -363,8 +389,8 @@ __attribute__((naked)) int main(void) { SPCR = (1<> 0)& 0x1; @@ -390,8 +416,8 @@ __attribute__((naked)) int main(void) { dcdc_settings[3][2] = (eeprom_value >> 2)& 0x1; dcdc_settings[3][3] = (eeprom_value >> 3)& 0x1; - setVoltages(); - SHIFT_EN_OUTPUT(); // Enable output of shift register to mosfet + //setVoltages(); + //SHIFT_EN_OUTPUT(); // Enable output of shift register to mosfet sei(); uint8_t ADC_State = 0; // 0: start of uC; 1: Volt; 2: Current; 3: Temp uint16_t lasttime = 0; @@ -399,12 +425,13 @@ __attribute__((naked)) int main(void) { if((time != lasttime) && ((time %10) == 0)) { //~330us for each conversion - if (ADC_State == 3) {adc_temp = read_ADC(); ADC_State = 0;} + /*if (ADC_State == 3) {adc_temp = read_ADC(); ADC_State = 0;} if (ADC_State == 2) {adc_volt = read_ADC(); ADC_State = 3;} if (ADC_State == 1) {adc_curr = read_ADC(); ADC_State = 2;} if (ADC_State == 0) ADC_State = 1; conversion_ADC(ADC_State - 1); + */ } lasttime = time; -- 2.43.0