@@ -32,7 +32,6 @@ HardwareSerial::HardwareSerial(XMC_UART_t *xmc_uart_config,
3232 RingBuffer *tx_buffer) {
3333 _XMC_UART_config = xmc_uart_config;
3434 _rx_buffer = rx_buffer;
35- _tx_buffer = tx_buffer;
3635}
3736
3837// Public Methods //////////////////////////////////////////////////////////////
@@ -118,12 +117,7 @@ int HardwareSerial::available(void) {
118117 return SERIAL_BUFFER_SIZE - _rx_buffer->_iTail + head;
119118}
120119
121- int HardwareSerial::availableForWrite (void ) {
122- int tail = _tx_buffer->_iTail ; // Snapshot index affected by irq
123- if (_tx_buffer->_iHead >= tail)
124- return SERIAL_BUFFER_SIZE - 1 - _tx_buffer->_iHead + tail;
125- return tail - _tx_buffer->_iHead - 1 ;
126- }
120+ int HardwareSerial::availableForWrite (void ) { return 1 ; }
127121
128122int HardwareSerial::peek (void ) {
129123 if (_rx_buffer->_iHead == _rx_buffer->_iTail )
@@ -144,63 +138,21 @@ int HardwareSerial::read(void) {
144138}
145139
146140void HardwareSerial::flush (void ) {
147- while (_tx_buffer->_iHead != _tx_buffer->_iTail )
148- ; // wait for transmit data to be sent
149141
150142 while (XMC_USIC_CH_GetTransmitBufferStatus (_XMC_UART_config->channel ) ==
151143 XMC_USIC_CH_TBUF_STATUS_BUSY)
152144 ;
153145}
154146
155147size_t HardwareSerial::write (const uint8_t uc_data) {
156- // Is the hardware currently busy?
157- #if defined(SERIAL_USE_U1C1)
158- if (_tx_buffer->_iTail != _tx_buffer->_iHead )
159- #else
160- if ((XMC_USIC_CH_GetTransmitBufferStatus (_XMC_UART_config->channel ) ==
161- XMC_USIC_CH_TBUF_STATUS_BUSY) ||
162- (_tx_buffer->_iTail != _tx_buffer->_iHead ))
163- #endif
164- {
165- // If busy we buffer
166- int nextWrite = _tx_buffer->_iHead + 1 ;
167- if (nextWrite >= SERIAL_BUFFER_SIZE)
168- nextWrite = 0 ;
169-
170- // This should always be false but in case transmission is completed before buffer, we need
171- // to reenable IRQ
172- if (XMC_USIC_CH_GetTransmitBufferStatus (_XMC_UART_config->channel ) !=
173- XMC_USIC_CH_TBUF_STATUS_BUSY) {
174- XMC_UART_CH_EnableEvent (_XMC_UART_config->channel , XMC_UART_CH_EVENT_TRANSMIT_BUFFER);
175- XMC_UART_CH_Transmit (_XMC_UART_config->channel ,
176- _tx_buffer->_aucBuffer [_tx_buffer->_iTail ]);
177- _tx_buffer->_iTail ++;
178- if (_tx_buffer->_iTail >= SERIAL_BUFFER_SIZE)
179- _tx_buffer->_iTail %= SERIAL_BUFFER_SIZE; // If iTail is larger than Serial Buffer
180- // Size calculate the correct index value
181- }
182-
183- unsigned long startTime = millis ();
184- while (_tx_buffer->_iTail == nextWrite) {
185- if (millis () - startTime > 1000 ) {
186- return 0 ; // Spin locks if we're about to overwrite the buffer. This continues once
187- // the data is
188- // sent
189- }
190- }
191-
192- _tx_buffer->_aucBuffer [_tx_buffer->_iHead ] = uc_data;
193- _tx_buffer->_iHead = nextWrite;
194- } else {
195- // Make sure TX interrupt is enabled
196- XMC_UART_CH_EnableEvent (_XMC_UART_config->channel , XMC_UART_CH_EVENT_TRANSMIT_BUFFER);
197- // Bypass buffering and send character directly
198- XMC_UART_CH_Transmit (_XMC_UART_config->channel , uc_data);
199- }
148+ // For sending, write immediately
149+ // This API already have a check for available buffer
150+ XMC_UART_CH_Transmit (_XMC_UART_config->channel , uc_data);
200151 return 1 ;
201152}
202153
203154void HardwareSerial::IrqHandler (void ) {
155+ // Receive data Interrupt handler
204156 uint32_t status = XMC_UART_CH_GetStatusFlag (_XMC_UART_config->channel );
205157
206158 // Did we receive data?
@@ -214,24 +166,6 @@ void HardwareSerial::IrqHandler(void) {
214166 (USIC_CH_RBUFSR_RDV0_Msk | USIC_CH_RBUFSR_RDV1_Msk))
215167 _rx_buffer->store_char (XMC_UART_CH_GetReceivedData (_XMC_UART_config->channel ));
216168 }
217-
218- // Do we need to keep sending data?
219- if ((status & XMC_UART_CH_STATUS_FLAG_TRANSMIT_BUFFER_INDICATION) != 0U ) {
220- XMC_UART_CH_ClearStatusFlag (_XMC_UART_config->channel ,
221- XMC_UART_CH_STATUS_FLAG_TRANSMIT_BUFFER_INDICATION);
222-
223- if (_tx_buffer->_iTail != _tx_buffer->_iHead ) {
224- XMC_UART_CH_Transmit (_XMC_UART_config->channel ,
225- _tx_buffer->_aucBuffer [_tx_buffer->_iTail ]);
226- _tx_buffer->_iTail ++;
227- if (_tx_buffer->_iTail >= SERIAL_BUFFER_SIZE)
228- _tx_buffer->_iTail %= SERIAL_BUFFER_SIZE; // If iTail is larger than Serial Buffer
229- // Size calculate the correct index value
230- } else {
231- // Mask off transmit interrupt so we don't get it any more
232- XMC_UART_CH_DisableEvent (_XMC_UART_config->channel , XMC_UART_CH_EVENT_TRANSMIT_BUFFER);
233- }
234- }
235169}
236170
237171// ****************************************************************************
0 commit comments