1#include "main-header.h"
9#pragma GCC optimize ("O0")
11#define ONEWIRE_TIMER 5
24#define ONEWIRE_INTERVAL 600
25static volatile uint32_t usctr;
30static int freq_divider = 0;
40static inline void read_buf(
struct onewire_hwdef *hw,
int cnt);
47static int is_valid(
byte *recvBuf) {
48 int crc_calc = onewire_crc(recvBuf, 8);
49 int crc_read = recvBuf[8];
51 return (crc_calc == crc_read) ? 1 : 0;
54static void print_recvbuf(
byte *recvBuf) {
57 bufToHex((
char *)&hexbuf, recvBuf, 10);
58 printf(
"Recv: %s", hexbuf);
59 int crc_calc = onewire_crc(recvBuf, 8);
60 int crc_read = recvBuf[8];
62 printf(
"(CRC calc: 0x%x, read: 0x%x) %s\r\n", crc_calc, crc_read,
63 (crc_calc == crc_read) ?
"OK" :
"FAIL");
69volatile uint32_t *r_adr = (
volatile uint32_t *)0x40020810;
70static inline byte read_pin(
int mask) __attribute__ ((always_inline));
72static inline byte read_pin(
int mask) {
75 return ((*r_adr) & mask) ? 1 : 0;
81static inline void pull_low() __attribute__ ((always_inline));
83volatile uint32_t *wr_adr = (volatile uint32_t *)0x40020814;
84static inline
void pull_low(
int offset,
int us) {
85 int delayus = (us == 0) ? 0 : (us / freq_divider);
87 (*wr_adr) &= ~(1 << offset);
90 while (delayus > usctr) {
92 (*wr_adr) |= 1 << offset;
98static void delay_us(
int us) {
99 int delayus = us / freq_divider;
102 while (usctr < delayus) {
108static void ow_timer_irq() {
114static int timer_enable() {
117 int freqreq = 100000;
120 r = mculib_timer_enable_simple(MAIN_MCULIBHANDLE, ONEWIRE_TIMER, freqreq, &ow_timer_irq);
122 mculib_print_mappings(&printf);
123 printf(
"onewire MCULIB Error (failed to enable timer %i): %i\r\n", ONEWIRE_TIMER, r);
125 printf(
"Timer %i enabled with frequency %i (divider to us: %i)\r\n", ONEWIRE_TIMER, freqreq, freq_divider);
132static int read_temperature(
struct onewire_hwdef *hw,
byte *error) {
139 if ((r = mculib_pin_out_opendrain(MAIN_MCULIBHANDLE, hw->pin, HAL_PIN_FASTEST))) {
140 printf(
"Failed to configure pin %i for opendrain output: %i\r\n", hw->pin, r);
154 printf(
"No onewire device detected. abort\r\n");
159 printf(
"Onewire device detected.\r\n");
160 for (repeat = 0; repeat < 5; repeat++) {
161 write_byte(hw, 0x33);
166 write_byte(hw, 0xCC);
169 write_byte(hw, 0x44);
173 write_byte(hw, 0xCC);
175 write_byte(hw, 0xBE);
178 if (is_valid((
byte *)buf)) {
182 if (!is_valid((
byte *)buf)) {
187 uint16_t tempRaw = ((uint16_t)buf[1]) << 8 | buf[0];
190 if (tempRaw & (1 << 15)) {
192 tempRaw = 0xFFFF - tempRaw;
194 int32_t temp_c100 = (6 * ((int32_t)tempRaw)) + (tempRaw / 4);
197 temp_c100 = 0 - temp_c100;
199 printf(
"Temp: %i, %i\r\n", (
int)tempRaw, (
int)temp_c100);
202 mculib_timer_disable(MAIN_MCULIBHANDLE, ONEWIRE_TIMER);
203 mculib_pin_release(MAIN_MCULIBHANDLE, hw->pin);
204 mculib_pin_out(MAIN_MCULIBHANDLE, hw->pin, HAL_PIN_SLOWEST);
205 mculib_pin_release(MAIN_MCULIBHANDLE, hw->pin);
214 pull_low(hw->offset, 500);
219 while (usctr < (480)) {
220 r = read_pin(1 << hw->offset);
226 printf(
"Error reading pin: %i\r\n", r);
234 if ((low == 0) || (high == 0)) {
243 for (i = 0; i < 8; i++) {
244 byte b = read_bit(hw);
253 for (i = 0; i < 8; i++) {
264 pull_low(hw->offset, 0);
269 pull_low(hw->offset, 61);
277 int sampletime = 15 / freq_divider;
281 pull_low(hw->offset, 5);
283 val = read_pin(1 << hw->offset);
284 while (usctr < sampletime) {
286 val = val + read_pin(1 << hw->offset);
290 return (val > 0) ? 1 : 0;
293static inline void read_buf(
struct onewire_hwdef *hw,
int cnt) {
296 for (i = 0; i <
sizeof(buf); i++) {
299 for (i = 0; i < cnt; i++) {
300 byte b = read_byte(hw);
303 print_recvbuf((
byte *)buf);
312 for (i = 0; i < 5; i++) {
313 temp = read_temperature(hw, error);
314 printf(
"Temperature (pin %i): %i\r\n", hw->pin, temp);
315 if ((temp > 8500) || (*error != 0)) {
316 printf(
"Onewire above threshold - failed! \r\n");
331int onewire_run(
byte idx,
byte *error) {
335 if ((idx == 212) && (CONFIG_FLAGS_TEMP212)) {
338 return query_onewire(&hw, error);
341 if ((idx == 207) && (CONFIG_FLAGS_TEMP207)) {
344 return query_onewire(&hw, error);
347 if ((idx == 211) && (CONFIG_FLAGS_TEMP211)) {
350 return query_onewire(&hw, error);
355int onewire_reading_size(
byte idx) {
359int onewire_init(
byte idx) {