From a0408c28edb0c949f025ca5934fe0c9642954acd Mon Sep 17 00:00:00 2001 From: questor Date: Thu, 27 Apr 2023 15:53:59 +0200 Subject: [PATCH 1/6] some refactoring --- core0.c | 51 +++++++++++++++++++++++++++++++++++++-------------- core0.h | 7 +++++++ shared.h | 3 +++ 3 files changed, 47 insertions(+), 14 deletions(-) diff --git a/core0.c b/core0.c index 53b0662..9f41edf 100644 --- a/core0.c +++ b/core0.c @@ -45,6 +45,8 @@ float two_std_dev(const float arr[], uint32_t length){ // Measures constant adc offset and programs the offset into flash void adc_offset_adjustment(uint32_t n){ + LOG(1, "adc_offset_adjustment\n"); + float offsets_fwd[n]; float offsets_rev[n]; // Set PWM level to 0 just to be sure @@ -131,18 +133,24 @@ void write_cv_handler(uint16_t cv_index, uint8_t cv_data){ switch (cv_index) { case 0: //CV_1 // CV_1 => Value = 0 is not allowed - if (cv_data == 0 || cv_data > 127) break; - else regular_cv_write(cv_index,cv_data); + if (cv_data == 0 || cv_data > 127) + break; + else + regular_cv_write(cv_index,cv_data); break; case 6: //CV_7 // Read only (CV_7 - Version no.) // ADC offset Adjustment (CV_7; Value = 7); - if (cv_data == 7) adc_offset_adjustment(8192); + if (cv_data == 7) { + LOG(1, "trigger adc offset adjustment via cv7 => 7\n"); + adc_offset_adjustment(8192); + } break; case 7: //CV_8 // Read only (CV_8 - Manufacturer ID) // 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); } @@ -150,12 +158,16 @@ void write_cv_handler(uint16_t cv_index, uint8_t cv_data){ // CV_17 must have a value between 11000000->(192dec) and 11100111->(231dec) inclusive // CV_17 == 192 && CV_18 == 0 -> Address = 0 is also not valid and therefore shall not be set case 16: //CV_17 - if ( (cv_data < 192 || cv_data > 231) || (CV_ARRAY_FLASH[17] == 0 && cv_data == 192) ) break; - else regular_cv_write(cv_index,cv_data); + if ( (cv_data < 192 || cv_data > 231) || (CV_ARRAY_FLASH[17] == 0 && cv_data == 192) ) + break; + else + regular_cv_write(cv_index,cv_data); break; case 17: //CV_18 - if(CV_ARRAY_FLASH[16] == 192 && cv_data == 0) break; - else regular_cv_write(cv_index,cv_data); + if(CV_ARRAY_FLASH[16] == 192 && cv_data == 0) + break; + else + regular_cv_write(cv_index,cv_data); break; // CV_31 & CV_32 Index high byte isn't implemented and shall not be set case 30: //CV_31 @@ -169,9 +181,9 @@ void write_cv_handler(uint16_t cv_index, uint8_t cv_data){ // CV Programming function resets core1 and evaluates the type of programming instruction (e.g. write byte) -void program_mode(uint8_t number_of_bytes, const uint8_t * const byte_array){ +void program_mode(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){ + if (byte_array[number_of_bytes - 1]<128 && byte_array[number_of_bytes - 1]>111) { uint8_t instruction_type_mask = 0b00001100; uint8_t instruction_type = instruction_type_mask & byte_array[number_of_bytes - 1]; uint8_t cv_address_ms_bits_mask = 0b00000011; @@ -179,9 +191,13 @@ void program_mode(uint8_t number_of_bytes, const uint8_t * const byte_array){ 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); + 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(); + uint32_t saved_interrupts = save_and_disable_interrupts(); // Verify CV bit instruction @@ -549,15 +565,16 @@ uint16_t measure_base_pwm(bool direction, uint8_t iterations){ // 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 ){ + LOG(1, "found cv[64] equals to 0xff, reseting CVs (and CV[8] = 8)\n"); uint8_t arr[4] = {125,8,7,124}; program_mode(4,arr); //reset to CV_ARRAY_DEFAULT (write CV_8 = 8) } // Check for adc offset setup if ( CV_ARRAY_FLASH[171] == 0xFF ){ + LOG(1, "found cv[171] equals to 0xff, adc offset adjustments (and cr[8] = 8)\n"); uint8_t arr[4] = {125,7,6,124}; program_mode(4,arr); //ADC offset adjustment (write CV_7 = 7) } @@ -565,6 +582,7 @@ void cv_setup_check(){ // 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"); uint16_t base_pwm_fwd = measure_base_pwm(true,10); uint8_t base_pwm_fwd_high_byte = base_pwm_fwd>>8; uint8_t base_pwm_fwd_low_byte = base_pwm_fwd&0x00FF; @@ -575,6 +593,7 @@ void cv_setup_check(){ } // Reverse Direction if (get_16bit_CV(177) == 0){ + LOG(1, "found cv[177] equals to 0, reverse direction\n"); uint16_t base_pwm_rev = measure_base_pwm(false,10); uint8_t base_pwm_rev_high_byte = base_pwm_rev>>8; uint8_t base_pwm_rev_low_byte = base_pwm_rev&0x00FF; @@ -625,16 +644,20 @@ void init_main(){ int main() { stdio_init_all(); - printf("core0 init...\n"); + LOG(0, "core0 init\n"); init_main(); + LOG(1, "init motor pwms\n"); init_motor_pwm(MOTOR_FWD_PIN); init_motor_pwm(MOTOR_REV_PIN); + LOG(1, "check cvs\n"); cv_setup_check(); + LOG(1, "init outputs\n"); init_outputs(); + LOG(1, "init gpios\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); - printf("core0 done\n"); + LOG(0, "core0 done\n"); while (1); } \ No newline at end of file diff --git a/core0.h b/core0.h index 1cb0602..f37cd49 100644 --- a/core0.h +++ b/core0.h @@ -30,6 +30,13 @@ void verify_cv_bit(uint16_t cv_address,bool bit_val, uint8_t bit_pos); void verify_cv_byte(uint16_t cv_address, uint8_t cv_data); void regular_cv_write(uint16_t cv_index, uint8_t cv_data); void write_cv_handler(uint16_t cv_index, uint8_t cv_data); + + +#define ProgramInstrVerifyBit 0b10 +#define ProgramInstrVerifyByte 0b01 +#define ProgramInstrWriteByte 0b11 +#define ProgramCmd(instr, adrmsbits) ((instr<<2) | (adrmsbits)) + 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 byte, uint8_t clr_bit_ind,bool direction_change); diff --git a/shared.h b/shared.h index 1652762..5f48721 100644 --- a/shared.h +++ b/shared.h @@ -15,6 +15,9 @@ #include "hardware/flash.h" #include "hardware/irq.h" +#define LOGLEVEL 3 +#define LOG(level, x, ...) {if(level <= LOGLEVEL) { printf((x), ##__VA_ARGS__);}} + // Constant Value of 125 x 10⁶ #define _125M 125000000 From 23b69d68238f0ff07236514c7ba1cea6895eff70 Mon Sep 17 00:00:00 2001 From: questor/Inter Date: Thu, 27 Apr 2023 23:07:19 +0200 Subject: [PATCH 2/6] more debug output --- CV.h | 4 ++-- core0.c | 20 ++++++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/CV.h b/CV.h index fe12167..7a53513 100644 --- a/CV.h +++ b/CV.h @@ -203,8 +203,8 @@ uint8_t CV_ARRAY_DEFAULT [CV_ARRAY_SIZE] = { //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //Offset adjustment for ADC // //If CV_172 = 0xFF = 255 there will be a measurement procedure on startup // - 0b11111111, //CV_172 - ADC offset Default: 255 // - 0b01111111, //CV_173 - ADC offset measurement cycles Default: 127 // + 0b11111111, //CV_172 - ADC offset Default: 255 (triggers new measurement!) // + 0b01111111, //CV_173 - ADC offset measurement cycles Default: 127 // //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 0b00000001, //CV_174 - Additional motor-PWM clock divider. 0b00000111, //CV_175 - speed_helper timer delay - can be used to adjust accel/decel rate diff --git a/core0.c b/core0.c index 9f41edf..6964f54 100644 --- a/core0.c +++ b/core0.c @@ -79,6 +79,8 @@ void adc_offset_adjustment(uint32_t n){ float overall_avg = (offset_avg_fwd+offset_avg_rev)/2; uint8_t offset = (uint8_t)roundf(overall_avg); + LOG(1, "new adc offset CV[171] (%f): (uint8_t)%d\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)); @@ -550,14 +552,18 @@ uint16_t measure_base_pwm(bool direction, uint8_t iterations){ CV_ARRAY_FLASH[63], direction); // Abort measurement and write default value of 0 to flash - if (level > max_level) return 0; - } - while((measurement - (float)CV_ARRAY_FLASH[171]) < 5.0f); + if (level > max_level) { + LOG(1, "measure_base_pwm: abort measurement and return 0\n"); + return 0; + } + } while((measurement - (float)CV_ARRAY_FLASH[171]) < 5.0f); level_arr[i] = (float)level; busy_wait_ms(100); } // Find and return overall average discarding outliers in measurement - multiply with 0.9 - return (uint16_t)(0.9f*two_std_dev(level_arr,5)); + float retVal = 0.9f*two_std_dev(level_arr,5); + printf("measure_base_pwm: %d(%f)\n", (uint16_t)retVal, retVal); + return (uint16_t)(retVal); } @@ -566,7 +572,7 @@ uint16_t measure_base_pwm(bool direction, uint8_t iterations){ // 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 ){ + if ( CV_ARRAY_FLASH[64] == 0xFF ){ LOG(1, "found cv[64] equals to 0xff, reseting CVs (and CV[8] = 8)\n"); uint8_t arr[4] = {125,8,7,124}; program_mode(4,arr); //reset to CV_ARRAY_DEFAULT (write CV_8 = 8) @@ -574,7 +580,7 @@ void cv_setup_check(){ // Check for adc offset setup if ( CV_ARRAY_FLASH[171] == 0xFF ){ - LOG(1, "found cv[171] equals to 0xff, adc offset adjustments (and cr[8] = 8)\n"); + LOG(1, "found cv[171] equals to 0xff, adc offset adjstments (and cr[7] = 7)\n"); uint8_t arr[4] = {125,7,6,124}; program_mode(4,arr); //ADC offset adjustment (write CV_7 = 7) } @@ -602,6 +608,8 @@ void cv_setup_check(){ uint8_t arr1[4] = {125,base_pwm_rev_low_byte,178,124}; program_mode(4,arr1); } + LOG(1, "int_lim_max %f\n", 10*(float)CV_ARRAY_FLASH[51]); + LOG(1, "int_lim_min %f\n", -10*(float)CV_ARRAY_FLASH[52]); } From 995dca15c8d00cba5bb6c8d781362ec7d2cb181f Mon Sep 17 00:00:00 2001 From: questor/Inter Date: Mon, 8 May 2023 22:50:19 +0200 Subject: [PATCH 3/6] more debug printfs, disabled function usage of gpio0/1, disabled usb interface to save some 25kb in the firmware image --- CMakeLists.txt | 4 ++-- core0.c | 14 ++++++++++---- core1.c | 26 +++++++++++++++++++++----- shared.h | 13 ++++++++++--- 4 files changed, 43 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6774443..06d035a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,8 +44,8 @@ target_link_libraries( ) # enable usb output, disable uart output -pico_enable_stdio_usb(RP2040-Decoder 1) +pico_enable_stdio_usb(RP2040-Decoder 0) pico_enable_stdio_uart(RP2040-Decoder 1) # create map/bin/hex file etc. -pico_add_extra_outputs(RP2040-Decoder) +#pico_add_extra_outputs(RP2040-Decoder) diff --git a/core0.c b/core0.c index 6964f54..d92b675 100644 --- a/core0.c +++ b/core0.c @@ -364,7 +364,9 @@ void instruction_evaluation(uint8_t number_of_bytes,const uint8_t * const byte_a if (command_byte_n == 0b00111111){ static bool prev_dir; uint32_t speed_step = byte_array[command_byte_start_index - 1]; +// LOG(1, "new speed %d", speed_step); multicore_fifo_push_blocking(speed_step); +// LOG(1, " submitted to list\n"); // In case of a direction change, functions need to be updated because functions might depend on direction state if (get_direction(0) != prev_dir) { update_active_functions(0,0,true); @@ -541,6 +543,7 @@ uint16_t measure_base_pwm(bool direction, uint8_t iterations){ for (int i = 0; i < iterations; ++i) { uint16_t max_level = _125M/(CV_ARRAY_FLASH[8]*100+10000); uint16_t level = max_level/20; + LOG(1, "iteration %d: maxlevel %d level %d\n", i, max_level, level); float measurement; do { pwm_set_gpio_level(gpio, level); @@ -552,11 +555,13 @@ uint16_t measure_base_pwm(bool direction, uint8_t iterations){ CV_ARRAY_FLASH[63], direction); // Abort measurement and write default value of 0 to flash + LOG(1, "level %d measurement %f loop-cond: %f\n", level, measurement, measurement-(float)CV_ARRAY_FLASH[171]); if (level > max_level) { LOG(1, "measure_base_pwm: abort measurement and return 0\n"); return 0; } } while((measurement - (float)CV_ARRAY_FLASH[171]) < 5.0f); + LOG(1, "level_arr[%d] = %f\n", i, level); level_arr[i] = (float)level; busy_wait_ms(100); } @@ -572,7 +577,8 @@ uint16_t measure_base_pwm(bool direction, uint8_t iterations){ // 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 ){ +// if ( CV_ARRAY_FLASH[64] == 0xFF ){ + if(1) { LOG(1, "found cv[64] equals to 0xff, reseting CVs (and CV[8] = 8)\n"); uint8_t arr[4] = {125,8,7,124}; program_mode(4,arr); //reset to CV_ARRAY_DEFAULT (write CV_8 = 8) @@ -608,8 +614,8 @@ void cv_setup_check(){ uint8_t arr1[4] = {125,base_pwm_rev_low_byte,178,124}; program_mode(4,arr1); } - LOG(1, "int_lim_max %f\n", 10*(float)CV_ARRAY_FLASH[51]); - LOG(1, "int_lim_min %f\n", -10*(float)CV_ARRAY_FLASH[52]); + LOG(1, "int_lim_max %d\n", CV_ARRAY_FLASH[51]); + LOG(1, "int_lim_min %d\n", CV_ARRAY_FLASH[52]); } @@ -652,7 +658,7 @@ void init_main(){ int main() { stdio_init_all(); - LOG(0, "core0 init\n"); + LOG(0, "\n\n======\ncore0 init\n"); init_main(); LOG(1, "init motor pwms\n"); init_motor_pwm(MOTOR_FWD_PIN); diff --git a/core1.c b/core1.c index c2072ab..1caf2be 100644 --- a/core1.c +++ b/core1.c @@ -16,14 +16,18 @@ void update_speed_dir(pid_params *pid, uint32_t speed_step_target){ // Forward if(speed_step_target > 127) { pid->direction = true^reverse_dir; - if (pid->speed_step_target == 128) pid->end_target_index = 0; - else pid->end_target_index = (pid->speed_step_target-129); + if (pid->speed_step_target == 128) + pid->end_target_index = 0; + else + pid->end_target_index = (pid->speed_step_target-129); } // Reverse else { pid->direction = false^reverse_dir; - if (pid->speed_step_target == 0) pid->end_target_index = 0; - else pid->end_target_index = (pid->speed_step_target-1); + if (pid->speed_step_target == 0) + pid->end_target_index = 0; + else + pid->end_target_index = (pid->speed_step_target-1); } } @@ -192,6 +196,15 @@ bool pid_control(struct repeating_timer *t){ if (output < 0 || !pid->setpoint) output = 0.0f; else if (output > pid->max_output) output = pid->max_output; + if(LOGLEVEL >= 1) { + static int counter = 0; + counter++; + if(counter > 100) { + LOG(1, "pid error(%f) output(%f)\n", e, output); + counter = 0; + } + } + // Set PWM Level and discard results in adc fifo adjust_pwm_level((uint16_t)roundf(output),pid); adc_fifo_drain(); @@ -226,6 +239,9 @@ void init_pid(pid_params *pid){ double v_min = (double)CV_ARRAY_FLASH[1]; double v_mid = (double)CV_ARRAY_FLASH[5]*16; 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); + double delta_x = 63; double m_1 = (v_mid-v_min)/delta_x; double m_2 = (v_max-v_mid)/delta_x; @@ -305,7 +321,7 @@ void core1_entry() { multicore_fifo_push_blocking((uint32_t) &(pid->direction)); uint64_t packet_timeout_threshold_in_us = CV_ARRAY_FLASH[10]*1000000; absolute_time_t time_last_update = get_absolute_time(); - printf("core1 done\n"); +// printf("core1 done\n"); while (1){ // Check for time difference between last packet and current time (Packet Timeout) absolute_time_t current_time = get_absolute_time(); diff --git a/shared.h b/shared.h index 5f48721..b8bc27f 100644 --- a/shared.h +++ b/shared.h @@ -16,7 +16,8 @@ #include "hardware/irq.h" #define LOGLEVEL 3 -#define LOG(level, x, ...) {if(level <= LOGLEVEL) { printf((x), ##__VA_ARGS__);}} +//#define LOG(level, x, ...) {if(level <= LOGLEVEL) { printf((x), ##__VA_ARGS__);}} +#define LOG(level, ...) printf(__VA_ARGS__) // Constant Value of 125 x 10⁶ #define _125M 125000000 @@ -28,8 +29,14 @@ #define FWD_V_EMF_ADC_PIN 28u #define REV_V_EMF_ADC_PIN 29u -// 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) +#if LOGLEVEL != 0 + // 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) +#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< Date: Fri, 12 May 2023 21:05:48 +0200 Subject: [PATCH 4/6] disable logging by default, added comments how to enable it and what are the consequences --- core0.c | 21 +++++++++++---------- core0.h | 2 +- core1.c | 2 +- shared.h | 12 ++++++++++-- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/core0.c b/core0.c index d92b675..1e3c212 100644 --- a/core0.c +++ b/core0.c @@ -543,7 +543,7 @@ uint16_t measure_base_pwm(bool direction, uint8_t iterations){ for (int i = 0; i < iterations; ++i) { uint16_t max_level = _125M/(CV_ARRAY_FLASH[8]*100+10000); uint16_t level = max_level/20; - LOG(1, "iteration %d: maxlevel %d level %d\n", i, max_level, level); + LOG(3, "iteration %d: maxlevel %d level %d\n", i, max_level, level); float measurement; do { pwm_set_gpio_level(gpio, level); @@ -554,20 +554,20 @@ uint16_t measure_base_pwm(bool direction, uint8_t iterations){ CV_ARRAY_FLASH[62], CV_ARRAY_FLASH[63], direction); - // Abort measurement and write default value of 0 to flash - LOG(1, "level %d measurement %f loop-cond: %f\n", level, measurement, measurement-(float)CV_ARRAY_FLASH[171]); + LOG(3, "level %d measurement %f loop-cond: %f\n", level, measurement, measurement-(float)CV_ARRAY_FLASH[171]); if (level > max_level) { + // Abort measurement and write default value of 0 to flash LOG(1, "measure_base_pwm: abort measurement and return 0\n"); return 0; } } while((measurement - (float)CV_ARRAY_FLASH[171]) < 5.0f); - LOG(1, "level_arr[%d] = %f\n", i, level); + LOG(3, "level_arr[%d] = %f\n", i, level); level_arr[i] = (float)level; busy_wait_ms(100); } // Find and return overall average discarding outliers in measurement - multiply with 0.9 float retVal = 0.9f*two_std_dev(level_arr,5); - printf("measure_base_pwm: %d(%f)\n", (uint16_t)retVal, retVal); + LOG(1, "measure_base_pwm: %d(%f)\n", (uint16_t)retVal, retVal); return (uint16_t)(retVal); } @@ -577,8 +577,7 @@ uint16_t measure_base_pwm(bool direction, uint8_t iterations){ // 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 ){ - if(1) { + if ( CV_ARRAY_FLASH[64] == 0xFF ){ LOG(1, "found cv[64] equals to 0xff, reseting CVs (and CV[8] = 8)\n"); uint8_t arr[4] = {125,8,7,124}; program_mode(4,arr); //reset to CV_ARRAY_DEFAULT (write CV_8 = 8) @@ -640,11 +639,13 @@ void init_motor_pwm(uint8_t gpio) { pwm_set_wrap(slice_num, wrap_counter); pwm_set_gpio_level(gpio,0); 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_main(){ +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); @@ -658,8 +659,8 @@ void init_main(){ int main() { stdio_init_all(); - LOG(0, "\n\n======\ncore0 init\n"); - init_main(); + LOG(1, "\n\n======\ncore0 init\n"); + init_gpio_adc(); LOG(1, "init motor pwms\n"); init_motor_pwm(MOTOR_FWD_PIN); init_motor_pwm(MOTOR_REV_PIN); diff --git a/core0.h b/core0.h index f37cd49..49159e5 100644 --- a/core0.h +++ b/core0.h @@ -55,4 +55,4 @@ uint16_t measure_base_pwm(bool direction, uint8_t iterations); void cv_setup_check(); bool get_direction(bool *direction_ptr); void init_motor_pwm(uint8_t gpio); -void init_main(); \ No newline at end of file +void init_gpio_adc(); \ No newline at end of file diff --git a/core1.c b/core1.c index 1caf2be..a468a33 100644 --- a/core1.c +++ b/core1.c @@ -300,7 +300,7 @@ void init_pid(pid_params *pid){ void core1_entry() { - printf("core1 init...\n"); + LOG(1, "core1 init...\n"); pid_params pid_parameters; pid_params *pid = &pid_parameters; init_pid(pid); diff --git a/shared.h b/shared.h index b8bc27f..68fd3ac 100644 --- a/shared.h +++ b/shared.h @@ -15,9 +15,17 @@ #include "hardware/flash.h" #include "hardware/irq.h" -#define LOGLEVEL 3 + +// 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!) +#define LOGLEVEL 0 //#define LOG(level, x, ...) {if(level <= LOGLEVEL) { printf((x), ##__VA_ARGS__);}} -#define LOG(level, ...) printf(__VA_ARGS__) +#define LOG(level, ...) { if(level <= LOGLEVEL) {printf(__VA_ARGS__);}} // Constant Value of 125 x 10⁶ #define _125M 125000000 From 36f20a8ca3d77fd99c45623b51fca0cdeb5a4f48 Mon Sep 17 00:00:00 2001 From: questor/Inter Date: Sat, 27 May 2023 22:04:56 +0200 Subject: [PATCH 5/6] some more comments --- CMakeLists.txt | 6 ++++-- shared.h | 7 ++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 06d035a..8f08180 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,9 +43,11 @@ target_link_libraries( pico_multicore ) -# enable usb output, disable uart output +# disable usb stack pico_enable_stdio_usb(RP2040-Decoder 0) -pico_enable_stdio_uart(RP2040-Decoder 1) + +# disable uart output (to enable put a 1 instead of the 0) +pico_enable_stdio_uart(RP2040-Decoder 0) # create map/bin/hex file etc. #pico_add_extra_outputs(RP2040-Decoder) diff --git a/shared.h b/shared.h index 68fd3ac..dca4270 100644 --- a/shared.h +++ b/shared.h @@ -23,8 +23,8 @@ // 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 0 -//#define LOG(level, x, ...) {if(level <= LOGLEVEL) { printf((x), ##__VA_ARGS__);}} #define LOG(level, ...) { if(level <= LOGLEVEL) {printf(__VA_ARGS__);}} // Constant Value of 125 x 10⁶ @@ -37,8 +37,9 @@ #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 0-5 incl.) as outputs or to switch auxiliary output transistors (GPIO 24-27 incl.) + // 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.) @@ -64,4 +65,4 @@ float measure(uint8_t total_iterations, uint8_t r_side_arr_cutoff, bool direction); uint32_t get_32bit_CV (uint16_t CV_start_index); -uint16_t get_16bit_CV (uint16_t CV_start_index); \ No newline at end of file +uint16_t get_16bit_CV (uint16_t CV_start_index); From 562258e433de411625e2c818ef080ade3b258a4e Mon Sep 17 00:00:00 2001 From: questor/Inter Date: Sat, 27 May 2023 22:41:52 +0200 Subject: [PATCH 6/6] remove ALL logging if not enabled --- core0.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core0.c b/core0.c index 1e3c212..81ee4e6 100644 --- a/core0.c +++ b/core0.c @@ -673,6 +673,6 @@ int main() { 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(0, "core0 done\n"); + LOG(1, "core0 done\n"); while (1); } \ No newline at end of file