Skip to content

Commit 125326b

Browse files
committed
enable PWM output for STM8S-Discovery board
1 parent a13bd0d commit 125326b

5 files changed

Lines changed: 67 additions & 7 deletions

File tree

docs/hardware/stm8sdiscovery.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ At least Blink.c is known to work already.
1010
STM8S105C6T6 microcontroller, 32 KB Flash, 2 KB RAM, 1 KB EEPROM
1111

1212
- LED on PD0, active low.
13-
- Touch button on PC1, PC2.
13+
- Touch button on PC1, PC2, PC3
1414
- external crystal 16MHz on PA1/PA2
1515

16+
The touch pins PC1 and PC3 (Arduino-Pin 24 and 26) are not connected to the
17+
pin header on the board. To use them as I/O-pins you need to modify some
18+
solder bridges (See STM8S-Discovery user manual).
1619

1720

1821
## SWIM connector

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,39 @@ void init()
610610
#endif
611611
#endif // #ifdef (TIM2)
612612

613+
#ifdef TIM3
614+
TIM3_DeInit();
615+
TIM3_TimeBaseInit(TIM3_PRESCALER_64, 255);
616+
617+
#ifdef USE_SPL
618+
TIM3_OC1Init(
619+
TIM3_OCMODE_PWM1,
620+
TIM3_OUTPUTSTATE_DISABLE,
621+
0,
622+
TIM3_OCPOLARITY_HIGH
623+
);
624+
625+
TIM3_OC2Init(
626+
TIM3_OCMODE_PWM1,
627+
TIM3_OUTPUTSTATE_DISABLE,
628+
0,
629+
TIM3_OCPOLARITY_HIGH
630+
);
631+
632+
TIM3_OC1PreloadConfig(ENABLE); // TIM3->CCMR1 |= (uint8_t)TIM3_CCMR_OCxPE;
633+
TIM3_OC2PreloadConfig(ENABLE); // TIM3->CCMR2 |= (uint8_t)TIM3_CCMR_OCxPE;
634+
TIM3_Cmd(ENABLE); // TIM3->CR1 |= (uint8_t)TIM3_CR1_CEN;
635+
#else
636+
TIM3->CCER1 = 0; // channel 1 and 2 disabled
637+
// TIM3->CCER2 = 0; // channel 3 disabled
638+
639+
TIM3->CCMR1 = TIM3_OCMODE_PWM1 | TIM3_CCMR_OCxPE;
640+
TIM3->CCMR2 = TIM3_OCMODE_PWM1 | TIM3_CCMR_OCxPE;
641+
642+
TIM3->CR1 = TIM3_CR1_CEN; // TIM1_Cmd(ENABLE);
643+
#endif
644+
#endif // #ifdef (TIM3)
645+
613646
/* De-Init ADC peripheral, sets prescaler to 2 */
614647
ADC1_DeInit();
615648
// optional:

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@ void analogWrite(uint8_t pin, int val)
117117
{
118118
switch(digitalPinToTimer(pin))
119119
{
120-
#ifdef SUPPORT_ALTERNATE_MAPPINGS
120+
#ifdef NEED_TIMER_11_12
121121
case TIMER11:
122122
// connect pwm to pin on timer 1, channel 1
123-
alternateFunction(1);
123+
// alternateFunction(1);
124124
#ifdef USE_SPL
125125
TIM1_OC1Init(
126126
TIM1_OCMODE_PWM1,
@@ -146,7 +146,7 @@ void analogWrite(uint8_t pin, int val)
146146
break;
147147
case TIMER12:
148148
// connect pwm to pin on timer 1, channel 2
149-
alternateFunction(1);
149+
// alternateFunction(1);
150150
#ifdef USE_SPL
151151
TIM1_OC2Init(
152152
TIM1_OCMODE_PWM1,
@@ -170,7 +170,7 @@ void analogWrite(uint8_t pin, int val)
170170
TIM1->CCR2L = (uint8_t)(val);
171171
#endif // if use_spl
172172
break;
173-
#endif // if alternate_mappings
173+
#endif // if NEED_TIMER_11_12
174174
case TIMER13:
175175
// connect pwm to pin on timer 1, channel 3
176176
#ifdef USE_SPL
@@ -272,6 +272,24 @@ void analogWrite(uint8_t pin, int val)
272272
TIM2->CCR3L = (uint8_t)(val);
273273
#endif
274274
break;
275+
#ifdef NEED_TIMER_31_32
276+
case TIMER31:
277+
// connect pwm to pin on timer 3, channel 1
278+
// eigentlich würde dies genügen:
279+
// write MSB first, DO NOT USE ldw instruction!
280+
TIM3->CCER1 |= TIM3_CCER1_CC1E;
281+
TIM3->CCMR1 = TIM3_OCMODE_PWM1 | TIM3_CCMR_OCxPE;
282+
TIM3->CCR1H = 0;
283+
TIM3->CCR1L = (uint8_t)(val);
284+
break;
285+
case TIMER32:
286+
// connect pwm to pin on timer 3, channel 2
287+
TIM3->CCMR2 = TIM3_OCMODE_PWM1 | TIM3_CCMR_OCxPE;
288+
TIM3->CCER1 |= TIM3_CCER1_CC2E;
289+
TIM3->CCR2H = 0;
290+
TIM3->CCR2L = (uint8_t)(val);
291+
break;
292+
#endif // ifdef NEED_TIMER_31_32
275293
case NOT_ON_TIMER:
276294
default:
277295
if (val < 128) {

sduino/hardware/sduino/stm8/variants/standard/pins_arduino.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,6 @@ const uint8_t PROGMEM digital_pin_to_timer_PGM[] = {
285285
NOT_ON_TIMER,
286286
};
287287

288-
289288
#define NO_ANALOG 0xff
290289

291290
const uint8_t digitalPinToAnalogChannelMap[] = {
@@ -303,6 +302,11 @@ const uint8_t digitalPinToAnalogChannelMap[] = {
303302

304303
#endif /* ARDUINO_MAIN */
305304

305+
#ifdef SUPPORT_ALTERNATE_MAPPINGS
306+
# define NEED_TIMER_11_12
307+
#endif
308+
309+
306310
// These serial port names are intended to allow libraries and architecture-neutral
307311
// sketches to automatically default to the correct port name for a particular type
308312
// of use. For example, a GPS module would normally connect to SERIAL_PORT_HARDWARE_OPEN,

sduino/hardware/sduino/stm8/variants/stm8sdisco/pins_arduino.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,11 @@ const uint8_t PROGMEM digital_pin_to_timer_PGM[] = {
396396
NOT_ON_TIMER,
397397
};
398398

399-
400399
#endif
401400

401+
#define NEED_TIMER_11_12
402+
#define NEED_TIMER_31_32
403+
402404
// These serial port names are intended to allow libraries and architecture-neutral
403405
// sketches to automatically default to the correct port name for a particular type
404406
// of use. For example, a GPS module would normally connect to SERIAL_PORT_HARDWARE_OPEN,

0 commit comments

Comments
 (0)