Re: {GCC-HCS12} Re: can't get SPI to work on my MC9S12XDT256

67 views
Skip to first unread message
Message has been deleted
Message has been deleted

matt brown

unread,
Jan 27, 2010, 11:01:28 AM1/27/10
to gcc-...@googlegroups.com
u know I haven't tried using interrupts with the SPI yet.  I'll give it a try today, thx.

2010/1/26 Navid <navid.m...@gmail.com>
Hello,

I used use this at 12 MHz to interface FPU with HCS12 ... hope it
helps:

Please make sure you init the SPI as well:
       spi0_init();
       enable_interrupts();

/****************************************SPI
START********************************/

/*512 Byte circular FIFO output spi0_buffer*/
#define BUFSIZE 512
unsigned char spi0_buffer[BUFSIZE] ;
unsigned char * volatile spi0_bufin = spi0_buffer ;
unsigned char * volatile spi0_bufout = spi0_buffer ;
volatile unsigned char spi0_charin = 0 ; //char in

int long_wait_mode = FALSE; //FPU needs a long wait of 150ms at
restart!

void INTERRUPT spi0_isr( void );

void spi0_init(void) ;
unsigned char spi0_read_char(void) ;
void spi0_write_char(unsigned char val) ;



/*
 * Initialize serial line with 12MHz clock rate
 */
void spi0_init()
{
       // Repoint SPI interupt service routin to our own custom made ISR
       UserSPI0 = (unsigned int) & spi0_isr;

       SPI0CR1 = 0xF0;
       //      1 1 1 1 0 0 0 0     = SPI0CR1 = 0xF0
       //      | | | | | | | |
       //      | | | | | | | \____ MSB first
       //      | | | | | | \______ Do not use SS to automatically select slave
       //      | | | | | \________ 0 phase (data on 1st clock edge)
       //      | | | | \__________ 0 polarity (clock idle low)
       //      | | | \____________ Master mode
       //      | | \______________ Have interrupts on transmit
       //      | \________________ Enable SPI
       //      \__________________ Have SPI interrupts

       // Normal (not bi-directional) mode and enable pwoer save (on bit
two)
       SPI0CR2 = 0x02;

       // 12 MHz SPI clock on a 24Mz Bus speed
       SPI0BR = 0;

       // Port S Pull ups enabled
       WOMS = 0;

}

/*
 * Our own interrupt service routine for SPI interrupts
 */
void INTERRUPT spi0_isr( void )
{
       // read status register of SPI
       char status = SPI0SR;

       // MASTER HCS12 will recive data
       if(status & 0x80) //SPIF is set. This bit is set after the eighth SCK
cycle in a data transfer
       {
               // if there is a data, read it and clear the interrupt
               spi0_charin = SPI0DR;

               // if we are in long wait mode, print the debug information to waste
some time ...
               if (long_wait_mode) DB12FNP->printf("SPI0ISR - READ %d\r\n",
spi0_charin);
       }


       // MASTER HCS12 will send data
       else if(status & 0x20) //SPI Transmit Empty Interrupt Flag is set and
we have sapce in SPI buffer
       {
               if ( spi0_bufin == spi0_bufout )
               {
                       // We are done and don't have anything to transfer
                       // so clear and shutdown SPI Transmit Empty Interrupt Flag
                       SPI0CR1 &= ~0x20;
               }
               else
               {       // still have data to send and send it
                       char temp = *spi0_bufout++ ;
                       SPI0DR = temp;

                       // if we are in long wait mode, print the debug information to
waste some time ...
                       if (long_wait_mode) DB12FNP->printf("SPI0ISR - WRITE %d\r\n",
temp);

                       // adjust the buffer
                       if(spi0_bufout == spi0_buffer + BUFSIZE)
                       {
                               spi0_bufout = spi0_buffer ;
                       }
               }
       }
}



/*
 * Retreives a character from the serail line
 */
unsigned char spi0_read_char(void)
{
       unsigned char result ;
       while((result = spi0_charin) == 0)
       {
               wait_for_interrupts();
       }
       spi0_charin = 0 ;
       return result ;
}




/*
 * Put a character into serial port
 */
void spi0_write_char(unsigned char val)
{
       do
       {
               int used = spi0_bufin - spi0_bufout ;
               if ( used < 0 )
               {
                       used += BUFSIZE ;
               }
               if(used < BUFSIZE - 1)
               {
                       break ;
               }
               wait_for_interrupts();
       }
       while(1);


       *spi0_bufin++ = val ;
       if(spi0_bufin == spi0_buffer + BUFSIZE)
       {
               spi0_bufin = spi0_buffer ;
       }

       // Re-enable transmitter interrupt
        SPI0CR1 |= 0x20 ;
}

/****************************************SPI
DONE********************************/

--
You received this message because you are subscribed to the Google Groups "GCC-HCS12" group.
To post to this group, send email to gcc-...@googlegroups.com.
To unsubscribe from this group, send email to gcc-hcs12+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/gcc-hcs12?hl=en.


Reply all
Reply to author
Forward
0 new messages