@@ -103,7 +103,7 @@ void Uart::end(void) {
103103 // Disable UART interrupt in NVIC
104104 NVIC_DisableIRQ (_XMC_UART_config->irq_num );
105105 // Clear any received data after stopping interrupts
106- _rx_buffer->_iHead = _rx_buffer-> _iTail ;
106+ _rx_buffer->clear () ;
107107}
108108
109109void Uart::setInterruptPriority (uint32_t priority) {
@@ -112,92 +112,28 @@ void Uart::setInterruptPriority(uint32_t priority) {
112112
113113uint32_t Uart::getInterruptPriority () { return NVIC_GetPriority (_XMC_UART_config->irq_num ); }
114114
115- int Uart::available (void ) {
116- int head = _rx_buffer->_iHead ; // Snapshot index affected by irq
117- if (head >= _rx_buffer->_iTail )
118- return head - _rx_buffer->_iTail ;
119- return SERIAL_BUFFER_SIZE - _rx_buffer->_iTail + head;
120- }
115+ int Uart::available (void ) { return _rx_buffer->available (); }
121116
122- int Uart::availableForWrite (void ) {
123- int tail = _tx_buffer->_iTail ; // Snapshot index affected by irq
124- if (_tx_buffer->_iHead >= tail)
125- return SERIAL_BUFFER_SIZE - 1 - _tx_buffer->_iHead + tail;
126- return tail - _tx_buffer->_iHead - 1 ;
127- }
117+ int Uart::availableForWrite (void ) { return _tx_buffer->availableForStore (); }
128118
129- int Uart::peek (void ) {
130- if (_rx_buffer->_iHead == _rx_buffer->_iTail )
131- return -1 ;
132- return _rx_buffer->_aucBuffer [_rx_buffer->_iTail ];
133- }
119+ int Uart::peek (void ) { return _rx_buffer->peek (); }
134120
135- int Uart::read (void ) {
136- // if the head isn't ahead of the tail, we don't have any characters
137- if (_rx_buffer->_iHead == _rx_buffer->_iTail )
138- return -1 ;
139-
140- uint8_t uc = _rx_buffer->_aucBuffer [_rx_buffer->_iTail ];
141- _rx_buffer->_iTail ++;
142- if (_rx_buffer->_iTail >= SERIAL_BUFFER_SIZE)
143- _rx_buffer->_iTail = 0 ;
144- return uc;
145- }
121+ int Uart::read (void ) { return _rx_buffer->read_char (); }
146122
147123void Uart::flush (void ) {
148- while (_tx_buffer->_iHead != _tx_buffer->_iTail )
149- ; // wait for transmit data to be sent
150-
151- while (XMC_USIC_CH_GetTransmitBufferStatus (_XMC_UART_config->channel ) ==
152- XMC_USIC_CH_TBUF_STATUS_BUSY)
153- ;
124+ while (_tx_buffer->available () ||
125+ XMC_USIC_CH_GetTransmitBufferStatus (_XMC_UART_config->channel ) ==
126+ XMC_USIC_CH_TBUF_STATUS_BUSY) {
127+ }
154128}
155129
156130size_t Uart::write (const uint8_t uc_data) {
157- // Is the hardware currently busy?
158- #if defined(SERIAL_USE_U1C1)
159- if (_tx_buffer->_iTail != _tx_buffer->_iHead )
160- #else
161- if ((XMC_USIC_CH_GetTransmitBufferStatus (_XMC_UART_config->channel ) ==
162- XMC_USIC_CH_TBUF_STATUS_BUSY) ||
163- (_tx_buffer->_iTail != _tx_buffer->_iHead ))
164- #endif
165- {
166- // If busy we buffer
167- int nextWrite = _tx_buffer->_iHead + 1 ;
168- if (nextWrite >= SERIAL_BUFFER_SIZE)
169- nextWrite = 0 ;
170-
171- // This should always be false but in case transmission is completed before buffer, we need
172- // to reenable IRQ
173- if (XMC_USIC_CH_GetTransmitBufferStatus (_XMC_UART_config->channel ) !=
174- XMC_USIC_CH_TBUF_STATUS_BUSY) {
175- XMC_UART_CH_EnableEvent (_XMC_UART_config->channel , XMC_UART_CH_EVENT_TRANSMIT_BUFFER);
176- XMC_UART_CH_Transmit (_XMC_UART_config->channel ,
177- _tx_buffer->_aucBuffer [_tx_buffer->_iTail ]);
178- _tx_buffer->_iTail ++;
179- if (_tx_buffer->_iTail >= SERIAL_BUFFER_SIZE)
180- _tx_buffer->_iTail %= SERIAL_BUFFER_SIZE; // If iTail is larger than Serial Buffer
181- // Size calculate the correct index value
182- }
183-
184- unsigned long startTime = millis ();
185- while (_tx_buffer->_iTail == nextWrite) {
186- if (millis () - startTime > 1000 ) {
187- return 0 ; // Spin locks if we're about to overwrite the buffer. This continues once
188- // the data is
189- // sent
190- }
191- }
131+ // Is the hardware currently busy?
192132
193- _tx_buffer->_aucBuffer [_tx_buffer->_iHead ] = uc_data;
194- _tx_buffer->_iHead = nextWrite;
195- } else {
196- // Make sure TX interrupt is enabled
197- XMC_UART_CH_EnableEvent (_XMC_UART_config->channel , XMC_UART_CH_EVENT_TRANSMIT_BUFFER);
198- // Bypass buffering and send character directly
199- XMC_UART_CH_Transmit (_XMC_UART_config->channel , uc_data);
200- }
133+ // Make sure TX interrupt is enabled
134+ XMC_UART_CH_EnableEvent (_XMC_UART_config->channel , XMC_UART_CH_EVENT_TRANSMIT_BUFFER);
135+ // Bypass buffering and send character directly
136+ XMC_UART_CH_Transmit (_XMC_UART_config->channel , uc_data);
201137 return 1 ;
202138}
203139
0 commit comments