5 #include "libs/LPC17xx/sLPC17xx.h" // smoothed mbed.h lib
6 #include "libs/Kernel.h"
7 #include "libs/utils.h"
14 Pin
* from_string(std::string value
){
15 LPC_GPIO_TypeDef
* gpios
[5] ={LPC_GPIO0
,LPC_GPIO1
,LPC_GPIO2
,LPC_GPIO3
,LPC_GPIO4
};
16 if( value
.find_first_of("n")!=string::npos
? true : false ){
17 this->port_number
= 0;
18 this->port
= gpios
[(unsigned int) this->port_number
];
19 this->inverting
= false;
22 this->port_number
= atoi(value
.substr(0,1).c_str());
23 this->port
= gpios
[(unsigned int) this->port_number
];
24 this->inverting
= ( value
.find_first_of("!")!=string::npos
? true : false );
25 this->pin
= atoi( value
.substr(2, value
.size()-2-(this->inverting
?1:0)).c_str() );
30 inline bool connected(){
31 if( this->pin
== 255 ){ return false; }
35 inline Pin
* as_output(){
36 this->port
->FIODIR
|= 1<<this->pin
;
40 inline Pin
* as_input(){
41 this->port
->FIODIR
&= ~(1<<this->pin
);
45 inline Pin
* as_open_drain(){
46 if( this->port_number
== 0 ){ LPC_PINCON
->PINMODE_OD0
|= (1<<this->pin
); }
47 if( this->port_number
== 1 ){ LPC_PINCON
->PINMODE_OD1
|= (1<<this->pin
); }
48 if( this->port_number
== 2 ){ LPC_PINCON
->PINMODE_OD2
|= (1<<this->pin
); }
49 if( this->port_number
== 3 ){ LPC_PINCON
->PINMODE_OD3
|= (1<<this->pin
); }
50 if( this->port_number
== 4 ){ LPC_PINCON
->PINMODE_OD4
|= (1<<this->pin
); }
54 inline Pin
* pull_up(){
55 // Set the two bits for this pin as 00
56 if( this->port_number
== 0 && this->pin
< 16 ){ LPC_PINCON
->PINMODE0
&= ~(3<<( this->pin
*2)); }
57 if( this->port_number
== 0 && this->pin
>= 16 ){ LPC_PINCON
->PINMODE1
&= ~(3<<((this->pin
-16)*2)); }
58 if( this->port_number
== 1 && this->pin
< 16 ){ LPC_PINCON
->PINMODE2
&= ~(3<<( this->pin
*2)); }
59 if( this->port_number
== 1 && this->pin
>= 16 ){ LPC_PINCON
->PINMODE3
&= ~(3<<((this->pin
-16)*2)); }
60 if( this->port_number
== 2 && this->pin
< 16 ){ LPC_PINCON
->PINMODE4
&= ~(3<<( this->pin
*2)); }
61 if( this->port_number
== 3 && this->pin
>= 16 ){ LPC_PINCON
->PINMODE7
&= ~(3<<((this->pin
-16)*2)); }
62 if( this->port_number
== 4 && this->pin
>= 16 ){ LPC_PINCON
->PINMODE9
&= ~(3<<((this->pin
-16)*2)); }
66 inline Pin
* pull_down(){
67 // Set the two bits for this pin as 00
68 if( this->port_number
== 0 && this->pin
< 16 ){ LPC_PINCON
->PINMODE0
|= (3<<( this->pin
*2)); }
69 if( this->port_number
== 0 && this->pin
>= 16 ){ LPC_PINCON
->PINMODE1
|= (3<<((this->pin
-16)*2)); }
70 if( this->port_number
== 1 && this->pin
< 16 ){ LPC_PINCON
->PINMODE2
|= (3<<( this->pin
*2)); }
71 if( this->port_number
== 1 && this->pin
>= 16 ){ LPC_PINCON
->PINMODE3
|= (3<<((this->pin
-16)*2)); }
72 if( this->port_number
== 2 && this->pin
< 16 ){ LPC_PINCON
->PINMODE4
|= (3<<( this->pin
*2)); }
73 if( this->port_number
== 3 && this->pin
>= 16 ){ LPC_PINCON
->PINMODE7
|= (3<<((this->pin
-16)*2)); }
74 if( this->port_number
== 4 && this->pin
>= 16 ){ LPC_PINCON
->PINMODE9
|= (3<<((this->pin
-16)*2)); }
79 return this->inverting
^ (( this->port
->FIOPIN
>> this->pin
) & 1);
82 inline void set(bool value
){
83 if( this->inverting
^ value
){
84 this->port
->FIOSET
= 1 << this->pin
;
86 this->port
->FIOCLR
= 1 << this->pin
;
91 LPC_GPIO_TypeDef
* port
;