/*********************************************************************************************************
*
* File : ws_AT45DBXX.c
* Hardware Environment:
* Build Environment : RealView MDK-ARM Version: 4.20
* Version : V1.0
* By :
*
* (c) Copyright 2005-2011, WaveShare
* http://www.waveshare.net
* All Rights Reserved
*
*********************************************************************************************************/
#include "ws_AT45DBXX.h"
#include "debug_frmwrk.h"
#define BUF_SIZE 0x01 // 1 Byte
// SPI Data Setup structure variable
SSP_DATA_SETUP_Type dataCfg;
//void *tx_data; /**< Pointer to transmit data */
//uint32_t tx_cnt; /**< Transmit counter */
//void *rx_data; /**< Pointer to transmit data */
//uint32_t rx_cnt; /**< Receive counter */
//uint32_t length; /**< Length of transfer data */
//uint32_t status; /**< Current status of SSP activity */
uint8_t SPIx_communication(uint8_t data)
{
uint8_t touch_data_rx[BUF_SIZE]={0};//0x80;
uint8_t rl=1;
dataCfg.tx_data = &data; /**< Pointer to transmit data */
dataCfg.tx_cnt = BUF_SIZE; /**< Transmit counter */
dataCfg.rx_data = touch_data_rx; /**< Pointer to transmit data */
dataCfg.rx_cnt = BUF_SIZE; /**< Receive counter */
dataCfg.length = BUF_SIZE; /**< Length of transfer data */
//uint32_t status; /**< Current status of SSP activity */
rl = SSP_ReadWrite(LPC_SSPx,&dataCfg,SSP_TRANSFER_POLLING);
if(rl != 0)
{
//dataCfg.rx_data = &touch_data_rx;
touch_data_rx[0]=*(uint8_t *)dataCfg.rx_data;
return touch_data_rx[0];
//uprintf("[rx_data] = %xH\r\n",(*((uint8_t *)dataCfg.rx_data)));
//return (*((uint8_t *)dataCfg.rx_data));
}
return 0;
}
void spix_init(void)
{
//pin configuration P0.[9..7]
PINSEL_CFG_Type PinCfg = {0};
// SPI Configuration structure variable
SSP_CFG_Type SSP_ConfigStruct= {0};
/*SSP0
* MISO(P0.17) MOSI(P0.18) CLK(P0.15) CL(P0.16)
* MISO(P1.23) MOSI(P1.24) CLK(P1.20) CL(P1.21)
*
*
*
*SSP1
* MISO(P0.8) MOSI(P0.9) CLK(P0.7) CL(P0.7)
*
*
*/
#ifdef Open_SSP0
PinCfg.Portnum = 1;
PinCfg.Funcnum = PINSEL_FUNC_3;
PinCfg.Pinmode = PINSEL_PINMODE_PULLUP;
PinCfg.OpenDrain = PINSEL_PINMODE_NORMAL;
PinCfg.Pinnum = 23;
PINSEL_ConfigPin(&PinCfg);
PinCfg.Pinnum = 24;
PINSEL_ConfigPin(&PinCfg);
PinCfg.Pinnum = 20;
PINSEL_ConfigPin(&PinCfg);
#else
PinCfg.Portnum = 0;
PinCfg.Funcnum = PINSEL_FUNC_2;
PinCfg.Pinmode = PINSEL_PINMODE_PULLUP;
PinCfg.OpenDrain = PINSEL_PINMODE_NORMAL;
PinCfg.Pinnum = 8;
PINSEL_ConfigPin(&PinCfg);
PinCfg.Pinnum = 9;
PINSEL_ConfigPin(&PinCfg);
PinCfg.Portnum = 1;
PinCfg.Pinnum = 31;
PINSEL_ConfigPin(&PinCfg);
#endif /*Open_SSP0*/
/*SPIx configuration*/
SSP_ConfigStructInit(&SSP_ConfigStruct);
uprintf("SSP_ConfigStruct.Databit %3d \r\n ",SSP_ConfigStruct.Databit);
uprintf("SSP_ConfigStruct.CPHA %3d \r\n ",SSP_ConfigStruct.CPHA);
uprintf("SSP_ConfigStruct.CPOL %3d \r\n ",SSP_ConfigStruct.CPOL);
uprintf("SSP_ConfigStruct.Mode %3d \r\n ",SSP_ConfigStruct.Mode);
uprintf("SSP_ConfigStruct.FrameFormat %3d \r\n ",SSP_ConfigStruct.FrameFormat);
uprintf("SSP_ConfigStruct.ClockRate %3d \r\n ",SSP_ConfigStruct.ClockRate);
SSP_ConfigStruct.Databit = SSP_DATABIT_8;
SSP_ConfigStruct.CPHA = SSP_CPHA_FIRST ;
SSP_ConfigStruct.CPOL = SSP_CPOL_LO;
SSP_ConfigStruct.Mode = SSP_MASTER_MODE;
SSP_ConfigStruct.FrameFormat = SSP_FRAME_SPI;
SSP_ConfigStruct.ClockRate = 12000000; //400 KHz
/*uprintf("SSP_ConfigStruct.Databit %3d \r\n ",SSP_ConfigStruct.Databit);
uprintf("SSP_ConfigStruct.CPHA %3d \r\n ",SSP_ConfigStruct.CPHA);
uprintf("SSP_ConfigStruct.CPOL %3d \r\n ",SSP_ConfigStruct.CPOL);
uprintf("SSP_ConfigStruct.Mode %3d \r\n ",SSP_ConfigStruct.Mode);
uprintf("SSP_ConfigStruct.FrameFormat %3d \r\n ",SSP_ConfigStruct.FrameFormat);*/
uprintf("SSP_ConfigStruct.ClockRate %3d \r\n ",SSP_ConfigStruct.ClockRate);
// Initialize SSP peripheral with parameter given in structure above
SSP_Init(LPC_SSPx,&SSP_ConfigStruct);
// Enable SSP peripheral
SSP_Cmd(LPC_SSPx, ENABLE);
}
void AT45DBXX_Init(void)
{
// SPI初始化
AT45DBXX_nCS_H; //片选默认为高电平
spix_init();
}
/*static */void AT45DBXX_BUSY(void)
{
AT45DBXX_Enable;
while(!(0x80 & (SPIx_communication(READ_STATE_REGISTER))));
AT45DBXX_Disable;
}
void AT45DBXX_Read_ID(u8 *IData)
{
u8 i;
AT45DBXX_BUSY();
AT45DBXX_Enable;
SPIx_communication(Read_ID);
//for(i=0;i<4;i++)
for(i=0;i<3;i++)
IData[i]=SPIx_communication(0xff);
AT45DBXX_Disable;
}
void write_buffer(u16 BufferOffset,u8 Data)
{
AT45DBXX_Enable;
SPIx_communication(0x84);
SPIx_communication(0xff);
SPIx_communication((u8)BufferOffset>>8);
SPIx_communication((u8)BufferOffset);
SPIx_communication(Data);
AT45DBXX_Disable;
}
u8 read_buffer(u16 BufferOffset)
{
u8 temp=0;
AT45DBXX_Enable;
SPIx_communication(0xd4);
SPIx_communication(0xff);
SPIx_communication((u8)BufferOffset>>8);
SPIx_communication((u8)BufferOffset);
SPIx_communication(0xff);
temp=SPIx_communication(0xff);
AT45DBXX_Disable;
return temp;
}
uint8_t write_data_to_flash(uint16_t page_idx/**/,uint8_t *pdata)
{
//uint8_t ret=FALSE;
uint8_t data=0;
uint16_t i=0;
page_idx--;
AT45DBXX_BUSY();
AT45DBXX_Enable;
SPIx_communication(0x82);
SPIx_communication((uint8_t)page_idx>>6);
SPIx_communication((uint8_t)page_idx<<2);
SPIx_communication((uint8_t)0);
uprintf("\r\n write_data_to_flash page_idx:%4d \r\n",page_idx );
for (i=0;i<512;i++)
{
data=pdata[i];
//printf("writedata xxx i:%d %d\r\n",i+1,data);
SPIx_communication(data);
// uprintf("[i:%3d]%3d ",i+1,data);
//if ((i+1)%10==0)
// uprintf("\r\n");
}
AT45DBXX_Disable;
AT45DBXX_BUSY();
return FALSE;
}
uint8_t read_data_from_flash(uint16_t page_idx,uint16_t off_set,uint16_t read_len,uint8_t *pdata)
{
uint8_t read_byte=0;
uint32_t page_adr=0;
uint16_t i=0;
if (page_idx<=0 )
return FALSE;
page_idx--;
page_adr=((uint32_t)page_idx<<10)+off_set;
if (!pdata ||
read_len==0)
return FALSE;
AT45DBXX_BUSY();
AT45DBXX_Enable;
SPIx_communication(0x0B);
SPIx_communication((uint8_t)page_idx>>6);
SPIx_communication((uint8_t)page_idx<<2);
SPIx_communication((uint8_t)0);
uprintf("\r\n page_adr %d",page_adr);
SPIx_communication((uint8_t)0);
uprintf("\r\n read_data_from_flash page_idx:%4d\r\n",page_idx+1 );
for (i=0;i<read_len;i++)
{
read_byte=SPIx_communication(0xFF);
//uprintf("[i:%3d]%3d ",i+1,read_byte);
pdata[i]=read_byte;
//if ((i+1)%10==0)
// uprintf("\r\n");
}
AT45DBXX_Disable;
AT45DBXX_BUSY();
return TRUE;
}
评论12
最新资源