From b609a7b4618bc541972ad9bd0d0af153eb7c8c0c Mon Sep 17 00:00:00 2001 From: Arjan van Vught Date: Sat, 7 Dec 2024 20:00:46 +0100 Subject: [PATCH] W.I.P. #287 --- lib-h3/device/mmc/mmc.c | 6 +- lib-h3/device/mmc/mmc_bsp.c | 5 +- .../{src/h3_timer.c => device/mmc/msdelay.c} | 28 ++------- lib-h3/include/h3_timer.h | 13 +--- .../{h3_timer_init.cpp => h3_timer_avs.cpp} | 6 +- lib-hal/src/h3/hardware_init.cpp | 4 +- lib-network/src/esp8266/h3/esp8266.cpp | 62 +++++++++---------- 7 files changed, 47 insertions(+), 77 deletions(-) rename lib-h3/{src/h3_timer.c => device/mmc/msdelay.c} (73%) mode change 100644 => 100755 rename lib-h3/src/{h3_timer_init.cpp => h3_timer_avs.cpp} (91%) diff --git a/lib-h3/device/mmc/mmc.c b/lib-h3/device/mmc/mmc.c index 12c2b0434..656f12156 100644 --- a/lib-h3/device/mmc/mmc.c +++ b/lib-h3/device/mmc/mmc.c @@ -5,7 +5,7 @@ * @file mmc.c * */ -/* Copyright (C) 2018-2020 by Arjan van Vught mailto:info@orangepi-dmx.nl +/* Copyright (C) 2018-2024 by Arjan van Vught mailto:info@orangepi-dmx.nl * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -57,10 +57,10 @@ #include "mmc_internal.h" -#include "h3_timer.h" - #include "debug.h" +extern void __msdelay(const uint32_t ms); + #define __be32_to_cpu(x) ((0x000000ff&((x)>>24)) | (0x0000ff00&((x)>>8)) | \ (0x00ff0000&((x)<< 8)) | (0xff000000&((x)<<24))) diff --git a/lib-h3/device/mmc/mmc_bsp.c b/lib-h3/device/mmc/mmc_bsp.c index 3ae51c912..1a8777424 100644 --- a/lib-h3/device/mmc/mmc_bsp.c +++ b/lib-h3/device/mmc/mmc_bsp.c @@ -4,7 +4,7 @@ * @file mmc_bsp.c * */ -/* Copyright (C) 2018-2020 by Arjan van Vught mailto:info@orangepi-dmx.nl +/* Copyright (C) 2018-2024 by Arjan van Vught mailto:info@orangepi-dmx.nl * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -57,10 +57,11 @@ #include "mmc_internal.h" #include "h3.h" -#include "h3_timer.h" #include "debug.h" +extern void __msdelay(const uint32_t ms); + // CCU register #define SDMMC_CLK_SCLK_GATING (1U << 31) ///< 1 = Clock is ON. SCLK = Clock source/Divider N/ Divider M diff --git a/lib-h3/src/h3_timer.c b/lib-h3/device/mmc/msdelay.c old mode 100644 new mode 100755 similarity index 73% rename from lib-h3/src/h3_timer.c rename to lib-h3/device/mmc/msdelay.c index feb443a66..88dd02c6d --- a/lib-h3/src/h3_timer.c +++ b/lib-h3/device/mmc/msdelay.c @@ -1,8 +1,8 @@ /** - * @file h3_timer.c + * @file msdelay.c * */ -/* Copyright (C) 2018 by Arjan van Vught mailto:info@orangepi-dmx.nl +/* Copyright (C) 2024 by Arjan van Vught mailto:info@orangepi-dmx.nl * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -23,30 +23,14 @@ * THE SOFTWARE. */ -#include - #include "h3.h" -void __msdelay(uint32_t ms) { - uint32_t t1, t2; - - t1 = H3_TIMER->AVS_CNT0; - t2 = t1 + ms; - do { - t1 = H3_TIMER->AVS_CNT0; - } while (t2 >= t1); - - return; -} - -void __usdelay(uint32_t us) { - uint32_t t1, t2; +void __msdelay(const uint32_t ms) { + const uint32_t t1 = H3_TIMER->AVS_CNT0; - t1 = H3_TIMER->AVS_CNT1; - t2 = t1 + us; do { - t1 = H3_TIMER->AVS_CNT1; - } while (t2 >= t1); + __DMB(); + } while ((H3_TIMER->AVS_CNT0 - t1) < ms); return; } diff --git a/lib-h3/include/h3_timer.h b/lib-h3/include/h3_timer.h index 4b36f6779..9bd034a06 100644 --- a/lib-h3/include/h3_timer.h +++ b/lib-h3/include/h3_timer.h @@ -2,7 +2,7 @@ * @file h3_timer.h * */ -/* Copyright (C) 2018-2020 by Arjan van Vught mailto:info@orangepi-dmx.nl +/* Copyright (C) 2018-2024 by Arjan van Vught mailto:info@orangepi-dmx.nl * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -50,15 +50,4 @@ #define TIMER_INTV_1SECOND (0xB71B00) ///< 12.000.000 ticks with TIMER_CTRL_CLK_PRES_2 and TIMER_CTRL_CLK_SRC_OSC24M -#ifdef __cplusplus -extern "C" { -#endif - -extern void __msdelay(uint32_t); -extern void __usdelay(uint32_t); - -#ifdef __cplusplus -} -#endif - #endif /* H3_TIMER_H_ */ diff --git a/lib-h3/src/h3_timer_init.cpp b/lib-h3/src/h3_timer_avs.cpp similarity index 91% rename from lib-h3/src/h3_timer_init.cpp rename to lib-h3/src/h3_timer_avs.cpp index cea3808fb..0e1880152 100755 --- a/lib-h3/src/h3_timer_init.cpp +++ b/lib-h3/src/h3_timer_avs.cpp @@ -1,8 +1,8 @@ /** - * @file h3_h3_timer_init.cpp + * @file h3_timer_avs.cpp * */ -/* Copyright (C) 2023 by Arjan van Vught mailto:info@orangepi-dmx.nl +/* Copyright (C) 2023-2024 by Arjan van Vught mailto:info@orangepi-dmx.nl * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -36,7 +36,7 @@ #define DIV_N_CNT0 0x2EE0 // 24MHz / 2 / 12000 = 1KHz, period 1ms #define DIV_N_CNT1 0xC // 24MHz / 2 / 12 = 1MHz, period 1us -void __attribute__((cold)) h3_timer_init() { +void __attribute__((cold)) h3_timer_avs_init() { H3_CCU->AVS_CLK_CFG |= SCLK_GATING; H3_TIMER->AVS_CTRL = AVS_CNT1_EN |AVS_CNT0_EN; diff --git a/lib-hal/src/h3/hardware_init.cpp b/lib-hal/src/h3/hardware_init.cpp index 097df578f..66d51e0e1 100644 --- a/lib-hal/src/h3/hardware_init.cpp +++ b/lib-hal/src/h3/hardware_init.cpp @@ -62,7 +62,7 @@ static uint32_t s_hardware_init_startup_seconds = 0; extern void emac_init(void); extern void sys_time_init(void); -extern void h3_timer_init(void); +extern void h3_timer_avs_init(void); extern void h3_hs_timer_init(void); extern void h3_usb_end(void); @@ -120,7 +120,7 @@ void __attribute__((cold)) hardware_init(void) { h3_watchdog_disable(); h3_usb_end(); - h3_timer_init(); + h3_timer_avs_init(); h3_hs_timer_init(); sys_time_init(); console_init(); diff --git a/lib-network/src/esp8266/h3/esp8266.cpp b/lib-network/src/esp8266/h3/esp8266.cpp index 83b62e293..d2a219f70 100644 --- a/lib-network/src/esp8266/h3/esp8266.cpp +++ b/lib-network/src/esp8266/h3/esp8266.cpp @@ -2,10 +2,10 @@ # error #endif /** - * @file esp8266.c + * @file esp8266.cpp * */ -/* Copyright (C) 2018-2023 by Arjan van Vught mailto:info@orangepi-dmx.nl +/* Copyright (C) 2018-2024 by Arjan van Vught mailto:info@orangepi-dmx.nl * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -31,11 +31,8 @@ #include -#include "arm/synchronize.h" - #include "h3.h" #include "h3_gpio.h" -#include "h3_hs_timer.h" #include "h3_board.h" /* @@ -69,8 +66,8 @@ typedef union pcast32 { uint8_t u8[4]; } esp8266_pcast32; -static void data_gpio_fsel_output(void) { - isb(); +static void data_gpio_fsel_output() { + __ISB(); uint32_t value = H3_PIO_PORTA->CFG0; value &= (uint32_t) ~(GPIO_SELECT_MASK << PA2_SELECT_CFG0_SHIFT); @@ -86,11 +83,11 @@ static void data_gpio_fsel_output(void) { value |= (GPIO_FSEL_OUTPUT << PA19_SELECT_CFG2_SHIFT); H3_PIO_PORTA->CFG2 = value; - dmb(); + __DMB(); } -static void data_gpio_fsel_input(void) { - isb(); +static void data_gpio_fsel_input() { + __ISB(); uint32_t value = H3_PIO_PORTA->CFG0; value &= (uint32_t) ~(GPIO_SELECT_MASK << PA2_SELECT_CFG0_SHIFT); @@ -106,10 +103,10 @@ static void data_gpio_fsel_input(void) { value |= (GPIO_FSEL_INPUT << PA19_SELECT_CFG2_SHIFT); H3_PIO_PORTA->CFG2 = value; - dmb(); + __DMB(); } -void __attribute__((cold)) esp8266_init(void) { +void __attribute__((cold)) esp8266_init() { h3_gpio_fsel(GPIO_EXT_13, GPIO_FSEL_INPUT); h3_gpio_fsel(GPIO_EXT_11, GPIO_FSEL_OUTPUT); @@ -140,7 +137,7 @@ void esp8266_write_4bits(const uint8_t data) { h3_gpio_clr(GPIO_EXT_11); // we acknowledge, and wait for zero while (H3_PIO_PORTA->DAT & (1 << CIN)); - dmb(); + __DMB(); } inline static void _write_byte(const uint8_t data) { @@ -180,14 +177,14 @@ inline static void _write_byte(const uint8_t data) { void esp8266_write_byte(const uint8_t byte) { data_gpio_fsel_output(); _write_byte(byte); - dmb(); + __DMB(); } void esp8266_write_halfword(const uint16_t half_word) { data_gpio_fsel_output(); _write_byte((uint8_t)(half_word & (uint16_t)0xFF)); _write_byte((uint8_t)((half_word >> 8) & (uint16_t)0xFF)); - dmb(); + __DMB(); } void esp8266_write_word(const uint32_t word) { @@ -202,7 +199,7 @@ void esp8266_write_word(const uint32_t word) { _write_byte(u32.u8[2]); _write_byte(u32.u8[3]); - dmb(); + __DMB(); } void esp8266_write_bytes(const uint8_t *data, const uint32_t len) { @@ -218,7 +215,7 @@ void esp8266_write_bytes(const uint8_t *data, const uint32_t len) { p++; } - dmb(); + __DMB(); } void esp8266_write_str(const char *data) { @@ -235,10 +232,10 @@ void esp8266_write_str(const char *data) { _write_byte(0); - dmb(); + __DMB(); } -inline static uint8_t _read_byte(void) { +inline static uint8_t _read_byte() { uint8_t data; h3_gpio_set(GPIO_EXT_11); @@ -275,14 +272,14 @@ inline static uint8_t _read_byte(void) { return data; } -uint8_t esp8266_read_byte(void) { +uint8_t esp8266_read_byte() { uint8_t data; data_gpio_fsel_input(); data = _read_byte(); - dmb(); + __DMB(); return data; } @@ -298,10 +295,10 @@ void esp8266_read_bytes(const uint8_t *data, const uint32_t len){ p++; } - dmb(); + __DMB(); } -uint16_t esp8266_read_halfword(void) { +uint16_t esp8266_read_halfword() { uint16_t data; data_gpio_fsel_input(); @@ -309,12 +306,12 @@ uint16_t esp8266_read_halfword(void) { data = _read_byte(); data |= (uint16_t) (_read_byte() << 8); - dmb(); + __DMB(); return data; } -uint32_t esp8266_read_word(void) { +uint32_t esp8266_read_word() { esp8266_pcast32 u32 __attribute__((aligned(4))); data_gpio_fsel_input(); @@ -324,7 +321,7 @@ uint32_t esp8266_read_word(void) { u32.u8[2] = _read_byte(); u32.u8[3] = _read_byte(); - dmb(); + __DMB(); return u32.u32; } @@ -350,10 +347,10 @@ void esp8266_read_str(char *s, uint32_t *len) { --n; } - dmb(); + __DMB(); } -bool esp8266_detect(void) { +bool esp8266_detect() { esp8266_init(); data_gpio_fsel_output(); @@ -364,15 +361,14 @@ bool esp8266_detect(void) { h3_gpio_set(GPIO_EXT_11);// Tell that we have data available. Wait for ack, wait for 0 -> 1 - uint32_t micros_now = h3_hs_timer_lo_us(); + const auto micros_now = H3_TIMER->AVS_CNT1; // wait 0.5 second for the ESP8266 to respond - while ((!(PORT_CIN->DAT & (1 << CIN))) && (h3_hs_timer_lo_us() - micros_now < (uint32_t) 500000)) + while ((!(PORT_CIN->DAT & (1 << CIN))) && (H3_TIMER->AVS_CNT1 - micros_now < 500000U)) ; if (!(PORT_CIN->DAT & (1 << CIN))) { - - h3_gpio_clr(GPIO_EXT_11); + h3_gpio_clr(GPIO_EXT_11); return false; } @@ -380,7 +376,7 @@ bool esp8266_detect(void) { while (PORT_CIN->DAT & (1 << CIN)); - dmb(); + __DMB(); return true; }