timer.c ( File view )

  • By gregkam 2014-10-06
  • View(s):4272
  • Download(s):1
  • Point(s): 1
			/*
 * timer.c --- STM32F4 tick timer and PPM decoder.
 *
 * Copyright (C) 2012, Galois, Inc.
 * All Rights Reserved.
 *
 * This software is released under the "BSD3" license.  Read the file
 * "LICENSE" for more information.
 */

#include <stdbool.h>
#include <stdint.h>
#include <stm32f4xx.h>

#include <FreeRTOS.h>
#include <task.h>

#include "hwf4/gpio.h"
#include "hwf4/interrupt.h"
#include "hwf4/timer.h"

/**
 * Timer to use for tick counting and PPM decoding.  We also define
 * the timer clock frequency here, and it must match the SYSCLK and
 * APB prescalar settings in the startup code.
 *
 * Note that the timer clock is 2x the APB peripheral clock when the
 * APB prescalar is not /1, according to the reference manual.
 *
 * On the PX4FMU, the PPM input is on TIM1_CH3, so we use TIM1 for
 * this, even though one of the less capable timers would do.  Also,
 * TIM1 has multiple IRQs that are shared with other timers, so this
 * starts to get rather complex.  For now, let's assume nobody is
 * using TIM10 so we don't need to worry about IRQ sharing.
 */
#define TIMER_DEV    TIM1
#define TIMER_F_CLK  168000000UL
#define TIMER_RCCDEV RCCDEV_TIM1

/** Capture/compare channel to use for PPM decoding. */
#define TIMER_PPM_CHANNEL 3
#define TIMER_PPM_PIN_AF  PIN_AF_TIM1

/**
 * Priority of timer interrupts.  This does not need to be a FreeRTOS
 * API safe priority.
 */
#define TIMER_IRQ_PRIORITY INTERRUPT_PRIORITY_HIGHEST

/* For timers with separate IRQs: */
#define TIMER_UP_IRQ TIM1_UP_TIM10_IRQn
#define TIMER_UP_ISR TIM1_UP_TIM10_IRQHandler
#define TIMER_CC_IRQ TIM1_CC_IRQn
#define TIMER_CC_ISR TIM1_CC_IRQHandler

/* For timers with a single IRQ: */
/* #define TIMER_IRQ TIMx_IRQn */
/* #define TIMER_ISR TIMx_IRQHandler */

/* Ensure that TIMER_F_CLK is a multiple of 1MHz. */
#if ((TIMER_F_CLK % 1000000) != 0)
# error "TIMER_F_CLK is not a multiple of 1MHz."
#endif

/** Prescalar value used to run a timer at 1MHz. */
#define TIMER_PRESCALAR (((TIMER_F_CLK) / 1000000UL) - 1)

/*
 * Capture/compare registers and bits for our PPM channel.
 *
 * Currently only channel 3 is supported, since that is where the PPM
 * pin is connected on the PX4IO board.
 *
 * Note also that CC1 is used to implement "timer_usleep".
 */
#if (TIMER_PPM_CHANNEL == 3)
# define PPM_PIN      pin_a10
# define CCR_PPM      CCR3
# define DIER_PPM     TIM_DIER_CC3IE
# define SR_CCIF_PPM  TIM_SR_CC3IF
# define SR_CCOF_PPM  TIM_SR_CC3OF
# define CCMR1_PPM    0
# define CCMR2_PPM    1
# define CCER_PPM     (TIM_CCER_CC3E | TIM_CCER_CC3P | TIM_CCER_CC3NP)
#else
# error "Unsupported timer PPM channel."
#endif

/**
 * Running tick count in microseconds.  This is incremented on each
 * timer overflow by 2**16 and should be added to TIMER_DEV->CNT to
 * get the current time in ticks.
 */
static volatile uint64_t g_ticks;

/** Boolean signalling completion of a microsecond delay. */
static volatile bool g_delay_complete;

/* PPM timings, in microseconds. */
#define PPM_LOW_MAX   500       /* max width of low pulse */
#define PPM_HIGH_MIN  800       /* min width of high data pulse */
#define PPM_HIGH_MAX  2200      /* max width of high data pulse */
#define PPM_START_MIN 2500      /* min width of start high pulse */

/** State of the PPM decoder. */
volatile struct {

  uint16_t last_edge;           /* last capture time */
  uint16_t last_mark;           /* last low->high edge */

  /* Sample buffer being written to from the decoder. */
  uint16_t current_buf[PPM_MAX_CHANNELS];
  size_t   current_len;

  /* Last full sample buffer returned to the caller. */
  uint16_t sample_buf[PPM_MAX_CHANNELS];
  size_t   sample_len;

  timer_tick_t sample_time;     /* sample timestamp */

  enum {

    SYNC = 0,                   /* waiting for start pulse */
    ARM,                        /* waiting for first low edge */
    ACTIVE,                     /* waiting for low edge */
    INACTIVE                    /* waiting for high edge */
  
} state;                      /* current decoder state */

} g_ppm_state;

void timer_init(void)
{

  g_ticks = 0;

  rcc_enable(TIMER_RCCDEV);

  /* Configure the PPM input pin for input capture. */
  pin_enable(PPM_PIN);
  pin_set_af(PPM_PIN, TIMER_PPM_PIN_AF);
  pin_set_pupd(PPM_PIN, PIN_PUPD_UP);
  pin_set_mode(PPM_PIN, PIN_MODE_AF);

#if 0
  /* Configure the debugger to freeze TIM1 when the core is halted. */
  DBGMCU->APB2FZ |= DBGMCU_APB1_FZ_DBG_TIM1_STOP;
#endif

  TIMER_DEV->CR1   = 0;
  TIMER_DEV->CR2   = 0;
  TIMER_DEV->SMCR  = 0;
  TIMER_DEV->DIER  = TIM_DIER_UIE | DIER_PPM;
  TIMER_DEV->CCER  = 0;
  TIMER_DEV->CCMR1 = CCMR1_PPM;
  TIMER_DEV->CCMR2 = CCMR2_PPM;
  TIMER_DEV->CCER  = CCER_PPM;
  TIMER_DEV->CNT   = 0;
  TIMER_DEV->PSC   = TIMER_PRESCALAR;
  TIMER_DEV->ARR   = 0xFFFF;

#if defined(TIMER_IRQ)          /* single IRQ timer */
  interrupt_set_priority(TIMER_IRQ, TIMER_IRQ_PRIORITY);
  interrupt_enable(TIMER_IRQ);
#else  /* multi-IRQ timer */
  interrupt_set_priority(TIMER_UP_IRQ, TIMER_IRQ_PRIORITY);
  interrupt_set_priority(TIMER_CC_IRQ, TIMER_IRQ_PRIORITY);
  interrupt_enable(TIMER_UP_IRQ);
  interrupt_enable(TIMER_CC_IRQ);
#endif

  TIMER_DEV->CR1 |= TIM_CR1_CEN;

}

uint64_t timer_get_ticks(void)
{

  uint64_t ticks;
  uint16_t cnt;

  /* We read "g_ticks" in a loop here to avoid running into an edge
   * case where the timer rolls over during the read.
   *
   * Without doing this, even if we disable the update interrupt, the
   * counter register will still reload and the total sum will be
   * incorrect. */
  do {

    ticks = g_ticks;
    cnt   = TIMER_DEV->CNT;
  
} while (ticks != g_ticks);

  return ticks + (uint64_t)cnt;

}

void timer_usleep(uint16_t delay)
{

  portENTER_CRITICAL();

  g_delay_complete = false;
  TIMER_DEV->CCR1   = (uint16_t)(TIMER_DEV->CNT + delay);
  TIMER_DEV->DIER  |= TIM_DIER_CC1IE;
  TIMER_DEV->CCMR1 |= TIM_CCMR1_OC1M_0;

  while (!g_delay_complete)
    ;

  portEXIT_CRITICAL();

}

void timer_msleep(uint32_t delay)
{

  vTaskDelay(delay / portTICK_RATE_MS);

}

bool timer_is_ppm_valid(void)
{

  uint16_t sample_len;

  portDISABLE_INTERRUPTS();
  sample_len = g_ppm_state.sample_len;
  portENABLE_INTERRUPTS();

  return sample_len != 0;

}

bool timer_get_ppm_channel(int channel, uint16_t *result)
{

  bool ret = false;

  portDISABLE_INTERRUPTS();

  if (channel < g_ppm_state.sample_len) {

    *result = g_ppm_state.sample_buf[channel];
    ret = true;
  
}

  portENABLE_INTERRUPTS();

  return ret;

}

size_t timer_get_ppm(uint16_t *buf, size_t len, timer_tick_t *time)
{

  size_t result = 0;
  size_t i;

  /*
   * We disable interrupts here so we do not capture the sample
   * buffer in an intermediate state during an update.
   *
   * XXX This might not actually disable the capture/compare interrupt
   * since the priority is above the FreeRTOS syscall priority.
   */
  portDISABLE_INTERRUPTS();

  for (i = 0; i < len && i < g_pp
...
...
(Please download the complete source code to view)
			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 1

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
01.97 kB
.gitignore19.00 B2013-04-23|01:09
01.97 kB
.gitignore8.00 B2013-04-23|01:09
build.mk1.82 kB2013-04-23|01:09
01.97 kB
default_handlers.c6.01 kB2013-04-23|01:09
default_hooks.c159.00 B2013-04-23|01:09
syscalls.c3.28 kB2013-04-23|01:09
Makefile327.00 B2013-04-23|01:09
README.md217.00 B2013-04-23|01:09
config.mk.example151.00 B2013-04-23|01:09
01.97 kB
FreeRTOSConfig.h8.59 kB2013-04-23|01:09
arm_common_tables.h1.09 kB2013-04-23|01:09
arm_math.h234.69 kB2013-04-23|01:09
core_cm0.h31.20 kB2013-04-23|01:09
core_cm3.h68.09 kB2013-04-23|01:09
core_cm4.h77.42 kB2013-04-23|01:09
core_cm4_simd.h23.42 kB2013-04-23|01:09
core_cmFunc.h15.32 kB2013-04-23|01:09
core_cmInstr.h15.73 kB2013-04-23|01:09
stm32f4xx.h507.96 kB2013-04-23|01:09
system_stm32f4xx.h2.17 kB2013-04-23|01:09
01.97 kB
01.97 kB
cortex-m4.mk646.00 B2013-04-23|01:09
01.97 kB
stm32f4.mk131.00 B2013-04-23|01:09
command.mk768.00 B2013-04-23|01:09
toolchain.mk150.00 B2013-04-23|01:09
01.97 kB
build.mk227.00 B2013-04-23|01:09
startup_stm32f4xx.s22.85 kB2013-04-23|01:09
system_stm32f4xx.c22.24 kB2013-04-23|01:09
01.97 kB
.gitignore8.00 B2013-04-23|01:09
build.mk1.04 kB2013-04-23|01:09
01.97 kB
01.97 kB
eeprom.h1.97 kB2013-04-23|01:09
gpio.h14.62 kB2013-04-23|01:09
i2c.h1.63 kB2013-04-23|01:09
interrupt.h1.61 kB2013-04-23|01:09
led.h904.00 B2013-04-23|01:09
rcc.h1.65 kB2013-04-23|01:09
spi.h3.46 kB2013-04-23|01:09
01.97 kB
irq.h6.68 kB2013-04-23|01:09
timer.h2.26 kB2013-04-23|01:09
usart.h3.38 kB2013-04-23|01:09
usb_cdc.h1.32 kB2013-04-23|01:09
01.97 kB
eeprom.c2.97 kB2013-04-23|01:09
fault.c1.38 kB2013-04-23|01:09
gpio.c5.12 kB2013-04-23|01:09
i2c.c9.10 kB2013-04-23|01:09
interrupt.c744.00 B2013-04-23|01:09
led.c2.11 kB2013-04-23|01:09
rcc.c11.74 kB2013-04-23|01:09
spi.c12.33 kB2013-04-23|01:09
timer.c10.08 kB2013-04-23|01:09
usart.c10.83 kB2013-04-23|01:09
usb_cdc.c1.21 kB2013-04-23|01:09
01.97 kB
gdb-init61.00 B2013-04-23|01:09
gdb-tdesc-cortexm-fpa.xml5.74 kB2013-04-23|01:09
openocd-stm32f4.cfg124.00 B2013-04-23|01:09
stm32_flash.ld5.33 kB2013-04-23|01:09
01.97 kB
01.97 kB
main.c2.92 kB2013-04-23|01:09
01.97 kB
main.c1.51 kB2013-04-23|01:09
01.97 kB
.gitignore32.00 B2013-04-23|01:09
build.mk1.12 kB2013-04-23|01:09
main.c740.00 B2013-04-23|01:09
01.97 kB
main.c11.32 kB2013-04-23|01:09
01.97 kB
main.c2.51 kB2013-04-23|01:09
01.97 kB
main.c2.14 kB2013-04-23|01:09
01.97 kB
main.c1.03 kB2013-04-23|01:09
01.97 kB
main.c1.49 kB2013-04-23|01:09
01.97 kB
main.c1.90 kB2013-04-23|01:09
01.97 kB
.gitignore8.00 B2013-04-23|01:09
build.mk1.17 kB2013-04-23|01:09
01.97 kB
01.97 kB
usb_bsp.h2.29 kB2013-04-23|01:09
usb_conf.h10.48 kB2013-04-23|01:09
usb_core.h12.29 kB2013-04-23|01:09
usb_dcd.h4.48 kB2013-04-23|01:09
usb_dcd_int.h3.03 kB2013-04-23|01:09
usb_defines.h6.52 kB2013-04-23|01:09
usb_hcd.h2.76 kB2013-04-23|01:09
usb_hcd_int.h3.65 kB2013-04-23|01:09
usb_otg.h1.97 kB2013-04-23|01:09
usb_regs.h21.22 kB2013-04-23|01:09
usbd_cdc_core.h3.87 kB2013-04-23|01:09
usbd_cdc_if_template.h1.85 kB2013-04-23|01:09
usbd_cdc_vcp.h2.89 kB2013-04-23|01:09
usbd_conf.h3.29 kB2013-04-23|01:09
usbd_core.h2.50 kB2013-04-23|01:09
usbd_def.h4.82 kB2013-04-23|01:09
usbd_desc.h3.35 kB2013-04-23|01:09
usbd_ioreq.h2.89 kB2013-04-23|01:09
usbd_req.h2.50 kB2013-04-23|01:09
usbd_usr.h3.00 kB2013-04-23|01:09
01.97 kB
usb_bsp.c3.27 kB2013-04-23|01:09
usb_core.c55.46 kB2013-04-23|01:09
usb_dcd.c9.21 kB2013-04-23|01:09
usb_dcd_int.c21.32 kB2013-04-23|01:09
usb_hcd.c5.62 kB2013-04-23|01:09
usb_hcd_int.c21.58 kB2013-04-23|01:09
usb_otg.c9.48 kB2013-04-23|01:09
usbd_cdc_core.c24.57 kB2013-04-23|01:09
usbd_cdc_if_template.c6.07 kB2013-04-23|01:09
usbd_cdc_vcp.c6.71 kB2013-04-23|01:09
usbd_core.c11.39 kB2013-04-23|01:09
usbd_desc.c8.23 kB2013-04-23|01:09
usbd_ioreq.c5.38 kB2013-04-23|01:09
usbd_req.c19.55 kB2013-04-23|01:09
usbd_usr.c3.48 kB2013-04-23|01:09
...
Sponsored links

timer.c (278.02 kB)

Need 1 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D