@@ -34,7 +34,7 @@ void pinMode(uint8_t pin, PinMode mode)
34
34
35
35
PORT_t * port = digitalPinToPortStruct (pin );
36
36
if (port == NULL ) return ;
37
-
37
+
38
38
uint8_t bit_mask = (1 << bit_pos );
39
39
40
40
if (mode == OUTPUT ){
@@ -150,33 +150,59 @@ void digitalWrite(uint8_t pin, PinStatus val)
150
150
PORT_t * port = digitalPinToPortStruct (pin );
151
151
if (port == NULL ) return ;
152
152
153
- // /* Output direction */
154
- // if(port->DIR & bit_mask){
153
+ /* Output direction */
154
+ if (port -> DIR & bit_mask ){
155
155
156
156
/* Save system status and disable interrupts */
157
157
uint8_t status = SREG ;
158
158
cli ();
159
159
160
160
/* Set output to value */
161
- if (val == HIGH ) {
162
- port -> OUTSET = bit_mask ;
163
- } else if (val == TOGGLE ) {
161
+ if (val == LOW ) { /* If LOW */
162
+ port -> OUTCLR = bit_mask ;
163
+
164
+ } else if (val == TOGGLE ) { /* If TOGGLE */
164
165
port -> OUTTGL = bit_mask ;
166
+ /* If HIGH OR > TOGGLE */
165
167
} else {
166
- port -> OUTCLR = bit_mask ;
168
+ port -> OUTSET = bit_mask ;
167
169
}
168
170
169
171
/* Restore system status */
170
172
SREG = status ;
171
173
172
- // } else {
173
- // /* Old implementation has side effect when pin set as input -
174
- // pull up is enabled if this function is called.
175
- // Should we purposely implement this side effect?
176
- // */
177
- //
178
- // /* Enable pull-up ?? */
179
- // }
174
+
175
+ /* Input direction */
176
+ } else {
177
+ /* Old implementation has side effect when pin set as input -
178
+ pull up is enabled if this function is called.
179
+ Should we purposely implement this side effect?
180
+ */
181
+
182
+ /* Get bit position for getting pin ctrl reg */
183
+ uint8_t bit_pos = digitalPinToBitPosition (pin );
184
+
185
+ /* Calculate where pin control register is */
186
+ uint8_t * pin_ctrl_reg = getPINnCTRLregister (port , bit_pos );
187
+
188
+ /* Save system status and disable interrupts */
189
+ uint8_t status = SREG ;
190
+ cli ();
191
+
192
+ if (val == LOW ){
193
+ /* Disable pullup */
194
+ * pin_ctrl_reg &= ~PORT_PULLUPEN_bm ;
195
+
196
+ } else {
197
+ /* Enable pull-up */
198
+ * pin_ctrl_reg |= PORT_PULLUPEN_bm ;
199
+ }
200
+
201
+ /* Restore system status */
202
+ SREG = status ;
203
+
204
+ }
205
+
180
206
}
181
207
182
208
PinStatus digitalRead (uint8_t pin )
0 commit comments