You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

139 lines
3.8 KiB

/*
* This file is part of the MicroPython project, http://micropython.org/
*
* Development of the code in this file was sponsored by Microbric Pty Ltd
*
* The MIT License (MIT)
*
* Copyright (c) 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdio.h>
#include <sys/time.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "rom/uart.h"
#include "py/obj.h"
#include "py/mpstate.h"
#include "py/mphal.h"
#include "extmod/misc.h"
#include "lib/utils/pyexec.h"
STATIC uint8_t stdin_ringbuf_array[256];
ringbuf_t stdin_ringbuf = {stdin_ringbuf_array, sizeof(stdin_ringbuf_array)};
int mp_hal_stdin_rx_chr(void) {
for (;;) {
int c = ringbuf_get(&stdin_ringbuf);
if (c != -1) {
return c;
}
MICROPY_EVENT_POLL_HOOK
vTaskDelay(1);
}
}
void mp_hal_stdout_tx_char(char c) {
uart_tx_one_char(c);
//mp_uos_dupterm_tx_strn(&c, 1);
}
void mp_hal_stdout_tx_str(const char *str) {
MP_THREAD_GIL_EXIT();
while (*str) {
mp_hal_stdout_tx_char(*str++);
}
MP_THREAD_GIL_ENTER();
}
void mp_hal_stdout_tx_strn(const char *str, uint32_t len) {
MP_THREAD_GIL_EXIT();
while (len--) {
mp_hal_stdout_tx_char(*str++);
}
MP_THREAD_GIL_ENTER();
}
void mp_hal_stdout_tx_strn_cooked(const char *str, uint32_t len) {
MP_THREAD_GIL_EXIT();
while (len--) {
if (*str == '\n') {
mp_hal_stdout_tx_char('\r');
}
mp_hal_stdout_tx_char(*str++);
}
MP_THREAD_GIL_ENTER();
}
uint32_t mp_hal_ticks_ms(void) {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
uint32_t mp_hal_ticks_us(void) {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000000 + tv.tv_usec;
}
void mp_hal_delay_ms(uint32_t ms) {
struct timeval tv_start;
struct timeval tv_end;
uint64_t dt;
gettimeofday(&tv_start, NULL);
for (;;) {
gettimeofday(&tv_end, NULL);
dt = (tv_end.tv_sec - tv_start.tv_sec) * 1000 + (tv_end.tv_usec - tv_start.tv_usec) / 1000;
if (dt + portTICK_PERIOD_MS >= ms) {
// doing a vTaskDelay would take us beyound requested delay time
break;
}
MICROPY_EVENT_POLL_HOOK
vTaskDelay(1);
}
if (dt < ms) {
// do the remaining delay accurately
ets_delay_us((ms - dt) * 1000);
}
}
void mp_hal_delay_us(uint32_t us) {
ets_delay_us(us);
}
// this function could do with improvements (eg use ets_delay_us)
void mp_hal_delay_us_fast(uint32_t us) {
uint32_t delay = ets_get_cpu_frequency() / 19;
while (--us) {
for (volatile uint32_t i = delay; i; --i) {
}
}
}
/*
extern int mp_stream_errno;
int *__errno() {
return &mp_stream_errno;
}
*/