forked from DeqingSun/ch55xduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpinmode-c.c
More file actions
54 lines (49 loc) · 1.4 KB
/
pinmode-c.c
File metadata and controls
54 lines (49 loc) · 1.4 KB
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
/*
* test spi functions
*/
#include "Arduino.h"
void pinMode_c(uint8_t pin, uint8_t mode)
{
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
volatile GPIO_TypeDef *gpio;
if (port == NOT_A_PORT) return;
gpio = (GPIO_TypeDef *) portOutputRegister(port);
if (mode == INPUT) {
BEGIN_CRITICAL
gpio->CR2 &= ~bit; // first: deactivate interrupt
gpio->CR1 &= ~bit; // release top side
gpio->DDR &= ~bit; // now set direction
END_CRITICAL
} else if (mode == INPUT_PULLUP) {
BEGIN_CRITICAL
gpio->CR2 &= ~bit; // first: deactivate interrupt
gpio->DDR &= ~bit; // set direction before
gpio->CR1 |= bit; // activating the pull up
END_CRITICAL
} else if (mode == OUTPUT_FAST) {// output push-pull, fast
BEGIN_CRITICAL
gpio->CR1 |= bit;
gpio->DDR |= bit; // direction before setting CR2 to
gpio->CR2 |= bit; // avoid accidental interrupt
END_CRITICAL
} else if (mode == OUTPUT_OD_FAST) { // output open drain, fast
BEGIN_CRITICAL
gpio->CR1 &= ~bit;
gpio->DDR |= bit; // direction before setting CR2 to
gpio->CR2 |= bit; // avoid accidental interrupt
END_CRITICAL
} else if (mode == OUTPUT_OD) { // output open drain, slow
BEGIN_CRITICAL
gpio->CR1 &= ~bit;
gpio->CR2 &= ~bit;
gpio->DDR |= bit;
END_CRITICAL
} else { // output push-pull, slow
BEGIN_CRITICAL
gpio->CR1 |= bit;
gpio->CR2 &= ~bit;
gpio->DDR |= bit;
END_CRITICAL
}
}