SEGGER_RTT_Syscalls_KEIL.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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.18a *
  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. #include "sdk_config.h"
  66. #if !defined(RETARGET_ENABLED) || RETARGET_ENABLED == 0
  67. #ifdef __CC_ARM
  68. #include <stdio.h>
  69. #include <stdlib.h>
  70. #include <string.h>
  71. #include <rt_sys.h>
  72. #include <rt_misc.h>
  73. #include "SEGGER_RTT.h"
  74. /*********************************************************************
  75. *
  76. * #pragmas
  77. *
  78. **********************************************************************
  79. */
  80. #if defined(NRF_LOG_ENABLED) && NRF_LOG_ENABLED == 1
  81. #pragma import(__use_no_semihosting)
  82. #endif
  83. #ifdef _MICROLIB
  84. #pragma import(__use_full_stdio)
  85. #endif
  86. /*********************************************************************
  87. *
  88. * Defines non-configurable
  89. *
  90. **********************************************************************
  91. */
  92. /* Standard IO device handles - arbitrary, but any real file system handles must be
  93. less than 0x8000. */
  94. #define STDIN 0x8001 // Standard Input Stream
  95. #define STDOUT 0x8002 // Standard Output Stream
  96. #define STDERR 0x8003 // Standard Error Stream
  97. /*********************************************************************
  98. *
  99. * Public const
  100. *
  101. **********************************************************************
  102. */
  103. //const char __stdin_name[] = "STDIN";
  104. const char __stdout_name[] = "STDOUT";
  105. const char __stderr_name[] = "STDERR";
  106. /*********************************************************************
  107. *
  108. * Public code
  109. *
  110. **********************************************************************
  111. */
  112. /*********************************************************************
  113. *
  114. * _ttywrch
  115. *
  116. * Function description:
  117. * Outputs a character to the console
  118. *
  119. * Parameters:
  120. * c - character to output
  121. *
  122. */
  123. void _ttywrch(int c) {
  124. fputc(c, stdout); // stdout
  125. fflush(stdout);
  126. }
  127. /*********************************************************************
  128. *
  129. * _sys_open
  130. *
  131. * Function description:
  132. * Opens the device/file in order to do read/write operations
  133. *
  134. * Parameters:
  135. * sName - sName of the device/file to open
  136. * OpenMode - This parameter is currently ignored
  137. *
  138. * Return value:
  139. * != 0 - Handle to the object to open, otherwise
  140. * == 0 -"device" is not handled by this module
  141. *
  142. */
  143. FILEHANDLE _sys_open(const char * sName, int OpenMode) {
  144. (void)OpenMode;
  145. // Register standard Input Output devices.
  146. if (strcmp(sName, __stdout_name) == 0) {
  147. return (STDOUT);
  148. } else if (strcmp(sName, __stderr_name) == 0) {
  149. return (STDERR);
  150. } else
  151. return (0); // Not implemented
  152. }
  153. /*********************************************************************
  154. *
  155. * _sys_close
  156. *
  157. * Function description:
  158. * Closes the handle to the open device/file
  159. *
  160. * Parameters:
  161. * hFile - Handle to a file opened via _sys_open
  162. *
  163. * Return value:
  164. * 0 - device/file closed
  165. *
  166. */
  167. int _sys_close(FILEHANDLE hFile) {
  168. (void)hFile;
  169. return 0; // Not implemented
  170. }
  171. /*********************************************************************
  172. *
  173. * _sys_write
  174. *
  175. * Function description:
  176. * Writes the data to an open handle.
  177. * Currently this function only outputs data to the console
  178. *
  179. * Parameters:
  180. * hFile - Handle to a file opened via _sys_open
  181. * pBuffer - Pointer to the data that shall be written
  182. * NumBytes - Number of bytes to write
  183. * Mode - The Mode that shall be used
  184. *
  185. * Return value:
  186. * Number of bytes *not* written to the file/device
  187. *
  188. */
  189. int _sys_write(FILEHANDLE hFile, const unsigned char * pBuffer, unsigned NumBytes, int Mode) {
  190. int r = 0;
  191. (void)Mode;
  192. if (hFile == STDOUT) {
  193. return NumBytes - SEGGER_RTT_Write(0, (const char*)pBuffer, NumBytes);
  194. }
  195. return r;
  196. }
  197. /*********************************************************************
  198. *
  199. * _sys_read
  200. *
  201. * Function description:
  202. * Reads data from an open handle.
  203. * Currently this modules does nothing.
  204. *
  205. * Parameters:
  206. * hFile - Handle to a file opened via _sys_open
  207. * pBuffer - Pointer to buffer to store the read data
  208. * NumBytes - Number of bytes to read
  209. * Mode - The Mode that shall be used
  210. *
  211. * Return value:
  212. * Number of bytes read from the file/device
  213. *
  214. */
  215. int _sys_read(FILEHANDLE hFile, unsigned char * pBuffer, unsigned NumBytes, int Mode) {
  216. (void)hFile;
  217. (void)pBuffer;
  218. (void)NumBytes;
  219. (void)Mode;
  220. return (0); // Not implemented
  221. }
  222. /*********************************************************************
  223. *
  224. * _sys_istty
  225. *
  226. * Function description:
  227. * This function shall return whether the opened file
  228. * is a console device or not.
  229. *
  230. * Parameters:
  231. * hFile - Handle to a file opened via _sys_open
  232. *
  233. * Return value:
  234. * 1 - Device is a console
  235. * 0 - Device is not a console
  236. *
  237. */
  238. int _sys_istty(FILEHANDLE hFile) {
  239. if (hFile > 0x8000) {
  240. return (1);
  241. }
  242. return (0); // Not implemented
  243. }
  244. /*********************************************************************
  245. *
  246. * _sys_seek
  247. *
  248. * Function description:
  249. * Seeks via the file to a specific position
  250. *
  251. * Parameters:
  252. * hFile - Handle to a file opened via _sys_open
  253. * Pos -
  254. *
  255. * Return value:
  256. * int -
  257. *
  258. */
  259. int _sys_seek(FILEHANDLE hFile, long Pos) {
  260. (void)hFile;
  261. (void)Pos;
  262. return (0); // Not implemented
  263. }
  264. /*********************************************************************
  265. *
  266. * _sys_ensure
  267. *
  268. * Function description:
  269. *
  270. *
  271. * Parameters:
  272. * hFile - Handle to a file opened via _sys_open
  273. *
  274. * Return value:
  275. * int -
  276. *
  277. */
  278. int _sys_ensure(FILEHANDLE hFile) {
  279. (void)hFile;
  280. return (-1); // Not implemented
  281. }
  282. /*********************************************************************
  283. *
  284. * _sys_flen
  285. *
  286. * Function description:
  287. * Returns the length of the opened file handle
  288. *
  289. * Parameters:
  290. * hFile - Handle to a file opened via _sys_open
  291. *
  292. * Return value:
  293. * Length of the file
  294. *
  295. */
  296. long _sys_flen(FILEHANDLE hFile) {
  297. (void)hFile;
  298. return (0); // Not implemented
  299. }
  300. /*********************************************************************
  301. *
  302. * _sys_tmpnam
  303. *
  304. * Function description:
  305. * This function converts the file number fileno for a temporary
  306. * file to a unique filename, for example, tmp0001.
  307. *
  308. * Parameters:
  309. * pBuffer - Pointer to a buffer to store the name
  310. * FileNum - file number to convert
  311. * MaxLen - Size of the buffer
  312. *
  313. * Return value:
  314. * 1 - Error
  315. * 0 - Success
  316. *
  317. */
  318. int _sys_tmpnam(char * pBuffer, int FileNum, unsigned MaxLen) {
  319. (void)pBuffer;
  320. (void)FileNum;
  321. (void)MaxLen;
  322. return (1); // Not implemented
  323. }
  324. /*********************************************************************
  325. *
  326. * _sys_command_string
  327. *
  328. * Function description:
  329. * This function shall execute a system command.
  330. *
  331. * Parameters:
  332. * cmd - Pointer to the command string
  333. * len - Length of the string
  334. *
  335. * Return value:
  336. * == NULL - Command was not successfully executed
  337. * == sCmd - Command was passed successfully
  338. *
  339. */
  340. char * _sys_command_string(char * cmd, int len) {
  341. (void)len;
  342. return cmd; // Not implemented
  343. }
  344. /*********************************************************************
  345. *
  346. * _sys_exit
  347. *
  348. * Function description:
  349. * This function is called when the application returns from main
  350. *
  351. * Parameters:
  352. * ReturnCode - Return code from the main function
  353. *
  354. *
  355. */
  356. void _sys_exit(int ReturnCode) {
  357. (void)ReturnCode;
  358. while (1); // Not implemented
  359. }
  360. #endif // __CC_ARM
  361. #endif // !defined(RETARGET_ENABLED) || RETARGET_ENABLED == 0
  362. /*************************** End of file ****************************/