[fix] update for ref/sig/cnt with 1024 point per period in test example
This commit is contained in:
parent
e6251f3f03
commit
27f528f87b
@ -13,8 +13,10 @@
|
|||||||
#define DEBUG_UART_PIN (GPIO_Pin_6)
|
#define DEBUG_UART_PIN (GPIO_Pin_6)
|
||||||
#define DEBUG_UART_BAUDRATE (2 * 1000 * 1000)
|
#define DEBUG_UART_BAUDRATE (2 * 1000 * 1000)
|
||||||
|
|
||||||
#define DEBUG_CH_RAW (0)
|
#define DEBUG_CH_REF (0)
|
||||||
#define DEBUG_CHAN_MAX (1)
|
#define DEBUG_CH_SIG (1)
|
||||||
|
#define DEBUG_CH_CNT (2)
|
||||||
|
#define DEBUG_CHAN_MAX (3)
|
||||||
|
|
||||||
struct debug_data_s {
|
struct debug_data_s {
|
||||||
uint8_t enable;
|
uint8_t enable;
|
||||||
|
|||||||
@ -5,75 +5,59 @@
|
|||||||
#include "ltc1867.h"
|
#include "ltc1867.h"
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
|
|
||||||
|
__attribute__((section(".bss_ccm"))) uint32_t ltc1867_samp_cnt = 0;
|
||||||
|
__attribute__((section(".data_ccm"))) uint16_t ltc1867_spi_data[LTC1867_CH_CNT] = {0x9400, 0x8400}; /* order: ch2, ch0 */
|
||||||
|
|
||||||
void ltc1867_init(void)
|
void ltc1867_init(void)
|
||||||
{
|
{
|
||||||
GPIO_InitTypeDef GPIO_InitStructure;
|
GPIO_InitTypeDef GPIO_InitStructure;
|
||||||
NVIC_InitTypeDef NVIC_InitStructure;
|
NVIC_InitTypeDef NVIC_InitStructure;
|
||||||
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
||||||
TIM_OCInitTypeDef TIM_OCInitStructure;
|
|
||||||
SPI_InitTypeDef SPI_InitStructure;
|
SPI_InitTypeDef SPI_InitStructure;
|
||||||
|
|
||||||
/* TIM3 16bit for IRLED and sample */
|
/* TIM2 32bit for sample */
|
||||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
|
||||||
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
|
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
|
||||||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
|
||||||
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
||||||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
||||||
NVIC_Init(&NVIC_InitStructure);
|
NVIC_Init(&NVIC_InitStructure);
|
||||||
/* Time base configuration */
|
/* Time base configuration */
|
||||||
TIM_TimeBaseStructure.TIM_Period = 65535;
|
TIM_TimeBaseStructure.TIM_Period = TIMER_PERIOD_SAMPLE;
|
||||||
TIM_TimeBaseStructure.TIM_Prescaler = TIMER_SRC_FREQ / LEDIR_FREQ / 65536 - 1;
|
TIM_TimeBaseStructure.TIM_Prescaler = 0;
|
||||||
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
|
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
|
||||||
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
||||||
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
|
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
|
||||||
TIM_ARRPreloadConfig(TIM3, ENABLE);
|
TIM_PrescalerConfig(TIM2, 0, TIM_PSCReloadMode_Immediate);
|
||||||
/* TIM3 channel3 pwm output */
|
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
|
||||||
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
|
TIM_Cmd(TIM2, ENABLE);
|
||||||
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 */
|
/* TIM3 16bit for ADC conversion */
|
||||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
|
||||||
/* Time base configuration */
|
|
||||||
TIM_TimeBaseStructure.TIM_Period = TIMER_PERIOD_DELAY;
|
TIM_TimeBaseStructure.TIM_Period = TIMER_PERIOD_DELAY;
|
||||||
TIM_TimeBaseStructure.TIM_Prescaler = 0;
|
TIM_TimeBaseStructure.TIM_Prescaler = 0;
|
||||||
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
|
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
|
||||||
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
||||||
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
|
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
|
||||||
/* Prescaler configuration */
|
TIM_PrescalerConfig(TIM3, 0, TIM_PSCReloadMode_Immediate);
|
||||||
TIM_PrescalerConfig(TIM4, 0, TIM_PSCReloadMode_Immediate);
|
TIM_Cmd(TIM3, DISABLE);
|
||||||
/* TIM4 disable counter, start when ltc1867 need delay */
|
|
||||||
TIM_Cmd(TIM4, DISABLE);
|
|
||||||
|
|
||||||
/* LEDIR pin configure */
|
/* LEDIR pin configure */
|
||||||
GPIO_InitStructure.GPIO_Pin = PIN_LEDIR;
|
GPIO_InitStructure.GPIO_Pin = PIN_LEDIR;
|
||||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
||||||
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
||||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
|
||||||
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
||||||
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
|
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
|
||||||
GPIO_Init(PORT_LEDIR, &GPIO_InitStructure);
|
GPIO_Init(PORT_LEDIR, &GPIO_InitStructure);
|
||||||
GPIO_PinAFConfig(PORT_LEDIR, GPIO_PinSource0, GPIO_AF_TIM3);
|
ltc1867_ledir_off();
|
||||||
|
|
||||||
/* LTC1867 conv pin configure */
|
/* LTC1867 conv pin configure */
|
||||||
GPIO_InitStructure.GPIO_Pin = PIN_CONV;
|
GPIO_InitStructure.GPIO_Pin = PIN_CONV;
|
||||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
||||||
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
||||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
|
||||||
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
||||||
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
|
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
|
||||||
GPIO_Init(PORT_LTC1867, &GPIO_InitStructure);
|
GPIO_Init(PORT_LTC1867, &GPIO_InitStructure);
|
||||||
/* config SPI for LTC1867 communication */
|
/* config SPI for LTC1867 communication */
|
||||||
@ -99,24 +83,39 @@ void ltc1867_init(void)
|
|||||||
SPI_InitStructure.SPI_CRCPolynomial = 7;
|
SPI_InitStructure.SPI_CRCPolynomial = 7;
|
||||||
SPI_Init(SPI1, &SPI_InitStructure);
|
SPI_Init(SPI1, &SPI_InitStructure);
|
||||||
SPI_Cmd(SPI1, ENABLE);
|
SPI_Cmd(SPI1, ENABLE);
|
||||||
// ltc1867_spi_select();
|
ltc1867_spi_select();
|
||||||
// ltc1867_transfer(ltc1867_spi_data[LTC1867_CH_CNT - 1]);
|
ltc1867_transfer(ltc1867_spi_data[LTC1867_CH_CNT - 1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ltc1867_ledir_on(void)
|
||||||
|
{
|
||||||
|
GPIO_SetBits(PORT_LEDIR, PIN_LEDIR);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ltc1867_ledir_off(void)
|
||||||
|
{
|
||||||
|
GPIO_ResetBits(PORT_LEDIR, PIN_LEDIR);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ltc1867_ledir_toggle(void)
|
||||||
|
{
|
||||||
|
GPIO_ToggleBits(PORT_LEDIR, PIN_LEDIR);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ltc1867_delay(void)
|
void ltc1867_delay(void)
|
||||||
{
|
{
|
||||||
FlagStatus sts;
|
FlagStatus sts;
|
||||||
|
|
||||||
TIM_SetCounter(TIM4, 0);
|
TIM_SetCounter(TIM3, 0);
|
||||||
TIM_ClearFlag(TIM4, TIM_FLAG_Update);
|
TIM_ClearFlag(TIM3, TIM_FLAG_Update);
|
||||||
TIM_Cmd(TIM4, ENABLE);
|
TIM_Cmd(TIM3, ENABLE);
|
||||||
while (1) {
|
while (1) {
|
||||||
sts = TIM_GetFlagStatus(TIM4, TIM_FLAG_Update);
|
sts = TIM_GetFlagStatus(TIM3, TIM_FLAG_Update);
|
||||||
if (sts != RESET) {
|
if (sts != RESET) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TIM_Cmd(TIM4, DISABLE);
|
TIM_Cmd(TIM3, DISABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ltc1867_conv(void)
|
void ltc1867_conv(void)
|
||||||
@ -149,19 +148,36 @@ uint16_t ltc1867_transfer(uint16_t data_send)
|
|||||||
return SPI_I2S_ReceiveData(SPI1);
|
return SPI_I2S_ReceiveData(SPI1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TIM3_IRQHandler(void)
|
void TIM2_IRQHandler(void)
|
||||||
{
|
{
|
||||||
uint16_t val_spi, val_tim;
|
uint16_t val[LTC1867_CH_CNT];
|
||||||
|
|
||||||
ltc1867_conv(); /* this point start convert */
|
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
|
||||||
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);
|
/* for control led IR */
|
||||||
val_tim = TIM_GetCapture4(TIM3);
|
ltc1867_samp_cnt++;
|
||||||
TIM_SetCompare4(TIM3, val_tim + TIMER_INTERVAL);
|
if (ltc1867_samp_cnt == (SAMPLES_PER_PERIOD / 2)) {
|
||||||
|
ltc1867_ledir_on();
|
||||||
|
} else if (ltc1867_samp_cnt >= SAMPLES_PER_PERIOD) {
|
||||||
|
ltc1867_ledir_off();
|
||||||
|
ltc1867_samp_cnt = 0;
|
||||||
|
}
|
||||||
|
|
||||||
debug_write_data(0, (float)val_spi);
|
/* sample multi channel adc data */
|
||||||
|
for (uint8_t i = 0; i < LTC1867_CH_CNT; i++) {
|
||||||
|
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[i] = ltc1867_transfer(ltc1867_spi_data[i]); /* read convert data and send next channel information */
|
||||||
|
}
|
||||||
|
|
||||||
|
debug_write_data(DEBUG_CH_REF, (float)val[0]);
|
||||||
|
debug_write_data(DEBUG_CH_SIG, (float)val[1]);
|
||||||
|
// debug_write_data(DEBUG_CH_CNT, (float)ltc1867_samp_cnt * (65536 / SAMPLES_PER_PERIOD));
|
||||||
|
if (ltc1867_samp_cnt < SAMPLES_PER_PERIOD / 2) {
|
||||||
|
debug_write_data(DEBUG_CH_CNT, (float)0);
|
||||||
|
} else {
|
||||||
|
debug_write_data(DEBUG_CH_CNT, (float)65535);
|
||||||
|
}
|
||||||
debug_send_frame();
|
debug_send_frame();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,9 +8,12 @@
|
|||||||
#include "stm32f4xx.h"
|
#include "stm32f4xx.h"
|
||||||
|
|
||||||
#define LEDIR_FREQ (8)
|
#define LEDIR_FREQ (8)
|
||||||
|
#define SAMPLES_PER_PERIOD (1024)
|
||||||
#define TIMER_SRC_FREQ (84 * 1000 * 1000)
|
#define TIMER_SRC_FREQ (84 * 1000 * 1000)
|
||||||
|
#define TIMER_PERIOD_SAMPLE (TIMER_SRC_FREQ / LEDIR_FREQ / SAMPLES_PER_PERIOD - 1)
|
||||||
#define TIMER_PERIOD_DELAY (TIMER_SRC_FREQ / 1000 / 1000 * 35 / 10 - 1) /* 3.5us */
|
#define TIMER_PERIOD_DELAY (TIMER_SRC_FREQ / 1000 / 1000 * 35 / 10 - 1) /* 3.5us */
|
||||||
#define TIMER_INTERVAL (32)
|
|
||||||
|
#define LTC1867_CH_CNT (2)
|
||||||
|
|
||||||
#define PORT_LEDIR (GPIOB)
|
#define PORT_LEDIR (GPIOB)
|
||||||
#define PIN_LEDIR (GPIO_Pin_0)
|
#define PIN_LEDIR (GPIO_Pin_0)
|
||||||
@ -23,6 +26,9 @@
|
|||||||
#define PIN_SPI_SDI (GPIO_Pin_7)
|
#define PIN_SPI_SDI (GPIO_Pin_7)
|
||||||
|
|
||||||
void ltc1867_init(void);
|
void ltc1867_init(void);
|
||||||
|
void ltc1867_ledir_on(void);
|
||||||
|
void ltc1867_ledir_off(void);
|
||||||
|
void ltc1867_ledir_toggle(void);
|
||||||
void ltc1867_delay(void);
|
void ltc1867_delay(void);
|
||||||
void ltc1867_conv(void);
|
void ltc1867_conv(void);
|
||||||
void ltc1867_spi_select(void);
|
void ltc1867_spi_select(void);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user