esp_libc.c 1.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
* Copyright (c) 2015 Cesanta Software Limited
* All rights reserved
*/

#include <ctype.h>
#include <sys/time.h>
#include <stdint.h>

#include <math.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "esp_common.h"

/* Makes fprintf(stdout) and stderr work. */
_ssize_t _write_r(struct _reent *r, int fd, void *buf, size_t len) {
  if (fd == 1 || fd == 2) {
    size_t i;
    for (i = 0; i < len; i++) {
      os_putc(((char *) buf)[i]);
    }
    return len;
  }
  return -1;
}

/*
 * You'll need to implement _open_r and friends if you want file operations. See
32
 * https://github.com/cesanta/mongoose-iot/blob/master/fw/platforms/esp8266/user/esp_fs.c
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 * for SPIFFS-based implementation.
 */

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

void _exit(int status) {
  abort();
}

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