Rock/GPIO
< Rock
Checkout the rock/extension_header page for gpios on the extension header.
Below is programming in different languages to bit flipping the GPIO on RR.
Contents
C
wiringX is a modular approach to several GPIO interfaces. WiringX officially supports radxa rock/pro/lite, please check Rock/wiringX for how to use it.
#include <stdio.h>
int main()
{
FILE *fp = fopen("/sys/class/gpio/export", "w");
if( !fp )
{
printf("error open export file");
return 1;
}
fprintf(fp, "%d", 172);
fclose(fp);
fp = fopen("/sys/class/gpio/gpio175/direction", "w");
if( !fp )
{
printf("error open export file");
return 1;
}
fprintf(fp, "%s", "out");
fclose(fp);
fp = fopen("/sys/class/gpio/gpio175/value", "w");
if( !fp )
{
printf("error open export file");
return 1;
}
fprintf(fp, "%d", 1);
}
int main()
{
FILE *fp = fopen("/sys/class/gpio/export", "w");
if( !fp )
{
printf("error open export file");
return 1;
}
fprintf(fp, "%d", 172);
fclose(fp);
fp = fopen("/sys/class/gpio/gpio175/direction", "w");
if( !fp )
{
printf("error open export file");
return 1;
}
fprintf(fp, "%s", "out");
fclose(fp);
fp = fopen("/sys/class/gpio/gpio175/value", "w");
if( !fp )
{
printf("error open export file");
return 1;
}
fprintf(fp, "%d", 1);
}
Python
Python library.
sudo apt-get install gcc python-dev
import time
def initpin(pinnum, mode):
'''
pinnum: pin number, eg. 172, 175 etc.
mode: pin mode, valid values: in or out
'''
with open('/sys/class/gpio/export', 'w') as f:
f.write(str(pinnum))
with open('/sys/class/gpio/gpio' + str(pinnum) + '/direction', 'w') as f:
f.write(str(mode))
def setpin(pinnum, value):
with open('/sys/class/gpio/gpio' + str(pinnum) + '/value', 'w') as f:
f.write(str(value))
if __name__ == '__main__':
initpin(172, 'out')
while True:
setpin(172, 0)
time.sleep(1)
setpin(172, 1)
time.sleep(1)
def initpin(pinnum, mode):
'''
pinnum: pin number, eg. 172, 175 etc.
mode: pin mode, valid values: in or out
'''
with open('/sys/class/gpio/export', 'w') as f:
f.write(str(pinnum))
with open('/sys/class/gpio/gpio' + str(pinnum) + '/direction', 'w') as f:
f.write(str(mode))
def setpin(pinnum, value):
with open('/sys/class/gpio/gpio' + str(pinnum) + '/value', 'w') as f:
f.write(str(value))
if __name__ == '__main__':
initpin(172, 'out')
while True:
setpin(172, 0)
time.sleep(1)
setpin(172, 1)
time.sleep(1)
Shell
#!/bin/sh
# enable the gpio 172 -> green led
echo 172 > /sys/class/gpio/export
# set the direction to output
echo "out" > /sys/class/gpio/gpio172/direction
while true;
do
echo 0 > /sys/class/gpio/gpio172/value #led on
sleep 1
echo 1 > /sys/class/gpio/gpio172/value #led off
sleep 1
done
# enable the gpio 172 -> green led
echo 172 > /sys/class/gpio/export
# set the direction to output
echo "out" > /sys/class/gpio/gpio172/direction
while true;
do
echo 0 > /sys/class/gpio/gpio172/value #led on
sleep 1
echo 1 > /sys/class/gpio/gpio172/value #led off
sleep 1
done
JavaScript (Node.js)
Follow the directions at https://github.com/jdarling/radxa-nodejs-img
var fs = require('fs');
// Enable the GPIO 172
fs.writeFileSync('/sys/class/gpio/export', 172);
// Set it to output
fs.writeFileSync('/sys/class/gpio/gpio172/direction', 'out');
// Turn on the LED
fs.writeFileSync('/ssy/class/gpio/gpio172/value', 1);
// Wait 1 second then turn it off
setTimeout(function(){
fs.writeFileSync('/ssy/class/gpio/gpio172/value', 0);
}, 1000);
// Enable the GPIO 172
fs.writeFileSync('/sys/class/gpio/export', 172);
// Set it to output
fs.writeFileSync('/sys/class/gpio/gpio172/direction', 'out');
// Turn on the LED
fs.writeFileSync('/ssy/class/gpio/gpio172/value', 1);
// Wait 1 second then turn it off
setTimeout(function(){
fs.writeFileSync('/ssy/class/gpio/gpio172/value', 0);
}, 1000);
Or better yet, use https://github.com/jdarling/radxa-rock
pin numbers
Pin number for kernel 3.0.36 starts from 160. For kernel version 3.10 and later(device tree based), the pin number starts from 0.
Kernel 3.0.36
GPIO0_A0 = 160 GPIO0_A1 = 161 GPIO0_A2 = 162 GPIO0_A3 = 163 GPIO0_A4 = 164 GPIO0_A5 = 165 GPIO0_A6 = 166 GPIO0_A7 = 167 GPIO0_B0 = 168 GPIO0_B1 = 169 GPIO0_B2 = 170 GPIO0_B3 = 171 GPIO0_B4 = 172 GPIO0_B5 = 173 GPIO0_B6 = 174 GPIO0_B7 = 175 GPIO0_C0 = 176 GPIO0_C1 = 177 GPIO0_C2 = 178 GPIO0_C3 = 179 GPIO0_C4 = 180 GPIO0_C5 = 181 GPIO0_C6 = 182 GPIO0_C7 = 183 GPIO0_D0 = 184 GPIO0_D1 = 185 GPIO0_D2 = 186 GPIO0_D3 = 187 GPIO0_D4 = 188 GPIO0_D5 = 189 GPIO0_D6 = 190 GPIO0_D7 = 191 GPIO1_A0 = 192 GPIO1_A1 = 193 GPIO1_A2 = 194 GPIO1_A3 = 195 GPIO1_A4 = 196 GPIO1_A5 = 197 GPIO1_A6 = 198 GPIO1_A7 = 199 GPIO1_B0 = 200 GPIO1_B1 = 201 GPIO1_B2 = 202 GPIO1_B3 = 203 GPIO1_B4 = 204 GPIO1_B5 = 205 GPIO1_B6 = 206 GPIO1_B7 = 207 GPIO1_C0 = 208 GPIO1_C1 = 209 GPIO1_C2 = 210 GPIO1_C3 = 211 GPIO1_C4 = 212 GPIO1_C5 = 213 GPIO1_C6 = 214 GPIO1_C7 = 215 GPIO1_D0 = 216 GPIO1_D1 = 217 GPIO1_D2 = 218 GPIO1_D3 = 219 GPIO1_D4 = 220 GPIO1_D5 = 221 GPIO1_D6 = 222 GPIO1_D7 = 223 GPIO2_A0 = 224 GPIO2_A1 = 225 GPIO2_A2 = 226 GPIO2_A3 = 227 GPIO2_A4 = 228 GPIO2_A5 = 229 GPIO2_A6 = 230 GPIO2_A7 = 231 GPIO2_B0 = 232 GPIO2_B1 = 233 GPIO2_B2 = 234 GPIO2_B3 = 235 GPIO2_B4 = 236 GPIO2_B5 = 237 GPIO2_B6 = 238 GPIO2_B7 = 239 GPIO2_C0 = 240 GPIO2_C1 = 241 GPIO2_C2 = 242 GPIO2_C3 = 243 GPIO2_C4 = 244 GPIO2_C5 = 245 GPIO2_C6 = 246 GPIO2_C7 = 247 GPIO2_D0 = 248 GPIO2_D1 = 249 GPIO2_D2 = 250 GPIO2_D3 = 251 GPIO2_D4 = 252 GPIO2_D5 = 253 GPIO2_D6 = 254 GPIO2_D7 = 255 GPIO3_A0 = 256 GPIO3_A1 = 257 GPIO3_A2 = 258 GPIO3_A3 = 259 GPIO3_A4 = 260 GPIO3_A5 = 261 GPIO3_A6 = 262 GPIO3_A7 = 263 GPIO3_B0 = 264 GPIO3_B1 = 265 GPIO3_B2 = 266 GPIO3_B3 = 267 GPIO3_B4 = 268 GPIO3_B5 = 269 GPIO3_B6 = 270 GPIO3_B7 = 271 GPIO3_C0 = 272 GPIO3_C1 = 273 GPIO3_C2 = 274 GPIO3_C3 = 275 GPIO3_C4 = 276 GPIO3_C5 = 277 GPIO3_C6 = 278 GPIO3_C7 = 279 GPIO3_D0 = 280 GPIO3_D1 = 281 GPIO3_D2 = 282 GPIO3_D3 = 283 GPIO3_D4 = 284 GPIO3_D5 = 285 GPIO3_D6 = 286 GPIO3_D7 = 287
Kernel 3.10 and later
GPIO0_A0 = 0 GPIO0_A1 = 1 GPIO0_A2 = 2 GPIO0_A3 = 3 GPIO0_A4 = 4 GPIO0_A5 = 5 GPIO0_A6 = 6 GPIO0_A7 = 7 GPIO0_B0 = 8 GPIO0_B1 = 9 GPIO0_B2 = 10 GPIO0_B3 = 11 GPIO0_B4 = 12 GPIO0_B5 = 13 GPIO0_B6 = 14 GPIO0_B7 = 15 GPIO0_C0 = 16 GPIO0_C1 = 17 GPIO0_C2 = 18 GPIO0_C3 = 19 GPIO0_C4 = 20 GPIO0_C5 = 21 GPIO0_C6 = 22 GPIO0_C7 = 23 GPIO0_D0 = 24 GPIO0_D1 = 25 GPIO0_D2 = 26 GPIO0_D3 = 27 GPIO0_D4 = 28 GPIO0_D5 = 29 GPIO0_D6 = 30 GPIO0_D7 = 31 GPIO1_A0 = 32 GPIO1_A1 = 33 GPIO1_A2 = 34 GPIO1_A3 = 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 = 114 GPIO3_C3 = 115 GPIO3_C4 = 116 GPIO3_C5 = 117 GPIO3_C6 = 118 GPIO3_C7 = 119 GPIO3_D0 = 120 GPIO3_D1 = 121 GPIO3_D2 = 122 GPIO3_D3 = 123 GPIO3_D4 = 124 GPIO3_D5 = 125 GPIO3_D6 = 126 GPIO3_D7 = 127