Guest User

Untitled

a guest
Jan 22nd, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.13 KB | None | 0 0
  1. #include "lpc2xxx.h"    ///< LPC Header Definitions
  2. #include "uart0.h"      ///< Uart0 Header file to initialize UART0
  3. #include "rprintf.h"    ///< Reduced printf.  STDIO uses lot of FLASH space & Stack space.
  4. #include "lowLevel.h"
  5. #include "SPI_Flash.h"
  6.  
  7.  
  8. #include <stdbool.h>
  9. #include <string.h>
  10.  
  11. #include <stdio.h>
  12. #include <malloc.h>
  13.  
  14. /// Very rough delay of 1 ms.  Assuming 48Mhz CPU Clock.
  15. //void delay_ms(unsigned long count); //Moved to SPI_Flash...
  16. void initLED(void);
  17. void initSwitch(void);
  18. void blinkLED(void);
  19. void initSDCDWP(void);
  20. void checkCDWP(void);
  21. void lab6(void);
  22.  
  23. int main(void)
  24. {
  25.     lowLevelInit();
  26.     wakeupInit();
  27.     exceptionHandlerInit();
  28.     CS_Init();
  29.     ssp_SPI_Init();
  30.  
  31.     //int i = 0;
  32.     init_Uart0(38400);
  33.     rprintf_devopen(uart0_putchar);
  34.  
  35.     delay_ms(2000);
  36.     rprintf("\n--------------------------Mike's LPC2148!--------------------------------\n");
  37.     rprintf("--                        System Boot complete                         --\n");
  38.     rprintf("-------------------------------------------------------------------------\n");
  39.  
  40.     /*printf("You can use printf but it will bloat your code!!!\n");
  41.     printf("Only STDIO printf can print floating-point numbers: %f\n", 1.23f);
  42.     */
  43.     delay_ms(1000);
  44.     printf("Test Memory allocation of 1024 bytes: ");
  45.     char *memPointer = malloc(1024);
  46.     printf("Memory pointer = %p\n", memPointer);
  47.  
  48.     //Simple I/O Check--Hello World
  49.     /*while (1)
  50.     {
  51.         rprintf("-- Hello World %i --\n", ++i);
  52.         delay_ms(1000);
  53.     }*/
  54.  
  55.     //LAB2
  56.     /*char name[30];
  57.     int truth = 1;
  58.     int temp = 0;*/
  59.  
  60.     /*while(truth == 1)
  61.     {
  62.         rprintf("Hello, and welcome! I am Mike's LPC2148. What's your name?"); //ask for a name
  63.         scanf("%s", name ); //capture name
  64.         rprintf("Oh, your name is %s? Nice to meet you!", name); //reply name
  65.         rprintf("Would you like to reintroduce yourself? 1 for yes, or anything else for exit.\n");
  66.         if(scanf("%d", &temp))
  67.         {
  68.             truth = temp;
  69.         }
  70.         else
  71.         {
  72.             printf("You're done.\n");
  73.             truth = 0;
  74.         }
  75.     }*/
  76.  
  77.     //LAB3
  78.  
  79.     /*initLED();
  80.     initSwitch();
  81.  
  82.     initSDCDWP();
  83.  
  84.     //Basic LED/Switch check
  85.     int j = 0;
  86.     for( j = 0; j < 20; j++)
  87.     {
  88.         delay_ms(1000);
  89.         blinkLED();
  90.     }
  91.  
  92.  
  93.     //Check SD Card Detection/Write Protection
  94.     for(j = 0 ; ;j++ )
  95.     {
  96.         delay_ms(500);
  97.         rprintf("%d ", j);
  98.         checkCDWP();
  99.     }*/
  100.  
  101.     rprintf("DOING LAB 6\n");
  102.     lab6();
  103.  
  104.     rprintf("------------------------------EXIT-------------------------------------");
  105.  
  106.     return 0;
  107. }
  108.  
  109. //Moved to SPI_Flash
  110. /*void delay_ms(unsigned long count)
  111. {
  112.     volatile unsigned long c = count * 4000;
  113.  
  114.     // Each loop iteration should be:
  115.     // 2 LOADS, 1 STORE, 1 CMP, 1 SUB, and 1 BRANCH
  116.     // Around 16 instructions?
  117.     while (--c)
  118.         ;
  119. }*/
  120.  
  121. //Enable output on P0.7
  122. void initLED()
  123. {
  124.     PINSEL0 &= ~(1 << 15);
  125.     PINSEL0 &= ~(1 << 14);
  126.  
  127.     IODIR0 |= (1 << 7);
  128.     rprintf("P0.7 initialized for LED.\n");
  129. }
  130.  
  131. //Enable input on P0.8
  132. void initSwitch()
  133. {
  134.     PINSEL0 &= ~(1 << 17);
  135.     PINSEL0 &= ~(1 << 16);
  136.  
  137.     IODIR0 &= ~(1 <<8);
  138.     rprintf("P0.8 initialized for Switch input.\n");
  139. }
  140.  
  141. //Set input from SD Card Detect and Write Protect (P0.16 and P0.21, respectively)
  142. void initSDCDWP()
  143. {
  144.     PINSEL1 &= ~(1 << 0);
  145.     PINSEL1 &= ~(1 << 1);
  146.     PINSEL1 &= ~(1 << 2);
  147.     PINSEL1 &= ~(1 << 3);
  148.  
  149.     IODIR0 &= ~(1 << 16);
  150.     IODIR0 &= ~(1 << 21);
  151.  
  152.     rprintf("SD Card Detect and Write-Protect initialized...\n");
  153. }
  154.  
  155. //Blink LED depending on state of switch
  156. void blinkLED()
  157. {
  158.     if (IOPIN0 & (1 << 8))
  159.     {
  160.         IOSET0 = (1 << 7);
  161.         rprintf("LED ON\n");
  162.     }
  163.     else
  164.     {
  165.         IOCLR0 = (1 << 7);
  166.         rprintf("LED OFF\n");
  167.     }
  168. }
  169.  
  170. void checkCDWP()
  171. {
  172.     if (!(IOPIN0 & (1 << 16)))
  173.     {
  174.         rprintf("SD Card Detected... \n");
  175.         if (!(IOPIN0 & (1 << 21)))
  176.         {
  177.             rprintf("Card is not Write-Protected.\n\n");
  178.         }
  179.         else
  180.             rprintf("Card is Write-Protected!\n\n");
  181.     }
  182.     else
  183.         rprintf("No SD Card detected... \n");
  184. }
  185.  
  186. /*typedef union
  187. {
  188.     struct
  189.     {
  190.         char byte0;
  191.         char byte1;
  192.         char byte2;
  193.         char byte3;
  194.     }bytes;
  195.  
  196.     int raw;
  197. }intSplitter;*/
  198.  
  199. /*SPI_flash_Test(int address, char* testWrite, int length)
  200. {
  201.     intsplitter myAddress;
  202.     myAddress.raw = 256*1024;
  203.     //send these in order
  204.     myAddress.bytes.byte2
  205.     myAddress.bytes.byte1
  206.     myAddress.bytes.byte0
  207. }*/
  208.  
  209. void lab6(void)
  210. {
  211.     int address = 0;
  212.     unsigned char run = 1;
  213.     char option = '5';
  214.     char phrase[256] = {0};
  215.     char * phrasePtr = phrase;
  216.     char tempPhrase[256] = {0};
  217.     globalWriteEnable();
  218.  
  219.     while(run == 1)
  220.     {
  221.         rprintf("What would you like to do?");
  222.         rprintf("\n1. Write a message to the SPI Flash.");
  223.         rprintf("\n2. Read a message from the SPI Flash.");
  224.         rprintf("\n3. Erase the message from the SPI Flash.");
  225.         rprintf("\n4. Exit.\n");
  226.         scanf("%s",&option);
  227.         switch(option)
  228.         {
  229.         case '1':
  230.                 rprintf("Enter the data to write to the memory.\n");
  231.                 scanf("%s", phrase);
  232.                 strcpy(&tempPhrase, &phrase);
  233.                 SPI_Block_Erase(address);
  234.                 delay_ms(500);
  235.                 SPI_read(address, 10, tempPhrase);
  236.                 SPI_write(address, phrase, strlen(phrase));
  237.                 break;
  238.         case '2':
  239.                 SPI_read(address, 10, phrasePtr);
  240.                 rprintf("The phrase read was:\n%s.\n", phrasePtr);
  241.                 break;
  242.         case '3':
  243.                 SPI_Block_Erase(address);
  244.                 break;
  245.         case '4':
  246.         default:
  247.                 run = 0;
  248.                 break;
  249.         }
  250.     }
  251. }
Add Comment
Please, Sign In to add comment