diff --git a/CMakeLists.txt b/CMakeLists.txt
index b3b2d1b..50d798f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -44,7 +44,6 @@ target_link_libraries(
hardware_adc
hardware_flash
pico_multicore
- pico_platform
)
# disable usb stack
diff --git a/Doxyfile b/Doxyfile
index 7bb1ab9..c1d55c2 100644
--- a/Doxyfile
+++ b/Doxyfile
@@ -524,7 +524,7 @@ TIMESTAMP = NO
# normally produced when WARNINGS is set to YES.
# The default value is: NO.
-EXTRACT_ALL = YES
+EXTRACT_ALL = NO
# If the EXTRACT_PRIVATE tag is set to YES, all private members of a class will
# be included in the documentation.
diff --git a/core0.c b/core0.c
index 792f328..b80e073 100644
--- a/core0.c
+++ b/core0.c
@@ -185,7 +185,6 @@ void write_cv_handler(const uint16_t cv_index, const uint8_t cv_data) {
}
-// CV Programming function resets core1 and evaluates the type of programming instruction (e.g. write byte)
void program_mode(const uint8_t number_of_bytes, const uint8_t *const byte_array) {
//First check for valid programming command ("address" 112-127)
if (byte_array[number_of_bytes - 1] < 128 && byte_array[number_of_bytes - 1] > 111) {
@@ -516,8 +515,6 @@ void track_signal_fall( const unsigned int gpio, const long unsigned int events)
gpio_set_irq_enabled_with_callback(DCC_INPUT_PIN, GPIO_IRQ_EDGE_RISE, true, &track_signal_rise);
}
-
-// Output initialization function - Configures pwm frequency and clock divider according to CV_111 - CV_171
void init_outputs() {
gpio_init_mask(GPIO_ALLOWED_OUTPUTS);
gpio_set_dir_out_masked(GPIO_ALLOWED_OUTPUTS); // Note: This might also disable UART on GPIO0 & GPIO1
@@ -541,7 +538,6 @@ void init_outputs() {
}
}
-// Measures "base" pwm duty cycle needed to overcome friction
uint16_t measure_base_pwm(const bool direction, const uint8_t iterations) {
uint gpio;
if (direction) gpio = MOTOR_FWD_PIN;
@@ -575,13 +571,11 @@ uint16_t measure_base_pwm(const bool direction, const uint8_t iterations) {
// Find and return overall average discarding outliers in measurement - multiply with 0.9
const float retVal = 0.9f * two_std_dev(level_arr, 5);
LOG(1, "measure_base_pwm: %d(%f)\n", (uint16_t) retVal, retVal);
- return (uint16_t) (retVal);
+ return (uint16_t) retVal;
}
-// Check for flash memory factory condition and adc offset setup
-// When factory condition is found the CV_ARRAY_DEFAULT will be written into flash
-// When ADC Setup is not configured run adc offset adjustment function and write adc offset into flash
+
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) {
@@ -590,11 +584,12 @@ void cv_setup_check() {
program_mode(4, arr); //reset to CV_ARRAY_DEFAULT (write CV_8 = 8)
}
- // Check for adc offset setup
+ // Check for existing ADC offset setup
if (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)
+ program_mode(4, arr); // ADC offset adjustment (write CV_7 = 7)
}
// Check for base PWM configuration - used for feed-forward
@@ -627,37 +622,38 @@ void cv_setup_check() {
// Motor PWM initialization
void init_motor_pwm(const uint8_t gpio) {
+ // Set GPIO pin to PWM functionality
+ gpio_set_function(gpio, GPIO_FUNC_PWM);
+ // Set wrap counter value saved in CV
const uint16_t wrap_counter = (_125M / (CV_ARRAY_FLASH[8] * 100 + 10000)) - 1;
const uint32_t slice_num = pwm_gpio_to_slice_num(gpio);
pwm_set_clkdiv_int_frac(slice_num, CV_ARRAY_FLASH[173], 0);
pwm_set_wrap(slice_num, wrap_counter);
+ // Set initial level to 0
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]);
}
-
-// Main initialization - GPIO Config / ADC Config
-void init_gpio_adc() {
- if (gpio_get_function(MOTOR_FWD_PIN) != 4) {
- gpio_set_function(MOTOR_FWD_PIN, GPIO_FUNC_PWM);
- gpio_set_function(MOTOR_REV_PIN, GPIO_FUNC_PWM);
- adc_init();
- adc_gpio_init(FWD_V_EMF_ADC_PIN);
- adc_gpio_init(REV_V_EMF_ADC_PIN);
- adc_fifo_setup(true, false, 0, false, false);
- }
+void init_adc() {
+ // Initialize ADC
+ adc_init();
+ // Set ADC GPIO pins to ADC functionality
+ adc_gpio_init(FWD_V_EMF_ADC_PIN);
+ adc_gpio_init(REV_V_EMF_ADC_PIN);
+ // Configure ADC FIFO
+ adc_fifo_setup(true, false, 0, false, false);
}
-
int main() {
stdio_init_all();
LOG(1, "\n\n======\ncore0 init\n");
- init_gpio_adc();
- LOG(1, "init motor pwms\n");
+ LOG(1, "Init motor PWM\n");
init_motor_pwm(MOTOR_FWD_PIN);
init_motor_pwm(MOTOR_REV_PIN);
+ LOG(1, "Init ADC")
+ init_adc();
LOG(1, "check cvs\n");
cv_setup_check();
LOG(1, "init outputs\n");
diff --git a/core0.h b/core0.h
index cf5f00c..3ffea24 100644
--- a/core0.h
+++ b/core0.h
@@ -1,9 +1,9 @@
-//////////////////////////
-// RP2040-Decoder //
-// Gabriel Koppenstein //
-// core0.h //
-//////////////////////////
-
+/*!
+ *
+ * \file core0.h
+ * Used exclusively by core0
+ *
+ */
#pragma once
#include "shared.h"
#include "CV.h"
@@ -37,6 +37,23 @@ void regular_cv_write(uint16_t cv_index, uint8_t cv_data);
#define ProgramInstrWriteByte 0b11
#define ProgramCmd(instr, adrmsbits) ((instr<<2) | (adrmsbits))
+/*!
+ * \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.
+ * Refers to NMRA Standard 9.2.3 ("Service Mode Programming") or RCN-216
+ *
+ * Procedure:
+ * 1. Checks for valid programming command
+ * 2. Disables core1 timers
+ * 3. Resets core1
+ * 4. Saves interrupts
+ * 5. Evaluates the type of programming instruction Write byte / Verify byte / Verify bit and runs corresponding function
+ * 6. Restores interrupts
+ * 7. Launch core1
+ *
+ * \param number_of_bytes Number of bytes in byte_array[]
+ * \param byte_array Pointer to byte_array[]
+ */
void program_mode(uint8_t number_of_bytes, const uint8_t byte_array[]);
void set_outputs(uint32_t functions_to_set_bitmask);
void update_active_functions(uint32_t new_function_bitmask, uint8_t clr_bit_ind, bool direction_change);
@@ -50,8 +67,50 @@ void bits_to_byte_array(int8_t number_of_bytes,uint8_t byte_array[]);
void evaluate_message();
void track_signal_rise(unsigned int gpio, long unsigned int events);
void track_signal_fall(unsigned int gpio, long unsigned int events);
+
+/*!
+ * \brief Output initialization function
+ *
+ * Initializes all allowed GPIO outputs as outputs
+ * Sets PWM functionality for GPIOs configured as PWM pins CV_112 - CV_115 (see CV.h for reference)
+ * PWM frequency and clock divider are configured according to CV_116 - CV_171 (see CV.h for reference)
+ */
void init_outputs();
+
+/*!
+ * \brief Measures base pwm duty cycles at which the motor barely starts moving.
+ *
+ * Function starts with 1/20 of the maximum duty cycle then increases the duty cycle by 1/500 of the maximum until
+ * the motor movement can be measured. This is done for the specified amount of iterations.
+ * The array with raw measurements values is then passed to two_std_dev() in order to detect outliers and
+ * calculate the average.An additional correction factor is applied and the overall measurement result is then
+ * returned. In case no motor movement could be measured a value of 0 is returned and normally written to flash,
+ * indicating a pending calibration next time the decoder starts, rerunning the calibration.
+ * \param direction Specifies direction which is to be measured
+ * \param iterations Specifies the amount of iterations the measurement is to be repeated and averaged out over
+ * \return Measurement
+ */
uint16_t measure_base_pwm(bool direction, uint8_t iterations);
+
+/*!
+ * \brief Checks various CV values in order to detect factory condition or pending calibrations
+ *
+ * Procedure:
+ * 1. Check for flash memory factory condition when found write default values specified in CV.h to flash memory
+ * 2. Check for existing ADC offset setup when not found write 7 to read-only CV_7 in order to trigger a ADC offset measurement/calibration
+ * 3. Check for base PWM configuration/calibration, run calibration when none is found
+ */
void cv_setup_check();
+
+/*!
+ * \brief Function initializes motor PWM for pin specified
+ * \param gpio GPIO pin
+ */
void init_motor_pwm(uint8_t gpio);
-void init_gpio_adc();
\ No newline at end of file
+
+/*!
+ * \brief Initializes ADC
+ *
+ * Function initializes ADC, corresponding GPIO pins and ADC FIFO
+ */
+void init_adc();
\ No newline at end of file
diff --git a/core1.h b/core1.h
index e0b3b1c..354e544 100644
--- a/core1.h
+++ b/core1.h
@@ -1,8 +1,9 @@
-//////////////////////////
-// RP2040-Decoder //
-// Gabriel Koppenstein //
-// core1.h //
-//////////////////////////
+/*!
+*
+ * \file core1.h
+ * Used exclusively by core1
+ *
+ */
#pragma once
#include "shared.h"
diff --git a/shared.h b/shared.h
index 378c935..ceb603b 100644
--- a/shared.h
+++ b/shared.h
@@ -1,8 +1,9 @@
-//////////////////////////
-// RP2040-Decoder //
-// Gabriel Koppenstein //
-// shared.h //
-//////////////////////////
+/*!
+*
+ * \file shared.h
+ * Used by both cores
+ *
+ */
#pragma once
#include "string.h"
@@ -10,11 +11,9 @@
#include "pico/stdlib.h"
#include "pico/multicore.h"
#include "pico/printf.h"
-#include "pico/platform.h"
#include "hardware/pwm.h"
#include "hardware/adc.h"
#include "hardware/flash.h"
-#include "hardware/irq.h"
// here you can enable printf debugging (needs extra soldering and you have to enable uart in the cmake configuration)