2016-08-23 02:48:09 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "asf/common/services/usb/class/cdc/device/udi_cdc.h"
|
2016-08-24 16:17:55 -04:00
|
|
|
#include "asf/common2/services/delay/delay.h"
|
2016-08-23 19:47:53 -04:00
|
|
|
#include "asf/sam0/drivers/sercom/usart/usart.h"
|
2016-08-23 02:48:09 -04:00
|
|
|
#include "py/mphal.h"
|
2016-08-23 19:47:53 -04:00
|
|
|
#include "py/mpstate.h"
|
2016-08-23 02:48:09 -04:00
|
|
|
|
2016-08-23 19:47:53 -04:00
|
|
|
#include "mpconfigboard.h"
|
|
|
|
|
2016-08-23 02:48:09 -04:00
|
|
|
static volatile bool mp_cdc_enabled = false;
|
2016-08-23 19:47:53 -04:00
|
|
|
extern struct usart_module usart_instance;
|
|
|
|
|
2016-08-23 02:48:09 -04:00
|
|
|
bool mp_cdc_enable(uint8_t port)
|
|
|
|
{
|
|
|
|
mp_cdc_enabled = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mp_cdc_disable(uint8_t port)
|
|
|
|
{
|
|
|
|
mp_cdc_enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mp_hal_stdin_rx_chr(void) {
|
|
|
|
for (;;) {
|
2016-08-23 19:47:53 -04:00
|
|
|
#ifdef USB_REPL
|
2016-08-23 02:48:09 -04:00
|
|
|
if (mp_cdc_enabled && udi_cdc_is_rx_ready()) {
|
2016-08-24 03:14:11 -04:00
|
|
|
return udi_cdc_getc();
|
2016-08-23 02:48:09 -04:00
|
|
|
}
|
2016-08-23 19:47:53 -04:00
|
|
|
#endif
|
|
|
|
#ifdef UART_REPL
|
|
|
|
uint16_t temp;
|
|
|
|
if (usart_read_wait(&usart_instance, &temp) == STATUS_OK) {
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
// TODO(tannewt): Figure out how we can sleep while waiting for input and
|
|
|
|
// add it here. The current UART implementation doesn't cause a wake.
|
|
|
|
//__WFI();
|
2016-08-23 02:48:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//void mp_hal_stdout_tx_str(const char *str) {
|
|
|
|
// mp_hal_stdout_tx_strn(str, strlen(str));
|
|
|
|
//}
|
|
|
|
|
|
|
|
void mp_hal_stdout_tx_strn(const char *str, size_t len) {
|
2016-08-24 03:14:11 -04:00
|
|
|
#ifdef UART_REPL
|
2016-08-23 19:47:53 -04:00
|
|
|
usart_write_buffer_wait(&usart_instance, (uint8_t*) str, len);
|
2016-08-24 03:14:11 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USB_REPL
|
|
|
|
if (mp_cdc_enabled && udi_cdc_is_tx_ready()) {
|
|
|
|
udi_cdc_write_buf(str, len);
|
|
|
|
}
|
|
|
|
#endif
|
2016-08-23 02:48:09 -04:00
|
|
|
}
|
|
|
|
|
2016-08-24 16:17:55 -04:00
|
|
|
void mp_hal_delay_ms(mp_uint_t delay) {
|
|
|
|
delay_ms(delay);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mp_hal_delay_us(mp_uint_t delay) {
|
|
|
|
delay_us(delay);
|
|
|
|
}
|