APM:Libraries
system.cpp
Go to the documentation of this file.
1 /*
2 (c) 2017 night_ghost@ykoctpa.ru
3 
4 */
5 
6 #pragma GCC optimize ("O2")
7 
8 #include <stdarg.h>
9 #include <stdio.h>
10 
11 #include <AP_HAL/AP_HAL.h>
12 #include <AP_HAL/system.h>
13 
14 #include "Scheduler.h"
16 
17 extern const AP_HAL::HAL& hal;
18 
19 using namespace F4Light;
20 
21 
22 namespace AP_HAL {
23 
24 void cond_yield();
25 
26 void init() // early init on creation of HAL class
27 {
28 }
29 
30 void panic(const char *errormsg, ...)
31 {
32  va_list ap;
33 
34  if(boardEmergencyHandler) boardEmergencyHandler(); // call emergency handler before
35 
36  timer_disable_all(); // turn off all PWM
37 
39 
40  va_start(ap, errormsg);
41  hal.console->vprintf(errormsg, ap);
42  va_end(ap);
43  hal.console->printf("\n");
44 
45  if(is_bare_metal() || hal_param_helper->_boot_dfu) { // bare metal build without bootloader should reboot to DFU after any fault
47  } else {
49  }
50 
51  error_throb(0);
52 }
53 
54 void cond_yield(){
55  static uint32_t last_yield=0;
56 
57  uint32_t t=F4Light::Scheduler::_micros();
58  if(t-last_yield>300 || last_yield==t) { // if yield was long ago or mills() called too often
60  }
61 
62  last_yield = F4Light::Scheduler::_micros();
63 }
64 
65 
66 uint32_t millis(){
67  cond_yield();
69 }
70 
71 
72 uint64_t millis64(){
73  cond_yield(); // to prevent CPU eating by wait loops
75 }
76 
77 uint32_t micros() {
79 }
80 
81 uint64_t micros64(){
83 }
84 
85 // revo internals
86 
87 void delay(uint32_t ms);
88 void delay_microseconds(uint16_t us);
89 void yield(uint32_t us);
90 
91 void delay(uint32_t ms){
93 }
94 
95 void delay_microseconds(uint16_t us) {
97 }
98 
99 void yield(uint32_t us){
101 }
102 
103 
104 } // namespace AP_HAL
105 
106 
107 // export console IO to low-level functions
108 // so any printf will go to console, not to /dev/null
109 extern "C" {
110  unsigned char getch(void);
111 
112  int _read(int fd, char *buf, size_t cnt);
113 
114  void putch(unsigned char c);
115 
116  extern int printf(const char *msg, ...);
117 }
118 
119 
120 unsigned char getch(void) {
121  if(hal.console->available())
122  return hal.console->read();
123  return 0;
124 }
125 
126 int _read(int fd, char *buf, size_t cnt) {
127  uint16_t rcv=0;
128 
129  while(cnt--){
130  if(hal.console->available()){
131  *buf++ = hal.console->read();
132  rcv++;
133  } else break;
134  }
135 
136  return rcv;
137 }
138 
139 void putch(unsigned char c) {
140  hal.console->write(c);
141 }
142 
143 int printf(const char *msg, ...){
144  va_list ap;
145 
146  va_start(ap, msg);
147  hal.console->vprintf(msg, ap);
148  va_end(ap);
149  return 1;
150 }
151 
static void timer_disable_all(void)
Definition: timer.h:668
unsigned char getch(void)
Definition: system.cpp:120
AP_HAL::UARTDriver * console
Definition: HAL.h:110
int printf(const char *msg,...)
Definition: system.cpp:143
void cond_yield()
Definition: system.cpp:54
void delay(uint32_t ms)
Definition: system.cpp:91
void delay_microseconds(uint16_t us)
Definition: system.cpp:95
uint64_t millis64()
Definition: system.cpp:167
-*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
voidFuncPtr boardEmergencyHandler
void yield(uint32_t us)
Definition: system.cpp:99
virtual void printf(const char *,...) FMT_PRINTF(2
Definition: BetterStream.cpp:5
static uint32_t _millis()
Definition: Scheduler.h:229
#define BOOT_RTC_SIGNATURE
Definition: boards.h:291
static uint64_t _micros64()
Definition: Scheduler.cpp:492
static uint64_t _millis64()
Definition: Scheduler.h:230
const AP_HAL::HAL & hal
Definition: AC_PID_test.cpp:14
virtual size_t write(uint8_t)=0
uint32_t millis()
Definition: system.cpp:157
uint64_t micros64()
Definition: system.cpp:162
int _read(int fd, char *buf, size_t cnt)
Definition: system.cpp:126
virtual int16_t read()=0
static void yield(uint16_t ttw=0)
Definition: Scheduler.cpp:1188
virtual uint32_t available()=0
static uint32_t _micros()
Definition: Scheduler.h:232
static void _delay(uint16_t ms)
Definition: Scheduler.cpp:258
virtual void virtual void vprintf(const char *, va_list)
void init()
Generic board initialization function.
Definition: system.cpp:136
void putch(unsigned char c)
Definition: system.cpp:139
void board_set_rtc_register(uint32_t sig, uint16_t reg)
Definition: boards.cpp:141
#define DFU_RTC_SIGNATURE
Definition: boards.h:292
static void _stop_multitask()
Definition: Scheduler.cpp:955
void panic(const char *errormsg,...) FMT_PRINTF(1
Definition: system.cpp:140
#define RTC_SIGNATURE_REG
Definition: boards.h:312
static void _delay_microseconds(uint16_t us)
Definition: Scheduler.cpp:305
static bool is_bare_metal()
Definition: boards.h:149
uint32_t micros()
Definition: system.cpp:152