|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2016 Damien P. George |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "common-hal/microcontroller/__init__.h" |
| 28 | +#include "shared-bindings/microcontroller/__init__.h" |
| 29 | +#include "shared-bindings/nativeio/UART.h" |
| 30 | + |
| 31 | +#include "ets_sys.h" |
| 32 | +#include "uart.h" |
| 33 | + |
| 34 | +#include "py/nlr.h" |
| 35 | + |
| 36 | +// UartDev is defined and initialized in rom code. |
| 37 | +extern UartDevice UartDev; |
| 38 | + |
| 39 | +void common_hal_nativeio_uart_construct(nativeio_uart_obj_t *self, |
| 40 | + const mcu_pin_obj_t * tx, const mcu_pin_obj_t * rx, uint32_t baudrate, |
| 41 | + uint8_t bits, uart_parity_t parity, uint8_t stop, uint32_t timeout, |
| 42 | + uint8_t receiver_buffer_size) { |
| 43 | + if (rx != NULL || tx != &pin_GPIO2) { |
| 44 | + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Only tx supported on UART1 (GPIO2).")); |
| 45 | + } |
| 46 | + // set baudrate |
| 47 | + UartDev.baut_rate = baudrate; |
| 48 | + |
| 49 | + // set data bits |
| 50 | + switch (bits) { |
| 51 | + case 5: |
| 52 | + UartDev.data_bits = UART_FIVE_BITS; |
| 53 | + break; |
| 54 | + case 6: |
| 55 | + UartDev.data_bits = UART_SIX_BITS; |
| 56 | + break; |
| 57 | + case 7: |
| 58 | + UartDev.data_bits = UART_SEVEN_BITS; |
| 59 | + break; |
| 60 | + case 8: |
| 61 | + UartDev.data_bits = UART_EIGHT_BITS; |
| 62 | + break; |
| 63 | + default: |
| 64 | + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid data bits")); |
| 65 | + break; |
| 66 | + } |
| 67 | + |
| 68 | + if (parity == PARITY_NONE) { |
| 69 | + UartDev.parity = UART_NONE_BITS; |
| 70 | + UartDev.exist_parity = UART_STICK_PARITY_DIS; |
| 71 | + } else { |
| 72 | + UartDev.exist_parity = UART_STICK_PARITY_EN; |
| 73 | + if (parity == PARITY_ODD) { |
| 74 | + UartDev.parity = UART_ODD_BITS; |
| 75 | + } else { |
| 76 | + UartDev.parity = UART_EVEN_BITS; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + switch (stop) { |
| 81 | + case 1: |
| 82 | + UartDev.stop_bits = UART_ONE_STOP_BIT; |
| 83 | + break; |
| 84 | + case 2: |
| 85 | + UartDev.stop_bits = UART_TWO_STOP_BIT; |
| 86 | + break; |
| 87 | + default: |
| 88 | + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid stop bits")); |
| 89 | + break; |
| 90 | + } |
| 91 | + |
| 92 | + uart_setup(UART1); |
| 93 | +} |
| 94 | + |
| 95 | +void common_hal_nativeio_uart_deinit(nativeio_uart_obj_t *self) { |
| 96 | + PIN_FUNC_SELECT(FUNC_U1TXD_BK, 0); |
| 97 | +} |
| 98 | + |
| 99 | +size_t common_hal_nativeio_uart_read(nativeio_uart_obj_t *self, uint8_t *data, size_t len, int *errcode) { |
| 100 | + return 0; |
| 101 | +} |
| 102 | + |
| 103 | +// Write characters. |
| 104 | +size_t common_hal_nativeio_uart_write(nativeio_uart_obj_t *self, const uint8_t *data, size_t len, int *errcode) { |
| 105 | + // write the data |
| 106 | + for (size_t i = 0; i < len; ++i) { |
| 107 | + uart_tx_one_char(UART1, *data++); |
| 108 | + } |
| 109 | + |
| 110 | + // return number of bytes written |
| 111 | + return len; |
| 112 | +} |
| 113 | + |
| 114 | +uint32_t common_hal_nativeio_uart_rx_characters_available(nativeio_uart_obj_t *self) { |
| 115 | + return 0; |
| 116 | +} |
| 117 | + |
| 118 | +bool common_hal_nativeio_uart_ready_to_tx(nativeio_uart_obj_t *self) { |
| 119 | + return true; |
| 120 | +} |
0 commit comments