all repos — adxl-firmware.git @ 20c298a5db8b262f905fd637b693eef014c6d742

Unnamed repository; edit this file 'description' to name the repository.

first
Gio e@mail
Thu, 03 Sep 2026 15:51:54 -0400
commit

20c298a5db8b262f905fd637b693eef014c6d742

3 files changed, 241 insertions(+), 0 deletions(-)

jump to
A CMakeLists.txt

@@ -0,0 +1,29 @@

+cmake_minimum_required(VERSION 4.2.2) + +# Inlcude the pi pico C/C++ SDK +include(pico_sdk_import.cmake) + +project(hello_world C CXX) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +# Initialize the pi pico C/C++ SDK +pico_sdk_init() + +add_executable(${PROJECT_NAME} + + main.c + +) + +# Link to pico standard library +target_link_libraries(${PROJECT_NAME} + + pico_stdlib + hardware_spi + +) + +pico_add_extra_outputs(${PROJECT_NAME}) + +pico_enable_stdio_usb(${PROJECT_NAME} 1) +pico_enable_stdio_uart(${PROJECT_NAME} 0)
A main.c

@@ -0,0 +1,139 @@

+ +#include <hardware/structs/io_bank0.h> +#include <pico/time.h> +#include <stdint.h> +#include <stdio.h> +#include "pico/stdlib.h" +#include "hardware/gpio.h" +#include "hardware/spi.h" + +#define SPI_PORT spi1 + +#define MOSI 11 +#define MISO 8 +#define CS 9 +#define CLK 10 + +#define READ_BIT 0x80 +#define WRITE_BIT 0x00 +#define MB_BIT 0x40 + +#define DATAX0 0x32 + +#define POWERCTL 0x2D +#define MEASURE_BIT 0x08 + +#define DATA_FORMAT 0x30 +#define ADXL345_RANGE_PM_2G 0x00 +#define ADXL345_RANGE_PM_4G 0x01 +#define ADXL345_RANGE_PM_8G 0x02 +#define ADXL345_RANGE_PM_16G 0x03 +#define ADXL345_LEFT_JUSTIFY 0x04 +#define ADXL345_SCALE_FACTOR 0.0039 + +struct Config { + uint scale; + bool full_resolution; +}; + +static inline void cs_select() { + asm volatile("nop \n nop \n nop"); + gpio_put(CS, 0); // Active low + asm volatile("nop \n nop \n nop"); +} + +static inline void cs_deselect() { + asm volatile("nop \n nop \n nop"); + gpio_put(CS, 1); + asm volatile("nop \n nop \n nop"); +} + +void read_registers(uint8_t reg_address, uint8_t *buf, uint16_t len) { + reg_address = READ_BIT | reg_address; + + cs_select(); + spi_write_blocking(SPI_PORT, &reg_address, 1); + spi_read_blocking(SPI_PORT, 0, buf, len); + cs_deselect(); +} + +void write_register(uint8_t reg_address, uint8_t byte) { + uint8_t buf[2] = {WRITE_BIT | reg_address,byte}; + + cs_select(); + spi_write_blocking(SPI_PORT, buf, 2); + cs_deselect(); +} + +void read_acceleration(uint16_t accel[3]) { + uint8_t reg_address = MB_BIT | DATAX0; + uint8_t buffer[6]; + + read_registers(reg_address, buffer, 6); + + for (int i = 0; i < 3; i++) { + accel[i] = (uint16_t)(buffer[(i * 2) + 1] << 8) + buffer[i * 2]; + } +} + +void acceleration_in_g(uint16_t accel[3], float accel_unit_g[3], struct Config conf) { + uint bit_mask = 0x3FF; + uint msb = 0x200; + + if (conf.full_resolution == true) { + switch (conf.scale) { + case 16: + bit_mask += 0x1C00; + msb <<= 3; + case 8: + bit_mask += 0xC00; + msb <<= 2; + case 4: + bit_mask += 0x400; + msb <<= 1; + } + } + + for (int i = 0; i < 3; i++) { + if ( (accel[i] & msb) != 0 ) { + accel_unit_g[i] = (float)( (accel[i] ^ 0xffff) + 1 ) * ADXL345_SCALE_FACTOR; + accel_unit_g[i] *= -1; + } else { + accel_unit_g[i] = (float)( accel[i] ) * ADXL345_SCALE_FACTOR; + } + } +} + +int main() { + stdio_init_all(); + + spi_init(SPI_PORT, 2000*1000); + spi_set_format(SPI_PORT, 8, SPI_CPOL_1, SPI_CPHA_1, SPI_MSB_FIRST); + + gpio_set_function(MOSI, GPIO_FUNC_SPI); + gpio_set_function(MISO, GPIO_FUNC_SPI); + gpio_set_function(CLK, GPIO_FUNC_SPI); + + gpio_init(CS); + gpio_set_dir(CS, GPIO_OUT); + gpio_put(CS, 1); + + uint16_t accel[3] = {0x67, 0x67, 0x67}; + float float_accel[3]; + + uint8_t id; + read_registers(0x00, &id, 1); + write_register(POWERCTL, MEASURE_BIT); + + struct Config config = {.scale = 16, .full_resolution = false}; + write_register(DATA_FORMAT, ADXL345_RANGE_PM_16G); + + while (true) { + read_acceleration(accel); + acceleration_in_g(accel, float_accel, config); + printf("%f %f %f\n", float_accel[0],float_accel[1],float_accel[2]); + //printf("%x %x %x\n\n", accel[0],accel[1],accel[2]); + sleep_ms(100); + } +} +
A pico_sdk_import.cmake

@@ -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})