GadgetSeed  0.9.6
NUCLEO-L152RE.c
[詳解]
1 /** @file
2  @brief STM32L152RE Nucleo Initialize
3 
4  @date 2018.08.11
5  @author Takashi SHUDO
6 */
7 
8 #include "sysconfig.h"
9 #include "tkprintf.h"
10 #include "device/i2c_ioctl.h"
11 #include "device/spi_ioctl.h"
12 #include "device/sd_ioctl.h"
13 #include "device/rtc_ioctl.h"
14 #include "device/video_ioctl.h"
15 #include "device/vio_ioctl.h"
16 
17 #ifdef GSC_COMP_ENABLE_GRAPHICS
18 #include "graphics.h"
19 #endif
20 
21 #include "stm32l1xx_hal.h"
22 
23 /**
24  * @brief System Clock Configuration
25  * The system Clock is configured as follow :
26  * System Clock source = PLL (HSI)
27  * SYSCLK(Hz) = 24000000
28  * HCLK(Hz) = 24000000
29  * AHB Prescaler = 1
30  * APB1 Prescaler = 1
31  * APB2 Prescaler = 1
32  * HSI Frequency(Hz) = 12000000
33  * PLLMUL = 6
34  * PLLDIV = 3
35  * Flash Latency(WS) = 1
36  * @retval None
37  */
39 {
40  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
41  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
42 
43  /* Enable HSE Oscillator and Activate PLL with HSE as source */
44  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
45  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
46  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
47  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
48  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
49 #if GSC_CPU_CLOCK_HZ == 24000000 // 24MHz
50  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
51  RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV4;
52 #else // 32MHz
53  // 32MHz だとWFI命令からの復帰が正常にできない
54  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
55  RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV3;
56 #endif
57  if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
58  // Error
59  }
60 
61  /* Set Voltage scale1 as MCU will run at 32MHz */
62  __HAL_RCC_PWR_CLK_ENABLE();
63  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
64 
65  /* Poll VOSF bit of in PWR_CSR. Wait until it is reset to 0 */
66  while(__HAL_PWR_GET_FLAG(PWR_FLAG_VOS) != RESET) {
67  //
68  };
69 
70  /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
71  clocks dividers */
72  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
73  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
74  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
75  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
76  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
77  if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) {
78  // Error
79  }
80 }
81 
82 /**
83  @brief CPUを初期化する
84 */
85 void init_cpu(void)
86 {
87  SystemInit();
88 
90 }
91 
92 
93 #include "system.h"
94 #include "sysconfig.h"
95 #include "interrupt.h"
96 #include "device.h"
97 #include "random.h"
98 #include "datetime.h"
99 #include "console.h"
100 #include "tprintf.h"
101 
102 #ifdef GSC_COMP_ENABLE_FATFS
103 #include "storage.h"
104 #include "file.h"
105 #endif
106 
107 extern const struct st_device usart2_device;
108 extern const struct st_device usart2_low_device;
109 extern const struct st_device usart4_device;
110 
111 extern const struct st_device null_device;
112 
113 extern const struct st_device rtc_device;
114 
115 extern const struct st_device i2c_device;
116 extern const struct st_device adt7410_device;
117 extern const struct st_device bme280_device;
118 
119 extern const struct st_device spi1_device;
120 extern const struct st_device spi2_device;
121 extern const struct st_device stm_mmc_device;
122 extern const struct st_device mmc_device;
123 
124 extern const struct st_device led_device;
125 
126 extern const struct st_device lcd_gpio_device;
127 extern const struct st_device ts_analog_device;
128 extern const struct st_device ili9325_lcd_device;
129 extern const struct st_device ili9341_lcd_device;
130 extern const struct st_device hx8357d_lcd_device;
131 
132 extern const struct st_device buzzer_device;
133 extern const struct st_device sound_device;
134 
135 extern const struct st_device adc_device;
136 
137 extern const struct st_device grconsole_device;
138 
139 extern const struct st_device gpio_button_device;
140 
141 extern const struct st_device eeprom_device;
142 
143 extern const struct st_device irq_device;
144 extern const struct st_device gpio_device;
145 
146 #ifdef GSC_COMP_ENABLE_FATFS
147 static const char * const storade_devices[] = {
149  0
150 };
151 #endif
152 
153 /**
154  @brief ドライバ初期化以前に行う初期化処理
155 */
156 void init_system(int *argc, char ***argv)
157 {
158  /*
159  ハンドラモードのどのレベルからでもスレッドモードに移行可能に
160  */
161  SCB->CCR |= SCB_CCR_NONBASETHRDENA_Msk;
162  SCB->CCR |= SCB_CCR_STKALIGN_Msk;
163 }
164 
165 /**
166  @brief 基本ドライバ初期化後に登録するユーザドライバ登録処理
167 */
169 {
170  // シリアルコンソール初期化
171  register_device(&usart2_device, 0);
172  register_console_out_dev(&usart2_device);
173  register_console_in_dev(&usart2_device);
174  register_error_out_dev(&usart2_low_device);
175 
176  // USART4
177  //register_device(&usart4_device, 0);
178 
179 #ifdef GSC_DEV_ENABLE_RTC
180  // RTC初期化
181  register_device(&rtc_device, 0);
183 #else
184  init_time(0);
185 #endif
186 
187 #ifdef GSC_DEV_ENABLE_LED
188  // LED
189  register_device(&led_device, 0);
190 #endif
191 
192 #ifdef GSC_DEV_ENABLE_BUTTON
193  // ボタン
194  register_device(&gpio_button_device, 0);
195 #endif
196 
197 #ifdef GSC_LIB_ENABLE_RANDOM
198  // 乱数初期化
199  init_genrand(get_systime_sec());
200 #endif
201 }
202 
203 /**
204  @brief タスク起動後の初期化処理
205 
206  @note タスクAPIを必要とする初期化を行う
207 */
209 {
210  /*
211  I2Cドライバ
212  */
213 #ifdef GSC_DEV_ENABLE_I2C
214  register_device(&i2c_device, 0);
215 # ifdef GSC_DEV_ENABLE_TEMPSENSOR_ADT7410 // $gsc ADT7410温度センサデバイスを有効にする
216  register_device(&adt7410_device, DEF_DEV_NAME_I2C);
217 # endif
218 # ifdef GSC_DEV_ENABLE_TEMPSENSOR_BME280 // $gsc BME280温度、湿度、気圧センサデバイスを有効にする
219  register_device(&bme280_device, DEF_DEV_NAME_I2C);
220 # endif
221 # ifdef GSC_DEV_ENABLE_I2CEEPROM
222  register_device(&eeprom_device, DEF_DEV_NAME_I2C);
223 # endif
224 #endif
225 
226  /*
227  SPIドライバ
228  */
229 #ifdef GSC_DEV_ENABLE_STORAGE
230  register_device(&spi1_device, 0);
231  register_device(&stm_mmc_device, DEF_DEV_NAME_SPI);
232  register_device(&mmc_device, "mmc_spi");
233 #else
234 # ifdef GSC_DEV_ENABLE_SPI // $gsc SPIマスタコントローラデバイスを有効にする
235  register_device(&spi1_device, 0);
236 # endif
237 #endif
238 
239 #ifdef GSC_DEV_ENABLE_SPI2 // $gsc SPI(2)マスタコントローラデバイスを有効にする
240  register_device(&spi2_device, 0);
241 #endif
242 
243  /*
244  グラフィックスドライバ
245  */
246 #ifdef GSC_COMP_ENABLE_GRAPHICS
247  register_device(&lcd_gpio_device, 0);
248 # ifdef GSC_DEV_ENABLE_LCD_ILI9325 // $gsc ILI9325 LCDデバイスを有効にする
249  register_device(&ili9325_lcd_device, DEF_DEV_NAME_VIDEOIO);
250 # endif
251 # ifdef GSC_DEV_ENABLE_LCD_ILI9341 // $gsc ILI9341 LCDデバイスを有効にする
252  register_device(&ili9341_lcd_device, DEF_DEV_NAME_VIDEOIO);
253 # endif
254 # ifdef GSC_DEV_ENABLE_LCD_HX8357D // $gsc HX8357D LCDデバイスを有効にする
255  register_device(&hx8357d_lcd_device, DEF_DEV_NAME_VIDEOIO);
256 # endif
257  init_graphics(DEF_DEV_NAME_VIDEO); // グラフィックス初期化
258 # ifdef GSC_DEV_ENABLE_TOUCHSENSOR
259  register_device(&ts_analog_device, 0);
260 # endif
261 # ifdef GSC_DEV_ENABLE_GRCONSOLE
262  register_device(&grconsole_device, 0);
263 # endif
264 #endif
265 
266  /*
267  ファイルシステム
268  */
269 #ifdef GSC_COMP_ENABLE_FATFS
270  init_storage();
271  init_file();
272  register_storage_device(storade_devices);
273 #endif
274 
275  /*
276  圧電ブザーサウンド
277  */
278 #ifdef GSC_DEV_ENABLE_BUZZER
279 #include "device/buzzer_ioctl.h"
280  register_device(&buzzer_device, 0);
281  register_device(&sound_device, DEF_DEV_NAME_BUZZER);
282 #endif
283 
284  /*
285  ADC
286  */
287 #ifdef GSC_DEV_ENABLE_ADC
288  register_device(&adc_device, 0);
289 #endif
290 
291 #ifdef GSC_DEV_ENABLE_NULL
292  // NULLデバイス初期化
293  register_device(&null_device, 0);
294 #endif
295 }
296 
297 #include "stm32l1xx_hal_iwdg.h"
298 
299 static IWDG_HandleTypeDef IwdgHandle;
300 
301 void reset_system(void)
302 {
303  IwdgHandle.Instance = IWDG;
304  IwdgHandle.Init.Prescaler = IWDG_PRESCALER_32;
305  IwdgHandle.Init.Reload = 0;
306 
307  if(HAL_IWDG_Init(&IwdgHandle) != HAL_OK) {
308  SYSERR_PRINT("HAL_IWDG_Init() error\n");
309  }
310 }
Buzzerドライバ ioctl 用マクロ定義
RTCドライバ ioctl 用マクロ定義
int register_storage_device(const char *const device_name[])
ストレージデバイスをリストでマウントする
Definition: storage.c:160
画像表示デバイスドライバ ioctl 用マクロ定義
SPIドライバ ioctl 用マクロ定義
ファイル
#define DEF_DEV_NAME_SD
標準ストレージデバイス名(MMC,SD等)
Definition: sd_ioctl.h:15
日付時刻
int init_time(char *devname)
時計を初期する
Definition: datetime.c:517
void SystemClock_Config(void)
System Clock Configuration The system Clock is configured as follow : System Clock source = PLL (HSI)...
Definition: NUCLEO-L152RE.c:38
カーネル用機能限定printf
void register_console_in_dev(const struct st_device *in_dev)
システム標準のコンソール入力デバイスを登録する
Definition: console.c:100
void init_system_drivers(void)
基本ドライバ初期化後に登録するユーザドライバ登録処理
#define DEF_DEV_NAME_SPI
標準SPIマスターコントローラデバイス名
Definition: spi_ioctl.h:15
#define DEF_DEV_NAME_BUZZER
標準ブザーデバイス名
Definition: buzzer_ioctl.h:15
外部記憶装置管理
void init_file(void)
全てのファイルディスクリプタを初期化する
Definition: file.c:84
void init_cpu(void)
CPUを初期化する
Definition: NUCLEO-L152RE.c:85
void register_console_out_dev(const struct st_device *out_dev)
システム標準のコンソール出力デバイスを登録する
Definition: console.c:110
割り込みハンドラ
#define DEF_DEV_NAME_I2C
標準I2Cコントローラデバイス名
Definition: i2c_ioctl.h:15
ストレージデバイスドライバ ioctl 用マクロ定義
void register_error_out_dev(const struct st_device *err_dev)
システム標準のエラー出力デバイスを登録する
Definition: console.c:255
#define DEF_DEV_NAME_RTC
標準リアルタイムクロックデバイス名
Definition: rtc_ioctl.h:15
システム固有初期化関連
コンソールI/O
映像関連ドライバIO ioctl 用マクロ定義
void init_storage(void)
外部記憶装置管理初期化
Definition: storage.c:44
void init_system_process(void)
タスク起動後の初期化処理
void init_system(int *argc, char ***argv)
ドライバ初期化以前に行う初期化処理
#define DEF_DEV_NAME_VIDEO
標準ビデオデバイス名
Definition: video_ioctl.h:16
#define DEF_DEV_NAME_VIDEOIO
標準ビデオIOデバイスドライバ名
Definition: vio_ioctl.h:15
デバイスドライバAPI
デバイスドライバ構造体
Definition: device.h:25
疑似乱数発生
int init_graphics(char *devname)
グラフィックスライブラリを初期化する
Definition: graphics.c:305
I2Cドライバ ioctl 用マクロ定義
グラフィックライブラリ
t_time get_systime_sec(void)
システム時間(秒)を取得する
Definition: datetime.c:765
機能限定printf