Spi.c
Upload User: yzchenlin
Upload Date: 2022-03-09
Package Size: 712k
Code Size: 2k
Development Platform:

C/C++

  1. #include <string.h>
  2. #include "2410lib.h"
  3. #include "spi.h"
  4. #include "def.h"
  5. #define spi_count 0x80
  6. #define SPI_BUFFER _NONCACHE_STARTADDRESS
  7. volatile char *spiTxStr,*spiRxStr;
  8. volatile int endSpiTx;
  9. /****************************************************************
  10.  *              SMDK2400 SPI configuration                 *
  11.  *  GPG2=nSS0, GPE11=SPIMISO0, GPE12=SPIMOSI0, GPE13=SPICLK0     *
  12.  *  GPG3=nSS1, GPG5 =SPIMISO1, GPG6 =SPIMOSI1, GPG7 =SPICLK1      *
  13.  *  SPI1 is tested by OS(WINCE). So, Only SPI0 is tested by this code          *               
  14.  ****************************************************************/
  15. void Test_Spi_MS_poll(void)
  16. {
  17.     int i=0;
  18.     char *txStr,*rxStr;
  19. //    SPI_Port_Init(0); 
  20. rGPEUP&=~(0x3800);
  21.     rGPEUP|=0x2000;
  22.     rGPECON=((rGPECON&0xf03fffff)|0xa800000);
  23.     rGPGUP|=0x4;
  24.     rGPGCON=((rGPGCON&0xffffffcf)|0x30); // Slave(nSS)
  25.     
  26.     Uart_Printf("[SPI Polling Tx/Rx Test]n");
  27.     Uart_Printf("Connect SPIMOSI0 into SPIMISO0.n");
  28.     endSpiTx=0;
  29.     spiTxStr="ABC";
  30.     spiRxStr=(char *) SPI_BUFFER;
  31.     txStr=(char *)spiTxStr;
  32.     rxStr=(char *)spiRxStr;
  33. Uart_Printf("[Test point 1]n");
  34.     rSPPRE0=0x0; //if PCLK=50Mhz,SPICLK=25Mhz
  35.     rSPCON0=(0<<5)|(1<<4)|(1<<3)|(1<<2)|(0<<1)|(0<<0);//Polling,en-SCK,master,low,A,normal
  36.     rSPPIN0=(0<<2)|(1<<1)|(0<<0);//dis-ENMUL,SBO,release
  37.     Uart_Printf("[Test point 1]n");
  38.     while(endSpiTx==0)
  39.     {
  40. if(rSPSTA0&0x1)   //Check Tx ready state    
  41. {
  42.      if(*spiTxStr!='')
  43. rSPTDAT0=*spiTxStr++;
  44.      else
  45. endSpiTx=1;
  46.      while(!(rSPSTA0&0x1));   //Check Rx ready state 
  47. *spiRxStr++=rSPRDAT0;
  48. }
  49. i=i+1;
  50. Uart_Printf("[Test point %d]n",i);
  51.     }
  52.     rSPCON0=(0<<5)|(0<<4)|(1<<3)|(1<<2)|(0<<1)|(0<<0);//Polling,dis-SCK,master,low,A,normal
  53.     *(spiRxStr-1)='';//remove last dummy data & attach End of String(Null)
  54. //
  55. Port_Init();
  56.     Uart_Init(0,115200);
  57.     Uart_Select(0);
  58. //    
  59.     Uart_Printf("Tx Strings:%sn",txStr);
  60.     Uart_Printf("Rx Strings:%s :",rxStr);
  61.     
  62.     if(strcmp(rxStr,txStr)==0)
  63.         Uart_Printf("O.K.n");
  64.     else 
  65.         Uart_Printf("ERROR!!!n");
  66. }