Statistics
| Branch: | Tag: | Revision:

mongoose / examples / ESP8266_RTOS / user / esp_libc.c @ eaef5bd1

History | View | Annotate | Download (1.58 KB)

1
/*
2
* Copyright (c) 2015 Cesanta Software Limited
3
* All rights reserved
4
*/
5

    
6
#include <ctype.h>
7
#include <sys/time.h>
8
#include <stdint.h>
9

    
10
#include <math.h>
11
#include <stdarg.h>
12
#include <stdlib.h>
13
#include <stdio.h>
14
#include <string.h>
15

    
16
#include "esp_common.h"
17

    
18
/* Makes fprintf(stdout) and stderr work. */
19
_ssize_t _write_r(struct _reent *r, int fd, void *buf, size_t len) {
20
  if (fd == 1 || fd == 2) {
21
    size_t i;
22
    for (i = 0; i < len; i++) {
23
      os_putc(((char *) buf)[i]);
24
    }
25
    return len;
26
  }
27
  return -1;
28
}
29

    
30
/*
31
 * You'll need to implement _open_r and friends if you want file operations. See
32
 * https://github.com/cesanta/mongoose-os/blob/master/fw/platforms/esp8266/user/esp_fs.c
33
 * for SPIFFS-based implementation.
34
 */
35

    
36
void abort(void) __attribute__((no_instrument_function));
37
void abort(void) {
38
  /* cause an unaligned access exception, that will drop you into gdb */
39
  *(int *) 1 = 1;
40
  while (1)
41
    ; /* avoid gcc warning because stdlib abort() has noreturn attribute */
42
}
43

    
44
void _exit(int status) {
45
  abort();
46
}
47

    
48
/*
49
 * This will prevent counter wrap if time is read regularly.
50
 * At least Mongoose poll queries time, so we're covered.
51
 */
52
int _gettimeofday_r(struct _reent *r, struct timeval *tp, void *tzp) {
53
  static uint32_t prev_time = 0;
54
  static uint32_t num_overflows = 0;
55
  uint32_t time = system_get_time();
56
  uint64_t time64 = time;
57
  if (prev_time > 0 && time < prev_time) num_overflows++;
58
  time64 += (((uint64_t) num_overflows) * (1ULL << 32));
59
  tp->tv_sec = time64 / 1000000ULL;
60
  tp->tv_usec = time64 % 1000000ULL;
61
  prev_time = time;
62
  return 0;
63
  (void) r;
64
  (void) tzp;
65
}