Revision 2d379838

View differences:

Target/Modules/DiWheelDrive_1-1/Boot/main.c
38 38
#include "timer.h"
39 39
#include "ARMCM3_STM32/types.h"
40 40
#include "AMiRo/amiroblt.h"
41
#include "AMiRo/helper.h"
41
#include "helper.h"
42 42
#include "iodef.h"
43 43

  
44 44
/****************************************************************************************
Target/Modules/DiWheelDrive_1-1/Boot/makefile
170 170
OFLAGS   = -O ihex
171 171
ODFLAGS  = -x
172 172
SZFLAGS  = -B -d
173
CFLAGS  += -D AMIRO_MODULE_DIWHEELDRIVE
173
CFLAGS  +=
174 174

  
175 175

  
176 176
#|---------------------------------------------------------------------------------------|
Target/Modules/LightRing_1-0/Boot/main.c
38 38
#include "timer.h"
39 39
#include "ARMCM3_STM32/types.h"
40 40
#include "AMiRo/amiroblt.h"
41
#include "AMiRo/helper.h"
41
#include "helper.h"
42 42
#include "iodef.h"
43 43

  
44 44
/****************************************************************************************
Target/Modules/LightRing_1-0/Boot/makefile
171 171
OFLAGS   = -O ihex
172 172
ODFLAGS  = -x
173 173
SZFLAGS  = -B -d
174
CFLAGS  += -D AMIRO_MODULE_LIGHTRING
174
CFLAGS  +=
175 175

  
176 176

  
177 177
#|---------------------------------------------------------------------------------------|
Target/Modules/PowerManagement_1-1/Boot/main.c
38 38
#include "com.h"
39 39
#include "ARMCM4_STM32/types.h"
40 40
#include "AMiRo/amiroblt.h"
41
#include "AMiRo/helper.h"
41
#include "helper.h"
42 42
#include "iodef.h"
43 43

  
44 44
/****************************************************************************************
Target/Modules/PowerManagement_1-1/Boot/makefile
190 190
OFLAGS   = -O ihex
191 191
ODFLAGS  = -x
192 192
SZFLAGS  = -B -d
193
CFLAGS  += -D AMIRO_MODULE_POWERMANAGEMENT
193
CFLAGS  +=
194 194

  
195 195

  
196 196
#|---------------------------------------------------------------------------------------|
Target/Source/AMiRo/helper.c
1
#include "helper.h"
2
#include <blt_conf.h>
3

  
4
/*
5
 * Initialized the system timer.
6
 */
7
void saTimerInit(void) {
8
  /* reset the timer configuration */
9
  saTimerReset();
10

  
11
  /* configure the systick frequency as a 1 ms event generator */
12
  SysTick->LOAD = BOOT_CPU_SYSTEM_SPEED_KHZ - 1;
13
  /* reset the current counter value */
14
  SysTick->VAL = 0;
15
  /* select core clock as source and enable the timer */
16
  SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk;
17
}
18

  
19
/*
20
 * Resets the systick status of the system timer.
21
 */
22
void saTimerReset(void) {
23
  /* set the systick's status and control register back into the default reset value */
24
  SysTick->CTRL = 0;
25
}
26

  
27
/*
28
 * Updates the given timer variable.
29
 * More specifically, the given variable in incremented if a millisecond event occurred.
30
 */
31
void saTimerUpdate(uint32_t* millisecond_counter) {
32
  /* check if the millisecond event occurred */
33
  if ((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) != 0)
34
  {
35
    /* increment the millisecond counter */
36
    ++(*millisecond_counter);
37
  }
38

  
39
  return;
40
}
41

  
42
/*
43
 * Actively polls the standalone timer until the specified time has passed.
44
 */
45
void msleep(uint32_t ms)
46
{
47
  uint32_t current;
48
  saTimerUpdate(&current);
49
  uint32_t end = current + ms;
50

  
51
  while (current < end)
52
  {
53
    saTimerUpdate(&current);
54
  }
55

  
56
  return;
57
}
58

  
59
/*
60
 * Actively reads the specified GPIO until it has the specified state.
61
 */
62
void waitForSignal(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction state) {
63
  /* check whether the signal has been set */
64
  while (GPIO_ReadInputDataBit(GPIOx, GPIO_Pin) != state) {
65
    continue;
66
  }
67
  return;
68
}
69

  
70
/*
71
 * Actively reads the specified GPIO until it has the specified state, or the specified time has passed.
72
 */
73
uint8_t waitForSignalTimeout(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, BitAction state, uint32_t timeout_ms) {
74
  uint32_t current_time;
75
  saTimerUpdate(&current_time);
76
  uint32_t timeout_time = current_time + timeout_ms;
77
  while ((GPIO_ReadInputDataBit(GPIOx, GPIO_Pin) != state) &&
78
         (current_time < timeout_time)) {
79
    saTimerUpdate(&current_time);
80
  }
81
  if (current_time < timeout_time) {
82
    return 1;
83
  } else {
84
    return 0;
85
  }
86
}
87

  
88
/*
89
 * Turns the board LED or or off respectively.
90
 * If the argument is zero, the LED is switched off.
91
 * If the argument is not zero, the LED is switched on.
92
 */
93
void setLed(uint8_t on) {
94

  
95
#if defined(AMIRO_MODULE_POWERMANAGEMENT)
96
#define LED_GPIO  GPIOB
97
#define LED_PIN   GPIO_Pin_12
98
#endif
99
#if defined(AMIRO_MODULE_DIWHEELDRIVE)
100
#define LED_GPIO  GPIOA
101
#define LED_PIN   GPIO_Pin_1
102
#endif
103
#if defined(AMIRO_MODULE_LIGHTRING)
104
  /* This is just a pseudo LED, since the LightRing does not feature a status LED */
105
#define LED_GPIO  GPIOA
106
#define LED_PIN   GPIO_Pin_1
107
#endif
108

  
109
#if defined(LED_GPIO) && defined(LED_PIN)
110
  if (on == 0) {
111
    GPIO_SetBits(LED_GPIO, LED_PIN);
112
  } else {
113
    GPIO_ResetBits(LED_GPIO, LED_PIN);
114
  }
115
#endif
116

  
117
  return;
118
}
119

  
120
/*
121
 * Makes the LED blink 'SOS' in morese code (... --- ...).
122
 * If the specified number of loops is zero, the function will loop infinitely.
123
 */
124
void blinkSOS(uint32_t loops) {
125
  /* initialize some variables and constants */
126
  enum State {BLINK_ERROR_S1,
127
              BLINK_ERROR_O,
128
              BLINK_ERROR_S2,
129
              BLINK_ERROR_BREAK
130
             } state = BLINK_ERROR_S1;
131
  uint8_t led = 0;
132
  uint32_t loop = 0;
133
  const uint32_t sigS = 50;
134
  const uint32_t sigL = 200;
135
  const uint32_t sigB = 100;
136
  const uint32_t letterBreakTime = 200;
137
  const uint32_t wordBreakTime = 1000;
138
  uint32_t stateStartTime = 0;
139
  saTimerUpdate(&stateStartTime);
140
  uint32_t currentTime = stateStartTime;
141

  
142
  /* either loop the specified number, or infinitely */
143
  while (loop < loops || loops == 0) {
144
    /* make the LED blink "SOS" (morse code: ... --- ...)*/
145
    led = 0;
146
    saTimerUpdate(&currentTime);
147
    switch (state) {
148
      case BLINK_ERROR_S1:
149
      case BLINK_ERROR_S2:
150
      {
151
        if (currentTime < stateStartTime + sigS) {
152
          led = 1;
153
        } else if (currentTime < stateStartTime + sigS+sigB) {
154
          led = 0;
155
        } else if (currentTime < stateStartTime + sigS+sigB+sigS) {
156
          led = 1;
157
        } else if (currentTime < stateStartTime + sigS+sigB+sigS+sigB) {
158
          led = 0;
159
        } else if (currentTime < stateStartTime + sigS+sigB+sigS+sigB+sigS) {
160
          led = 1;
161
        } else if (currentTime < stateStartTime + sigS+sigB+sigS+sigB+sigS+letterBreakTime) {
162
          led = 0;
163
        } else {
164
          if (state == BLINK_ERROR_S1) {
165
            state = BLINK_ERROR_O;
166
          } else {
167
            state = BLINK_ERROR_BREAK;
168
            ++loop;
169
          }
170
          stateStartTime = currentTime;
171
        }
172
        break;
173
      }
174
      case BLINK_ERROR_O:
175
      {
176
        if (currentTime < stateStartTime + sigL) {
177
          led = 1;
178
        } else if (currentTime < stateStartTime + sigL+sigB) {
179
          led = 0;
180
        } else if (currentTime < stateStartTime + sigL+sigB+sigL) {
181
          led = 1;
182
        } else if (currentTime < stateStartTime + sigL+sigB+sigL+sigB) {
183
          led = 0;
184
        } else if (currentTime < stateStartTime + sigL+sigB+sigL+sigB+sigL) {
185
          led = 1;
186
        } else if (currentTime < stateStartTime + sigL+sigB+sigL+sigB+sigL+letterBreakTime) {
187
          led = 0;
188
        } else {
189
          state = BLINK_ERROR_S2;
190
          stateStartTime = currentTime;
191
        }
192
        break;
193
      }
194
      case BLINK_ERROR_BREAK:
195
      {
196
        if (currentTime >= stateStartTime + wordBreakTime) {
197
          state = BLINK_ERROR_S1;
198
          stateStartTime = currentTime;
199
        }
200
        break;
201
      }
202
    }
203

  
204
    setLed(led);
205
  }
206

  
207
  return;
208
}
209

  
210
/*
211
 * Shortcut to make the LED blink SOS infinitely.
212
 */
213
inline void blinkSOSinf() {
214
  blinkSOS(0);
215
  return;
216
}
217

  
218
/*
219
 * Makes the LED blink 'OK' in morese code (... -.-).
220
 * If the specified number of loops is zero, the function will loop infinitely.
221
 */
222
void blinkOK(uint32_t loops) {
223
  /* initialize some variables and constants */
224
  enum State {BLINK_SUCCESS_O,
225
              BLINK_SUCCESS_K,
226
             BLINK_SUCCESS_BREAK
227
             } state = BLINK_SUCCESS_O;
228
  uint8_t led = 0;
229
  uint32_t loop = 0;
230
  const uint32_t sigS = 50;
231
  const uint32_t sigL = 200;
232
  const uint32_t sigB = 100;
233
  const uint32_t letterBreakTime = 200;
234
  const uint32_t wordBreakTime = 1000;
235
  uint32_t stateStartTime = 0;
236
  saTimerUpdate(&stateStartTime);
237
  uint32_t currentTime = stateStartTime;
238

  
239
  /* either loop the specified number, or infinitely */
240
  while (loop < loops || loops == 0)
241
  {
242
    /* make the LED blink "OK" (morse code: --- -.-)*/
243
    led = 0;
244
    saTimerUpdate(&currentTime);
245
    switch (state) {
246
      case BLINK_SUCCESS_O:
247
      {
248
        if (currentTime < stateStartTime + sigL) {
249
          led = 1;
250
        } else if (currentTime < stateStartTime + sigL+sigB) {
251
          led = 0;
252
        } else if (currentTime < stateStartTime + sigL+sigB+sigL) {
253
          led = 1;
254
        } else if (currentTime < stateStartTime + sigL+sigB+sigL+sigB) {
255
          led = 0;
256
        } else if (currentTime < stateStartTime + sigL+sigB+sigL+sigB+sigL) {
257
          led = 1;
258
        } else if (currentTime < stateStartTime + sigL+sigB+sigL+sigB+sigL+letterBreakTime) {
259
          led = 0;
260
        } else {
261
          state = BLINK_SUCCESS_K;
262
          stateStartTime = currentTime;
263
        }
264
        break;
265
      }
266
      case BLINK_SUCCESS_K:
267
      {
268
        if (currentTime < stateStartTime + sigL) {
269
          led = 1;
270
        } else if (currentTime < stateStartTime + sigL+sigB) {
271
          led = 0;
272
        } else if (currentTime < stateStartTime + sigL+sigB+sigS) {
273
          led = 1;
274
        } else if (currentTime < stateStartTime + sigL+sigB+sigS+sigB) {
275
          led = 0;
276
        } else if (currentTime < stateStartTime + sigL+sigB+sigS+sigB+sigL) {
277
          led = 1;
278
        } else if (currentTime < stateStartTime + sigL+sigB+sigS+sigB+sigL+letterBreakTime) {
279
          led = 0;
280
        } else {
281
          state = BLINK_SUCCESS_BREAK;
282
          ++loop;
283
          stateStartTime = currentTime;
284
        }
285
        break;
286
      }
287
      case BLINK_SUCCESS_BREAK:
288
      {
289
        if (currentTime >= stateStartTime + wordBreakTime) {
290
          state = BLINK_SUCCESS_O;
291
          stateStartTime = currentTime;
292
        }
293
        break;
294
      }
295
    }
296

  
297
    setLed(led);
298
  }
299

  
300
  return;
301
}
302

  
303
/*
304
 * Shortcut to make the LED blink OK infinitely.
305
 */
306
inline void blinkOKinf() {
307
  blinkOK(0);
308
  return;
309
}
310

  
311
/*
312
 * Makes the LED visualize the specified data.
313
 * Starting with the MSB of the first of the 'n' bytes, zeros are visualized as short flash and ones as long flash.
314
 * If the specified number of loops is zero, the function will loop infinitely.
315
 */
316
void visualizeData(uint8_t* data, uint32_t bytes, uint32_t loops) {
317
  /* initialize some variables and constants */
318
  enum State {BLINK_DATA_BIT,
319
              BLINK_DATA_BYTE_BREAK,
320
              BLINK_DATA_LOOP_BREAK
321
             } state = BLINK_DATA_BIT;
322
  uint8_t led = 0;
323
  uint8_t mask = 0x80;
324
  uint32_t byte = 0;
325
  uint32_t loop = 0;
326
  const uint32_t sigS = 50;
327
  const uint32_t sigL = 200;
328
  const uint32_t interBitBreak = 500;
329
  const uint32_t interByteBreak = 1000;
330
  const uint32_t interLoopBreak = 2500;
331
  uint32_t flash_dur = 0;
332
  uint32_t stateStartTime = 0;
333
  saTimerUpdate(&stateStartTime);
334
  uint32_t currentTime = stateStartTime;
335

  
336
  /* return immediately if the number of bytes is zero */
337
  if (bytes == 0) {
338
    return;
339
  }
340

  
341
  /* either loop the specified number, or infinetly */
342
  while (loop < loops || loops == 0) {
343
    led = 0;
344
    saTimerUpdate(&currentTime);
345
    switch (state) {
346
      case BLINK_DATA_BIT:
347
      {
348
        if (data[byte] & mask) {
349
          flash_dur = sigL;
350
        } else {
351
          flash_dur = sigS;
352
        }
353
        if (currentTime < stateStartTime + flash_dur) {
354
          led = 1;
355
        } else if (currentTime < stateStartTime + flash_dur+interBitBreak) {
356
          led = 0;
357
        } else {
358
          mask = mask >> 1;
359
          if (mask > 0) {
360
            state = BLINK_DATA_BIT;
361
          } else if (byte < bytes-1) {
362
            state = BLINK_DATA_BYTE_BREAK;
363
          } else {
364
            state = BLINK_DATA_LOOP_BREAK;
365
            ++loop;
366
          }
367
          stateStartTime = currentTime;
368
        }
369
        break;
370
      }
371
      case BLINK_DATA_BYTE_BREAK:
372
      {
373
        if (currentTime >= stateStartTime + interByteBreak) {
374
          mask = 0x80;
375
          state = BLINK_DATA_BIT;
376
          ++byte;
377
          stateStartTime = currentTime;
378
        }
379
        break;
380
      }
381
      case BLINK_DATA_LOOP_BREAK:
382
      {
383
        if (currentTime >= stateStartTime + interLoopBreak) {
384
          mask = 0x80;
385
          state = BLINK_DATA_BIT;
386
          byte = 0;
387
          stateStartTime = currentTime;
388
        }
389
        break;
390
      }
391
    }
392

  
393
    setLed(led);
394
  }
395

  
396
  return;
397
}
398

  
399
/*
400
 * Makes the LED visualize the specified byte.
401
 * Starting with the MSB, zeros are visualized as short flash and ones as long flash.
402
 * If the specified number of loops is zero, the function will loop infinitely.
403
 */
404
void visualizeByte(uint8_t byte, uint32_t loops) {
405
  visualizeData(&byte, 1, loops);
406
  return;
407
}
408

  
Target/Source/AMiRo/helper.h
1
#ifndef HELPER_H
2
#define HELPER_H
3

  
4
#include <stdint.h>
5
/*
6
 * The AMiRo module must is defined through the makefile
7
 */
8
#if defined(AMIRO_MODULE_POWERMANAGEMENT)
9
#include <stm32f4xx.h>
10
#endif
11
#if defined(AMIRO_MODULE_DIWHEELDRIVE) || defined(AMIRO_MODULE_LIGHTRING)
12
#include <stm32f10x.h>
13
#endif
14

  
15
/****************************************************************************************
16
* Standalone timer, that does not use any static variables.
17
* Except for the static counter variable, this timer is identical to the OpenBLT timer.h
18
* implementation. With the standalone timer, this variable must be stored externally and
19
* must be given to the saTimerUpdate() function as argument.
20
****************************************************************************************/
21
void saTimerInit(void);
22
void saTimerUpdate(uint32_t *millisecond_counter);
23
void saTimerReset(void);
24
/***************************************************************************************/
25

  
26
/****************************************************************************************
27
* Helper functions that implement a actively polling loop until a specific event occurs.
28
****************************************************************************************/
29
void msleep(uint32_t ms);
30
void waitForSignal(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, BitAction state);
31
uint8_t waitForSignalTimeout(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, BitAction state, uint32_t timeout_ms);
32
/***************************************************************************************/
33

  
34
/****************************************************************************************
35
* Helper functions that use the LED to signal some states or visualize data.
36
****************************************************************************************/
37
void setLed(uint8_t on);
38
void blinkSOS(uint32_t loops);
39
void blinkSOSinf(void);
40
void blinkOK(uint32_t loops);
41
void blinkOKinf(void);
42
void visualizeData(uint8_t* data, uint32_t bytes, uint32_t loops);
43
void visualizeByte(uint8_t byte, uint32_t loops);
44
/***************************************************************************************/
45

  
46
#endif // HELPER_H
Target/Source/helper.c
1
#include "helper.h"
2

  
3
#include "blt_conf.h"
4

  
5
/*
6
 * Initialized the system timer.
7
 */
8
void saTimerInit(void) {
9
  /* reset the timer configuration */
10
  saTimerReset();
11

  
12
  /* configure the systick frequency as a 1 ms event generator */
13
  SysTick->LOAD = BOOT_CPU_SYSTEM_SPEED_KHZ - 1;
14
  /* reset the current counter value */
15
  SysTick->VAL = 0;
16
  /* select core clock as source and enable the timer */
17
  SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk;
18
}
19

  
20
/*
21
 * Resets the systick status of the system timer.
22
 */
23
void saTimerReset(void) {
24
  /* set the systick's status and control register back into the default reset value */
25
  SysTick->CTRL = 0;
26
}
27

  
28
/*
29
 * Updates the given timer variable.
30
 * More specifically, the given variable in incremented if a millisecond event occurred.
31
 */
32
void saTimerUpdate(uint32_t* millisecond_counter) {
33
  /* check if the millisecond event occurred */
34
  if ((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) != 0)
35
  {
36
    /* increment the millisecond counter */
37
    ++(*millisecond_counter);
38
  }
39

  
40
  return;
41
}
42

  
43
/*
44
 * Actively polls the standalone timer until the specified time has passed.
45
 */
46
void msleep(uint32_t ms)
47
{
48
  uint32_t current;
49
  saTimerUpdate(&current);
50
  uint32_t end = current + ms;
51

  
52
  while (current < end)
53
  {
54
    saTimerUpdate(&current);
55
  }
56

  
57
  return;
58
}
59

  
60
/*
61
 * Actively reads the specified GPIO until it has the specified state.
62
 */
63
void waitForSignal(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction state) {
64
  /* check whether the signal has been set */
65
  while (GPIO_ReadInputDataBit(GPIOx, GPIO_Pin) != state) {
66
    continue;
67
  }
68
  return;
69
}
70

  
71
/*
72
 * Actively reads the specified GPIO until it has the specified state, or the specified time has passed.
73
 */
74
uint8_t waitForSignalTimeout(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, BitAction state, uint32_t timeout_ms) {
75
  uint32_t current_time;
76
  saTimerUpdate(&current_time);
77
  uint32_t timeout_time = current_time + timeout_ms;
78
  while ((GPIO_ReadInputDataBit(GPIOx, GPIO_Pin) != state) &&
79
         (current_time < timeout_time)) {
80
    saTimerUpdate(&current_time);
81
  }
82
  if (current_time < timeout_time) {
83
    return 1;
84
  } else {
85
    return 0;
86
  }
87
}
88

  
89
/*
90
 * Turns the board LED or or off respectively.
91
 * If the argument is zero, the LED is switched off.
92
 * If the argument is not zero, the LED is switched on.
93
 */
94
void setLed(uint8_t on) {
95

  
96
#if defined(LED_GPIO) && defined(LED_PIN)
97
  if (on == 0) {
98
    GPIO_SetBits(LED_GPIO, LED_PIN);
99
  } else {
100
    GPIO_ResetBits(LED_GPIO, LED_PIN);
101
  }
102
#endif
103

  
104
  return;
105
}
106

  
107
/*
108
 * Makes the LED blink 'SOS' in morese code (... --- ...).
109
 * If the specified number of loops is zero, the function will loop infinitely.
110
 */
111
void blinkSOS(uint32_t loops) {
112
  /* initialize some variables and constants */
113
  enum State {BLINK_ERROR_S1,
114
              BLINK_ERROR_O,
115
              BLINK_ERROR_S2,
116
              BLINK_ERROR_BREAK
117
             } state = BLINK_ERROR_S1;
118
  uint8_t led = 0;
119
  uint32_t loop = 0;
120
  const uint32_t sigS = 50;
121
  const uint32_t sigL = 200;
122
  const uint32_t sigB = 100;
123
  const uint32_t letterBreakTime = 200;
124
  const uint32_t wordBreakTime = 1000;
125
  uint32_t stateStartTime = 0;
126
  saTimerUpdate(&stateStartTime);
127
  uint32_t currentTime = stateStartTime;
128

  
129
  /* either loop the specified number, or infinitely */
130
  while (loop < loops || loops == 0) {
131
    /* make the LED blink "SOS" (morse code: ... --- ...)*/
132
    led = 0;
133
    saTimerUpdate(&currentTime);
134
    switch (state) {
135
      case BLINK_ERROR_S1:
136
      case BLINK_ERROR_S2:
137
      {
138
        if (currentTime < stateStartTime + sigS) {
139
          led = 1;
140
        } else if (currentTime < stateStartTime + sigS+sigB) {
141
          led = 0;
142
        } else if (currentTime < stateStartTime + sigS+sigB+sigS) {
143
          led = 1;
144
        } else if (currentTime < stateStartTime + sigS+sigB+sigS+sigB) {
145
          led = 0;
146
        } else if (currentTime < stateStartTime + sigS+sigB+sigS+sigB+sigS) {
147
          led = 1;
148
        } else if (currentTime < stateStartTime + sigS+sigB+sigS+sigB+sigS+letterBreakTime) {
149
          led = 0;
150
        } else {
151
          if (state == BLINK_ERROR_S1) {
152
            state = BLINK_ERROR_O;
153
          } else {
154
            state = BLINK_ERROR_BREAK;
155
            ++loop;
156
          }
157
          stateStartTime = currentTime;
158
        }
159
        break;
160
      }
161
      case BLINK_ERROR_O:
162
      {
163
        if (currentTime < stateStartTime + sigL) {
164
          led = 1;
165
        } else if (currentTime < stateStartTime + sigL+sigB) {
166
          led = 0;
167
        } else if (currentTime < stateStartTime + sigL+sigB+sigL) {
168
          led = 1;
169
        } else if (currentTime < stateStartTime + sigL+sigB+sigL+sigB) {
170
          led = 0;
171
        } else if (currentTime < stateStartTime + sigL+sigB+sigL+sigB+sigL) {
172
          led = 1;
173
        } else if (currentTime < stateStartTime + sigL+sigB+sigL+sigB+sigL+letterBreakTime) {
174
          led = 0;
175
        } else {
176
          state = BLINK_ERROR_S2;
177
          stateStartTime = currentTime;
178
        }
179
        break;
180
      }
181
      case BLINK_ERROR_BREAK:
182
      {
183
        if (currentTime >= stateStartTime + wordBreakTime) {
184
          state = BLINK_ERROR_S1;
185
          stateStartTime = currentTime;
186
        }
187
        break;
188
      }
189
    }
190

  
191
    setLed(led);
192
  }
193

  
194
  return;
195
}
196

  
197
/*
198
 * Shortcut to make the LED blink SOS infinitely.
199
 */
200
inline void blinkSOSinf() {
201
  blinkSOS(0);
202
  return;
203
}
204

  
205
/*
206
 * Makes the LED blink 'OK' in morese code (... -.-).
207
 * If the specified number of loops is zero, the function will loop infinitely.
208
 */
209
void blinkOK(uint32_t loops) {
210
  /* initialize some variables and constants */
211
  enum State {BLINK_SUCCESS_O,
212
              BLINK_SUCCESS_K,
213
             BLINK_SUCCESS_BREAK
214
             } state = BLINK_SUCCESS_O;
215
  uint8_t led = 0;
216
  uint32_t loop = 0;
217
  const uint32_t sigS = 50;
218
  const uint32_t sigL = 200;
219
  const uint32_t sigB = 100;
220
  const uint32_t letterBreakTime = 200;
221
  const uint32_t wordBreakTime = 1000;
222
  uint32_t stateStartTime = 0;
223
  saTimerUpdate(&stateStartTime);
224
  uint32_t currentTime = stateStartTime;
225

  
226
  /* either loop the specified number, or infinitely */
227
  while (loop < loops || loops == 0)
228
  {
229
    /* make the LED blink "OK" (morse code: --- -.-)*/
230
    led = 0;
231
    saTimerUpdate(&currentTime);
232
    switch (state) {
233
      case BLINK_SUCCESS_O:
234
      {
235
        if (currentTime < stateStartTime + sigL) {
236
          led = 1;
237
        } else if (currentTime < stateStartTime + sigL+sigB) {
238
          led = 0;
239
        } else if (currentTime < stateStartTime + sigL+sigB+sigL) {
240
          led = 1;
241
        } else if (currentTime < stateStartTime + sigL+sigB+sigL+sigB) {
242
          led = 0;
243
        } else if (currentTime < stateStartTime + sigL+sigB+sigL+sigB+sigL) {
244
          led = 1;
245
        } else if (currentTime < stateStartTime + sigL+sigB+sigL+sigB+sigL+letterBreakTime) {
246
          led = 0;
247
        } else {
248
          state = BLINK_SUCCESS_K;
249
          stateStartTime = currentTime;
250
        }
251
        break;
252
      }
253
      case BLINK_SUCCESS_K:
254
      {
255
        if (currentTime < stateStartTime + sigL) {
256
          led = 1;
257
        } else if (currentTime < stateStartTime + sigL+sigB) {
258
          led = 0;
259
        } else if (currentTime < stateStartTime + sigL+sigB+sigS) {
260
          led = 1;
261
        } else if (currentTime < stateStartTime + sigL+sigB+sigS+sigB) {
262
          led = 0;
263
        } else if (currentTime < stateStartTime + sigL+sigB+sigS+sigB+sigL) {
264
          led = 1;
265
        } else if (currentTime < stateStartTime + sigL+sigB+sigS+sigB+sigL+letterBreakTime) {
266
          led = 0;
267
        } else {
268
          state = BLINK_SUCCESS_BREAK;
269
          ++loop;
270
          stateStartTime = currentTime;
271
        }
272
        break;
273
      }
274
      case BLINK_SUCCESS_BREAK:
275
      {
276
        if (currentTime >= stateStartTime + wordBreakTime) {
277
          state = BLINK_SUCCESS_O;
278
          stateStartTime = currentTime;
279
        }
280
        break;
281
      }
282
    }
283

  
284
    setLed(led);
285
  }
286

  
287
  return;
288
}
289

  
290
/*
291
 * Shortcut to make the LED blink OK infinitely.
292
 */
293
inline void blinkOKinf() {
294
  blinkOK(0);
295
  return;
296
}
297

  
298
/*
299
 * Makes the LED visualize the specified data.
300
 * Starting with the MSB of the first of the 'n' bytes, zeros are visualized as short flash and ones as long flash.
301
 * If the specified number of loops is zero, the function will loop infinitely.
302
 */
303
void visualizeData(uint8_t* data, uint32_t bytes, uint32_t loops) {
304
  /* initialize some variables and constants */
305
  enum State {BLINK_DATA_BIT,
306
              BLINK_DATA_BYTE_BREAK,
307
              BLINK_DATA_LOOP_BREAK
308
             } state = BLINK_DATA_BIT;
309
  uint8_t led = 0;
310
  uint8_t mask = 0x80;
311
  uint32_t byte = 0;
312
  uint32_t loop = 0;
313
  const uint32_t sigS = 50;
314
  const uint32_t sigL = 200;
315
  const uint32_t interBitBreak = 500;
316
  const uint32_t interByteBreak = 1000;
317
  const uint32_t interLoopBreak = 2500;
318
  uint32_t flash_dur = 0;
319
  uint32_t stateStartTime = 0;
320
  saTimerUpdate(&stateStartTime);
321
  uint32_t currentTime = stateStartTime;
322

  
323
  /* return immediately if the number of bytes is zero */
324
  if (bytes == 0) {
325
    return;
326
  }
327

  
328
  /* either loop the specified number, or infinetly */
329
  while (loop < loops || loops == 0) {
330
    led = 0;
331
    saTimerUpdate(&currentTime);
332
    switch (state) {
333
      case BLINK_DATA_BIT:
334
      {
335
        if (data[byte] & mask) {
336
          flash_dur = sigL;
337
        } else {
338
          flash_dur = sigS;
339
        }
340
        if (currentTime < stateStartTime + flash_dur) {
341
          led = 1;
342
        } else if (currentTime < stateStartTime + flash_dur+interBitBreak) {
343
          led = 0;
344
        } else {
345
          mask = mask >> 1;
346
          if (mask > 0) {
347
            state = BLINK_DATA_BIT;
348
          } else if (byte < bytes-1) {
349
            state = BLINK_DATA_BYTE_BREAK;
350
          } else {
351
            state = BLINK_DATA_LOOP_BREAK;
352
            ++loop;
353
          }
354
          stateStartTime = currentTime;
355
        }
356
        break;
357
      }
358
      case BLINK_DATA_BYTE_BREAK:
359
      {
360
        if (currentTime >= stateStartTime + interByteBreak) {
361
          mask = 0x80;
362
          state = BLINK_DATA_BIT;
363
          ++byte;
364
          stateStartTime = currentTime;
365
        }
366
        break;
367
      }
368
      case BLINK_DATA_LOOP_BREAK:
369
      {
370
        if (currentTime >= stateStartTime + interLoopBreak) {
371
          mask = 0x80;
372
          state = BLINK_DATA_BIT;
373
          byte = 0;
374
          stateStartTime = currentTime;
375
        }
376
        break;
377
      }
378
    }
379

  
380
    setLed(led);
381
  }
382

  
383
  return;
384
}
385

  
386
/*
387
 * Makes the LED visualize the specified byte.
388
 * Starting with the MSB, zeros are visualized as short flash and ones as long flash.
389
 * If the specified number of loops is zero, the function will loop infinitely.
390
 */
391
void visualizeByte(uint8_t byte, uint32_t loops) {
392
  visualizeData(&byte, 1, loops);
393
  return;
394
}
395

  
Target/Source/helper.h
1
#ifndef HELPER_H
2
#define HELPER_H
3

  
4
#include <stdint.h>
5
#include <iodef.h>
6

  
7
/****************************************************************************************
8
* Standalone timer, that does not use any static variables.
9
* Except for the static counter variable, this timer is identical to the OpenBLT timer.h
10
* implementation. With the standalone timer, this variable must be stored externally and
11
* must be given to the saTimerUpdate() function as argument.
12
****************************************************************************************/
13
void saTimerInit(void);
14
void saTimerUpdate(uint32_t *millisecond_counter);
15
void saTimerReset(void);
16
/***************************************************************************************/
17

  
18
/****************************************************************************************
19
* Helper functions that implement a actively polling loop until a specific event occurs.
20
****************************************************************************************/
21
void msleep(uint32_t ms);
22
void waitForSignal(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, BitAction state);
23
uint8_t waitForSignalTimeout(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, BitAction state, uint32_t timeout_ms);
24
/***************************************************************************************/
25

  
26
/****************************************************************************************
27
* Helper functions that use the LED to signal some states or visualize data.
28
****************************************************************************************/
29
void setLed(uint8_t on);
30
void blinkSOS(uint32_t loops);
31
void blinkSOSinf(void);
32
void blinkOK(uint32_t loops);
33
void blinkOKinf(void);
34
void visualizeData(uint8_t* data, uint32_t bytes, uint32_t loops);
35
void visualizeByte(uint8_t byte, uint32_t loops);
36
/***************************************************************************************/
37

  
38
#endif // HELPER_H

Also available in: Unified diff