2016-11-16 15:38:25 -05:00
|
|
|
/*
|
2017-08-04 12:05:38 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2016-11-16 15:38:25 -05:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016 Glenn Ruben Bakke
|
|
|
|
*
|
|
|
|
* 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 <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2017-01-08 10:40:18 -05:00
|
|
|
#include "nrf.h"
|
2016-11-16 15:38:25 -05:00
|
|
|
#include "mphalport.h"
|
|
|
|
#include "hal_uart.h"
|
2017-12-21 07:49:14 -05:00
|
|
|
#include "fifo.h"
|
2016-11-16 15:38:25 -05:00
|
|
|
|
2017-12-29 10:43:31 -05:00
|
|
|
#include "lib/utils/interrupt_char.h"
|
|
|
|
|
2016-12-13 14:27:26 -05:00
|
|
|
#ifdef HAL_UART_MODULE_ENABLED
|
|
|
|
|
2017-12-21 07:49:14 -05:00
|
|
|
FIFO_DEF(_ff_uart, 128, uint8_t, true, UARTE0_UART0_IRQn);
|
|
|
|
|
2016-11-16 15:38:25 -05:00
|
|
|
uint32_t hal_uart_baudrate_lookup[] = {
|
|
|
|
UART_BAUDRATE_BAUDRATE_Baud1200, ///< 1200 baud.
|
|
|
|
UART_BAUDRATE_BAUDRATE_Baud2400, ///< 2400 baud.
|
|
|
|
UART_BAUDRATE_BAUDRATE_Baud4800, ///< 4800 baud.
|
|
|
|
UART_BAUDRATE_BAUDRATE_Baud9600, ///< 9600 baud.
|
|
|
|
UART_BAUDRATE_BAUDRATE_Baud14400, ///< 14400 baud.
|
|
|
|
UART_BAUDRATE_BAUDRATE_Baud19200, ///< 19200 baud.
|
|
|
|
UART_BAUDRATE_BAUDRATE_Baud28800, ///< 28800 baud.
|
|
|
|
UART_BAUDRATE_BAUDRATE_Baud38400, ///< 38400 baud.
|
|
|
|
UART_BAUDRATE_BAUDRATE_Baud57600, ///< 57600 baud.
|
|
|
|
UART_BAUDRATE_BAUDRATE_Baud76800, ///< 76800 baud.
|
|
|
|
UART_BAUDRATE_BAUDRATE_Baud115200, ///< 115200 baud.
|
|
|
|
UART_BAUDRATE_BAUDRATE_Baud230400, ///< 230400 baud.
|
|
|
|
UART_BAUDRATE_BAUDRATE_Baud250000, ///< 250000 baud.
|
|
|
|
UART_BAUDRATE_BAUDRATE_Baud460800, ///< 460800 baud.
|
|
|
|
UART_BAUDRATE_BAUDRATE_Baud921600, ///< 921600 baud.
|
|
|
|
UART_BAUDRATE_BAUDRATE_Baud1M, ///< 1000000 baud.
|
|
|
|
};
|
|
|
|
|
2017-03-10 16:21:19 -05:00
|
|
|
hal_uart_error_t hal_uart_char_write(NRF_UART_Type * p_instance, uint8_t ch) {
|
|
|
|
p_instance->ERRORSRC = 0;
|
|
|
|
p_instance->TXD = (uint8_t)ch;
|
|
|
|
while (p_instance->EVENTS_TXDRDY != 1) {
|
2016-11-16 15:38:25 -05:00
|
|
|
// Blocking wait.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the TX flag.
|
2017-03-10 16:21:19 -05:00
|
|
|
p_instance->EVENTS_TXDRDY = 0;
|
|
|
|
|
|
|
|
return p_instance->ERRORSRC;
|
2016-11-16 15:38:25 -05:00
|
|
|
}
|
|
|
|
|
2017-03-10 16:21:19 -05:00
|
|
|
hal_uart_error_t hal_uart_char_read(NRF_UART_Type * p_instance, uint8_t * ch) {
|
2017-12-21 07:49:14 -05:00
|
|
|
while ( !fifo_read(_ff_uart, ch) ) {
|
|
|
|
// wait for fifo data
|
|
|
|
}
|
|
|
|
|
|
|
|
return HAL_UART_ERROR_NONE;
|
2016-11-16 15:38:25 -05:00
|
|
|
}
|
|
|
|
|
2017-03-10 16:21:19 -05:00
|
|
|
hal_uart_error_t hal_uart_buffer_write(NRF_UART_Type * p_instance, uint8_t * p_buffer, uint32_t num_of_bytes, uart_complete_cb cb) {
|
2016-11-16 15:38:25 -05:00
|
|
|
int i = 0;
|
2017-03-10 16:21:19 -05:00
|
|
|
hal_uart_error_t err = 0;
|
2016-11-16 15:38:25 -05:00
|
|
|
uint8_t ch = p_buffer[i++];
|
|
|
|
while (i < num_of_bytes) {
|
2017-03-10 16:21:19 -05:00
|
|
|
err = hal_uart_char_write(p_instance, ch);
|
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
2016-11-16 15:38:25 -05:00
|
|
|
ch = p_buffer[i++];
|
|
|
|
}
|
|
|
|
cb();
|
2017-03-10 16:21:19 -05:00
|
|
|
return err;
|
2016-11-16 15:38:25 -05:00
|
|
|
}
|
|
|
|
|
2017-03-10 16:21:19 -05:00
|
|
|
hal_uart_error_t hal_uart_buffer_read(NRF_UART_Type * p_instance, uint8_t * p_buffer, uint32_t num_of_bytes, uart_complete_cb cb) {
|
2016-11-16 15:38:25 -05:00
|
|
|
int i = 0;
|
2017-03-10 16:21:19 -05:00
|
|
|
hal_uart_error_t err = 0;
|
2016-11-16 15:38:25 -05:00
|
|
|
while (i < num_of_bytes) {
|
2017-03-10 16:21:19 -05:00
|
|
|
hal_uart_error_t err = hal_uart_char_read(p_instance, &p_buffer[i]);
|
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
2016-11-16 15:38:25 -05:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
cb();
|
2017-03-10 16:21:19 -05:00
|
|
|
return err;
|
2016-11-16 15:38:25 -05:00
|
|
|
}
|
|
|
|
|
2017-12-21 07:49:14 -05:00
|
|
|
int hal_uart_available(NRF_UART_Type * p_instance)
|
|
|
|
{
|
|
|
|
return fifo_count(_ff_uart);
|
|
|
|
}
|
|
|
|
|
2017-03-10 16:21:19 -05:00
|
|
|
void hal_uart_init(NRF_UART_Type * p_instance, hal_uart_init_t const * p_uart_init) {
|
2017-01-26 15:58:12 -05:00
|
|
|
hal_gpio_cfg_pin(p_uart_init->tx_pin->port, p_uart_init->tx_pin->pin, HAL_GPIO_MODE_OUTPUT, HAL_GPIO_PULL_DISABLED);
|
|
|
|
hal_gpio_cfg_pin(p_uart_init->tx_pin->port, p_uart_init->rx_pin->pin, HAL_GPIO_MODE_INPUT, HAL_GPIO_PULL_DISABLED);
|
2017-01-08 10:40:18 -05:00
|
|
|
|
2017-01-26 15:58:12 -05:00
|
|
|
hal_gpio_pin_clear(p_uart_init->tx_pin->port, p_uart_init->tx_pin->pin);
|
|
|
|
|
2017-03-10 16:21:19 -05:00
|
|
|
p_instance->PSELTXD = p_uart_init->tx_pin->pin;
|
|
|
|
p_instance->PSELRXD = p_uart_init->rx_pin->pin;
|
2016-11-16 15:38:25 -05:00
|
|
|
|
2017-01-08 10:40:18 -05:00
|
|
|
#if NRF52840_XXAA
|
2017-03-10 16:21:19 -05:00
|
|
|
p_instance->PSELTXD |= (p_uart_init->tx_pin->port << UARTE_PSEL_TXD_PORT_Pos);
|
|
|
|
p_instance->PSELRXD |= (p_uart_init->rx_pin->port << UARTE_PSEL_RXD_PORT_Pos);
|
2017-01-08 10:40:18 -05:00
|
|
|
#endif
|
2017-01-26 15:58:12 -05:00
|
|
|
|
2016-11-16 15:38:25 -05:00
|
|
|
if (p_uart_init->flow_control) {
|
2017-01-26 15:58:12 -05:00
|
|
|
hal_gpio_cfg_pin(p_uart_init->rts_pin->port, p_uart_init->rts_pin->pin, HAL_GPIO_MODE_OUTPUT, HAL_GPIO_PULL_DISABLED);
|
|
|
|
hal_gpio_cfg_pin(p_uart_init->cts_pin->port, p_uart_init->cts_pin->pin, HAL_GPIO_MODE_INPUT, HAL_GPIO_PULL_DISABLED);
|
|
|
|
|
2017-03-10 16:21:19 -05:00
|
|
|
p_instance->PSELCTS = p_uart_init->cts_pin->pin;
|
|
|
|
p_instance->PSELRTS = p_uart_init->rts_pin->pin;
|
2017-01-08 10:40:18 -05:00
|
|
|
|
|
|
|
#if NRF52840_XXAA
|
2017-03-10 16:21:19 -05:00
|
|
|
p_instance->PSELCTS |= (p_uart_init->cts_pin->port << UARTE_PSEL_CTS_PORT_Pos);
|
|
|
|
p_instance->PSELRTS |= (p_uart_init->rts_pin->port << UARTE_PSEL_RTS_PORT_Pos);
|
2017-01-08 10:40:18 -05:00
|
|
|
#endif
|
|
|
|
|
2017-03-10 16:21:19 -05:00
|
|
|
p_instance->CONFIG = (UART_CONFIG_HWFC_Enabled << UART_CONFIG_HWFC_Pos);
|
2016-11-16 15:38:25 -05:00
|
|
|
}
|
|
|
|
|
2017-03-10 16:21:19 -05:00
|
|
|
p_instance->BAUDRATE = (hal_uart_baudrate_lookup[p_uart_init->baud_rate]);
|
|
|
|
p_instance->ENABLE = (UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos);
|
|
|
|
p_instance->EVENTS_TXDRDY = 0;
|
|
|
|
p_instance->EVENTS_RXDRDY = 0;
|
|
|
|
p_instance->TASKS_STARTTX = 1;
|
|
|
|
p_instance->TASKS_STARTRX = 1;
|
2017-12-21 07:49:14 -05:00
|
|
|
|
|
|
|
// Adafruit IRQ + fifo
|
|
|
|
fifo_clear(_ff_uart);
|
|
|
|
p_instance->INTENSET = UART_INTENSET_RXDRDY_Msk;
|
|
|
|
NVIC_ClearPendingIRQ(p_uart_init->irq_num);
|
|
|
|
NVIC_SetPriority(p_uart_init->irq_num, p_uart_init->irq_priority);
|
|
|
|
NVIC_EnableIRQ(p_uart_init->irq_num);
|
|
|
|
}
|
|
|
|
|
2018-05-15 07:40:49 -04:00
|
|
|
bool hal_uart_inited(NRF_UART_Type * p_instance)
|
|
|
|
{
|
|
|
|
return !(p_instance->PSELTXD & (1 << 31)) && !(p_instance->PSELRXD & (1 << 31));
|
|
|
|
}
|
2017-12-21 07:49:14 -05:00
|
|
|
|
|
|
|
void UARTE0_UART0_IRQHandler(void)
|
|
|
|
{
|
|
|
|
NRF_UART_Type * p_instance = NRF_UART0;
|
|
|
|
|
|
|
|
if (p_instance->EVENTS_RXDRDY)
|
|
|
|
{
|
|
|
|
uint8_t ch = (uint8_t) p_instance->RXD;
|
2017-12-29 10:43:31 -05:00
|
|
|
|
|
|
|
// Keyboard interrupt
|
|
|
|
if (mp_interrupt_char != -1 && ch == mp_interrupt_char)
|
|
|
|
{
|
|
|
|
mp_keyboard_interrupt();
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
fifo_write(_ff_uart, &ch);
|
|
|
|
}
|
2017-12-21 07:49:14 -05:00
|
|
|
|
|
|
|
p_instance->EVENTS_RXDRDY = 0x0UL;
|
|
|
|
}
|
2016-11-16 15:38:25 -05:00
|
|
|
}
|
2016-12-13 14:27:26 -05:00
|
|
|
|
|
|
|
#endif // HAL_UART_MODULE_ENABLED
|