diff --git a/lib/onewire_master.c b/lib/onewire_master.c index 736fb79..730eb6f 100644 --- a/lib/onewire_master.c +++ b/lib/onewire_master.c @@ -12,8 +12,8 @@ * along with this program. If not, see . * */ -/** library for 1-wire protocol as master (code) - * @file onewire_master.c +/** library for 1-wire protocol as master + * @file * @author King Kévin * @date 2017-2018 * @note peripherals used: timer @ref onewire_master_timer, GPIO @ref onewire_master_gpio @@ -35,14 +35,20 @@ /* own libraries */ #include "global.h" // help macros +#include "interrupt.h" // runtime interrupt table #include "onewire_master.h" // own definitions /** @defgroup onewire_master_timer timer used to measure 1-wire signal timing * @{ */ -#define ONEWIRE_MASTER_TIMER 2 /**< timer ID */ +#define ONEWIRE_MASTER_TIMER 5 /**< timer ID */ /** @} */ +/** set if the timer ISR should be set in the interrupt table instead of the vector table + * @note the vector table is faster, but doesn't allow to change the ISR + */ +#define ONEWIRE_MASTER_TIMER_USE_INTERRUPT_TABLE false + /** state of 1-Wire communication */ volatile enum { ONEWIRE_STATE_IDLE, /**< no current communication */ @@ -59,6 +65,106 @@ static volatile bool slave_presence = false; /**< if slaves have been detected * static uint8_t* buffer = NULL; /**< input/output buffer for read/write commands/functions */ static uint32_t buffer_size = 0; /**< size of buffer in bits */ static volatile uint32_t buffer_bit = 0; /**< number of bits read/written */ +#if defined(ONEWIRE_MASTER_TIMER_USE_INTERRUPT_TABLE) && ONEWIRE_MASTER_TIMER_USE_INTERRUPT_TABLE +static void (*isr_backup)(void) = NULL; /**< backup for the existing timer ISR */ +static bool irq_backup = false; /**< backup for the existing timer IRQ */ +#endif + +/** interrupt service routine called for timer */ +#if defined(ONEWIRE_MASTER_TIMER_USE_INTERRUPT_TABLE) && ONEWIRE_MASTER_TIMER_USE_INTERRUPT_TABLE +static void onewire_master_timer_isr(void) +#else +void TIM_ISR(ONEWIRE_MASTER_TIMER)(void) +#endif +{ + if (timer_get_flag(TIM(ONEWIRE_MASTER_TIMER), TIM_SR_UIF)) { // overflow update event happened + timer_clear_flag(TIM(ONEWIRE_MASTER_TIMER), TIM_SR_UIF); // clear flag + switch (onewire_master_state) { + case ONEWIRE_STATE_MASTER_RESET: // reset pulse has been started + timer_clear_flag(TIM(ONEWIRE_MASTER_TIMER), TIM_SR_CC4IF); // clear output compare flag + timer_enable_irq(TIM(ONEWIRE_MASTER_TIMER), TIM_DIER_CC4IE); // enable compare interrupt for presence detection + gpio_set(GPIO(ONEWIRE_MASTER_PORT),GPIO(ONEWIRE_MASTER_PIN)); // set signal high again for slaves to respond + onewire_master_state = ONEWIRE_STATE_SLAVE_PRESENCE; // set new state + break; + case ONEWIRE_STATE_SLAVE_PRESENCE: // waiting for slave presence but none received + timer_disable_counter(TIM(ONEWIRE_MASTER_TIMER)); // disable timer + timer_disable_irq(TIM(ONEWIRE_MASTER_TIMER), TIM_DIER_CC4IE); // disable compare interrupt for presence detection + onewire_master_state = ONEWIRE_STATE_DONE; // go to next state + break; + case ONEWIRE_STATE_MASTER_READ: // end of time slot and recovery time for reading bit + case ONEWIRE_STATE_MASTER_WRITE: // end of time slot and recovery time for writing bit + if (buffer_bit. * */ -/** library for 1-wire protocol as master (API) - * @file onewire_master.h +/** library for 1-wire protocol as master + * @file * @author King Kévin * @date 2017-2018 * @note peripherals used: timer @ref onewire_master_timer, GPIO @ref onewire_master_gpio