sggt/App/APP_WU/Src/eeprom_spi.c

120 lines
2.1 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-17 17:00:38
* @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;
delay_cnt(1000);
uint8_t i;
for(i = 0;i < 8;i++)
{
EEPROM_SCLK_L;
wu_delay_us(2); // 空等待
#if 0
SPI_MOSI((Byte & 0x80) >> 7);
#else
if(wrt_data & 0x80)
{
EEPROM_MOSI_H;
}
else
{
EEPROM_MOSI_L;
}
#endif
wrt_data <<= 1;
wu_delay_us(2); // 空等待
EEPROM_SCLK_H;
wu_delay_us(2); // 空等待
wrt_data |= EEPROM_MISO;
}
EEPROM_SCLK_L;
EEPROM_CS_H;
}
void eeprom_spi_readbyte(uint8_t rd_data)
{
EEPROM_CS_L;
delay_cnt(1000);
uint8_t i;
uint8_t temp = 0;
for(i = 0;i < 8;i++)
{
EEPROM_SCLK_L;
wu_delay_us(2); // 空等待
temp <<= 1;
#if 1
temp |= EEPROM_MISO;
#else
if(SPI_MISO)
{
temp++;
}
#endif
EEPROM_SCLK_H;
wu_delay_us(2); // 空等待
}
EEPROM_SCLK_L;
EEPROM_CS_H;
}
void eeprom_writedata(uint8_t addr[3], uint8_t txd)
{
eeprom_spi_writebyte(WRITE_ENABLE);
eeprom_spi_writebyte(WRITE_MEMORY);
eeprom_spi_writebyte(addr[0]);
eeprom_spi_writebyte(addr[1]);
eeprom_spi_writebyte(addr[2]);
eeprom_spi_writebyte(txd);
eeprom_spi_writebyte(WRITE_DISABLE);
}
void eeprom_readdata(uint8_t addr[3], uint8_t rxd)
{
eeprom_spi_writebyte(WRITE_ENABLE);
eeprom_spi_writebyte(READ_MEMORY);
eeprom_spi_writebyte(addr[0]);
eeprom_spi_writebyte(addr[1]);
eeprom_spi_writebyte(addr[2]);
eeprom_spi_readbyte(rxd);
eeprom_spi_writebyte(WRITE_DISABLE);
}