Skip to content

Commit 340e267

Browse files
Roy, ElizabethWalberg, Irun
authored andcommitted
digitalWrite() changes made after review
1 parent 5ad321d commit 340e267

File tree

1 file changed

+41
-15
lines changed

1 file changed

+41
-15
lines changed

cores/arduino/wiring_digital.c

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void pinMode(uint8_t pin, PinMode mode)
3434

3535
PORT_t* port = digitalPinToPortStruct(pin);
3636
if(port == NULL) return;
37-
37+
3838
uint8_t bit_mask = (1 << bit_pos);
3939

4040
if(mode == OUTPUT){
@@ -150,33 +150,59 @@ void digitalWrite(uint8_t pin, PinStatus val)
150150
PORT_t *port = digitalPinToPortStruct(pin);
151151
if(port == NULL) return;
152152

153-
// /* Output direction */
154-
// if(port->DIR & bit_mask){
153+
/* Output direction */
154+
if(port->DIR & bit_mask){
155155

156156
/* Save system status and disable interrupts */
157157
uint8_t status = SREG;
158158
cli();
159159

160160
/* 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 */
164165
port->OUTTGL = bit_mask;
166+
/* If HIGH OR > TOGGLE */
165167
} else {
166-
port->OUTCLR = bit_mask;
168+
port->OUTSET = bit_mask;
167169
}
168170

169171
/* Restore system status */
170172
SREG = status;
171173

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+
180206
}
181207

182208
PinStatus digitalRead(uint8_t pin)

0 commit comments

Comments
 (0)