circuitpython/ports/nrf/supervisor/serial.c

128 lines
3.4 KiB
C
Raw Normal View History

/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017, 2018 Scott Shawcroft for Adafruit Industries
*
* 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.
*/
2018-09-25 05:14:44 -04:00
#include "py/mphal.h"
#include "supervisor/serial.h"
#if CIRCUITPY_CONSOLE_BLE
2018-01-15 12:03:22 -05:00
#include "ble_uart.h"
#elif CIRCUITPY_CONSOLE_UART
#include <string.h>
2018-07-08 09:17:06 -04:00
#include "nrf_gpio.h"
2018-09-25 05:14:44 -04:00
#include "nrfx_uarte.h"
2018-01-15 12:03:22 -05:00
#endif
#if CIRCUITPY_CONSOLE_BLE
2018-07-23 17:43:24 -04:00
void serial_init(void) {
ble_uart_init();
}
bool serial_connected(void) {
return ble_uart_connected();
}
char serial_read(void) {
2021-03-15 09:57:36 -04:00
return (char)ble_uart_rx_chr();
2018-07-23 17:43:24 -04:00
}
bool serial_bytes_available(void) {
return ble_uart_stdin_any();
}
void serial_write(const char *text) {
ble_uart_stdout_tx_str(text);
}
#elif CIRCUITPY_CONSOLE_UART
2018-09-25 05:14:44 -04:00
uint8_t serial_received_char;
nrfx_uarte_t serial_instance = NRFX_UARTE_INSTANCE(0);
2018-07-08 09:17:06 -04:00
void serial_init(void) {
2018-09-25 05:14:44 -04:00
nrfx_uarte_config_t config = {
.pseltxd = MICROPY_HW_UART_TX,
.pselrxd = MICROPY_HW_UART_RX,
.pselcts = NRF_UARTE_PSEL_DISCONNECTED,
.pselrts = NRF_UARTE_PSEL_DISCONNECTED,
.p_context = NULL,
.hwfc = NRF_UARTE_HWFC_DISABLED,
.parity = NRF_UARTE_PARITY_EXCLUDED,
.baudrate = NRF_UARTE_BAUDRATE_115200,
.interrupt_priority = 7
};
nrfx_uarte_uninit(&serial_instance);
const nrfx_err_t err = nrfx_uarte_init(&serial_instance, &config, NULL); // no callback for blocking mode
if (err != NRFX_SUCCESS) {
2018-07-08 09:17:06 -04:00
NRFX_ASSERT(err);
2018-09-25 05:14:44 -04:00
}
2018-09-25 05:14:44 -04:00
// enabled receiving
nrf_uarte_task_trigger(serial_instance.p_reg, NRF_UARTE_TASK_STARTRX);
}
bool serial_connected(void) {
return true;
}
char serial_read(void) {
uint8_t data;
nrfx_uarte_rx(&serial_instance, &data, 1);
return data;
}
bool serial_bytes_available(void) {
return nrf_uarte_event_check(serial_instance.p_reg, NRF_UARTE_EVENT_RXDRDY);
}
2021-03-15 09:57:36 -04:00
void serial_write(const char *text) {
serial_write_substring(text, strlen(text));
}
void serial_write_substring(const char *text, uint32_t len) {
if (len == 0) {
return;
}
// EasyDMA can only access SRAM
2021-03-15 09:57:36 -04:00
uint8_t *tx_buf = (uint8_t *)text;
if (!nrfx_is_in_ram(text)) {
tx_buf = (uint8_t *)m_malloc(len, false);
memcpy(tx_buf, text, len);
}
nrfx_uarte_tx(&serial_instance, tx_buf, len);
2021-03-15 09:57:36 -04:00
if (!nrfx_is_in_ram(text)) {
m_free(tx_buf);
}
}
#endif // CIRCUITPY_CONSOLE_UART