sggt/App/APP_WU/Src/eeprom_spi.c

188 lines
3.5 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-19 11:08:41
* @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"
//使用方法:
// uint8_t eep_tx = 0x72;
// uint8_t eep_rx = 0;
// uint8_t eep_addr[3];
// uint8_t eep_test_flag = 0;
// uint8_t eep_status = 0;
// if(eep_test_flag == 0)
// {
// eep_test_flag = 1;
// eep_addr[0] = 0x00;
// eep_addr[1] = 0x00;
// eep_addr[2] = 0x00;
// eeprom_writedata(eep_addr, eep_tx);
// eep_rx = eeprom_readdata(eep_addr);
// }
// 结果eep_rx = 0x72
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)
{
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(5); // 空等待
EEPROM_SCLK_H; //CPHA=0
wu_delay_us(5); // 空等待
}
EEPROM_SCLK_L;
}
uint8_t eeprom_spi_readbyte(void)
{
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(5); // 空等待
EEPROM_SCLK_H;
wu_delay_us(5); // 空等待
if(EEPROM_MISO)
{
read_data = read_data + 1;
}
}
EEPROM_SCLK_L;
return read_data;
}
uint8_t SPI_WriteReadByte(uint8_t data)
{
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;
wu_delay_us(5); // 空等待
if(EEPROM_MISO)
{
data |= 0x01;
}
EEPROM_SCLK_L;
wu_delay_us(5); // 空等待
}
return(data);
}
void eeprom_writedata(uint8_t addr[3], uint8_t txd)
{
uint8_t write_en_flag = 0;
uint8_t ee_status = 0;
EEPROM_CS_L;
delay_cnt(10);
eeprom_spi_writebyte(WRITE_ENABLE);
delay_cnt(10);
EEPROM_CS_H;
EEPROM_CS_L;
delay_cnt(10);
eeprom_spi_writebyte(READ_STATUS);
ee_status = eeprom_spi_readbyte();
delay_cnt(10);
EEPROM_CS_H;
write_en_flag = ee_status & 0x02;
if(write_en_flag)
{
EEPROM_CS_L;
delay_cnt(10);
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(10);
EEPROM_CS_H;
}
}
uint8_t eeprom_readdata(uint8_t addr[3])
{
uint8_t rxd = 0;
uint8_t write_en_flag = 0;
uint8_t ee_status = 0;
EEPROM_CS_L;
delay_cnt(10);
eeprom_spi_writebyte(WRITE_ENABLE);
delay_cnt(10);
EEPROM_CS_H;
EEPROM_CS_L;
delay_cnt(10);
eeprom_spi_writebyte(READ_STATUS);
ee_status = eeprom_spi_readbyte();
delay_cnt(10);
EEPROM_CS_H;
write_en_flag = ee_status & 0x02;
if(write_en_flag)
{
EEPROM_CS_L;
delay_cnt(10);
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(10);
EEPROM_CS_H;
}
return rxd;
}