| 1 | package VIC::PIC::Functions::USART; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 31 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 31 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 31 | our $VERSION = '0.32'; | |||||||||||||||||||||||||||||||||||||||||||||
| 5 | 31 | $VERSION = eval $VERSION; | |||||||||||||||||||||||||||||||||||||||||||||
| 6 | 31 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 31 | ▶ | use POSIX (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 31 | ▶ | use Scalar::Util qw(looks_like_number); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 31 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | sub usart_setup { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
usart_setup: called CC=33 SCAR=44.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | my ($self, $outp, $baudr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | ▶ | return unless $self->doesroles(qw(USART GPIO CodeGen Chip)); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 3 | ▶ | return unless $outp =~ /UART|USART/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 3 | ▶ | my $sync = ($outp =~ /^UART/) ? 0 : 1; # the other is USART | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 3 | my $ipin = $self->usart_pins->{async_in}; | |||||||||||||||||||||||||||||||||||||||||||||
| 17 | 3 | my $opin = $self->usart_pins->{async_out}; | |||||||||||||||||||||||||||||||||||||||||||||
| 18 | 3 | my $sclk = $self->usart_pins->{sync_clock}; | |||||||||||||||||||||||||||||||||||||||||||||
| 19 | 3 | my $sdat = $self->usart_pins->{sync_data}; | |||||||||||||||||||||||||||||||||||||||||||||
| 20 | 3 | ▶ | return unless (defined $ipin and defined $opin); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $ipin and defined $opin
MC/DC0%0/2
defined $ipin and defined $opin
defined $ipin
defined $opin
Truth table33%1/3
A and B
A= defined $ipinB= defined $opin
| |||||||||||||||||||||||||||||||||||||||||||||||
| 21 | #return if ($sync == 1 and not defined $sclk and not defined $sdat); | ||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 3 | ▶ | return unless (exists $self->pins->{$ipin} and exists $self->pins->{$opin}); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $self->pins->{$ipin} and exists $self->pins->{$opin}
MC/DC0%0/2
exists $self->pins->{$ipin} and exists $self->pins->{$opin}
exists $self->pins->{$ipin}
exists $self->pins->{$opin}
Truth table33%1/3
A and B
A= exists $self->pins->{$ipin}B= exists $self->pins->{$opin}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 3 | my ($code, $funcs, $macros, $tables) = ('', {}, {}, []); | |||||||||||||||||||||||||||||||||||||||||||||
| 24 | 3 | $macros->{m_usart_var} = $self->_usart_var; | |||||||||||||||||||||||||||||||||||||||||||||
| 25 | 3 | ▶ | if (exists $self->registers->{SPBRGH} and | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%2/6
exists $self->registers->{"SPBRGH"} and exists $self->registers->{"SPBRG"} and exists $self->registers->{"BAUDCTL"}
exists $self->registers->{"SPBRGH"} and exists $self->registers->{"SPBRG"}
MC/DC0%0/3
exists $self->registers->{"SPBRGH"} and exists $self->registers->{"SPBRG"} and exists $self->registers->{"BAUDCTL"}
exists $self->registers->{"SPBRGH"}
exists $self->registers->{"SPBRG"}
exists $self->registers->{"BAUDCTL"}
Truth table25%1/4
A and B and C
A= exists $self->registers->{"SPBRGH"}B= exists $self->registers->{"SPBRG"}C= exists $self->registers->{"BAUDCTL"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | exists $self->registers->{SPBRG} and | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | exists $self->registers->{BAUDCTL}) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 28 | ## Enhanced USART (16-bit) | ||||||||||||||||||||||||||||||||||||||||||||||
| 29 | ## Required registers TXSTA, RCSTA, BAUDCTL, TXREG, RCREG | ||||||||||||||||||||||||||||||||||||||||||||||
| 30 | ## To enable the transmitter for asynchronous ops | ||||||||||||||||||||||||||||||||||||||||||||||
| 31 | ## TXEN = 1, SYNC = 0, SPEN = 1 | ||||||||||||||||||||||||||||||||||||||||||||||
| 32 | ## if TX/CK pin is shared with analog I/O then clear the appropriate | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | ## ANSEL bit | ||||||||||||||||||||||||||||||||||||||||||||||
| 34 | ## find if the $ipin/$opin is shared with an analog pin | ||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 3 | my ($baud_code, $io_code, $an_code) = ('', '', ''); | |||||||||||||||||||||||||||||||||||||||||||||
| 36 | 3 | ▶ | my $key = $sync ? 'usart' : 'uart'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | ## calculate the Baud rate | ||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 3 | my $baudrate = $baudr; | |||||||||||||||||||||||||||||||||||||||||||||
| 39 | 3 | ▶ | $baudrate = $self->code_config->{$key}->{baud} unless defined $baudr; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 40 | # find closest approximation of baud-rate | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | # if baud-rate not given assume 9600 | ||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 3 | ▶ | my $f_osc = $self->code_config->{$key}->{f_osc} || $self->f_osc; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->code_config->{$key}{'f_osc'} || $self->f_osc
MC/DC0%0/2
$self->code_config->{$key}{'f_osc'} || $self->f_osc
$self->code_config->{$key}{'f_osc'}
$self->f_osc
Truth table33%1/3
A || B
A= $self->code_config->{$key}{'f_osc'}B= $self->f_osc
| |||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 3 | my $baudref = $self->usart_baudrates($baudrate, $f_osc, $sync); | |||||||||||||||||||||||||||||||||||||||||||||
| 44 | 3 | ▶ | unless (ref $baudref eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | carp "Baud rate $baudrate cannot be supported"; | |||||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 3 | my $spbrgh = sprintf "0x%02X", (($baudref->{SPBRG} >> 8) & 0xFF); | |||||||||||||||||||||||||||||||||||||||||||||
| 49 | 3 | my $spbrg = sprintf "0x%02X", ($baudref->{SPBRG} & 0xFF); | |||||||||||||||||||||||||||||||||||||||||||||
| 50 | 3 | my $baudctl_code = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 51 | 3 | ▶ | if ($baudref->{BRG16}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | $baudctl_code .= "\tbanksel BAUDCTL\n\tbsf BAUDCTL, BRG16\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 53 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 3 | $baudctl_code .= "\tbanksel BAUDCTL\n\tbcf BAUDCTL, BRG16\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 3 | ▶ | if ($baudref->{BRGH}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 3 | $baudctl_code .= "\tbanksel TXSTA\n\tbsf TXSTA, BRGH\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 58 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | $baudctl_code .= "\tbanksel TXSTA\n\tbcf TXSTA, BRGH\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 3 | chomp $baudctl_code; | |||||||||||||||||||||||||||||||||||||||||||||
| 62 | 3 | my $cbaud = sprintf "%0.04f", $baudref->{actual}; | |||||||||||||||||||||||||||||||||||||||||||||
| 63 | 3 | my $ebaud = sprintf "%0.06f%%", $baudref->{error}; | |||||||||||||||||||||||||||||||||||||||||||||
| 64 | 3 | $baud_code .= <<"..."; | |||||||||||||||||||||||||||||||||||||||||||||
| 65 | ;;;Desired Baud: $baudref->{baud} | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | ;;;Calculated Baud: $cbaud | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | ;;;Error: $ebaud | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | ;;;SPBRG: $baudref->{SPBRG} | ||||||||||||||||||||||||||||||||||||||||||||||
| 69 | ;;;BRG16: $baudref->{BRG16} | ||||||||||||||||||||||||||||||||||||||||||||||
| 70 | ;;;BRGH: $baudref->{BRGH} | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | $baudctl_code | ||||||||||||||||||||||||||||||||||||||||||||||
| 72 | \tbanksel SPBRG | ||||||||||||||||||||||||||||||||||||||||||||||
| 73 | \tmovlw $spbrgh | ||||||||||||||||||||||||||||||||||||||||||||||
| 74 | \tmovwf SPBRGH | ||||||||||||||||||||||||||||||||||||||||||||||
| 75 | \tmovlw $spbrg | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | \tmovwf SPBRG | ||||||||||||||||||||||||||||||||||||||||||||||
| 77 | ... | ||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 3 | ▶ | if (exists $self->registers->{ANSEL}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 3 | my $ipin_no = $self->pins->{$ipin}; | |||||||||||||||||||||||||||||||||||||||||||||
| 80 | 3 | my $opin_no = $self->pins->{$opin}; | |||||||||||||||||||||||||||||||||||||||||||||
| 81 | 3 | my $iallpins = $self->pins->{$ipin_no}; | |||||||||||||||||||||||||||||||||||||||||||||
| 82 | 3 | my $oallpins = $self->pins->{$opin_no}; | |||||||||||||||||||||||||||||||||||||||||||||
| 83 | 3 | ▶ | unless (ref $iallpins eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | carp "Invalid data for pin $ipin_no"; | |||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 3 | ▶ | unless (ref $oallpins eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | carp "Invalid data for pin $opin_no"; | |||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 3 | my @anpins = (); | |||||||||||||||||||||||||||||||||||||||||||||
| 92 | 3 | foreach (@$iallpins) { | |||||||||||||||||||||||||||||||||||||||||||||
| 93 | 12 | ▶ | push @anpins, $_ if exists $self->analog_pins->{$_}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 3 | foreach (@$oallpins) { | |||||||||||||||||||||||||||||||||||||||||||||
| 96 | 9 | ▶ | push @anpins, $_ if exists $self->analog_pins->{$_}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 3 | my $pansel = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 99 | 3 | foreach (sort @anpins) { | |||||||||||||||||||||||||||||||||||||||||||||
| 100 | 3 | my ($pno, $pbit) = @{$self->analog_pins->{$_}}; | |||||||||||||||||||||||||||||||||||||||||||||
| 101 | 3 | my $ansel = 'ANSEL'; | |||||||||||||||||||||||||||||||||||||||||||||
| 102 | 3 | ▶ | if (exists $self->registers->{ANSELH}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 3 | ▶ | $ansel = ($pbit >= 8) ? 'ANSELH' : 'ANSEL'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 3 | ▶ | if ($ansel ne $pansel) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 3 | $an_code .= "\tbanksel $ansel\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 107 | 3 | $pansel = $ansel; | |||||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 3 | $an_code .= "\tbcf $ansel, ANS$pbit\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 3 | ▶ | unless (exists $self->registers->{TXSTA} and | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $self->registers->{"TXSTA"} and exists $self->registers->{"RCSTA"}
MC/DC0%0/2
exists $self->registers->{"TXSTA"} and exists $self->registers->{"RCSTA"}
exists $self->registers->{"TXSTA"}
exists $self->registers->{"RCSTA"}
Truth table33%1/3
A and B
A= exists $self->registers->{"TXSTA"}B= exists $self->registers->{"RCSTA"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 113 | exists $self->registers->{RCSTA}) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | carp "Register TXSTA & RCSTA are required for operations for $outp"; | |||||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 3 | ▶ | if ($sync) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 118 | #TODO | ||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | carp "Synchronous operations not implemented\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 3 | $io_code .= <<"..."; | |||||||||||||||||||||||||||||||||||||||||||||
| 123 | \tbanksel TXSTA | ||||||||||||||||||||||||||||||||||||||||||||||
| 124 | \t;; asynchronous operation | ||||||||||||||||||||||||||||||||||||||||||||||
| 125 | \tbcf TXSTA, SYNC | ||||||||||||||||||||||||||||||||||||||||||||||
| 126 | \t;; transmit enable | ||||||||||||||||||||||||||||||||||||||||||||||
| 127 | \tbsf TXSTA, TXEN | ||||||||||||||||||||||||||||||||||||||||||||||
| 128 | \tbanksel RCSTA | ||||||||||||||||||||||||||||||||||||||||||||||
| 129 | \t;; serial port enable | ||||||||||||||||||||||||||||||||||||||||||||||
| 130 | \tbsf RCSTA, SPEN | ||||||||||||||||||||||||||||||||||||||||||||||
| 131 | \t;; continuous receive enable | ||||||||||||||||||||||||||||||||||||||||||||||
| 132 | \tbsf RCSTA, CREN | ||||||||||||||||||||||||||||||||||||||||||||||
| 133 | $an_code | ||||||||||||||||||||||||||||||||||||||||||||||
| 134 | ... | ||||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 3 | $code = <<"EUSARTCODE"; | |||||||||||||||||||||||||||||||||||||||||||||
| 137 | $baud_code | ||||||||||||||||||||||||||||||||||||||||||||||
| 138 | $io_code | ||||||||||||||||||||||||||||||||||||||||||||||
| 139 | EUSARTCODE | ||||||||||||||||||||||||||||||||||||||||||||||
| 140 | } elsif (exists $self->registers->{SPBRG}) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 141 | ## USART (8-bit) | ||||||||||||||||||||||||||||||||||||||||||||||
| 142 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | carp "$outp for chip ", $self->type, " is not supported"; | |||||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 3 | ▶ | return wantarray ? ($code, $funcs, $macros, $tables) : $code; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 3 | ▶ | sub _usart_var { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_usart_var: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 3 | return <<'...'; | |||||||||||||||||||||||||||||||||||||||||||||
| 151 | ;;;;;;; USART I/O VARS ;;;;;; | ||||||||||||||||||||||||||||||||||||||||||||||
| 152 | VIC_VAR_USART_UDATA udata | ||||||||||||||||||||||||||||||||||||||||||||||
| 153 | VIC_VAR_USART_WLEN res 1 | ||||||||||||||||||||||||||||||||||||||||||||||
| 154 | VIC_VAR_USART_WIDX res 1 | ||||||||||||||||||||||||||||||||||||||||||||||
| 155 | VIC_VAR_USART_RLEN res 1 | ||||||||||||||||||||||||||||||||||||||||||||||
| 156 | VIC_VAR_USART_RIDX res 1 | ||||||||||||||||||||||||||||||||||||||||||||||
| 157 | ... | ||||||||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 5 | ▶ | sub _usart_write_bytetbl { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_usart_write_bytetbl: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 5 | return <<"...."; | |||||||||||||||||||||||||||||||||||||||||||||
| 162 | m_usart_write_bytetbl macro tblentry, wlen | ||||||||||||||||||||||||||||||||||||||||||||||
| 163 | \tlocal _usart_write_bytetbl_loop_0 | ||||||||||||||||||||||||||||||||||||||||||||||
| 164 | \tlocal _usart_write_bytetbl_loop_1 | ||||||||||||||||||||||||||||||||||||||||||||||
| 165 | \tbanksel VIC_VAR_USART_WLEN | ||||||||||||||||||||||||||||||||||||||||||||||
| 166 | \tmovlw wlen | ||||||||||||||||||||||||||||||||||||||||||||||
| 167 | \tmovwf VIC_VAR_USART_WLEN | ||||||||||||||||||||||||||||||||||||||||||||||
| 168 | \tbanksel VIC_VAR_USART_WIDX | ||||||||||||||||||||||||||||||||||||||||||||||
| 169 | \tclrf VIC_VAR_USART_WIDX | ||||||||||||||||||||||||||||||||||||||||||||||
| 170 | _usart_write_bytetbl_loop_0: | ||||||||||||||||||||||||||||||||||||||||||||||
| 171 | \tmovf VIC_VAR_USART_WIDX, W | ||||||||||||||||||||||||||||||||||||||||||||||
| 172 | \tcall tblentry | ||||||||||||||||||||||||||||||||||||||||||||||
| 173 | \tbanksel TXREG | ||||||||||||||||||||||||||||||||||||||||||||||
| 174 | \tmovwf TXREG | ||||||||||||||||||||||||||||||||||||||||||||||
| 175 | \tbanksel TXSTA | ||||||||||||||||||||||||||||||||||||||||||||||
| 176 | \tbtfss TXSTA, TRMT | ||||||||||||||||||||||||||||||||||||||||||||||
| 177 | \tgoto \$ - 1 | ||||||||||||||||||||||||||||||||||||||||||||||
| 178 | \tbanksel VIC_VAR_USART_WIDX | ||||||||||||||||||||||||||||||||||||||||||||||
| 179 | \tincf VIC_VAR_USART_WIDX, F | ||||||||||||||||||||||||||||||||||||||||||||||
| 180 | \tbcf STATUS, Z | ||||||||||||||||||||||||||||||||||||||||||||||
| 181 | \tbcf STATUS, C | ||||||||||||||||||||||||||||||||||||||||||||||
| 182 | \tmovf VIC_VAR_USART_WIDX, W | ||||||||||||||||||||||||||||||||||||||||||||||
| 183 | \tsubwf VIC_VAR_USART_WLEN, W | ||||||||||||||||||||||||||||||||||||||||||||||
| 184 | \t;; W == 0 | ||||||||||||||||||||||||||||||||||||||||||||||
| 185 | \tbtfsc STATUS, Z | ||||||||||||||||||||||||||||||||||||||||||||||
| 186 | \tgoto _usart_write_bytetbl_loop_1 | ||||||||||||||||||||||||||||||||||||||||||||||
| 187 | \tgoto _usart_write_bytetbl_loop_0 | ||||||||||||||||||||||||||||||||||||||||||||||
| 188 | _usart_write_bytetbl_loop_1: | ||||||||||||||||||||||||||||||||||||||||||||||
| 189 | \t;; finish the sending | ||||||||||||||||||||||||||||||||||||||||||||||
| 190 | \tbanksel TXSTA | ||||||||||||||||||||||||||||||||||||||||||||||
| 191 | \tbtfss TXSTA, TRMT | ||||||||||||||||||||||||||||||||||||||||||||||
| 192 | \tgoto \$ - 1 | ||||||||||||||||||||||||||||||||||||||||||||||
| 193 | \tbanksel VIC_VAR_USART_WIDX | ||||||||||||||||||||||||||||||||||||||||||||||
| 194 | \tclrf VIC_VAR_USART_WIDX | ||||||||||||||||||||||||||||||||||||||||||||||
| 195 | \tclrf VIC_VAR_USART_WLEN | ||||||||||||||||||||||||||||||||||||||||||||||
| 196 | \tendm | ||||||||||||||||||||||||||||||||||||||||||||||
| 197 | .... | ||||||||||||||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 1 | ▶ | sub _usart_write_byte { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_usart_write_byte: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 1 | return <<"...."; | |||||||||||||||||||||||||||||||||||||||||||||
| 202 | m_usart_write_byte macro wvar | ||||||||||||||||||||||||||||||||||||||||||||||
| 203 | \tbanksel wvar | ||||||||||||||||||||||||||||||||||||||||||||||
| 204 | \tmovf wvar, W | ||||||||||||||||||||||||||||||||||||||||||||||
| 205 | \tbanksel TXREG | ||||||||||||||||||||||||||||||||||||||||||||||
| 206 | \tmovwf TXREG | ||||||||||||||||||||||||||||||||||||||||||||||
| 207 | \tbanksel TXSTA | ||||||||||||||||||||||||||||||||||||||||||||||
| 208 | \tbtfss TXSTA, TRMT | ||||||||||||||||||||||||||||||||||||||||||||||
| 209 | \tgoto \$ - 1 | ||||||||||||||||||||||||||||||||||||||||||||||
| 210 | \tendm | ||||||||||||||||||||||||||||||||||||||||||||||
| 211 | .... | ||||||||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 1 | ▶ | sub _usart_write_bytes { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_usart_write_bytes: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 1 | return <<"...."; | |||||||||||||||||||||||||||||||||||||||||||||
| 216 | m_usart_write_bytes macro wvar, wlen | ||||||||||||||||||||||||||||||||||||||||||||||
| 217 | \tlocal _usart_write_bytes_loop_0 | ||||||||||||||||||||||||||||||||||||||||||||||
| 218 | \tlocal _usart_write_bytes_loop_1 | ||||||||||||||||||||||||||||||||||||||||||||||
| 219 | \tbanksel VIC_VAR_USART_WLEN | ||||||||||||||||||||||||||||||||||||||||||||||
| 220 | \tmovlw (wlen - 1) | ||||||||||||||||||||||||||||||||||||||||||||||
| 221 | \tmovwf VIC_VAR_USART_WLEN | ||||||||||||||||||||||||||||||||||||||||||||||
| 222 | \tclrf VIC_VAR_USART_WIDX | ||||||||||||||||||||||||||||||||||||||||||||||
| 223 | \tbanksel wvar | ||||||||||||||||||||||||||||||||||||||||||||||
| 224 | \tmovlw (wvar - 1) ;; load address into FSR | ||||||||||||||||||||||||||||||||||||||||||||||
| 225 | \tmovwf FSR | ||||||||||||||||||||||||||||||||||||||||||||||
| 226 | _usart_write_bytes_loop_0: | ||||||||||||||||||||||||||||||||||||||||||||||
| 227 | \tincf FSR, F ;; increment the FSR pointer | ||||||||||||||||||||||||||||||||||||||||||||||
| 228 | \tmovf INDF, W ;; load byte into register | ||||||||||||||||||||||||||||||||||||||||||||||
| 229 | \tbanksel TXREG | ||||||||||||||||||||||||||||||||||||||||||||||
| 230 | \tmovwf TXREG | ||||||||||||||||||||||||||||||||||||||||||||||
| 231 | \tbanksel TXSTA | ||||||||||||||||||||||||||||||||||||||||||||||
| 232 | \tbtfss TXSTA, TRMT | ||||||||||||||||||||||||||||||||||||||||||||||
| 233 | \tgoto \$ - 1 | ||||||||||||||||||||||||||||||||||||||||||||||
| 234 | \tbanksel VIC_VAR_USART_WIDX | ||||||||||||||||||||||||||||||||||||||||||||||
| 235 | \tincf VIC_VAR_USART_WIDX, F | ||||||||||||||||||||||||||||||||||||||||||||||
| 236 | \tbcf STATUS, Z | ||||||||||||||||||||||||||||||||||||||||||||||
| 237 | \tbcf STATUS, C | ||||||||||||||||||||||||||||||||||||||||||||||
| 238 | \tmovf VIC_VAR_USART_WIDX, W | ||||||||||||||||||||||||||||||||||||||||||||||
| 239 | \tsubwf VIC_VAR_USART_WLEN, W | ||||||||||||||||||||||||||||||||||||||||||||||
| 240 | \t;; W == 0 | ||||||||||||||||||||||||||||||||||||||||||||||
| 241 | \tbtfsc STATUS, Z | ||||||||||||||||||||||||||||||||||||||||||||||
| 242 | \tgoto _usart_write_bytes_loop_1 | ||||||||||||||||||||||||||||||||||||||||||||||
| 243 | \tgoto _usart_write_bytes_loop_0 | ||||||||||||||||||||||||||||||||||||||||||||||
| 244 | _usart_write_bytes_loop_1: | ||||||||||||||||||||||||||||||||||||||||||||||
| 245 | \tbanksel TXSTA | ||||||||||||||||||||||||||||||||||||||||||||||
| 246 | \tbtfss TXSTA, TRMT | ||||||||||||||||||||||||||||||||||||||||||||||
| 247 | \tgoto \$ - 1 | ||||||||||||||||||||||||||||||||||||||||||||||
| 248 | \tbanksel VIC_VAR_USART_WIDX | ||||||||||||||||||||||||||||||||||||||||||||||
| 249 | \tclrf VIC_VAR_USART_WIDX | ||||||||||||||||||||||||||||||||||||||||||||||
| 250 | \tclrf VIC_VAR_USART_WLEN | ||||||||||||||||||||||||||||||||||||||||||||||
| 251 | \tendm | ||||||||||||||||||||||||||||||||||||||||||||||
| 252 | .... | ||||||||||||||||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 1 | ▶ | sub _usart_read_byte { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_usart_read_byte: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 256 | # TODO: check RX9 for 9th-bit | ||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 1 | return <<"..."; | |||||||||||||||||||||||||||||||||||||||||||||
| 258 | m_usart_read_byte macro rvar | ||||||||||||||||||||||||||||||||||||||||||||||
| 259 | \tlocal _usart_read_byte_0 | ||||||||||||||||||||||||||||||||||||||||||||||
| 260 | \tbanksel VIC_VAR_USART_RIDX | ||||||||||||||||||||||||||||||||||||||||||||||
| 261 | \tclrf VIC_VAR_USART_RIDX | ||||||||||||||||||||||||||||||||||||||||||||||
| 262 | \tbanksel PIR1 | ||||||||||||||||||||||||||||||||||||||||||||||
| 263 | \tbtfss PIR1, RCIF | ||||||||||||||||||||||||||||||||||||||||||||||
| 264 | \tgoto \$ - 1 | ||||||||||||||||||||||||||||||||||||||||||||||
| 265 | \tbtfsc RCSTA, OERR | ||||||||||||||||||||||||||||||||||||||||||||||
| 266 | \tbcf RCSTA, CREN | ||||||||||||||||||||||||||||||||||||||||||||||
| 267 | \tbtfsc RCSTA, FERR | ||||||||||||||||||||||||||||||||||||||||||||||
| 268 | \tbcf RCSTA, CREN | ||||||||||||||||||||||||||||||||||||||||||||||
| 269 | _usart_read_byte_0: | ||||||||||||||||||||||||||||||||||||||||||||||
| 270 | \tbanksel RCREG | ||||||||||||||||||||||||||||||||||||||||||||||
| 271 | \tmovf RCREG, W | ||||||||||||||||||||||||||||||||||||||||||||||
| 272 | \tbanksel rvar | ||||||||||||||||||||||||||||||||||||||||||||||
| 273 | \tmovwf rvar | ||||||||||||||||||||||||||||||||||||||||||||||
| 274 | \tbanksel RCSTA | ||||||||||||||||||||||||||||||||||||||||||||||
| 275 | \tbtfss RCSTA, CREN | ||||||||||||||||||||||||||||||||||||||||||||||
| 276 | \tbsf RCSTA, CREN | ||||||||||||||||||||||||||||||||||||||||||||||
| 277 | \tendm | ||||||||||||||||||||||||||||||||||||||||||||||
| 278 | ... | ||||||||||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 7 | ▶ | sub usart_write { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
usart_write: called CC=13 SCAR=26.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 7 | my ($self, $outp, $data) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 283 | 7 | ▶ | return unless $self->doesroles(qw(USART GPIO CodeGen Chip)); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 7 | ▶ | return unless $outp =~ /US?ART/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 7 | ▶ | return unless defined $data; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 7 | my ($code, $funcs, $macros, $tables) = ('', {}, {}, []); | |||||||||||||||||||||||||||||||||||||||||||||
| 287 | # check if $data is a string or value or variable | ||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 7 | my @bytearr = (); | |||||||||||||||||||||||||||||||||||||||||||||
| 289 | 7 | my $nstr; | |||||||||||||||||||||||||||||||||||||||||||||
| 290 | 7 | my $table_entry = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 291 | 7 | my $szvar; | |||||||||||||||||||||||||||||||||||||||||||||
| 292 | 7 | ▶ | if (ref $data eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 5 | ▶ | if (exists $data->{type}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 294 | # this is a variable with data | ||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 1 | ▶ | unless ($data->{type} eq 'string') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 0 | carp "Only string variables can use this part of the code"; | |||||||||||||||||||||||||||||||||||||||||||||
| 297 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 1 | $szvar = $data->{size}; | |||||||||||||||||||||||||||||||||||||||||||||
| 300 | 1 | $data = $data->{name}; | |||||||||||||||||||||||||||||||||||||||||||||
| 301 | 1 | $code .= ";;; sending contents of the variable '$data' of size '$szvar' to $outp\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 302 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 303 | # this is a string | ||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 4 | $nstr = $data->{string}; | |||||||||||||||||||||||||||||||||||||||||||||
| 305 | 4 | my $nstr2 = $nstr; | |||||||||||||||||||||||||||||||||||||||||||||
| 306 | 4 | $nstr2 =~ s/[\n]/\\n/gs; | |||||||||||||||||||||||||||||||||||||||||||||
| 307 | 4 | $nstr2 =~ s/[\r]/\\r/gs; | |||||||||||||||||||||||||||||||||||||||||||||
| 308 | 4 | $code .= ";;; sending the string '$nstr2' to $outp\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 309 | 4 | @bytearr = split //, $nstr; | |||||||||||||||||||||||||||||||||||||||||||||
| 310 | push @$tables, { | ||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 41 | bytes => [(map { sprintf "0x%02X", ord($_) } @bytearr), "0x00"], | |||||||||||||||||||||||||||||||||||||||||||||
| 312 | name => $data->{name}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 4 | comment => "\t;;storing string '$nstr2'", | |||||||||||||||||||||||||||||||||||||||||||||
| 314 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 4 | $table_entry = $data->{name}; | |||||||||||||||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 317 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 2 | ▶ | if (looks_like_number($data)) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 1 | $code .= ";;; sending the number '$data' to $outp in big-endian mode\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 320 | 1 | my $nstr = pack "N", $data; | |||||||||||||||||||||||||||||||||||||||||||||
| 321 | 1 | $nstr =~ s/^\x00{1,3}//g; # remove the beginning nulls | |||||||||||||||||||||||||||||||||||||||||||||
| 322 | 1 | @bytearr = split //, $nstr; | |||||||||||||||||||||||||||||||||||||||||||||
| 323 | 1 | $table_entry = sprintf("_vic_bytes_0x%02X", $data); | |||||||||||||||||||||||||||||||||||||||||||||
| 324 | push @$tables, { | ||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 1 | bytes => [(map { sprintf "0x%02X", ord($_) } @bytearr), "0x00"], | |||||||||||||||||||||||||||||||||||||||||||||
| 326 | name => $table_entry, | ||||||||||||||||||||||||||||||||||||||||||||||
| 327 | comment => "\t;;storing number $data", | ||||||||||||||||||||||||||||||||||||||||||||||
| 328 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 329 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 1 | $code .= ";;; sending the variable '$data' to $outp\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 7 | ▶ | if (@bytearr) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 334 | ## length has to be 1 byte only | ||||||||||||||||||||||||||||||||||||||||||||||
| 335 | ## use TXREG and TRMT bit of TXSTA to check if it is done | ||||||||||||||||||||||||||||||||||||||||||||||
| 336 | ## by polling the TRMT check | ||||||||||||||||||||||||||||||||||||||||||||||
| 337 | ## use DECFSZ to manage the loop | ||||||||||||||||||||||||||||||||||||||||||||||
| 338 | ## use a table to store multiple strings/byte arrays | ||||||||||||||||||||||||||||||||||||||||||||||
| 339 | ## TODO: call store_string() to store the string/array of bytes | ||||||||||||||||||||||||||||||||||||||||||||||
| 340 | ## the best way is to store all strings as temporary variables and | ||||||||||||||||||||||||||||||||||||||||||||||
| 341 | ## send the variable into the functions to be detected appropriately | ||||||||||||||||||||||||||||||||||||||||||||||
| 342 | ## use the dt directive to store each string entry in a table | ||||||||||||||||||||||||||||||||||||||||||||||
| 343 | ## the byte arrays generated by a number can be pushed back using a | ||||||||||||||||||||||||||||||||||||||||||||||
| 344 | ## temporary variable | ||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 5 | ▶ | if (scalar @bytearr > 256) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 0 | carp "Warning: Cannot write more than 256 bytes at a time to $outp. You tried to write ", scalar @bytearr; | |||||||||||||||||||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 348 | 5 | ▶ | my $len = scalar(@bytearr) < 256 ? scalar(@bytearr) : 0xFF; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 5 | $len = sprintf "0x%02X", $len; | |||||||||||||||||||||||||||||||||||||||||||||
| 350 | 5 | $macros->{m_usart_write_bytetbl} = $self->_usart_write_bytetbl; | |||||||||||||||||||||||||||||||||||||||||||||
| 351 | 5 | $code .= <<"..."; | |||||||||||||||||||||||||||||||||||||||||||||
| 352 | ;;;; byte array has length $len | ||||||||||||||||||||||||||||||||||||||||||||||
| 353 | \tm_usart_write_bytetbl $table_entry, $len | ||||||||||||||||||||||||||||||||||||||||||||||
| 354 | ... | ||||||||||||||||||||||||||||||||||||||||||||||
| 355 | } elsif (defined $szvar) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 356 | ## multiple bytes writing | ||||||||||||||||||||||||||||||||||||||||||||||
| 357 | 1 | $macros->{m_usart_write_bytes} = $self->_usart_write_bytes; | |||||||||||||||||||||||||||||||||||||||||||||
| 358 | 1 | $data = uc $data; | |||||||||||||||||||||||||||||||||||||||||||||
| 359 | 1 | $code .= <<"..."; | |||||||||||||||||||||||||||||||||||||||||||||
| 360 | \tm_usart_write_bytes $data, $szvar | ||||||||||||||||||||||||||||||||||||||||||||||
| 361 | ... | ||||||||||||||||||||||||||||||||||||||||||||||
| 362 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 1 | $macros->{m_usart_write_byte} = $self->_usart_write_byte; | |||||||||||||||||||||||||||||||||||||||||||||
| 364 | 1 | $data = uc $data; | |||||||||||||||||||||||||||||||||||||||||||||
| 365 | 1 | $code .= <<"..."; | |||||||||||||||||||||||||||||||||||||||||||||
| 366 | \tm_usart_write_byte $data | ||||||||||||||||||||||||||||||||||||||||||||||
| 367 | ... | ||||||||||||||||||||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 7 | ▶ | return wantarray ? ($code, $funcs, $macros, $tables) : $code; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 1 | ▶ | sub usart_read { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
usart_read: called CC=17 SCAR=36.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 374 | 1 | my $inp = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 375 | 1 | my $var = undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 376 | 1 | my %action = (); | |||||||||||||||||||||||||||||||||||||||||||||
| 377 | 1 | ▶ | if (scalar(@_) == 1) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 0 | $var = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 379 | } elsif (scalar(@_) > 1){ | ||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 1 | %action = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 381 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 382 | 0 | carp 'Invalid invocation of usart_read() function'; | |||||||||||||||||||||||||||||||||||||||||||||
| 383 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 385 | 1 | ▶ | return unless $self->doesroles(qw(USART GPIO CodeGen Chip)); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 386 | 1 | ▶ | return unless $inp =~ /US?ART/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 387 | 1 | my ($code, $funcs, $macros, $tables) = ('', {}, {}, []); | |||||||||||||||||||||||||||||||||||||||||||||
| 388 | 1 | ▶ | unless (defined $var) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 1 | ▶ | if (defined $action{PARAM}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 390 | 1 | $var = $action{PARAM} . '0'; | |||||||||||||||||||||||||||||||||||||||||||||
| 391 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 392 | 0 | carp "Implementation errors implementing the Action block"; | |||||||||||||||||||||||||||||||||||||||||||||
| 393 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 395 | 1 | $var = uc $var; | |||||||||||||||||||||||||||||||||||||||||||||
| 396 | 1 | $macros->{lc("m_read_$var")} = $self->_macro_read_var($var); | |||||||||||||||||||||||||||||||||||||||||||||
| 397 | 1 | ▶ | return unless (defined $action{ACTION} or defined $action{ISR}); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $action{"ACTION"} or defined $action{"ISR"}
MC/DC0%0/2
defined $action{"ACTION"} or defined $action{"ISR"}
defined $action{"ACTION"}
defined $action{"ISR"}
Truth table33%1/3
A or B
A= defined $action{"ACTION"}B= defined $action{"ISR"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 398 | 1 | ▶ | return unless defined $action{END}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 400 | 1 | $macros->{m_usart_read_byte} = $self->_usart_read_byte; | |||||||||||||||||||||||||||||||||||||||||||||
| 401 | 1 | $code .= <<"..."; | |||||||||||||||||||||||||||||||||||||||||||||
| 402 | ;;;; reading single byte on the $inp port | ||||||||||||||||||||||||||||||||||||||||||||||
| 403 | \tm_usart_read_byte $var | ||||||||||||||||||||||||||||||||||||||||||||||
| 404 | ... | ||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 1 | ▶ | if (%action) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 406 | 1 | ▶ | if (exists $action{ACTION}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 407 | 0 | my $action_label = $action{ACTION}; | |||||||||||||||||||||||||||||||||||||||||||||
| 408 | 0 | my $end_label = $action{END}; | |||||||||||||||||||||||||||||||||||||||||||||
| 409 | 0 | $code .= <<"..."; | |||||||||||||||||||||||||||||||||||||||||||||
| 410 | ;;; invoking $action_label | ||||||||||||||||||||||||||||||||||||||||||||||
| 411 | \tgoto $action_label | ||||||||||||||||||||||||||||||||||||||||||||||
| 412 | $end_label:\n | ||||||||||||||||||||||||||||||||||||||||||||||
| 413 | ... | ||||||||||||||||||||||||||||||||||||||||||||||
| 414 | } elsif (exists $action{ISR}) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 415 | 1 | my $pictype = $self->type; | |||||||||||||||||||||||||||||||||||||||||||||
| 416 | 1 | ▶ | unless ($self->doesrole('ISR')) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 417 | 0 | carp "$pictype does not do the ISR role"; | |||||||||||||||||||||||||||||||||||||||||||||
| 418 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 420 | 1 | my $rx_int = $self->usart_pins->{rx_int}; | |||||||||||||||||||||||||||||||||||||||||||||
| 421 | 1 | ▶ | carp "rx_int not defined for $pictype" and return unless $rx_int; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition20%1/5
$rx_int || carp("rx_int not defined for $pictype") && (return)
carp("rx_int not defined for $pictype") and return
MC/DC0%0/2
$rx_int || carp("rx_int not defined for $pictype") && (return)
$rx_int
carp("rx_int not defined for $pictype")
Truth table33%1/3
A || B
A= $rx_intB= carp("rx_int not defined for $pictype")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 422 | 1 | my $isr_label = lc "isr_rx_$inp"; # required by receiver | |||||||||||||||||||||||||||||||||||||||||||||
| 423 | 1 | $code = ";;;; $inp read is done using $isr_label"; | |||||||||||||||||||||||||||||||||||||||||||||
| 424 | 1 | $code .= $self->_usart_isr_setup($inp, $rx_int); | |||||||||||||||||||||||||||||||||||||||||||||
| 425 | 1 | $funcs->{$isr_label} = $self->_usart_isr_read($inp, $rx_int, %action); | |||||||||||||||||||||||||||||||||||||||||||||
| 426 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 427 | 0 | carp "Unknown action requested. Probably a bug in implementation"; | |||||||||||||||||||||||||||||||||||||||||||||
| 428 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 430 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 431 | 1 | ▶ | return wantarray ? ($code, $funcs, $macros, $tables) : $code; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||||||
| 434 | 1 | ▶ | sub _usart_isr_setup { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_usart_isr_setup: called CC=5 SCAR=17.5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 435 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 436 | 1 | my $inp = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 437 | 1 | my $href = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 438 | 1 | ▶ | return unless (defined $href and ref $href eq 'HASH'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $href and ref $href eq "HASH"
MC/DC0%0/2
defined $href and ref $href eq "HASH"
defined $href
ref $href eq "HASH"
Truth table33%1/3
A and B
A= defined $hrefB= ref $href eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 439 | 1 | ▶ | unless (exists $self->registers->{INTCON}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 440 | 0 | carp $self->type, " has no register named INTCON"; | |||||||||||||||||||||||||||||||||||||||||||||
| 441 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 443 | 1 | my $reg = $href->{reg}; | |||||||||||||||||||||||||||||||||||||||||||||
| 444 | 1 | my $enable = $href->{enable}; | |||||||||||||||||||||||||||||||||||||||||||||
| 445 | 1 | my $preg = $href->{preg}; | |||||||||||||||||||||||||||||||||||||||||||||
| 446 | 1 | my $penable = $href->{penable}; | |||||||||||||||||||||||||||||||||||||||||||||
| 447 | 1 | my $code = << "..."; | |||||||||||||||||||||||||||||||||||||||||||||
| 448 | ;;; enable interrupt servicing for $inp | ||||||||||||||||||||||||||||||||||||||||||||||
| 449 | \tbanksel INTCON | ||||||||||||||||||||||||||||||||||||||||||||||
| 450 | \tbsf INTCON, GIE | ||||||||||||||||||||||||||||||||||||||||||||||
| 451 | ... | ||||||||||||||||||||||||||||||||||||||||||||||
| 452 | 1 | ▶ | if ($preg eq 'INTCON') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 453 | 1 | $code .= "\tbsf $preg, $penable\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 454 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 455 | 0 | $code .= "\tbanksel $preg\n\tbsf $preg, $penable\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 456 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 457 | 1 | $code .= << "..."; | |||||||||||||||||||||||||||||||||||||||||||||
| 458 | \tbanksel $reg | ||||||||||||||||||||||||||||||||||||||||||||||
| 459 | \tbsf $reg, $enable | ||||||||||||||||||||||||||||||||||||||||||||||
| 460 | ;;; end of interrupt servicing for $inp | ||||||||||||||||||||||||||||||||||||||||||||||
| 461 | ... | ||||||||||||||||||||||||||||||||||||||||||||||
| 462 | 1 | return $code; | |||||||||||||||||||||||||||||||||||||||||||||
| 463 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||||||||||||||
| 465 | 1 | ▶ | sub _usart_isr_read { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_usart_isr_read: called CC=6 SCAR=18.7
| |||||||||||||||||||||||||||||||||||||||||||||||
| 466 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 467 | 1 | my $inp = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 468 | 1 | my $href = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 469 | 1 | my %isr = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 470 | 1 | ▶ | return unless (defined $href and ref $href eq 'HASH'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $href and ref $href eq "HASH"
MC/DC0%0/2
defined $href and ref $href eq "HASH"
defined $href
ref $href eq "HASH"
Truth table33%1/3
A and B
A= defined $hrefB= ref $href eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 471 | 1 | ▶ | return unless (defined $isr{ISR} and $isr{END}); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $isr{"ISR"} and $isr{"END"}
MC/DC0%0/2
defined $isr{"ISR"} and $isr{"END"}
defined $isr{"ISR"}
$isr{"END"}
Truth table33%1/3
A and B
A= defined $isr{"ISR"}B= $isr{"END"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 472 | 1 | my $reg = $href->{reg}; | |||||||||||||||||||||||||||||||||||||||||||||
| 473 | 1 | my $enable = $href->{enable}; | |||||||||||||||||||||||||||||||||||||||||||||
| 474 | 1 | my $flag = $href->{flag}; | |||||||||||||||||||||||||||||||||||||||||||||
| 475 | 1 | my $begin_label = $isr{ISR}; | |||||||||||||||||||||||||||||||||||||||||||||
| 476 | 1 | my $end_label = $isr{END}; | |||||||||||||||||||||||||||||||||||||||||||||
| 477 | 1 | my $isr_var_code = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 478 | 1 | ▶ | if (defined $isr{PARAM}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 479 | 1 | my $ivar = uc ($isr{PARAM} . '0'); | |||||||||||||||||||||||||||||||||||||||||||||
| 480 | 1 | $isr_var_code = "\tbanksel $ivar\n\tmovwf $ivar\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 482 | 1 | my $isr_label = lc "_isr_rx_$inp"; # required by receiver to be this way | |||||||||||||||||||||||||||||||||||||||||||||
| 483 | 1 | return << "...."; | |||||||||||||||||||||||||||||||||||||||||||||
| 484 | $isr_label: | ||||||||||||||||||||||||||||||||||||||||||||||
| 485 | \tbanksel $reg | ||||||||||||||||||||||||||||||||||||||||||||||
| 486 | \tbtfss $reg, $flag | ||||||||||||||||||||||||||||||||||||||||||||||
| 487 | \tgoto $end_label | ||||||||||||||||||||||||||||||||||||||||||||||
| 488 | \tbtfsc RCSTA, OERR | ||||||||||||||||||||||||||||||||||||||||||||||
| 489 | \tbcf RCSTA, CREN | ||||||||||||||||||||||||||||||||||||||||||||||
| 490 | \tbtfsc RCSTA, FERR | ||||||||||||||||||||||||||||||||||||||||||||||
| 491 | \tbcf RCSTA, CREN | ||||||||||||||||||||||||||||||||||||||||||||||
| 492 | \tbanksel RCREG | ||||||||||||||||||||||||||||||||||||||||||||||
| 493 | \tmovf RCREG, W | ||||||||||||||||||||||||||||||||||||||||||||||
| 494 | $isr_var_code | ||||||||||||||||||||||||||||||||||||||||||||||
| 495 | \tbanksel RCSTA | ||||||||||||||||||||||||||||||||||||||||||||||
| 496 | \tbtfss RCSTA, CREN | ||||||||||||||||||||||||||||||||||||||||||||||
| 497 | \tbsf RCSTA, CREN | ||||||||||||||||||||||||||||||||||||||||||||||
| 498 | \tgoto $begin_label | ||||||||||||||||||||||||||||||||||||||||||||||
| 499 | $end_label: | ||||||||||||||||||||||||||||||||||||||||||||||
| 500 | .... | ||||||||||||||||||||||||||||||||||||||||||||||
| 501 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||||||||||||||||
| 503 | 31 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 504 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||