44 #include <px4_platform_common/px4_config.h> 45 #include <px4_platform_common/time.h> 46 #include <nuttx/arch.h> 47 #include <nuttx/irq.h> 49 #include <sys/types.h> 62 #include <board_config.h> 68 #include "up_internal.h" 71 #include "stm32_gpio.h" 72 #include "stm32_tim.h" 82 #include <sys/types.h> 87 #define GPIO_VDD_RANGEFINDER_EN GPIO_GPIO5_OUTPUT 89 #if HRT_TIMER == PWMIN_TIMER 90 #error cannot share timer between HRT and PWMIN 93 #if !defined(GPIO_PWM_IN) || !defined(PWMIN_TIMER) || !defined(PWMIN_TIMER_CHANNEL) 94 #error PWMIN defines are needed in board_config.h for this board 98 #define INPUT_TIMER PWMIN_TIMER 100 #define PWMIN_TIMER_BASE TIMER_BASE 101 #define PWMIN_TIMER_CLOCK TIMER_CLOCK 102 #define PWMIN_TIMER_POWER_REG TIMER_CLOCK_POWER_REG 103 #define PWMIN_TIMER_POWER_BIT TIMER_CLOCK_POWER_BIT 104 #define PWMIN_TIMER_VECTOR TIMER_IRQ_REG 109 #if PWMIN_TIMER_CLOCK <= 1000000 110 # error PWMIN_TIMER_CLOCK must be greater than 1MHz 116 #define REG(_reg) (*(volatile uint32_t *)(PWMIN_TIMER_BASE + _reg)) 118 #define rCR1 REG(STM32_GTIM_CR1_OFFSET) 119 #define rCR2 REG(STM32_GTIM_CR2_OFFSET) 120 #define rSMCR REG(STM32_GTIM_SMCR_OFFSET) 121 #define rDIER REG(STM32_GTIM_DIER_OFFSET) 122 #define rSR REG(STM32_GTIM_SR_OFFSET) 123 #define rEGR REG(STM32_GTIM_EGR_OFFSET) 124 #define rCCMR1 REG(STM32_GTIM_CCMR1_OFFSET) 125 #define rCCMR2 REG(STM32_GTIM_CCMR2_OFFSET) 126 #define rCCER REG(STM32_GTIM_CCER_OFFSET) 127 #define rCNT REG(STM32_GTIM_CNT_OFFSET) 128 #define rPSC REG(STM32_GTIM_PSC_OFFSET) 129 #define rARR REG(STM32_GTIM_ARR_OFFSET) 130 #define rCCR1 REG(STM32_GTIM_CCR1_OFFSET) 131 #define rCCR2 REG(STM32_GTIM_CCR2_OFFSET) 132 #define rCCR3 REG(STM32_GTIM_CCR3_OFFSET) 133 #define rCCR4 REG(STM32_GTIM_CCR4_OFFSET) 134 #define rDCR REG(STM32_GTIM_DCR_OFFSET) 135 #define rDMAR REG(STM32_GTIM_DMAR_OFFSET) 140 #if PWMIN_TIMER_CHANNEL == 1 141 #define rCCR_PWMIN_A rCCR1 142 #define DIER_PWMIN_A (GTIM_DIER_CC1IE) 143 #define SR_INT_PWMIN_A GTIM_SR_CC1IF 144 #define rCCR_PWMIN_B rCCR2 145 #define SR_INT_PWMIN_B GTIM_SR_CC2IF 146 #define CCMR1_PWMIN ((0x02 << GTIM_CCMR1_CC2S_SHIFT) | (0x01 << GTIM_CCMR1_CC1S_SHIFT)) 147 #define CCMR2_PWMIN 0 148 #define CCER_PWMIN (GTIM_CCER_CC2P | GTIM_CCER_CC1E | GTIM_CCER_CC2E) 149 #define SR_OVF_PWMIN (GTIM_SR_CC1OF | GTIM_SR_CC2OF) 150 #define SMCR_PWMIN_1 (0x05 << GTIM_SMCR_TS_SHIFT) 151 #define SMCR_PWMIN_2 ((0x04 << GTIM_SMCR_SMS_SHIFT) | SMCR_PWMIN_1) 152 #elif PWMIN_TIMER_CHANNEL == 2 153 #define rCCR_PWMIN_A rCCR2 154 #define DIER_PWMIN_A (GTIM_DIER_CC2IE) 155 #define SR_INT_PWMIN_A GTIM_SR_CC2IF 156 #define rCCR_PWMIN_B rCCR1 157 #define DIER_PWMIN_B GTIM_DIER_CC1IE 158 #define SR_INT_PWMIN_B GTIM_SR_CC1IF 159 #define CCMR1_PWMIN ((0x01 << GTIM_CCMR1_CC2S_SHIFT) | (0x02 << GTIM_CCMR1_CC1S_SHIFT)) 160 #define CCMR2_PWMIN 0 161 #define CCER_PWMIN (GTIM_CCER_CC1P | GTIM_CCER_CC1E | GTIM_CCER_CC2E) 162 #define SR_OVF_PWMIN (GTIM_SR_CC1OF | GTIM_SR_CC2OF) 163 #define SMCR_PWMIN_1 (0x06 << GTIM_SMCR_TS_SHIFT) 164 #define SMCR_PWMIN_2 ((0x04 << GTIM_SMCR_SMS_SHIFT) | SMCR_PWMIN_1) 166 #error PWMIN_TIMER_CHANNEL must be either 1 and 2. 170 #define TIMEOUT_POLL 300000 171 #define TIMEOUT_READ 200000 180 virtual int open(
struct file *filp);
181 virtual ssize_t
read(
struct file *filp,
char *buffer,
size_t buflen);
182 virtual int ioctl(
struct file *filp,
int cmd,
unsigned long arg);
184 void publish(uint16_t
status, uint32_t period, uint32_t pulse_width);
268 irqstate_t flags = px4_enter_critical_section();
271 px4_arch_configgpio(GPIO_PWM_IN);
287 rDIER = DIER_PWMIN_A;
291 rSMCR = SMCR_PWMIN_1;
292 rSMCR = SMCR_PWMIN_2;
305 rPSC = prescaler - 1;
320 px4_leave_critical_section(flags);
364 if (g_dev ==
nullptr) {
368 int ret = CDev::open(filp);
396 return CDev::ioctl(filp, cmd, arg);
409 unsigned count = buflen /
sizeof(
struct pwm_input_s);
426 return ret ? ret : -EAGAIN;
435 if (status & SR_OVF_PWMIN) {
457 printf(
"timer not started - try the 'test' command\n");
460 printf(
"count=%u period=%u width=%u\n",
474 uint32_t
period = rCCR_PWMIN_A;
480 if (g_dev !=
nullptr) {
481 g_dev->
publish(status, period, pulse_width);
492 if (g_dev !=
nullptr) {
493 errx(1,
"driver already started");
498 if (g_dev ==
nullptr) {
499 errx(1,
"driver allocation failed");
502 if (g_dev->
init() !=
OK) {
503 errx(1,
"driver init failed");
517 errx(1,
"Failed to open device");
522 printf(
"Showing samples for 5 seconds\n");
527 if (::
read(fd, &buf,
sizeof(buf)) ==
sizeof(buf)) {
528 printf(
"period=%u width=%u error_count=%u\n",
552 errx(1,
"Failed to open device");
556 errx(1,
"reset failed");
568 if (g_dev ==
nullptr) {
569 printf(
"driver not started\n");
579 PX4_ERR(
"unrecognized command, try 'start', 'info', 'reset' or 'test'");
592 const char *verb = argv[1];
597 if (!strcmp(verb,
"start")) {
604 if (!strcmp(verb,
"info")) {
611 if (!strcmp(verb,
"test")) {
618 if (!strcmp(verb,
"reset")) {
static struct vehicle_status_s status
API for the uORB lightweight object broker.
virtual ssize_t read(struct file *filp, char *buffer, size_t buflen)
hrt_call _hard_reset_call
hrt_abstime _last_read_time
High-resolution timer with callouts and timekeeping.
virtual int close(file_t *filep)
Handle a close of the device.
virtual int open(struct file *filp)
hrt_call _freeze_test_call
Generic device / sensor interface.
__EXPORT void hrt_call_after(struct hrt_call *entry, hrt_abstime delay, hrt_callout callout, void *arg)
Call callout(arg) after delay has elapsed.
Abstract class for any character device.
void init()
Activates/configures the hardware registers.
static hrt_abstime hrt_elapsed_time(const hrt_abstime *then)
Compute the delta between a timestamp taken in the past and now.
Simple error/warning functions, heavily inspired by the BSD functions of the same names...
__EXPORT void hrt_call_every(struct hrt_call *entry, hrt_abstime delay, hrt_abstime interval, hrt_callout callout, void *arg)
Call callout(arg) after delay, and then after every interval.
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
uint32_t _pulses_captured
ringbuffer::RingBuffer * _reports
hrt_abstime _last_poll_time
virtual int ioctl(struct file *filp, int cmd, unsigned long arg)
struct @83::@85::@87 file
#define SENSORIOCRESET
Reset the sensor to its default configuration.
CDev(const char *devname)
Constructor.
void publish(uint16_t status, uint32_t period, uint32_t pulse_width)
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).
Get architecture-specific timer register defines.