먼 옛날에 쓰다만건데
임시저장 된거 없앨라고 걍 올리는거
(저는 stm32f103RBT6 - NUCLEO board를 사용합니다)
RCC를 설정하기 위해 필요한 내용
-RCC를 설정할 수 있는 4개의 Data Structures
-Data Structures의 내용에 따라 초기화를 하거나 초기화 취소해주는 함수
RCC를 설정할 수 있는 4개의 Data Structures
RCC_PLLInitTypeDef : RCC PLL configuration structure definition.
RCC_ClkInitTypeDef : RCC System, AHB and APB busses clock configuration structure definition.
RCC_OscInitTypeDef : RCC Internal/External Oscillator (HSE, HSI, LSE and LSI) configuration structure definition.
RCC_PeriphCLKInitTypeDef : RCC extended clocks structure definition.
(stm32f103xB 보드는 I2s2ClockSelection, I2s3ClockSelection이 없는 듯 하다.)
마지막으로 초기화와 초기화 취소 함수
Cube MX 툴을 이용한 Clock Configuration
HSE = high-speed external clock signal
HSI = hight-speed internal clock signal
LSI = low-speed internal clock signal
LSE = low-speed external clock signal
만약 HSI을 사용한다면
system clock frequency를 최대 64MHz 밖에 설정할 수 없다.
HSE에는 선택지가 BYPASS Clock Source와 Crystal/Ceramic Resonator가 있는데 후자로 설정해서 sysclk을 72MHz로 설정했다.
BYPASS Clock Source : High-speed external user clock generated from an external source
Crystal/Ceramic Resonator : High-speed external clock generated from a crystal/ceramic resonator
schematic을 보면 8MHz 크리스탈 오실레이터가 붙어있다.
cube mx의 코드 제너레이트로 생성된 RCC 설정 함수
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OScInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitstruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
<코드 설명>
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
구조체 초기화
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
The oscillators to be configured. This parameter can be a value of Oscillator Type.
Oscillator Type
-RCC_OSCILLATORTYPE_HSE
-RCC_OSCILLATORTYPE_HSI
-RCC_OSCILLATORTYPE_LSE
-RCC_OSCILLATORTYPE_LSI
-RCC_OSCILLATORTYPE_NONE
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
The new state of the HSE. This parameter can be value of HSE Config.
HSE Config
-RCC_HSE_BYPASS
-RCC_HSE_OFF
-RCC_HSE_ON
RCC_OScInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
The Prediv1 factor value (named PREDIV1 or PLLXTPRE in RM) This parameter can be a value of HSE Prediv1 Factor
HSE Prediv1 Factor
-RCC_HSE_PREDIV_DIV1
-RCC_HSE_PREDIV_DIV2
HSE에서 PLL Source Mux로 들어가기 전에 클럭 조정
RCC_OscInitstruct.HSIState = RCC_HSI_ON;
The new state of the HSI. This parameter can be a value of HSI Config
HSI Config
-RCC_HSI_OFF
-RCC_HSI_ON
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
The new state of the PLL. This parameter can be value of PLL Conig
PLL Conig
-RCC_PLL_NONE
-RCC_PLL_OFF
-RCC_PLL_ON
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
PLLSource : PLL entry clock source. This parameter must be a value of PLL Clock Source
PLL Clock Source
-RCC_PLLSOURCE_HSE
-RCC_PLLSOURCE_HSI_DIV2
PLL clock source로 HSI 쓰면 클럭 반절 되는건 고정이네요.
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
PLLMUL : Multiplication factor for PLL VCO input clock. This parameter must be a value of PLL Multiplication Factor
PLL Multiplication Factor
-RCC_PLL_MUL2
-RCC_PLL_MUL3
....
-RCC_PLL_MUL16
HAL_RCC_OscConfig(&RCC_OscInitStruct)
HAL_StatusTypeDef HAL_RCC_OscConfig ( RCC_OscInitTypeDef * RCC_OscInitStruct )
Initializes the RCC Oscillators according to the specified parameters in the RCC_OscInitTypeDef
Parameter : RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that contains the configuration information for the RCC Oscillator.
Return values : HAL status
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
초기화 함에 있어서 오류가 없으면 HAL_OK를 반환한다. 위 코드는 그렇지 않을 경우를 확인하기 위해 있다.
Error_Handler(); 함수의 내용은 유저가 작성한다.