ARMEBS4  revision-26.06.2015
bsp.c
1 #include "heivs/bsp.h"
2 #include "heivs/stm32_gpio_irq.h"
3 #include "heivs/delay.h"
4 #include "stm32/stm32f4xx_rcc.h"
5 
6 static uint32_t was;
7 static void (*user_handler)(uint32_t is, uint32_t was);
8 
9 uint32_t bsp_button_get_all(void)
10 {
11  uint32_t i;
12  uint32_t value = 0;
13 
14  for (i = 0 ; i < BSP_BUTTON_NR; i++)
15  {
16  value |= (bsp_button_get(i) << i);
17  }
18 
19  return value;
20 }
21 
22 static uint32_t buttons_handler(const struct gpio_t *gpio)
23 {
24  uint32_t is = bsp_button_get_all();
25 
26  (void)gpio;
27 
28  user_handler(is, was);
29  was = is;
30 
31  return 1;
32 }
33 
34 status_e bsp_set_button_irq_handler(void (*handler)(uint32_t is, uint32_t was))
35 {
36  uint32_t i ;
37 
38  if (!handler)
39  {
40  return ERROR_BAD_PARAM;
41  }
42 
43  user_handler = handler;
44 
45  was = bsp_button_get_all();
46 
47  for (i = 0 ; i < BSP_BUTTON_NR; i++)
48  {
49  gpio_irq_setup(&bsp_button_gpios()[i], buttons_handler, GPIO_IRQ_MODE_EDGE_BOTH, 12);
50  }
51 
52  return NO_ERROR;
53 }
54 
55 /**
56  * \brief set all leds
57  *
58  * \param enable for leds on
59  */
60 static void set_all_leds(uint32_t enable)
61 {
62  uint32_t i;
63 
64  for (i = 0 ; i < BSP_LED_NR ; i++)
65  {
66  bsp_led_set(i, enable);
67  }
68 }
69 
70 
71 /**
72  * \brief light a short pulse
73  */
74 static void short_pulse(void)
75 {
76  set_all_leds(1);
77  delay_wait_ms(125);
78  set_all_leds(0);
79  delay_wait_ms(250);
80 }
81 
82 /*
83  * \brief light a long pulse
84  */
85 static void long_pulse(void)
86 {
87  set_all_leds(1);
88  delay_wait_ms(500);
89  set_all_leds(0);
90  delay_wait_ms(250);
91 }
92 
93 /**
94  * \brief send a SOS using the leds
95  */
96 static __attribute__ ((noreturn))
97 void SOS(status_e status)
98 {
99  (void)status;
100  for (;;)
101  {
102  short_pulse();
103  short_pulse();
104  short_pulse();
105 
106  delay_wait_ms(500);
107 
108  long_pulse();
109  long_pulse();
110  long_pulse();
111 
112  delay_wait_ms(500);
113 
114  short_pulse();
115  short_pulse();
116  short_pulse();
117 
118  delay_wait_ms(1000);
119  }
120 }
121 
122 __attribute__ ((weak, noreturn))
123 void bsp_fatal(status_e status)
124 {
125  (void)status;
126 
127  __disable_irq();
128  __disable_fault_irq();
129 
130  /**
131  * When the debugger is connected, attract attention from the programmer
132  */
133  if (debugger_is_connected())
134  {
135  breakpoint();
136  }
137 
138  /**
139  * FIXME : Should we SOS or fire the external watchdog ?
140  */
141  SOS(status);
142 }
143 
144 /**
145  * \brief Called by the libc exit function
146  *
147  * Embedded software should not exit main :-/
148  */
149 __attribute__ ((weak, noreturn))
150 void _exit(int status)
151 {
152  /**
153  * When the debugger is connected, attract attention from the programmer
154  */
155  if (debugger_is_connected())
156  {
157  /**
158  * Uh oh, main exited ?!?
159  */
160  breakpoint();
161  }
162 
163  if (status == NO_ERROR)
164  {
165  // main is not expected to return in an embedded system
167  }
168 
169  //Forward the current error to bsp_fatal
170  bsp_fatal(status);
171 }
172 
173 static uint32_t rcc_csr_at_boot = 0;
174 
176 {
177  rcc_csr_at_boot = RCC->CSR;
178 }
179 
180 uint32_t bsp_reset_reason_get(void)
181 {
182  return rcc_csr_at_boot;
183 }
184 
186 {
187  RCC->CSR |= RCC_CSR_RMVF;
188 }
This file contains all the functions prototypes for the RCC firmware library.
BSP - Board Support Package.
void delay_wait_ms(uint32_t ms)
Wait for at least that time.
Definition: delay.c:12
void bsp_reset_reason_clear(void)
Reset reason clear.
Definition: bsp.c:185
#define BSP_BUTTON_NR
Number of buttons.
Definition: bsp_armebs4.h:23
#define breakpoint()
Breakpoint (from code)
Definition: utils.h:74
void bsp_fatal(status_e status)
fatal error
Definition: bsp.c:123
status_e gpio_irq_setup(const struct gpio_t *gpio, uint32_t(*handler)(const struct gpio_t *gpio), enum gpio_irq_mode_e mode, uint32_t prio)
Setup interrupt on GPIO.
void bsp_led_set(uint32_t nr, uint32_t value)
Set a led (ON or OFF)
Definition: bsp_armebs4.c:244
Parameter unsupported.
Definition: error.h:56
GPIO control structure.
Definition: stm32_gpio.h:96
status_e bsp_set_button_irq_handler(void(*handler)(uint32_t is, uint32_t was))
Set a irq handler for all button changes.
Definition: bsp.c:34
uint32_t bsp_reset_reason_get(void)
Reset reason get.
Definition: bsp.c:180
simple delays
uint32_t bsp_button_get_all(void)
Get all buttons.
Definition: bsp.c:9
void bsp_reset_reason_init(void)
Reset reason initialization.
Definition: bsp.c:175
#define debugger_is_connected()
Detect is debugger is connected.
Definition: utils.h:67
#define BSP_LED_NR
Number of LEDs.
Definition: bsp_armebs4.h:20
status_e
Known errors.
Definition: error.h:21
No error.
Definition: error.h:28
const struct gpio_t * bsp_button_gpios(void)
get buttons gpios
Definition: bsp_armebs4.c:350
GPIO interrupts.
main has finished (shouldn't happen in embedded software)
Definition: error.h:70
uint32_t bsp_button_get(uint32_t nr)
Get a button.
Definition: bsp_armebs4.c:340