Guest User

Untitled

a guest
Jan 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.02 KB | None | 0 0
  1. #include "SPI_Flash.h"
  2.  
  3.  
  4. void CS_Sel(CS_Signal s)
  5. {
  6.     IOSET0 = (1 << 10); /* SD:    High to Deselect */
  7.     IOSET0 = (1 << 12); /* Flash: High to Deselect */
  8.     IOCLR1 = (1 << 29); /* MP3:   Low to Deselect */
  9.  
  10.     switch(s)
  11.     {
  12.         case sel_SD:    IOCLR0 = (1 << 10); break; /* Active Low = LOW to sel */
  13.         case sel_Flash: IOCLR0 = (1 << 12); break; /* Active Low = LOW to sel */
  14.         case sel_MP3:   IOSET1 = (1 << 29); break; /* Active High = HIGH to sel */
  15.  
  16.         case sel_Nothing:
  17.         default:
  18.             break;
  19.     }
  20. }
  21.  
  22.  
  23. void CS_Init()
  24. {
  25.     IODIR0 |= (1 << 10); // SD: P0.10
  26.     IODIR0 |= (1 << 12); // Flash: P0.12
  27.     IODIR1 |= (1 << 29); // MP3: P1.29
  28.  
  29.     CS_Sel(sel_Nothing);
  30.  
  31.     CS_Sel(sel_SD);    delay_ms(1000);
  32.     CS_Sel(sel_Flash); delay_ms(1000);
  33.     CS_Sel(sel_MP3);   delay_ms(1000);
  34.     CS_Sel(sel_Nothing);
  35.  
  36.     ssp_SPI_Init();
  37. }
  38.  
  39. void ssp_SPI_Init()
  40. {
  41.     //Set up pin selection struct for the SPI, MISO, and MOSI pins
  42.     /*pinSelect myPin1;
  43.     myPin1.data = 0;
  44.     myPin1.pins.p1 = 2;
  45.     myPin1.pins.p2 = 2;
  46.     myPin1.pins.p3 = 2;*/
  47.  
  48.     //PINSEL1 |= myPin1.data; //Set P0.17 to SCK, P0.18 to MISO, P0.19 to MOSI
  49.     PINSEL1 |= (2 << 2);
  50.     PINSEL1 |= (2 << 4);
  51.     PINSEL1 |= (2 << 6);
  52.  
  53.     SSPCR0 = (7 << 0); //Set 8 bit frame, SPI format, don't change clock polarity/phase, SCR value
  54.     SSPCR1 = (1 << 1); //Enable SSP Controller
  55.  
  56.     SSPCPSR = 4; // CPSDVSR = 4 because we want 12 MHz = 48Mhz/CPSDVSR
  57. }
  58.  
  59. char SPI_Exch(char sendMe)
  60. {
  61.     //rprintf("Hang here.\n");
  62.     SSPDR = sendMe; //or |=?
  63.     while (((SSPSR & (1 << 4))))
  64.     {
  65.         //rprintf("Hang here.\n");
  66.         //Stay on target...!
  67.     }
  68.     return SSPDR;
  69. }
  70.  
  71. void globalWriteEnable(void)
  72. {
  73.     SPI_busy();
  74.  
  75.     CS_Sel(sel_Flash);
  76.     SPI_Exch(0x06); //Set WEL (write enable latch) to 1;
  77.     CS_Sel(sel_Nothing);
  78.     delay_ms(50);
  79.     CS_Sel(sel_Flash);
  80.     SPI_Exch(0x01); //Writing to Status Register
  81.     SPI_Exch(0x80); //Set Status Register to Unprotected
  82.     CS_Sel(sel_Nothing);
  83.  
  84.     SPI_busy();
  85. }
  86.  
  87. void writeEnable(void)
  88. {
  89.     SPI_busy();
  90.  
  91.     CS_Sel(sel_Flash);
  92.     SPI_Exch(0x06);
  93.     CS_Sel(sel_Nothing);
  94.  
  95.     SPI_busy();
  96. }
  97.  
  98. void SPI_Block_Erase(int page)
  99. {
  100.     writeEnable();
  101.  
  102.     SPI_busy();
  103.  
  104.     rprintf("Erasing page %i.\n", page);
  105.     bytes address;
  106.     address.data = page*256;
  107.     CS_Sel(sel_Flash);
  108.     SPI_Exch(0x20);
  109.     /*SPI_Exch(address.bs.b0);
  110.     SPI_Exch(address.bs.b1);
  111.     SPI_Exch(address.bs.b2);*/
  112.     SPI_Exch(0x00);
  113.     SPI_Exch(0x00);
  114.     SPI_Exch(0x00);
  115.  
  116.     CS_Sel(sel_Nothing);
  117.  
  118.     SPI_busy();
  119. }
  120.  
  121. void SPI_busy(void)
  122. {
  123.     //rprintf("Hang here...\n");
  124.     char status = 0xFF;
  125.     CS_Sel(sel_Flash);
  126.     SPI_Exch(0x05); //Read Status Register
  127.     do
  128.     {
  129.         //rprintf("Hang here...\n");
  130.         status = SPI_Exch(0x00); //Dummy data to read
  131.     } while  ((status & (1 << 0)));
  132.  
  133.     CS_Sel(sel_Nothing);
  134. }
  135.  
  136. void SPI_write(int page, char data[], int length)
  137. {
  138.     writeEnable();
  139.  
  140.     rprintf("Writing...%d, %s, %d\n", page, data, length);
  141.     SPI_busy();
  142.     int i = 0;
  143.     if(length > 256)
  144.     {
  145.         rprintf("------------------Your data is too long for a page.-------------------------\n");
  146.         return;
  147.     }
  148.  
  149.     bytes address;
  150.     address.data = page*256;
  151.  
  152.     CS_Sel(sel_Flash);
  153.     SPI_Exch(0x02);
  154.     /*SPI_Exch(address.bs.b0);
  155.     SPI_Exch(address.bs.b1);
  156.     SPI_Exch(address.bs.b2);*/
  157.     SPI_Exch(0x00);
  158.     SPI_Exch(0x00);
  159.     SPI_Exch(0x00);
  160.  
  161.     for (i = 0; i < length; i++)
  162.     {
  163.         SPI_Exch(data[i]);
  164.         rprintf("%c, %d, %d\n", data[i], i, length);
  165.     }
  166.  
  167.     CS_Sel(sel_Nothing);
  168.  
  169.     SPI_busy();
  170. }
  171.  
  172. void SPI_read(int address, int length, char *ret)
  173. {
  174.     SPI_busy();
  175.  
  176.     int i = 0;
  177.     bytes add;
  178.     add.data = address*256;
  179.     CS_Sel(sel_Flash);
  180.     SPI_Exch(0x0B);
  181.     /*SPI_Exch(add.bs.b0);
  182.     SPI_Exch(add.bs.b1);
  183.     SPI_Exch(add.bs.b2);*/
  184.     SPI_Exch(0x00);
  185.     SPI_Exch(0x00);
  186.     SPI_Exch(0x00);
  187.  
  188.     SPI_Exch(0x00);
  189.     for (i = 0; i < length; i++)
  190.     {
  191.         //rprintf(SPI_Exch(0x00));
  192.         ret[i] = SPI_Exch(0x00);
  193.         rprintf("%c, %d, %d\n", ret[i], i, length);
  194.     }
  195.     CS_Sel(sel_Nothing);
  196.  
  197.     SPI_busy();
  198. }
  199.  
  200. void delay_ms(unsigned long count)
  201. {
  202.     volatile unsigned long c = count * 4000;
  203.  
  204.     // Each loop iteration should be:
  205.     // 2 LOADS, 1 STORE, 1 CMP, 1 SUB, and 1 BRANCH
  206.     // Around 16 instructions?
  207.     while (--c)
  208.         ;
  209. }
Add Comment
Please, Sign In to add comment