最新Keil V6编译器,完美解决串口重定向打印,卡BKPT的问题(不使用MicroLIB)

想玩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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值