Skip to content

Commit

Permalink
Added ADC support (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
orenskl authored Jan 25, 2024
1 parent 8471f44 commit c5a1219
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 38 deletions.
28 changes: 0 additions & 28 deletions .vscode/tasks.json

This file was deleted.

10 changes: 3 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ if (${TARGET} STREQUAL PICO)
include(pico_sdk_import.cmake)
endif()

project(pjvm VERSION 0.0.2)
project(pjvm VERSION 0.0.4)

include(CTest)

Expand Down Expand Up @@ -49,14 +49,10 @@ if (${TARGET} STREQUAL LINUX)
set(PJVM_SOURCES src/linux/Main.cpp src/linux/OS.cpp)
endif()

#add_library(libjvm STATIC IMPORTED)
#set_property(TARGET libjvm PROPERTY IMPORTED_LOCATION ${BINARY_DIR}/libjvm.a)
#add_dependencies(jvmprojlib jvmproj)

add_executable(pjvm ${PJVM_SOURCES})

if (${TARGET} STREQUAL PICO)
set(PJVM_INCLUDE_OS_DIRS lib/src/vm/os/pico)
set(PJVM_INCLUDE_OS_DIRS lib/src/vm/os/pico ${PICO_SDK_PATH}/src/rp2_common/hardware_adc/include)
endif()
if (${TARGET} STREQUAL LINUX)
set(PJVM_INCLUDE_OS_DIRS lib/src/vm/os/linux)
Expand Down Expand Up @@ -87,7 +83,7 @@ target_link_directories(pjvm PRIVATE ${CMAKE_BINARY_DIR})

if (${TARGET} STREQUAL PICO)
# Add pico_stdlib library which aggregates commonly used features
target_link_libraries(pjvm pico_stdlib)
target_link_libraries(pjvm pico_stdlib hardware_adc)
# create map/bin/hex/uf2 file in addition to ELF.
pico_add_extra_outputs(pjvm)
pico_enable_stdio_usb(pjvm 1)
Expand Down
56 changes: 56 additions & 0 deletions examples/adc/build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<!--
SPDX-License-Identifier: GPL-2.0-or-later
Copyright (C) 2024 Oren Sokoler (https://github.com/orenskl)
-->

<project name="adc" basedir="." default="bin">

<property environment="env"/>
<fail unless="env.JAVA_PICO_HOME" message="Please set JAVA_PICO_HOME to the location of the pjvm base directory"/>
<property name="pjvm.dir" value="${env.JAVA_PICO_HOME}"/>

<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/>
<property name="main-class" value="Main"/>

<target name="clean">
<delete dir="${build.dir}"/>
</target>

<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac target="1.4" source="1.4" srcdir="${src.dir}" destdir="${classes.dir}"
bootclasspath="${pjvm.dir}/lib/classes.jar" includeantruntime="false"/>
</target>

<target name="preverify" depends="compile">
<echo message="Preverifying classes"/>
<exec executable="${pjvm.dir}/bin/preverify">
<arg value="-d"/>
<arg value="${classes.dir}.preverify"/>
<arg value="-classpath"/>
<arg value="${pjvm.dir}/lib/classes.jar"/>
<arg value="${classes.dir}"/>
</exec>
</target>

<target name="jar" depends="preverify">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}.preverify" compress="false" />
</target>

<target name="bin" depends="jar">
<echo message="Wrapping JAR"/>
<exec executable="${pjvm.dir}/bin/wrapjar.sh">
<arg value="${jar.dir}/${ant.project.name}.jar"/>
<arg value="${jar.dir}/${ant.project.name}.jar.bin"/>
</exec>
</target>

<target name="clean-build" depends="clean,bin"/>

</project>
29 changes: 29 additions & 0 deletions examples/adc/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
*
* SPDX-License-Identifier: GPL-2.0-or-later
* Copyright (C) 2024 Oren Sokoler (https://github.com/orenskl)
*
*/

import pico.hardware.ADCChannel;

class Main {

/* A simple ADC test */
private static final int ADC_CHANNEL = 2;

public static void main(String[] args) {
System.out.println("ADC example");
ADCChannel a = new ADCChannel(ADC_CHANNEL);
while (true) {
int value = a.read();
System.out.println("ADC Channel " + ADC_CHANNEL + " value is " + value);
try {
Thread.sleep(500);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}

}
5 changes: 4 additions & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ if (${TARGET} STREQUAL PICO)
set(CMAKE_CXX_COMPILER arm-none-eabi-g++)
endif()
if (${TARGET} STREQUAL PICO)
set(JVM_DEVICE_CLASSES ${CMAKE_SOURCE_DIR}/src/javaapi/device/pico/hardware/GPIOPin.java)
set(JVM_DEVICE_CLASSES ${CMAKE_SOURCE_DIR}/src/javaapi/device/pico/hardware/GPIOPin.java
${CMAKE_SOURCE_DIR}/src/javaapi/device/pico/hardware/ADCChannel.java
)
endif()

set(JVM_CLASSES
Expand Down Expand Up @@ -135,6 +137,7 @@ set(JVM_CLASSES

add_custom_command(OUTPUT ROMImage.cpp
DEPENDS ${JVM_CLASSES} ${BINARY_DIR}/romgen
COMMAND rm ARGS -rf ${CMAKE_BINARY_DIR}/classes ${CMAKE_BINARY_DIR}/classes.preverify
COMMAND mkdir ARGS -p ${CMAKE_BINARY_DIR}/classes
COMMAND javac ARGS -source 1.4 -target 1.4 -d ${CMAKE_BINARY_DIR}/classes -bootclasspath ${CMAKE_BINARY_DIR}/classes ${JVM_CLASSES}
COMMAND ${CMAKE_SOURCE_DIR}/tools/preverify/bin/preverify ARGS -d ${CMAKE_BINARY_DIR}/classes.preverify ${CMAKE_BINARY_DIR}/classes
Expand Down
53 changes: 53 additions & 0 deletions lib/src/javaapi/device/pico/hardware/ADCChannel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
*
* SPDX-License-Identifier: GPL-2.0-or-later
* Copyright (C) 2024 Oren Sokoler (https://github.com/orenskl)
*
*/

package pico.hardware;

/**
* A <code>ADCChannel</code> class represents a single Analog To Digital
* conversion channel, the channel number is hardware specific and is not
* decoded or encoded in anyway, it is passed to the hardware level as is.
* The values that are read from the ADC are integers and are the direct
* read of the ADC, e.g. if the ADC is a 12 bit ADC then the values you
* could get are from 0 to 4095.
*/

public class ADCChannel {

/**
* The ADC channel number
*/
private int channel;

/**
*
* Constructor for a single ADC channel
*
* @param channel The channel number
*
*/
public ADCChannel ( int channel ) {
this.channel = channel;
adc_init(channel);
}

/**
* Perform a single conversion and read its value from the ADC channel
*
* @return the ADC channel digital value
*/
public int read () {
return adc_read(this.channel);
}

/*
* Natives for the device hardware
*/
private static native void adc_init ( int channel );
private static native int adc_read ( int channel );

}
3 changes: 3 additions & 0 deletions src/pico/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "sni.h"

#include "pico/stdlib.h"
#include "hardware/adc.h"

#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -87,6 +88,8 @@ int main( void )
stdio_usb_init();
/* We have to wait for the USB stack */
sleep_ms(3000);
/* Initialize the ADC hardware */
adc_init();

// Call this before any other Jvm_ functions.
JVM_Initialize();
Expand Down
34 changes: 32 additions & 2 deletions src/pico/Natives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@
#include "kni.h"

#include "hardware/gpio.h"
#include "hardware/adc.h"

#define ADC_GPIO_PIN_MIN 26
#define ADC_GPIO_PIN_MAX 29

extern "C" {

/*
* This file is the native implementation of the device specific
* classes that access the hardware, these functions are called from
* the Java classes. All the functions are void but they get thei
* the Java classes. All the functions are void but they get their
* input parameters from the JVM stack via KNI calls
*/

Expand Down Expand Up @@ -80,4 +84,30 @@ int Java_pico_hardware_GPIOPin_gpio_1get( void )
return gpio_get(pinNumber);
}

}
/**
* Initialize an ADC channel
@ *
* @param The 1st parameter is the channel number
*
*/
void Java_pico_hardware_ADCChannel_adc_1init ( void )
{
int channel = KNI_GetParameterAsInt(1);
adc_gpio_init(ADC_GPIO_PIN_MIN + channel);
}

/**
* Read a single sample from the ADC channel
*
* @param The 1st parameter is the channel number
* @return The sample read as an integer
*
*/
int Java_pico_hardware_ADCChannel_adc_1read ( void )
{
int channel = KNI_GetParameterAsInt(1);
adc_select_input(channel);
return adc_read();
}

} /* extern "C" */

0 comments on commit c5a1219

Please sign in to comment.