想玩CPP,奈何CPP不支持MicroLib,所有就。。。找找咋打印的问题。Keil的CMSIS 的STDOUT也可以,但每次都得搞,烦的很。
简化版C标准IO输出,用于解决STM32 半主机模式
随便找个地方把下面代码放上去,就可以愉快的使用RTT或者Printf;
C嘎嘎我来了。
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
/* Standard IO device name defines. */
const char __stdin_name[] = ":STDIN";
const char __stdout_name[] = ":STDOUT";
const char __stderr_name[] = ":STDERR";
__attribute__((weak))
long _sys_flen (int fh) {
switch (fh) {
case 0x8001:return (0);
case 0x8002:return (0);
case 0x8003:return (0);
}
return (-1);
}
__attribute__((weak))
int _sys_seek (int fh, long pos) {
(void)pos;
switch (fh) {
case 0x8001:return (-1);
case 0x8002:return (-1);
case 0x8003:return (-1);
}
return (-1);
}
__attribute__((weak))
int _sys_istty (int fh) {
switch (fh) {
case 0x8001:return (1);
case 0x8002:return (1);
case 0x8003:return (1);
}
return (0);
}
__attribute__((weak))
int _sys_read (int fh, uint8_t *buf, uint32_t len, int mode) {
(void)buf;
(void)len;
(void)mode;
switch (fh) {
case 0x8001:return ((int)(len | 0x80000000U));
case 0x8002:return (-1);
case 0x8003:return (-1);
}
return (-1);
}
__attribute__((weak))
int _sys_write (int fh, const uint8_t *buf, uint32_t len, int mode) {
(void)buf;
(void)len;
(void)mode;
switch (fh) {
case 0x8001:return (-1);
case 0x8002:return (0);
case 0x8003:return (0);
}
return (-1);
}
__attribute__((weak))
int _sys_close (int fh) {
switch (fh) {
case 0x8001:return (0);
case 0x8002:return (0);
case 0x8003:return (0);
}
return (-1);
}
__attribute__((weak))
int _sys_open (const char *name, int openmode) {
(void)openmode;
if (name == NULL) return (-1);
if (name[0] == ':'){
if (strcmp(name, ":STDIN") == 0)return (0x8001);
if (strcmp(name, ":STDOUT") == 0)return (0x8002);
if (strcmp(name, ":STDERR") == 0)return (0x8003);
return (-1);
}
return (-1);
}