@@ -69,45 +69,66 @@ void pinMode( uint32_t ulPin, uint32_t ulMode )
69
69
}
70
70
}
71
71
72
- void digitalWrite ( uint32_t ulPin , uint32_t ulVal )
72
+ void digitalWrite (uint32_t ulPin , uint32_t ulVal )
73
73
{
74
- // Handle the case the pin isn't usable as PIO
75
- if ( g_APinDescription [ulPin ].ulPinType == PIO_NOT_A_PIN )
74
+ uint32_t ulGPIOPin = g_APinDescription [ulPin ].ulPin ;
75
+ PortGroup * port = & (PORT -> Group [g_APinDescription [ulPin ].ulPort ]);
76
+
77
+ // Handle the case the pin is invalid
78
+ if (ulPin >= PINS_COUNT )
76
79
{
77
80
return ;
78
81
}
79
82
80
- // Enable pull-up resistor
81
- PORT -> Group [g_APinDescription [ulPin ].ulPort ].PINCFG [g_APinDescription [ulPin ].ulPin ].reg = (uint8_t )(PORT_PINCFG_PULLEN ) ;
82
-
83
- switch ( ulVal )
83
+ // Test if pin is set to INPUT mode, then activate pull-up according to ulVal
84
+ if (port -> DIR .reg & (1ul <<ulGPIOPin ) != 0 )
84
85
{
85
- case LOW :
86
- PORT -> Group [g_APinDescription [ulPin ].ulPort ].OUTCLR .reg = (1ul << g_APinDescription [ulPin ].ulPin ) ;
87
- break ;
88
-
89
- default :
90
- PORT -> Group [g_APinDescription [ulPin ].ulPort ].OUTSET .reg = (1ul << g_APinDescription [ulPin ].ulPin ) ;
91
- break ;
86
+ switch (ulVal )
87
+ {
88
+ case LOW :
89
+ // Disable pull-up resistor
90
+ port -> PINCFG [ulGPIOPin ].bit .PULLEN = 0 ;
91
+ break ;
92
+
93
+ case HIGH :
94
+ default :
95
+ // Enable pull-up resistor
96
+ port -> PINCFG [ulGPIOPin ].bit .PULLEN = 1 ;
97
+ break ;
98
+ }
99
+ }
100
+ else // pin is set to OUTPUT mode, we output the requested voltage level
101
+ {
102
+ switch (ulVal )
103
+ {
104
+ case LOW :
105
+ port -> OUTCLR .reg = (1ul <<ulGPIOPin );
106
+ break ;
107
+
108
+ case HIGH :
109
+ default :
110
+ port -> OUTSET .reg = (1ul <<ulGPIOPin );
111
+ break ;
112
+ }
92
113
}
93
114
94
115
return ;
95
116
}
96
117
97
118
int digitalRead ( uint32_t ulPin )
98
119
{
99
- // Handle the case the pin isn't usable as PIO
100
- if ( g_APinDescription [ ulPin ]. ulPinType == PIO_NOT_A_PIN )
120
+ // Handle the case the pin is invalid
121
+ if (ulPin >= PINS_COUNT )
101
122
{
102
- return LOW ;
123
+ return LOW ;
103
124
}
104
125
105
126
if ( (PORT -> Group [g_APinDescription [ulPin ].ulPort ].IN .reg & (1ul << g_APinDescription [ulPin ].ulPin )) != 0 )
106
127
{
107
- return HIGH ;
128
+ return HIGH ;
108
129
}
109
130
110
- return LOW ;
131
+ return LOW ;
111
132
}
112
133
113
134
#ifdef __cplusplus
0 commit comments