Skip to content

Commit

Permalink
fixing style
Browse files Browse the repository at this point in the history
  • Loading branch information
mirellameelo committed Oct 28, 2024
1 parent dd21108 commit 7ce11ad
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 106 deletions.
26 changes: 14 additions & 12 deletions stm32/lib/fram/include/fifo.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ extern "C" {
#endif /* FRAM_BUFFER_END */

/** Addresses in FRAM for buffer state variables */
#define FRAM_BUFFER_READ_ADDR 0x06F0 // 1776 (close to the end of buffer space)
#define FRAM_BUFFER_WRITE_ADDR 0x06F2 // 1778
#define FRAM_BUFFER_LEN_ADDR 0x06F4 // 1780
#define FRAM_BUFFER_START 0x0000 // 0
#define FRAM_BUFFER_READ_ADDR 0x06F0 // 1776 (close to the end of buffer space)
#define FRAM_BUFFER_WRITE_ADDR 0x06F2 // 1778
#define FRAM_BUFFER_LEN_ADDR 0x06F4 // 1780
#define FRAM_BUFFER_START 0x0000 // 0

#if FRAM_BUFFER_START > FRAM_BUFFER_END
#error "Buffer end address must be greater than buffer start address"
Expand Down Expand Up @@ -91,30 +91,32 @@ uint16_t FramBufferLen(void);
FramStatus FramBufferClear(void);

/**
* @brief Saves the buffer state (read address, write address, and buffer length)
* to FRAM.
* @brief Saves the buffer state (read address, write address, and buffer
* length) to FRAM.
*
* @param read_addr Current read address of the circular buffer.
* @param write_addr Current write address of the circular buffer.
* @param buffer_len Current length of the circular buffer.
* @return FramStatus, status of the FRAM operation.
*/
FramStatus FramSaveBufferState(uint16_t read_addr, uint16_t write_addr, uint16_t buffer_len);
FramStatus FramSaveBufferState(uint16_t read_addr, uint16_t write_addr,
uint16_t buffer_len);

/**
* @brief Loads the buffer state (read address, write address, and buffer length)
* from FRAM.
* @brief Loads the buffer state (read address, write address, and buffer
* length) from FRAM.
*
* @param read_addr Pointer to store the retrieved read address.
* @param write_addr Pointer to store the retrieved write address.
* @param buffer_len Pointer to store the retrieved buffer length.
* @return FramStatus, status of the FRAM operation.
*/
FramStatus FramLoadBufferState(uint16_t *read_addr, uint16_t *write_addr, uint16_t *buffer_len);
FramStatus FramLoadBufferState(uint16_t *read_addr, uint16_t *write_addr,
uint16_t *buffer_len);

/**
* @brief Initializes the FIFO buffer by loading the buffer state (read address,
* write address, and buffer length) from FRAM. If the state cannot be
* @brief Initializes the FIFO buffer by loading the buffer state (read address,
* write address, and buffer length) from FRAM. If the state cannot be
* loaded or is invalid, it initializes the buffer with default values.
* @return FramStatus, status of the FRAM operation.
*/
Expand Down
153 changes: 83 additions & 70 deletions stm32/lib/fram/src/fifo.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
**/

#include "fifo.h"
#include "usart.h"

#include "sys_app.h"
#include "usart.h"

// head and tail
static uint16_t read_addr;
Expand Down Expand Up @@ -65,7 +66,7 @@ FramStatus FramPut(const uint8_t *data, const uint16_t num_bytes) {
FramStatus status;

// write single byte length to buffer
status = FramWrite(write_addr, (uint8_t*)&num_bytes, 1);
status = FramWrite(write_addr, (uint8_t *)&num_bytes, 1);
if (status != FRAM_OK) {
return status;
}
Expand Down Expand Up @@ -128,87 +129,99 @@ FramStatus FramBufferClear(void) {
}

void print(const char *format, ...) {
char buffer[128];
va_list args;
va_start(args, format);
vsnprintf(buffer, sizeof(buffer), format, args);
va_end(args);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), HAL_MAX_DELAY);
char buffer[128];
va_list args;
va_start(args, format);
vsnprintf(buffer, sizeof(buffer), format, args);
va_end(args);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), HAL_MAX_DELAY);
}

FramStatus FIFO_Init(void) {
FramStatus status = FramLoadBufferState(&read_addr, &write_addr, &buffer_len);
if (status != FRAM_OK) {
print("Failed to load FIFO state. FRAM Status: %d\n", status);
// If loading the buffer state fails, assume it's an empty state
read_addr = FRAM_BUFFER_START;
write_addr = FRAM_BUFFER_START;
buffer_len = 0;
FramSaveBufferState(read_addr, write_addr, buffer_len);
print("Initialized to empty buffer state.\n");
return FRAM_OK;
FramStatus status = FramLoadBufferState(&read_addr, &write_addr, &buffer_len);
if (status != FRAM_OK) {
print("Failed to load FIFO state. FRAM Status: %d\n", status);
// If loading the buffer state fails, assume it's an empty state
read_addr = FRAM_BUFFER_START;
write_addr = FRAM_BUFFER_START;
buffer_len = 0;
FramSaveBufferState(read_addr, write_addr, buffer_len);
print("Initialized to empty buffer state.\n");
return FRAM_OK;
} else {
if (read_addr == FRAM_BUFFER_START && write_addr == FRAM_BUFFER_START &&
buffer_len == 0) {
print("Buffer is empty or freshly initialized.\n");
} else {
if (read_addr == FRAM_BUFFER_START && write_addr == FRAM_BUFFER_START && buffer_len == 0) {
print("Buffer is empty or freshly initialized.\n");
} else {
print("Buffer contains data. Ready to resume operations.\n");
}
print("Buffer contains data. Ready to resume operations.\n");
}
return FRAM_OK;
}
return FRAM_OK;
}

FramStatus FramSaveBufferState(uint16_t read_addr, uint16_t write_addr,
uint16_t buffer_len) {
FramStatus status;
uint8_t *read_addr_bytes = (uint8_t *)&read_addr;
uint8_t *write_addr_bytes = (uint8_t *)&write_addr;
uint8_t *buffer_len_bytes = (uint8_t *)&buffer_len;

FramStatus FramSaveBufferState(uint16_t read_addr, uint16_t write_addr, uint16_t buffer_len) {
FramStatus status;
uint8_t *read_addr_bytes = (uint8_t*)&read_addr;
uint8_t *write_addr_bytes = (uint8_t*)&write_addr;
uint8_t *buffer_len_bytes = (uint8_t*)&buffer_len;

// Save read_addr
status = FramWrite(FRAM_BUFFER_READ_ADDR, read_addr_bytes, sizeof(read_addr));
if (status != FRAM_OK) {
print("Failed to save read address. FRAM Status: %d\n", status);
return status;
}
// Save read_addr
status = FramWrite(FRAM_BUFFER_READ_ADDR, read_addr_bytes, sizeof(read_addr));
if (status != FRAM_OK) {
print("Failed to save read address. FRAM Status: %d\n", status);
return status;
}

// Save write_addr
status = FramWrite(FRAM_BUFFER_WRITE_ADDR, write_addr_bytes, sizeof(write_addr));
if (status != FRAM_OK) {
print("Failed to save write address. FRAM Status: %d\n", status);
return status;
}
// Save write_addr
status =
FramWrite(FRAM_BUFFER_WRITE_ADDR, write_addr_bytes, sizeof(write_addr));
if (status != FRAM_OK) {
print("Failed to save write address. FRAM Status: %d\n", status);
return status;
}

// Save buffer_len
status = FramWrite(FRAM_BUFFER_LEN_ADDR, buffer_len_bytes, sizeof(buffer_len));
if (status != FRAM_OK) {
print("Failed to save buffer length. FRAM Status: %d\n", status);
return status;
}
// Save buffer_len
status =
FramWrite(FRAM_BUFFER_LEN_ADDR, buffer_len_bytes, sizeof(buffer_len));
if (status != FRAM_OK) {
print("Failed to save buffer length. FRAM Status: %d\n", status);
return status;
}

// Print a single message summarizing the saved buffer state
print("Buffer State Saved Successfully. Read Address: 0x%04X (%d), Write Address: 0x%04X (%d), Buffer Length: %d\n",
read_addr, read_addr, write_addr, write_addr, buffer_len);
// Print a single message summarizing the saved buffer state
print(
"Buffer State Saved Successfully. Read Address: 0x%04X (%d), Write "
"Address: 0x%04X (%d), Buffer Length: %d\n",
read_addr, read_addr, write_addr, write_addr, buffer_len);

return FRAM_OK;
return FRAM_OK;
}

FramStatus FramLoadBufferState(uint16_t *read_addr, uint16_t *write_addr, uint16_t *buffer_len) {
FramStatus status;

// Load read_addr and print each byte
status = FramRead(FRAM_BUFFER_READ_ADDR, sizeof(*read_addr), (uint8_t*)read_addr);
if (status != FRAM_OK) return status;
print("Loaded Read Address: 0x%02X 0x%02X (%d)\n", ((uint8_t*)read_addr)[0], ((uint8_t*)read_addr)[1], *read_addr);

// Load write_addr and print each byte
status = FramRead(FRAM_BUFFER_WRITE_ADDR, sizeof(*write_addr), (uint8_t*)write_addr);
if (status != FRAM_OK) return status;
print("Loaded Write Address: 0x%02X 0x%02X (%d)\n", ((uint8_t*)write_addr)[0], ((uint8_t*)write_addr)[1], *write_addr);
FramStatus FramLoadBufferState(uint16_t *read_addr, uint16_t *write_addr,
uint16_t *buffer_len) {
FramStatus status;

// Load buffer_len and print each byte
status = FramRead(FRAM_BUFFER_LEN_ADDR, sizeof(*buffer_len), (uint8_t*)buffer_len);
if (status != FRAM_OK) return status;
print("Loaded Buffer Length: 0x%02X 0x%02X (%d)\n", ((uint8_t*)buffer_len)[0], ((uint8_t*)buffer_len)[1], *buffer_len);
// Load read_addr and print each byte
status =
FramRead(FRAM_BUFFER_READ_ADDR, sizeof(*read_addr), (uint8_t *)read_addr);
if (status != FRAM_OK) return status;
print("Loaded Read Address: 0x%02X 0x%02X (%d)\n", ((uint8_t *)read_addr)[0],
((uint8_t *)read_addr)[1], *read_addr);

// Load write_addr and print each byte
status = FramRead(FRAM_BUFFER_WRITE_ADDR, sizeof(*write_addr),
(uint8_t *)write_addr);
if (status != FRAM_OK) return status;
print("Loaded Write Address: 0x%02X 0x%02X (%d)\n",
((uint8_t *)write_addr)[0], ((uint8_t *)write_addr)[1], *write_addr);

// Load buffer_len and print each byte
status = FramRead(FRAM_BUFFER_LEN_ADDR, sizeof(*buffer_len),
(uint8_t *)buffer_len);
if (status != FRAM_OK) return status;
print("Loaded Buffer Length: 0x%02X 0x%02X (%d)\n",
((uint8_t *)buffer_len)[0], ((uint8_t *)buffer_len)[1], *buffer_len);

return FRAM_OK;
}
return FRAM_OK;
}
49 changes: 25 additions & 24 deletions stm32/test/test_fifo/test_fifo.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,34 +242,35 @@ void test_FramBuffer_Wraparound(void) {
}

void test_LoadSaveBufferState(void) {
// Clear the buffer and check initial state
FramStatus status = FramBufferClear();
// Clear the buffer and check initial state
FramStatus status = FramBufferClear();
TEST_ASSERT_EQUAL(FRAM_OK, status);

// Add some data
const uint8_t test_data[] = {0x11, 0x22, 0x33};
for (int i = 0; i < 10; i++) {
status = FramPut(test_data, sizeof(test_data));
TEST_ASSERT_EQUAL(FRAM_OK, status);
}

// Add some data
const uint8_t test_data[] = {0x11, 0x22, 0x33};
for (int i = 0; i < 10; i++) {
status = FramPut(test_data, sizeof(test_data));
TEST_ASSERT_EQUAL(FRAM_OK, status);
}
// Load the new buffer state
uint16_t saved_read_addr, saved_write_addr, saved_buffer_len;
status = FramLoadBufferState(&saved_read_addr, &saved_write_addr,
&saved_buffer_len);
TEST_ASSERT_EQUAL(FRAM_OK, status);

// Load the new buffer state
uint16_t saved_read_addr, saved_write_addr, saved_buffer_len;
status = FramLoadBufferState(&saved_read_addr, &saved_write_addr, &saved_buffer_len);
TEST_ASSERT_EQUAL(FRAM_OK, status);
// Validate the updated state: buffer_len should reflect 10 puts
TEST_ASSERT_EQUAL(10, saved_buffer_len);
TEST_ASSERT_NOT_EQUAL(FRAM_BUFFER_START, saved_write_addr);
TEST_ASSERT_EQUAL(FRAM_BUFFER_START, saved_read_addr);

// Validate the updated state: buffer_len should reflect 10 puts
TEST_ASSERT_EQUAL(10, saved_buffer_len);
TEST_ASSERT_NOT_EQUAL(FRAM_BUFFER_START, saved_write_addr);
TEST_ASSERT_EQUAL(FRAM_BUFFER_START, saved_read_addr);

uint8_t retrieved_data[sizeof(test_data)];
uint8_t retrieved_len;
for (int i = 0; i < 10; i++) {
status = FramGet(retrieved_data, &retrieved_len);
TEST_ASSERT_EQUAL(FRAM_OK, status);
TEST_ASSERT_EQUAL_UINT8_ARRAY(test_data, retrieved_data, sizeof(test_data));
}
uint8_t retrieved_data[sizeof(test_data)];
uint8_t retrieved_len;
for (int i = 0; i < 10; i++) {
status = FramGet(retrieved_data, &retrieved_len);
TEST_ASSERT_EQUAL(FRAM_OK, status);
TEST_ASSERT_EQUAL_UINT8_ARRAY(test_data, retrieved_data, sizeof(test_data));
}
}

/**
Expand Down

0 comments on commit 7ce11ad

Please sign in to comment.