F203/Core/Src/tim.c

416 lines
11 KiB
C
Raw Blame History

/**
******************************************************************************
* File Name : TIM.c
* Description : This file provides code for the configuration
* of the TIM instances.
******************************************************************************
* This notice applies to any and all portions of this file
* that are not between comment pairs USER CODE BEGIN and
* USER CODE END. Other portions of this file, whether
* inserted by the user or by software development tools
* are owned by their respective copyright owners.
*
* Copyright (c) 2018 STMicroelectronics International N.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted, provided that the following conditions are met:
*
* 1. Redistribution of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of other
* contributors to this software may be used to endorse or promote products
* derived from this software without specific written permission.
* 4. This software, including modifications and/or derivative works of this
* software, must execute solely and exclusively on microcontroller or
* microprocessor devices manufactured by or for STMicroelectronics.
* 5. Redistribution and use of this software other than as permitted under
* this license is void and will automatically terminate your rights under
* this license.
*
* THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
* RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
* SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "tim.h"
#include "adc.h"
#include "arm_math.h"
#include "my.h"
/* USER CODE BEGIN 0 */
#define TIM2_SOURCE ((uint32_t) TIM2)
#define TIM6_SOURCE ((uint32_t) TIM6)
#define TIM7_SOURCE ((uint32_t) TIM7)
/* USER CODE END 0 */
extern __IO bool AT_OK;
extern __IO uint32_t pWr, pRd; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> (<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>) <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
extern __IO uint32_t pCur; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> (<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>) <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
extern volatile uint32_t TimerRefreshLCD;
extern volatile uint32_t adcThr, _adcThr;
extern volatile uint32_t StateThr;
extern volatile uint32_t ZeroThr;
extern __IO bool ChargerIsConnect;
extern volatile uint8_t menu;
extern volatile uint8_t menupos;
extern volatile uint32_t MAX_CNT_ADC;
extern volatile uint32_t Stability;
extern volatile uint32_t timeRelax;
extern volatile bool SeeTemperature;
extern volatile bool FlagGetTemperature;
extern volatile uint32_t STATUS;
extern volatile uint32_t KEY_STATE;
extern volatile bool Recording;
extern volatile UserData_TypeDef pardata, pardata_old;
extern volatile bool screen, Redrawing;
extern volatile uint16_t cntFs;
extern volatile uint16_t faseADC;
extern volatile uint16_t cntADC;
extern volatile uint32_t Ns;
extern volatile uint16_t Nr;
extern volatile uint32_t Fs;
extern volatile uint16_t repeat;
extern volatile uint8_t Flag_Set_KU;
extern volatile uint8_t READY;
extern volatile uint16_t cntmeas;
extern volatile uint16_t lastcntmeas;
extern volatile uint32_t NSamples;
extern volatile uint16_t measlen;
extern volatile bool USBConnected, ChangeUSBState, SavingPassed, DrawStatus, ThisFileExist, SavingUnable;
extern volatile bool RepeatKm;
extern volatile bool firstmeas;
extern volatile bool FirstTime;
extern volatile bool measuring;
extern volatile bool RefreshScreen, RefreshNums;
extern volatile bool Recording;
extern volatile bool Threshold;
extern volatile bool needDispOn;
TIM_HandleTypeDef htim1;
TIM_HandleTypeDef htim2;
TIM_HandleTypeDef htim5;
TIM_HandleTypeDef htim6;
TIM_HandleTypeDef htim7;
TIM_HandleTypeDef htim10;
extern __IO uint16_t cntmeas1;
__IO ulong_t AdcSample;
__IO uint32_t sampleCnt = 0;
__IO int32_t sampleDiff = 0;
__IO uint32_t periodCmplt = 0;
__IO uint32_t TimeRec = 0;
__IO bool DataCopied = false;
__IO uint8_t blink = 0;
__IO uint8_t Cursorblink = 0;
__IO uint16_t cntblink = 2;
__IO uint8_t Flag_Get_Vacc = 1;
__IO uint8_t cntBat = 0;
__IO uint32_t dc = 0;
__IO bool FlagAsk = false;
__IO uint16_t timer1s = sec1;
__IO uint16_t timer500ms = msec500;
__IO uint16_t timer250ms = msec250;
__IO uint16_t timerKeys = msecKeys;
__IO uint16_t timerRefreshScreen = msecRefresh;
__IO uint16_t timerSend = msecSend;
__IO uint16_t timerTemperature = sec1;
__IO uint16_t timerStatus = 0;
__IO uint16_t timerAsk = msec800;
__IO uint16_t timerBattery = sec1;
__IO bool needBattery = true;
__IO uint32_t TIMEOUT_OFF = min15;
__IO uint32_t TIMEOUT_OFF2 = min1;
__IO uint32_t TIMEOUT_BAT = sec5 + sec2;
__IO bool FLAG_CALC_MEAN = false;
__IO float64_t REC_MEAN = 0;
__IO float32_t MEAN = 8388608.0f;
__IO float32_t MEAN_TEMP = 8388608.0f;
__IO uint32_t CNT_MEAN = 0;
/* TIM5 init function */
void MX_TIM1_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;
htim1.Instance = TIM1;
htim1.Init.Prescaler = 0; // (96000000Hz / 10000) = 9600Hz
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = (10800 - 1); // (216000000 / 10800) ~20kHz
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if(HAL_TIM_Base_Init(&htim1) != HAL_OK) {
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if(HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK) {
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE;
sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_UPDATE;
if(HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK) {
Error_Handler();
}
}
/* TIM5 init function */
void MX_TIM5_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;
htim5.Instance = TIM5;
htim5.Init.Prescaler = 0; // (96000000Hz / 10000) = 9600Hz
htim5.Init.CounterMode = TIM_COUNTERMODE_UP;
htim5.Init.Period = (432 - 1); // (96000000Hz / 240) ~400kHz
htim5.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim5.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if(HAL_TIM_Base_Init(&htim5) != HAL_OK) {
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if(HAL_TIM_ConfigClockSource(&htim5, &sClockSourceConfig) != HAL_OK) {
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE;
sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_UPDATE;
if(HAL_TIMEx_MasterConfigSynchronization(&htim5, &sMasterConfig) != HAL_OK) {
Error_Handler();
}
}
/* TIM7 init function */
void MX_TIM7_Init(void)
{
TIM_MasterConfigTypeDef sMasterConfig;
htim7.Instance = TIM7;
htim7.Init.Prescaler = (10 - 1);
htim7.Init.CounterMode = TIM_COUNTERMODE_UP;
htim7.Init.Period = (54000 - 1);
htim7.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if(HAL_TIM_Base_Init(&htim7) != HAL_OK) {
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim7, &sMasterConfig) != HAL_OK) {
Error_Handler();
}
HAL_TIM_Base_Start_IT(&htim7);
}
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
{
if(tim_baseHandle->Instance == TIM1)
{
__HAL_RCC_TIM1_CLK_ENABLE();
}
else
if(tim_baseHandle->Instance == TIM5)
{
__HAL_RCC_TIM5_CLK_ENABLE();
}
else
if(tim_baseHandle->Instance == TIM7)
{
__HAL_RCC_TIM7_CLK_ENABLE();
HAL_NVIC_SetPriority(TIM7_IRQn, 3, 0);
HAL_NVIC_EnableIRQ(TIM7_IRQn);
}
}
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle)
{
}
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
{
if(tim_baseHandle->Instance == TIM1)
{
__HAL_RCC_TIM1_CLK_DISABLE();
}
else
if(tim_baseHandle->Instance == TIM5)
{
__HAL_RCC_TIM5_CLK_DISABLE();
}
else
if(tim_baseHandle->Instance == TIM7)
{
__HAL_RCC_TIM7_CLK_DISABLE();
HAL_NVIC_DisableIRQ(TIM7_IRQn);
}
}
__IO uint32_t maxtime = 0;
/* USER CODE BEGIN 1 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
__IO uint16_t Data32, i;
switch((uint32_t) htim->Instance)
{
case TIM7_SOURCE: // TIM7_IRQ
__HAL_TIM_CLEAR_IT(htim, TIM_IT_UPDATE);
if((--timerAsk) == 0)
{
timerAsk = msec1900;
FlagAsk = true;
}
if(timerKeys)
timerKeys--;
else
{
timerKeys = 2;
if(!KEY_STATE)
{
KEY_STATE = kbhit();
/*if(KEY_STATE == KEY_PW)
{
Off();
KEY_STATE = 0;
}*/
}
else
{
/*if(kbhit() == KEY_PW)
{
Off();
}*/
}
}
if(timerRefreshScreen)
timerRefreshScreen--;
else
{
timerRefreshScreen = MSEC_REFRESH[0];
Cursorblink ^= 1;
if(cntblink)
cntblink--;
else
{
cntblink = 2;
blink ^= 1;
cntBat = 1;
}
screen = true;
}
if(repeat)
repeat--;
break;
}
}
void SPI_Wr(uint8_t Input)
{
__IO uint8_t tmp;
while(!(SPI1->SR & SPI_SR_TXE));
SPI1->DR = (uint8_t) Input;
while(!(SPI1->SR & SPI_SR_TXE));
while(!(SPI1->SR & SPI_SR_RXNE));
tmp = (uint8_t) SPI1->DR;
}
void SPI_WrRd(uint16_t* tx, uint16_t *rx, uint16_t cnt)
{
uint16_t i;
while(!(SPI1->SR & SPI_SR_TXE));
for(i = 0; i < cnt; i++)
{
SPI1->DR = (uint16_t) *tx++;
while(!(SPI1->SR & SPI_SR_TXE));
while(!(SPI1->SR & SPI_SR_RXNE));
*rx++ = ((uint16_t) SPI1->DR);
}
/*while(!(SPI1->SR & SPI_SR_RXNE));
return ((uint8_t) SPI1->DR);*/
}
uint8_t SPI_Rd(void)
{
while(!(SPI1->SR & SPI_SR_RXNE));
return ((uint8_t) SPI1->DR);
}
/* USER CODE END 1 */
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/