이번에 고생 했던 EEPROM M95512 이제는 잘 동작 한다~
#define WRSR 0x01 //Write Status Register
#define WRITE 0x02 //Write to Memory Array
#define READ 0x03 //Read from Memory Array
#define WRDI 0x04 //Write Disable
#define RDSR 0x05 //Read Status Register
#define WREN 0x06 //Write Enable
#define DUMMY 0x00 //DUMMY BYTE
#define EEPROM_CS_LOW() GPIO_ResetBits(GPIOB, GPIO_Pin_0);
#define EEPROM_CS_HIGH() GPIO_SetBits(GPIOB, GPIO_Pin_0);
void SPI_Config(void);
uint8_t SPI_SendByte(uint8_t Data);
uint8_t SPI_Flash_Read(uint16_t address);
void SPI_Flash_Write(uint16_t address, uint8_t data);
/* Includes ------------------------------------------------------------------*/
#include "stdio.h"
#include "stm32f10x.h"
#include "M95512.h"
void SPI_Config(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
SPI_Cmd(SPI1, DISABLE);
SPI_I2S_DeInit(SPI1);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 | RCC_APB2Periph_AFIO, DISABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Enable SPI clock, SPI1 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_SPI1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // 50 MHz
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Configure CS pin as output floating
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* SPI configuration -------------------------------------------------------*/
EEPROM_CS_HIGH();
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
}
uint8_t SPI_SendByte(uint8_t Data)
{
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, Data);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
return SPI_I2S_ReceiveData(SPI1);
}
static void M95512_WREN(void)
{//0x06 Write Enable
EEPROM_CS_LOW();
SPI_SendByte(WREN);
EEPROM_CS_HIGH();
}
static void M95512_WRDI(void)
{//0x04 Write Disable
EEPROM_CS_LOW();
SPI_SendByte(WRDI);
EEPROM_CS_HIGH();
}
static void M95512_RDSR(void)
{//0x05 Read Status Register
unsigned char data_H, data_L;
unsigned int data = 0;
EEPROM_CS_LOW();
SPI_SendByte(RDSR);
// data_H = SPI_ReadByte();
// data_L = SPI_ReadByte();
EEPROM_CS_HIGH();
data = (data_H << 1) + (data_L << 7);
}
static void M95512_WRSR(unsigned char reg)
{//0x01 Write Status Register
EEPROM_CS_LOW();
SPI_SendByte(WRSR);
SPI_SendByte(reg);
EEPROM_CS_HIGH();
}
uint8_t SPI_Flash_Read(uint16_t address)
{
uint8_t data;
EEPROM_CS_LOW();
SPI_SendByte(READ);
SPI_SendByte((address & 0xFF00) >> 8);
SPI_SendByte(address & 0xFF);
data = SPI_SendByte(DUMMY);
EEPROM_CS_HIGH();
return data;
}
void SPI_Flash_Write(uint16_t address, uint8_t data)
{
M95512_WREN();
EEPROM_CS_LOW();
SPI_SendByte(WRITE); // Send SPI instruction
SPI_SendByte((address & 0xFF00) >> 8); // Send SPI address MSB
SPI_SendByte(address & 0xFF); // Send SPI address LSB
SPI_SendByte(data); // Send SPI Write data(Byte)
EEPROM_CS_HIGH();
}
위와 같이 하여 놓고
main에서
SPI_Flash_Write(0x0000,0x11);
SPI1_READ_DATA = SPI_Flash_Read(0x0000);
사용하면 데이타가 잘 들어 온다~
'Embedded Lab > ARM' 카테고리의 다른 글
[STM32 : SPI, using chip select] (0) | 2014.03.05 |
---|---|
[SWI의 진실] (0) | 2013.03.22 |
[논리 스프트와 산술 시프트의 차이] (0) | 2013.03.20 |