mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/driver-w25qxxx.git
synced 2025-09-29 00:37:33 +00:00
Added first implementations
This commit is contained in:
136
src/w25qxx.cpp
Normal file
136
src/w25qxx.cpp
Normal file
@@ -0,0 +1,136 @@
|
||||
#include <sta/sensors/w25qxx.hpp>
|
||||
|
||||
|
||||
namespace sta
|
||||
{
|
||||
W25Qxx::W25Qxx(SPIDevice * device)
|
||||
: device_{device}
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
uint8_t W25Qxx::init()
|
||||
{
|
||||
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool W25Qxx::isBusy()
|
||||
{
|
||||
uint8_t status = 0;
|
||||
readStatusRegister(1, &status);
|
||||
|
||||
return (0x01 & status) == 0x01;
|
||||
}
|
||||
|
||||
uint8_t
|
||||
|
||||
uint8_t W25Qxx::readData(const uint8_t * addr, uint8_t * buffer, size_t length)
|
||||
{
|
||||
device_->beginTransmission();
|
||||
|
||||
const uint8_t instruction = W25QXX_FAST_READ;
|
||||
device_->transfer(&instruction, 1);
|
||||
device_->transfer(addr, 3);
|
||||
device_->receive(buffer, length);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint8_t W25Qxx::busWrite(uint8_t instruction, const uint8_t * data /* = nullptr */, size_t length /* = 0 */)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint8_t W25Qxx::busRead(uint8_t instruction, uint8_t * data, size_t length)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint8_t W25Qxx::writeEnable()
|
||||
{
|
||||
return busWrite(W25QXX_WRITE_ENABLE);
|
||||
}
|
||||
|
||||
uint8_t W25Qxx::writeVolatileEnable()
|
||||
{
|
||||
return busWrite(W25QXX_VOL_SR_WRITE_ENABLE);
|
||||
}
|
||||
|
||||
uint8_t W25Qxx::writeDisable()
|
||||
{
|
||||
return busWrite(W25QXX_WRITE_DISABLE);
|
||||
}
|
||||
|
||||
uint8_t W25Qxx::readStatusRegister(uint8_t regID, uint8_t * status_byte)
|
||||
{
|
||||
if (regID == 1)
|
||||
{
|
||||
return busRead(W25QXX_STATUS_REG_1_READ, status_byte, 1);
|
||||
}
|
||||
|
||||
if (regID == 2)
|
||||
{
|
||||
return busRead(W25QXX_STATUS_REG_2_READ, status_byte, 1);
|
||||
}
|
||||
|
||||
if (regID == 3)
|
||||
{
|
||||
return busRead(W25QXX_STATUS_REG_3_READ, status_byte, 1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t W25Qxx::writeStatusRegister(uint8_t regID, uint8_t * status_byte, bool nonvolatile /* = false */)
|
||||
{
|
||||
if (!writeEnable())
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!nonvolatile)
|
||||
{
|
||||
uint8_t rslt = writeVolatileEnable();
|
||||
|
||||
if (rslt == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (regID == 1)
|
||||
{
|
||||
return busRead(W25QXX_STATUS_REG_1_READ, status_byte, 1);
|
||||
}
|
||||
|
||||
if (regID == 2)
|
||||
{
|
||||
return busRead(W25QXX_STATUS_REG_2_READ, status_byte, 1);
|
||||
}
|
||||
|
||||
if (regID == 3)
|
||||
{
|
||||
return busRead(W25QXX_STATUS_REG_3_READ, status_byte, 1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t W25Qxx::pageProgram(const uint8_t * addr, uint8_t * buffer, size_t length)
|
||||
{
|
||||
if (!writeEnable())
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
device_->beginTransmission();
|
||||
uint8_t instruction = W25QXX_PAGE_PROGAM;
|
||||
device_->transfer(&instruction, 1);
|
||||
device_->transfer(buffer, length);
|
||||
device_->endTransmission();
|
||||
|
||||
return 1;
|
||||
}
|
||||
} // namespace sta
|
Reference in New Issue
Block a user