hello sir ,
i have used TWR-K20D72 freescale board. in this board i connect CAT25256 (EPROM). i have write data in eeprom memory but faced a problem because of my initilization problem also problem of data write and read.
my system clock frequency is 72mhz. i have set 1 mhz baud rate for spi and how to set it.
#include "common.h"
/* ================== EEPROM command macros ==================*/
#define WR_EN 0x06
#define WR_DI 0x04
#define RD_SR 0x05
#define WR_SR 0x01
#define READ 0x03
#define WRITE 0x02
#define MAX_PAGE_SIZE 64
#define CS_ENABLE GPIOD_PCOR |= (1 << 4)
#define CS_DISABLE GPIOD_PSOR |= (1 << 4)
#define CS_GPIO_CONFIG PORTD_PCR4 = PORT_PCR_MUX(0x1)
#define CS_DIR_OUT GPIOD_PDDR |= (1 << 4)
#define PCS0 0x1
#define DATA 'A'
#define ADDRESS 0x0000
/*=============== Functions declaration ====================*/
void spi_init(void);
char spi_send(char);
void delay(void);
void isDeviceReady(void);
void my_delay(void);
void eeprom_write(int address);
char eeprom_read(int address);
static char tmp;
///please write a correct spi_send byte function
char spi_send(char Txfifo)
{
SPI0_MCR &= ~SPI_MCR_HALT_MASK; //Start Transfer
SPI0_PUSHR = Txfifo;
SPI0_MCR |= SPI_MCR_HALT_MASK; //Stop Transfer
while(!(SPI0_SR & SPI_SR_TCF_MASK)); //Wait for transfer complete flag
SPI0_MCR |= SPI_MCR_CLR_TXF_MASK;
tmp=SPI0_POPR;
return tmp;
}
/////please write a correct spi_receive byte function
uint8_t spi_receive()
{
SPI0_MCR |= SPI_MCR_CLR_RXF_MASK;
SPI0_SR |= SPI_SR_TCF_MASK;
SPI0_PUSHR = 0x00;
//while (!(SPI0_SR & SPI_SR_TCF_MASK)) {}
return ((char)SPI0_POPR);
}
void spi_init(void)
{
SIM_SCGC5 |= SIM_SCGC5_PORTD_MASK; //Turn on clock to D module
SIM_SCGC6 |= SIM_SCGC6_SPI0_MASK; //Enable SPI0 clock
CS_GPIO_CONFIG; // Configured GPIO
CS_DIR_OUT; // Select CS pin as Output direction
CS_DISABLE; // Disable CS
PORTD_PCR1 = PORT_PCR_MUX(0x2); //Set PTC5 to mux 2 [SPI0_SCK]
PORTD_PCR2 = PORT_PCR_MUX(0x2); //Set PTC6 to mux 2 [SPI0_MOSI]
PORTD_PCR3 = PORT_PCR_MUX(0x2); //Set PTC7 to mux 2 [SPIO_MISO]
SPI0_MCR |= (SPI_MCR_MSTR_MASK | //DSPI is in Master mode.
SPI_MCR_HALT_MASK); //Stop transfers
SPI0_MCR &= ~SPI_MCR_PCSIS(1);
// how to set 1 mhz???please write a correction
SPI0_CTAR0 |= (SPI_CTAR_PBR(3) | SPI_CTAR_BR(0x04));
//Frame Size = 8 bits
SPI0_CTAR0 &= ~SPI_CTAR_FMSZ(7);
SPI0_MCR &= ~SPI_MCR_MDIS_MASK; //Enable module
}
/* ============ Send Write Enable Command =========== */
void write_enable(void)
{
CS_ENABLE;
my_delay();
spi_send(WR_EN); // Send Write Enable Command
my_delay();
CS_DISABLE;
}
/* ============ Send Write Disable Command =========== */
void write_disable(void)
{
CS_ENABLE;
my_delay();
spi_send(WR_DI); // Send Write Enable Command
my_delay();
CS_DISABLE;
}
void eeprom_write(int address)
{
// Send Write Enable Comamnd
write_enable();
// Sequence of Write Command
CS_ENABLE;
my_delay();
spi_send(WRITE); // Send writing command to EEPROM
spi_send(address >> 8); // Send Writing address
spi_send(address); // Send LSB Address
spi_send(DATA);
my_delay();
// Disable Chip Selection Pin
CS_DISABLE;
// Delay for processing Write byte into EEPROM location
// Disable Write
write_disable();
my_delay();
}
char eeprom_read(int address)
{
static char temp;
// Enable Chip selection pin
CS_ENABLE;
my_delay();
spi_send(READ); // Send Read command
spi_send(address >> 8); // Send address from where to read form EEPROM
spi_send(address); // Send LSB Address
temp=spi_receive(0xff);
CS_DISABLE;
my_delay();
CS_ENABLE;
my_delay();
// Disable Chip selection pin
CS_DISABLE;
return temp;
}
void my_delay(void)
{
volatile uint32_t delayTicks = 2000;
while(delayTicks--)
{
__asm("NOP");
}
}
void delay(void)
{
volatile uint32_t delayTicks = 2000000;
while(delayTicks--)
{
__asm("NOP");
}
}
void main()
{
char data;
spi_init();
delay();
// Write multiple bytes into EEPROM
eeprom_write(ADDRESS);
// Read multiples bytes from EEPROM
data=eeprom_read(ADDRESS);
while(1);
}
sir, i am currently used IAR embedded workbatch software.
In this how i can set 1MHZ clock frequency.
how SPI0_PUSH & SPI0_POPR worked???
Hello Harshad,
Please refer to the calculation formula of Reference manual
Have a great day,
Alice
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Hello harshad,
Are you sure the the core/system clock is 72M?
I recommend you create on PE project ,then add the component of SPI,
there is demo code about SPI and the easy configuration :
About how to find these demos you can refer to this DOC:
How to Start CAN Module Development on KDS v3.2.0 + Processor Expert (this is the same with CW)
Hope it helps
Alice