Skip to content

Commit 453ea74

Browse files
committed
allow to deactivate serial, analog in and analog out by compile parameter
1 parent 0ba35bc commit 453ea74

4 files changed

Lines changed: 61 additions & 17 deletions

File tree

docs/api.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,28 @@ timer2: PWM for PA3, PD3, PD4 or PC5 (mutual exclusive)
161161
timer4: millis()
162162

163163

164+
### Leaving out unused parts
165+
166+
Some functions of the core Arduino system can be left out on compilation to
167+
save code space. This is done by compiler flags that can be defined in the
168+
Makefile:
169+
170+
```make
171+
BOARD_TAG = stm8sblue
172+
CFLAGS = -DNO_SERIAL -DNO_ANALOG_IN -DNO_ANALOG_OUT
173+
174+
include ../../../sduino.mk
175+
```
176+
177+
These flags are supported:
178+
179+
Flag | Bytes saved | Functions lost
180+
----- | -------: | ---------
181+
NO_SERIAL | 765 | all serial communication
182+
NO_ANALOG_OUT | 406 | analogWrite()
183+
NO_ANALOG_IN | 56 | analogRead()
184+
185+
164186

165187
### Other modifications
166188

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

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,6 @@
3131
#include "HardwareSerial.h"
3232

3333

34-
#define SERIAL_BUFFER_SIZE 16
35-
typedef struct ring_buffer
36-
{
37-
unsigned char buffer[SERIAL_BUFFER_SIZE];
38-
volatile unsigned int head;
39-
volatile unsigned int tail;
40-
} ring_buffer;
41-
42-
static ring_buffer rx_buffer;// = { { 0 }, 0, 0};
43-
static ring_buffer tx_buffer;// = { { 0 }, 0, 0};
44-
45-
static volatile char transmitting;//=0;
46-
static unsigned char initialized;//=0 internal status. Returned on HardwareSerial()
47-
4834
// device dependent definitions ////////////////////////////////////////////
4935

5036
#if defined(UART1)
@@ -120,7 +106,34 @@ static unsigned char initialized;//=0 internal status. Returned on HardwareSeria
120106
#endif
121107

122108

123-
// private functions //////////////////////////////////////////////////////////
109+
#ifdef NO_SERIAL
110+
/*
111+
* empty default IRQ functions to make the linker happy if the
112+
* respective module is not to linked.
113+
*/
114+
115+
void UARTx_TX_IRQHandler(void) __interrupt(ITC_IRQ_UARTx_TX){}
116+
void UARTx_RX_IRQHandler(void) __interrupt(ITC_IRQ_UARTx_RX){}
117+
#else // ifdef NO_SERIAL
118+
119+
120+
// private data //////////////////////////////////////////////////////////////
121+
122+
#define SERIAL_BUFFER_SIZE 16
123+
typedef struct ring_buffer
124+
{
125+
unsigned char buffer[SERIAL_BUFFER_SIZE];
126+
volatile unsigned int head;
127+
volatile unsigned int tail;
128+
} ring_buffer;
129+
130+
static ring_buffer rx_buffer;// = { { 0 }, 0, 0};
131+
static ring_buffer tx_buffer;// = { { 0 }, 0, 0};
132+
133+
static volatile char transmitting;//=0;
134+
static unsigned char initialized;//=0 internal status. Returned on HardwareSerial()
135+
136+
// private functions ////////////////////////////////////////////////////////
124137

125138
static void store_char(unsigned char c, ring_buffer *buffer)
126139
//inline void store_char(unsigned char c, ring_buffer *buffer)
@@ -347,3 +360,5 @@ size_t HardwareSerial_write(uint8_t c)
347360

348361
return 1;
349362
}
363+
364+
#endif // ifdef NO_SERIAL

sduino/hardware/sduino/stm8/cores/sduino/wiring.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ void init()
501501
// this is better for motors as it ensures an even waveform
502502
// note, however, that fast pwm mode can achieve a frequency of up
503503
// 8 MHz (with a 16 MHz clock) at 50% duty cycle
504-
504+
#ifndef NO_ANALOG_OUT
505505
TIM1_DeInit(); // keep this
506506
// actual prescaler is (n+1)
507507
TIM1_TimeBaseInit(63, TIM1_COUNTERMODE_UP, 255, 0); // keep this
@@ -642,7 +642,9 @@ void init()
642642
TIM3->CR1 = TIM3_CR1_CEN; // TIM1_Cmd(ENABLE);
643643
#endif
644644
#endif // #ifdef (TIM3)
645+
#endif // ifndef NO_ANALOG_OUT
645646

647+
#ifndef NO_ANALOG_IN
646648
/* De-Init ADC peripheral, sets prescaler to 2 */
647649
ADC1_DeInit();
648650
// optional:
@@ -666,6 +668,7 @@ void init()
666668
// #else // minimum prescaler is 2, already set by ADC1_DeInit();
667669
// ADC1->CR1 = 0 <<4;
668670
#endif
671+
#endif // ifndef NO_ANALOG_IN
669672

670673
// this needs to be called before setup() or some functions won't
671674
// work there

sduino/hardware/sduino/stm8/cores/sduino/wiring_analog.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include "wiring_private.h"
2828
#include "pins_arduino.h"
2929

30+
31+
#ifndef NO_ANALOG_IN
3032
/*
3133
uint8_t analog_reference = DEFAULT;
3234
@@ -87,8 +89,10 @@ int analogRead(uint8_t pin)
8789
// combine the two bytes
8890
return (high << 8) | low;
8991
}
92+
#endif //ifndef NO_ANALOG_IN
9093

9194

95+
#ifndef NO_ANALOG_OUT
9296
// Right now, PWM output only works on the pins with
9397
// hardware support. These are defined in the appropriate
9498
// pins_*.c file. For the rest of the pins, we default
@@ -300,4 +304,4 @@ void analogWrite(uint8_t pin, int val)
300304
}
301305
}
302306
}
303-
307+
#endif // ifndef NO_ANALOG_OUT

0 commit comments

Comments
 (0)