Statistics
| Branch: | Tag: | Revision:

amiro-os / devices / DiWheelDrive / main.cpp @ 47364c68

History | View | Annotate | Download (32.952 KB)

<
1
#define BL_CALLBACK_TABLE_ADDR  (0x08000000 + 0x01C0)
2
#define BL_MAGIC_NUMBER         ((uint32_t)0xFF669900u)
3

    
4
#define SHUTDOWN_NONE             0
5
#define SHUTDOWN_TRANSPORTATION   1
6
#define SHUTDOWN_DEEPSLEEP        2
7
#define SHUTDOWN_HIBERNATE        3
8
#define SHUTDOWN_RESTART          4
9
#define SHUTDOWN_HANDLE_REQUEST   5
10

    
11
#include <ch.hpp>
12

    
13
#include <amiro/util/util.h>
14
#include <global.hpp>
15
#include <exti.hpp>
16

    
17
#include <chprintf.h>
18
#include <shell.h>
19

    
20
using namespace chibios_rt;
21

    
22
Global global;
23

    
24
struct blVersion_t {
25
  const uint8_t identifier;
26
  const uint8_t major;
27
  const uint8_t minor;
28
  const uint8_t patch;
29
} __attribute__((packed));
30

    
31
void systemShutdown() {
32
  types::kinematic k;
33
  uint8_t i;
34

    
35
//  // make sure we assert SYS_PD_N to delay shutdown until we're done.
36
//  boardRequestShutdown();
37

    
38
    // stop the user thread
39
  global.userThread.requestTerminate();
40
  global.userThread.wait();
41

    
42
  k.x = 0x00u;
43
  k.w_z = 0x00u;
44

    
45
  // stop wheels
46
  global.robot.setTargetSpeed(k);
47
  global.robot.terminate();
48

    
49
  for (i = 0x00; i < global.vcnl4020.size(); i++) {
50
    global.vcnl4020[i].requestTerminate();
51
    global.vcnl4020[i].wait();
52
  }
53

    
54
  global.ina219.requestTerminate();
55
  global.ina219.wait();
56
  global.hmc5883l.requestTerminate();
57
  global.hmc5883l.wait();
58
  global.l3g4200d.requestTerminate();
59
  global.l3g4200d.wait();
60

    
61
  global.motorcontrol.requestTerminate();
62
  global.motorcontrol.wait();
63
  global.odometry.requestTerminate();
64
  global.odometry.wait();
65

    
66
  // stop I²C
67
  for (i = 0; i < global.V_I2C2.size(); ++i)
68
    global.V_I2C2[i].stop();
69
  global.HW_I2C2.stop();
70

    
71
  global.lis331dlh.requestTerminate();
72
  global.lis331dlh.wait();
73

    
74
  global.lis331dlh.configure(&global.accel_sleep_config);
75
//  global.lis331dlh.start(NORMALPRIO +4);
76

    
77
//  boardWriteIoPower(0);
78
//  boardStandby();
79

    
80
  return;
81
}
82

    
83

    
84
//void (*shellcmd_t)(BaseSequentialStream *chp, int argc, char *argv[]);
85

    
86
void shellRequestShutdown(BaseSequentialStream *chp, int argc, char *argv[]) {
87

    
88
  chprintf(chp, "shellRequestShutdown\n");
89

    
90
  /* if nor argument was given, print some help text */
91
  if (argc == 0 || strcmp(argv[0], "help") == 0) {
92
    chprintf(chp, "\tUSAGE:\n");
93
    chprintf(chp, "> shutdown <type>\n");
94
    chprintf(chp, "\n");
95
    chprintf(chp, "\ttype\n");
96
    chprintf(chp, "The type of shutdown to perform.\n");
97
    chprintf(chp, "Choose one of the following types:\n");
98
    chprintf(chp, "  transportation - Ultra low-power mode with all wakeups disabled.\n");
99
    chprintf(chp, "                   The robot can not be charged.\n");
100
    chprintf(chp, "  deepsleep      - Ultra low-power mode with several wakeups enabled.\n");
101
    chprintf(chp, "                   The robot can only be charged via the power plug.\n");
102
    chprintf(chp, "  hibernate      - Medium low-power mode, but with full charging capabilities.\n");
103
    chprintf(chp, "  restart        - Performs a system restart.\n");
104
    chprintf(chp, "Alternatively, you can use the shortcuts 't', 'd', 'h', and 'r' respectively.");
105
    chprintf(chp, "\n");
106
    return;
107
  }
108

    
109
  if (strcmp(argv[0],"transportation") == 0 || strcmp(argv[0],"t") == 0) {
110
    shutdown_now = SHUTDOWN_TRANSPORTATION;
111
    chprintf(chp, "shutdown to transportation mode initialized\n");
112
  } else if (strcmp(argv[0],"deepsleep") == 0 || strcmp(argv[0],"d") == 0) {
113
    shutdown_now = SHUTDOWN_DEEPSLEEP;
114
    chprintf(chp, "shutdown to deepsleep mode initialized\n");
115
  } else if (strcmp(argv[0],"hibernate") == 0 || strcmp(argv[0],"h") == 0) {
116
    shutdown_now = SHUTDOWN_HIBERNATE;
117
    chprintf(chp, "shutdown to hibernate mode initialized\n");
118
  } else if (strcmp(argv[0],"restart") == 0 || strcmp(argv[0],"r") == 0) {
119
    chprintf(chp, "restart initialized\n");
120
    shutdown_now = SHUTDOWN_RESTART;
121
  } else {
122
    chprintf(chp, "ERROR: unknown argument!\n");
123
    shutdown_now = SHUTDOWN_NONE;
124
  }
125

    
126
  return;
127
}
128

    
129
void shellRequestWakeup(BaseSequentialStream *chp, int argc, char *argv[]) {
130
  int i;
131
  chprintf(chp, "shellRequestWakeup\n");
132

    
133
  for (i = 0x00u; i < argc; i++)
134
    chprintf(chp, "%s\n", argv[i]);
135

    
136
  boardWakeup();
137
}
138

    
139
void shellRequestGetMemoryData(BaseSequentialStream *chp, int argc, char *argv[]) {
140
  enum Type {HEX, U8, U16, U32, S8, S16, S32};
141

    
142
  chprintf(chp, "shellRequestReadData\n");
143

    
144
  if (argc < 2 || strcmp(argv[0],"help") == 0)
145
  {
146
    chprintf(chp, "Usage: %s\n","get_memory_data <type> <start> [<count>]");
147
    chprintf(chp, "\n");
148
    chprintf(chp, "\ttype\n");
149
    chprintf(chp, "The data type as which to interpret the data.\n");
150
    chprintf(chp, "Choose one of the following types:\n");
151
    chprintf(chp, "  hex - one byte as hexadecimal value\n");
152
    chprintf(chp, "  u8  - unsigned integer (8 bit)\n");
153
    chprintf(chp, "  u16 - unsigned integer (16 bit)\n");
154
    chprintf(chp, "  u32 - unsigned integer (32 bit)\n");
155
    chprintf(chp, "  s8  - signed integer (8 bit)\n");
156
    chprintf(chp, "  s16 - signed integer (16 bit)\n");
157
    chprintf(chp, "  s32 - signed integer (32 bit)\n");
158
    chprintf(chp, "\tstart\n");
159
    chprintf(chp, "The first byte to read from the memory.\n");
160
    chprintf(chp, "\tcount [default = 1]\n");
161
    chprintf(chp, "The number of elements to read.\n");
162
    chprintf(chp, "\n");
163
    chprintf(chp, "\tNOTE\n");
164
    chprintf(chp, "Type conversions of this function might fail.\n");
165
    chprintf(chp, "If so, use type=hex and convert by hand.\n");
166
    chprintf(chp, "\n");
167
    return;
168
  }
169

    
170
  uint8_t type_size = 0;
171
  Type type = HEX;
172
  if (strcmp(argv[0],"hex") == 0) {
173
    type_size = sizeof(unsigned char);
174
    type = HEX;
175
  } else if(strcmp(argv[0],"u8") == 0) {
176
    type_size = sizeof(uint8_t);
177
    type = U8;
178
  } else if(strcmp(argv[0],"u16") == 0) {
179
    type_size = sizeof(uint16_t);
180
    type = U16;
181
  } else if(strcmp(argv[0],"u32") == 0) {
182
    type_size = sizeof(uint32_t);
183
    type = U32;
184
  } else if(strcmp(argv[0],"s8") == 0) {
185
    type_size = sizeof(int8_t);
186
    type = S8;
187
  } else if(strcmp(argv[0],"s16") == 0) {
188
    type_size = sizeof(int16_t);
189
    type = S16;
190
  } else if(strcmp(argv[0],"s32") == 0) {
191
    type_size = sizeof(int32_t);
192
    type = S32;
193
  } else {
194
    chprintf(chp, "First argument invalid. Use 'get_memory_data help' for help.\n");
195
    return;
196
  }
197

    
198
  unsigned int start_byte = atoi(argv[1]);
199

    
200
  unsigned int num_elements = 1;
201
  if (argc >= 3)
202
    num_elements = atoi(argv[2]);
203

    
204
  const size_t eeprom_size = EEPROM::getsize(&global.at24c01);
205
  uint8_t buffer[eeprom_size];
206
  if (start_byte + (type_size * num_elements) > eeprom_size) {
207
    num_elements = (eeprom_size - start_byte) / type_size;
208
    chprintf(chp, "Warning: request exceeds eeprom size -> limiting to %u values.\n", num_elements);
209
  }
210

    
211
  chFileStreamSeek((BaseFileStream*)&global.at24c01, start_byte);
212

    
213
  // Work around, because stm32f1 cannot read a single byte
214
  if (type_size*num_elements < 2)
215
    type_size = 2;
216

    
217
  uint32_t bytes_read = chSequentialStreamRead((BaseFileStream*)&global.at24c01, buffer, type_size*num_elements);
218

    
219
  if (bytes_read != type_size*num_elements)
220
    chprintf(chp, "Warning: %u of %u requested bytes were read.\n", bytes_read, type_size*num_elements);
221

    
222
  for (unsigned int i = 0; i < num_elements; ++i) {
223
    switch (type) {
224
      case HEX:
225
        chprintf(chp, "%02X ", buffer[i]);
226
        break;
227
      case U8:
228
        chprintf(chp, "%03u ", ((uint8_t*)buffer)[i]);
229
        break;
230
      case U16:
231
        chprintf(chp, "%05u ", ((uint16_t*)buffer)[i]);
232
        break;
233
      case U32:
234
        chprintf(chp, "%010u ", ((uint32_t*)buffer)[i]);
235
        break;
236
      case S8:
237
        chprintf(chp, "%+03d ", ((int8_t*)buffer)[i]);
238
        break;
239
      case S16:
240
        chprintf(chp, "%+05d ", ((int16_t*)buffer)[i]);
241
        break;
242
      case S32:
243
        chprintf(chp, "%+010d ", ((int32_t*)buffer)[i]);
244
        break;
245
      default:
246
        break;
247
    }
248
  }
249
  chprintf(chp, "\n");
250

    
251
  return;
252
}
253

    
254
void shellRequestSetLights(BaseSequentialStream *chp, int argc, char *argv[]) {
255

    
256
  if (argc < 2 || argc == 3 ||strcmp(argv[0],"help") == 0) {
257
    chprintf(chp, "\tUSAGE:\n");
258
    chprintf(chp, "> set_lights <led mask> <white/red> [<green> <blue>]\n");
259
    chprintf(chp, "\n");
260
    chprintf(chp, "\tled mask\n");
261
    chprintf(chp, "The LEDs to be set.\n");
262
    chprintf(chp, "You can set multiple LEDs at once by adding the following values:\n");
263
    chprintf(chp, "  0x01 - rear left LED (SSW)\n");
264
    chprintf(chp, "  0x02 - left rear LED (WSW)\n");
265
    chprintf(chp, "  0x04 - left front LED (WNW)\n");
266
    chprintf(chp, "  0x08 - front left LED (NNW)\n");
267
    chprintf(chp, "  0x10 - front right LED (NNE)\n");
268
    chprintf(chp, "  0x20 - right front LED (ENE)\n");
269
    chprintf(chp, "  0x40 - right rear LED (ESE)\n");
270
    chprintf(chp, "  0x80 - rear right LED (SSE)\n");
271
    chprintf(chp, "\twhite/red\n");
272
    chprintf(chp, "If no optional argument is given, this arguments sets the white value of the selected LEDs.\n");
273
    chprintf(chp, "Otherwise this arguments sets the red color channel value.\n");
274
    chprintf(chp, "\tgreen\n");
275
    chprintf(chp, "Sets the green color channel value.\n");
276
    chprintf(chp, "\tblue\n");
277
    chprintf(chp, "Sets the blue color channel value.\n");
278
    chprintf(chp, "\n");
279
    chprintf(chp, "\tExample:\n");
280
    chprintf(chp, "This line will set the two most left and two most right LEDs to bright cyan.\n");
281
    chprintf(chp, "> set_lights 0x66 0 255 255\n");
282
    chprintf(chp, "\n");
283
    return;
284
  }
285

    
286
  int arg_mask = strtol(argv[0], NULL, 0);
287
  int red = strtol(argv[1], NULL, 0);
288
  int green = red;
289
  int blue = red;
290
  if (argc >= 4) {
291
    green = strtol(argv[2], NULL, 0);
292
    blue = strtol(argv[3], NULL, 0);
293
  }
294
  Color color(red, green, blue);
295

    
296
  if (arg_mask & 0x01) {
297
    global.robot.setLightColor(constants::LightRing::LED_SSW, color);
298
  }
299
  if (arg_mask & 0x02) {
300
    global.robot.setLightColor(constants::LightRing::LED_WSW, color);
301
  }