SEGGER_RTT_Syscalls_KEIL.c 11 KB

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