/*-----------------------------------------------------------------------*/
/* MMC/SDSC/SDHC (in UASRT MSPI mode) control module  (C)ChaN, 2007      */
/*-----------------------------------------------------------------------*/
/* Only rcvr_spi(), xmit_spi(), disk_timerproc() and some macros         */
/* are platform dependent.                                               */
/*-----------------------------------------------------------------------*/

#ifndef _MMC_USPI_CONF
#define _MMC_USPI_CONF

#include <avr/io.h>
#include "diskio.h"
#include "includes.h"

#define _USPITYPE                               // Define which version of hardware we're using.



// ................................................................................................
// Initialize handware and anything else related to USART based SPI port used to access
//  the SD Card.
//  Currently the SD Card module will be located only in personality module location 3,
//    and ports and pins are assigned accordingly.
//
// Parameters:
//    None
// Returns:
//    Nothing.
//

#if _DRIVES > 0
// Definitions for personality module 3 USART SPI tied to Drive 0.
#define _USPI0_BAUD(rate)  ((LRI_FOSC / 16 / rate) -1)     // Baud rate.

#define USPI0SRA            UCSR3A              // Device definitions.
  #define USPI0TXC          _BV(TXC3)
  #define USPI0RXC          _BV(RXC3)
  #define USPI0UDRE         _BV(UDRE3)
#define USPI0SRB            UCSR3B
  #define USPI0RXEN         RXEN3
  #define USPI0TXEN         TXEN3
#define USPI0SRC            UCSR3C
  #define USPI0UMSEL1       UMSEL31
  #define USPI0UMSEL0       UMSEL30
  #define USPI0UCORD        UCORD3
  #define USPI0UCPHA        UCPHA3
  #define USPI0UCPOL        UCPOL3
#define USPI0BRRL           UBRR3L
#define USPI0BRRH           UBRR3H
#define USPI0DAT            UDR3
#define USPI0PRR            PRR1                // Same port for all USARTs.
  #define USPI0PRUSART      PRUSART3
                                                    // Port definitions.
#define USPI0INITPXCK       { DDRJ  |=  _BV(PJ2); } // Outputs.
#define USPI0INITPnPON      { DDRL  |=  _BV(PL6); }
#define USPI0INITPnSS       { DDRF  |=  _BV(PF0); }
#define USPI0INITPioMOSI    { DDRJ  |=  _BV(PJ1); }
#define USPI0INITPioMISO    { DDRJ  |=  _BV(PJ0); }
#define USPI0INITPnCD       { DDRF  &= ~_BV(PF3); } // Input.
#define USPI0INITPnPOK      { DDRK  &= ~_BV(PK6); }

#define USPI0SETTO1XCK      { PORTJ |=  _BV(PJ2); } // Set to 1.
#define USPI0SETTO1nPON     { PORTL |=  _BV(PL6); }
#define USPI0SETTO1nSS      { PORTF |=  _BV(PF0); }

#define USPI0SETTO0XCK      { PORTJ &= ~_BV(PJ2); } // Set to 0.
#define USPI0SETTO0nPON     { PORTL &= ~_BV(PL6); }
#define USPI0SETTO0nSS      { PORTF &= ~_BV(PF0); }
#define USPI0SETTO0ioMOSI   { PORTJ &= ~_BV(PJ1); }
#define USPI0SETTO0ioMISO   { PORTJ &= ~_BV(PJ0); }

#define USPI0GETXCK         ( PORTJ & _BV(PJ2))     // Get bit value.
#define USPI0GETnPON        ( PORTL & _BV(PL6))
#define USPI0GETnSS         ( PORTF & _BV(PF0))
#define USPI0GETnCD         ( PORTF & _BV(PF3))
#define USPI0GETnPOK        ( PORTK & _BV(PK6))
#endif // _DRIVES > 0

#if _DRIVES > 1
// Definitions for personality module 1 USART SPI tied to Drive 1.
#define _USPI1_BAUD(rate)  ((LRI_FOSC / 16 / rate) -1)     // Baud rate.

#define USPI1SRA            UCSR3A              // Device definitions.
  #define USPI1TXC          _BV(TXC3)
  #define USPI1RXC          _BV(RXC3)
  #define USPI1UDRE         _BV(UDRE3)
#define USPI1SRB            UCSR3B
  #define USPI1RXEN         RXEN3
  #define USPI1TXEN         TXEN3
#define USPI1SRC            UCSR3C
  #define USPI1UMSEL1       UMSEL31
  #define USPI1UMSEL0       UMSEL30
  #define USPI1UCORD        UCORD3
  #define USPI1UCPHA        UCPHA3
  #define USPI1UCPOL        UCPOL3
#define USPI1BRRL           UBRR3L
#define USPI1BRRH           UBRR3H
#define USPI1DAT            UDR3
#define USPI1PRR            PRR1                // Same port for all USARTs.
  #define USPI1PRUSART      PRUSART1
                                                // Port definitions.
#define USPI1INITPXCK       { DDRJ  |=  _BV(PJ2); } // Outputs.
#define USPI1INITPnPON      { DDRL  |=  _BV(PL6); }
#define USPI1INITPnSS       { DDRF  |=  _BV(PF0); }
#define USPI1INITPioMOSI    { DDRD  |=  _BV(PD3); }
#define USPI1INITPioMISO    { DDRD  |=  _BV(PD2); } // CONFIRM! Has two inputs! Also at J3.
#define USPI1INITPnCD       { DDRF  &= ~_BV(PF3); } // Input.
#define USPI1INITPnPOK      { DDRK  &= ~_BV(PK6); }

#define USPI1SETTO1XCK      { PORTJ |=  _BV(PJ2); } // Set to 1.
#define USPI1SETTO1nPON     { PORTL |=  _BV(PL6); }
#define USPI1SETTO1nSS      { PORTF |=  _BV(PF0); }

#define USPI1SETTO0XCK      { PORTJ &= ~_BV(PJ2); } // Set to 0.
#define USPI1SETTO0nPON     { PORTL &= ~_BV(PL6); }
#define USPI1SETTO0nSS      { PORTF &= ~_BV(PF0); }
#define USPI1SETTO0ioMOSI   { PORTD &= ~_BV(PD3); }
#define USPI1SETTO0ioMISO   { PORTD &= ~_BV(PD2); } // CONFIRM! Has two inputs! Also at J3.

#define USPI1GETXCK         ( PORTJ & _BV(PJ2)) // Get bit value.
#define USPI1GETnPON        ( PORTL & _BV(PL6))
#define USPI1GETnSS         ( PORTF & _BV(PF0))
#define USPI1GETnCD         ( PORTF & _BV(PF3))
#define USPI1GETnPOK        ( PORTK & _BV(PK6))
#endif // _DRIVES > 1

// ------------------------------------------------------------------------------------------------

void USPI_Device_Init(BYTE drv);
void USPI_Device_Disable(BYTE drv);
void USPI_Device_PowerOn(BYTE drv);
void USPI_Device_PowerOff(BYTE drv);

#endif //f _MMC_USPI_CONF
