Skip to content

Commit

Permalink
Added support for BOARD_GD32F470Z_EVAL
Browse files Browse the repository at this point in the history
  • Loading branch information
vanvught committed Feb 22, 2024
1 parent 61c51c2 commit 2e081c2
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 7 deletions.
217 changes: 217 additions & 0 deletions lib-flashcode/src/gd32/h7xx/flashcode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
/**
* @file flashcode.cpp
*
*/
/* Copyright (C) 2024 by Arjan van Vught mailto:[email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include <cstdint>
#include <stdio.h>
#include <cstring>
#include <cassert>

#include "flashcode.h"

#include "gd32.h"

#include "debug.h"

namespace flashcode {
/* Backwards compatibility with SPI FLASH */
static constexpr auto FLASH_SECTOR_SIZE = 4096U;
/* The flash page size is 4KB for bank1 */
static constexpr auto BANK1_FLASH_PAGE = (4U * 1024U);

enum class State {
IDLE,
ERASE_BUSY,
ERASE_PROGAM,
WRITE_BUSY,
WRITE_PROGRAM,
ERROR
};

static State s_State = State::IDLE;
static uint32_t s_nPage;
static uint32_t s_nLength;
static uint32_t s_nAddress;
static uint32_t *s_pData;
} // namespace flashcode

using namespace flashcode;

uint32_t FlashCode::GetSize() const {
const auto FLASH_DENSITY = ((REG32(0x1FF0F7E0) >> 16) & 0xFFFF) * 1024U;
return FLASH_DENSITY;
}

uint32_t FlashCode::GetSectorSize() const {
return flashcode::FLASH_SECTOR_SIZE;
}

bool FlashCode::Read(uint32_t nOffset, uint32_t nLength, uint8_t *pBuffer, flashcode::result& nResult) {
DEBUG_ENTRY
DEBUG_PRINTF("offset=%p[%d], len=%u[%d], data=%p[%d]", nOffset, (((uint32_t)(nOffset) & 0x3) == 0), nLength, (((uint32_t)(nLength) & 0x3) == 0), pBuffer, (((uint32_t)(pBuffer) & 0x3) == 0));

const auto *pSrc = reinterpret_cast<uint32_t *>(nOffset + FLASH_BASE);
auto *pDst = reinterpret_cast<uint32_t *>(pBuffer);

while (nLength > 0) {
*pDst++ = *pSrc++;
nLength -= 4;
}

nResult = flashcode::result::OK;

DEBUG_EXIT
return true;
}

bool FlashCode::Erase(uint32_t nOffset, uint32_t nLength, flashcode::result& nResult) {
DEBUG_ENTRY
DEBUG_PRINTF("State=%d", static_cast<int>(s_State));

nResult = result::OK;

switch (s_State) {
case State::IDLE:
s_nPage = nOffset + FLASH_BASE;
s_nLength = nLength;
fmc_unlock();
s_State = State::ERASE_BUSY;
DEBUG_EXIT
return false;
break;
case State::ERASE_BUSY:
if (SET == fmc_flag_get(FMC_FLAG_BUSY)) {
DEBUG_EXIT
return false;
}

if (s_nLength == 0) {
s_State = State::IDLE;
fmc_lock();
DEBUG_EXIT
return true;
}

s_State = State::ERASE_PROGAM;
DEBUG_EXIT
return false;
break;
case State::ERASE_PROGAM:
if (s_nLength > 0) {
DEBUG_PRINTF("s_nPage=%p", s_nPage);

fmc_sector_erase(s_nPage);

s_nLength -= BANK1_FLASH_PAGE;
s_nPage += BANK1_FLASH_PAGE;
}

s_State = State::ERASE_BUSY;
DEBUG_EXIT
return false;
break;
default:
assert(0);
__builtin_unreachable();
break;
}

assert(0);
__builtin_unreachable();
return true;
}

bool FlashCode::Write(uint32_t nOffset, uint32_t nLength, const uint8_t *pBuffer, flashcode::result& nResult) {
if ((s_State == flashcode::State::WRITE_PROGRAM) || (s_State == flashcode::State::WRITE_BUSY)) {
} else {
DEBUG_ENTRY
}
nResult = result::OK;

switch (s_State) {
case flashcode::State::IDLE:
DEBUG_PUTS("State::IDLE");
flashcode::s_nAddress = nOffset + FLASH_BASE;
s_pData = const_cast<uint32_t *>(reinterpret_cast<const uint32_t *>(pBuffer));
s_nLength = nLength;
fmc_unlock();
s_State = State::WRITE_BUSY;
DEBUG_EXIT
return false;
break;
case flashcode::State::WRITE_BUSY:
if (SET == fmc_flag_get(FMC_FLAG_BUSY)) {
DEBUG_EXIT
return false;
}

if (s_nLength == 0) {
fmc_lock();
s_State = State::IDLE;

if( memcmp(reinterpret_cast<void *>(nOffset + FLASH_BASE), pBuffer, nLength) == 0) {
DEBUG_PUTS("memcmp OK");
} else {
DEBUG_PUTS("memcmp failed");
}

DEBUG_EXIT
return true;
}

s_State = flashcode::State::WRITE_PROGRAM;
return false;
break;
case flashcode::State::WRITE_PROGRAM:
if (s_nLength >= 4) {
if (FMC_READY == fmc_ready_wait(0xFF)) {
/* set the PG bit to start program */
FMC_CTL |= FMC_CTL_PG;
__ISB();
__DSB();
REG32(s_nAddress) = *s_pData;
__ISB();
__DSB();
/* reset the PG bit */
FMC_CTL &= ~FMC_CTL_PG;
s_pData++;
s_nAddress += 4;
s_nLength -= 4;
}
} else if (s_nLength > 0) {
DEBUG_PUTS("Error!");
}
s_State = flashcode::State::WRITE_BUSY;
return false;
break;
default:
assert(0);
__builtin_unreachable();
break;
}

assert(0);
__builtin_unreachable();
return true;
}
3 changes: 3 additions & 0 deletions lib-flashcodeinstall/include/flashcodeinstall.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
# elif defined (BOARD_GD32F470Z_EVAL)
# define OFFSET_UIMAGE 0x008000 // 32K
# define FIRMWARE_MAX_SIZE (171 * 1024) // 171K
# elif defined (BOARD_GD32H759I_EVAL)
# define OFFSET_UIMAGE 0x008000 // 32K
# define FIRMWARE_MAX_SIZE (171 * 1024) // 171K
# else
# error Board is not supported
# endif
Expand Down
6 changes: 2 additions & 4 deletions lib-hal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
Supported platforms:

- Board's: Orange Pi Zero / One (baremetal) {Allwinner SoC H2+/H3 ARM}
- MCU's: GD32F103R / GD32F107R / GD32F207R / GD32F407R {GigaDevice MCU ARM}
- MCU's: GD32F103R / GD32F107R / GD32F207R / GD32F407R / GD32F450V/ GD32F470Z / GD32H7XX {GigaDevice MCU ARM}
- OS's: Mac OS / Linux


[http://www.orangepi-dmx.org](http://www.orangepi-dmx.org)

[http://www.gd32-dmx.org](http://www.gd32-dmx.org)

[http://www.orangepi-dmx.org](http://www.orangepi-dmx.org)
15 changes: 13 additions & 2 deletions lib-hal/include/gd32/hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

#include "gd32.h"
#include "gd32_adc.h"
#include "gd32_micros.h"

#if defined (ENABLE_USB_HOST) && defined (CONFIG_USB_HOST_MSC)
extern "C" {
Expand Down Expand Up @@ -77,7 +76,19 @@ class Hardware {
}

uint32_t Micros() {
return micros();
static uint32_t nMicrosPrevious;
static uint32_t nResult;
const auto nMicros = DWT->CYCCNT / (MCU_CLOCK_FREQ / 1000000U);

if (nMicros > nMicrosPrevious) {
nResult += (nMicros - nMicrosPrevious);
} else {
nResult += ((UINT32_MAX / (MCU_CLOCK_FREQ / 1000000U)) - nMicrosPrevious + nMicros);
}

nMicrosPrevious = nMicros;

return nResult;
}

uint32_t GetUpTime() {
Expand Down
4 changes: 3 additions & 1 deletion lib-remoteconfig/include/tftp/tftpfileserver.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @file tftpfileserver.h
*
*/
/* Copyright (C) 2019-2023 by Arjan van Vught mailto:[email protected]
/* Copyright (C) 2019-2024 by Arjan van Vught mailto:[email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -51,6 +51,8 @@ namespace tftpfileserver {
static constexpr char FILE_NAME[] = "gd32f207.bin";
# elif defined (GD32F4XX)
static constexpr char FILE_NAME[] = "gd32f4xx.bin";
# elif defined (GD32H7XX)
static constexpr char FILE_NAME[] = "gd32h7xx.bin";
# else
# error MCU is not defined
# endif
Expand Down

0 comments on commit 2e081c2

Please sign in to comment.