ARMEBS4  revision-26.06.2015
bsp_stm32f4_discovery_lcd.c
1 #if defined (BOARD_STM32F4_DISCOVERY_LCD)
2 #include "heivs/config.h"
3 #include "heivs/bsp.h"
4 #include "heivs/time.h"
5 #include "heivs/delay.h"
6 #include "heivs/retarget.h"
7 #include "stm32/usb/usb_core.h"
8 #include "stm32/stm32f4xx_rcc.h"
9 #include "stm32/stm32f4xx_fsmc.h"
10 #include "heivs/lightness_to_pwm.h"
11 
12 <<<<<<< .mine
13 static struct gpio_t sram_gpios[] =
14 {
15  GPIO_FSMC_A0_PF0,
16  GPIO_FSMC_A1_PF1,
17  GPIO_FSMC_A2_PF2,
18  GPIO_FSMC_A3_PF3,
19  GPIO_FSMC_A4_PF4,
20  GPIO_FSMC_A5_PF5,
21  GPIO_FSMC_A6_PF12,
22  GPIO_FSMC_A7_PF13,
23  GPIO_FSMC_A8_PF14,
24  GPIO_FSMC_A9_PF15,
25  GPIO_FSMC_A10_PG0,
26  GPIO_FSMC_A11_PG1,
27  GPIO_FSMC_A12_PG2,
28  GPIO_FSMC_A13_PG3,
29  GPIO_FSMC_A14_PG4,
30  GPIO_FSMC_A15_PG5,
31  GPIO_FSMC_A16_PD11,
32  GPIO_FSMC_A17_PD12,
33 // GPIO_FSMC_A18_PD13,
34  GPIO_FSMC_A19_PE3,
35  GPIO_FSMC_A20_PE4,
36  GPIO_FSMC_A21_PE5,
37 
38  GPIO_FSMC_D0_PD14,
39  GPIO_FSMC_D1_PD15,
40  GPIO_FSMC_D2_PD0,
41  GPIO_FSMC_D3_PD1,
42  GPIO_FSMC_D4_PE7,
43  GPIO_FSMC_D5_PE8,
44  GPIO_FSMC_D6_PE9,
45  GPIO_FSMC_D7_PE10,
46  GPIO_FSMC_D8_PE11,
47  GPIO_FSMC_D9_PE12,
48  GPIO_FSMC_D10_PE13,
49  GPIO_FSMC_D11_PE14,
50  GPIO_FSMC_D12_PE15,
51  GPIO_FSMC_D13_PD8,
52  GPIO_FSMC_D14_PD9,
53  GPIO_FSMC_D15_PD10,
54 
55  GPIO_FSMC_NE1_FSMC_NCE2_PD7,
56  GPIO_FSMC_NWE_PD5,
57  GPIO_FSMC_NOE_PD4,
58  GPIO_FSMC_BLN1_PE1,
59  GPIO_FSMC_NBL0_PE0,
60 };
61 static status_e bsp_init_FSMC(void)
62 {
63  // Setup gpios used by our RAM and LCD
64  gpio_setup_list(sram_gpios, ARRAY_SIZE(sram_gpios));
65  // enable the clock peripheral
66  RCC_AHB3PeriphClockCmd(RCC_AHB3ENR_FSMCEN, ENABLE);
67 
70 
71  // init the FSMC for LCD
72  timings.FSMC_AddressSetupTime = 6;
73  timings.FSMC_AddressHoldTime = 0;
74  timings.FSMC_DataSetupTime = 6;
75 
76 // timings.FSMC_AddressSetupTime = 12;
77 // timings.FSMC_AddressHoldTime = 6;
78 // timings.FSMC_DataSetupTime = 12;
79 
80  timings.FSMC_BusTurnAroundDuration = 1;
81  timings.FSMC_CLKDivision = 0;
82  timings.FSMC_DataLatency = 0;
83  timings.FSMC_AccessMode = FSMC_AccessMode_A;
84 
85  fscm.FSMC_Bank = FSMC_Bank1_NORSRAM1;
86  fscm.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
87  fscm.FSMC_MemoryType = FSMC_MemoryType_SRAM;
88  fscm.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
89  fscm.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
90  fscm.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
91  fscm.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
92  fscm.FSMC_WrapMode = FSMC_WrapMode_Disable;
93  fscm.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
94  fscm.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
95  fscm.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
96  fscm.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
97  fscm.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
98  fscm.FSMC_ReadWriteTimingStruct = &timings;
99  fscm.FSMC_WriteTimingStruct = &timings;
100  FSMC_NORSRAMInit(&fscm);
101  FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE);
102 
103  return NO_ERROR;
104 }
105 
106 static status_e bsp_pwm_leds_init(uint32_t frequency, uint32_t steps, uint32_t initial)
107 =======
108 static void bsp_pwm_leds_init(uint32_t frequency, uint32_t steps, uint32_t initial)
109 >>>>>>> .r213
110 {
111  const struct gpio_t pins[] =
112  {
113  GPIO_TIM4_CH1_PD12,
114  GPIO_TIM4_CH2_PD13,
115 // GPIO_TIM4_CH3_PD14,
116 // GPIO_TIM4_CH4_PD15,
117  };
118 
119  /* Pin setup */
120  gpio_setup_list(pins, ARRAY_SIZE(pins));
121 
122  /* Enable the timer clock */
123  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
124 
125  /* Reset the timer */
126  RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM4, ENABLE);
127  RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM4, DISABLE);
128 
129  TIM4->CCER =
131  | TIM_CCER_CC2E
132  | TIM_CCER_CC3E
133  | TIM_CCER_CC4E
134  ;
135 
136  /**
137  * Timer frequency = steps * input_frequency
138  */
139  TIM4->PSC = (((SystemClock.timer)/steps)/frequency) - 1;
140 
141  /**
142  * Count steps (from zero to steps - 1)
143  */
144  TIM4->ARR = steps - 1;
145 
146  /* Set the output value to 50 % */
147  TIM4->CCR1 = initial;
148  TIM4->CCR2 = initial;
149  TIM4->CCR3 = initial;
150  TIM4->CCR4 = initial;
151 
152  TIM4->CCMR1 =
155  ;
156  TIM4->CCMR2 =
159  ;
160 
161  /* Update registers now */
162  TIM4->EGR |= TIM_EGR_UG;
163 
164  /* Start the timer */
165  TIM4->CR1 = TIM_CR1_ARPE | TIM_CR1_CEN;
166 }
167 
168 void bsp_led_set_pwm(uint32_t nr, uint32_t value)
169 {
170  if (nr < 4)
171  {
172  volatile uint32_t *addr[] =
173  {
174  &TIM4->CCR1,
175  &TIM4->CCR3,
176  &TIM4->CCR4,
177  &TIM4->CCR2,
178  };
179 
180  *addr[nr] = lightness_to_pwm(value);
181  }
182 }
183 
184 void bsp_led_set(uint32_t nr, uint32_t value)
185 {
186 
187  bsp_led_set_pwm(nr, value > 0 ? 255 : 0);
188 }
189 
190 void bsp_led_toggle(uint32_t nr)
191 {
192  if (nr < 4)
193  {
194  volatile uint32_t *addr[] =
195  {
196  &TIM4->CCR1,
197  &TIM4->CCR3,
198  &TIM4->CCR4,
199  &TIM4->CCR2,
200  };
201 
202  if (*addr[nr])
203  {
204  *addr[nr] = 0;
205  }
206  else
207  {
208  *addr[nr] = 255;
209  }
210  }
211 }
212 
213 static const struct gpio_t bsp_buttons[BSP_BUTTON_NR] =
214 {
215  GPIO_MOD(0, 0, GPIO_INPUT | GPIO_PP | GPIO_SPEED_2),
216 };
217 
218 static status_e bsp_buttons_init(void)
219 {
220  gpio_setup_list(bsp_buttons, BSP_BUTTON_NR);
221 
222  return NO_ERROR;
223 }
224 
225 static status_e bsp_lcd_backlight_init(uint32_t frequency, uint32_t steps, uint32_t initial)
226 {
227  const struct gpio_t pins[] =
228  {
229  GPIO_TIM4_CH2_PD13,
230  };
231 
232  /* Pin setup */
233  gpio_setup_list(pins, ARRAY_SIZE(pins));
234 
235  /* Enable the timer clock */
236  RCC_APB2PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
237  /* Reset the timer */
238 // RCC_APB2PeriphResetCmd(RCC_APB1Periph_TIM4, ENABLE);
239 // RCC_APB2PeriphResetCmd(RCC_APB1Periph_TIM4, DISABLE);
240 
241  TIM4->CCER |=
242  TIM_CCER_CC2E /* Enable compare 1 output */
243  ;
244 
245  /**
246  * Timer frequency = steps * input_frequency
247  */
248  TIM4->PSC = (((SystemClock.timer)/steps)/frequency) - 1;
249 
250  /**
251  * Count steps (from zero to steps - 1)
252  */
253  TIM4->ARR = steps - 1;
254 
255  /* Set the intial value */
256  TIM4->CCR2 = steps-initial;
257 
258  TIM4->CCMR2 |=
259  TIM_CCMR1_OC2M_2 | TIM_CCMR1_OC2M_1 /* PWM mode 1 */
260  | TIM_CCMR1_OC2PE /* CCR2 shadow mode */;
261 
262  /* Update registers now */
263  TIM4->EGR |= TIM_EGR_UG;
264 
265  /* Start the timer */
266  TIM4->CR1 |=
268  | TIM_CR1_CEN;
269 
270  return NO_ERROR;
271 }
272 
273 void bsp_lcd_backlight_set(uint32_t value)
274 {
275  TIM4->CCR2 = lightness_to_pwm(value);
276 }
277 
278 status_e bsp_init(void)
279 {
280  status_e status;
281 
282  time_init();
283  delay_wait_ms(1000); // test purpose because of fucking processor
284  bsp_init_FSMC();
285  bsp_pwm_leds_init(50, 1000, 150);
286 
287  retarget_init();
288 
289 <<<<<<< .mine
290  status = bsp_pwm_leds_init(20000, LIGHTNESS_PWM_STEP, lightness_to_pwm(50));
291  if (status != NO_ERROR)
292  {
293  return status;
294  }
295  status = bsp_lcd_backlight_init(20000,LIGHTNESS_PWM_STEP,lightness_to_pwm(50));
296  if (status != NO_ERROR)
297  {
298  return status;
299  }
300 =======
301 >>>>>>> .r213
302  status = bsp_buttons_init();
303  if (status != NO_ERROR)
304  {
305  return status;
306  }
307 
308  return NO_ERROR;
309 }
310 
311 uint32_t bsp_button_get(uint32_t nr)
312 {
313  if (nr > BSP_LED_NR)
314  {
315  return 0;
316  }
317 
318  return gpio_get(&bsp_buttons[nr]);
319 }
320 
321 #if (USE_STM32_USB_HOST_MODE || USE_STM32_USB_USE_DEVICE_MODE || USE_STM32_USB_OTG_MODE)
322 
323 static const struct gpio_t gpio_vbus_not = GPIO_MOD(2, 0, GPIO_OUTPUT_1);
324 
325 void USB_OTG_BSP_DriveVBUS(USB_OTG_CORE_HANDLE *pdev, uint8_t state)
326 {
327  gpio_set(&gpio_vbus_not, !state);
328 }
329 
330 void USB_OTG_BSP_ConfigVBUS(USB_OTG_CORE_HANDLE *pdev)
331 {
332  gpio_setup(&gpio_vbus_not);
333  /* By Default, DISABLE is needed on output of the Power Switch */
334  USB_OTG_BSP_DriveVBUS (pdev, 0);
335 
336  delay_wait_ms(200); /* Delay is need for stabilising the Vbus Low
337  in Reset Condition, when Vbus=1 and Reset-button is pressed by user */
338 }
339 #endif /* (USE_STM32_USB_HOST_MODE || USE_STM32_USB_USE_DEVICE_MODE || USE_STM32_USB_OTG_MODE) */
340 
341 #endif
This file contains all the functions prototypes for the RCC firmware library.
BSP - Board Support Package.
stdio redirection
uint8_t nr
Pin number (0 for GPIOx0, ... , 3 for GPIOx3, ...)
Definition: stm32_gpio.h:99
Handle non-linearity in human eye brightness sensitivity.
#define TIM_CCMR2_OC4M_2
Definition: stm32f4xx.h:6229
void delay_wait_ms(uint32_t ms)
Wait for at least that time.
Definition: delay.c:12
Output (default 1)
Definition: stm32_gpio.h:53
#define BSP_BUTTON_NR
Number of buttons.
Definition: bsp_armebs4.h:23
Timing parameters For NOR/SRAM Banks.
simple time abstraction
#define TIM_CR1_CEN
Definition: stm32f4xx.h:6051
#define TIM_CCMR2_OC3M_2
Definition: stm32f4xx.h:6215
void bsp_lcd_backlight_set(uint32_t value)
Set the brightness of the backlight.
Definition: bsp_armebs4.c:314
#define TIM_CCMR1_OC2M_2
Definition: stm32f4xx.h:6178
#define TIM_CCER_CC1E
Definition: stm32f4xx.h:6256
FSMC NOR/SRAM Init structure definition.
libheivs configuration file
uint8_t lightness_to_pwm(uint8_t percentage)
PWM value for driving a LED expressed as perceived percentage.
FSMC_NORSRAMTimingInitTypeDef * FSMC_WriteTimingStruct
#define TIM_CCMR1_OC2PE
Definition: stm32f4xx.h:6173
static void gpio_set(const struct gpio_t *gpio, uint32_t value)
Set a gpio.
Definition: stm32_gpio.h:260
static uint32_t gpio_get(const struct gpio_t *gpio)
Get gpio level.
Definition: stm32_gpio.h:219
#define TIM_CCMR1_OC2M_1
Definition: stm32f4xx.h:6177
#define TIM_CCER_CC3E
Definition: stm32f4xx.h:6264
#define TIM_CCMR2_OC3M_1
Definition: stm32f4xx.h:6214
void bsp_led_set(uint32_t nr, uint32_t value)
Set a led (ON or OFF)
Definition: bsp_armebs4.c:244
#define TIM_CCMR2_OC4PE
Definition: stm32f4xx.h:6224
void bsp_led_toggle(uint32_t nr)
toggle a led
Definition: bsp_armebs4.c:249
#define TIM_CCMR1_OC1M_2
Definition: stm32f4xx.h:6164
#define TIM_CCMR1_OC1M_1
Definition: stm32f4xx.h:6163
GPIO control structure.
Definition: stm32_gpio.h:96
Input.
Definition: stm32_gpio.h:44
FSMC_NORSRAMTimingInitTypeDef * FSMC_ReadWriteTimingStruct
#define TIM_CCER_CC2E
Definition: stm32f4xx.h:6260
#define TIM_CCER_CC4E
Definition: stm32f4xx.h:6268
This file contains all the functions prototypes for the FSMC firmware library.
simple delays
#define TIM_CR1_ARPE
Definition: stm32f4xx.h:6061
#define LIGHTNESS_PWM_STEP
Number a step for a pwm using lightness_to_pwm.
#define TIM_CCMR2_OC4M_1
Definition: stm32f4xx.h:6228
#define TIM_EGR_UG
Definition: stm32f4xx.h:6144
#define TIM_CCMR2_OC3PE
Definition: stm32f4xx.h:6210
struct system_clock_t SystemClock
#define BSP_LED_NR
Number of LEDs.
Definition: bsp_armebs4.h:20
Output type : Push-Pull.
Definition: stm32_gpio.h:62
status_e gpio_setup_list(const struct gpio_t gpio[], size_t len)
Setup an array of gpio.
Definition: stm32_gpio.c:47
Output speed : 2 MHz.
Definition: stm32_gpio.h:68
status_e gpio_setup(const struct gpio_t *gpio)
Setup a gpio.
Definition: stm32_gpio.c:9
status_e
Known errors.
Definition: error.h:21
#define ARRAY_SIZE(x)
Number of elements in the array.
Definition: utils.h:19
#define TIM_CCMR1_OC1PE
Definition: stm32f4xx.h:6159
No error.
Definition: error.h:28
status_e bsp_init(void)
Initialize the whole board.
Definition: bsp_armebs4.c:525
uint32_t bsp_button_get(uint32_t nr)
Get a button.
Definition: bsp_armebs4.c:340