2019-07-31 14:58:55 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2019-11-12 11:26:14 -05:00
|
|
|
* Copyright (c) 2019 Lucian Copeland for Adafruit Industries
|
2019-07-31 14:58:55 -04:00
|
|
|
*
|
|
|
|
* 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 "mpconfigport.h"
|
|
|
|
#include "lib/utils/interrupt_char.h"
|
|
|
|
#include "py/gc.h"
|
|
|
|
#include "py/mperrno.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/stream.h"
|
|
|
|
#include "supervisor/shared/translate.h"
|
|
|
|
|
|
|
|
#include "tick.h"
|
2019-11-27 12:52:11 -05:00
|
|
|
#include "stm32f4xx_hal.h"
|
2019-10-11 15:47:55 -04:00
|
|
|
|
2019-11-12 11:26:14 -05:00
|
|
|
#define ALL_UARTS 0xFFFF
|
|
|
|
|
2019-10-16 13:50:54 -04:00
|
|
|
STATIC bool reserved_uart[MAX_UART];
|
2019-11-01 17:00:01 -04:00
|
|
|
int errflag; //Used to restart read halts
|
2019-10-04 14:37:18 -04:00
|
|
|
|
2019-11-12 11:26:14 -05:00
|
|
|
STATIC void uart_clock_enable(uint16_t mask);
|
|
|
|
STATIC void uart_clock_disable(uint16_t mask);
|
|
|
|
STATIC void uart_assign_irq(busio_uart_obj_t* self, USART_TypeDef* USARTx);
|
|
|
|
|
2019-10-04 14:37:18 -04:00
|
|
|
void uart_reset(void) {
|
2019-11-12 11:26:14 -05:00
|
|
|
for (uint8_t i = 0; i < MAX_UART; i++) {
|
|
|
|
reserved_uart[i] = false;
|
|
|
|
MP_STATE_PORT(cpy_uart_obj_all)[i] = NULL;
|
|
|
|
}
|
|
|
|
uart_clock_disable(ALL_UARTS);
|
2019-10-04 14:37:18 -04:00
|
|
|
}
|
|
|
|
|
2019-11-27 12:52:11 -05:00
|
|
|
STATIC USART_TypeDef * assign_uart_or_throw(busio_uart_obj_t* self, bool pin_eval,
|
2019-10-16 13:50:54 -04:00
|
|
|
int uart_index, bool uart_taken) {
|
2019-11-12 11:26:14 -05:00
|
|
|
if (pin_eval) {
|
2019-10-16 13:50:54 -04:00
|
|
|
//assign a root pointer pointer for IRQ
|
|
|
|
MP_STATE_PORT(cpy_uart_obj_all)[uart_index] = self;
|
|
|
|
return mcu_uart_banks[uart_index];
|
|
|
|
} else {
|
|
|
|
if (uart_taken) {
|
2019-11-11 15:32:47 -05:00
|
|
|
mp_raise_ValueError(translate("Hardware in use, try alternative pins"));
|
2019-10-16 13:50:54 -04:00
|
|
|
} else {
|
|
|
|
mp_raise_ValueError(translate("Invalid UART pin selection"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 11:26:14 -05:00
|
|
|
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,
|
2019-07-31 14:58:55 -04:00
|
|
|
uint8_t bits, uart_parity_t parity, uint8_t stop, mp_float_t timeout,
|
2019-08-07 18:12:13 -04:00
|
|
|
uint16_t receiver_buffer_size) {
|
2019-10-04 11:42:38 -04:00
|
|
|
|
2019-10-04 14:37:18 -04:00
|
|
|
//match pins to UART objects
|
|
|
|
USART_TypeDef * USARTx;
|
|
|
|
|
|
|
|
uint8_t tx_len = sizeof(mcu_uart_tx_list)/sizeof(*mcu_uart_tx_list);
|
|
|
|
uint8_t rx_len = sizeof(mcu_uart_rx_list)/sizeof(*mcu_uart_rx_list);
|
|
|
|
bool uart_taken = false;
|
2019-11-12 11:26:14 -05:00
|
|
|
uint8_t uart_index = 0; //origin 0 corrected
|
2019-11-27 12:52:11 -05:00
|
|
|
|
2019-10-11 15:47:55 -04:00
|
|
|
//Can have both pins, or either
|
|
|
|
if ((tx != mp_const_none) && (rx != mp_const_none)) {
|
|
|
|
//normal find loop if both pins exist
|
2019-11-12 11:26:14 -05:00
|
|
|
for (uint i = 0; i < tx_len; i++) {
|
2019-10-11 15:47:55 -04:00
|
|
|
if (mcu_uart_tx_list[i].pin == tx) {
|
|
|
|
//rx
|
2019-11-12 11:26:14 -05:00
|
|
|
for (uint j = 0; j < rx_len; j++) {
|
2019-11-27 12:52:11 -05:00
|
|
|
if (mcu_uart_rx_list[j].pin == rx
|
2019-10-11 15:47:55 -04:00
|
|
|
&& mcu_uart_rx_list[j].uart_index == mcu_uart_tx_list[i].uart_index) {
|
|
|
|
//keep looking if the UART is taken, edge case
|
2019-11-12 11:26:14 -05:00
|
|
|
if (reserved_uart[mcu_uart_tx_list[i].uart_index - 1]) {
|
2019-10-11 15:47:55 -04:00
|
|
|
uart_taken = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
//store pins if not
|
|
|
|
self->tx = &mcu_uart_tx_list[i];
|
|
|
|
self->rx = &mcu_uart_rx_list[j];
|
|
|
|
break;
|
2019-10-04 14:37:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-12 11:26:14 -05:00
|
|
|
uart_index = self->tx->uart_index - 1;
|
2019-11-27 12:52:11 -05:00
|
|
|
USARTx = assign_uart_or_throw(self, (self->tx != NULL && self->rx != NULL),
|
2019-11-12 11:26:14 -05:00
|
|
|
uart_index, uart_taken);
|
|
|
|
} else if (tx == mp_const_none) {
|
2019-10-16 13:50:54 -04:00
|
|
|
//If there is no tx, run only rx
|
2019-11-12 11:26:14 -05:00
|
|
|
for (uint i = 0; i < rx_len; i++) {
|
2019-10-11 15:47:55 -04:00
|
|
|
if (mcu_uart_rx_list[i].pin == rx) {
|
|
|
|
//keep looking if the UART is taken, edge case
|
2019-11-12 11:26:14 -05:00
|
|
|
if (reserved_uart[mcu_uart_rx_list[i].uart_index - 1]) {
|
2019-10-11 15:47:55 -04:00
|
|
|
uart_taken = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
//store pins if not
|
|
|
|
self->rx = &mcu_uart_rx_list[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-11-12 11:26:14 -05:00
|
|
|
uart_index = self->rx->uart_index - 1;
|
2019-11-27 12:52:11 -05:00
|
|
|
USARTx = assign_uart_or_throw(self, (self->rx != NULL),
|
2019-11-12 11:26:14 -05:00
|
|
|
uart_index, uart_taken);
|
|
|
|
} else if (rx == mp_const_none) {
|
2019-10-16 13:50:54 -04:00
|
|
|
//If there is no rx, run only tx
|
2019-11-12 11:26:14 -05:00
|
|
|
for (uint i = 0; i < tx_len; i++) {
|
2019-10-11 15:47:55 -04:00
|
|
|
if (mcu_uart_tx_list[i].pin == tx) {
|
|
|
|
//keep looking if the UART is taken, edge case
|
2019-11-12 11:26:14 -05:00
|
|
|
if (reserved_uart[mcu_uart_tx_list[i].uart_index - 1]) {
|
2019-10-11 15:47:55 -04:00
|
|
|
uart_taken = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
//store pins if not
|
|
|
|
self->tx = &mcu_uart_tx_list[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-11-12 11:26:14 -05:00
|
|
|
uart_index = self->tx->uart_index - 1;
|
2019-11-27 12:52:11 -05:00
|
|
|
USARTx = assign_uart_or_throw(self, (self->tx != NULL),
|
2019-11-12 11:26:14 -05:00
|
|
|
uart_index, uart_taken);
|
2019-10-11 15:47:55 -04:00
|
|
|
} else {
|
|
|
|
//both pins cannot be empty
|
2019-11-11 15:32:47 -05:00
|
|
|
mp_raise_ValueError(translate("Supply at least one UART pin"));
|
2019-10-11 15:47:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
//Other errors
|
|
|
|
if ( receiver_buffer_size == 0 ) {
|
|
|
|
mp_raise_ValueError(translate("Invalid buffer size"));
|
|
|
|
}
|
|
|
|
if ( bits != 8 && bits != 9 ) {
|
|
|
|
mp_raise_ValueError(translate("Invalid word/bit length"));
|
2019-10-04 14:37:18 -04:00
|
|
|
}
|
2019-11-12 11:26:14 -05:00
|
|
|
if ( USARTx == NULL) { //this can only be hit if the periph file is wrong
|
|
|
|
mp_raise_ValueError(translate("Internal define error"));
|
|
|
|
}
|
2019-10-04 14:37:18 -04:00
|
|
|
|
2019-10-11 15:47:55 -04:00
|
|
|
//GPIO Init
|
2019-10-04 14:37:18 -04:00
|
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
2019-11-12 11:26:14 -05:00
|
|
|
if (self->tx != NULL) {
|
2019-10-11 15:47:55 -04:00
|
|
|
GPIO_InitStruct.Pin = pin_mask(tx->number);
|
|
|
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
|
|
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
|
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
|
|
|
GPIO_InitStruct.Alternate = self->tx->altfn_index;
|
|
|
|
HAL_GPIO_Init(pin_port(tx->port), &GPIO_InitStruct);
|
|
|
|
}
|
2019-11-12 11:26:14 -05:00
|
|
|
if (self->rx != NULL) {
|
2019-10-11 15:47:55 -04:00
|
|
|
GPIO_InitStruct.Pin = pin_mask(rx->number);
|
|
|
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
|
|
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
|
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
2019-11-27 12:52:11 -05:00
|
|
|
GPIO_InitStruct.Alternate = self->rx->altfn_index;
|
2019-10-11 15:47:55 -04:00
|
|
|
HAL_GPIO_Init(pin_port(rx->port), &GPIO_InitStruct);
|
|
|
|
}
|
2019-10-04 14:37:18 -04:00
|
|
|
|
2019-11-12 11:26:14 -05:00
|
|
|
//reserve uart and enable the peripheral
|
|
|
|
reserved_uart[uart_index] = true;
|
|
|
|
uart_clock_enable(1 << (uart_index));
|
|
|
|
uart_assign_irq(self, USARTx);
|
2019-10-11 15:47:55 -04:00
|
|
|
|
2019-10-04 14:37:18 -04:00
|
|
|
self->handle.Instance = USARTx;
|
2019-10-11 15:47:55 -04:00
|
|
|
self->handle.Init.BaudRate = baudrate;
|
|
|
|
self->handle.Init.WordLength = (bits == 9) ? UART_WORDLENGTH_9B : UART_WORDLENGTH_8B;
|
2019-11-12 11:26:14 -05:00
|
|
|
self->handle.Init.StopBits = (stop > 1) ? UART_STOPBITS_2 : UART_STOPBITS_1;
|
2019-11-27 12:52:11 -05:00
|
|
|
self->handle.Init.Parity = (parity == PARITY_ODD) ? UART_PARITY_ODD :
|
2019-11-12 11:26:14 -05:00
|
|
|
(parity == PARITY_EVEN) ? UART_PARITY_EVEN :
|
2019-10-11 15:47:55 -04:00
|
|
|
UART_PARITY_NONE;
|
|
|
|
self->handle.Init.Mode = (self->tx != NULL && self->rx != NULL) ? UART_MODE_TX_RX :
|
|
|
|
(self->tx != NULL) ? UART_MODE_TX :
|
|
|
|
UART_MODE_RX;
|
2019-10-04 14:37:18 -04:00
|
|
|
self->handle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
|
|
|
self->handle.Init.OverSampling = UART_OVERSAMPLING_16;
|
|
|
|
if (HAL_UART_Init(&self->handle) != HAL_OK)
|
2019-10-04 11:42:38 -04:00
|
|
|
{
|
2019-10-04 14:37:18 -04:00
|
|
|
mp_raise_ValueError(translate("UART Init Error"));
|
2019-10-07 14:22:12 -04:00
|
|
|
|
2019-10-04 11:42:38 -04:00
|
|
|
}
|
2019-10-08 16:03:51 -04:00
|
|
|
|
2019-10-11 15:47:55 -04:00
|
|
|
// Init buffer for rx and claim pins
|
|
|
|
if (self->rx != NULL) {
|
|
|
|
ringbuf_alloc(&self->rbuf, receiver_buffer_size, true);
|
|
|
|
if (!self->rbuf.buf) {
|
|
|
|
mp_raise_ValueError(translate("UART Buffer allocation error"));
|
|
|
|
}
|
|
|
|
claim_pin(rx);
|
|
|
|
}
|
|
|
|
if (self->tx != NULL) {
|
|
|
|
claim_pin(tx);
|
|
|
|
}
|
|
|
|
self->baudrate = baudrate;
|
|
|
|
self->timeout_ms = timeout * 1000;
|
|
|
|
|
2019-10-30 16:59:53 -04:00
|
|
|
//start the interrupt series
|
|
|
|
if ((HAL_UART_GetState(&self->handle) & HAL_UART_STATE_BUSY_RX) == HAL_UART_STATE_BUSY_RX) {
|
|
|
|
mp_raise_ValueError(translate("Could not start interrupt, RX busy"));
|
2019-10-11 15:47:55 -04:00
|
|
|
}
|
2019-10-30 16:59:53 -04:00
|
|
|
|
2019-11-11 15:32:47 -05:00
|
|
|
//start the receive interrupt chain
|
2019-11-01 17:00:01 -04:00
|
|
|
HAL_NVIC_DisableIRQ(self->irq); //prevent handle lock contention
|
2019-10-30 16:59:53 -04:00
|
|
|
HAL_UART_Receive_IT(&self->handle, &self->rx_char, 1);
|
|
|
|
HAL_NVIC_SetPriority(self->irq, UART_IRQPRI, UART_IRQSUB_PRI);
|
|
|
|
HAL_NVIC_EnableIRQ(self->irq);
|
|
|
|
|
2019-11-01 17:00:01 -04:00
|
|
|
errflag = HAL_OK;
|
2019-07-31 14:58:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_busio_uart_deinited(busio_uart_obj_t *self) {
|
2019-10-08 16:03:51 -04:00
|
|
|
return self->tx->pin == mp_const_none;
|
2019-07-31 14:58:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
|
2019-11-12 11:26:14 -05:00
|
|
|
if (common_hal_busio_uart_deinited(self)) return;
|
2019-11-27 12:52:11 -05:00
|
|
|
|
2019-10-08 16:03:51 -04:00
|
|
|
reset_pin_number(self->tx->pin->port,self->tx->pin->number);
|
|
|
|
reset_pin_number(self->rx->pin->port,self->rx->pin->number);
|
|
|
|
self->tx = mp_const_none;
|
|
|
|
self->rx = mp_const_none;
|
2019-10-21 16:24:32 -04:00
|
|
|
gc_free(self->rbuf.buf);
|
|
|
|
self->rbuf.size = 0;
|
|
|
|
self->rbuf.iput = self->rbuf.iget = 0;
|
2019-07-31 14:58:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t len, int *errcode) {
|
2019-10-16 12:32:34 -04:00
|
|
|
if (self->rx == NULL) {
|
|
|
|
mp_raise_ValueError(translate("No RX pin"));
|
|
|
|
}
|
2019-10-11 15:47:55 -04:00
|
|
|
|
|
|
|
size_t rx_bytes = 0;
|
2019-11-22 15:30:08 -05:00
|
|
|
uint64_t start_ticks = supervisor_ticks_ms64();
|
2019-10-11 15:47:55 -04:00
|
|
|
|
|
|
|
// Wait for all bytes received or timeout, same as nrf
|
2019-11-22 15:30:08 -05:00
|
|
|
while ( (ringbuf_count(&self->rbuf) < len) && (supervisor_ticks_ms64() - start_ticks < self->timeout_ms) ) {
|
2019-10-11 15:47:55 -04:00
|
|
|
RUN_BACKGROUND_TASKS;
|
2019-10-30 16:59:53 -04:00
|
|
|
//restart if it failed in the callback
|
2019-11-12 11:26:14 -05:00
|
|
|
if (errflag != HAL_OK) {
|
2019-10-30 16:59:53 -04:00
|
|
|
errflag = HAL_UART_Receive_IT(&self->handle, &self->rx_char, 1);
|
|
|
|
}
|
2019-10-11 15:47:55 -04:00
|
|
|
// Allow user to break out of a timeout with a KeyboardInterrupt.
|
|
|
|
if ( mp_hal_is_interrupted() ) {
|
|
|
|
return 0;
|
2019-10-08 16:03:51 -04:00
|
|
|
}
|
2019-10-04 14:37:18 -04:00
|
|
|
}
|
2019-10-08 16:03:51 -04:00
|
|
|
|
2019-10-11 15:47:55 -04:00
|
|
|
// Halt reception
|
2019-10-30 16:59:53 -04:00
|
|
|
HAL_NVIC_DisableIRQ(self->irq);
|
2019-10-11 15:47:55 -04:00
|
|
|
// copy received data
|
|
|
|
rx_bytes = ringbuf_count(&self->rbuf);
|
|
|
|
rx_bytes = MIN(rx_bytes, len);
|
2019-11-12 11:26:14 -05:00
|
|
|
for (uint16_t i = 0; i < rx_bytes; i++) {
|
2019-10-11 15:47:55 -04:00
|
|
|
data[i] = ringbuf_get(&self->rbuf);
|
|
|
|
}
|
2019-10-30 16:59:53 -04:00
|
|
|
HAL_NVIC_EnableIRQ(self->irq);
|
|
|
|
|
2019-10-11 15:47:55 -04:00
|
|
|
if (rx_bytes == 0) {
|
2019-10-08 16:03:51 -04:00
|
|
|
*errcode = EAGAIN;
|
|
|
|
return MP_STREAM_ERROR;
|
|
|
|
}
|
2019-11-27 12:52:11 -05:00
|
|
|
return rx_bytes;
|
2019-07-31 14:58:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write characters.
|
|
|
|
size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data, size_t len, int *errcode) {
|
2019-10-16 12:32:34 -04:00
|
|
|
if (self->tx == NULL) {
|
|
|
|
mp_raise_ValueError(translate("No TX pin"));
|
|
|
|
}
|
2019-11-12 11:26:14 -05:00
|
|
|
bool write_err = false; //write error shouldn't disable interrupts
|
2019-10-16 12:32:34 -04:00
|
|
|
|
2019-11-01 17:00:01 -04:00
|
|
|
HAL_NVIC_DisableIRQ(self->irq);
|
2019-11-27 12:52:11 -05:00
|
|
|
if (HAL_UART_Transmit(&self->handle, (uint8_t*)data, len, HAL_MAX_DELAY) != HAL_OK) {
|
2019-11-12 11:26:14 -05:00
|
|
|
write_err = true;
|
2019-10-04 14:37:18 -04:00
|
|
|
}
|
2019-11-01 17:00:01 -04:00
|
|
|
HAL_UART_Receive_IT(&self->handle, &self->rx_char, 1);
|
|
|
|
HAL_NVIC_EnableIRQ(self->irq);
|
|
|
|
|
2019-11-27 12:52:11 -05:00
|
|
|
if (write_err) {
|
|
|
|
mp_raise_ValueError(translate("UART write error"));
|
|
|
|
}
|
2019-11-01 17:00:01 -04:00
|
|
|
return len;
|
2019-07-31 14:58:55 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 15:47:55 -04:00
|
|
|
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *handle)
|
|
|
|
{
|
2019-11-12 11:26:14 -05:00
|
|
|
for (int i = 0; i < 7; i++) {
|
2019-10-16 11:33:55 -04:00
|
|
|
//get context pointer and cast it as struct pointer
|
2019-11-12 11:26:14 -05:00
|
|
|
busio_uart_obj_t * context = (busio_uart_obj_t*)MP_STATE_PORT(cpy_uart_obj_all)[i];
|
|
|
|
if (handle == &context->handle) {
|
2019-10-30 16:59:53 -04:00
|
|
|
//check if transaction is ongoing
|
2019-11-12 11:26:14 -05:00
|
|
|
if ((HAL_UART_GetState(handle) & HAL_UART_STATE_BUSY_RX) == HAL_UART_STATE_BUSY_RX) {
|
2019-10-30 16:59:53 -04:00
|
|
|
return;
|
2019-10-21 16:24:32 -04:00
|
|
|
}
|
2019-10-30 16:59:53 -04:00
|
|
|
ringbuf_put_n(&context->rbuf, &context->rx_char, 1);
|
|
|
|
errflag = HAL_UART_Receive_IT(handle, &context->rx_char, 1);
|
2019-11-27 12:52:11 -05:00
|
|
|
|
2019-10-30 16:59:53 -04:00
|
|
|
return;
|
2019-10-11 15:47:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle)
|
|
|
|
{
|
2019-10-30 16:59:53 -04:00
|
|
|
if (__HAL_UART_GET_FLAG(UartHandle, UART_FLAG_PE) != RESET) {
|
|
|
|
__HAL_UART_CLEAR_PEFLAG(UartHandle);
|
|
|
|
} else if (__HAL_UART_GET_FLAG(UartHandle, UART_FLAG_FE) != RESET) {
|
|
|
|
__HAL_UART_CLEAR_FEFLAG(UartHandle);
|
|
|
|
} else if (__HAL_UART_GET_FLAG(UartHandle, UART_FLAG_NE) != RESET) {
|
|
|
|
__HAL_UART_CLEAR_NEFLAG(UartHandle);
|
|
|
|
} else if (__HAL_UART_GET_FLAG(UartHandle, UART_FLAG_ORE) != RESET) {
|
|
|
|
__HAL_UART_CLEAR_OREFLAG(UartHandle);
|
|
|
|
}
|
|
|
|
//restart serial read after an error
|
2019-11-12 11:26:14 -05:00
|
|
|
for (int i = 0; i < 7; i++) {
|
2019-10-30 16:59:53 -04:00
|
|
|
busio_uart_obj_t * context = (busio_uart_obj_t *)MP_STATE_PORT(cpy_uart_obj_all)[i];
|
2019-11-12 11:26:14 -05:00
|
|
|
if (UartHandle == &context->handle) {
|
2019-10-30 16:59:53 -04:00
|
|
|
HAL_UART_Receive_IT(UartHandle, &context->rx_char, 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 15:47:55 -04:00
|
|
|
}
|
|
|
|
|
2019-07-31 14:58:55 -04:00
|
|
|
uint32_t common_hal_busio_uart_get_baudrate(busio_uart_obj_t *self) {
|
2019-10-11 15:47:55 -04:00
|
|
|
return self->baudrate;
|
2019-07-31 14:58:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_busio_uart_set_baudrate(busio_uart_obj_t *self, uint32_t baudrate) {
|
2019-10-11 15:47:55 -04:00
|
|
|
//Don't reset if it's the same value
|
|
|
|
if (baudrate == self->baudrate) return;
|
|
|
|
|
|
|
|
//Otherwise de-init and set new rate
|
2019-11-12 11:26:14 -05:00
|
|
|
if (HAL_UART_DeInit(&self->handle) != HAL_OK) {
|
2019-10-11 15:47:55 -04:00
|
|
|
mp_raise_ValueError(translate("UART De-init error"));
|
|
|
|
}
|
|
|
|
self->handle.Init.BaudRate = baudrate;
|
2019-11-12 11:26:14 -05:00
|
|
|
if (HAL_UART_Init(&self->handle) != HAL_OK) {
|
2019-10-11 15:47:55 -04:00
|
|
|
mp_raise_ValueError(translate("UART Re-init error"));
|
|
|
|
}
|
|
|
|
|
|
|
|
self->baudrate = baudrate;
|
2019-07-31 14:58:55 -04:00
|
|
|
}
|
|
|
|
|
2019-11-27 12:52:11 -05:00
|
|
|
mp_float_t common_hal_busio_uart_get_timeout(busio_uart_obj_t *self) {
|
|
|
|
return (mp_float_t) (self->timeout_ms / 1000.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_busio_uart_set_timeout(busio_uart_obj_t *self, mp_float_t timeout) {
|
|
|
|
self->timeout_ms = timeout * 1000;
|
|
|
|
}
|
|
|
|
|
2019-07-31 14:58:55 -04:00
|
|
|
uint32_t common_hal_busio_uart_rx_characters_available(busio_uart_obj_t *self) {
|
2019-10-11 15:47:55 -04:00
|
|
|
return ringbuf_count(&self->rbuf);
|
2019-07-31 14:58:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_busio_uart_clear_rx_buffer(busio_uart_obj_t *self) {
|
2019-10-11 15:47:55 -04:00
|
|
|
// Halt reception
|
2019-10-30 16:59:53 -04:00
|
|
|
HAL_NVIC_DisableIRQ(self->irq);
|
2019-10-11 15:47:55 -04:00
|
|
|
ringbuf_clear(&self->rbuf);
|
2019-10-30 16:59:53 -04:00
|
|
|
HAL_NVIC_EnableIRQ(self->irq);
|
2019-07-31 14:58:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_busio_uart_ready_to_tx(busio_uart_obj_t *self) {
|
2019-11-12 11:26:14 -05:00
|
|
|
return __HAL_UART_GET_FLAG(&self->handle, UART_FLAG_TXE);
|
2019-07-31 14:58:55 -04:00
|
|
|
}
|
2019-10-16 11:33:55 -04:00
|
|
|
|
2019-10-21 16:24:32 -04:00
|
|
|
STATIC void call_hal_irq(int uart_num) {
|
2019-10-16 11:33:55 -04:00
|
|
|
//Create casted context pointer
|
2019-11-12 11:26:14 -05:00
|
|
|
busio_uart_obj_t * context = (busio_uart_obj_t*)MP_STATE_PORT(cpy_uart_obj_all)[uart_num - 1];
|
|
|
|
if (context != NULL) {
|
2019-10-30 16:59:53 -04:00
|
|
|
HAL_NVIC_ClearPendingIRQ(context->irq);
|
2019-10-16 11:33:55 -04:00
|
|
|
HAL_UART_IRQHandler(&context->handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UART/USART IRQ handlers
|
|
|
|
void USART1_IRQHandler(void) {
|
|
|
|
call_hal_irq(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void USART2_IRQHandler(void) {
|
|
|
|
call_hal_irq(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void USART3_IRQHandler(void) {
|
|
|
|
call_hal_irq(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UART4_IRQHandler(void) {
|
|
|
|
call_hal_irq(4);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UART5_IRQHandler(void) {
|
|
|
|
call_hal_irq(5);
|
|
|
|
}
|
|
|
|
|
|
|
|
void USART6_IRQHandler(void) {
|
|
|
|
call_hal_irq(6);
|
|
|
|
}
|
2019-11-12 11:26:14 -05:00
|
|
|
|
|
|
|
STATIC void uart_clock_enable(uint16_t mask) {
|
|
|
|
#ifdef USART1
|
|
|
|
if (mask & (1 << 0)) {
|
|
|
|
__HAL_RCC_USART1_FORCE_RESET();
|
|
|
|
__HAL_RCC_USART1_RELEASE_RESET();
|
2019-11-27 12:52:11 -05:00
|
|
|
__HAL_RCC_USART1_CLK_ENABLE();
|
2019-11-12 11:26:14 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef USART2
|
|
|
|
if (mask & (1 << 1)) {
|
|
|
|
__HAL_RCC_USART2_FORCE_RESET();
|
|
|
|
__HAL_RCC_USART2_RELEASE_RESET();
|
2019-11-27 12:52:11 -05:00
|
|
|
__HAL_RCC_USART2_CLK_ENABLE();
|
2019-11-12 11:26:14 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef USART3
|
|
|
|
if (mask & (1 << 2)) {
|
|
|
|
__HAL_RCC_USART3_FORCE_RESET();
|
|
|
|
__HAL_RCC_USART3_RELEASE_RESET();
|
2019-11-27 12:52:11 -05:00
|
|
|
__HAL_RCC_USART3_CLK_ENABLE();
|
2019-11-12 11:26:14 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef UART4
|
|
|
|
if (mask & (1 << 3)) {
|
|
|
|
__HAL_RCC_UART4_FORCE_RESET();
|
|
|
|
__HAL_RCC_UART4_RELEASE_RESET();
|
2019-11-27 12:52:11 -05:00
|
|
|
__HAL_RCC_UART4_CLK_ENABLE();
|
2019-11-12 11:26:14 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef UART5
|
|
|
|
if (mask & (1 << 4)) {
|
|
|
|
__HAL_RCC_UART5_FORCE_RESET();
|
|
|
|
__HAL_RCC_UART5_RELEASE_RESET();
|
2019-11-27 12:52:11 -05:00
|
|
|
__HAL_RCC_UART5_CLK_ENABLE();
|
2019-11-12 11:26:14 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef USART6
|
|
|
|
if (mask & (1 << 5)) {
|
|
|
|
__HAL_RCC_USART6_FORCE_RESET();
|
|
|
|
__HAL_RCC_USART6_RELEASE_RESET();
|
2019-11-27 12:52:11 -05:00
|
|
|
__HAL_RCC_USART6_CLK_ENABLE();
|
|
|
|
}
|
2019-11-12 11:26:14 -05:00
|
|
|
#endif
|
|
|
|
#ifdef UART7
|
|
|
|
if (mask & (1 << 6)) {
|
|
|
|
__HAL_RCC_UART7_FORCE_RESET();
|
|
|
|
__HAL_RCC_UART7_RELEASE_RESET();
|
2019-11-27 12:52:11 -05:00
|
|
|
__HAL_RCC_UART7_CLK_ENABLE();
|
|
|
|
}
|
2019-11-12 11:26:14 -05:00
|
|
|
#endif
|
|
|
|
#ifdef UART8
|
|
|
|
if (mask & (1 << 7)) {
|
|
|
|
__HAL_RCC_UART8_FORCE_RESET();
|
|
|
|
__HAL_RCC_UART8_RELEASE_RESET();
|
2019-11-27 12:52:11 -05:00
|
|
|
__HAL_RCC_UART8_CLK_ENABLE();
|
|
|
|
}
|
2019-11-12 11:26:14 -05:00
|
|
|
#endif
|
|
|
|
#ifdef UART9
|
|
|
|
if (mask & (1 << 8)) {
|
|
|
|
__HAL_RCC_UART9_FORCE_RESET();
|
|
|
|
__HAL_RCC_UART9_RELEASE_RESET();
|
2019-11-27 12:52:11 -05:00
|
|
|
__HAL_RCC_UART9_CLK_ENABLE();
|
|
|
|
}
|
2019-11-12 11:26:14 -05:00
|
|
|
#endif
|
|
|
|
#ifdef UART10
|
|
|
|
if (mask & (1 << 9)) {
|
|
|
|
__HAL_RCC_UART10_FORCE_RESET();
|
|
|
|
__HAL_RCC_UART10_RELEASE_RESET();
|
2019-11-27 12:52:11 -05:00
|
|
|
__HAL_RCC_UART10_CLK_ENABLE();
|
|
|
|
}
|
2019-11-12 11:26:14 -05:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void uart_clock_disable(uint16_t mask) {
|
|
|
|
#ifdef USART1
|
|
|
|
if (mask & (1 << 0)) {
|
|
|
|
__HAL_RCC_USART1_FORCE_RESET();
|
|
|
|
__HAL_RCC_USART1_RELEASE_RESET();
|
2019-11-27 12:52:11 -05:00
|
|
|
__HAL_RCC_USART1_CLK_DISABLE();
|
2019-11-12 11:26:14 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef USART2
|
|
|
|
if (mask & (1 << 1)) {
|
|
|
|
__HAL_RCC_USART2_FORCE_RESET();
|
|
|
|
__HAL_RCC_USART2_RELEASE_RESET();
|
2019-11-27 12:52:11 -05:00
|
|
|
__HAL_RCC_USART2_CLK_DISABLE();
|
2019-11-12 11:26:14 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef USART3
|
|
|
|
if (mask & (1 << 2)) {
|
|
|
|
__HAL_RCC_USART3_FORCE_RESET();
|
|
|
|
__HAL_RCC_USART3_RELEASE_RESET();
|
2019-11-27 12:52:11 -05:00
|
|
|
__HAL_RCC_USART3_CLK_DISABLE();
|
2019-11-12 11:26:14 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef UART4
|
|
|
|
if (mask & (1 << 3)) {
|
|
|
|
__HAL_RCC_UART4_FORCE_RESET();
|
|
|
|
__HAL_RCC_UART4_RELEASE_RESET();
|
2019-11-27 12:52:11 -05:00
|
|
|
__HAL_RCC_UART4_CLK_DISABLE();
|
2019-11-12 11:26:14 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef UART5
|
|
|
|
if (mask & (1 << 4)) {
|
|
|
|
__HAL_RCC_UART5_FORCE_RESET();
|
|
|
|
__HAL_RCC_UART5_RELEASE_RESET();
|
2019-11-27 12:52:11 -05:00
|
|
|
__HAL_RCC_UART5_CLK_DISABLE();
|
2019-11-12 11:26:14 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef USART6
|
|
|
|
if (mask & (1 << 5)) {
|
|
|
|
__HAL_RCC_USART6_FORCE_RESET();
|
|
|
|
__HAL_RCC_USART6_RELEASE_RESET();
|
2019-11-27 12:52:11 -05:00
|
|
|
__HAL_RCC_USART6_CLK_DISABLE();
|
|
|
|
}
|
2019-11-12 11:26:14 -05:00
|
|
|
#endif
|
|
|
|
#ifdef UART7
|
|
|
|
if (mask & (1 << 6)) {
|
|
|
|
__HAL_RCC_UART7_FORCE_RESET();
|
|
|
|
__HAL_RCC_UART7_RELEASE_RESET();
|
2019-11-27 12:52:11 -05:00
|
|
|
__HAL_RCC_UART7_CLK_DISABLE();
|
|
|
|
}
|
2019-11-12 11:26:14 -05:00
|
|
|
#endif
|
|
|
|
#ifdef UART8
|
|
|
|
if (mask & (1 << 7)) {
|
|
|
|
__HAL_RCC_UART8_FORCE_RESET();
|
|
|
|
__HAL_RCC_UART8_RELEASE_RESET();
|
2019-11-27 12:52:11 -05:00
|
|
|
__HAL_RCC_UART8_CLK_DISABLE();
|
|
|
|
}
|
2019-11-12 11:26:14 -05:00
|
|
|
#endif
|
|
|
|
#ifdef UART9
|
|
|
|
if (mask & (1 << 8)) {
|
|
|
|
__HAL_RCC_UART9_FORCE_RESET();
|
|
|
|
__HAL_RCC_UART9_RELEASE_RESET();
|
2019-11-27 12:52:11 -05:00
|
|
|
__HAL_RCC_UART9_CLK_DISABLE();
|
|
|
|
}
|
2019-11-12 11:26:14 -05:00
|
|
|
#endif
|
|
|
|
#ifdef UART10
|
|
|
|
if (mask & (1 << 9)) {
|
|
|
|
__HAL_RCC_UART10_FORCE_RESET();
|
|
|
|
__HAL_RCC_UART10_RELEASE_RESET();
|
2019-11-27 12:52:11 -05:00
|
|
|
__HAL_RCC_UART10_CLK_DISABLE();
|
|
|
|
}
|
2019-11-12 11:26:14 -05:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void uart_assign_irq(busio_uart_obj_t *self, USART_TypeDef * USARTx) {
|
|
|
|
#ifdef USART1
|
|
|
|
if (USARTx == USART1) self->irq = USART1_IRQn;
|
|
|
|
#endif
|
|
|
|
#ifdef USART2
|
|
|
|
if (USARTx == USART2) self->irq = USART2_IRQn;
|
|
|
|
#endif
|
|
|
|
#ifdef USART3
|
|
|
|
if (USARTx == USART3) self->irq = USART3_IRQn;
|
|
|
|
#endif
|
|
|
|
#ifdef UART4
|
|
|
|
if (USARTx == UART4) self->irq = UART4_IRQn;
|
|
|
|
#endif
|
|
|
|
#ifdef UART5
|
|
|
|
if (USARTx == UART5) self->irq = UART5_IRQn;
|
|
|
|
#endif
|
|
|
|
#ifdef USART6
|
|
|
|
if (USARTx == USART6) self->irq = USART6_IRQn;
|
|
|
|
#endif
|
|
|
|
#ifdef UART7
|
|
|
|
if (USARTx == UART7) self->irq = UART7_IRQn;
|
|
|
|
#endif
|
|
|
|
#ifdef UART8
|
|
|
|
if (USARTx == UART8) self->irq = UART8_IRQn;
|
|
|
|
#endif
|
|
|
|
#ifdef UART9
|
|
|
|
if (USARTx == UART9) self->irq = UART9_IRQn;
|
|
|
|
#endif
|
|
|
|
#ifdef UART10
|
|
|
|
if (USARTx == UART10) self->irq = UART10_IRQn;
|
|
|
|
#endif
|
|
|
|
}
|