-
Notifications
You must be signed in to change notification settings - Fork 0
/
retarget.c
104 lines (89 loc) · 2.26 KB
/
retarget.c
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* @brief This for STM32 device retarget printf func to ITM/SWV, Semihost or UART
*
* @file retarget.c
* @author bryan
* @date 2018-07-12
* @note
* There three type retarget according to macro define
* - RETARGET_ITM
*
* - RETARGET_SEMIHOST
* + Add '-specs=rdimon.specs -lc -lrdimon' to link option
* + if want using %f with printf(), add '-u _scanf_float -u _printf_float'
* link option
*
* - RETARGET_UART
* + Add UART handler
* + Remove '-specs=rdimon.specs -lc -lrdimon' link option
* + if want using %f with printf(), add '-u _scanf_float -u _printf_float'
* link option
* When using printf, remember to
* fflush(stdout); // This will flush any pending printf output
* or using retarget_disable_buffer()
* So you don't need to add "\n"
*/
#include "stm32f4xx_hal.h"
#include <stdio.h>
#define RETARGET_UART
#ifdef RETARGET_SEMIHOST
extern void initialise_monitor_handles(void);
#endif
#ifdef RETARGET_UART
extern UART_HandleTypeDef huart1;
#endif
#ifdef RETARGET_ITM
#endif
void retarget_init()
{
#ifdef RETARGET_SEMIHOST
initialise_monitor_handles();
#endif
}
/**
* @brief Write "len" of char from "ptr" to file id "fd", Return number of char written.
*
* @details
* 1- When using semihost mode, don't implement this _write func
* */
#ifndef RETARGET_SEMIHOST
int _write (int fd, char *ptr, int len)
{
int DataIdx;
for (DataIdx = 0; DataIdx < len; DataIdx++)
{
#ifdef RETARGET_UART
HAL_UART_Transmit(&huart1, ptr++, 1, 0xFFFF);
#endif
#ifdef RETARGET_ITM
// the implementation of ITM_SendChar is provided in CMSIS package.
ITM_SendChar(*ptr++);
#endif
}
return len;
}
#endif
//
//int _read (int fd, char *ptr, int len)
//{
// /* Read "len" of char to "ptr" from file id "fd"
// * Return number of char read.
// * Need implementing with UART here. */
// return len;
//}
//
//void _ttywrch(int ch) {
// /* Write one char "ch" to the default console
// * Need implementing with UART here. */
//}
/** @brief disable I/O buffer for printf
* @parm None
* @retval None
* @note Disable I/O buffering for stdout stream
* so that chars are sent out as soon as
* they are printed
*/
void retarget_disable_buffer(void)
{
setvbuf(stdout, NULL, _IONBF, 0);
}