Skip to content

Commit

Permalink
Merge pull request #29 from alba-ado/master
Browse files Browse the repository at this point in the history
Added bool return types to the SPI functions that doesn't return anything
  • Loading branch information
hathach authored Nov 2, 2023
2 parents dc166ba + ed0aed4 commit 536d12d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ Doxyfile*
doxygen_sqlite3.db
html
*.tmp
.vscode/*
42 changes: 26 additions & 16 deletions Adafruit_FRAM_SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,20 @@ bool Adafruit_FRAM_SPI::begin(uint8_t nAddressSizeBytes) {
}

/*!
@brief Enables or disables writing to the SPI flash
@param enable
True enables writes, false disables writes
*/
void Adafruit_FRAM_SPI::writeEnable(bool enable) {
* @brief Enables or disables writing to the SPI flash
* @param enable
* True enables writes, false disables writes
* @return true if successful
*/
bool Adafruit_FRAM_SPI::writeEnable(bool enable) {
uint8_t cmd;

if (enable) {
cmd = OPCODE_WREN;
} else {
cmd = OPCODE_WRDI;
}
spi_dev->write(&cmd, 1);
return spi_dev->write(&cmd, 1);
}

/*!
Expand All @@ -179,8 +180,9 @@ void Adafruit_FRAM_SPI::writeEnable(bool enable) {
* The 32-bit address to write to in FRAM memory
* @param value
* The 8-bit value to write at framAddr
* @return true if successful
*/
void Adafruit_FRAM_SPI::write8(uint32_t addr, uint8_t value) {
bool Adafruit_FRAM_SPI::write8(uint32_t addr, uint8_t value) {
uint8_t buffer[10];
uint8_t i = 0;

Expand All @@ -193,7 +195,7 @@ void Adafruit_FRAM_SPI::write8(uint32_t addr, uint8_t value) {
buffer[i++] = (uint8_t)(addr & 0xFF);
buffer[i++] = value;

spi_dev->write(buffer, i);
return spi_dev->write(buffer, i);
}

/*!
Expand All @@ -204,8 +206,9 @@ void Adafruit_FRAM_SPI::write8(uint32_t addr, uint8_t value) {
* The pointer to an array of 8-bit values to write starting at addr
* @param count
* The number of bytes to write
* @return true if successful
*/
void Adafruit_FRAM_SPI::write(uint32_t addr, const uint8_t *values,
bool Adafruit_FRAM_SPI::write(uint32_t addr, const uint8_t *values,
size_t count) {
uint8_t prebuf[10];
uint8_t i = 0;
Expand All @@ -218,7 +221,7 @@ void Adafruit_FRAM_SPI::write(uint32_t addr, const uint8_t *values,
prebuf[i++] = (uint8_t)(addr >> 8);
prebuf[i++] = (uint8_t)(addr & 0xFF);

spi_dev->write(values, count, prebuf, i);
return spi_dev->write(values, count, prebuf, i);
}

/*!
Expand Down Expand Up @@ -252,8 +255,9 @@ uint8_t Adafruit_FRAM_SPI::read8(uint32_t addr) {
* The pointer to an array of 8-bit values to read starting at addr
* @param count
* The number of bytes to read
* @return true if successful
*/
void Adafruit_FRAM_SPI::read(uint32_t addr, uint8_t *values, size_t count) {
bool Adafruit_FRAM_SPI::read(uint32_t addr, uint8_t *values, size_t count) {
uint8_t buffer[10];
uint8_t i = 0;

Expand All @@ -265,7 +269,7 @@ void Adafruit_FRAM_SPI::read(uint32_t addr, uint8_t *values, size_t count) {
buffer[i++] = (uint8_t)(addr >> 8);
buffer[i++] = (uint8_t)(addr & 0xFF);

spi_dev->write_then_read(buffer, i, values, count);
return spi_dev->write_then_read(buffer, i, values, count);
}

/*!
Expand All @@ -276,13 +280,16 @@ void Adafruit_FRAM_SPI::read(uint32_t addr, uint8_t *values, size_t count) {
* The memory density (bytes 15..8) and proprietary
* Product ID fields (bytes 7..0). Should be 0x0302 for
* the MB85RS64VPNF-G-JNERE1.
* @return true if successful
*/
void Adafruit_FRAM_SPI::getDeviceID(uint8_t *manufacturerID,
bool Adafruit_FRAM_SPI::getDeviceID(uint8_t *manufacturerID,
uint16_t *productID) {
uint8_t cmd = OPCODE_RDID;
uint8_t a[4] = {0, 0, 0, 0};

spi_dev->write_then_read(&cmd, 1, a, 4);
if (!spi_dev->write_then_read(&cmd, 1, a, 4)) {
return false;
}

if (a[1] == 0x7f) {
// Device with continuation code (0x7F) in their second byte
Expand All @@ -295,6 +302,8 @@ void Adafruit_FRAM_SPI::getDeviceID(uint8_t *manufacturerID,
*manufacturerID = (a[0]);
*productID = (a[1] << 8) + a[2];
}

return true;
}

/*!
Expand All @@ -315,14 +324,15 @@ uint8_t Adafruit_FRAM_SPI::getStatusRegister() {
* @brief Sets the status register
* @param value
* value that will be set
* @return true if successful
*/
void Adafruit_FRAM_SPI::setStatusRegister(uint8_t value) {
bool Adafruit_FRAM_SPI::setStatusRegister(uint8_t value) {
uint8_t cmd[2];

cmd[0] = OPCODE_WRSR;
cmd[1] = value;

spi_dev->write(cmd, 2);
return spi_dev->write(cmd, 2);
}

/*!
Expand Down
12 changes: 6 additions & 6 deletions Adafruit_FRAM_SPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ class Adafruit_FRAM_SPI {
Adafruit_FRAM_SPI(int8_t clk, int8_t miso, int8_t mosi, int8_t cs);

bool begin(uint8_t nAddressSizeBytes = 2);
void writeEnable(bool enable);
void write8(uint32_t addr, uint8_t value);
void write(uint32_t addr, const uint8_t *values, size_t count);
bool writeEnable(bool enable);
bool write8(uint32_t addr, uint8_t value);
bool write(uint32_t addr, const uint8_t *values, size_t count);
uint8_t read8(uint32_t addr);
void read(uint32_t addr, uint8_t *values, size_t count);
void getDeviceID(uint8_t *manufacturerID, uint16_t *productID);
bool read(uint32_t addr, uint8_t *values, size_t count);
bool getDeviceID(uint8_t *manufacturerID, uint16_t *productID);
uint8_t getStatusRegister(void);
void setStatusRegister(uint8_t value);
bool setStatusRegister(uint8_t value);
void setAddressSize(uint8_t nAddressSize);

private:
Expand Down

0 comments on commit 536d12d

Please sign in to comment.