Paparazzi UAS  v5.14.0_stable-0-g3f680d1
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
board.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 The Paparazzi Team
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, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
28 #include "boards/swing.h"
29 #include "mcu.h"
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <pthread.h>
36 #include <linux/input.h>
37 #include "subsystems/electrical.h"
38 
42 static void *bat_read(void *data __attribute__((unused)))
43 {
44  FILE *fp;
45  char path[16];
46 
47  while (TRUE) {
48  /* Open the command for reading. */
49  fp = popen("cat /sys/devices/platform/p6-spi.2/spi2.0/vbat", "r");
50  if (fp == NULL) {
51  printf("Failed to read battery\n");
52  } else {
53  /* Read the output a line at a time - output it. */
54  while (fgets(path, sizeof(path) - 1, fp) != NULL) {
55  int raw_bat = atoi(path);
56  // convert to decivolt
57  // from /bin/mcu_vbat.sh: MILLIVOLTS_VALUE=$(( ($RAW_VALUE * 4250) / 1023 ))
58  electrical.vsupply = ((raw_bat * 4250) / 1023) / 100;
59  }
60  /* close */
61  pclose(fp);
62  }
63 
64  // Wait 100ms
65  // reading is done at 10Hz like the electrical_periodic from rotorcraft main program
66  usleep(100000);
67  }
68 
69  return NULL;
70 }
71 
75 static void *button_read(void *data __attribute__((unused)))
76 {
77  struct input_event ev;
78  ssize_t n;
79 
80  /* Open power button event sysfs file */
81  int fd_button = open("/dev/input/pm_mcu_event", O_RDONLY);
82  if (fd_button == -1) {
83  printf("Unable to open mcu_event to read power button state\n");
84  return NULL;
85  }
86 
87  while (TRUE) {
88  /* Check power button (read is blocking) */
89  n = read(fd_button, &ev, sizeof(ev));
90  if (n == sizeof(ev) && ev.type == EV_KEY && ev.code == KEY_POWER && ev.value > 0) {
91  printf("Stopping Paparazzi from power button and rebooting\n");
92  usleep(1000);
93  int ret __attribute__((unused)) = system("reboot.sh");
94  exit(0);
95  }
96  }
97 
98  return NULL;
99 }
100 
101 void board_init(void)
102 {
103  /*
104  * Stop original processes using pstop/ptart commands
105  * Don't kill to avoid automatic restart
106  *
107  */
108  int ret __attribute__((unused));
109  ret = system("pstop delosd");
110  ret = system("pstop dragon-prog");
111  usleep(50000); /* Give 50ms time to end on a busy system */
112 
113  /* Start bat reading thread */
114  pthread_t bat_thread;
115  if (pthread_create(&bat_thread, NULL, bat_read, NULL) != 0) {
116  printf("[swing_board] Could not create battery reading thread!\n");
117  }
118  pthread_setname_np(bat_thread, "pprz_bat_thread");
119 
120  /* Start button reading thread */
121  pthread_t button_thread;
122  if (pthread_create(&button_thread, NULL, button_read, NULL) != 0) {
123  printf("[swing_board] Could not create button reading thread!\n");
124  }
125  pthread_setname_np(button_thread, "pprz_button_thread");
126 
127 }
128 
129 void board_init2(void)
130 {
131 }
132 
static void * bat_read(void *data)
Battery reading thread.
Definition: board.c:42
#define TRUE
Definition: std.h:4
void board_init2(void)
Optional board init function called at the end of mcu_init().
Definition: board.c:129
Interface for electrical status: supply voltage, current, battery status, etc.
static void * button_read(void *data)
Check button thread.
Definition: board.c:75
void board_init(void)
Optional board init function called at the start of mcu_init().
Definition: board.c:130
Arch independent mcu ( Micro Controller Unit ) utilities.
uint16_t vsupply
supply voltage in decivolts
Definition: electrical.h:48
struct Electrical electrical
Definition: electrical.c:65