168 lines
6.2 KiB
C
Executable File
168 lines
6.2 KiB
C
Executable File
#include "stm32f4xx_rcc.h"
|
|
#include "stm32f4xx_gpio.h"
|
|
#include "stm32f4xx_tim.h"
|
|
#include "stm32f4xx_spi.h"
|
|
#include "ltc1867.h"
|
|
#include "debug.h"
|
|
|
|
void ltc1867_init(void)
|
|
{
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
NVIC_InitTypeDef NVIC_InitStructure;
|
|
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
|
TIM_OCInitTypeDef TIM_OCInitStructure;
|
|
SPI_InitTypeDef SPI_InitStructure;
|
|
|
|
/* TIM3 16bit for IRLED and sample */
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
|
|
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
|
NVIC_Init(&NVIC_InitStructure);
|
|
/* Time base configuration */
|
|
TIM_TimeBaseStructure.TIM_Period = 65535;
|
|
TIM_TimeBaseStructure.TIM_Prescaler = TIMER_SRC_FREQ / LEDIR_FREQ / 65536 - 1;
|
|
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
|
|
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
|
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
|
|
TIM_ARRPreloadConfig(TIM3, ENABLE);
|
|
/* TIM3 channel3 pwm output */
|
|
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
|
|
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
|
|
TIM_OCInitStructure.TIM_Pulse = 32768;
|
|
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
|
|
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
|
|
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
|
|
/* TIM3 channel4 for sample interval */
|
|
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;
|
|
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;
|
|
TIM_OCInitStructure.TIM_Pulse = 0;
|
|
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
|
|
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
|
|
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Disable);
|
|
TIM_ITConfig(TIM3, TIM_IT_CC4, ENABLE);
|
|
/* TIM2 enable counter */
|
|
TIM_Cmd(TIM3, ENABLE);
|
|
|
|
/* TIM4 16bit for ADC conversion */
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
|
|
/* Time base configuration */
|
|
TIM_TimeBaseStructure.TIM_Period = TIMER_PERIOD_DELAY;
|
|
TIM_TimeBaseStructure.TIM_Prescaler = 0;
|
|
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
|
|
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
|
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
|
|
/* Prescaler configuration */
|
|
TIM_PrescalerConfig(TIM4, 0, TIM_PSCReloadMode_Immediate);
|
|
/* TIM4 disable counter, start when ltc1867 need delay */
|
|
TIM_Cmd(TIM4, DISABLE);
|
|
|
|
/* LEDIR pin configure */
|
|
GPIO_InitStructure.GPIO_Pin = PIN_LEDIR;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
|
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
|
|
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
|
|
GPIO_Init(PORT_LEDIR, &GPIO_InitStructure);
|
|
GPIO_PinAFConfig(PORT_LEDIR, GPIO_PinSource0, GPIO_AF_TIM3);
|
|
|
|
/* LTC1867 conv pin configure */
|
|
GPIO_InitStructure.GPIO_Pin = PIN_CONV;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
|
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
|
|
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
|
|
GPIO_Init(PORT_LTC1867, &GPIO_InitStructure);
|
|
/* config SPI for LTC1867 communication */
|
|
GPIO_PinAFConfig(PORT_SPI, GPIO_PinSource5, GPIO_AF_SPI1);
|
|
GPIO_PinAFConfig(PORT_SPI, GPIO_PinSource6, GPIO_AF_SPI1);
|
|
GPIO_PinAFConfig(PORT_SPI, GPIO_PinSource7, GPIO_AF_SPI1);
|
|
GPIO_InitStructure.GPIO_Pin = PIN_SPI_SCK | PIN_SPI_SDI | PIN_SPI_SDO;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
|
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
|
|
GPIO_Init(PORT_SPI, &GPIO_InitStructure);
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
|
|
SPI_I2S_DeInit(SPI1);
|
|
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
|
|
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
|
|
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
|
|
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
|
|
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
|
|
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
|
|
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
|
|
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
|
|
SPI_InitStructure.SPI_CRCPolynomial = 7;
|
|
SPI_Init(SPI1, &SPI_InitStructure);
|
|
SPI_Cmd(SPI1, ENABLE);
|
|
// ltc1867_spi_select();
|
|
// ltc1867_transfer(ltc1867_spi_data[LTC1867_CH_CNT - 1]);
|
|
}
|
|
|
|
void ltc1867_delay(void)
|
|
{
|
|
FlagStatus sts;
|
|
|
|
TIM_SetCounter(TIM4, 0);
|
|
TIM_ClearFlag(TIM4, TIM_FLAG_Update);
|
|
TIM_Cmd(TIM4, ENABLE);
|
|
while (1) {
|
|
sts = TIM_GetFlagStatus(TIM4, TIM_FLAG_Update);
|
|
if (sts != RESET) {
|
|
break;
|
|
}
|
|
}
|
|
TIM_Cmd(TIM4, DISABLE);
|
|
}
|
|
|
|
void ltc1867_conv(void)
|
|
{
|
|
GPIO_SetBits(PORT_LTC1867, PIN_CONV);
|
|
}
|
|
|
|
void ltc1867_spi_select(void)
|
|
{
|
|
GPIO_ResetBits(PORT_LTC1867, PIN_SPI_CS);
|
|
}
|
|
|
|
uint16_t ltc1867_transfer(uint16_t data_send)
|
|
{
|
|
FlagStatus flag;
|
|
|
|
while (1) {
|
|
flag = SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE);
|
|
if (flag != RESET) {
|
|
break;
|
|
}
|
|
}
|
|
SPI_I2S_SendData(SPI1, data_send);
|
|
while (1) {
|
|
flag = SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE);
|
|
if (flag != RESET) {
|
|
break;
|
|
}
|
|
}
|
|
return SPI_I2S_ReceiveData(SPI1);
|
|
}
|
|
|
|
void TIM3_IRQHandler(void)
|
|
{
|
|
uint16_t val_spi, val_tim;
|
|
|
|
ltc1867_conv(); /* this point start convert */
|
|
ltc1867_delay(); /* delay 3.5us for convert time, reference for LTC1867 datasheet */
|
|
ltc1867_spi_select(); /* convert complete, make convert pin low for reading sample data by SPI */
|
|
val_spi = ltc1867_transfer(0x8400); /* read convert data and send next channel information */
|
|
|
|
TIM_ClearITPendingBit(TIM3, TIM_IT_CC4);
|
|
val_tim = TIM_GetCapture4(TIM3);
|
|
TIM_SetCompare4(TIM3, val_tim + TIMER_INTERVAL);
|
|
|
|
debug_write_data(0, (float)val_spi);
|
|
debug_send_frame();
|
|
}
|