forked from thanks4opensource/regbits
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.html
1957 lines (1430 loc) · 99.6 KB
/
README.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<h1>regbits: C++ templates for type-safe bit manipulation</h1>
<p><strong>regbits</strong> is a C++ template library for type-safe manipulation of bits in microcontroller peripheral registers. Compiled executable object code equals or outperforms traditional techniques in terms of size and speed, and complexity of header files describing peripheral/register layout is comparable.</p>
<h2>Contents <a name="contents"></a></h2>
<ul>
<li><a href="#license">License</a></li>
<li><a href="#what_does_regbits_do">What does regbits do?</a></li>
<li><a href="#what_is_regbits_for">What is regbits for?</a></li>
<li><a href="#how_is_regbits_used">How is regbits used?</a></li>
<li><a href="#how_is_regbits_implemented?">How is regbits implemented?</a></li>
<li><a href="#comparison_with_other_approaches">Comparison with other approaches</a>
<ul>
<li><a href="#raw_numeric_values">Raw numeric values</a></li>
<li><a href="#preprocessor_symbolic_peripheral_addresses_and_register_offsets">Preprocessor symbolic peripheral addresses and register offsets</a></li>
<li><a href="#preprocessor_symbolic_bit_definitions">Preprocessor symbolic bit definitions</a></li>
<li><a href="#static_consts">Static consts</a></li>
<li><a href="#c_cxx_enums">C/C++ enums</a></li>
<li><a href="#c_structs">C structs</a></li>
<li><a href="#c_bitfields">C bitfields</a></li>
<li><a href="#helper_functions">Helper functions</a></li>
<li><a href="#Higher-level_wrappers">Higher-level wrappers</a></li>
</ul>
</li>
<li><a href="#requirements_limitations_problems">Requirements, limitations, problems</a></li>
<li><a href="#unit_test">Unit test</a>
<ul>
<li><a href="#type_safe_error_detection">Type-safe error detection</a></li>
<li><a href="#computational_correctness">Computational correctness</a></li>
<li><a href="#performance_claims">Performance claims</a></li>
</ul>
</li>
<li><a href="#future_enhancements">Future enhancements</a></li>
</ul>
<h2>License <a name="license"></a></h2>
<p>regbits: C++ templates for type-safe bit manipulation</p>
<p>Copyright © 2019 Mark R. Rubin</p>
<p>This file is part of regbits.</p>
<p>The regbits program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.</p>
<p>The regbits program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.</p>
<p>You should have received a copy of the <a href="LICENSE.txt">GNU General Public License</a> along with the regbits program. If not, see <a href="https://www.gnu.org/licenses/gpl.html">https://www.gnu.org/licenses/gpl.html</a></p>
<h2>What does regbits do? <a name="what_does_regbits_do"></a></h2>
<p>Regbits enables easy to use, optimally efficient low-level access to <a href="#microcontroller_registers">microcontroller registers</a>.</p>
<p>Given appropriate header files, the following all compile to executable object code of equivalent size and speed (see <a href="#performance_claims">Performance claims</a>, below):</p>
<pre><code> // raw address and bits
*(int*)(0x40008008) = 0xc0;
// symbolic names for peripheral, register, and bits
*(TIMER1 + TIMER_CTRL_OFFSET) = TIMER_ENABLE | TIMER_COUNT_UP;
// C struct peripheral definition with symbolic bit names
TIMER1->CTRL = TIMER_ENABLE | TIMER_COUNT_UP;
// C++ regbits
timer1->ctrl = TIMER_ENABLE | TIMER_COUNT_UP;
</code></pre>
<p>Things get more interesting when manipulating contiguous spans of several bits:</p>
<pre><code> // raw address and bits
*(int*)(0x400c0004) = (*(int*)(0x400c0004) & ~0xf00) | 0x600;
// symbolic names for peripheral, register, and bits
*(TIMER3 + TIMER_CONFIG_OFFSET) = (*(TIMER3 + TIMER_CONFIG_OFFSET) & TIMER_CLOCK_DIV_MASK) | TIMER_CLOCK_DIV_8;
// C struct peripheral definition with symbolic bit names and bit manipulation preprocessor macro
INSERT_BITS(USART2->CONFIG, USART_BAUD_MASK, USART_BAUD_19200);
// C++ regbits
usart2->config /= USART_BAUD_19200; // or usart2->config.ins(USART_BAUD_19200)
</code></pre>
<p>But more important than what regbits does is what it does NOT do. All of the following will compile without error but almost certainly cause serious, hard to debug problems when executed:</p>
<pre><code> // read the manual if you want to know what this is trying to do
*(int*)(0x40100010) = 0x802;
// they probably use the same bit positions for all peripherals
*(SPI0 + SPI_CONFIG_OFFSET) = I2C_ENABLE | I2C_MASTER_MODE;
// this might do something interesting
ADC1->CTRL = USART_PARITY_EVEN | TIMER_ENABLE_INTERRUPT;
// we'll catch any problems during debug and test
init_usb_periph(USART3);
// hope this works when used later on
uint32_t timer_config_settings = SPI_LSB_FIRST | I2C_NACK_ENABLE;
</code></pre>
<p>Compared to regbits which won’t allow any of the following to compile: <a name="regbits_compile_time_errors"></a></p>
<pre><code> // ERROR: can't set SPI peripheral with I2C configuration constants
spi0->ctrl = I2C_ENABLE;
// ERROR: can't set register with another register's constant
i2c1->interrupts = I2C_MODE_FAST;
// ERROR: can't call function with wrong kind of peripheral
init_usb(USART3);
// ERROR: can't mix different peripherals' constants
uint32_t timer_config_settings = (TIMER_COUNT_DOWN | USART_BAUD_9600).bits();
</code></pre>
<p>These are contrived, exaggerated examples. However, the point remains that this kind of error is easy to make, hard to detect by source code inspection, and possibly even harder to find during runtime testing and debugging. A one-letter typo can easily hide in thousands of lines of code:</p>
<pre><code> I2C3->CTRL = I2C_TIMEOUT_ON | I2S_ENABLE | I2C_MASTER_MODE;
</code></pre>
<h2>What is regbits for? <a name="what_is_regbits_for"></a></h2>
<p>Microcontrollers (MCUs / “Systems On A Chip” / SOCs) typically include <em>peripherals</em>: Hardware functionality such as input/output ports, timers, serial communications, analog-to-digital and digital-to-analog conversion, etc.</p>
<p><a name="microcontroller_registers"></a>
These peripherals typically contain multiple <em>registers</em>: Sets of memory locations (usually contiguous) which when written to and read from control the peripheral and access its data. Additionally, the MCU itself – clock speed, interrupts, I/O configuration – is configured via similar sets of registers.</p>
<p>Single and spans of multiple (usually contiguous) bits within each register typically control or access specific capabilities of the peripheral or MCU. A register for a serial communications peripheral might be structured:</p>
<pre><code> 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
| | | | | | | | P | | | PORT | | TR| RR| CLK | | | TI| RI| | | | SPEED | M | E |
bit(s) label functionality
0 E Enable peripheral
1 M Mode (0==transmit, 1==receive)
2-4 SPEED Speed (0==slowest, 7==fastest)
8 RI Enable receive interrupt
9 TI Enable transmit interrupt
12-13 CLK Clock source (0==peripherals, 1==main, 2==external)
14 RR Receive data available
15 TR Transmit ready
17-21 PORT I/O port
24 P Polarity (0=positive, 1=negative)
</code></pre>
<p>Although the examples in this <code>README.md</code> and the <a href="unittest"><code>unittest</code></a> code are for 32-bit architectures, all are equally applicable to 64-bit and other word widths. And although designed for register access, regbits and similar methods are not limited to such usage.</p>
<h4>Hardware/software limitations</h4>
<p>All or almost all contemporary MCU hardware permits accessing memory by words (32/64/etc bits), bytes (8 bits), shorts (typically 16 bits) and other fixed, multiples-of-8, bit widths (32 bits on 64-bit architecture, etc).</p>
<p>Software frequently needs to access register bits (single or multiple) as separate, isolated atomic units, changing only those bits without affecting others in the same register. Hardware does not support this, and with the exception of C bitfields (<a href="#c_bitfields">see below</a>), neither do the C and C++ languages. Regbits and other software techniques (see <a href="#comparison_with_other_approaches">below</a>) address these limitations.</p>
<h4>Register memory accesses <a name="register_memory_access"></a></h4>
<p>Although registers are accessed as memory locations, they may or may not behave as such. C and C++ declarations should always include the <code>volatile</code> keyword because register bits may change independent of the CPU activity and the C/C++ execution model. There can be further differences; see <a href="#future_enhancements">Future enhancements</a>, below.</p>
<h2>How is regbits used? <a name="how_is_regbits_used"></a></h2>
<p>Given a microcontroller definition file based on the regbits C++ class templates (see <a href="#how_is_regbits_implemented?">below</a>), the set of operators and functions defined in <a href="regbits.hxx"><code>regbits.hxx</code></a> can be used in client code.</p>
<p>The examples below are based on the sample definition file included in this repository’s unit test directory, <a href="unittest/mcu_regbits.hxx"><code>mcu_regbits.hxx</code></a> (see <a href="#unit_test">Unit test</a>, below). This file contains a small set of example peripheral and register definitions describing a fictitious MCU and some of its peripherals.</p>
<p>The examples show correct regbits usage. The type-safe design of regbits will intentionally produce compilation failures in case of mismatches between regbits objects, as per <a href="#regbits_compile_time_errors">above</a>. Also see <a href="#type_safe_error_detection">“Type-safe error detection”</a> in the unittest documentation, below.</p>
<p>Many of the examples (and the <a href="unittest">unit test suite</a>) show regbits' operator and function/method syntaxes, both of which produce the same results and can be used interchangeably.</p>
<h4>Single bits vs bit spans</h4>
<p>Regbits defines two C++ classes for manipulating regisers: <code>Bits</code> and <code>Mskd</code>.</p>
<p><code>Bits</code> are used for setting/clearing/testing/etc single bits. The <code>Bits</code> (from <a href="regbits.hxx"><code>regbits.hxx</code></a> used in the following examples are:</p>
<pre><code> Serial::Config::POLARITY // bit: 1<<6
Serial::Config::ENDIAN // bit: 1<<5
Serial::Config::MODE // bit: 1<<0
</code></pre>
<p><code>Mskd</code> objects represent a (usually) contiguous bit span and contain a bit mask:</p>
<pre><code> Serial::Config::DATALEN_16_BITS // bits: 1<<1 mask: 0x03<<1
Timer::Prescale::PRESCALER_HIGH<17>() // bits: 17<<25 mask: 0x1f<<25
</code></pre>
<p>The bit mask allows setting/testing/etc bit spans to the exact “bits” pattern, both setting and clearing bits within the mask, as necessary.</p>
<p>Single <code>Bits</code> and <code>Mskd</code> bit spans can largely be used interchangeably, with a few exceptions. <code>Bits</code> and <code>Mskd</code> objects can be combined with the <code>|</code> operator in either order, but the type of the result is different:</p>
<pre><code> Serial::Config::DATALEN_16_BITS | Serial::Config::MODE // Mskd|Bits returns Mskd
Serial::Config::MODE | Serial::Config::DATALEN_16_BITS // Bits|Mskd returns Bits, losing mask information
</code></pre>
<p>The <code>Mskd</code> type’s mask information is important for operations such as <code>/=</code>, <code>ins()</code>, <code>all()</code>, <code>></code>, <code>>=</code>, <code><</code>, and <code><=</code>, described below. <code>Bits</code> objects will be automatically be converted to <code>Mskd</code> as required, but with a trivial mask equal to the bits themselves; thus losing a <code>Mskd</code>’s mask via the <code>|</code> operator may produce unexpected results. In practice these issues are largely intuitive and obvious. Note, however, that when masking information is not needed, <code>Bits</code> are more efficient than <code>Mskd</code> spans.</p>
<h4>Set register to single bit, bit span, or numeric value</h4>
<pre><code> // set register to single bit
serial2->config = Serial::Config::POLARITY ;
serial2->config.wrt(Serial::Config::POLARITY);
// set register to multiple single bits
serial2->config = Serial::Config::ENDIAN | Serial::Config::POLARITY ;
serial2->config.wrt(Serial::Config::ENDIAN | Serial::Config::POLARITY);
// set register to bit span
serial2->config = Serial::Config::DATALEN_16_BITS ; // datalen 8, 16, or 32 encoded in 2 bits
serial2->config.wrt(Serial::Config::DATALEN_16_BITS);
// set register to multiple bit spans
serial2->config = Serial::Config::DATALEN_16_BITS | Serial::Config::PARITY_EVEN ; // parity EVEN, ODD, CRC encoded in 2 bits
serial2->config.wrt(Serial::Config::DATALEN_16_BITS | Serial::Config::PARITY_EVEN);
// set register to mix of single and bit span
serial2->config = Serial::Config::DATALEN_16_BITS | Serial::Config::MODE ; // mode is single bit
serial2->config.wrt(Serial::Config::DATALEN_16_BITS | Serial::Config::MODE);
// order unimportant
serial2->config = Serial::Config::MODE | Serial::Config::DATALEN_16_BITS ;
serial2->config.wrt(Serial::Config::MODE | Serial::Config::DATALEN_16_BITS);
// set register to numeric value
serial2->config = 0x12345678 ;
serial2->config.wrt(0x12345678);
</code></pre>
<h4>Set, clear, flip bits</h4>
<pre><code> // set single/multiple bits/spans and mixtures thereof
serial2->config |= Serial::Config::POLARITY;
serial2->config.set(Serial::Config::POLARITY | Serial::Config::ENDIAN );
serial2->config |= Serial::Config::DATALEN_16_BITS | Serial::Config::PARITY_EVEN ;
serial2->config.set(Serial::Config::POLARITY | Serial::Config::ENDIAN );
// clr single/multiple bits/spans and mixtures thereof
serial2->config -= Serial::Config::POLARITY;
serial2->config.clr(Serial::Config::POLARITY | Serial::Config::ENDIAN );
serial2->config -= Serial::Config::DATALEN_16_BITS | Serial::Config::PARITY_EVEN ;
serial2->config.clr(Serial::Config::POLARITY | Serial::Config::ENDIAN );
// flip single/multiple bits/spans and mixtures thereof
serial2->config ^= Serial::Config::POLARITY;
serial2->config.flp(Serial::Config::POLARITY | Serial::Config::ENDIAN );
serial2->config ^= Serial::Config::DATALEN_16_BITS | Serial::Config::PARITY_EVEN ;
serial2->config.flp(Serial::Config::POLARITY | Serial::Config::ENDIAN );
</code></pre>
<h4>Insert bits (set without changing others)</h4>
<p>(see <a href="#insert_operator_syntax">Insert operator syntax</a>, below)</p>
<pre><code> // insert span of bits into register
serial2->config /= Serial::Config::DATALEN_16_BITS ;
serial2->config.ins(Serial::Config::DATALEN_16_BITS);
// insert multiple spans of bits into register
serial2->config /= Serial::Config::DATALEN_16_BITS | Serial::Config::PARITY_EVEN ;
serial2->config.ins(Serial::Config::DATALEN_16_BITS | Serial::Config::PARITY_EVEN);
// insert mix of single and contiguous span of bits
// note order is important: span|bits yields span, but
// bits|span yields bits, losing mask information for span
serial2->config /= Serial::Config::DATALEN_16_BITS | Serial::Config::MODE ;
serial2->config.ins(Serial::Config::DATALEN_16_BITS | Serial::Config::MODE);
// insert single or multiple single bits into register
// produces expected results, but identical and less efficient than using |=
serial2->config /= Serial::Config::ENDIAN ;
serial2->config.ins(Serial::Config::ENDIAN);
serial2->config /= Serial::Config::ENDIAN | Serial::Config::POLARITY ;
serial2->config.ins(Serial::Config::ENDIAN | Serial::Config::POLARITY);
</code></pre>
<h4>Test register bits</h4>
<p><em>Updated: Previous versions of regbits used operator syntax (</em> <code>==</code> <em>and</em> <code>!=</code>) <em>combined with the explicit</em> <code>.bits()</code> <em>method. Although workable and supporting all capabilities, this syntax was felt to be ambigous and was replaced with the following named methods.</em></p>
<pre><code> // Test if any of the specified single bits are set
if (serial2->config.any(Serial::Config::ENDIAN)) // ...
if (serial2->config.any(Serial::Config::ENDIAN | Serial::Config::POLARITY)) // ...
// Test if all of the specified single bits are set
// Can be used with one single bit but identical results and less efficient than .any()
if (serial2->config.all(Serial::Config::ENDIAN | Serial::Config::POLARITY)) // ...
// Test one or more spans of bits (both zero and one bits within span(s))
if (serial2->config.all(Serial::Config::DATALEN_16_BITS)) // ...
if (serial2->config.all(Serial::Config::DATALEN_16_BITS | Serial::Config::PARITY_EVEN)) // ...
// Note .any() cannot be implemented efficiently for spans of bits,
// nor at all if spans are contiguous
// Test all register bits exactly equal to specified single/multiple
// bits and/or spans, including bits outside specified bits/spans
if (serial2->config.is(Serial::Config::ENDIAN)) // ...
if (serial2->config.any(Serial::Config::ENDIAN | Serial::Config::POLARITY)) // ...
if (serial2->config.is(Serial::Config::DATALEN_16_BITS)) // ...
if (serial2->config.is(Serial::Config::DATALEN_16_BITS | Serial::Config::PARITY_EVEN)) // ...
if (serial2->config.is(Serial::Config::ENDIAN | Serial::Config::DATALEN_16_BITS)) // ...
if (serial2->config.is(Serial::Config::DATALEN_16_BITS | Serial::Config::ENDIAN)) // ...
// Test exactly equal to zero or arbitrary value
if (serial2->config.is(0)) // ...
if (serial2->config.is(0x12345678)) // ...
// Compare numeric values of bit spans
// Note that if used with multiple bit spans, MSBs take precedence
if (serial2->config > Serial::Config::DATALEN_16_BITS) // ...
if (serial2->config >= Serial::Config::DATALEN_16_BITS) // ...
if (serial2->config < Serial::Config::DATALEN_16_BITS) // ...
if (serial2->config <= Serial::Config::DATALEN_16_BITS) // ...
</code></pre>
<h4>Extract single and multiple bit spans from a register</h4>
<pre><code> // extract all bits from register
Serial::Config::bits_t config_bits = serial2->config;
// extract all bits from register into bit span (maintaining bit span masking information)
Serial::Config::mskd_t config_mskd = Serial::Config::DATALEN_16_BITS; // or any other DATALEN_XX_BITS constant
config_mskd = serial2->config ;
</code></pre>
<h4>Atomic (single) read of hardware register</h4>
<p>See discussion <a href="#future_enhancements">below</a> about self-modifying registers.</p>
<pre><code> // hardware register might change between accesses
if (serial2->config.any(Serial::Config::ENDIAN) || serial2->config >= Serial::Config::DATALEN_16_BITS) // ...
// single atomic read
Serial::config_t config(serial2->config);
if (config.any(Serial::Config::ENDIAN) || config >= Serial::Config::DATALEN_16_BITS) // ...
</code></pre>
<h4>Working with multiple bit spans containing arbitrary values <a name="mskd_arbitrary_values"></a></h4>
<p>(see rationale for syntax <a href="#ugly_indexing_syntax">below</a>)</p>
<pre><code> timer1->prescale = Timer::Prescale::PRESCALER_HIGH<17>();
if (timer1->prescale.all(Timer::Prescale::PRESCALER_HIGH<17>())) // ...
</code></pre>
<h4>Shifted values <a name="shifted_values"></a></h4>
<p>Regbits canonically works with multiple bit spans in their hardware-defined position within the register, avoiding unnecessary shift operations. (See <a href="#c_bitfields">C bitfields</a>, below.) Cases exist, however, where shifting is required:</p>
<pre><code> 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
| | | | | | | | | | | DATA | | | | | | | | | | | | |STATUS |
Usart::rxdata_t rxdata(usart3->rxdata);
if (rxdata.any(Usart::Rxdata::STATUS_DATA_RCVD))
return copy >> Usart::Rxdata::DATA_SHIFTED; // 0..255
// or copy.shifted(Usart::Rxdata::DATA_SHIFTED);
</code></pre>
<h2>How is regbits implemented? <a name="how_is_regbits_implemented?"></a></h2>
<p>The C++ class templates defining regbits are contained in the file <a href="regbits.hxx"><code>regbits.hxx</code></a>. This file is intended for inclusion in an MCU peripheral/register/bits definition file such as the <a href="unittest/mcu_regbits.hxx"><code>mcu_regbits.hxx</code></a> example in the <a href="unittest"><code>unittest</code></a> subdirectory (see <a href="#unit_test">Unit test</a>, below).</p>
<p>Following are some commented excerpts from <code>mcu_regbits.hxx</code> illustrating use of the templates:</p>
<p>Include the regbits.hxx templates:</p>
<pre><code> #include <regbits.hxx>
</code></pre>
<p>Typically, MCU regbits definitions should go in their own namespace – a manufacturer or chip name such as <code>stm32f767xx</code> or <code>lpc824</code> would replace the example’s generic “mcu” namespace name:</p>
<pre><code> namespace mcu {
</code></pre>
<p>Start definition of a peripheral:</p>
<pre><code> struct Serial {
</code></pre>
<p>Start definition of a register in the peripheral.:</p>
<pre><code> struct Config {
</code></pre>
<p>MCU regbits definitions implement type-safety by instantiating several classes templated on each specific register. Other instantiated template classes' methods will only accept objects template-instantiated on the same register.</p>
<p><code>pos_t</code> is a type-safe wrapper for unsigned integers. It ensures that the values can only be used with the classes/register they are intended for</p>
<pre><code> using pos_t = Pos<uint32_t, Config>;
</code></pre>
<p>Define bit positions of individual bits and spans of bits within the register …</p>
<p>Note that these, and all other definitions, are <code>static constexpr</code>. Combined with the regbits <code>constexpr</code> constructors in the <code>regbits.hxx</code> class templates (and when compiled with appropriate optimization flags) they become compile-time only constants – no runtime storage is allocated for them.</p>
<p>Note also that these values are externally visible in case client code has use for them.</p>
<p>Bit positions:</p>
<pre><code> static constexpr pos_t MODE_POS = pos_t( 0),
DATALEN_POS = pos_t( 1),
PARITY_POS = pos_t( 3),
ENDIAN_POS = pos_t( 5),
POLARITY_POS = pos_t( 6),
RXPORT_POS = pos_t( 7),
TXPORT_POS = pos_t(12);
</code></pre>
<p>Define bitmasks describing the size of multi-bit spans:</p>
<pre><code> static const uint32_t DATALEN_MASK = 0x03,
PARITY_MASK = 0x03,
RXPORT_MASK = 0x1f,
TXPORT_MASK = 0x1f;
</code></pre>
<p>Instantiation of the regbits templated classes. <code>Bits</code> is used for single bits (although when object instances are combined with others via the “<code>|</code>” operator they can contain multiple single bits). <code>Mskd</code> is used for spans of multiple bits. <code>Shft</code> is used to normalize (right shift to LSB) masked values.</p>
<pre><code> using bits_t = Bits<uint32_t, Config>;
using mskd_t = Mskd<uint32_t, Config>;
using shft_t = Shft<uint32_t, Config>;
</code></pre>
<p>The fact that Mskd objects hold both a bitmask and an bit value allows it to be used as a standalone object that supports bit span insertion into a register. Expressions using regbits insertion operators/methods objects such as:</p>
<pre><code> serial2->config /= Serial::Config::DATALEN_16_BITS
</code></pre>
<p>compile to executable object code identical that generated when not using regbits:</p>
<pre><code> serial2_register = (serial2_register & ~datalen_mask) | datalen_bits;
</code></pre>
<p><a name="regbits_bits_t_and_mskd_t"></a>
Continuing with the example definition file, define constants for single bits:</p>
<pre><code> static constexpr bits_t MODE = bits_t(1, MODE_POS ),
ENDIAN = bits_t(1, ENDIAN_POS ),
POLARITY = bits_t(1, POLARITY_POS);
</code></pre>
<p>Defining constants for multiple bit spans:</p>
<pre><code> static constexpr mskd_t
DATALEN_8_BITS = mskd_t(DATALEN_MASK, 0, DATALEN_POS),
DATALEN_16_BITS = mskd_t(DATALEN_MASK, 1, DATALEN_POS),
DATALEN_32_BITS = mskd_t(DATALEN_MASK, 2, DATALEN_POS),
PARITY_ODD = mskd_t(PARITY_MASK, 0, PARITY_POS ),
PARITY_EVEN = mskd_t(PARITY_MASK, 1, PARITY_POS ),
PARITY_CRC = mskd_t(PARITY_MASK, 2, PARITY_POS );
</code></pre>
<p>Defining multiple bit spans with arbitrary values, limit checking, and bit shifting (see <a href="#shifted_values">Shifted values</a>, above, and <a href="#templates_numeric_constants">Templates for numerically-defined constants</a>, below).</p>
<pre><code> REGBITS_MSKD_RANGE("Serial::Config",
RXPORT,
rxport,
RXPORT_MASK,
RXPORT_POS,
MAX_PORT_NUM);
</code></pre>
<p>End the register definition, define a type name for possible external use, and allocate storage for the register. This where the peripheral struct gets a finite size (and layout) to match the actual hardware peripheral when a pointer to its address is declared.</p>
<pre><code> }; // struct Config
using config_t = Reg<uint32_t, Config>;
config_t config;
</code></pre>
<p>More register definitions:</p>
<pre><code> struct Interrupts {
// ...
</code></pre>
<p>End peripheral definition and check that size is correct:</p>
<pre><code> }; // struct Serial
static_assert(sizeof(Serial) == 24, "sizeof(Serial)");
</code></pre>
<p>Define pointers to actual hardware peripherals:</p>
<pre><code> static const uint32_t PERIPH_BASE = 0x40000000;
static const uint32_t SERIAL_BASE = PERIPH_BASE + 0x00a00000;
static const uint32_t SERIAL0_BASE = SERIAL_BASE + 0x00000000,
SERIAL1_BASE = SERIAL_BASE + 0x00001000,
SERIAL1_BASE = SERIAL_BASE + 0x00002000;
static Serial* const serial0 = reinterpret_cast<Serial*>(SERIAL0_BASE);
static Serial* const serial1 = reinterpret_cast<Serial*>(SERIAL1_BASE);
static Serial* const serial2 = reinterpret_cast<Serial*>(SERIAL2_BASE);
</code></pre>
<h2>Comparison with other approaches <a name="comparison_with_other_approaches"></a></h2>
<p>(This material is included as background for the design of regbits. Experienced microcontroller programmers will be very familiar with the approaches and should skip ahead to the <a href="#requirements_limitations_problems">Requirements, limitations, problems</a> section, below.)</p>
<p>Two different sets of information need to be provided to client code:</p>
<ol>
<li>Registers order/layout within peripherals</li>
<li>Bits within the registers</li>
</ol>
<p>Some current approaches for addressing both of these requirements are described (somewhat intermixed) in the following sections.</p>
<h4>Raw numeric register addresses and bits <a name="raw_numeric_values"></a></h4>
<p>source (.c) file:</p>
<pre><code> *(int*)(0x400080a2) = 0xc0;
</code></pre>
<p>This is sadomasochism: Sadism inflicted on anyone else who has to work with the code, and masochism on the part of the original developer who needs to maintain or debug it in the future.</p>
<h5>pros:</h5>
<ul>
<li>no definition file needed</li>
</ul>
<h5>cons:</h5>
<ul>
<li>completely opaque; inherently undocumented</li>
<li>requires constant reference to external manuals</li>
<li>error prone, typos almost impossible to detect</li>
</ul>
<h4>Preprocessor symbolic peripheral addresses and register offsets <a name="preprocessor_symbolic_peripheral_addresses_and_register_offsets"></a></h4>
<h5>source (.c) file:</h5>
<pre><code> *(TIMER1 + TIMER_CTRL_OFFSET) = //...
</code></pre>
<h5>definition (.h) file:</h5>
<pre><code> #define TIMER_BASE ((PERIPH_BASE) + 0x00200000)
#define TIMER0_BASE ((TIMER_BASE) + 0x00000000)
#define TIMER1_BASE ((TIMER_BASE) + 0x00001000)
#define TIMER2_BASE ((TIMER_BASE) + 0x00002000)
#define TIMER0 ((volatile uint32_t*)(TIMER0_BASE))
#define TIMER1 ((volatile uint32_t*)(TIMER1_BASE))
#define TIMER2 ((volatile uint32_t*)(TIMER2_BASE))
#define TIMER_CONTROL_OFFSET 0
#define TIMER_PRESCALE_OFFSET 1
#define TIMER_AUTORELOAD_OFFSET 2
</code></pre>
<h5>pros:</h5>
<ul>
<li>more readable / self-documenting</li>
</ul>
<h5>cons:</h5>
<ul>
<li>verbose and error-prone</li>
<li>not type-safe, no error detection</li>
</ul>
<h4>Preprocessor symbolic bit definitions <a name="preprocessor_symbolic_bit_definitions"></a></h4>
<h6>source (.c) file:</h6>
<pre><code> // set_bits
/*...*/ = TIMER_CONTROL_ENABLE | TIMER_CONTROL_CLOCK_SOURCE_PLL;
// clear bits
/*...*/ &= ~(TIMER_CONTROL_ENABLE | TIMER_CONTROL_DIRECTION);
// insert field
/*...*/ = (/*...*/ & ~TIMER_CONTROL_CLOCK_SOURCE_MSK) | TIMER_CONTROL_CLOCK_SOURCE_PLL;
</code></pre>
<h6>definition (.h) file:</h6>
<pre><code> #define TIMER_CONTROL_ENABLE_POS 4
#define TIMER_CONTROL_SINGLE_CYCLE_POS 5
#define TIMER_CONTROL_ARR_ENABLE_POS 6
#define TIMER_CONTROL_CLOCK_SOURCE_POS 7
#define TIMER_CONTROL_DIRECTION_POS 10
#define TIMER_CONTROL_ENABLE_MSK (1 << TIMER_CONTROL_ENABLE_POS)
#define TIMER_CONTROL_SINGLE_CYCLE_MSK (1<< TIMER_CONTROL_SINGLE_CYCLE_POS)
#define TIMER_CONTROL_SINGLE_CYCLE TIMER_CONTROL_SINGLE_CYCLE_MSK
#define TIMER_CONTROL_ARR_ENABLE_MSK (1 << TIMER_CONTROL_ARR_ENABLE_POS)
#define TIMER_CONTROL_ARR_ENABLE TIMER_CONTROL_ARR_ENABLE_MSK
#define TIMER_CONTROL_ENABLE TIMER_CONTROL_ENABLE_MSK
#define TIMER_CONTROL_CLOCK_SOURCE_MSK (0x7 << TIMER_CONTROL_CLOCK_SOURCE_POS)
#define TIMER_CONTROL_CLOCK_SOURCE_MAIN (0x0 << TIMER_CONTROL_CLOCK_SOURCE_POS)
#define TIMER_CONTROL_CLOCK_SOURCE_PLL (0x1 << TIMER_CONTROL_CLOCK_SOURCE_POS)
#define TIMER_CONTROL_CLOCK_SOURCE_EXTERN (0x2 << TIMER_CONTROL_CLOCK_SOURCE_POS)
#define TIMER_CONTROL_DIRECTION_MSK (1 << TIMER_CONTROL_DIRECTION_POS)
#define TIMER_CONTROL_DIRECTION TIMER_CONTROL_DIRECTION_MSK
#define TIMER_MAX_PRESCALER_VAL 0xff
#define TIMER_PRESCALE_PRESCALER_POS 24
#define TIMER_PRESCALE_PRESCALER_MSK (0xff << TIMER_PRESCALE_PRESCALER_POS)
#define TIMER_MAX_AUTORELOAD_VAL 0x00ffffff
#define TIMER_AUTORELOAD_AUTORELOAD_POS 0
#define TIMER_AUTORELOAD_AUTORELOAD_MSK (0xffffff << TIMER_AUTORELOAD_AUTORELOAD_POS)
</code></pre>
<h6>pros:</h6>
<ul>
<li>self-documenting</li>
</ul>
<h6>cons:</h6>
<ul>
<li>global scope</li>
<li>verbose (due to need to prefix/disambiguate many similar names)</li>
<li>not type-safe</li>
<li>no error checking</li>
<li>not visible in debuggers (exception: gcc “<code>-g3</code>” flag)</li>
</ul>
<h4>Static consts <a name="static_consts"></a></h4>
<h5>source (.c) file:</h5>
<p>(same as <a href="#preprocessor_symbolic_peripheral_addresses_and_register_offsets">Preprocessor symbolic peripheral addresses and register offsets</a> and <a href="#preprocessor_symbolic_bit_definitions">Preprocessor symbolic bit definitions</a>, above.)</p>
<h5>definition (.h) file:</h5>
<pre><code> static const uint8_t TIMER_CONTROL_ENABLE_POS = 4,
TIMER_CONTROL_SINGLE_CYCLE_POS = 5,
TIMER_CONTROL_ARR_ENABLE_POS = 6,
TIMER_CONTROL_CLOCK_SOURCE_POS = 7,
TIMER_CONTROL_DIRECTION_POS = 10;
static const uint32_t TIMER_CONTROL_ENABLE_MSK = (1 << TIMER_CONTROL_ENABLE_POS) ,
TIMER_CONTROL_SINGLE_CYCLE_MSK = (1<< TIMER_CONTROL_SINGLE_CYCLE_POS) ,
TIMER_CONTROL_SINGLE_CYCLE = TIMER_CONTROL_SINGLE_CYCLE_MSK ,
TIMER_CONTROL_ARR_ENABLE_MSK = (1 << TIMER_CONTROL_ARR_ENABLE_POS) ,
TIMER_CONTROL_ARR_ENABLE = TIMER_CONTROL_ARR_ENABLE_MSK ,
TIMER_CONTROL_ENABLE = TIMER_CONTROL_ENABLE_MSK ,
TIMER_CONTROL_CLOCK_SOURCE_MSK = (0x7 << TIMER_CONTROL_CLOCK_SOURCE_POS) ,
TIMER_CONTROL_CLOCK_SOURCE_MAIN = (0x0 << TIMER_CONTROL_CLOCK_SOURCE_POS) ,
TIMER_CONTROL_CLOCK_SOURCE_PLL = (0x1 << TIMER_CONTROL_CLOCK_SOURCE_POS) ,
TIMER_CONTROL_CLOCK_SOURCE_EXTERN = (0x2 << TIMER_CONTROL_CLOCK_SOURCE_POS) ,
TIMER_CONTROL_DIRECTION_MSK = (1 << TIMER_CONTROL_DIRECTION_POS) ,
TIMER_CONTROL_DIRECTION = TIMER_CONTROL_DIRECTION_MSK ,
= TIMER_MAX_PRESCALER_VAL= 0xff ,
TIMER_PRESCALE_PRESCALER_POS = 24 ,
TIMER_PRESCALE_PRESCALER_MSK = (0xff << TIMER_PRESCALE_PRESCALER_POS) ,
= ,
TIMER_MAX_AUTORELOAD_VAL = 0x00ffffff ,
TIMER_AUTORELOAD_AUTORELOAD_POS = 0 ,
TIMER_AUTORELOAD_AUTORELOAD_MSK = (0xffffff << TIMER_AUTORELOAD_AUTORELOAD_POS);
</code></pre>
<p>and if used for raw numeric register addresses and bits ([above[(raw_numeric_values)):</p>
<pre><code> static const uint32_t TIMER_BASE = PERIPH_BASE + 0x00c00000;
static const uint32_t TIMER0_BASE = TIMER_BASE + 0x00000000,
TIMER1_BASE = TIMER_BASE + 0x00001000,
TIMER2_BASE = TIMER_BASE + 0x00002000;
static volatile uint32_t* const TIMER0 = (volatile uint32_t*)TIMER0_BASE;
static volatile uint32_t* const TIMER1 = (volatile uint32_t*)TIMER1_BASE;
static volatile uint32_t* const TIMER2 = (volatile uint32_t*)TIMER2_BASE;
static const uint32_t TIMER_CONTROL_OFFSET = 0,
TIMER_PRESCALE_OFFSET = 1,
TIMER_AUTORELOAD_OFFSET = 2;
</code></pre>
<h5>pros:</h5>
<ul>
<li>self-documenting</li>
<li>type-safe</li>
<li>can be namespace-scoped (C++, short, non-prefixed names)</li>
</ul>
<h5>cons:</h5>
<ul>
<li>(in C) global scope, verbose (due to need to prefix/disambiguate many similar names)</li>
<li>no error checking</li>
<li>memory space for variables (potentially thousands) in executable binary if appropriate compiler flags not used</li>
</ul>
<h4>C/C++ enums <a name="c_cxx_enums"></a></h4>
<h5>source (.c) file:</h5>
<p>(same as <a href="#preprocessor_symbolic_peripheral_addresses_and_register_offsets">Preprocessor symbolic peripheral addresses, register offsets</a> and <a href="#preprocessor_symbolic_bit_definitions">Preprocessor symbolic bit definitions</a>, and <a href="#static_consts">Static consts</a> above.)</p>
<h5>definition (.h) file (C code):</h5>
<pre><code> enum TIMER_CONTROL_POS {
TIMER_CONTROL_ENABLE_POS = 4,
TIMER_CONTROL_SINGLE_CYCLE_POS = 5,
TIMER_CONTROL_ARR_ENABLE_POS = 6,
TIMER_CONTROL_CLOCK_SOURCE_POS = 7,
TIMER_CONTROL_DIRECTION_POS = 10,
};
enum TIMER_CONTROL_BITS {
TIMER_CONTROL_ENABLE_MSK = (1 << TIMER_CONTROL_ENABLE_POS),
TIMER_CONTROL_SINGLE_CYCLE_MSK = (1<< TIMER_CONTROL_SINGLE_CYCLE_POS),
TIMER_CONTROL_SINGLE_CYCLE = TIMER_CONTROL_SINGLE_CYCLE_MSK,
TIMER_CONTROL_ARR_ENABLE_MSK = (1 << TIMER_CONTROL_ARR_ENABLE_POS),
TIMER_CONTROL_ARR_ENABLE = TIMER_CONTROL_ARR_ENABLE_MSK,
TIMER_CONTROL_ENABLE = TIMER_CONTROL_ENABLE_MSK,
TIMER_CONTROL_CLOCK_SOURCE_MSK = (0x7 << TIMER_CONTROL_CLOCK_SOURCE_POS),
TIMER_CONTROL_CLOCK_SOURCE_MAIN = (0x0 << TIMER_CONTROL_CLOCK_SOURCE_POS),
TIMER_CONTROL_CLOCK_SOURCE_PLL = (0x1 << TIMER_CONTROL_CLOCK_SOURCE_POS),
TIMER_CONTROL_CLOCK_SOURCE_EXTERN = (0x2 << TIMER_CONTROL_CLOCK_SOURCE_POS),
TIMER_CONTROL_DIRECTION_MSK = (1 << TIMER_CONTROL_DIRECTION_POS),
TIMER_CONTROL_DIRECTION = TIMER_CONTROL_DIRECTION_MSK,
};
enum TIMER_PRESCALE_BITS {
TIMER_MAX_PRESCALER_VAL = 0xff,
TIMER_PRESCALE_PRESCALER_POS = 16,
TIMER_PRESCALE_PRESCALER_MSK = 0xff << TIMER_PRESCALE_PRESCALER_POS,
};
enum TIMER_AUTORELOAD_BTIS {
TIMER_MAX_AUTORELOAD_VAL = 0x00ffffff,
TIMER_AUTORELOAD_AUTORELOAD = 0,
TIMER_AUTORELOAD_AUTORELOAD_MSK = 0xffffff<< << TIMER_AUTORELOAD_AUTORELOAD_POS,
};
</code></pre>
<h5>definition (.h) file (C++ code):</h5>
<pre><code> enum class TIMER_CONTROL {
ENABLE = 4,
SINGLE_CYCLE = 5,
ARR_ENABLE = 6,
CLOCK_SOURCE = 7,
DIRECTION = 10,
};
enum class TIMER_CONTROL_BITS {
ENABLE_MSK = (1 << static_cast<uint32_t>(TIMER_CONTROL::ENABLE)),
ENABLE = ENABLE_MSK,
SINGLE_CYCLE_MSK = (1<< static_cast<uint32_t>(TIMER_CONTROL::SINGLE_CYCLE)),
SINGLE_CYCLE = SINGLE_CYCLE_MSK,
ARR_ENABLE_MSK = (1 << static_cast<uint32_t>(TIMER_CONTROL::ARR_ENABLE)),
ARR_ENABLE = ARR_ENABLE_MSK,
CLOCK_SOURCE_MSK = (0x7 << static_cast<uint32_t>(TIMER_CONTROL::CLOCK_SOURCE)),
CLOCK_SOURCE_MAIN = (0x0 << static_cast<uint32_t>(TIMER_CONTROL::CLOCK_SOURCE)),
CLOCK_SOURCE_PLL = (0x1 << static_cast<uint32_t>(TIMER_CONTROL::CLOCK_SOURCE)),
CLOCK_SOURCE_EXTERN = (0x2 << static_cast<uint32_t>(TIMER_CONTROL::CLOCK_SOURCE)),
DIRECTION_MSK = (1 << static_cast<uint32_t>(TIMER_CONTROL::DIRECTION)),
DIRECTION = DIRECTION_MSK,
};
enum class TIMER_PRESCALE_BITS {
MAX_PRESCALER_VAL = 0xff,
PRESCALER_POS = 16,
PRESCALER_MSK = (0xff << static_cast<uint32_t>(PRESCALER_POS),
};
enum class TIMER_AUTORELOAD_BTIS {
AUTORELOAD_VAL = 0x00ffffff,
AUTORELOAD_POS = 0,
AUTORELOAD_MSK = (0xffffff << static_cast<uint32_t>(AUTORELOAD_POS),
};
</code></pre>
<h5>pros:</h5>
<ul>
<li>self-documenting</li>
<li>short class-scoped names (C++)</li>
<li>type-safe (C++) (but not really due to need to cast before use)</li>
</ul>
<h5>cons:</h5>
<ul>
<li>requires casting before use (C++)</li>
<li>requires C++11 or higher (C++)</li>
<li>global scope, verbose (due to need to prefix/disambiguate many similar names) ©</li>
<li>not type-safe (<code>C</code>)</li>
<li>no error checking (<code>C</code>)</li>
</ul>
<h4>C structs <a name="c_structs"></a></h4>
<p>(The most commonly used technique.)</p>
<h5>source (.c) file:</h5>
<pre><code> // set value
TIMER1->CTRL = value;
// set bits without affecting others
GPIO1->SET |= value;
// clear bits without affecting others
SYS->INTERRUPTS &= ~value;
// insert bits without affecting others
USART1->SPEED = (USART1->SPEED & mask) | value;
</code></pre>
<p>(values via numeric values, #defines, static consts, enums, etc)</p>
<h5>definition (.h) file: <a name="c_structs_definition_file"></a></h5>
<pre><code> typedef struct serial {
volatile uint32_t CONTROL ;
volatile uint32_t CONFIG ;
volatile uint32_t INTERRUPT;
volatile uint32_t STATUS ;
volatile uint32_t RXBUFFER ;
volatile uint32_t TXBUFFER ;
} serial_t;
#ifdef USE_PREPROCESSOR_ADDRESSES
#define SERIAL_BASE ((PERIPH_BASE) + 0x00a00000)
#define SERIAL0_BASE ((SERIAL_BASE) + 0x00000000)
#define SERIAL1_BASE ((SERIAL_BASE) + 0x00001000)
#define SERIAL2_BASE ((SERIAL_BASE) + 0x00002000)
#define SERIAL0 ((serial_t*)(SERIAL0_BASE))
#define SERIAL1 ((serial_t*)(SERIAL1_BASE))
#define SERIAL2 ((serial_t*)(SERIAL2_BASE))
#endif
#ifdef USE_STATIC_CONST_ADDRESSES
static const uint32_t SERIAL_BASE = PERIPH_BASE + 0x00a00000;
static const uint32_t SERIAL0_BASE = SERIAL_BASE + 0x00000000,
SERIAL1_BASE = SERIAL_BASE + 0x00001000,
SERIAL1_BASE = SERIAL_BASE + 0x00002000;
static serial_t* const SERIAL0 = (serial_t*)SERIAL0_BASE;
static serial_t* const SERIAL1 = (serial_t*)SERIAL1_BASE;
static serial_t* const SERIAL2 = (serial_t*)SERIAL2_BASE;
#endif
// constants via #defines, static consts, enums, etc)
// ...
</code></pre>
<h5>pros:</h5>
<ul>
<li>self-documenting</li>
<li>type-safe peripheral and register definitions</li>
</ul>
<h5>cons:</h5>
<ul>
<li>bit values not type-safe</li>
<li>struct name frequently omitted (superceded by typedef) thus not allowing forward declaration</li>
</ul>
<h4>C bitfields <a name="c_bitfields"></a></h4>
<p>C bitfields would seem to solve all requirements for independent/atomic/isolated register bit access:</p>
<h5>source (.c) file:</h5>
<pre><code> // set single bit
SERIAL2->config.polarity = 1;
// clear single bit
SERIAL2->config.polarity = 0;
// set span of bits
SERIAL2->config.datalen = SERIAL_CONFIG_DATALEN_8_BITS;
// clear span of bits
SERIAL2->config.datalen = 0;
</code></pre>
<h5>definition (.h) file:</h5>
<pre><code> typedef struct serial {
volatile struct {
unsigned enable : 1,
recv : 1,
xmit : 1,
: 29;
};
} control;
volatile struct {
unsigned mode : 1,
datalen : 2,
parity : 2,
endian : 1,
polarity : 1,
rxport : 5,
txport : 5,
: 15;
} config;
} serial_t;
// (addresses of peripheral instances as per [C structs](#c_structs_definition_file), above)
// (values for multi-bit fields via numeric values, #defines, static consts, enums, etc)
</code></pre>
<p>Unfortunately, there are several problems with this feature of C/C++. First, testing bitfield values generates more executable/object code than other approaches by necessitating large amounts of bit masking and shifting:</p>
<pre><code> // other approaches
SERIAL2->parity = ODD_PARITY;
if (SERIAL2->parity == ODD_PARITY) // ...
// bitfield
register = 0;
SERIAL2->config.parity = ODD_PARITY;
if (SERIAL2->config.parity == ODD_PARITY) // ...
// equivalent code to that generated by bitfield approach
SERIAL2->config = ODD_PARITY << PARITY_POSITION;
if (((SERIAL2->config & 0x18) >> 3) == ODD_PARITY) // ...
</code></pre>
<p>(In the other approaches <code>ODD_PARITY</code> has been pre-shifted to its correct bit position.)</p>
<p>But a much worse inefficiency, both in source code verbosity and more importantly executable/object code size and speed, occurs when multiple individual bit fields need to be set/cleared/modified:</p>
<pre><code> // other approaches
SERIAL2->parity = ODD_PARITY | BIG_ENDIAN;
// bitfields
SERIAL2->config.parity = ODD_PARITY;
SERIAL2->config.endian = 1;
// equivalent bitfield code -- two writes to register instead of one
SERIAL2->config = (SERIAL2->config & ~PARITY_MASK) | (ODD_PARITY << PARITY_POSITION);
SERIAL2->config = (SERIAL2->config & ~ENDIAN_MASK) | (1 << ENDIAN_POSITION);
</code></pre>
<p>Some of this inefficiency can be avoided by creating a union between the C bitfields and a variable of the architecture’s native word size:</p>
<pre><code> union CONFIG_T {
volatile uint32_t word;
volatile struct {
unsigned mode : 1,
datalen : 2,
parity : 2,
endian : 1,
polarity : 1,
rxport : 5,
txport : 5,
: 15;
};
} config;
</code></pre>
<p>In this way at least the entire register can be cleared in one operation. But setting or clearing multiple fields at once requires including <code>#define</code> or <code>static const</code> definitions as per the other approaches. This effectively doubles the size and complexity of the definition file and the effort to create it, and introduces the risk of mismatched definitions, etc.</p>
<p>Finally, there does exist the possibility that a very “smart” compiler could detect the multiple settings and coalesce them into a single or at least smaller set of operations.</p>
<h5>pros:</h5>
<ul>
<li>self-documenting</li>
<li>type-safe, intuitive access to register bits/fields</li>
</ul>
<h5>cons:</h5>
<ul>
<li>inefficient</li>