|
| 1 | +/** |
| 2 | + * MIT License: |
| 3 | + * Copyright (c) 2019 Bumsik kim <k.bumsik@gmail.com> |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in |
| 13 | + * all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | + * THE SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +#if defined (VIRTIOCON) |
| 25 | + |
| 26 | +#include "openamp.h" |
| 27 | +#include "openamp_log.h" |
| 28 | +#include "wiring.h" |
| 29 | +#include "ringbuffer.h" |
| 30 | + |
| 31 | +VirtIOSerial SerialVirtIO; |
| 32 | +void serialEventVirtIO() __attribute__((weak)); |
| 33 | + |
| 34 | +static VIRT_UART_HandleTypeDef huart; |
| 35 | +static bool initialized = false; |
| 36 | +static ringbuffer_t ring; |
| 37 | + |
| 38 | +void rxCallback(VIRT_UART_HandleTypeDef *huart); |
| 39 | + |
| 40 | +void VirtIOSerial::begin(void) |
| 41 | +{ |
| 42 | + ringbuffer_init(&ring); |
| 43 | + if (initialized) { |
| 44 | + return; |
| 45 | + } |
| 46 | + OPENAMP_Init(NULL); |
| 47 | + if (VIRT_UART_Init(&huart) != VIRT_UART_OK) { |
| 48 | + // log_err("VIRT_UART_Init UART0 failed.\r\n"); |
| 49 | + Error_Handler(); |
| 50 | + } |
| 51 | + /*Need to register callback for message reception by channels*/ |
| 52 | + if (VIRT_UART_RegisterCallback(&huart, VIRT_UART_RXCPLT_CB_ID, rxCallback) != VIRT_UART_OK) { |
| 53 | + Error_Handler(); |
| 54 | + } |
| 55 | + initialized = true; |
| 56 | +} |
| 57 | + |
| 58 | +void VirtIOSerial::begin(uint32_t /* baud_count */) |
| 59 | +{ |
| 60 | + // uart config is ignored in USB-CDC |
| 61 | + begin(); |
| 62 | +} |
| 63 | + |
| 64 | +void VirtIOSerial::begin(uint32_t /* baud_count */, uint8_t /* config */) |
| 65 | +{ |
| 66 | + // uart config is ignored in USB-CDC |
| 67 | + begin(); |
| 68 | +} |
| 69 | + |
| 70 | +void VirtIOSerial::end() |
| 71 | +{ |
| 72 | + OPENAMP_DeInit(); |
| 73 | + ringbuffer_init(&ring); |
| 74 | + initialized = false; |
| 75 | +} |
| 76 | + |
| 77 | +int VirtIOSerial::available(void) |
| 78 | +{ |
| 79 | + return ringbuffer_read_available(&ring); |
| 80 | +} |
| 81 | + |
| 82 | +int VirtIOSerial::availableForWrite() |
| 83 | +{ |
| 84 | + // Just return max length of VIRT_UART_Transmit() can transmit. |
| 85 | + // See VIRT_UART_Transmit(). |
| 86 | + return RPMSG_BUFFER_SIZE - 16; |
| 87 | +} |
| 88 | + |
| 89 | +int VirtIOSerial::peek(void) |
| 90 | +{ |
| 91 | + if (ringbuffer_read_available(&ring) > 0) { |
| 92 | + uint8_t tmp; |
| 93 | + ringbuffer_peek(&ring, &tmp, 1); |
| 94 | + return tmp; |
| 95 | + } else { |
| 96 | + return -1; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +int VirtIOSerial::read(void) |
| 101 | +{ |
| 102 | + if (available() > 0) { |
| 103 | + char ch; |
| 104 | + readBytes(&ch, 1); |
| 105 | + return ch; |
| 106 | + } else { |
| 107 | + return -1; |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +size_t VirtIOSerial::readBytes(char *buffer, size_t length) |
| 112 | +{ |
| 113 | + const size_t size = length; |
| 114 | + _startMillis = millis(); |
| 115 | + while (length > 0 && (millis() - _startMillis < _timeout)) { |
| 116 | + uint16_t prev_write_available = ringbuffer_write_available(&ring); |
| 117 | + length -= ringbuffer_read(&ring, reinterpret_cast<uint8_t *>(buffer), length); |
| 118 | + if (prev_write_available < RPMSG_BUFFER_SIZE |
| 119 | + && ringbuffer_write_available(&ring) >= RPMSG_BUFFER_SIZE) { |
| 120 | + MAILBOX_Notify_Rx_Buf_Free(); |
| 121 | + } |
| 122 | + } |
| 123 | + return size - length; |
| 124 | +} |
| 125 | + |
| 126 | +size_t VirtIOSerial::write(uint8_t ch) |
| 127 | +{ |
| 128 | + // Just write single-byte buffer. |
| 129 | + return write(&ch, 1); |
| 130 | +} |
| 131 | + |
| 132 | +// Warning: Currently VirtIOSerial implementation is synchronous, blocking |
| 133 | +// until all bytes are sent. |
| 134 | +size_t VirtIOSerial::write(const uint8_t *buffer, size_t size) |
| 135 | +{ |
| 136 | + if (VIRT_UART_Transmit(&huart, const_cast<uint8_t *>(buffer), size) == VIRT_UART_ERROR) { |
| 137 | + return 0; |
| 138 | + } |
| 139 | + return size; |
| 140 | +} |
| 141 | + |
| 142 | +void VirtIOSerial::flush(void) |
| 143 | +{ |
| 144 | + // write() is blocked until all bytes are sent. So flush() doesn't need to do |
| 145 | + // anything. See rpmsg_send(). |
| 146 | + return; |
| 147 | +} |
| 148 | + |
| 149 | +/* USER CODE BEGIN 4 */ |
| 150 | +void rxCallback(VIRT_UART_HandleTypeDef *huart) |
| 151 | +{ |
| 152 | + log_info("Msg received on VIRTUAL UART0 channel: %s \n\r", (char *) huart->pRxBuffPtr); |
| 153 | + |
| 154 | + /* copy received msg in a variable to sent it back to master processor in main infinite loop*/ |
| 155 | + size_t size = min(huart->RxXferSize, ringbuffer_write_available(&ring)); |
| 156 | + while (size > 0) { |
| 157 | + size -= ringbuffer_write(&ring, huart->pRxBuffPtr, size); |
| 158 | + } |
| 159 | + if (ringbuffer_write_available(&ring) >= RPMSG_BUFFER_SIZE) { |
| 160 | + MAILBOX_Notify_Rx_Buf_Free(); |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +#endif /* VIRTIOCON */ |
0 commit comments