2 * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
\r
3 * All rights reserved.
\r
5 * Redistribution and use in source and binary forms, with or without
\r
6 * modification, are permitted provided that the following conditions
\r
9 * - Redistributions of source code must retain the above copyright
\r
10 * notice, this list of conditions and the following disclaimer.
\r
11 * - Redistributions in binary form must reproduce the above copyright
\r
12 * notice, this list of conditions and the following disclaimer in the
\r
13 * documentation and/or other materials provided with the distribution.
\r
14 * - The name of the author may not be used to endorse or promote products
\r
15 * derived from this software without specific prior written permission.
\r
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
\r
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
\r
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
\r
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
\r
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
\r
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
\r
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
\r
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
\r
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
\r
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
30 #include <stm32f4xx.h>
\r
31 #include <stm32f429i_discovery_lcd.h>
\r
32 #include <lcd_log.h>
\r
35 #include <usbh_core.h>
\r
36 #include <usbh_msc.h>
\r
40 #include <hw_init.h>
\r
41 #include <stdbool.h>
\r
43 USBH_HandleTypeDef hUSB_Host;
\r
46 static bool msc_connected = false;
\r
47 static bool enum_done = false;
\r
49 static void USBH_UserProcess(USBH_HandleTypeDef *phost, uint8_t id)
\r
53 case HOST_USER_SELECT_CONFIGURATION:
\r
56 case HOST_USER_DISCONNECTION:
\r
57 msc_connected = false;
\r
61 case HOST_USER_CLASS_ACTIVE:
\r
65 case HOST_USER_CONNECTION:
\r
66 msc_connected = true;
\r
73 #define LCD_FRAME_BUFFER_LAYER0 (LCD_FRAME_BUFFER+0x130000)
\r
74 #define LCD_FRAME_BUFFER_LAYER1 LCD_FRAME_BUFFER
\r
75 #define CONVERTED_FRAME_BUFFER (LCD_FRAME_BUFFER+0x260000)
\r
77 static void SystemClock_Config(void)
\r
79 RCC_ClkInitTypeDef RCC_ClkInitStruct;
\r
80 RCC_OscInitTypeDef RCC_OscInitStruct;
\r
82 /* Enable Power Control clock */
\r
85 /* The voltage scaling allows optimizing the power consumption when the device is
\r
86 clocked below the maximum system frequency, to update the voltage scaling value
\r
87 regarding system frequency refer to product datasheet. */
\r
88 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
\r
90 /* Enable HSE Oscillator and activate PLL with HSE as source */
\r
91 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
\r
92 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
\r
93 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
\r
94 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
\r
95 RCC_OscInitStruct.PLL.PLLM = 8;
\r
96 RCC_OscInitStruct.PLL.PLLN = 336;
\r
97 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
\r
98 RCC_OscInitStruct.PLL.PLLQ = 7;
\r
99 HAL_RCC_OscConfig (&RCC_OscInitStruct);
\r
101 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
\r
103 RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
\r
104 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
\r
105 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
\r
106 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
\r
107 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
\r
108 HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
\r
116 SystemClock_Config();
\r
118 /* Initialize the LEDs */
\r
119 BSP_LED_Init(LED3);
\r
120 BSP_LED_Init(LED4);
\r
122 SysTick_Config(CFG_CCLK_FREQ / 1000);
\r
124 USBH_Init(&hUSB_Host, USBH_UserProcess, 0);
\r
125 USBH_RegisterClass(&hUSB_Host, USBH_MSC_CLASS);
\r
126 USBH_Start(&hUSB_Host);
\r
131 BSP_LCD_LayerDefaultInit(1, LCD_FRAME_BUFFER_LAYER1);
\r
132 BSP_LCD_SelectLayer(1);
\r
133 BSP_LCD_Clear(LCD_COLOR_WHITE);
\r
134 BSP_LCD_SetColorKeying(1, LCD_COLOR_WHITE);
\r
135 BSP_LCD_SetLayerVisible(1, DISABLE);
\r
138 BSP_LCD_LayerDefaultInit(0, LCD_FRAME_BUFFER_LAYER0);
\r
139 BSP_LCD_SelectLayer(0);
\r
140 BSP_LCD_DisplayOn();
\r
141 BSP_LCD_Clear(LCD_COLOR_BLACK);
\r
145 LCD_LOG_SetHeader((uint8_t *)"STM32 LWEXT4 DEMO ");
\r
148 void hw_usb_process(void)
\r
150 USBH_Process(&hUSB_Host);
\r
153 bool hw_usb_connected(void)
\r
155 return msc_connected;
\r
158 bool hw_usb_enum_done(void)
\r
163 void hw_led_red(bool on)
\r
165 on ? BSP_LED_On(LED4) : BSP_LED_Off(LED4);
\r
168 void hw_led_green(bool on)
\r
170 on ? BSP_LED_On(LED3) : BSP_LED_Off(LED3);
\r
173 uint32_t tim_get_ms(void)
\r
175 return HAL_GetTick();
\r
178 uint64_t tim_get_us(void)
\r
180 return (uint64_t)HAL_GetTick() * 1000 +
\r
181 ((SysTick->LOAD - SysTick->VAL) * 1000) / SysTick->LOAD;
\r
184 void tim_wait_ms(uint32_t ms)
\r
186 volatile uint32_t t = HAL_GetTick();
\r
188 while((t + ms) > HAL_GetTick())
\r