@@ -18,8 +18,13 @@ pub const DriveStrength = enum(u2) {
1818 @"40mA" = 3 ,
1919};
2020
21- pub const Pin = struct {
22- number : u5 ,
21+ pub fn num (n : u5 ) Pin {
22+ std .debug .assert (n < 22 );
23+ return @as (Pin , @enumFromInt (n ));
24+ }
25+
26+ pub const Pin = enum (u5 ) {
27+ _ ,
2328
2429 pub const Config = struct {
2530 input_enable : bool = false ,
@@ -34,7 +39,8 @@ pub const Pin = struct {
3439 };
3540
3641 pub fn apply (self : Pin , config : Config ) void {
37- IO_MUX .GPIO [self .number ].modify (.{
42+ const n = @intFromEnum (self );
43+ IO_MUX .GPIO [n ].modify (.{
3844 .SLP_SEL = 0 ,
3945 .FUN_WPD = @intFromBool (config .pulldown_enable ),
4046 .FUN_WPU = @intFromBool (config .pullup_enable ),
@@ -44,21 +50,21 @@ pub const Pin = struct {
4450 .MCU_SEL = 1 ,
4551 });
4652
47- GPIO .FUNC_OUT_SEL_CFG [self . number ].write (.{
53+ GPIO .FUNC_OUT_SEL_CFG [n ].write (.{
4854 .OUT_SEL = @intFromEnum (config .output_signal ),
4955 .OEN_SEL = @intFromBool (config .output_enable ),
5056 .INV_SEL = @intFromBool (config .output_invert ),
5157 .OEN_INV_SEL = 0 ,
5258 .padding = 0 ,
5359 });
5460
55- GPIO .PIN [self . number ].modify (.{
61+ GPIO .PIN [n ].modify (.{
5662 .PIN_PAD_DRIVER = @intFromBool (config .open_drain ),
5763 });
5864
5965 // NOTE: Assert that the USB_SERIAL_JTAG peripheral which uses pins GPIO18 and GPIO19
6066 // is disabled and USB pullup/down resistors are disabled.
61- if (self . number == 18 or self . number == 19 ) {
67+ if (n == 18 or n == 19 ) {
6268 const usb_conf0 = peripherals .USB_DEVICE .CONF0 .read ();
6369 std .debug .assert (usb_conf0 .USB_PAD_ENABLE == 0 and
6470 usb_conf0 .DP_PULLUP == 0 and
@@ -67,84 +73,86 @@ pub const Pin = struct {
6773 usb_conf0 .DM_PULLDOWN == 0 );
6874 }
6975
70- GPIO .ENABLE_W1TS .write (.{ .ENABLE_W1TS = @as (u26 , 1 ) << self . number , .padding = 0 });
76+ GPIO .ENABLE_W1TS .write (.{ .ENABLE_W1TS = @as (u26 , 1 ) << n , .padding = 0 });
7177 }
7278
7379 pub fn reset (self : Pin ) void {
74- IO_MUX .GPIO [self .number ].write_raw (0x00 );
75- GPIO .FUNC_OUT_SEL_CFG [self .number ].write_raw (0x00 );
76- GPIO .PIN [self .number ].write_raw (0x00 );
77- GPIO .ENABLE_W1TC .write (.{ .ENABLE_W1TC = @as (u26 , 1 ) << self .number , .padding = 0 });
80+ const n = @intFromEnum (self );
81+ IO_MUX .GPIO [n ].write_raw (0x00 );
82+ GPIO .FUNC_OUT_SEL_CFG [n ].write_raw (0x00 );
83+ GPIO .PIN [n ].write_raw (0x00 );
84+ GPIO .ENABLE_W1TC .write (.{ .ENABLE_W1TC = @as (u26 , 1 ) << n , .padding = 0 });
7885 }
7986
8087 pub fn set_output_enable (self : Pin , enable : bool ) void {
81- GPIO .FUNC_OUT_SEL_CFG [self . number ].modify (.{
88+ GPIO .FUNC_OUT_SEL_CFG [@intFromEnum ( self ) ].modify (.{
8289 .OEN_SEL = @intFromBool (enable ),
8390 });
8491 }
8592
8693 pub fn set_open_drain (self : Pin , enable : bool ) void {
87- GPIO .PIN [self . number ].modify (.{
94+ GPIO .PIN [@intFromEnum ( self ) ].modify (.{
8895 .PIN_PAD_DRIVER = @intFromBool (enable ),
8996 });
9097 }
9198
9299 pub fn set_pullup (self : Pin , enable : bool ) void {
93- IO_MUX .GPIO [self . number ].modify (.{
100+ IO_MUX .GPIO [@intFromEnum ( self ) ].modify (.{
94101 .MCU_WPU = @intFromBool (enable ),
95102 });
96103 }
97104
98105 pub fn set_pulldown (self : Pin , enable : bool ) void {
99- IO_MUX .GPIO [self . number ].modify (.{
106+ IO_MUX .GPIO [@intFromEnum ( self ) ].modify (.{
100107 .MCU_WPD = @intFromBool (enable ),
101108 });
102109 }
103110
104111 pub fn set_output_invert (self : Pin , enable : bool ) void {
105- GPIO .FUNC_OUT_SEL_CFG [self . number ].modify (.{
112+ GPIO .FUNC_OUT_SEL_CFG [@intFromEnum ( self ) ].modify (.{
106113 .OEN_INV_SEL = enable ,
107114 });
108115 }
109116
110117 pub fn set_output_drive_strength (self : Pin , strength : DriveStrength ) void {
111- IO_MUX .GPIO [self . number ].modify (.{
118+ IO_MUX .GPIO [@intFromEnum ( self ) ].modify (.{
112119 .FUN_DRV = @intFromEnum (strength ),
113120 });
114121 }
115122
116123 pub fn set_input_filter (self : Pin , enable : bool ) void {
117- IO_MUX .GPIO [self . number ].modify (.{
124+ IO_MUX .GPIO [@intFromEnum ( self ) ].modify (.{
118125 .FILTER_EN = @intFromBool (enable ),
119126 });
120127 }
121128
122129 pub fn write (self : Pin , level : Level ) void {
130+ const n = @intFromEnum (self );
123131 // Assert that the pin is set to output enabled
124- std .debug .assert (GPIO .FUNC_OUT_SEL_CFG [self . number ].read ().OEN_SEL == 1 );
132+ std .debug .assert (GPIO .FUNC_OUT_SEL_CFG [n ].read ().OEN_SEL == 1 );
125133
126134 switch (level ) {
127- Level .low = > GPIO .OUT_W1TC .write (.{ .OUT_W1TC = @as (u26 , 1 ) << self . number }),
128- Level .high = > GPIO .OUT_W1TS .write (.{ .OUT_W1TS = @as (u26 , 1 ) << self . number }),
135+ Level .low = > GPIO .OUT_W1TC .write (.{ .OUT_W1TC = @as (u26 , 1 ) << n }),
136+ Level .high = > GPIO .OUT_W1TS .write (.{ .OUT_W1TS = @as (u26 , 1 ) << n }),
129137 }
130138 }
131139
132140 pub fn get_output_state (self : Pin ) Level {
133- std .debug .assert (GPIO .FUNC_OUT_SEL_CFG [self . number ].read ().OEN_SEL == 1 );
141+ std .debug .assert (GPIO .FUNC_OUT_SEL_CFG [@intFromEnum ( self ) ].read ().OEN_SEL == 1 );
134142
135- return @enumFromInt ((GPIO .OUT .raw >> self . number & 0x01 ));
143+ return @enumFromInt ((GPIO .OUT .raw >> @intFromEnum ( self ) & 0x01 ));
136144 }
137145
138146 pub fn read (self : Pin ) Level {
139- std .debug .assert (IO_MUX .GPIO [self . number ].read ().FUN_IE == 1 );
147+ std .debug .assert (IO_MUX .GPIO [@intFromEnum ( self ) ].read ().FUN_IE == 1 );
140148
141- return @enumFromInt (GPIO .IN .raw >> self . number & 0x01 );
149+ return @enumFromInt (GPIO .IN .raw >> @intFromEnum ( self ) & 0x01 );
142150 }
143151
144152 pub fn toggle (self : Pin ) void {
145- switch (get_output_state (self )) {
146- Level .low = > write (self , Level .high ),
147- Level .high = > write (self , Level .low ),
153+ switch (self . get_output_state ()) {
154+ Level .low = > self . write (Level .high ),
155+ Level .high = > self . write (Level .low ),
148156 }
149157 }
150158};
@@ -153,33 +161,3 @@ pub const OutputSignal = enum(u8) {
153161 ledc_ls_sig_out0 = 45 ,
154162 gpio = 128 ,
155163};
156-
157- pub const instance = struct {
158- pub const GPIO0 : Pin = .{ .number = 0 };
159- pub const GPIO1 : Pin = .{ .number = 1 };
160- pub const GPIO2 : Pin = .{ .number = 2 };
161- pub const GPIO3 : Pin = .{ .number = 3 };
162- pub const GPIO4 : Pin = .{ .number = 4 };
163- pub const GPIO5 : Pin = .{ .number = 5 };
164- pub const GPIO6 : Pin = .{ .number = 6 };
165- pub const GPIO7 : Pin = .{ .number = 7 };
166- pub const GPIO8 : Pin = .{ .number = 8 };
167- pub const GPIO9 : Pin = .{ .number = 9 };
168- pub const GPIO10 : Pin = .{ .number = 10 };
169- pub const GPIO11 : Pin = .{ .number = 11 };
170- pub const GPIO12 : Pin = .{ .number = 12 };
171- pub const GPIO13 : Pin = .{ .number = 13 };
172- pub const GPIO14 : Pin = .{ .number = 14 };
173- pub const GPIO15 : Pin = .{ .number = 15 };
174- pub const GPIO16 : Pin = .{ .number = 16 };
175- pub const GPIO17 : Pin = .{ .number = 17 };
176- pub const GPIO18 : Pin = .{ .number = 18 };
177- pub const GPIO19 : Pin = .{ .number = 19 };
178- pub const GPIO20 : Pin = .{ .number = 20 };
179- pub const GPIO21 : Pin = .{ .number = 21 };
180-
181- pub fn num (instance_number : u5 ) Pin {
182- std .debug .assert (instance_number < 22 );
183- return .{ .number = instance_number };
184- }
185- };
0 commit comments