diff --git a/software/firmware_module/module_rev0/src/global.h b/software/firmware_module/module_rev0/src/global.h new file mode 100644 index 0000000..72f5d4f --- /dev/null +++ b/software/firmware_module/module_rev0/src/global.h @@ -0,0 +1,33 @@ +/* Copyright (C) 2025 Dennis Gunia - All Rights Reserved + * You may use, distribute and modify this code under the + * terms of the AGPL-3.0 license. + * + * https://www.dennisgunia.de + * https://github.com/dennis9819/splitflap_v1 + */ + +#include +#include +#include +#include +#include +#include + + + +// I/O Pin definition +#define BUX_RX PD0 +#define BUX_TX PD1 +#define BUX_DIR PD2 +#define SENSOR_HOME PD3 + +#define MOTOR_A PC0 +#define MOTOR_B PC1 +#define MOTOR_C PC2 +#define MOTOR_D PC3 + +// EEPROM Addresses +#define CONF_CONST_OKAY (uint8_t)0xAA +#define CONF_ADDR_OKAY 0x0004 +#define CONF_ADDR_ADDR 0x0000 +#define CONF_ADDR_OFFSET 0x0002 diff --git a/software/firmware_module/module_rev0/src/main.c b/software/firmware_module/module_rev0/src/main.c index b8f4b08..8c8c031 100644 --- a/software/firmware_module/module_rev0/src/main.c +++ b/software/firmware_module/module_rev0/src/main.c @@ -1,20 +1,15 @@ +/* Copyright (C) 2025 Dennis Gunia - All Rights Reserved + * You may use, distribute and modify this code under the + * terms of the AGPL-3.0 license. + * + * https://www.dennisgunia.de + * https://github.com/dennis9819/splitflap_v1 + */ + +#include "global.h" #include "mctrl.h" #include "rcount.h" #include "rs485.h" -#include -#include -#include -#include -/* - * PD0 -> BUS_RX - * PD1 -> BUS_TX - * PD2 -> BUS_DIR (0=RECV/1=SEND) - * - * PD3 -> SENSOR_IN - * - * PC0-3 -> MOTOR CTRL - */ - uint16_t address = 0x0000; int16_t calib_offset = 0x0000; @@ -38,11 +33,6 @@ uint8_t eeprom_read_c(uint16_t address) { return EEDR; } -#define CONF_CONST_OKAY (uint8_t)0xAA -#define CONF_ADDR_OKAY 0x0004 -#define CONF_ADDR_ADDR 0x0000 -#define CONF_ADDR_OFFSET 0x0002 - void initialSetup() { wdt_disable(); if (eeprom_read_c(CONF_ADDR_OKAY) == CONF_CONST_OKAY) { @@ -159,4 +149,4 @@ int main() { while (1 == 1) { readCommand(); } -} \ No newline at end of file +} diff --git a/software/firmware_module/module_rev0/src/mctrl.c b/software/firmware_module/module_rev0/src/mctrl.c index 55130c1..e2a0fa1 100644 --- a/software/firmware_module/module_rev0/src/mctrl.c +++ b/software/firmware_module/module_rev0/src/mctrl.c @@ -1,3 +1,12 @@ +/* Copyright (C) 2025 Dennis Gunia - All Rights Reserved + * You may use, distribute and modify this code under the + * terms of the AGPL-3.0 license. + * + * https://www.dennisgunia.de + * https://github.com/dennis9819/splitflap_v1 + */ + + #include "mctrl.h" // Motor driver steps definition. Reverse for direction change. @@ -97,7 +106,7 @@ ISR(TIMER1_COMPA_vect) { return; } // if sts_flag_pwrdwn, STOP! if (steps_since_home > - STEPS_PRE_REV * 1.5) { // check if home is missing for too long + STEPS_PER_REV * 1.5) { // check if home is missing for too long // home missing error. Wheel probably stuck or power fail sts_flag_noHome = 1; failSafe(); @@ -124,24 +133,24 @@ ISR(TIMER1_COMPA_vect) { absolute_pos--; } else { // when no failsafe is triggered and homing is done // calculate target position - uint16_t target_pos = (target_flap * STEPS_PRE_FLAP) + rel_offset; - if (target_pos >= STEPS_PRE_REV) { - target_pos -= STEPS_PRE_REV; + uint16_t target_pos = (target_flap * STEPS_PER_FLAP) + rel_offset; + if (target_pos >= STEPS_PER_REV) { + target_pos -= STEPS_PER_REV; } if (absolute_pos != target_pos) { // if target position is not reached, move motor ticksSinceMove = 0; mctrl_step(); absolute_pos++; - if (absolute_pos >= STEPS_PRE_REV) { - absolute_pos -= STEPS_PRE_REV; + if (absolute_pos >= STEPS_PER_REV) { + absolute_pos -= STEPS_PER_REV; } // detect home position if ((PIND & (1 << PD3)) == 0) { if (lastSens == 0) { // new home transition int16_t errorDelta = - (absolute_pos > (STEPS_PRE_REV / 2) ? absolute_pos - STEPS_PRE_REV + (absolute_pos > (STEPS_PER_REV / 2) ? absolute_pos - STEPS_PER_REV : absolute_pos); sts_flag_errorTooBig = (errorDelta > 30) || (errorDelta < -30) ? 1 : 0; @@ -208,11 +217,11 @@ void mctrl_set(uint8_t flap, uint8_t fullRotation) { if (fullRotation == 0) { target_flap = flap; // if (absolute_pos < STEPS_ADJ) { - // absolute_pos += STEPS_PRE_REV; + // absolute_pos += STEPS_PER_REV; // } // absolute_pos -= STEPS_ADJ; } else { - target_flap = (target_flap + (STEPS_PRE_FLAP - 1)) % STEPS_PRE_FLAP; + target_flap = (target_flap + (STEPS_PER_FLAP - 1)) % STEPS_PER_FLAP; afterRotation = flap; } } @@ -239,4 +248,4 @@ void mctrl_step() { step_index = 0; } PORTC = motor_steps[step_index]; -} \ No newline at end of file +} diff --git a/software/firmware_module/module_rev0/src/mctrl.h b/software/firmware_module/module_rev0/src/mctrl.h index cedbc3f..bc9861c 100644 --- a/software/firmware_module/module_rev0/src/mctrl.h +++ b/software/firmware_module/module_rev0/src/mctrl.h @@ -1,24 +1,25 @@ +/* Copyright (C) 2025 Dennis Gunia - All Rights Reserved + * You may use, distribute and modify this code under the + * terms of the AGPL-3.0 license. + * + * https://www.dennisgunia.de + * https://github.com/dennis9819/splitflap_v1 + */ + + +#include "global.h" +#include "rcount.h" + #pragma once -#define MP_A PC0 -#define MP_B PC1 -#define MP_C PC2 -#define MP_D PC3 -#define STEPS_PRE_REV 2025 -#define STEPS_PRE_FLAP 45 +#define STEPS_PER_REV 2025 +#define STEPS_PER_FLAP 45 #define STEPS_ADJ 0 // added per flap to compensate for motor power down #define AMOUNTFLAPS 45 #define ERROR_DATASETS 8 -#include -#include -#include -#include -#include -#include "rcount.h" - #ifdef __cplusplus extern "C" { #endif // __cplusplus @@ -33,4 +34,4 @@ void mctrl_power(uint8_t state); #ifdef __cplusplus } -#endif // __cplusplus \ No newline at end of file +#endif // __cplusplus diff --git a/software/firmware_module/module_rev0/src/rcount.c b/software/firmware_module/module_rev0/src/rcount.c index 58c3c7f..789831f 100644 --- a/software/firmware_module/module_rev0/src/rcount.c +++ b/software/firmware_module/module_rev0/src/rcount.c @@ -1,3 +1,12 @@ +/* Copyright (C) 2025 Dennis Gunia - All Rights Reserved + * You may use, distribute and modify this code under the + * terms of the AGPL-3.0 license. + * + * https://www.dennisgunia.de + * https://github.com/dennis9819/splitflap_v1 + */ + + #include "rcount.h" uint8_t rc_eeprom_write_c(uint16_t address, uint8_t data) { diff --git a/software/firmware_module/module_rev0/src/rcount.h b/software/firmware_module/module_rev0/src/rcount.h index 7f0303d..d4e0d8e 100644 --- a/software/firmware_module/module_rev0/src/rcount.h +++ b/software/firmware_module/module_rev0/src/rcount.h @@ -1,5 +1,12 @@ -#include -#include +/* Copyright (C) 2025 Dennis Gunia - All Rights Reserved + * You may use, distribute and modify this code under the + * terms of the AGPL-3.0 license. + * + * https://www.dennisgunia.de + * https://github.com/dennis9819/splitflap_v1 + */ + +#include "global.h" #ifdef __cplusplus extern "C" { @@ -10,4 +17,3 @@ void rc_tick(); #ifdef __cplusplus } #endif // __cplusplus - diff --git a/software/firmware_module/module_rev0/src/rs458.c b/software/firmware_module/module_rev0/src/rs458.c index 87da365..3682bf5 100644 --- a/software/firmware_module/module_rev0/src/rs458.c +++ b/software/firmware_module/module_rev0/src/rs458.c @@ -1,3 +1,11 @@ +/* Copyright (C) 2025 Dennis Gunia - All Rights Reserved + * You may use, distribute and modify this code under the + * terms of the AGPL-3.0 license. + * + * https://www.dennisgunia.de + * https://github.com/dennis9819/splitflap_v1 + */ + #include "rs485.h" void rs485_init() { diff --git a/software/firmware_module/module_rev0/src/rs485.h b/software/firmware_module/module_rev0/src/rs485.h index fb2d228..f787c9f 100644 --- a/software/firmware_module/module_rev0/src/rs485.h +++ b/software/firmware_module/module_rev0/src/rs485.h @@ -1,3 +1,12 @@ +/* Copyright (C) 2025 Dennis Gunia - All Rights Reserved + * You may use, distribute and modify this code under the + * terms of the AGPL-3.0 license. + * + * https://www.dennisgunia.de + * https://github.com/dennis9819/splitflap_v1 + */ +#include "global.h" + #pragma once //#define F_CPU 16000000UL #define UART_BAUD 19200 @@ -6,8 +15,7 @@ #define SFBUS_SOF_BYTE '+' #define SFBUS_EOF_BYTE '$' -#include -#include + #ifdef __cplusplus extern "C" { @@ -24,4 +32,4 @@ void sfbus_send_frame(uint16_t address, char* payload, uint8_t length); #ifdef __cplusplus } -#endif // __cplusplus \ No newline at end of file +#endif // __cplusplus