struct completion rx_byte_received; /* * protect rx_err and rx_byte from concurrent access in * w1-callbacks and serdev-receive.
*/ struct mutex rx_mutex; int rx_err;
u8 rx_byte;
};
/** * struct w1_uart_limits - limits for 1-Wire operations * @baudrate: Requested baud-rate to create 1-Wire timing pattern * @bit_min_us: minimum time for a bit (in us) * @bit_max_us: maximum time for a bit (in us) * @sample_us: timespan to sample 1-Wire response * @cycle_us: duration of the 1-Wire cycle
*/ struct w1_uart_limits { unsignedint baudrate; unsignedint bit_min_us; unsignedint bit_max_us; unsignedint sample_us; unsignedint cycle_us;
};
staticinlineunsignedint to_ns(unsignedint us)
{ return us * NSEC_PER_USEC;
}
/* * Set baud-rate, delay and tx-byte to create a 1-Wire pulse and adapt * the tx-byte according to the actual baud-rate. * * Reject when: * - time for a bit outside min/max range * - a 1-Wire response is not detectable for sent byte
*/ staticint w1_uart_set_config(struct serdev_device *serdev, conststruct w1_uart_limits *limits, struct w1_uart_config *w1cfg)
{ unsignedint packet_ns; unsignedint bits_low; unsignedint bit_ns; unsignedint low_ns;
w1cfg->baudrate = serdev_device_set_baudrate(serdev, limits->baudrate); if (w1cfg->baudrate == 0) return -EINVAL;
/* Compute in nanoseconds for accuracy */
bit_ns = baud_to_bit_ns(w1cfg->baudrate);
bits_low = to_ns(limits->bit_min_us) / bit_ns; /* start bit is always low */
low_ns = bit_ns * (bits_low + 1);
if (low_ns < to_ns(limits->bit_min_us)) return -EINVAL;
if (low_ns > to_ns(limits->bit_max_us)) return -EINVAL;
/* 1-Wire response detectable for sent byte */ if (limits->sample_us > 0 &&
bit_ns * BITS_PER_BYTE < low_ns + to_ns(limits->sample_us)) return -EINVAL;
/* delay: 1-Wire cycle takes longer than the UART packet */
packet_ns = bit_ns * W1_UART_BITS_PER_PACKET;
w1cfg->delay_us = 0; if (to_ns(limits->cycle_us) > packet_ns)
w1cfg->delay_us =
(to_ns(limits->cycle_us) - packet_ns) / NSEC_PER_USEC;
/* * 1-wire reset and presence detect: A present slave will manipulate * the received byte by pulling the 1-Wire low.
*/ static u8 w1_uart_reset_bus(void *data)
{ struct w1_uart_device *w1dev = data; conststruct w1_uart_config *w1cfg = &w1dev->cfg_reset; int ret;
u8 val;
ret = w1_uart_serdev_tx_rx(w1dev, w1cfg, &val); if (ret < 0) return -1;
/* Device present (0) or no device (1) */ return val != w1cfg->tx_byte ? 0 : 1;
}
/* * 1-Wire read and write cycle: Only the read-0 manipulates the * received byte, all others left the line untouched.
*/ static u8 w1_uart_touch_bit(void *data, u8 bit)
{ struct w1_uart_device *w1dev = data; conststruct w1_uart_config *w1cfg = bit ? &w1dev->cfg_touch_1 :
&w1dev->cfg_touch_0; int ret;
u8 val;
ret = w1_uart_serdev_tx_rx(w1dev, w1cfg, &val);
/* return inactive bus state on error */ if (ret < 0) return 1;
/* * Waits until w1-uart callbacks are finished, serdev is closed * and its device data released automatically by devres (waits * until serdev-receive is finished).
*/
w1_remove_master_device(&w1dev->bus);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.