Skip to content

Instantly share code, notes, and snippets.

@goog
Created July 18, 2019 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goog/222020c4d46e751f591559f3f3ff06e0 to your computer and use it in GitHub Desktop.
Save goog/222020c4d46e751f591559f3f3ff06e0 to your computer and use it in GitHub Desktop.
/**
******************************************************************************
* @file Project/main.c
* @author MCD Application Team
* @version V2.3.0
* @date 16-June-2017
* @brief Main program body
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
*
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.st.com/software_license_agreement_liberty_v2
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stdio.h"
#include "stm8s.h"
#include "stm8s_gpio.h"
#include "stm8s_uart2.h"
/* Private defines -----------------------------------------------------------*/
#define LED_GPIO_PORT (GPIOE)
#define LED_GPIO_PINS (GPIO_PIN_5)
/* Private function prototypes -----------------------------------------------*/
static void TIM1_Config(void);
static void UART_Config(void);
/* Private functions ---------------------------------------------------------*/
uint32_t TIM1ClockFreq = 2000000;
__IO uint32_t LSIClockFreq = 0;
uint16_t ICValue1 = 0, ICValue2 = 0;
void Delay(uint32_t nCount)
{
/* Decrement nCount value */
while (nCount != 0)
{
nCount--;
}
}
void main(void)
{
int i = 0;
uint8_t freq_string[12] = {0};
/* Initialize I/Os in Output Mode */
GPIO_Init(LED_GPIO_PORT, (GPIO_Pin_TypeDef)LED_GPIO_PINS, GPIO_MODE_OUT_PP_LOW_FAST);
GPIO_Init(GPIOD, GPIO_PIN_0, GPIO_MODE_OUT_PP_LOW_FAST);
//CLK_LSICmd(ENABLE);
CLK_CCOConfig(CLK_OUTPUT_LSI);
CLK_CCOCmd(ENABLE);
//while(CLK_GetFlagStatus(CLK_FLAG_CCORDY) == FALSE);
UART_Config();
LSIClockFreq = (8 * TIM1ClockFreq) / (ICValue2 - ICValue1);
sprintf(freq_string, "%u", LSIClockFreq);
/* Infinite loop */
while (1)
{
GPIO_WriteReverse(LED_GPIO_PORT, (GPIO_Pin_TypeDef)LED_GPIO_PINS);
Delay(0xFFFF);
for(i = 0; i < 12; i++)
{
UART2_SendData8(freq_string[i]);
Delay(60);
}
}
}
/**
* @brief Configure TIM1 to to capture the internal clock source (LSI)
* @param None
* @retval None
*/
static void TIM1_Config(void)
{
TIM1_ICInit( TIM1_CHANNEL_1, TIM1_ICPOLARITY_FALLING, TIM1_ICSELECTION_DIRECTTI,
TIM1_ICPSC_DIV8, 0x0);
/* Enable TIM1 */
TIM1_Cmd(ENABLE);
/* Clear CC1 Flag*/
TIM1_ClearFlag(TIM1_FLAG_CC1);
/* wait a capture on CC1 */ // CC1IF
while((TIM1->SR1 & TIM1_FLAG_CC1) != TIM1_FLAG_CC1);
/* Get CCR1 value*/
ICValue1 = TIM1_GetCapture1();
TIM1_ClearFlag(TIM1_FLAG_CC1);
/* wait a capture on cc1 */
while((TIM1->SR1 & TIM1_FLAG_CC1) != TIM1_FLAG_CC1);
/* Get CCR1 value*/
ICValue2 = TIM1_GetCapture1();
TIM1_ClearFlag(TIM1_FLAG_CC1);
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval : None
*/
void assert_failed(u8* file, u32 line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
static void UART_Config(void){
//Reset uart
UART2_DeInit();
UART2_Init((uint32_t)9600, UART2_WORDLENGTH_8D, UART2_STOPBITS_1, UART2_PARITY_NO, UART2_SYNCMODE_CLOCK_DISABLE, UART2_MODE_TXRX_ENABLE );
}
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment