2021-01-21 00:34:08 +11:00
|
|
|
/*
|
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
|
*
|
|
|
|
|
* The MIT License (MIT)
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2020-2021 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 "py/runtime.h"
|
|
|
|
|
#include "py/stream.h"
|
|
|
|
|
#include "py/mphal.h"
|
2022-01-15 04:32:01 +02:00
|
|
|
#include "extmod/misc.h"
|
2022-03-30 23:00:46 +02:00
|
|
|
#include "shared/runtime/interrupt_char.h"
|
2023-11-14 13:21:13 +11:00
|
|
|
#include "shared/runtime/softtimer.h"
|
2021-07-09 14:19:15 +10:00
|
|
|
#include "shared/timeutils/timeutils.h"
|
2023-11-09 16:56:21 +11:00
|
|
|
#include "shared/tinyusb/mp_usbd.h"
|
2024-05-10 22:23:05 +10:00
|
|
|
#include "shared/tinyusb/mp_usbd_cdc.h"
|
2023-11-14 13:21:13 +11:00
|
|
|
#include "pendsv.h"
|
2021-01-21 00:34:08 +11:00
|
|
|
#include "tusb.h"
|
|
|
|
|
#include "uart.h"
|
2024-01-02 12:19:33 +11:00
|
|
|
#include "hardware/irq.h"
|
2022-05-05 17:26:16 +10:00
|
|
|
#include "pico/unique_id.h"
|
2024-05-23 10:57:14 +01:00
|
|
|
#include "pico/aon_timer.h"
|
2021-01-21 00:34:08 +11:00
|
|
|
|
2022-06-30 16:01:02 +10:00
|
|
|
#if MICROPY_PY_NETWORK_CYW43
|
|
|
|
|
#include "lib/cyw43-driver/src/cyw43.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-05-23 10:57:14 +01:00
|
|
|
#if PICO_RP2040
|
2023-10-05 16:32:19 +11:00
|
|
|
// This needs to be added to the result of time_us_64() to get the number of
|
|
|
|
|
// microseconds since the Epoch.
|
|
|
|
|
static uint64_t time_us_64_offset_from_epoch;
|
2024-05-23 10:57:14 +01:00
|
|
|
#endif
|
2023-10-05 16:32:19 +11:00
|
|
|
|
2022-11-02 15:21:21 +11:00
|
|
|
#if MICROPY_HW_ENABLE_UART_REPL || MICROPY_HW_USB_CDC
|
2021-01-21 00:34:08 +11:00
|
|
|
|
2022-03-30 23:00:46 +02:00
|
|
|
#ifndef MICROPY_HW_STDIN_BUFFER_LEN
|
|
|
|
|
#define MICROPY_HW_STDIN_BUFFER_LEN 512
|
2021-01-21 00:34:08 +11:00
|
|
|
#endif
|
|
|
|
|
|
2022-03-30 23:00:46 +02:00
|
|
|
static uint8_t stdin_ringbuf_array[MICROPY_HW_STDIN_BUFFER_LEN];
|
2021-01-21 00:34:08 +11:00
|
|
|
ringbuf_t stdin_ringbuf = { stdin_ringbuf_array, sizeof(stdin_ringbuf_array) };
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
|
|
|
|
|
uintptr_t ret = 0;
|
2022-11-02 15:21:21 +11:00
|
|
|
#if MICROPY_HW_USB_CDC
|
2024-05-10 22:23:05 +10:00
|
|
|
ret |= mp_usbd_cdc_poll_interfaces(poll_flags);
|
2022-03-30 23:00:46 +02:00
|
|
|
#endif
|
2024-05-10 22:23:05 +10:00
|
|
|
#if MICROPY_HW_ENABLE_UART_REPL
|
2023-03-22 00:14:46 +11:00
|
|
|
if (poll_flags & MP_STREAM_POLL_WR) {
|
|
|
|
|
ret |= MP_STREAM_POLL_WR;
|
|
|
|
|
}
|
2021-01-21 00:34:08 +11:00
|
|
|
#endif
|
2022-01-15 04:32:01 +02:00
|
|
|
#if MICROPY_PY_OS_DUPTERM
|
2022-08-18 14:47:56 +10:00
|
|
|
ret |= mp_os_dupterm_poll(poll_flags);
|
2022-01-15 04:32:01 +02:00
|
|
|
#endif
|
2021-01-21 00:34:08 +11:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Receive single character
|
|
|
|
|
int mp_hal_stdin_rx_chr(void) {
|
|
|
|
|
for (;;) {
|
2022-11-02 15:21:21 +11:00
|
|
|
#if MICROPY_HW_USB_CDC
|
2024-05-10 22:23:05 +10:00
|
|
|
mp_usbd_cdc_poll_interfaces(0);
|
2022-03-30 23:00:46 +02:00
|
|
|
#endif
|
|
|
|
|
|
2021-01-21 00:34:08 +11:00
|
|
|
int c = ringbuf_get(&stdin_ringbuf);
|
|
|
|
|
if (c != -1) {
|
|
|
|
|
return c;
|
|
|
|
|
}
|
2022-01-15 04:32:01 +02:00
|
|
|
#if MICROPY_PY_OS_DUPTERM
|
2022-08-18 14:47:56 +10:00
|
|
|
int dupterm_c = mp_os_dupterm_rx_chr();
|
2022-01-15 04:32:01 +02:00
|
|
|
if (dupterm_c >= 0) {
|
|
|
|
|
return dupterm_c;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2023-11-30 14:34:07 +11:00
|
|
|
mp_event_wait_indefinite();
|
2021-01-21 00:34:08 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Send string of given length
|
ports: Fix sys.stdout.buffer.write() return value.
MicroPython code may rely on the return value of sys.stdout.buffer.write()
to reflect the number of bytes actually written. While in most scenarios a
write() operation is successful, there are cases where it fails, leading to
data loss. This problem arises because, currently, write() merely returns
the number of bytes it was supposed to write, without indication of
failure.
One scenario where write() might fail, is where USB is used and the
receiving end doesn't read quickly enough to empty the receive buffer. In
that case, write() on the MicroPython side can timeout, resulting in the
loss of data without any indication, a behavior observed notably in
communication between a Pi Pico as a client and a Linux host using the ACM
driver.
A complex issue arises with mp_hal_stdout_tx_strn() when it involves
multiple outputs, such as USB, dupterm and hardware UART. The challenge is
in handling cases where writing to one output is successful, but another
fails, either fully or partially. This patch implements the following
solution:
mp_hal_stdout_tx_strn() attempts to write len bytes to all of the possible
destinations for that data, and returns the minimum successful write
length.
The implementation of this is complicated by several factors:
- multiple outputs may be enabled or disabled at compiled time
- multiple outputs may be enabled or disabled at runtime
- mp_os_dupterm_tx_strn() is one such output, optionally containing
multiple additional outputs
- each of these outputs may or may not be able to report success
- each of these outputs may or may not be able to report partial writes
As a result, there's no single strategy that fits all ports, necessitating
unique logic for each instance of mp_hal_stdout_tx_strn().
Note that addressing sys.stdout.write() is more complex due to its data
modification process ("cooked" output), and it remains unchanged in this
patch. Developers who are concerned about accurate return values from
write operations should use sys.stdout.buffer.write().
This patch might disrupt some existing code, but it's also expected to
resolve issues, considering that the peculiar return value behavior of
sys.stdout.buffer.write() is not well-documented and likely not widely
known. Therefore, it's improbable that much existing code relies on the
previous behavior.
Signed-off-by: Maarten van der Schrieck <maarten@thingsconnected.nl>
2023-06-18 11:46:25 +02:00
|
|
|
mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
|
|
|
|
|
mp_uint_t ret = len;
|
|
|
|
|
bool did_write = false;
|
2021-01-21 00:34:08 +11:00
|
|
|
#if MICROPY_HW_ENABLE_UART_REPL
|
|
|
|
|
mp_uart_write_strn(str, len);
|
ports: Fix sys.stdout.buffer.write() return value.
MicroPython code may rely on the return value of sys.stdout.buffer.write()
to reflect the number of bytes actually written. While in most scenarios a
write() operation is successful, there are cases where it fails, leading to
data loss. This problem arises because, currently, write() merely returns
the number of bytes it was supposed to write, without indication of
failure.
One scenario where write() might fail, is where USB is used and the
receiving end doesn't read quickly enough to empty the receive buffer. In
that case, write() on the MicroPython side can timeout, resulting in the
loss of data without any indication, a behavior observed notably in
communication between a Pi Pico as a client and a Linux host using the ACM
driver.
A complex issue arises with mp_hal_stdout_tx_strn() when it involves
multiple outputs, such as USB, dupterm and hardware UART. The challenge is
in handling cases where writing to one output is successful, but another
fails, either fully or partially. This patch implements the following
solution:
mp_hal_stdout_tx_strn() attempts to write len bytes to all of the possible
destinations for that data, and returns the minimum successful write
length.
The implementation of this is complicated by several factors:
- multiple outputs may be enabled or disabled at compiled time
- multiple outputs may be enabled or disabled at runtime
- mp_os_dupterm_tx_strn() is one such output, optionally containing
multiple additional outputs
- each of these outputs may or may not be able to report success
- each of these outputs may or may not be able to report partial writes
As a result, there's no single strategy that fits all ports, necessitating
unique logic for each instance of mp_hal_stdout_tx_strn().
Note that addressing sys.stdout.write() is more complex due to its data
modification process ("cooked" output), and it remains unchanged in this
patch. Developers who are concerned about accurate return values from
write operations should use sys.stdout.buffer.write().
This patch might disrupt some existing code, but it's also expected to
resolve issues, considering that the peculiar return value behavior of
sys.stdout.buffer.write() is not well-documented and likely not widely
known. Therefore, it's improbable that much existing code relies on the
previous behavior.
Signed-off-by: Maarten van der Schrieck <maarten@thingsconnected.nl>
2023-06-18 11:46:25 +02:00
|
|
|
did_write = true;
|
2021-01-21 00:34:08 +11:00
|
|
|
#endif
|
|
|
|
|
|
2022-11-02 15:21:21 +11:00
|
|
|
#if MICROPY_HW_USB_CDC
|
2024-05-10 22:23:05 +10:00
|
|
|
mp_uint_t cdc_res = mp_usbd_cdc_tx_strn(str, len);
|
|
|
|
|
if (cdc_res > 0) {
|
ports: Fix sys.stdout.buffer.write() return value.
MicroPython code may rely on the return value of sys.stdout.buffer.write()
to reflect the number of bytes actually written. While in most scenarios a
write() operation is successful, there are cases where it fails, leading to
data loss. This problem arises because, currently, write() merely returns
the number of bytes it was supposed to write, without indication of
failure.
One scenario where write() might fail, is where USB is used and the
receiving end doesn't read quickly enough to empty the receive buffer. In
that case, write() on the MicroPython side can timeout, resulting in the
loss of data without any indication, a behavior observed notably in
communication between a Pi Pico as a client and a Linux host using the ACM
driver.
A complex issue arises with mp_hal_stdout_tx_strn() when it involves
multiple outputs, such as USB, dupterm and hardware UART. The challenge is
in handling cases where writing to one output is successful, but another
fails, either fully or partially. This patch implements the following
solution:
mp_hal_stdout_tx_strn() attempts to write len bytes to all of the possible
destinations for that data, and returns the minimum successful write
length.
The implementation of this is complicated by several factors:
- multiple outputs may be enabled or disabled at compiled time
- multiple outputs may be enabled or disabled at runtime
- mp_os_dupterm_tx_strn() is one such output, optionally containing
multiple additional outputs
- each of these outputs may or may not be able to report success
- each of these outputs may or may not be able to report partial writes
As a result, there's no single strategy that fits all ports, necessitating
unique logic for each instance of mp_hal_stdout_tx_strn().
Note that addressing sys.stdout.write() is more complex due to its data
modification process ("cooked" output), and it remains unchanged in this
patch. Developers who are concerned about accurate return values from
write operations should use sys.stdout.buffer.write().
This patch might disrupt some existing code, but it's also expected to
resolve issues, considering that the peculiar return value behavior of
sys.stdout.buffer.write() is not well-documented and likely not widely
known. Therefore, it's improbable that much existing code relies on the
previous behavior.
Signed-off-by: Maarten van der Schrieck <maarten@thingsconnected.nl>
2023-06-18 11:46:25 +02:00
|
|
|
did_write = true;
|
2024-05-10 22:23:05 +10:00
|
|
|
ret = MIN(cdc_res, ret);
|
2021-01-21 00:34:08 +11:00
|
|
|
}
|
|
|
|
|
#endif
|
2022-01-15 04:32:01 +02:00
|
|
|
|
|
|
|
|
#if MICROPY_PY_OS_DUPTERM
|
ports: Fix sys.stdout.buffer.write() return value.
MicroPython code may rely on the return value of sys.stdout.buffer.write()
to reflect the number of bytes actually written. While in most scenarios a
write() operation is successful, there are cases where it fails, leading to
data loss. This problem arises because, currently, write() merely returns
the number of bytes it was supposed to write, without indication of
failure.
One scenario where write() might fail, is where USB is used and the
receiving end doesn't read quickly enough to empty the receive buffer. In
that case, write() on the MicroPython side can timeout, resulting in the
loss of data without any indication, a behavior observed notably in
communication between a Pi Pico as a client and a Linux host using the ACM
driver.
A complex issue arises with mp_hal_stdout_tx_strn() when it involves
multiple outputs, such as USB, dupterm and hardware UART. The challenge is
in handling cases where writing to one output is successful, but another
fails, either fully or partially. This patch implements the following
solution:
mp_hal_stdout_tx_strn() attempts to write len bytes to all of the possible
destinations for that data, and returns the minimum successful write
length.
The implementation of this is complicated by several factors:
- multiple outputs may be enabled or disabled at compiled time
- multiple outputs may be enabled or disabled at runtime
- mp_os_dupterm_tx_strn() is one such output, optionally containing
multiple additional outputs
- each of these outputs may or may not be able to report success
- each of these outputs may or may not be able to report partial writes
As a result, there's no single strategy that fits all ports, necessitating
unique logic for each instance of mp_hal_stdout_tx_strn().
Note that addressing sys.stdout.write() is more complex due to its data
modification process ("cooked" output), and it remains unchanged in this
patch. Developers who are concerned about accurate return values from
write operations should use sys.stdout.buffer.write().
This patch might disrupt some existing code, but it's also expected to
resolve issues, considering that the peculiar return value behavior of
sys.stdout.buffer.write() is not well-documented and likely not widely
known. Therefore, it's improbable that much existing code relies on the
previous behavior.
Signed-off-by: Maarten van der Schrieck <maarten@thingsconnected.nl>
2023-06-18 11:46:25 +02:00
|
|
|
int dupterm_res = mp_os_dupterm_tx_strn(str, len);
|
|
|
|
|
if (dupterm_res >= 0) {
|
|
|
|
|
did_write = true;
|
|
|
|
|
ret = MIN((mp_uint_t)dupterm_res, ret);
|
|
|
|
|
}
|
2022-01-15 04:32:01 +02:00
|
|
|
#endif
|
ports: Fix sys.stdout.buffer.write() return value.
MicroPython code may rely on the return value of sys.stdout.buffer.write()
to reflect the number of bytes actually written. While in most scenarios a
write() operation is successful, there are cases where it fails, leading to
data loss. This problem arises because, currently, write() merely returns
the number of bytes it was supposed to write, without indication of
failure.
One scenario where write() might fail, is where USB is used and the
receiving end doesn't read quickly enough to empty the receive buffer. In
that case, write() on the MicroPython side can timeout, resulting in the
loss of data without any indication, a behavior observed notably in
communication between a Pi Pico as a client and a Linux host using the ACM
driver.
A complex issue arises with mp_hal_stdout_tx_strn() when it involves
multiple outputs, such as USB, dupterm and hardware UART. The challenge is
in handling cases where writing to one output is successful, but another
fails, either fully or partially. This patch implements the following
solution:
mp_hal_stdout_tx_strn() attempts to write len bytes to all of the possible
destinations for that data, and returns the minimum successful write
length.
The implementation of this is complicated by several factors:
- multiple outputs may be enabled or disabled at compiled time
- multiple outputs may be enabled or disabled at runtime
- mp_os_dupterm_tx_strn() is one such output, optionally containing
multiple additional outputs
- each of these outputs may or may not be able to report success
- each of these outputs may or may not be able to report partial writes
As a result, there's no single strategy that fits all ports, necessitating
unique logic for each instance of mp_hal_stdout_tx_strn().
Note that addressing sys.stdout.write() is more complex due to its data
modification process ("cooked" output), and it remains unchanged in this
patch. Developers who are concerned about accurate return values from
write operations should use sys.stdout.buffer.write().
This patch might disrupt some existing code, but it's also expected to
resolve issues, considering that the peculiar return value behavior of
sys.stdout.buffer.write() is not well-documented and likely not widely
known. Therefore, it's improbable that much existing code relies on the
previous behavior.
Signed-off-by: Maarten van der Schrieck <maarten@thingsconnected.nl>
2023-06-18 11:46:25 +02:00
|
|
|
return did_write ? ret : 0;
|
2021-01-21 00:34:08 +11:00
|
|
|
}
|
|
|
|
|
|
2024-08-08 22:26:14 +10:00
|
|
|
#if PICO_RISCV
|
|
|
|
|
__attribute__((naked)) mp_uint_t mp_hal_ticks_cpu(void) {
|
|
|
|
|
__asm volatile (
|
|
|
|
|
"li a0, 4\n" // mask value to uninhibit mcycle counter
|
|
|
|
|
"csrw mcountinhibit, a0\n" // uninhibit mcycle counter
|
|
|
|
|
"csrr a0, mcycle\n" // get mcycle counter
|
|
|
|
|
"ret\n"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-01-02 16:35:27 +11:00
|
|
|
void mp_hal_delay_us(mp_uint_t us) {
|
|
|
|
|
// Avoid calling sleep_us() and invoking the alarm pool by splitting long
|
|
|
|
|
// sleeps into an optional longer sleep and a shorter busy-wait
|
|
|
|
|
uint64_t end = time_us_64() + us;
|
|
|
|
|
if (us > 1000) {
|
|
|
|
|
mp_hal_delay_ms(us / 1000);
|
|
|
|
|
}
|
|
|
|
|
while (time_us_64() < end) {
|
|
|
|
|
// Tight loop busy-wait for accurate timing
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-21 00:34:08 +11:00
|
|
|
void mp_hal_delay_ms(mp_uint_t ms) {
|
2024-01-02 16:35:27 +11:00
|
|
|
mp_uint_t start = mp_hal_ticks_ms();
|
|
|
|
|
mp_uint_t elapsed = 0;
|
2023-11-14 16:17:24 +11:00
|
|
|
do {
|
2024-01-02 16:35:27 +11:00
|
|
|
mp_event_wait_ms(ms - elapsed);
|
|
|
|
|
elapsed = mp_hal_ticks_ms() - start;
|
|
|
|
|
} while (elapsed < ms);
|
2021-01-21 00:34:08 +11:00
|
|
|
}
|
|
|
|
|
|
2023-10-05 16:32:19 +11:00
|
|
|
void mp_hal_time_ns_set_from_rtc(void) {
|
2024-05-23 10:57:14 +01:00
|
|
|
#if PICO_RP2040
|
2024-01-02 16:35:27 +11:00
|
|
|
// Outstanding RTC register writes need at least two RTC clock cycles to
|
|
|
|
|
// update. (See RP2040 datasheet section 4.8.4 "Reference clock").
|
|
|
|
|
mp_hal_delay_us(44);
|
2023-10-05 16:32:19 +11:00
|
|
|
|
|
|
|
|
// Sample RTC and time_us_64() as close together as possible, so the offset
|
|
|
|
|
// calculated for the latter can be as accurate as possible.
|
2024-05-23 10:57:14 +01:00
|
|
|
struct timespec ts;
|
|
|
|
|
aon_timer_get_time(&ts);
|
2023-10-05 16:32:19 +11:00
|
|
|
uint64_t us = time_us_64();
|
|
|
|
|
|
2024-05-23 10:57:14 +01:00
|
|
|
// Calculate the difference between the RTC Epoch and time_us_64().
|
|
|
|
|
time_us_64_offset_from_epoch = ((uint64_t)ts.tv_sec * 1000000ULL) + ((uint64_t)ts.tv_nsec / 1000ULL) - us;
|
|
|
|
|
#endif
|
2023-10-05 16:32:19 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint64_t mp_hal_time_ns(void) {
|
2024-05-23 10:57:14 +01:00
|
|
|
#if PICO_RP2040
|
|
|
|
|
// The RTC probably has limited resolution, so instead use time_us_64() to get a more
|
2023-10-05 16:32:19 +11:00
|
|
|
// precise measure of Epoch time. Both these "clocks" are clocked from the same
|
|
|
|
|
// source so they remain synchronised, and only differ by a fixed offset (calculated
|
|
|
|
|
// in mp_hal_time_ns_set_from_rtc).
|
|
|
|
|
return (time_us_64_offset_from_epoch + time_us_64()) * 1000ULL;
|
2024-05-23 10:57:14 +01:00
|
|
|
#else
|
|
|
|
|
// aon timer has ms resolution
|
|
|
|
|
struct timespec ts;
|
|
|
|
|
aon_timer_get_time(&ts);
|
|
|
|
|
return ((uint64_t)ts.tv_sec * 1000000000ULL) + (uint64_t)ts.tv_nsec;
|
|
|
|
|
#endif
|
2021-01-21 00:34:08 +11:00
|
|
|
}
|
2022-05-05 17:26:16 +10:00
|
|
|
|
|
|
|
|
// Generate a random locally administered MAC address (LAA)
|
|
|
|
|
void mp_hal_generate_laa_mac(int idx, uint8_t buf[6]) {
|
2022-06-30 16:01:02 +10:00
|
|
|
#ifndef NDEBUG
|
|
|
|
|
printf("Warning: No MAC in OTP, generating MAC from board id\n");
|
|
|
|
|
#endif
|
2022-05-05 17:26:16 +10:00
|
|
|
pico_unique_board_id_t pid;
|
|
|
|
|
pico_get_unique_board_id(&pid);
|
|
|
|
|
buf[0] = 0x02; // LAA range
|
|
|
|
|
buf[1] = (pid.id[7] << 4) | (pid.id[6] & 0xf);
|
|
|
|
|
buf[2] = (pid.id[5] << 4) | (pid.id[4] & 0xf);
|
|
|
|
|
buf[3] = (pid.id[3] << 4) | (pid.id[2] & 0xf);
|
|
|
|
|
buf[4] = pid.id[1];
|
|
|
|
|
buf[5] = (pid.id[0] << 2) | idx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A board can override this if needed
|
|
|
|
|
MP_WEAK void mp_hal_get_mac(int idx, uint8_t buf[6]) {
|
2022-06-30 16:01:02 +10:00
|
|
|
#if MICROPY_PY_NETWORK_CYW43
|
|
|
|
|
// The mac should come from cyw43 otp when CYW43_USE_OTP_MAC is defined
|
|
|
|
|
// This is loaded into the state after the driver is initialised
|
|
|
|
|
// cyw43_hal_generate_laa_mac is only called by the driver to generate a mac if otp is not set
|
2023-05-05 13:08:55 +02:00
|
|
|
if (idx == MP_HAL_MAC_WLAN0) {
|
|
|
|
|
memcpy(buf, cyw43_state.mac, 6);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-06-30 16:01:02 +10:00
|
|
|
#endif
|
2023-05-05 13:08:55 +02:00
|
|
|
mp_hal_generate_laa_mac(idx, buf);
|
2022-06-30 16:01:02 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void mp_hal_get_mac_ascii(int idx, size_t chr_off, size_t chr_len, char *dest) {
|
|
|
|
|
static const char hexchr[16] = "0123456789ABCDEF";
|
|
|
|
|
uint8_t mac[6];
|
|
|
|
|
mp_hal_get_mac(idx, mac);
|
|
|
|
|
for (; chr_len; ++chr_off, --chr_len) {
|
|
|
|
|
*dest++ = hexchr[mac[chr_off >> 1] >> (4 * (1 - (chr_off & 1))) & 0xf];
|
|
|
|
|
}
|
2022-05-05 17:26:16 +10:00
|
|
|
}
|
2022-06-30 15:35:23 +01:00
|
|
|
|
|
|
|
|
// Shouldn't be used, needed by cyw43-driver in debug build.
|
|
|
|
|
uint32_t storage_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks) {
|
|
|
|
|
panic_unsupported();
|
|
|
|
|
}
|
2023-11-14 13:21:13 +11:00
|
|
|
|
|
|
|
|
uint32_t soft_timer_get_ms(void) {
|
|
|
|
|
return mp_hal_ticks_ms();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void soft_timer_schedule_at_ms(uint32_t ticks_ms) {
|
|
|
|
|
int32_t ms = soft_timer_ticks_diff(ticks_ms, mp_hal_ticks_ms());
|
|
|
|
|
ms = MAX(0, ms);
|
2024-01-02 12:19:33 +11:00
|
|
|
if (hardware_alarm_set_target(MICROPY_HW_SOFT_TIMER_ALARM_NUM, delayed_by_ms(get_absolute_time(), ms))) {
|
|
|
|
|
// "missed" hardware alarm target
|
|
|
|
|
hardware_alarm_force_irq(MICROPY_HW_SOFT_TIMER_ALARM_NUM);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void soft_timer_hardware_callback(unsigned int alarm_num) {
|
|
|
|
|
// The timer alarm ISR needs to call here and trigger PendSV dispatch via
|
|
|
|
|
// a second ISR, as PendSV may be currently suspended by the other CPU.
|
|
|
|
|
pendsv_schedule_dispatch(PENDSV_DISPATCH_SOFT_TIMER, soft_timer_handler);
|
2024-07-03 15:52:31 +10:00
|
|
|
|
|
|
|
|
// This ISR only runs on core0, but if core1 is running Python code then it
|
|
|
|
|
// may be blocked in WFE so wake it up as well. Unfortunately this also sets
|
|
|
|
|
// the event flag on core0, so a subsequent WFE on this core will not suspend
|
2024-08-13 14:55:26 +01:00
|
|
|
#if MICROPY_PY_THREAD
|
2024-07-03 15:52:31 +10:00
|
|
|
if (core1_entry != NULL) {
|
|
|
|
|
__sev();
|
|
|
|
|
}
|
2024-08-13 14:55:26 +01:00
|
|
|
#endif
|
2024-01-02 12:19:33 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void soft_timer_init(void) {
|
|
|
|
|
hardware_alarm_claim(MICROPY_HW_SOFT_TIMER_ALARM_NUM);
|
|
|
|
|
hardware_alarm_set_callback(MICROPY_HW_SOFT_TIMER_ALARM_NUM, soft_timer_hardware_callback);
|
2023-11-14 13:21:13 +11:00
|
|
|
}
|
2024-01-02 16:35:27 +11:00
|
|
|
|
|
|
|
|
void mp_wfe_or_timeout(uint32_t timeout_ms) {
|
|
|
|
|
soft_timer_entry_t timer;
|
|
|
|
|
|
|
|
|
|
// Note the timer doesn't have an associated callback, it just exists to create a
|
|
|
|
|
// hardware interrupt to wake the CPU
|
|
|
|
|
soft_timer_static_init(&timer, SOFT_TIMER_MODE_ONE_SHOT, 0, NULL);
|
|
|
|
|
soft_timer_insert(&timer, timeout_ms);
|
|
|
|
|
|
|
|
|
|
__wfe();
|
|
|
|
|
|
|
|
|
|
// Clean up the timer node if it's not already
|
|
|
|
|
soft_timer_remove(&timer);
|
|
|
|
|
}
|