/* This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
/** library for 1-wire protocol as master (API)
 *  @file onewire_master.h
 *  @author King Kévin <kingkevin@cuvoodoo.info>
 *  @date 2017-2018
 *  @note peripherals used: timer @ref onewire_master_timer, GPIO @ref onewire_master_gpio
 *  @note overdrive mode is not provided
 */
#pragma once

/** @defgroup onewire_master_gpio GPIO used for 1-wire signal
 *  @note external pull-up resistor on pin is required (< 5 kOhm)
 *  @{
 */
#define ONEWIRE_MASTER_PORT C /**< GPIO port */
#define ONEWIRE_MASTER_PIN 9  /**< GPIO pin */
/** @} */

/** setup 1-wire peripheral
 */
void onewire_master_setup(void);
/** release 1-wire peripheral
 */
void onewire_master_release(void);
/** send reset pulse
 *  @return if slaves have indicated their presence
 */
bool onewire_master_reset(void);
/** compute CRC for 1-Wire
 *  @note this CRC-8 uses normal polynomial 0x31, reverse polynomial 0x8C, start value 0x00
 *  @param[in] data bytes on which to calculate CRC checksum on
 *  @param[in] length number of bytes in data
 *  @return computed CRC checksum
 */
uint8_t onewire_master_crc(uint8_t* data, uint32_t length);
/** send READ ROM command and read ROM code response
 *  @note user needs to send reset pulse before
 *  @return ROM code read
 */
uint64_t onewire_master_rom_read(void);
/** send SEARCH ROM command
 *  @note user needs to send reset pulse before
 *  @warning undefined behaviour if a ROM code different than the last found is provided
 *  @param[in,out] code use 0 to start search ROM code from scratch, or last know value to search next; writes back next ROM code found, or 0 if error occurred
 *  @param[in] alarm search only for ROM codes for slaves with an alarm flag set
 *  @return if an additional slave has been detected
 */
bool onewire_master_rom_search(uint64_t* code, bool alarm);
/** send SKIP ROM command (all slaves on the bus will be selected)
 *  @note user needs to send reset pulse before
 *  @return if operation succeeded
 */
bool onewire_master_rom_skip(void);
/** send MATCH ROM command to select a specific slave
 *  @note user needs to send reset pulse before
 *  @param[in] code ROM code of slave to select
 *  @return if operation succeeded
 */
bool onewire_master_rom_match(uint64_t code);
/** read data byte
 *  @note it is up to the user to send the reset pulse 
 *  @param[out] data buffer to save data read
 *  @return if operation succeeded
 */
bool onewire_master_read_byte(uint8_t* data);
/** write data byte
 *  @note it is up to the user to send the reset pulse 
 *  @param[in] data byte to write
 *  @return if operation succeeded
 */
bool onewire_master_write_byte(uint8_t data);
/** issue function and read data
 *  @note user needs to send a ROM command before
 *  @param[in] function function command to send
 *  @param[out] data buffer to save read bits (NULL if only the function command should be sent)
 *  @param[in] bits number of bits to read (0 if only the function command should be sent)
 *  @return if operation succeeded
 */
bool onewire_master_function_read(uint8_t function, uint8_t* data, uint32_t bits);
/** issue function and write data
 *  @note user needs to send a ROM command before
 *  @param[in] function function command to send
 *  @param[out] data data to write (NULL if only the function command should be sent)
 *  @param[in] bits number of bits to write (0 if only the function command should be sent)
 *  @return if operation succeeded
 */
bool onewire_master_function_write(uint8_t function, uint8_t* data, uint32_t bits);
