From 785f8a6806113fd5a1b1e2d03db7f4b4a9ffe6ca Mon Sep 17 00:00:00 2001 From: Vasily Davydov Date: Tue, 4 Apr 2023 22:19:24 +0300 Subject: [PATCH] peripherals: add EEPROM lib [#2] --- source/shoh/src/peripherals/EEPROMWrapper.cpp | 95 +++++++++++++++++++ source/shoh/src/peripherals/EEPROMWrapper.h | 83 ++++++++++++++++ 2 files changed, 178 insertions(+) create mode 100644 source/shoh/src/peripherals/EEPROMWrapper.cpp create mode 100644 source/shoh/src/peripherals/EEPROMWrapper.h diff --git a/source/shoh/src/peripherals/EEPROMWrapper.cpp b/source/shoh/src/peripherals/EEPROMWrapper.cpp new file mode 100644 index 0000000..4f9f160 --- /dev/null +++ b/source/shoh/src/peripherals/EEPROMWrapper.cpp @@ -0,0 +1,95 @@ +/* + * EEPROMWrapper.cpp + * + * Created on: Dec 4, 2022 + * Author: tylen + */ + +#include + +EEPROM_Wrapper::EEPROM_Wrapper () +{ + /* Enable EEPROM clock and reset EEPROM controller */ + Chip_Clock_EnablePeriphClock (SYSCTL_CLOCK_EEPROM); + Chip_SYSCTL_PeriphReset (RESET_EEPROM); + iap_exec = reinterpret_cast (IAP_ENTRY_LOCATION); +} + +EEPROM_Wrapper::~EEPROM_Wrapper () +{ + // TODO Auto-generated destructor stub +} + +static void +e_memcpy (void *from, void *to, unsigned int n) +{ + if (!from) + return; + char *source = (char *)from; + char *dest = (char *)to; + while (n) + { + (*dest++) = (*source++); + n--; + } +} + +void +EEPROM_Wrapper::eeprom_execute (EEPROM *rom) +{ + command[0] = rom->mode; + command[1] = rom->addr; + command[2] = rom->data; + command[3] = rom->size; + command[4] = rom->clock; + this->iap_exec (command, result); +} + +void +EEPROM_Wrapper::eeprom_use (uint8_t *data, uint32_t addr, uint32_t size, + bool mode) +{ + rom.addr = addr; + rom.data = (uint32_t)data; + rom.mode = (mode) ? IAP_EEPROM_READ : IAP_EEPROM_WRITE; + rom.size = size; +#if INCLUDE_vTaskSuspend + vTaskSuspendAll (); +#endif + /* Main execution of eeprom r/w */ + eeprom_execute (&rom); +#if INCLUDE_vTaskSuspend + xTaskResumeAll (); +#endif + assert (result[0] == IAP_CMD_SUCCESS); +} + +std::string +EEPROM_Wrapper::str_read_from (uint32_t addr, uint32_t amount) +{ + eeprom_use (buffer, addr, amount, READ); + std::string str = (char *)buffer; + return str; +} + +void +EEPROM_Wrapper::write_to (uint32_t addr, std::string str) +{ + std::copy (str.begin (), str.end (), std::begin (buffer)); + eeprom_use (buffer, addr, str.length (), WRITE); +} + +void * +EEPROM_Wrapper::read_from (uint32_t addr, uint32_t amount) +{ + eeprom_use (buffer, addr, amount, READ); + void *data = (void *)buffer; + return data; +} +void +EEPROM_Wrapper::write_to (uint32_t addr, void *data, uint32_t size_of_data) +{ + assert (size_of_data < EEPROM_MAX_BUFER_SIZE); + e_memcpy (data, buffer, size_of_data); + eeprom_use (buffer, addr, size_of_data, WRITE); +} diff --git a/source/shoh/src/peripherals/EEPROMWrapper.h b/source/shoh/src/peripherals/EEPROMWrapper.h new file mode 100644 index 0000000..b0aed9b --- /dev/null +++ b/source/shoh/src/peripherals/EEPROMWrapper.h @@ -0,0 +1,83 @@ +/* + * EEPROMWrapper.h + * + * Created on: Dec 4, 2022 + * Author: tylen + */ + +#ifndef EEPROMWRAPPER_H_ +#define EEPROMWRAPPER_H_ + +#include "FreeRTOS.h" +#include "task.h" +#include "chip.h" +#include +#include + + +typedef void (*IAP_call) (uint32_t[], uint32_t[]); + +typedef struct _eeprom +{ + uint32_t data; + uint32_t addr; + uint32_t size; + uint32_t mode; + uint32_t clock; +} EEPROM; + +#define READ true +#define WRITE false +#define EEPROM_MAX_BUFER_SIZE 1000 + +class EEPROM_Wrapper +{ +public: + /** + * @brief Construct a new eeprom wrapper object + * + */ + EEPROM_Wrapper (); + virtual ~EEPROM_Wrapper (); + /** + * @brief Read a string from EEPROM + * + * @param addr - address to read from + * @param amount - amount of bytes to read + * @return std::string - that was read + */ + std::string str_read_from (uint32_t addr, uint32_t amount); + /** + * @brief Write a string to EEPROM + * + * @param addr - address to write on + * @param str - string to write + */ + void write_to (uint32_t addr, std::string str); + /** + * @brief Read data from EEPROM + * + * @param addr - address to read from + * @param amount - amount of bytes to read + * @return void* - data that was read + */ + void *read_from (uint32_t addr, uint32_t amount); + /** + * @brief Write data to EEPROM + * + * @param addr - address to write on + * @param data - data to be written + * @param size_of_data - size of data to be written + */ + void write_to (uint32_t addr, void *data, uint32_t size_of_data); + +private: + IAP_call iap_exec; + uint32_t command[5], result[5]; + EEPROM rom = { 0, 0, 0, 0, 72000 }; + void eeprom_execute (EEPROM *rom); + void eeprom_use (uint8_t *data, uint32_t addr, uint32_t size, bool mode); + uint8_t buffer[EEPROM_MAX_BUFER_SIZE] = { 0 }; +}; + +#endif /* EEPROMWRAPPER_H_ */