2016-03-16 12:13:47 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 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_cortexm_common
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief Shared CPU specific function for the STM32 CPU family
|
|
|
|
*
|
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2016-12-07 15:32:46 +01:00
|
|
|
#include "periph_conf.h"
|
2016-03-16 12:13:47 +01:00
|
|
|
#include "periph_cpu_common.h"
|
|
|
|
|
2016-08-29 17:21:36 +02:00
|
|
|
#define ENABLE_DEBUG (0)
|
|
|
|
#include "debug.h"
|
|
|
|
|
2017-11-10 17:25:13 +01:00
|
|
|
/**
|
|
|
|
* @brief Timer specific additional bus clock prescaler
|
|
|
|
*
|
|
|
|
* This prescale factor is dependent on the actual APBx bus clock divider, if
|
|
|
|
* the APBx presacler is != 1, it is set to 2, if the APBx prescaler is == 1, it
|
|
|
|
* is set to 1.
|
|
|
|
*
|
|
|
|
* See reference manuals section 'reset and clock control'.
|
|
|
|
*/
|
|
|
|
static const uint8_t apbmul[] = {
|
|
|
|
#if (CLOCK_APB1 < CLOCK_CORECLOCK)
|
|
|
|
[APB1] = 2,
|
|
|
|
#else
|
|
|
|
[APB1] = 1,
|
|
|
|
#endif
|
|
|
|
#if (CLOCK_APB2 < CLOCK_CORECLOCK)
|
|
|
|
[APB2] = 2
|
|
|
|
#else
|
|
|
|
[APB2] = 1
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2016-12-07 15:32:46 +01:00
|
|
|
uint32_t periph_apb_clk(uint8_t bus)
|
|
|
|
{
|
|
|
|
if (bus == APB1) {
|
|
|
|
return CLOCK_APB1;
|
|
|
|
}
|
2018-05-21 21:46:32 +02:00
|
|
|
#if defined (CPU_FAM_STM32L4)
|
|
|
|
else if (bus == APB12) {
|
|
|
|
return CLOCK_APB1;
|
|
|
|
}
|
|
|
|
#endif
|
2016-12-07 15:32:46 +01:00
|
|
|
else {
|
|
|
|
return CLOCK_APB2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 17:25:13 +01:00
|
|
|
uint32_t periph_timer_clk(uint8_t bus)
|
|
|
|
{
|
|
|
|
return periph_apb_clk(bus) * apbmul[bus];
|
|
|
|
}
|
|
|
|
|
2016-08-29 17:21:36 +02:00
|
|
|
void periph_clk_en(bus_t bus, uint32_t mask)
|
2016-03-16 12:13:47 +01:00
|
|
|
{
|
2016-08-29 17:21:36 +02:00
|
|
|
switch (bus) {
|
|
|
|
case APB1:
|
2017-03-29 14:09:54 +02:00
|
|
|
#if defined(CPU_FAM_STM32L4)
|
|
|
|
RCC->APB1ENR1 |= mask;
|
|
|
|
#else
|
2016-08-29 17:21:36 +02:00
|
|
|
RCC->APB1ENR |= mask;
|
2017-03-29 14:09:54 +02:00
|
|
|
#endif
|
2016-08-29 17:21:36 +02:00
|
|
|
break;
|
|
|
|
case APB2:
|
|
|
|
RCC->APB2ENR |= mask;
|
|
|
|
break;
|
2018-05-21 21:46:32 +02:00
|
|
|
#if defined(CPU_FAM_STM32L4)
|
|
|
|
case APB12:
|
|
|
|
RCC->APB1ENR2 |= mask;
|
|
|
|
break;
|
|
|
|
#endif
|
2017-02-12 16:24:48 +01:00
|
|
|
#if defined(CPU_FAM_STM32L0)
|
|
|
|
case AHB:
|
|
|
|
RCC->AHBENR |= mask;
|
|
|
|
break;
|
|
|
|
case IOP:
|
|
|
|
RCC->IOPENR |= mask;
|
|
|
|
break;
|
|
|
|
#elif defined(CPU_FAM_STM32L1) || defined(CPU_FAM_STM32F1) \
|
2017-04-13 14:32:34 +02:00
|
|
|
|| defined(CPU_FAM_STM32F0) || defined(CPU_FAM_STM32F3)
|
2016-08-29 17:21:36 +02:00
|
|
|
case AHB:
|
|
|
|
RCC->AHBENR |= mask;
|
|
|
|
break;
|
2017-03-29 14:09:54 +02:00
|
|
|
#elif defined(CPU_FAM_STM32F2) || defined(CPU_FAM_STM32F4) \
|
2017-04-13 14:32:34 +02:00
|
|
|
|| defined(CPU_FAM_STM32L4) || defined(CPU_FAM_STM32F7)
|
2016-08-29 17:21:36 +02:00
|
|
|
case AHB1:
|
|
|
|
RCC->AHB1ENR |= mask;
|
|
|
|
break;
|
2017-02-03 13:28:22 +01:00
|
|
|
/* STM32F410 RCC doesn't provide AHB2 and AHB3 */
|
2018-07-09 11:51:39 +02:00
|
|
|
#if !defined(CPU_LINE_STM32F410Rx)
|
2016-08-29 17:21:36 +02:00
|
|
|
case AHB2:
|
|
|
|
RCC->AHB2ENR |= mask;
|
|
|
|
break;
|
|
|
|
case AHB3:
|
|
|
|
RCC->AHB3ENR |= mask;
|
|
|
|
break;
|
2017-02-03 13:28:22 +01:00
|
|
|
#endif
|
2016-08-29 17:21:36 +02:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
DEBUG("unsupported bus %d\n", (int)bus);
|
|
|
|
break;
|
2016-03-16 12:13:47 +01:00
|
|
|
}
|
2016-08-29 17:21:36 +02:00
|
|
|
/* stm32xx-errata: Delay after a RCC peripheral clock enable */
|
|
|
|
__DSB();
|
2016-03-16 12:13:47 +01:00
|
|
|
}
|
|
|
|
|
2016-08-29 17:21:36 +02:00
|
|
|
void periph_clk_dis(bus_t bus, uint32_t mask)
|
2016-03-16 12:13:47 +01:00
|
|
|
{
|
2016-08-29 17:21:36 +02:00
|
|
|
switch (bus) {
|
|
|
|
case APB1:
|
2017-03-29 14:09:54 +02:00
|
|
|
#if defined(CPU_FAM_STM32L4)
|
|
|
|
RCC->APB1ENR1 &= ~(mask);
|
|
|
|
#else
|
2016-08-29 17:21:36 +02:00
|
|
|
RCC->APB1ENR &= ~(mask);
|
2017-03-29 14:09:54 +02:00
|
|
|
#endif
|
2016-08-29 17:21:36 +02:00
|
|
|
break;
|
|
|
|
case APB2:
|
|
|
|
RCC->APB2ENR &= ~(mask);
|
|
|
|
break;
|
2018-05-21 21:46:32 +02:00
|
|
|
#if defined(CPU_FAM_STM32L4)
|
|
|
|
case APB12:
|
|
|
|
RCC->APB1ENR2 &= ~(mask);
|
|
|
|
break;
|
|
|
|
#endif
|
2017-02-12 16:24:48 +01:00
|
|
|
#if defined(CPU_FAM_STM32L0)
|
|
|
|
case AHB:
|
|
|
|
RCC->AHBENR &= ~(mask);
|
|
|
|
break;
|
|
|
|
case IOP:
|
|
|
|
RCC->IOPENR &= ~(mask);
|
|
|
|
break;
|
|
|
|
#elif defined(CPU_FAM_STM32L1) || defined(CPU_FAM_STM32F1) \
|
2017-04-13 14:32:34 +02:00
|
|
|
|| defined(CPU_FAM_STM32F0) || defined(CPU_FAM_STM32F3)
|
2016-08-29 17:21:36 +02:00
|
|
|
case AHB:
|
|
|
|
RCC->AHBENR &= ~(mask);
|
|
|
|
break;
|
2017-03-29 14:09:54 +02:00
|
|
|
#elif defined(CPU_FAM_STM32F2) || defined(CPU_FAM_STM32F4) \
|
2017-04-13 14:32:34 +02:00
|
|
|
|| defined(CPU_FAM_STM32L4) || defined(CPU_FAM_STM32F7)
|
2016-08-29 17:21:36 +02:00
|
|
|
case AHB1:
|
|
|
|
RCC->AHB1ENR &= ~(mask);
|
|
|
|
break;
|
2017-02-03 13:28:22 +01:00
|
|
|
/* STM32F410 RCC doesn't provide AHB2 and AHB3 */
|
2018-07-09 11:51:39 +02:00
|
|
|
#if !defined(CPU_LINE_STM32F410Rx)
|
2016-08-29 17:21:36 +02:00
|
|
|
case AHB2:
|
|
|
|
RCC->AHB2ENR &= ~(mask);
|
|
|
|
break;
|
|
|
|
case AHB3:
|
|
|
|
RCC->AHB3ENR &= ~(mask);
|
|
|
|
break;
|
2017-02-03 13:28:22 +01:00
|
|
|
#endif
|
2016-08-29 17:21:36 +02:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
DEBUG("unsupported bus %d\n", (int)bus);
|
|
|
|
break;
|
2016-03-16 12:13:47 +01:00
|
|
|
}
|
|
|
|
}
|