584,833 active members*
5,892 visitors online*
Register for free
Login
IndustryArena Forum > Machine Controllers Software and Solutions > Dynomotion/Kflop/Kanalog > Programming KFlop in C. Tiny C compiler. Development tools.
Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2013
    Posts
    12

    Question Programming KFlop in C. Tiny C compiler. Development tools.

    Hello Tom,

    Apart of the material support (I bought KFlop & KStep) I want to say thank you and your team for what you are doing.

    I'm embedded software developer and want to start this thread for everyone who is interested in the questions related to TCC compiler, programming techniques, software threads and their colaboration. Of course, all this should be related to KFlop board and processor. I hope this will create many technical discussions, so, good programming knowledge is required.

    To begin, I have some practical questions for Tom:

    1. Can we have an external linkage for our user C code files similar to such when I call the functions from KFlop library like MoveAtVel(), fast_abs(), WaitNextTimeSlice() etc?
    What I could not solve myself was linking from one file to the functions implemented in another module (file). This is available in standard C-language model.

    2. Having existing model of threads it whould be helpful to add at least one input parameter to the KMotionCNC interface when executing user programs (threads). This parameter would be an input to the tread. Often it can be more efficient (less code and faster) to run the same thread with different input parameters than loading each time to KFlop a new piece of code even though very similar to what was executed just before. Similar thing already exists for I/O, DAC options.

    3. I would add more flexibility for user threads, inluding ability to create/assign them dynamically plus more options to synch between them.
    Now we have only functions to capture and release mutex object. By the way, what should be the initial state of "yet free" mutex? (I guess zero from my experiments).

    Best regards,
    Alex

  2. #2
    Join Date
    Nov 2013
    Posts
    12

    Re: Programming KFlop in C. Tiny C compiler. Development tools.

    Since I could not find any mechanism of libraries or modules for the user programs, I started to use a client-server architecture, when one of the user threads is the server, which starts automatically when the KFlop is powered. The other threads use shared memory technic to send the commands to the server and get them executed. This really minimized the size of client threads to few hundred bytes...

    But there is another question about TCC:
    - What is added to each compiled user program? The output file is ~28K, while the TCC compiler says it should be only 160 bytes!?

    - Does anyone know if the user threads are isolated from each other (running in the protected memory) or not? May I call some function code of one thread from the other thread?

    - Ok, I found that threads are running in the common address space, not protected from each other. This is good news for me to organize co-work between threads. We can "export" functions from one thread to another while keeping local thread's structures not exposed, same as local stack data of the functions.

  3. #3
    Join Date
    May 2006
    Posts
    4043

    Re: Programming KFlop in C. Tiny C compiler. Development tools.

    Hi Alexa Flame,

    Can we have an external linkage for our user C code files similar to such when I call the functions from KFlop library like MoveAtVel(), fast_abs(), WaitNextTimeSlice() etc?
    What I could not solve myself was linking from one file to the functions implemented in another module (file). This is available in standard C-language model.
    Linking together multiple C Files is a bit beyond the intended use of typical simple C Programs. A simpler method to use multiple files is just to use #include to combine and compile the files together. We recently provided an example how to link two files together. Here are two simple C files and a header:

    file1.c
    Code:
    #include "KMotionDef.h"
    #include "file2.h"
    
    int main(void)
    {
    	printf("Called do_something at time %f\r\n",Time_sec());	
    	do_something();
    }
    file2.c
    Code:
    #include "KMotionDef.h"
    
    void do_something(void)
    {
    	printf("Called do_something at time %f\r\n",Time_sec());
    }
    file2.h
    Code:
    #ifndef FILE2_H
    #define FILE2_H
    
    void do_something(void);
    
    #endif
    Here are the command line options used for TCC67. BTW hex 80050000 is the address of Thread 1 memory in KFLOP (see PC_DSP.h)
    Code:
     -text 80050000 -g -nostdinc -I "c:\KMotionSrc\DSP_KFLOP" -I "c:\KMotionSrc\C Programs" -o "c:\KMotionSrc\C Programs\file1(1).out" "c:\KMotionSrc\C Programs\file1.c" "c:\KMotionSrc\C Programs\file2.c" "c:\KMotionSrc\DSP_KFLOP\DSPKFLOP.out"
    This creates the file c:\KMotionSrc\C Programs\file1(1).out

    I then go to KMotion.exe c Programs Screen and load c:\KMotionSrc\C Programs\file1.c into Thread #1

    Then select Download and Run (do NOT select compile)

    Console shows:

    Called do_something at time 187.922084
    Called do_something at time 187.922332



    Another option is to use the Texas Instruments Compiler. We had a discussion in the Yahoo Group recently. See Here:

    And the video




    Having existing model of threads it whould be helpful to add at least one input parameter to the KMotionCNC interface when executing user programs (threads). This parameter would be an input to the tread. Often it can be more efficient (less code and faster) to run the same thread with different input parameters than loading each time to KFlop a new piece of code even though very similar to what was executed just before. Similar thing already exists for I/O, DAC options.
    The MCode Number (or Button Action index) is stored in the Specified UserData Variable. So you could use that as a Key to have the same C Program do slightly different things.



    I would add more flexibility for user threads, inluding ability to create/assign them dynamically plus more options to synch between them.
    Now we have only functions to capture and release mutex object. By the way, what should be the initial state of "yet free" mutex? (I guess zero from my experiments).
    We try to keep things as simple and fast as possible. Yes 0 is the available state for the mutex.

    What is added to each compiled user program? The output file is ~28K, while the TCC compiler says it should be only 160 bytes!?
    There is symbol table and debug info included in the binary file. That data is not downloaded to KFLOP.

    Does anyone know if the user threads are isolated from each other (running in the protected memory) or not? May I call some function code of one thread from the other thread?
    All KFLOPs memory is globally accessible. You can use the 200 persist.UserData variables to pass addresses of functions or data structures between Threads.

    HTH
    Regards
    TK
    http://dynomotion.com

  4. #4
    Join Date
    Nov 2013
    Posts
    12

    Re: Programming KFlop in C. Tiny C compiler. Development tools.

    Hello Tom,

    Quote Originally Posted by TomKerekes View Post
    Here are the command line options used for TCC67.
    I did not know this. This will be an option when I can't live simple anymore.

    Quote Originally Posted by TomKerekes View Post
    Another option is to use the Texas Instruments Compiler. We had a discussion in the Yahoo Group recently. See Here:
    Also interesting, but not sure if I can use TI's compiler for free.

    Quote Originally Posted by TomKerekes View Post
    The MCode Number (or Button Action index) is stored in the Specified UserData Variable. So you could use that as a Key to have the same C Program do slightly different things.
    Good idea! I'll look it if it can fit my design.



    Now my design uses a command server in one thread, which is laoded and started automatically on KFlop power up. Then it exports his command function API for all the other threads using persistent data area (as the one I can be sure not used or corrupted arbitrarily):
    Code:
    //===========================================================================
    // Exposed server API for the client threads.
    //===========================================================================
    typedef S4 T_SrvAPI (S4 CmdID, S4 Input);
    
    //===========================================================================
    // The address of server's API is exported by the server when it starts up.
    //===========================================================================
    T_SrvAPI ** SrvAPI = (T_SrvAPI **) persist.UserData+16;
    Then the other threads (loaded to other thread slots than the one used for server) are as simple as the following:
    Code:
    #include "Cmd_Server.h"
    
    //===========================================================================
    //===========================================================================
    VD main (VD)
    {
        (*SrvAPI) (Cmd_Find_Home, 3);
    }
    Regards,
    Alexa Flame

  5. #5
    Join Date
    May 2006
    Posts
    4043

    Re: Programming KFlop in C. Tiny C compiler. Development tools.

    Hi Alexa Flame,

    Also interesting, but not sure if I can use TI's compiler for free.
    Actually it now seems the Texas Instrument's Code Generation Tools (Compiler without CCS) may be a free download.
    https://www-a.ti.com/downloads/sds_s...ad.htm

    Then the other threads (loaded to other thread slots than the one used for server) are as simple as the following:
    Clever idea. If you want minimum size and don't mind using a define here is one simpler:
    Code:
    #include "KMotionDef.h"
    
    typedef int T_SrvAPI (int CmdID, int Input);
    #define SrvAPI ((T_SrvAPI *) persist.UserData[16])
    	
    void main()
    {
    	SrvAPI(1, 3);
    }
    This avoids the need for a global variable, the need to initialize the global variable, and the double indirect reference to make the call.

    before: No Errors, No Warnings, text=144, bss=0, data=4, total=192
    after: No Errors, No Warnings, text=108, bss=0, data=0, total=128

    Your way is definitely clearer.

    Regards
    TK
    http://dynomotion.com

  6. #6
    Join Date
    Nov 2013
    Posts
    12

    Post Re: Programming KFlop in C. Tiny C compiler. Development tools.

    Hi Tom,

    Generally I don't like defines/macros as they quickly lead to bad programming style. I don't mind your define specially when there is only one static API.
    You are right - it saves one variable and the code.

    I had the aswers, but I would keep this thread open in case of other questions later. Plus people can use it for their relevant questions or comments to minimize multiplication of similar topics.

    Thank you.

    Best regards,
    Alexa Flame

  7. #7
    Join Date
    Nov 2013
    Posts
    12

    Re: Programming KFlop in C. Tiny C compiler. Development tools.

    Hello Tom and KFlop community,

    I would like to know the maximum downloadable size of user program for each KFlop thread.
    I saw one constant 0x10000 (64 KiB) - is it text area size limitation or text+data?

    Now I have these numbers, but they will grow soon:
    No Errors, No Warnings, text=60252, bss=932, data=5003, total=66212

    Since I opened the question of software limits, I would like to know also available stack size for each thread?

    Plus to this - is there any RAM to allocate memory from there apart user PERSIST data area (very small for some purposes).

    Best regards,
    Alex

  8. #8
    Join Date
    May 2006
    Posts
    4043

    Re: Programming KFlop in C. Tiny C compiler. Development tools.

    Hi Alex,

    I would like to know the maximum downloadable size of user program for each KFlop thread.
    I saw one constant 0x10000 (64 KiB)

    I would like to know also available stack size for each thread?
    See our wiki here

    is there any RAM to allocate memory from there apart user PERSIST data area (very small for some purposes).
    See the 8 MByte Gather Buffer described here.

    Regards
    TK
    http://dynomotion.com

Similar Threads

  1. compiler Heidenhain to DIN/ISO
    By Ce-en-ce in forum G-Code Programing
    Replies: 0
    Last Post: 05-13-2014, 07:51 AM
  2. Kflop e programming
    By RimappaGCode in forum Dynomotion/Kflop/Kanalog
    Replies: 7
    Last Post: 12-06-2013, 08:04 PM
  3. Writing my own CNC firmware, looking for g-code compiler
    By MMarz in forum Uncategorised CAD Discussion
    Replies: 10
    Last Post: 03-16-2011, 06:47 AM
  4. PicForge NEW FREE Pic Basic Compiler
    By Pic2000 in forum PIC Programing / Design
    Replies: 1
    Last Post: 07-12-2008, 06:31 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •