Adding external clock for STM32F4 (pin PA15)

This commit is contained in:
Andy CA6JAU 2016-12-01 01:04:40 -03:00
parent b49747b282
commit 7607c81994
1 changed files with 27 additions and 2 deletions

View File

@ -40,6 +40,8 @@ P25 PD11 output
RX PA0 analog input RX PA0 analog input
RSSI PA1 analog input RSSI PA1 analog input
TX PA4 analog output TX PA4 analog output
EXT_CLK PA15 input
*/ */
#define PIN_COS GPIO_Pin_5 #define PIN_COS GPIO_Pin_5
@ -97,6 +99,8 @@ P25 PC9 output
RX PA0 analog input RX PA0 analog input
RSSI PA7 analog input RSSI PA7 analog input
TX PA4 analog output TX PA4 analog output
EXT_CLK PA15 input
*/ */
#define PIN_COS GPIO_Pin_0 #define PIN_COS GPIO_Pin_0
@ -144,6 +148,8 @@ TX PA4 analog output
const uint16_t DC_OFFSET = 2048U; const uint16_t DC_OFFSET = 2048U;
#define SAMP_FREQ 24000
extern "C" { extern "C" {
void TIM2_IRQHandler() { void TIM2_IRQHandler() {
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) { if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) {
@ -306,19 +312,38 @@ void CIO::startInt()
DAC_Cmd(DAC_Channel_1, ENABLE); DAC_Cmd(DAC_Channel_1, ENABLE);
// Init the timer // Init the timer
#if defined(EXTERNAL_OSC)
// Configure GPIO PA15 as external TIM2 clock source
GPIO_PinAFConfig(GPIOA, GPIO_PinSource15, GPIO_AF_TIM2);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_Init(GPIOA, &GPIO_InitStructure);
#endif
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseInitTypeDef timerInitStructure; TIM_TimeBaseInitTypeDef timerInitStructure;
TIM_TimeBaseStructInit (&timerInitStructure); TIM_TimeBaseStructInit (&timerInitStructure);
// TIM2 at 24 kHz // TIM2 frequency
timerInitStructure.TIM_Prescaler = (uint16_t) ((SystemCoreClock/(4*24000)) - 1); #if defined(EXTERNAL_OSC)
timerInitStructure.TIM_Prescaler = (uint16_t) ((EXTERNAL_OSC/SAMP_FREQ) - 1);
#else
timerInitStructure.TIM_Prescaler = (uint16_t) ((SystemCoreClock/(4*SAMP_FREQ)) - 1);
#endif
timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up; timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
timerInitStructure.TIM_Period = 1; timerInitStructure.TIM_Period = 1;
timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
timerInitStructure.TIM_RepetitionCounter = 0; timerInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &timerInitStructure); TIM_TimeBaseInit(TIM2, &timerInitStructure);
// Enable external clock
#if defined(EXTERNAL_OSC)
TIM_ETRClockMode2Config(TIM2, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0x00);
#endif
TIM_Cmd(TIM2, ENABLE); TIM_Cmd(TIM2, ENABLE);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);