sggt/App/APP_WU/Src/eeprom_spi.c

160 lines
2.8 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* @Author: wujunchao wujunchao@wuxismart.com
* @Date: 2025-03-17 14:38:34
* @LastEditors: wujunchao wujunchao@wuxismart.com
* @LastEditTime: 2025-03-18 09:18:55
* @FilePath: \signal_generator\App\APP_WU\Src\eeprom_spi.c
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
#include "eeprom_spi.h"
void eeprom_spi_init(void)
{
EEPROM_CS_H; // CS 初始化高手册中描述S拉低时片选
EEPROM_SCLK_L; // CLK 初始化低
EEPROM_WR_H; // 不使用硬件保护
}
void eeprom_spi_writebyte(uint8_t wrt_data)
{
// EEPROM_CS_L;
// wu_delay_us(100);
uint8_t i = 0;
uint8_t temp = 0;
for(i = 0; i < 8; i++)
{
temp = ((wrt_data&0x80)==0x80)? 1:0;
wrt_data = wrt_data << 1;
EEPROM_SCLK_L; //CPOL=0
if(temp)
{
EEPROM_MOSI_H;
}
else
{
EEPROM_MOSI_L;
}
wu_delay_us(10); // 空等待
EEPROM_SCLK_H; //CPHA=0
wu_delay_us(10); // 空等待
}
EEPROM_SCLK_L;
//EEPROM_CS_H;
}
uint8_t eeprom_spi_readbyte(void)
{
// EEPROM_CS_L;
// wu_delay_us(100);
uint8_t i = 0;
uint8_t read_data = 0xFF;
for(i = 0; i < 8; i++)
{
read_data = read_data << 1;
EEPROM_SCLK_L;
wu_delay_us(10); // 空等待
EEPROM_SCLK_H;
wu_delay_us(10); // 空等待
if(EEPROM_MISO == 1)
{
read_data = read_data + 1;
}
}
EEPROM_SCLK_L;
// EEPROM_CS_H;
return read_data;
}
uint8_t SPI_WriteReadByte(uint8_t data)
{
// EEPROM_CS_L;
// delay_cnt(0xff);
uint16_t bit_ctr;
for(bit_ctr=0;bit_ctr<8;bit_ctr++)
{
if(data & 0x80)
{
EEPROM_MOSI_H;
}
else
{
EEPROM_MOSI_L;
}
data = (data << 1);
EEPROM_SCLK_H;
delay_cnt(0xff);
if(EEPROM_MISO)
{
data |= 0x01;
}
EEPROM_SCLK_L;
delay_cnt(0xff);
}
//EEPROM_CS_H;
return(data);
}
void eeprom_writedata(uint8_t addr[3], uint8_t txd)
{
EEPROM_CS_L;
delay_cnt(0xff);
eeprom_spi_writebyte(WRITE_ENABLE);
delay_cnt(0xff);
EEPROM_CS_H;
EEPROM_CS_L;
delay_cnt(0xff);
eeprom_spi_writebyte(WRITE_MEMORY);
eeprom_spi_writebyte(addr[0]);
eeprom_spi_writebyte(addr[1]);
eeprom_spi_writebyte(addr[2]);
eeprom_spi_writebyte(txd);
delay_cnt(0xff);
EEPROM_CS_H;
}
uint8_t eeprom_readdata(uint8_t addr[3])
{
uint8_t rxd = 0;
EEPROM_CS_L;
delay_cnt(0xff);
eeprom_spi_writebyte(WRITE_ENABLE);
delay_cnt(0xff);
EEPROM_CS_H;
EEPROM_CS_L;
delay_cnt(0xff);
eeprom_spi_writebyte(READ_MEMORY);
eeprom_spi_writebyte(addr[0]);
eeprom_spi_writebyte(addr[1]);
eeprom_spi_writebyte(addr[2]);
rxd = eeprom_spi_readbyte();
delay_cnt(0xff);
EEPROM_CS_H;
return rxd;
}