ds1394Spi.c
资源名称:ds1394Spi.rar [点击查看]
上传用户:whwilliam
上传日期:2022-02-24
资源大小:3k
文件大小:13k
源码类别:
VxWorks
开发平台:
Visual C++
- /*ds1394spi.c - SPI interface to serial rtc library */
- /*
- DESCRIPTION
- This library contains routines to manipulate ds1394 which is accessed as a
- Serial Peripheral Interface (SPI). Read and write routines are included.
- */
- /* includes */
- #include "vxWorks.h"
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
- //#include "copyright_wrs.h"
- #include "iv.h"
- #include "netLib.h"
- #include "semLib.h"
- #include "vxWorks.h"
- #include "vme.h"
- #include "memLib.h"
- #include "cacheLib.h"
- #include "sysLib.h"
- #include "config.h"
- #include "string.h"
- #include "intLib.h"
- #include "logLib.h"
- #include "stdio.h"
- #include "taskLib.h"
- #include "vxLib.h"
- #include "c_type.h"
- #include "drv/mem/m82xxDpramLib.h"
- #include "drv/parallel/m8260IOPort.h"
- #include "drv/sio/m8260Cp.h"
- #include "ds1394spi.h"
- SPI_DEV * pSpi;
- void DS1394CSEn(void)
- {
- int immrVal=vxImmrGet();
- * M8260_IOP_PADAT(immrVal) &= ~(BigEndBit(8));
- }
- void DS1394CSDis(void)
- {
- int immrVal=vxImmrGet();
- * M8260_IOP_PADAT(immrVal) |= (BigEndBit(8));
- }
- /******************************************************************************
- *
- * sysHwSpiInit - initializes NV-RAM
- *
- * This routine initializes NV-RAM to prepare for subsequent reads from or
- * writes to NV-RAM.
- *
- * RETURNS: none
- */
- LOCAL STATUS sysHwSpiInit(void)
- {
- SPI_BD *BufferPoint=NULL;
- char *BufferRx=NULL;
- char *BufferTx=NULL;
- UINT32 cpcrVal;
- int immrVal=vxImmrGet();
- // SPI_DEV* pSpi;
- // pSpi = &spiDev;
- /* SPI initialization cannot occur more than once. */
- //if (pSpi->init == TRUE)
- //{
- //return;
- //}
- BufferPoint=malloc(sizeof(SPI_DEV));
- pSpi=(SPI_DEV *)(BufferPoint);
- /*----------------------------------------------*/
- /* config io port */
- /*----------------------------------------------*/
- * M8260_IOP_PDDIR(immrVal) &= ~(BigEndBit(16)|BigEndBit(17)|BigEndBit(18));
- * M8260_IOP_PDSO(immrVal) |= (BigEndBit(16)|BigEndBit(17)|BigEndBit(18));
- * M8260_IOP_PDODR(immrVal) &= ~(BigEndBit(16)|BigEndBit(17)|BigEndBit(18));
- * M8260_IOP_PDPAR(immrVal) |= (BigEndBit(16)|BigEndBit(17)|BigEndBit(18));
- * M8260_IOP_PDPAR(immrVal) &= ~(BigEndBit(19));
- * M8260_IOP_PADIR(immrVal) |= (BigEndBit(8));
- * M8260_IOP_PASO(immrVal) &= ~(BigEndBit(8));
- * M8260_IOP_PAODR(immrVal) &= ~(BigEndBit(8));
- * M8260_IOP_PAPAR(immrVal) &= ~(BigEndBit(8));
- * M8260_IOP_PADAT(immrVal) |= (BigEndBit(8));
- /* Get IMMR register base. */
- pSpi->regBase = immrVal;
- /* Get pointer to dual-ported RAM. */
- pSpi->dualpRam = immrVal;
- * M8260_SPI_BASE(immrVal) = SPI_PARAM_OFFSET;
- /* Set pointer to SPI parameter RAM. */
- pSpi->pPram = (pSPI_PARAM) (immrVal+SPI_PARAM_OFFSET);
- /* Clear parameter RAM values. */
- pSpi->pPram->rbase = 0;
- pSpi->pPram->tbase = 0;
- pSpi->pPram->rfcr = 0;
- pSpi->pPram->tfcr = 0;
- pSpi->pPram->mrblr = 0;
- pSpi->pPram->rstate = 0;
- pSpi->pPram->res1 = 0;
- pSpi->pPram->rbptr = 0;
- pSpi->pPram->res2 = 0;
- pSpi->pPram->res3 = 0;
- pSpi->pPram->tstate = 0;
- pSpi->pPram->res4 = 0;
- pSpi->pPram->tbptr = 0;
- pSpi->pPram->res5 = 0;
- pSpi->pPram->res6 = 0;
- pSpi->pPram->res7 = 0;
- //首先在DRAM分配中分配SPI bd
- BufferPoint=(SPI_BD *)m82xxDpramFccMalloc(SPI_BD_SIZE+2,16);
- if(BufferPoint==NULL)
- return ERROR;
- memset(BufferPoint,0,SPI_BD_SIZE+2);
- pSpi->pPram->rbase = (UINT16)((INT32)BufferPoint-immrVal);
- pSpi->pPram->tbase = (UINT16) ((INT32)BufferPoint-immrVal+sizeof(SPI_BD));
- pSpi->pPram->rfcr = 0x18;
- pSpi->pPram->tfcr = 0x18;
- pSpi->pPram->mrblr = 1;
- //SPI BD 初始化
- //BufferRx=malloc(SPI_RX_BUF_SZ);
- //BufferTx=malloc(SPI_TX_BUF_SZ);
- //if(BufferRx==NULL | BufferTx==NULL)
- //return ERROR;
- //memset(BufferRx,0,SPI_RX_BUF_SZ);
- //memset(BufferTx,0,SPI_TX_BUF_SZ);
- //pSpi->pRxBdBase = (BufferPoint);
- //pSpi->pTxBdBase = (BufferPoint+sizeof(SPI_BD));
- pSpi->pRxBdBase = (INT32)BufferPoint;
- pSpi->pTxBdBase = (INT32)BufferPoint+sizeof(SPI_BD);
- pSpi->pTxBdBase->statusMode =
- (SPI_TRANS_TX_BD_LAST | SPI_TRANS_TX_BD_WRAP);
- pSpi->pTxBdBase->dataLength = 0;
- pSpi->pTxBdBase->dataPointer = spiTxBuf;
- pSpi->pRxBdBase->statusMode =
- (SPI_TRANS_RX_BD_EMPTY | SPI_TRANS_RX_BD_LAST | SPI_TRANS_RX_BD_WRAP);
- pSpi->pRxBdBase->dataLength = 0;
- pSpi->pRxBdBase->dataPointer = spiRxBuf;
- //SPI模式等寄存器设置
- * M8260_SPI_SPMODE(immrVal) = DisSPI; /* Clear BSY */
- * M8260_SPI_SPIE(immrVal) = 0x17;
- * M8260_SPI_SPIM(immrVal) = 0x17;
- pSpi->init = TRUE;
- /* issue start command*/
- while ((*M8260_CPCR(immrVal) & M8260_CPCR_FLG) != 0);
- * M8260_CPCR(immrVal) = M8260_CPCR_OP(M8260_CPCR_RT_INIT) |
- M8260_CPCR_SBC(M8260_CPCR_SBC_SPI) |
- M8260_CPCR_PAGE(M8260_CPCR_PAGE_SPI) |
- M8260_CPCR_FLG;
- while ((*M8260_CPCR(immrVal) & M8260_CPCR_FLG) != 0);
- return;
- } /* end of sysHwSpiInit */
- /******************************************************************************
- *
- * ds1394nbyteread - get the contents of ds1394
- *
- * This routine copies the contents of ds1394 into a specified
- * string. The string is terminated with an EOS.
- *
- * RETURNS: OK, or ERROR if access is outside the ds1394 range.
- *
- */
- LOCAL STATUS Ds1394nByteRead(UCHAR cmdadds,char* string,int strLen)
- {
- // SPI_DEV* pSpi;
- int immrVal=vxImmrGet();
- // pSpi = &spiDev;
- if (pSpi->init != TRUE)
- return(ERROR);
- /* Wait for transmit to finish (first). */
- while (pSpi->pTxBdBase->statusMode & SPI_TRANS_TX_BD_READY)
- {
- ;
- }
- /* Disable SPI (serial port interface). */
- * M8260_SPI_SPMODE(immrVal) = DisSPI; /* Clear BSY */
- /* Set up receive buffer descriptor. */
- pSpi->pRxBdBase->statusMode = ( SPI_TRANS_RX_BD_WRAP |
- SPI_TRANS_RX_BD_EMPTY );
- pSpi->pRxBdBase->dataLength = 0;
- pSpi->pRxBdBase->dataPointer = spiRxBuf;
- //写发送数据
- spiTxBuf[0]=cmdadds;
- /* Set up transmit buffer descriptor. */
- pSpi->pTxBdBase->statusMode = ( SPI_TRANS_TX_BD_READY |
- SPI_TRANS_TX_BD_WRAP |
- SPI_TRANS_TX_BD_LAST );
- pSpi->pTxBdBase->dataLength = strLen+1;
- pSpi->pTxBdBase->dataPointer = spiTxBuf;
- pSpi->pPram->mrblr = strLen + 1;
- /* issue start command*/
- while ((*M8260_CPCR(immrVal) & M8260_CPCR_FLG) != 0);
- * M8260_CPCR(immrVal) = M8260_CPCR_OP(M8260_CPCR_RT_INIT) |
- M8260_CPCR_SBC(M8260_CPCR_SBC_SPI) |
- M8260_CPCR_PAGE(M8260_CPCR_PAGE_SPI) |
- M8260_CPCR_FLG;
- while ((*M8260_CPCR(immrVal) & M8260_CPCR_FLG) != 0);
- /* Enable SPI. */
- * M8260_SPI_SPMODE(immrVal) = EnSPI;
- /* Assert chip select (CS). */
- DS1394CSEn();
- /* Start transfer. */
- * M8260_SPI_SPCOM(immrVal) = SPI_STR;
- /* Wait for reading to finish. */
- while (pSpi->pRxBdBase->statusMode & SPI_TRANS_RX_BD_EMPTY)
- {
- /* Busy-wait. */
- ;
- }
- /* De-assert chip select (CS). */
- DS1394CSDis();
- /* Copy read data into given string. */
- bcopyBytes(&spiRxBuf[1], string, strLen);
- return(OK);
- } /* end of ds1394nbyteread */
- /******************************************************************************
- *
- * Ds1394nByteWrite - write a byte to ds1394
- *
- * This routine writes one byte to ds1394
- *
- * RETURNS: none
- *
- * NOMANUAL
- */
- LOCAL STATUS Ds1394nByteWrite(UCHAR writeadds,char* string,int strLen)
- {
- // SPI_DEV* pSpi;
- // pSpi = &spiDev;
- int immrVal=vxImmrGet();
- /* Wait for transmit to finish (first). */
- while (pSpi->pTxBdBase->statusMode & SPI_TRANS_TX_BD_READY)
- {
- ;
- }
- /* Disable SPI (serial port interface). */
- * M8260_SPI_SPMODE(immrVal) = DisSPI; /* Clear BSY */
- //写发送地址
- spiTxBuf[0]=writeadds;
- /* Copy given string into write data. */
- bcopyBytes(string,&spiTxBuf[1],strLen);
- /* Set up transmit buffer descriptor. */
- pSpi->pTxBdBase->dataPointer = spiTxBuf;
- pSpi->pTxBdBase->dataLength = strLen+1;
- pSpi->pTxBdBase->statusMode = ( SPI_TRANS_TX_BD_READY |
- SPI_TRANS_TX_BD_WRAP |
- SPI_TRANS_TX_BD_LAST );
- /* Set SPI read value. */
- pSpi->pPram->mrblr = strLen+1;
- /* Set up receive buffer descriptor. */
- pSpi->pRxBdBase->dataPointer = spiRxBuf;
- pSpi->pRxBdBase->statusMode = ( SPI_TRANS_RX_BD_WRAP |
- SPI_TRANS_RX_BD_EMPTY
- | SPI_TRANS_RX_BD_LAST);
- /* issue start command*/
- while ((*M8260_CPCR(immrVal) & M8260_CPCR_FLG) != 0);
- * M8260_CPCR(immrVal) = M8260_CPCR_OP(M8260_CPCR_RT_INIT) |
- M8260_CPCR_SBC(M8260_CPCR_SBC_SPI) |
- M8260_CPCR_PAGE(M8260_CPCR_PAGE_SPI) |
- M8260_CPCR_FLG;
- while ((*M8260_CPCR(immrVal) & M8260_CPCR_FLG) != 0);
- /* Enable SPI. */
- * M8260_SPI_SPMODE(immrVal) = EnSPI;
- /* Assert chip select (CS). */
- DS1394CSEn();
- /* Start transfer. */
- * M8260_SPI_SPCOM(immrVal) = SPI_STR;
- /* Wait for reading to finish. */
- while (pSpi->pRxBdBase->statusMode & SPI_TRANS_RX_BD_EMPTY)
- {
- /* Busy-wait. */
- ;
- }
- /* De-assert chip select (CS). */
- DS1394CSDis();
- return;
- } /* end of Ds1394nByteWrite */
- /******************************************************************************
- *
- * Ds1394WriteByte - write a byte to NVRAM
- *
- * This routine writes one byte to NVRAM
- *
- * RETURNS: none
- *
- * NOMANUAL
- */
- //LOCAL Ds1394WriteByte(UCHAR writeadds,UCHAR writebyte)
- //{
- // SPI_DEV* pSpi;
- // pSpi = &spiDev;
- // int immrVal=vxImmrGet();
- // /* Wait for transmit to finish (first). */
- // while (pSpi->pTxBdBase->statusMode & SPI_TRANS_TX_BD_READY)
- // {
- // ;
- // }
- // /* Disable SPI (serial port interface). */
- // * M8260_SPI_SPMODE(immrVal) = DisSPI; /* Clear BSY */
- // //写发送数据
- // spiTxBuf[0]=writeadds;
- // spiTxBuf[1]=writebyte;
- // /* Set up transmit buffer descriptor. */
- // pSpi->pTxBdBase->dataPointer = spiTxBuf;
- // pSpi->pTxBdBase->dataLength = 2;
- // pSpi->pTxBdBase->statusMode = ( SPI_TRANS_TX_BD_READY |
- // SPI_TRANS_TX_BD_WRAP |
- // SPI_TRANS_TX_BD_LAST );
- /* Set SPI read value. */
- // pSpi->pPram->mrblr = 2;
- /* Set up receive buffer descriptor. */
- // pSpi->pRxBdBase->dataPointer = spiRxBuf;
- // pSpi->pRxBdBase->statusMode = ( SPI_TRANS_RX_BD_WRAP |
- // SPI_TRANS_RX_BD_EMPTY );
- /* issue start command*/
- // while ((*M8260_CPCR(immrVal) & M8260_CPCR_FLG) != 0);
- // * M8260_CPCR(immrVal) = M8260_CPCR_OP(M8260_CPCR_RT_INIT) |
- // M8260_CPCR_SBC(M8260_CPCR_SBC_SPI) |
- // M8260_CPCR_PAGE(M8260_CPCR_PAGE_SPI) |
- // M8260_CPCR_FLG;
- // while ((*M8260_CPCR(immrVal) & M8260_CPCR_FLG) != 0);
- /* Enable SPI. */
- // * M8260_SPI_SPMODE(immrVal) = EnSPI;
- /* Assert chip select (CS). */
- // DS1394CSEn();
- /* Start transfer. */
- // * M8260_SPI_SPCOM(immrVal) = SPI_STR;
- /* Wait for reading to finish. */
- // while (pSpi->pRxBdBase->statusMode & SPI_TRANS_RX_BD_EMPTY)
- // {
- /* Busy-wait. */
- // ;
- // }
- /* De-assert chip select (CS). */
- // DS1394CSDis();
- // return;
- //} /* end of sysNvRamWriteByte */
- /* ds1394 test. */
- UCHAR read_from_ds1394[SPI_RX_BUF_SZ];
- UCHAR write_to_ds1394[SPI_TX_BUF_SZ];
- void ds1394_test(void)
- {
- STATUS status;
- int strLen;
- UCHAR cmdadds;
- sysHwSpiInit();
- cmdadds=0x8d;
- strLen=0x01;
- write_to_ds1394[0]=0x00;
- status=Ds1394nByteWrite(cmdadds,&write_to_ds1394[0],strLen);
- cmdadds=0x8e;
- strLen=0x01;
- write_to_ds1394[0]=0x00;
- status=Ds1394nByteWrite(cmdadds,&write_to_ds1394[0],strLen);
- cmdadds=0x8f;
- strLen=0x01;
- write_to_ds1394[0]=0x00;
- status=Ds1394nByteWrite(cmdadds,&write_to_ds1394[0],strLen);
- cmdadds=0x00;
- strLen=0x0d;
- status=Ds1394nByteRead(cmdadds,&read_from_ds1394[0],strLen);
- if(strLen){
- cmdadds=0x80;
- write_to_ds1394[0]=0x00;
- write_to_ds1394[1]=0x48;
- write_to_ds1394[2]=0x36;
- write_to_ds1394[3]=0x09;
- write_to_ds1394[4]=0x02;
- write_to_ds1394[5]=0x26;
- write_to_ds1394[6]=0x11;
- write_to_ds1394[7]=0x12;
- strLen=0x08;
- status=Ds1394nByteWrite(cmdadds,&write_to_ds1394[0],strLen);
- }
- cmdadds=0x00;
- strLen=0x10;
- status=Ds1394nByteRead(cmdadds,&read_from_ds1394[0],strLen);
- while(1)
- {
- cmdadds=0x00;
- strLen=0x10;
- status=Ds1394nByteRead(cmdadds,&read_from_ds1394[0],strLen);
- }
- //STATUS Ds1394nByteRead(UCHAR cmdadds,char* string,int strLen)
- //LOCAL void Ds1394nByteWrite(UCHAR writeadds,char* string,int strLen)
- /*
- printf("About to init SPI.\n");
- sysHwSpiInit();
- printf("About to read contents of entire EEPROM.\n");
- status = sysNvRamGet(&read_from_EEPROM[0], SPI_SERIAL_EEPROM_SZ, 0);
- printf("Status of sysNvRamGet is %d.\n", status);
- printf("Contents of EEPROM are: \n");
- for (i = 0; i < 64; i++)
- {
- printf("\n%x %x %x %x",
- *((UINT*) &read_from_EEPROM[i * 16]),
- *((UINT*) &read_from_EEPROM[(i * 16) + 4]),
- *((UINT*) &read_from_EEPROM[(i * 16) + 8]),
- *((UINT*) &read_from_EEPROM[(i * 16) + 12]));
- }
- for (i = 0; i < SPI_SERIAL_EEPROM_SZ; i++)
- {
- write_to_EEPROM = (unsigned char)0;
- }
- printf("About to write contents of entire EEPROM.\n");
- status = sysNvRamSet(&write_to_EEPROM[0], SPI_SERIAL_EEPROM_SZ - offset,offset);
- printf("Status of sysNvRamSet is %d.\n", status);
- printf("About to read contents of entire EEPROM.\n");
- status = sysNvRamGet(&read_from_EEPROM[0], SPI_SERIAL_EEPROM_SZ, 0);
- for (i = 0; i < SPI_SERIAL_EEPROM_SZ; i++)
- {
- if (read_from_EEPROM != write_to_EEPROM)
- {
- printf("\n%d: 0x%02x != 0x%02x ", i, read_from_EEPROM,write_to_EEPROM);
- }
- }
- printf("\nDONE");
- */
- //return;
- }
- /* end of sysSpiNvRam.c */