mirror of
https://github.com/sipeed/Maixduino.git
synced 2026-03-03 09:04:00 +01:00
Add pulseIn
set baudrate to 2M can't work Signed-off-by: Bigbits <btx000@qq.com>
This commit is contained in:
53
cores/arduino/wiring_pulse.c
Normal file
53
cores/arduino/wiring_pulse.c
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
Copyright (c) 2011 Arduino. All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
See the GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "wiring_private.h"
|
||||
#include "sysctl.h"
|
||||
|
||||
|
||||
/* Measures the length (in microseconds) of a pulse on the pin; state is HIGH
|
||||
* or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds
|
||||
* to 3 minutes in length, but must be called at least a few dozen microseconds
|
||||
* before the start of the pulse. */
|
||||
extern uint32_t pulseIn(uint32_t pin, bool state, uint32_t timeout)
|
||||
{
|
||||
uint32_t cpu_freq = sysctl_clock_get_freq(SYSCTL_CLOCK_CPU);
|
||||
uint32_t numloops = 0;
|
||||
uint32_t maxloops = timeout * (cpu_freq / 1000000L) / 16; // microsecondsToClockCycles(timeout) / 16; ???
|
||||
uint32_t start, end;
|
||||
|
||||
// wait for any previous pulse to end
|
||||
while (digitalRead(pin) == state)
|
||||
if (numloops++ == maxloops)
|
||||
return 0;
|
||||
|
||||
// wait for the pulse to start
|
||||
while (digitalRead(pin) != state)
|
||||
if (numloops++ == maxloops)
|
||||
return 0;
|
||||
|
||||
// wait for the pulse to stop
|
||||
start = micros();
|
||||
while (digitalRead(pin) == state)
|
||||
if (numloops++ == maxloops)
|
||||
return 0;
|
||||
end = micros();
|
||||
|
||||
return (end - start);
|
||||
}
|
||||
40
cores/arduino/wiring_pulse.h
Normal file
40
cores/arduino/wiring_pulse.h
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
Copyright (c) 2011 Arduino. All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
See the GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef _WIRING_PULSE_
|
||||
#define _WIRING_PULSE_
|
||||
|
||||
#include<stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"{
|
||||
#endif // __cplusplus
|
||||
|
||||
/*
|
||||
* \brief Measures the length (in microseconds) of a pulse on the pin; state is HIGH
|
||||
* or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds
|
||||
* to 3 minutes in length, but must be called at least a few dozen microseconds
|
||||
* before the start of the pulse.
|
||||
*/
|
||||
extern uint32_t pulseIn(uint32_t ulPin, bool ulState, uint32_t ulTimeout = 1000000L);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif // __cplusplus
|
||||
|
||||
#endif /* _WIRING_PULSE_ */
|
||||
@@ -98,5 +98,5 @@ tools.manual_openocd.program.pattern="{path}{cmd}" -d0 -f {program.config} -c "f
|
||||
tools.kflash.path={runtime.tools.kflash.path}/
|
||||
tools.kflash.cmd={runtime.tools.kflash.path}/kflash
|
||||
tools.kflash.bootloader.path={runtime.tools.kflash.path}/bootloader.bin
|
||||
tools.kflash.upload.pattern="{cmd}" -p {serial.port} -l "{bootloader.path}" -b 2000000 {build.path}/{build.project_name}.bin
|
||||
tools.kflash.program.pattern="{cmd}" -p {serial.port} -l "{bootloader.path}" -b 2000000 {build.path}/{build.project_name}.bin
|
||||
tools.kflash.upload.pattern="{cmd}" -p {serial.port} -l "{bootloader.path}" {build.path}/{build.project_name}.bin
|
||||
tools.kflash.program.pattern="{cmd}" -p {serial.port} -l "{bootloader.path}" {build.path}/{build.project_name}.bin
|
||||
Reference in New Issue
Block a user