Expanded on stdio logging functionality, also configuration of logging is now entirely in CMakeLists.txt for convenience, also warns now for logging configurations that make no sense; Clarification in CV table comment; Moved pin definitions into board definition files; two_std_dev() now works mostly with integers; Added function for resetting all CVs saved in flash; Added various LOG() messages;
This commit is contained in:
1 parent
7b1d5ed152
commit
f8b99de801
9 files changed
+311
-119
No files matched your search
+38
-3
@@ -52,9 +52,44 @@ add_executable( RP2040-Decoder
|
||||
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)
|
||||
|
||||
# LOGLEVEL>0 enables debugging via stdio, this can be done either via USB or alternatively,
|
||||
# directly with the UART TX/RX pins (see pin definition in RP2040-Decoder-board.h).
|
||||
#
|
||||
# In order to use stdio via USB please set STDIO_USB_ENABLED to 1 below
|
||||
# In order to use stdio via UART TX/RX pins please set STDIO_UART_ENABLED to 1 below
|
||||
#
|
||||
# Additional Notes:
|
||||
# - When logging is disabled (LOGLEVEL 0), all logging functions will be completely excluded during compilation.
|
||||
# - Be cautious: Logging can alter timing behavior. For example, the PID controller is time critical,
|
||||
# limit the frequency of log outputs—such as printing only every 100th iteration.
|
||||
# - Note that with UART logging enabled, PICO_DEFAULT_UART_TX_PIN and PICO_DEFAULT_UART_RX_PIN are disabled for decoder functions
|
||||
# because they are used for UART communication. This is automatically configured for you below.
|
||||
set(LOGLEVEL 1)
|
||||
# Additionally, the LOG_WAIT parameter can be configured to make the controller wait for user input via stdio before continuing.
|
||||
# This is useful for ensuring that all stdio logging outputs are captured.
|
||||
# If disabled, there may not be enough time to connect the serial terminal before most of the logging has already been completed.
|
||||
set(LOG_WAIT 1)
|
||||
# Modify the below lines to enable/disable output over UART and/or USB
|
||||
set(STDIO_USB_ENABLED 1)
|
||||
set(STDIO_UART_ENABLED 1)
|
||||
# Leave those two unmodified
|
||||
pico_enable_stdio_uart(RP2040-Decoder ${STDIO_UART_ENABLED})
|
||||
pico_enable_stdio_usb(RP2040-Decoder ${STDIO_USB_ENABLED})
|
||||
# Add a preprocessor definition which defines whether UART is used or not.
|
||||
# This is done to make the C-Code know whether pins used for UART need to be reserved and restricted from setting them as PWM/GPIO.
|
||||
target_compile_definitions( RP2040-Decoder PRIVATE
|
||||
LOGLEVEL=${LOGLEVEL}
|
||||
LOG_WAIT=${LOG_WAIT}
|
||||
STDIO_UART_ENABLED=${STDIO_UART_ENABLED}
|
||||
STDIO_USB_ENABLED=${STDIO_USB_ENABLED})
|
||||
# Warn when logging is enabled (LOGLEVEL>0) and neither stdio is enabled
|
||||
if (LOGLEVEL GREATER 0 AND (STDIO_USB_ENABLED EQUAL 0 AND STDIO_UART_ENABLED EQUAL 0))
|
||||
message(WARNING "\nWARNING: Logging (LOGLEVEL>0) enabled with both STDIO_USB_ENABLED and STDIO_UART_ENABLED disabled!")
|
||||
endif()
|
||||
if (LOGLEVEL EQUAL 0 AND LOG_WAIT EQUAL 1)
|
||||
message(WARNING "\nWARNING: LOG_WAIT enabled with Logging disabled (LOGLEVEL==0)!")
|
||||
endif()
|
||||
|
||||
# Add the standard library to the build
|
||||
target_link_libraries( RP2040-Decoder
|
||||
|
||||
+3
-3
@@ -2,10 +2,10 @@
|
||||
#include "pico/stdlib.h"
|
||||
|
||||
#define CV_ARRAY_SIZE 1024
|
||||
// All CVs are 8-bit numbers ranging from (0 - 255)dec = (0b00000000 - 0b11111111)bin = (0x00 - 0xFF)hex
|
||||
// Note : CV_1 = index 0, CV_2 = index 1, CV_3 = index 2, ...
|
||||
// All CVs are 8-bit numbers or bytes ranging from (0 - 255)dec = (0b00000000 - 0b11111111)bin = (0x00 - 0xFF)hex
|
||||
// IMPORTANT NOTE : CV_1 @ array_index = 0; CV_2 @ array_index = 1; CV_3 @ array_index = 2; ...;
|
||||
// This effectively means: array_index = cv_index - 1
|
||||
|
||||
// Explicitly write CV_ARRAY_DEFAULT into flash memory using __in_flash attribute macro
|
||||
uint8_t CV_ARRAY_DEFAULT [CV_ARRAY_SIZE] = {
|
||||
0b00000011, //CV_1 - Basic address - CV_1 = 0 and CV_1 > 127 are not valid - Default = 3
|
||||
0b00001000, //CV_2 - V_min - Default = 8
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* 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
|
||||
// NOTE: THIS IS FOR THE "LEGACY" Version without a USB-C Connector
|
||||
|
||||
// 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
|
||||
|
||||
// Other pins
|
||||
// GPIO used for DCC-Signal, Motor PWM or ADC
|
||||
#define DCC_INPUT_PIN 21u
|
||||
#define MOTOR_FWD_PIN 23u
|
||||
#define MOTOR_REV_PIN 22u
|
||||
#define FWD_V_EMF_ADC_PIN 28u
|
||||
#define REV_V_EMF_ADC_PIN 29u
|
||||
|
||||
#if LOGLEVEL != 0
|
||||
// GPIO used directly (GPIO 2-5 incl.) as outputs or to switch auxiliary output transistors (GPIO 24-27 incl.)
|
||||
#define GPIO_OUTPUT_PIN_MASK ((1u<<24) | (1u<<25) | (1u<<26) | (1u<<27) | (1u<<5) | (1u<<4) | (1u<<3) | (1u<<2))
|
||||
#else
|
||||
// GPIO used directly (GPIO 0-5 incl.) as outputs or to switch auxiliary output transistors (GPIO 24-27 incl.)
|
||||
#define GPIO_OUTPUT_PIN_MASK ((1u<<24) | (1u<<25) | (1u<<26) | (1u<<27) | (1u<<5) | (1u<<4) | (1u<<3) | (1u<<2) | (1u<<1) | (1u<<0))
|
||||
#endif
|
||||
|
||||
// GPIO pin mask to prevent setting illegal GPIOs (ADC, Motor, DCC Input Pin)
|
||||
// When logging to stdio using UART is enabled do NOT use PICO_DEFAULT_UART_TX_PIN and PICO_DEFAULT_UART_RX_PIN as GPIO/PWM outputs
|
||||
#if (LOGLEVEL != 0) && (STDIO_UART_ENABLED != 0)
|
||||
#define GPIO_ILLEGAL_MASK ((1u<<DCC_INPUT_PIN) | (1u<<MOTOR_FWD_PIN) | (1u<<MOTOR_REV_PIN) | (1u<<FWD_V_EMF_ADC_PIN) | (1u<<REV_V_EMF_ADC_PIN) | (1u<<PICO_DEFAULT_UART_TX_PIN) | (1u<<PICO_DEFAULT_UART_RX_PIN))
|
||||
#else
|
||||
#define GPIO_ILLEGAL_MASK ((1u<<DCC_INPUT_PIN) | (1u<<MOTOR_FWD_PIN) | (1u<<MOTOR_REV_PIN) | (1u<<FWD_V_EMF_ADC_PIN) | (1u<<REV_V_EMF_ADC_PIN))
|
||||
#endif
|
||||
|
||||
// GPIO pin mask with allowed outputs (AUX & GPIO (configured as outputs))
|
||||
#define GPIO_ALLOWED_OUTPUTS (GPIO_OUTPUT_PIN_MASK) & ~(GPIO_ILLEGAL_MASK)
|
||||
|
||||
#endif
|
||||
@@ -58,6 +58,33 @@
|
||||
#define PICO_RP2040_B0_SUPPORTED 1
|
||||
#endif
|
||||
|
||||
// Other pins
|
||||
// GPIO used for DCC-Signal, Motor PWM or ADC
|
||||
#define DCC_INPUT_PIN 21u
|
||||
#define MOTOR_FWD_PIN 23u
|
||||
#define MOTOR_REV_PIN 22u
|
||||
#define FWD_V_EMF_ADC_PIN 29u
|
||||
#define REV_V_EMF_ADC_PIN 28u
|
||||
|
||||
|
||||
#if LOGLEVEL != 0
|
||||
// GPIO used directly (GPIO 2-5 incl.) as outputs or to switch auxiliary output transistors (GPIO 24-27 incl.)
|
||||
#define GPIO_OUTPUT_PIN_MASK ((1u<<24) | (1u<<25) | (1u<<26) | (1u<<27) | (1u<<5) | (1u<<4) | (1u<<3) | (1u<<2))
|
||||
#else
|
||||
// GPIO used directly (GPIO 0-5 incl.) as outputs or to switch auxiliary output transistors (GPIO 24-27 incl.)
|
||||
#define GPIO_OUTPUT_PIN_MASK ((1u<<24) | (1u<<25) | (1u<<26) | (1u<<27) | (1u<<5) | (1u<<4) | (1u<<3) | (1u<<2) | (1u<<1) | (1u<<0))
|
||||
#endif
|
||||
|
||||
// GPIO pin mask to prevent setting illegal GPIOs (ADC, Motor, DCC Input Pin)
|
||||
// When logging to stdio using UART is enabled do NOT use PICO_DEFAULT_UART_TX_PIN and PICO_DEFAULT_UART_RX_PIN as GPIO/PWM outputs
|
||||
#if (LOGLEVEL != 0) && (STDIO_UART_ENABLED != 0)
|
||||
#define GPIO_ILLEGAL_MASK ((1u<<DCC_INPUT_PIN) | (1u<<MOTOR_FWD_PIN) | (1u<<MOTOR_REV_PIN) | (1u<<FWD_V_EMF_ADC_PIN) | (1u<<REV_V_EMF_ADC_PIN) | (1u<<PICO_DEFAULT_UART_TX_PIN) | (1u<<PICO_DEFAULT_UART_RX_PIN))
|
||||
#else
|
||||
#define GPIO_ILLEGAL_MASK ((1u<<DCC_INPUT_PIN) | (1u<<MOTOR_FWD_PIN) | (1u<<MOTOR_REV_PIN) | (1u<<FWD_V_EMF_ADC_PIN) | (1u<<REV_V_EMF_ADC_PIN))
|
||||
#endif
|
||||
|
||||
// GPIO pin mask with allowed outputs (AUX & GPIO (configured as outputs))
|
||||
#define GPIO_ALLOWED_OUTPUTS (GPIO_OUTPUT_PIN_MASK) & ~(GPIO_ILLEGAL_MASK)
|
||||
|
||||
|
||||
#endif
|
||||
+126
-74
@@ -63,43 +63,43 @@ static void call_flash_range_program(void *param) {
|
||||
}
|
||||
|
||||
|
||||
float two_std_dev(const float arr[], const uint32_t length) {
|
||||
uint16_t two_std_dev(const uint16_t arr[], const uint32_t length) {
|
||||
// Calculate arithmetic average
|
||||
float sum = 0;
|
||||
uint64_t sum = 0;
|
||||
for (uint32_t i = 0; i < length; ++i) {
|
||||
sum += arr[i];
|
||||
}
|
||||
const float x_avg = sum / (float)length;
|
||||
const uint64_t x_avg = sum / length;
|
||||
|
||||
// Calculate sample variance and sampled standard deviation
|
||||
sum = 0;
|
||||
for (uint32_t i = 0; i < length; ++i) {
|
||||
sum += (arr[i] - x_avg) * (arr[i] - x_avg);
|
||||
}
|
||||
const float variance = sum / (float)(length-1);
|
||||
const float std_dev = sqrtf(variance);
|
||||
const uint16_t variance = sum / (length-1);
|
||||
const uint16_t std_dev = (uint16_t) sqrtf((float) variance);
|
||||
|
||||
// Sum every element that is within two times the sampled standard deviation and compute new average
|
||||
sum = 0;
|
||||
uint32_t counter = 0;
|
||||
for (uint32_t i = 0; i < length; ++i) {
|
||||
const float diff = fabsf(arr[i] - x_avg);
|
||||
if ( diff < std_dev * 2.0f ){
|
||||
const uint32_t diff = abs(arr[i] - x_avg);
|
||||
if ( diff <= 2*std_dev ){
|
||||
sum += arr[i];
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
return sum / (float) counter;
|
||||
return sum / counter;
|
||||
}
|
||||
|
||||
|
||||
// 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");
|
||||
LOG(1, "ADC offset adjustment...\n");
|
||||
|
||||
float offsets_fwd[n];
|
||||
float offsets_rev[n];
|
||||
uint16_t offsets_fwd[n];
|
||||
uint16_t offsets_rev[n];
|
||||
// Set PWM level to 0 just to be sure
|
||||
pwm_set_gpio_level(MOTOR_FWD_PIN, 0);
|
||||
pwm_set_gpio_level(MOTOR_REV_PIN, 0);
|
||||
@@ -109,7 +109,7 @@ void adc_offset_adjustment(const uint32_t n) {
|
||||
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();
|
||||
offsets_rev[i] = adc_fifo_get_blocking();
|
||||
}
|
||||
adc_run(false);
|
||||
adc_fifo_drain();
|
||||
@@ -119,23 +119,22 @@ void adc_offset_adjustment(const uint32_t n) {
|
||||
adc_select_input(3);
|
||||
adc_run(true);
|
||||
for (uint32_t i = 0; i < n; i++) {
|
||||
offsets_fwd[i] = (float) adc_fifo_get_blocking();
|
||||
offsets_fwd[i] = adc_fifo_get_blocking();
|
||||
}
|
||||
adc_run(false);
|
||||
adc_fifo_drain();
|
||||
|
||||
// Find overall average discarding outliers in measurement
|
||||
const float offset_avg_fwd = two_std_dev(offsets_fwd, n);
|
||||
const float offset_avg_rev = two_std_dev(offsets_rev, n);
|
||||
const float overall_avg = (offset_avg_fwd + offset_avg_rev) / 2;
|
||||
const uint8_t offset = (uint8_t) roundf(overall_avg);
|
||||
const uint16_t offset_avg_fwd = two_std_dev(offsets_fwd, n);
|
||||
const uint16_t offset_avg_rev = two_std_dev(offsets_rev, n);
|
||||
const uint16_t overall_avg_offset = (offset_avg_fwd + offset_avg_rev) / 2;
|
||||
|
||||
LOG(1, "new adc offset CV[171] (%f): (uint8_t)%d\n", overall_avg, offset);
|
||||
LOG(1, "New ADC Offset value CV_172 = %u\n", overall_avg_offset);
|
||||
|
||||
// Create temporary array -> change CV 172 in temp array -> erase flash -> write temp array to flash
|
||||
uint8_t CV_ARRAY_TEMP[CV_ARRAY_SIZE];
|
||||
memcpy(CV_ARRAY_TEMP, CV_ARRAY_FLASH, sizeof(CV_ARRAY_TEMP));
|
||||
CV_ARRAY_TEMP[171] = offset;
|
||||
CV_ARRAY_TEMP[171] = overall_avg_offset;
|
||||
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){
|
||||
@@ -198,6 +197,20 @@ void write_cv_byte(uint16_t cv_address, uint8_t cv_data) {
|
||||
acknowledge();
|
||||
}
|
||||
|
||||
void reset_cv_array_to_default(){
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// There are some exceptions to writing CVs - e.g. writing to a read only CV is not valid - this gets handled here
|
||||
void write_cv_handler(const uint16_t cv_index, const uint8_t cv_data) {
|
||||
@@ -213,29 +226,18 @@ void write_cv_handler(const uint16_t cv_index, const uint8_t cv_data) {
|
||||
break;
|
||||
case 6: //CV_7
|
||||
// Read only (CV_7 - Version no.)
|
||||
// ADC offset Adjustment (CV_7; Value = 7);
|
||||
// ADC offset Adjustment is triggered when setting CV_7 = 7
|
||||
if (cv_data == 7) {
|
||||
LOG(1, "trigger adc offset adjustment via cv7 => 7\n");
|
||||
adc_offset_adjustment(8192);
|
||||
LOG(1, "Triggered ADC offset adjustment via CV_7 = 7\n");
|
||||
adc_offset_adjustment(ADC_CALIBRATION_ITERATIONS);
|
||||
}
|
||||
break;
|
||||
case 7: //CV_8
|
||||
// Read only (CV_8 - Manufacturer ID)
|
||||
// Reset all CVs to Default (CV_8; Value = 8)
|
||||
// Reset all CVs to default (CV_ARRAY_DEFAULT) when setting CV_8 = 8)
|
||||
if (cv_data == 8) {
|
||||
LOG(1, "reset of flash triggered via cv8 => 8\n");
|
||||
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;
|
||||
}
|
||||
LOG(1, "reset of flash triggered via CV_8 = 8\n");
|
||||
reset_cv_array_to_default();
|
||||
}
|
||||
break;
|
||||
// CV_17 must have a value between 11000000->(192dec) and 11100111->(231dec) inclusive
|
||||
@@ -575,6 +577,7 @@ void track_signal_fall( const unsigned int gpio, const long unsigned int events)
|
||||
}
|
||||
|
||||
void init_outputs() {
|
||||
LOG(1, "Initializing Outputs...\n");
|
||||
gpio_init_mask(GPIO_ALLOWED_OUTPUTS);
|
||||
gpio_set_dir_out_masked(GPIO_ALLOWED_OUTPUTS); // Note: This might also disable UART on GPIO0 & GPIO1
|
||||
const uint32_t PWM_enabled_outputs = get_32bit_CV(111) & GPIO_ALLOWED_OUTPUTS;
|
||||
@@ -592,33 +595,32 @@ void init_outputs() {
|
||||
level_table[i] = level;
|
||||
pwm_set_clkdiv_int_frac(slice, clock_divider, 0);
|
||||
pwm_set_enabled(slice, true);
|
||||
LOG(1, "Set GPIO pin: %u as PWM output; PWM slice: %u; PWM channel: %u; Duty cycle: %f; Clock divider: %u\n", i, slice, channel, (float) level/wrap, clock_divider);
|
||||
}
|
||||
mask <<= 1;
|
||||
}
|
||||
}
|
||||
|
||||
void cv_setup_check() {
|
||||
LOG(1, "Checking CV array for factory state of flash or missing ADC offset setup...\n");
|
||||
// Check for flash factory setting and set CV_FLASH_ARRAY to default values when factory condition ("0xFF") is found.
|
||||
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)
|
||||
if (CV_ARRAY_FLASH[64] == 0xFF) {
|
||||
LOG(1, "Detected flash memory factory condidition (CV_65 == 0xFF), resetting all CVs to default values...\n");
|
||||
reset_cv_array_to_default();
|
||||
}
|
||||
|
||||
// Check for existing ADC offset setup TODO: always measure?
|
||||
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 existing ADC offset setup
|
||||
if (CV_ARRAY_FLASH[171] == 0xFF) {
|
||||
LOG(1, "Detected ADC offset factory condidition (CV_172 == 0xFF), running offset adjustment measurement function...\n");
|
||||
adc_offset_adjustment(ADC_CALIBRATION_ITERATIONS);
|
||||
}
|
||||
LOG(1, "int_lim_max %d\n", CV_ARRAY_FLASH[51]);
|
||||
LOG(1, "int_lim_min %d\n", CV_ARRAY_FLASH[52]);
|
||||
LOG(1, "CV check done!\n");
|
||||
}
|
||||
|
||||
|
||||
// Motor PWM initialization
|
||||
void init_motor_pwm(const uint8_t gpio) {
|
||||
LOG(2, "Initializing Motor PWM on GPIO pin: %u...\n", gpio);
|
||||
// Set GPIO pin to PWM functionality
|
||||
gpio_set_function(gpio, GPIO_FUNC_PWM);
|
||||
// Set wrap counter value saved in CV
|
||||
@@ -630,10 +632,11 @@ void init_motor_pwm(const uint8_t gpio) {
|
||||
pwm_set_gpio_level(gpio, 0);
|
||||
// Enable PWM
|
||||
pwm_set_enabled(slice_num, true);
|
||||
LOG(2, "Initialized Motor PWM on pin: %d; wrap_counter %d; clkdiv %d;\n", gpio, wrap_counter, CV_ARRAY_FLASH[173]);
|
||||
LOG(2, "Initialized motor PWM on pin: %u; wrap_counter %u; clkdiv %d;\n", gpio, wrap_counter, CV_ARRAY_FLASH[173]);
|
||||
}
|
||||
|
||||
void init_adc() {
|
||||
LOG(1, "Initializing ADC...\n")
|
||||
// Initialize ADC
|
||||
adc_init();
|
||||
// Set ADC GPIO pins to ADC functionality
|
||||
@@ -641,35 +644,84 @@ void init_adc() {
|
||||
adc_gpio_init(REV_V_EMF_ADC_PIN);
|
||||
// Configure ADC FIFO
|
||||
adc_fifo_setup(true, false, 0, false, false);
|
||||
LOG(1, "ADC initialization done!\n")
|
||||
}
|
||||
|
||||
int main() {
|
||||
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, "Initializing ADC...")
|
||||
init_adc();
|
||||
LOG(1, "Initializing Outputs...\n");
|
||||
init_outputs();
|
||||
LOG(1, "Initializing GPIO...\n");
|
||||
void init_digital_inputs() {
|
||||
LOG(1, "Initializing digital inputs and track signal irq...\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 Initialized!\n");
|
||||
while (true); //sleep_ms(100); //TODO: any potential issues when using sleep?
|
||||
LOG(1, "Digital inputs and track signal irq initialization done!\n");
|
||||
}
|
||||
|
||||
void wait_for_input() {
|
||||
while (true) {
|
||||
// Send a message to the user asking them to acknowledge it by sending a reply
|
||||
LOG(1, "Send any character to continue.\n");
|
||||
// Wait for a character (with a 1-second timeout)
|
||||
int received_char = getchar_timeout_us(1000000); // 1 second timeout
|
||||
if (received_char != PICO_ERROR_TIMEOUT) {
|
||||
// If a character is received, exit the function
|
||||
LOG(1, "Character '%c' received. Continuing...\n", (char)received_char);
|
||||
return;
|
||||
}
|
||||
// If no character is received, retry...
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
// Initialize LED GPIO pin
|
||||
gpio_init(RP2040_DECODER_DEFAULT_LED_PIN);
|
||||
gpio_set_dir(RP2040_DECODER_DEFAULT_LED_PIN, GPIO_OUT);
|
||||
|
||||
// Initialize stdio
|
||||
if(stdio_init_all() != true){
|
||||
set_error(STDIO_INIT_FAILURE);
|
||||
}
|
||||
|
||||
// Wait for user input before continuing, this is only enabled when LOG_WAIT is set to 1 and logging is enabled (LOGLEVEL > 0)
|
||||
// Also disabled when no STDIO is configured as the user would not be able to send anything
|
||||
# if (LOGLEVEL > 0 && LOG_WAIT == 1) && (STDIO_UART_ENABLED || STDIO_USB_ENABLED)
|
||||
wait_for_input();
|
||||
printf("%u", STDIO_UART_ENABLED);
|
||||
#endif
|
||||
|
||||
LOG(1, "core0 Initialization...\n");
|
||||
|
||||
// Launch core1
|
||||
multicore_launch_core1(core1_entry);
|
||||
|
||||
// Wait 10ms to give core1 some time to call flash_safe_execute_core_init()
|
||||
busy_wait_ms(10);
|
||||
|
||||
// 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!");
|
||||
}
|
||||
|
||||
// Initialize Motor PWM pins
|
||||
init_motor_pwm(MOTOR_FWD_PIN);
|
||||
init_motor_pwm(MOTOR_REV_PIN);
|
||||
|
||||
// Initialize outputs
|
||||
init_outputs();
|
||||
|
||||
// Initialize ADC
|
||||
init_adc();
|
||||
|
||||
// Check CV array for factory state of flash or missing ADC offset setup
|
||||
cv_setup_check();
|
||||
|
||||
// Initialize digital inputs and track signal irq
|
||||
init_digital_inputs();
|
||||
|
||||
LOG(1, "core0 initialization done!\n");
|
||||
|
||||
// Endless loop
|
||||
while (true) {
|
||||
tight_loop_contents();
|
||||
}
|
||||
}
|
||||
+9
-2
@@ -8,6 +8,7 @@
|
||||
|
||||
#include "shared.h"
|
||||
#include "CV.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
#define SIZE_BYTE_ARRAY 5
|
||||
#define MESSAGE_3_BYTES 0b11111111110000000000000000000000000001
|
||||
@@ -18,6 +19,7 @@
|
||||
#define MESSAGE_MASK_5_BYTES 0b11111111111000000001000000001000000001000000001000000001
|
||||
|
||||
#define FLASH_CMD_READ_JEDEC_ID 0x9F
|
||||
#define ADC_CALIBRATION_ITERATIONS 8192
|
||||
|
||||
/*!
|
||||
* \brief Bitmasks used for clearing bits corresponding to function blocks
|
||||
@@ -46,7 +48,7 @@ const uint32_t clr_bit_arr[6] = {
|
||||
* \param length amount of elements in input array
|
||||
* \return average value of values deviating less than 2*standard_dev.
|
||||
*/
|
||||
float two_std_dev(const float arr[], uint32_t length);
|
||||
uint16_t two_std_dev(const uint16_t arr[], uint32_t length);
|
||||
|
||||
/*!
|
||||
* \brief Measures ADC offset and saves the offset value to flash memory.
|
||||
@@ -85,11 +87,16 @@ void verify_cv_byte(uint16_t cv_address, uint8_t cv_data);
|
||||
*/
|
||||
void write_cv_byte(uint16_t cv_address, uint8_t cv_data);
|
||||
|
||||
/*!
|
||||
* \brief This function sets all the CV values stored in flash back to their default settings (CV_ARRAY_DEFAULT).
|
||||
*/
|
||||
void reset_cv_array_to_default();
|
||||
|
||||
/*!
|
||||
* \brief CV Programming mode function
|
||||
* Used on entering programming mode in order to write CV values to persistent flash memory or verify/read bits/bytes from flash. <br>
|
||||
* Refers to NMRA Standard 9.2.3 ("Service Mode Programming") or RCN-216<br>
|
||||
*
|
||||
* TODO: describe new procedure
|
||||
* Procedure:
|
||||
* 1. Checks for valid programming command
|
||||
* 2. Disables core1 timers
|
||||
|
||||
+12
-5
@@ -236,6 +236,7 @@ bool controller_general(struct repeating_timer *const t) {
|
||||
|
||||
// PID controller and measurement parameter, and speed_table initialization
|
||||
void init_controller(controller_parameter_t *const ctrl_par) {
|
||||
LOG(1, "Motor controller initialization...\n")
|
||||
// General controller variables
|
||||
ctrl_par->mode = STARTUP_MODE;
|
||||
ctrl_par->feed_fwd = 0;
|
||||
@@ -260,8 +261,6 @@ void init_controller(controller_parameter_t *const ctrl_par) {
|
||||
const double v_mid = (double) CV_ARRAY_FLASH[5] * 16;
|
||||
const double v_max = (double) CV_ARRAY_FLASH[4] * 16;
|
||||
|
||||
LOG(1, "speedtable min(%f) mid(%f) max(%f)\n", v_min, v_mid, v_max);
|
||||
|
||||
const double delta_x = 63;
|
||||
const double m_1 = (v_mid - v_min) / delta_x;
|
||||
const double m_2 = (v_max - v_mid) / delta_x;
|
||||
@@ -301,18 +300,21 @@ void init_controller(controller_parameter_t *const ctrl_par) {
|
||||
ctrl_par->measurement_prev = 0.0f;
|
||||
ctrl_par->measurement_corrected = 0.0f;
|
||||
ctrl_par->setpoint = 0;
|
||||
|
||||
LOG(1, "Motor controller initialization done!\n")
|
||||
}
|
||||
|
||||
void core1_entry() {
|
||||
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);
|
||||
|
||||
alarm_pool_add_repeating_timer_ms(alarm_pool_create(0, 1),
|
||||
CV_ARRAY_FLASH[48],
|
||||
controller_general,
|
||||
@@ -325,6 +327,11 @@ void core1_entry() {
|
||||
speed_helper,
|
||||
ctrl_par,
|
||||
&speed_helper_timer);
|
||||
LOG(1, "core1 Initialized!\n");
|
||||
while (true); //sleep_ms(100); //TODO: any potential issues when using sleep?
|
||||
|
||||
LOG(1, "core1 initialization done!\n");
|
||||
|
||||
// Endless loop
|
||||
while (true) {
|
||||
tight_loop_contents();
|
||||
}
|
||||
}
|
||||
@@ -74,6 +74,7 @@ bool get_direction_of_speed_step(uint8_t speed_step){
|
||||
|
||||
void set_error(error_t err){
|
||||
error_state |= err;
|
||||
gpio_put(RP2040_DECODER_DEFAULT_LED_PIN, true);
|
||||
}
|
||||
|
||||
void clear_error(error_t err){
|
||||
|
||||
+6
-32
@@ -26,42 +26,16 @@ typedef enum {
|
||||
} error_t;
|
||||
|
||||
|
||||
// here you can enable printf debugging (needs extra soldering and you have to enable uart in the cmake configuration)
|
||||
// please note
|
||||
// - if it's disabled it will be compiled completely out
|
||||
// - be careful that printf debugging can change your timing behavior, for example in the pid controller limit the outputs
|
||||
// like already done (only print every 100th iteration for example!)
|
||||
// - 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 1
|
||||
#define LOG(level, ...) { if(level <= LOGLEVEL) {printf(__VA_ARGS__);}}
|
||||
#define LOG(level, ...) { \
|
||||
if (level <= LOGLEVEL) { \
|
||||
printf("[Core %u] ", get_core_num()); \
|
||||
printf(__VA_ARGS__); \
|
||||
} \
|
||||
}
|
||||
|
||||
// Constant Value of 125 x 10^6
|
||||
#define _125M 125000000
|
||||
|
||||
// GPIO used for DCC-Signal, Motor PWM or ADC
|
||||
#define DCC_INPUT_PIN 21u
|
||||
#define MOTOR_FWD_PIN 23u
|
||||
#define MOTOR_REV_PIN 22u
|
||||
#define FWD_V_EMF_ADC_PIN 28u
|
||||
#define REV_V_EMF_ADC_PIN 29u
|
||||
|
||||
// if logging is enabled do NOT use gpio0/1 for functions in the decoder
|
||||
#if LOGLEVEL != 0
|
||||
// GPIO used directly (GPIO 2-5 incl.) as outputs or to switch auxiliary output transistors (GPIO 24-27 incl.)
|
||||
#define GPIO_OUTPUT_PIN_MASK (1u<<24) | (1u<<25) | (1u<<26) | (1u<<27) | (1u<<5) | (1u<<4) | (1u<<3) | (1u<<2)
|
||||
#else
|
||||
// GPIO used directly (GPIO 0-5 incl.) as outputs or to switch auxiliary output transistors (GPIO 24-27 incl.)
|
||||
#define GPIO_OUTPUT_PIN_MASK (1u<<24) | (1u<<25) | (1u<<26) | (1u<<27) | (1u<<5) | (1u<<4) | (1u<<3) | (1u<<2) | (1u<<1) | (1u<<0)
|
||||
#endif
|
||||
|
||||
// GPIO pin mask to prevent setting illegal GPIOs (ADC, Motor, DCC Input Pin)
|
||||
#define GPIO_ILLEGAL_MASK (1u<<DCC_INPUT_PIN) | (1u<<MOTOR_FWD_PIN) | (1u<<MOTOR_REV_PIN) | (1u<<FWD_V_EMF_ADC_PIN) | (1u<<REV_V_EMF_ADC_PIN)
|
||||
|
||||
// GPIO pin mask with allowed outputs (AUX & GPIO (configured as outputs))
|
||||
#define GPIO_ALLOWED_OUTPUTS (GPIO_OUTPUT_PIN_MASK) & ~(GPIO_ILLEGAL_MASK)
|
||||
|
||||
|
||||
extern repeating_timer_t pid_control_timer;
|
||||
extern repeating_timer_t speed_helper_timer;
|
||||
|
||||
Reference in new issue
Block a user