SEGGER_RTT_Syscalls_KEIL.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*********************************************************************
  2. * SEGGER Microcontroller GmbH *
  3. * The Embedded Experts *
  4. **********************************************************************
  5. * *
  6. * (c) 1995 - 2021 SEGGER Microcontroller GmbH *
  7. * *
  8. * www.segger.com Support: support@segger.com *
  9. * *
  10. **********************************************************************
  11. * *
  12. * SEGGER SystemView * Real-time application analysis *
  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 SystemView and 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. * condition is met: *
  25. * *
  26. * o Redistributions of source code must retain the above copyright *
  27. * notice, this condition and the following disclaimer. *
  28. * *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND *
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, *
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF *
  32. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE *
  33. * DISCLAIMED. IN NO EVENT SHALL SEGGER Microcontroller BE LIABLE FOR *
  34. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR *
  35. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT *
  36. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; *
  37. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
  38. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
  39. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE *
  40. * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
  41. * DAMAGE. *
  42. * *
  43. **********************************************************************
  44. * *
  45. * SystemView version: 3.32 *
  46. * *
  47. **********************************************************************
  48. ---------------------------END-OF-HEADER------------------------------
  49. File : RTT_Syscalls_KEIL.c
  50. Purpose : Retargeting module for KEIL MDK-CM3.
  51. Low-level functions for using printf() via RTT
  52. Revision: $Rev: 24316 $
  53. Notes : (1) https://wiki.segger.com/Keil_MDK-ARM#RTT_in_uVision
  54. ----------------------------------------------------------------------
  55. */
  56. #if (defined __CC_ARM) || (defined __ARMCC_VERSION)
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59. #include <string.h>
  60. #include <rt_sys.h>
  61. #include <rt_misc.h>
  62. #include "SEGGER_RTT.h"
  63. /*********************************************************************
  64. *
  65. * #pragmas
  66. *
  67. **********************************************************************
  68. */
  69. #if __ARMCC_VERSION < 6000000
  70. #pragma import(__use_no_semihosting)
  71. #endif
  72. #ifdef _MICROLIB
  73. #pragma import(__use_full_stdio)
  74. #endif
  75. /*********************************************************************
  76. *
  77. * Defines non-configurable
  78. *
  79. **********************************************************************
  80. */
  81. /* Standard IO device handles - arbitrary, but any real file system handles must be
  82. less than 0x8000. */
  83. #define STDIN 0x8001 // Standard Input Stream
  84. #define STDOUT 0x8002 // Standard Output Stream
  85. #define STDERR 0x8003 // Standard Error Stream
  86. /*********************************************************************
  87. *
  88. * Public const
  89. *
  90. **********************************************************************
  91. */
  92. #if __ARMCC_VERSION < 5000000
  93. //const char __stdin_name[] = "STDIN";
  94. const char __stdout_name[] = "STDOUT";
  95. const char __stderr_name[] = "STDERR";
  96. #endif
  97. /*********************************************************************
  98. *
  99. * Public code
  100. *
  101. **********************************************************************
  102. */
  103. /*********************************************************************
  104. *
  105. * _ttywrch
  106. *
  107. * Function description:
  108. * Outputs a character to the console
  109. *
  110. * Parameters:
  111. * c - character to output
  112. *
  113. */
  114. void _ttywrch(int c) {
  115. fputc(c, stdout); // stdout
  116. fflush(stdout);
  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. (void)OpenMode;
  136. // Register standard Input Output devices.
  137. if (strcmp(sName, __stdout_name) == 0) {
  138. return (STDOUT);
  139. } else if (strcmp(sName, __stderr_name) == 0) {
  140. return (STDERR);
  141. } else
  142. return (0); // Not implemented
  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. (void)hFile;
  160. return 0; // Not implemented
  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. (void)Mode;
  183. if (hFile == STDOUT) {
  184. SEGGER_RTT_Write(0, (const char*)pBuffer, NumBytes);
  185. return 0;
  186. }
  187. return r;
  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. (void)hFile;
  209. (void)pBuffer;
  210. (void)NumBytes;
  211. (void)Mode;
  212. return (0); // Not implemented
  213. }
  214. /*********************************************************************
  215. *
  216. * _sys_istty
  217. *
  218. * Function description:
  219. * This function shall return whether the opened file
  220. * is a console device or not.
  221. *
  222. * Parameters:
  223. * hFile - Handle to a file opened via _sys_open
  224. *
  225. * Return value:
  226. * 1 - Device is a console
  227. * 0 - Device is not a console
  228. *
  229. */
  230. int _sys_istty(FILEHANDLE hFile) {
  231. if (hFile > 0x8000) {
  232. return (1);
  233. }
  234. return (0); // Not implemented
  235. }
  236. /*********************************************************************
  237. *
  238. * _sys_seek
  239. *
  240. * Function description:
  241. * Seeks via the file to a specific position
  242. *
  243. * Parameters:
  244. * hFile - Handle to a file opened via _sys_open
  245. * Pos -
  246. *
  247. * Return value:
  248. * int -
  249. *
  250. */
  251. int _sys_seek(FILEHANDLE hFile, long Pos) {
  252. (void)hFile;
  253. (void)Pos;
  254. return (0); // Not implemented
  255. }
  256. /*********************************************************************
  257. *
  258. * _sys_ensure
  259. *
  260. * Function description:
  261. *
  262. *
  263. * Parameters:
  264. * hFile - Handle to a file opened via _sys_open
  265. *
  266. * Return value:
  267. * int -
  268. *
  269. */
  270. int _sys_ensure(FILEHANDLE hFile) {
  271. (void)hFile;
  272. return (-1); // Not implemented
  273. }
  274. /*********************************************************************
  275. *
  276. * _sys_flen
  277. *
  278. * Function description:
  279. * Returns the length of the opened file handle
  280. *
  281. * Parameters:
  282. * hFile - Handle to a file opened via _sys_open
  283. *
  284. * Return value:
  285. * Length of the file
  286. *
  287. */
  288. long _sys_flen(FILEHANDLE hFile) {
  289. (void)hFile;
  290. return (0); // Not implemented
  291. }
  292. /*********************************************************************
  293. *
  294. * _sys_tmpnam
  295. *
  296. * Function description:
  297. * This function converts the file number fileno for a temporary
  298. * file to a unique filename, for example, tmp0001.
  299. *
  300. * Parameters:
  301. * pBuffer - Pointer to a buffer to store the name
  302. * FileNum - file number to convert
  303. * MaxLen - Size of the buffer
  304. *
  305. * Return value:
  306. * 1 - Error
  307. * 0 - Success
  308. *
  309. */
  310. int _sys_tmpnam(char * pBuffer, int FileNum, unsigned MaxLen) {
  311. (void)pBuffer;
  312. (void)FileNum;
  313. (void)MaxLen;
  314. return (1); // Not implemented
  315. }
  316. /*********************************************************************
  317. *
  318. * _sys_command_string
  319. *
  320. * Function description:
  321. * This function shall execute a system command.
  322. *
  323. * Parameters:
  324. * cmd - Pointer to the command string
  325. * len - Length of the string
  326. *
  327. * Return value:
  328. * == NULL - Command was not successfully executed
  329. * == sCmd - Command was passed successfully
  330. *
  331. */
  332. char * _sys_command_string(char * cmd, int len) {
  333. (void)len;
  334. return cmd; // Not implemented
  335. }
  336. /*********************************************************************
  337. *
  338. * _sys_exit
  339. *
  340. * Function description:
  341. * This function is called when the application returns from main
  342. *
  343. * Parameters:
  344. * ReturnCode - Return code from the main function
  345. *
  346. *
  347. */
  348. void _sys_exit(int ReturnCode) {
  349. (void)ReturnCode;
  350. while (1); // Not implemented
  351. }
  352. #if __ARMCC_VERSION >= 5000000
  353. /*********************************************************************
  354. *
  355. * stdout_putchar
  356. *
  357. * Function description:
  358. * Put a character to the stdout
  359. *
  360. * Parameters:
  361. * ch - Character to output
  362. *
  363. *
  364. */
  365. int stdout_putchar(int ch) {
  366. (void)ch;
  367. return ch; // Not implemented
  368. }
  369. #endif
  370. #endif
  371. /*************************** End of file ****************************/