HWA
Bare metal programming with style
hwa_1.h
Go to the documentation of this file.
1 
2 /* This file is part of the HWA project.
3  * Copyright (c) 2012,2015 Christophe Duparquet.
4  * All rights reserved. Read LICENSE.TXT for details.
5  */
6 
12 #include "hwa_errors.h"
13 #include "hwa_macros.h"
14 #include "hwa_interrupts.h"
15 #include "hwa_path.h"
16 
17 
18 /* State definitions , positive, negative
19  */
20 #define _hw_state_1 , 1, 0
21 #define _hw_state_0 , 0, 1
22 #define _hw_state_on , 1, 0
23 #define _hw_state_off , 0, 1
24 #define _hw_state_yes , 1, 0
25 #define _hw_state_no , 0, 1
26 #define _hw_state_enabled , 1, 0
27 #define _hw_state_disabled , 0, 1
28 
29 
30 /* Keywords
31  */
32 #define _hw_is_0_0 , 1
33 #define _hw_is_16_16 , 1
34 #define _hw_is_1_1 , 1
35 #define _hw_is_8_8 , 1
36 #define _hw_is__ , 1
37 #define _hw_is__fake__fake , 1
38 #define _hw_is__io1a__io1a , 1
39 #define _hw_is__irq__irq , 1
40 #define _hw_is__m11__m11 , 1
41 #define _hw_is__pin__pin , 1
42 #define _hw_is_ack_ack , 1
43 #define _hw_is_action_action , 1
44 #define _hw_is_address_address , 1
45 #define _hw_is_after_bottom_after_bottom , 1
46 #define _hw_is_after_top_after_top , 1
47 #define _hw_is_ahb_ahb , 1
48 #define _hw_is_alias_alias , 1
49 #define _hw_is_align_align , 1
50 #define _hw_is_bandgap_bandgap , 1
51 #define _hw_is_bottom_bottom , 1
52 #define _hw_is_bps_baudrate , 1
53 #define _hw_is_bps_bps , 1
54 #define _hw_is_class_class , 1
55 #define _hw_is_clock_clock , 1
56 #define _hw_is_cols_cols , 1
57 #define _hw_is_compare_compare , 1
58 #define _hw_is_connected_connected , 1
59 #define _hw_is_counter_counter , 1
60 #define _hw_is_cpb_cpb , 1
61 #define _hw_is_data_data , 1
62 #define _hw_is_data_order_data_order , 1
63 #define _hw_is_databits_databits , 1
64 #define _hw_is_delay_h_delay_h , 1
65 #define _hw_is_delay_l_delay_l , 1
66 #define _hw_is_direction_direction , 1
67 #define _hw_is_direction_direction , 1
68 #define _hw_is_disabled_disabled , 1
69 #define _hw_is_disconnected_disconnected , 1
70 #define _hw_is_e_e , 1
71 #define _hw_is_edge_edge , 1
72 #define _hw_is_enabled_enabled , 1
73 #define _hw_is_external_external , 1
74 #define _hw_is_fast_fast , 1
75 #define _hw_is_filter_filter , 1
76 #define _hw_is_frequency_frequency , 1
77 #define _hw_is_function_function , 1
78 #define _hw_is_gain_gain , 1
79 #define _hw_is_general_call_general_call , 1
80 #define _hw_is_gpio_gpio , 1
81 #define _hw_is_hi8_hi8 , 1
82 #define _hw_is_idle_state_idle_state , 1
83 #define _hw_is_input_input , 1
84 #define _hw_is_interface_interface , 1
85 #define _hw_is_ioclk_ioclk , 1
86 #define _hw_is_irq_irq , 1
87 #define _hw_is_irq_type_irq_type , 1
88 #define _hw_is_latch_latch , 1
89 #define _hw_is_level_level , 1
90 #define _hw_is_lines_lines , 1
91 #define _hw_is_lo8_lo8 , 1
92 #define _hw_is_mode_mode , 1
93 #define _hw_is_multiplier_multiplier , 1
94 #define _hw_is_nack_nack , 1
95 #define _hw_is_negative_input_negative_input , 1
96 #define _hw_is_no_no , 1
97 #define _hw_is_off_off , 1
98 #define _hw_is_output_l_output_l , 1
99 #define _hw_is_output_output , 1
100 #define _hw_is_overflow_overflow , 1
101 #define _hw_is_parity_parity , 1
102 #define _hw_is_pcic_pcic , 1
103 #define _hw_is_polarity_polarity , 1
104 #define _hw_is_port_port , 1
105 #define _hw_is_positive_input_positive_input , 1
106 #define _hw_is_prescaler_prescaler , 1
107 #define _hw_is_pulldown_pulldown , 1
108 #define _hw_is_pullup_pullup , 1
109 #define _hw_is_receiver_receiver , 1
110 #define _hw_is_reload_reload , 1
111 #define _hw_is_reset_reset , 1
112 #define _hw_is_rs_rs , 1
113 #define _hw_is_rule_rule , 1
114 #define _hw_is_run_run , 1
115 #define _hw_is_rw_rw , 1
116 #define _hw_is_rxd_rxd , 1
117 #define _hw_is_sampling_edge_sampling_edge , 1
118 #define _hw_is_scl_scl , 1
119 #define _hw_is_sclhz_sclhz , 1
120 #define _hw_is_sda_sda , 1
121 #define _hw_is_sense_sense , 1
122 #define _hw_is_slave_address_mask_slave_address_mask , 1
123 #define _hw_is_slave_address_slave_address , 1
124 #define _hw_is_sleep_mode_sleep_mode , 1
125 #define _hw_is_sleep_sleep , 1
126 #define _hw_is_source_source , 1
127 #define _hw_is_stopbits_stopbits , 1
128 #define _hw_is_sysclk_sysclk , 1
129 #define _hw_is_timeout_timeout , 1
130 #define _hw_is_top_top , 1
131 #define _hw_is_transmitter_transmitter , 1
132 #define _hw_is_trigger_trigger , 1
133 #define _hw_is_txd_txd , 1
134 #define _hw_is_up_loop_up_loop , 1
135 #define _hw_is_update_update , 1
136 #define _hw_is_var_var , 1
137 #define _hw_is_vref_vref , 1
138 #define _hw_is_yes_yes , 1
139 
140 
141 /* Define wich classes are hardware bits
142  */
143 #define _hw_isa_reg__r8 , 1
144 #define _hw_isa_reg__r16 , 1
145 #define _hw_isa_reg__r32 , 1
146 #define _hw_isa_reg__cb1 , 1
147 #define _hw_isa_reg__cb2 , 1
148 #define _hw_isa_reg__ob1 , 1
149 #define _hw_isa_reg__ob2 , 1
150 #define _hw_isa_reg__xb1 , 1
151 #define _hw_isa_reg__xb2 , 1
152 
153 
154 /* 3-bit numbers
155  */
156 #define _hw_isa_3bn_0 , 1
157 #define _hw_isa_3bn_1 , 1
158 #define _hw_isa_3bn_2 , 1
159 #define _hw_isa_3bn_3 , 1
160 #define _hw_isa_3bn_4 , 1
161 #define _hw_isa_3bn_5 , 1
162 #define _hw_isa_3bn_6 , 1
163 #define _hw_isa_3bn_7 , 1
164 
165 
166 /* 4-bit numbers
167  */
168 #define _hw_isa_4bn_0 , 1
169 #define _hw_isa_4bn_1 , 1
170 #define _hw_isa_4bn_2 , 1
171 #define _hw_isa_4bn_3 , 1
172 #define _hw_isa_4bn_4 , 1
173 #define _hw_isa_4bn_5 , 1
174 #define _hw_isa_4bn_6 , 1
175 #define _hw_isa_4bn_7 , 1
176 #define _hw_isa_4bn_8 , 1
177 #define _hw_isa_4bn_9 , 1
178 #define _hw_isa_4bn_10 , 1
179 #define _hw_isa_4bn_11 , 1
180 #define _hw_isa_4bn_12 , 1
181 #define _hw_isa_4bn_13 , 1
182 #define _hw_isa_4bn_14 , 1
183 #define _hw_isa_4bn_15 , 1
184 
185 
192 #define _hw_par(...) , 1
193 
194 
201 #define hw_class__obj
202 
203 
213 #define hw_class__r8
214 
215 
221 #define hw_class__r16
222 
223 
229 #define hw_class__r32
230 
231 
237 #define hw_class__cb1
238 
239 
245 #define hw_class__cb2
246 
247 
253 #define hw_class__ob1
254 
255 
261 #define hw_class__ob2
262 
263 
269 #define hw_class__xb1
270 
271 
277 #define hw_class__xb2
278 
279 
285 #define hw_class__m11
286 
287 #define _hw_actions__m11 , (read,toggle,write)
288 #define _hwa_actions__m11 , (toggle,write)
289 
290 
291 /*
292  * Provide relative notation for accessing one group of bits of a memory location
293  * '(register,bp)' -> one bit at position bp
294  * '(register,bn,bp)' -> bn bits at position bp
295  * '(register,bn,bp,x)' -> FIXME should trigger an error
296  *
297  * FIXME: assumes 8 bit register (3bn for position). TODO: consider 16 bit registers.
298  * FIXME: should use classes _m11p and _m11np
299  */
300 #define hw__m11_ , _hw_m11_
301 #define _hw_m11_(oo,x,...) _HW_B(_hw_m11r,_hw_isa_3bn_##x)(oo,x,__VA_ARGS__)
302 #define _hw_m11r0(oo,x,...) ,,HW_EM_XNIL(x,(0..7))
303 #define _hw_m11r1(oo,x,o,r,rc,ra,rwm,rfm,rbn,rbp) _HW_B(_hw_m11r1,_hw_is_1_##rbn)(oo,x,o,r,rc,ra,rwm,rfm,rbn,rbp)
304 #define _hw_m11r10(oo,x,o,r,rc,ra,rwm,rfm,rbn,rbp) _m11,(o,r,1,x),(o,r,rc,ra,rwm,rfm,1,x)
305 #define _hw_m11r11(oo,x,o,r,rc,ra,rwm,rfm,rbn,rbp) _m11,(o,r,rbp,x),(o,r,rc,ra,rwm,rfm,rbp,x)
306 
307 
313 #define hw_class__m12
314 
315 
321 #define hw_class__m22
322 
323 #define _hw_actions__m22 , (read,toggle,write)
324 #define _hwa_actions__m22 , (toggle,write)
325 
413 #define hw_class__fake
414 
415 
422 #define hw_fake _fake, -1
423 
424 /* Relatives of fake are fake
425  */
426 #define hw__fake_ , _hw_fake_
427 #define _hw_fake_(o,x,d) _fake, fake, (-1)
428 
429 
437 #define hw_pin _pin, -1
438 #define hw_class__pin
439 
440 
454 #define HW_ADDRESS(...) HW_F( HW_ADDRESS, __VA_ARGS__ )
455 #define HW_ADDRESS_E(...) -1 // Error
456 
457 
458 /* Address correction
459  * C and assembly addresses are offset by 0x20 bytes with avr-gcc (or avrlibc?)
460  */
461 #if !defined HW_AC
462 # define HW_AC
463 #endif
464 
465 #define HW_ADDRESS__m11 , _hw_address_m11
466 #define _hw_address_m11(n,o,r,c,a,...) (a HW_AC)
467 
468 #define HW_ADDRESS_ , _hw_address_ // By default
469 #define _hw_address_(o,a,...) a
470 
471 
480 #define HW_AP(...) HW_F( HW_AP, __VA_ARGS__ )
481 #define HW_AP_E(...) 0 // An error occured
482 
483 #define HW_AP__m11 , _hw_ap_m11
484 #define _hw_ap_m11(n,o,r,c,a,wm,fm,bn,bp,...) (a HW_AC), bp
485 
486 
500 #define HW_BITS(...) HW_F( HW_BITS, __VA_ARGS__ )
501 #define HW_BITS_E(...) 0
502 
503 
526 #define HW_DECLARE(...) HW_F(HW_DECLARE,__VA_ARGS__)
527 #define HW_DECLARE_E(e) HW_E(e) extern uint8_t hw_foo()
528 
529 #define _hw_declare_ ,
530 #define _hw_declare_weak , __attribute__((weak))
531 
532 
548 #define HW_IMPLEMENT(...) HW_F(HW_IMPLEMENT,__VA_ARGS__)
549 #define HW_IMPLEMENT_E(e) HW_E(e) extern uint8_t hw_foo()
550 
551 
557 #define HW_POSITION(...) HW_F( HW_POSITION, __VA_ARGS__ )
558 #define HW_POSITION_E(...) 0 // An error occured
559 //#define HW_POSITION_E(e) HW_E(e) /* FIXME: causes problems with swuarta */
560 
561 #define HW_POSITION__m11 , _hw_position_m11
562 #define _hw_position_m11(n,o,r,c,a,wm,fm,bn,bp,...) bp
563 
564 
565 #if !defined __ASSEMBLER__
566 
567 /* Storage class of HWA functions.
568  *
569  * HWA function-like macros MUST be injected inside their callers and not leave
570  * any trace elsewhere, i.e. they must be strictly equivalent to macros,
571  * otherwise all kinds of HWA warnings and errors will be triggered.
572  *
573  * GCC does the job with only the 'inline' keyword when optimisations are on,
574  * and optimisations must be on when using HWA.
575  *
576  * However, it seems that the 'static' keyword prevents the function to be
577  * inlined unless the 'always_inline' attribute is also specified.
578  *
579  * `__attribute__((always_inline))` triggers a warning if gcc fails to inline the
580  * function.
581  */
582 #define HW_INLINE static inline __attribute__((always_inline))
583 
584 
585 #if defined __cplusplus
586 # define HW_EXTERN_C extern "C"
587 #else
588 # define HW_EXTERN_C
589 #endif
590 
591 
592 /* This structure is used in the HWA context to store one 8-bit hardware
593  * register.
594  */
595 typedef struct
596 {
597  intptr_t a ;
598  uint8_t mmask ;
599  uint8_t mvalue ;
600  uint8_t omask ;
601  uint8_t ovalue ;
602 } hwa_r8_t ;
603 
604 typedef struct
605 {
606  intptr_t a ;
607  uint16_t mmask ;
608  uint16_t mvalue ;
609  uint16_t omask ;
610  uint16_t ovalue ;
611 } hwa_r16_t ;
612 
613 typedef struct
614 {
615  intptr_t a ;
616  uint32_t mmask ;
617  uint32_t mvalue ;
618  uint32_t omask ;
619  uint32_t ovalue ;
620 } hwa_r32_t ;
621 
622 
623 #endif /* !defined __ASSEMBLER__ */
hwa_interrupts.h
Definitions for interrupts.
hwa_path.h
Expand the definition of an object from a path.
hwa_errors.h
Error management: macros and messages (in English)
hwa_macros.h
General purpose macro definitions usable in C or assembler source code.