ARMEBS4  revision-26.06.2015
bsp_stm32f4_discovery.c
1 #if defined (BOARD_STM32F4_DISCOVERY)
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 
10 static void bsp_pwm_leds_init(uint32_t frequency, uint32_t steps, uint32_t initial)
11 {
12  const struct gpio_t pins[] =
13  {
14  GPIO_TIM4_CH1_PD12,
15  GPIO_TIM4_CH2_PD13,
16  GPIO_TIM4_CH3_PD14,
17  GPIO_TIM4_CH4_PD15,
18  };
19 
20  /* Pin setup */
21  gpio_setup_list(pins, ARRAY_SIZE(pins));
22 
23  /* Enable the timer clock */
24  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
25 
26  /* Reset the timer */
27  RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM4, ENABLE);
28  RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM4, DISABLE);
29 
30  TIM4->CCER =
35  ;
36 
37  /**
38  * Timer frequency = steps * input_frequency
39  */
40  TIM4->PSC = (((SystemClock.timer)/steps)/frequency) - 1;
41 
42  /**
43  * Count steps (from zero to steps - 1)
44  */
45  TIM4->ARR = steps - 1;
46 
47  /* Set the output value to 50 % */
48  TIM4->CCR1 = initial;
49  TIM4->CCR2 = initial;
50  TIM4->CCR3 = initial;
51  TIM4->CCR4 = initial;
52 
53  TIM4->CCMR1 =
56  ;
57  TIM4->CCMR2 =
60  ;
61 
62  /* Update registers now */
63  TIM4->EGR |= TIM_EGR_UG;
64 
65  /* Start the timer */
66  TIM4->CR1 = TIM_CR1_ARPE | TIM_CR1_CEN;
67 }
68 
69 void bsp_led_set_pwm(uint32_t nr, uint32_t value)
70 {
71  if (nr < 4)
72  {
73  volatile uint32_t *addr[] =
74  {
75  &TIM4->CCR1,
76  &TIM4->CCR3,
77  &TIM4->CCR4,
78  &TIM4->CCR2,
79  };
80 
81  *addr[nr] = value;
82  }
83 }
84 
85 void bsp_led_set(uint32_t nr, uint32_t value)
86 {
87  bsp_led_set_pwm(nr, value > 0 ? 255 : 0);
88 }
89 
90 void bsp_led_toggle(uint32_t nr)
91 {
92  if (nr < 4)
93  {
94  volatile uint32_t *addr[] =
95  {
96  &TIM4->CCR1,
97  &TIM4->CCR3,
98  &TIM4->CCR4,
99  &TIM4->CCR2,
100  };
101 
102  if (*addr[nr])
103  {
104  *addr[nr] = 0;
105  }
106  else
107  {
108  *addr[nr] = 255;
109  }
110  }
111 }
112 
113 static const struct gpio_t bsp_buttons[BSP_BUTTON_NR] =
114 {
116 };
117 
118 static status_e bsp_buttons_init(void)
119 {
120  gpio_setup_list(bsp_buttons, ARRAY_SIZE(bsp_buttons));
121 
122  return NO_ERROR;
123 }
124 
125 const struct gpio_t *bsp_button_gpios(void)
126 {
127  return &bsp_buttons[0];
128 }
129 
130 status_e bsp_init(void)
131 {
132  status_e status;
133 
134  time_init();
135 
136  bsp_pwm_leds_init(50, BSP_PWM_LED_STEPS, 150);
137 
138  retarget_init();
139 
140  status = bsp_buttons_init();
141  if (status != NO_ERROR)
142  {
143  return status;
144  }
145 
146  return NO_ERROR;
147 }
148 
149 uint32_t bsp_button_get(uint32_t nr)
150 {
151  if (nr >= BSP_BUTTON_NR)
152  {
153  return 0;
154  }
155 
156  return gpio_get(&bsp_buttons[nr]);
157 }
158 
159 #if (USE_STM32_USB_HOST_MODE || USE_STM32_USB_USE_DEVICE_MODE || USE_STM32_USB_OTG_MODE)
160 
161 static const struct gpio_t gpio_vbus_not = DEF_GPIOC( 0, GPIO_OUTPUT_1);
162 
163 void USB_OTG_BSP_DriveVBUS(USB_OTG_CORE_HANDLE *pdev, uint8_t state)
164 {
165  gpio_set(&gpio_vbus_not, !state);
166 }
167 
168 void USB_OTG_BSP_ConfigVBUS(USB_OTG_CORE_HANDLE *pdev)
169 {
170  gpio_setup(&gpio_vbus_not);
171  /* By Default, DISABLE is needed on output of the Power Switch */
172  USB_OTG_BSP_DriveVBUS (pdev, 0);
173 
174  delay_wait_ms(200); /* Delay is need for stabilising the Vbus Low
175  in Reset Condition, when Vbus=1 and Reset-button is pressed by user */
176 }
177 #endif /* (USE_STM32_USB_HOST_MODE || USE_STM32_USB_USE_DEVICE_MODE || USE_STM32_USB_OTG_MODE) */
178 
179 #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
#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
simple time abstraction
#define TIM_CR1_CEN
Definition: stm32f4xx.h:6051
#define TIM_CCMR2_OC3M_2
Definition: stm32f4xx.h:6215
#define TIM_CCMR1_OC2M_2
Definition: stm32f4xx.h:6178
#define TIM_CCER_CC1E
Definition: stm32f4xx.h:6256
libheivs configuration file
#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 DEF_GPIOA(__PIN, __MODE)
Helper for defining a pin.
Definition: stm32_gpio.h:358
#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 BSP_PWM_LED_STEPS
Lightness correction.
Definition: bsp_armebs4.h:21
#define TIM_CCMR1_OC1M_1
Definition: stm32f4xx.h:6163
GPIO control structure.
Definition: stm32_gpio.h:96
Input.
Definition: stm32_gpio.h:44
#define TIM_CCER_CC2E
Definition: stm32f4xx.h:6260
#define TIM_CCER_CC4E
Definition: stm32f4xx.h:6268
simple delays
#define TIM_CR1_ARPE
Definition: stm32f4xx.h:6061
#define TIM_CCMR2_OC4M_1
Definition: stm32f4xx.h:6228
#define TIM_EGR_UG
Definition: stm32f4xx.h:6144
#define DEF_GPIOC(__PIN, __MODE)
same as DEF_GPIOA for port C
Definition: stm32_gpio.h:361
#define TIM_CCMR2_OC3PE
Definition: stm32f4xx.h:6210
struct system_clock_t SystemClock
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
const struct gpio_t * bsp_button_gpios(void)
get buttons gpios
Definition: bsp_armebs4.c:350
Pull up.
Definition: stm32_gpio.h:83
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