diff --git a/lib/onewire_slave.c b/lib/onewire_slave.c index 0bf87ab..0d64c84 100644 --- a/lib/onewire_slave.c +++ b/lib/onewire_slave.c @@ -12,10 +12,10 @@ * along with this program. If not, see . * */ -/** library for 1-wire protocol as master (code) - * @file onewire_slave.c +/** library for 1-wire protocol as master + * @file * @author King Kévin - * @date 2017 + * @date 2017-2020 * @note peripherals used: GPIO and timer @ref onewire_slave_timer, GPIO @ref onewire_slave_gpio * @note overdrive mode is not supported * @implements 1-Wire protocol description from Book of iButton Standards @@ -125,15 +125,15 @@ void onewire_slave_setup(uint8_t family, uint64_t serial) rcc_periph_clock_enable(RCC_TIM(ONEWIRE_SLAVE_TIMER)); // enable clock for timer peripheral rcc_periph_reset_pulse(RST_TIM(ONEWIRE_SLAVE_TIMER)); // reset timer state timer_set_mode(TIM(ONEWIRE_SLAVE_TIMER), TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); // set timer mode, use undivided timer clock, edge alignment (simple count), and count up - timer_set_prescaler(TIM(ONEWIRE_SLAVE_TIMER), 1-1); // don't use prescale since this 16 bits timer allows to wait > 480 us used for the reset pulse ( 1/(72E6/1/(2**16))=910us ) + timer_set_prescaler(TIM(ONEWIRE_SLAVE_TIMER), 1 - 1); // don't use prescale since this 16 bits timer allows to wait > 480 us used for the reset pulse ( 1/(72E6/1/(2**16))=910us ) // use comparator to time signal (without using the output), starting at slot start - timer_set_period(TIM(ONEWIRE_SLAVE_TIMER), 480*(rcc_ahb_frequency/1000000)-1-1300); // minimum time needed for a reset pulse (480 < Trst), plus hand tuning + timer_set_period(TIM(ONEWIRE_SLAVE_TIMER), 480 * (rcc_ahb_frequency / 1000000) - 1 - 1300); // minimum time needed for a reset pulse (480 < Trst), plus hand tuning timer_set_oc_mode(TIM(ONEWIRE_SLAVE_TIMER), TIM_OC1, TIM_OCM_FROZEN); - timer_set_oc_value(TIM(ONEWIRE_SLAVE_TIMER), TIM_OC1, 16*(rcc_ahb_frequency/1000000)-1); // time to wait before sending the presence pulse, after the rising edge of the reset pulse (15 < Tpdh < 60) + timer_set_oc_value(TIM(ONEWIRE_SLAVE_TIMER), TIM_OC1, 16 * (rcc_ahb_frequency / 1000000) - 1); // time to wait before sending the presence pulse, after the rising edge of the reset pulse (15 < Tpdh < 60) timer_set_oc_mode(TIM(ONEWIRE_SLAVE_TIMER), TIM_OC2, TIM_OCM_FROZEN); - timer_set_oc_value(TIM(ONEWIRE_SLAVE_TIMER), TIM_OC2, 45*(rcc_ahb_frequency/1000000)-1-350); // time to sample the bit after being set (1 < Tlow1 < 15, 60 < Tslot < 120), or stop sending the bit use compare function to detect slave presence (15 = Trdv + 0 < Trelease < 45), plus hand tuning - timer_set_oc_value(TIM(ONEWIRE_SLAVE_TIMER), TIM_OC3, 90*(rcc_ahb_frequency/1000000)-1); // time to stop the presence pulse (60 < Tpdl < 120) + timer_set_oc_value(TIM(ONEWIRE_SLAVE_TIMER), TIM_OC2, 45 * (rcc_ahb_frequency / 1000000) - 1 - 350); // time to sample the bit after being set (1 < Tlow1 < 15, 60 < Tslot < 120), or stop sending the bit use compare function to detect slave presence (15 = Trdv + 0 < Trelease < 45), plus hand tuning + timer_set_oc_value(TIM(ONEWIRE_SLAVE_TIMER), TIM_OC3, 90 * (rcc_ahb_frequency / 1000000) - 1); // time to stop the presence pulse (60 < Tpdl < 120) timer_clear_flag(TIM(ONEWIRE_SLAVE_TIMER), TIM_SR_UIF | TIM_SR_CC1IF | TIM_SR_CC2IF | TIM_SR_CC3IF); // clear all interrupt flags timer_update_on_overflow(TIM(ONEWIRE_SLAVE_TIMER)); // only use counter overflow as UEV source (use overflow as start time or timeout) timer_enable_irq(TIM(ONEWIRE_SLAVE_TIMER), TIM_DIER_UIE); // enable update interrupt for overflow @@ -156,13 +156,13 @@ void onewire_slave_setup(uint8_t family, uint64_t serial) bool onewire_slave_function_read(uint8_t* data, size_t size) { - if (NULL==data || 0==size) { // verify input + if (NULL == data || 0 == size) { // verify input return false; } - if (UINT32_MAX/87) { // complete ROM command code received + if (bits_bit > 7) { // complete ROM command code received bits_bit = 0; // reset buffer rom_code_byte = 0; // reset ROM code byte index switch (bits_buffer) { // act depending on ROM command code @@ -322,7 +322,7 @@ void TIM_ISR(ONEWIRE_SLAVE_TIMER)(void) } break; case ONEWIRE_STATE_ROM_READ: // send ROM code - if (bits_bit>7) { // complete byte transmitted + if (bits_bit > 7) { // complete byte transmitted rom_code_byte++; // go to next ROM code byte if (rom_code_byte>LENGTH(onewire_slave_rom_code)) { // complete ROM code send onewire_slave_state = ONEWIRE_STATE_IDLE; // go back to default idle state @@ -333,11 +333,11 @@ void TIM_ISR(ONEWIRE_SLAVE_TIMER)(void) } break; case ONEWIRE_STATE_ROM_MATCH: // compare ROM code - if (bits_bit>7) { // complete byte received - if (bits_buffer==onewire_slave_rom_code[rom_code_byte]) { // ROM code byte matches + if (bits_bit > 7) { // complete byte received + if (bits_buffer == onewire_slave_rom_code[rom_code_byte]) { // ROM code byte matches bits_bit = 0; // reset buffer rom_code_byte++; // go to next ROM code byte - if (rom_code_byte>=LENGTH(onewire_slave_rom_code)) { // complete ROM code matches + if (rom_code_byte >= LENGTH(onewire_slave_rom_code)) { // complete ROM code matches onewire_slave_state = ONEWIRE_STATE_FUNCTION_COMMAND; // now read function command code } } else { // ROM code does not match @@ -346,22 +346,22 @@ void TIM_ISR(ONEWIRE_SLAVE_TIMER)(void) } break; case ONEWIRE_STATE_ROM_SEARCH_TRUE: // ROM code bit is send, prepare to send negated version - bits_buffer ^= (1<7) { // complete byte searched + if (bits_bit > 7) { // complete byte searched bits_bit = 0; // reset buffer rom_code_byte++; // go to next ROM code byte - if (rom_code_byte>=LENGTH(onewire_slave_rom_code)) { // complete ROM code search + if (rom_code_byte >= LENGTH(onewire_slave_rom_code)) { // complete ROM code search onewire_slave_state = ONEWIRE_STATE_FUNCTION_COMMAND; // now read function command code } else { bits_buffer = onewire_slave_rom_code[rom_code_byte]; // prepare next ROM code byte @@ -369,27 +369,27 @@ void TIM_ISR(ONEWIRE_SLAVE_TIMER)(void) } break; case ONEWIRE_STATE_FUNCTION_COMMAND: // read function command - if (bits_bit>7) { // complete function command code received + if (bits_bit > 7) { // complete function command code received onewire_slave_function_code = bits_buffer; // save function command code to user buffer onewire_slave_state = ONEWIRE_STATE_FUNCTION_DATA; // let the user transfer data onewire_slave_function_code_received = true; // notify user } break; case ONEWIRE_STATE_FUNCTION_READ: // save function data bit - if (0==bits_bit%8) { // complete byte received - onewire_slave_transfer_data[(bits_bit-1)/8] = bits_buffer; // save received bytes + if (0 == bits_bit % 8) { // complete byte received + onewire_slave_transfer_data[(bits_bit - 1) / 8] = bits_buffer; // save received bytes } - if (bits_bit>=onewire_slave_transfer_bits) { // read transfer complete - onewire_slave_transfer_data[(bits_bit-1)/8] = bits_buffer; // save last bits + if (bits_bit >= onewire_slave_transfer_bits) { // read transfer complete + onewire_slave_transfer_data[(bits_bit - 1) / 8] = bits_buffer; // save last bits onewire_slave_state = ONEWIRE_STATE_FUNCTION_DATA; // let the user transfer more data onewire_slave_transfer_complete = true; // notify user } break; case ONEWIRE_STATE_FUNCTION_WRITE: // update function data bit to write - if (0==bits_bit%8) { // complete byte transfer - bits_buffer = onewire_slave_transfer_data[bits_bit/8]; // prepare next byte to write + if (0 == bits_bit % 8) { // complete byte transfer + bits_buffer = onewire_slave_transfer_data[bits_bit / 8]; // prepare next byte to write } - if (bits_bit>=onewire_slave_transfer_bits) { // write transfer complete + if (bits_bit >= onewire_slave_transfer_bits) { // write transfer complete onewire_slave_state = ONEWIRE_STATE_FUNCTION_DATA; // let the user transfer more data onewire_slave_transfer_complete = true; // notify user } @@ -401,7 +401,7 @@ void TIM_ISR(ONEWIRE_SLAVE_TIMER)(void) } if (timer_interrupt_source(TIM(ONEWIRE_SLAVE_TIMER), TIM_SR_CC3IF)) { // end of presence pulse timer triggered timer_clear_flag(TIM(ONEWIRE_SLAVE_TIMER), TIM_SR_CC3IF); // clear flag - if (ONEWIRE_STATE_PULSE_PRESENCE==onewire_slave_state) { + if (ONEWIRE_STATE_PULSE_PRESENCE == onewire_slave_state) { gpio_set(GPIO(ONEWIRE_SLAVE_PORT), GPIO(ONEWIRE_SLAVE_PIN)); // stop sending presence pulse // if the pin stays low the reset timer will catch it } diff --git a/lib/onewire_slave.h b/lib/onewire_slave.h index af05eb4..94450cd 100644 --- a/lib/onewire_slave.h +++ b/lib/onewire_slave.h @@ -12,10 +12,10 @@ * along with this program. If not, see . * */ -/** library for 1-wire protocol as slave (API) - * @file onewire_slave.h +/** library for 1-wire protocol as slave + * @file * @author King Kévin - * @date 2017 + * @date 2017-2020 * @note peripherals used: timer @ref onewire_slave_timer, GPIO @ref onewire_slave_gpio * @note overdrive mode is not supported */