Skip to content

Commit

Permalink
Sync with local development
Browse files Browse the repository at this point in the history
  • Loading branch information
vanvught committed May 22, 2024
1 parent 0c10ac9 commit bae66de
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 37 deletions.
1 change: 0 additions & 1 deletion lib-dmx/src/gd32/dmx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

#pragma GCC push_options
#pragma GCC optimize ("O3")
#pragma GCC optimize ("-funroll-loops")
#pragma GCC optimize ("-fprefetch-loop-arrays")

#include <cstdint>
Expand Down
3 changes: 3 additions & 0 deletions lib-gd32/Makefile.GD32
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ ifneq ($(MAKE_FLAGS),)
endif
endif
EXTRA_SRCDIR+=src/f
ifeq ($(findstring CONFIG_ENET_ENABLE_PTP,$(MAKE_FLAGS)), CONFIG_ENET_ENABLE_PTP)
EXTRA_SRCDIR+=src/f/ptp
endif
endif

ifeq ($(findstring gd32h7xx,$(FAMILY)), gd32h7xx)
Expand Down
36 changes: 35 additions & 1 deletion lib-gd32/include/gd32_ptp.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,55 @@ static constexpr uint8_t ADJ_FREQ_BASE_INCREMENT = static_cast<uint8_t>((PTP_TI
static constexpr uint32_t ADJ_FREQ_BASE_ADDEND = (static_cast<uint64_t>(1ULL << 63) / MCU_CLOCK_FREQ) / ADJ_FREQ_BASE_INCREMENT;
static constexpr int32_t ADJ_FREQ_MAX = 5120000;

struct ptptime {
struct time_t {
int32_t tv_sec;
int32_t tv_nsec;
};

struct ptptime {
uint32_t tv_sec;
uint32_t tv_nsec;
};
} // namespace ptp

inline uint32_t ptp_nanosecond_2_subsecond(const uint32_t nanosecond) {
uint64_t val = nanosecond * 0x80000000Ull;
val /= 1000000000U;
return static_cast<uint32_t>(val);
}

inline uint32_t ptp_subsecond_2_nanosecond(const uint32_t subsecond) {
uint64_t val = subsecond * 1000000000Ull;
val >>= 31U;
return (uint32_t) val;
}

inline void normalize_time(ptp::time_t *r) {
r->tv_sec += r->tv_nsec / 1000000000;
r->tv_nsec -= r->tv_nsec / 1000000000 * 1000000000;

if (r->tv_sec > 0 && r->tv_nsec < 0) {
r->tv_sec -= 1;
r->tv_nsec += 1000000000;
} else if (r->tv_sec < 0 && r->tv_nsec > 0) {
r->tv_sec += 1;
r->tv_nsec -= 1000000000;
}
}

inline void sub_time(struct ptp::time_t *r, const struct ptp::time_t *x, const struct ptp::time_t *y) {
r->tv_sec = x->tv_sec - y->tv_sec;
r->tv_nsec = x->tv_nsec - y->tv_nsec;

normalize_time(r);
}

} // namespace gd32

void gd32_ptp_start();
void gd32_ptp_get_time(gd32::ptp::ptptime *ptp_time);
void gd32_ptp_set_time(const gd32::ptp::ptptime *ptp_time);
void gd32_ptp_update_time(const gd32::ptp::time_t *ptp_time);
bool gd32_adj_frequency(const int32_t adjust_pbb);

#endif /* GD32_PTP_H_ */
65 changes: 57 additions & 8 deletions lib-gd32/src/f/ptp/gd32_ptp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <cstdint>
#include <cstring>
#include <time.h>
#include <sys/time.h>

#include "gd32_ptp.h"
#include "gd32.h"
Expand All @@ -39,18 +40,39 @@ extern "C" {
extern void console_error(const char *);
}

static void ptp_start(const uint32_t init_sec, const uint32_t init_subsec, const uint32_t carry_cfg, const uint32_t accuracy_cfg) {
static FlagStatus enet_ptpflagstatus_get(const uint32_t flag) {
FlagStatus bitstatus = RESET;

if ((ENET_PTP_TSCTL & flag) != RESET) {
bitstatus = SET;
}

return bitstatus;
}

static void ptp_start(const uint32_t init_sec, const uint32_t init_subsec, [[maybe_unused]] const uint32_t carry_cfg, const uint32_t accuracy_cfg) {
DEBUG_ENTRY

enet_interrupt_disable(ENET_MAC_INT_TMSTIM);
#if !defined(GD32F4XX)
enet_ptp_feature_enable(ENET_RXTX_TIMESTAMP);
#else
enet_ptp_feature_enable(ENET_ALL_RX_TIMESTAMP | ENET_RXTX_TIMESTAMP);
#endif
enet_ptp_subsecond_increment_config(accuracy_cfg);

enet_ptp_timestamp_addend_config(carry_cfg);

enet_ptp_timestamp_function_config(ENET_PTP_ADDEND_UPDATE);
while (SET == enet_ptp_flag_get((uint32_t) ENET_PTP_ADDEND_UPDATE)) {}
while(enet_ptpflagstatus_get(ENET_PTP_ADDEND_UPDATE) == SET);

enet_ptp_timestamp_function_config(ENET_PTP_FINEMODE);

enet_ptp_timestamp_update_config(ENET_PTP_ADD_TO_TIME, init_sec, init_subsec);
enet_ptp_timestamp_function_config(ENET_PTP_SYSTIME_INIT);
while(enet_ptpflagstatus_get(ENET_PTP_SYSTIME_INIT) == SET);

DEBUG_EXIT
}

void gd32_ptp_start() {
Expand All @@ -67,6 +89,13 @@ void gd32_ptp_start() {

ptp_start(mktime(&tmbuf), 0, gd32::ptp::ADJ_FREQ_BASE_ADDEND, gd32::ptp::ADJ_FREQ_BASE_INCREMENT);

#ifndef NDEBUG
struct timeval tv;
gettimeofday(&tv, nullptr);
auto *tm = localtime(&tv.tv_sec);

DEBUG_PRINTF("%.2d-%.2d-%.4d %.2d:%.2d:%.2d.%.6d", tm->tm_mday, tm->tm_mon + 1, tm->tm_year + 1900, tm->tm_hour, tm->tm_min, tm->tm_sec, static_cast<int>(tv.tv_usec));
#endif
DEBUG_EXIT
}

Expand All @@ -76,28 +105,48 @@ void gd32_ptp_get_time(gd32::ptp::ptptime *ptp_time) {
enet_ptp_system_time_get(&systime);

ptp_time->tv_sec = systime.second;
#if !defined (GD32F4XX)
ptp_time->tv_nsec = systime.nanosecond;
#else
ptp_time->tv_nsec = gd32::ptp_subsecond_2_nanosecond(systime.subsecond);
#endif
}

void gd32_ptp_set_time(const gd32::ptp::ptptime *ptp_time) {
const auto nSign = ENET_PTP_ADD_TO_TIME;
const auto nSecond = ptp_time->tv_sec;
const auto nNanoSecond = ptp_time->tv_nsec;
const auto nSubSecond = gd32::ptp_nanosecond_2_subsecond(nNanoSecond);

enet_ptp_timestamp_update_config(nSign, nSecond, nSubSecond);
enet_ptp_timestamp_function_config(ENET_PTP_SYSTIME_INIT);
while(enet_ptpflagstatus_get(ENET_PTP_SYSTIME_INIT) == SET);
}

void gd32_ptp_update_time(const gd32::ptp::time_t *pTime) {
uint32_t nSign;
uint32_t nSecond;
uint32_t nNanoSecond;

if (ptp_time->tv_sec < 0 || (ptp_time->tv_sec == 0 && ptp_time->tv_nsec < 0)) {
if (pTime->tv_sec < 0 || (pTime->tv_sec == 0 && pTime->tv_nsec < 0)) {
nSign = ENET_PTP_SUBSTRACT_FROM_TIME;
nSecond = -ptp_time->tv_sec;
nNanoSecond = -ptp_time->tv_nsec;
nSecond = -pTime->tv_sec;
nNanoSecond = -pTime->tv_nsec;
} else {
nSign = ENET_PTP_ADD_TO_TIME;
nSecond = ptp_time->tv_sec;
nNanoSecond = ptp_time->tv_nsec;
nSecond = pTime->tv_sec;
nNanoSecond = pTime->tv_nsec;
}

const auto nSubSecond = gd32::ptp_nanosecond_2_subsecond(nNanoSecond);
const auto nAddend = ENET_PTP_TSADDEND;

enet_ptp_timestamp_update_config(nSign, nSecond, nSubSecond);
enet_ptp_timestamp_function_config(ENET_PTP_SYSTIME_INIT);
enet_ptp_timestamp_function_config(ENET_PTP_SYSTIME_UPDATE);
while(enet_ptpflagstatus_get(ENET_PTP_SYSTIME_UPDATE) == SET);

enet_ptp_timestamp_addend_config(nAddend);
enet_ptp_timestamp_function_config(ENET_PTP_ADDEND_UPDATE);
}

bool gd32_adj_frequency(const int32_t adjust_ppb) {
Expand Down
33 changes: 21 additions & 12 deletions lib-hal/include/gd32/hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
#include "gd32.h"
#include "gd32_adc.h"

#if !defined (CONFIG_LEDBLINK_USE_PANELLED) && (defined (GD32F4XX) || defined(GD32H7XX))
# define HAL_HAVE_PORT_BIT_TOGGLE
#endif

#if defined (ENABLE_USB_HOST) && defined (CONFIG_USB_HOST_MSC)
extern "C" {
#include "usbh_core.h"
Expand Down Expand Up @@ -199,24 +203,27 @@ class Hardware {
# endif
#endif
if (__builtin_expect (m_nTicksPerSecond != 0, 1)) {
if (__builtin_expect (!(s_nSysTickMillis - m_nMillisPrevious < m_nTicksPerSecond), 1)) {
m_nMillisPrevious = s_nSysTickMillis;

m_nToggleLed ^= 0x1;
if (__builtin_expect (!(Hardware::Get()->Millis() - m_nMillisPrevious < m_nTicksPerSecond), 1)) {
m_nMillisPrevious = Hardware::Get()->Millis();
#if defined(HAL_HAVE_PORT_BIT_TOGGLE)
GPIO_TG(LED_BLINK_GPIO_PORT) = LED_BLINK_PIN;
#else
m_nToggleLed = -m_nToggleLed;

if (m_nToggleLed != 0) {
#if defined (CONFIG_LEDBLINK_USE_PANELLED)
if (m_nToggleLed > 0) {
# if defined (CONFIG_LEDBLINK_USE_PANELLED)
hal::panel_led_on(hal::panelled::ACTIVITY);
#else
# else
GPIO_BOP(LED_BLINK_GPIO_PORT) = LED_BLINK_PIN;
#endif
# endif
} else {
#if defined (CONFIG_LEDBLINK_USE_PANELLED)
# if defined (CONFIG_LEDBLINK_USE_PANELLED)
hal::panel_led_off(hal::panelled::ACTIVITY);
#else
# else
GPIO_BC(LED_BLINK_GPIO_PORT) = LED_BLINK_PIN;
#endif
# endif
}
#endif
}
}

Expand Down Expand Up @@ -280,7 +287,9 @@ class Hardware {
hardware::ledblink::Mode m_Mode { hardware::ledblink::Mode::UNKNOWN };
bool m_doLock { false };
uint32_t m_nTicksPerSecond { 1000 / 2 };
int32_t m_nToggleLed { 0 };
#if !defined(HAL_HAVE_PORT_BIT_TOGGLE)
int32_t m_nToggleLed { 1 };
#endif
uint32_t m_nMillisPrevious { 0 };

static Hardware *s_pThis;
Expand Down
20 changes: 11 additions & 9 deletions lib-hal/rtc/gd32/hwclockrtc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,18 @@ void HwClock::RtcProbe() {
return;
}

#if defined (GD32F4XX) || defined (GD32H7XX)
const auto ltime = time(nullptr);
const auto *tm = localtime(&ltime);
RtcSet(tm);
#else
rtc_lwoff_wait();
rtc_counter_set(time(nullptr));
rtc_lwoff_wait();
#endif
bkp_data_write(BKP_DATA_0, 0xA5A5);

struct tm RtcTime;

RtcTime.tm_hour = 0;
RtcTime.tm_min = 0;
RtcTime.tm_sec = 0;
RtcTime.tm_mday = _TIME_STAMP_DAY_;
RtcTime.tm_mon = _TIME_STAMP_MONTH_ - 1;
RtcTime.tm_year = _TIME_STAMP_YEAR_ - 1900;

RtcSet(&RtcTime);
} else {
DEBUG_PUTS("No need to configure RTC");
rtc_register_sync_wait();
Expand Down
19 changes: 13 additions & 6 deletions lib-hal/src/gd32/hardware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,17 @@ Hardware::Hardware() {
const struct timeval tv = { seconds, 0 };

settimeofday(&tv, nullptr);
#endif

# if !defined(DISABLE_RTC)
#if !defined(DISABLE_RTC)
m_HwClock.RtcProbe();
m_HwClock.Print();
# if !defined (CONFIG_ENET_ENABLE_PTP)
// Set the System Clock from the Hardware Clock
m_HwClock.HcToSys();
# endif
#endif

#if defined (DEBUG_I2C)
I2cDetect i2cdetect;
#endif

#if !defined(CONFIG_LEDBLINK_USE_PANELLED)
rcu_periph_clock_enable(LED_BLINK_GPIO_CLK);
# if defined (GPIO_INIT)
Expand Down Expand Up @@ -222,16 +221,24 @@ Hardware::Hardware() {

logic_analyzer::init();

#if defined (DEBUG_I2C)
I2cDetect i2cdetect;
#endif

DEBUG_EXIT
}

#include <cstdio>

bool Hardware::Reboot() {
puts("Rebooting ...");

#if !defined(DISABLE_RTC)
m_HwClock.SysToHc();
#endif

WatchdogStop();

RebootHandler();

SetMode(hardware::ledblink::Mode::OFF_OFF);
Expand Down
2 changes: 2 additions & 0 deletions lib-hal/src/json_get_directory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ uint32_t json_get_directory(char *pOutBuffer, const uint32_t nOutBufferSize) {
}
} while (dp != nullptr);

closedir(dirp);

if (pOutBuffer[nLength - 1] == ',') {
nLength--;
}
Expand Down

0 comments on commit bae66de

Please sign in to comment.