ARMEBS4  revision-26.06.2015
bsp_stm32_camera.c
1 #if defined (BOARD_STM32_CAMERA)
2 #include "heivs/bsp.h"
3 
4 #include "heivs/stm32_mcp4552.h"
5 #include "stm32/stm32f4xx_fsmc.h"
6 #include "stm32/stm32f4xx_rcc.h"
7 #include "heivs/ram_test.h"
9 #include "heivs/time.h"
10 #include "heivs/retarget.h"
11 #include "heivs/lightness_to_pwm.h"
12 
13 static const struct gpio_t 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 
62 static status_e bsp_init_extRAM(void)
63 {
64  /* Setup gpios used by our RAM*/
65  gpio_setup_list(gpios, ARRAY_SIZE(gpios));
66 
67  RCC_AHB3PeriphClockCmd(RCC_AHB3ENR_FSMCEN, ENABLE);
68 
71  timings.FSMC_AddressSetupTime = 5;
72  timings.FSMC_AddressHoldTime = 2;
73  timings.FSMC_DataSetupTime = 7;
74  timings.FSMC_BusTurnAroundDuration = 3;
75  timings.FSMC_CLKDivision = 0;
76  timings.FSMC_DataLatency = 2;
77  timings.FSMC_AccessMode = FSMC_AccessMode_A;
78 
79  fscm.FSMC_Bank = FSMC_Bank1_NORSRAM1;
80  fscm.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
81  fscm.FSMC_MemoryType = FSMC_MemoryType_PSRAM;
82  fscm.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
83  fscm.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
84  fscm.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
85  fscm.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
86  fscm.FSMC_WrapMode = FSMC_WrapMode_Disable;
87  fscm.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
88  fscm.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
89  fscm.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
90  fscm.FSMC_ExtendedMode = FSMC_ExtendedMode_Enable;
91  fscm.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
92  fscm.FSMC_ReadWriteTimingStruct = &timings;
93  fscm.FSMC_WriteTimingStruct = &timings;
94 
95  FSMC_NORSRAMInit(&fscm);
96 
97  FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE);
98 
99  return NO_ERROR;
100 }
101 
102 static void load_init_external_ram(void)
103 {
104  extern uint8_t __ext_ram_bss_start;
105  extern uint8_t __ext_ram_bss_end;
106 
107  extern uint8_t __ext_ram_dst_start;
108  extern uint8_t __ext_ram_dst_end;
109  extern uint8_t __ext_ram_src_start;
110 
111  memset(&__ext_ram_bss_start, 0x0, &__ext_ram_bss_end - &__ext_ram_bss_start);
112  memcpy(&__ext_ram_dst_start,&__ext_ram_src_start, &__ext_ram_dst_end - & __ext_ram_dst_start);
113 }
114 
115 static void bsp_pwm_leds_init(uint32_t frequency, uint32_t steps, uint32_t initial)
116 {
117  const struct gpio_t pins[] =
118  {
119  GPIO_TIM2_CH1_PA0,
120  GPIO_TIM2_CH2_PA1,
121  GPIO_TIM2_CH3_PA2,
122  GPIO_TIM2_CH4_PA3,
123  };
124 
125  /* Pin setup */
126  gpio_setup_list(pins, ARRAY_SIZE(pins));
127 
128  /* Enable the timer clock */
129  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
130 
131  /* Reset the timer */
132  RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM2, ENABLE);
133  RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM2, DISABLE);
134 
135  TIM2->CCER =
137  | TIM_CCER_CC2E
138  | TIM_CCER_CC3E
139  | TIM_CCER_CC4E
140  ;
141 
142  /**
143  * Timer frequency = steps * input_frequency
144  */
145  TIM2->PSC = (((SystemClock.timer)/steps)/frequency) - 1;
146 
147  /**
148  * Count steps (from zero to steps - 1)
149  */
150  TIM2->ARR = steps - 1;
151 
152  /* Set the output value to 50 % */
153  TIM2->CCR1 = initial;
154  TIM2->CCR2 = initial;
155  TIM2->CCR3 = initial;
156  TIM2->CCR4 = initial;
157 
158  TIM2->CCMR1 =
161  ;
162  TIM2->CCMR2 =
165  ;
166 
167  /* Update registers now */
168  TIM2->EGR |= TIM_EGR_UG;
169 
170  /* Start the timer */
171  TIM2->CR1 = TIM_CR1_ARPE | TIM_CR1_CEN;
172 }
173 
174 void bsp_led_set_pwm(uint32_t nr, uint32_t value)
175 {
176  if (nr < 4)
177  {
178  volatile uint32_t *addr[] =
179  {
180  &TIM2->CCR1,
181  &TIM2->CCR3,
182  &TIM2->CCR4,
183  &TIM2->CCR2,
184  };
185 
186  *addr[nr] = lightness_to_pwm(value);
187  }
188 }
189 
190 void bsp_led_set(uint32_t nr, uint32_t value)
191 {
192  bsp_led_set_pwm(nr, value > 0 ? LIGHTNESS_PWM_STEP : 0);
193 }
194 
195 void bsp_led_toggle(uint32_t nr)
196 {
197  if (nr < 4)
198  {
199  volatile uint32_t *addr[] =
200  {
201  &TIM2->CCR1,
202  &TIM2->CCR3,
203  &TIM2->CCR4,
204  &TIM2->CCR2,
205  };
206 
207  if (*addr[nr])
208  {
209  *addr[nr] = 0;
210  }
211  else
212  {
213  *addr[nr] = LIGHTNESS_PWM_STEP;
214  }
215  }
216 }
217 
218 static status_e bsp_light_init(uint32_t frequency, uint32_t steps, uint32_t initial)
219 {
220  const struct gpio_t pins[] =
221  {
222  GPIO_TIM10_CH1_PB8,
223  };
224 
225  /* Pin setup */
226  gpio_setup_list(pins, ARRAY_SIZE(pins));
227 
228  /* Enable the timer clock */
229  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM10, ENABLE);
230 
231  /* Reset the timer */
232  RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM10, ENABLE);
233  RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM10, DISABLE);
234 
235  TIM10->CCER =
236  TIM_CCER_CC1E /* Enable compare 1 output */
237  ;
238 
239  /**
240  * Timer frequency = steps * input_frequency
241  */
242  TIM10->PSC = (((SystemClock.timer)/steps)/frequency) - 1;
243 
244  /**
245  * Count steps (from zero to steps - 1)
246  */
247  TIM10->ARR = steps - 1;
248 
249  /* Set the output value to 50 % */
250  TIM10->CCR1 = initial;
251 
252  TIM10->CCMR1 =
253  TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1 /* PWM mode 1 */
254  | TIM_CCMR1_OC1PE /* CCR2 shadow mode */;
255 
256  /* Update registers now */
257  TIM10->EGR |= TIM_EGR_UG;
258 
259  /* Start the timer */
260  TIM10->CR1 =
262  | TIM_CR1_CEN;
263 
264  return NO_ERROR;
265 }
266 
267 void bsp_light_set(uint32_t value)
268 {
269  TIM10->CCR1 = value;
270 }
271 
272 static const struct gpio_t bsp_buttons[BSP_BUTTON_NR] =
273 {
276 };
277 
278 static status_e bsp_buttons_init(void)
279 {
280  gpio_setup_list(bsp_buttons, ARRAY_SIZE(bsp_buttons));
281 
282  return NO_ERROR;
283 }
284 
285 uint32_t bsp_button_get(uint32_t nr)
286 {
287  if (nr >= BSP_BUTTON_NR)
288  {
289  return 0;
290  }
291 
292  return !gpio_get(&bsp_buttons[nr]);
293 }
294 
295 const struct gpio_t *bsp_button_gpios(void)
296 {
297  return &bsp_buttons[0];
298 }
299 
300 static const struct gpio_t all_pins_safe_values[] =
301 {
315  // JTAG
316  //DEF_GPIOA(13, GPIO_INPUT | GPIO_PULLDOWN),
317  //DEF_GPIOA(14, GPIO_INPUT | GPIO_PULLDOWN),
318  //DEF_GPIOA(15, GPIO_INPUT | GPIO_PULLDOWN),
319 
323 
324  // JTAG
325  //DEF_GPIOB( 3, GPIO_INPUT | GPIO_PULLDOWN),
326  //DEF_GPIOB( 4, GPIO_INPUT | GPIO_PULLDOWN),
327 
332  DEF_GPIOB( 9, GPIO_INPUT),
339 
356 
373 
390 
407 
420  /* FIXME WD */
424  DEF_GPIOG(15, GPIO_INPUT),
425 
426  // Crystal
427  //DEF_GPIOH( 0, GPIO_INPUT | GPIO_PULLUP),
428  //DEF_GPIOH( 1, GPIO_INPUT | GPIO_PULLUP),
443 
456 };
457 
458 status_e bsp_init(void)
459 {
460  status_e status;
461 
462  /* Set all pins to safe values */
463  gpio_setup_list(all_pins_safe_values, ARRAY_SIZE(all_pins_safe_values));
464 
465  time_init();
466 
467  bsp_pwm_leds_init(10*1000, LIGHTNESS_PWM_STEP, 0);
468 
469  retarget_init();
470 
471  /* Initialize the external RAM */
472  status = bsp_init_extRAM();
473  if (status != NO_ERROR)
474  {
475  return status;
476  }
477 
478  status = ram_test_basic((uint32_t *)0x60000000, 8*1024*1024);
479  if (status != NO_ERROR)
480  {
481  return status;
482  }
483 
484  load_init_external_ram();
485 
486  /* Setup auxiliary PSU for the camera */
487  status = psu_aux1_set(3000);
488  if (status != NO_ERROR)
489  {
490  return status;
491  }
492 
493  status = psu_aux0_set(1800);
494  if (status != NO_ERROR)
495  {
496  return status;
497  }
498 
499  status = bsp_light_init(10*1000, 255, 0);
500  if (status != NO_ERROR)
501  {
502  return status;
503  }
504 
505  status = bsp_buttons_init();
506  if (status != NO_ERROR)
507  {
508  return status;
509  }
510 
511  return NO_ERROR;
512 }
513 #endif
This file contains all the functions prototypes for the RCC firmware library.
BSP - Board Support Package.
#define DEF_GPIOF(__PIN, __MODE)
same as DEF_GPIOA for port F
Definition: stm32_gpio.h:364
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.
RAM test.
#define TIM_CCMR2_OC4M_2
Definition: stm32f4xx.h:6229
Output (default 1)
Definition: stm32_gpio.h:53
#define BSP_BUTTON_NR
Number of buttons.
Definition: bsp_armebs4.h:23
pull down
Definition: stm32_gpio.h:86
Timing parameters For NOR/SRAM Banks.
simple time abstraction
void * memcpy(void *dest, const void *src, size_t n)
void * memset(void *dest, int n, size_t n)
#define DEF_GPIOB(__PIN, __MODE)
same as DEF_GPIOA for port B
Definition: stm32_gpio.h:360
#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
FSMC NOR/SRAM Init structure definition.
Manageable PSU with an enable and a digital potentiometer.
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
#define DEF_GPIOD(__PIN, __MODE)
same as DEF_GPIOA for port D
Definition: stm32_gpio.h:362
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 DEF_GPIOG(__PIN, __MODE)
same as DEF_GPIOA for port G
Definition: stm32_gpio.h:365
#define TIM_CCMR2_OC4PE
Definition: stm32f4xx.h:6224
void bsp_led_toggle(uint32_t nr)
toggle a led
Definition: bsp_armebs4.c:249
Driver for mcp4552 digital potentiometer.
#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.
#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 DEF_GPIOC(__PIN, __MODE)
same as DEF_GPIOA for port C
Definition: stm32_gpio.h:361
#define TIM_CCMR2_OC3PE
Definition: stm32f4xx.h:6210
status_e ram_test_basic(uint32_t *base, size_t size)
Quickly test a RAM region.
Definition: ram_test.c:8
struct system_clock_t SystemClock
#define DEF_GPIOH(__PIN, __MODE)
same as DEF_GPIOA for port H
Definition: stm32_gpio.h:366
#define DEF_GPIOE(__PIN, __MODE)
same as DEF_GPIOA for port E
Definition: stm32_gpio.h:363
Output (default 0)
Definition: stm32_gpio.h:50
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
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
#define DEF_GPIOI(__PIN, __MODE)
same as DEF_GPIOA for port I
Definition: stm32_gpio.h:367
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