cpu: Initial import of stm32f3
parent
5e8517d956
commit
c5c860f435
@ -0,0 +1,7 @@
|
||||
# define the module that is build
|
||||
MODULE = cpu
|
||||
|
||||
# add a list of subdirectories, that should also be build
|
||||
DIRS = periph $(CORTEX_M4_COMMON)
|
||||
|
||||
include $(RIOTBASE)/Makefile.base
|
@ -0,0 +1,31 @@
|
||||
# this CPU implementation is using the explicit core/CPU interface
|
||||
export CFLAGS += -DCOREIF_NG=1
|
||||
|
||||
# export the peripheral drivers to be linked into the final binary
|
||||
export USEMODULE += periph
|
||||
|
||||
# this CPU implementation makes use of the ringbuffer, so include the lib module
|
||||
export USEMODULE += lib
|
||||
|
||||
# tell the build system that the CPU depends on the Cortex-M common files
|
||||
export USEMODULE += cortex-m4_common
|
||||
|
||||
# define path to cortex-m common module, which is needed for this CPU
|
||||
export CORTEX_M4_COMMON = $(RIOTCPU)/cortex-m4_common/
|
||||
|
||||
# CPU depends on the cortex-m common module, so include it
|
||||
include $(CORTEX_M4_COMMON)Makefile.include
|
||||
|
||||
# define the linker script to use for this CPU
|
||||
export LINKERSCRIPT = $(RIOTCPU)/$(CPU)/$(CPU_MODEL)_linkerscript.ld
|
||||
|
||||
#export the CPU model
|
||||
MODEL = $(shell echo $(CPU_MODEL)|tr 'a-z' 'A-Z')
|
||||
export CFLAGS += -DCPU_MODEL_$(MODEL)
|
||||
|
||||
# include CPU specific includes
|
||||
export INCLUDES += -I$(RIOTCPU)/$(CPU)/include
|
||||
|
||||
# add the CPU specific system calls implementations for the linker
|
||||
export UNDEF += $(BINDIR)cpu/syscalls.o
|
||||
export UNDEF += $(BINDIR)cpu/startup.o
|
@ -0,0 +1,126 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Freie Universität Berlin
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser General
|
||||
* Public License v2.1. See the file LICENSE in the top level directory for more
|
||||
* details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ingroup cpu_stm32f3
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief Implementation of the CPU initialization
|
||||
*
|
||||
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include "cpu.h"
|
||||
#include "periph_conf.h"
|
||||
|
||||
/**
|
||||
* @name Pattern to write into the Coprocessor Access Control Register to allow full FPU access
|
||||
*/
|
||||
#define FULL_FPU_ACCESS (0x00f00000)
|
||||
|
||||
static void cpu_clock_init(void);
|
||||
|
||||
/**
|
||||
* @brief Initialize the CPU, set IRQ priorities
|
||||
*/
|
||||
void cpu_init(void)
|
||||
{
|
||||
/* give full access to the FPU */
|
||||
SCB->CPACR |= (uint32_t)FULL_FPU_ACCESS;
|
||||
|
||||
/* configure the vector table location to internal flash */
|
||||
SCB->VTOR = FLASH_BASE;
|
||||
|
||||
/* initialize the clock system */
|
||||
cpu_clock_init();
|
||||
|
||||
/* set pendSV interrupt to lowest possible priority */
|
||||
NVIC_SetPriority(PendSV_IRQn, 0xff);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Configure the controllers clock system
|
||||
*
|
||||
* The clock initialization make the following assumptions:
|
||||
* - the external HSE clock from an external oscillator is used as base clock
|
||||
* - the internal PLL circuit is used for clock refinement
|
||||
*
|
||||
* The actual used values are specified in the board's `periph_conf.h` file.
|
||||
*
|
||||
* NOTE: currently there is not timeout for initialization of PLL and other locks
|
||||
* -> when wrong values are chosen, the initialization could stall
|
||||
*/
|
||||
static void cpu_clock_init(void)
|
||||
{
|
||||
/* configure the HSE clock */
|
||||
|
||||
/* enable the HSI clock */
|
||||
RCC->CR |= RCC_CR_HSION;
|
||||
|
||||
/* reset clock configuration register */
|
||||
RCC->CFGR = 0;
|
||||
|
||||
/* disable HSE, CSS and PLL */
|
||||
RCC->CR &= ~(RCC_CR_HSEON | RCC_CR_HSEBYP | RCC_CR_CSSON | RCC_CR_PLLON);
|
||||
|
||||
/* disable all clock interrupts */
|
||||
RCC->CIR = 0;
|
||||
|
||||
/* enable the HSE clock */
|
||||
RCC->CR |= RCC_CR_HSEON;
|
||||
|
||||
/* wait for HSE to be ready */
|
||||
while (!(RCC->CR & RCC_CR_HSERDY));
|
||||
|
||||
/* setup the peripheral bus prescalers */
|
||||
|
||||
/* set the AHB clock divider */
|
||||
RCC->CFGR &= ~RCC_CFGR_HPRE;
|
||||
RCC->CFGR |= CLOCK_AHB_DIV;
|
||||
/* set the APB2 (high speed) bus clock divider */
|
||||
RCC->CFGR &= ~RCC_CFGR_PPRE2;
|
||||
RCC->CFGR |= CLOCK_APB2_DIV;
|
||||
/* set the APB1 (low speed) bus clock divider */
|
||||
RCC->CFGR &= ~RCC_CFGR_PPRE1;
|
||||
RCC->CFGR |= CLOCK_APB1_DIV;
|
||||
|
||||
/* configure the PLL */
|
||||
|
||||
/* reset PLL configuration */
|
||||
RCC->CFGR &= ~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMUL);
|
||||
/* set PLL to use HSE clock with prescaler 1 as input */
|
||||
RCC->CFGR |= RCC_CFGR_PLLSRC_HSE_PREDIV | RCC_CFGR_PLLXTPRE_HSE_PREDIV_DIV1 |
|
||||
(((CLOCK_PLL_MUL - 2) & 0xf) << 18);
|
||||
|
||||
/* enable PLL again */
|
||||
RCC->CR |= RCC_CR_PLLON;
|
||||
/* wait until PLL is stable */
|
||||
while(!(RCC->CR & RCC_CR_PLLRDY));
|
||||
|
||||
/* configure flash latency */
|
||||
|
||||
/* reset flash access control register */
|
||||
FLASH->ACR = 0;
|
||||
/* enable pre-fetch buffer */
|
||||
FLASH->ACR |= FLASH_ACR_PRFTBE;
|
||||
/* set flash latency */
|
||||
FLASH->ACR &= ~FLASH_ACR_LATENCY;
|
||||
FLASH->ACR |= CLOCK_FLASH_LATENCY;
|
||||
|
||||
/* configure the sysclock and the peripheral clocks */
|
||||
|
||||
/* set sysclock to be driven by the PLL clock */
|
||||
RCC->CFGR &= ~RCC_CFGR_SW;
|
||||
RCC->CFGR |= RCC_CFGR_SW_PLL;
|
||||
|
||||
/* wait for sysclock to be stable */
|
||||
while (!(RCC->CFGR & RCC_CFGR_SWS_PLL));
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Freie Universität Berlin
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser General
|
||||
* Public License v2.1. See the file LICENSE in the top level directory for more
|
||||
* details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ingroup cpu_stm32f3
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief Implementation of the kernels hwtimer interface
|
||||
*
|
||||
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include "arch/hwtimer_arch.h"
|
||||
#include "board.h"
|
||||
#include "periph/timer.h"
|
||||
#include "thread.h"
|
||||
|
||||
|
||||
void irq_handler(int channel);
|
||||
void (*timeout_handler)(int);
|
||||
|
||||
|
||||
void hwtimer_arch_init(void (*handler)(int), uint32_t fcpu)
|
||||
{
|
||||
timeout_handler = handler;
|
||||
timer_init(HW_TIMER, 1, &irq_handler);
|
||||
}
|
||||
|
||||
void hwtimer_arch_enable_interrupt(void)
|
||||
{
|
||||
timer_irq_enable(HW_TIMER);
|
||||
}
|
||||
|
||||
void hwtimer_arch_disable_interrupt(void)
|
||||
{
|
||||
timer_irq_disable(HW_TIMER);
|
||||
}
|
||||
|
||||
void hwtimer_arch_set(unsigned long offset, short timer)
|
||||
{
|
||||
timer_set(HW_TIMER, timer, offset);
|
||||
}
|
||||
|
||||
void hwtimer_arch_set_absolute(unsigned long value, short timer)
|
||||
{
|
||||
timer_set_absolute(HW_TIMER, timer, value);
|
||||
}
|
||||
|
||||
void hwtimer_arch_unset(short timer)
|
||||
{
|
||||
timer_clear(HW_TIMER, timer);
|
||||
}
|
||||
|
||||
unsigned long hwtimer_arch_now(void)
|
||||
{
|
||||
return timer_read(HW_TIMER);
|
||||
}
|
||||
|
||||
void irq_handler(int channel)
|
||||
{
|
||||
timeout_handler((short)(channel));
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Freie Universität Berlin
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser General
|
||||
* Public License v2.1. See the file LICENSE in the top level directory for more
|
||||
* details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @defgroup cpu_stm32f3 STM32F3
|
||||
* @ingroup cpu
|
||||
* @brief CPU specific implementations for the STM32F3
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief Implementation specific CPU configuration options
|
||||
*
|
||||
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
||||
*/
|
||||
|
||||
#ifndef __CPU_CONF_H
|
||||
#define __CPU_CONF_H
|
||||
|
||||
#ifdef CPU_MODEL_STM32F303VC
|
||||
#include "stm32f303xc.h"
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @name Kernel configuration
|
||||
*
|
||||
* TODO: measure and adjust for the Cortex-M4f
|
||||
* @{
|
||||
*/
|
||||
#define KERNEL_CONF_STACKSIZE_PRINTF (1024)
|
||||
|
||||
#ifndef KERNEL_CONF_STACKSIZE_DEFAULT
|
||||
#define KERNEL_CONF_STACKSIZE_DEFAULT (1024)
|
||||
#endif
|
||||
|
||||
#define KERNEL_CONF_STACKSIZE_IDLE (256)
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @name UART0 buffer size definition for compatibility reasons
|
||||
*
|
||||
* TODO: remove once the remodeling of the uart0 driver is done
|
||||
* @{
|
||||
*/
|
||||
#ifndef UART0_BUFSIZE
|
||||
#define UART0_BUFSIZE (128)
|
||||
#endif
|
||||
/** @} */
|
||||
|
||||
#endif /* __CPU_CONF_H */
|
||||
/** @} */
|
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Freie Universität Berlin
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser General
|
||||
* Public License v2.1. See the file LICENSE in the top level directory for more
|
||||
* details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ingroup cpu_stm32f3
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief CPU specific hwtimer configuration options
|
||||
*
|
||||
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
||||
*/
|
||||
|
||||
#ifndef __HWTIMER_CPU_H
|
||||
#define __HWTIMER_CPU_H
|
||||
|
||||
/**
|
||||
* @name Hardware timer configuration
|
||||
* @{
|
||||
*/
|
||||
#define HWTIMER_MAXTIMERS 4 /**< the CPU implementation supports 4 HW timers */
|
||||
#define HWTIMER_SPEED 1000000 /**< the HW timer runs with 1MHz */
|
||||
#define HWTIMER_MAXTICKS (0xFFFFFFFF) /**< 32-bit timer */
|
||||
/** @} */
|
||||
|
||||
#endif /* __HWTIMER_CPU_H */
|
||||
/** @} */
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Freie Universität Berlin
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser General
|
||||
* Public License v2.1. See the file LICENSE in the top level directory for more
|
||||
* details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ingroup cpu_stm32f3
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief Implementation of the kernel's architecture dependent IO interface
|
||||
*
|
||||
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include "board.h"
|
||||
#include "arch/io_arch.h"
|
||||
#include "periph/uart.h"
|
||||
|
||||
|
||||
int io_arch_puts(char *data, int size)
|
||||
{
|
||||
int i = 0;
|
||||
for (; i < size; i++) {
|
||||
uart_write_blocking(STDIO, data[i]);
|
||||
}
|
||||
return i;
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Freie Universität Berlin
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser General
|
||||
* Public License v2.1. See the file LICENSE in the top level directory for more
|
||||
* details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ingroup cpu_stm32f3
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief Implementation of the kernels power management interface
|
||||
*
|
||||
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include "arch/lpm_arch.h"
|
||||
|
||||
void lpm_arch_init(void)
|
||||
{
|
||||
/* TODO */
|
||||
}
|
||||
|
||||
enum lpm_mode lpm_arch_set(enum lpm_mode target)
|
||||
{
|
||||
/* TODO */
|
||||
return 0;
|
||||
}
|
||||
|
||||
enum lpm_mode lpm_arch_get(void)
|
||||
{
|
||||
/* TODO */
|
||||
return 0;
|
||||
}
|
||||
|
||||
void lpm_arch_awake(void)
|
||||
{
|
||||
/* TODO */
|
||||
}
|
||||
|
||||
void lpm_arch_begin_awake(void)
|
||||
{
|
||||
/* TODO */
|
||||
}
|
||||
|
||||
void lpm_arch_end_awake(void)
|
||||
{
|
||||
/* TODO */
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
MODULE = periph
|
||||
|
||||
include $(RIOTBASE)/Makefile.base
|
@ -0,0 +1,756 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Freie Universität Berlin
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser General
|
||||
* Public License v2.1. See the file LICENSE in the top level directory for more
|
||||
* details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ingroup cpu_stm32f3
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief Low-level GPIO driver implementation
|
||||
*
|
||||
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include "cpu.h"
|
||||
#include "sched.h"
|
||||
#include "thread.h"
|
||||
#include "periph/gpio.h"
|
||||
#include "periph_conf.h"
|
||||
|
||||
typedef struct {
|
||||
void (*cb)(void);
|
||||
} gpio_state_t;
|
||||
|
||||
static inline void irq_handler(gpio_t dev);
|
||||
|
||||
static gpio_state_t config[GPIO_NUMOF];
|
||||
|
||||
|
||||
int gpio_init_out(gpio_t dev, gpio_pp_t pushpull)
|
||||
{
|
||||
GPIO_TypeDef *port;
|
||||
uint32_t pin;
|
||||
|
||||
switch (dev) {
|
||||
#if GPIO_0_EN
|
||||
case GPIO_0:
|
||||
GPIO_0_CLKEN();
|
||||
port = GPIO_0_PORT;
|
||||
pin = GPIO_0_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_1_EN
|
||||
case GPIO_1:
|
||||
GPIO_1_CLKEN();
|
||||
port = GPIO_1_PORT;
|
||||
pin = GPIO_1_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_2_EN
|
||||
case GPIO_2:
|
||||
GPIO_2_CLKEN();
|
||||
port = GPIO_2_PORT;
|
||||
pin = GPIO_2_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_3_EN
|
||||
case GPIO_3:
|
||||
GPIO_3_CLKEN();
|
||||
port = GPIO_3_PORT;
|
||||
pin = GPIO_3_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_4_EN
|
||||
case GPIO_4:
|
||||
GPIO_4_CLKEN();
|
||||
port = GPIO_4_PORT;
|
||||
pin = GPIO_4_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_5_EN
|
||||
case GPIO_5:
|
||||
GPIO_5_CLKEN();
|
||||
port = GPIO_5_PORT;
|
||||
pin = GPIO_5_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_6_EN
|
||||
case GPIO_6:
|
||||
GPIO_6_CLKEN();
|
||||
port = GPIO_6_PORT;
|
||||
pin = GPIO_6_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_7_EN
|
||||
case GPIO_7:
|
||||
GPIO_7_CLKEN();
|
||||
port = GPIO_7_PORT;
|
||||
pin = GPIO_7_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_8_EN
|
||||
case GPIO_8:
|
||||
GPIO_8_CLKEN();
|
||||
port = GPIO_8_PORT;
|
||||
pin = GPIO_8_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_9_EN
|
||||
case GPIO_9:
|
||||
GPIO_9_CLKEN();
|
||||
port = GPIO_9_PORT;
|
||||
pin = GPIO_9_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_10_EN
|
||||
case GPIO_10:
|
||||
GPIO_10_CLKEN();
|
||||
port = GPIO_10_PORT;
|
||||
pin = GPIO_10_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_11_EN
|
||||
case GPIO_11:
|
||||
GPIO_11_CLKEN();
|
||||
port = GPIO_11_PORT;
|
||||
pin = GPIO_11_PIN;
|
||||
break;
|
||||
#endif
|
||||
case GPIO_UNDEFINED:
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
port->MODER &= ~(2 << (2 * pin)); /* set pin to output mode */
|
||||
port->MODER |= (1 << (2 * pin));
|
||||
port->OTYPER &= ~(1 << pin); /* set to push-pull configuration */
|
||||
port->OSPEEDR |= (3 << (2 * pin)); /* set to high speed */
|
||||
port->PUPDR &= ~(3 << (2 * pin)); /* configure push-pull resistors */
|
||||
port->PUPDR |= (pushpull << (2 * pin));
|
||||
port->ODR &= ~(1 << pin); /* set pin to low signal */
|
||||
|
||||
return 0; /* all OK */
|
||||
}
|
||||
|
||||
int gpio_init_in(gpio_t dev, gpio_pp_t pushpull)
|
||||
{
|
||||
GPIO_TypeDef *port;
|
||||
uint32_t pin;
|
||||
|
||||
switch (dev) {
|
||||
#if GPIO_0_EN
|
||||
case GPIO_0:
|
||||
GPIO_0_CLKEN();
|
||||
port = GPIO_0_PORT;
|
||||
pin = GPIO_0_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_1_EN
|
||||
case GPIO_1:
|
||||
GPIO_1_CLKEN();
|
||||
port = GPIO_1_PORT;
|
||||
pin = GPIO_1_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_2_EN
|
||||
case GPIO_2:
|
||||
GPIO_2_CLKEN();
|
||||
port = GPIO_2_PORT;
|
||||
pin = GPIO_2_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_3_EN
|
||||
case GPIO_3:
|
||||
GPIO_3_CLKEN();
|
||||
port = GPIO_3_PORT;
|
||||
pin = GPIO_3_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_4_EN
|
||||
case GPIO_4:
|
||||
GPIO_4_CLKEN();
|
||||
port = GPIO_4_PORT;
|
||||
pin = GPIO_4_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_5_EN
|
||||
case GPIO_5:
|
||||
GPIO_5_CLKEN();
|
||||
port = GPIO_5_PORT;
|
||||
pin = GPIO_5_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_6_EN
|
||||
case GPIO_6:
|
||||
GPIO_6_CLKEN();
|
||||
port = GPIO_6_PORT;
|
||||
pin = GPIO_6_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_7_EN
|
||||
case GPIO_7:
|
||||
GPIO_7_CLKEN();
|
||||
port = GPIO_7_PORT;
|
||||
pin = GPIO_7_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_8_EN
|
||||
case GPIO_8:
|
||||
GPIO_8_CLKEN();
|
||||
port = GPIO_8_PORT;
|
||||
pin = GPIO_8_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_9_EN
|
||||
case GPIO_9:
|
||||
GPIO_9_CLKEN();
|
||||
port = GPIO_9_PORT;
|
||||
pin = GPIO_9_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_10_EN
|
||||
case GPIO_10:
|
||||
GPIO_10_CLKEN();
|
||||
port = GPIO_10_PORT;
|
||||
pin = GPIO_10_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_11_EN
|
||||
case GPIO_11:
|
||||
GPIO_11_CLKEN();
|
||||
port = GPIO_11_PORT;
|
||||
pin = GPIO_11_PIN;
|
||||
break;
|
||||
#endif
|
||||
case GPIO_UNDEFINED:
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
port->MODER &= ~(3 << (2 * pin)); /* configure pin as input */
|
||||
port->PUPDR &= ~(3 << (2 * pin)); /* configure push-pull resistors */
|
||||
port->PUPDR |= (pushpull << (2 * pin));
|
||||
|
||||
return 0; /* everything alright here */
|
||||
}
|
||||
|
||||
int gpio_init_int(gpio_t dev, gpio_pp_t pushpull, gpio_flank_t flank, void (*cb)(void))
|
||||
{
|
||||
uint32_t pin;
|
||||
|
||||
int res = gpio_init_in(dev, pushpull);
|
||||
if (res < 0) {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* enable the SYSCFG clock */
|
||||
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
|
||||
|
||||
switch (dev) {
|
||||
#if GPIO_0_EN
|
||||
case GPIO_0:
|
||||
pin = GPIO_0_PIN;
|
||||
GPIO_0_EXTI_CFG1();
|
||||
GPIO_0_EXTI_CFG2();
|
||||
NVIC_SetPriority(GPIO_0_IRQ, GPIO_IRQ_PRIO);
|
||||
NVIC_EnableIRQ(GPIO_0_IRQ);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_1_EN
|
||||
case GPIO_1:
|
||||
pin = GPIO_1_PIN;
|
||||
GPIO_1_EXTI_CFG1();
|
||||
GPIO_1_EXTI_CFG2();
|
||||
NVIC_SetPriority(GPIO_1_IRQ, GPIO_IRQ_PRIO);
|
||||
NVIC_EnableIRQ(GPIO_1_IRQ);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_2_EN
|
||||
case GPIO_2:
|
||||
pin = GPIO_2_PIN;
|
||||
GPIO_2_EXTI_CFG1();
|
||||
GPIO_2_EXTI_CFG2();
|
||||
NVIC_SetPriority(GPIO_2_IRQ, GPIO_IRQ_PRIO);
|
||||
NVIC_EnableIRQ(GPIO_2_IRQ);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_3_EN
|
||||
case GPIO_3:
|
||||
pin = GPIO_3_PIN;
|
||||
GPIO_3_EXTI_CFG1();
|
||||
GPIO_3_EXTI_CFG2();
|
||||
NVIC_SetPriority(GPIO_3_IRQ, GPIO_IRQ_PRIO);
|
||||
NVIC_EnableIRQ(GPIO_3_IRQ);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_4_EN
|
||||
case GPIO_4:
|
||||
pin = GPIO_4_PIN;
|
||||
GPIO_4_EXTI_CFG1();
|
||||
GPIO_4_EXTI_CFG2();
|
||||
NVIC_SetPriority(GPIO_4_IRQ, GPIO_IRQ_PRIO);
|
||||
NVIC_EnableIRQ(GPIO_4_IRQ);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_5_EN
|
||||
case GPIO_5:
|
||||
pin = GPIO_5_PIN;
|
||||
GPIO_5_EXTI_CFG1();
|
||||
GPIO_5_EXTI_CFG2();
|
||||
NVIC_SetPriority(GPIO_5_IRQ, GPIO_IRQ_PRIO);
|
||||
NVIC_EnableIRQ(GPIO_5_IRQ);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_6_EN
|
||||
case GPIO_6:
|
||||
pin = GPIO_6_PIN;
|
||||
GPIO_6_EXTI_CFG1();
|
||||
GPIO_6_EXTI_CFG2();
|
||||
NVIC_SetPriority(GPIO_6_IRQ, GPIO_IRQ_PRIO);
|
||||
NVIC_EnableIRQ(GPIO_6_IRQ);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_7_EN
|
||||
case GPIO_7:
|
||||
pin = GPIO_7_PIN;
|
||||
GPIO_7_EXTI_CFG1();
|
||||
GPIO_7_EXTI_CFG2();
|
||||
NVIC_SetPriority(GPIO_7_IRQ, GPIO_IRQ_PRIO);
|
||||
NVIC_EnableIRQ(GPIO_7_IRQ);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_8_EN
|
||||
case GPIO_8:
|
||||
pin = GPIO_8_PIN;
|
||||
GPIO_8_EXTI_CFG1();
|
||||
GPIO_8_EXTI_CFG2();
|
||||
NVIC_SetPriority(GPIO_8_IRQ, GPIO_IRQ_PRIO);
|
||||
NVIC_EnableIRQ(GPIO_8_IRQ);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_9_EN
|
||||
case GPIO_9:
|
||||
pin = GPIO_9_PIN;
|
||||
GPIO_9_EXTI_CFG1();
|
||||
GPIO_9_EXTI_CFG2();
|
||||
NVIC_SetPriority(GPIO_9_IRQ, GPIO_IRQ_PRIO);
|
||||
NVIC_EnableIRQ(GPIO_9_IRQ);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_10_EN
|
||||
case GPIO_10:
|
||||
pin = GPIO_10_PIN;
|
||||
GPIO_10_EXTI_CFG1();
|
||||
GPIO_10_EXTI_CFG2();
|
||||
NVIC_SetPriority(GPIO_10_IRQ, GPIO_IRQ_PRIO);
|
||||
NVIC_EnableIRQ(GPIO_10_IRQ);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_11_EN
|
||||
case GPIO_11:
|
||||
pin = GPIO_11_PIN;
|
||||
GPIO_11_EXTI_CFG1();
|
||||
GPIO_11_EXTI_CFG2();
|
||||
NVIC_SetPriority(GPIO_11_IRQ, GPIO_IRQ_PRIO);
|
||||
NVIC_EnableIRQ(GPIO_11_IRQ);
|
||||
break;
|
||||
#endif
|
||||
case GPIO_UNDEFINED:
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* set callback */
|
||||
config[dev].cb = cb;
|
||||
|
||||
/* configure the active edges */
|
||||
switch (flank) {
|
||||
case GPIO_RISING:
|
||||
EXTI->RTSR |= (1 << pin);
|
||||
break;
|
||||
case GPIO_FALLING:
|
||||
EXTI->FTSR |= (1 << pin);
|
||||
break;
|
||||
case GPIO_BOTH:
|
||||
EXTI->RTSR |= (1 << pin);
|
||||
EXTI->FTSR |= (1 << pin);
|
||||
break;
|
||||
}
|
||||
|
||||
/* enable interrupt for EXTI line */
|
||||
EXTI->IMR |= (1 << pin);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int gpio_read(gpio_t dev)
|
||||
{
|
||||
GPIO_TypeDef *port;
|
||||
uint32_t pin;
|
||||
|
||||
switch (dev) {
|
||||
#if GPIO_0_EN
|
||||
case GPIO_0:
|
||||
port = GPIO_0_PORT;
|
||||
pin = GPIO_0_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_1_EN
|
||||
case GPIO_1:
|
||||
port = GPIO_1_PORT;
|
||||
pin = GPIO_1_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_2_EN
|
||||
case GPIO_2:
|
||||
port = GPIO_2_PORT;
|
||||
pin = GPIO_2_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_3_EN
|
||||
case GPIO_3:
|
||||
port = GPIO_3_PORT;
|
||||
pin = GPIO_3_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_4_EN
|
||||
case GPIO_4:
|
||||
port = GPIO_4_PORT;
|
||||
pin = GPIO_4_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_5_EN
|
||||
case GPIO_5:
|
||||
port = GPIO_5_PORT;
|
||||
pin = GPIO_5_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_6_EN
|
||||
case GPIO_6:
|
||||
port = GPIO_6_PORT;
|
||||
pin = GPIO_6_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_7_EN
|
||||
case GPIO_7:
|
||||
port = GPIO_7_PORT;
|
||||
pin = GPIO_7_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_8_EN
|
||||
case GPIO_8:
|
||||
port = GPIO_8_PORT;
|
||||
pin = GPIO_8_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_9_EN
|
||||
case GPIO_9:
|
||||
port = GPIO_9_PORT;
|
||||
pin = GPIO_9_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_10_EN
|
||||
case GPIO_10:
|
||||
port = GPIO_10_PORT;
|
||||
pin = GPIO_10_PIN;
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_11_EN
|
||||
case GPIO_11:
|
||||
port = GPIO_11_PORT;
|
||||
pin = GPIO_11_PIN;
|
||||
break;
|
||||
#endif
|
||||
case GPIO_UNDEFINED:
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (port->MODER & (3 << (pin * 2))) { /* if configured as output */
|
||||
return port->ODR & (1 << pin); /* read output data register */
|
||||
}
|
||||
else {
|
||||
return port->IDR & (1 << pin); /* else read input data register */
|
||||
}
|
||||
}
|
||||
|
||||
int gpio_set(gpio_t dev)
|
||||
{
|
||||
switch (dev) {
|
||||
#if GPIO_0_EN
|
||||
case GPIO_0:
|
||||
GPIO_0_PORT->BSRRL = (1 << GPIO_0_PIN);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_1_EN
|
||||
case GPIO_1:
|
||||
GPIO_1_PORT->BSRRL = (1 << GPIO_1_PIN);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_2_EN
|
||||
case GPIO_2:
|
||||
GPIO_2_PORT->BSRRL = (1 << GPIO_2_PIN);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_3_EN
|
||||
case GPIO_3:
|
||||
GPIO_3_PORT->BSRRL = (1 << GPIO_3_PIN);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_4_EN
|
||||
case GPIO_4:
|
||||
GPIO_4_PORT->BSRRL = (1 << GPIO_4_PIN);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_5_EN
|
||||
case GPIO_5:
|
||||
GPIO_5_PORT->BSRRL = (1 << GPIO_5_PIN);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_6_EN
|
||||
case GPIO_6:
|
||||
GPIO_6_PORT->BSRRL = (1 << GPIO_6_PIN);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_7_EN
|
||||
case GPIO_7:
|
||||
GPIO_7_PORT->BSRRL = (1 << GPIO_7_PIN);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_8_EN
|
||||
case GPIO_8:
|
||||
GPIO_8_PORT->BSRRL = (1 << GPIO_8_PIN);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_9_EN
|
||||
case GPIO_9:
|
||||
GPIO_9_PORT->BSRRL = (1 << GPIO_9_PIN);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_10_EN
|
||||
case GPIO_10:
|
||||
GPIO_10_PORT->BSRRL = (1 << GPIO_10_PIN);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_11_EN
|
||||
case GPIO_11:
|
||||
GPIO_11_PORT->BSRRL = (1 << GPIO_11_PIN);
|
||||
#endif
|
||||
break;
|
||||
case GPIO_UNDEFINED:
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int gpio_clear(gpio_t dev)
|
||||
{
|
||||
switch (dev) {
|
||||
#if GPIO_0_EN
|
||||
case GPIO_0:
|
||||
GPIO_0_PORT->BSRRH = (1 << GPIO_0_PIN);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_1_EN
|
||||
case GPIO_1:
|
||||
GPIO_1_PORT->BSRRH = (1 << GPIO_1_PIN);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_2_EN
|
||||
case GPIO_2:
|
||||
GPIO_2_PORT->BSRRH = (1 << GPIO_2_PIN);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_3_EN
|
||||
case GPIO_3:
|
||||
GPIO_3_PORT->BSRRH = (1 << GPIO_3_PIN);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_4_EN
|
||||
case GPIO_4:
|
||||
GPIO_4_PORT->BSRRH = (1 << GPIO_4_PIN);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_5_EN
|
||||
case GPIO_5:
|
||||
GPIO_5_PORT->BSRRH = (1 << GPIO_5_PIN);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_6_EN
|
||||
case GPIO_6:
|
||||
GPIO_6_PORT->BSRRH = (1 << GPIO_6_PIN);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_7_EN
|
||||
case GPIO_7:
|
||||
GPIO_7_PORT->BSRRH = (1 << GPIO_7_PIN);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_8_EN
|
||||
case GPIO_8:
|
||||
GPIO_8_PORT->BSRRH = (1 << GPIO_8_PIN);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_9_EN
|
||||
case GPIO_9:
|
||||
GPIO_9_PORT->BSRRH = (1 << GPIO_9_PIN);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_10_EN
|
||||
case GPIO_10:
|
||||
GPIO_10_PORT->BSRRH = (1 << GPIO_10_PIN);
|
||||
break;
|
||||
#endif
|
||||
#if GPIO_11_EN
|
||||
case GPIO_11:
|
||||
GPIO_11_PORT->BSRRH = (1 << GPIO_11_PIN);
|
||||
break;
|
||||
#endif
|
||||
case GPIO_UNDEFINED:
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int gpio_toggle(gpio_t dev)
|
||||
{
|
||||
if (gpio_read(dev)) {
|
||||
return gpio_clear(dev);
|
||||
}
|
||||
else {
|
||||
return gpio_set(dev);
|
||||
}
|
||||
}
|
||||
|
||||
int gpio_write(gpio_t dev, int value)
|
||||
{
|
||||
if (value) {
|
||||
return gpio_set(dev);
|
||||
}
|
||||
else {
|
||||
return gpio_clear(dev);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void irq_handler(gpio_t dev)
|
||||
{
|
||||
config[dev].cb();
|
||||
if (sched_context_switch_request) {
|
||||
thread_yield();
|
||||
}
|
||||
}
|
||||
|
||||
__attribute__((naked)) void isr_exti0(void)
|
||||
{
|
||||
ISR_ENTER();
|
||||
if (EXTI->PR & EXTI_PR_PR0) {
|
||||
EXTI->PR |= EXTI_PR_PR0; /* clear status bit by writing a 1 to it */
|
||||
irq_handler(GPIO_IRQ_0);
|
||||
}
|
||||
ISR_EXIT();
|
||||
}
|
||||
|
||||
__attribute__((naked)) void isr_exti1(void)
|
||||
{
|
||||
ISR_ENTER();
|
||||
if (EXTI->PR & EXTI_PR_PR1) {
|
||||
EXTI->PR |= EXTI_PR_PR1; /* clear status bit by writing a 1 to it */
|
||||
irq_handler(GPIO_IRQ_1);
|
||||
}
|
||||
ISR_EXIT();
|
||||
}
|
||||
|
||||
__attribute__((naked)) void isr_exti2(void)
|
||||
{
|
||||
ISR_ENTER();
|
||||
if (EXTI->PR & EXTI_PR_PR2) {
|
||||
EXTI->PR |= EXTI_PR_PR2; /* clear status bit by writing a 1 to it */
|
||||
irq_handler(GPIO_IRQ_2);
|
||||
}
|
||||
ISR_EXIT();
|
||||
}
|
||||
|
||||
__attribute__((naked)) void isr_exti3(void)
|
||||
{
|
||||
ISR_ENTER();
|
||||
if (EXTI->PR & EXTI_PR_PR3) {
|
||||
EXTI->PR |= EXTI_PR_PR3; /* clear status bit by writing a 1 to it */
|
||||
irq_handler(GPIO_IRQ_3);
|
||||
}
|
||||
ISR_EXIT();
|
||||
}
|
||||
|
||||
__attribute__((naked)) void isr_exti4(void)
|
||||
{
|
||||
ISR_ENTER();
|
||||
if (EXTI->PR & EXTI_PR_PR4) {
|
||||
EXTI->PR |= EXTI_PR_PR4; /* clear status bit by writing a 1 to it */
|
||||
irq_handler(GPIO_IRQ_4);
|
||||
}
|
||||
ISR_EXIT();
|
||||
}
|
||||
|
||||
__attribute__((naked)) void isr_exti9_5(void)
|
||||
{
|
||||
ISR_ENTER();
|
||||
if (EXTI->PR & EXTI_PR_PR5) {
|
||||
EXTI->PR |= EXTI_PR_PR5; /* clear status bit by writing a 1 to it */
|
||||
irq_handler(GPIO_IRQ_5);
|
||||
}
|
||||
else if (EXTI->PR & EXTI_PR_PR6) {
|
||||
EXTI->PR |= EXTI_PR_PR6; /* clear status bit by writing a 1 to it */
|
||||
irq_handler(GPIO_IRQ_6);
|
||||
}
|
||||
else if (EXTI->PR & EXTI_PR_PR7) {
|
||||
EXTI->PR |= EXTI_PR_PR7; /* clear status bit by writing a 1 to it */
|
||||
irq_handler(GPIO_IRQ_7);
|
||||
}
|
||||
else if (EXTI->PR & EXTI_PR_PR8) {
|
||||
EXTI->PR |= EXTI_PR_PR8; /* clear status bit by writing a 1 to it */
|
||||
irq_handler(GPIO_IRQ_8);
|
||||
}
|
||||
else if (EXTI->PR & EXTI_PR_PR9) {
|
||||
EXTI->PR |= EXTI_PR_PR9; /* clear status bit by writing a 1 to it */
|
||||
irq_handler(GPIO_IRQ_9);
|
||||
}
|
||||
ISR_EXIT();
|
||||
}
|
||||
|
||||
__attribute__((naked)) void isr_exti15_10(void)
|
||||
{
|
||||
ISR_ENTER();
|
||||
if (EXTI->PR & EXTI_PR_PR10) {
|
||||
EXTI->PR |= EXTI_PR_PR10; /* clear status bit by writing a 1 to it */
|
||||
irq_handler(GPIO_IRQ_10);
|
||||
}
|
||||
else if (EXTI->PR & EXTI_PR_PR11) {
|
||||
EXTI->PR |= EXTI_PR_PR11; /* clear status bit by writing a 1 to it */
|
||||
irq_handler(GPIO_IRQ_11);
|
||||
}
|
||||
else if (EXTI->PR & EXTI_PR_PR12) {
|
||||
EXTI->PR |= EXTI_PR_PR12; /* clear status bit by writing a 1 to it */
|
||||
irq_handler(GPIO_IRQ_12);
|
||||
}
|
||||
else if (EXTI->PR & EXTI_PR_PR13) {
|
||||
EXTI->PR |= EXTI_PR_PR13; /* clear status bit by writing a 1 to it */
|
||||
irq_handler(GPIO_IRQ_13);
|
||||
}
|
||||
else if (EXTI->PR & EXTI_PR_PR14) {
|
||||
EXTI->PR |= EXTI_PR_PR14; /* clear status bit by writing a 1 to it */
|
||||
irq_handler(GPIO_IRQ_14);
|
||||
}
|
||||
else if (EXTI->PR & EXTI_PR_PR15) {
|
||||
EXTI->PR |= EXTI_PR_PR15; /* clear status bit by writing a 1 to it */
|
||||
irq_handler(GPIO_IRQ_15);
|
||||
}
|
||||
ISR_EXIT();
|
||||
}
|
@ -0,0 +1,278 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Freie Universität Berlin
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser General
|
||||
* Public License v2.1. See the file LICENSE in the top level directory for more
|
||||
* details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ingroup cpu_stm32f3
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief Low-level timer driver implementation
|
||||
*
|
||||
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "cpu.h"
|
||||
#include "board.h"
|
||||
#include "sched.h"
|
||||
#include "thread.h"
|
||||
#include "periph_conf.h"
|
||||
#include "periph/timer.h"
|
||||
|
||||
/** Unified IRQ handler for all timers */
|
||||
static inline void irq_handler(tim_t timer, TIM_TypeDef *dev);
|
||||
|
||||
/** Type for timer state */
|
||||
typedef struct {
|
||||
void (*cb)(int);
|
||||
} timer_conf_t;
|
||||
|
||||
/** Timer state memory */
|
||||
timer_conf_t config[TIMER_NUMOF];
|
||||
|
||||
|
||||
int timer_init(tim_t dev, unsigned int ticks_per_us, void (*callback)(int))
|
||||
{
|
||||
TIM_TypeDef *timer;
|
||||
|
||||
switch (dev) {
|
||||
#if TIMER_0_EN
|
||||
case TIMER_0:
|
||||
/* enable timer peripheral clock */
|
||||
TIMER_0_CLKEN();
|
||||
/* set timer's IRQ priority */
|
||||
NVIC_SetPriority(TIMER_0_IRQ_CHAN, TIMER_IRQ_PRIO);
|
||||
/* select timer */
|
||||
timer = TIMER_0_DEV;
|
||||
break;
|
||||
#endif
|
||||
case TIMER_UNDEFINED:
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* set callback function */
|
||||
config[dev].cb = callback;
|
||||
|
||||
/* set timer to run in counter mode */
|
||||
timer->CR1 = 0;
|
||||
timer->CR2 = 0;
|
||||
|
||||
/* set auto-reload and prescaler values and load new values */
|
||||
timer->PSC = TIMER_0_PRESCALER * ticks_per_us;
|
||||
timer->EGR |= TIM_EGR_UG;
|
||||
|
||||
/* enable the timer's interrupt */
|
||||
timer_irq_enable(dev);
|
||||
|
||||
/* start the timer */
|
||||
timer_start(dev);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int timer_set(tim_t dev, int channel, unsigned int timeout)
|
||||
{
|
||||
int now = timer_read(dev);
|
||||
return timer_set_absolute(dev, channel, now + timeout - 1);
|
||||
}
|
||||
|
||||
int timer_set_absolute(tim_t dev, int channel, unsigned int value)
|
||||
{
|
||||
TIM_TypeDef *timer;
|
||||
|
||||
switch (dev) {
|
||||
#if TIMER_0_EN
|
||||
case TIMER_0:
|
||||
timer = TIMER_0_DEV;
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
switch (channel) {
|
||||
case 0:
|
||||
timer->CCR1 = value;
|
||||
timer->SR &= ~TIM_SR_CC1IF;
|
||||
timer->DIER |= TIM_DIER_CC1IE;
|
||||
break;
|
||||
case 1:
|
||||
timer->CCR2 = value;
|
||||
timer->SR &= ~TIM_SR_CC2IF;
|
||||
timer->DIER |= TIM_DIER_CC2IE;
|
||||
break;
|
||||
case 2:
|
||||
timer->CCR3 = value;
|
||||
timer->SR &= ~TIM_SR_CC3IF;
|
||||
timer->DIER |= TIM_DIER_CC3IE;
|
||||
break;
|
||||
case 3:
|
||||
timer->CCR4 = value;
|
||||
timer->SR &= ~TIM_SR_CC4IF;
|
||||
timer->DIER |= TIM_DIER_CC4IE;
|
||||
break;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int timer_clear(tim_t dev, int channel)
|
||||
{
|
||||
TIM_TypeDef *timer;
|
||||
|
||||
switch (dev) {
|
||||
#if TIMER_0_EN
|
||||
case TIMER_0:
|
||||
timer = TIMER_0_DEV;
|
||||
break;
|
||||
#endif
|
||||
case TIMER_UNDEFINED:
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
switch (channel) {
|
||||
case 0:
|
||||
timer->DIER &= ~TIM_DIER_CC1IE;
|
||||
break;
|
||||
case 1:
|
||||
timer->DIER &= ~TIM_DIER_CC2IE;
|
||||
break;
|
||||
case 2:
|
||||
timer->DIER &= ~TIM_DIER_CC3IE;
|
||||
break;
|
||||
case 3:
|
||||
timer->DIER &= ~TIM_DIER_CC4IE;
|
||||
break;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned int timer_read(tim_t dev)
|
||||
{
|
||||
switch (dev) {
|
||||
#if TIMER_0_EN
|
||||
case TIMER_0:
|
||||
return TIMER_0_DEV->CNT;
|
||||
break;
|
||||
#endif
|
||||
case TIMER_UNDEFINED:
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void timer_start(tim_t dev)
|
||||
{
|
||||
switch (dev) {
|
||||
#if TIMER_0_EN
|
||||
case TIMER_0:
|
||||
TIMER_0_DEV->CR1 |= TIM_CR1_CEN;
|
||||
break;
|
||||
#endif
|
||||
case TIMER_UNDEFINED:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void timer_stop(tim_t dev)
|
||||
{
|
||||
switch (dev) {
|
||||
#if TIMER_0_EN
|
||||
case TIMER_0:
|
||||
TIMER_0_DEV->CR1 &= ~TIM_CR1_CEN;
|
||||
break;
|
||||
#endif
|
||||
case TIMER_UNDEFINED:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void timer_irq_enable(tim_t dev)
|
||||
{
|
||||
switch (dev) {
|
||||
#if TIMER_0_EN
|
||||
case TIMER_0:
|
||||
NVIC_EnableIRQ(TIMER_0_IRQ_CHAN);
|
||||
break;
|
||||
#endif
|
||||
case TIMER_UNDEFINED:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void timer_irq_disable(tim_t dev)
|
||||
{
|
||||
switch (dev) {
|
||||
#if TIMER_0_EN
|
||||
case TIMER_0:
|
||||
NVIC_DisableIRQ(TIMER_0_IRQ_CHAN);
|
||||
break;
|
||||
#endif
|
||||
case TIMER_UNDEFINED:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void timer_reset(tim_t dev)
|
||||
{
|
||||
switch (dev) {
|
||||
#if TIMER_0_EN
|
||||
case TIMER_0:
|
||||
TIMER_0_DEV->CNT = 0;
|
||||
break;
|
||||
#endif
|
||||
case TIMER_UNDEFINED:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
#if TIMER_0_EN
|
||||
__attribute__ ((naked)) void TIMER_0_ISR(void)
|
||||
{
|
||||
ISR_ENTER();
|
||||
irq_handler(TIMER_0, TIMER_0_DEV);
|
||||
ISR_EXIT();
|
||||
}
|
||||
#endif
|
||||
|
||||
static inline void irq_handler(tim_t timer, TIM_TypeDef *dev)
|
||||
{
|
||||
if (dev->SR & TIM_SR_CC1IF) {
|
||||
dev->DIER &= ~TIM_DIER_CC1IE;
|
||||
dev->SR &= ~TIM_SR_CC1IF;
|
||||
config[timer].cb(0);
|
||||
}
|
||||
else if (dev->SR & TIM_SR_CC2IF) {
|
||||
dev->DIER &= ~TIM_DIER_CC2IE;
|
||||
dev->SR &= ~TIM_SR_CC2IF;
|
||||
config[timer].cb(1);
|
||||
}
|
||||
else if (dev->SR & TIM_SR_CC3IF) {
|
||||
dev->DIER &= ~TIM_DIER_CC3IE;
|
||||
dev->SR &= ~TIM_SR_CC3IF;
|
||||
config[timer].cb(2);
|
||||
}
|
||||
else if (dev->SR & TIM_SR_CC4IF) {
|
||||
dev->DIER &= ~TIM_DIER_CC4IE;
|
||||
dev->SR &= ~TIM_SR_CC4IF;
|
||||
config[timer].cb(3);
|
||||
}
|
||||
if (sched_context_switch_request) {
|
||||
thread_yield();
|
||||
}
|
||||
}
|
@ -0,0 +1,367 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Freie Universität Berlin
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser General
|
||||
* Public License v2.1. See the file LICENSE in the top level directory for more
|
||||
* details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ingroup cpu_stm32f3
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief Low-level UART driver implementation
|
||||
*
|
||||
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include "cpu.h"
|
||||
#include "sched.h"
|
||||
#include "thread.h"
|
||||
#include "periph_conf.h"
|
||||
#include "periph/uart.h"
|
||||
|
||||
|
||||
/**
|
||||
* @brief Each UART device has to store two callbacks.
|
||||
*/
|
||||
typedef struct {
|
||||
void (*rx_cb)(char);
|
||||
void (*tx_cb)(void);
|
||||
} uart_conf_t;
|
||||
|
||||
/**
|
||||
* @brief Unified interrupt handler for all UART devices
|
||||
*
|
||||
* @param uartnum the number of the UART that triggered the ISR
|
||||
* @param uart the UART device that triggered the ISR
|
||||
*/
|
||||
static inline void irq_handler(uart_t uartnum, USART_TypeDef *uart);
|
||||
|
||||
/**
|
||||
* @brief Allocate memory to store the callback functions.
|
||||
*/
|
||||
static uart_conf_t config[UART_NUMOF];
|
||||
|
||||
int uart_init(uart_t uart, uint32_t baudrate, void (*rx_cb)(char), void (*tx_cb)(void))
|
||||
{
|
||||
/* do basic initialization */
|
||||
int res = uart_init_blocking(uart, baudrate);
|
||||
if (res < 0) {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* remember callback addresses */
|
||||
config[uart].rx_cb = rx_cb;
|
||||
config[uart].tx_cb = tx_cb;
|
||||
|
||||
/* enable receive interrupt */
|
||||
switch (uart) {
|
||||
#if UART_0_EN
|
||||
case UART_0:
|
||||
NVIC_SetPriority(UART_0_IRQ_CHAN, UART_IRQ_PRIO);
|
||||
NVIC_EnableIRQ(UART_0_IRQ_CHAN);
|
||||
UART_0_DEV->CR1 |= USART_CR1_RXNEIE;
|
||||
break;
|
||||
#endif
|
||||
#if UART_1_EN
|
||||
case UART_1:
|
||||
NVIC_SetPriority(UART_1_IRQ_CHAN, UART_IRQ_PRIO);
|
||||
NVIC_EnableIRQ(UART_1_IRQ_CHAN);
|
||||
UART_1_DEV->CR1 |= USART_CR1_RXNEIE;
|
||||
break;
|
||||
#endif
|
||||
#if UART_2_EN
|
||||
case UART_2:
|
||||
NVIC_SetPriority(UART_2_IRQ_CHAN, UART_IRQ_PRIO);
|
||||
NVIC_EnableIRQ(UART_2_IRQ_CHAN);
|
||||
UART_2_DEV->CR1 |= USART_CR1_RXNEIE;
|
||||
break;
|
||||
#endif
|
||||
case UART_UNDEFINED:
|
||||
default:
|
||||
return -2;
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int uart_init_blocking(uart_t uart, uint32_t baudrate)
|
||||
{
|
||||
USART_TypeDef *dev;
|
||||
GPIO_TypeDef *port;
|
||||
uint32_t tx_pin;
|
||||
uint32_t rx_pin;
|
||||
uint8_t af;
|
||||
float clk;
|
||||
float divider;
|
||||
uint16_t mantissa;
|
||||
uint8_t fraction;
|
||||
|
||||
switch (uart) {
|
||||
#if UART_0_EN
|
||||
case UART_0:
|
||||
dev = UART_0_DEV;
|
||||
port = UART_0_PORT;
|
||||
clk = UART_0_CLK;
|
||||
tx_pin = UART_0_TX_PIN;
|
||||
rx_pin = UART_0_RX_PIN;
|
||||
af = UART_0_AF;
|
||||
UART_0_CLKEN();
|
||||
UART_0_PORT_CLKEN();
|
||||
break;
|
||||
#endif
|
||||
#if UART_1_EN
|