chore: onewire

This commit is contained in:
2024-11-11 01:31:19 +06:00
parent 39f7755477
commit 8fe50deeed
16 changed files with 1180 additions and 66 deletions

98
lib/onewire/onewire.h Normal file
View File

@@ -0,0 +1,98 @@
#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 __attribute__((always_inline)) void directModeInput() {
GPIOB->CFGHR &= ~(0xF << (4 * (9 - 8)));
GPIOB->CFGHR |= (0x4 << (4 * (9 - 8)));
}
static inline __attribute__((always_inline)) void directModeOutput() {
GPIOB->CFGHR &= ~(0xF << (4 * (9 - 8)));
GPIOB->CFGHR |= (0x3 << (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
// 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