- improved PIN_MASK definitions; -implemented adc offset adjustment; - implemented startup checks for: factory flash setting, adc offset adjustment, motor offset adjustment; - implemented adc_offset CVs; - implemented CV_65 as indication for flash factory condition; -fixed core0.h

This commit is contained in:
1nct committed 2022-03-05 14:46:48 +01:00
1 parent a6fa9ef249
commit 65250846f9
4 files changed
+128 -70

No files matched your search

+28 -23
View File
@@ -9,27 +9,27 @@
// - V_max/V_min & V_emf
// -
uint8_t CV_ARRAY_DEFAULT [CV_ARRAY_SIZE] = {
0b00000011, //CV_1 - Basic Address
0b00000000, //CV_2 - Minimum Speed
0b00000011, //CV_1 - Basic address CV_1 = 0 is not allowed and used for initiating ADC offset adjustment
0b00000000, //CV_2 - Minimum speed
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 0 == Fastest dec/acc rate; 255 == slowest;
0b00000000, //CV_3 - Acceleration Rate - CV_3*CV_59 = Time for one discrete speed step change in ms
0b00000000, //CV_4 - Deceleration Rate - CV_4*CV_59 = Time for one discrete speed step change in ms
0b00000000, //CV_3 - Acceleration rate - CV_3*CV_59 = Time for one discrete speed step change in ms
0b00000000, //CV_4 - Deceleration rate - CV_4*CV_59 = Time for one discrete speed step change in ms
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
0b11111111, //CV_5 - V_max
0b01111111, //CV_6 - V_mid
0b00000001, //CV_7 - Version No.
0b00001101, //CV_8 - Manufacturer (13 = Public Domain & Do-It-Yourself Decoders)
0b10010110, //CV_9 - PWM Frequency in Hz = CV_9*100+10000 - Default = (150*100+10000)Hz = 25kHz
0b00000001, //CV_7 - Version no. (read-only)
0b00001101, //CV_8 - Manufacturer (13 = Public Domain & Do-It-Yourself Decoders) (read-only)
0b10010110, //CV_9 - PWM frequency in Hz = CV_9*100+10000 - Default = (150*100+10000)Hz = 25kHz
0b00000001, //CV_10 -
0b11111111, //CV_11 - Packet Timeout in seconds
0b11111111, //CV_11 - Packet timeout in seconds
0b00000100, //CV_12 - Permitted operating modes
0b00000000, //CV_13 -
0b00000000, //CV_14 -
0b00000000, //CV_15 -
0b00000000, //CV_16 -
0b11000011, //CV_17 - Extended/Long 14-Bit Address (Bits 8 to 13)
0b11101000, //CV_18 - Extended/Long 14-Bit Address (Bits 0 to 7)
0b11000011, //CV_17 - Extended/Long 14-Bit address (Bits 8 to 13)
0b11101000, //CV_18 - Extended/Long 14-Bit address (Bits 0 to 7)
0b00000000, //CV_19 -
0b00000000, //CV_20 -
0b00000000, //CV_21 -
@@ -47,8 +47,8 @@ uint8_t CV_ARRAY_DEFAULT [CV_ARRAY_SIZE] = {
0b00000000, //CV_29 - Decoder Configuration
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
0b00000000, //CV_30 -
0b00010000, //CV_31 - Extended CV Pointer Bits 8-15
0b00000000, //CV_32 - Extended CV Pointer Bits 0-7
0b00010000, //CV_31 - Extended CV pointer Bits 8-15
0b00000000, //CV_32 - Extended CV pointer Bits 0-7
0b00000000, //CV_33 -
0b00000000, //CV_34 -
0b00000000, //CV_35 -
@@ -63,7 +63,7 @@ uint8_t CV_ARRAY_DEFAULT [CV_ARRAY_SIZE] = {
0b00000000, //CV_44 -
0b00000000, //CV_45 -
0b00000000, //CV_46 -
0b00000001, //CV_47 - Additional Motor-PWM Clock Divider.
0b00000001, //CV_47 - Additional motor-PWM clock divider.
// PID - Configuration //////////////////////////////////////////////////////////////////////////////////////////////
0b00000101, //CV_48 - PID Control Sampling Time t_s in ms ≙ Duration of 1 PID-Cycle //
0b01110011, //CV_49 - PID Control P_Factor ≙ CV_49/256 i.e. Default = 115 -> 0.4492 //
@@ -74,12 +74,13 @@ uint8_t CV_ARRAY_DEFAULT [CV_ARRAY_SIZE] = {
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//This Offset is used to establish a "baseline" motor PWM duty cycle at which the motor starts to move //
//If CV_54+CV_55+CV_56+CV_57 = 0 (after reset or manual overwrite) there will be a measurement procedure on startup//
//alternatively, the measurement can be done by writing a 7 to CV_7 on the programming track. //
0b00000000, //CV_54 - Forward Direction Offset Bits 0-7 Default = 0 //
0b00000000, //CV_55 - Forward Direction Offset Bits 8-15 Default = 0 //
0b00000000, //CV_56 - Reverse Direction Offset Bits 0-7 Default = 0 //
0b00000000, //CV_57 - Reverse Direction Offset Bits 8-15 Default = 0 //
0b00001000, //CV_58 - Offset Measurement Cycles Default = 8 //
//alternatively, the measurement can be done by writing a 7 to CV_7 on the programming track, although you should //
//keep in mind, that the programming track might limit current. //
0b00000000, //CV_54 - Forward direction offset bits 0-7 Default = 0 //
0b00000000, //CV_55 - Forward direction offset bits 8-15 Default = 0 //
0b00000000, //CV_56 - Reverse direction offset bits 0-7 Default = 0 //
0b00000000, //CV_57 - Reverse direction offset bits 8-15 Default = 0 //
0b00001000, //CV_58 - Offset measurement cycles Default = 8 //
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
0b00000111, //CV_59 - speed_helper timer delay - can be used to adjust accel/decel rate //
0b00010100, //CV_60 - PWM scaling factor (target EMF Voltage) -> 126*CV_60 = end_target|max //
@@ -93,7 +94,7 @@ uint8_t CV_ARRAY_DEFAULT [CV_ARRAY_SIZE] = {
0b00001111, //CV_63 - left_side_array_cutoff - Default = 15 //
0b00001111, //CV_64 - right_side_array_cutoff - Default = 15 // //
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
0b00000000, //CV_65 -
0b00000000, //CV_65 - Values > 0 indicate that flash is still in factory condition
0b00000000, //CV_66 -
0b00000000, //CV_67 -
0b00000000, //CV_68 -
@@ -210,9 +211,13 @@ uint8_t CV_ARRAY_DEFAULT [CV_ARRAY_SIZE] = {
0b00000000, //CV_170 - Channel B Level Bits 0-7
0b00000000, //CV_171 - Channel B Level Bits 8-15
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
0b00000000, //CV_172 -
0b00000000, //CV_173 -
0b00000000, //CV_174 -
//Offset adjustment for ADC //
//If CV_172&CV_173 = 255 there will be a measurement procedure on startup (Note: Bitwise &) //
//alternatively, the measurement can be done by writing a 0 to CV_1 on the programming track //
0b11111111, //CV_172 - ADC offset - forward direction (FWD_V_EMF_ADC_PIN) Default: 255 //
0b11111111, //CV_173 - ADC offset - reverse direction (REV_V_EMF_ADC_PIN) Default: 255 //
0b01111111, //CV_174 - ADC offset measurement cycles Default: 127 //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
0b00000000, //CV_175 -
0b00000000, //CV_176 -
0b00000000, //CV_177 -
+77 -35
View File
@@ -12,17 +12,6 @@ uint64_t last_bits = 0;
bool reset_package_flag = false;
absolute_time_t falling_edge_time,rising_edge_time;
uint16_t find_offset(uint16_t level, uint8_t step, uint8_t delay, uint32_t threshold, bool direction){
pwm_set_gpio_level(!direction,0);
uint8_t gpio = MOTOR_FWD_PIN * direction + MOTOR_REV_PIN * !direction;
while( measure(direction) < threshold ){
pwm_set_gpio_level(gpio,level);
busy_wait_ms(delay);
level += step;
}
pwm_set_gpio_level(direction,0);
return level;
}
uint16_t two_sigma(const uint16_t arr[], uint8_t length){
//Calculate arithmetic average
uint32_t sum = 0;
@@ -51,14 +40,49 @@ uint16_t two_sigma(const uint16_t arr[], uint8_t length){
//Calculate and return "new" arithmetic average from "filtered" array
return sum / counter;
}
void setup_offsets(uint8_t length){
void adc_offset_adjustment(uint8_t length){
uint16_t offsets_fwd[length];
uint16_t offsets_rev[length];
//Set Motor PWM level to 0 just for good measure
pwm_set_gpio_level(MOTOR_FWD_PIN, 0);
pwm_set_gpio_level(MOTOR_REV_PIN, 0);
for (uint8_t i = 0; i < 100; i++) {
offsets_fwd[i] = measure(true);
}
for (uint8_t i = 0; i < 100; i++) {
offsets_rev[i] = measure(false);
}
uint8_t offsets_fwd_avg = two_sigma(offsets_fwd, length);
uint8_t offsets_rev_avg = two_sigma(offsets_rev, length);
uint8_t CV_ARRAY_TEMP[CV_ARRAY_SIZE];
memcpy(CV_ARRAY_TEMP, CV_ARRAY_FLASH, sizeof(CV_ARRAY_TEMP));
CV_ARRAY_TEMP[171] = offsets_fwd_avg;
CV_ARRAY_TEMP[172] = offsets_rev_avg;
flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE);
flash_range_program(FLASH_TARGET_OFFSET, CV_ARRAY_TEMP, FLASH_PAGE_SIZE * 2);
acknowledge();
}
uint16_t m_pwm_find_offset(uint16_t level, uint8_t step, uint8_t delay, uint32_t threshold, bool direction){
uint8_t gpio = MOTOR_FWD_PIN * direction + MOTOR_REV_PIN * !direction;
pwm_set_gpio_level(MOTOR_FWD_PIN,0);
pwm_set_gpio_level(MOTOR_REV_PIN,0);
while( measure(direction) < threshold ){
pwm_set_gpio_level(gpio,level);
busy_wait_ms(delay);
level += step;
}
pwm_set_gpio_level(MOTOR_FWD_PIN,0);
pwm_set_gpio_level(MOTOR_REV_PIN,0);
return level;
}
void m_pwm_offset_adjustment(uint8_t length){
uint16_t offsets_fwd[length];
uint16_t offsets_rev[length];
uint16_t max_lvl = _125M/(CV_ARRAY_FLASH[8]*100+10000);
for (uint8_t i = 0; i < length; ++i) {
offsets_fwd[i] = find_offset(0,max_lvl/3000,1,30,true);
offsets_fwd[i] = m_pwm_find_offset(0, max_lvl / 3000, 1, 30, true);
busy_wait_ms(500);
offsets_rev[i] = find_offset(0,max_lvl/3000,1,30,false);
offsets_rev[i] = m_pwm_find_offset(0, max_lvl / 3000, 1, 30, false);
busy_wait_ms(500);
}
uint16_t offsets_fwd_avg = two_sigma(offsets_fwd, length);
@@ -100,12 +124,14 @@ void write_cv_byte(uint16_t cv_index, uint8_t cv_data){
flash_range_program(FLASH_TARGET_OFFSET, CV_ARRAY_DEFAULT, FLASH_PAGE_SIZE * 2);
acknowledge();
}
//Re-setup offset Adjustment (CV_7; Value = 7);
//Motor PWM offset Adjustment (CV_7; Value = 7);
else if(cv_index == 6 && cv_data == 7){
setup_offsets(CV_ARRAY_FLASH[57]);
m_pwm_offset_adjustment(CV_ARRAY_FLASH[57]);
}
//CV_1 = 0 is not permitted, therefore this is used to start the ADC offset adjustment routine
else if( !( cv_index&cv_data) ){
adc_offset_adjustment(CV_ARRAY_FLASH[173]);
}
//CV_1 = 0 is not permitted
else if(!cv_index && !cv_data);
else{
uint8_t CV_ARRAY_TEMP[CV_ARRAY_SIZE];
memcpy(CV_ARRAY_TEMP, CV_ARRAY_FLASH, sizeof(CV_ARRAY_TEMP));
@@ -163,20 +189,20 @@ uint16_t get_16bit_CV (uint16_t CV_start_index){
}
void set_outputs(uint32_t active_functions) {
uint32_t slice_mask = 0;
uint32_t outputs_to_enable = 0;
uint32_t outputs_to_enable_PWM = get_32bit_CV(111);
uint32_t outputs_to_set = 0;
uint32_t outputs_to_set_PWM = get_32bit_CV(111);
for (uint8_t i = 0; i < 32; i++) {
if ( (active_functions>>i) & 1 ){
outputs_to_enable |= get_32bit_CV(i * 8 + 260 - 4 * target_direction);
outputs_to_set |= get_32bit_CV(i * 8 + 260 - 4 * target_direction);
}
}
outputs_to_enable &= GPIO_ALLOWED; // Prevent illegal GPIO
outputs_to_enable_PWM &= outputs_to_enable; // Outputs with PWM to be enabled
outputs_to_enable &= ~outputs_to_enable_PWM; // Outputs without PWM to be enabled
gpio_put_masked(GPIO_ALLOWED, outputs_to_enable);
outputs_to_set &= GPIO_ALLOWED_OUTPUTS; // Prevent illegal GPIO
outputs_to_set_PWM &= outputs_to_set; // Outputs with PWM to be enabled
outputs_to_set &= ~outputs_to_set_PWM; // Outputs without PWM to be enabled
gpio_put_masked(GPIO_ALLOWED_OUTPUTS, outputs_to_set);
uint32_t temp_mask = 1;
for (uint8_t i = 0; i < 32; ++i) {
if(temp_mask & outputs_to_enable_PWM){
if(temp_mask & outputs_to_set_PWM){
uint32_t slice_num = pwm_gpio_to_slice_num(i);
slice_mask |= (1<<slice_num);
}
@@ -360,13 +386,14 @@ void track_signal_fall(unsigned int gpio, long unsigned int events) {
gpio_set_irq_enabled_with_callback(DCC_INPUT_PIN, GPIO_IRQ_EDGE_RISE, true, &track_signal_rise);
}
void init_outputs(){
gpio_init_mask(GPIO_ALLOWED);
gpio_set_dir_out_masked(GPIO_ALLOWED);
gpio_init_mask(GPIO_ALLOWED_OUTPUTS);
gpio_set_dir_out_masked(GPIO_ALLOWED_OUTPUTS);
uint32_t Outputs_with_PWM_enabled = get_32bit_CV(111);
uint32_t slice, channel;
uint16_t wrap, level;
uint32_t temp_mask = 1;
for (uint8_t i = 0; i < 32; ++i) {
if ( (Outputs_with_PWM_enabled>>i) & 1 ){
if (Outputs_with_PWM_enabled&temp_mask){
slice = pwm_gpio_to_slice_num(i);
channel = pwm_gpio_to_channel(i); //0 for A, 1 for B ?
wrap = get_16bit_CV(115 + slice*7);
@@ -376,18 +403,33 @@ void init_outputs(){
pwm_set_gpio_level(i,level);
pwm_set_clkdiv_int_frac(slice,CV_ARRAY_FLASH[117+7*slice],0);
}
temp_mask<<=1;
}
}
void cv_setup_check(){
//Check for flash factory setting and set CV_FLASH_ARRAY to default values when factory condition is found.
if (CV_ARRAY_FLASH[64]){
uint8_t arr[4] = {125,8,7,114};
program_mode(4,arr); //reset to CV_ARRAY_DEFAULT (CV_8; Value = 8)
}
//Check for adc offset setup
if ( (CV_ARRAY_FLASH[171] & CV_ARRAY_FLASH[172]) == 0xFF ){
//adc offset routine...
uint8_t arr[4] = {125,0,0,125};
program_mode(4,arr); //reset to CV_ARRAY_DEFAULT (CV_1; Value = 0)
}
//Check for motor duty cycle offset setup
uint sum = (CV_ARRAY_FLASH[53]) + (CV_ARRAY_FLASH[54]) + (CV_ARRAY_FLASH[55]) + (CV_ARRAY_FLASH[56]);
if( !sum && target_speed_step != 128 && target_speed_step) {
uint8_t arr[4] = {125,7,6,124};
program_mode(4,arr); //Re-setup offset Adjustment (CV_7; Value = 7);
}
}
int main() {
stdio_init_all();
printf("core0 init...\n");
cv_setup_check();
init_outputs();
//Check for offset setup
uint sum = (CV_ARRAY_FLASH[53]) + (CV_ARRAY_FLASH[54]) + (CV_ARRAY_FLASH[55]) + (CV_ARRAY_FLASH[56]);
if( !sum && target_speed_step != 128 && target_speed_step) {
uint8_t arr[4] = {125,7,6,124};
program_mode(4,arr);
}
multicore_launch_core1(core1_entry);
gpio_init(DCC_INPUT_PIN);
gpio_set_dir(DCC_INPUT_PIN, GPIO_IN);
+13 -9
View File
@@ -9,7 +9,6 @@
#include "CV.h"
#include "pico/float.h"
#define SIZE_BYTE_ARRAY 5
#define SIZE_ACTIVE_FUNCTIONS 32
#define PACKAGE_3_BYTES 0b11111111110000000000000000000000000001
#define PACKAGE_MASK_3_BYTES 0b11111111111000000001000000001000000001
#define PACKAGE_4_BYTES 0b11111111110000000000000000000000000000000000001
@@ -24,24 +23,29 @@ const uint32_t clr_bit_arr[6] = {
0b11100000000111111111111111111111, // Clear Bits 21-28 (F21-F28)
0b00011111111111111111111111111111, // Clear Bits 29-31 (F29-F31)
};
int8_t check_for_package();
void writeLastBit(bool bit) ;
void bits_to_byte_array(int8_t number_of_bytes,uint8_t byte_array[]) ;
uint16_t two_sigma(const uint16_t arr[], uint8_t length);
void adc_offset_adjustment(uint8_t length);
uint16_t m_pwm_find_offset(uint16_t level, uint8_t step, uint8_t delay, uint32_t threshold, bool direction);
void m_pwm_offset_adjustment(uint8_t length);
void acknowledge();
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 write_cv_byte(uint16_t cv_index, uint8_t cv_data);
bool reset_package_check(uint8_t number_of_bytes,const uint8_t byte_array[]);
void program_mode(uint8_t number_of_bytes, const uint8_t byte_array[]);
uint32_t get_32bit_CV (uint16_t CV_start_index);
uint16_t get_16bit_CV (uint16_t CV_start_index);
void set_outputs(uint32_t active_functions);
void update_active_functions(uint32_t byte, uint8_t clr_bit_ind) ;
bool error_detection(int8_t number_of_bytes, const uint8_t byte_array[]) ;
bool is_long_address(uint8_t number_of_bytes, const uint8_t byte_array[]) ;
bool address_evaluation(uint8_t number_of_bytes,const uint8_t byte_array[]) ;
void instruction_evaluation(uint8_t number_of_bytes,const uint8_t byte_array[]);
bool reset_package_check(uint8_t number_of_bytes,const uint8_t byte_array[]);
int8_t check_for_package();
void writeLastBit(bool bit) ;
void bits_to_byte_array(int8_t number_of_bytes,uint8_t byte_array[]) ;
void evaluation();
void track_signal_rise(unsigned int gpio, long unsigned int events);
void track_signal_fall(unsigned int gpio, long unsigned int events);
void evaluation();
uint16_t find_offset(uint16_t level, uint8_t step, uint8_t delay, uint32_t threshold, bool direction);
uint16_t two_sigma(const uint16_t arr[], uint8_t length);
void setup_offsets(uint8_t length);
void init_outputs();
void cv_setup_check();
+10 -3
View File
@@ -21,9 +21,16 @@
#define MOTOR_REV_PIN 23u
#define FWD_V_EMF_ADC_PIN 26u
#define REV_V_EMF_ADC_PIN 27u
#define GPIO_USED 0b00001100110000000011111000011111u
#define GPIO_ILLEGAL (DCC_INPUT_PIN | MOTOR_FWD_PIN | MOTOR_REV_PIN | FWD_V_EMF_ADC_PIN | REV_V_EMF_ADC_PIN)
#define GPIO_ALLOWED GPIO_USED & ~GPIO_ILLEGAL //GPIO mask to prevent setting illegal GPIOs (ADC, Motor, DCC Input Pin)
//Aux output pins
#define AUX_PIN_MASK 0b00000000000000000011110000000000
//GPIO pins configured as inputs (Input functionality not implemented yet)
#define GPIO_INPUT_MASK 0b00000000000000000000000000000000
//GPIO pins configured as outputs
#define GPIO_OUTPUT_MASK 0b00000000000000000000000000111111
//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_MASK|AUX_PIN_MASK) & ~(GPIO_ILLEGAL_MASK)
#define FLASH_TARGET_OFFSET (PICO_FLASH_SIZE_BYTES-FLASH_SECTOR_SIZE) //This Offset should be far enough away from program data
extern repeating_timer_t pid_control_timer;
extern repeating_timer_t speed_helper_timer;