Paparazzi UAS  v5.12_stable-4-g9b43e9b
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  }
53  else {
54  /* Read the output a line at a time - output it. */
55  while (fgets(path, sizeof(path)-1, fp) != NULL) {
56  int raw_bat = atoi(path);
57  // convert to decivolt
58  // from /bin/mcu_vbat.sh: MILLIVOLTS_VALUE=$(( ($RAW_VALUE * 4250) / 1023 ))
59  electrical.vsupply = ((raw_bat * 4250) / 1023) / 100;
60  }
61  /* close */
62  pclose(fp);
63  }
64 
65  // Wait 100ms
66  // reading is done at 10Hz like the electrical_periodic from rotorcraft main program
67  usleep(100000);
68  }
69 
70  return NULL;
71 }
72 
76 static void *button_read(void *data __attribute__((unused)))
77 {
78  struct input_event ev;
79  ssize_t n;
80 
81  /* Open power button event sysfs file */
82  int fd_button = open("/dev/input/pm_mcu_event", O_RDONLY);
83  if (fd_button == -1) {
84  printf("Unable to open mcu_event to read power button state\n");
85  return NULL;
86  }
87 
88  while (TRUE) {
89  /* Check power button (read is blocking) */
90  n = read(fd_button, &ev, sizeof(ev));
91  if (n == sizeof(ev) && ev.type == EV_KEY && ev.code == KEY_POWER && ev.value > 0) {
92  printf("Stopping Paparazzi from power button and rebooting\n");
93  usleep(1000);
94  int ret __attribute__((unused)) = system("reboot.sh");
95  exit(0);
96  }
97  }
98 
99  return NULL;
100 }
101 
102 void board_init(void)
103 {
104  /*
105  * Stop original processes using pstop/ptart commands
106  * Don't kill to avoid automatic restart
107  *
108  */
109  int ret __attribute__((unused));
110  ret = system("pstop delosd");
111  ret = system("pstop dragon-prog");
112  usleep(50000); /* Give 50ms time to end on a busy system */
113 
114  /* Start bat reading thread */
115  pthread_t bat_thread;
116  if (pthread_create(&bat_thread, NULL, bat_read, NULL) != 0) {
117  printf("[swing_board] Could not create battery reading thread!\n");
118  }
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 
126 }
127 
128 void board_init2(void)
129 {
130 }
131 
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:126
Interface for electrical status: supply voltage, current, battery status, etc.
static void * button_read(void *data)
Check button thread.
Definition: board.c:76
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