Skip to content

Commit 2edb077

Browse files
committed
Signed-off-by: Bigbits <[email protected]>
1 parent de11bcc commit 2edb077

File tree

6 files changed

+290
-46
lines changed

6 files changed

+290
-46
lines changed

cores/arduino/RingBuffer.h

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
Copyright (c) 2014 Arduino. All right reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
See the GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#ifdef __cplusplus
20+
21+
#ifndef _RING_BUFFER_
22+
#define _RING_BUFFER_
23+
24+
#include <stdint.h>
25+
26+
// Define constants and variables for buffering incoming serial data. We're
27+
// using a ring buffer (I think), in which head is the index of the location
28+
// to which to write the next incoming character and tail is the index of the
29+
// location from which to read.
30+
#define SERIAL_BUFFER_SIZE 64
31+
32+
template <int N>
33+
class RingBufferN
34+
{
35+
public:
36+
uint8_t _aucBuffer[N] ;
37+
volatile int _iHead ;
38+
volatile int _iTail ;
39+
40+
public:
41+
RingBufferN( void ) ;
42+
void store_char( uint8_t c ) ;
43+
void clear();
44+
int read_char();
45+
int available();
46+
int availableForStore();
47+
int peek();
48+
bool isFull();
49+
50+
private:
51+
int nextIndex(int index);
52+
};
53+
54+
typedef RingBufferN<SERIAL_BUFFER_SIZE> RingBuffer;
55+
56+
57+
template <int N>
58+
RingBufferN<N>::RingBufferN( void )
59+
{
60+
memset( _aucBuffer, 0, N ) ;
61+
clear();
62+
}
63+
64+
template <int N>
65+
void RingBufferN<N>::store_char( uint8_t c )
66+
{
67+
int i = nextIndex(_iHead);
68+
69+
// if we should be storing the received character into the location
70+
// just before the tail (meaning that the head would advance to the
71+
// current location of the tail), we're about to overflow the buffer
72+
// and so we don't write the character or advance the head.
73+
if ( i != _iTail )
74+
{
75+
_aucBuffer[_iHead] = c ;
76+
_iHead = i ;
77+
}
78+
}
79+
80+
template <int N>
81+
void RingBufferN<N>::clear()
82+
{
83+
_iHead = 0;
84+
_iTail = 0;
85+
}
86+
87+
template <int N>
88+
int RingBufferN<N>::read_char()
89+
{
90+
if(_iTail == _iHead)
91+
return -1;
92+
93+
uint8_t value = _aucBuffer[_iTail];
94+
_iTail = nextIndex(_iTail);
95+
96+
return value;
97+
}
98+
99+
template <int N>
100+
int RingBufferN<N>::available()
101+
{
102+
int delta = _iHead - _iTail;
103+
104+
if(delta < 0)
105+
return N + delta;
106+
else
107+
return delta;
108+
}
109+
110+
template <int N>
111+
int RingBufferN<N>::availableForStore()
112+
{
113+
if (_iHead >= _iTail)
114+
return N - 1 - _iHead + _iTail;
115+
else
116+
return _iTail - _iHead - 1;
117+
}
118+
119+
template <int N>
120+
int RingBufferN<N>::peek()
121+
{
122+
if(_iTail == _iHead)
123+
return -1;
124+
125+
return _aucBuffer[_iTail];
126+
}
127+
128+
template <int N>
129+
int RingBufferN<N>::nextIndex(int index)
130+
{
131+
return (uint32_t)(index + 1) % N;
132+
}
133+
134+
template <int N>
135+
bool RingBufferN<N>::isFull()
136+
{
137+
return (nextIndex(_iHead) == _iTail);
138+
}
139+
140+
#endif /* _RING_BUFFER_ */
141+
142+
#endif /* __cplusplus */

cores/arduino/UARTClass.cpp

Lines changed: 108 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,79 +24,158 @@
2424
#include "pins_arduino.h"
2525
#include "uarths.h"
2626
#include "fpioa.h"
27+
#include "sysctl.h"
2728

28-
UARTClass Serial;
29+
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
30+
UARTHSClass Serial;
31+
UARTClass Serial1(UART_DEVICE_1);
32+
UARTClass Serial2(UART_DEVICE_2);
33+
UARTClass Serial3(UART_DEVICE_3);
34+
#endif
35+
36+
static int uart_rec_callback(void *ctx);
37+
static int uarths_rec_callback(void *ctx);
2938

3039
// Public Methods //////////////////////////////////////////////////////////////
31-
UARTClass::UARTClass(void)
40+
UARTClass::UARTClass()
3241
{
33-
_rxPin = RX;
34-
_txPin = TX;
42+
3543
}
3644

37-
UARTClass::UARTClass(uint8_t rxPin, uint8_t txPin)
45+
UARTClass::UARTClass(uart_device_number_t device_select)
3846
{
39-
_rxPin = rxPin;
40-
_txPin = txPin;
47+
this->_uart = device_select;
48+
switch(this->_uart){
49+
case UART_DEVICE_1:
50+
this->_rxfunc = FUNC_UART1_RX;
51+
this->_txfunc = FUNC_UART1_TX;
52+
break;
53+
case UART_DEVICE_2:
54+
this->_rxfunc = FUNC_UART2_RX;
55+
this->_txfunc = FUNC_UART2_TX;
56+
break;
57+
case UART_DEVICE_3:
58+
this->_rxfunc = FUNC_UART3_RX;
59+
this->_txfunc = FUNC_UART3_TX;
60+
break;
61+
case UART_DEVICE_MAX:
62+
break;
63+
}
4164
}
4265

43-
4466
void
45-
UARTClass::begin(uint32_t bauds)
67+
UARTClass::begin(uint32_t dwBaudRate, uint8_t _rx, uint8_t _tx)
4668
{
47-
fpioa_set_function((int)_rxPin, FUNC_UARTHS_RX);
48-
fpioa_set_function((int)_txPin, FUNC_UARTHS_TX);
49-
uarths_config(bauds, UARTHS_STOP_1);
69+
fpioa_set_function((int)_rx, this->_rxfunc);
70+
fpioa_set_function((int)_tx, this->_txfunc);
71+
uart_init(this->_uart);
72+
uart_configure(this->_uart, dwBaudRate, UART_BITWIDTH_8BIT, UART_STOP_1, UART_PARITY_NONE);
73+
this->_buff = new RingBuffer();
74+
uart_set_receive_trigger(this->_uart, UART_RECEIVE_FIFO_1);
75+
uart_irq_register(this->_uart, UART_RECEIVE, uart_rec_callback, this, 5);
76+
sysctl_enable_irq();
5077
}
5178

52-
53-
5479
void
55-
UARTClass::end(void) //TODO k210 sdk not have api
80+
UARTClass::end(void)
5681
{
57-
82+
delete this->_buff;
83+
uart_irq_unregister(this->_uart, UART_RECEIVE);
5884
}
5985

60-
6186
int
62-
UARTClass::available(void) //TODO Get the number of bytes (characters) available for reading from the serial port.
87+
UARTClass::available(void)
6388
{
64-
return(1);
89+
return this->_buff->available();
6590
}
6691

67-
6892
int
6993
UARTClass::availableForWrite(void) //TODO Get the number of bytes (characters) available for writing from the serial port.
7094
{
7195
return (1);
7296
}
7397

74-
7598
int
76-
UARTClass::peek(void) //TODO Returns the next byte (character) of incoming serial data without removing it from the internal serial buffer.
99+
UARTClass::peek(void)
77100
{
78-
return (-1);
101+
return this->_buff->peek();
79102
}
80103

81-
82104
int
83105
UARTClass::read(void)
84106
{
107+
while(this->_buff->available()){
108+
return this->_buff->read_char();
109+
}
110+
}
85111

86-
return (uarths_getc());
112+
void
113+
UARTClass::flush(void)
114+
{
115+
this->_buff->clear();
87116
}
88117

118+
size_t
119+
UARTClass::write(const uint8_t c)
120+
{
121+
while (uart[this->_uart]->LSR & (1u << 5))
122+
continue;
123+
uart[this->_uart]->THR = c;
124+
return 0;
125+
}
89126

90-
void
91-
UARTClass::flush(void) //TODO Waits for the transmission of outgoing serial data to complete.
127+
static int
128+
uart_rec_callback(void *ctx)
92129
{
130+
char data;
131+
auto &driver = *reinterpret_cast<UARTClass *>(ctx);
132+
while(uart_receive_data(driver._uart, &data, 1)){
133+
driver._buff->store_char(data);
134+
}
135+
return 0;
93136
}
94137

138+
UARTHSClass::UARTHSClass()
139+
{
95140

96-
size_t
97-
UARTClass::write(const uint8_t uc_data)
141+
}
142+
143+
void
144+
UARTHSClass::begin(uint32_t dwBaudRate, uint8_t _rx, uint8_t _tx)
145+
{
146+
fpioa_set_function((int)_rx, FUNC_UARTHS_RX);
147+
fpioa_set_function((int)_tx, FUNC_UARTHS_TX);
148+
uarths_init();
149+
uarths_config(dwBaudRate, UARTHS_STOP_1);
150+
this->_buff = new RingBuffer();
151+
this->_buff->clear();
152+
uarths_set_interrupt_cnt(UARTHS_RECEIVE, 0);
153+
uarths_set_irq(UARTHS_RECEIVE, uarths_rec_callback, this,1);
154+
sysctl_enable_irq();
155+
}
156+
157+
void
158+
UARTHSClass::end()
98159
{
160+
delete _buff;
161+
plic_irq_unregister(IRQN_UARTHS_INTERRUPT);
162+
}
99163

164+
size_t
165+
UARTHSClass::write(const uint8_t uc_data)
166+
{
100167
uarths_putchar(uc_data);
101168
return (1);
102169
}
170+
171+
static int
172+
uarths_rec_callback(void *ctx)
173+
{
174+
int data;
175+
auto &driver = *reinterpret_cast<UARTHSClass *>(ctx);
176+
data = uarths_getc();
177+
if(data != 0){
178+
driver._buff->store_char((char)data);
179+
}
180+
return 0;
181+
}

cores/arduino/UARTClass.h

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@
2323
#include "HardwareSerial.h"
2424
#include "uarths.h"
2525
#include "uart.h"
26+
#include "fpioa.h"
27+
#include "pins_arduino.h"
28+
#include "RingBuffer.h"
2629

2730
class UARTClass : public HardwareSerial
2831
{
2932
public:
30-
UARTClass(uint8_t rxPin, uint8_t txPin);
31-
UARTClass(void);
32-
void begin(uint32_t dwBaudRate);
33+
UARTClass();
34+
UARTClass(uart_device_number_t device_select);
35+
void begin(uint32_t dwBaudRate, uint8_t _rx = 6, uint8_t _tx = 7);
3336
void end(void);
3437
int available(void);
3538
int availableForWrite(void);
@@ -40,12 +43,30 @@ class UARTClass : public HardwareSerial
4043
using Print::write; // pull in write(str) and write(buf, size) from Print
4144

4245
operator bool() {return (true);}; // UART always active
46+
//protected:
47+
RingBuffer *_buff;
48+
uint32_t _timeout = 1000;
49+
uart_device_number_t _uart;
50+
private:
51+
fpioa_function_t _rxfunc;
52+
fpioa_function_t _txfunc;
53+
//static int _rec_callback(void *ctx);
54+
};
4355

44-
protected:
45-
uint8_t _rxPin;
46-
uint8_t _txPin;
47-
56+
class UARTHSClass : public UARTClass
57+
{
58+
public:
59+
UARTHSClass();
60+
void begin(uint32_t dwBaudRate, uint8_t _rx = 4, uint8_t _tx = 5);
61+
void end(void);
62+
size_t write(const uint8_t c);
63+
using Print::write;
64+
operator bool() {return (true);}; // UART always active
65+
//private:
66+
//static int _rec_callback(void *ctx);
4867
};
68+
69+
extern volatile uart_t* const uart[3];
4970
extern int uarths_putchar(char c);
5071
extern int uarths_getc(void);
5172

libraries/SPI/src/SPI.cpp

Whitespace-only changes.

libraries/SPI/src/SPI.h

Whitespace-only changes.

0 commit comments

Comments
 (0)