Embedded Template Library 1.0
Loading...
Searching...
No Matches
io_port.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2014 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_IO_PORT_INCLUDED
32#define ETL_IO_PORT_INCLUDED
33
37
38#include "platform.h"
39#include "nullptr.h"
40#include "iterator.h"
41
42#include <stdint.h>
43
44namespace etl
45{
46 //***************************************************************************
48 //***************************************************************************
49 template <typename T, uintptr_t ADDRESS = 0>
50 class io_port_rw : public etl::iterator<ETL_OR_STD::forward_iterator_tag, T>
51 {
52 public:
53
54 typedef volatile T* pointer;
55 typedef volatile const T* const_pointer;
56 typedef volatile T& reference;
57 typedef volatile const T& const_reference;
58
60 operator T() const
61 {
62 return *reinterpret_cast<const_pointer>(ADDRESS);
63 }
64
66 T read() const
67 {
68 return *reinterpret_cast<const_pointer>(ADDRESS);
69 }
70
73 {
74 *reinterpret_cast<pointer>(ADDRESS) = value_;
75 }
76
79 {
80 *reinterpret_cast<pointer>(ADDRESS) = value_;
81 return *this;
82 }
83
86 {
87 return *reinterpret_cast<pointer>(ADDRESS);
88 }
89
92 {
93 return *reinterpret_cast<const_pointer>(ADDRESS);
94 }
95
98 {
99 return *this;
100 }
101
104 {
105 return *this;
106 }
107
110 {
111 return reinterpret_cast<pointer>(ADDRESS);
112 }
113
116 {
117 return reinterpret_cast<const_pointer>(ADDRESS);
118 }
119
120 private:
121
124 };
125
126 //***************************************************************************
128 //***************************************************************************
129 template <typename T, uintptr_t ADDRESS = 0>
130 class io_port_ro : public etl::iterator<ETL_OR_STD::input_iterator_tag, T>
131 {
132 public:
133
134 typedef volatile T* pointer;
135 typedef volatile const T* const_pointer;
136 typedef volatile T& reference;
137 typedef volatile const T& const_reference;
138
140 operator T() const
141 {
142 return *reinterpret_cast<const_pointer>(ADDRESS);
143 }
144
146 T read() const
147 {
148 return *reinterpret_cast<const_pointer>(ADDRESS);
149 }
150
153 {
154 return *reinterpret_cast<const_pointer>(ADDRESS);
155 }
156
159 {
160 return *this;
161 }
162
165 {
166 return *this;
167 }
168
171 {
172 return reinterpret_cast<pointer>(ADDRESS);
173 }
174
177 {
178 return reinterpret_cast<const_pointer>(ADDRESS);
179 }
180
181 private:
182
184 void operator =(T value);
185
188 };
189
190 //***************************************************************************
192 //***************************************************************************
193 template <typename T, uintptr_t ADDRESS = 0>
194 class io_port_wo : public etl::iterator<ETL_OR_STD::output_iterator_tag, T>
195 {
196 public:
197
198 typedef volatile T* pointer;
199 typedef volatile const T* const_pointer;
200 typedef volatile T& reference;
201 typedef volatile const T& const_reference;
202
204 void operator =(T value)
205 {
206 *reinterpret_cast<pointer>(ADDRESS) = value;
207 }
208
211 {
212 *reinterpret_cast<pointer>(ADDRESS) = value_;
213 }
214
217 {
218 return *this;
219 }
220
223 {
224 return *this;
225 }
226
229 {
230 return *this;
231 }
232
235 {
236 return reinterpret_cast<pointer>(ADDRESS);
237 }
238
241 {
242 return reinterpret_cast<const_pointer>(ADDRESS);
243 }
244
245 private:
246
248 operator T() const;
249
252 };
253
254 //***************************************************************************
256 //***************************************************************************
257 template <typename T, uintptr_t ADDRESS = 0>
258 class io_port_wos : public etl::iterator<ETL_OR_STD::forward_iterator_tag, T>
259 {
260 public:
261
262 typedef volatile T* pointer;
263 typedef volatile const T* const_pointer;
264 typedef volatile T& reference;
265 typedef volatile const T& const_reference;
266
269 : shadow_value(T())
270 {
271 }
272
274 operator T() const
275 {
276 return shadow_value;
277 }
278
280 T read() const
281 {
282 return shadow_value;
283 }
284
287 {
288 shadow_value = value_;
289 *reinterpret_cast<pointer>(ADDRESS) = shadow_value;
290 }
291
294 {
295 shadow_value = value_;
296 *reinterpret_cast<pointer>(ADDRESS) = shadow_value;
297 return *this;
298 }
299
302 {
303 return *this;
304 }
305
308 {
309 return shadow_value;
310 }
311
314 {
315 return *this;
316 }
317
320 {
321 return *this;
322 }
323
326 {
327 return reinterpret_cast<pointer>(ADDRESS);
328 }
329
330 private:
331
334
335 T shadow_value;
336 };
337
338 //***************************************************************************
341 //***************************************************************************
342 template <typename T>
343 class io_port_rw<T, 0> : public etl::iterator<ETL_OR_STD::forward_iterator_tag, T>
344 {
345 public:
346
347 typedef volatile T* pointer;
348 typedef volatile const T* const_pointer;
349 typedef volatile T& reference;
350 typedef volatile const T& const_reference;
351
354 : address(ETL_NULLPTR)
355 {
356 }
357
361 {
362 }
363
366 : address(reinterpret_cast<pointer>(other_.address))
367 {
368 }
369
372 {
373 address = other_.address;
374 return *this;
375 }
376
379 {
380 address = reinterpret_cast<pointer>(address_);
381 }
382
385 {
386 return address;
387 }
388
391 {
392 return address;
393 }
394
396 operator T() const
397 {
398 return *address;
399 }
400
402 T read() const
403 {
404 return *address;
405 }
406
409 {
410 *address = value_;
411 }
412
415 {
416 *address = value_;
417 return *this;
418 }
419
422 {
423 return *address;
424 }
425
428 {
429 return *address;
430 }
431
434 {
435 return *this;
436 }
437
440 {
441 return *this;
442 }
443
444 private:
445
446 pointer address;
447 };
448
449 //***************************************************************************
452 //***************************************************************************
453 template <typename T>
454 class io_port_ro<T, 0> : public etl::iterator<ETL_OR_STD::input_iterator_tag, T>
455 {
456 public:
457
458 typedef volatile T* pointer;
459 typedef volatile const T* const_pointer;
460 typedef volatile T& reference;
461 typedef volatile const T& const_reference;
462
465 : address(ETL_NULLPTR)
466 {
467 }
468
472 {
473 }
474
477 : address(reinterpret_cast<pointer>(other_.address))
478 {
479 }
480
483 {
484 address = other_.address;
485 return *this;
486 }
487
490 {
491 address = reinterpret_cast<pointer>(address_);
492 }
493
496 {
497 return address;
498 }
499
501 operator T() const
502 {
503 return *address;
504 }
505
507 T read() const
508 {
509 return *address;
510 }
511
514 {
515 return *address;
516 }
517
520 {
521 return *this;
522 }
523
526 {
527 return *this;
528 }
529
530 private:
531
533 void operator =(T value);
534
535 pointer address;
536 };
537
538 //***************************************************************************
541 //***************************************************************************
542 template <typename T>
543 class io_port_wo<T, 0> : public etl::iterator<ETL_OR_STD::output_iterator_tag, T>
544 {
545 public:
546
547 typedef volatile T* pointer;
548 typedef volatile const T* const_pointer;
549 typedef volatile T& reference;
550 typedef volatile const T& const_reference;
551
554 : address(ETL_NULLPTR)
555 {
556 }
557
561 {
562 }
563
566 : address(reinterpret_cast<pointer>(other_.address))
567 {
568 }
569
572 {
573 address = other_.address;
574 return *this;
575 }
576
579 {
580 address = reinterpret_cast<pointer>(address_);
581 }
582
585 {
586 return address;
587 }
588
591 {
592 return address;
593 }
594
597 {
598 *address = value_;
599 }
600
602 void operator =(T value)
603 {
604 *address = value;
605 }
606
609 {
610 return *this;
611 }
612
615 {
616 return *this;
617 }
618
621 {
622 return *this;
623 }
624
625 private:
626
628 operator T() const;
629
630 pointer address;
631 };
632
633 //***************************************************************************
636 //***************************************************************************
637 template <typename T>
638 class io_port_wos<T, 0> : public etl::iterator<ETL_OR_STD::forward_iterator_tag, T>
639 {
640 public:
641
642 typedef volatile T* pointer;
643 typedef volatile const T* const_pointer;
644 typedef volatile T& reference;
645 typedef volatile const T& const_reference;
646
647 class iterator : public etl::iterator<ETL_OR_STD::bidirectional_iterator_tag, T>
648 {
649 typedef io_port_wos<T, 0> iop_t;
650
651 public:
652
654 : p_iop(&iop)
655 {
656 }
657
658 iterator(const iterator& other)
659 : p_iop(other.p_iop)
660 {
661 }
662
664 {
665 p_iop = other.p_iop;
666 return *this;
667 }
668
670 {
671 return *p_iop;
672 }
673
674 const iop_t& operator *() const
675 {
676 return *p_iop;
677 }
678
680 {
681 return *this;
682 }
683
685 {
686 return *this;
687 }
688
690 {
691 return *this;
692 }
693
695 {
696 return *this;
697 }
698
699 private:
700
701 iop_t* p_iop;
702 };
703
706 : address(ETL_NULLPTR)
707 {
708 }
709
713 {
714 }
715
718 : shadow_value(other_.shadow_value),
719 address(reinterpret_cast<pointer>(other_.address))
720 {
721 }
722
725 {
726 shadow_value = other_.shadow_value;
727 address = other_.address;
728 return *this;
729 }
730
733 {
734 address = reinterpret_cast<pointer>(address_);
735 }
736
739 {
740 return address;
741 }
742
745 {
746 return address;
747 }
748
751 {
752 return iterator(*this);
753 }
754
756 operator T() const
757 {
758 return shadow_value;
759 }
760
762 T read() const
763 {
764 return shadow_value;
765 }
766
769 {
770 shadow_value = value_;
771 *address = shadow_value;
772 }
773
776 {
777 shadow_value = value_;
778 *address = shadow_value;
779 return *this;
780 }
781
784 {
785 return *this;
786 }
787
790 {
791 return shadow_value;
792 }
793
796 {
797 return *this;
798 }
799
802 {
803 return *this;
804 }
805
806 private:
807
808 T shadow_value;
809 pointer address;
810 };
811}
812
813#endif
void set_address(void *address_)
Set the IO port address.
Definition io_port.h:489
const_pointer get_address() const
Get the IO port address.
Definition io_port.h:495
T read() const
Read.
Definition io_port.h:507
io_port_ro(void *address_)
Constructor.
Definition io_port.h:470
io_port_ro()
Default constructor.
Definition io_port.h:464
io_port_ro(const io_port_ro &other_)
Copy Constructor.
Definition io_port.h:476
Read only port.
Definition io_port.h:131
io_port_ro & operator++()
Increment.
Definition io_port.h:158
T read() const
Read.
Definition io_port.h:146
pointer get_address()
Get the IO port address.
Definition io_port.h:170
const_pointer get_address() const
Get the IO port address.
Definition io_port.h:176
const_reference operator*() const
Read.
Definition io_port.h:152
const_pointer get_address() const
Get the IO port address.
Definition io_port.h:390
pointer get_address()
Get the IO port address.
Definition io_port.h:384
T read() const
Read.
Definition io_port.h:402
io_port_rw(const io_port_rw &other_)
Copy Constructor.
Definition io_port.h:365
void set_address(void *address_)
Set the IO port address.
Definition io_port.h:378
io_port_rw(void *address_)
Constructor.
Definition io_port.h:359
io_port_rw()
Default constructor.
Definition io_port.h:353
void write(T value_)
Write.
Definition io_port.h:408
Read write port.
Definition io_port.h:51
T read() const
Read.
Definition io_port.h:66
reference operator*()
Read / Write.
Definition io_port.h:85
pointer get_address()
Get the IO port address.
Definition io_port.h:109
const_pointer get_address() const
Get the IO port address.
Definition io_port.h:115
io_port_rw & operator=(T value_)
Write.
Definition io_port.h:78
io_port_rw & operator++()
Increment.
Definition io_port.h:97
void write(T value_)
Write.
Definition io_port.h:72
io_port_wo(const io_port_wo &other_)
Copy Constructor.
Definition io_port.h:565
void set_address(void *address_)
Set the IO port address.
Definition io_port.h:578
pointer get_address()
Get the IO port address.
Definition io_port.h:584
io_port_wo(void *address_)
Constructor.
Definition io_port.h:559
void write(T value_)
Write.
Definition io_port.h:596
const_pointer get_address() const
Get the IO port address.
Definition io_port.h:590
io_port_wo()
Default constructor.
Definition io_port.h:553
Write only port.
Definition io_port.h:195
const_pointer get_address() const
Get the IO port address.
Definition io_port.h:240
void write(T value_)
Write.
Definition io_port.h:210
pointer get_address()
Get the IO port address.
Definition io_port.h:234
io_port_wo & operator*()
Write.
Definition io_port.h:216
void operator=(T value)
Write.
Definition io_port.h:204
io_port_wo & operator++()
Increment.
Definition io_port.h:222
Definition io_port.h:639
const_pointer get_address() const
Get the IO port address.
Definition io_port.h:744
iterator get_iterator()
Get the iterator.
Definition io_port.h:750
pointer get_address()
Get the IO port address.
Definition io_port.h:738
void set_address(void *address_)
Set the IO port address.
Definition io_port.h:732
void write(T value_)
Write.
Definition io_port.h:768
io_port_wos(void *address_)
Constructor.
Definition io_port.h:711
io_port_wos()
Default constructor.
Definition io_port.h:705
T read() const
Read.
Definition io_port.h:762
io_port_wos(const io_port_wos &other_)
Copy Constructor.
Definition io_port.h:717
Write only port with shadow register.
Definition io_port.h:259
io_port_wos & operator=(T value_)
Write.
Definition io_port.h:293
void write(T value_)
Write.
Definition io_port.h:286
io_port_wos & operator++()
Increment.
Definition io_port.h:313
T read() const
Read.
Definition io_port.h:280
io_port_wos()
Default constructor.
Definition io_port.h:268
pointer get_address()
Get the IO port address.
Definition io_port.h:325
io_port_wos & operator*()
Read / Write.
Definition io_port.h:301
bitset_ext
Definition absolute.h:38
iterator
Definition iterator.h:399
pair holds two objects of arbitrary type
Definition utility.h:164