File Coverage

blib/lib/Device/Chip/Adapter/UART.pm
Criterion Covered Total %
statement 18 53 33.9
branch 0 10 0.0
condition 0 6 0.0
subroutine 6 16 37.5
pod 7 10 70.0
total 31 95 32.6


line stmt bran cond sub pod time code
1             # You may distribute under the terms of either the GNU General Public License
2             # or the Artistic License (the same terms as Perl itself)
3             #
4             # (C) Paul Evans, 2018-2019 -- leonerd@leonerd.org.uk
5              
6             package Device::Chip::Adapter::UART;
7              
8 1     1   682 use strict;
  1         2  
  1         29  
9 1     1   5 use warnings;
  1         2  
  1         28  
10 1     1   5 use base qw( Device::Chip::Adapter );
  1         2  
  1         543  
11              
12             our $VERSION = '0.01';
13              
14 1     1   14023 use Carp;
  1         3  
  1         64  
15              
16 1     1   8 use Future;
  1         2  
  1         18  
17 1     1   470 use IO::Termios;
  1         24734  
  1         713  
18              
19             =head1 NAME
20              
21             C - a C implementation for
22             serial devices
23              
24             =head1 DESCRIPTION
25              
26             This class implements the L interface around a regular
27             serial port, such as a USB UART adapter, allowing an instance of a
28             L driver to communicate with actual chip hardware using this
29             adapter.
30              
31             At present, this adapter only provides the C protocol as a wrapper
32             around the modem control and handshaking lines. A future version will also
33             provide access to the actual transmit and receive data, once a suitable
34             interface is designed.
35              
36             =cut
37              
38             =head1 CONSTRUCTOR
39              
40             =cut
41              
42             =head2 new
43              
44             $adapter = Device::Chip::Adapter::UART->new( %args )
45              
46             Returns a new instance of a C.
47              
48             Takes the following named arguments:
49              
50             =over 4
51              
52             =item dev => STRING
53              
54             Path to the device node representing the UART; usually something like
55             F or F.
56              
57             =back
58              
59             =cut
60              
61             sub new
62             {
63 0     0 1   my $class = shift;
64 0           my %args = @_;
65              
66 0 0         my $termios = IO::Termios->open( $args{dev} ) or
67             die "Cannot open $args{dev} - $!";
68              
69 0           return bless {
70             termios => $termios,
71              
72             # protocol defaults
73             bits => 8,
74             parity => "n",
75             stop => 1,
76             }, $class;
77             }
78              
79             sub new_from_description
80             {
81 0     0 1   my $class = shift;
82 0           my %args = @_;
83 0           return $class->new( map { $_ => $args{$_} } qw( dev ) );
  0            
84             }
85              
86             =head1 PROTOCOLS
87              
88             The following C protocol types are supported
89              
90             =over 2
91              
92             =item *
93              
94             GPIO
95              
96             =back
97              
98             =cut
99              
100 0     0 0   sub make_protocol_GPIO { return Future->done( $_[0] ) }
101 0     0 0   sub make_protocol_UART { return Future->done( $_[0] ) }
102              
103             # Protocol implementation
104              
105             my %GPIOS_READ = (
106             DSR => 1,
107             CTS => 1,
108             CD => 1,
109             RI => 1,
110             );
111              
112             sub configure
113             {
114 0     0 1   my $self = shift;
115 0           my %args = @_;
116              
117             defined $args{$_} and $self->{$_} = delete $args{$_}
118 0   0       for qw( baud bits parity stop );
119              
120 0 0         keys %args and
121             croak "Unrecognised configure options: " . join( ", ", keys %args );
122              
123             $self->{termios}->set_mode( join ",",
124 0           @{$self}{qw( baud bits parity stop )}
  0            
125             );
126              
127 0           return Future->done;
128             }
129              
130             sub list_gpios
131             {
132 0     0 1   return qw( DTR DSR RTS CTS CD RI );
133             }
134              
135             sub meta_gpios
136             {
137             return map {
138 0 0   0 0   $GPIOS_READ{$_} ?
  0            
139             Device::Chip::Adapter::GPIODefinition( $_, "r", 1 ) :
140             Device::Chip::Adapter::GPIODefinition( $_, "w", 1 );
141             } shift->list_gpios;
142             }
143              
144             sub read_gpios
145             {
146 0     0 1   my $self = shift;
147 0           my ( $gpios ) = @_;
148              
149 0           my $values = $self->{termios}->get_modem();
150              
151 0           my %ret;
152              
153 0           foreach my $gpio ( @$gpios ) {
154 0 0         $ret{$gpio} = $values->{lc $gpio} if $GPIOS_READ{$gpio};
155             }
156              
157 0           return Future->done( \%ret );
158             }
159              
160             sub write_gpios
161             {
162 0     0 1   my $self = shift;
163 0           my ( $gpios ) = @_;
164              
165 0           my %set;
166             defined $gpios->{$_} and $set{lc $_} = $gpios->{$_}
167 0   0       for qw( DTR RTS );
168              
169 0 0         if( %set ) {
170 0           $self->{termios}->set_modem( \%set );
171             }
172              
173 0           return Future->done;
174             }
175              
176             sub tris_gpios
177             {
178             # ignore
179 0     0 1   Future->done;
180             }
181              
182             =head1 AUTHOR
183              
184             Paul Evans
185              
186             =cut
187              
188             0x55AA;