/*
===============================================================================
Name : main.c
Author :
Version :
Copyright : Copyright (C)
Description : main definition
===============================================================================
*/
#ifdef __USE_CMSIS
#include "LPC17xx.h"
#endif
#include <cr_section_macros.h>
#include <NXP/crp.h>
#include "leds.h"
#include "timer.h"
#include "Systemtick_Delay.h"
#include "lpc17xx_ssp.h" // added for eeprom
//Defines for spi
#define SSP_CHANNEL LPC_SSP1
#define PORT_CSLPC_GPIO2
#define PIN_MASK_CS (1<<2)
//Define instructions of 25LC512 EEPROM
#define WREN 0x06 // write enable
#define WRDI 0x04 // write disable
#define WRITE 0x02 // initialize start of write sequence
#define READ 0x03 // initialize start of read sequence
#define CE 0xc7 // erase all sectors of memory
#define RDSR 0x05 // read STATUS register
// NOTE: #define PLL0CFG_Val 0x00090063 ensure this is the case in CMISvp00_17xx under system_LPCxx.c
// see user manual section 4.5 for more information
int main(void) {
led2_init();// Setup GPIO for LED2
led2_on();
SysTick_Init();
unsigned char readSPIvalue=0;
//Initialize timer
//init_timer( 0, TIMER0_INTERVAL );
//enable_timer( 0 );
//eeprom spi set up
SSP_CFG_Type sspChannelConfig;
// see pg 108/840 in datasheet, Pin Function Select register 0
LPC_PINCON->PINSEL0 |= 0x2<<14; //SCK1
LPC_PINCON->PINSEL0 |= 0x2<<18;//MOSI1
LPC_PINCON->PINSEL0 |= 0x2<<16;//MISO1
PORT_CS->FIODIR |= 1<<2;//P2.2 as CSn
SSP_ConfigStructInit(&sspChannelConfig);
SSP_Init(SSP_CHANNEL, &sspChannelConfig);
SSP_Cmd(SSP_CHANNEL, ENABLE);
// config EEPROM to enable writing
PORT_CS->FIOCLR |= PIN_MASK_CS;//CS low
SSP_SendData(SSP_CHANNEL,WREN); // write enable command
PORT_CS->FIOSET |= PIN_MASK_CS; //CS High
PORT_CS->FIOCLR |= PIN_MASK_CS;//CS low
SSP_SendData(SSP_CHANNEL,WRITE);// write command
SSP_SendData(SSP_CHANNEL,0x00); // write address
SSP_SendData(SSP_CHANNEL,0x00); // write address
SSP_SendData(SSP_CHANNEL,100); // Data to send
PORT_CS->FIOSET |= PIN_MASK_CS; //CS High
//------------------------ Main Loop---------------------------
while(1) {
if(readSPIvalue==100) // initially it is 0
{
//make LED blink if eeprom read
led2_invert();// Toggle state of LED2
}
else
{
//checkEEPROM_Busy();
PORT_CS->FIOCLR |= PIN_MASK_CS;//CS low
SSP_SendData(SSP_CHANNEL,READ); // read command
SSP_SendData(SSP_CHANNEL,0x00); // byte 1 16bit address to read
SSP_SendData(SSP_CHANNEL,0x00); // byte 2 16bit address to read
SSP_SendData(SSP_CHANNEL,0x00); // clock data out for reading
readSPIvalue=SSP_ReceiveData(SSP_CHANNEL);
PORT_CS->FIOSET |= PIN_MASK_CS; //CS High
}
systick_delay(200); // wait 2 seconds (2000ms)
//led2_invert();// Toggle state of LED2
}
return 0 ;
}
|
void SSP_ConfigStructInit(SSP_CFG_Type *SSP_InitStruct)
{
SSP_InitStruct->CPHA = SSP_CPHA_FIRST;
SSP_InitStruct->CPOL = SSP_CPOL_HI;
SSP_InitStruct->ClockRate = 1000000;
SSP_InitStruct->Databit = SSP_DATABIT_8;
SSP_InitStruct->Mode = SSP_MASTER_MODE;
SSP_InitStruct->FrameFormat = SSP_FRAME_SPI;
}
|