Skip to content

Commit cdfb50d

Browse files
committed
split HardwareSerial.c
size of Blink.ino before split: 2519 after split: 1937
1 parent c5c23e2 commit cdfb50d

12 files changed

Lines changed: 421 additions & 411 deletions

sduino/hardware/sduino/stm8/cores/sduino/HardwareSerial.c

Lines changed: 0 additions & 411 deletions
This file was deleted.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#include "HardwareSerial.c.h"
2+
/**
3+
* This part contains the bare minimum of serial support functions
4+
*
5+
* The interrupt functions (and the supporting basic data structures) can't
6+
* be removed by the linker, even if no serial functions are used in the sketch.
7+
*
8+
* Add a "-DNO_SERIAL" to the CPPFLAGS or CFLAGS to full remove this code.
9+
*/
10+
11+
#ifdef NO_SERIAL
12+
/*
13+
* empty default IRQ functions to make the linker happy if the
14+
* respective module is not to linked.
15+
*/
16+
17+
void UARTx_RX_IRQHandler(void) __interrupt(ITC_IRQ_UARTx_RX){}
18+
void UARTx_TX_IRQHandler(void) __interrupt(ITC_IRQ_UARTx_TX){}
19+
20+
#else // ifdef NO_SERIAL
21+
22+
// private data //////////////////////////////////////////////////////////////
23+
24+
// These variables should be static, but that doesn't go together with
25+
// splitting the source code into smaller units.
26+
27+
ring_buffer rx_buffer;// = { { 0 }, 0, 0};
28+
ring_buffer tx_buffer;// = { { 0 }, 0, 0};
29+
30+
volatile char transmitting;//=0;
31+
unsigned char initialized;//=0 internal status. Returned on HardwareSerial()
32+
33+
34+
35+
// private functions ////////////////////////////////////////////////////////
36+
37+
static void store_char(unsigned char c, ring_buffer *buffer)
38+
//inline void store_char(unsigned char c, ring_buffer *buffer)
39+
{
40+
int i = (unsigned int)(buffer->head + 1) % SERIAL_BUFFER_SIZE;
41+
42+
// if we should be storing the received character into the location
43+
// just before the tail (meaning that the head would advance to the
44+
// current location of the tail), we're about to overflow the buffer
45+
// and so we don't write the character or advance the head.
46+
if (i != buffer->tail) {
47+
buffer->buffer[buffer->head] = c;
48+
buffer->head = i;
49+
runSerialEvent = 1;
50+
}
51+
}
52+
53+
// Interrupt handler ///////////////////////////////////////////////////////////
54+
55+
void UARTx_RX_IRQHandler(void) __interrupt(ITC_IRQ_UARTx_RX) /* UART1/2 RX */
56+
{
57+
unsigned char c;
58+
59+
#ifdef USE_SPL
60+
c = UARTx_ReceiveData8();
61+
// check for parity error
62+
if (!UARTx_GetFlagStatus(UARTx_FLAG_PE)) {
63+
#else
64+
c = UARTx->DR;
65+
// check for parity error
66+
if (!(UARTx->SR & UARTx_FLAG_PE)) {
67+
#endif
68+
// no parity error, so store the data
69+
store_char(c, &rx_buffer);
70+
};
71+
}
72+
73+
74+
void UARTx_TX_IRQHandler(void) __interrupt(ITC_IRQ_UARTx_TX) /* UART1/2 TX */
75+
{
76+
#ifdef USE_SPL
77+
if (tx_buffer.head == tx_buffer.tail) {
78+
// Buffer empty, so disable interrupts
79+
transmitting = 0;
80+
UARTx_ITConfig(UARTx_IT_TXE, DISABLE);
81+
}
82+
else {
83+
// There is more data in the output buffer. Send the next byte
84+
unsigned char c = tx_buffer.buffer[tx_buffer.tail];
85+
tx_buffer.tail = (tx_buffer.tail + 1) % SERIAL_BUFFER_SIZE;
86+
87+
UARTx_SendData8(c);
88+
}
89+
#else
90+
if (tx_buffer.head == tx_buffer.tail) {
91+
// Buffer empty, so disable interrupts
92+
transmitting = 0;
93+
UARTx->CR2 &= ~UARTx_CR2_TIEN;
94+
}
95+
else {
96+
// There is more data in the output buffer. Send the next byte
97+
unsigned char c = tx_buffer.buffer[tx_buffer.tail];
98+
tx_buffer.tail = (tx_buffer.tail + 1) % SERIAL_BUFFER_SIZE;
99+
100+
UARTx->DR = c;
101+
}
102+
#endif
103+
}
104+
#endif // ifdef NO_SERIAL
105+
106+
// Public Methods //////////////////////////////////////////////////////////////
107+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "HardwareSerial.c.h"
2+
/**
3+
*/
4+
size_t HardwareSerial_write(uint8_t c)
5+
{
6+
int i = (tx_buffer.head + 1) % SERIAL_BUFFER_SIZE;
7+
8+
// If the output buffer is full, there's nothing for it other than to
9+
// wait for the interrupt handler to empty it a bit
10+
// ???: return 0 here instead?
11+
while (i == tx_buffer.tail)
12+
;
13+
14+
tx_buffer.buffer[tx_buffer.head] = c;
15+
tx_buffer.head = i;
16+
17+
#ifdef USE_SPL
18+
UARTx_ITConfig(UARTx_IT_TXE, ENABLE); // enable TXE interrupt
19+
#else
20+
UARTx->CR2 |= UARTx_CR2_TIEN; // enable TXE interrupt
21+
#endif
22+
transmitting = 1;
23+
//FIXME: unklar, ob das auf dem STM8 wirklich nötig ist.
24+
// Das TXE-Bit in UART_SR ist jedenfalls nur lesbar.
25+
// clear the TXC bit -- "can be cleared by writing a one to its bit location"
26+
// sbi(*_ucsra, TXC0);
27+
28+
return 1;
29+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "HardwareSerial.c.h"
2+
/**
3+
*/
4+
uint8_t HardwareSerial(void)
5+
{
6+
return initialized;
7+
}
8+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "HardwareSerial.c.h"
2+
/**
3+
*/
4+
void HardwareSerial_begin(unsigned long baud)
5+
{
6+
#ifdef USE_SPL
7+
//set the data bits, parity, and stop bits
8+
UARTx_Init(baud,
9+
UARTx_WORDLENGTH_8D, UARTx_STOPBITS_1, UARTx_PARITY_NO,
10+
UARTx_SYNCMODE_CLOCK_DISABLE, UARTx_MODE_TXRX_ENABLE);
11+
12+
UARTx_ITConfig(UARTx_IT_RXNE, ENABLE); // enable RXNE interrupt
13+
#else
14+
uint16_t divider;
15+
16+
/* Set the UARTx BaudRates in BRR1 and BRR2 registers according to UARTx_BaudRate value */
17+
divider = (uint16_t) ((uint32_t)CLK_GetClockFreq() / (uint32_t) baud);
18+
19+
UARTx->BRR2 = divider & 0x0f;
20+
divider >>= 4;
21+
UARTx->BRR1 = divider & 0xff;
22+
divider >> 4;
23+
UARTx->BRR2 |= (uint8_t) divider & 0xf0;
24+
25+
UARTx->CR1 = 0; // 8 Bit, no parity
26+
UARTx->CR3 = 0; // one stop bit
27+
// enable RXNE interrupt, enable transmitter, enable receiver
28+
UARTx->CR2 = UARTx_CR2_RIEN | UARTx_CR2_TEN | UARTx_CR2_REN;
29+
#endif
30+
initialized = 1;
31+
runSerialEvent = 0;
32+
}
33+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "HardwareSerial.c.h"
2+
/**
3+
*/
4+
void HardwareSerial_begin_config(unsigned long baud, uint8_t config)
5+
{
6+
#ifdef USE_SPL
7+
UARTx_StopBits_TypeDef stopbits;
8+
UARTx_Parity_TypeDef parity;
9+
UARTx_WordLength_TypeDef wordlength;
10+
11+
wordlength = (config & 0x10) ? UARTx_WORDLENGTH_9D : UARTx_WORDLENGTH_8D;
12+
stopbits = (config & 0x20) ? UARTx_STOPBITS_2 : UARTx_STOPBITS_1;
13+
parity=UARTx_PARITY_NO; // default
14+
config &= 0x6;
15+
if (config == 0x4) parity=UARTx_PARITY_EVEN;
16+
else if (config == 0x6) parity=UARTx_PARITY_ODD;
17+
18+
//set the data bits, parity, and stop bits
19+
UARTx_Init(baud, wordlength, stopbits, parity,
20+
UARTx_SYNCMODE_CLOCK_DISABLE, UARTx_MODE_TXRX_ENABLE);
21+
22+
UARTx_ITConfig(UARTx_IT_RXNE, ENABLE); // enable RXNE interrupt
23+
#else
24+
uint16_t divider;
25+
26+
/* Set the UARTx BaudRates in BRR1 and BRR2 registers according to UARTx_BaudRate value */
27+
divider = (uint16_t) ((uint32_t)CLK_GetClockFreq() / (uint32_t) baud);
28+
29+
UARTx->BRR2 = divider & 0x0f;
30+
divider >>= 4;
31+
UARTx->BRR1 = divider & 0xff;
32+
divider >> 4;
33+
UARTx->BRR2 |= (uint8_t) divider & 0xf0;
34+
35+
UARTx->CR1 = config & (MASK_DATABITS | MASK_PARITY);
36+
UARTx->CR3 = config & (MASK_STOPBITS);
37+
// enable RXNE interrupt, enable transmitter, enable receiver
38+
UARTx->CR2 = UARTx_CR2_RIEN | UARTx_CR2_TEN | UARTx_CR2_REN;
39+
#endif
40+
initialized = 1;
41+
runSerialEvent = 0;
42+
}
43+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "HardwareSerial.c.h"
2+
/**
3+
*/
4+
void HardwareSerial_end(void)
5+
{
6+
// wait for transmission of outgoing data
7+
while (tx_buffer.head != tx_buffer.tail)
8+
;
9+
10+
UARTx_DeInit();
11+
12+
// clear any received data
13+
rx_buffer.head = rx_buffer.tail;
14+
initialized = 0;
15+
runSerialEvent = 0;
16+
}
17+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "HardwareSerial.c.h"
2+
/**
3+
*/
4+
int HardwareSerial_available(void)
5+
{
6+
return (unsigned int)(SERIAL_BUFFER_SIZE + rx_buffer.head - rx_buffer.tail) % SERIAL_BUFFER_SIZE;
7+
}
8+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "HardwareSerial.c.h"
2+
/**
3+
*/
4+
int HardwareSerial_peek(void)
5+
{
6+
if (rx_buffer.head == rx_buffer.tail) {
7+
// if (!runSerialEvent) {
8+
return -1;
9+
} else {
10+
return rx_buffer.buffer[rx_buffer.tail];
11+
}
12+
}
13+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "HardwareSerial.c.h"
2+
/**
3+
*/
4+
int HardwareSerial_read(void)
5+
{
6+
// if the head isn't ahead of the tail, we don't have any characters
7+
if (rx_buffer.head == rx_buffer.tail) {
8+
// if (!runSerialEvent) {
9+
return -1;
10+
} else {
11+
unsigned char c = rx_buffer.buffer[rx_buffer.tail];
12+
rx_buffer.tail = (unsigned int)(rx_buffer.tail + 1) % SERIAL_BUFFER_SIZE;
13+
runSerialEvent = (rx_buffer.head != rx_buffer.tail);
14+
return c;
15+
}
16+
}
17+

0 commit comments

Comments
 (0)