🧑‍💻 Update GD32 MFL Platform, Arduino Core (#27830)

This commit is contained in:
B 2025-05-05 11:08:45 -07:00 committed by GitHub
parent 735cd9a092
commit c9e7d6f55c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 253 additions and 242 deletions

View file

@ -105,7 +105,7 @@ extern "C" char* dtostrf(double val, signed char width, unsigned char prec, char
class MarlinHAL {
public:
// Before setup()
MarlinHAL() {}
MarlinHAL() = default;
// Watchdog
static void watchdog_init() IF_DISABLED(USE_WATCHDOG, {});

View file

@ -33,8 +33,8 @@
using namespace arduino;
MarlinSerial& MarlinSerial::get_instance(usart::USART_Base Base, pin_size_t rxPin, pin_size_t txPin) {
UsartSerial& serial = UsartSerial::get_instance(Base, rxPin, txPin);
auto MarlinSerial::get_instance(usart::USART_Base Base, pin_size_t rxPin, pin_size_t txPin) -> MarlinSerial& {
auto& serial = UsartSerial::get_instance(Base, rxPin, txPin);
return *reinterpret_cast<MarlinSerial*>(&serial);
}
@ -61,7 +61,7 @@ MarlinSerial& MarlinSerial::get_instance(usart::USART_Base Base, pin_size_t rxPi
static void emergency_callback() {
if (!current_serial_instance) return;
const uint8_t last_data = current_serial_instance->get_last_data();
const auto last_data = current_serial_instance->get_last_data();
emergency_parser.update(current_serial_instance->emergency_state, last_data);
}

View file

@ -43,7 +43,7 @@
using namespace arduino;
struct MarlinSerial : public UsartSerial {
static MarlinSerial& get_instance(usart::USART_Base Base, pin_size_t rxPin = NO_PIN, pin_size_t txPin = NO_PIN);
static auto get_instance(usart::USART_Base Base, pin_size_t rxPin = NO_PIN, pin_size_t txPin = NO_PIN) -> MarlinSerial&;
void begin(unsigned long baudrate, uint16_t config);
inline void begin(unsigned long baudrate) { begin(baudrate, SERIAL_8N1); }
@ -57,7 +57,7 @@ struct MarlinSerial : public UsartSerial {
EmergencyParser::State emergency_state;
// Accessor method to get the last received byte
uint8_t get_last_data() { return usart_.get_last_data(); }
auto get_last_data() -> uint8_t { return usart_.get_last_data(); }
// Register the emergency callback
void register_emergency_callback(void (*callback)());

View file

@ -21,26 +21,27 @@
*/
#pragma once
// Fast I/O interfaces for GD32F303RE
// Fast I/O interfaces for GD32
#include <GPIO.hpp>
#include <variant.h>
#include <PinOps.hpp>
#include <PinOpsMap.hpp>
template<typename T>
static inline void fast_write_pin_wrapper(pin_size_t IO, T V) {
auto port = getPortFromPin(IO);
auto pin = getPinInPort(IO);
if (static_cast<bool>(V)) gpio::fast_set_pin(port, pin);
else gpio::fast_clear_pin(port, pin);
const PortPinPair& pp = port_pin_map[IO];
gpio::fast_write_pin(pp.port, pp.pin, static_cast<bool>(V));
}
static inline bool fast_read_pin_wrapper(pin_size_t IO) {
return gpio::fast_read_pin(getPortFromPin(IO), getPinInPort(IO));
static inline auto fast_read_pin_wrapper(pin_size_t IO) -> bool {
const PortPinPair& pp = port_pin_map[IO];
return gpio::fast_read_pin(pp.port, pp.pin);
}
static inline void fast_toggle_pin_wrapper(pin_size_t IO) {
gpio::fast_toggle_pin(getPortFromPin(IO), getPinInPort(IO));
const PortPinPair& pp = port_pin_map[IO];
gpio::fast_toggle_pin(pp.port, pp.pin);
}
// ------------------------

View file

@ -70,8 +70,9 @@ bool isAnalogPin(const pin_t pin) {
if (!isValidPin(pin)) return false;
if (getAdcChannel(pin) != adc::ADC_Channel::INVALID) {
auto& instance = gpio::GPIO::get_instance(getPortFromPin(pin)).value();
return instance.get_pin_mode(getPinInPort(pin)) == gpio::Pin_Mode::ANALOG && !M43_NEVER_TOUCH(pin);
const PortPinPair& pp = port_pin_map[pin];
auto& instance = gpio::GPIO::get_instance(pp.port).value();
return instance.get_pin_mode(pp.pin) == gpio::Pin_Mode::ANALOG && !M43_NEVER_TOUCH(pin);
}
return false;
@ -80,8 +81,9 @@ bool isAnalogPin(const pin_t pin) {
bool getValidPinMode(const pin_t pin) {
if (!isValidPin(pin)) return false;
auto& instance = gpio::GPIO::get_instance(getPortFromPin(pin)).value();
gpio::Pin_Mode mode = instance.get_pin_mode(getPinInPort(pin));
const PortPinPair& pp = port_pin_map[pin];
auto& instance = gpio::GPIO::get_instance(pp.port).value();
gpio::Pin_Mode mode = instance.get_pin_mode(pp.pin);
return mode != gpio::Pin_Mode::ANALOG && mode != gpio::Pin_Mode::INPUT_FLOATING &&
mode != gpio::Pin_Mode::INPUT_PULLUP && mode != gpio::Pin_Mode::INPUT_PULLDOWN;

View file

@ -28,7 +28,7 @@
namespace sdio {
CardDMA& CardDMA::get_instance() {
auto CardDMA::get_instance() -> CardDMA& {
static CardDMA instance;
return instance;
}
@ -38,9 +38,7 @@ CardDMA::CardDMA() :
sdcard_cid_{0U, 0U, 0U, 0U},
sdcard_scr_{0U, 0U},
desired_clock_(Default_Desired_Clock),
stop_condition_(0U),
total_bytes_(0U),
count_(0U),
sdio_(SDIO::get_instance()),
config_(sdio_.get_config()),
dmaBase_(dma::DMA_Base::DMA1_BASE),
@ -50,15 +48,15 @@ CardDMA::CardDMA() :
transfer_error_(SDIO_Error_Type::OK),
interface_version_(Interface_Version::UNKNOWN),
card_type_(Card_Type::UNKNOWN),
current_state_(Operational_State::READY),
transfer_end_(false),
is_rx_(false),
multiblock_(false),
current_state_(Operational_State::READY)
is_rx_(false)
{
}
// Initialize card and put in standby state
SDIO_Error_Type CardDMA::init() {
auto CardDMA::init() -> SDIO_Error_Type {
// Reset SDIO peripheral
sdio_.reset();
sync_domains();
@ -79,7 +77,7 @@ SDIO_Error_Type CardDMA::init() {
}
// Startup command procedure according to SDIO specification
SDIO_Error_Type CardDMA::begin_startup_procedure() {
auto CardDMA::begin_startup_procedure() -> SDIO_Error_Type {
sdio_.set_power_mode(Power_Control::POWER_ON);
sdio_.set_clock_enable(true);
sync_domains();
@ -124,12 +122,12 @@ SDIO_Error_Type CardDMA::begin_startup_procedure() {
}
// Voltage validation
SDIO_Error_Type CardDMA::validate_voltage() {
auto CardDMA::validate_voltage() -> SDIO_Error_Type {
uint32_t response = 0U;
uint32_t count = 0U;
bool valid_voltage = false;
while ((count < Max_Voltage_Checks) && (valid_voltage == false)) {
while (count < Max_Voltage_Checks && valid_voltage == false) {
if (send_command_and_check(Command_Index::CMD55, 0, Command_Response::RSP_SHORT,
Wait_Type::WT_NONE, [this, cmd = Command_Index::CMD55]() {
return get_r1_result(cmd);
@ -144,7 +142,7 @@ SDIO_Error_Type CardDMA::validate_voltage() {
return SDIO_Error_Type::ACMD41_FAILED;
}
response = sdio_.get_response(Response_Type::RESPONSE0);
valid_voltage = (((response >> 31U) == 1U) ? true : false);
valid_voltage = ((response >> 31U) == 1U);
count++;
}
@ -171,7 +169,7 @@ void CardDMA::begin_shutdown_procedure() {
}
// Initialize card
SDIO_Error_Type CardDMA::card_init() {
auto CardDMA::card_init() -> SDIO_Error_Type {
if (sdio_.get_power_mode() == static_cast<uint32_t>(Power_Control::POWER_OFF)) {
return SDIO_Error_Type::INVALID_OPERATION;
}
@ -221,7 +219,7 @@ SDIO_Error_Type CardDMA::card_init() {
return SDIO_Error_Type::OK;
}
SDIO_Error_Type CardDMA::store_cid() {
auto CardDMA::store_cid() -> SDIO_Error_Type {
// Store the CID register values
sdcard_cid_[0] = sdio_.get_response(Response_Type::RESPONSE0);
sdcard_cid_[1] = sdio_.get_response(Response_Type::RESPONSE1);
@ -231,7 +229,7 @@ SDIO_Error_Type CardDMA::store_cid() {
return SDIO_Error_Type::OK;
}
SDIO_Error_Type CardDMA::store_csd() {
auto CardDMA::store_csd() -> SDIO_Error_Type {
// Store the CSD register values
sdcard_csd_[0] = sdio_.get_response(Response_Type::RESPONSE0);
sdcard_csd_[1] = sdio_.get_response(Response_Type::RESPONSE1);
@ -241,7 +239,7 @@ SDIO_Error_Type CardDMA::store_csd() {
return SDIO_Error_Type::OK;
}
SDIO_Error_Type CardDMA::set_hardware_bus_width(Bus_Width width) {
auto CardDMA::set_hardware_bus_width(Bus_Width width) -> SDIO_Error_Type {
if (card_type_ == Card_Type::SD_MMC || width == Bus_Width::WIDTH_8BIT) {
return SDIO_Error_Type::UNSUPPORTED_FUNCTION;
}
@ -283,7 +281,7 @@ SDIO_Error_Type CardDMA::set_hardware_bus_width(Bus_Width width) {
return SDIO_Error_Type::UNSUPPORTED_FUNCTION;
}
SDIO_Error_Type CardDMA::read(uint8_t* buf, uint32_t address, uint32_t count) {
auto CardDMA::read(uint8_t* buf, uint32_t address, uint32_t count) -> SDIO_Error_Type {
if (current_state_ == Operational_State::READY) {
transfer_error_ = SDIO_Error_Type::OK;
current_state_ = Operational_State::BUSY;
@ -340,7 +338,7 @@ SDIO_Error_Type CardDMA::read(uint8_t* buf, uint32_t address, uint32_t count) {
}
}
SDIO_Error_Type CardDMA::write(uint8_t* buf, uint32_t address, uint32_t count) {
auto CardDMA::write(uint8_t* buf, uint32_t address, uint32_t count) -> SDIO_Error_Type {
// Enable the interrupts
sdio_.set_interrupt_enable(Interrupt_Type::DTCRCERRIE, true);
sdio_.set_interrupt_enable(Interrupt_Type::DTTMOUTIE, true);
@ -363,7 +361,7 @@ SDIO_Error_Type CardDMA::write(uint8_t* buf, uint32_t address, uint32_t count) {
// CMD25/CMD24 (WRITE_MULTIPLE_BLOCK/WRITE_BLOCK) send write command
Command_Index write_cmd = (count > 1U) ? Command_Index::CMD25 : Command_Index::CMD24;
if (send_command_and_check(write_cmd, address, Command_Response::RSP_SHORT,
Wait_Type::WT_NONE, [this, cmd = write_cmd]() {
Wait_Type::WT_NONE, [this, cmd = write_cmd]() {
return get_r1_result(cmd);
}) != SDIO_Error_Type::OK) {
sdio_.clear_multiple_interrupt_flags(clear_common_flags);
@ -379,16 +377,16 @@ SDIO_Error_Type CardDMA::write(uint8_t* buf, uint32_t address, uint32_t count) {
Block_Size block_size = get_data_block_size_index(total_bytes_);
sdio_.set_data_state_machine_and_send(Data_Timeout, total_bytes_, block_size,
Transfer_Mode::BLOCK, Transfer_Direction::SDIO_TO_CARD, true);
Transfer_Mode::BLOCK, Transfer_Direction::SDIO_TO_CARD, true);
while ((dma_.get_flag(dma::Status_Flags::FLAG_FTFIF)) || (dma_.get_flag(dma::Status_Flags::FLAG_ERRIF))) {
while (dma_.get_flag(dma::Status_Flags::FLAG_FTFIF) || dma_.get_flag(dma::Status_Flags::FLAG_ERRIF)) {
// Wait for the IRQ handler to clear
}
return SDIO_Error_Type::OK;
}
SDIO_Error_Type CardDMA::erase(uint32_t address_start, uint32_t address_end) {
auto CardDMA::erase(uint32_t address_start, uint32_t address_end) -> SDIO_Error_Type {
SDIO_Error_Type result = SDIO_Error_Type::OK;
// Card command classes CSD
@ -459,9 +457,11 @@ void CardDMA::handle_interrupts() {
disable_all_interrupts();
sdio_.set_data_state_machine_enable(false);
if ((multiblock_) && (!is_rx_)) {
if (multiblock_ && !is_rx_) {
transfer_error_ = stop_transfer();
if (transfer_error_ != SDIO_Error_Type::OK) {}
if (transfer_error_ != SDIO_Error_Type::OK) {
return;
}
}
if (!is_rx_) {
@ -497,7 +497,7 @@ void CardDMA::handle_interrupts() {
}
}
SDIO_Error_Type CardDMA::select_deselect() {
auto CardDMA::select_deselect() -> SDIO_Error_Type {
// CMD7 (SELECT/DESELECT_CARD)
if (send_command_and_check(Command_Index::CMD7, static_cast<uint32_t>(sdcard_rca_ << RCA_Shift),
Command_Response::RSP_SHORT, Wait_Type::WT_NONE, [this, cmd = Command_Index::CMD7]() {
@ -508,7 +508,7 @@ SDIO_Error_Type CardDMA::select_deselect() {
return SDIO_Error_Type::OK;
}
SDIO_Error_Type CardDMA::get_card_interface_status(uint32_t* status) {
auto CardDMA::get_card_interface_status(uint32_t* status) -> SDIO_Error_Type {
if (status == nullptr) return SDIO_Error_Type::INVALID_PARAMETER;
// CMD13 (SEND_STATUS)
@ -524,7 +524,7 @@ SDIO_Error_Type CardDMA::get_card_interface_status(uint32_t* status) {
return SDIO_Error_Type::OK;
}
SDIO_Error_Type CardDMA::get_sdcard_status(uint32_t* status) {
auto CardDMA::get_sdcard_status(uint32_t* status) -> SDIO_Error_Type {
uint32_t count = 0U;
// CMD16 (SET_BLOCKLEN)
@ -599,7 +599,7 @@ void CardDMA::check_dma_complete() {
}
}
SDIO_Error_Type CardDMA::stop_transfer() {
auto CardDMA::stop_transfer() -> SDIO_Error_Type {
// CMD12 (STOP_TRANSMISSION)
if (send_command_and_check(Command_Index::CMD12, 0, Command_Response::RSP_SHORT, Wait_Type::WT_NONE, [this, cmd = Command_Index::CMD12]() {
return get_r1_result(cmd);
@ -609,7 +609,7 @@ SDIO_Error_Type CardDMA::stop_transfer() {
return SDIO_Error_Type::OK;
}
Transfer_State CardDMA::get_transfer_state() {
auto CardDMA::get_transfer_state() -> Transfer_State {
Transfer_State transfer_state = Transfer_State::IDLE;
if (sdio_.get_flag(Status_Flags::FLAG_TXRUN) | sdio_.get_flag(Status_Flags::FLAG_RXRUN)) {
transfer_state = Transfer_State::BUSY;
@ -618,7 +618,7 @@ Transfer_State CardDMA::get_transfer_state() {
return transfer_state;
}
uint32_t CardDMA::get_card_capacity() const {
[[nodiscard]] auto CardDMA::get_card_capacity() const -> uint32_t {
auto extract_bits = [](uint32_t value, uint8_t start_bit, uint8_t length) -> uint32_t {
return (value >> start_bit) & ((1U << length) - 1U);
};
@ -665,7 +665,7 @@ uint32_t CardDMA::get_card_capacity() const {
return capacity;
}
SDIO_Error_Type CardDMA::get_card_specific_data(Card_Info* info) {
auto CardDMA::get_card_specific_data(Card_Info* info) -> SDIO_Error_Type {
if (info == nullptr) return SDIO_Error_Type::INVALID_PARAMETER;
// Store basic card information
@ -735,28 +735,20 @@ SDIO_Error_Type CardDMA::get_card_specific_data(Card_Info* info) {
return SDIO_Error_Type::OK;
}
constexpr Block_Size CardDMA::get_data_block_size_index(uint16_t size) {
switch (size) {
case 1: return Block_Size::BYTES_1;
case 2: return Block_Size::BYTES_2;
case 4: return Block_Size::BYTES_4;
case 8: return Block_Size::BYTES_8;
case 16: return Block_Size::BYTES_16;
case 32: return Block_Size::BYTES_32;
case 64: return Block_Size::BYTES_64;
case 128: return Block_Size::BYTES_128;
case 256: return Block_Size::BYTES_256;
case 512: return Block_Size::BYTES_512;
case 1024: return Block_Size::BYTES_1024;
case 2048: return Block_Size::BYTES_2048;
case 4096: return Block_Size::BYTES_4096;
case 8192: return Block_Size::BYTES_8192;
case 16384: return Block_Size::BYTES_16384;
default: return Block_Size::BYTES_1;
}
constexpr auto CardDMA::get_data_block_size_index(uint16_t size) -> Block_Size {
if (size < 1 || size > 16384) return Block_Size::BYTES_1;
// Check if size is a power of two
if ((size & (size - 1)) != 0) return Block_Size::BYTES_1;
// Count trailing zeros to find the index
uint16_t index = 0;
while ((size >>= 1) != 0) ++index;
return static_cast<Block_Size>(index);
}
SDIO_Error_Type CardDMA::get_card_state(Card_State* card_state) {
auto CardDMA::get_card_state(Card_State* card_state) -> SDIO_Error_Type {
// CMD13 (SEND_STATUS)
if (send_command_and_check(Command_Index::CMD13, static_cast<uint32_t>(sdcard_rca_ << RCA_Shift),
Command_Response::RSP_SHORT, Wait_Type::WT_NONE, [this, cmd = Command_Index::CMD13]() {
@ -773,7 +765,7 @@ SDIO_Error_Type CardDMA::get_card_state(Card_State* card_state) {
if (response & All_R1_Error_Bits) {
for (const auto& entry : errorTableR1) {
if (response & entry.mask) {
if (TEST(response, entry.bit_position)) {
return entry.errorType;
}
}
@ -783,23 +775,28 @@ SDIO_Error_Type CardDMA::get_card_state(Card_State* card_state) {
return SDIO_Error_Type::OK;
}
SDIO_Error_Type CardDMA::get_command_sent_result() {
volatile uint32_t timeout = 0x00FFFFFFU;
auto CardDMA::get_command_sent_result() -> SDIO_Error_Type {
constexpr uint32_t MAX_TIMEOUT = 0x00FFFFFFU;
uint32_t timeout = MAX_TIMEOUT;
while ((sdio_.get_flag(Status_Flags::FLAG_CMDSEND) == false) && (timeout != 0U)) {
timeout = timeout - 1U;
// Wait for command sent flag or timeout
while (!sdio_.get_flag(Status_Flags::FLAG_CMDSEND) && timeout) {
--timeout;
}
if (timeout == 0U) return SDIO_Error_Type::RESPONSE_TIMEOUT;
sdio_.clear_multiple_interrupt_flags(clear_command_flags);
// Check if timeout occurred
if (timeout == 0U) {
return SDIO_Error_Type::RESPONSE_TIMEOUT;
}
// Clear command flags and return success
sdio_.clear_multiple_interrupt_flags(clear_command_flags);
return SDIO_Error_Type::OK;
}
SDIO_Error_Type CardDMA::check_sdio_status(Command_Index index, bool check_index, bool ignore_crc) {
auto CardDMA::check_sdio_status(Command_Index index, bool check_index, bool ignore_crc) -> SDIO_Error_Type {
// Wait until one of the relevant flags is set
bool flag_set = sdio_.wait_cmd_flags();
if (!flag_set) {
if (!sdio_.wait_cmd_flags()) {
return SDIO_Error_Type::RESPONSE_TIMEOUT;
}
@ -810,56 +807,55 @@ SDIO_Error_Type CardDMA::check_sdio_status(Command_Index index, bool check_index
// If cmd was received, check the index
// Responses that dont do an index check will send an invalid cmd index
if (check_index && (index != Command_Index::INVALID)) {
uint8_t idx = sdio_.get_command_index();
if (idx != static_cast<uint8_t>(index)) {
uint8_t received_idx = sdio_.get_command_index();
if (received_idx != static_cast<uint8_t>(index)) {
sdio_.clear_multiple_interrupt_flags(clear_command_flags);
return SDIO_Error_Type::ILLEGAL_COMMAND;
}
}
// Clear all flags before returning
// Command received successfully
sdio_.clear_multiple_interrupt_flags(clear_command_flags);
return SDIO_Error_Type::OK;
}
// Timeout check
// Check for timeout
if (sdio_.get_flag(Status_Flags::FLAG_CMDTMOUT)) {
sdio_.clear_flag(Clear_Flags::FLAG_CMDTMOUTC);
return SDIO_Error_Type::RESPONSE_TIMEOUT;
}
// CRC check
if (!ignore_crc) {
if (sdio_.get_flag(Status_Flags::FLAG_CCRCERR)) {
sdio_.clear_flag(Clear_Flags::FLAG_CCRCERRC);
return SDIO_Error_Type::COMMAND_CRC_ERROR;
}
// Check for CRC error if not ignored
if (!ignore_crc && sdio_.get_flag(Status_Flags::FLAG_CCRCERR)) {
sdio_.clear_flag(Clear_Flags::FLAG_CCRCERRC);
return SDIO_Error_Type::COMMAND_CRC_ERROR;
}
// Responses that dont do an index check will send an invalid cmd index
// Final index check (redundant with the first check, but keeping for safety)
// This code path should rarely be taken due to the earlier checks
if (check_index && (index != Command_Index::INVALID)) {
uint8_t idx = sdio_.get_command_index();
if (idx != static_cast<uint8_t>(index)) {
uint8_t received_idx = sdio_.get_command_index();
if (received_idx != static_cast<uint8_t>(index)) {
sdio_.clear_multiple_interrupt_flags(clear_command_flags);
return SDIO_Error_Type::ILLEGAL_COMMAND;
}
}
// Clear all flags before returning
// Clear all flags and return success
sdio_.clear_multiple_interrupt_flags(clear_command_flags);
return SDIO_Error_Type::OK;
}
SDIO_Error_Type CardDMA::get_r1_result(Command_Index index) {
auto CardDMA::get_r1_result(Command_Index index) -> SDIO_Error_Type {
SDIO_Error_Type result = check_sdio_status(index, true, false);
if (result != SDIO_Error_Type::OK) {
return result;
}
if (result != SDIO_Error_Type::OK) return result;
// Get the R1 response and check for errors
uint32_t response = sdio_.get_response(Response_Type::RESPONSE0);
if (response & All_R1_Error_Bits) {
for (const auto& entry : errorTableR1) {
if (response & entry.mask) {
if (TEST(response, entry.bit_position)) {
return entry.errorType;
}
}
@ -869,7 +865,7 @@ SDIO_Error_Type CardDMA::get_r1_result(Command_Index index) {
return SDIO_Error_Type::OK;
}
SDIO_Error_Type CardDMA::get_r6_result(Command_Index index, uint16_t* rca) {
auto CardDMA::get_r6_result(Command_Index index, uint16_t* rca) -> SDIO_Error_Type {
SDIO_Error_Type result = check_sdio_status(index, true, false);
if (result != SDIO_Error_Type::OK) return result;
@ -877,7 +873,7 @@ SDIO_Error_Type CardDMA::get_r6_result(Command_Index index, uint16_t* rca) {
if (response & R6_Error_Bits) {
for (const auto& entry : errorTableR6) {
if (response & entry.mask) {
if (TEST(response, entry.bit_position)) {
return entry.errorType;
}
}
@ -888,18 +884,13 @@ SDIO_Error_Type CardDMA::get_r6_result(Command_Index index, uint16_t* rca) {
return SDIO_Error_Type::OK;
}
SDIO_Error_Type CardDMA::get_r7_result() {
return check_sdio_status(Command_Index::INVALID, false, false);
}
SDIO_Error_Type CardDMA::get_scr(uint16_t rca, uint32_t* scr) {
auto CardDMA::get_scr(uint16_t rca, uint32_t* scr) -> SDIO_Error_Type {
uint32_t temp_scr[2] = {0U, 0U};
uint32_t index_scr = 0U;
uint32_t* src_data = scr;
// CMD16 (SET_BLOCKLEN)
if (send_command_and_check(Command_Index::CMD16, 8U, Command_Response::RSP_SHORT,
Wait_Type::WT_NONE, [this, cmd = Command_Index::CMD16]() {
Wait_Type::WT_NONE, [this, cmd = Command_Index::CMD16]() {
return get_r1_result(cmd);
}) != SDIO_Error_Type::OK) {
return SDIO_Error_Type::CMD16_FAILED;
@ -907,7 +898,7 @@ SDIO_Error_Type CardDMA::get_scr(uint16_t rca, uint32_t* scr) {
// CMD55 (APP_CMD)
if (send_command_and_check(Command_Index::CMD55, static_cast<uint32_t>(sdcard_rca_ << RCA_Shift),
Command_Response::RSP_SHORT, Wait_Type::WT_NONE, [this, cmd = Command_Index::CMD55]() {
Command_Response::RSP_SHORT, Wait_Type::WT_NONE, [this, cmd = Command_Index::CMD55]() {
return get_r1_result(cmd);
}) != SDIO_Error_Type::OK) {
return SDIO_Error_Type::CMD55_FAILED;
@ -919,7 +910,7 @@ SDIO_Error_Type CardDMA::get_scr(uint16_t rca, uint32_t* scr) {
// ACMD51 (SEND_SCR)
if (send_command_and_check(Command_Index::ACMD51, 0U, Command_Response::RSP_SHORT,
Wait_Type::WT_NONE, [this, cmd = Command_Index::ACMD51]() {
Wait_Type::WT_NONE, [this, cmd = Command_Index::ACMD51]() {
return get_r1_result(cmd);
}) != SDIO_Error_Type::OK) {
return SDIO_Error_Type::ACMD51_FAILED;
@ -928,36 +919,41 @@ SDIO_Error_Type CardDMA::get_scr(uint16_t rca, uint32_t* scr) {
// Store SCR
while (!sdio_.check_scr_flags()) {
if (sdio_.get_flag(Status_Flags::FLAG_RXDTVAL)) {
*(temp_scr + index_scr) = sdio_.read_fifo_word();
++index_scr;
temp_scr[index_scr++] = sdio_.read_fifo_word();
}
}
// Check for errors
if (sdio_.get_flag(Status_Flags::FLAG_DTTMOUT)) {
sdio_.clear_flag(Clear_Flags::FLAG_DTTMOUTC);
return SDIO_Error_Type::DATA_TIMEOUT;
} else if (sdio_.get_flag(Status_Flags::FLAG_DTCRCERR)) {
}
else if (sdio_.get_flag(Status_Flags::FLAG_DTCRCERR)) {
sdio_.clear_flag(Clear_Flags::FLAG_DTCRCERRC);
return SDIO_Error_Type::DATA_CRC_ERROR;
} else if (sdio_.get_flag(Status_Flags::FLAG_RXORE)) {
}
else if (sdio_.get_flag(Status_Flags::FLAG_RXORE)) {
sdio_.clear_flag(Clear_Flags::FLAG_RXOREC);
return SDIO_Error_Type::RX_FIFO_OVERRUN;
}
sdio_.clear_multiple_interrupt_flags(clear_data_flags);
constexpr uint32_t Zero_Seven = (0xFFU << 0U);
constexpr uint32_t Eight_Fifteen = (0xFFU << 8U);
constexpr uint32_t Sixteen_Twentythree = (0xFFU << 16U);
constexpr uint32_t TwentyFour_Thirtyone = (0xFFU << 24U);
constexpr uint32_t BYTE0_MASK = 0xFFU;
constexpr uint32_t BYTE1_MASK = 0xFF00U;
constexpr uint32_t BYTE2_MASK = 0xFF0000U;
constexpr uint32_t BYTE3_MASK = 0xFF000000U;
// adjust SCR value
*src_data = ((temp_scr[1] & Zero_Seven) << 24U) | ((temp_scr[1] & Eight_Fifteen) << 8U) |
((temp_scr[1] & Sixteen_Twentythree) >> 8U) | ((temp_scr[1] & TwentyFour_Thirtyone) >> 24U);
// Byte-swap the SCR values (convert from big-endian to little-endian)
scr[0] = ((temp_scr[1] & BYTE0_MASK) << 24) |
((temp_scr[1] & BYTE1_MASK) << 8) |
((temp_scr[1] & BYTE2_MASK) >> 8) |
((temp_scr[1] & BYTE3_MASK) >> 24);
src_data = src_data + 1U;
*src_data = ((temp_scr[0] & Zero_Seven) << 24U) | ((temp_scr[0] & Eight_Fifteen) << 8U) |
((temp_scr[0] & Sixteen_Twentythree) >> 8U) | ((temp_scr[0] & TwentyFour_Thirtyone) >> 24U);
scr[1] = ((temp_scr[0] & BYTE0_MASK) << 24) |
((temp_scr[0] & BYTE1_MASK) << 8) |
((temp_scr[0] & BYTE2_MASK) >> 8) |
((temp_scr[0] & BYTE3_MASK) >> 24);
return SDIO_Error_Type::OK;
}
@ -965,23 +961,22 @@ SDIO_Error_Type CardDMA::get_scr(uint16_t rca, uint32_t* scr) {
// DMA for rx/tx is always DMA1 channel 3
void CardDMA::set_dma_parameters(uint8_t* buf, uint32_t count, bool is_write) {
constexpr uint32_t flag_bits = (1U << static_cast<uint32_t>(dma::INTF_Bits::GIF3));
dma_.clear_flags(flag_bits);
// Disable and reset DMA
dma_.set_channel_enable(false);
dma_.clear_channel();
dma_.init({
count,
static_cast<uint32_t>(reinterpret_cast<uintptr_t>(buf)),
static_cast<uint32_t>(reinterpret_cast<uintptr_t>(sdio_.reg_address(SDIO_Regs::FIFO))),
dma::Bit_Width::WIDTH_32BIT,
dma::Bit_Width::WIDTH_32BIT,
dma::Increase_Mode::INCREASE_DISABLE,
dma::Increase_Mode::INCREASE_ENABLE,
dma::Channel_Priority::MEDIUM_PRIORITY,
is_write ? dma::Transfer_Direction::M2P : dma::Transfer_Direction::P2M
dma_.init(dma::DMA_Config{
.count = count,
.memory_address = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(buf)),
.peripheral_address = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(sdio_.reg_address(SDIO_Regs::FIFO))),
.peripheral_bit_width = dma::Bit_Width::WIDTH_32BIT,
.memory_bit_width = dma::Bit_Width::WIDTH_32BIT,
.peripheral_increase = dma::Increase_Mode::INCREASE_DISABLE,
.memory_increase = dma::Increase_Mode::INCREASE_ENABLE,
.channel_priority = dma::Channel_Priority::MEDIUM_PRIORITY,
.direction = is_write ? dma::Transfer_Direction::M2P : dma::Transfer_Direction::P2M
});
dma_.set_memory_to_memory_enable(false);
@ -995,24 +990,29 @@ void CardDMA::set_dma_parameters(uint8_t* buf, uint32_t count, bool is_write) {
dma_.set_channel_enable(true);
}
SDIO_Error_Type CardDMA::wait_for_card_ready() {
volatile uint32_t timeout = 0x00FFFFFFU;
auto CardDMA::wait_for_card_ready() -> SDIO_Error_Type {
constexpr uint32_t MAX_TIMEOUT = 0x00FFFFFFU;
uint32_t timeout = MAX_TIMEOUT;
uint32_t response = sdio_.get_response(Response_Type::RESPONSE0);
while (((response & static_cast<uint32_t>(R1_Status::READY_FOR_DATA)) == 0U) && (timeout != 0U)) {
// Continue to send CMD13 to poll the state of card until buffer empty or timeout
timeout = timeout - 1U;
// Poll until card is ready for data or timeout occurs
while (((response & static_cast<uint32_t>(R1_Status::READY_FOR_DATA)) == 0U) && timeout) {
--timeout;
// CMD13 (SEND_STATUS)
if (send_command_and_check(Command_Index::CMD13, static_cast<uint32_t>(sdcard_rca_ << RCA_Shift),
Command_Response::RSP_SHORT, Wait_Type::WT_NONE, [this, cmd = Command_Index::CMD13]() {
Command_Response::RSP_SHORT, Wait_Type::WT_NONE, [this, cmd = Command_Index::CMD13]() {
return get_r1_result(cmd);
}) != SDIO_Error_Type::OK) {
return SDIO_Error_Type::CMD13_FAILED;
}
// Get updated response
response = sdio_.get_response(Response_Type::RESPONSE0);
}
return (timeout == 0U) ? SDIO_Error_Type::ERROR : SDIO_Error_Type::OK;
// Return error if timeout occurred, otherwise success
return timeout ? SDIO_Error_Type::OK : SDIO_Error_Type::ERROR;
}
} // namespace sdio

View file

@ -26,76 +26,82 @@ class DMA;
class CardDMA {
public:
static CardDMA& get_instance();
static auto get_instance() -> CardDMA&;
SDIO_Error_Type init();
SDIO_Error_Type card_init();
SDIO_Error_Type begin_startup_procedure();
// Initialization
auto init() -> SDIO_Error_Type;
auto card_init() -> SDIO_Error_Type;
// Startup and shutdown procedures
auto begin_startup_procedure() -> SDIO_Error_Type;
void begin_shutdown_procedure();
// Configuration
SDIO_Error_Type set_hardware_bus_width(Bus_Width width);
// Main read/write functions for single and multiblock transfers
SDIO_Error_Type read(uint8_t* buf, uint32_t address, uint32_t count);
SDIO_Error_Type write(uint8_t* buf, uint32_t address, uint32_t count);
// DMA transfers
// Other card functions
SDIO_Error_Type erase(uint32_t address_start, uint32_t address_end);
// Interrupt handler
void handle_interrupts();
auto set_hardware_bus_width(Bus_Width width) -> SDIO_Error_Type;
auto send_bus_width_command(uint32_t width_value) -> SDIO_Error_Type;
// Main read/write/erase functions
auto read(uint8_t* buf, uint32_t address, uint32_t count) -> SDIO_Error_Type;
auto write(uint8_t* buf, uint32_t address, uint32_t count) -> SDIO_Error_Type;
auto erase(uint32_t address_start, uint32_t address_end) -> SDIO_Error_Type;
// Card select
SDIO_Error_Type select_deselect();
auto select_deselect() -> SDIO_Error_Type;
SDIO_Error_Type get_card_interface_status(uint32_t* status);
SDIO_Error_Type get_sdcard_status(uint32_t* status);
// Status and state
auto get_card_interface_status(uint32_t* status) -> SDIO_Error_Type;
auto get_sdcard_status(uint32_t* status) -> SDIO_Error_Type;
auto get_transfer_state() -> Transfer_State;
auto get_card_state(Card_State* card_state) -> SDIO_Error_Type;
auto check_sdio_status(Command_Index index = Command_Index::INVALID, bool check_index = false, bool ignore_crc = false) -> SDIO_Error_Type;
void check_dma_complete();
SDIO_Error_Type stop_transfer();
Transfer_State get_transfer_state();
uint32_t get_card_capacity() const;
SDIO_Error_Type send_bus_width_command(uint32_t width_value);
SDIO_Error_Type get_card_specific_data(Card_Info* info);
constexpr Block_Size get_data_block_size_index(uint16_t size);
SDIO_Error_Type get_card_state(Card_State* card_state);
SDIO_Error_Type check_sdio_status(Command_Index index = Command_Index::INVALID, bool check_index = false, bool ignore_crc = false);
// DMA configuration
// DMA
void set_dma_parameters(uint8_t* buf, uint32_t count, bool is_write);
void check_dma_complete();
// Stop transfer
auto stop_transfer() -> SDIO_Error_Type;
// Card information
auto get_card_specific_data(Card_Info* info) -> SDIO_Error_Type;
constexpr auto get_data_block_size_index(uint16_t size) -> Block_Size;
[[nodiscard]] auto get_card_capacity() const -> uint32_t;
// SDIO configuration
void sdio_configure(const SDIO_Config config) { sdio_.init(config); }
// Varaible stored parameters
SDIO_Error_Type get_scr(uint16_t rca, uint32_t* scr);
SDIO_Error_Type store_cid();
SDIO_Error_Type store_csd();
// Interrupt handler
void handle_interrupts();
// Accessor methods
SDIO_Config& get_config() { return config_; }
dma::DMA& get_dma_instance() { return dma_; }
// Varaible stored parameters
auto get_scr(uint16_t rca, uint32_t* scr) -> SDIO_Error_Type;
auto store_cid() -> SDIO_Error_Type;
auto store_csd() -> SDIO_Error_Type;
// Inlined accessor methods
auto get_config() -> SDIO_Config& { return config_; }
auto get_dma_instance() -> dma::DMA& { return dma_; }
void set_data_end_interrupt() { sdio_.set_interrupt_enable(Interrupt_Type::DTENDIE, true); }
void set_sdio_dma_enable(bool enable) { sdio_.set_dma_enable(enable); }
bool get_is_sdio_rx() { return is_rx_; }
auto get_is_sdio_rx() -> bool { return is_rx_; }
void clear_sdio_data_flags() { sdio_.clear_multiple_interrupt_flags(clear_data_flags); }
void clear_sdio_cmd_flags() { sdio_.clear_multiple_interrupt_flags(clear_command_flags); }
void clear_sdio_common_flags() { sdio_.clear_multiple_interrupt_flags(clear_common_flags); }
Operational_State get_state() { return current_state_; }
auto get_state() -> Operational_State { return current_state_; }
void set_state(Operational_State state) { current_state_ = state; }
void set_transfer_end(bool value) { transfer_end_ = value; }
void set_transfer_error(SDIO_Error_Type error) { transfer_error_ = error; }
void set_transfer_end(bool end) { transfer_end_ = end; };
inline SDIO_Error_Type set_desired_clock(uint32_t desired_clock, bool wide_bus, bool low_power) {
sdio_.init({
desired_clock,
Clock_Edge::RISING_EDGE,
wide_bus ? Bus_Width::WIDTH_4BIT : Bus_Width::WIDTH_1BIT,
false,
low_power,
false
auto set_desired_clock(uint32_t desired_clock, bool wide_bus, bool low_power) -> SDIO_Error_Type {
sdio_.init(SDIO_Config{
.desired_clock = desired_clock,
.enable_bypass = false,
.enable_powersave = low_power,
.enable_hwclock = false,
.clock_edge = Clock_Edge::RISING_EDGE,
.width = wide_bus ? Bus_Width::WIDTH_4BIT : Bus_Width::WIDTH_1BIT
});
sync_domains();
desired_clock_ = desired_clock;
@ -107,19 +113,17 @@ private:
// Prevent copying or assigning
CardDMA(const CardDMA&) = delete;
CardDMA& operator=(const CardDMA&) = delete;
auto operator=(const CardDMA&) -> CardDMA& = delete;
// Helper function
SDIO_Error_Type wait_for_card_ready();
auto wait_for_card_ready() -> SDIO_Error_Type;
// Member variables
alignas(4) uint32_t sdcard_csd_[4];
alignas(4) uint32_t sdcard_cid_[4];
alignas(4) uint32_t sdcard_scr_[2];
uint32_t desired_clock_;
uint32_t stop_condition_;
uint32_t total_bytes_;
uint32_t count_;
SDIO& sdio_;
SDIO_Config& config_;
const dma::DMA_Base dmaBase_;
@ -129,66 +133,70 @@ private:
SDIO_Error_Type transfer_error_;
Interface_Version interface_version_;
Card_Type card_type_;
volatile bool transfer_end_;
volatile bool is_rx_;
volatile bool multiblock_;
volatile Operational_State current_state_;
Operational_State current_state_;
bool transfer_end_;
bool multiblock_;
bool is_rx_;
// Private helper methods
SDIO_Error_Type validate_voltage();
SDIO_Error_Type get_r1_result(Command_Index index);
//SDIO_Error_Type get_r2_r3_result();
SDIO_Error_Type get_r6_result(Command_Index index, uint16_t* rca);
SDIO_Error_Type get_r7_result();
//SDIO_Error_Type get_r1_error_type(uint32_t response);
SDIO_Error_Type get_command_sent_result();
auto validate_voltage() -> SDIO_Error_Type;
auto get_command_sent_result() -> SDIO_Error_Type;
auto get_r1_result(Command_Index index) -> SDIO_Error_Type;
auto get_r6_result(Command_Index index, uint16_t* rca) -> SDIO_Error_Type;
auto get_r7_result() -> SDIO_Error_Type { return check_sdio_status(Command_Index::INVALID, false, false); };
void sync_domains() { delayMicroseconds(8); }
inline void sync_domains() {
delayMicroseconds(8);
}
inline bool validate_transfer_params(uint32_t* buf, uint16_t size) {
auto validate_transfer_params(uint32_t* buf, uint16_t size) -> bool {
if (buf == nullptr) return false;
// Size must be > 0, <= 2048 and power of 2
if ((size == 0U) || (size > 2048U) || (size & (size - 1U))) {
return false;
}
return true;
return size > 0U && size <= 2048U && !(size & (size - 1U));
}
void process_sdsc_specific_csd(Card_Info* info, const uint8_t* csd_bytes) {
info->csd.device_size = (static_cast<uint32_t>(csd_bytes[6] & 0x03U) << 10U) |
(static_cast<uint32_t>(csd_bytes[7]) << 2U) |
(static_cast<uint32_t>((csd_bytes[8] & 0xC0U) >> 6U));
info->csd.device_size_multiplier = static_cast<uint8_t>((csd_bytes[9] & 0x03U) << 1U |
(csd_bytes[10] & 0x80U) >> 7U);
const uint32_t device_size = ((csd_bytes[6] & 0x3U) << 10) |
(csd_bytes[7] << 2) |
((csd_bytes[8] >> 6) & 0x3U);
info->block_size = static_cast<uint32_t>(1 << info->csd.read_block_length);
info->capacity = static_cast<uint32_t>((info->csd.device_size + 1U) *
(1U << (info->csd.device_size_multiplier + 2U)) *
info->block_size);
const uint8_t device_size_multiplier = ((csd_bytes[9] & 0x3U) << 1) |
((csd_bytes[10] >> 7) & 0x1U);
// Store calculated values
info->csd.device_size = device_size;
info->csd.device_size_multiplier = device_size_multiplier;
// Calculate block size and capacity
info->block_size = 1U << info->csd.read_block_length;
info->capacity = (device_size + 1U) *
(1U << (device_size_multiplier + 2U)) *
info->block_size;
}
void process_sdhc_specific_csd(Card_Info* info, const uint8_t* csd_bytes) {
info->csd.device_size = static_cast<uint32_t>((csd_bytes[7] & 0x3FU) << 16U) |
static_cast<uint32_t>((csd_bytes[8]) << 8U) |
static_cast<uint32_t>(csd_bytes[9]);
info->csd.device_size = static_cast<uint32_t>((csd_bytes[7] & 0x3FU) << 16) |
static_cast<uint32_t>((csd_bytes[8]) << 8) |
static_cast<uint32_t>(csd_bytes[9]);
// Set block size and calculate capacity
info->block_size = BLOCK_SIZE;
info->capacity = static_cast<uint32_t>((info->csd.device_size + 1U) *
BLOCK_SIZE * KILOBYTE);
BLOCK_SIZE * KILOBYTE);
}
void process_common_csd_tail(Card_Info* info, const uint8_t* csd_bytes) {
info->csd.sector_size = static_cast<uint8_t>(((csd_bytes[9] & 0x3FU) << 1U) |
(csd_bytes[10] & 0x80U) >> 7U);
info->csd.speed_factor = static_cast<uint8_t>((csd_bytes[11] & 0x1CU) >> 2U);
info->csd.write_block_length = static_cast<uint8_t>(((csd_bytes[11] & 0x03U) << 2U) |
((csd_bytes[12] & 0xC0U) >> 6U));
info->csd.checksum = static_cast<uint8_t>((csd_bytes[15] & 0xFEU) >> 1U);
// Calculate sector_size
info->csd.sector_size = static_cast<uint8_t>(((csd_bytes[9] & 0x3FU) << 1) |
(csd_bytes[10] & 0x80U) >> 7);
// Calculate speed_factor and write_block_length
info->csd.speed_factor = static_cast<uint8_t>((csd_bytes[11] & 0x1CU) >> 2);
info->csd.write_block_length = static_cast<uint8_t>(((csd_bytes[11] & 0x3U) << 2) |
((csd_bytes[12] & 0xC0U) >> 6));
// Calculate checksum
info->csd.checksum = static_cast<uint8_t>((csd_bytes[15] & 0xFEU) >> 1);
}
inline void disable_all_interrupts() {
void disable_all_interrupts() {
sdio_.set_interrupt_enable(Interrupt_Type::DTCRCERRIE, false);
sdio_.set_interrupt_enable(Interrupt_Type::DTTMOUTIE, false);
sdio_.set_interrupt_enable(Interrupt_Type::DTENDIE, false);
@ -200,8 +208,8 @@ private:
}
template <typename CheckFunc>
inline SDIO_Error_Type send_command_and_check(Command_Index command, uint32_t argument,
Command_Response response, Wait_Type type, CheckFunc check_result) {
auto send_command_and_check(Command_Index command, uint32_t argument,
Command_Response response, Wait_Type type, CheckFunc check_result) -> SDIO_Error_Type {
sdio_.set_command_state_machine(command, argument, response, type);
sync_domains();
sdio_.set_command_state_machine_enable(true);

View file

@ -47,7 +47,7 @@ inline constexpr uint8_t SDIO_READ_RETRIES = READ_RETRIES;
Card_State cardState = Card_State::READY;
bool SDIO_SetBusWidth(Bus_Width width) {
auto SDIO_SetBusWidth(Bus_Width width) -> bool {
return (CardDMA_I.set_hardware_bus_width(width) == SDIO_Error_Type::OK);
}

View file

@ -32,5 +32,5 @@
#define SDIO_CMD_PIN PD2
void sdio_mfl_init();
bool SDIO_SetBusWidth(sdio::Bus_Width width);
auto SDIO_SetBusWidth(sdio::Bus_Width width) -> bool;
void DMA1_IRQHandler(dma::DMA_Channel channel);

View file

@ -89,7 +89,7 @@ static inline constexpr struct {timer::TIMER_Base base; uint8_t timer_number;} b
};
// Converts a timer base to an integer timer index.
constexpr int timer_base_to_index(timer::TIMER_Base base) {
constexpr auto timer_base_to_index(timer::TIMER_Base base) -> int {
for (const auto& timer : base_to_index) {
if (timer.base == base) {
return static_cast<int>(timer.timer_number);
@ -131,7 +131,7 @@ FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_number)
FORCE_INLINE static void HAL_timer_set_compare(const uint8_t timer_number, const hal_timer_t value) {
if (!HAL_timer_initialized(timer_number)) return;
const uint32_t new_value = static_cast<uint32_t>(value + 1U);
const auto new_value = static_cast<uint32_t>(value + 1U);
GeneralTimer& timer = (timer_number == MF_TIMER_STEP) ? Step_Timer : Temp_Timer;
if (timer_number == MF_TIMER_STEP || timer_number == MF_TIMER_TEMP) {

View file

@ -10,7 +10,7 @@
####################################
[gd32_base]
platform = https://github.com/bmourit/platform-mfl/archive/refs/tags/V1.0.3.zip
platform = https://github.com/bmourit/platform-mfl/archive/refs/tags/V1.0.4.zip
board_build.core = gd32
build_src_filter = ${common.default_src_filter} +<src/HAL/GD32_MFL> +<src/HAL/shared/backtrace>
build_unflags = -std=gnu++11 -std=gnu++14 -std=gnu++17
@ -28,7 +28,7 @@ extra_scripts = ${common.extra_scripts}
#
[env:GD32F303RE_creality_mfl]
extends = gd32_base
board = mfl_creality_422
board = mfl_creality_v4
board_build.offset = 0x7000
board_upload.offset_address = 0x08007000
board_build.rename = firmware-{time}.bin