Skip to content

Commit 09409f0

Browse files
updated asr650x Serial.read
1 parent 002673e commit 09409f0

File tree

3 files changed

+99
-8
lines changed

3 files changed

+99
-8
lines changed

cores/asr650x/Serial/HardwareSerial.cpp

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,51 @@
1111
HardwareSerial Serial(UART_NUM_0);
1212
HardwareSerial Serial1(UART_NUM_1);
1313

14+
uart_rxbuff_t _rxbuff[2];
15+
1416
HardwareSerial::HardwareSerial(int8_t uart_num)
1517
:_uart_num(uart_num)
1618
,_rx(-1)
1719
,_tx(-1)
1820
{}
1921

22+
void writeRxToBuff0()
23+
{
24+
uint8_t uart_num = 0;
25+
while(UART_1_SpiUartGetRxBufferSize())
26+
{
27+
if( ((_rxbuff[uart_num].rx_w + 1) % UART_RX_SIZE) != _rxbuff[uart_num].rx_r )
28+
{
29+
_rxbuff[uart_num].rx_buf[_rxbuff[uart_num].rx_w ++] = UART_1_UartGetByte();;
30+
_rxbuff[uart_num].rx_w = _rxbuff[uart_num].rx_w % UART_RX_SIZE;
31+
}
32+
else
33+
{
34+
UART_1_UartGetByte();
35+
}
36+
}
37+
UART_1_ClearRxInterruptSource(UART_1_INTR_RX_NOT_EMPTY);
38+
}
39+
40+
void writeRxToBuff1()
41+
{
42+
uint8_t uart_num = 1;
43+
while(UART_2_SpiUartGetRxBufferSize())
44+
{
45+
if( ((_rxbuff[uart_num].rx_w + 1) % UART_RX_SIZE) != _rxbuff[uart_num].rx_r )
46+
{
47+
_rxbuff[uart_num].rx_buf[_rxbuff[uart_num].rx_w ++] = UART_2_UartGetByte();;
48+
_rxbuff[uart_num].rx_w = _rxbuff[uart_num].rx_w % UART_RX_SIZE;
49+
}
50+
else
51+
{
52+
UART_2_UartGetByte();
53+
}
54+
}
55+
UART_2_ClearRxInterruptSource(UART_2_INTR_RX_NOT_EMPTY);
56+
}
57+
58+
2059
bool HardwareSerial::begin(uint32_t baud, uint32_t config, int rxPin, int txPin, bool invert, unsigned long timeout_ms)
2160
{
2261
if(_uart_num == 0 && rxPin < 0 && txPin < 0) {
@@ -96,6 +135,20 @@ bool HardwareSerial::begin(uint32_t baud, uint32_t config, int rxPin, int txPin,
96135
UART_2_UART_TX_CTRL_REG =tx_ctrl0;
97136
UART_2_TX_CTRL_REG = tx_ctrl1;
98137
}
138+
_rxbuff[_uart_num].rx_r=0;
139+
_rxbuff[_uart_num].rx_w=0;
140+
_rxbuff[_uart_num].rx_buf=(uint8_t*) malloc(UART_RX_SIZE);
141+
142+
if( _uart_num == UART_NUM_0)
143+
{
144+
UART_1_SCB_IRQ_StartEx(writeRxToBuff0);
145+
}
146+
else
147+
{
148+
UART_2_SCB_IRQ_StartEx(writeRxToBuff1);
149+
}
150+
151+
99152
return true;
100153
/*
101154
printf("config %d\r\n",config);
@@ -153,9 +206,12 @@ void HardwareSerial::setDebugOutput(bool en)
153206

154207
int HardwareSerial::available(void)
155208
{
209+
/*
210+
156211
uint8_t buffsize;
157212
//for(uint32_t i=0;i<(23040000/SerialBaud);i++)
158213
//{
214+
159215
if( _uart_num == UART_NUM_0)
160216
{
161217
buffsize=UART_1_SpiUartGetRxBufferSize();
@@ -168,7 +224,14 @@ int HardwareSerial::available(void)
168224
return buffsize;
169225
}
170226
//}
171-
return 0;
227+
return 0;*/
228+
229+
if(_rxbuff[_uart_num].rx_r == _rxbuff[_uart_num].rx_w)
230+
return 0;
231+
if(_rxbuff[_uart_num].rx_r < _rxbuff[_uart_num].rx_w)
232+
return _rxbuff[_uart_num].rx_w - _rxbuff[_uart_num].rx_r;
233+
else
234+
return UART_RX_SIZE - _rxbuff[_uart_num].rx_r + _rxbuff[_uart_num].rx_w;
172235
}
173236

174237
void HardwareSerial::delayByte(void)
@@ -190,15 +253,14 @@ int HardwareSerial::availableForWrite(void)
190253

191254
int HardwareSerial::peek(void)
192255
{
193-
// if (available()) {
194-
// return uartPeek(_uart);
195-
// }
196-
// return -1;
197-
return 0;
256+
if(_rxbuff[_uart_num].rx_r == _rxbuff[_uart_num].rx_w)
257+
return -1;
258+
return _rxbuff[_uart_num].rx_buf[_rxbuff[_uart_num].rx_r];
198259
}
199260

200261
int HardwareSerial::read(void)
201262
{
263+
/*
202264
if(available()) {
203265
if( _uart_num == UART_NUM_0)
204266
{
@@ -209,7 +271,13 @@ int HardwareSerial::read(void)
209271
return UART_2_UartGetByte();
210272
}
211273
}
212-
return (uint32)(-1);
274+
return (uint32)(-1);*/
275+
if(_rxbuff[_uart_num].rx_r == _rxbuff[_uart_num].rx_w)
276+
return -1;
277+
278+
uint8_t data = _rxbuff[_uart_num].rx_buf[_rxbuff[_uart_num].rx_r++];
279+
_rxbuff[_uart_num].rx_r = _rxbuff[_uart_num].rx_r % UART_RX_SIZE;
280+
return data;
213281
}
214282

215283
int HardwareSerial::read(uint8_t* buff, uint32_t timeout)
@@ -243,6 +311,8 @@ int HardwareSerial::read(uint8_t* buff, uint32_t timeout)
243311

244312
void HardwareSerial::flush()
245313
{
314+
_rxbuff[_uart_num].rx_r = 0;
315+
_rxbuff[_uart_num].rx_w = 0;
246316
if( _uart_num == UART_NUM_0)
247317
{
248318
UART_1_SpiUartClearRxBuffer();

cores/asr650x/Serial/HardwareSerial.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,27 @@
4949
#include <project.h>
5050
#include "Stream.h"
5151
#include "cytypes.h"
52+
#include <UART_1_SCB_IRQ.h>
53+
#include <UART_2_SCB_IRQ.h>
54+
5255

5356
#define UART_NUM_0 0
5457
#define UART_NUM_1 1
5558

59+
#define UART_BUFF_SIZE 255
60+
#define UART_RX_SIZE (UART_BUFF_SIZE+1)
61+
62+
typedef struct {
63+
uint8_t * rx_buf;
64+
uint16_t rx_w;
65+
uint16_t rx_r;
66+
} uart_rxbuff_t;
67+
5668
class HardwareSerial: public Stream
5769
{
5870
public:
5971
HardwareSerial(int8_t uart_nr);
60-
72+
6173
bool begin(uint32_t baud=115200, uint32_t config=SERIAL_8N1, int rxPin=-1, int txPin=-1, bool invert=false, unsigned long timeout_ms = 20000UL);
6274
void end();
6375
void updateBaudRate(unsigned long baud);

cores/asr650x/projects/PSoC4/UART_2_SCB_IRQ.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
* disclaimers, and limitations in the end user license agreement accompanying
1313
* the software package with which this file was provided.
1414
*******************************************************************************/
15+
16+
#ifdef __cplusplus
17+
extern "C" {
18+
#endif
19+
1520
#if !defined(CY_ISR_UART_2_SCB_IRQ_H)
1621
#define CY_ISR_UART_2_SCB_IRQ_H
1722

@@ -67,5 +72,9 @@ void UART_2_SCB_IRQ_ClearPending(void);
6772

6873
#endif /* CY_ISR_UART_2_SCB_IRQ_H */
6974

75+
#ifdef __cplusplus
76+
}
77+
#endif
78+
7079

7180
/* [] END OF FILE */

0 commit comments

Comments
 (0)