What functions are called and when by the firmware? - page 2 - General Discussion and Assistance - CHDK Forum supplierdeeply

What functions are called and when by the firmware?

  • 20 Replies
  • 9762 Views
Re: What functions are called and when by the firmware?
« Reply #10 on: 16 / August / 2011, 08:15:58 »
Advertisements
Why not put "if (conf.remote_enable)" in the beginning of the "wait_until_remote_button_is_released" function and "if (conf.save_raw)" in the beginning of "capt_seq_hook_raw_here"?   
I assume you are thinking that its best to do that in the C code rather than try to reference CHDK flags from the assembler in capt_seq.c ?

This reminds me that when I was doing my first port, I think I found that the code in gui.c constantly performs at least some of the processing for the histogram OSD function, regardless of whether histograms are enabled or not.  Was busy chasing a different problem at the time so I did not pursue it.

Ported :   A1200    SD940   G10    Powershot N    G16

Re: What functions are called and when by the firmware?
« Reply #11 on: 16 / August / 2011, 09:34:37 »
I assume you are thinking that its best to do that in the C code rather than try to reference CHDK flags from the assembler in capt_seq.c ?

This reminds me that when I was doing my first port, I think I found that the code in gui.c constantly performs at least some of the processing for the histogram OSD function, regardless of whether histograms are enabled or not.  Was busy chasing a different problem at the time so I did not pursue it.
doing it in C would be easy, in fact I've already done it, I don't know how to make a patch though...  As for doing in asm? My knowledge in that area equates to following along with the branches, not understanding whats REALLY happening, but I do imagine it would take less cycles of processing... would it make enough of a difference to bother when it's so easy to do it in C? Nah...

Re: What functions are called and when by the firmware?
« Reply #12 on: 16 / August / 2011, 19:39:20 »
doing it in C would be easy, in fact I've already done it, I don't know how to make a patch though...  As for doing in asm? My knowledge in that area equates to following along with the branches, not understanding whats REALLY happening, but I do imagine it would take less cycles of processing... would it make enough of a difference to bother when it's so easy to do it in C? Nah...
I was thinking more about which file you edit to add the patch - the capt_seq.c or kbd.c&generic/capt_seq.c .  The former essentially requires assembler code while the later two are both in C.

As far as a patch goes,  it looks like someone would have to modify kbd.c for every camera to bypass the remote button stuff. The raw stuff is all in generic/capt_seq.c so only needs one file to be changed.  If you are going to continue to work on CHDK,  you might want to download TortoiseSVN and learn to use is - should only take an hour or two.
Ported :   A1200    SD940   G10    Powershot N    G16

*

Offline reyalp

  • ******
  • 14121
Re: What functions are called and when by the firmware?
« Reply #13 on: 16 / August / 2011, 23:05:05 »
If I'm seeing this right "wait_until_remote_button_is_released" is always called before a capture is taken and  "capt_seq_hook_raw_here" is always called after (but before image processing).

even if you have both those options disabled it looks like a lot of processing is happening anyways, especially for the raw saving function which passes back and forth between threads. Why not put "if (conf.remote_enable)" in the beginning of the "wait_until_remote_button_is_released" function and "if (conf.save_raw)" in the beginning of "capt_seq_hook_raw_here"?   

Even if it's only saving a few fractions of a second this is where we want things as fast as possible.
In the case of the raw hook, it actually does some stuff which may need to be done if raw isn't enabled.  wait_until_remote_button_is_released already pretty much has everything in a big if, although it could certainly stand some cleanup!

Saving a few instructions is totally pointless in this case. Compared to the amount of code the rest of the shot goes through, it's utterly insignificant.
Don't forget what the H stands for.


Re: What functions are called and when by the firmware?
« Reply #14 on: 17 / August / 2011, 00:49:09 »
As far as a patch goes,  it looks like someone would have to modify kbd.c for every camera to bypass the remote button stuff. The raw stuff is all in generic/capt_seq.c so only needs one file to be changed.  If you are going to continue to work on CHDK,  you might want to download TortoiseSVN and learn to use is - should only take an hour or two.
I see what you mean, working outside the platform folder whenever possible makes changes easier to implement (and less assembly!). I'm working on adding the functionality to remap buttons/mode in normal operation (outside alt) - to get that working will require changes in each kbd.c, I'll revamp the remote mess then.

yeah, I need to figure out a 'workflow' with this whole thing. Time a home is very slim at the moment, while at work I cannot install anything or bring in any electronics. But hey, how many people can sit and read a book, or tinker on the net for hours like i do and get paid for it?

In the case of the raw hook, it actually does some stuff which may need to be done if raw isn't enabled. 
Ah, I see "raw_savefile" is where the main chunk of code for this is and besides raw saving there's shot_histogram stuff and bracketing. State_shooting_progress stuff might need to be modified too I can't tell yet, but this still should be a fairly easy patch once i decode how it all works.

As for saving a few instructions, it looks to me the processing is halted while the chdk raw stuff is running. Just passing back and forth between threads there's a a few sleep_tasks to go through. Does it add up to much? Maybe not, but should we not try to streamline the code where fairly easily can?  (its a matter of priorities and available time)

My focus is to make CHDK feel like less of a hack and more integrated into the workings of the camera - a camera carried in my pocket used for snapshots and when a scene catches my eye and I left the rest of my photography kit at home (which is most the time lately).  I want to make my camera work like I want instead of dummied down for the average consumer. Responsiveness is a big part of this. I'm sure I'm not alone in my wants, so I'll try to incorporate my improvements into the trunk instead of veering off into my own custom build.

Re: What functions are called and when by the firmware?
« Reply #15 on: 17 / August / 2011, 04:44:03 »
Ok, I've modified raw.c and /generic/capt_seq.c so it won't break bracketing or shot_histogram. unfortunately I won't be able to do anything with it for a couple of days. so I'll post it here if anyone wants to mess with it.

Here's the changed section in raw.c
Code: [Select]
int raw_savefile() {
    int ret = 0;
    int fd;
    static struct utimbuf t;
    //moved to capt_seq.c
    //static int br_counter;
#if DNG_SUPPORT
    struct t_data_for_exif* exif_data = NULL;
    char *thumbnail_buf = NULL;
    if (conf.dng_raw) exif_data=capture_data_for_exif();
#endif    

    //moved to capt_seq.c
    //if (state_kbd_script_run && shot_histogram_isenabled()) build_shot_histogram();  

    // Get pointers to RAW buffers (will be the same on cameras that don't have two or more buffers)
    char* rawadr = get_raw_image_addr();
    char* altrawadr = get_alt_raw_image_addr();

    // count/save badpixels if requested
    if(raw_init_badpixel_bin()) {
        return 0;
    }

    if (develop_raw) {
        started();
        fd = open(fn, O_RDONLY, 0777);
        if (fd>=0) {
            read(fd, get_raw_image_addr(), hook_raw_size());
            close(fd);
        }
#ifdef OPT_CURVES
        if (conf.curve_enable) curve_apply();
#endif
        finished();
        develop_raw=0;
        return 0;
    }

    if (conf.bad_pixel_removal) patch_bad_pixels();

    /*-----\/\/\/\/ section moved to raw.c \/\/\/\/-----JLT
    shooting_bracketing();

    if(conf.tv_bracket_value || conf.av_bracket_value || conf.iso_bracket_value || conf.subj_dist_bracket_value) {
        if(state_shooting_progress != SHOOTING_PROGRESS_PROCESSING)
            br_counter = 1;
        else
            br_counter++;
    }
    else
        br_counter=0;
    -----/\/\/\/\ section moved to raw.c /\/\/\/\-----JLT */

    // got here second time in a row. Skip second RAW saving.
    if (conf.raw_save_first_only && state_shooting_progress == SHOOTING_PROGRESS_PROCESSING) {
        return 0;
    }

    state_shooting_progress = SHOOTING_PROGRESS_PROCESSING; //kept to keep gui from redrawing while building raw -JLT

    if (is_raw_enabled()) // already checked conf.save_raw before being called -JLT
Below that is unchanged. And changes to capt_seq.c
Code: [Select]
#include "conf.h" //needed now -JLT

void __attribute__((naked,noinline)) capt_seq_hook_raw_here()
{
    asm volatile("STMFD   SP!, {R0-R12,LR}\n");

    //-----\/\/\/\/ section moved from raw.c \/\/\/\/-----JLT
    static int br_counter;

    if (state_kbd_script_run && shot_histogram_isenabled()) build_shot_histogram();
    
    if(conf.tv_bracket_value || conf.av_bracket_value || conf.iso_bracket_value || conf.subj_dist_bracket_value) {      
        shooting_bracketing(); //moved this inside the if statement so it's not called if no bracketing set -JLT
        
        if(state_shooting_progress != SHOOTING_PROGRESS_PROCESSING)
            br_counter = 1;
        else
            br_counter++;
    }
    else
        br_counter=0;
    //-----/\/\/\/\ section moved from raw.c /\/\/\/\-----JLT
    
    if (conf.save_raw) {
        state_shooting_progress = SHOOTING_PROGRESS_PROCESSING;
        #ifdef PAUSE_FOR_FILE_COUNTER
            // Some cameras need a slight delay for the file counter to be updated correctly
            // before raw_savefile tries to get the file name & directory.
            // Add '#define PAUSE_FOR_FILE_COUNTER 100' in the camera firmware capt_seq.c file.
            // The value can be adjusted as needed for different cameras.
            _SleepTask(PAUSE_FOR_FILE_COUNTER);
        #endif
    
        raw_save_stage = RAWDATA_AVAILABLE;
        core_rawdata_available();
        while (raw_save_stage != RAWDATA_SAVED){
        _SleepTask(10);
        }
    }
    
    state_shooting_progress = SHOOTING_PROGRESS_PROCESSING; //copied from raw.c, keep it there too so GUI doesnt redraw while building raw -JLT
    
    asm volatile("LDMFD   SP!, {R0-R12,PC}\n");
}
« Last Edit: 17 / August / 2011, 05:24:28 by geoffire »

*

Offline reyalp

  • ******
  • 14121
Re: What functions are called and when by the firmware?
« Reply #16 on: 17 / August / 2011, 23:41:14 »
As for saving a few instructions, it looks to me the processing is halted while the chdk raw stuff is running. Just passing back and forth between threads there's a a few sleep_tasks to go through. Does it add up to much? Maybe not, but should we not try to streamline the code where fairly easily can?  (its a matter of priorities and available time)
I would suggest determining whether there is an actual problem first. If the changes also make the code clearer and smaller, then it might be worthwhile even if the performance gain is minimal. If they have the opposite effect, then I'd want to see a measurable performance gain before adding it.
Quote
Responsiveness is a big part of this. I'm sure I'm not alone in my wants, so I'll try to incorporate my improvements into the trunk instead of veering off into my own custom build.
Again, if you are worried about responsiveness, the first step is to figure out where a significant delay occurs.

You need to be very careful adding stuff to functions like capt_seq_hook_raw_here. See http://chdk.wikia.com/wiki/CHDK_Coding_Guidelines#Naked_functions

You also moved some bracketing and shot_histogram from spytask to capt_seq_task. Offhand I'd expect that to be OK, but it makes me nervous. If there were subtle side effects that only affected a few of our >100 firmware variants, that would be unfortunate.
Don't forget what the H stands for.

Re: What functions are called and when by the firmware?
« Reply #17 on: 19 / August / 2011, 05:24:49 »
You need to be very careful adding stuff to functions like capt_seq_hook_raw_here. See http://chdk.wikia.com/wiki/CHDK_Coding_Guidelines#Naked_functions

Thanks again reyalp, There's so much I need to learn!  I think what matters here is that "LDMFD   SP!, {R0-R12,PC}\n" restores what "STMFD   SP!, {R0-R12,LR}\n" pushed into the stack.  I'm not sure what C commands play with the stack, but I can look at the dump file after compiling.

looks like The build_shot_histogram and shooting_bracketing do play with the stack (I'm guessing local variables), and I broke renaming the raws for bracketing as now it cannot reach br_counter. I could make everything static and extern, but I don't think that's a good approach.  So much for my easy fix...



*

Offline reyalp

  • ******
  • 14121
Re: What functions are called and when by the firmware?
« Reply #18 on: 20 / August / 2011, 00:06:39 »
You need to be very careful adding stuff to functions like capt_seq_hook_raw_here. See http://chdk.wikia.com/wiki/CHDK_Coding_Guidelines#Naked_functions
Thanks again reyalp, There's so much I need to learn!  I think what matters here is that "LDMFD   SP!, {R0-R12,PC}\n" restores what "STMFD   SP!, {R0-R12,LR}\n" pushed into the stack.  I'm not sure what C commands play with the stack, but I can look at the dump file after compiling.

looks like The build_shot_histogram and shooting_bracketing do play with the stack (I'm guessing local variables),
Normal functions called from a naked function shouldn't need anything special, you just have to worry about the C code you put in capt_seq_hook_raw_here itself.
Don't forget what the H stands for.

Re: What functions are called and when by the firmware?
« Reply #19 on: 23 / August / 2011, 03:46:32 »
Normal functions called from a naked function shouldn't need anything special, you just have to worry about the C code you put in capt_seq_hook_raw_here itself.
Ok, I understand what's happening here now. If i moved everything in capt_seq_hook_raw_here to another function and just called it then it's perfectly safe as when that function returns(with no value) the stack will be in the same state and "LDMFD   SP!, {R0-R12,PC}\n" will restore all the registers.

 still have questions about the tasks. I've dug through the forums and wiki and havn't found very much info. I understand it's "cooperative" multitasking which means the task runs until a "sleeptask" is then the next task can run, but how does it really work? I imagine "sleeptask" essentially is the "thread tasker" (or calls it) that sets a timer for when the task is ran again, then resumes to the first task to have it's timer elapse. While the exact method doesn't really matter i suppose, this sound about right? I Still need to disassemble my firmware and study it... I've been trying to understand whats happening in the assembly in boot.c and capt_seq.c.

exp_drv_task does the live view and metering right?

Also, I've been wondering, why do we need spytask? Couldn't raw processing be done at "capt_seq_hook_raw_here()" and the gui redrawn in kbd_task? The other stuff spytask does (initialization) could be called as functions, why does it need to be another task? I know... if it ain't broke, don't fix it. Just wondering.


 

Related Topics


SimplePortal 2.3.6 © 2008-2014, SimplePortal