Skip to content

Commit

Permalink
pico example
Browse files Browse the repository at this point in the history
  • Loading branch information
Grinkers committed Sep 2, 2024
1 parent 18533c4 commit 1302510
Show file tree
Hide file tree
Showing 10 changed files with 238 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@
## floats

Pulls in the dependency `ordered-float` for Edn::Double. Without this feature, parsing floating-point numbers will result in an Err.

# no_std

See `examples/pico` for a minimalistic example of using this crate with the raspberry pi pico (rp2040)
3 changes: 3 additions & 0 deletions examples/pico/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/build
/pico-edn/target
/.cache
35 changes: 35 additions & 0 deletions examples/pico/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
cmake_minimum_required(VERSION 3.13)
set(CMAKE_C_STANDARD 11)

# initialize the SDK based on PICO_SDK_PATH
# note: this must happen before project()
include(pico_sdk_import.cmake)

project(hello_edn)

# initialize the Raspberry Pi Pico SDK
pico_sdk_init()

add_executable(hello_edn
hello_edn.c
)

add_custom_target(
rustlib
COMMAND cd ${CMAKE_SOURCE_DIR}/pico-edn && cargo build --release --target thumbv6m-none-eabi
)
add_dependencies(hello_edn rustlib)

# Pull in our pico_stdlib which aggregates commonly used features
target_link_libraries(
hello_edn
pico_stdlib hardware_adc
${CMAKE_SOURCE_DIR}/pico-edn/target/thumbv6m-none-eabi/release/libpicoedn.a
)

# enable usb output, disable uart output
pico_enable_stdio_usb(hello_edn 1)
pico_enable_stdio_uart(hello_edn 0)

# create map/bin/hex file etc.
pico_add_extra_outputs(hello_edn)
8 changes: 8 additions & 0 deletions examples/pico/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Pico

## Build instructions

1. Set up [`The C/C++ SDK`](https://github.com/edn-format/edn)
2. mkdir build && cd build
3. cmake .. -DPICO_SDK_PATH=~/tools/pico-sdk
4. make
43 changes: 43 additions & 0 deletions examples/pico/hello_edn.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

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

void some_edn(char* edn);

int main() {
stdio_init_all();
const uint LED_PIN = 25;
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);

adc_init();
// Make sure GPIO is high-impedance, no pullups etc
adc_gpio_init(26);
// Select ADC input 0 (GPIO26)
adc_select_input(4);
adc_set_temp_sensor_enabled(true);

char buf[200];
while (1) {
sleep_ms(500);
gpio_put(LED_PIN, 1);

// 12-bit conversion, assume max value == ADC_VREF == 3.3 V
const float conversion_factor = 3.3f / (1 << 12);
uint16_t result = adc_read();
printf("Raw value: 0x%03x, voltage: %f V\n", result, result * conversion_factor);

float temperature = 27 - (((result * conversion_factor) - 0.706) / 0.001721);
printf("Internal Temperature: %.2f degrees Celsius\n", temperature);

sprintf(buf, "{:temp %.2f :foo #{1 2 3 42}}", temperature);
some_edn(buf);

sleep_ms(500);
gpio_put(LED_PIN, 0);
}
}
2 changes: 2 additions & 0 deletions examples/pico/pico-edn/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.thumbv6m-none-eabi]
rustflags = [ "--cfg", "portable_atomic_unsafe_assume_single_core" ]
1 change: 1 addition & 0 deletions examples/pico/pico-edn/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
26 changes: 26 additions & 0 deletions examples/pico/pico-edn/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "pico-edn"
version = "0.1.0"
edition = "2021"

[lib]
name = "picoedn"
crate-type = ["staticlib"]

[lints.rust]
rust_2018_idioms = "warn"
future-incompatible = "warn"

[lints.clippy]
pedantic = "warn"
nursery = "warn"

[dependencies]
clojure-reader = { version = "*", default-features = false, features = ["floats"] }
emballoc = { version = "*", features = ["portable_atomic"] }

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"
43 changes: 43 additions & 0 deletions examples/pico/pico-edn/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#![no_main]
#![allow(unsafe_code)]
#![no_std]

extern crate alloc;

use alloc::ffi::CString;
use alloc::format;
use core::ffi::CStr;
use core::panic::PanicInfo;

use clojure_reader::edn;

#[panic_handler]
fn panic(_panic: &PanicInfo<'_>) -> ! {
loop {
unsafe {
printf("panic\n".as_ptr() as *const i8);
sleep_ms(500);
}
}
}

extern "C" {
fn printf(format: *const i8, ...) -> i32;
fn sleep_ms(ms: u32);
}

#[global_allocator]
static ALLOCATOR: emballoc::Allocator<4096> = emballoc::Allocator::new();

#[no_mangle]
pub extern "C" fn some_edn(edn: *const i8) {
let c_str: &CStr = unsafe { CStr::from_ptr(edn) };
let str_slice: &str = c_str.to_str().unwrap();

let edn = edn::read_string(str_slice).unwrap();
let edn_str = format!("{edn}");
let c_str = CString::new(edn_str.as_str()).unwrap();
unsafe {
printf("hello edn %s\n\0".as_ptr() as *const i8, c_str.as_ptr());
}
}
73 changes: 73 additions & 0 deletions examples/pico/pico_sdk_import.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# This is a copy of <PICO_SDK_PATH>/external/pico_sdk_import.cmake

# This can be dropped into an external project to help locate this SDK
# It should be include()ed prior to project()

if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH))
set(PICO_SDK_PATH $ENV{PICO_SDK_PATH})
message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')")
endif ()

if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT))
set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT})
message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')")
endif ()

if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH))
set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH})
message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')")
endif ()

set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK")
set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable")
set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK")

if (NOT PICO_SDK_PATH)
if (PICO_SDK_FETCH_FROM_GIT)
include(FetchContent)
set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR})
if (PICO_SDK_FETCH_FROM_GIT_PATH)
get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}")
endif ()
# GIT_SUBMODULES_RECURSE was added in 3.17
if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.17.0")
FetchContent_Declare(
pico_sdk
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
GIT_TAG master
GIT_SUBMODULES_RECURSE FALSE
)
else ()
FetchContent_Declare(
pico_sdk
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
GIT_TAG master
)
endif ()

if (NOT pico_sdk)
message("Downloading Raspberry Pi Pico SDK")
FetchContent_Populate(pico_sdk)
set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR})
endif ()
set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE})
else ()
message(FATAL_ERROR
"SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git."
)
endif ()
endif ()

get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
if (NOT EXISTS ${PICO_SDK_PATH})
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found")
endif ()

set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake)
if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE})
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK")
endif ()

set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE)

include(${PICO_SDK_INIT_CMAKE_FILE})

0 comments on commit 1302510

Please sign in to comment.