01 stm32f10x.h

どこにあるか? → こんな深いところにありました.

ソースを読む

このヘッダファイルの役割はCPUのハードウエアによって決まる割り込み番号やレジスタアドレスを定義しています.

とても長いファイルです.

ファイルの冒頭で、STM32F10X_LDなどをdefineしています.STM32-DISCOVERYの場合は、STM32F10X_MD_VLです.ですがこのソースコードを見ると全部コメントアウトしてしまっています.それはSTM32F10X_MD_VLをmakefileで別途定義してますので、ここはコメントアウトのままで問題ありません.STM32F10X_MD_VLを定義したことにより、多種あるSTM32のうち中規模のバリューラインの製品を指定できるようになり、割り込み番号やレジスタアドレスを正しく定義できます.

#if !defined (STM32F10X_LD) && !defined (STM32F10X_LD_VL) && !defined (STM32F10X_MD) && !defined (STM32F10X_MD_VL) && !defined (STM32F10X_HD) && !defined (STM32F10X_HD_VL) && !defined (STM32F10X_XL) && !defined (STM32F10X_CL)

/* #define STM32F10X_LD */ /*!< STM32F10X_LD: STM32 Low density devices */

/* #define STM32F10X_LD_VL */ /*!< STM32F10X_LD_VL: STM32 Low density Value Line devices */

/* #define STM32F10X_MD */ /*!< STM32F10X_MD: STM32 Medium density devices */

/* #define STM32F10X_MD_VL */ /*!< STM32F10X_MD_VL: STM32 Medium density Value Line devices */

/* #define STM32F10X_HD */ /*!< STM32F10X_HD: STM32 High density devices */

/* #define STM32F10X_HD_VL */ /*!< STM32F10X_HD_VL: STM32 High density value line devices */

/* #define STM32F10X_XL */ /*!< STM32F10X_XL: STM32 XL-density devices */

/* #define STM32F10X_CL */ /*!< STM32F10X_CL: STM32 Connectivity line devices */

#endif

その少し下に、clock周波数を定義しているところがあります.

HSE_VALUEというのは、外部Xtalの周波数のことです.25MHzとか8MHzという数字が見えます.でも外部Xtal周波数ですから、設計者がどんなXtalをつけるかによってここの数字を変える必要はあります.注意が必要なところです.

STM32F10X_MD_VLが定義されているので、HSE_VALUE=8MHzになります.

#if !defined HSE_VALUE

#ifdef STM32F10X_CL

#define HSE_VALUE ((uint32_t)25000000) /*!< Value of the External oscillator in Hz */

#else

#define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */

#endif /* STM32F10X_CL */

#endif /* HSE_VALUE */

その少し下に、CPU内部発振周波数を定義しているところがあります.CPUの種別に依らず8MHzであるようです.

#define HSI_VALUE ((uint32_t)8000000) /*!< Value of the Internal oscillator in Hz*/

その下には、IRQ番号を定義している場所があります.

typedef enum IRQn

{

/****** Cortex-M3 Processor Exceptions Numbers *******************************************/

NonMaskableInt_IRQn = -14, /*!< 2 Non Maskable Interrupt */

MemoryManagement_IRQn = -12, /*!< 4 Cortex-M3 Memory Management Interrupt */

BusFault_IRQn = -11, /*!< 5 Cortex-M3 Bus Fault Interrupt */

UsageFault_IRQn = -10, /*!< 6 Cortex-M3 Usage Fault Interrupt */

SVCall_IRQn = -5, /*!< 11 Cortex-M3 SV Call Interrupt */

DebugMonitor_IRQn = -4, /*!< 12 Cortex-M3 Debug Monitor Interrupt */

PendSV_IRQn = -2, /*!< 14 Cortex-M3 Pend SV Interrupt */

SysTick_IRQn = -1, /*!< 15 Cortex-M3 System Tick Interrupt */

/****** STM32 specific Interrupt Numbers *************************************************/

WWDG_IRQn = 0, /*!< Window WatchDog Interrupt */

PVD_IRQn = 1, /*!< PVD through EXTI Line detection Interrupt */

TAMPER_IRQn = 2, /*!< Tamper Interrupt */

RTC_IRQn = 3, /*!< RTC global Interrupt */

FLASH_IRQn = 4, /*!< FLASH global Interrupt */

RCC_IRQn = 5, /*!< RCC global Interrupt */

EXTI0_IRQn = 6, /*!< EXTI Line0 Interrupt */

EXTI1_IRQn = 7, /*!< EXTI Line1 Interrupt */

EXTI2_IRQn = 8, /*!< EXTI Line2 Interrupt */

EXTI3_IRQn = 9, /*!< EXTI Line3 Interrupt */

EXTI4_IRQn = 10, /*!< EXTI Line4 Interrupt */

DMA1_Channel1_IRQn = 11, /*!< DMA1 Channel 1 global Interrupt */

DMA1_Channel2_IRQn = 12, /*!< DMA1 Channel 2 global Interrupt */

DMA1_Channel3_IRQn = 13, /*!< DMA1 Channel 3 global Interrupt */

DMA1_Channel4_IRQn = 14, /*!< DMA1 Channel 4 global Interrupt */

DMA1_Channel5_IRQn = 15, /*!< DMA1 Channel 5 global Interrupt */

DMA1_Channel6_IRQn = 16, /*!< DMA1 Channel 6 global Interrupt */

DMA1_Channel7_IRQn = 17, /*!< DMA1 Channel 7 global Interrupt */

#ifdef STM32F10X_MD_VL

ADC1_IRQn = 18, /*!< ADC1 global Interrupt */

EXTI9_5_IRQn = 23, /*!< External Line[9:5] Interrupts */

TIM1_BRK_TIM15_IRQn = 24, /*!< TIM1 Break and TIM15 Interrupts */

TIM1_UP_TIM16_IRQn = 25, /*!< TIM1 Update and TIM16 Interrupts */

TIM1_TRG_COM_TIM17_IRQn = 26, /*!< TIM1 Trigger and Commutation and TIM17 Interrupt*/

TIM1_CC_IRQn = 27, /*!< TIM1 Capture Compare Interrupt */

TIM2_IRQn = 28, /*!< TIM2 global Interrupt */

TIM3_IRQn = 29, /*!< TIM3 global Interrupt */

TIM4_IRQn = 30, /*!< TIM4 global Interrupt */

I2C1_EV_IRQn = 31, /*!< I2C1 Event Interrupt */

I2C1_ER_IRQn = 32, /*!< I2C1 Error Interrupt */

I2C2_EV_IRQn = 33, /*!< I2C2 Event Interrupt */

I2C2_ER_IRQn = 34, /*!< I2C2 Error Interrupt */

SPI1_IRQn = 35, /*!< SPI1 global Interrupt */

SPI2_IRQn = 36, /*!< SPI2 global Interrupt */

USART1_IRQn = 37, /*!< USART1 global Interrupt */

USART2_IRQn = 38, /*!< USART2 global Interrupt */

USART3_IRQn = 39, /*!< USART3 global Interrupt */

EXTI15_10_IRQn = 40, /*!< External Line[15:10] Interrupts */

RTCAlarm_IRQn = 41, /*!< RTC Alarm through EXTI Line Interrupt */

CEC_IRQn = 42, /*!< HDMI-CEC Interrupt */

TIM6_DAC_IRQn = 54, /*!< TIM6 and DAC underrun Interrupt */

TIM7_IRQn = 55 /*!< TIM7 Interrupt */

#endif /* STM32F10X_MD_VL */

} IRQn_Type;

その下では、ファイルをインクルードしてます.

#include "core_cm3.h" は、CPUハードウエアに密接な構造体とかサブルーチンを定義しているようです.中身を読んでもよくわかりませんでした.

#include "system_stm32f10x.h" は、clock関係の関数を定義しています.こちらを参照してください

#include <stdint.h> は、interruptではなくてintegerの型宣言をしているようです.同じ名前のファイルが複数あるので、どれをポイントしているのかはよくわかってません.

peripheralの構造体宣言がたくさんあります.下記は一例です.

typedef struct{ GPIOの構造体

__IO uint32_t CRL;

__IO uint32_t CRH;

__IO uint32_t IDR;

__IO uint32_t ODR;

__IO uint32_t BSRR;

__IO uint32_t BRR;

__IO uint32_t LCKR;

} GPIO_TypeDef;

typedef struct option byteの構造体

{

__IO uint16_t RDP;

__IO uint16_t USER;

__IO uint16_t Data0;

__IO uint16_t Data1;

__IO uint16_t WRP0;

__IO uint16_t WRP1;

__IO uint16_t WRP2;

__IO uint16_t WRP3;

} OB_TypeDef;

typedef struct{ USARTの構造体

__IO uint16_t SR;

uint16_t RESERVED0;

__IO uint16_t DR;

uint16_t RESERVED1;

__IO uint16_t BRR;

uint16_t RESERVED2;

__IO uint16_t CR1;

uint16_t RESERVED3;

__IO uint16_t CR2;

uint16_t RESERVED4;

__IO uint16_t CR3;

uint16_t RESERVED5;

__IO uint16_t GTPR;

uint16_t RESERVED6;

} USART_TypeDef;

peripheralのアドレスマップが定義されています.下記はその一部です.

#define FLASH_BASE ((uint32_t)0x08000000) /*!< FLASH base address in the alias region */

#define SRAM_BASE ((uint32_t)0x20000000) /*!< SRAM base address in the alias region */

#define PERIPH_BASE ((uint32_t)0x40000000) /*!< Peripheral base address in the alias region */

#define RTC_BASE (APB1PERIPH_BASE + 0x2800)

#define WWDG_BASE (APB1PERIPH_BASE + 0x2C00)

#define IWDG_BASE (APB1PERIPH_BASE + 0x3000)

#define I2C1_BASE (APB1PERIPH_BASE + 0x5400)

#define CAN1_BASE (APB1PERIPH_BASE + 0x6400)

#define BKP_BASE (APB1PERIPH_BASE + 0x6C00)

#define PWR_BASE (APB1PERIPH_BASE + 0x7000)

#define DAC_BASE (APB1PERIPH_BASE + 0x7400)

#define CEC_BASE (APB1PERIPH_BASE + 0x7800)

#define AFIO_BASE (APB2PERIPH_BASE + 0x0000)

#define EXTI_BASE (APB2PERIPH_BASE + 0x0400)

#define GPIOA_BASE (APB2PERIPH_BASE + 0x0800)

#define ADC1_BASE (APB2PERIPH_BASE + 0x2400)

#define TIM1_BASE (APB2PERIPH_BASE + 0x2C00)

#define SPI1_BASE (APB2PERIPH_BASE + 0x3000)

#define USART1_BASE (APB2PERIPH_BASE + 0x3800)

クロックのレジスタ

#define RCC_CFGR2_PREDIV1 ((uint32_t)0x0000000F) /*!< PREDIV1[3:0] bits */

#define RCC_CFGR2_PREDIV1_DIV1 ((uint32_t)0x00000000) /*!< PREDIV1 input clock not divided */

#define RCC_CFGR2_PREDIV1_DIV2 ((uint32_t)0x00000001) /*!< PREDIV1 input clock divided by 2 */

GPIOレジスタ

#define GPIO_IDR_IDR0 ((uint16_t)0x0001) /*!< Port input data, bit 0 */

#define GPIO_ODR_ODR0 ((uint16_t)0x0001) /*!< Port output data, bit 0 */

#define GPIO_BSRR_BS0 ((uint32_t)0x00000001) /*!< Port x Set bit 0 */

#define GPIO_LCKR_LCK0 ((uint32_t)0x00000001) /*!< Port x Lock bit 0 */

ADCのレジスタ

#define ADC_SR_AWD ((uint8_t)0x01) /*!< Analog watchdog flag */

#define ADC_CR1_AWDCH ((uint32_t)0x0000001F) /*!< AWDCH[4:0] bits (Analog watchdog channel select bits) */

#define ADC_CR1_EOCIE ((uint32_t)0x00000020) /*!< Interrupt enable for EOC */

#define ADC_CR1_AWDIE ((uint32_t)0x00000040) /*!< Analog Watchdog interrupt enable */

DACのレジスタ

#define DAC_CR_EN1 ((uint32_t)0x00000001) /*!< DAC channel1 enable */

#define DAC_CR_BOFF1 ((uint32_t)0x00000002) /*!< DAC channel1 output buffer disable */

#define DAC_CR_TEN1 ((uint32_t)0x00000004) /*!< DAC channel1 Trigger enable */

#define DAC_CR_TSEL1 ((uint32_t)0x00000038) /*!< TSEL1[2:0] (DAC channel1 Trigger selection) */

タイマのレジスタ

#define TIM_CR1_CEN ((uint16_t)0x0001) /*!< Counter enable */

#define TIM_CR1_UDIS ((uint16_t)0x0002) /*!< Update disable */

#define TIM_CR1_URS ((uint16_t)0x0004) /*!< Update request source */

#define TIM_CR1_OPM ((uint16_t)0x0008) /*!< One pulse mode */

RTCのレジスタ

#define RTC_CRH_SECIE ((uint8_t)0x01) /*!< Second Interrupt Enable */

#define RTC_CRH_ALRIE ((uint8_t)0x02) /*!< Alarm Interrupt Enable */

#define RTC_CRL_SECF ((uint8_t)0x01) /*!< Second Flag */

#define RTC_CRL_OWF ((uint8_t)0x04) /*!< OverfloW Flag */

SPIのレジスタ

#define SPI_CR1_CPHA ((uint16_t)0x0001) /*!< Clock Phase */

#define SPI_CR1_CPOL ((uint16_t)0x0002) /*!< Clock Polarity */

#define SPI_CR1_MSTR ((uint16_t)0x0004) /*!< Master Selection */

#define SPI_CR1_BR ((uint16_t)0x0038) /*!< BR[2:0] bits (Baud Rate Control) */

USARTのレジスタ

#define USART_SR_ORE ((uint16_t)0x0008) /*!< OverRun Error */

#define USART_SR_IDLE ((uint16_t)0x0010) /*!< IDLE line detected */

#define USART_SR_RXNE ((uint16_t)0x0020) /*!< Read Data Register Not Empty */

#define USART_DR_DR ((uint16_t)0x01FF) /*!< Data value */