|
| 1 | +/* |
| 2 | + * Copyright (c) 2016 Thomas Roell. All rights reserved. |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to |
| 6 | + * deal with the Software without restriction, including without limitation the |
| 7 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 8 | + * sell copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimers. |
| 13 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | + * notice, this list of conditions and the following disclaimers in the |
| 15 | + * documentation and/or other materials provided with the distribution. |
| 16 | + * 3. Neither the name of Thomas Roell, nor the names of its contributors |
| 17 | + * may be used to endorse or promote products derived from this Software |
| 18 | + * without specific prior written permission. |
| 19 | + * |
| 20 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | + * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 25 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 26 | + * WITH THE SOFTWARE. |
| 27 | + */ |
| 28 | + |
| 29 | +#include <errno.h> |
| 30 | +#include <stdint.h> |
| 31 | +#include <stdbool.h> |
| 32 | +#include <sys/stat.h> |
| 33 | +#include <sys/times.h> |
| 34 | +#include <sys/unistd.h> |
| 35 | + |
| 36 | +#include "armv7m.h" |
| 37 | +#include "stm32l4_usbd_cdc.h" |
| 38 | + |
| 39 | +#undef errno |
| 40 | +extern int errno; |
| 41 | + |
| 42 | +extern stm32l4_usbd_cdc_t stm32l4_usbd_cdc; |
| 43 | + |
| 44 | +extern uint32_t __HeapBase[]; |
| 45 | +extern uint32_t __StackLimit[]; |
| 46 | + |
| 47 | +void * _sbrk (int nbytes) |
| 48 | +{ |
| 49 | + void *p; |
| 50 | + |
| 51 | + static void *__HeapCurrent = (void*)(&__HeapBase[0]); |
| 52 | + |
| 53 | + if (((uint8_t*)__HeapCurrent + nbytes) <= (uint8_t*)(&__StackLimit[0])) |
| 54 | + { |
| 55 | + p = __HeapCurrent; |
| 56 | + |
| 57 | + __HeapCurrent = (void*)((uint8_t*)__HeapCurrent + nbytes); |
| 58 | + |
| 59 | + return p; |
| 60 | + } |
| 61 | + else |
| 62 | + { |
| 63 | + errno = ENOMEM; |
| 64 | + |
| 65 | + return (void *) -1; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +int _getpid(void) |
| 70 | +{ |
| 71 | + return 1; |
| 72 | +} |
| 73 | + |
| 74 | +int _kill(int pid, int sig) |
| 75 | +{ |
| 76 | + errno = EINVAL; |
| 77 | + |
| 78 | + return -1; |
| 79 | +} |
| 80 | + |
| 81 | +int _close(int file) { |
| 82 | + return -1; |
| 83 | +} |
| 84 | + |
| 85 | +int _isatty(int file) |
| 86 | +{ |
| 87 | + switch (file) { |
| 88 | + case STDOUT_FILENO: |
| 89 | + case STDERR_FILENO: |
| 90 | + case STDIN_FILENO: |
| 91 | + return 1; |
| 92 | + |
| 93 | + default: |
| 94 | + errno = EBADF; |
| 95 | + return 0; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +int _fstat(int file, struct stat *st) |
| 100 | +{ |
| 101 | + st->st_mode = S_IFCHR; |
| 102 | + |
| 103 | + return 0; |
| 104 | +} |
| 105 | + |
| 106 | +int _lseek(int file, int offset, int whence) |
| 107 | +{ |
| 108 | + return 0; |
| 109 | +} |
| 110 | + |
| 111 | +int _read(int file, char *buf, int nbytes) |
| 112 | +{ |
| 113 | + switch (file) { |
| 114 | + case STDIN_FILENO: |
| 115 | + return 0; |
| 116 | + |
| 117 | + default: |
| 118 | + errno = EBADF; |
| 119 | + return -1; |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +int _write(int file, char *buf, int nbytes) |
| 124 | +{ |
| 125 | + switch (file) { |
| 126 | + case STDOUT_FILENO: |
| 127 | + case STDERR_FILENO: |
| 128 | + if (stm32l4_usbd_cdc.state == USBD_CDC_STATE_NONE) |
| 129 | + { |
| 130 | + stm32l4_usbd_cdc_create(&stm32l4_usbd_cdc); |
| 131 | + } |
| 132 | + |
| 133 | + if (stm32l4_usbd_cdc.state == USBD_CDC_STATE_INIT) |
| 134 | + { |
| 135 | + stm32l4_usbd_cdc_enable(&stm32l4_usbd_cdc, 0, NULL, NULL, 0); |
| 136 | + } |
| 137 | + |
| 138 | + if (!stm32l4_usbd_cdc_done(&stm32l4_usbd_cdc)) |
| 139 | + { |
| 140 | + if (armv7m_core_priority() <= (int)NVIC_GetPriority(OTG_FS_IRQn)) |
| 141 | + { |
| 142 | + while (!stm32l4_usbd_cdc_done(&stm32l4_usbd_cdc)) |
| 143 | + { |
| 144 | + stm32l4_usbd_cdc_poll(&stm32l4_usbd_cdc); |
| 145 | + } |
| 146 | + } |
| 147 | + else |
| 148 | + { |
| 149 | + while (!stm32l4_usbd_cdc_done(&stm32l4_usbd_cdc)) |
| 150 | + { |
| 151 | + armv7m_core_yield(); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + stm32l4_usbd_cdc_transmit(&stm32l4_usbd_cdc, (const uint8_t*)buf, nbytes); |
| 157 | + |
| 158 | + if (armv7m_core_priority() <= (int)NVIC_GetPriority(OTG_FS_IRQn)) |
| 159 | + { |
| 160 | + while (!stm32l4_usbd_cdc_done(&stm32l4_usbd_cdc)) |
| 161 | + { |
| 162 | + stm32l4_usbd_cdc_poll(&stm32l4_usbd_cdc); |
| 163 | + } |
| 164 | + } |
| 165 | + else |
| 166 | + { |
| 167 | + while (!stm32l4_usbd_cdc_done(&stm32l4_usbd_cdc)) |
| 168 | + { |
| 169 | + armv7m_core_yield(); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + return nbytes; |
| 174 | + |
| 175 | + default: |
| 176 | + errno = EBADF; |
| 177 | + return -1; |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +void _exit(int status) |
| 182 | +{ |
| 183 | + while (1) { }; |
| 184 | +} |
0 commit comments