Files
ch32v203-eth-node/include/onewire.h
kuwoyuki 8adc726b0b chore: debloat (remove platformio), onewire improvements
Squashed commit of the following:

commit 5f16309f629b9928d2134b85ae64af69bc3ebbcd
Author: kuwoyuki <kuwoyuki@cock.li>
Date:   Sun Nov 24 22:55:15 2024 +0600

    fix: Makefile, improve onewire retries

commit 55496a3bda941b52ff349dc75c9c06eb5a37c07d
Author: kuwoyuki <kuwoyuki@cock.li>
Date:   Mon Nov 18 00:41:18 2024 +0600

    fix: make onewire validity less strict

commit 3428a9bc9792508972ce3e7e4e35a64f047bca10
Author: kuwoyuki <kuwoyuki@cock.li>
Date:   Sun Nov 17 23:57:55 2024 +0600

    chore: rm bins

commit 1594e5ed430522b15466c8afa62ff7fb1b28947c
Author: kuwoyuki <kuwoyuki@cock.li>
Date:   Sun Nov 17 23:32:01 2024 +0600

    chore: unplatformiofy
2024-11-24 22:56:05 +06:00

119 lines
3.1 KiB
C

#ifndef CH32V003_ONEWIRE_H
#define CH32V003_ONEWIRE_H
#include <stdbool.h>
#include <stdint.h>
#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