summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorroot <root@lab.panaceas.james.local>2019-02-22 10:45:35 +0000
committerroot <root@lab.panaceas.james.local>2019-02-22 10:45:35 +0000
commit904bc0ebdb9c059b7b1d276829bad766f73dc3e9 (patch)
tree30c01ecf79b72c6953d593d2d3dccfbcd31f76cd
parente761edee5e8ef7106fc8080fc386447e7956be71 (diff)
downloadclock-904bc0ebdb9c059b7b1d276829bad766f73dc3e9.tar.gz
clock-904bc0ebdb9c059b7b1d276829bad766f73dc3e9.tar.bz2
clock-904bc0ebdb9c059b7b1d276829bad766f73dc3e9.zip
switch to stlink, blinky
-rw-r--r--Makefile.include2
-rw-r--r--Makefile.rules2
-rw-r--r--app/Makefile2
-rw-r--r--app/dcf77.c2
-rw-r--r--app/gps.c2
-rw-r--r--app/led.c29
-rw-r--r--app/main.c4
-rw-r--r--app/msf.c2
-rw-r--r--app/pll.c126
-rw-r--r--app/project.h1
-rw-r--r--app/prototypes.h5
-rw-r--r--app/steth.c86
-rw-r--r--app/ticker.c3
-rw-r--r--app/time_fn.c7
-rw-r--r--oocd/board/stm32f407vet6.cfg3
15 files changed, 194 insertions, 82 deletions
diff --git a/Makefile.include b/Makefile.include
index 7a60f2f..9225b89 100644
--- a/Makefile.include
+++ b/Makefile.include
@@ -31,7 +31,7 @@ ARCH_FLAGS = -mthumb -mcpu=cortex-m4 $(FP_FLAGS)
OOCD ?= openocd
#OOCD_INTERFACE ?= cmsis-dap
#OOCD_BOARD ?= arch_max
-OOCD_INTERFACE ?= j-link
+OOCD_INTERFACE ?= stlink-v2
OOCD_BOARD ?= stm32f407vet6
################################################################################
diff --git a/Makefile.rules b/Makefile.rules
index fe31837..29294bb 100644
--- a/Makefile.rules
+++ b/Makefile.rules
@@ -207,7 +207,7 @@ ifeq ($(OOCD_SERIAL),)
-c "init" -c "reset init" \
-c "flash write_image erase $(*).hex" \
-c "reset" \
- -c "shutdown" $(NULL)
+ -c "shutdown" $(NULL)
else
%.flash: %.hex
@printf " FLASH $<\n"
diff --git a/app/Makefile b/app/Makefile
index e8ae217..c325681 100644
--- a/app/Makefile
+++ b/app/Makefile
@@ -109,4 +109,4 @@ tidy:
astyle -A3 -s2 --attach-extern-c -L -c -w -Y -m0 -f -p -H -U -k3 -xj -xd ${CSRCS} ${HSRCS}
slog:
- sympathy -d /dev/ttyUSB6 -b 38400 -L ../log -w 132x59 -s
+ sympathy -d /dev/ttyUSB5 -b 38400 -L ../log -w 132x59 -s
diff --git a/app/dcf77.c b/app/dcf77.c
index 909188b..2a952cd 100644
--- a/app/dcf77.c
+++ b/app/dcf77.c
@@ -72,7 +72,7 @@ static void process_bits (uint64_t abs)
if (bits[17]) dcf77_time.s -= 3600; /*CEST*/
- dcf77_last_happy = abs - (int64_t) 60 * (int64_t) HZ;
+ dcf77_last_happy = make_happy (abs, 60);
pll_set_offset (dcf77_time, abs);
diff --git a/app/gps.c b/app/gps.c
index b3c322d..39bb13f 100644
--- a/app/gps.c
+++ b/app/gps.c
@@ -217,7 +217,7 @@ ubx_recv_utc (uint8_t *ptr, unsigned len)
now = SCS_DWT_CYCCNT;
abs = abs_extend (now);
- gps_last_happy = abs + (int64_t) HZ * (int64_t) 60;
+ gps_last_happy = make_happy (abs, 180);
pll_set_offset (gps_time, abs);
diff --git a/app/led.c b/app/led.c
index 825bd0d..b7f34a0 100644
--- a/app/led.c
+++ b/app/led.c
@@ -1,9 +1,11 @@
#include "project.h"
-#define LED (GPIO3)
-#define LED_PORT GPIOB
+#define LED (GPIO6)
+#define LED_PORT GPIOA
+static unsigned led_ms;
+
void
led_init (void)
{
@@ -15,24 +17,29 @@ led_init (void)
void
led_clear (void)
{
- CLEAR (LED);
+ SET (LED);
}
void
led_set()
{
- gpio_set (LED_PORT, LED);
+ CLEAR (LED);
+}
+
+void led_blink (unsigned ms)
+{
+ led_set();
+ led_ms = ms;
}
+
void
-led_slow_tick (void)
+led_tick (void)
{
- static int c;
+ if (!led_ms) return;
+
+ led_ms--;
- c = !c;
+ if (!led_ms) led_clear();
- if (c)
- led_set();
- else
- led_clear();
}
diff --git a/app/main.c b/app/main.c
index 703d366..356d4da 100644
--- a/app/main.c
+++ b/app/main.c
@@ -3,7 +3,7 @@
int time_known;
-static void cmd_dispatch()
+static void cmd_dispatch (void)
{
uint8_t c;
@@ -49,7 +49,7 @@ static void
board_setup (void)
{
rcc_osc_bypass_enable (HSE);
- RCC_SSCGR=0;
+ RCC_SSCGR = 0;
rcc_clock_setup_hse_3v3 (&hse_10mhz_3v3_168);
rcc_periph_clock_enable (RCC_SYSCFG);
diff --git a/app/msf.c b/app/msf.c
index 4b3c350..82dea7d 100644
--- a/app/msf.c
+++ b/app/msf.c
@@ -104,7 +104,7 @@ static void process_bits (uint64_t abs)
msf_time = time_utc_to_epoch (u);
- msf_last_happy = abs - (int64_t) 120 * (int64_t) HZ;
+ msf_last_happy = make_happy (abs, 0);
pll_set_offset (msf_time, abs);
diff --git a/app/pll.c b/app/pll.c
index 944e6cc..460b789 100644
--- a/app/pll.c
+++ b/app/pll.c
@@ -2,7 +2,7 @@
#define JUMP_THRESH 0.1
#define JUMP_TICKS 30
-#define FEEDBACK 0.01
+#define FEEDBACK 0.0001
#define WARM_UP 30
@@ -15,8 +15,56 @@ static int out_of_lock = JUMP_TICKS + 1;
uint64_t pll_last_update;
int pll_valid = 0;
+int pll_ready = 0;
+
+
+# if 0
+#define PLL_BW 0.01
+#define PLL_DAMP 0.707
+#define PLL_GAIN 0.001
+
+#define F_T1 ((PLL_GAIN)/((PLL_BW)*(PLL_BW)))
+#define F_T2 ((2*(PLL_DAMP))/(PLL_BW))
+
+
+#define F_B0 (((4*(PLL_GAIN))/(F_T1))*(1.0+((F_T2)/(2.0))))
+#define F_B1 ((8*(PLL_GAIN))/(F_T1))
+#define F_B2 (((4*(PLL_GAIN))/(F_T1))*(1.0-((F_T2)/(2.0))))
+
+#define F_A0 (1.0)
+#define F_A1 (-2.0)
+#define F_A2 (1.0)
+
+void pll_dump_filter (void)
+{
+ printf ("%g %g %g\n", F_A0, F_A1, F_A2);
+ printf ("%g %g %g\n", F_B0, F_B1, F_B2);
+}
+
+
+
+static double filter (double in)
+{
+ static double v[3];
+ double ret;
+
+ v[2] = v[1];
+ v[1] = v[0];
+ v[0] = in - (v[1] * F_A1) - (v[2] * F_A2);
+
+ return (v[0] * F_B0) + (v[1] * F_B1) + (v[2] * F_B2);
+
+}
+#endif
+
+
+#define PLL_ALPHA (0.005)
+#define PLL_BETA (0.5*PLL_ALPHA*PLL_ALPHA)
+
+
+
+
-static int warming_up = WARM_UP;
void pll_meh (void)
@@ -32,10 +80,15 @@ static void modify_pll_freq (uint64_t now, int d)
int64_t pd1, pd2, te;
pd1 = now - phase;
-
te = pd1 / pll_freq;
pd1 %= pll_freq;
+ if (pd1 > (pll_freq >> 1)) {
+ te++;
+ pd1 = pd1 - pll_freq;
+ }
+
+
if (d > 0)
pll_freq += d;
else
@@ -47,10 +100,27 @@ static void modify_pll_freq (uint64_t now, int d)
phase = now - pd2;
}
+uint64_t make_happy (uint64_t abs, int64_t shift)
+{
+ shift *= HZ;
+
+ if (shift < 0) {
+ shift = -shift;
+
+ if (abs < (uint64_t) shift) return 0;
+ else
+ return abs - shift;
+ }
+
+ return abs + shift;
+
+
+}
+
void pll_dispatch (uint64_t happy, uint64_t edge, const char *src)
{
- double f;
+ double f, g;
int64_t pd;
@@ -59,23 +129,40 @@ void pll_dispatch (uint64_t happy, uint64_t edge, const char *src)
{
int h1, h2, h3, h4;
EPOCH e;
+ UTC u;
+ char s1[80];
+ char s2[80];
+ char s3[80];
+ char s4[80];
e = pll_decompose (happy);
+ u = time_epoch_to_utc (e);
+ utc_to_str (s1, u);
h1 = e.s;
+
e = pll_decompose (gps_last_happy);
+ u = time_epoch_to_utc (e);
+ utc_to_str (s2, u);
h2 = e.s;
+
e = pll_decompose (dcf77_last_happy);
+ u = time_epoch_to_utc (e);
+ utc_to_str (s3, u);
h3 = e.s;
+
e = pll_decompose (msf_last_happy);
+ u = time_epoch_to_utc (e);
+ utc_to_str (s4, u);
h4 = e.s;
printf ("H %d %d %d %d\r\n", h1 - h2, h2 - h2, h3 - h2, h4 - h2);
+ // printf ("H %s %s %s %s\r\n",s1,s2,s3,s4);
}
#endif
-if ((!gps_last_happy) && (!dcf77_last_happy) && (!msf_last_happy)) return;
+ if ((!gps_last_happy) && (!dcf77_last_happy) && (!msf_last_happy)) return;
if (happy < gps_last_happy) return;
@@ -83,14 +170,17 @@ if ((!gps_last_happy) && (!dcf77_last_happy) && (!msf_last_happy)) return;
if (happy < msf_last_happy) return;
- if (warming_up) {
- warming_up--;
+ if (!pll_ready && (edge < ((uint64_t) WARM_UP * (uint64_t) HZ)))
return;
- }
-printf("EDGE %08x%08x\r\n",
- (uint32_t) (edge >>32),
- (uint32_t) (edge & 0xffffffff));
+ led_blink (100);
+
+
+#if 1
+ printf ("EDGE %08x%08x\r\n",
+ (unsigned) (edge >> 32),
+ (unsigned) (edge & 0xffffffff));
+#endif
{
int diff, hf;
@@ -107,27 +197,27 @@ printf("EDGE %08x%08x\r\n",
f = (double) diff;
- f /= (double) pll_freq;
+ g = f / (double) pll_freq;
}
- if ((f > (JUMP_THRESH)) || (f < - (JUMP_THRESH)))
+ if ((g > (JUMP_THRESH)) || (g < - (JUMP_THRESH)))
out_of_lock++;
else if (out_of_lock <= JUMP_TICKS)
out_of_lock = 0;
- printf ("PLL pd %.3f pll_freq %d phase %d %s\r\n", (float) f, (int) pll_freq, (int) phase, src);
+ printf ("PLL pd %.3f %.1f pll_freq %d phase %d %s\r\n", (float) g, (float)f, (int) pll_freq, (int) phase, src);
- if (out_of_lock > JUMP_TICKS) {
+ if ((out_of_lock > JUMP_TICKS) || !pll_ready) {
phase += pd;
out_of_lock = 0;
printf ("PLL - jumping\r\n");
pll_freq = HZ;
+ pll_ready = 1;
} else {
- f *= FEEDBACK;
- f *= (double) pll_freq;
+ phase += (int) (f * PLL_BETA);
+ modify_pll_freq (edge, (int) (f * PLL_ALPHA));
- modify_pll_freq (edge, (int) f);
}
pll_last_update = edge;
diff --git a/app/project.h b/app/project.h
index d19cd61..869cb3d 100644
--- a/app/project.h
+++ b/app/project.h
@@ -21,6 +21,7 @@
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/cm3/cortex.h>
#include <libopencm3/cm3/scs.h>
+#include <libopencm3/cm3/scb.h>
#include <lwip/init.h>
#include <lwip/sys.h>
diff --git a/app/prototypes.h b/app/prototypes.h
index 44cc1c8..933e583 100644
--- a/app/prototypes.h
+++ b/app/prototypes.h
@@ -2,7 +2,8 @@
extern void led_init(void);
extern void led_clear(void);
extern void led_set(void);
-extern void led_slow_tick(void);
+extern void led_blink(unsigned ms);
+extern void led_tick(void);
/* ticker.c */
extern volatile uint32_t ticks;
extern void delay_us(uint32_t d);
@@ -64,6 +65,7 @@ extern int64_t pll_freq;
extern uint64_t pll_last_update;
extern int pll_valid;
extern void pll_meh(void);
+extern uint64_t make_happy(uint64_t abs, int64_t shift);
extern void pll_dispatch(uint64_t happy, uint64_t edge, const char *src);
extern void pll_set_offset(EPOCH epoch, uint64_t abs);
extern EPOCH _pll_decompose(uint64_t abs);
@@ -75,6 +77,7 @@ extern int main(void);
/* time_fn.c */
extern UTC time_epoch_to_utc(EPOCH epoch);
extern EPOCH time_utc_to_epoch(UTC u);
+extern void utc_to_str(char *dst, UTC u);
extern void time_print_utc(const char *p, UTC u);
extern void time_print_epoch(const char *p, EPOCH e);
/* ntp.c */
diff --git a/app/steth.c b/app/steth.c
index 2603725..ead4efd 100644
--- a/app/steth.c
+++ b/app/steth.c
@@ -2,10 +2,6 @@
#include "steth.h"
-#define FUCKED
-
-static int fucked;
-
#define AN_RETRY 6
@@ -86,13 +82,15 @@ static void phy_stat_reg (unsigned i)
if (cur == last_phy[i]) return;
printf (" phy:%02x %4x (was %4x +%4x -%4x)\r\n",
- i, (unsigned) cur, (unsigned) last_phy[i],
+ i,
+ (unsigned) cur, (unsigned) last_phy[i],
(unsigned) ((last_phy[i] ^ cur) & cur),
(unsigned) ((last_phy[i] ^ cur) & last_phy[i]));
last_phy[i] = cur;
}
+
static void phy_stat (void)
{
unsigned i;
@@ -104,7 +102,7 @@ static void phy_stat (void)
static bool
phy_link_an_done (uint8_t phy)
{
- return eth_smi_read (PHY, PHY_REG_BSR) & PHY_REG_BSR_ANDONE;
+ return eth_smi_read (phy, PHY_REG_BSR) & PHY_REG_BSR_ANDONE;
}
@@ -197,22 +195,9 @@ steth_lwip_init (struct netif *netif)
return ERR_OK;
}
-
-#ifdef FUCKED
-
-static void fucked_phy_reset (uint8_t phy)
-{
- unsigned i;
- eth_smi_write (phy, PHY_REG_BCR, PHY_REG_BCR_RESET);
-
- for (i = 0; (eth_smi_read (phy, PHY_REG_BCR) & PHY_REG_BCR_RESET) && (i < 1000); ++i) delay_us (1000);
-
-}
-
-static void fucked_eth_init (uint8_t phy, enum eth_clk clock)
+static void my_eth_init (uint8_t phy, enum eth_clk clock)
{
ETH_MACMIIAR = clock;
- fucked_phy_reset (phy);
ETH_MACCR = ETH_MACCR_CSTF | ETH_MACCR_FES | ETH_MACCR_DM |
ETH_MACCR_APCS | ETH_MACCR_RD;
@@ -227,7 +212,35 @@ static void fucked_eth_init (uint8_t phy, enum eth_clk clock)
(32 << ETH_DMABMR_RDP_SHIFT) | (32 << ETH_DMABMR_PBL_SHIFT) |
ETH_DMABMR_PM_2_1 | ETH_DMABMR_USP;
}
-#endif
+
+
+
+static void phy_set_ignore_address (void)
+{
+ unsigned i;
+
+ i = 0;
+
+ while (eth_smi_read (PHY0, 0) == 0xffff) {
+ delay_us (1000);
+ i++;
+
+ if (i > 10) break;
+ }
+
+ eth_smi_write (PHY, 0x11, 0x8);
+
+ i = 0;
+
+ while (eth_smi_read (PHY1, 0) == 0xffff) {
+ delay_us (1000);
+ i++;
+
+ if (i > 10) break;
+ }
+
+ eth_smi_write (PHY1, 0x11, 0x8);
+}
static void eth_reset (void)
@@ -237,21 +250,21 @@ static void eth_reset (void)
printf ("Eth_reset()\r\n");
rcc_periph_reset_hold (RST_ETHMAC);
- delay_ms (1);
+ delay_us (1000);
#ifndef SYSCFG_PMC_MII_RMII_SEL
#define SYSCFG_PMC_MII_RMII_SEL (1UL << 23)
#endif
SYSCFG_PMC |= SYSCFG_PMC_MII_RMII_SEL;
- delay_ms (1);
+ delay_us (1000);
rcc_periph_reset_release (RST_ETHMAC);
- delay_ms (1);
+ delay_us (1000);
CLEAR (NRST);
delay_us (1);
SET (NRST);
- delay_ms (1);
+ delay_us (1000);
TRACE;
ETH_DMABMR |= ETH_DMABMR_SR;
@@ -259,7 +272,7 @@ static void eth_reset (void)
i = 0;
while (ETH_DMABMR & ETH_DMABMR_SR) {
- delay_ms (1);
+ delay_us (1000);
i++;
if (i > 1000) {
@@ -270,16 +283,9 @@ static void eth_reset (void)
/*MDC = HCLK / 102 (0b100) => 1.6MHz */
TRACE;
-#ifdef FUCKED
- fucked_eth_init (PHY, ETH_CLK_150_168MHZ);
-#else
- eth_init (PHY, ETH_CLK_150_168MHZ);
-#endif
- if (eth_smi_read (PHY, 0) == 0xffff) {
- fucked = 1;
- printf ("WARNING: PHY is AWOL\r\n");
- }
+ my_eth_init (PHY, ETH_CLK_150_168MHZ);
+ phy_set_ignore_address();
TRACE;
phy_stat();
@@ -364,7 +370,7 @@ steth_init (void)
eth_smi_write (PHY, PHY_REG_BCR, 0x2100);
printf ("Waiting for link\r\n");
- while (!phy_link_isup (PHY)) {
+ while (!phy_link_isup (phy)) {
phy_stat();
delay_ms (1000);
}
@@ -373,12 +379,6 @@ steth_init (void)
eth_start_an();
- if (fucked) delay_ms (1000);
-
-
-
- //
- //eth_start();
nvic_enable_irq (NVIC_ETH_IRQ);
@@ -389,11 +389,13 @@ steth_init (void)
+#if 0
static void eth_stop (void)
{
ETH_MACCR &= (ETH_MACCR_TE & ETH_MACCR_RE);
ETH_DMAOMR &= ~ (ETH_DMAOMR_FTF | ETH_DMAOMR_ST | ETH_DMAOMR_SR);
}
+#endif
void steth_slow_tick (void)
{
diff --git a/app/ticker.c b/app/ticker.c
index f5bece6..0f2b3e3 100644
--- a/app/ticker.c
+++ b/app/ticker.c
@@ -24,6 +24,8 @@ sys_tick_handler (void)
ticks++;
+ led_tick();
+
slow++;
if (slow < 1000)
@@ -31,7 +33,6 @@ sys_tick_handler (void)
slow = 0;
- led_slow_tick();
abs_slow_tick();
steth_slow_tick();
}
diff --git a/app/time_fn.c b/app/time_fn.c
index 1067dd9..957bc47 100644
--- a/app/time_fn.c
+++ b/app/time_fn.c
@@ -209,6 +209,13 @@ EPOCH time_utc_to_epoch (UTC u)
return ret;
}
+void utc_to_str (char *dst, UTC u)
+{
+ const char *dname[] = {"Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat", "Sun"};
+ const char *mname[] = {"", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
+ sprintf (dst, "%s %04d-%s-%02d %02d:%02d:%02d.%09d", dname[u.wday], u.year, mname[u.month], u.mday, u.hour, u.minute, u.second, u.nanosecond);
+}
+
void time_print_utc (const char *p, UTC u)
{
diff --git a/oocd/board/stm32f407vet6.cfg b/oocd/board/stm32f407vet6.cfg
index 4ba3a0a..9be7ac5 100644
--- a/oocd/board/stm32f407vet6.cfg
+++ b/oocd/board/stm32f407vet6.cfg
@@ -1,4 +1,5 @@
source [find target/stm32f4x.cfg]
-reset_config srst_only
+#reset_config srst_only
+reset_config trst_only