6 this->inverting
= false;
9 // Make a new pin object from a string
10 Pin
* Pin::from_string(std::string value
){
11 LPC_GPIO_TypeDef
* gpios
[5] ={LPC_GPIO0
,LPC_GPIO1
,LPC_GPIO2
,LPC_GPIO3
,LPC_GPIO4
};
13 // cs is the current position in the string
14 const char* cs
= value
.c_str();
15 // cn is the position of the next char after the number we just read
18 // grab first integer as port. pointer to first non-digit goes in cn
19 this->port_number
= strtol(cs
, &cn
, 10);
20 // if cn > cs then strtol read at least one digit
21 if ((cn
> cs
) && (port_number
<= 4)){
22 // translate port index into something useful
23 this->port
= gpios
[(unsigned int) this->port_number
];
24 // if the char after the first integer is a . then we should expect a pin index next
26 // move pointer to first digit (hopefully) of pin index
30 this->pin
= strtol(cs
, &cn
, 10);
32 // if strtol read some numbers, cn will point to the first non-digit
33 if ((cn
> cs
) && (pin
< 32)){
34 this->port
->FIOMASK
&= ~(1 << this->pin
);
36 // now check for modifiers:-
38 // o = set pin to open drain
39 // ^ = set pin to pull up
40 // v = set pin to pull down
41 // - = set pin to no pull up or down
42 // @ = set pin to repeater mode
46 this->inverting
= true;
64 // skip any whitespace following the pin index
65 if (!is_whitespace(*cn
))
74 // from_string failed. TODO: some sort of error
82 // Configure this pin as OD
83 Pin
* Pin::as_open_drain(){
84 if (this->pin
>= 32) return this;
85 if( this->port_number
== 0 ){ LPC_PINCON
->PINMODE_OD0
|= (1<<this->pin
); }
86 if( this->port_number
== 1 ){ LPC_PINCON
->PINMODE_OD1
|= (1<<this->pin
); }
87 if( this->port_number
== 2 ){ LPC_PINCON
->PINMODE_OD2
|= (1<<this->pin
); }
88 if( this->port_number
== 3 ){ LPC_PINCON
->PINMODE_OD3
|= (1<<this->pin
); }
89 if( this->port_number
== 4 ){ LPC_PINCON
->PINMODE_OD4
|= (1<<this->pin
); }
90 pull_none(); // no pull up by default
95 // Configure this pin as a repeater
96 Pin
* Pin::as_repeater(){
97 if (this->pin
>= 32) return this;
98 // Set the two bits for this pin as 01
99 if( this->port_number
== 0 && this->pin
< 16 ){ LPC_PINCON
->PINMODE0
|= (1<<( this->pin
*2)); LPC_PINCON
->PINMODE0
&= ~(2<<( this->pin
*2)); }
100 if( this->port_number
== 0 && this->pin
>= 16 ){ LPC_PINCON
->PINMODE1
|= (1<<( this->pin
*2)); LPC_PINCON
->PINMODE1
&= ~(2<<((this->pin
-16)*2)); }
101 if( this->port_number
== 1 && this->pin
< 16 ){ LPC_PINCON
->PINMODE2
|= (1<<( this->pin
*2)); LPC_PINCON
->PINMODE2
&= ~(2<<( this->pin
*2)); }
102 if( this->port_number
== 1 && this->pin
>= 16 ){ LPC_PINCON
->PINMODE3
|= (1<<( this->pin
*2)); LPC_PINCON
->PINMODE3
&= ~(2<<((this->pin
-16)*2)); }
103 if( this->port_number
== 2 && this->pin
< 16 ){ LPC_PINCON
->PINMODE4
|= (1<<( this->pin
*2)); LPC_PINCON
->PINMODE4
&= ~(2<<( this->pin
*2)); }
104 if( this->port_number
== 3 && this->pin
>= 16 ){ LPC_PINCON
->PINMODE7
|= (1<<( this->pin
*2)); LPC_PINCON
->PINMODE7
&= ~(2<<((this->pin
-16)*2)); }
105 if( this->port_number
== 4 && this->pin
>= 16 ){ LPC_PINCON
->PINMODE9
|= (1<<( this->pin
*2)); LPC_PINCON
->PINMODE9
&= ~(2<<((this->pin
-16)*2)); }
109 // Configure this pin as no pullup or pulldown
110 Pin
* Pin::pull_none(){
111 if (this->pin
>= 32) return this;
112 // Set the two bits for this pin as 10
113 if( this->port_number
== 0 && this->pin
< 16 ){ LPC_PINCON
->PINMODE0
|= (2<<( this->pin
*2)); LPC_PINCON
->PINMODE0
&= ~(1<<( this->pin
*2)); }
114 if( this->port_number
== 0 && this->pin
>= 16 ){ LPC_PINCON
->PINMODE1
|= (2<<( this->pin
*2)); LPC_PINCON
->PINMODE1
&= ~(1<<((this->pin
-16)*2)); }
115 if( this->port_number
== 1 && this->pin
< 16 ){ LPC_PINCON
->PINMODE2
|= (2<<( this->pin
*2)); LPC_PINCON
->PINMODE2
&= ~(1<<( this->pin
*2)); }
116 if( this->port_number
== 1 && this->pin
>= 16 ){ LPC_PINCON
->PINMODE3
|= (2<<( this->pin
*2)); LPC_PINCON
->PINMODE3
&= ~(1<<((this->pin
-16)*2)); }
117 if( this->port_number
== 2 && this->pin
< 16 ){ LPC_PINCON
->PINMODE4
|= (2<<( this->pin
*2)); LPC_PINCON
->PINMODE4
&= ~(1<<( this->pin
*2)); }
118 if( this->port_number
== 3 && this->pin
>= 16 ){ LPC_PINCON
->PINMODE7
|= (2<<( this->pin
*2)); LPC_PINCON
->PINMODE7
&= ~(1<<((this->pin
-16)*2)); }
119 if( this->port_number
== 4 && this->pin
>= 16 ){ LPC_PINCON
->PINMODE9
|= (2<<( this->pin
*2)); LPC_PINCON
->PINMODE9
&= ~(1<<((this->pin
-16)*2)); }
123 // Configure this pin as a pullup
125 if (this->pin
>= 32) return this;
126 // Set the two bits for this pin as 00
127 if( this->port_number
== 0 && this->pin
< 16 ){ LPC_PINCON
->PINMODE0
&= ~(3<<( this->pin
*2)); }
128 if( this->port_number
== 0 && this->pin
>= 16 ){ LPC_PINCON
->PINMODE1
&= ~(3<<((this->pin
-16)*2)); }
129 if( this->port_number
== 1 && this->pin
< 16 ){ LPC_PINCON
->PINMODE2
&= ~(3<<( this->pin
*2)); }
130 if( this->port_number
== 1 && this->pin
>= 16 ){ LPC_PINCON
->PINMODE3
&= ~(3<<((this->pin
-16)*2)); }
131 if( this->port_number
== 2 && this->pin
< 16 ){ LPC_PINCON
->PINMODE4
&= ~(3<<( this->pin
*2)); }
132 if( this->port_number
== 3 && this->pin
>= 16 ){ LPC_PINCON
->PINMODE7
&= ~(3<<((this->pin
-16)*2)); }
133 if( this->port_number
== 4 && this->pin
>= 16 ){ LPC_PINCON
->PINMODE9
&= ~(3<<((this->pin
-16)*2)); }
137 // Configure this pin as a pulldown
138 Pin
* Pin::pull_down(){
139 if (this->pin
>= 32) return this;
140 // Set the two bits for this pin as 11
141 if( this->port_number
== 0 && this->pin
< 16 ){ LPC_PINCON
->PINMODE0
|= (3<<( this->pin
*2)); }
142 if( this->port_number
== 0 && this->pin
>= 16 ){ LPC_PINCON
->PINMODE1
|= (3<<((this->pin
-16)*2)); }
143 if( this->port_number
== 1 && this->pin
< 16 ){ LPC_PINCON
->PINMODE2
|= (3<<( this->pin
*2)); }
144 if( this->port_number
== 1 && this->pin
>= 16 ){ LPC_PINCON
->PINMODE3
|= (3<<((this->pin
-16)*2)); }
145 if( this->port_number
== 2 && this->pin
< 16 ){ LPC_PINCON
->PINMODE4
|= (3<<( this->pin
*2)); }
146 if( this->port_number
== 3 && this->pin
>= 16 ){ LPC_PINCON
->PINMODE7
|= (3<<((this->pin
-16)*2)); }
147 if( this->port_number
== 4 && this->pin
>= 16 ){ LPC_PINCON
->PINMODE9
|= (3<<((this->pin
-16)*2)); }