summaryrefslogtreecommitdiff
path: root/shared/usart_stdio.c
blob: 6a6866edf4c77145f201e15b1223aa271a44e4ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
 * support for stdio output to a usart
 * Karl Palsson, 2015 <karlp@tweak.net.au>
 */

#include <errno.h>
#include <stdio.h>
#include <unistd.h>

#include <libopencm3/stm32/usart.h>

#ifndef STDIO_USART
#define STDIO_USART USART1
#endif

int _write(int file, char *ptr, int len);
int _write(int file, char *ptr, int len)
{
	int i;

	if (file == STDOUT_FILENO || file == STDERR_FILENO) {
		for (i = 0; i < len; i++) {
			if (ptr[i] == '\n') {
				usart_send_blocking(STDIO_USART, '\r');
			}
			usart_send_blocking(STDIO_USART, ptr[i]);
		}
		return i;
	}
	errno = EIO;
	return -1;
}