2024-03-16 22:28:04 +08:00
|
|
|
#include "ltc1867.h"
|
|
|
|
|
#include "filter.h"
|
|
|
|
|
#include "device.h"
|
|
|
|
|
#include "cali.h"
|
|
|
|
|
#include "cali_flash.h"
|
|
|
|
|
#include "debug.h"
|
|
|
|
|
#include "cli.h"
|
|
|
|
|
#include "stm32f4xx_rcc.h"
|
|
|
|
|
#include "stm32f4xx_gpio.h"
|
|
|
|
|
#include "stm32f4xx_tim.h"
|
|
|
|
|
#include "stm32f4xx_spi.h"
|
|
|
|
|
|
|
|
|
|
#define diff32768(data) ((data > 32768) ? (data - 32768) : (32768 - data))
|
|
|
|
|
|
|
|
|
|
struct data_ltc1867_s data_ltc1867 __attribute__((section(".bss_ccm")));
|
2024-04-04 16:16:41 +08:00
|
|
|
static const uint16_t ltc1867_spi_data[LTC1867_CH_CNT] = {0xC400, 0x9400, 0xD400, 0xA400, 0x8400}; /* order: ch1, ch2, ch3, ch4, ch0 */
|
2024-03-16 22:28:04 +08:00
|
|
|
|
|
|
|
|
void ltc1867_init(void)
|
|
|
|
|
{
|
|
|
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
|
|
|
NVIC_InitTypeDef NVIC_InitStructure;
|
|
|
|
|
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
|
|
|
|
SPI_InitTypeDef SPI_InitStructure;
|
|
|
|
|
|
|
|
|
|
/* TIM2 32bit for sample */
|
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
|
|
|
|
NVIC_Init(&NVIC_InitStructure);
|
|
|
|
|
// NVIC_SetPriority(PendSV_IRQn, 1); /* priority is just lower than sample, for write cali data to flash */
|
|
|
|
|
/* Time base configuration */
|
|
|
|
|
TIM_TimeBaseStructure.TIM_Period = TIMER_PERIOD_SAMPLE;
|
|
|
|
|
TIM_TimeBaseStructure.TIM_Prescaler = 0;
|
|
|
|
|
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
|
|
|
|
|
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
|
|
|
|
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
|
|
|
|
|
/* Prescaler configuration */
|
|
|
|
|
TIM_PrescalerConfig(TIM2, 0, TIM_PSCReloadMode_Immediate);
|
|
|
|
|
/* TIM2 Interrupts enable */
|
|
|
|
|
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
|
|
|
|
|
/* TIM2 enable counter */
|
|
|
|
|
TIM_Cmd(TIM2, ENABLE);
|
|
|
|
|
|
|
|
|
|
/* TIM3 16bit for ADC conversion */
|
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, 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(TIM3, &TIM_TimeBaseStructure);
|
|
|
|
|
/* Prescaler configuration */
|
|
|
|
|
TIM_PrescalerConfig(TIM3, 0, TIM_PSCReloadMode_Immediate);
|
|
|
|
|
/* TIM3 disable counter, start when ltc1867 need delay */
|
|
|
|
|
TIM_Cmd(TIM3, DISABLE);
|
|
|
|
|
|
|
|
|
|
/* LEDIR pin configure */
|
|
|
|
|
GPIO_InitStructure.GPIO_Pin = PIN_LEDIR;
|
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
|
|
|
|
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
|
|
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
|
|
|
|
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
|
|
|
|
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
|
|
|
|
|
GPIO_Init(PORT_LEDIR, &GPIO_InitStructure);
|
|
|
|
|
ltc1867_ledir_off();
|
|
|
|
|
|
|
|
|
|
/* 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_25MHz;
|
|
|
|
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
|
|
|
|
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();
|
2024-04-04 16:16:41 +08:00
|
|
|
ltc1867_transfer(ltc1867_spi_data[LTC1867_CH_CNT - 1]);
|
2024-03-16 22:28:04 +08:00
|
|
|
|
|
|
|
|
/* data_ltc1867 init */
|
|
|
|
|
data_ltc1867.sum[0] = 0; /* store temporary sum of half period sum */
|
|
|
|
|
data_ltc1867.sum[1] = 0;
|
|
|
|
|
data_ltc1867.sum[2] = 0;
|
|
|
|
|
data_ltc1867.sum[3] = 0;
|
|
|
|
|
data_ltc1867.calc_flag = 0; /* half period complete, need calc */
|
|
|
|
|
data_ltc1867.ledir_cnt = 0; /* index of led count, for togglr IR led */
|
|
|
|
|
data_ltc1867.val_idx = 0; /* index of value in half period, for sum */
|
|
|
|
|
data_ltc1867.warm = SAMPLES_PER_PERIOD * LEDIR_FREQ; /* in first 1s, only toggle IR led, discard sample ADC value */
|
|
|
|
|
data_ltc1867.locked = 0; /* zero point capture */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
FlagStatus sts;
|
|
|
|
|
|
|
|
|
|
TIM_SetCounter(TIM3, 0);
|
|
|
|
|
TIM_ClearFlag(TIM3, TIM_FLAG_Update);
|
|
|
|
|
TIM_Cmd(TIM3, ENABLE);
|
|
|
|
|
while (1) {
|
|
|
|
|
sts = TIM_GetFlagStatus(TIM3, TIM_FLAG_Update);
|
|
|
|
|
if (sts != RESET) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
TIM_Cmd(TIM3, 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 TIM2_IRQHandler(void)
|
|
|
|
|
{
|
2024-04-04 16:16:41 +08:00
|
|
|
uint16_t val[LTC1867_CH_CNT];
|
2024-03-16 22:28:04 +08:00
|
|
|
|
|
|
|
|
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
|
|
|
|
|
|
|
|
|
|
/* for control led IR */
|
|
|
|
|
data_ltc1867.ledir_cnt++;
|
|
|
|
|
if (data_ltc1867.ledir_cnt == (SAMPLES_PER_PERIOD / 2)) {
|
|
|
|
|
ltc1867_ledir_on();
|
|
|
|
|
} else if (data_ltc1867.ledir_cnt >= SAMPLES_PER_PERIOD) {
|
|
|
|
|
ltc1867_ledir_off();
|
|
|
|
|
data_ltc1867.ledir_cnt = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-04 16:16:41 +08:00
|
|
|
/* sample 5 channel adc data */
|
|
|
|
|
for (uint8_t i = 0; i < LTC1867_CH_CNT; i++) {
|
2024-03-16 22:28:04 +08:00
|
|
|
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 */
|
|
|
|
|
}
|
|
|
|
|
/* split 2 for loop, because sample timing is strict, only place important code in sample duration */
|
|
|
|
|
for (uint8_t i = 0; i < 4; i++) {
|
|
|
|
|
data_ltc1867.sum[i] += diff32768(val[i]); /* add every channel original data for next process */
|
2024-04-04 16:16:41 +08:00
|
|
|
debug_write_data(i, (float)val[i]); /* channel 0~3 for ltc1867 original sample data */
|
2024-03-16 22:28:04 +08:00
|
|
|
}
|
2024-04-04 16:16:41 +08:00
|
|
|
adc_det_temp_from_ltc1867 = val[4]; /* channel 4 for ltc1867 detector temperature sample data */
|
2024-03-16 22:28:04 +08:00
|
|
|
data_ltc1867.val_idx++;
|
|
|
|
|
if ((data_ltc1867.val_idx >= CALC_CNT) && (data_ltc1867.locked != 0)) {
|
|
|
|
|
data_ltc1867.val_idx = 0;
|
|
|
|
|
if (cli_data_comb.force_available & CLI_FORCE_FILTER_INPUT1_MASK) {
|
|
|
|
|
filter_input_data(0, cli_data_comb.force_data[CLI_FORCE_FILTER_INPUT1] / FILTER_SUM_LEN_GAS1);
|
|
|
|
|
} else {
|
|
|
|
|
filter_input_data(0, data_ltc1867.sum[0]);
|
|
|
|
|
}
|
|
|
|
|
if (cli_data_comb.force_available & CLI_FORCE_FILTER_INPUT2_MASK) {
|
|
|
|
|
filter_input_data(1, cli_data_comb.force_data[CLI_FORCE_FILTER_INPUT2] / FILTER_SUM_LEN_GAS2);
|
|
|
|
|
} else {
|
|
|
|
|
filter_input_data(1, data_ltc1867.sum[1]);
|
|
|
|
|
}
|
|
|
|
|
if (cli_data_comb.force_available & CLI_FORCE_FILTER_INPUT3_MASK) {
|
|
|
|
|
filter_input_data(2, cli_data_comb.force_data[CLI_FORCE_FILTER_INPUT3] / FILTER_SUM_LEN_GAS3);
|
|
|
|
|
} else {
|
|
|
|
|
filter_input_data(2, data_ltc1867.sum[2]);
|
|
|
|
|
}
|
|
|
|
|
if (cli_data_comb.force_available & CLI_FORCE_FILTER_INPUT4_MASK) {
|
|
|
|
|
filter_input_data(3, cli_data_comb.force_data[CLI_FORCE_FILTER_INPUT4] / FILTER_SUM_LEN_GAS4);
|
|
|
|
|
} else {
|
|
|
|
|
filter_input_data(3, data_ltc1867.sum[3]);
|
|
|
|
|
}
|
|
|
|
|
data_ltc1867.sum[0] = 0;
|
|
|
|
|
data_ltc1867.sum[1] = 0;
|
|
|
|
|
data_ltc1867.sum[2] = 0;
|
|
|
|
|
data_ltc1867.sum[3] = 0;
|
|
|
|
|
data_ltc1867.calc_flag++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data_ltc1867.warm > 0) {
|
|
|
|
|
/* in warm stage */
|
|
|
|
|
data_ltc1867.warm--;
|
|
|
|
|
} else {
|
|
|
|
|
/* warm complete */
|
|
|
|
|
if (data_ltc1867.locked > 0) {
|
|
|
|
|
/* nothing, normal running state */
|
|
|
|
|
} else {
|
|
|
|
|
/* has not locked wave */
|
|
|
|
|
static uint16_t pre_ref = 32768;
|
|
|
|
|
if ((pre_ref < 32768) && (val[0] >= 32768)) {
|
|
|
|
|
/* meet lock condition */
|
|
|
|
|
data_ltc1867.sum[0] = diff32768(val[0]);
|
|
|
|
|
data_ltc1867.sum[1] = diff32768(val[1]);
|
|
|
|
|
data_ltc1867.sum[2] = diff32768(val[2]);
|
|
|
|
|
data_ltc1867.sum[3] = diff32768(val[3]);
|
|
|
|
|
data_ltc1867.val_idx = 1;
|
|
|
|
|
data_ltc1867.locked = 1;
|
|
|
|
|
} else {
|
|
|
|
|
/* not meet lock condition */
|
|
|
|
|
pre_ref = val[0];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cali_flash_write();
|
|
|
|
|
debug_send_frame();
|
|
|
|
|
}
|