RTT_Syscalls_KEIL.c 9.9 KB

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