| 1 | /********************************************************************* | 
|---|
| 2 | *                    SEGGER Microcontroller GmbH                     * | 
|---|
| 3 | *                        The Embedded Experts                        * | 
|---|
| 4 | ********************************************************************** | 
|---|
| 5 | *                                                                    * | 
|---|
| 6 | *            (c) 2014 - 2019 SEGGER Microcontroller GmbH             * | 
|---|
| 7 | *                                                                    * | 
|---|
| 8 | *           www.segger.com     Support: support@segger.com           * | 
|---|
| 9 | *                                                                    * | 
|---|
| 10 | ********************************************************************** | 
|---|
| 11 | *                                                                    * | 
|---|
| 12 | * All rights reserved.                                               * | 
|---|
| 13 | *                                                                    * | 
|---|
| 14 | * Redistribution and use in source and binary forms, with or         * | 
|---|
| 15 | * without modification, are permitted provided that the following    * | 
|---|
| 16 | * conditions are met:                                                * | 
|---|
| 17 | *                                                                    * | 
|---|
| 18 | * - Redistributions of source code must retain the above copyright   * | 
|---|
| 19 | *   notice, this list of conditions and the following disclaimer.    * | 
|---|
| 20 | *                                                                    * | 
|---|
| 21 | * - Neither the name of SEGGER Microcontroller GmbH                  * | 
|---|
| 22 | *   nor the names of its contributors may be used to endorse or      * | 
|---|
| 23 | *   promote products derived from this software without specific     * | 
|---|
| 24 | *   prior written permission.                                        * | 
|---|
| 25 | *                                                                    * | 
|---|
| 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND             * | 
|---|
| 27 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,        * | 
|---|
| 28 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF           * | 
|---|
| 29 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE           * | 
|---|
| 30 | * DISCLAIMED.                                                        * | 
|---|
| 31 | * IN NO EVENT SHALL SEGGER Microcontroller GmbH BE LIABLE FOR        * | 
|---|
| 32 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR           * | 
|---|
| 33 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT  * | 
|---|
| 34 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;    * | 
|---|
| 35 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF      * | 
|---|
| 36 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT          * | 
|---|
| 37 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE  * | 
|---|
| 38 | * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH   * | 
|---|
| 39 | * DAMAGE.                                                            * | 
|---|
| 40 | *                                                                    * | 
|---|
| 41 | ********************************************************************** | 
|---|
| 42 | *                                                                    * | 
|---|
| 43 | *       RTT version: 5.12e                                           * | 
|---|
| 44 | *                                                                    * | 
|---|
| 45 | ********************************************************************** | 
|---|
| 46 | --------- END-OF-HEADER -------------------------------------------- | 
|---|
| 47 | File    : RTT_Syscalls_KEIL.c | 
|---|
| 48 | Purpose : Retargeting module for KEIL MDK-CM3. | 
|---|
| 49 |           Low-level functions for using printf() via RTT | 
|---|
| 50 | ---------------------------------------------------------------------- | 
|---|
| 51 | */ | 
|---|
| 52 |  | 
|---|
| 53 | #include <stdio.h> | 
|---|
| 54 | #include <stdlib.h> | 
|---|
| 55 | #include <string.h> | 
|---|
| 56 | #include <rt_sys.h> | 
|---|
| 57 | #include <rt_misc.h> | 
|---|
| 58 |  | 
|---|
| 59 | #include "SEGGER_RTT.h" | 
|---|
| 60 | /********************************************************************* | 
|---|
| 61 | * | 
|---|
| 62 | *       #pragmas | 
|---|
| 63 | * | 
|---|
| 64 | ********************************************************************** | 
|---|
| 65 | */ | 
|---|
| 66 | #pragma import(__use_no_semihosting) | 
|---|
| 67 |  | 
|---|
| 68 | #ifdef _MICROLIB | 
|---|
| 69 |   #pragma import(__use_full_stdio) | 
|---|
| 70 | #endif | 
|---|
| 71 |  | 
|---|
| 72 | /********************************************************************* | 
|---|
| 73 | * | 
|---|
| 74 | *       Defines non-configurable | 
|---|
| 75 | * | 
|---|
| 76 | ********************************************************************** | 
|---|
| 77 | */ | 
|---|
| 78 |  | 
|---|
| 79 | /* Standard IO device handles - arbitrary, but any real file system handles must be | 
|---|
| 80 |    less than 0x8000. */ | 
|---|
| 81 | #define STDIN             0x8001    // Standard Input Stream | 
|---|
| 82 | #define STDOUT            0x8002    // Standard Output Stream | 
|---|
| 83 | #define STDERR            0x8003    // Standard Error Stream | 
|---|
| 84 |  | 
|---|
| 85 | /********************************************************************* | 
|---|
| 86 | * | 
|---|
| 87 | *       Public const | 
|---|
| 88 | * | 
|---|
| 89 | ********************************************************************** | 
|---|
| 90 | */ | 
|---|
| 91 | const char __stdin_name[]  = "STDIN"; | 
|---|
| 92 | const char __stdout_name[] = "STDOUT"; | 
|---|
| 93 | const char __stderr_name[] = "STDERR"; | 
|---|
| 94 |  | 
|---|
| 95 | /********************************************************************* | 
|---|
| 96 | * | 
|---|
| 97 | *       Public code | 
|---|
| 98 | * | 
|---|
| 99 | ********************************************************************** | 
|---|
| 100 | */ | 
|---|
| 101 |  | 
|---|
| 102 | /********************************************************************* | 
|---|
| 103 | * | 
|---|
| 104 | *       _ttywrch | 
|---|
| 105 | * | 
|---|
| 106 | *  Function description: | 
|---|
| 107 | *    Outputs a character to the console | 
|---|
| 108 | * | 
|---|
| 109 | *  Parameters: | 
|---|
| 110 | *    c    - character to output | 
|---|
| 111 | *   | 
|---|
| 112 | */ | 
|---|
| 113 | void _ttywrch(int c) { | 
|---|
| 114 |   fputc(c, stdout); // stdout | 
|---|
| 115 |   fflush(stdout); | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | /********************************************************************* | 
|---|
| 119 | * | 
|---|
| 120 | *       _sys_open | 
|---|
| 121 | * | 
|---|
| 122 | *  Function description: | 
|---|
| 123 | *    Opens the device/file in order to do read/write operations | 
|---|
| 124 | * | 
|---|
| 125 | *  Parameters: | 
|---|
| 126 | *    sName        - sName of the device/file to open | 
|---|
| 127 | *    OpenMode    - This parameter is currently ignored | 
|---|
| 128 | *   | 
|---|
| 129 | *  Return value: | 
|---|
| 130 | *    != 0     - Handle to the object to open, otherwise  | 
|---|
| 131 | *    == 0     -"device" is not handled by this module | 
|---|
| 132 | * | 
|---|
| 133 | */ | 
|---|
| 134 | FILEHANDLE _sys_open(const char * sName, int OpenMode) { | 
|---|
| 135 |   // Register standard Input Output devices. | 
|---|
| 136 |   if (strcmp(sName, __stdout_name) == 0) { | 
|---|
| 137 |     return (STDOUT); | 
|---|
| 138 |   } else if (strcmp(sName, __stderr_name) == 0) { | 
|---|
| 139 |     return (STDERR); | 
|---|
| 140 |   } else | 
|---|
| 141 |   return (0);  // Not implemented | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|
| 144 | /********************************************************************* | 
|---|
| 145 | * | 
|---|
| 146 | *       _sys_close | 
|---|
| 147 | * | 
|---|
| 148 | *  Function description: | 
|---|
| 149 | *    Closes the handle to the open device/file | 
|---|
| 150 | * | 
|---|
| 151 | *  Parameters: | 
|---|
| 152 | *    hFile    - Handle to a file opened via _sys_open | 
|---|
| 153 | *   | 
|---|
| 154 | *  Return value: | 
|---|
| 155 | *    0     - device/file closed | 
|---|
| 156 | * | 
|---|
| 157 | */ | 
|---|
| 158 | int _sys_close(FILEHANDLE hFile) { | 
|---|
| 159 |   return 0;  // Not implemented | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | /********************************************************************* | 
|---|
| 163 | * | 
|---|
| 164 | *       _sys_write | 
|---|
| 165 | * | 
|---|
| 166 | *  Function description: | 
|---|
| 167 | *    Writes the data to an open handle. | 
|---|
| 168 | *    Currently this function only outputs data to the console | 
|---|
| 169 | * | 
|---|
| 170 | *  Parameters: | 
|---|
| 171 | *    hFile    - Handle to a file opened via _sys_open | 
|---|
| 172 | *    pBuffer  - Pointer to the data that shall be written | 
|---|
| 173 | *    NumBytes      - Number of bytes to write | 
|---|
| 174 | *    Mode     - The Mode that shall be used | 
|---|
| 175 | *   | 
|---|
| 176 | *  Return value: | 
|---|
| 177 | *    Number of bytes *not* written to the file/device | 
|---|
| 178 | * | 
|---|
| 179 | */ | 
|---|
| 180 | int _sys_write(FILEHANDLE hFile, const unsigned char * pBuffer, unsigned NumBytes, int Mode) { | 
|---|
| 181 |   int r = 0; | 
|---|
| 182 |  | 
|---|
| 183 |   if (hFile == STDOUT) { | 
|---|
| 184 |     return NumBytes - SEGGER_RTT_Write(0, (const char*)pBuffer, NumBytes); | 
|---|
| 185 |   } | 
|---|
| 186 |   return r; | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | /********************************************************************* | 
|---|
| 190 | * | 
|---|
| 191 | *       _sys_read | 
|---|
| 192 | * | 
|---|
| 193 | *  Function description: | 
|---|
| 194 | *    Reads data from an open handle. | 
|---|
| 195 | *    Currently this modules does nothing. | 
|---|
| 196 | * | 
|---|
| 197 | *  Parameters: | 
|---|
| 198 | *    hFile    - Handle to a file opened via _sys_open | 
|---|
| 199 | *    pBuffer  - Pointer to buffer to store the read data | 
|---|
| 200 | *    NumBytes      - Number of bytes to read | 
|---|
| 201 | *    Mode     - The Mode that shall be used | 
|---|
| 202 | *   | 
|---|
| 203 | *  Return value: | 
|---|
| 204 | *    Number of bytes read from the file/device | 
|---|
| 205 | * | 
|---|
| 206 | */ | 
|---|
| 207 | int _sys_read(FILEHANDLE hFile, unsigned char * pBuffer, unsigned NumBytes, int Mode) { | 
|---|
| 208 |   return (0);  // Not implemented | 
|---|
| 209 | } | 
|---|
| 210 |  | 
|---|
| 211 | /********************************************************************* | 
|---|
| 212 | * | 
|---|
| 213 | *       _sys_istty | 
|---|
| 214 | * | 
|---|
| 215 | *  Function description: | 
|---|
| 216 | *    This function shall return whether the opened file  | 
|---|
| 217 | *    is a console device or not. | 
|---|
| 218 | * | 
|---|
| 219 | *  Parameters: | 
|---|
| 220 | *    hFile    - Handle to a file opened via _sys_open | 
|---|
| 221 | *   | 
|---|
| 222 | *  Return value: | 
|---|
| 223 | *    1       - Device is     a console | 
|---|
| 224 | *    0       - Device is not a console | 
|---|
| 225 | * | 
|---|
| 226 | */ | 
|---|
| 227 | int _sys_istty(FILEHANDLE hFile) { | 
|---|
| 228 |   if (hFile > 0x8000) { | 
|---|
| 229 |     return (1); | 
|---|
| 230 |   } | 
|---|
| 231 |   return (0);  // Not implemented | 
|---|
| 232 | } | 
|---|
| 233 |  | 
|---|
| 234 | /********************************************************************* | 
|---|
| 235 | * | 
|---|
| 236 | *       _sys_seek | 
|---|
| 237 | * | 
|---|
| 238 | *  Function description: | 
|---|
| 239 | *    Seeks via the file to a specific position | 
|---|
| 240 | * | 
|---|
| 241 | *  Parameters: | 
|---|
| 242 | *    hFile  - Handle to a file opened via _sys_open | 
|---|
| 243 | *    Pos    -  | 
|---|
| 244 | *   | 
|---|
| 245 | *  Return value: | 
|---|
| 246 | *    int       -  | 
|---|
| 247 | * | 
|---|
| 248 | */ | 
|---|
| 249 | int _sys_seek(FILEHANDLE hFile, long Pos) { | 
|---|
| 250 |   return (0);  // Not implemented | 
|---|
| 251 | } | 
|---|
| 252 |  | 
|---|
| 253 | /********************************************************************* | 
|---|
| 254 | * | 
|---|
| 255 | *       _sys_ensure | 
|---|
| 256 | * | 
|---|
| 257 | *  Function description: | 
|---|
| 258 | *     | 
|---|
| 259 | * | 
|---|
| 260 | *  Parameters: | 
|---|
| 261 | *    hFile    - Handle to a file opened via _sys_open | 
|---|
| 262 | *   | 
|---|
| 263 | *  Return value: | 
|---|
| 264 | *    int       -  | 
|---|
| 265 | * | 
|---|
| 266 | */ | 
|---|
| 267 | int _sys_ensure(FILEHANDLE hFile) { | 
|---|
| 268 |   return (-1);  // Not implemented | 
|---|
| 269 | } | 
|---|
| 270 |  | 
|---|
| 271 | /********************************************************************* | 
|---|
| 272 | * | 
|---|
| 273 | *       _sys_flen | 
|---|
| 274 | * | 
|---|
| 275 | *  Function description: | 
|---|
| 276 | *    Returns the length of the opened file handle | 
|---|
| 277 | * | 
|---|
| 278 | *  Parameters: | 
|---|
| 279 | *    hFile    - Handle to a file opened via _sys_open | 
|---|
| 280 | *   | 
|---|
| 281 | *  Return value: | 
|---|
| 282 | *    Length of the file | 
|---|
| 283 | * | 
|---|
| 284 | */ | 
|---|
| 285 | long _sys_flen(FILEHANDLE hFile) { | 
|---|
| 286 |   return (0);  // Not implemented | 
|---|
| 287 | } | 
|---|
| 288 |  | 
|---|
| 289 | /********************************************************************* | 
|---|
| 290 | * | 
|---|
| 291 | *       _sys_tmpnam | 
|---|
| 292 | * | 
|---|
| 293 | *  Function description: | 
|---|
| 294 | *    This function converts the file number fileno for a temporary  | 
|---|
| 295 | *    file to a unique filename, for example, tmp0001. | 
|---|
| 296 | * | 
|---|
| 297 | *  Parameters: | 
|---|
| 298 | *    pBuffer    - Pointer to a buffer to store the name | 
|---|
| 299 | *    FileNum    - file number to convert | 
|---|
| 300 | *    MaxLen     - Size of the buffer | 
|---|
| 301 | *   | 
|---|
| 302 | *  Return value: | 
|---|
| 303 | *     1 - Error | 
|---|
| 304 | *     0 - Success   | 
|---|
| 305 | * | 
|---|
| 306 | */ | 
|---|
| 307 | int _sys_tmpnam(char * pBuffer, int FileNum, unsigned MaxLen) { | 
|---|
| 308 |   return (1);  // Not implemented | 
|---|
| 309 | } | 
|---|
| 310 |  | 
|---|
| 311 | /********************************************************************* | 
|---|
| 312 | * | 
|---|
| 313 | *       _sys_command_string | 
|---|
| 314 | * | 
|---|
| 315 | *  Function description: | 
|---|
| 316 | *    This function shall execute a system command. | 
|---|
| 317 | * | 
|---|
| 318 | *  Parameters: | 
|---|
| 319 | *    cmd    - Pointer to the command string | 
|---|
| 320 | *    len    - Length of the string | 
|---|
| 321 | *   | 
|---|
| 322 | *  Return value: | 
|---|
| 323 | *    == NULL - Command was not successfully executed | 
|---|
| 324 | *    == sCmd - Command was passed successfully | 
|---|
| 325 | * | 
|---|
| 326 | */ | 
|---|
| 327 | char * _sys_command_string(char * cmd, int len) { | 
|---|
| 328 |   return cmd;  // Not implemented | 
|---|
| 329 | } | 
|---|
| 330 |  | 
|---|
| 331 | /********************************************************************* | 
|---|
| 332 | * | 
|---|
| 333 | *       _sys_exit | 
|---|
| 334 | * | 
|---|
| 335 | *  Function description: | 
|---|
| 336 | *    This function is called when the application returns from main | 
|---|
| 337 | * | 
|---|
| 338 | *  Parameters: | 
|---|
| 339 | *    ReturnCode    - Return code from the main function | 
|---|
| 340 | *   | 
|---|
| 341 | * | 
|---|
| 342 | */ | 
|---|
| 343 | void _sys_exit(int ReturnCode) { | 
|---|
| 344 |   while (1);  // Not implemented | 
|---|
| 345 | } | 
|---|