Embedded Template Library  1.0
io_port.h
Go to the documentation of this file.
1 
3 /******************************************************************************
4 The MIT License(MIT)
5 
6 Embedded Template Library.
7 https://github.com/ETLCPP/etl
8 https://www.etlcpp.com
9 
10 Copyright(c) 2014 jwellbelove
11 
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files(the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions :
18 
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21 
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29 ******************************************************************************/
30 
31 #ifndef ETL_IO_PORT_INCLUDED
32 #define ETL_IO_PORT_INCLUDED
33 
37 
38 #include <stdint.h>
39 
40 #include "platform.h"
41 #include "nullptr.h"
42 #include "iterator.h"
43 
44 #include "iterator.h"
45 
46 namespace etl
47 {
48  //***************************************************************************
50  //***************************************************************************
51  template <typename T, uintptr_t ADDRESS = 0>
52  class io_port_rw : public etl::iterator<ETL_OR_STD::forward_iterator_tag, T>
53  {
54  public:
55 
56  typedef volatile T* pointer;
57  typedef volatile const T* const_pointer;
58  typedef volatile T& reference;
59  typedef volatile const T& const_reference;
60 
62  operator T() const
63  {
64  return *reinterpret_cast<const_pointer>(ADDRESS);
65  }
66 
68  T read() const
69  {
70  return *reinterpret_cast<const_pointer>(ADDRESS);
71  }
72 
74  void write(T value_)
75  {
76  *reinterpret_cast<pointer>(ADDRESS) = value_;
77  }
78 
81  {
82  *reinterpret_cast<pointer>(ADDRESS) = value_;
83  return *this;
84  }
85 
87  reference operator *()
88  {
89  return *reinterpret_cast<pointer>(ADDRESS);
90  }
91 
93  const_reference operator *() const
94  {
95  return *reinterpret_cast<const_pointer>(ADDRESS);
96  }
97 
100  {
101  return *this;
102  }
103 
106  {
107  return *this;
108  }
109 
111  pointer get_address()
112  {
113  return reinterpret_cast<pointer>(ADDRESS);
114  }
115 
117  const_pointer get_address() const
118  {
119  return reinterpret_cast<const_pointer>(ADDRESS);
120  }
121 
122  private:
123 
126  };
127 
128  //***************************************************************************
130  //***************************************************************************
131  template <typename T, uintptr_t ADDRESS = 0>
132  class io_port_ro : public etl::iterator<ETL_OR_STD::input_iterator_tag, T>
133  {
134  public:
135 
136  typedef volatile T* pointer;
137  typedef volatile const T* const_pointer;
138  typedef volatile T& reference;
139  typedef volatile const T& const_reference;
140 
142  operator T() const
143  {
144  return *reinterpret_cast<const_pointer>(ADDRESS);
145  }
146 
148  T read() const
149  {
150  return *reinterpret_cast<const_pointer>(ADDRESS);
151  }
152 
154  const_reference operator *() const
155  {
156  return *reinterpret_cast<const_pointer>(ADDRESS);
157  }
158 
161  {
162  return *this;
163  }
164 
167  {
168  return *this;
169  }
170 
172  pointer get_address()
173  {
174  return reinterpret_cast<pointer>(ADDRESS);
175  }
176 
178  const_pointer get_address() const
179  {
180  return reinterpret_cast<const_pointer>(ADDRESS);
181  }
182 
183  private:
184 
186  void operator =(T value);
187 
189  io_port_ro& operator =(const io_port_ro&);
190  };
191 
192  //***************************************************************************
194  //***************************************************************************
195  template <typename T, uintptr_t ADDRESS = 0>
196  class io_port_wo : public etl::iterator<ETL_OR_STD::output_iterator_tag, T>
197  {
198  public:
199 
200  typedef volatile T* pointer;
201  typedef volatile const T* const_pointer;
202  typedef volatile T& reference;
203  typedef volatile const T& const_reference;
204 
206  void operator =(T value)
207  {
208  *reinterpret_cast<pointer>(ADDRESS) = value;
209  }
210 
212  void write(T value_)
213  {
214  *reinterpret_cast<pointer>(ADDRESS) = value_;
215  }
216 
219  {
220  return *this;
221  }
222 
225  {
226  return *this;
227  }
228 
231  {
232  return *this;
233  }
234 
236  pointer get_address()
237  {
238  return reinterpret_cast<pointer>(ADDRESS);
239  }
240 
242  const_pointer get_address() const
243  {
244  return reinterpret_cast<const_pointer>(ADDRESS);
245  }
246 
247  private:
248 
250  operator T() const;
251 
254  };
255 
256  //***************************************************************************
258  //***************************************************************************
259  template <typename T, uintptr_t ADDRESS = 0>
260  class io_port_wos : public etl::iterator<ETL_OR_STD::forward_iterator_tag, T>
261  {
262  public:
263 
264  typedef volatile T* pointer;
265  typedef volatile const T* const_pointer;
266  typedef volatile T& reference;
267  typedef volatile const T& const_reference;
268 
271  : shadow_value(T())
272  {
273  }
274 
276  operator T() const
277  {
278  return shadow_value;
279  }
280 
282  T read() const
283  {
284  return shadow_value;
285  }
286 
288  void write(T value_)
289  {
290  shadow_value = value_;
291  *reinterpret_cast<pointer>(ADDRESS) = shadow_value;
292  }
293 
296  {
297  shadow_value = value_;
298  *reinterpret_cast<pointer>(ADDRESS) = shadow_value;
299  return *this;
300  }
301 
304  {
305  return *this;
306  }
307 
309  const_reference operator *() const
310  {
311  return shadow_value;
312  }
313 
316  {
317  return *this;
318  }
319 
322  {
323  return *this;
324  }
325 
327  pointer get_address()
328  {
329  return reinterpret_cast<pointer>(ADDRESS);
330  }
331 
332  private:
333 
336 
337  T shadow_value;
338  };
339 
340  //***************************************************************************
343  //***************************************************************************
344  template <typename T>
345  class io_port_rw<T, 0> : public etl::iterator<ETL_OR_STD::forward_iterator_tag, T>
346  {
347  public:
348 
349  typedef volatile T* pointer;
350  typedef volatile const T* const_pointer;
351  typedef volatile T& reference;
352  typedef volatile const T& const_reference;
353 
356  : address(ETL_NULLPTR)
357  {
358  }
359 
361  io_port_rw(void* address_)
362  : address(reinterpret_cast<pointer>(address_))
363  {
364  }
365 
367  io_port_rw(const io_port_rw& other_)
368  : address(reinterpret_cast<pointer>(other_.address))
369  {
370  }
371 
374  {
375  address = other_.address;
376  return *this;
377  }
378 
380  void set_address(void* address_)
381  {
382  address = reinterpret_cast<pointer>(address_);
383  }
384 
386  pointer get_address()
387  {
388  return address;
389  }
390 
392  const_pointer get_address() const
393  {
394  return address;
395  }
396 
398  operator T() const
399  {
400  return *address;
401  }
402 
404  T read() const
405  {
406  return *address;
407  }
408 
410  void write(T value_)
411  {
412  *address = value_;
413  }
414 
417  {
418  *address = value_;
419  return *this;
420  }
421 
423  reference operator *()
424  {
425  return *address;
426  }
427 
429  const_reference operator *() const
430  {
431  return *address;
432  }
433 
436  {
437  return *this;
438  }
439 
442  {
443  return *this;
444  }
445 
446  private:
447 
448  pointer address;
449  };
450 
451  //***************************************************************************
454  //***************************************************************************
455  template <typename T>
456  class io_port_ro<T, 0> : public etl::iterator<ETL_OR_STD::input_iterator_tag, T>
457  {
458  public:
459 
460  typedef volatile T* pointer;
461  typedef volatile const T* const_pointer;
462  typedef volatile T& reference;
463  typedef volatile const T& const_reference;
464 
467  : address(ETL_NULLPTR)
468  {
469  }
470 
472  io_port_ro(void* address_)
473  : address(reinterpret_cast<pointer>(address_))
474  {
475  }
476 
478  io_port_ro(const io_port_ro& other_)
479  : address(reinterpret_cast<pointer>(other_.address))
480  {
481  }
482 
484  io_port_ro& operator =(const io_port_ro& other_)
485  {
486  address = other_.address;
487  return *this;
488  }
489 
491  void set_address(void* address_)
492  {
493  address = reinterpret_cast<pointer>(address_);
494  }
495 
497  const_pointer get_address() const
498  {
499  return address;
500  }
501 
503  operator T() const
504  {
505  return *address;
506  }
507 
509  T read() const
510  {
511  return *address;
512  }
513 
515  const_reference operator *() const
516  {
517  return *address;
518  }
519 
522  {
523  return *this;
524  }
525 
528  {
529  return *this;
530  }
531 
532  private:
533 
535  void operator =(T value);
536 
537  pointer address;
538  };
539 
540  //***************************************************************************
543  //***************************************************************************
544  template <typename T>
545  class io_port_wo<T, 0> : public etl::iterator<ETL_OR_STD::output_iterator_tag, T>
546  {
547  public:
548 
549  typedef volatile T* pointer;
550  typedef volatile const T* const_pointer;
551  typedef volatile T& reference;
552  typedef volatile const T& const_reference;
553 
556  : address(ETL_NULLPTR)
557  {
558  }
559 
561  io_port_wo(void* address_)
562  : address(reinterpret_cast<pointer>(address_))
563  {
564  }
565 
567  io_port_wo(const io_port_wo& other_)
568  : address(reinterpret_cast<pointer>(other_.address))
569  {
570  }
571 
574  {
575  address = other_.address;
576  return *this;
577  }
578 
580  void set_address(void* address_)
581  {
582  address = reinterpret_cast<pointer>(address_);
583  }
584 
586  pointer get_address()
587  {
588  return address;
589  }
590 
592  const_pointer get_address() const
593  {
594  return address;
595  }
596 
598  void write(T value_)
599  {
600  *address = value_;
601  }
602 
604  void operator =(T value)
605  {
606  *address = value;
607  }
608 
611  {
612  return *this;
613  }
614 
617  {
618  return *this;
619  }
620 
623  {
624  return *this;
625  }
626 
627  private:
628 
630  operator T() const;
631 
632  pointer address;
633  };
634 
635  //***************************************************************************
638  //***************************************************************************
639  template <typename T>
640  class io_port_wos<T, 0> : public etl::iterator<ETL_OR_STD::forward_iterator_tag, T>
641  {
642  public:
643 
644  typedef volatile T* pointer;
645  typedef volatile const T* const_pointer;
646  typedef volatile T& reference;
647  typedef volatile const T& const_reference;
648 
649  class iterator : public etl::iterator<ETL_OR_STD::bidirectional_iterator_tag, T>
650  {
651  typedef io_port_wos<T, 0> iop_t;
652 
653  public:
654 
655  iterator(iop_t& iop)
656  : p_iop(&iop)
657  {
658  }
659 
660  iterator(const iterator& other)
661  : p_iop(other.p_iop)
662  {
663  }
664 
665  iterator& operator =(const iterator& other)
666  {
667  p_iop = other.p_iop;
668  return *this;
669  }
670 
671  iop_t& operator *()
672  {
673  return *p_iop;
674  }
675 
676  const iop_t& operator *() const
677  {
678  return *p_iop;
679  }
680 
682  {
683  return *this;
684  }
685 
686  iterator operator ++(int)
687  {
688  return *this;
689  }
690 
691  iterator& operator --()
692  {
693  return *this;
694  }
695 
696  iterator operator --(int)
697  {
698  return *this;
699  }
700 
701  private:
702 
703  iop_t* p_iop;
704  };
705 
708  : address(ETL_NULLPTR)
709  {
710  }
711 
713  io_port_wos(void* address_)
714  : address(reinterpret_cast<pointer>(address_))
715  {
716  }
717 
719  io_port_wos(const io_port_wos& other_)
720  : shadow_value(other_.shadow_value),
721  address(reinterpret_cast<pointer>(other_.address))
722  {
723  }
724 
727  {
728  shadow_value = other_.shadow_value;
729  address = other_.address;
730  return *this;
731  }
732 
734  void set_address(void* address_)
735  {
736  address = reinterpret_cast<pointer>(address_);
737  }
738 
740  pointer get_address()
741  {
742  return address;
743  }
744 
746  const_pointer get_address() const
747  {
748  return address;
749  }
750 
753  {
754  return iterator(*this);
755  }
756 
758  operator T() const
759  {
760  return shadow_value;
761  }
762 
764  T read() const
765  {
766  return shadow_value;
767  }
768 
770  void write(T value_)
771  {
772  shadow_value = value_;
773  *address = shadow_value;
774  }
775 
778  {
779  shadow_value = value_;
780  *address = shadow_value;
781  return *this;
782  }
783 
786  {
787  return *this;
788  }
789 
791  const_reference operator *() const
792  {
793  return shadow_value;
794  }
795 
798  {
799  return *this;
800  }
801 
804  {
805  return *this;
806  }
807 
808  private:
809 
810  T shadow_value;
811  pointer address;
812  };
813 }
814 
815 #endif
void set_address(void *address_)
Set the IO port address.
Definition: io_port.h:491
const_pointer get_address() const
Get the IO port address.
Definition: io_port.h:497
T read() const
Read.
Definition: io_port.h:509
io_port_ro(void *address_)
Constructor.
Definition: io_port.h:472
io_port_ro()
Default constructor.
Definition: io_port.h:466
io_port_ro(const io_port_ro &other_)
Copy Constructor.
Definition: io_port.h:478
Read only port.
Definition: io_port.h:133
T read() const
Read.
Definition: io_port.h:148
pointer get_address()
Get the IO port address.
Definition: io_port.h:172
io_port_ro & operator++()
Increment.
Definition: io_port.h:160
const_pointer get_address() const
Get the IO port address.
Definition: io_port.h:178
const_reference operator*() const
Read.
Definition: io_port.h:154
const_pointer get_address() const
Get the IO port address.
Definition: io_port.h:392
pointer get_address()
Get the IO port address.
Definition: io_port.h:386
T read() const
Read.
Definition: io_port.h:404
io_port_rw(const io_port_rw &other_)
Copy Constructor.
Definition: io_port.h:367
void set_address(void *address_)
Set the IO port address.
Definition: io_port.h:380
io_port_rw(void *address_)
Constructor.
Definition: io_port.h:361
io_port_rw()
Default constructor.
Definition: io_port.h:355
void write(T value_)
Write.
Definition: io_port.h:410
Read write port.
Definition: io_port.h:53
T read() const
Read.
Definition: io_port.h:68
reference operator*()
Read / Write.
Definition: io_port.h:87
io_port_rw & operator++()
Increment.
Definition: io_port.h:99
pointer get_address()
Get the IO port address.
Definition: io_port.h:111
const_pointer get_address() const
Get the IO port address.
Definition: io_port.h:117
io_port_rw & operator=(T value_)
Write.
Definition: io_port.h:80
void write(T value_)
Write.
Definition: io_port.h:74
io_port_wo(const io_port_wo &other_)
Copy Constructor.
Definition: io_port.h:567
void set_address(void *address_)
Set the IO port address.
Definition: io_port.h:580
pointer get_address()
Get the IO port address.
Definition: io_port.h:586
io_port_wo(void *address_)
Constructor.
Definition: io_port.h:561
void write(T value_)
Write.
Definition: io_port.h:598
const_pointer get_address() const
Get the IO port address.
Definition: io_port.h:592
io_port_wo()
Default constructor.
Definition: io_port.h:555
Write only port.
Definition: io_port.h:197
io_port_wo & operator*()
Write.
Definition: io_port.h:218
const_pointer get_address() const
Get the IO port address.
Definition: io_port.h:242
void write(T value_)
Write.
Definition: io_port.h:212
pointer get_address()
Get the IO port address.
Definition: io_port.h:236
io_port_wo & operator++()
Increment.
Definition: io_port.h:224
void operator=(T value)
Write.
Definition: io_port.h:206
Definition: io_port.h:641
const_pointer get_address() const
Get the IO port address.
Definition: io_port.h:746
iterator get_iterator()
Get the iterator.
Definition: io_port.h:752
pointer get_address()
Get the IO port address.
Definition: io_port.h:740
void set_address(void *address_)
Set the IO port address.
Definition: io_port.h:734
void write(T value_)
Write.
Definition: io_port.h:770
io_port_wos(void *address_)
Constructor.
Definition: io_port.h:713
io_port_wos()
Default constructor.
Definition: io_port.h:707
T read() const
Read.
Definition: io_port.h:764
io_port_wos(const io_port_wos &other_)
Copy Constructor.
Definition: io_port.h:719
Write only port with shadow register.
Definition: io_port.h:261
void write(T value_)
Write.
Definition: io_port.h:288
io_port_wos & operator*()
Read / Write.
Definition: io_port.h:303
io_port_wos & operator=(T value_)
Write.
Definition: io_port.h:295
T read() const
Read.
Definition: io_port.h:282
io_port_wos()
Default constructor.
Definition: io_port.h:270
pointer get_address()
Get the IO port address.
Definition: io_port.h:327
io_port_wos & operator++()
Increment.
Definition: io_port.h:315
Definition: absolute.h:37
iterator
Definition: iterator.h:422