circuitpython/ports/nrf/common-hal/busio/UART.c

396 lines
12 KiB
C
Raw Normal View History

/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
2018-10-16 11:05:02 -04:00
* Copyright (c) 2018 Ha Thach 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.
*/
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/busio/UART.h"
#include "lib/utils/interrupt_char.h"
#include "py/mpconfig.h"
#include "py/gc.h"
#include "py/mperrno.h"
#include "py/runtime.h"
#include "py/stream.h"
2018-08-16 17:21:44 -04:00
#include "supervisor/shared/translate.h"
#include "tick.h"
2018-09-24 03:37:28 -04:00
#include "nrfx_uarte.h"
2018-09-20 16:48:13 -04:00
#include <string.h>
2018-09-25 03:29:45 -04:00
#ifdef NRF52840_XXAA
2018-09-19 06:59:15 -04:00
// expression to examine, and return value in case of failing
2018-09-19 14:07:45 -04:00
#define _VERIFY_ERR(_exp) \
2018-09-19 06:59:15 -04:00
do {\
uint32_t _err = (_exp);\
if (NRFX_SUCCESS != _err ) {\
2018-09-25 15:06:32 -04:00
mp_raise_msg_varg(&mp_type_RuntimeError, translate("error = 0x%08lX"), _err);\
2018-09-19 06:59:15 -04:00
}\
}while(0)
static uint32_t get_nrf_baud (uint32_t baudrate);
2018-12-13 11:48:53 -05:00
static uint16_t ringbuf_count(ringbuf_t *r)
{
volatile int count = r->iput - r->iget;
if ( count < 0 ) {
count += r->size;
}
return (uint16_t) count;
}
static void ringbuf_clear(ringbuf_t *r)
{
r->iput = r->iget = 0;
}
2018-09-24 03:37:28 -04:00
static void uart_callback_irq (const nrfx_uarte_event_t * event, void * context) {
2018-09-19 06:59:15 -04:00
busio_uart_obj_t* self = (busio_uart_obj_t*) context;
switch ( event->type ) {
2018-09-25 03:29:45 -04:00
case NRFX_UARTE_EVT_RX_DONE:
2018-12-13 11:48:53 -05:00
for(uint8_t i=0; i < event->data.rxtx.bytes; i++) {
2018-12-13 11:56:06 -05:00
if ( ringbuf_put(&self->rbuf, event->data.rxtx.p_data[i]) < 0 ) {
// if full overwrite old data
(void) ringbuf_get(&self->rbuf);
ringbuf_put(&self->rbuf, event->data.rxtx.p_data[i]);
}
2018-12-13 11:48:53 -05:00
}
// keep receiving
(void) nrfx_uarte_rx(&self->uarte, &self->rx_char, 1);
2018-09-25 01:37:31 -04:00
break;
2018-09-25 03:29:45 -04:00
case NRFX_UARTE_EVT_TX_DONE:
2018-12-13 11:48:53 -05:00
// nothing to do
2018-09-19 06:59:15 -04:00
break;
2018-09-25 03:29:45 -04:00
case NRFX_UARTE_EVT_ERROR:
// Possible Error source is Overrun, Parity, Framing, Break
// uint32_t errsrc = event->data.error.error_mask;
// Keep receiving
(void) nrfx_uarte_rx(&self->uarte, &self->rx_char, 1);
2018-09-19 14:07:45 -04:00
break;
2018-09-19 06:59:15 -04:00
default:
break;
}
}
void common_hal_busio_uart_construct (busio_uart_obj_t *self,
const mcu_pin_obj_t * tx, const mcu_pin_obj_t * rx, uint32_t baudrate,
uint8_t bits, uart_parity_t parity, uint8_t stop, mp_float_t timeout,
2018-09-19 06:59:15 -04:00
uint8_t receiver_buffer_size) {
2018-09-24 05:06:44 -04:00
if ( (tx == mp_const_none) && (rx == mp_const_none) ) {
mp_raise_ValueError(translate("tx and rx cannot both be None"));
2018-09-19 06:59:15 -04:00
}
if ( receiver_buffer_size == 0 ) {
mp_raise_ValueError(translate("Invalid buffer size"));
}
2018-09-20 14:27:52 -04:00
if ( parity == PARITY_ODD ) {
2018-09-25 15:10:44 -04:00
mp_raise_ValueError(translate("Odd parity is not supported"));
2018-09-20 14:27:52 -04:00
}
2018-09-24 03:37:28 -04:00
nrfx_uarte_config_t config = {
2018-09-25 05:14:44 -04:00
.pseltxd = (tx == mp_const_none) ? NRF_UARTE_PSEL_DISCONNECTED : tx->number,
.pselrxd = (rx == mp_const_none) ? NRF_UARTE_PSEL_DISCONNECTED : rx->number,
.pselcts = NRF_UARTE_PSEL_DISCONNECTED,
.pselrts = NRF_UARTE_PSEL_DISCONNECTED,
2018-09-19 06:59:15 -04:00
.p_context = self,
2018-09-25 05:14:44 -04:00
.hwfc = NRF_UARTE_HWFC_DISABLED,
.parity = (parity == PARITY_NONE) ? NRF_UARTE_PARITY_EXCLUDED : NRF_UARTE_PARITY_INCLUDED,
2018-09-20 14:27:52 -04:00
.baudrate = get_nrf_baud(baudrate),
2018-09-19 06:59:15 -04:00
.interrupt_priority = 7
};
2018-09-24 05:06:44 -04:00
// support only 1 instance for now
self->uarte = (nrfx_uarte_t ) NRFX_UARTE_INSTANCE(0);
nrfx_uarte_uninit(&self->uarte);
_VERIFY_ERR(nrfx_uarte_init(&self->uarte, &config, uart_callback_irq));
2018-09-19 06:59:15 -04:00
2018-09-20 14:27:52 -04:00
// Init buffer for rx
2018-09-24 05:06:44 -04:00
if ( rx != mp_const_none ) {
2018-12-13 11:48:53 -05:00
self->rbuf.buf = (uint8_t *) gc_alloc(receiver_buffer_size, false, false);
if ( !self->rbuf.buf ) {
2018-09-24 05:06:44 -04:00
nrfx_uarte_uninit(&self->uarte);
mp_raise_msg(&mp_type_MemoryError, translate("Failed to allocate RX buffer"));
}
2018-12-13 11:48:53 -05:00
self->rbuf.size = receiver_buffer_size;
self->rbuf.iget = self->rbuf.iput = 0;
2018-09-24 05:06:44 -04:00
self->rx_pin_number = rx->number;
2018-09-24 05:06:44 -04:00
claim_pin(rx);
}
if ( tx != mp_const_none ) {
self->tx_pin_number = tx->number;
2018-09-24 05:06:44 -04:00
claim_pin(tx);
} else {
self->tx_pin_number = NO_PIN;
2018-09-19 06:59:15 -04:00
}
self->baudrate = baudrate;
self->timeout_ms = timeout * 1000;
2018-09-25 01:37:31 -04:00
2018-12-13 11:48:53 -05:00
// Initial wait for incoming byte
_VERIFY_ERR(nrfx_uarte_rx(&self->uarte, &self->rx_char, 1));
}
bool common_hal_busio_uart_deinited(busio_uart_obj_t *self) {
return self->rx_pin_number == NO_PIN;
}
void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
2018-09-19 06:59:15 -04:00
if ( !common_hal_busio_uart_deinited(self) ) {
2018-09-24 05:06:44 -04:00
nrfx_uarte_uninit(&self->uarte);
reset_pin_number(self->tx_pin_number);
reset_pin_number(self->rx_pin_number);
self->tx_pin_number = NO_PIN;
self->rx_pin_number = NO_PIN;
2018-12-13 11:48:53 -05:00
gc_free(self->rbuf.buf);
self->rbuf.size = 0;
self->rbuf.iput = self->rbuf.iget = 0;
}
}
// Read characters.
size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t len, int *errcode) {
2018-09-25 05:14:44 -04:00
if ( nrf_uarte_rx_pin_get(self->uarte.p_reg) == NRF_UARTE_PSEL_DISCONNECTED ) {
2018-09-24 05:06:44 -04:00
mp_raise_ValueError(translate("No RX pin"));
}
2018-12-13 11:48:53 -05:00
size_t rx_bytes = 0;
2018-09-20 16:48:13 -04:00
uint64_t start_ticks = ticks_ms;
2018-12-13 11:48:53 -05:00
// Wait for all bytes received or timeout
while ( (ringbuf_count(&self->rbuf) < len) && (ticks_ms - start_ticks < self->timeout_ms) ) {
2018-09-20 16:48:13 -04:00
#ifdef MICROPY_VM_HOOK_LOOP
2018-12-13 11:48:53 -05:00
MICROPY_VM_HOOK_LOOP ;
// Allow user to break out of a timeout with a KeyboardInterrupt.
if ( mp_hal_is_interrupted() ) {
return 0;
2018-09-20 16:48:13 -04:00
}
2018-12-13 11:48:53 -05:00
#endif
}
2018-09-25 01:37:31 -04:00
2018-12-13 11:48:53 -05:00
// prevent conflict with uart irq
NVIC_DisableIRQ(nrfx_get_irq_number(self->uarte.p_reg));
2018-09-25 01:37:31 -04:00
2018-12-13 11:48:53 -05:00
// copy received data
rx_bytes = ringbuf_count(&self->rbuf);
rx_bytes = MIN(rx_bytes, len);
for ( uint16_t i = 0; i < rx_bytes; i++ ) {
data[i] = ringbuf_get(&self->rbuf);
2018-09-20 16:48:13 -04:00
}
2018-10-16 11:05:02 -04:00
2018-12-13 11:48:53 -05:00
NVIC_EnableIRQ(nrfx_get_irq_number(self->uarte.p_reg));
2018-09-25 01:37:31 -04:00
2018-12-13 11:48:53 -05:00
return rx_bytes;
}
// Write characters.
size_t common_hal_busio_uart_write (busio_uart_obj_t *self, const uint8_t *data, size_t len, int *errcode) {
2018-09-25 05:14:44 -04:00
if ( nrf_uarte_tx_pin_get(self->uarte.p_reg) == NRF_UARTE_PSEL_DISCONNECTED ) {
2018-09-24 05:06:44 -04:00
mp_raise_ValueError(translate("No TX pin"));
}
2018-09-24 03:37:28 -04:00
if ( len == 0 ) return 0;
2018-09-19 06:59:15 -04:00
2018-09-25 01:48:48 -04:00
uint64_t start_ticks = ticks_ms;
// Wait for on-going transfer to complete
while ( nrfx_uarte_tx_in_progress(&self->uarte) && (ticks_ms - start_ticks < self->timeout_ms) ) {
#ifdef MICROPY_VM_HOOK_LOOP
MICROPY_VM_HOOK_LOOP
#endif
}
// Time up
if ( !(ticks_ms - start_ticks < self->timeout_ms) ) {
*errcode = MP_EAGAIN;
return MP_STREAM_ERROR;
2018-09-24 04:54:32 -04:00
}
2018-09-24 05:18:49 -04:00
// EasyDMA can only access SRAM
uint8_t * tx_buf = (uint8_t*) data;
if ( !nrfx_is_in_ram(data) ) {
tx_buf = (uint8_t *) gc_alloc(len, false, false);
memcpy(tx_buf, data, len);
}
(*errcode) = nrfx_uarte_tx(&self->uarte, tx_buf, len);
2018-09-19 14:07:45 -04:00
_VERIFY_ERR(*errcode);
2018-09-19 06:59:15 -04:00
(*errcode) = 0;
2018-09-24 05:06:44 -04:00
while ( nrfx_uarte_tx_in_progress(&self->uarte) && (ticks_ms - start_ticks < self->timeout_ms) ) {
2018-09-19 06:59:15 -04:00
#ifdef MICROPY_VM_HOOK_LOOP
MICROPY_VM_HOOK_LOOP
#endif
}
2018-09-24 05:18:49 -04:00
if ( !nrfx_is_in_ram(data) ) {
gc_free(tx_buf);
}
2018-09-19 06:59:15 -04:00
return len;
}
uint32_t common_hal_busio_uart_get_baudrate(busio_uart_obj_t *self) {
return self->baudrate;
}
void common_hal_busio_uart_set_baudrate(busio_uart_obj_t *self, uint32_t baudrate) {
self->baudrate = baudrate;
2018-09-24 05:06:44 -04:00
nrf_uarte_baudrate_set(self->uarte.p_reg, get_nrf_baud(baudrate));
}
uint32_t common_hal_busio_uart_rx_characters_available(busio_uart_obj_t *self) {
2018-12-13 11:48:53 -05:00
return ringbuf_count(&self->rbuf);
}
void common_hal_busio_uart_clear_rx_buffer(busio_uart_obj_t *self) {
2018-12-13 11:48:53 -05:00
// prevent conflict with uart irq
NVIC_DisableIRQ(nrfx_get_irq_number(self->uarte.p_reg));
ringbuf_clear(&self->rbuf);
NVIC_EnableIRQ(nrfx_get_irq_number(self->uarte.p_reg));
}
bool common_hal_busio_uart_ready_to_tx(busio_uart_obj_t *self) {
2018-09-24 05:06:44 -04:00
return !nrfx_uarte_tx_in_progress(&self->uarte);
2018-09-19 06:59:15 -04:00
}
static uint32_t get_nrf_baud (uint32_t baudrate)
{
if ( baudrate <= 1200 ) {
2018-09-25 05:22:14 -04:00
return NRF_UARTE_BAUDRATE_1200;
2018-09-19 06:59:15 -04:00
}
else if ( baudrate <= 2400 ) {
2018-09-25 05:22:14 -04:00
return NRF_UARTE_BAUDRATE_2400;
2018-09-19 06:59:15 -04:00
}
else if ( baudrate <= 4800 ) {
2018-09-25 05:22:14 -04:00
return NRF_UARTE_BAUDRATE_4800;
2018-09-19 06:59:15 -04:00
}
else if ( baudrate <= 9600 ) {
2018-09-25 05:22:14 -04:00
return NRF_UARTE_BAUDRATE_9600;
2018-09-19 06:59:15 -04:00
}
else if ( baudrate <= 14400 ) {
2018-09-25 05:22:14 -04:00
return NRF_UARTE_BAUDRATE_14400;
2018-09-19 06:59:15 -04:00
}
else if ( baudrate <= 19200 ) {
2018-09-25 05:22:14 -04:00
return NRF_UARTE_BAUDRATE_19200;
2018-09-19 06:59:15 -04:00
}
else if ( baudrate <= 28800 ) {
2018-09-25 05:22:14 -04:00
return NRF_UARTE_BAUDRATE_28800;
2018-09-19 06:59:15 -04:00
}
else if ( baudrate <= 38400 ) {
2018-09-25 05:22:14 -04:00
return NRF_UARTE_BAUDRATE_38400;
2018-09-19 06:59:15 -04:00
}
else if ( baudrate <= 57600 ) {
2018-09-25 05:22:14 -04:00
return NRF_UARTE_BAUDRATE_57600;
2018-09-19 06:59:15 -04:00
}
else if ( baudrate <= 76800 ) {
2018-09-25 05:22:14 -04:00
return NRF_UARTE_BAUDRATE_76800;
2018-09-19 06:59:15 -04:00
}
else if ( baudrate <= 115200 ) {
2018-09-25 05:22:14 -04:00
return NRF_UARTE_BAUDRATE_115200;
2018-09-19 06:59:15 -04:00
}
else if ( baudrate <= 230400 ) {
2018-09-25 05:22:14 -04:00
return NRF_UARTE_BAUDRATE_230400;
2018-09-19 06:59:15 -04:00
}
else if ( baudrate <= 250000 ) {
2018-09-25 05:22:14 -04:00
return NRF_UARTE_BAUDRATE_250000;
2018-09-19 06:59:15 -04:00
}
else if ( baudrate <= 460800 ) {
2018-09-25 05:22:14 -04:00
return NRF_UARTE_BAUDRATE_460800;
2018-09-19 06:59:15 -04:00
}
else if ( baudrate <= 921600 ) {
2018-09-25 05:22:14 -04:00
return NRF_UARTE_BAUDRATE_921600;
2018-09-19 06:59:15 -04:00
}
else {
2018-09-25 05:22:14 -04:00
return NRF_UARTE_BAUDRATE_1000000;
2018-09-19 06:59:15 -04:00
}
}
2018-09-25 03:29:45 -04:00
#else
void common_hal_busio_uart_construct (busio_uart_obj_t *self,
const mcu_pin_obj_t * tx, const mcu_pin_obj_t * rx, uint32_t baudrate,
uint8_t bits, uart_parity_t parity, uint8_t stop, float timeout,
2018-09-25 03:29:45 -04:00
uint8_t receiver_buffer_size) {
mp_raise_NotImplementedError(translate("busio.UART not available"));
2018-09-25 03:29:45 -04:00
}
bool common_hal_busio_uart_deinited (busio_uart_obj_t *self) {
mp_raise_NotImplementedError(translate("busio.UART not available"));
2018-09-25 03:29:45 -04:00
return true;
}
void common_hal_busio_uart_deinit (busio_uart_obj_t *self) {
mp_raise_NotImplementedError(translate("busio.UART not available"));
2018-09-25 03:29:45 -04:00
}
// Read characters.
size_t common_hal_busio_uart_read (busio_uart_obj_t *self, uint8_t *data, size_t len, int *errcode) {
mp_raise_NotImplementedError(translate("busio.UART not available"));
2018-09-25 03:29:45 -04:00
return 0;
}
// Write characters.
size_t common_hal_busio_uart_write (busio_uart_obj_t *self, const uint8_t *data, size_t len, int *errcode) {
mp_raise_NotImplementedError(translate("busio.UART not available"));
2018-09-25 03:29:45 -04:00
return 0;
}
uint32_t common_hal_busio_uart_get_baudrate (busio_uart_obj_t *self) {
mp_raise_NotImplementedError(translate("busio.UART not available"));
2018-09-25 03:29:45 -04:00
return self->baudrate;
}
void common_hal_busio_uart_set_baudrate (busio_uart_obj_t *self, uint32_t baudrate) {
mp_raise_NotImplementedError(translate("busio.UART not available"));
2018-09-25 03:29:45 -04:00
}
uint32_t common_hal_busio_uart_rx_characters_available (busio_uart_obj_t *self) {
mp_raise_NotImplementedError(translate("busio.UART not available"));
2018-09-25 03:29:45 -04:00
}
void common_hal_busio_uart_clear_rx_buffer (busio_uart_obj_t *self) {
}
bool common_hal_busio_uart_ready_to_tx (busio_uart_obj_t *self) {
mp_raise_NotImplementedError(translate("busio.UART not available"));
2018-09-25 03:29:45 -04:00
return false;
}
#endif