- changes to ADC offset adjustment; - changes to output set function; - cv_setup_check() now waits for core1 to launch also after programming

This commit is contained in:
1nct committed 2022-03-06 01:03:17 +01:00
1 parent 65250846f9
commit 330d42a17e
3 files changed
+56 -54

No files matched your search

+19 -18
View File
@@ -142,8 +142,9 @@ uint8_t CV_ARRAY_DEFAULT [CV_ARRAY_SIZE] = {
0b00000000, //CV_110 -
0b00000000, //CV_111 -
///// PWM - Configuration //////////////////////////////////////////////////////////////////////////////////////////////
// This Config can cause conflicts when GPIOs controlled by the same slice are enabled at the same time.
0b00000000, //CV_112 - GPIO 0-7 enable PWM
0b00000100, //CV_113 - GPIO 8-15 enable PWM
0b00111100, //CV_113 - GPIO 8-15 enable PWM
0b00000000, //CV_114 - GPIO 16-23 enable PWM
0b00000000, //CV_115 - GPIO 24-31 enable PWM
//Slice 0
@@ -187,21 +188,21 @@ uint8_t CV_ARRAY_DEFAULT [CV_ARRAY_SIZE] = {
0b00000000, //CV_149 - Channel B Level Bits 0-7
0b00000000, //CV_150 - Channel B Level Bits 8-15
//Slice 5
0b11111111, //CV_151 - Wrap Bits 0-7
0b11111111, //CV_152 - Wrap Bits 8-15
0b10001000, //CV_151 - Wrap Bits 0-7
0b00010011, //CV_152 - Wrap Bits 8-15
0b00000001, //CV_153 - Clock Divider
0b11111111, //CV_154 - Channel A Level Bits 0-7
0b01111111, //CV_155 - Channel A Level Bits 8-15
0b00000000, //CV_156 - Channel B Level Bits 0-7
0b00000000, //CV_157 - Channel B Level Bits 8-15
0b11000100, //CV_154 - Channel A Level Bits 0-7
0b00001001, //CV_155 - Channel A Level Bits 8-15
0b11101000, //CV_156 - Channel B Level Bits 0-7
0b00000011, //CV_157 - Channel B Level Bits 8-15
//Slice 6
0b00000000, //CV_158 - Wrap Bits 0-7
0b00000000, //CV_159 - Wrap Bits 8-15
0b11000100, //CV_158 - Wrap Bits 0-7
0b00001001, //CV_159 - Wrap Bits 8-15
0b00000001, //CV_160 - Clock Divider
0b00000000, //CV_161 - Channel A Level Bits 0-7
0b00000000, //CV_162 - Channel A Level Bits 8-15
0b00000000, //CV_163 - Channel B Level Bits 0-7
0b00000000, //CV_164 - Channel B Level Bits 8-15
0b11100010, //CV_161 - Channel A Level Bits 0-7
0b00000100, //CV_162 - Channel A Level Bits 8-15
0b11100010, //CV_163 - Channel B Level Bits 0-7
0b00000100, //CV_164 - Channel B Level Bits 8-15
//Slice 7
0b00000000, //CV_165 - Wrap Bits 0-7
0b00000000, //CV_166 - Wrap Bits 8-15
@@ -212,12 +213,12 @@ uint8_t CV_ARRAY_DEFAULT [CV_ARRAY_SIZE] = {
0b00000000, //CV_171 - Channel B Level Bits 8-15
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Offset adjustment for ADC //
//If CV_172&CV_173 = 255 there will be a measurement procedure on startup (Note: Bitwise &) //
//If CV_172&CV_173 = 0xFF = 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 //
0b11111111, //CV_172 - ADC offset Default: 255 //
0b11111111, //CV_173 - ADC offset measurement cycles Default: 127 //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
0b01111111, //CV_174 -
0b00000000, //CV_175 -
0b00000000, //CV_176 -
0b00000000, //CV_177 -
@@ -416,7 +417,7 @@ uint8_t CV_ARRAY_DEFAULT [CV_ARRAY_SIZE] = {
0b00000000, //byte 3 - CV_344
//F11 forward
0b00000000, //byte 0 - CV_345
0b00000000, //byte 1 - CV_346
0b00111100, //byte 1 - CV_346
0b00000000, //byte 2 - CV_347
0b00000000, //byte 3 - CV_348
//F11 reverse
+31 -32
View File
@@ -11,6 +11,7 @@ const uint8_t *CV_ARRAY_FLASH = (const uint8_t *) (XIP_BASE + FLASH_TARGET_OFFSE
uint64_t last_bits = 0;
bool reset_package_flag = false;
absolute_time_t falling_edge_time,rising_edge_time;
uint16_t level_table[32] = {0};
uint16_t two_sigma(const uint16_t arr[], uint8_t length){
//Calculate arithmetic average
@@ -41,26 +42,22 @@ uint16_t two_sigma(const uint16_t arr[], uint8_t length){
return sum / counter;
}
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
length = 100;
uint16_t offsets[length];
//Set 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 < length; i++) {
uint16_t fwd = measure(true);
uint16_t rev = measure(false);
offsets[i] = (fwd+rev)/2;
}
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 offsets_fwd_avg = two_sigma(offsets, 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;
@@ -95,7 +92,6 @@ void m_pwm_offset_adjustment(uint8_t length){
CV_ARRAY_TEMP[56] = offsets_rev_avg>>8;
flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE);
flash_range_program(FLASH_TARGET_OFFSET, CV_ARRAY_TEMP, FLASH_PAGE_SIZE * 2);
acknowledge();
}
void acknowledge() {
uint16_t max_lvl = _125M/(CV_ARRAY_FLASH[8]*100+10000);
@@ -122,7 +118,6 @@ void write_cv_byte(uint16_t cv_index, uint8_t cv_data){
if(cv_index == 7 && cv_data == 8){
flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE);
flash_range_program(FLASH_TARGET_OFFSET, CV_ARRAY_DEFAULT, FLASH_PAGE_SIZE * 2);
acknowledge();
}
//Motor PWM offset Adjustment (CV_7; Value = 7);
else if(cv_index == 6 && cv_data == 7){
@@ -130,7 +125,7 @@ void write_cv_byte(uint16_t cv_index, uint8_t cv_data){
}
//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]);
adc_offset_adjustment(100/*CV_ARRAY_FLASH[173]*/);
}
else{
uint8_t CV_ARRAY_TEMP[CV_ARRAY_SIZE];
@@ -142,8 +137,6 @@ void write_cv_byte(uint16_t cv_index, uint8_t cv_data){
}
}
void program_mode(uint8_t number_of_bytes, const uint8_t byte_array[]){
if( !((CV_ARRAY_FLASH[53])+(CV_ARRAY_FLASH[54]<<8)) || !((CV_ARRAY_FLASH[55])+(CV_ARRAY_FLASH[56]<<8)) ){
}
//Check for valid programming command ("address" 112-127)
if (byte_array[number_of_bytes - 1]<128 && byte_array[number_of_bytes - 1]>111){
uint8_t instruction_type_mask = 0b00001100;
@@ -173,6 +166,7 @@ void program_mode(uint8_t number_of_bytes, const uint8_t byte_array[]){
}
restore_interrupts(saved_interrupts);
multicore_launch_core1(core1_entry);
multicore_fifo_pop_blocking(); //wait for core1 launch before proceeding
}
}
uint32_t get_32bit_CV (uint16_t CV_start_index){
@@ -188,9 +182,9 @@ uint16_t get_16bit_CV (uint16_t CV_start_index){
return (byte_0) + (byte_1<<8);
}
void set_outputs(uint32_t active_functions) {
uint32_t slice_mask = 0;
uint32_t outputs_to_set = 0;
uint32_t outputs_to_set_PWM = get_32bit_CV(111);
uint32_t outputs_with_PWM_enabled = get_32bit_CV(111); //get outputs with pwm enabled (bitmask)
uint32_t outputs_to_set_PWM = outputs_with_PWM_enabled;
for (uint8_t i = 0; i < 32; i++) {
if ( (active_functions>>i) & 1 ){
outputs_to_set |= get_32bit_CV(i * 8 + 260 - 4 * target_direction);
@@ -203,12 +197,15 @@ void set_outputs(uint32_t active_functions) {
uint32_t temp_mask = 1;
for (uint8_t i = 0; i < 32; ++i) {
if(temp_mask & outputs_to_set_PWM){
uint32_t slice_num = pwm_gpio_to_slice_num(i);
slice_mask |= (1<<slice_num);
pwm_set_gpio_level(i, level_table[i]);
pwm_gpio_to_channel(i);
pwm_gpio_to_slice_num(i);
}
else if (temp_mask & outputs_with_PWM_enabled){
pwm_set_gpio_level(i,0);
}
temp_mask<<=1;
}
pwm_set_mask_enabled(slice_mask);
}
void update_active_functions(uint32_t function_byte, uint8_t clr_bit_ind) {
static uint32_t active_functions;
@@ -387,7 +384,7 @@ void track_signal_fall(unsigned int gpio, long unsigned int events) {
}
void init_outputs(){
gpio_init_mask(GPIO_ALLOWED_OUTPUTS);
gpio_set_dir_out_masked(GPIO_ALLOWED_OUTPUTS);
gpio_set_dir_out_masked(GPIO_ALLOWED_OUTPUTS); //note: This also disables UART on GPIO0 & GPIO1
uint32_t Outputs_with_PWM_enabled = get_32bit_CV(111);
uint32_t slice, channel;
uint16_t wrap, level;
@@ -400,37 +397,39 @@ void init_outputs(){
level = get_16bit_CV(118 + 7*slice+ 2*channel);
gpio_set_function(i,GPIO_FUNC_PWM);
pwm_set_wrap(slice,wrap);
pwm_set_gpio_level(i,level);
pwm_set_gpio_level(i,0);
level_table[i] = level;
pwm_set_clkdiv_int_frac(slice,CV_ARRAY_FLASH[117+7*slice],0);
pwm_set_enabled(slice,true);
}
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.
multicore_fifo_pop_blocking(); //wait for core1 launch before proceeding
if (CV_ARRAY_FLASH[64]){
uint8_t arr[4] = {125,8,7,114};
uint8_t arr[4] = {125,8,7,124};
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)
if ( CV_ARRAY_FLASH[171] == 0xFF ){
uint8_t arr[4] = {125,0,0,124};
program_mode(4,arr); //ADC offset adjustment (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) {
if(!sum) {
uint8_t arr[4] = {125,7,6,124};
program_mode(4,arr); //Re-setup offset Adjustment (CV_7; Value = 7);
program_mode(4,arr); //Motor PWM offset adjustment (CV_7; Value = 7);
}
}
int main() {
stdio_init_all();
printf("core0 init...\n");
multicore_launch_core1(core1_entry);
cv_setup_check();
init_outputs();
multicore_launch_core1(core1_entry);
gpio_init(DCC_INPUT_PIN);
gpio_set_dir(DCC_INPUT_PIN, GPIO_IN);
gpio_pull_up(DCC_INPUT_PIN);
+6 -4
View File
@@ -35,6 +35,7 @@ typedef struct measure_params{
uint8_t total_iterations;
uint8_t left_side_array_cutoff;
uint8_t right_side_array_cutoff;
uint8_t adc_offset;
}measure_params;
measure_params msr;
@@ -140,7 +141,7 @@ uint32_t offset(){
bool pid_control(struct repeating_timer *t){
if(target_direction != pid.direction_prev || !latest_target) pid.e_sum = 0;
msr.val = measure(target_direction);
pid.e = (int32_t)latest_target - (int32_t)msr.val + ADC_OFFSET;
pid.e = (int32_t)latest_target - (int32_t)msr.val + msr.adc_offset;
pid.e_sum += pid.e;
if (pid.e_sum > pid.sum_limit_max) pid.e_sum = pid.sum_limit_max;
else if (pid.e_sum < pid.sum_limit_min) pid.e_sum = pid.sum_limit_min;
@@ -149,8 +150,8 @@ bool pid_control(struct repeating_timer *t){
if (pid.output < 0 || !latest_target) pid.output = 0;
else if (pid.output > pid.max_output) pid.output = pid.max_output;
adjust_pwm_level(pid.output);
//printf("o: %d\n",pid.output);
//printf("e: %d\n",pid.e);
printf("o: %d\n",pid.output);
printf("e: %d\n",pid.e);
adc_fifo_drain();
pid.direction_prev = target_direction;
pid.e_prev = pid.e;
@@ -161,6 +162,7 @@ void init_pid(){
msr.delay_in_us = 100; /*CV_ARRAY_FLASH[61];*/
msr.left_side_array_cutoff = 15;/*CV_ARRAY_FLASH[62];*/
msr.right_side_array_cutoff = 15;/*CV_ARRAY_FLASH[63];*/
msr.adc_offset = CV_ARRAY_FLASH[171];;
pid.e_sum = 0;
pid.e_prev = 0;
pid.k_p = 2500;
@@ -205,6 +207,6 @@ void core1_entry() {
speed_helper,
NULL,
&speed_helper_timer);
printf("core1 done\n");
multicore_fifo_push_blocking(20); //send some data to let core0 know that core1 is done
while (1);
}