Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 28 additions & 26 deletions bricks/ev3/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@

#include <pbdrv/config.h>
#include <pbdrv/clock.h>
#include <pbdrv/uart.h>

#include <pbio/main.h>
#include <pbdrv/../../drv/uart/uart_debug_first_port.h>
#include <pbsys/host.h>

#include "py/runtime.h"
#include "py/mphal.h"
Expand All @@ -32,49 +30,53 @@ void mp_hal_delay_ms(mp_uint_t Delay) {
} while (pbdrv_clock_get_ms() - start < Delay);
}

extern uint32_t pbdrv_usb_rx_data_available(void);

extern int32_t pbdrv_usb_get_char(void);

uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {

uintptr_t ret = 0;

if ((poll_flags & MP_STREAM_POLL_RD) && pbdrv_usb_rx_data_available() > 0) {
if ((poll_flags & MP_STREAM_POLL_RD) && pbsys_host_stdin_get_available()) {
ret |= MP_STREAM_POLL_RD;
}

return ret;
}

// Receive single character
int mp_hal_stdin_rx_chr(void) {
int c;
while (((c = pbdrv_uart_debug_get_char()) == -1) && (c = pbdrv_usb_get_char()) == -1) {
MICROPY_EVENT_POLL_HOOK;
uint32_t size;
uint8_t c;

// wait for rx interrupt
while (size = 1, pbsys_host_stdin_read(&c, &size) != PBIO_SUCCESS) {
MICROPY_EVENT_POLL_HOOK
}

return c;
}

extern uint32_t pbdrv_usb_write(const uint8_t *data, uint32_t size);

// Send string of given length
mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) {
pbdrv_uart_debug_printf("%.*s", len, str);

uint32_t done = 0;
while (done < len) {
done += pbdrv_usb_write((uint8_t *)str + done, len - done);
MICROPY_VM_HOOK_LOOP;
}
size_t remaining = len;

while (remaining) {
uint32_t size = remaining;
pbio_error_t err = pbsys_host_stdout_write((const uint8_t *)str, &size);
if (err == PBIO_SUCCESS) {
str += size;
remaining -= size;
} else if (err != PBIO_ERROR_AGAIN) {
// Ignoring error for now. This means stdout is lost if Bluetooth/USB
// is disconnected.
return len - remaining;
}

while (!pbdrv_uart_debug_is_done()) {
MICROPY_VM_HOOK_LOOP;
MICROPY_EVENT_POLL_HOOK
}

return len;
}

extern void pbdrv_usb_tx_flush(void);

void mp_hal_stdout_tx_flush(void) {
pbdrv_usb_tx_flush();
while (!pbsys_host_tx_is_idle()) {
MICROPY_EVENT_POLL_HOOK
}
}
22 changes: 0 additions & 22 deletions lib/pbio/drv/usb/stm32_usbd/usbd_pybricks.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,6 @@ extern "C" {
* @}
*/

// NOTE: These enums values are sent over the wire, so cannot be changed. Also,
// 0 is skipped to avoid a zeroed buffer from being misinterpreted as a message.

/** Hub to host messages via the Pybricks interface IN endpoint. */
enum {
/**
* Analog of BLE status response. Emitted in response to every OUT message
* received.
*/
USBD_PYBRICKS_IN_EP_MSG_RESPONSE = 1,
/**Analog to BLE notification. Only emitted if subscribed. */
USBD_PYBRICKS_IN_EP_MSG_EVENT = 2,
};

/** Host to hub messages via the Pybricks USB interface OUT endpoint. */
enum {
/** Analog of BLE Client Characteristic Configuration Descriptor (CCCD). */
USBD_PYBRICKS_OUT_EP_MSG_SUBSCRIBE = 1,
/** Analog of BLE Client Characteristic Write with response. */
USBD_PYBRICKS_OUT_EP_MSG_COMMAND = 2,
};


/** @defgroup USBD_Pybricks_Exported_TypesDefinitions
* @{
Expand Down
Loading