Menu

code startup problem

Help
2015-11-30
2015-11-30
  • Leonard Wojcik

    Leonard Wojcik - 2015-11-30

    I am using the Silabs IDE with the SDCC compiler under windows 7 for the Silabs C8051F340 embedded processor. I am Using the Silabs C8051F34x-DK development board for testing.
    SDCC version is 3.5.0

    I am compiling using: SDCC -c --debug --use-stdout -V -I"C:\program files\SDCC\include\mcs51" –model-large

    I include the “c8051f340.h” file in my main from the SDCC includes.

    Assembly with: sdas8051 -plosgff;

    Linking with: sdcc --debug --use-stdout -V –model-large.

    My program appears to hang in the start-up routines, looping around 5B to 5C and 7C to 7E. I think it is trying to clear memory. I need to use registers P2.0, P2.2, P2.6 and P2.7 for I/O.

    _XPAGE is defined in “c8051f340.h” – I don't know what to do with it.

    I need the large model to hold the data I intend to take in – it won't fit in the small model.

    Attaching: target_main.c, target_main.h, target.wsp, etc.

    BTW, the blinky program compiles and executes fine after a few changes to the code inserting underscores such as __SFR …. worked in large memory model.

    //===========================================================================
    //               Target Controller Program
    //
    //  copyright Wojcik Technical Services  11/07/2015
    //===========================================================================
    //===========================================================================
    //              Includes    
    //===========================================================================
    #include <C:\program files\sdcc\include\mcs51\c8051f340.h> // SFR declarations
    #include <target_main.h>
    #include <stdio.h>
    //===========================================================================
    //              Function Prototypes     
    //===========================================================================
    char Compare_characters (char *string1, char *string2, unsigned char n);
    char getchar (void);
    char _getkey (void);
    void Initialize (void);
    void Interrupts_Init(void);
    void Port_IO_Init (void);
    void putchar (char c);
    void Transmit_character (void);
    void Timer_Init(void); 
    void UART_Init(void);
    void Serial_interrupt (void) __interrupt 4;
    //===========================================================================
    //              Global Variables
    //===========================================================================
    
    //--------------    String Variables   ---------------------
    __code char uart_init[] = {"UART Initialized\r\n"};
    __code char hello[] = {"\n\rTarget Contoller Initialized\n\r"};
    __code char faced[]={"Faced\r\n"};
    __code char edged[]={"Edged\r\n"};
    //--------------    Serial Com Variables    -----------------
    __xdata unsigned char   tbuf[TBUFSIZE + 1], rbuf[RBUFSIZE + 1] ;
    signed   int    tbuf_head, tbuf_tail, rbuf_head, rbuf_tail, tbuf_count, rbuf_count;
    char            cmd_buf[15], cmd_counter, in_char;
    char            tbuf_flag;
    //--------------    Misc Variables    -----------------------
    unsigned char   button_status ;
    unsigned int    i, j;
    
    //===========================================================================
    void main(void) {
        LED = ON;       //??
        Initialize();
        TARGET = FACE ;
        LIGHT = !TARGET ;
        LED = OFF;
        LED = ON;       //??
        while(FOREVER) {
        if(rbuf_count > 0)
            {               //  process incoming from the serial port 
            in_char = _getkey() ;
            if(in_char == '\r')
                {
                while(rbuf_count > 0) _getkey ();  // if a cr, clear buffer
                cmd_counter = 0 ;
                }
            else
                {
                cmd_buf[cmd_counter++] = in_char;
                if(cmd_counter >= 4)                // process the command
                    {
                    cmd_buf[4] = '\0';
                    if(Compare_characters (cmd_buf, faced, 4))
                        {
                        TARGET = FACE;
                        LIGHT = !TARGET ;
                        printf(faced);
                        LED = OFF;
                            LED = ON;       //??
                        while (tbuf_count > 0)  Transmit_character ();
                        }
                    if(Compare_characters (cmd_buf, edged, 4))
                        {
                        TARGET = EDGE;
                        LIGHT = !TARGET ;
                        printf(edged);
                        LED = ON;
                        while (tbuf_count > 0)  Transmit_character ();
                        }
                    for(cmd_counter=0;cmd_counter<15; cmd_counter++)        // clean up
                        cmd_buf[cmd_counter] = 0;
    
                    }       // end process the comand
                }           // end else
            }               // end rbuf_count >0
    
        if(button_status != BUTTON)
            {
            button_status = BUTTON;
            if(button_status == 0)
                {
                printf("pressed\r\n");
                while (tbuf_count > 0)  Transmit_character ();
                }
            else
                {
                printf("released\r\n");
                while (tbuf_count > 0)  Transmit_character ();
                }
            for(i=0;i<40000;i++)        // waste some time for switch bounce
                j = i;
            }
        }                   // End of FOREVER, but of course FOREVER never ends
    }
    //  end of main
    //===========================================================================
    void Initialize(void)
    {
        EA = OFF;                       // All interrupts off
        PCA0MD = 0x00;                  // WDTE = 0 (clear watchdog timer enable)
        OSCICN = SIX_MHZ;               // Enable oscillator at 6 mhz  (0x82)
        Port_IO_Init();
        TARGET = FACE ;
        LIGHT = OFF ;
        LED = ON;
        tbuf_head = tbuf_tail = rbuf_head = rbuf_tail = 0;
        tbuf_count = rbuf_count = 0;
        cmd_counter = 0 ;
        Timer_Init();
        UART_Init();
        Interrupts_Init();
        button_status = ON;
        printf(hello);
        while (tbuf_count > 0)  Transmit_character ();
    }
    //===========================================================================
    void Port_IO_Init()
    {
        P2MDIN    = 0x01;           // P2.0  open collector, weak pull up, non analog
        P2MDOUT   = 0x44;           // P2.2 & P2.6 push/pull digital
        P2SKIP    = 0x44;           // Skip P2.2 & P2.6  (LED & relay outputs
        XBR0      = 0x01;           // UART 0 enabled
        XBR1      = 0x40;           // weak pull-up enabled, crossbar enabled
    }
    //===========================================================================
    void Timer_Init()           // Timer 1 used by UART0  set for 9600 baud
    {
        TCON      = 0x40;           // Timer 1 off
        TMOD      = 0x20;           // Timer 1  8 bit, auto reload
        CKCON     = 0x01;           // Timer 1 clock is SYSCLK  (6 mhz)
        TH1       = 0xB2;           // Timer 1 reload value 9600 baud
        TCON      = 0x40;           // Turn on timer 1 level triggered
    }
    //===========================================================================
    void UART_Init()
    {
        SCON0     = 0x10;           // enable reception
        tbuf_flag = TRUE;
        printf(uart_init);
    }
    //===========================================================================
    void Interrupts_Init()
    {
        IE      = 0x90;         // enable (EA) + UART0
    }
    //===========================================================================
    void Serial_interrupt (void) __interrupt 4  // Uses timer1
    {
        EA = 0;                         // Disable all interrupts
        if (RI0) {                      // If a read, put char in circular buffer
            rbuf[rbuf_tail++] = SBUF0;
            if(rbuf_tail > (RBUFSIZE - 1))
                rbuf_tail -= RBUFSIZE;
            if( (rbuf_count = (rbuf_tail - rbuf_head)) <0 )
                rbuf_count += RBUFSIZE;
            RI0 = FALSE;                // Clear read interrupt flag
            }
        if (TI0) {
            tbuf_flag = TRUE;           // If a write finished, set a flag
            TI0 = FALSE;                // Clear write interrupt flag
            }
        EA = 1;                     // enable all interrupts
    }
    //===========================================================================
    char getchar (void)
    {
        char r;
    
        r = _getkey();                  // Call _getkey to get a char from buffer
        putchar(r);                     // Echo the character
    //  if(r == '\r')                   // Convert returns to endline
    //      r = '\n';
        return r;                       // Return the character
    }
    //===========================================================================
    char _getkey ()         // returns a character from the circular buffer, or NULL
    {
        char c ;
    
        if( rbuf_count > 0) {
            c = rbuf[rbuf_head++];
            if(rbuf_head > RBUFSIZE_MINUS )
                rbuf_head -= RBUFSIZE;  
            if( (rbuf_count = (rbuf_tail - rbuf_head)) <0 )
            rbuf_count += RBUFSIZE;
            return (c);
            }
            else
                return (char) 0;
    }
    //===========================================================================
    void putchar (char c)               // Puts characters into a circular buffer
    {                       // Counts characters, buffer can overwite
        tbuf[tbuf_tail++] = c;
        if(tbuf_tail > TBUFSIZE_MINUS )
            tbuf_tail -= TBUFSIZE;
        if( (tbuf_count = tbuf_tail - tbuf_head) <0 )
            tbuf_count += TBUFSIZE;
    }
    //===========================================================================
    void Transmit_character (void)      // Unpacks characters from a circular buffer
    {                                   // and tansmits them
        if(tbuf_flag) {                 // tbuf_flag will be TRUE when xmitter available
            SBUF0 = tbuf[tbuf_head++];
            if(tbuf_head > (TBUFSIZE - 1))
                tbuf_head -= TBUFSIZE;  
            tbuf_flag = FALSE ;
            if( (tbuf_count = tbuf_tail - tbuf_head) <0 )
            tbuf_count += TBUFSIZE;
            }
    }
    //===========================================================================
    char    Compare_characters (char *string1, char *string2, unsigned char n)
    {
        unsigned char count,equal_flag;
    
        equal_flag = 0;
        for(count=0; count<n; count++) {
            if( string1[count] == string2[count]) {
                equal_flag = TRUE ;
                }
                else {
                    equal_flag = FALSE;
                    return equal_flag;
                    }
                }
        return equal_flag;
    }
    //===========================================================================
    //                  END OF FILE
    //===========================================================================
    //-------------------------------------------------------------------------
    //             Oscillator definitions
    //-------------------------------------------------------------------------
        #define     ONE_MHZ         0x00        // 1.5 mhz
        #define     THREE_MHZ       0x81        // 3 mhz
        #define     SIX_MHZ         0x82        // 6 mhz
        #define     TWELVE_MHZ      0x83        // 12 mhz
    //-------------------------------------------------------------------------
    //             Serial port definitions
    //-------------------------------------------------------------------------
        #define     TBUFSIZE        128
        #define     TBUFSIZE_MINUS  TBUFSIZE - 1
        #define     RBUFSIZE        128
        #define     RBUFSIZE_MINUS  RBUFSIZE - 1
    
    //-------------------------------------------------------------------------
    //             Hardware definitions
    //-------------------------------------------------------------------------
    __sbit __at (0xA6) TARGET       ;           // target relay control port (2.6)
    __sbit __at (0xA7) LIGHT        ;           // target relay control = !target port (2.7)
    __sbit __at (0xA2) LED          ;           // LED port (2.2)
    __sbit __at (0xA0) BUTTON       ;           // push button sense (2.0)
    //-------------------------------------------------------------------------
    //             Miscelaneous definitions
    //-------------------------------------------------------------------------
        #define     ONE         1
        #define     ZERO        0
        #define     ON          1
        #define     OFF         0
        #define     FOREVER     1
        #define     LOW         0
        #define     HIGH        1
        #define     TRUE        1
        #define     FALSE       0
        #define     FACE        0
        #define     EDGE        1
    //-------------------------------------------------------------------------
    //              End of File
    //-------------------------------------------------------------------------
    

    Thanks,

    Leonard Wojcik
    801-272-1813
    leonard@wojciktech.com

     

    Last edit: Maarten Brock 2015-12-05
  • Frieder Ferlemann

    Hi Leonard,

    your problem is most likely a watchdog problem, see Program never gets to the main() function

     

Log in to post a comment.