-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
board.c
133 lines (117 loc) · 3.43 KB
/
board.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* Copyright (C) 2017 The Paparazzi Team
*
* This file is part of paparazzi.
*
* paparazzi is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* paparazzi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with paparazzi; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*/
/**
* @file boards/swing/board.c
*
* Swing specific board initialization function.
*
*/
#include "boards/swing.h"
#include "mcu.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <linux/input.h>
#include "subsystems/electrical.h"
/**
* Battery reading thread
*/
static void *bat_read(void *data __attribute__((unused)))
{
FILE *fp;
char path[16];
while (TRUE) {
/* Open the command for reading. */
fp = popen("cat /sys/devices/platform/p6-spi.2/spi2.0/vbat", "r");
if (fp == NULL) {
printf("Failed to read battery\n" );
}
else {
/* Read the output a line at a time - output it. */
while (fgets(path, sizeof(path)-1, fp) != NULL) {
int raw_bat = atoi(path);
// convert to decivolt
// from /bin/mcu_vbat.sh: MILLIVOLTS_VALUE=$(( ($RAW_VALUE * 4250) / 1023 ))
electrical.vsupply = ((raw_bat * 4250) / 1023) / 100;
}
/* close */
pclose(fp);
}
// Wait 100ms
// reading is done at 10Hz like the electrical_periodic from rotorcraft main program
usleep(100000);
}
return NULL;
}
/**
* Check button thread
*/
static void *button_read(void *data __attribute__((unused)))
{
struct input_event ev;
ssize_t n;
/* Open power button event sysfs file */
int fd_button = open("/dev/input/pm_mcu_event", O_RDONLY);
if (fd_button == -1) {
printf("Unable to open mcu_event to read power button state\n");
return NULL;
}
while (TRUE) {
/* Check power button (read is blocking) */
n = read(fd_button, &ev, sizeof(ev));
if (n == sizeof(ev) && ev.type == EV_KEY && ev.code == KEY_POWER && ev.value > 0) {
printf("Stopping Paparazzi from power button and rebooting\n");
usleep(1000);
int ret __attribute__((unused)) = system("reboot.sh");
exit(0);
}
}
return NULL;
}
void board_init(void)
{
/*
* Stop original processes using pstop/ptart commands
* Don't kill to avoid automatic restart
*
*/
int ret __attribute__((unused));
ret = system("pstop delosd");
ret = system("pstop dragon-prog");
usleep(50000); /* Give 50ms time to end on a busy system */
/* Start bat reading thread */
pthread_t bat_thread;
if (pthread_create(&bat_thread, NULL, bat_read, NULL) != 0) {
printf("[swing_board] Could not create battery reading thread!\n");
}
pthread_setname_np(bat_thread, "pprz_bat_thread");
/* Start button reading thread */
pthread_t button_thread;
if (pthread_create(&button_thread, NULL, button_read, NULL) != 0) {
printf("[swing_board] Could not create button reading thread!\n");
}
pthread_setname_np(button_thread, "pprz_button_thread");
}
void board_init2(void)
{
}