Jump to content
43oh

interfacing MSP430g2553 and at45db041b


Recommended Posts

 Hi guys!

I am trying to interface with SPI flash,I am trying to write byte to buffer 1 and after read it,

The byte i have send and recived are not the same. Cant figure out my fault.

/*
 * main.c
 */
#include "msp430.h"
#include "stdint.h"

#define SPIRXBUF  UCB0RXBUF
#define SPI_SEND(x) UCB0TXBUF=x
#define SPITXREADY  (IFG2&UCB0TXIFG)     /* Wait for TX to be ready */
#define SPITXDONE  (UCB0STAT&UCBUSY)       /* Wait for TX to finish */
#define SPIRXREADY (IFG2&UCB0RXIFG)      /* Wait for TX to be ready */
#define SPIRXFG_CLR IFG2 &= ~UCB0RXIFG
#define dummy 0xff
#define f_on P1OUT&=~BIT4    //CS pin
#define f_off P1OUT|=BIT4

int data1;
char data;

void f_buf_write(int buf_nb,char data,int adress)
{
	f_on;
	switch(buf_nb)                          //choosing buffer to write
		{case 1:
			SPI_SEND(0x84);
			break;
		case 2:
			SPI_SEND(0x87);
			break;
		}
	while (!SPITXREADY);   //waitting untill TXvuffer will be ready
	SPI_SEND(dummy);
	while (!SPITXREADY);
	SPI_SEND(adress>>8);
	while (!SPITXREADY);
	SPI_SEND(adress);
	while (!SPITXREADY);
	SPI_SEND(data);
	while(SPITXDONE);    //waitting untill TX will be done
	f_off;
}

int f_buf_read(int buf_nb,char data,int adress)
{
	f_on;
	switch(buf_nb)                   //choosing buffer to read
		{case 1:
			SPI_SEND(0xD4);
			break;
		case 2:
			SPI_SEND(0x56);
			break;
		}
	while(!SPITXREADY);
	SPI_SEND(dummy);
	while(!SPITXREADY);
	SPI_SEND(adress>>8);
	while(!SPITXREADY);
	SPI_SEND(adress);
	while(!SPITXREADY);
	SPI_SEND(dummy);
	while(!SPITXREADY);
	SPI_SEND(dummy);    //transition starts here
	while(!SPIRXREADY); //whait while RX-buffer will got value
	data=SPIRXBUF;      // Breakpoint toggled here
	data1=(int)data;
	f_off;
	return data1;
}

void main(void) {
	P2SEL=0;
	P2SEL2=0;
	P2DIR&=~BIT0;
	P2OUT&=~BIT0;
	P1DIR|=BIT4|BIT0;
	P1OUT|=BIT4;  //  CS f_off
	P1SEL|=BIT5|BIT6|BIT7;
	P1SEL2|=BIT5|BIT6|BIT7;
	UCB0CTL0 = UCMST+UCMSB+UCSYNC+UCCKPL+UCCKPH;     // 3-pin, 8-bit SPI master;MSB-1st; mode 3 spi
	UCB0CTL1 = UCSSEL_2+UCSWRST;              // SMCLk
	UCB0BR0 |= 0x0F;                          //
	UCB0BR1 = 0;
	UCB0CTL1 &= ~UCSWRST;  //start SPI

	f_buf_write(1,0xa5,0);
	data1=f_buf_read(1,data,0);
}

datasheet is here:http://www.atmel.com/images/doc3443.pdf

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...