2014-05-03 23:27:38 +01:00
|
|
|
/*
|
2017-06-30 08:22:17 +01:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 23:27:38 +01:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* 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-02-15 04:47:04 +00:00
|
|
|
#ifndef MICROPY_INCLUDED_STM32_UART_H
|
|
|
|
#define MICROPY_INCLUDED_STM32_UART_H
|
2014-05-03 23:27:38 +01:00
|
|
|
|
2021-07-09 05:19:15 +01:00
|
|
|
#include "shared/runtime/mpirq.h"
|
2018-12-09 16:07:56 +00:00
|
|
|
|
2014-04-21 12:03:09 +01:00
|
|
|
typedef enum {
|
|
|
|
PYB_UART_NONE = 0,
|
|
|
|
PYB_UART_1 = 1,
|
|
|
|
PYB_UART_2 = 2,
|
|
|
|
PYB_UART_3 = 3,
|
|
|
|
PYB_UART_4 = 4,
|
|
|
|
PYB_UART_5 = 5,
|
|
|
|
PYB_UART_6 = 6,
|
2016-12-05 04:14:22 +00:00
|
|
|
PYB_UART_7 = 7,
|
|
|
|
PYB_UART_8 = 8,
|
2019-04-19 06:15:18 +01:00
|
|
|
PYB_UART_9 = 9,
|
|
|
|
PYB_UART_10 = 10,
|
2021-02-17 00:07:34 +00:00
|
|
|
#ifdef LPUART1
|
|
|
|
PYB_LPUART_1 = MICROPY_HW_MAX_UART + 1,
|
|
|
|
#endif
|
2022-02-12 20:36:58 +00:00
|
|
|
#ifdef LPUART2
|
|
|
|
PYB_LPUART_2 = MICROPY_HW_MAX_UART + 2,
|
|
|
|
#endif
|
2014-04-21 12:03:09 +01:00
|
|
|
} pyb_uart_t;
|
|
|
|
|
2018-12-07 07:36:43 +00:00
|
|
|
#define CHAR_WIDTH_8BIT (0)
|
|
|
|
#define CHAR_WIDTH_9BIT (1)
|
|
|
|
|
2020-08-14 06:21:23 +01:00
|
|
|
// OR-ed IRQ flags which are allowed to be used by the user
|
|
|
|
#define MP_UART_ALLOWED_FLAGS UART_FLAG_IDLE
|
|
|
|
|
|
|
|
// OR-ed IRQ flags which should not be touched by the user
|
|
|
|
#define MP_UART_RESERVED_FLAGS UART_FLAG_RXNE
|
|
|
|
|
2018-12-07 07:36:43 +00:00
|
|
|
typedef struct _pyb_uart_obj_t {
|
|
|
|
mp_obj_base_t base;
|
2018-12-09 23:44:52 +00:00
|
|
|
USART_TypeDef *uartx;
|
2018-12-07 07:36:43 +00:00
|
|
|
pyb_uart_t uart_id : 8;
|
2018-12-10 02:08:31 +00:00
|
|
|
bool is_static : 1;
|
2018-12-07 07:36:43 +00:00
|
|
|
bool is_enabled : 1;
|
|
|
|
bool attached_to_repl; // whether the UART is attached to REPL
|
|
|
|
byte char_width; // 0 for 7,8 bit chars, 1 for 9 bit chars
|
|
|
|
uint16_t char_mask; // 0x7f for 7 bit, 0xff for 8 bit, 0x1ff for 9 bit
|
|
|
|
uint16_t timeout; // timeout waiting for first char
|
|
|
|
uint16_t timeout_char; // timeout waiting between chars
|
|
|
|
uint16_t read_buf_len; // len in chars; buf can hold len-1 chars
|
|
|
|
volatile uint16_t read_buf_head; // indexes first empty slot
|
|
|
|
uint16_t read_buf_tail; // indexes first full slot (not full if equals head)
|
|
|
|
byte *read_buf; // byte or uint16_t, depending on char size
|
2018-12-09 16:07:56 +00:00
|
|
|
uint16_t mp_irq_trigger; // user IRQ trigger mask
|
|
|
|
uint16_t mp_irq_flags; // user IRQ active IRQ flags
|
2020-08-14 06:21:23 +01:00
|
|
|
mp_irq_obj_t *mp_irq_obj; // user IRQ object
|
2018-12-07 07:36:43 +00:00
|
|
|
} pyb_uart_obj_t;
|
|
|
|
|
2014-04-21 12:03:09 +01:00
|
|
|
extern const mp_obj_type_t pyb_uart_type;
|
2020-08-14 06:21:23 +01:00
|
|
|
extern const mp_irq_methods_t uart_irq_methods;
|
2014-04-21 12:03:09 +01:00
|
|
|
|
2014-10-11 17:57:10 +01:00
|
|
|
void uart_init0(void);
|
2018-12-07 07:36:43 +00:00
|
|
|
void uart_deinit_all(void);
|
|
|
|
bool uart_exists(int uart_id);
|
2018-12-10 00:25:06 +00:00
|
|
|
bool uart_init(pyb_uart_obj_t *uart_obj,
|
|
|
|
uint32_t baudrate, uint32_t bits, uint32_t parity, uint32_t stop, uint32_t flow);
|
2020-08-14 06:21:23 +01:00
|
|
|
void uart_irq_config(pyb_uart_obj_t *self, bool enable);
|
2018-12-09 23:21:37 +00:00
|
|
|
void uart_set_rxbuf(pyb_uart_obj_t *self, size_t len, void *buf);
|
2018-12-07 07:36:43 +00:00
|
|
|
void uart_deinit(pyb_uart_obj_t *uart_obj);
|
2014-10-11 17:57:10 +01:00
|
|
|
void uart_irq_handler(mp_uint_t uart_id);
|
|
|
|
|
2018-04-23 11:44:30 +01:00
|
|
|
void uart_attach_to_repl(pyb_uart_obj_t *self, bool attached);
|
2021-07-26 04:21:30 +01:00
|
|
|
uint32_t uart_get_source_freq(pyb_uart_obj_t *self);
|
2018-12-09 07:10:57 +00:00
|
|
|
uint32_t uart_get_baudrate(pyb_uart_obj_t *self);
|
2021-02-03 05:18:35 +00:00
|
|
|
void uart_set_baudrate(pyb_uart_obj_t *self, uint32_t baudrate);
|
|
|
|
|
2015-11-27 15:31:59 +00:00
|
|
|
mp_uint_t uart_rx_any(pyb_uart_obj_t *uart_obj);
|
2018-12-07 07:36:43 +00:00
|
|
|
bool uart_rx_wait(pyb_uart_obj_t *self, uint32_t timeout);
|
2014-04-21 12:03:09 +01:00
|
|
|
int uart_rx_char(pyb_uart_obj_t *uart_obj);
|
2018-12-07 07:36:43 +00:00
|
|
|
bool uart_tx_wait(pyb_uart_obj_t *self, uint32_t timeout);
|
|
|
|
size_t uart_tx_data(pyb_uart_obj_t *self, const void *src_in, size_t num_chars, int *errcode);
|
2014-04-21 12:03:09 +01:00
|
|
|
void uart_tx_strn(pyb_uart_obj_t *uart_obj, const char *str, uint len);
|
all: Unify header guard usage.
The code conventions suggest using header guards, but do not define how
those should look like and instead point to existing files. However, not
all existing files follow the same scheme, sometimes omitting header guards
altogether, sometimes using non-standard names, making it easy to
accidentally pick a "wrong" example.
This commit ensures that all header files of the MicroPython project (that
were not simply copied from somewhere else) follow the same pattern, that
was already present in the majority of files, especially in the py folder.
The rules are as follows.
Naming convention:
* start with the words MICROPY_INCLUDED
* contain the full path to the file
* replace special characters with _
In addition, there are no empty lines before #ifndef, between #ifndef and
one empty line before #endif. #endif is followed by a comment containing
the name of the guard macro.
py/grammar.h cannot use header guards by design, since it has to be
included multiple times in a single C file. Several other files also do not
need header guards as they are only used internally and guaranteed to be
included only once:
* MICROPY_MPHALPORT_H
* mpconfigboard.h
* mpconfigport.h
* mpthreadport.h
* pin_defs_*.h
* qstrdefs*.h
2017-06-29 22:14:58 +01:00
|
|
|
|
2018-12-09 23:44:52 +00:00
|
|
|
static inline bool uart_tx_avail(pyb_uart_obj_t *self) {
|
|
|
|
#if defined(STM32F4)
|
|
|
|
return self->uartx->SR & USART_SR_TXE;
|
2022-02-12 20:36:58 +00:00
|
|
|
#elif defined(STM32G0) || defined(STM32H7) || defined(STM32WL)
|
2019-07-03 14:40:49 +01:00
|
|
|
return self->uartx->ISR & USART_ISR_TXE_TXFNF;
|
2018-12-09 23:44:52 +00:00
|
|
|
#else
|
|
|
|
return self->uartx->ISR & USART_ISR_TXE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-02-15 04:47:04 +00:00
|
|
|
#endif // MICROPY_INCLUDED_STM32_UART_H
|