Moved all software related files to Software folder; Now using flash safe execution functions of pico_flash (pico SDK) lib; Added error handling; Added RP2040-Decoder-board.h definition; Adapted CMakeLists.txt for use with vscode pi pico extension;

This commit is contained in:
gab-k committed 2024-12-22 11:39:03 +01:00
1 parent e8a8ac6d98
commit 7b1d5ed152
12 files changed
+1452 -1271

No files matched your search

-59
View File
@@ -1,59 +0,0 @@
cmake_minimum_required(VERSION 3.12)
# Pull in SDK (must be before project)
include(pico_sdk_import.cmake)
project(RP2040-Decoder C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
set(PICO_DECODER_PATH ${PROJECT_SOURCE_DIR})
# Stop the compiler from inling method calls making it easier to debug
set(PICO_DEOPTIMIZED_DEBUG 1)
# Initialize the SDK
pico_sdk_init()
add_compile_options(-Wall
-Wno-format # int != int32 _t as far as the compiler is concerned because gcc has int32_t as long int
-Wno-unused-function # we have some for the docs that aren't called
-Wno-maybe-uninitialized
)
add_executable(
RP2040-Decoder
core0.c
core0.h
core1.c
core1.h
shared.c
shared.h)
# Multiplier to lengthen XOSC startup delay to accommodate slow-starting oscillator
target_compile_definitions(
RP2040-Decoder PUBLIC
PICO_XOSC_STARTUP_DELAY_MULTIPLIER=64
)
# Add pico_multicore which is required for multicore functionality
target_link_libraries(
RP2040-Decoder
pico_stdlib
hardware_pwm
hardware_adc
hardware_flash
pico_multicore
)
# disable usb stack
pico_enable_stdio_usb(RP2040-Decoder 0)
# disable uart output (to enable put a 1 instead of the 0)
pico_enable_stdio_uart(RP2040-Decoder 0)
# Copy code to from flash to memory, then run from memory
pico_set_binary_type(RP2040-Decoder copy_to_ram)
# create map/bin/hex file etc.
pico_add_extra_outputs(RP2040-Decoder)
+1
View File
@@ -0,0 +1 @@
build
+75
View File
@@ -0,0 +1,75 @@
# Generated Cmake Pico project file
cmake_minimum_required(VERSION 3.13)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Initialise pico_sdk from installed location
# (note this can come from environment, CMake cache etc)
# == DO NOT EDIT THE FOLLOWING LINES for the Raspberry Pi Pico VS Code Extension to work ==
if(WIN32)
set(USERHOME $ENV{USERPROFILE})
else()
set(USERHOME $ENV{HOME})
endif()
set(sdkVersion 2.1.0)
set(toolchainVersion 13_3_Rel1)
set(picotoolVersion 2.1.0)
set(picoVscode ${USERHOME}/.pico-sdk/cmake/pico-vscode.cmake)
if (EXISTS ${picoVscode})
include(${picoVscode})
endif()
# ====================================================================================
set(PICO_BOARD_HEADER_DIRS ${CMAKE_SOURCE_DIR})
set(PICO_BOARD RP2040-Decoder-board CACHE STRING "Board type")
# Pull in Raspberry Pi Pico SDK (must be before project)
include(pico_sdk_import.cmake)
project(RP2040-Decoder C CXX ASM)
# Initialise the Raspberry Pi Pico SDK
pico_sdk_init()
add_compile_options(#-Wall
-O0
-Wno-format # int != int32 _t as far as the compiler is concerned because gcc has int32_t as long int
-Wno-unused-function # we have some for the docs that aren't called
-Wno-maybe-uninitialized)
# Add executable
add_executable( RP2040-Decoder
core0.c
core0.h
core1.c
core1.h
shared.c
shared.h )
pico_set_program_name(RP2040-Decoder "RP2040-Decoder")
pico_set_program_version(RP2040-Decoder "1.0")
# Modify the below lines to enable/disable output over UART/USB
pico_enable_stdio_uart(RP2040-Decoder 0)
pico_enable_stdio_usb(RP2040-Decoder 1)
# Add the standard library to the build
target_link_libraries( RP2040-Decoder
pico_stdlib
hardware_pwm
hardware_adc
hardware_flash
pico_multicore
pico_flash)
# Add the standard include files to the build
target_include_directories(RP2040-Decoder PRIVATE
${CMAKE_CURRENT_LIST_DIR}
)
# create map/bin/hex file etc.
pico_add_extra_outputs(RP2040-Decoder)
+1135 -1135
View File
File diff suppressed because it is too large. Load diff
+63
View File
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
// -----------------------------------------------------
// NOTE: THIS HEADER IS ALSO INCLUDED BY ASSEMBLER SO
// SHOULD ONLY CONSIST OF PREPROCESSOR DIRECTIVES
// -----------------------------------------------------
// Modified version of "boards/pico.h" for using the RP2040-Decoder board
// pico_cmake_set PICO_PLATFORM=rp2040
#ifndef _BOARDS_RP2040_DECODER_H
#define _BOARDS_RP2040_DECODER_H
// For board detection
#define RP2040_DECODER
// In case the xosc takes longer to stabilize than usual
#ifndef PICO_XOSC_STARTUP_DELAY_MULTIPLIER
#define PICO_XOSC_STARTUP_DELAY_MULTIPLIER 64
#endif
// --- UART ---
#ifndef PICO_DEFAULT_UART
#define PICO_DEFAULT_UART 0
#endif
#ifndef PICO_DEFAULT_UART_TX_PIN
#define PICO_DEFAULT_UART_TX_PIN 0
#endif
#ifndef PICO_DEFAULT_UART_RX_PIN
#define PICO_DEFAULT_UART_RX_PIN 1
#endif
// --- LED ---
#ifndef RP2040_DECODER_DEFAULT_LED_PIN
#define RP2040_DECODER_DEFAULT_LED_PIN 12
#endif
// --- FLASH ---
// Flash size is 8 MiB for W25Q64JVSSIQ if you decide to use another capacity flash IC change the size to the corresponding value
#ifndef PICO_FLASH_SIZE_BYTES
#define PICO_FLASH_SIZE_BYTES (8 * 1024 * 1024)
#endif
// Offset from base address used for saving CV_ARRAY_FLASH - in this case one sector size from the end of flash
#define FLASH_TARGET_OFFSET (PICO_FLASH_SIZE_BYTES-FLASH_SECTOR_SIZE)
#ifndef PICO_FLASH_SPI_CLKDIV
#define PICO_FLASH_SPI_CLKDIV 2
#endif
#ifndef PICO_RP2040_B0_SUPPORTED
#define PICO_RP2040_B0_SUPPORTED 1
#endif
#endif
+111 -66
View File
@@ -8,11 +8,61 @@
// XIP_BASE + FLASH_TARGET_OFFSET = "0x101FF000" <- This address need to be smaller than __flash_binary_end stored in RP2040-Decoder.elf.map
const uint8_t *CV_ARRAY_FLASH = (const uint8_t *) (XIP_BASE + FLASH_TARGET_OFFSET);
uint64_t input_bit_buffer = 0;
uint64_t input_bit_buffer = 1;
uint16_t level_table[32] = {0};
absolute_time_t falling_edge_time, rising_edge_time;
// Function to read the flash size of the chip
uint32_t get_flash_size() {
// Prepare TX and RX buffers
uint8_t txbuf[4] = { FLASH_CMD_READ_JEDEC_ID, 0, 0, 0 }; // Send the 0x9F command
uint8_t rxbuf[4] = { 0, 0, 0, 0 }; // RX buffer for the response
// Read the JEDEC ID
flash_do_cmd(txbuf, rxbuf, sizeof(txbuf));
// The third byte of the response (rxbuf[3]) contains the log2 size (e.g., 0x18 for 16 MB)
if (rxbuf[3] == 0) {
set_error(FLASH_SIZE_READBACK_FAILURE);
}
// Calculate the actual size (2^size_code bytes)
uint32_t flash_size = (1u<<rxbuf[3]);
// Return the flash size in bytes
return flash_size;
}
// This function will be called when it's safe to call flash_range_erase
// Always erases one sector, see FLASH_SECTOR_SIZE
static void call_flash_range_erase(void *param) {
uint32_t offset = ((uintptr_t*)param)[0];
uint32_t byte_count = ((uintptr_t*)param)[1];
// Check for offset alignment (Must be aligned to a 4096-byte flash sector)
// Check byte count (Must be a multiple of 4096 bytes (one sector))
if ((offset % FLASH_SECTOR_SIZE != 0) || (byte_count % FLASH_SECTOR_SIZE !=0)) {
set_error(FLASH_SAFE_EXECUTE_PROGRAM_FAILURE);
return;
}
flash_range_erase(offset, byte_count);
}
// This function will be called when it's safe to call flash_range_program
static void call_flash_range_program(void *param) {
uint32_t offset = ((uintptr_t*)param)[0];
uint32_t byte_count = ((uintptr_t*)param)[1];
const uint8_t *data = (const uint8_t *)((uintptr_t*)param)[2];
// Check for offset alignment (Must be aligned to a 256-byte flash page)
// Check byte count (Must be a multiple of 256 bytes (one page))
if ((offset % FLASH_PAGE_SIZE != 0) || (byte_count % FLASH_PAGE_SIZE != 0)) {
set_error(FLASH_SAFE_EXECUTE_PROGRAM_FAILURE);
return;
}
flash_range_program(offset, data, byte_count);
}
float two_std_dev(const float arr[], const uint32_t length) {
// Calculate arithmetic average
float sum = 0;
@@ -44,6 +94,7 @@ float two_std_dev(const float arr[], const uint32_t length) {
// Measures constant adc offset and programs the offset into flash
// TODO: check necessity and correctness of adc offset function
void adc_offset_adjustment(const uint32_t n) {
LOG(1, "adc_offset_adjustment");
@@ -55,7 +106,7 @@ void adc_offset_adjustment(const uint32_t n) {
busy_wait_ms(1000);
// Measure n times in "reverse direction" (GPIO 28)
adc_select_input(2);
adc_select_input(2); //TODO: "2" as input argument?
adc_run(true);
for (uint32_t i = 0; i < n; i++) {
offsets_rev[i] = (float) adc_fifo_get_blocking();
@@ -85,8 +136,18 @@ void adc_offset_adjustment(const uint32_t n) {
uint8_t CV_ARRAY_TEMP[CV_ARRAY_SIZE];
memcpy(CV_ARRAY_TEMP, CV_ARRAY_FLASH, sizeof(CV_ARRAY_TEMP));
CV_ARRAY_TEMP[171] = offset;
flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE);
flash_range_program(FLASH_TARGET_OFFSET, CV_ARRAY_TEMP, FLASH_PAGE_SIZE * 2);
uintptr_t params_erase[] = {FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE};
int ret_val = flash_safe_execute(call_flash_range_erase, params_erase, UINT32_MAX);
if (ret_val != PICO_OK){
set_error(FLASH_SAFE_EXECUTE_ERASE_FAILURE);
return;
}
uintptr_t params_program[] = {FLASH_TARGET_OFFSET, sizeof(CV_ARRAY_TEMP), (uintptr_t)CV_ARRAY_TEMP};
ret_val = flash_safe_execute(call_flash_range_program, params_program, UINT32_MAX);
if(ret_val != PICO_OK){
set_error(FLASH_SAFE_EXECUTE_PROGRAM_FAILURE);
return;
}
}
@@ -122,8 +183,18 @@ void write_cv_byte(uint16_t cv_address, uint8_t cv_data) {
memcpy(CV_ARRAY_TEMP, CV_ARRAY_FLASH, sizeof(CV_ARRAY_TEMP));
CV_ARRAY_TEMP[cv_address] = cv_data;
// First erase necessary amount of blocks in flash and then rewrite to flash
flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE);
flash_range_program(FLASH_TARGET_OFFSET, CV_ARRAY_TEMP, FLASH_PAGE_SIZE * 2);
uintptr_t params_erase[] = {FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE};
int ret_val = flash_safe_execute(call_flash_range_erase, params_erase, UINT32_MAX);
if (ret_val != PICO_OK){
set_error(FLASH_SAFE_EXECUTE_ERASE_FAILURE);
return;
}
uintptr_t params_program[] = { FLASH_TARGET_OFFSET, sizeof(CV_ARRAY_TEMP), (uintptr_t)CV_ARRAY_TEMP};
ret_val = flash_safe_execute(call_flash_range_program, params_program, UINT32_MAX);
if(ret_val != PICO_OK){
set_error(FLASH_SAFE_EXECUTE_PROGRAM_FAILURE);
return;
}
acknowledge();
}
@@ -153,8 +224,18 @@ void write_cv_handler(const uint16_t cv_index, const uint8_t cv_data) {
// Reset all CVs to Default (CV_8; Value = 8)
if (cv_data == 8) {
LOG(1, "reset of flash triggered via cv8 => 8\n");
flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE);
flash_range_program(FLASH_TARGET_OFFSET, CV_ARRAY_DEFAULT, FLASH_PAGE_SIZE * 2);
uintptr_t params_erase[] = {FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE};
int ret_val = flash_safe_execute(call_flash_range_erase, params_erase, UINT32_MAX);
if (ret_val != PICO_OK){
set_error(FLASH_SAFE_EXECUTE_ERASE_FAILURE);
return;
}
uintptr_t params_program[] = {FLASH_TARGET_OFFSET, sizeof(CV_ARRAY_DEFAULT), (uintptr_t)CV_ARRAY_DEFAULT};
ret_val = flash_safe_execute(call_flash_range_program, params_program, UINT32_MAX);
if(ret_val != PICO_OK){
set_error(FLASH_SAFE_EXECUTE_PROGRAM_FAILURE);
return;
}
}
break;
// CV_17 must have a value between 11000000->(192dec) and 11100111->(231dec) inclusive
@@ -191,18 +272,6 @@ void program_mode(const uint8_t number_of_bytes, const uint8_t *const byte_array
const uint16_t cv_address_ms_bits = cv_address_ms_bits_mask & byte_array[number_of_bytes - 1];
const uint16_t cv_address = byte_array[number_of_bytes - 2] + (cv_address_ms_bits << 8);
// Before accessing flash, timers and interrupts need to be disabled and core1 needs to be shut down.
if (pid_control_timer.pool) {
alarm_pool_destroy(pid_control_timer.pool);
}
if (speed_helper_timer.pool) {
alarm_pool_destroy(speed_helper_timer.pool);
}
multicore_reset_core1();
const uint32_t saved_interrupts = save_and_disable_interrupts();
if (instruction_type == 0b000001000) {
// Verify CV bit instruction
const uint8_t bit_pos_mask = 0b00000111;
@@ -224,12 +293,6 @@ void program_mode(const uint8_t number_of_bytes, const uint8_t *const byte_array
const uint8_t cv_data = byte_array[number_of_bytes - 3];
write_cv_handler(cv_address, cv_data);
}
// Restore interrupts
restore_interrupts(saved_interrupts);
// TODO: verify core1 launch...
multicore_launch_core1(core1_entry);
// TODO: Check busy wait necessity
busy_wait_ms(5);
}
}
@@ -536,45 +599,19 @@ void init_outputs() {
void cv_setup_check() {
// Check for flash factory setting and set CV_FLASH_ARRAY to default values when factory condition ("0xFF") is found.
if (CV_ARRAY_FLASH[64] == 0xFF) {
if (true || CV_ARRAY_FLASH[64] == 0xFF) {
LOG(1, "found cv[64] equals to 0xff, reseting CVs (and CV[8] = 8)\n");
const uint8_t arr[4] = {125, 8, 7, 124};
program_mode(4, arr); //reset to CV_ARRAY_DEFAULT (write CV_8 = 8)
}
// Check for existing ADC offset setup TODO: always measure?
if (CV_ARRAY_FLASH[171] == 0xFF) {
if (false && CV_ARRAY_FLASH[171] == 0xFF) {
// Write a value of 7 to read-only CV_7 in order to trigger a ADC offset measurement
LOG(1, "found cv[171] equals to 0xff, adc offset adjstments (and cr[7] = 7)\n");
const uint8_t arr[4] = {125, 7, 6, 124};
program_mode(4, arr); // ADC offset adjustment (write CV_7 = 7)
}
// Check for base PWM configuration - used for feed-forward
// Forward Direction
/*
if (get_16bit_CV(175) == 0) {
LOG(1, "found cv[175] equals to 0, forward direction\n");
const uint16_t base_pwm_fwd = measure_base_pwm(true, 10);
const uint8_t base_pwm_fwd_high_byte = base_pwm_fwd >> 8;
const uint8_t base_pwm_fwd_low_byte = base_pwm_fwd & 0x00FF;
const uint8_t arr0[4] = {125, base_pwm_fwd_high_byte, 175, 124};
program_mode(4, arr0);
const uint8_t arr1[4] = {125, base_pwm_fwd_low_byte, 176, 124};
program_mode(4, arr1);
}
// Reverse Direction
if (get_16bit_CV(177) == 0) {
LOG(1, "found cv[177] equals to 0, reverse direction\n");
const uint16_t base_pwm_rev = measure_base_pwm(false, 10);
const uint8_t base_pwm_rev_high_byte = base_pwm_rev >> 8;
const uint8_t base_pwm_rev_low_byte = base_pwm_rev & 0x00FF;
const uint8_t arr0[4] = {125, base_pwm_rev_high_byte, 177, 124};
program_mode(4, arr0);
const uint8_t arr1[4] = {125, base_pwm_rev_low_byte, 178, 124};
program_mode(4, arr1);
}
*/
LOG(1, "int_lim_max %d\n", CV_ARRAY_FLASH[51]);
LOG(1, "int_lim_min %d\n", CV_ARRAY_FLASH[52]);
}
@@ -593,7 +630,7 @@ void init_motor_pwm(const uint8_t gpio) {
pwm_set_gpio_level(gpio, 0);
// Enable PWM
pwm_set_enabled(slice_num, true);
LOG(2, "init motor(%d): wrapCounter %d clkdiv %d\n", gpio, wrap_counter, CV_ARRAY_FLASH[173]);
LOG(2, "Initialized Motor PWM on pin: %d; wrap_counter %d; clkdiv %d;\n", gpio, wrap_counter, CV_ARRAY_FLASH[173]);
}
void init_adc() {
@@ -607,24 +644,32 @@ void init_adc() {
}
int main() {
stdio_init_all();
LOG(1, "\n\n======\ncore0 init\n");
LOG(1, "Init motor PWM\n");
if(stdio_init_all() != true){
set_error(STDIO_INIT_FAILURE);
}
multicore_launch_core1(core1_entry);
LOG(1, "core0 Initialization...\n");
// Wait 1ms to give core1 some time to call flash_safe_execute_core_init()
busy_wait_ms(1);
// Check for error calling flash_safe_execute_core_init() on core1
if(get_error_state() & FLASH_SAFE_EXECUTE_CORE_INIT_FAILURE){
panic("Error calling flash_safe_execute_core_init() on core1!");
}
// Check CV array for factory state of flash or missing ADC offset setup
LOG(1, "Check CV array\n");
cv_setup_check();
LOG(1, "Initializing Motor PWM...\n");
init_motor_pwm(MOTOR_FWD_PIN);
init_motor_pwm(MOTOR_REV_PIN);
LOG(1, "Init ADC")
LOG(1, "Initializing ADC...")
init_adc();
LOG(1, "check cvs\n");
cv_setup_check();
LOG(1, "init outputs\n");
LOG(1, "Initializing Outputs...\n");
init_outputs();
LOG(1, "init gpios\n");
LOG(1, "Initializing GPIO...\n");
gpio_init(DCC_INPUT_PIN);
gpio_set_dir(DCC_INPUT_PIN, GPIO_IN);
gpio_pull_up(DCC_INPUT_PIN);
gpio_set_irq_enabled_with_callback(DCC_INPUT_PIN, GPIO_IRQ_EDGE_RISE, true, &track_signal_rise);
LOG(1, "core0 done\n");
busy_wait_ms(100);
multicore_launch_core1(core1_entry);
LOG(1, "core0 Initialized!\n");
while (true); //sleep_ms(100); //TODO: any potential issues when using sleep?
}
+2
View File
@@ -17,6 +17,8 @@
#define MESSAGE_5_BYTES 0b11111111110000000000000000000000000000000000000000000001
#define MESSAGE_MASK_5_BYTES 0b11111111111000000001000000001000000001000000001000000001
#define FLASH_CMD_READ_JEDEC_ID 0x9F
/*!
* \brief Bitmasks used for clearing bits corresponding to function blocks
*
+6 -2
View File
@@ -305,7 +305,11 @@ void init_controller(controller_parameter_t *const ctrl_par) {
}
void core1_entry() {
LOG(1, "core1 init...\n");
LOG(1, "core1 Initialization...\n");
if (flash_safe_execute_core_init() != true){
set_error(FLASH_SAFE_EXECUTE_CORE_INIT_FAILURE);
return;
}
controller_parameter_t control_parameter;
controller_parameter_t *ctrl_par = &control_parameter;
init_controller(ctrl_par);
@@ -321,6 +325,6 @@ void core1_entry() {
speed_helper,
ctrl_par,
&speed_helper_timer);
LOG(1, "core1 done\n");
LOG(1, "core1 Initialized!\n");
while (true); //sleep_ms(100); //TODO: any potential issues when using sleep?
}
View File
File renamed without changes.
@@ -18,9 +18,20 @@ if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_P
message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')")
endif ()
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_TAG} AND (NOT PICO_SDK_FETCH_FROM_GIT_TAG))
set(PICO_SDK_FETCH_FROM_GIT_TAG $ENV{PICO_SDK_FETCH_FROM_GIT_TAG})
message("Using PICO_SDK_FETCH_FROM_GIT_TAG from environment ('${PICO_SDK_FETCH_FROM_GIT_TAG}')")
endif ()
if (PICO_SDK_FETCH_FROM_GIT AND NOT PICO_SDK_FETCH_FROM_GIT_TAG)
set(PICO_SDK_FETCH_FROM_GIT_TAG "master")
message("Using master as default value for PICO_SDK_FETCH_FROM_GIT_TAG")
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")
set(PICO_SDK_FETCH_FROM_GIT_TAG "${PICO_SDK_FETCH_FROM_GIT_TAG}" CACHE FILEPATH "release tag for SDK")
if (NOT PICO_SDK_PATH)
if (PICO_SDK_FETCH_FROM_GIT)
@@ -29,11 +40,22 @@ if (NOT PICO_SDK_PATH)
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 ()
FetchContent_Declare(
pico_sdk
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
GIT_TAG master
)
# 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 ${PICO_SDK_FETCH_FROM_GIT_TAG}
GIT_SUBMODULES_RECURSE FALSE
)
else ()
FetchContent_Declare(
pico_sdk
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG}
)
endif ()
if (NOT pico_sdk)
message("Downloading Raspberry Pi Pico SDK")
FetchContent_Populate(pico_sdk)
+15
View File
@@ -9,6 +9,9 @@
uint8_t speed_step_target = 0;
uint8_t speed_step_target_prev = 0;
uint32_t error_state = 0;
// Functions in shared.c can be accessed by both cores
// Measures Back-EMF voltage (proportional to the rotational speed of the motor) on GPIO 28 and GPIO 29 respectively (depending on direction)
float measure(uint8_t total_iterations,
@@ -68,3 +71,15 @@ bool get_direction_of_speed_step(uint8_t speed_step){
// Shift by 7 Bytes to move bit7 into bit0 position and return
return speed_step >> 7;
}
void set_error(error_t err){
error_state |= err;
}
void clear_error(error_t err){
error_state & (~err);
}
uint32_t get_error_state(){
return error_state;
}
+17 -4
View File
@@ -11,11 +11,20 @@
#include "pico/stdlib.h"
#include "pico/multicore.h"
#include "pico/printf.h"
#include "pico/flash.h"
#include "hardware/pwm.h"
#include "hardware/adc.h"
#include "hardware/flash.h"
#include "hardware/gpio.h"
typedef enum {
FLASH_SAFE_EXECUTE_CORE_INIT_FAILURE = (1<<0),
FLASH_SAFE_EXECUTE_ERASE_FAILURE = (1<<1),
FLASH_SAFE_EXECUTE_PROGRAM_FAILURE = (1<<2),
FLASH_SIZE_READBACK_FAILURE = (1<<3),
STDIO_INIT_FAILURE = (1<<4),
} error_t;
// here you can enable printf debugging (needs extra soldering and you have to enable uart in the cmake configuration)
// please note
@@ -25,7 +34,7 @@
// - also note that with logging enabled GPIO0/1 are DISABLED from decoder functions because they are used for uart to the
// outside world (this is configure automatically for you below!)
// - IMPORTANT: you have to enable uart in the cmakelists.txt to enable logging!
#define LOGLEVEL 0
#define LOGLEVEL 1
#define LOG(level, ...) { if(level <= LOGLEVEL) {printf(__VA_ARGS__);}}
// Constant Value of 125 x 10^6
@@ -53,8 +62,6 @@
// GPIO pin mask with allowed outputs (AUX & GPIO (configured as outputs))
#define GPIO_ALLOWED_OUTPUTS (GPIO_OUTPUT_PIN_MASK) & ~(GPIO_ILLEGAL_MASK)
// Offset from base address used for saving CV_ARRAY_FLASH
#define FLASH_TARGET_OFFSET (PICO_FLASH_SIZE_BYTES-FLASH_SECTOR_SIZE)
extern repeating_timer_t pid_control_timer;
extern repeating_timer_t speed_helper_timer;
@@ -103,4 +110,10 @@ uint16_t get_16bit_CV (uint16_t CV_start_index);
* @param speed_step The speed step byte.
* @return `true` for forward direction, `false` for reverse direction.
*/
bool get_direction_of_speed_step(uint8_t speed_step);
bool get_direction_of_speed_step(uint8_t speed_step);
void set_error(error_t err);
void clear_error(error_t err);
uint32_t get_error_state();