Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "lpc2xxx.h" ///< LPC Header Definitions
- #include "uart0.h" ///< Uart0 Header file to initialize UART0
- #include "rprintf.h" ///< Reduced printf. STDIO uses lot of FLASH space & Stack space.
- #include "lowLevel.h"
- #include "SPI_Flash.h"
- #include <stdbool.h>
- #include <string.h>
- #include <stdio.h>
- #include <malloc.h>
- /// Very rough delay of 1 ms. Assuming 48Mhz CPU Clock.
- //void delay_ms(unsigned long count); //Moved to SPI_Flash...
- void initLED(void);
- void initSwitch(void);
- void blinkLED(void);
- void initSDCDWP(void);
- void checkCDWP(void);
- void lab6(void);
- int main(void)
- {
- lowLevelInit();
- wakeupInit();
- exceptionHandlerInit();
- CS_Init();
- ssp_SPI_Init();
- //int i = 0;
- init_Uart0(38400);
- rprintf_devopen(uart0_putchar);
- delay_ms(2000);
- rprintf("\n--------------------------Mike's LPC2148!--------------------------------\n");
- rprintf("-- System Boot complete --\n");
- rprintf("-------------------------------------------------------------------------\n");
- /*printf("You can use printf but it will bloat your code!!!\n");
- printf("Only STDIO printf can print floating-point numbers: %f\n", 1.23f);
- */
- delay_ms(1000);
- printf("Test Memory allocation of 1024 bytes: ");
- char *memPointer = malloc(1024);
- printf("Memory pointer = %p\n", memPointer);
- //Simple I/O Check--Hello World
- /*while (1)
- {
- rprintf("-- Hello World %i --\n", ++i);
- delay_ms(1000);
- }*/
- //LAB2
- /*char name[30];
- int truth = 1;
- int temp = 0;*/
- /*while(truth == 1)
- {
- rprintf("Hello, and welcome! I am Mike's LPC2148. What's your name?"); //ask for a name
- scanf("%s", name ); //capture name
- rprintf("Oh, your name is %s? Nice to meet you!", name); //reply name
- rprintf("Would you like to reintroduce yourself? 1 for yes, or anything else for exit.\n");
- if(scanf("%d", &temp))
- {
- truth = temp;
- }
- else
- {
- printf("You're done.\n");
- truth = 0;
- }
- }*/
- //LAB3
- /*initLED();
- initSwitch();
- initSDCDWP();
- //Basic LED/Switch check
- int j = 0;
- for( j = 0; j < 20; j++)
- {
- delay_ms(1000);
- blinkLED();
- }
- //Check SD Card Detection/Write Protection
- for(j = 0 ; ;j++ )
- {
- delay_ms(500);
- rprintf("%d ", j);
- checkCDWP();
- }*/
- rprintf("DOING LAB 6\n");
- lab6();
- rprintf("------------------------------EXIT-------------------------------------");
- return 0;
- }
- //Moved to SPI_Flash
- /*void delay_ms(unsigned long count)
- {
- volatile unsigned long c = count * 4000;
- // Each loop iteration should be:
- // 2 LOADS, 1 STORE, 1 CMP, 1 SUB, and 1 BRANCH
- // Around 16 instructions?
- while (--c)
- ;
- }*/
- //Enable output on P0.7
- void initLED()
- {
- PINSEL0 &= ~(1 << 15);
- PINSEL0 &= ~(1 << 14);
- IODIR0 |= (1 << 7);
- rprintf("P0.7 initialized for LED.\n");
- }
- //Enable input on P0.8
- void initSwitch()
- {
- PINSEL0 &= ~(1 << 17);
- PINSEL0 &= ~(1 << 16);
- IODIR0 &= ~(1 <<8);
- rprintf("P0.8 initialized for Switch input.\n");
- }
- //Set input from SD Card Detect and Write Protect (P0.16 and P0.21, respectively)
- void initSDCDWP()
- {
- PINSEL1 &= ~(1 << 0);
- PINSEL1 &= ~(1 << 1);
- PINSEL1 &= ~(1 << 2);
- PINSEL1 &= ~(1 << 3);
- IODIR0 &= ~(1 << 16);
- IODIR0 &= ~(1 << 21);
- rprintf("SD Card Detect and Write-Protect initialized...\n");
- }
- //Blink LED depending on state of switch
- void blinkLED()
- {
- if (IOPIN0 & (1 << 8))
- {
- IOSET0 = (1 << 7);
- rprintf("LED ON\n");
- }
- else
- {
- IOCLR0 = (1 << 7);
- rprintf("LED OFF\n");
- }
- }
- void checkCDWP()
- {
- if (!(IOPIN0 & (1 << 16)))
- {
- rprintf("SD Card Detected... \n");
- if (!(IOPIN0 & (1 << 21)))
- {
- rprintf("Card is not Write-Protected.\n\n");
- }
- else
- rprintf("Card is Write-Protected!\n\n");
- }
- else
- rprintf("No SD Card detected... \n");
- }
- /*typedef union
- {
- struct
- {
- char byte0;
- char byte1;
- char byte2;
- char byte3;
- }bytes;
- int raw;
- }intSplitter;*/
- /*SPI_flash_Test(int address, char* testWrite, int length)
- {
- intsplitter myAddress;
- myAddress.raw = 256*1024;
- //send these in order
- myAddress.bytes.byte2
- myAddress.bytes.byte1
- myAddress.bytes.byte0
- }*/
- void lab6(void)
- {
- int address = 0;
- unsigned char run = 1;
- char option = '5';
- char phrase[256] = {0};
- char * phrasePtr = phrase;
- char tempPhrase[256] = {0};
- globalWriteEnable();
- while(run == 1)
- {
- rprintf("What would you like to do?");
- rprintf("\n1. Write a message to the SPI Flash.");
- rprintf("\n2. Read a message from the SPI Flash.");
- rprintf("\n3. Erase the message from the SPI Flash.");
- rprintf("\n4. Exit.\n");
- scanf("%s",&option);
- switch(option)
- {
- case '1':
- rprintf("Enter the data to write to the memory.\n");
- scanf("%s", phrase);
- strcpy(&tempPhrase, &phrase);
- SPI_Block_Erase(address);
- delay_ms(500);
- SPI_read(address, 10, tempPhrase);
- SPI_write(address, phrase, strlen(phrase));
- break;
- case '2':
- SPI_read(address, 10, phrasePtr);
- rprintf("The phrase read was:\n%s.\n", phrasePtr);
- break;
- case '3':
- SPI_Block_Erase(address);
- break;
- case '4':
- default:
- run = 0;
- break;
- }
- }
- }
Add Comment
Please, Sign In to add comment