Difference between revisions of "Rockpi4/android-mraa"
< Rockpi4
(9 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
<br> | <br> | ||
− | + | Mraa Java Lib is packaged in aar format and can be used in AndroidStudio: [http://rock.sh/radxa-android-mraa aar] | |
<br> | <br> | ||
+ | |||
+ | ==== Default IO Map ==== | ||
+ | |||
+ | Enable:Uart2(consloe),Uart4,SPI2,I2C2,I2C7,PWM0,PWM1 | ||
+ | |||
+ | Disable:I2C6,SPI1 | ||
+ | |||
+ | {| class="wikitable" | ||
+ | ! GPIO number || Function2 || Function1 || GPIO || Pin# || || Pin# || GPIO || Function1 || Function2 || GPIO number | ||
+ | |- | ||
+ | | || || +3.3V || || style="background:yellow"| 1 || ||style="background:red"| 2 || || +5.0V || || | ||
+ | |- | ||
+ | | 71 || || I2C7_SDA || GPIO2_A7(X) || style="background:green; color:white"| 3 || || style="background:red"| 4 || || +5.0V || || | ||
+ | |- | ||
+ | | 72 || || I2C7_SCL || GPIO2_B0(X) || style="background:green; color:white"| 5 || || style="background:black; color:white"| 6 || || GND || || | ||
+ | |- | ||
+ | | 75 || || SPI2_CLK || GPIO2_B3(X) || style="background:green; color:white"| 7 || || style="background:green; color:white"| 8 || GPIO4_C4(X) || style="background:orange"|UART2_TXD || || 148 | ||
+ | |- | ||
+ | | || || GND || || style="background:black; color:white"| 9 || || style="background:green; color:white"|10 || GPIO4_C3(X) || style="background:orange"|UART2_RXD || || 147 | ||
+ | |- | ||
+ | |146 || || PWM0 || GPIO4_C2(X) || style="background:green; color:white"|11 || || style="background:green; color:white"|12 || GPIO4_A3 || I2S1_SCLK || || 131 | ||
+ | |- | ||
+ | | 150 || || PWM1 || GPIO4_C6(X) || style="background:green; color:white"|13 || || style="background:black; color:white"|14 || || GND || || | ||
+ | |- | ||
+ | | 149 || || SPDIF_TX || GPIO4_C5 || style="background:green; color:white"|15 || || style="background:green; color:white"|16 || GPIO4_D2 || || || 154 | ||
+ | |- | ||
+ | | || || +3.3V || || style="background:yellow"|17 || || style="background:green; color:white"|18 || GPIO4_D4 || || || 156 | ||
+ | |- | ||
+ | |40 || UART4_TXD || SPI1_TXD(X) || GPIO1_B0(X) || style="background:green; color:white"|19 || ||style="background:black; color:white"|20 || || GND || || | ||
+ | |- | ||
+ | |39 || UART4_RXD || SPI1_RXD(X) || GPIO1_A7(X) || style="background:green; color:white"|21 || || style="background:green; color:white"|22 || GPIO4_D5 || || || 157 | ||
+ | |- | ||
+ | |41 || || SPI1_CLK(X) || GPIO1_B1 || style="background:green; color:white"|23 || || style="background:green; color:white"|24 || GPIO1_B2 || SPI1_CSn(X) || || 42 | ||
+ | |- | ||
+ | | || || GND || || style="background:black; color:white"|25 || || style="background:green; color:white"|26 || || ADC_IN0 || || | ||
+ | |- | ||
+ | | 64 || || I2C2_SDA || GPIO2_A0(X) || style="background:blue; color:white"|27 || || style="background:blue; color:white"|28 || GPIO2_A1(X) || I2C2_CLK || || 65 | ||
+ | |- | ||
+ | | 74 || I2C6_SCL(X) || SPI2_TXD || GPIO2_B2(X) || style="background:green; color:white"|29 || || style="background:black; color:white"|30 || || GND || || | ||
+ | |- | ||
+ | | 73 || I2C6_SDA(X) || SPI2_RXD || GPIO2_B1(X) || style="background:green; color:white"|31 || || style="background:green; color:white"|32 || GPIO3_C0 || SPDIF_TX || UART3_CTSn || 112 | ||
+ | |- | ||
+ | | 76 || || SPI2_CSn || GPIO2_B4(X) || style="background:green; color:white"|33 || || style="background:black; color:white"|34 || || GND || || | ||
+ | |- | ||
+ | | 133 || || I2S1_LRCK_TX || GPIO4_A5 || style="background:green; color:white"|35 || || style="background:green; color:white"|36 || GPIO4_A4 || I2S1_LRCK_RX || || 132 | ||
+ | |- | ||
+ | | 158 || || || GPIO4_D6 || style="background:green; color:white"|37 || || style="background:green; color:white"|38 || GPIO4_A6 || I2S1_SDI || || 134 | ||
+ | |- | ||
+ | | || || GND || || style="background:black; color:white"|39 || ||style="background:green; color:white"|40 || GPIO4_A7 || I2S1_SDO || || 135 | ||
+ | |} | ||
==== Class ==== | ==== Class ==== | ||
− | {| class="wikitable" | + | {| class="wikitable" style="width:70%;" |
|- | |- | ||
− | + | ! scope="col" width="10%" height="60px" style="text-align: left;" | class | |
− | + | ! scope="col" width="20%" style="text-align: left;" | constructor | |
− | + | ! scope="col" width="20%" style="text-align: left;" | constructor2 | |
| width="2px" | | | width="2px" | | ||
− | + | ! scope="col" width="10%" style="text-align: left;" | class | |
− | + | ! scope="col" width="20%" style="text-align: left;" | constructor | |
− | + | ! scope="col" width="20%" style="text-align: left;" | constructor2 | |
|- | |- | ||
− | | Aio || Aio (int pin_index) || NA || || Gpio || Gpio(int pin_index) || NA | + | | height="45px" | Aio || Aio (int pin_index) || NA || || Gpio || Gpio(int pin_index) || NA |
|- | |- | ||
− | | I2c || I2c (int i2c_index) || NA || || Pwm || Pwm (int pin_index) || NA | + | | height="45px" | I2c || I2c (int i2c_index) || NA || || Pwm || Pwm (int pin_index) || NA |
|- | |- | ||
− | | Spi || Spi (int spi_index) || NA || || Uart || Uart(int uart_index) || NA | + | | height="45px" | Spi || Spi (int spi_index) || NA || || Uart || Uart(int uart_index) || NA |
|} | |} | ||
==== Index Class ==== | ==== Index Class ==== | ||
− | {| class="wikitable" | + | {| class="wikitable" style="width:70%;" |
|- | |- | ||
− | + | ! scope="col" rowspan="5" width="10%" | RockPI4I2C | |
− | + | ! scope="col" width="10%" height="60px" style="text-align: left;" | Field | |
− | + | ! scope="col" width="10%" style="text-align: left;" | Index Value | |
− | + | ! scope="col" width="20%" style="text-align: left;" | Hardware I2c Number | |
− | + | ! scope="col" rowspan="5" width="10%" | RockPI4SPI | |
− | + | ! scope="col" width="10%" style="text-align: left;" | Field | |
− | + | ! scope="col" width="10%" style="text-align: left;" | Index Value | |
− | + | ! scope="col" width="20%" style="text-align: left;" | Hardware Spi Number | |
|- | |- | ||
− | | ROCK_PI_4_I2C2 || | + | | height="45px" | ROCK_PI_4_I2C2 || 1 || i2c2 || ROCK_PI_4_SPI1 || 1 || spi1 |
|- | |- | ||
− | | ROCK_PI_4_I2C6 || | + | | height="45px" | ROCK_PI_4_I2C6 || 2 || i2c6 || ROCK_PI_4_SPI2 || 0 || spi2 |
|- | |- | ||
− | | ROCK_PI_4_I2C7 || | + | | height="45px" | ROCK_PI_4_I2C7 || 0 || i2c7 || || || |
|} | |} | ||
− | {| class="wikitable" | + | {| class="wikitable" style="width:35%;" |
|- | |- | ||
− | + | ! scope="col" rowspan="5" width="20%" | RockPI4Uart | |
− | | width=" | + | | width="20%" height="60px" | Field |
− | | width=" | + | | width="20%" | Index Value |
− | | width=" | + | | width="40%" | Hardware Uart Number |
|- | |- | ||
− | | ROCK_PI_4_UART2 || 0 || uart2 | + | | height="45px" | ROCK_PI_4_UART2 || 0 || uart2 |
|- | |- | ||
− | | ROCK_PI_4_UART4 || 1 || uart4 | + | | height="45px" | ROCK_PI_4_UART4 || 1 || uart4 |
|} | |} | ||
==== Gpio ==== | ==== Gpio ==== | ||
− | {| class="wikitable" | + | {| class="wikitable" style="width:100%;" |
|- | |- | ||
− | + | ! scope="col" width="30%" height="60px" style="text-align: left;" | Methods | |
− | + | ! scope="col" width="15%" style="text-align: left;" | Parameter | |
− | + | ! scope="col" width="40%" style="text-align: left;" | Description | |
− | + | ! scope="col" width="15%" style="text-align: left;" | Return | |
|- | |- | ||
− | | dir(Dir dir) || Dir || Set input/output || Result | + | | height="45px" | dir(Dir dir) || Dir || Set input/output || Result |
|- | |- | ||
− | | readDir() || void || Read input/output dir || Dir | + | | height="45px" | readDir() || void || Read input/output dir || Dir |
|- | |- | ||
− | | mode(Mode mode) || Mode || Set STRONG/PULLUP/PULLDOWN/HIZ || Result | + | | height="45px" | mode(Mode mode) || Mode || Set STRONG/PULLUP/PULLDOWN/HIZ || Result |
|- | |- | ||
− | | read() || void || Set input and read gpio value || 0/1 | + | | height="45px" | read() || void || Set input and read gpio value || 0/1 |
|- | |- | ||
− | | write(int v) || 0/1 || Set output value || Result | + | | height="45px" | write(int v) || 0/1 || Set output value || Result |
|- | |- | ||
− | | unexport() || void || Unexport gpio in /dev/class/gpio || Result | + | | height="45px" | unexport() || void || Unexport gpio in /dev/class/gpio || Result |
|} | |} | ||
− | + | ==== Aio ==== | |
− | + | {| class="wikitable" style="width:100%;" | |
− | + | |- | |
− | + | ! scope="col" width="30%" height="60px" style="text-align: left;" | Methods | |
− | + | ! scope="col" width="15%" style="text-align: left;" | Parameter | |
− | + | ! scope="col" width="40%" style="text-align: left;" | Description | |
− | + | ! scope="col" width="15%" style="text-align: left;" | Return | |
− | + | |- | |
− | + | | height="45px" | read() || void || Read value. Default will shift the raw value up/down to a 10 bit. || long | |
− | + | |- | |
− | + | | height="45px" | readFloat() || void || Read value and return it as a normalized float. || 0.0 - 1.0 | |
− | + | |- | |
+ | | height="45px" | setBit(int b) || int || Set the bit value which will shift the raw reading from the ADC to. || Result | ||
+ | |- | ||
+ | | height="45px" | getBit() || void || Gets the bit value mraa is shifting the analog read to. || int | ||
+ | |} | ||
==== I2C ==== | ==== I2C ==== | ||
− | {| class="wikitable" | + | {| class="wikitable" style="width:100%;" |
|- | |- | ||
− | + | ! scope="col" width="30%" height="60px" style="text-align: left;" | Methods | |
− | + | ! scope="col" width="15%" style="text-align: left;" | Parameter | |
− | + | ! scope="col" width="40%" style="text-align: left;" | Description | |
− | + | ! scope="col" width="15%" style="text-align: left;" | Return | |
|- | |- | ||
− | | address(short a) || 0x00-0xFF || Set i2c address || Result | + | | height="45px" | address(short a) || 0x00-0xFF || Set i2c address || Result |
|- | |- | ||
− | | readByte() || void || Read a byte data form i2c || short | + | | height="45px" | readByte() || void || Read a byte data form i2c || short |
|- | |- | ||
− | | writeByte(short b) || Mode || Write a byte data to i2c || Result | + | | height="45px" | writeByte(short b) || Mode || Write a byte data to i2c || Result |
|- | |- | ||
− | | read(byte[] buf) || byte[] || Read a byte[] data form i2c || read size | + | | height="45px" | read(byte[] buf) || byte[] || Read a byte[] data form i2c || read size |
|- | |- | ||
− | | write(byte[] buf) || byte[] || Write a byte[] data to i2c || Result | + | | height="45px" | write(byte[] buf) || byte[] || Write a byte[] data to i2c || Result |
|- | |- | ||
− | | readReg(short a) || 0x00-0xFF || Read a byte data form i2c addr || short | + | | height="45px" | readReg(short a) || 0x00-0xFF || Read a byte data form i2c addr || short |
|- | |- | ||
− | | writeReg(short a, short d) || 0x00-0xFF, 0x00-0xFF || Write a byte data to i2c addr || Result | + | | height="45px" | writeReg(short a, short d) || 0x00-0xFF, 0x00-0xFF || Write a byte data to i2c addr || Result |
|- | |- | ||
− | | readWordReg(short a) || 0x00-0xFF || Read a byte[2] data form i2c addr || int | + | | height="45px" | readWordReg(short a) || 0x00-0xFF || Read a byte[2] data form i2c addr || int |
|- | |- | ||
− | | writeWordReg(short a, int d) || 0x00-0xFF, 0x0000-0xFFFF || Write a byte[2] data to i2c addr || Result | + | | height="45px" | writeWordReg(short a, int d) || 0x00-0xFF, 0x0000-0xFFFF || Write a byte[2] data to i2c addr || Result |
|- | |- | ||
− | | readBytesReg(short a, byte[] b) || 0x00-0xFF, byte[] || Read a byte[] data form i2c addr || int | + | | height="45px" | readBytesReg(short a, byte[] b) || 0x00-0xFF, byte[] || Read a byte[] data form i2c addr || int |
|- | |- | ||
|} | |} | ||
==== Pwm ==== | ==== Pwm ==== | ||
− | {| class="wikitable" | + | {| class="wikitable" style="width:100%;" |
|- | |- | ||
− | + | ! scope="col" width="30%" height="60px" style="text-align: left;" | Methods | |
− | + | ! scope="col" width="15%" style="text-align: left;" | Parameter | |
− | + | ! scope="col" width="40%" style="text-align: left;" | Description | |
− | + | ! scope="col" width="15%" style="text-align: left;" | Return | |
|- | |- | ||
− | | period(float s) || 0.0001 - 2.147483 || Set pwm period || Result | + | | height="45px" | period(float s) || 0.0001 - 2.147483 || Set pwm period || Result |
|- | |- | ||
− | | period_ms(int m) || 1 - 2147 || Set pwm period || Result | + | | height="45px" | period_ms(int m) || 1 - 2147 || Set pwm period || Result |
|- | |- | ||
− | | period_us(int u) || 1 - 2147483 || Set pwm period || Result | + | | height="45px" | period_us(int u) || 1 - 2147483 || Set pwm period || Result |
|- | |- | ||
− | | pulsewidth(float s) || 0.0001 - 2.147483 || Set pwm duty || Result | + | | height="45px" | pulsewidth(float s) || 0.0001 - 2.147483 || Set pwm duty || Result |
|- | |- | ||
− | | pulsewidth_ms(int m) || 1 - 2147 || Set pwm duty || Result | + | | height="45px" | pulsewidth_ms(int m) || 1 - 2147 || Set pwm duty || Result |
|- | |- | ||
− | | pulsewidth_us(int u) || 1 - 2147483 || Set pwm duty || Result | + | | height="45px" | pulsewidth_us(int u) || 1 - 2147483 || Set pwm duty || Result |
|- | |- | ||
− | | max_period() || void || Get pwm max period || int(us) | + | | height="45px" | max_period() || void || Get pwm max period || int(us) |
|- | |- | ||
− | | min_period() || void || Get pwm min period || int(us) | + | | height="45px" | min_period() || void || Get pwm min period || int(us) |
|- | |- | ||
− | | read() || void || Get pwm period/duty || 0.0 - 1.0 | + | | height="45px" | read() || void || Get pwm period/duty || 0.0 - 1.0 |
|- | |- | ||
− | | write(float p) || 0.0 - 1.0 || Set pwm period/duty percentage || Result | + | | height="45px" | write(float p) || 0.0 - 1.0 || Set pwm period/duty percentage || Result |
|- | |- | ||
− | | enable(boolean e) || true/false || dis/enable pwm || Result | + | | height="45px" | enable(boolean e) || true/false || dis/enable pwm || Result |
|- | |- | ||
− | | unexport() || void || Unexport pwm in /dev/class/pwm || Result | + | | height="45px" | unexport() || void || Unexport pwm in /dev/class/pwm || Result |
|} | |} | ||
==== SPI ==== | ==== SPI ==== | ||
− | {| class="wikitable" | + | {| class="wikitable" style="width:100%;" |
|- | |- | ||
− | + | ! scope="col" width="30%" height="60px" style="text-align: left;" | Methods | |
− | + | ! scope="col" width="15%" style="text-align: left;" | Parameter | |
− | + | ! scope="col" width="40%" style="text-align: left;" | Description | |
− | + | ! scope="col" width="15%" style="text-align: left;" | Return | |
|- | |- | ||
− | | defaultConfig() ||void || Set mraa default config (mode0,lsb=0,bits=8) || Result | + | | height="45px" | defaultConfig() ||void || Set mraa default config (mode0,lsb=0,bits=8) || Result |
|- | |- | ||
− | | mode(Spi_Mode m) || void || Set spi mode || Result | + | | height="45px" | mode(Spi_Mode m) || void || Set spi mode || Result |
|- | |- | ||
− | | frequency(int f) || int || Set spi frequency max=48000000 || Result | + | | height="45px" | frequency(int f) || int || Set spi frequency max=48000000 || Result |
|- | |- | ||
− | | lsbmode(boolean l) || true/false || Set spi lsmode || Result | + | | height="45px" | lsbmode(boolean l) || true/false || Set spi lsmode || Result |
|- | |- | ||
− | | bitPerWord(long b) || 8/16 || Set spi bit_pre_word || Result | + | | height="45px" | bitPerWord(long b) || 8/16 || Set spi bit_pre_word || Result |
|- | |- | ||
− | | writeByte(short a) || 0x00-0xFF || Write a byte data to spi || int(recv data) | + | | height="45px" | writeByte(short a) || 0x00-0xFF || Write a byte data to spi || int(recv data) |
|- | |- | ||
− | | writeWord(int a) || 0x0000-0xFFFF || Write a byte[2] data to spi || int(recv data) | + | | height="45px" | writeWord(int a) || 0x0000-0xFFFF || Write a byte[2] data to spi || int(recv data) |
|- | |- | ||
− | | write(byte[] b) || byte[] || Write a byte[] data to spi || byte[](recv data) | + | | height="45px" | write(byte[] b) || byte[] || Write a byte[] data to spi || byte[](recv data) |
|- | |- | ||
|} | |} | ||
==== UART ==== | ==== UART ==== | ||
− | {| class="wikitable" | + | {| class="wikitable" style="width:100%;" |
|- | |- | ||
− | + | ! scope="col" width="30%" height="60px" style="text-align: left;" | Methods | |
− | + | ! scope="col" width="15%" style="text-align: left;" | Parameter | |
− | + | ! scope="col" width="40%" style="text-align: left;" | Description | |
− | + | ! scope="col" width="15%" style="text-align: left;" | Return | |
|- | |- | ||
− | | defaultConfig() ||void || Set mraa default config (9600 8N1, no echo or special character) || Result | + | | height="45px" | defaultConfig() ||void || Set mraa default config (9600 8N1, no echo or special character) || Result |
|- | |- | ||
− | | setBaudRate(long b) || long || Set uart baudrate max=150000000 || Result | + | | height="45px" | setBaudRate(long b) || long || Set uart baudrate max=150000000 || Result |
|- | |- | ||
− | | setMode(int bytesize, UartParity parity, int stopbits) || int || Set the transfer mode || Result | + | | height="45px" | setMode(int bytesize, UartParity parity, int stopbits) || int || Set the transfer mode || Result |
|- | |- | ||
− | | setFlowcontrol(boolean xonxoff, boolean rtscts) || true/false || Set the flowcontrol || Result | + | | height="45px" | setFlowcontrol(boolean xonxoff, boolean rtscts) || true/false || Set the flowcontrol || Result |
|- | |- | ||
− | | setTimeout(int read, int write, int interchar) || -1 - int_max || Set the timeout for read and write operations || Result | + | | height="45px" | setTimeout(int read, int write, int interchar) || -1 - int_max || Set the timeout for read and write operations || Result |
|- | |- | ||
− | | setNonBlocking(boolean | + | | height="45px" | setNonBlocking(boolean b) || true/false || Set the blocking state for write operations || Result |
|- | |- | ||
− | | sendBreak(int | + | | height="45px" | sendBreak(int b) || 0 - max_int || Send a break to the device. || Result |
|- | |- | ||
− | | flush() || void || Flush the outbound data. || Result | + | | height="45px" | flush() || void || Flush the outbound data. || Result |
|- | |- | ||
− | | dataAvailable() || void || Check to see if data is available on the device for reading, return immediately || boolean | + | | height="45px" | dataAvailable() || void || Check to see if data is available on the device for reading, return immediately || boolean |
|- | |- | ||
− | | dataAvailable(long | + | | height="45px" | dataAvailable(long timeout) || 1 - int_max || Check to see if data is available on the device for reading and time out || boolean |
|- | |- | ||
− | | readStr(int length) || 1 - int_max || Read bytes from the device into a String object || String | + | | height="45px" | readStr(int length) || 1 - int_max || Read bytes from the device into a String object || String |
|- | |- | ||
− | | writeStr(String s) || | + | | height="45px" | writeStr(String s) || String || Write bytes in String object to a device || int |
|- | |- | ||
− | | read(byte[] data) || | + | | height="45px" | read(byte[] data) || byte[] || Check to see if data is available on the device for reading and time out || int(read size) |
|- | |- | ||
− | | wrtie(byte[] data) || | + | | height="45px" | wrtie(byte[] data) || byte[] || Check to see if data is available on the device for reading and time out || int(write size) |
|- | |- | ||
|} | |} | ||
+ | |||
+ | ==== Example ==== | ||
+ | Gpio | ||
+ | <code java> | ||
+ | import mraa.*; | ||
+ | |||
+ | Gpio gpio40 = new Gpio(RockPI4.ROCK_PI_4_PIN40.swigValue()); | ||
+ | gpio40.dir(Dir.DIR_OUT); | ||
+ | gpio40.write(1); | ||
+ | // do sth | ||
+ | // if you want | ||
+ | gpio40.unexport(); | ||
+ | </code> | ||
+ | |||
+ | Spi | ||
+ | <code java> | ||
+ | import mraa.*; | ||
+ | |||
+ | Spi spi = new Spi(RockPI4SPI.ROCK_PI_4_SPI1.swigValue()); | ||
+ | byte[] recv = spi.write(new byte[]{0x11, 0x22}); | ||
+ | Log.d(TAG, String.format("onCreate: recv[0]=0x%x, recv[1]=0x%x", recv[0], recv[1])); | ||
+ | </code> | ||
+ | |||
+ | Pwm | ||
+ | <code java> | ||
+ | import mraa.*; | ||
+ | |||
+ | Pwm pwm = new Pwm(RockPI4.ROCK_PI_4_PIN11.swigValue()); | ||
+ | pwm.period_us(20000); | ||
+ | pwm.write((float) 0.5); | ||
+ | pwm.enable(true); | ||
+ | // do sth | ||
+ | ..... | ||
+ | |||
+ | pwm.enable(false); | ||
+ | pwm.unexport(); | ||
+ | </code> | ||
+ | |||
+ | Aio | ||
+ | <code java> | ||
+ | import mraa.*; | ||
+ | |||
+ | Aio aio = new Aio(0); | ||
+ | Log.d(TAG, "aio0: " + aio.read()); | ||
+ | </code> | ||
+ | |||
+ | I2c | ||
+ | <code java> | ||
+ | import mraa.*; | ||
+ | |||
+ | I2c i2c = new I2c(RockPI4I2C.ROCK_PI_4_I2C2.swigValue()); | ||
+ | i2c.address((short) 0x50); | ||
+ | i2c.writeReg((short)0x01, (short) 0x57); | ||
+ | try { | ||
+ | Thread.sleep(1000); | ||
+ | } catch (InterruptedException e) { | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | i2c.address((short) 0x50); | ||
+ | Log.d(TAG, "i2c2 read: 0x" + Integer.toHexString(i2c.readReg((short)0x01))); | ||
+ | </code> | ||
+ | |||
+ | Uart | ||
+ | <code java> | ||
+ | import mraa.*; | ||
+ | |||
+ | Uart uart = new Uart(RockPI4Uart.ROCK_PI_4_UART4.swigValue()); | ||
+ | uart.defaultConfig(); | ||
+ | uart.setBaudRate(115200); | ||
+ | uart.writeStr("radxa"); | ||
+ | String read = uart.readStr(6); | ||
+ | Log.d(TAG, "uart4 read: " + read); | ||
+ | </code> |
Latest revision as of 02:16, 21 March 2020
Contents
Rock PI 4 Android Mraa API
Mraa Java Lib is packaged in aar format and can be used in AndroidStudio: aar
Default IO Map
Enable:Uart2(consloe),Uart4,SPI2,I2C2,I2C7,PWM0,PWM1
Disable:I2C6,SPI1
GPIO number | Function2 | Function1 | GPIO | Pin# | Pin# | GPIO | Function1 | Function2 | GPIO number | |
---|---|---|---|---|---|---|---|---|---|---|
+3.3V | 1 | 2 | +5.0V | |||||||
71 | I2C7_SDA | GPIO2_A7(X) | 3 | 4 | +5.0V | |||||
72 | I2C7_SCL | GPIO2_B0(X) | 5 | 6 | GND | |||||
75 | SPI2_CLK | GPIO2_B3(X) | 7 | 8 | GPIO4_C4(X) | UART2_TXD | 148 | |||
GND | 9 | 10 | GPIO4_C3(X) | UART2_RXD | 147 | |||||
146 | PWM0 | GPIO4_C2(X) | 11 | 12 | GPIO4_A3 | I2S1_SCLK | 131 | |||
150 | PWM1 | GPIO4_C6(X) | 13 | 14 | GND | |||||
149 | SPDIF_TX | GPIO4_C5 | 15 | 16 | GPIO4_D2 | 154 | ||||
+3.3V | 17 | 18 | GPIO4_D4 | 156 | ||||||
40 | UART4_TXD | SPI1_TXD(X) | GPIO1_B0(X) | 19 | 20 | GND | ||||
39 | UART4_RXD | SPI1_RXD(X) | GPIO1_A7(X) | 21 | 22 | GPIO4_D5 | 157 | |||
41 | SPI1_CLK(X) | GPIO1_B1 | 23 | 24 | GPIO1_B2 | SPI1_CSn(X) | 42 | |||
GND | 25 | 26 | ADC_IN0 | |||||||
64 | I2C2_SDA | GPIO2_A0(X) | 27 | 28 | GPIO2_A1(X) | I2C2_CLK | 65 | |||
74 | I2C6_SCL(X) | SPI2_TXD | GPIO2_B2(X) | 29 | 30 | GND | ||||
73 | I2C6_SDA(X) | SPI2_RXD | GPIO2_B1(X) | 31 | 32 | GPIO3_C0 | SPDIF_TX | UART3_CTSn | 112 | |
76 | SPI2_CSn | GPIO2_B4(X) | 33 | 34 | GND | |||||
133 | I2S1_LRCK_TX | GPIO4_A5 | 35 | 36 | GPIO4_A4 | I2S1_LRCK_RX | 132 | |||
158 | GPIO4_D6 | 37 | 38 | GPIO4_A6 | I2S1_SDI | 134 | ||||
GND | 39 | 40 | GPIO4_A7 | I2S1_SDO | 135 |
Class
class | constructor | constructor2 | class | constructor | constructor2 | |
---|---|---|---|---|---|---|
Aio | Aio (int pin_index) | NA | Gpio | Gpio(int pin_index) | NA | |
I2c | I2c (int i2c_index) | NA | Pwm | Pwm (int pin_index) | NA | |
Spi | Spi (int spi_index) | NA | Uart | Uart(int uart_index) | NA |
Index Class
RockPI4I2C | Field | Index Value | Hardware I2c Number | RockPI4SPI | Field | Index Value | Hardware Spi Number |
---|---|---|---|---|---|---|---|
ROCK_PI_4_I2C2 | 1 | i2c2 | ROCK_PI_4_SPI1 | 1 | spi1 | ||
ROCK_PI_4_I2C6 | 2 | i2c6 | ROCK_PI_4_SPI2 | 0 | spi2 | ||
ROCK_PI_4_I2C7 | 0 | i2c7 |
RockPI4Uart | Field | Index Value | Hardware Uart Number |
---|---|---|---|
ROCK_PI_4_UART2 | 0 | uart2 | |
ROCK_PI_4_UART4 | 1 | uart4 |
Gpio
Methods | Parameter | Description | Return |
---|---|---|---|
dir(Dir dir) | Dir | Set input/output | Result |
readDir() | void | Read input/output dir | Dir |
mode(Mode mode) | Mode | Set STRONG/PULLUP/PULLDOWN/HIZ | Result |
read() | void | Set input and read gpio value | 0/1 |
write(int v) | 0/1 | Set output value | Result |
unexport() | void | Unexport gpio in /dev/class/gpio | Result |
Aio
Methods | Parameter | Description | Return |
---|---|---|---|
read() | void | Read value. Default will shift the raw value up/down to a 10 bit. | long |
readFloat() | void | Read value and return it as a normalized float. | 0.0 - 1.0 |
setBit(int b) | int | Set the bit value which will shift the raw reading from the ADC to. | Result |
getBit() | void | Gets the bit value mraa is shifting the analog read to. | int |
I2C
Methods | Parameter | Description | Return |
---|---|---|---|
address(short a) | 0x00-0xFF | Set i2c address | Result |
readByte() | void | Read a byte data form i2c | short |
writeByte(short b) | Mode | Write a byte data to i2c | Result |
read(byte[] buf) | byte[] | Read a byte[] data form i2c | read size |
write(byte[] buf) | byte[] | Write a byte[] data to i2c | Result |
readReg(short a) | 0x00-0xFF | Read a byte data form i2c addr | short |
writeReg(short a, short d) | 0x00-0xFF, 0x00-0xFF | Write a byte data to i2c addr | Result |
readWordReg(short a) | 0x00-0xFF | Read a byte[2] data form i2c addr | int |
writeWordReg(short a, int d) | 0x00-0xFF, 0x0000-0xFFFF | Write a byte[2] data to i2c addr | Result |
readBytesReg(short a, byte[] b) | 0x00-0xFF, byte[] | Read a byte[] data form i2c addr | int |
Pwm
Methods | Parameter | Description | Return |
---|---|---|---|
period(float s) | 0.0001 - 2.147483 | Set pwm period | Result |
period_ms(int m) | 1 - 2147 | Set pwm period | Result |
period_us(int u) | 1 - 2147483 | Set pwm period | Result |
pulsewidth(float s) | 0.0001 - 2.147483 | Set pwm duty | Result |
pulsewidth_ms(int m) | 1 - 2147 | Set pwm duty | Result |
pulsewidth_us(int u) | 1 - 2147483 | Set pwm duty | Result |
max_period() | void | Get pwm max period | int(us) |
min_period() | void | Get pwm min period | int(us) |
read() | void | Get pwm period/duty | 0.0 - 1.0 |
write(float p) | 0.0 - 1.0 | Set pwm period/duty percentage | Result |
enable(boolean e) | true/false | dis/enable pwm | Result |
unexport() | void | Unexport pwm in /dev/class/pwm | Result |
SPI
Methods | Parameter | Description | Return |
---|---|---|---|
defaultConfig() | void | Set mraa default config (mode0,lsb=0,bits=8) | Result |
mode(Spi_Mode m) | void | Set spi mode | Result |
frequency(int f) | int | Set spi frequency max=48000000 | Result |
lsbmode(boolean l) | true/false | Set spi lsmode | Result |
bitPerWord(long b) | 8/16 | Set spi bit_pre_word | Result |
writeByte(short a) | 0x00-0xFF | Write a byte data to spi | int(recv data) |
writeWord(int a) | 0x0000-0xFFFF | Write a byte[2] data to spi | int(recv data) |
write(byte[] b) | byte[] | Write a byte[] data to spi | byte[](recv data) |
UART
Methods | Parameter | Description | Return |
---|---|---|---|
defaultConfig() | void | Set mraa default config (9600 8N1, no echo or special character) | Result |
setBaudRate(long b) | long | Set uart baudrate max=150000000 | Result |
setMode(int bytesize, UartParity parity, int stopbits) | int | Set the transfer mode | Result |
setFlowcontrol(boolean xonxoff, boolean rtscts) | true/false | Set the flowcontrol | Result |
setTimeout(int read, int write, int interchar) | -1 - int_max | Set the timeout for read and write operations | Result |
setNonBlocking(boolean b) | true/false | Set the blocking state for write operations | Result |
sendBreak(int b) | 0 - max_int | Send a break to the device. | Result |
flush() | void | Flush the outbound data. | Result |
dataAvailable() | void | Check to see if data is available on the device for reading, return immediately | boolean |
dataAvailable(long timeout) | 1 - int_max | Check to see if data is available on the device for reading and time out | boolean |
readStr(int length) | 1 - int_max | Read bytes from the device into a String object | String |
writeStr(String s) | String | Write bytes in String object to a device | int |
read(byte[] data) | byte[] | Check to see if data is available on the device for reading and time out | int(read size) |
wrtie(byte[] data) | byte[] | Check to see if data is available on the device for reading and time out | int(write size) |
Example
Gpio
import mraa.*; Gpio gpio40 = new Gpio(RockPI4.ROCK_PI_4_PIN40.swigValue()); gpio40.dir(Dir.DIR_OUT); gpio40.write(1); // do sth // if you want gpio40.unexport();
Spi
import mraa.*; Spi spi = new Spi(RockPI4SPI.ROCK_PI_4_SPI1.swigValue()); byte[] recv = spi.write(new byte[]{0x11, 0x22}); Log.d(TAG, String.format("onCreate: recv[0]=0x%x, recv[1]=0x%x", recv[0], recv[1]));
Pwm
import mraa.*; Pwm pwm = new Pwm(RockPI4.ROCK_PI_4_PIN11.swigValue()); pwm.period_us(20000); pwm.write((float) 0.5); pwm.enable(true); // do sth ..... pwm.enable(false); pwm.unexport();
Aio
import mraa.*; Aio aio = new Aio(0); Log.d(TAG, "aio0: " + aio.read());
I2c
import mraa.*; I2c i2c = new I2c(RockPI4I2C.ROCK_PI_4_I2C2.swigValue()); i2c.address((short) 0x50); i2c.writeReg((short)0x01, (short) 0x57); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } i2c.address((short) 0x50); Log.d(TAG, "i2c2 read: 0x" + Integer.toHexString(i2c.readReg((short)0x01)));
Uart
import mraa.*; Uart uart = new Uart(RockPI4Uart.ROCK_PI_4_UART4.swigValue()); uart.defaultConfig(); uart.setBaudRate(115200); uart.writeStr("radxa"); String read = uart.readStr(6); Log.d(TAG, "uart4 read: " + read);