//======================================================================
// Name : SPI.c
// Date : 2007/03/12
// Author : Xu
// Comment: Driving MCP41010
// SPIMOSI0(GPE12) working is in master mode
// SPICLK0(GPE13) is the master outpuc clock
// GPG3 is master CS pin
// Version:
// History: 2007/03/12 Create
//======================================================================
#include <windows.h>
#include <types.h>
#include <tchar.h>
#include <cardserv.h>
#include <cardapi.h>
#include <tuple.h>
#include <devload.h>
#include <diskio.h>
#include <nkintr.h>
#include <oalintr.h>
#include <windev.h>
#include "s2410.h"
#define PUBLIC
volatile IOPreg * v_pIOPregs;
volatile SSPreg *v_pSSPregs;
BOOL SPI_InitAddrIO(VOID);
BOOL SPI_InitAddrSPI(void);
//======================================================================
// Name : InitSPI
// Date : 2007/03/12
// Author : XuHaibo
// Parameters:
// Return :
// Comment :
// Version :
// History : 2007/03/12 Create
//======================================================================
void InitSPI(void) //init uart2
{
unsigned int pclk = S2410PCLK; // s2410.h define
// #define S2410FCLK (203 * 1000 * 1000) // 203MHz (FCLK).
// #define PCLKDIV 4 // P-clock (PCLK) divisor.
// #define S2410PCLK (S2410FCLK / PCLKDIV) // PCLK.
// Set I/O is SPI interface
// Config GPE12,13 is SPIMOSI0,SPICLK0
v_pIOPregs->rGPECON &= ~(0x0F << 24);
v_pIOPregs->rGPECON |= (0x0A << 24);
// Config GPG3 is Master SPI CS
v_pIOPregs->rGPGCON &= ~(0x3 << 6);
v_pIOPregs->rGPGCON |= (0x1 << 6);
v_pIOPregs->rGPGUP &= ~(0x1 << 3);
// Initialize CS is high
v_pIOPregs->rGPGDAT |= (0x1 << 3);
// Baudrate = PCLK/2/(Prescaler value + 1)
// PCLK = 203000000/4 = 50750000 Hz
// Prescaler value = 0x18 = 24
// Baudrate = 50750000/2/(24 + 1) = 1015000 = 1.015MHz
// MCP41010 max clock frequency is 10MHz
v_pSSPregs->rSPPRE0 = 0x18;
// Set SPCON0 to configure properly the SPI module.
// Master
// SCK enable
// polling mode
v_pSSPregs->rSPCON0 = 0x18;
// 0 Tx Auto Garbage Data mode enable (TAGD):Decide whether the receiving data only needs or not.
// 0 = normal mode, 1 = Tx auto garbage data mode
// NOTE: In normal mode, if you only want to receive data,
// you should transmit dummy 0xFF data.
// 1,2 It is possible to operate the devices in SPI modes 0,0and 1,1. (MCP41010 datasheet)
// Set SPI mode is 0,0.
// 3 Set S3C2410 is Master.
// 4 SCK Enable (ENSCK)
// 5,6 Determine how and by what SPTDAT is read/written.
// 00 = polling mode, 01 = interrupt mode
// 10 = DMA mode, 11 = reserved
// Set polling mode
}
//======================================================================
// Name : InitSPI
// Date : 2007/03/12
// Author : XuHaibo
// Parameters:
// Return :
// Comment :
// Version :
// History : 2007/03/12 Create
//======================================================================
BOOL
SPI_InitAddrIO(VOID)
{
BOOL RetValue = TRUE;
v_pIOPregs = (volatile IOPreg *)VirtualAlloc(0, sizeof(IOPreg), MEM_RESERVE, PAGE_NOACCESS);
if (v_pIOPregs == NULL)
{
// ERRORMSG(1,(TEXT("For SPI_IOPregs : VirtualAlloc failed!\r\n")));
RetValue = FALSE;
}
else
{
if (!VirtualCopy((PVOID)v_pIOPregs, (PVOID)(IOP_BASE), sizeof(IOPreg), PAGE_READWRITE | PAGE_NOCACHE))
{
// ERRORMSG(1,(TEXT("For SPI_IOPregs: VirtualCopy failed!\r\n")));
RetValue = FALSE;
}
}
if (!RetValue)
{
// RETAILMSG (1, (TEXT("::: SPI_InitializeAddresses - Fail!!\r\n") ));
if (v_pIOPregs)
{
VirtualFree((PVOID) v_pIOPregs, 0, MEM_RELEASE);
}
v_pIOPregs = NULL;
}
else
{
;
// RETAILMSG (1, (TEXT("::: SPI_InitializeAddresses - Success\r\n") ));
}
return(RetValue);
}
//======================================================================
// Name : InitSPI
// Date : 2007/03/12
// Author : XuHaibo
// Parameters:
// Return :
// Comment :
// Version :
// History : 2007/03/12 Create
//======================================================================
BOOL
SPI_InitAddrSPI(void)
{
BOOL RetValue = TRUE;
// RETAILMSG (1, (TEXT("SPI_InitializeAddresses \r\n") ));
v_pSSPregs = (volatile SSPreg *)VirtualAlloc(0, sizeof(UART2reg), MEM_RESERVE, PAGE_NOACCESS);
if (v_pSSPregs == NULL)
{
// ERRORMSG(1,(TEXT("For SPI_UART2regs : VirtualAlloc failed!\r\n")));
RetValue = FALSE;
}
else
{
if (!VirtualCopy((PVOID)v_pSSPregs, (PVOID)(UART2_BASE), sizeof(UART2reg), PAGE_READWRITE | PAGE_NOCACHE))
{
// ERRORMSG(1,(TEXT("For SPI_UART2regs: VirtualCopy failed!\r\n")));
RetValue = FALSE;
}
}
if (!RetValue)
{
// RETAILMSG (1, (TEXT("::: SPI_InitializeAddresses - Fail!!\r\n") ));
if (v_pSSPregs)
{
VirtualFree((PVOID) v_pSSPregs, 0, MEM_RELEASE);
}
v_pSSPregs = NULL;
}
else
{
;
// RETAILMSG (1, (TEXT("SPI_InitializeAddresses - Success\r\n") ));
}
return(RetValue);
}