Paparazzi UAS  v5.8.2_stable-0-g6260b7c
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
chibios_init.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Gautier Hattenberger, Alexandre Bustico
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
22 /*
23  * @file firmwares/fixedwing/chibios-libopencm3/chibios_init.c
24  *
25  */
26 
27 #include <ch.h>
28 #include <hal.h>
29 #include "subsystems/chibios-libopencm3/chibios_sdlog.h"
30 #include "sdLog.h"
31 #include "usbStorage.h"
32 #include "pprz_stub.h"
33 #include "rtcAccess.h"
34 #include "generated/airframe.h"
35 #include "chibios_init.h"
36 
37 // Delay before starting SD log
38 #ifndef SDLOG_START_DELAY
39 #define SDLOG_START_DELAY 30
40 #endif
41 
42 
43 #ifndef SYS_TIME_FREQUENCY
44 #error SYS_TIME_FREQUENCY should be defined in Makefile.chibios or airframe.xml and be equal to CH_FREQUENCY
45 #elif SYS_TIME_FREQUENCY != CH_FREQUENCY
46 #error SYS_TIME_FREQUENCY should be equal to CH_FREQUENCY
47 #elif CH_FREQUENCY < (2 * PERIODIC_FREQUENCY)
48 #error CH_FREQUENCY and SYS_TIME_FREQUENCY should be >= 2 x PERIODIC_FREQUENCY
49 #endif
50 
51 static __attribute__((noreturn)) msg_t thd_heartbeat(void *arg);
52 #define MAX(x , y) (((x) > (y)) ? (x) : (y))
53 #define ARRAY_LEN(a) (sizeof(a)/sizeof(a[0]))
54 
55 Thread *pprzThdPtr = NULL;
56 
57 static WORKING_AREA(wa_thd_heartbeat, 2048);
58 void chibios_launch_heartbeat (void);
59 bool_t sdOk = FALSE;
60 
61 
62 
63 /*
64  * Init ChibiOS HAL and Sys
65  */
66 bool_t chibios_init(void) {
67  halInit();
68  chSysInit();
69 
70  PWR->CSR &= ~PWR_CSR_BRE;
71  DBGMCU->APB1FZ |= DBGMCU_APB1_FZ_DBG_IWDG_STOP;
72 
73  chThdCreateStatic(wa_thd_heartbeat, sizeof(wa_thd_heartbeat),
74  NORMALPRIO, thd_heartbeat, NULL);
75 
76  usbStorageStartPolling ();
77  return RDY_OK;
78 }
79 
80 static WORKING_AREA(pprzThd, 4096);
81 void launch_pprz_thd (int32_t (*thd) (void *arg))
82 {
83  pprzThdPtr = chThdCreateStatic(pprzThd, sizeof(pprzThd), NORMALPRIO+1, thd, NULL);
84 }
85 
86 
87 /*
88  * Heartbeat thread
89  */
90 static __attribute__((noreturn)) msg_t thd_heartbeat(void *arg)
91 {
92  (void) arg;
93  chRegSetThreadName("pprz heartbeat");
94 
95  chThdSleepSeconds (SDLOG_START_DELAY);
96  if (usbStorageIsItRunning ())
97  chThdSleepSeconds (20000); // stuck here for hours
98  else
99  sdOk = chibios_logInit();
100 
101  while (TRUE) {
102  palTogglePad (GPIOC, GPIOC_LED3);
103  chThdSleepMilliseconds (sdOk == TRUE ? 1000 : 200);
104  static uint32_t timestamp = 0;
105 
106 
107  // we sync gps time to rtc every 5 seconds
108  if (chTimeNow() - timestamp > 5000) {
109  timestamp = chTimeNow();
110  if (getGpsTimeOfWeek() != 0) {
111  setRtcFromGps (getGpsWeek(), getGpsTimeOfWeek());
112  }
113  }
114 
115  }
116 }
117 
118 
static msg_t thd_heartbeat(void *arg)
Definition: chibios_init.c:90
#define GPIOC
Definition: gpio_arch.h:34
void chibios_launch_heartbeat(void)
static WORKING_AREA(wa_thd_heartbeat, 2048)
bool_t chibios_init(void)
Definition: chibios_init.c:66
bool_t sdOk
Definition: chibios_init.c:59
#define FALSE
Definition: std.h:5
Thread * pprzThdPtr
Definition: chibios_init.c:55
#define TRUE
Definition: std.h:4
unsigned long uint32_t
Definition: types.h:18
#define SDLOG_START_DELAY
Definition: chibios_init.c:39
signed long int32_t
Definition: types.h:19
void launch_pprz_thd(int32_t(*thd)(void *arg))
Definition: chibios_init.c:81