Statistics
| Branch: | Tag: | Revision:

amiro-os / devices / DiWheelDrive / main.cpp @ b8b3a9c9

History | View | Annotate | Download (46.398 KB)

1 b8b3a9c9 galberding
#include <cstdint>
2 58fe0e0b Thomas Schöpping
#define BL_CALLBACK_TABLE_ADDR  (0x08000000 + 0x01C0)
3
#define BL_MAGIC_NUMBER         ((uint32_t)0xFF669900u)
4
5
#define SHUTDOWN_NONE             0
6
#define SHUTDOWN_TRANSPORTATION   1
7
#define SHUTDOWN_DEEPSLEEP        2
8
#define SHUTDOWN_HIBERNATE        3
9
#define SHUTDOWN_RESTART          4
10
#define SHUTDOWN_HANDLE_REQUEST   5
11
12
#include <ch.hpp>
13
14
#include <amiro/util/util.h>
15
#include <global.hpp>
16
#include <exti.hpp>
17
18
#include <chprintf.h>
19
#include <shell.h>
20
21 b8b3a9c9 galberding
#include "linefollow.hpp"
22
#include "amiro_map.hpp"
23 58fe0e0b Thomas Schöpping
using namespace chibios_rt;
24
25
Global global;
26
27 10687985 Thomas Schöpping
struct blVersion_t {
28
  const uint8_t identifier;
29
  const uint8_t major;
30
  const uint8_t minor;
31
  const uint8_t patch;
32
} __attribute__((packed));
33
34 58fe0e0b Thomas Schöpping
void systemShutdown() {
35
  types::kinematic k;
36
  uint8_t i;
37
38
//  // make sure we assert SYS_PD_N to delay shutdown until we're done.
39
//  boardRequestShutdown();
40
41
    // stop the user thread
42
  global.userThread.requestTerminate();
43
  global.userThread.wait();
44
45
  k.x = 0x00u;
46
  k.w_z = 0x00u;
47
48
  // stop wheels
49
  global.robot.setTargetSpeed(k);
50
  global.robot.terminate();
51
52
  for (i = 0x00; i < global.vcnl4020.size(); i++) {
53
    global.vcnl4020[i].requestTerminate();
54
    global.vcnl4020[i].wait();
55
  }
56
57
  global.ina219.requestTerminate();
58
  global.ina219.wait();
59
  global.hmc5883l.requestTerminate();
60
  global.hmc5883l.wait();
61
  global.l3g4200d.requestTerminate();
62
  global.l3g4200d.wait();
63
64
  global.motorcontrol.requestTerminate();
65
  global.motorcontrol.wait();
66
  global.odometry.requestTerminate();
67
  global.odometry.wait();
68
69
  // stop I²C
70
  for (i = 0; i < global.V_I2C2.size(); ++i)
71
    global.V_I2C2[i].stop();
72
  global.HW_I2C2.stop();
73
74
  global.lis331dlh.requestTerminate();
75
  global.lis331dlh.wait();
76
77
  global.lis331dlh.configure(&global.accel_sleep_config);
78
//  global.lis331dlh.start(NORMALPRIO +4);
79
80
//  boardWriteIoPower(0);
81
//  boardStandby();
82
83
  return;
84
}
85
86
87
//void (*shellcmd_t)(BaseSequentialStream *chp, int argc, char *argv[]);
88
89
void shellRequestShutdown(BaseSequentialStream *chp, int argc, char *argv[]) {
90
91
  chprintf(chp, "shellRequestShutdown\n");
92
93
  /* if nor argument was given, print some help text */
94
  if (argc == 0 || strcmp(argv[0], "help") == 0) {
95
    chprintf(chp, "\tUSAGE:\n");
96
    chprintf(chp, "> shutdown <type>\n");
97
    chprintf(chp, "\n");
98
    chprintf(chp, "\ttype\n");
99
    chprintf(chp, "The type of shutdown to perform.\n");
100
    chprintf(chp, "Choose one of the following types:\n");
101
    chprintf(chp, "  transportation - Ultra low-power mode with all wakeups disabled.\n");
102
    chprintf(chp, "                   The robot can not be charged.\n");
103
    chprintf(chp, "  deepsleep      - Ultra low-power mode with several wakeups enabled.\n");
104
    chprintf(chp, "                   The robot can only be charged via the power plug.\n");
105
    chprintf(chp, "  hibernate      - Medium low-power mode, but with full charging capabilities.\n");
106
    chprintf(chp, "  restart        - Performs a system restart.\n");
107
    chprintf(chp, "Alternatively, you can use the shortcuts 't', 'd', 'h', and 'r' respectively.");
108
    chprintf(chp, "\n");
109
    return;
110
  }
111
112
  if (strcmp(argv[0],"transportation") == 0 || strcmp(argv[0],"t") == 0) {
113
    shutdown_now = SHUTDOWN_TRANSPORTATION;
114
    chprintf(chp, "shutdown to transportation mode initialized\n");
115
  } else if (strcmp(argv[0],"deepsleep") == 0 || strcmp(argv[0],"d") == 0) {
116
    shutdown_now = SHUTDOWN_DEEPSLEEP;
117
    chprintf(chp, "shutdown to deepsleep mode initialized\n");
118
  } else if (strcmp(argv[0],"hibernate") == 0 || strcmp(argv[0],"h") == 0) {
119
    shutdown_now = SHUTDOWN_HIBERNATE;
120
    chprintf(chp, "shutdown to hibernate mode initialized\n");
121
  } else if (strcmp(argv[0],"restart") == 0 || strcmp(argv[0],"r") == 0) {
122
    chprintf(chp, "restart initialized\n");
123
    shutdown_now = SHUTDOWN_RESTART;
124
  } else {
125
    chprintf(chp, "ERROR: unknown argument!\n");
126
    shutdown_now = SHUTDOWN_NONE;
127
  }
128
129
  return;
130
}
131
132
void shellRequestWakeup(BaseSequentialStream *chp, int argc, char *argv[]) {
133
  int i;
134
  chprintf(chp, "shellRequestWakeup\n");
135
136
  for (i = 0x00u; i < argc; i++)
137
    chprintf(chp, "%s\n", argv[i]);
138
139
  boardWakeup();
140
}
141
142
void shellRequestGetMemoryData(BaseSequentialStream *chp, int argc, char *argv[]) {
143
  enum Type {HEX, U8, U16, U32, S8, S16, S32};
144
145
  chprintf(chp, "shellRequestReadData\n");
146
147
  if (argc < 2 || strcmp(argv[0],"help") == 0)
148
  {
149
    chprintf(chp, "Usage: %s\n","get_memory_data <type> <start> [<count>]");
150
    chprintf(chp, "\n");
151
    chprintf(chp, "\ttype\n");
152
    chprintf(chp, "The data type as which to interpret the data.\n");
153
    chprintf(chp, "Choose one of the following types:\n");
154
    chprintf(chp, "  hex - one byte as hexadecimal value\n");
155
    chprintf(chp, "  u8  - unsigned integer (8 bit)\n");
156
    chprintf(chp, "  u16 - unsigned integer (16 bit)\n");
157
    chprintf(chp, "  u32 - unsigned integer (32 bit)\n");
158
    chprintf(chp, "  s8  - signed integer (8 bit)\n");
159
    chprintf(chp, "  s16 - signed integer (16 bit)\n");
160
    chprintf(chp, "  s32 - signed integer (32 bit)\n");
161
    chprintf(chp, "\tstart\n");
162
    chprintf(chp, "The first byte to read from the memory.\n");
163
    chprintf(chp, "\tcount [default = 1]\n");
164
    chprintf(chp, "The number of elements to read.\n");
165
    chprintf(chp, "\n");
166
    chprintf(chp, "\tNOTE\n");
167
    chprintf(chp, "Type conversions of this function might fail.\n");
168
    chprintf(chp, "If so, use type=hex and convert by hand.\n");
169
    chprintf(chp, "\n");
170
    return;
171
  }
172
173
  uint8_t type_size = 0;
174
  Type type = HEX;
175
  if (strcmp(argv[0],"hex") == 0) {
176
    type_size = sizeof(unsigned char);
177
    type = HEX;
178
  } else if(strcmp(argv[0],"u8") == 0) {
179
    type_size = sizeof(uint8_t);
180
    type = U8;
181
  } else if(strcmp(argv[0],"u16") == 0) {
182
    type_size = sizeof(uint16_t);
183
    type = U16;
184
  } else if(strcmp(argv[0],"u32") == 0) {
185
    type_size = sizeof(uint32_t);
186
    type = U32;
187
  } else if(strcmp(argv[0],"s8") == 0) {
188
    type_size = sizeof(int8_t);
189
    type = S8;
190
  } else if(strcmp(argv[0],"s16") == 0) {
191
    type_size = sizeof(int16_t);
192
    type = S16;
193
  } else if(strcmp(argv[0],"s32") == 0) {
194
    type_size = sizeof(int32_t);
195
    type = S32;
196
  } else {
197
    chprintf(chp, "First argument invalid. Use 'get_memory_data help' for help.\n");
198
    return;
199
  }
200
201
  unsigned int start_byte = atoi(argv[1]);
202
203
  unsigned int num_elements = 1;
204
  if (argc >= 3)
205
    num_elements = atoi(argv[2]);
206
207
  const size_t eeprom_size = EEPROM::getsize(&global.at24c01);
208
  uint8_t buffer[eeprom_size];
209
  if (start_byte + (type_size * num_elements) > eeprom_size) {
210
    num_elements = (eeprom_size - start_byte) / type_size;
211
    chprintf(chp, "Warning: request exceeds eeprom size -> limiting to %u values.\n", num_elements);
212
  }
213
214
  chFileStreamSeek((BaseFileStream*)&global.at24c01, start_byte);
215
216
  // Work around, because stm32f1 cannot read a single byte
217
  if (type_size*num_elements < 2)
218
    type_size = 2;
219
220
  uint32_t bytes_read = chSequentialStreamRead((BaseFileStream*)&global.at24c01, buffer, type_size*num_elements);
221
222