Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firmware modification to adapt hardware changes #139

Open
fsaulo opened this issue Aug 29, 2021 · 0 comments
Open

Firmware modification to adapt hardware changes #139

fsaulo opened this issue Aug 29, 2021 · 0 comments

Comments

@fsaulo
Copy link

fsaulo commented Aug 29, 2021

I've modified one of the potentiometers of the direction gimbals (channel 1 or roll). I've managed to find online an almost identical copy of the originals, both are 5k precision pots and have the same measures, but the originals can vary with 60 degrees and the clone with 220 degrees. That makes a huge difference in the stick sensor scale. Is it possible to rescale the stick analog readings to adapt to the new values?

I have been messing with the C code in the last few days but couldn't find a definitive lead on how to make those changes.

I know from source/adc.c that this signal (ch1 roll sensor) is called ADC0_SE7b internally, and is assigned to PORTD6 or pin 63 of the MCU. I kind of found out on how to configure things such as reference voltage, resolution, clock sources etc; using the adc16_config_t config struct and the ADC16_Init function from drivers/fsl_adc16.c. I've also seen a bunch of commented out functions in the source, one specific that seems to tackle this issue, but for some reason it's not supported any longer is int32_t adc_get_channel_rescaled(uint8_t idx). Perhaps formatSensorValue(char* target, int sensorID, uint16_t sensorValue) from source/alt.c is doing something similar? I don't really understand the IBUS_MEAS_TYPE signals but my guess is that the telemetry data is not related with ADC calibration module, so the answer is no?

Other problem that could possibly fit in a different issue, but since this is just a question: every change I make doesn't seems to be reflected on the final build. I have just an idea on how to map the generated bytecode to memory using the linker scripts. I've modified a few variables that most certainly would have an affect on my FlySky, but it didn't change anything. I didn't add any new function nor my build causes memory overlapping issues, so in my understanding, I wouldn't have to edit the memory map, right? Does the fs-i6_updater_*.bin resulted of make all contains the original firmware binaries or do I have to manually combine it? If so, how can I do it?

For building I'm using the GNU GCC ARM Toolchain binaries found on the default RPM repositories provided by Fedora 34 5.13.12-200.fc34.x86_64.

make all seems to return successfully

Invoking: Cross ARM GNU Print Size
arm-none-eabi-size --format=berkeley "fs-i6.elf"
   text	   data	    bss	    dec	    hex	filename
  81117	   2178	   1292	  84587	  14a6b	fs-i6.elf
arm-none-eabi-size --format=sysv "fs-i6.elf" -x
fs-i6.elf  :
section                                     size         addr
.interrupts                              0x10000          0x0
.flash_config                               0x10        0x400
.text                                     0x18b0        0x410
.TestCODE                                  0x6d0       0x1cc0
.notImplemented_AUX_CHANNEL_PAGE             0x2       0x2390
.GFX_ARRAY                                 0x400   0x1ffff8c0
.MOD_CONFIGNG                              0x352   0x20000aaa
.AUX_MEM                                    0x56   0x20000f4a
.mod_25C8_277F                             0x1a7       0x25c8
.mod_SIGNATURE_2780                         0x10       0x2780
.mod_2790_27FF                              0x68       0x2790
.reserved_main                               0xc       0x2800
.mod_configurePins_ASM                       0x4       0xc0d2
.mod_extraChannels_ASM                      0x3c       0xab5c
.reserved_after_code_extraChannels_ASM       0x5       0xab9f
.mod_swB_ASM                                 0x4       0x2896
.mod_swE_ASM                                 0x4       0x289a
.mod_displaySensors1_ASM                     0x4       0xc1d8
.mod_displaySensors2_ASM                     0x4       0xc5c2
.mod_parseAcFrame_ASM                        0x4       0x65ae
.mod_getSensorName_ASM                       0x4       0x2fa0
.mod_getSensorName2_ASM                      0x4       0x376a
.mod_loadSettings_ASM                        0x4       0xc070
.mod_printTimer_ASM                          0x4       0xc1de
.mod_printTimer1_ASM                         0x4       0xc1f4
.mod_printTimer2_ASM                         0x4       0xc5e2
.mod_formatSensorValue_ASM                   0x4       0x5418
.mod_formatSensorValue2_ASM                  0x4       0xc236
.mod_callCreatePacket1_ASM                   0x4       0xa8d2
.mod_callCreatePacket2_ASM                   0x4       0xa8e8
.mod_mainScreenIndexAddress                  0x4       0xc9a0
.mod_timerValueAddress                       0x4       0xc9a4
.mod_timerBufferAddress                      0x4       0xc9a8
.mod_tx_voltage_alarm_address                0x4       0x6344
.mod_extra_menu_str_ptr                      0x4       0xd4c8
.mod_extra_menu_ptr                          0x4       0xd4cc
.mod_version                                 0x6       0xeeb5
.mod_rxMenuSpeedDistStr                      0xa       0xd2ee
.mod_code_AC34                             0x15d       0xac34
.reserved_after_code_AC34                    0x4       0xae44
.mod_code_9A60                             0x14c       0x9a60
.reserved_after_code_9A60                    0x4       0x9bc0
.mod_code_9830                              0x24       0x9830
.reserved_after_code_9830                    0x4       0x9928
.mod_code_5174_5353                        0x1a8       0x5174
.reserved_after_code_5174_5353               0x4       0x5354
.mod_595C                                  0x198       0x595c
.reserved_after_code_595C                    0x4       0x5b28
.mod_code_65C8_68A7                        0x2b4       0x65c8
.reserved_after_code_65C8_68A7               0x4       0x68a8
.reserved_after_code_in_display_method       0xe       0xc306
.mod_code_C9B0_CA4F                         0x94       0xc9b0
.reserved_after_code_C9B0_CA4F               0x4       0xca50
.mod_code_D510_D5EF                         0x34       0xd50c
.mod_code_D560_D5EF                         0x88       0xd560
.reserved_after_code_D50C_D5EF               0x4       0xd5f0
.mod_code_E140_E754                        0x5ec       0xe140
.reserved_after_code_E140_E754               0x4       0xe754
.mod_voltTelemetry_ASM                      0x14       0x57da
.reserved_after_voltTelemetry_ASM            0x6       0x57ee
.mod_code_F4D0_FF8F                        0xa91       0xf4d0
.reserved_checksum                           0x3       0xfffd
.ARM                                         0x8       0x2394
.init_array                                  0x4       0x239c
.fini_array                                  0x4       0x23a0
.mtb                                         0x0   0x20000fa0
.interrupts_ram                              0x0   0x20000fa0
.data                                       0x64   0x20000fa0
.bss                                       0x208   0x20001004
.heap                                      0x204   0x2000120c
.stack                                     0x100   0x20001410
.ARM.attributes                             0x28          0x0
.comment                                    0x46          0x0
.debug_line                               0xfeba          0x0
.debug_info                              0x1e77d          0x0
.debug_abbrev                             0x5397          0x0
.debug_aranges                            0x10a8          0x0
.debug_str                                0x7a02          0x0
.debug_loc                               0x13bba          0x0
.debug_ranges                             0x1b40          0x0
.debug_frame                              0x2308          0x0
Total                                    0x68953


Finished building: fs-i6.siz

I couldn't compile exactly 1.7.6 release source code (probably related with #121 but 863819f seems to address this issue) to compare against my modified build. So I did the following:

  1. Downloaded the latest source code available (parent 863819f)
  2. Compiled using make all and generated fs-i6_updater_08_29_15_37.bin & copied it to a different location
  3. Made a few changes in the source code:

I've changed lines 68-89 of adc_init_internal(void) from source/adc.c

void adc_init_internal(void){
...    
adc16_config_t config = {
        kADC16_ReferenceVoltageSourceVref,
        kADC16_ClockSourceAlt0,
        false,
        kADC16_ClockDivider2,
        kADC16_ResolutionSE12Bit,
        kADC16_LongSampleCycle24,
        true,
        false,
        true //not sure
    };

    //ADC16_SetChannelMuxMode(ADC0, kADC16_ChannelMuxB);
    ADC0->CFG1 = 52;
    ADC0->CFG2 = 22;
    ADC0->SC2 = 0;
    //ADC0->SC3 = 133;
    ADC0->SC3 = 5;
    ADC0->MG = 0x500;
    // ADC0->PG = 100;
    ADC16_Init(ADC0, &config);
    ADC16_DoAutoCalibration(ADC0);
}
  1. Removed all generated files using rm -rf fs-i6_*.bin and make clean
  2. Rebuilt with make all and copied fs-i6_updater_08_29_15_38.bin to a different location
  3. Dumped the hex of fs-i6_updater_08_29_15_37.bin and fs-i6_updater_08_29_15_38.bin using xxd
  4. Compared the dumped hex using diff i6_updater_08_29_15_37.hex i6_updater_08_29_15_38.hex

Expected behavior:
Different bytecode of section 0x0C24 (adc_init_internal according to build/fs-i6.map) of the dumped hex

Observed results:
Even though they have different checksum:

a8700c18506eeebf7b90c7f90df8415b2f9a3c0c  fs-i6_updater_08_29_15_37.bin
a60c024d12a71c0d39cc85df611c6df80ff63f25  fs-i6_updater_08_29_15_38.bin

They are virtually the same binary, except for the building date and the last 2 bytes (maybe a checksum?). See from diff:

3437c3437
< 0000d6c0: 3239 2f30 382f 3231 2031 353a 3337 0000  29/08/21 15:37..
---
> 0000d6c0: 3239 2f30 382f 3231 2031 353a 3338 0000  29/08/21 15:38..
3712c3712
< 0000e7f0: 0000 0000 0000 0000 0000 0000 0084 39    ..............9
---
> 0000e7f0: 0000 0000 0000 0000 0000 0000 0040 97    .............@.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant