#ifndef CH32V003_ONEWIRE_H #define CH32V003_ONEWIRE_H #include #include #include "ch32v003fun.h" // GPIO Direct Access Definitions #ifndef OneWire_Direct_GPIO_h #define OneWire_Direct_GPIO_h static inline __attribute__((always_inline)) uint8_t directRead() { return (GPIOB->INDR & (1 << 9)) ? 1 : 0; } static inline void directModeInput() { GPIOB->CFGHR &= ~(0xF << (4 * (9 - 8))); GPIOB->CFGHR |= (GPIO_CNF_IN_FLOATING << (4 * (9 - 8))); } static inline void directModeOutput() { GPIOB->CFGHR &= ~(0xF << (4 * (9 - 8))); GPIOB->CFGHR |= ((GPIO_Speed_50MHz | GPIO_CNF_OUT_PP) << (4 * (9 - 8))); } static inline __attribute__((always_inline)) void directWriteLow() { GPIOB->BCR = (1 << 9); } static inline __attribute__((always_inline)) void directWriteHigh() { GPIOB->BSHR = (1 << 9); } #define DIRECT_READ() directRead() #define DIRECT_WRITE_LOW() directWriteLow() #define DIRECT_WRITE_HIGH() directWriteHigh() #define DIRECT_MODE_INPUT() directModeInput() #define DIRECT_MODE_OUTPUT() directModeOutput() #endif // timing configuration // time between line check retries #define ONEWIRE_RESET_RETRY_TIME 2 // reset cycle #define ONEWIRE_RESET_LOW_TIME 480 #define ONEWIRE_RESET_SAMPLE_TIME 60 #define ONEWIRE_RESET_POST_TIME 410 // write 1 bit #define ONEWIRE_WRITE_1_LOW_TIME 6 #define ONEWIRE_WRITE_1_TOTAL_TIME 64 // write 0 bit #define ONEWIRE_WRITE_0_LOW_TIME 80 #define ONEWIRE_WRITE_0_TOTAL_TIME 84 // read bit #define ONEWIRE_READ_INIT_LOW_TIME 6 #define ONEWIRE_READ_SAMPLE_TIME 8 #define ONEWIRE_READ_TOTAL_TIME 64 // OneWire Function Declarations // Initialize the OneWire bus void OneWireBegin(void); // Perform a 1-Wire reset cycle. Returns 1 if a device responds with a presence // pulse uint8_t OneWireReset(void); // Select a device on the bus using its ROM code void OneWireSelect(const uint8_t rom[8]); // Skip ROM selection - addresses all devices on the bus void OneWireSkip(void); // Write a byte to the bus. If 'power' is true, maintain strong pullup after // write void OneWireWrite(uint8_t v, uint8_t power); // Write multiple bytes to the bus void OneWireWriteBytes(const uint8_t *buf, uint16_t count, bool power); // Read a byte from the bus uint8_t OneWireRead(void); // Read multiple bytes from the bus void OneWireReadBytes(uint8_t *buf, uint16_t count); // Write a single bit to the bus void OneWireWriteBit(uint8_t v); // Read a single bit from the bus uint8_t OneWireReadBit(void); // Stop forcing power onto the bus void OneWireDepower(void); // Reset the search state void OneWireResetSearch(void); // Setup search to find devices of a specific family code void OneWireTargetSearch(uint8_t family_code); // Search for the next device on the bus bool OneWireSearch(uint8_t *newAddr, bool search_mode); // Calculate 8-bit CRC uint8_t OneWireCrc8(const uint8_t *addr, uint8_t len); // Check if received CRC matches calculated CRC bool OneWireCheckCrc16(const uint8_t *input, uint16_t len, const uint8_t *inverted_crc, uint16_t crc); // Calculate 16-bit CRC uint16_t OneWireCrc16(const uint8_t *input, uint16_t len, uint16_t crc); #endif // CH32V003_ONEWIRE_H