Skip to content

Commit

Permalink
W.I.P. #287
Browse files Browse the repository at this point in the history
  • Loading branch information
vanvught committed Dec 7, 2024
1 parent 97caeca commit b609a7b
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 77 deletions.
6 changes: 3 additions & 3 deletions lib-h3/device/mmc/mmc.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @file mmc.c
*
*/
/* Copyright (C) 2018-2020 by Arjan van Vught mailto:[email protected]
/* Copyright (C) 2018-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 @@ -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)))

Expand Down
5 changes: 3 additions & 2 deletions lib-h3/device/mmc/mmc_bsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @file mmc_bsp.c
*
*/
/* Copyright (C) 2018-2020 by Arjan van Vught mailto:[email protected]
/* Copyright (C) 2018-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 @@ -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

Expand Down
28 changes: 6 additions & 22 deletions lib-h3/src/h3_timer.c → lib-h3/device/mmc/msdelay.c
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* @file h3_timer.c
* @file msdelay.c
*
*/
/* Copyright (C) 2018 by Arjan van Vught mailto:[email protected]
/* 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
Expand All @@ -23,30 +23,14 @@
* THE SOFTWARE.
*/

#include <stdint.h>

#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;
}
13 changes: 1 addition & 12 deletions lib-h3/include/h3_timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @file h3_timer.h
*
*/
/* Copyright (C) 2018-2020 by Arjan van Vught mailto:[email protected]
/* Copyright (C) 2018-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 @@ -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_ */
6 changes: 3 additions & 3 deletions lib-h3/src/h3_timer_init.cpp → lib-h3/src/h3_timer_avs.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* @file h3_h3_timer_init.cpp
* @file h3_timer_avs.cpp
*
*/
/* Copyright (C) 2023 by Arjan van Vught mailto:[email protected]
/* Copyright (C) 2023-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 @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions lib-hal/src/h3/hardware_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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();
Expand Down
62 changes: 29 additions & 33 deletions lib-network/src/esp8266/h3/esp8266.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# error
#endif
/**
* @file esp8266.c
* @file esp8266.cpp
*
*/
/* Copyright (C) 2018-2023 by Arjan van Vught mailto:[email protected]
/* Copyright (C) 2018-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 All @@ -31,11 +31,8 @@

#include <cstdint>

#include "arm/synchronize.h"

#include "h3.h"
#include "h3_gpio.h"
#include "h3_hs_timer.h"
#include "h3_board.h"

/*
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
Expand All @@ -298,23 +295,23 @@ 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();

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();
Expand All @@ -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;
}
Expand All @@ -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();
Expand All @@ -364,23 +361,22 @@ 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;
}

h3_gpio_clr(GPIO_EXT_11); // we acknowledge, and wait for zero

while (PORT_CIN->DAT & (1 << CIN));

dmb();
__DMB();

return true;
}

0 comments on commit b609a7b

Please sign in to comment.