Skip to content

Commit b6aba0d

Browse files
committed
step toward removing stuff (still working)
1 parent 0b7f8e5 commit b6aba0d

File tree

2 files changed

+87
-95
lines changed

2 files changed

+87
-95
lines changed

cores/arduino/Serial.cpp

Lines changed: 72 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,87 @@
2929
#undef Serial
3030
#endif
3131

32+
static tx_buffer_index_t _tx_buffer_head[10];
33+
static tx_buffer_index_t _tx_buffer_tail[10];
34+
static rx_buffer_index_t _rx_buffer_tail[10];
35+
36+
static unsigned char *_rx_buffer[10];
37+
static unsigned char *_tx_buffer[10];
38+
39+
static bool _sending[10];
40+
3241

3342
uint8_t tx_buff[SERIAL_TX_BUFFER_SIZE];
3443
int to_send_i = -1;
3544
int send_i = -1;
3645

37-
typedef enum {
38-
TX_STARTED,
39-
TX_STOPPED
40-
} TxStatus_t;
46+
4147

4248
TxStatus_t tx_status = TX_STOPPED;
4349

50+
#define MAX_UARTS 10
51+
52+
static UART *_uarts[MAX_UARTS];
53+
54+
55+
void uart_callback(uart_callback_args_t *p_args)
56+
{
57+
/* This callback function is not used but it is referenced into
58+
FSP configuration so that (for the moment it is necessary to keep it) */
59+
}
60+
61+
/* -------------------------------------------------------------------------- */
62+
void UART::WrapperCallback(uart_callback_args_t *p_args) {
63+
/* -------------------------------------------------------------------------- */
64+
65+
uint32_t channel = p_args->channel;
66+
67+
switch (p_args->event){
68+
case UART_EVENT_RX_COMPLETE:
69+
{
70+
R_SCI_UART_Read((uart_ctrl_t*)(SciTable[channel].uart_instance->p_ctrl), _rx_buffer[channel], SERIAL_RX_BUFFER_SIZE);
71+
break;
72+
}
73+
case UART_EVENT_ERR_PARITY:
74+
case UART_EVENT_ERR_FRAMING:
75+
case UART_EVENT_ERR_OVERFLOW:
76+
{
77+
break;
78+
}
79+
case UART_EVENT_TX_COMPLETE:
80+
{
81+
if(send_i != to_send_i) {
82+
inc(send_i, SERIAL_TX_BUFFER_SIZE);
83+
R_SCI_UART_Write ((uart_ctrl_t*)(SciTable[channel].uart_instance->p_ctrl), (tx_buff + send_i), 1);
84+
}
85+
else {
86+
tx_status = TX_STOPPED;
87+
}
88+
break;
89+
}
90+
case UART_EVENT_RX_CHAR:
91+
case UART_EVENT_BREAK_DETECT:
92+
case UART_EVENT_TX_DATA_EMPTY:
93+
{
94+
break;
95+
}
96+
}
97+
98+
}
99+
100+
/* -------------------------------------------------------------------------- */
101+
UART::UART(int ch) :
102+
tx_st(TX_STOPPED),
103+
_channel(ch)
104+
/* -------------------------------------------------------------------------- */
105+
{
106+
_uarts[_channel] = this;
107+
}
108+
109+
44110

45-
static tx_buffer_index_t _tx_buffer_head[10];
46-
static tx_buffer_index_t _tx_buffer_tail[10];
47-
static rx_buffer_index_t _rx_buffer_tail[10];
48111

49-
static unsigned char *_rx_buffer[10];
50-
static unsigned char *_tx_buffer[10];
51112

52-
static bool _sending[10];
53113

54114

55115
UART::UART(
@@ -63,9 +123,7 @@ UART::UART(
63123
{ }
64124

65125

66-
UART::UART(int ch) :
67-
_channel(ch)
68-
{ }
126+
69127

70128
/* -------------------------------------------------------------------------- */
71129
bool UART::setUpUartIrqs(uart_cfg_t &cfg) {
@@ -140,7 +198,7 @@ void UART::begin(unsigned long baudrate, uint16_t config) {
140198

141199
_config = *_uart_config;
142200

143-
_config.p_callback = irq_callback;
201+
_config.p_callback = UART::WrapperCallback;
144202

145203
switch(config){
146204
case SERIAL_8N1:
@@ -253,49 +311,7 @@ void UART::flush() {
253311

254312

255313

256-
void irq_callback(uart_callback_args_t *p_args) {
257-
uint32_t channel = p_args->channel;
258-
259-
switch (p_args->event){
260-
case UART_EVENT_RX_COMPLETE:
261-
{
262-
R_SCI_UART_Read((uart_ctrl_t*)(SciTable[channel].uart_instance->p_ctrl), _rx_buffer[channel], SERIAL_RX_BUFFER_SIZE);
263-
break;
264-
}
265-
case UART_EVENT_ERR_PARITY:
266-
case UART_EVENT_ERR_FRAMING:
267-
case UART_EVENT_ERR_OVERFLOW:
268-
{
269-
break;
270-
}
271-
case UART_EVENT_TX_COMPLETE:
272-
{
273-
if(send_i != to_send_i) {
274-
inc(send_i, SERIAL_TX_BUFFER_SIZE);
275-
R_SCI_UART_Write ((uart_ctrl_t*)(SciTable[channel].uart_instance->p_ctrl), (tx_buff + send_i), 1);
276-
}
277-
else {
278-
tx_status = TX_STOPPED;
279-
}
280-
break;
281-
}
282-
case UART_EVENT_RX_CHAR:
283-
case UART_EVENT_BREAK_DETECT:
284-
case UART_EVENT_TX_DATA_EMPTY:
285-
{
286-
break;
287-
}
288-
}
289-
}
290314

291-
bool is_write_buffer_possible(int to_send, int send, int _max ) {
292-
int a = next(to_send,_max);
293-
294-
if(a == send) {
295-
return false;
296-
}
297-
return true;
298-
}
299315

300316

301317

@@ -420,40 +436,6 @@ void UART::enableUartIrqs() {
420436
}
421437

422438

423-
void uart_callback(uart_callback_args_t *p_args)
424-
{
425-
uint32_t channel = p_args->channel;
426-
switch (p_args->event){
427-
case UART_EVENT_RX_COMPLETE:
428-
{
429-
R_SCI_UART_Read((uart_ctrl_t*)(SciTable[channel].uart_instance->p_ctrl), _rx_buffer[channel], SERIAL_RX_BUFFER_SIZE);
430-
break;
431-
}
432-
case UART_EVENT_ERR_PARITY:
433-
case UART_EVENT_ERR_FRAMING:
434-
case UART_EVENT_ERR_OVERFLOW:
435-
{
436-
break;
437-
}
438-
case UART_EVENT_TX_COMPLETE:
439-
{
440-
if (_tx_buffer_head[channel] != _tx_buffer_tail[channel]) {
441-
R_SCI_UART_Write ((uart_ctrl_t*)(SciTable[channel].uart_instance->p_ctrl), &_tx_buffer[channel][_tx_buffer_tail[channel]], 1);
442-
_tx_buffer_tail[channel] = (tx_buffer_index_t)((_tx_buffer_tail[channel] + 1) % SERIAL_TX_BUFFER_SIZE);
443-
} else {
444-
_sending[channel] = false;
445-
}
446-
break;
447-
}
448-
case UART_EVENT_RX_CHAR:
449-
case UART_EVENT_BREAK_DETECT:
450-
case UART_EVENT_TX_DATA_EMPTY:
451-
{
452-
break;
453-
}
454-
}
455-
}
456-
457439

458440
#if SERIAL_HOWMANY > 0
459441
UART _UART1_(UART1_CHANNEL);

cores/arduino/Serial.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
// often work, but occasionally a race condition can occur that makes
4646
// Serial behave erratically. See https://github.com/arduino/Arduino/issues/2405
4747

48+
4849
#define SERIAL_TX_BUFFER_SIZE 512
4950
#define SERIAL_RX_BUFFER_SIZE 512
5051

@@ -103,13 +104,15 @@ typedef uint8_t rx_buffer_index_t;
103104
#define SERIAL_7O2 0x3C
104105
#define SERIAL_8O2 0x3E
105106

106-
void irq_callback(uart_callback_args_t *p_args);
107-
108-
107+
typedef enum {
108+
TX_STARTED,
109+
TX_STOPPED
110+
} TxStatus_t;
109111

110112

111113
class UART : public arduino::HardwareSerial {
112114
public:
115+
static void WrapperCallback(uart_callback_args_t *p_args);
113116
UART(sci_uart_instance_ctrl_t *_uart_ctrl,
114117
const uart_cfg_t* _uart_config,
115118
dtc_instance_ctrl_t* _dtc_ctrl, int ch);
@@ -149,10 +152,17 @@ class UART : public arduino::HardwareSerial {
149152
volatile bool _begin;
150153

151154
private:
155+
TxStatus_t tx_st;
156+
uint8_t tx_buffer[SERIAL_TX_BUFFER_SIZE];
157+
uint8_t rx_buffer[SERIAL_RX_BUFFER_SIZE];
158+
159+
160+
152161
sci_uart_instance_ctrl_t uart_ctrl;
153162
uart_cfg_t uart_cfg;
154163
int _channel;
155164

165+
156166

157167

158168
bool setUpUartIrqs(uart_cfg_t &cfg);
@@ -162,8 +172,8 @@ class UART : public arduino::HardwareSerial {
162172
};
163173

164174
inline void inc(int &x,int _max) { x = ++x % _max; }
165-
inline int previous(int x, int _max) { return ((--x) >= 0) ? x : _max -1; }
166-
inline int next(int x, int _max) {return (++x % _max); }
175+
inline int previous(int x, int _max) { return ((--x) >= 0) ? x : _max -1; }
176+
inline int next(int x, int _max) {return (++x % _max); }
167177

168178

169179

0 commit comments

Comments
 (0)