From f75d0b7eabe275a4330eb72dc4a4c5d2af1fb13d Mon Sep 17 00:00:00 2001 From: root Date: Mon, 22 Nov 2021 11:23:35 +0000 Subject: turn on cycle counter correctly, and add a for-real motor --- stm32/app/hands.c | 21 +++++++++++++++- stm32/app/leds.c | 4 +-- stm32/app/main.c | 2 +- stm32/app/motor.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++-- stm32/app/pins.h | 1 + stm32/app/project.h | 3 ++- 6 files changed, 96 insertions(+), 7 deletions(-) diff --git a/stm32/app/hands.c b/stm32/app/hands.c index 5414d9e..7ac8bb9 100644 --- a/stm32/app/hands.c +++ b/stm32/app/hands.c @@ -5,6 +5,24 @@ unsigned hands_pos[HANDS]; unsigned hands_ready; +static unsigned calc_second_pos (MTIME *m) +{ + uint64_t p; + + p += m->second; + p *= 1000; + p += m->nanosecond / 1000000; + + p *= MOTOR_STEPS; + p /= 100000; + + + + return (unsigned) p; + +} + + static unsigned calc_minute_pos (MTIME *m) { uint64_t p; @@ -58,7 +76,7 @@ void hands_tick (void) static uint32_t last; - if ((ticks - last) < 100) return; + if ((ticks - last) < 10) return; last = ticks; @@ -72,6 +90,7 @@ void hands_tick (void) hands_pos[0] = calc_hour_pos (&m); hands_pos[1] = calc_minute_pos (&m); + //hands_pos[1] = calc_second_pos (&m); hands_ready = 1; for (i = 0, p = 0; i < HANDS; ++i) { diff --git a/stm32/app/leds.c b/stm32/app/leds.c index bcac9b6..b02e584 100644 --- a/stm32/app/leds.c +++ b/stm32/app/leds.c @@ -38,7 +38,7 @@ static void metric_pll (void) v %= 864; - // printf("M: %5u %9u\r\n",(unsigned) v, (unsigned ) (e.ns/1000000)); + //printf("M: %5u %9u\r\n",(unsigned) v, (unsigned ) (e.ns/1000000)); ttg = 864 - (unsigned)v; } @@ -60,7 +60,7 @@ static void customary_pll (void) last = ticks; v = e.ns / 1000000; - v %= 864; + v %= 1000; ttg = 1000 - (unsigned)v; diff --git a/stm32/app/main.c b/stm32/app/main.c index 344a106..31f220d 100644 --- a/stm32/app/main.c +++ b/stm32/app/main.c @@ -18,7 +18,7 @@ int main (void) rcc_periph_clock_enable (RCC_PWR); rcc_periph_clock_enable (RCC_BKP); pwr_disable_backup_domain_write_protect(); - DWT_CTRL |= DWT_CTRL_CYCCNTENA; + dwt_enable_cycle_counter(); nvic_set_priority (NVIC_SYSTICK_IRQ, 0x80); diff --git a/stm32/app/motor.c b/stm32/app/motor.c index 392089b..8ffc5e6 100644 --- a/stm32/app/motor.c +++ b/stm32/app/motor.c @@ -3,9 +3,42 @@ static unsigned motor_pos[HANDS]; +#define M1_A GPIO11 +#define M1_A_PORT GPIOB +#define M1_B GPIO12 +#define M1_B_PORT GPIOB +#define M1_C GPIO13 +#define M1_C_PORT GPIOB +#define M1_D GPIO14 +#define M1_D_PORT GPIOB + static void coils (unsigned m, int a, int b, int c, int d) { + + if (!m) return; + + if (a) + SET (M1_A); + else + CLEAR (M1_A); + + if (b) + SET (M1_B); + else + CLEAR (M1_B); + + if (c) + SET (M1_C); + else + CLEAR (M1_C); + + if (d) + SET (M1_D); + else + CLEAR (M1_D); + + printf ("Motor %d: %+d %+d\r\n", m, a - b, c - d); } @@ -18,7 +51,26 @@ static void step (unsigned m, int d) s += d; s %= MOTOR_STEPS; - coils (m, !! ((s + 6) & 4), !! ((s + 1) & 4), !! ((s + 4) & 4), !! ((s + 7) & 4)); + + switch (s & 3) { + case 0: + coils (m, 0, 1, 0, 1); + break; + + case 1: + coils (m, 0, 1, 1, 0); + break; + + case 2: + coils (m, 1, 0, 1, 0); + break; + + case 3: + coils (m, 1, 0, 0, 1); + break; + } + + if (!m) BKP_DR8 = motor_pos[0] = s; @@ -36,11 +88,20 @@ void motor_tick (void) { static uint32_t last; unsigned i, d; + static unsigned wiggle = MOTOR_STEPS; + - if ((ticks - last) < 10) return; + + if ((ticks - last) < 4) return; last = ticks; + if (wiggle) { + step (1, 1); + wiggle--; + return; + } + if (!hands_ready) return; //printf("HANDS: %d -> %d %d -> %d\r\n",hands_pos[0],motor_pos[0],hands_pos[1],motor_pos[1]); @@ -51,6 +112,7 @@ void motor_tick (void) d -= motor_pos[i]; d %= MOTOR_STEPS; + if (d) { if (d < (MOTOR_STEPS / 2)) step (i, 1); @@ -66,10 +128,16 @@ void motor_tick (void) void motor_init (void) { + MAP_OUTPUT_PP (M1_A); + MAP_OUTPUT_PP (M1_B); + MAP_OUTPUT_PP (M1_C); + MAP_OUTPUT_PP (M1_D); + motor_pos[0] = BKP_DR8; motor_pos[1] = BKP_DR9; step (0, 0); step (1, 0); + } diff --git a/stm32/app/pins.h b/stm32/app/pins.h index 954396f..41fd0a1 100644 --- a/stm32/app/pins.h +++ b/stm32/app/pins.h @@ -49,6 +49,7 @@ #define CLEAR(a) gpio_clear( a ## _PORT, a) #define SET(a) gpio_set( a ## _PORT, a) #define GET(a) gpio_get( a ## _PORT, a) +#define WRITE(a,b) do { if (b) SET(a); else CLEAR(a) } while (0) #define TOGGLE(a) gpio_toggle( a ## _PORT, a) #endif diff --git a/stm32/app/project.h b/stm32/app/project.h index fd71876..0d73544 100644 --- a/stm32/app/project.h +++ b/stm32/app/project.h @@ -35,7 +35,8 @@ #define DWT_FREQ 48000000 #define HANDS 2 -#define MOTOR_STEPS (1080 * 2) +//#define MOTOR_STEPS (1080 * 2) +#define MOTOR_STEPS (720 ) #include "board.h" #include "ring.h" -- cgit v1.2.3