/*
 * @brief   blocking call to measure a high or low pulse
 * @returns uint32_t pulse width in microseconds up to 3 seconds,
 *          returns 0 on 3 second timeout error, or invalid pin.
 */
uint32_t HAL_Pulse_In(pin_t pin, uint16_t value)
{
    #define pinReadFast(_pin) ((PIN_MAP[_pin].gpio_peripheral->IDR & PIN_MAP[_pin].gpio_pin) == 0 ? 0 : 1)

    volatile uint32_t timeoutStart = SYSTEM_TICK_COUNTER; // total 3 seconds for entire function!

    /* If already on the value we want to measure, wait for the next one.
     * Time out after 3 seconds so we don't block the background tasks
     */
    while (pinReadFast(pin) == value) {
        if (SYSTEM_TICK_COUNTER - timeoutStart > 216000000UL) {
            return 0;
        }
    }

    /* Wait until the start of the pulse.
     * Time out after 3 seconds so we don't block the background tasks
     */
    while (pinReadFast(pin) != value) {
        if (SYSTEM_TICK_COUNTER - timeoutStart > 216000000UL) {
            return 0;
        }
    }

    /* Wait until this value changes, this will be our elapsed pulse width.
     * Time out after 3 seconds so we don't block the background tasks
     */
    volatile uint32_t pulseStart = SYSTEM_TICK_COUNTER;
    while (pinReadFast(pin) == value) {
        if (SYSTEM_TICK_COUNTER - timeoutStart > 216000000UL) {
            return 0;
        }
    }

    return (SYSTEM_TICK_COUNTER - pulseStart)/SYSTEM_US_TICKS;
}
예제 #2
0
uint8_t RFID::softSPITranser(uint8_t data) {

  uint8_t b=0;

  for (uint8_t bit = 0; bit < 8; bit++) {
    if (data & (1 << (7-bit)))		// walks down mask from bit 7 to bit 0
      pinSetFast(_mosiPin); // Data High
    else
      pinResetFast(_mosiPin); // Data Low
		
    pinSetFast(_clockPin); // Clock High

    b <<= 1;
    if (pinReadFast(_misoPin))
      b |= 1;

    pinResetFast(_clockPin); // Clock Low
  }
  return b;

}
예제 #3
0
uint8_t Sd2Card::sparkSPISend(uint8_t data) {
	uint8_t b=0;

	if (SPImode_) {				// SPI Mode is Hardware so use Spark SPI function
		b = SPI.transfer(data);
	}
	else {						// SPI Mode is Software so use bit bang method
		for (uint8_t bit = 0; bit < 8; bit++)  {
			if (data & (1 << (7-bit)))		// walks down mask from bit 7 to bit 0
				pinSetFast(mosiPin_); // Data High
			else
				pinResetFast(mosiPin_); // Data Low
			
			pinSetFast(clockPin_); // Clock High

			b <<= 1;
			if (pinReadFast(misoPin_))
				b |= 1;

			pinResetFast(clockPin_); // Clock Low
		}
	}
	return b;
}