From 8ae731e569e72ab6931b8118d302ce8a7a24c602 Mon Sep 17 00:00:00 2001 From: Connell Reffo Date: Mon, 7 Oct 2024 19:41:24 -0600 Subject: [PATCH 1/8] Added initial QSPI driver files (header + src) --- .gitignore | 3 +- .pre-commit-config.yaml | 63 ++++++++++++++++++++--------------------- QSPI/QSPI_Driver.cpp | 1 + QSPI/QSPI_Driver.hpp | 26 +++++++++++++++++ README.md | 1 + 5 files changed, 61 insertions(+), 33 deletions(-) create mode 100644 QSPI/QSPI_Driver.cpp create mode 100644 QSPI/QSPI_Driver.hpp diff --git a/.gitignore b/.gitignore index 0b2bbbc..8ed112d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ # Ignore .pre-commit-config.yaml -.pre-commit-config.yaml \ No newline at end of file +.vscode/ +.pre-commit-config.yaml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 5cc5c04..0a26cda 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,32 +1,31 @@ -repos: - - - repo: https://github.com/pre-commit/mirrors-clang-format - rev: 7d85583be209cb547946c82fbe51f4bc5dd1d017 - hooks: - - id: clang-format - args: [--style=Google] - files: \.(cpp|hpp|c|h)$ - stages: [commit] - - - repo: https://github.com/pre-commit/mirrors-prettier - rev: v2.5.1 - hooks: - - id: prettier - files: \.(js|ts|jsx|tsx|css|less|html|json|markdown|md|yaml|yml)$ - - - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.4.1 - hooks: - - id: ruff - types_or: [ python, pyi ] - args: [ --fix ] - stages: [commit] - - id: ruff-format - stages: [commit] - - - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.4.0 - hooks: - - id: trailing-whitespace - - id: end-of-file-fixer - - id: check-yaml \ No newline at end of file +repos: + - repo: https://github.com/pre-commit/mirrors-clang-format + rev: 7d85583be209cb547946c82fbe51f4bc5dd1d017 + hooks: + - id: clang-format + args: [--style=Google] + files: \.(cpp|hpp|c|h)$ + stages: [commit] + + - repo: https://github.com/pre-commit/mirrors-prettier + rev: v2.5.1 + hooks: + - id: prettier + files: \.(js|ts|jsx|tsx|css|less|html|json|markdown|md|yaml|yml)$ + + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.4.1 + hooks: + - id: ruff + types_or: [python, pyi] + args: [--fix] + stages: [commit] + - id: ruff-format + stages: [commit] + + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.4.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml diff --git a/QSPI/QSPI_Driver.cpp b/QSPI/QSPI_Driver.cpp new file mode 100644 index 0000000..465bde4 --- /dev/null +++ b/QSPI/QSPI_Driver.cpp @@ -0,0 +1 @@ +#include "QSPI_Driver.hpp" diff --git a/QSPI/QSPI_Driver.hpp b/QSPI/QSPI_Driver.hpp new file mode 100644 index 0000000..24d2d93 --- /dev/null +++ b/QSPI/QSPI_Driver.hpp @@ -0,0 +1,26 @@ +/** + * @author Connell Reffo + * @brief This driver is specific to STM32H743XX chips + * + * To set up correctly in VS Code, add the following to c_cpp_properties.json: + * + * ```json + * "includePath": [ + * "${workspaceFolder}/**", + * "C:/.../AvionicsTemplateRepository/Drivers/CMSIS/Device/ST/STM32H7xx/Include/**", + * "C:/.../AvionicsTemplateRepository/Drivers/CMSIS/Include/**", + * "C:/.../AvionicsTemplateRepository/Drivers/STM32H7xx_HAL_Driver/Inc/**" + * ] + * ``` + */ +#ifndef QSPI_DRIVER_H +#define QSPI_DRIVER_H +#define QUADSPI + +#include +#include +#include + +class QSPIDriver {}; + +#endif // QSPI_DRIVER_H diff --git a/README.md b/README.md index 75072ad..e3c28ca 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # CommunicationSystemsSubmodule + This repository will host drivers for communication protocols such as UART drivers. From 2a3f57348c804b3cd05cef83cf838feb2276f84f Mon Sep 17 00:00:00 2001 From: Connell Reffo Date: Mon, 7 Oct 2024 20:13:44 -0600 Subject: [PATCH 2/8] Some conceptual modelling (mainly backup) --- QSPI/QSPI_Driver.hpp | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/QSPI/QSPI_Driver.hpp b/QSPI/QSPI_Driver.hpp index 24d2d93..38a5df9 100644 --- a/QSPI/QSPI_Driver.hpp +++ b/QSPI/QSPI_Driver.hpp @@ -1,6 +1,6 @@ /** * @author Connell Reffo - * @brief This driver is specific to STM32H743XX chips + * @brief This driver is specific to STM32H7xx chips * * To set up correctly in VS Code, add the following to c_cpp_properties.json: * @@ -21,6 +21,29 @@ #include #include -class QSPIDriver {}; +struct QSPIConfig { + private: + int frequency; + int mode; + int address_size; + + public: + QSPIConfig(int frequency, int mode, int address_size) + : frequency(frequency), mode(mode), address_size(address_size) {} + + int get_frequency() const { return this->frequency; } + + int get_mode() const { return this->mode; } + + int get_address_size() const { return this->address_size; } +}; + +class QSPIDriver { + private: + QSPI_HandleTypeDef *qspi_handle; + + public: + QSPIDriver(QSPI_HandleTypeDef *hqspi) : qspi_handle(hqspi) {} +}; #endif // QSPI_DRIVER_H From dc486905ce8b6bf6a036d647636ec328148fd8bb Mon Sep 17 00:00:00 2001 From: Connell Reffo Date: Tue, 8 Oct 2024 16:16:19 -0600 Subject: [PATCH 3/8] Ironed out usage details. Theoretically implemented command sending (not sure if it works) --- QSPI/Inc/QSPI_DeviceContext.hpp | 25 +++++++++++++ QSPI/Inc/QSPI_Driver.hpp | 34 ++++++++++++++++++ QSPI/Inc/QSPI_Result.hpp | 29 +++++++++++++++ QSPI/QSPI_Driver.cpp | 1 - QSPI/QSPI_Driver.hpp | 49 ------------------------- QSPI/Src/QSPI_Driver.cpp | 63 +++++++++++++++++++++++++++++++++ 6 files changed, 151 insertions(+), 50 deletions(-) create mode 100644 QSPI/Inc/QSPI_DeviceContext.hpp create mode 100644 QSPI/Inc/QSPI_Driver.hpp create mode 100644 QSPI/Inc/QSPI_Result.hpp delete mode 100644 QSPI/QSPI_Driver.cpp delete mode 100644 QSPI/QSPI_Driver.hpp create mode 100644 QSPI/Src/QSPI_Driver.cpp diff --git a/QSPI/Inc/QSPI_DeviceContext.hpp b/QSPI/Inc/QSPI_DeviceContext.hpp new file mode 100644 index 0000000..21493a8 --- /dev/null +++ b/QSPI/Inc/QSPI_DeviceContext.hpp @@ -0,0 +1,25 @@ +#ifndef QSPI_DEVICE_CTX_HPP +#define QSPI_DEVICE_CTX_HPP + +#include + +struct QSPI_DeviceContext { + private: + GPIO_TypeDef &cs_port; + uint16_t cs_pin; + unsigned int timeout; + + public: + QSPI_DeviceContext(GPIO_TypeDef &cs_port, uint16_t cs_pin, + unsigned int timeout = HAL_MAX_DELAY) + : cs_port(cs_port), cs_pin(cs_pin), timeout(timeout) {} + + GPIO_TypeDef *get_cs_port_as_ptr() const { return &this->cs_port; } + GPIO_TypeDef &get_cs_port_as_ref() const { return this->cs_port; } + + uint16_t get_cs_pin() const { return this->cs_pin; } + + unsigned int get_timeout() const { return this->timeout; } +}; + +#endif // QSPI_DEVICE_CTX_HPP diff --git a/QSPI/Inc/QSPI_Driver.hpp b/QSPI/Inc/QSPI_Driver.hpp new file mode 100644 index 0000000..99b46f3 --- /dev/null +++ b/QSPI/Inc/QSPI_Driver.hpp @@ -0,0 +1,34 @@ +#ifndef QSPI_DRIVER_HPP +#define QSPI_DRIVER_HPP +#define QUADSPI + +#include +#include + +// Forward declaration to satisfy linker without include statement +struct QSPI_DeviceContext; +enum class QSPI_Result; + +/** + * This class provides another layer of abstraction over HAL. + * It encapsulates a HAL QSPI handle to provide a safer and easier to use API. + * + * The intent is for only one instance of this class to exist. + */ +class QSPI_Driver { + private: + QSPI_HandleTypeDef &qspi_handle; + + public: + QSPI_Driver(QSPI_HandleTypeDef &hqspi) : qspi_handle(hqspi) {} + + QSPI_Result send_command(QSPI_DeviceContext &ctx, uint8_t *reg_ptr, + uint8_t *cmd_ptr) const; + QSPI_Result read(QSPI_DeviceContext &ctx, uint8_t *cmd_ptr, + uint8_t *dest_ptr) const; + + size_t get_transfer_size() const { return this->qspi_handle.TxXferSize; } + size_t get_receive_size() const { return this->qspi_handle.RxXferSize; } +}; + +#endif // QSPI_DRIVER_HPP diff --git a/QSPI/Inc/QSPI_Result.hpp b/QSPI/Inc/QSPI_Result.hpp new file mode 100644 index 0000000..bef1f96 --- /dev/null +++ b/QSPI/Inc/QSPI_Result.hpp @@ -0,0 +1,29 @@ +#ifndef QSPI_RESULT_HPP +#define QSPI_RESULT_HPP + +#include + +/** + * The `Ok` variant is the only truthy variant + */ +enum class QSPI_Result { Ok = 1, Err = -1, Busy = -2, Timeout = -3 }; + +inline bool operator!(QSPI_Result error) { return error != QSPI_Result::Ok; } + +inline bool operator==(QSPI_Result error, bool value) { + return (error == QSPI_Result::Ok) == value; +} + +inline bool operator!=(QSPI_Result error, bool value) { + return !(error == value); +} + +QSPI_Result from_hal_status(HAL_StatusTypeDef status) { + if (status == HAL_OK) { + return QSPI_Result::Ok; + } + + return static_cast(static_cast(status) * -1); +} + +#endif // QSPI_RESULT_HPP diff --git a/QSPI/QSPI_Driver.cpp b/QSPI/QSPI_Driver.cpp deleted file mode 100644 index 465bde4..0000000 --- a/QSPI/QSPI_Driver.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "QSPI_Driver.hpp" diff --git a/QSPI/QSPI_Driver.hpp b/QSPI/QSPI_Driver.hpp deleted file mode 100644 index 38a5df9..0000000 --- a/QSPI/QSPI_Driver.hpp +++ /dev/null @@ -1,49 +0,0 @@ -/** - * @author Connell Reffo - * @brief This driver is specific to STM32H7xx chips - * - * To set up correctly in VS Code, add the following to c_cpp_properties.json: - * - * ```json - * "includePath": [ - * "${workspaceFolder}/**", - * "C:/.../AvionicsTemplateRepository/Drivers/CMSIS/Device/ST/STM32H7xx/Include/**", - * "C:/.../AvionicsTemplateRepository/Drivers/CMSIS/Include/**", - * "C:/.../AvionicsTemplateRepository/Drivers/STM32H7xx_HAL_Driver/Inc/**" - * ] - * ``` - */ -#ifndef QSPI_DRIVER_H -#define QSPI_DRIVER_H -#define QUADSPI - -#include -#include -#include - -struct QSPIConfig { - private: - int frequency; - int mode; - int address_size; - - public: - QSPIConfig(int frequency, int mode, int address_size) - : frequency(frequency), mode(mode), address_size(address_size) {} - - int get_frequency() const { return this->frequency; } - - int get_mode() const { return this->mode; } - - int get_address_size() const { return this->address_size; } -}; - -class QSPIDriver { - private: - QSPI_HandleTypeDef *qspi_handle; - - public: - QSPIDriver(QSPI_HandleTypeDef *hqspi) : qspi_handle(hqspi) {} -}; - -#endif // QSPI_DRIVER_H diff --git a/QSPI/Src/QSPI_Driver.cpp b/QSPI/Src/QSPI_Driver.cpp new file mode 100644 index 0000000..c9a22d7 --- /dev/null +++ b/QSPI/Src/QSPI_Driver.cpp @@ -0,0 +1,63 @@ +/** + * @author Connell Reffo + * @brief This driver is specific to STM32H7xx chips + * + * To set up correctly in VS Code, add the following to c_cpp_properties.json: + * + * ```json + * "includePath": [ + * "${workspaceFolder}/**", + * "C:/.../AvionicsTemplateRepository/Drivers/CMSIS/Device/ST/STM32H7xx/Include/**", + * "C:/.../AvionicsTemplateRepository/Drivers/CMSIS/Include/**", + * "C:/.../AvionicsTemplateRepository/Drivers/STM32H7xx_HAL_Driver/Inc/**", + * "C:/.../AvionicsTemplateRepository/Core/Inc/**" + * ] + * ``` + */ + +#include "../Inc/QSPI_Driver.hpp" + +#include +#include +#include + +#include "../Inc/QSPI_DeviceContext.hpp" +#include "../Inc/QSPI_Result.hpp" + +QSPI_Result QSPI_Driver::send_command(QSPI_DeviceContext &ctx, uint8_t *reg_ptr, + uint8_t *cmd_ptr) const { + // Pull chip select to low + // Standard communiaction protocol before sending command + const QSPI_Result cs_low_result = from_hal_status(HAL_GPIO_WritePin( + ctx.get_cs_port_as_ptr(), ctx.get_cs_pin(), GPIO_PIN_RESET)); + if (!cs_low_result) { + return cs_low_result; + } + + // Send address of target register + const QSPI_Result send_addr_result = from_hal_status( + HAL_QSPI_Transmit(&this->qspi_handle, reg_ptr, ctx.get_timeout())); + if (!send_addr_result) { + return send_addr_result; + } + + // Write command to register + const QSPI_Result write_cmd_result = from_hal_status( + HAL_QSPI_Transmit(&this->qspi_handle, cmd_ptr, ctx.get_timeout())); + if (!write_cmd_result) { + return write_cmd_result; + } + + // Pull CS back to high + // This signifies the end of the command + const QSPI_Result cs_high_result = from_hal_status(HAL_GPIO_WritePin( + ctx.get_cs_port_as_ptr(), ctx.get_cs_pin(), GPIO_PIN_SET)); + if (!cs_high_result) { + return cs_high_result; + } + + return QSPI_Result::Ok; +} + +QSPI_Result QSPI_Driver::read(QSPI_DeviceContext &ctx, uint8_t *cmd_ptr, + uint8_t *dest_ptr) const {} From 55c06f58ae0e51f3ef1acf806e0ae7445e0c9844 Mon Sep 17 00:00:00 2001 From: Connell Reffo Date: Wed, 9 Oct 2024 09:18:13 -0600 Subject: [PATCH 4/8] Introducing a struct for composing QSPI commands -- Abstraction over HAL version --- QSPI/Inc/QSPI_Command.hpp | 22 ++++++++++++++++++++++ QSPI/Inc/QSPI_DeviceContext.hpp | 8 ++++---- QSPI/Inc/QSPI_Driver.hpp | 13 ++++++------- QSPI/Inc/QSPI_Result.hpp | 8 +------- QSPI/Src/QSPI_Command.cpp | 3 +++ QSPI/Src/QSPI_Driver.cpp | 28 ++++++++-------------------- QSPI/Src/QSPI_Result.cpp | 9 +++++++++ 7 files changed, 53 insertions(+), 38 deletions(-) create mode 100644 QSPI/Inc/QSPI_Command.hpp create mode 100644 QSPI/Src/QSPI_Command.cpp create mode 100644 QSPI/Src/QSPI_Result.cpp diff --git a/QSPI/Inc/QSPI_Command.hpp b/QSPI/Inc/QSPI_Command.hpp new file mode 100644 index 0000000..e3a5948 --- /dev/null +++ b/QSPI/Inc/QSPI_Command.hpp @@ -0,0 +1,22 @@ +#ifndef QSPI_COMMAND_HPP +#define QSPI_COMMAND_HPP + +#include +#include + +enum class QSPI_AddressSize { + OneByte = QSPI_ADDRESS_8_BITS, + TwoBytes = QSPI_ADDRESS_16_BITS, + ThreeBytes = QSPI_ADDRESS_24_BITS, + FourBytes = QSPI_ADDRESS_32_BITS +}; + +struct QSPI_Command { + uint8_t instruction; + uint32_t address; + QSPI_AddressSize address_size; +}; + +QSPI_CommandTypeDef to_hal_cmd(const QSPI_Command &cmd); + +#endif // QSPI_COMMAND_HPP diff --git a/QSPI/Inc/QSPI_DeviceContext.hpp b/QSPI/Inc/QSPI_DeviceContext.hpp index 21493a8..46fc091 100644 --- a/QSPI/Inc/QSPI_DeviceContext.hpp +++ b/QSPI/Inc/QSPI_DeviceContext.hpp @@ -1,25 +1,25 @@ #ifndef QSPI_DEVICE_CTX_HPP #define QSPI_DEVICE_CTX_HPP +#include #include struct QSPI_DeviceContext { private: GPIO_TypeDef &cs_port; uint16_t cs_pin; - unsigned int timeout; + uint32_t timeout; public: QSPI_DeviceContext(GPIO_TypeDef &cs_port, uint16_t cs_pin, - unsigned int timeout = HAL_MAX_DELAY) + uint32_t timeout = HAL_MAX_DELAY) : cs_port(cs_port), cs_pin(cs_pin), timeout(timeout) {} GPIO_TypeDef *get_cs_port_as_ptr() const { return &this->cs_port; } GPIO_TypeDef &get_cs_port_as_ref() const { return this->cs_port; } uint16_t get_cs_pin() const { return this->cs_pin; } - - unsigned int get_timeout() const { return this->timeout; } + uint32_t get_timeout() const { return this->timeout; } }; #endif // QSPI_DEVICE_CTX_HPP diff --git a/QSPI/Inc/QSPI_Driver.hpp b/QSPI/Inc/QSPI_Driver.hpp index 99b46f3..296878d 100644 --- a/QSPI/Inc/QSPI_Driver.hpp +++ b/QSPI/Inc/QSPI_Driver.hpp @@ -6,12 +6,14 @@ #include // Forward declaration to satisfy linker without include statement -struct QSPI_DeviceContext; -enum class QSPI_Result; +#include "QSPI_Command.hpp" +#include "QSPI_DeviceContext.hpp" +#include "QSPI_Result.hpp" /** * This class provides another layer of abstraction over HAL. * It encapsulates a HAL QSPI handle to provide a safer and easier to use API. + * The QSPI handle must be intialized prior to this wrapper being initialized. * * The intent is for only one instance of this class to exist. */ @@ -22,13 +24,10 @@ class QSPI_Driver { public: QSPI_Driver(QSPI_HandleTypeDef &hqspi) : qspi_handle(hqspi) {} - QSPI_Result send_command(QSPI_DeviceContext &ctx, uint8_t *reg_ptr, - uint8_t *cmd_ptr) const; + QSPI_Result send_command(const QSPI_DeviceContext &ctx, + const QSPI_Command &cmd) const; QSPI_Result read(QSPI_DeviceContext &ctx, uint8_t *cmd_ptr, uint8_t *dest_ptr) const; - - size_t get_transfer_size() const { return this->qspi_handle.TxXferSize; } - size_t get_receive_size() const { return this->qspi_handle.RxXferSize; } }; #endif // QSPI_DRIVER_HPP diff --git a/QSPI/Inc/QSPI_Result.hpp b/QSPI/Inc/QSPI_Result.hpp index bef1f96..5ded6e5 100644 --- a/QSPI/Inc/QSPI_Result.hpp +++ b/QSPI/Inc/QSPI_Result.hpp @@ -18,12 +18,6 @@ inline bool operator!=(QSPI_Result error, bool value) { return !(error == value); } -QSPI_Result from_hal_status(HAL_StatusTypeDef status) { - if (status == HAL_OK) { - return QSPI_Result::Ok; - } - - return static_cast(static_cast(status) * -1); -} +QSPI_Result from_hal_status(HAL_StatusTypeDef status); #endif // QSPI_RESULT_HPP diff --git a/QSPI/Src/QSPI_Command.cpp b/QSPI/Src/QSPI_Command.cpp new file mode 100644 index 0000000..0514f8c --- /dev/null +++ b/QSPI/Src/QSPI_Command.cpp @@ -0,0 +1,3 @@ +#include "../Inc/QSPI_Command.hpp" + +QSPI_CommandTypeDef to_hal_cmd(const QSPI_Command &cmd) {} diff --git a/QSPI/Src/QSPI_Driver.cpp b/QSPI/Src/QSPI_Driver.cpp index c9a22d7..41032b5 100644 --- a/QSPI/Src/QSPI_Driver.cpp +++ b/QSPI/Src/QSPI_Driver.cpp @@ -24,37 +24,25 @@ #include "../Inc/QSPI_DeviceContext.hpp" #include "../Inc/QSPI_Result.hpp" -QSPI_Result QSPI_Driver::send_command(QSPI_DeviceContext &ctx, uint8_t *reg_ptr, - uint8_t *cmd_ptr) const { +QSPI_Result QSPI_Driver::send_command(const QSPI_DeviceContext &ctx, + const QSPI_Command &cmd) const { // Pull chip select to low // Standard communiaction protocol before sending command - const QSPI_Result cs_low_result = from_hal_status(HAL_GPIO_WritePin( - ctx.get_cs_port_as_ptr(), ctx.get_cs_pin(), GPIO_PIN_RESET)); - if (!cs_low_result) { - return cs_low_result; - } - - // Send address of target register - const QSPI_Result send_addr_result = from_hal_status( - HAL_QSPI_Transmit(&this->qspi_handle, reg_ptr, ctx.get_timeout())); - if (!send_addr_result) { - return send_addr_result; - } + HAL_GPIO_WritePin(ctx.get_cs_port_as_ptr(), ctx.get_cs_pin(), GPIO_PIN_RESET); // Write command to register + QSPI_CommandTypeDef hal_cmd = to_hal_cmd(cmd); + const QSPI_Result write_cmd_result = from_hal_status( - HAL_QSPI_Transmit(&this->qspi_handle, cmd_ptr, ctx.get_timeout())); + HAL_QSPI_Command(&this->qspi_handle, &hal_cmd, ctx.get_timeout())); + if (!write_cmd_result) { return write_cmd_result; } // Pull CS back to high // This signifies the end of the command - const QSPI_Result cs_high_result = from_hal_status(HAL_GPIO_WritePin( - ctx.get_cs_port_as_ptr(), ctx.get_cs_pin(), GPIO_PIN_SET)); - if (!cs_high_result) { - return cs_high_result; - } + HAL_GPIO_WritePin(ctx.get_cs_port_as_ptr(), ctx.get_cs_pin(), GPIO_PIN_SET); return QSPI_Result::Ok; } diff --git a/QSPI/Src/QSPI_Result.cpp b/QSPI/Src/QSPI_Result.cpp new file mode 100644 index 0000000..3e2ebdc --- /dev/null +++ b/QSPI/Src/QSPI_Result.cpp @@ -0,0 +1,9 @@ +#include "../Inc/QSPI_Result.hpp" + +QSPI_Result from_hal_status(HAL_StatusTypeDef status) { + if (status == HAL_OK) { + return QSPI_Result::Ok; + } + + return static_cast(static_cast(status) * -1); +} From 68cc7f8174e477a1544f237728d98db88948d4fa Mon Sep 17 00:00:00 2001 From: connellr023 Date: Wed, 9 Oct 2024 18:13:15 -0700 Subject: [PATCH 5/8] Ironed out write + read functions --- QSPI/Inc/QSPI_Command.hpp | 6 ++-- QSPI/Inc/QSPI_DeviceContext.hpp | 8 ++--- QSPI/Inc/QSPI_Driver.hpp | 18 +++-------- QSPI/Src/QSPI_Command.cpp | 24 +++++++++++++- QSPI/Src/QSPI_Driver.cpp | 57 ++++++++++++++++++++++++++------- 5 files changed, 82 insertions(+), 31 deletions(-) diff --git a/QSPI/Inc/QSPI_Command.hpp b/QSPI/Inc/QSPI_Command.hpp index e3a5948..5d0bc8d 100644 --- a/QSPI/Inc/QSPI_Command.hpp +++ b/QSPI/Inc/QSPI_Command.hpp @@ -15,8 +15,10 @@ struct QSPI_Command { uint8_t instruction; uint32_t address; QSPI_AddressSize address_size; -}; + size_t transfer_size; + uint8_t *data_buffer = nullptr; -QSPI_CommandTypeDef to_hal_cmd(const QSPI_Command &cmd); + QSPI_CommandTypeDef to_hal_cmd() const; +}; #endif // QSPI_COMMAND_HPP diff --git a/QSPI/Inc/QSPI_DeviceContext.hpp b/QSPI/Inc/QSPI_DeviceContext.hpp index 46fc091..b27f56d 100644 --- a/QSPI/Inc/QSPI_DeviceContext.hpp +++ b/QSPI/Inc/QSPI_DeviceContext.hpp @@ -8,18 +8,18 @@ struct QSPI_DeviceContext { private: GPIO_TypeDef &cs_port; uint16_t cs_pin; - uint32_t timeout; + uint32_t max_timeout; public: QSPI_DeviceContext(GPIO_TypeDef &cs_port, uint16_t cs_pin, - uint32_t timeout = HAL_MAX_DELAY) - : cs_port(cs_port), cs_pin(cs_pin), timeout(timeout) {} + uint32_t max_timeout = HAL_MAX_DELAY) + : cs_port(cs_port), cs_pin(cs_pin), max_timeout(max_timeout) {} GPIO_TypeDef *get_cs_port_as_ptr() const { return &this->cs_port; } GPIO_TypeDef &get_cs_port_as_ref() const { return this->cs_port; } uint16_t get_cs_pin() const { return this->cs_pin; } - uint32_t get_timeout() const { return this->timeout; } + uint32_t get_max_timeout() const { return this->max_timeout; } }; #endif // QSPI_DEVICE_CTX_HPP diff --git a/QSPI/Inc/QSPI_Driver.hpp b/QSPI/Inc/QSPI_Driver.hpp index 296878d..0bb4de7 100644 --- a/QSPI/Inc/QSPI_Driver.hpp +++ b/QSPI/Inc/QSPI_Driver.hpp @@ -1,22 +1,13 @@ #ifndef QSPI_DRIVER_HPP #define QSPI_DRIVER_HPP -#define QUADSPI #include #include -// Forward declaration to satisfy linker without include statement #include "QSPI_Command.hpp" #include "QSPI_DeviceContext.hpp" #include "QSPI_Result.hpp" -/** - * This class provides another layer of abstraction over HAL. - * It encapsulates a HAL QSPI handle to provide a safer and easier to use API. - * The QSPI handle must be intialized prior to this wrapper being initialized. - * - * The intent is for only one instance of this class to exist. - */ class QSPI_Driver { private: QSPI_HandleTypeDef &qspi_handle; @@ -24,10 +15,11 @@ class QSPI_Driver { public: QSPI_Driver(QSPI_HandleTypeDef &hqspi) : qspi_handle(hqspi) {} - QSPI_Result send_command(const QSPI_DeviceContext &ctx, - const QSPI_Command &cmd) const; - QSPI_Result read(QSPI_DeviceContext &ctx, uint8_t *cmd_ptr, - uint8_t *dest_ptr) const; + QSPI_Result write(const QSPI_DeviceContext &ctx, + const QSPI_Command &cmd) const; + + QSPI_Result read(const QSPI_DeviceContext &ctx, + const QSPI_Command &cmd) const; }; #endif // QSPI_DRIVER_HPP diff --git a/QSPI/Src/QSPI_Command.cpp b/QSPI/Src/QSPI_Command.cpp index 0514f8c..cb1ced8 100644 --- a/QSPI/Src/QSPI_Command.cpp +++ b/QSPI/Src/QSPI_Command.cpp @@ -1,3 +1,25 @@ #include "../Inc/QSPI_Command.hpp" -QSPI_CommandTypeDef to_hal_cmd(const QSPI_Command &cmd) {} +QSPI_CommandTypeDef QSPI_Command::to_hal_cmd() const { + QSPI_CommandTypeDef hal_cmd; + + hal_cmd.Address = this->address; + hal_cmd.AddressSize = static_cast(this->address_size); + hal_cmd.AddressMode = QSPI_ADDRESS_NONE; + + hal_cmd.Instruction = this->instruction; + hal_cmd.InstructionMode = QSPI_INSTRUCTION_NONE; + + hal_cmd.AlternateByteMode = QSPI_ALTERNATE_BYTES_1_LINE; + hal_cmd.AlternateBytes = 0; + hal_cmd.AlternateBytesSize = 0; + + hal_cmd.DataMode = QSPI_DATA_1_LINE; + hal_cmd.DummyCycles = 0; + hal_cmd.NbData = this->transfer_size; + hal_cmd.DdrMode = QSPI_DDR_MODE_DISABLE; + hal_cmd.DdrHoldHalfCycle = QSPI_DDR_HHC_ANALOG_DELAY; + hal_cmd.SIOOMode = QSPI_SIOO_INST_EVERY_CMD; + + return hal_cmd; +} diff --git a/QSPI/Src/QSPI_Driver.cpp b/QSPI/Src/QSPI_Driver.cpp index 41032b5..f9dcaab 100644 --- a/QSPI/Src/QSPI_Driver.cpp +++ b/QSPI/Src/QSPI_Driver.cpp @@ -7,11 +7,12 @@ * ```json * "includePath": [ * "${workspaceFolder}/**", - * "C:/.../AvionicsTemplateRepository/Drivers/CMSIS/Device/ST/STM32H7xx/Include/**", - * "C:/.../AvionicsTemplateRepository/Drivers/CMSIS/Include/**", - * "C:/.../AvionicsTemplateRepository/Drivers/STM32H7xx_HAL_Driver/Inc/**", - * "C:/.../AvionicsTemplateRepository/Core/Inc/**" + * "/AvionicsTemplateRepository/Drivers/CMSIS/Device/ST/STM32H7xx/Include/**", + * "/AvionicsTemplateRepository/Drivers/CMSIS/Include/**", + * "/AvionicsTemplateRepository/Drivers/STM32H7xx_HAL_Driver/Inc/**", + * "/AvionicsTemplateRepository/Core/Inc/**" * ] + * "defines": ["QUADSPI"] * ``` */ @@ -24,22 +25,30 @@ #include "../Inc/QSPI_DeviceContext.hpp" #include "../Inc/QSPI_Result.hpp" -QSPI_Result QSPI_Driver::send_command(const QSPI_DeviceContext &ctx, - const QSPI_Command &cmd) const { +QSPI_Result QSPI_Driver::write(const QSPI_DeviceContext &ctx, + const QSPI_Command &cmd) const { // Pull chip select to low // Standard communiaction protocol before sending command HAL_GPIO_WritePin(ctx.get_cs_port_as_ptr(), ctx.get_cs_pin(), GPIO_PIN_RESET); - // Write command to register - QSPI_CommandTypeDef hal_cmd = to_hal_cmd(cmd); + // Setup command + QSPI_CommandTypeDef hal_cmd = cmd.to_hal_cmd(); const QSPI_Result write_cmd_result = from_hal_status( - HAL_QSPI_Command(&this->qspi_handle, &hal_cmd, ctx.get_timeout())); + HAL_QSPI_Command(&this->qspi_handle, &hal_cmd, ctx.get_max_timeout())); if (!write_cmd_result) { return write_cmd_result; } + // Transmit command + const QSPI_Result transmit_cmd_result = from_hal_status(HAL_QSPI_Transmit( + &this->qspi_handle, cmd.data_buffer, ctx.get_max_timeout())); + + if (!transmit_cmd_result) { + return transmit_cmd_result; + } + // Pull CS back to high // This signifies the end of the command HAL_GPIO_WritePin(ctx.get_cs_port_as_ptr(), ctx.get_cs_pin(), GPIO_PIN_SET); @@ -47,5 +56,31 @@ QSPI_Result QSPI_Driver::send_command(const QSPI_DeviceContext &ctx, return QSPI_Result::Ok; } -QSPI_Result QSPI_Driver::read(QSPI_DeviceContext &ctx, uint8_t *cmd_ptr, - uint8_t *dest_ptr) const {} +QSPI_Result QSPI_Driver::read(const QSPI_DeviceContext &ctx, + const QSPI_Command &cmd) const { + // Pull CS low + HAL_GPIO_WritePin(ctx.get_cs_port_as_ptr(), ctx.get_cs_pin(), GPIO_PIN_RESET); + + // Setup command + QSPI_CommandTypeDef hal_cmd = cmd.to_hal_cmd(); + + const QSPI_Result read_cmd_result = from_hal_status( + HAL_QSPI_Command(&this->qspi_handle, &hal_cmd, ctx.get_max_timeout())); + + if (!read_cmd_result) { + return read_cmd_result; + } + + // Receive data + const QSPI_Result receive_cmd_result = from_hal_status(HAL_QSPI_Receive( + &this->qspi_handle, cmd.data_buffer, ctx.get_max_timeout())); + + if (!receive_cmd_result) { + return receive_cmd_result; + } + + // Pull CS back high + HAL_GPIO_WritePin(ctx.get_cs_port_as_ptr(), ctx.get_cs_pin(), GPIO_PIN_SET); + + return QSPI_Result::Ok; +} From 7d8ba17b35b557f14f38f336bcf5ad3f513a42ea Mon Sep 17 00:00:00 2001 From: connellr023 Date: Wed, 9 Oct 2024 18:26:30 -0700 Subject: [PATCH 6/8] Added documentation --- QSPI/Inc/QSPI_Command.hpp | 35 ++++++++++++++++++++------- QSPI/Inc/QSPI_DeviceContext.hpp | 43 ++++++++++++++++++++++++++++++--- QSPI/Inc/QSPI_Driver.hpp | 29 ++++++++++++++++++++-- QSPI/Src/QSPI_Command.cpp | 3 +++ QSPI/Src/QSPI_Driver.cpp | 14 ----------- README.md | 21 ++++++++++++++-- 6 files changed, 115 insertions(+), 30 deletions(-) diff --git a/QSPI/Inc/QSPI_Command.hpp b/QSPI/Inc/QSPI_Command.hpp index 5d0bc8d..3aa8ac0 100644 --- a/QSPI/Inc/QSPI_Command.hpp +++ b/QSPI/Inc/QSPI_Command.hpp @@ -4,20 +4,37 @@ #include #include +/** + * @brief Enum class representing the size of the QSPI address. + */ enum class QSPI_AddressSize { - OneByte = QSPI_ADDRESS_8_BITS, - TwoBytes = QSPI_ADDRESS_16_BITS, - ThreeBytes = QSPI_ADDRESS_24_BITS, - FourBytes = QSPI_ADDRESS_32_BITS + OneByte = QSPI_ADDRESS_8_BITS, ///< 8-bit address + TwoBytes = QSPI_ADDRESS_16_BITS, ///< 16-bit address + ThreeBytes = QSPI_ADDRESS_24_BITS, ///< 24-bit address + FourBytes = QSPI_ADDRESS_32_BITS ///< 32-bit address }; +/** + * @brief Struct representing a QSPI command. + * + * This struct encapsulates the details of a QSPI command, including the + * instruction, address, address size, transfer size, and data buffer. It also + * provides a method to convert the command to a HAL-compatible QSPI command + * structure. + */ struct QSPI_Command { - uint8_t instruction; - uint32_t address; - QSPI_AddressSize address_size; - size_t transfer_size; - uint8_t *data_buffer = nullptr; + uint8_t instruction; ///< The instruction byte for the QSPI command. + uint32_t address; ///< The address for the QSPI command. + QSPI_AddressSize address_size; ///< The size of the address. + size_t transfer_size; ///< The size of the data transfer. + uint8_t *data_buffer = nullptr; ///< Pointer to the data buffer. + /** + * @brief Converts the QSPI command to a HAL-compatible QSPI command + * structure. + * + * @return QSPI_CommandTypeDef The HAL-compatible QSPI command structure. + */ QSPI_CommandTypeDef to_hal_cmd() const; }; diff --git a/QSPI/Inc/QSPI_DeviceContext.hpp b/QSPI/Inc/QSPI_DeviceContext.hpp index b27f56d..591a2ac 100644 --- a/QSPI/Inc/QSPI_DeviceContext.hpp +++ b/QSPI/Inc/QSPI_DeviceContext.hpp @@ -4,21 +4,58 @@ #include #include +/** + * @brief Struct representing the context of a QSPI device. + * + * This struct encapsulates the context required for QSPI communication, + * including the chip select (CS) port and pin, and the maximum timeout for + * operations. + */ struct QSPI_DeviceContext { private: - GPIO_TypeDef &cs_port; - uint16_t cs_pin; - uint32_t max_timeout; + GPIO_TypeDef &cs_port; ///< Reference to the GPIO port for chip select. + uint16_t cs_pin; ///< Pin number for chip select. + uint32_t max_timeout; ///< Maximum timeout for QSPI operations. public: + /** + * @brief Constructor for QSPI_DeviceContext. + * + * @param cs_port Reference to the GPIO port for chip select. + * @param cs_pin Pin number for chip select. + * @param max_timeout Maximum timeout for QSPI operations (default is + * HAL_MAX_DELAY). + */ QSPI_DeviceContext(GPIO_TypeDef &cs_port, uint16_t cs_pin, uint32_t max_timeout = HAL_MAX_DELAY) : cs_port(cs_port), cs_pin(cs_pin), max_timeout(max_timeout) {} + /** + * @brief Get the chip select port as a pointer. + * + * @return GPIO_TypeDef* Pointer to the GPIO port for chip select. + */ GPIO_TypeDef *get_cs_port_as_ptr() const { return &this->cs_port; } + + /** + * @brief Get the chip select port as a reference. + * + * @return GPIO_TypeDef& Reference to the GPIO port for chip select. + */ GPIO_TypeDef &get_cs_port_as_ref() const { return this->cs_port; } + /** + * @brief Get the chip select pin number. + * + * @return uint16_t Pin number for chip select. + */ uint16_t get_cs_pin() const { return this->cs_pin; } + + /** + * @brief Get the maximum timeout for QSPI operations. + * + * @return uint32_t Maximum timeout for QSPI operations. + */ uint32_t get_max_timeout() const { return this->max_timeout; } }; diff --git a/QSPI/Inc/QSPI_Driver.hpp b/QSPI/Inc/QSPI_Driver.hpp index 0bb4de7..473f31f 100644 --- a/QSPI/Inc/QSPI_Driver.hpp +++ b/QSPI/Inc/QSPI_Driver.hpp @@ -1,23 +1,48 @@ #ifndef QSPI_DRIVER_HPP #define QSPI_DRIVER_HPP -#include #include #include "QSPI_Command.hpp" #include "QSPI_DeviceContext.hpp" #include "QSPI_Result.hpp" +/** + * @brief Class representing a QSPI driver. + * + * This class provides methods to perform read and write operations using QSPI. + * It encapsulates a QSPI handle and provides methods to interact with QSPI + * devices. + */ class QSPI_Driver { private: - QSPI_HandleTypeDef &qspi_handle; + QSPI_HandleTypeDef &qspi_handle; ///< Reference to the QSPI handle. public: + /** + * @brief Constructor for QSPI_Driver. + * + * @param hqspi Reference to the QSPI handle. + */ QSPI_Driver(QSPI_HandleTypeDef &hqspi) : qspi_handle(hqspi) {} + /** + * @brief Write data to a QSPI device. + * + * @param ctx The context of the QSPI device. + * @param cmd The QSPI command to be executed. + * @return QSPI_Result The result of the write operation. + */ QSPI_Result write(const QSPI_DeviceContext &ctx, const QSPI_Command &cmd) const; + /** + * @brief Read data from a QSPI device. + * + * @param ctx The context of the QSPI device. + * @param cmd The QSPI command to be executed. + * @return QSPI_Result The result of the read operation. + */ QSPI_Result read(const QSPI_DeviceContext &ctx, const QSPI_Command &cmd) const; }; diff --git a/QSPI/Src/QSPI_Command.cpp b/QSPI/Src/QSPI_Command.cpp index cb1ced8..b680065 100644 --- a/QSPI/Src/QSPI_Command.cpp +++ b/QSPI/Src/QSPI_Command.cpp @@ -1,5 +1,8 @@ #include "../Inc/QSPI_Command.hpp" +// If anything causes problems, it probably has to do with this function +// This requires testing along with the rest of the QSPI driver but this is the +// most likely source of issues QSPI_CommandTypeDef QSPI_Command::to_hal_cmd() const { QSPI_CommandTypeDef hal_cmd; diff --git a/QSPI/Src/QSPI_Driver.cpp b/QSPI/Src/QSPI_Driver.cpp index f9dcaab..4b8923e 100644 --- a/QSPI/Src/QSPI_Driver.cpp +++ b/QSPI/Src/QSPI_Driver.cpp @@ -1,19 +1,5 @@ /** * @author Connell Reffo - * @brief This driver is specific to STM32H7xx chips - * - * To set up correctly in VS Code, add the following to c_cpp_properties.json: - * - * ```json - * "includePath": [ - * "${workspaceFolder}/**", - * "/AvionicsTemplateRepository/Drivers/CMSIS/Device/ST/STM32H7xx/Include/**", - * "/AvionicsTemplateRepository/Drivers/CMSIS/Include/**", - * "/AvionicsTemplateRepository/Drivers/STM32H7xx_HAL_Driver/Inc/**", - * "/AvionicsTemplateRepository/Core/Inc/**" - * ] - * "defines": ["QUADSPI"] - * ``` */ #include "../Inc/QSPI_Driver.hpp" diff --git a/README.md b/README.md index e3c28ca..15592f0 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,20 @@ -# CommunicationSystemsSubmodule +# Communication Systems Submodule -This repository will host drivers for communication protocols such as UART drivers. +> This repository will host drivers for communication protocols such as UART drivers. + +### Setting up VSCode + +Add the following to `c_cpp_properties.json`: + +```json +"includePath": [ + "${workspaceFolder}/**", + "/AvionicsTemplateRepository/Drivers/CMSIS/Device/ST/STM32H7xx/Include/**", + "/AvionicsTemplateRepository/Drivers/CMSIS/Include/**", + "/AvionicsTemplateRepository/Drivers/STM32H7xx_HAL_Driver/Inc/**", + "/AvionicsTemplateRepository/Core/Inc/**" +], +"defines": ["QUADSPI"] +``` + +replace `` with the path to `AvionicsTemplateRepository`. From f801a7ed2b65bb0cbe6d94da322e4a43ae79c882 Mon Sep 17 00:00:00 2001 From: connellr023 Date: Wed, 16 Oct 2024 19:27:19 -0600 Subject: [PATCH 7/8] Starting DMA work --- QSPI_DMA/Inc/QSPI_DMA_Driver.hpp | 17 +++++++++++++++++ QSPI_DMA/Src/QSPI_DMA_Driver.cpp | 1 + 2 files changed, 18 insertions(+) create mode 100644 QSPI_DMA/Inc/QSPI_DMA_Driver.hpp create mode 100644 QSPI_DMA/Src/QSPI_DMA_Driver.cpp diff --git a/QSPI_DMA/Inc/QSPI_DMA_Driver.hpp b/QSPI_DMA/Inc/QSPI_DMA_Driver.hpp new file mode 100644 index 0000000..0de8f9b --- /dev/null +++ b/QSPI_DMA/Inc/QSPI_DMA_Driver.hpp @@ -0,0 +1,17 @@ +#ifndef QSPI_DMA_DRIVER_HPP +#define QSPI_DMA_DRIVER_HPP + +#include + +// https://github.com/STMicroelectronics/STM32CubeH7/tree/master/Projects/STM32H743I-EVAL/Examples/QSPI + +class QSPI_DMA_Driver +{ +private: + QSPI_HandleTypeDef &handle; + +public: + QSPI_DMA_Driver(QSPI_HandleTypeDef *hqspi) : handle(*hqspi) {} +}; + +#endif // QPSI_DMA_DRIVER_HPP diff --git a/QSPI_DMA/Src/QSPI_DMA_Driver.cpp b/QSPI_DMA/Src/QSPI_DMA_Driver.cpp new file mode 100644 index 0000000..52f45e8 --- /dev/null +++ b/QSPI_DMA/Src/QSPI_DMA_Driver.cpp @@ -0,0 +1 @@ +#include "../Inc/QSPI_DMA_Driver.hpp" From d81b4d43df8fcd8197979687433d993362f47752 Mon Sep 17 00:00:00 2001 From: connellr023 Date: Sat, 19 Oct 2024 14:18:07 -0600 Subject: [PATCH 8/8] Added QSPI DMA transfer function + refactoring QSPI_DMA files --- QSPI_DMA/Inc/QSPI_DMA_Context.hpp | 44 +++++++++++++++++++++++++++++++ QSPI_DMA/Inc/QSPI_DMA_Driver.hpp | 17 ------------ QSPI_DMA/Inc/QSPI_DMA_Result.hpp | 21 +++++++++++++++ QSPI_DMA/Src/QSPI_DMA_Context.cpp | 26 ++++++++++++++++++ QSPI_DMA/Src/QSPI_DMA_Driver.cpp | 1 - 5 files changed, 91 insertions(+), 18 deletions(-) create mode 100644 QSPI_DMA/Inc/QSPI_DMA_Context.hpp delete mode 100644 QSPI_DMA/Inc/QSPI_DMA_Driver.hpp create mode 100644 QSPI_DMA/Inc/QSPI_DMA_Result.hpp create mode 100644 QSPI_DMA/Src/QSPI_DMA_Context.cpp delete mode 100644 QSPI_DMA/Src/QSPI_DMA_Driver.cpp diff --git a/QSPI_DMA/Inc/QSPI_DMA_Context.hpp b/QSPI_DMA/Inc/QSPI_DMA_Context.hpp new file mode 100644 index 0000000..20b0880 --- /dev/null +++ b/QSPI_DMA/Inc/QSPI_DMA_Context.hpp @@ -0,0 +1,44 @@ +#ifndef QSPI_DMA_CONTEXT_HPP +#define QSPI_DMA_CONTEXT_HPP + +#include + +#include "./QSPI_DMA_Result.hpp" + +// https://github.com/STMicroelectronics/STM32CubeH7/tree/master/Projects/STM32H743I-EVAL/Examples/QSPI + +class QSPI_DMA_Context +{ +private: + QSPI_HandleTypeDef &qspi_handle; + DMA_HandleTypeDef &dma_handle; + + uint8_t *src_buffer; + uint8_t *dest_buffer; + + // Total flash capacity in bytes + size_t flash_capacity; + + // Amount of bytes that + size_t amount_used; + +public: + QSPI_DMA_Context( + QSPI_HandleTypeDef *hqspi, + DMA_HandleTypeDef *hdma, + uint8_t *src_buffer, + uint8_t *dest_buffer, + size_t flash_capacity, + ) : + qspi_handle(*hqspi), + dma_handle(*hdma), + src_buffer(src_buffer), + dest_buffer(dest_buffer), + flash_capacity(flash_capacity), + amount_used(0) + {} + + QSPI_DMA_Result transfer(size_t bytes, uint64_t max_delay = HAL_MAX_DELAY); +}; + +#endif // QPSI_DMA_CONTEXT_HPP diff --git a/QSPI_DMA/Inc/QSPI_DMA_Driver.hpp b/QSPI_DMA/Inc/QSPI_DMA_Driver.hpp deleted file mode 100644 index 0de8f9b..0000000 --- a/QSPI_DMA/Inc/QSPI_DMA_Driver.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef QSPI_DMA_DRIVER_HPP -#define QSPI_DMA_DRIVER_HPP - -#include - -// https://github.com/STMicroelectronics/STM32CubeH7/tree/master/Projects/STM32H743I-EVAL/Examples/QSPI - -class QSPI_DMA_Driver -{ -private: - QSPI_HandleTypeDef &handle; - -public: - QSPI_DMA_Driver(QSPI_HandleTypeDef *hqspi) : handle(*hqspi) {} -}; - -#endif // QPSI_DMA_DRIVER_HPP diff --git a/QSPI_DMA/Inc/QSPI_DMA_Result.hpp b/QSPI_DMA/Inc/QSPI_DMA_Result.hpp new file mode 100644 index 0000000..2010319 --- /dev/null +++ b/QSPI_DMA/Inc/QSPI_DMA_Result.hpp @@ -0,0 +1,21 @@ +#ifndef QSPI_DMA_Result_RESULT_HPP +#define QSPI_DMA_Result_RESULT_HPP + +#include + +/** + * The `Ok` variant is the only truthy variant + */ +enum class QSPI_DMA_Result { Ok = 1, Err = -1, Busy = -2, Timeout = -3, Overflow = -4 }; + +inline bool operator!(QSPI_DMA_Result error) { return error != QSPI_DMA_Result::Ok; } + +inline bool operator==(QSPI_DMA_Result error, bool value) { + return (error == QSPI_DMA_Result::Ok) == value; +} + +inline bool operator!=(QSPI_DMA_Result error, bool value) { + return !(error == value); +} + +#endif // QSPI_DMA_Result_HPP diff --git a/QSPI_DMA/Src/QSPI_DMA_Context.cpp b/QSPI_DMA/Src/QSPI_DMA_Context.cpp new file mode 100644 index 0000000..d0d3f7f --- /dev/null +++ b/QSPI_DMA/Src/QSPI_DMA_Context.cpp @@ -0,0 +1,26 @@ +#include "../Inc/QSPI_DMA_Context.hpp" + +QSPI_DMA_Result QSPI_DMA_Context::transfer(size_t bytes, uint64_t max_delay = HAL_MAX_DELAY) { + const size_t new_bytes_used = this->amount_used + bytes; + + if (new_bytes_used > this->flash_capacity) { + return QSPI_DMA_Result::Overflow; + } + + HAL_DMA_Start(&this->dma_handle, (uint32_t) this->src_buffer, (uint32_t) this->dest_buffer, bytes); + + HAL_StatusTypeDef status = HAL_BUSY; + + while (status != HAL_OK) { + status = HAL_DMA_PollForTransfer(&this->dma_handle, HAL_DMA_FULL_TRANSFER, max_delay); + __NOP(); + } + + if (!status) { + return QSPI_DMA_Result::Err; + } + + this->amount_used = new_bytes_used; + + return QSPI_DMA_Result::Ok; +} diff --git a/QSPI_DMA/Src/QSPI_DMA_Driver.cpp b/QSPI_DMA/Src/QSPI_DMA_Driver.cpp deleted file mode 100644 index 52f45e8..0000000 --- a/QSPI_DMA/Src/QSPI_DMA_Driver.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "../Inc/QSPI_DMA_Driver.hpp"