Paparazzi UAS  v5.10_stable-5-g83a0da5-dirty
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
sdlogger_spi_direct.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) Bart Slinger
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  */
30 #define PERIODIC_C_LOGGER
31 
35 #include "led.h"
36 
37 #if SDLOGGER_ON_ARM
38 #include "autopilot.h"
39 #endif
40 
41 #ifdef LOGGER_LED
42 #define LOGGER_LED_ON LED_ON(LOGGER_LED);
43 #define LOGGER_LED_OFF LED_OFF(LOGGER_LED);
44 #else
45 #define LOGGER_LED_ON {}
46 #define LOGGER_LED_OFF {}
47 #endif
48 
49 #ifndef TELEMETRY_MODE_Main_empty
50 #warning You need to define a main telemetry mode named "empty" without any \
51  messages in your config file in /conf/telemetry/<your_config.xml>. \
52  \
53  Add <mode name="empty"></mode> to your main telemetry process.
54 #endif
55 
56 #ifndef TELEMETRY_PROCESS_Logger
57 #error "You need to use a telemetry xml file with Logger process!"
58 #endif
59 
60 #ifndef DOWNLINK_DEVICE
61 #warning This module can only be used with uart downlink for now.
62 #endif
63 
65 
71 {
72  /* Initialize the SD Card */
73  sdcard_spi_init(&sdcard1, &(SDLOGGER_SPI_LINK_DEVICE),
74  SDLOGGER_SPI_LINK_SLAVE_NUMBER);
75 
76  /* Set values in the struct to their defaults */
81 
82  /* Fill internal buffer with zeros */
83  for (uint8_t i = 0; i < sizeof(sdlogger_spi.buffer); i++) {
84  sdlogger_spi.buffer[i] = 0;
85  }
86  sdlogger_spi.idx = 0;
92  sdlogger_spi.do_log = 0;
93 
94  /* Set function pointers in link_device to the logger functions */
95  sdlogger_spi.device.check_free_space = (check_free_space_t)sdlogger_spi_direct_check_free_space;
96  sdlogger_spi.device.put_byte = (put_byte_t)sdlogger_spi_direct_put_byte;
97  sdlogger_spi.device.put_buffer = (put_buffer_t)sdlogger_spi_direct_put_buffer;
98  sdlogger_spi.device.send_message = (send_message_t)sdlogger_spi_direct_send_message;
99  sdlogger_spi.device.char_available = (char_available_t)sdlogger_spi_direct_char_available;
100  sdlogger_spi.device.get_byte = (get_byte_t)sdlogger_spi_direct_get_byte;
101  sdlogger_spi.device.periph = &sdlogger_spi;
102 
103 }
104 
110 {
112 
113 #if SDLOGGER_ON_ARM
114  if(autopilot_motors_on) {
115  sdlogger_spi.do_log = 1;
116  } else {
117  sdlogger_spi.do_log = 0;
118  }
119 #endif
120 
121  switch (sdlogger_spi.status) {
123  if (sdcard1.status == SDCard_Idle) {
126  }
127  break;
128 
129  case SDLogger_Ready:
130  if ((sdlogger_spi.do_log == 1) &&
135  }
136  break;
137 
138  case SDLogger_Logging:
139  /* This line is NOT unit-tested because it is an inline function */
140  #if PERIODIC_TELEMETRY
141  periodic_telemetry_send_Logger(DefaultPeriodic,
142  &pprzlog_tp.trans_tx,
144  #endif
145  /* Check if SD Card buffer is full and SD Card is ready for new data */
146  if (sdlogger_spi.sdcard_buf_idx > 512 &&
149  }
150  /* Check if switch is flipped to stop logging */
151  if (sdlogger_spi.do_log == 0) {
153  }
154  break;
155 
158  if (sdlogger_spi.sdcard_buf_idx > 512) {
160  }
161  else if (sdlogger_spi.sdcard_buf_idx > 1) {
162  /* Fill with trailing zero's */
163  for (uint16_t i = sdlogger_spi.sdcard_buf_idx; i < (SD_BLOCK_SIZE+1); i++) {
164  sdcard1.output_buf[i] = 0x00;
165  }
167  }
168  else if (sdlogger_spi.sdcard_buf_idx == 1) {
171  }
172  }
173  break;
174 
176  if (sdcard1.status == SDCard_Idle) {
179  }
180  break;
181 
183  if (sdcard1.status == SDCard_Idle) {
186  }
187  break;
188 
190  if (sdcard1.status == SDCard_Idle) {
191  /* Put bytes to the buffer until all is written or buffer is full */
192  for (uint16_t i = sdlogger_spi.sdcard_buf_idx; i < SD_BLOCK_SIZE; i++) {
193  long fd = 0;
194  if (uart_check_free_space(&(DOWNLINK_DEVICE), &fd, 1)) {
196  }
197  else {
198  /* No free space left, abort for-loop */
199  break;
200  }
202  }
203  /* Request next block if entire buffer was written to uart */
205  if (sdlogger_spi.download_length > 0) {
209  }
210  else {
213  }
215  }
216  }
217  break;
218 
219  default:
220  break;
221  }
222 }
223 
226 
232 {
233 
234  switch (sdlogger_spi.status) {
238  // Save data for later use
240  (sdcard1.input_buf[1] << 16) |
241  (sdcard1.input_buf[2] << 8) |
242  (sdcard1.input_buf[3]);
244 
245  if(sdlogger_spi.next_available_address < 0x00004000) {
247  }
248 
249  /* Ready to start logging */
251  break;
252 
254  /* Copy input buffer to output buffer */
255  for (uint16_t i = 0; i < SD_BLOCK_SIZE; i++) {
257  }
258 
259  /* Increment last completed log */
261  /* Write log info at dedicated location */
262  {
263  uint16_t log_idx_start = 5 + 6 + (sdlogger_spi.last_completed - 1) * 12;
264 
265  /* Set start address and length at location that belongs to the log nr */
266  sdcard1.output_buf[log_idx_start+0] = sdlogger_spi.next_available_address >> 24;
267  sdcard1.output_buf[log_idx_start+1] = sdlogger_spi.next_available_address >> 16;
268  sdcard1.output_buf[log_idx_start+2] = sdlogger_spi.next_available_address >> 8;
269  sdcard1.output_buf[log_idx_start+3] = sdlogger_spi.next_available_address >> 0;
270  sdcard1.output_buf[log_idx_start+4] = sdlogger_spi.log_len >> 24;
271  sdcard1.output_buf[log_idx_start+5] = sdlogger_spi.log_len >> 16;
272  sdcard1.output_buf[log_idx_start+6] = sdlogger_spi.log_len >> 8;
273  sdcard1.output_buf[log_idx_start+7] = sdlogger_spi.log_len >> 0;
274  }
275 
276  /* Increment and update the next available address */
282 
283  sdcard_spi_write_block(&sdcard1, 0x00002000);
284  /* Reset log length */
285  sdlogger_spi.log_len = 0;
287  break;
288 
290  {
291  uint16_t info_idx = 5 + (sdlogger_spi.download_id - 1) * 12;
292  sdlogger_spi.download_address = (sdcard1.input_buf[info_idx+0] << 24) |
293  (sdcard1.input_buf[info_idx+1] << 16) |
294  (sdcard1.input_buf[info_idx+2] << 8) |
295  (sdcard1.input_buf[info_idx+3] << 0);
296  sdlogger_spi.download_length = (sdcard1.input_buf[info_idx+4] << 24) |
297  (sdcard1.input_buf[info_idx+5] << 16) |
298  (sdcard1.input_buf[info_idx+6] << 8) |
299  (sdcard1.input_buf[info_idx+7] << 0);
300  if (sdlogger_spi.download_length > 0) {
301  /* Request the first block */
303  /* After each read block, incr address, decr length */
307  }
308  else {
311  }
313  }
314  break;
315 
316  default:
317  break;
318  }
319 
320 }
321 
328 {
329  /* Increment log length */
331 
332  /* Copy data from logger buffer to SD Card buffer */
333  for (uint8_t i = 0; i < sdlogger_spi.idx; i++) {
335  }
336  /* Set sdcard buffer index to new value */
338  /* And reset the logger buffer index */
339  sdlogger_spi.idx = 0;
340 }
341 
343 {
345  sdlogger_spi.command < 43) {
347  sdcard_spi_read_block(&sdcard1, 0x00002000,
351  }
352  else if (sdcard1.status == SDCard_Idle && sdlogger_spi.command == 255) {
353  telemetry_mode_Main = TELEMETRY_MODE_Main_empty;
355  sdcard_spi_read_block(&sdcard1, 0x00002000, NULL);
359  }
360  /* Always reset command value back to zero */
361  sdlogger_spi.command = 0;
362 }
363 
364 bool sdlogger_spi_direct_check_free_space(struct sdlogger_spi_periph *p, long *fd __attribute__((unused)), uint16_t len)
365 {
366  if (p->status == SDLogger_Logging) {
367  /* Calculating free space in both buffers */
368  if ( (513 - p->sdcard_buf_idx) + (SDLOGGER_BUFFER_SIZE - p->idx) >= len) {
369  return true;
370  }
371  }
372  return false;
373 }
374 
375 void sdlogger_spi_direct_put_byte(struct sdlogger_spi_periph *p, long fd __attribute__((unused)), uint8_t data)
376 {
377  /* SD Buffer full, write in logger buffer */
378  if (p->sdcard_buf_idx > 512) {
379  if (p->idx < SDLOGGER_BUFFER_SIZE) {
380  p->buffer[p->idx++] = data;
381  }
382  /* else: data lost */
383  }
384  /* Writing directly to SD Card buffer */
385  else {
386  sdcard1.output_buf[p->sdcard_buf_idx++] = data;
387 
388  /* Flush buffer */
389  if (p->sdcard_buf_idx > 512 && sdcard1.status == SDCard_MultiWriteIdle) {
391  }
392  }
393 }
394 
396 {
397  int i;
398  for (i = 0; i < len; i++) {
399  sdlogger_spi_direct_put_byte(p, fd, data[i]);
400  }
401 }
402 
403 void sdlogger_spi_direct_send_message(void *p, long fd __attribute__((unused)))
404 {
405  (void) p;
406 }
407 
409  (void) p;
410  return 0;
411 }
412 
414 {
415  (void) p;
416  return 0;
417 }
418 
419 
420 
enum SDLoggerStatus status
unsigned short uint16_t
Definition: types.h:16
Initialization sequence succesful.
Definition: sdcard_spi.h:58
uint8_t buffer[SDLOGGER_BUFFER_SIZE]
void sdlogger_spi_direct_send_message(void *p, long fd)
struct link_device device
uint8_t input_buf[SD_BLOCK_SIZE+10]
The input buffer for the SPI transaction.
Definition: sdcard_spi.h:113
uint8_t sdlogger_spi_direct_get_byte(void *p)
#define DOWNLINK_DEVICE
void sdcard_spi_write_block(struct SDCard *sdcard, uint32_t addr)
Write a single block (512 bytes) to the SDCard at a given address.
Definition: sdcard_spi.c:560
Periodic telemetry system header (includes downlink utility and generated code).
void sdlogger_spi_direct_init(void)
sdlogger_spi_direct_init Initialize the logger and SD Card.
int sdlogger_spi_direct_char_available(void *p)
enum SDCardStatus status
The status of the SD card.
Definition: sdcard_spi.h:112
void sdcard_spi_init(struct SDCard *sdcard, struct spi_periph *spi_p, const uint8_t slave_idx)
Configure initial values for SDCard.
Definition: sdcard_spi.c:74
void sdcard_spi_read_block(struct SDCard *sdcard, uint32_t addr, SDCardCallback callback)
Read a single block (512 bytes) from the SDCard at a given address.
Definition: sdcard_spi.c:587
uint8_t output_buf[SD_BLOCK_SIZE+10]
The output buffer for the SPI transaction.
Definition: sdcard_spi.h:114
bool sdlogger_spi_direct_check_free_space(struct sdlogger_spi_periph *p, long *fd, uint16_t len)
bool autopilot_motors_on
Definition: autopilot.c:76
bool uart_check_free_space(struct uart_periph *p, long *fd, uint16_t len)
Definition: uart_arch.c:899
#define SDLOGGER_BUFFER_SIZE
void sdcard_spi_multiwrite_stop(struct SDCard *sdcard)
Stop with multiwrite procedure.
Definition: sdcard_spi.c:666
void sdcard_spi_multiwrite_next(struct SDCard *sdcard, SDCardCallback callback)
Write a(nother) data block (512 bytes) to the SDCard.
Definition: sdcard_spi.c:638
void sdlogger_spi_direct_stop(void)
void sdcard_spi_periodic(struct SDCard *sdcard)
Periodic function of the SDCard.
Definition: sdcard_spi.c:99
#define LOGGER_LED_OFF
struct sdlogger_spi_periph sdlogger_spi
#define SD_BLOCK_SIZE
Definition: sdcard_spi.h:38
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
void sdlogger_spi_direct_multiwrite_written(void)
sdlogger_spi_direct_multiwrite_written Called when a multiwrite is complete.
void sdcard_spi_multiwrite_start(struct SDCard *sdcard, uint32_t addr)
Start writing multiple blocks of 512 bytes to the SDCard.
Definition: sdcard_spi.c:616
void sdlogger_spi_direct_periodic(void)
sdlogger_spi_direct_periodic Periodic function called at module frequency
void uart_put_byte(struct uart_periph *p, long fd, uint8_t data)
Uart transmit implementation.
Definition: uart_arch.c:917
void sdlogger_spi_direct_start(void)
struct SDCard sdcard1
This is the definition of the SD card.
Definition: sdcard_spi.c:56
unsigned char uint8_t
Definition: types.h:14
void sdlogger_spi_direct_index_received(void)
sdlogger_spi_direct_index_received Callback from SD Card when block at index location is received...
void sdlogger_spi_direct_command(void)
int fd
Definition: serial.c:26
arch independent LED (Light Emitting Diodes) API
static float p[2][2]
#define LOGGER_LED_ON
void sdlogger_spi_direct_put_byte(struct sdlogger_spi_periph *p, long fd, uint8_t data)
void sdlogger_spi_direct_put_buffer(struct sdlogger_spi_periph *p, long fd, uint8_t *data, uint16_t len)
CMD25 complete, ready to sent blocks.
Definition: sdcard_spi.h:84