我正在尝试为 ChibiOS 实现“单线”,在 atmega1280(Arduino 板)上运行。我主要是从 arduino 实现中复制/粘贴(效果很好):
uint8_t OneWire::reset(void)
{
IO_REG_TYPE mask = bitmask;
volatile IO_REG_TYPE *reg IO_REG_ASM = baseReg;
uint8_t r;
uint8_t retries = 125;
noInterrupts();
DIRECT_MODE_INPUT(reg, mask);
interrupts();
// wait until the wire is high... just in case
do {
if (--retries == 0) return 0;
delayMicroseconds(2);
} while ( !DIRECT_READ(reg, mask));
noInterrupts();
DIRECT_WRITE_LOW(reg, mask);
DIRECT_MODE_OUTPUT(reg, mask); // drive output low
interrupts();
delayMicroseconds(480);
noInterrupts();
DIRECT_MODE_INPUT(reg, mask); // allow it to float
delayMicroseconds(70);
r = !DIRECT_READ(reg, mask);
interrupts();
delayMicroseconds(410);
return r;
}
我为“重置”功能的 ChibiOS 实现编写的内容如下:
uint8_t OneWire::reset(void)
{
palSetPadMode((ioportid_t)IOPORT2, 3, PAL_MODE_INPUT);
// Wait until the wire is high.
uint8_t retries = 125;
do {
if (--retries == 0) {
return 0;
}
chThdSleepMicroseconds(2);
} while (palReadPad(IOPORT2, 3) == PAL_LOW);
palSetPadMode((ioportid_t)IOPORT2, 3, PAL_MODE_OUTPUT_PUSHPULL);
palWritePad(IOPORT2, 3, PAL_LOW); // drive output low
chThdSleepMicroseconds(480);
palSetPadMode((ioportid_t)IOPORT2, 3, PAL_MODE_INPUT);
chThdSleepMicroseconds(70);
uint8_t result = (palReadPad(IOPORT2, 3) == PAL_LOW);
chThdSleepMicroseconds(410);
return result;
}
我究竟做错了什么?