forked from suikan4github/murasaki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uartlogger.cpp
84 lines (62 loc) · 2.06 KB
/
uartlogger.cpp
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
/*
* uartlogger.cpp
*
* Created on: 2018/01/20
* Author: takemasa
*/
#include "uartlogger.hpp"
#include "murasaki_config.hpp"
#include "murasaki_assert.hpp"
// Check if CubeMX generates UART module
#ifdef HAL_UART_MODULE_ENABLED
namespace murasaki {
UartLogger::UartLogger(UartStrategy * uart):
uart_ ( uart)
{
MURASAKI_ASSERT(nullptr != uart)
}
void UartLogger::putMessage(char message[], unsigned int size)
{
MURASAKI_ASSERT(nullptr != message)
MURASAKI_ASSERT(65536 > size);
MURASAKI_ASSERT(IsTaskContext());
uart_->Transmit(reinterpret_cast<uint8_t *>(message), // Message to send
size, // length of message by byte.
static_cast<WaitMilliSeconds>( PLATFORM_CONFIG_DEBUG_SERIAL_TIMEOUT) // wait eternaly
);
}
char UartLogger::getCharacter()
{
char buf;
MURASAKI_ASSERT(IsTaskContext());
uart_->Receive(
reinterpret_cast<uint8_t *>(&buf), // buffer to receive
1 // receive one char
);
return buf;
}
void UartLogger::DoPostMortem(void* debugger_fifo) {
// Set the peripheral handle of UART.
UART_HandleTypeDef * const huart = static_cast<UART_HandleTypeDef * > (GetPeripheralHandle(uart_));
// Set the FIFO.
DebuggerFifo * const fifo = reinterpret_cast<DebuggerFifo *> (debugger_fifo);
// Set FIFO mode to post mortem. Now, FIFO is not synchronizing.
fifo->SetPostMortem();
// Stop all UART activity.
HAL_UART_DeInit(huart);
// Then, restart it.
HAL_UART_Init(huart);
do {
uint16_t transfered_num;
uint8_t data[10]; // data transfer buffer.
do {
// retrieve data from FIFO.
transfered_num = fifo->Get(data, sizeof(data));
HAL_UART_Transmit(huart, data, transfered_num, HAL_MAX_DELAY); // no time out.
} while ( transfered_num != 0);
HAL_UART_Receive(huart, data, 1, HAL_MAX_DELAY); // wait any type in from UART
fifo->ReWind(); // then, rewind the FIFO.
} while (true);
}
} /* namespace murasaki */
#endif // HAL_UART_MODULE_ENABLED