This commit is contained in:
Mike Trudeau
2026-02-13 20:30:52 +00:00
parent 71ffec0d88
commit 5b106896a1
127 changed files with 35217 additions and 0 deletions

40
fw/stm8s/common/Makefile Normal file
View File

@@ -0,0 +1,40 @@
DEVICE = stm8s103f3
F_CPU ?= 16000000
OLED ?= SSD1306_128_64
FONTSET_MINI ?= FONTSET_MINI
SRCDIR = ./src
INCDIR = ./inc
## Get program name from enclosing directory name
PROGRAM = $(lastword $(subst /, ,$(CURDIR)))
SOURCES=$(wildcard *.c $(SRCDIR)/*.c)
OBJECTS=$(SOURCES:.c=.rel)
HEADERS=$(wildcard *.h $(INCDIR)/*.h)
CC = sdcc
PROGRAMMER = stlinkv2
DEFINES=
DEFINES += -DSTM8S103
CPPFLAGS = -I$(INCDIR)
CFLAGS = --Werror --std-sdcc99 -mstm8 --opt-code-size $(DEFINES) -DF_CPU=$(F_CPU)UL -D$(OLED) -D$(FONTSET_MINI)
AR = sdar
ARFLAGS = -rc
.PHONY: all clean flash
all: $(PROGRAM).lib
$(PROGRAM).lib: $(OBJECTS)
$(AR) $(ARFLAGS) $(PROGRAM).lib $^
%.rel : %.c $(HEADERS)
$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<
CCOMPILEDFILES=$(SOURCES:.c=.asm) $(SOURCES:.c=.lst) $(SOURCES:.c=.rel) \
$(SOURCES:.c=.rst) $(SOURCES:.c=.sym)
clean:
@rm -f $(PROGRAM).lib $(PROGRAM).cdb $(PROGRAM).lk $(PROGRAM).map $(CCOMPILEDFILES)

216
fw/stm8s/common/extra.def Normal file
View File

@@ -0,0 +1,216 @@
// Extra rules generated by rules_gen.py
// REG ^= (1 << 0)
replace restart {
ld a, %1
xor a, #0x01
ld %1, a
} by {
bcpl %1, #0 ; peephole replaced xor by bcpl.
} if notUsed('a')
// REG |= (1 << 0)
replace restart {
ld a, %1
or a, #0x01
ld %1, a
} by {
bset %1, #0 ; peephole replaced or by bset.
} if notUsed('a')
// REG &= ~(1 << 0)
replace restart {
ld a, %1
and a, #0xfe
ld %1, a
} by {
bres %1, #0 ; peephole replaced and by bres.
} if notUsed('a')
// REG ^= (1 << 1)
replace restart {
ld a, %1
xor a, #0x02
ld %1, a
} by {
bcpl %1, #1 ; peephole replaced xor by bcpl.
} if notUsed('a')
// REG |= (1 << 1)
replace restart {
ld a, %1
or a, #0x02
ld %1, a
} by {
bset %1, #1 ; peephole replaced or by bset.
} if notUsed('a')
// REG &= ~(1 << 1)
replace restart {
ld a, %1
and a, #0xfd
ld %1, a
} by {
bres %1, #1 ; peephole replaced and by bres.
} if notUsed('a')
// REG ^= (1 << 2)
replace restart {
ld a, %1
xor a, #0x04
ld %1, a
} by {
bcpl %1, #2 ; peephole replaced xor by bcpl.
} if notUsed('a')
// REG |= (1 << 2)
replace restart {
ld a, %1
or a, #0x04
ld %1, a
} by {
bset %1, #2 ; peephole replaced or by bset.
} if notUsed('a')
// REG &= ~(1 << 2)
replace restart {
ld a, %1
and a, #0xfb
ld %1, a
} by {
bres %1, #2 ; peephole replaced and by bres.
} if notUsed('a')
// REG ^= (1 << 3)
replace restart {
ld a, %1
xor a, #0x08
ld %1, a
} by {
bcpl %1, #3 ; peephole replaced xor by bcpl.
} if notUsed('a')
// REG |= (1 << 3)
replace restart {
ld a, %1
or a, #0x08
ld %1, a
} by {
bset %1, #3 ; peephole replaced or by bset.
} if notUsed('a')
// REG &= ~(1 << 3)
replace restart {
ld a, %1
and a, #0xf7
ld %1, a
} by {
bres %1, #3 ; peephole replaced and by bres.
} if notUsed('a')
// REG ^= (1 << 4)
replace restart {
ld a, %1
xor a, #0x10
ld %1, a
} by {
bcpl %1, #4 ; peephole replaced xor by bcpl.
} if notUsed('a')
// REG |= (1 << 4)
replace restart {
ld a, %1
or a, #0x10
ld %1, a
} by {
bset %1, #4 ; peephole replaced or by bset.
} if notUsed('a')
// REG &= ~(1 << 4)
replace restart {
ld a, %1
and a, #0xef
ld %1, a
} by {
bres %1, #4 ; peephole replaced and by bres.
} if notUsed('a')
// REG ^= (1 << 5)
replace restart {
ld a, %1
xor a, #0x20
ld %1, a
} by {
bcpl %1, #5 ; peephole replaced xor by bcpl.
} if notUsed('a')
// REG |= (1 << 5)
replace restart {
ld a, %1
or a, #0x20
ld %1, a
} by {
bset %1, #5 ; peephole replaced or by bset.
} if notUsed('a')
// REG &= ~(1 << 5)
replace restart {
ld a, %1
and a, #0xdf
ld %1, a
} by {
bres %1, #5 ; peephole replaced and by bres.
} if notUsed('a')
// REG ^= (1 << 6)
replace restart {
ld a, %1
xor a, #0x40
ld %1, a
} by {
bcpl %1, #6 ; peephole replaced xor by bcpl.
} if notUsed('a')
// REG |= (1 << 6)
replace restart {
ld a, %1
or a, #0x40
ld %1, a
} by {
bset %1, #6 ; peephole replaced or by bset.
} if notUsed('a')
// REG &= ~(1 << 6)
replace restart {
ld a, %1
and a, #0xbf
ld %1, a
} by {
bres %1, #6 ; peephole replaced and by bres.
} if notUsed('a')
// REG ^= (1 << 7)
replace restart {
ld a, %1
xor a, #0x80
ld %1, a
} by {
bcpl %1, #7 ; peephole replaced xor by bcpl.
} if notUsed('a')
// REG |= (1 << 7)
replace restart {
ld a, %1
or a, #0x80
ld %1, a
} by {
bset %1, #7 ; peephole replaced or by bset.
} if notUsed('a')
// REG &= ~(1 << 7)
replace restart {
ld a, %1
and a, #0x7f
ld %1, a
} by {
bres %1, #7 ; peephole replaced and by bres.
} if notUsed('a')

View File

@@ -0,0 +1,16 @@
#ifndef BEEP_H
#define BEEP_H
/*
* BEEP -> PD4
*/
#include <stdint.h>
#include "stm8s.h"
void beep_init(void);
void beep_on(void);
void beep_off(void);
void beep_freq(uint8_t freq);
#endif /* BEEP_H */

View File

@@ -0,0 +1,21 @@
#ifndef CLOCK_H
#define CLOCK_H
#ifndef F_CPU
#warning "F_CPU not defined, using 2MHz by default"
#define F_CPU 16000000UL
#endif
#include <stdint.h>
#include "stm8s.h"
inline void clock_init(void)
{
#if (F_CPU == 2000000UL)
#elif (F_CPU == 16000000UL)
CLK_CKDIVR = 0x00; // Set the frequency to 16 MHz
#endif
}
#endif /* CLOCK_H */

View File

@@ -0,0 +1,23 @@
#ifndef DELAY_H
#define DELAY_H
#ifndef F_CPU
#warning "F_CPU not defined, using 2MHz by default"
#define F_CPU 2000000UL
#endif
#include <stdint.h>
inline void delay_ms(uint32_t ms) {
for (uint32_t i = 0; i < ((F_CPU / 18 / 1000UL) * ms); i++) {
__asm__("nop");
}
}
inline void delay_us(uint32_t us) {
for (uint32_t i = 0; i < ((F_CPU / 18 /1000UL / 1000UL) * us); i++) {
__asm__("nop");
}
}
#endif /* DELAY_H */

View File

@@ -0,0 +1,14 @@
#ifndef EEPROM_H
#define EEPROM_H
#include <stdint.h>
#include "stm8s.h"
void eeprom_unlock();
void eeprom_lock();
void eeprom_wait_busy();
void eeprom_read(uint16_t addr, uint8_t *buf, int len);
void eeprom_write(uint16_t addr, uint8_t *buf, uint16_t len);
#endif /* EEPROM_H */

2832
fw/stm8s/common/inc/font.h Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,10 @@
#ifndef FORMAT_H
#define FORMAT_H
#include <stdint.h>
#include <stdarg.h>
void format_vsprintf(uint8_t *str, uint8_t *format, va_list args);
void format_sprintf(uint8_t *str, uint8_t *format, ...);
#endif /* FORMAT_H */

View File

@@ -0,0 +1,69 @@
#ifndef LCD12864_H
#define LCD12864_H
#include "stdint.h"
#include "stdbool.h"
#include "stm8s.h"
/*
* Initialize LCD12864.
*
* Pinout:
* SCK -> PC5 [spi]
* MOSI -> PC6 [spi]
* MISO -> PC7 [spi]
* CS -> PC4
* RES -> PA2 //PC3
* DC -> PD2
*/
#define LCD12864_TRANSACTION_START \
SPI_CR1 = SPI_CR1_MSTR | SPI_CR1_BR(0) | SPI_CR1_SPE; \
PC_ODR &= ~PIN4;
#define LCD12864_TRANSACTION_END \
while ((SPI_SR & SPI_SR_BSY)) \
; \
PC_ODR |= PIN4; \
SPI_CR1 &= ~SPI_CR1_SPE;
#define LCD12864_MODE_COMMAND PD_ODR &= ~PIN2;
#define LCD12864_MODE_DATA PD_ODR |= PIN2;
#define LCD12864_NORMALDISPLAY 0xA7
#define LCD12864_INVERTDISPLAY 0xA6
#define LCD12864_CLEARON 0xA5
#define LCD12864_CLEAROFF 0xA4
#define LCD12864_RESET 0xE2
#define LCD12864_LCDWIDTH 128
#define LCD12864_LCDHEIGHT 64
#define LCD12864_PIX_START 0
struct LCD12864_SEGMENT
{
uint8_t fontWidth;
uint8_t fontHeight;
uint8_t fontSpacing;
uint8_t rows;
uint8_t cols;
uint8_t width;
uint8_t height;
uint8_t *buffer;
uint16_t bufferDepth;
uint8_t page0;
uint8_t clearChar;
};
bool lcd12864_init(bool reset);
void lcd12864_invert_display(bool i);
void lcd12864_clear_display(bool i);
struct LCD12864_SEGMENT *lcd12864_new_segment(uint8_t page0, uint8_t fontWidth, uint8_t fontHeight, uint8_t fontSpacing, uint8_t rows, uint8_t clearChar);
bool lcd12864_free_segment(struct LCD12864_SEGMENT *segment);
bool lcd12864_clear_segment(struct LCD12864_SEGMENT *segment);
bool lcd12864_display_segment(struct LCD12864_SEGMENT *segment);
void lcd12864_clear(uint8_t d);
void lcd12864_command1(uint8_t c);
void lcd12864_command_list(const uint8_t *c, uint8_t n);
#endif /* LCD12864_H */

View File

@@ -0,0 +1,26 @@
#ifndef MAX31855K_H
#define MAX31855K_H
#include <stdint.h>
/**
* Initialize MAX31855K.
*
* Pinout:
* CS -> PA1
* SCK -> PC5 [spi]
* MISO -> PC7 [spi]
*
*/
#define MAX31855K_TRANSACTION_START \
PC_ODR |= PIN5; \
PA_ODR &= ~PIN1;
#define MAX31855K_TRANSACTION_END \
PA_ODR |= PIN1;
void max31855k_init();
uint16_t max31855k_read();
#endif /* MAX31855K_H */

View File

@@ -0,0 +1,27 @@
#ifndef MAX6675_H
#define MAX6675_H
#include <stdint.h>
//#include "spi.h"
/**
* Initialize MAX6675.
*
* Pinout:
* CS -> PA1
* SCK -> PC5 [spi]
* MISO -> PC7 [spi]
*
*/
#define MAX6675_TRANSACTION_START \
PC_ODR |= PIN5; \
PA_ODR &= ~PIN1;
#define MAX6675_TRANSACTION_END \
PA_ODR |= PIN1;
void max6675_init();
uint16_t max6675_read();
#endif /* MAX6675_H */

108
fw/stm8s/common/inc/oled.h Normal file
View File

@@ -0,0 +1,108 @@
#ifndef OLED_H
#define OLED_H
#include "stdint.h"
#include "stdbool.h"
#include "stm8s.h"
/*
* Initialize OLED.
*
* Pinout:
* SCK -> PC5 [spi]
* MOSI -> PC6 [spi]
* MISO -> PC7 [spi]
* CS -> PC4
* RES -> PC3
* DC -> PD6
*/
struct SEGMENT
{
uint8_t fontWidth;
uint8_t fontHeight;
uint8_t fontSpacing;
uint8_t rows;
uint8_t cols;
uint8_t width;
uint8_t height;
uint8_t *buffer;
uint16_t bufferDepth;
uint8_t page0;
uint8_t clearChar;
};
bool oled_init(uint8_t vcs, bool reset);
struct SEGMENT *oled_new_segment(uint8_t page0, uint8_t fontWidth, uint8_t fontHeight, uint8_t fontSpacing, uint8_t rows, uint8_t clearChar);
bool oled_free_segment(struct SEGMENT *segment);
bool oled_clear_segment(struct SEGMENT *segment);
bool oled_display_segment(struct SEGMENT *segment);
void oled_clear_display(void);
void oled_invert_display(bool i);
void oled_dim_display(bool dim);
void oled_command_list(const uint8_t *c, uint8_t n);
void oled_command1(uint8_t c);
#if defined SSD1306_128_64
#define OLED_LCDWIDTH 128
#define OLED_LCDHEIGHT 64
#define OLED_PIX_START 0
#endif
#if defined SSD1306_128_32
#define OLED_LCDWIDTH 128
#define OLED_LCDHEIGHT 32
#define OLED_PIX_START 0
#endif
#if defined SH1106_128_64
#define OLED_LCDWIDTH 128
#define OLED_LCDHEIGHT 64
#define OLED_PIX_START 2
#endif
#ifndef OLED_LCDWIDTH
#define OLED_LCDWIDTH 128
#define OLED_LCDHEIGHT 64
#define OLED_PIX_START 0
#endif
#define SSD1306_BLACK 0 ///< Draw 'off' pixels
#define SSD1306_WHITE 1 ///< Draw 'on' pixels
#define SSD1306_INVERSE 2 ///< Invert pixels
#define TRANSACTION_START \
SPI_CR1 = SPI_CR1_MSTR | SPI_CR1_BR(0) | SPI_CR1_SPE; \
PC_ODR &= ~PIN4;
#define TRANSACTION_END \
while ((SPI_SR & SPI_SR_BSY)) \
; \
PC_ODR |= PIN4; \
SPI_CR1 &= ~SPI_CR1_SPE;
#define OLED_MODE_COMMAND PD_ODR &= ~PIN6;
#define OLED_MODE_DATA PD_ODR |= PIN6;
#define SSD1306_EXTERNALVCC 0x01 ///< External display voltage source
#define SSD1306_SWITCHCAPVCC 0x02 ///< Gen. display voltage from 3.3V
#define SSD1306_MEMORYMODE 0x20
#define SSD1306_COLUMNADDR 0x21
#define SSD1306_PAGEADDR 0x22
#define SSD1306_DEACTIVATE_SCROLL 0x2E
#define SSD1306_SETSTARTLINE 0x40
#define SSD1306_SETCONTRAST 0x81
#define SSD1306_CHARGEPUMP 0x8D
#define SSD1306_SEGREMAP 0xA0
#define SSD1306_DISPLAYALLON_RESUME 0xA4
#define SSD1306_NORMALDISPLAY 0xA6
#define SSD1306_INVERTDISPLAY 0xA7
#define SSD1306_SETMULTIPLEX 0xA8
#define SSD1306_DISPLAYOFF 0xAE
#define SSD1306_DISPLAYON 0xAF
#define SSD1306_COMSCANDEC 0xC8
#define SSD1306_SETDISPLAYOFFSET 0xD3
#define SSD1306_SETDISPLAYCLOCKDIV 0xD5
#define SSD1306_SETPRECHARGE 0xD9
#define SSD1306_SETCOMPINS 0xDA
#define SSD1306_SETVCOMDETECT 0xDB
#endif /* OLED_H */

21
fw/stm8s/common/inc/spi.h Normal file
View File

@@ -0,0 +1,21 @@
#ifndef SPI_H
#define SPI_H
#include <stdint.h>
/*
* Initialize SPI in MODE1.
*
* Pinout:
* SCK -> PC5
* MOSI -> PC6
* MISO -> PC7
* CS -> user defined
*/
void spi_init();
void spi_write(uint8_t data);
uint8_t spi_read();
#endif /* SPI_H */

425
fw/stm8s/common/inc/stm8s.h Normal file
View File

@@ -0,0 +1,425 @@
/*
* Register definitions for STM8S103 (and STM8S003)
* Still incomplete.
*/
#ifndef _STH8_H
#define _STH8_H
/* Handy macros for GPIO */
#define CONCAT(a, b) a##_##b
#define PORT(a, b) CONCAT(a, b)
#define PIN0 (1 << 0)
#define PIN1 (1 << 1)
#define PIN2 (1 << 2)
#define PIN3 (1 << 3)
#define PIN4 (1 << 4)
#define PIN5 (1 << 5)
#define PIN6 (1 << 6)
#define PIN7 (1 << 7)
/* Register addresses */
/* Clock */
#define CLK_CKDIVR *(volatile unsigned char *)0x50C6
/* GPIO */
#define PA_ODR *(volatile unsigned char *)0x5000
#define PA_IDR *(volatile unsigned char *)0x5001
#define PA_DDR *(volatile unsigned char *)0x5002
#define PA_CR1 *(volatile unsigned char *)0x5003
#define PA_CR2 *(volatile unsigned char *)0x5004
#define PB_ODR *(volatile unsigned char *)0x5005
#define PB_IDR *(volatile unsigned char *)0x5006
#define PB_DDR *(volatile unsigned char *)0x5007
#define PB_CR1 *(volatile unsigned char *)0x5008
#define PB_CR2 *(volatile unsigned char *)0x5009
#define PC_ODR *(volatile unsigned char *)0x500A
#define PC_IDR *(volatile unsigned char *)0x500B
#define PC_DDR *(volatile unsigned char *)0x500C
#define PC_CR1 *(volatile unsigned char *)0x500D
#define PC_CR2 *(volatile unsigned char *)0x500E
#define PD_ODR *(volatile unsigned char *)0x500F
#define PD_IDR *(volatile unsigned char *)0x5010
#define PD_DDR *(volatile unsigned char *)0x5011
#define PD_CR1 *(volatile unsigned char *)0x5012
#define PD_CR2 *(volatile unsigned char *)0x5013
#define EXTI_CR1 *(volatile unsigned char *)0x50A0
#define EXTI_CR2 *(volatile unsigned char *)0x50A1
/* UART */
#define UART1_SR *(volatile unsigned char *)0x5230
#define UART1_DR *(volatile unsigned char *)0x5231
#define UART1_BRR1 *(volatile unsigned char *)0x5232
#define UART1_BRR2 *(volatile unsigned char *)0x5233
#define UART1_CR1 *(volatile unsigned char *)0x5234
#define UART1_CR2 *(volatile unsigned char *)0x5235
#define UART1_CR3 *(volatile unsigned char *)0x5236
#define UART1_CR4 *(volatile unsigned char *)0x5237
#define UART1_CR5 *(volatile unsigned char *)0x5238
#define UART1_GTR *(volatile unsigned char *)0x5239
#define UART1_PSCR *(volatile unsigned char *)0x523A
#define UART_SR_TXE (1 << 7)
#define UART_SR_TC (1 << 6)
#define UART_SR_RXNE (1 << 5)
#define UART_SR_IDLE (1 << 4)
#define UART_SR_OR (1 << 3)
#define UART_SR_NF (1 << 2)
#define UART_SR_FE (1 << 1)
#define UART_SR_PE (1 << 0)
#define UART_CR1_R8 (1 << 7)
#define UART_CR1_T8 (1 << 6)
#define UART_CR1_UARTD (1 << 5)
#define UART_CR1_M (1 << 4)
#define UART_CR1_WAKE (1 << 3)
#define UART_CR1_PCEN (1 << 2)
#define UART_CR1_PS (1 << 1)
#define UART_CR1_PIEN (1 << 0)
#define UART_CR2_TIEN (1 << 7)
#define UART_CR2_TCIEN (1 << 6)
#define UART_CR2_RIEN (1 << 5)
#define UART_CR2_ILIEN (1 << 4)
#define UART_CR2_TEN (1 << 3)
#define UART_CR2_REN (1 << 2)
#define UART_CR2_RWU (1 << 1)
#define UART_CR2_SBK (1 << 0)
#define UART_CR3_LINEN (1 << 6)
#define UART_CR3_STOP2 (1 << 5)
#define UART_CR3_STOP1 (1 << 4)
#define UART_CR3_CLKEN (1 << 3)
#define UART_CR3_CPOL (1 << 2)
#define UART_CR3_CPHA (1 << 1)
#define UART_CR3_LBCL (1 << 0)
/* Timers */
#define TIM1_CR1 *(volatile unsigned char *)0x5250
#define TIM1_CR2 *(volatile unsigned char *)0x5251
#define TIM1_SMCR *(volatile unsigned char *)0x5252
#define TIM1_ETR *(volatile unsigned char *)0x5253
#define TIM1_IER *(volatile unsigned char *)0x5254
#define TIM1_SR1 *(volatile unsigned char *)0x5255
#define TIM1_SR2 *(volatile unsigned char *)0x5256
#define TIM1_EGR *(volatile unsigned char *)0x5257
#define TIM1_CCMR1 *(volatile unsigned char *)0x5258
#define TIM1_CCMR2 *(volatile unsigned char *)0x5259
#define TIM1_CCMR3 *(volatile unsigned char *)0x525A
#define TIM1_CCMR4 *(volatile unsigned char *)0x525B
#define TIM1_CCER1 *(volatile unsigned char *)0x525C
#define TIM1_CCER2 *(volatile unsigned char *)0x525D
#define TIM1_CNTRH *(volatile unsigned char *)0x525E
#define TIM1_CNTRL *(volatile unsigned char *)0x525F
#define TIM1_PSCRH *(volatile unsigned char *)0x5260
#define TIM1_PSCRL *(volatile unsigned char *)0x5261
#define TIM1_ARRH *(volatile unsigned char *)0x5262
#define TIM1_ARRL *(volatile unsigned char *)0x5263
#define TIM1_RCR *(volatile unsigned char *)0x5264
#define TIM1_CCR1H *(volatile unsigned char *)0x5265
#define TIM1_CCR1L *(volatile unsigned char *)0x5266
#define TIM1_CCR2H *(volatile unsigned char *)0x5267
#define TIM1_CCR2L *(volatile unsigned char *)0x5268
#define TIM1_CCR3H *(volatile unsigned char *)0x5269
#define TIM1_CCR3L *(volatile unsigned char *)0x526A
#define TIM1_CCR4H *(volatile unsigned char *)0x526B
#define TIM1_CCR4L *(volatile unsigned char *)0x526C
#define TIM1_BKR *(volatile unsigned char *)0x526D
#define TIM1_DTR *(volatile unsigned char *)0x526E
#define TIM1_OISR *(volatile unsigned char *)0x526F
/* Note these are for STM8S103 and STM8S003
STM8S105,104/207/208 are different */
#define TIM2_CR1 *(volatile unsigned char *)0x5300
#define TIM2_CR2 *(volatile unsigned char *)0x5301
#define TIM2_SMCR *(volatile unsigned char *)0x5302
#define TIM2_IER *(volatile unsigned char *)0x5303
#define TIM2_SR1 *(volatile unsigned char *)0x5304
#define TIM2_SR2 *(volatile unsigned char *)0x5305
#define TIM2_EGR *(volatile unsigned char *)0x5306
#define TIM2_CCMR1 *(volatile unsigned char *)0x5307
#define TIM2_CCMR2 *(volatile unsigned char *)0x5308
#define TIM2_CCMR3 *(volatile unsigned char *)0x5309
#define TIM2_CCER1 *(volatile unsigned char *)0x530A
#define TIM2_CCER2 *(volatile unsigned char *)0x530B
#define TIM2_CNTRH *(volatile unsigned char *)0x530C
#define TIM2_CNTRL *(volatile unsigned char *)0x530D
#define TIM2_PSCR *(volatile unsigned char *)0x530E
#define TIM2_ARRH *(volatile unsigned char *)0x530F
#define TIM2_ARRL *(volatile unsigned char *)0x5310
#define TIM2_CCR1H *(volatile unsigned char *)0x5311
#define TIM2_CCR1L *(volatile unsigned char *)0x5312
#define TIM2_CCR2H *(volatile unsigned char *)0x5313
#define TIM2_CCR2L *(volatile unsigned char *)0x5314
#define TIM2_CCR3H *(volatile unsigned char *)0x5315
#define TIM2_CCR3L *(volatile unsigned char *)0x5316
#define TIM2_CR1_CEN (1 << 0)
#define TIM2_CCER1_CC1P (1 << 1)
#define TIM2_CCER1_CC1E (1 << 0)
#define TIM2_CCER1_CC2P (1 << 5)
#define TIM2_CCER1_CC2E (1 << 4)
#define TIM2_CCER2_CC3P (1 << 1)
#define TIM2_CCER2_CC3E (1 << 0)
#define TIM2_CCMR2_OC2M0 (1 << 4)
#define TIM2_CCMR2_OC2M1 (1 << 5)
#define TIM2_CCMR2_OC2M2 (1 << 6)
/* Note these are for STM8S103 and STM8S003
STM8S105,104/207/208 are different */
#define TIM4_CR1 *(volatile unsigned char *)0x5340
#define TIM4_CR2 *(volatile unsigned char *)0x5341
#define TIM4_SMCR *(volatile unsigned char *)0x5342
#define TIM4_IER *(volatile unsigned char *)0x5343
#define TIM4_SR *(volatile unsigned char *)0x5344
#define TIM4_EGR *(volatile unsigned char *)0x5345
#define TIM4_CNTR *(volatile unsigned char *)0x5346
#define TIM4_PSCR *(volatile unsigned char *)0x5347
#define TIM4_ARR *(volatile unsigned char *)0x5348
#define TIM_IER_BIE (1 << 7)
#define TIM_IER_TIE (1 << 6)
#define TIM_IER_COMIE (1 << 5)
#define TIM_IER_CC4IE (1 << 4)
#define TIM_IER_CC3IE (1 << 3)
#define TIM_IER_CC2IE (1 << 2)
#define TIM_IER_CC1IE (1 << 1)
#define TIM_IER_UIE (1 << 0)
#define TIM_CR1_APRE (1 << 7)
#define TIM_CR1_CMSH (1 << 6)
#define TIM_CR1_CMSL (1 << 5)
#define TIM_CR1_DIR (1 << 4)
#define TIM_CR1_OPM (1 << 3)
#define TIM_CR1_URS (1 << 2)
#define TIM_CR1_UDIS (1 << 1)
#define TIM_CR1_CEN (1 << 0)
#define TIM_SR1_BIF (1 << 7)
#define TIM_SR1_TIF (1 << 6)
#define TIM_SR1_COMIF (1 << 5)
#define TIM_SR1_CC4IF (1 << 4)
#define TIM_SR1_CC3IF (1 << 3)
#define TIM_SR1_CC2IF (1 << 2)
#define TIM_SR1_CC1IF (1 << 1)
#define TIM_SR1_UIF (1 << 0)
/* SPI */
#define SPI_CR1 *(volatile unsigned char *)0x5200
#define SPI_CR2 *(volatile unsigned char *)0x5201
#define SPI_ICR *(volatile unsigned char *)0x5202
#define SPI_SR *(volatile unsigned char *)0x5203
#define SPI_DR *(volatile unsigned char *)0x5204
#define SPI_CRCPR *(volatile unsigned char *)0x5205
#define SPI_RXCRCR *(volatile unsigned char *)0x5206
#define SPI_TXCRCR *(volatile unsigned char *)0x5207
#define SPI_CR1_LSBFIRST (1 << 7)
#define SPI_CR1_SPE (1 << 6)
#define SPI_CR1_BR(br) ((br) << 3)
#define SPI_CR1_MSTR (1 << 2)
#define SPI_CR1_CPOL (1 << 1)
#define SPI_CR1_CPHA (1 << 0)
#define SPI_CR2_BDM (1 << 7)
#define SPI_CR2_BDOE (1 << 6)
#define SPI_CR2_CRCEN (1 << 5)
#define SPI_CR2_CRCNEXT (1 << 4)
#define SPI_CR2_RXONLY (1 << 2)
#define SPI_CR2_SSM (1 << 1)
#define SPI_CR2_SSI (1 << 0)
#define SPI_ICR_TXIE (1 << 7)
#define SPI_ICR_RXIE (1 << 6)
#define SPI_ICR_ERRIE (1 << 5)
#define SPI_ICR_WKIE (1 << 4)
#define SPI_SR_BSY (1 << 7)
#define SPI_SR_OVR (1 << 6)
#define SPI_SR_MODF (1 << 5)
#define SPI_SR_CRCERR (1 << 4)
#define SPI_SR_WKUP (1 << 3)
#define SPI_SR_TXE (1 << 1)
#define SPI_SR_RxNE (1 << 0)
/* I2C */
#define I2C_CR1 *(volatile unsigned char *)0x5210
#define I2C_CR2 *(volatile unsigned char *)0x5211
#define I2C_FREQR *(volatile unsigned char *)0x5212
#define I2C_OARL *(volatile unsigned char *)0x5213
#define I2C_OARH *(volatile unsigned char *)0x5214
#define I2C_DR *(volatile unsigned char *)0x5216
#define I2C_SR1 *(volatile unsigned char *)0x5217
#define I2C_SR2 *(volatile unsigned char *)0x5218
#define I2C_SR3 *(volatile unsigned char *)0x5219
#define I2C_ITR *(volatile unsigned char *)0x521A
#define I2C_CCRL *(volatile unsigned char *)0x521B
#define I2C_CCRH *(volatile unsigned char *)0x521C
#define I2C_TRISER *(volatile unsigned char *)0x521D
#define I2C_PECR *(volatile unsigned char *)0x521E
/* ADC */
#define ADC_DBxR *(volatile unsigned char *)0x53E0
#define ADC_CSR *(volatile unsigned char *)0x5400
#define ADC_CR1 *(volatile unsigned char *)0x5401
#define ADC_CR2 *(volatile unsigned char *)0x5402
#define ADC_CR3 *(volatile unsigned char *)0x5403
#define ADC_DRH *(volatile unsigned char *)0x5404
#define ADC_DRL *(volatile unsigned char *)0x5405
#define ADC_TDRH *(volatile unsigned char *)0x5406
#define ADC_TDRL *(volatile unsigned char *)0x5407
#define ADC_HTRH *(volatile unsigned char *)0x5408
#define ADC_HTRL *(volatile unsigned char *)0x5409
#define ADC_LTRH *(volatile unsigned char *)0x540A
#define ADC_LTRL *(volatile unsigned char *)0x540B
#define ADC_AWSRH *(volatile unsigned char *)0x540C
#define ADC_AWSRL *(volatile unsigned char *)0x540D
#define ADC_AWCRH *(volatile unsigned char *)0x540E
#define ADC_AWCRL *(volatile unsigned char *)0x540F
#define ADC_CSR_EOC (1 << 7)
#define ADC_CSR_AWD (1 << 6)
#define ADC_CSR_EOCIE (1 << 5)
#define ADC_CSR_AWDIE (1 << 4)
#define ADC_CR1_CONT (1 << 1)
#define ADC_CR1_ADON (1 << 0)
#define ADC_CR2_EXTTRIG (1 << 6)
#define ADC_CR2_EXTSEL (1 << 4)
#define ADC_CR2_ALIGN (1 << 3)
#define ADC_CR2_SCAN (1 << 1)
/* BEEP */
#define BEEP_CSR *(volatile unsigned char *)0x50F3
#define BEEP_CSR_EN (1 << 5)
#define BEEP_CSR_SEL(sel) ((sel & 0b00000011) << 6)
#define BEEP_CSR_DIV(div) ((div & 0b00011111) << 0)
#define BEEP_CSR_BEEPDIV ((uint8_t)0x1F) /*!< Beeper Divider prescalar mask */
#define BEEP_FREQUENCY_1KHZ ((uint8_t)0x00) /*!< Beep signal output frequency equals to 1 KHz */
#define BEEP_FREQUENCY_2KHZ ((uint8_t)0x40) /*!< Beep signal output frequency equals to 2 KHz */
#define BEEP_FREQUENCY_4KHZ ((uint8_t)0x80) /*!< Beep signal output frequency equals to 4 KHz */
#define BEEP_CALIBRATION_DEFAULT ((uint8_t)0x0B) /*!< Default value when calibration is not done */
#define LSI_FREQUENCY_MIN ((uint32_t)110000) /*!< LSI minimum value in Hertz */
#define LSI_FREQUENCY_MAX ((uint32_t)150000) /*!< LSI maximum value in Hertz */
/* FLASH */
#define FLASH_IAPSR *(volatile unsigned char *)0x505F
#define FLASH_IAPSR_DUL (1 << 3)
#define FLASH_IAPSR_EOP (1 << 2)
#define FLASH_PUKR_KEY1 0x56
#define FLASH_PUKR_KEY2 0xAE
#define FLASH_DUKR *(volatile unsigned char *)0x5064
#define FLASH_DUKR_KEY1 FLASH_PUKR_KEY2
#define FLASH_DUKR_KEY2 FLASH_PUKR_KEY1
#define EEPROM_START_ADDR 0x4000
#define EEPROM_END_ADDR 0x407F
/* Interrupt commands */
#define enableInterrupts() \
{ \
__asm__("rim\n"); \
} /* enable interrupts */
#define disableInterrupts() \
{ \
__asm__("sim\n"); \
} /* disable interrupts */
#define rim() \
{ \
__asm__("rim\n"); \
} /* enable interrupts */
#define sim() \
{ \
__asm__("sim\n"); \
} /* disable interrupts */
#define nop() \
{ \
__asm__("nop\n"); \
} /* No Operation */
#define trap() \
{ \
__asm__("trap\n"); \
} /* Trap (soft IT) */
#define wfi() \
{ \
__asm__("wfi\n"); \
} /* Wait For Interrupt */
#define halt() \
{ \
__asm__("halt\n"); \
} /* Halt */
/* Interrupt numbers */
#define TIM1_OVR_UIF_IRQ 11
#define TIM2_OVR_UIF_IRQ 13
#define TIM3_OVR_UIF_IRQ 15
#define ADC1_EOC_IRQ 22
#define TIM4_OVR_UIF_IRQ 23
#define ISR(name, vector) void name(void) __interrupt(vector - 2)
#define EXTI_PORTA_vector 0x05
#define EXTI_PORTB_vector 0x06
/*
Interrupts:
0 TLI
1 AWU Auto Wake up from Halt
2 CLK Clock controller
3 EXTI0 Port A external interrupts
4 EXTI1 Port B external interrupts
5 EXTI2 Port C external interrupts
6 EXTI3 Port D external interrupts
7 EXTI4 Port E external interrupts
8 CAN CAN RX interrupt
9 CAN CAN TX/ER/SC interrupt
10 SPI End of Transfer
11 TIM1 Update /Overflow/Underflow/Trigger/Break
12 TIM1 Capture/Compare
13 TIM2 Update /Overflow
14 TIM2 Capture/Compare
15 TIM3 Update /Overflow
16 TIM3 Capture/Compare
17 UART1 Tx complete
18 UART1 Receive Register DATA FULL
19 I2C I2C interrupt
20 UART2/3 Tx complete
21 UART2/3 Receive Register DATA FULL
22 ADC End of Conversion
23 TIM4 Update/Overflow
24 FLASH EOP/WR_PG_DIS
TLI 0
AWU 1
CLK 2
EXTI_PORTA 3
EXTI_PORTB 4
EXTI_PORTC
EXTI_PORTD
EXTI_PORTE
CAN_RX
CAN_TX
SPI
TIM1_UPD_OVF_TRG_BRK
TIM1_CAP_COM
TIM2_UPD_OVF_BRK
TIM2_CAP_COM
TIM3_UPD_OVF_BRK
TIM3_CAP_COM
UART1_TX
UART1_RX
I2C 19
ADC1 22
TIM4_UPD_OVF 23
EEPROM_EEC 24
*/
#endif

View File

@@ -0,0 +1,28 @@
#ifndef UART_H
#define UART_H
#include <stdint.h>
#ifndef BAUDRATE
#define BAUDRATE 115200
#endif
#ifndef F_CPU
#warning "F_CPU not defined, using 2MHz by default"
#define F_CPU 2000000UL
#endif
/**
* Initialize UART1.
* Mode: 8-N-1, flow-control: none.
*
* PD5 -> TX
* PD6 -> RX
*/
void uart_init();
void uart_write(uint8_t data);
uint8_t uart_read();
#endif /* UART_H */

View File

@@ -0,0 +1,6 @@
#ifndef UART_STDIN_H
#define UART_STDIN_H
int getchar(void);
#endif /* UART_STDIN_H */

View File

@@ -0,0 +1,8 @@
#ifndef UART_STDOUT_H
#define UART_STDOUT_H
int putchar(int c);
void putstring(uint8_t *str);
void putsf(uint8_t *format, ...);
#endif /* UART_STDOUT_H */

View File

@@ -0,0 +1,6 @@
#include "beep.h"
void beep_freq(uint8_t freq)
{
BEEP_CSR_DIV(freq);
}

View File

@@ -0,0 +1,7 @@
#include "beep.h"
void beep_init(void)
{
BEEP_CSR &= ~BEEP_CSR_BEEPDIV; /* Clear bits */
BEEP_CSR |= BEEP_CALIBRATION_DEFAULT;
}

View File

@@ -0,0 +1,6 @@
#include "beep.h"
void beep_off(void)
{
BEEP_CSR &= ~BEEP_CSR_EN;
}

View File

@@ -0,0 +1,6 @@
#include "beep.h"
void beep_on(void)
{
BEEP_CSR |= BEEP_CSR_EN;
}

View File

@@ -0,0 +1,3 @@
#include "clock.h"
extern inline void clock_init(void);

View File

@@ -0,0 +1,3 @@
#include "delay.h"
extern inline void delay_ms(uint32_t ms);

View File

@@ -0,0 +1,3 @@
#include "delay.h"
extern inline void delay_us(uint32_t us);

View File

@@ -0,0 +1,5 @@
#include "eeprom.h"
void eeprom_lock() {
FLASH_IAPSR &= ~FLASH_IAPSR_DUL;
}

View File

@@ -0,0 +1,6 @@
#include "eeprom.h"
void eeprom_read(uint16_t addr, uint8_t *buf, int len) {
for (int i = 0; i < len; i++, addr++)
buf[i] = (*(volatile uint8_t *)(addr));
}

View File

@@ -0,0 +1,7 @@
#include "eeprom.h"
void eeprom_unlock() {
FLASH_DUKR = FLASH_DUKR_KEY1;
FLASH_DUKR = FLASH_DUKR_KEY2;
while (!(FLASH_IAPSR & FLASH_IAPSR_DUL));
}

View File

@@ -0,0 +1,5 @@
#include "eeprom.h"
void eeprom_wait_busy() {
while (!(FLASH_IAPSR & FLASH_IAPSR_EOP));
}

View File

@@ -0,0 +1,10 @@
#include "eeprom.h"
void eeprom_write(uint16_t addr, uint8_t *buf, uint16_t len) {
eeprom_unlock();
for (uint16_t i = 0; i < len; i++, addr++) {
(*(volatile uint8_t *)(addr)) = buf[i];
eeprom_wait_busy();
}
eeprom_unlock();
}

View File

@@ -0,0 +1,12 @@
#include <stdint.h>
#include <stdbool.h>
#include <stdarg.h>
#include "format.h"
void format_sprintf(uint8_t *str, uint8_t *format, ...)
{
va_list args;
va_start(args, format);
format_vsprintf(str, format, args);
va_end(args);
}

View File

@@ -0,0 +1,114 @@
#include <stdint.h>
#include <stdbool.h>
#include <stdarg.h>
#include "uart_stdout.h"
#include "format.h"
static const uint16_t dv[] = {
10000,
1000,
100,
10,
1,
};
static uint8_t xtoa(uint8_t *str, uint16_t x, const uint16_t *dp)
{
uint8_t len = 0;
uint8_t c;
uint16_t d;
if (x)
{
while (x < *dp)
++dp;
do
{
d = *dp++;
c = '0';
while (x >= d)
++c, x -= d;
*str++ = c;
len++;
} while (!(d & 1));
}
else
{
len++;
*str++ = (uint8_t)'0';
}
*str++ = 0x00;
return len;
}
void format_vsprintf(uint8_t *str, uint8_t *format, va_list args) {
uint8_t c, len;
int16_t i;
uint8_t *string_temp;
uint8_t buff[6];
while (c = *format++)
{
bool zl = false;
uint8_t zs = 0;
uint8_t cnt = 0;
if (c == '%')
{
if (*format == (uint8_t)'0')
{
*format++;
zl = true;
}
if (*format >= (uint8_t)'1' && *format <= (uint8_t)'5')
{
zs = (uint8_t)*format - 0x30;
*format++;
}
switch (c = *format++)
{
case '%': // %
*str++ = (uint8_t)'%';
break;
case 's': // String
string_temp = va_arg(args, uint8_t *);
while (*string_temp != 0)
{
*str++ = *string_temp++;
}
break;
case 'c': // Char
*str++ = va_arg(args, uint8_t);
break;
case 'i': // 16 bit Integer
case 'u': // 16 bit Unsigned
i = va_arg(args, int16_t);
if (c == 'i' && i < 0)
i = -i, *str++ = (uint8_t)'-';
len = xtoa(buff, (uint16_t)i, dv);
if (zs && (len < zs))
{
while (++len <= zs)
*str++ = (zl) ? (uint8_t)'0' : (uint8_t)' ';
}
string_temp = &buff[0];
for (uint8_t index = 0; index <= sizeof(buff); index++)
while (*string_temp != 0)
{
*str++ = *string_temp++;
}
break;
case 0:
return;
default:
goto bad_fmt;
}
}
else
{
bad_fmt:
*str++ = c;
}
}
*str++ = 0x00;
}

View File

@@ -0,0 +1,21 @@
#include <stdint.h>
#include "lcd12864.h"
#include "spi.h"
void lcd12864_clear(uint8_t d)
{
for (uint8_t page = 0; page < 8; page++)
{
uint8_t dlist1[] = {
0x40 | LCD12864_PIX_START,
0xB0 | page,
(0x10 | (0 >> 4)),
(0x0f & 0) | 0x04};
lcd12864_command_list(dlist1, sizeof(dlist1));
LCD12864_TRANSACTION_START
LCD12864_MODE_DATA
for (uint8_t idx = 0; idx < 128; idx++)
spi_write(d);
LCD12864_TRANSACTION_END
}
}

View File

@@ -0,0 +1,10 @@
#include <stdbool.h>
#include "lcd12864.h"
#include "spi.h"
#include "stm8s.h"
void lcd12864_clear_display(bool i)
{
lcd12864_command1(i ? LCD12864_CLEARON : LCD12864_CLEAROFF);
}

View File

@@ -0,0 +1,13 @@
#include <stdbool.h>
#include <string.h>
#include "lcd12864.h"
bool lcd12864_clear_segment(struct LCD12864_SEGMENT *segment)
{
if (!segment)
return false;
memset(segment->buffer, segment->clearChar, segment->bufferDepth);
return true;
}

View File

@@ -0,0 +1,11 @@
#include <stdint.h>
#include "lcd12864.h"
#include "spi.h"
void lcd12864_command1(uint8_t c)
{
LCD12864_TRANSACTION_START
LCD12864_MODE_COMMAND
spi_write(c);
LCD12864_TRANSACTION_END
}

View File

@@ -0,0 +1,13 @@
#include <stdint.h>
#include "lcd12864.h"
#include "spi.h"
#include "stm8s.h"
void lcd12864_command_list(const uint8_t *c, uint8_t n)
{
LCD12864_TRANSACTION_START
LCD12864_MODE_COMMAND
for (uint8_t i = 0; i < n; i++)
spi_write(c[i]);
LCD12864_TRANSACTION_END
}

View File

@@ -0,0 +1,88 @@
#include <stdlib.h>
#include <string.h>
#include "lcd12864.h"
#include "font.h"
#include "spi.h"
bool lcd12864_display_segment(struct LCD12864_SEGMENT *segment)
{
if (!segment)
return false;
// allocate spi buffer (for page)
uint8_t spiDepth = segment->cols * ((segment->fontWidth * _FONT_W) + segment->fontSpacing);
uint8_t *spiBuffer = (uint8_t *)malloc(spiDepth);
if (!spiBuffer)
return false;
memset(spiBuffer, 0x00, spiDepth);
uint8_t spiIdx, rb_now, rbit, rpix2, glyph = 0x00;
uint16_t glyphP = 0x00;
uint8_t seg_fH = segment->fontHeight;
// row by row
for (uint8_t row = 0; row < segment->rows; row++)
{
for (uint8_t fH_now = 0; fH_now < seg_fH; fH_now++)
{
uint8_t dlist1[] = {
0x40 | LCD12864_PIX_START,
0xB0 | (segment->page0 + row + fH_now),
(0x10 | (0 >> 4)),
(0x0f & 0x00)}; // |0x04 is offset for normal; 0x00 for upsidedown
lcd12864_command_list(dlist1, sizeof(dlist1));
spiIdx = 0;
rb_now = _FONT_H * fH_now;
for (uint8_t col = 0; col < segment->cols; col++)
{
glyph = segment->buffer[(row * segment->cols) + col];
#ifdef FONTSET_MINI
if (glyph < 0x20 || glyph > 0x60)
glyph = 0x00;
else
glyph -= 0x20;
#endif
glyphP = glyph * _FONT_W;
for (uint8_t fw = 0; fw < _FONT_W; fw++)
{
uint8_t line = _font[glyphP + fw];
if (seg_fH > 1)
{
uint8_t new_line = 0x00;
for (uint8_t idx_now = 0; idx_now < 8; idx_now++)
{
rbit = rb_now + idx_now;
rpix2 = (rbit / seg_fH);
if ((line & (1 << rpix2)))
new_line |= (1 << idx_now);
}
line = new_line;
}
for (uint8_t fW_now = 0; fW_now < segment->fontWidth; fW_now++)
{
spiBuffer[spiIdx++] = line;
}
}
for (uint8_t fs = 0; fs < segment->fontSpacing; fs++)
{
spiBuffer[spiIdx++] = 0x00;
}
}
// push page to display
LCD12864_TRANSACTION_START
LCD12864_MODE_DATA
for (uint8_t idx = 0; idx < spiDepth; idx++)
spi_write(spiBuffer[idx]);
LCD12864_TRANSACTION_END
}
}
if (spiBuffer)
{
free(spiBuffer);
spiBuffer = NULL;
}
return true;
}

View File

@@ -0,0 +1,19 @@
#include <stdbool.h>
#include <stdlib.h>
#include "lcd12864.h"
bool lcd12864_free_segment(struct LCD12864_SEGMENT *segment)
{
if (segment->buffer)
{
free(segment->buffer);
segment->buffer = NULL;
}
if (segment)
{
free(segment);
segment = NULL;
}
return true;
}

View File

@@ -0,0 +1,63 @@
#include <stdint.h>
#include <stdbool.h>
#include "stm8s.h"
#include "lcd12864.h"
#include "spi.h"
#include "delay.h"
bool lcd12864_init(bool reset)
{
/* Initialize CS pin HIGH */
PC_DDR |= PIN4;
PC_CR1 |= PIN4;
PC_ODR |= PIN4;
/* Initialize RES pin HIGH */
PA_DDR |= PIN2;
PA_CR1 |= PIN2;
PA_ODR |= PIN2;
/* Initialize DC pin LOW */
PD_DDR |= PIN2;
PD_CR1 |= PIN2;
PD_ODR &= ~PIN2;
/* Initialize spi */
spi_init();
if (reset)
{
lcd12864_command1(LCD12864_RESET); // Clear Bias: bias 0
PA_ODR |= PIN2;
delay_ms(2);
PA_ODR &= ~PIN2;
delay_ms(2);
PA_ODR |= PIN2;
delay_ms(10);
}
//lcd12864_command1(0xA1); // Reverse direction (SEG131-SEG0)
//lcd12864_command1(0xC8); // SHL 1,COM63-COM0
lcd12864_command1(0xA2); // Clear Bias: bias 0
// Power_Control 4 (internal converter ON) + 2 (internal regulator ON) + 1 (internal follower ON)
lcd12864_command1(0x28 | 0x07); // Power Control
/* Regulator resistor select
** 1+Rb/Ra Vo=(1+Rb/Ra)Vev Vev=(1-(63-a)/162)Vref 2.1v
** 0 3.0 4 5.0(default)
** 1 3.5 5 5.5
** 2 4 6 6
** 3 4.5 7 6.4
*/
lcd12864_command1(0x20 | 0x05); // Regulator resistor
// a(0-63) 32=default Vev=(1-(63-a)/162)Vref 2.1v
lcd12864_command1(0x81); // Set Contrast
lcd12864_command1(21);
lcd12864_command1(0x40); // DisplayLine=0
lcd12864_clear(0x00);
lcd12864_command1(0xAF); // Display ON
return true;
}

View File

@@ -0,0 +1,9 @@
#include <stdbool.h>
#include "lcd12864.h"
#include "spi.h"
#include "stm8s.h"
void lcd12864_invert_display(bool i)
{
lcd12864_command1(i ? LCD12864_INVERTDISPLAY : LCD12864_NORMALDISPLAY);
}

View File

@@ -0,0 +1,31 @@
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "lcd12864.h"
#include "font.h"
struct LCD12864_SEGMENT *lcd12864_new_segment(uint8_t page0, uint8_t fontWidth, uint8_t fontHeight, uint8_t fontSpacing, uint8_t rows, uint8_t clearChar)
{
struct LCD12864_SEGMENT *pSeg = malloc(sizeof(struct LCD12864_SEGMENT));
if (!pSeg)
return NULL;
pSeg->page0 = page0;
pSeg->fontWidth = fontWidth;
pSeg->fontHeight = fontHeight;
pSeg->rows = rows;
pSeg->clearChar = clearChar;
pSeg->fontSpacing = fontSpacing;
pSeg->bufferDepth = (LCD12864_LCDWIDTH / ((fontWidth * _FONT_W) + fontSpacing)) * rows;
pSeg->cols = pSeg->bufferDepth / rows;
if ((!pSeg->buffer) && !(pSeg->buffer = (uint8_t *)malloc(pSeg->bufferDepth)))
{
lcd12864_free_segment(pSeg);
return NULL;
}
lcd12864_clear_segment(pSeg);
return pSeg;
}

View File

@@ -0,0 +1,20 @@
#include "max31855k.h"
#include "stm8s.h"
#include "spi.h"
void max31855k_init()
{
// CS (PA1) as output
PA_DDR |= PIN1;
PA_CR1 |= PIN1;
PA_ODR |= PIN1; // CS high
// CLK PC5 as output
PC_DDR |= PIN5;
PC_CR1 |= PIN5;
PC_ODR |= PIN5; // CLK high
/* Initialize spi */
spi_init();
}

View File

@@ -0,0 +1,30 @@
#include <stdint.h>
#include "delay.h"
#include "max31855k.h"
#include "stm8s.h"
uint16_t max31855k_read()
{
volatile uint16_t value = 0;
// I'm doing bit-bang spi here to return a 16bit uint...
// there is probably a cleaner way..
MAX31855K_TRANSACTION_START
for (uint8_t i = 16; i >= 1; --i)
{
PC_ODR &= ~PIN5;
delay_us(100);
if (PC_IDR & PIN7)
value |= (1 << (i - 1));
PC_ODR |= PIN5;
}
// I stop after 16bits, it works...
MAX31855K_TRANSACTION_END
// With just 16 bits.. We only get a working or not working...
if (value & 0x01)
{
return 0;
}
// Ignoring +/- (always returns as postivive)
return ( value & 0x7fff ) >> 2;
}

View File

@@ -0,0 +1,20 @@
#include "max6675.h"
#include "delay.h"
#include "stm8s.h"
//#include "spi.h"
void max6675_init()
{
// CS (PA1) as output
PA_DDR |= PIN1;
PA_CR1 |= PIN1;
PA_ODR |= PIN1; // CS high
// CLK PC5 as output
PC_DDR |= PIN5;
PC_CR1 |= PIN5;
PC_ODR |= PIN5; // CLK high
delay_ms(600); // gross startup delay
}

View File

@@ -0,0 +1,25 @@
#include <stdint.h>
#include "delay.h"
#include "max6675.h"
#include "stm8s.h"
uint16_t max6675_read()
{
volatile uint16_t value = 0;
MAX6675_TRANSACTION_START
for (uint8_t i = 16; i >= 1; --i)
{
PC_ODR &= ~PIN5;
delay_us(100);
if (PC_IDR & PIN7)
value |= (1 << (i - 1));
PC_ODR |= PIN5;
}
MAX6675_TRANSACTION_END
if (value & 0x4)
{
return 0;
}
return (value >> 3);
}

View File

@@ -0,0 +1,23 @@
#include <stdint.h>
#include "oled.h"
#include "spi.h"
#include "stm8s.h"
void oled_clear_display(void)
{
TRANSACTION_START
for (uint8_t page = 0; page < (OLED_LCDHEIGHT / 8); page++)
{
OLED_MODE_COMMAND
uint8_t dlist1[] = {
0xB0 + page,
OLED_PIX_START,
0x10};
oled_command_list(dlist1, sizeof(dlist1));
OLED_MODE_DATA
for (uint16_t i = 0; i < OLED_LCDWIDTH; i++)
spi_write(0x00);
}
TRANSACTION_END
}

View File

@@ -0,0 +1,14 @@
#include <stdbool.h>
#include <string.h>
#include "oled.h"
bool oled_clear_segment(struct SEGMENT *segment)
{
if (!segment)
return false;
memset(segment->buffer, segment->clearChar, segment->bufferDepth);
return true;
}

View File

@@ -0,0 +1,9 @@
#include <stdint.h>
#include "oled.h"
#include "spi.h"
void oled_command1(uint8_t c)
{
OLED_MODE_COMMAND
spi_write(c);
}

View File

@@ -0,0 +1,12 @@
#include <stdint.h>
#include "oled.h"
#include "spi.h"
#include "stm8s.h"
void oled_command_list(const uint8_t *c, uint8_t n)
{
OLED_MODE_COMMAND
for (uint8_t i = 0; i < n; i++)
spi_write(c[i]);
}

View File

@@ -0,0 +1,19 @@
#include <stdint.h>
#include <stdbool.h>
#include "oled.h"
#include "spi.h"
#include "stm8s.h"
extern uint8_t _contrast;
/*
* Fix me
*/
void oled_dim_display(bool dim)
{
uint8_t contrast = (dim) ? 0 : _contrast;
TRANSACTION_START
oled_command1(SSD1306_SETCONTRAST);
oled_command1(contrast);
TRANSACTION_END
}

View File

@@ -0,0 +1,90 @@
#include <stdlib.h>
#include <string.h>
#include "oled.h"
#include "font.h"
#include "spi.h"
bool oled_display_segment(struct SEGMENT *segment)
{
if (!segment)
return false;
// allocate spi buffer (for page)
uint8_t spiDepth = segment->cols * ((segment->fontWidth * _FONT_W) + segment->fontSpacing);
uint8_t *spiBuffer = (uint8_t *)malloc(spiDepth);
if (!spiBuffer)
return false;
memset(spiBuffer, 0x00, spiDepth);
TRANSACTION_START
uint8_t spiIdx, rb_now, rbit, rpix2, glyph = 0x00;
uint16_t glyphP = 0x00;
uint8_t seg_fH = segment->fontHeight;
// row by row
for (uint8_t row = 0; row < segment->rows; row++)
{
for (uint8_t fH_now = 0; fH_now < seg_fH; fH_now++)
{
OLED_MODE_COMMAND
uint8_t dlist1[] = {
0xB0 + segment->page0 + row + fH_now,
OLED_PIX_START,
0x10};
oled_command_list(dlist1, sizeof(dlist1));
OLED_MODE_DATA
spiIdx = 0;
rb_now = _FONT_H * fH_now;
for (uint8_t col = 0; col < segment->cols; col++)
{
glyph = segment->buffer[(row * segment->cols) + col];
#ifdef FONTSET_MINI
if (glyph < 0x20 || glyph > 0x60)
glyph = 0x00;
else
glyph -= 0x20;
#endif
glyphP = glyph * _FONT_W;
for (uint8_t fw = 0; fw < _FONT_W; fw++)
{
uint8_t line = _font[glyphP + fw];
if (seg_fH > 1)
{
uint8_t new_line = 0x00;
for (uint8_t idx_now = 0; idx_now < 8; idx_now++)
{
rbit = rb_now + idx_now;
rpix2 = (rbit / seg_fH);
if ((line & (1 << rpix2)))
new_line |= (1 << idx_now);
}
line = new_line;
}
for (uint8_t fW_now = 0; fW_now < segment->fontWidth; fW_now++)
{
spiBuffer[spiIdx++] = line;
}
}
for (uint8_t fs = 0; fs < segment->fontSpacing; fs++)
{
spiBuffer[spiIdx++] = 0x00;
}
}
// push page to display
for (uint8_t idx = 0; idx < spiDepth; idx++)
spi_write(spiBuffer[idx]);
}
}
TRANSACTION_END
if (spiBuffer)
{
free(spiBuffer);
spiBuffer = NULL;
}
return true;
}

View File

@@ -0,0 +1,21 @@
#include <stdbool.h>
#include <stdlib.h>
#include "oled.h"
bool oled_free_segment(struct SEGMENT *segment)
{
if (segment->buffer)
{
free(segment->buffer);
segment->buffer = NULL;
}
if (segment)
{
free(segment);
segment = NULL;
}
return true;
}

View File

@@ -0,0 +1,100 @@
#include <stdint.h>
#include <stdbool.h>
#include "stm8s.h"
#include "oled.h"
#include "spi.h"
#include "delay.h"
uint8_t _contrast;
bool oled_init(uint8_t vcs, bool reset)
{
/* Initialize CS pin HIGH */
PC_DDR |= PIN4;
PC_CR1 |= PIN4;
PC_ODR |= PIN4;
/* Initialize RES pin LOW */
PC_DDR |= PIN3;
PC_CR1 |= PIN3;
PC_ODR &= ~PIN3;
/* Initialize DC pin LOW */
PD_DDR |= PIN6;
PD_CR1 |= PIN6;
PD_ODR &= ~PIN6;
/* Initialize spi */
spi_init();
if (reset)
{
PC_ODR |= PIN3;
delay_ms(1);
PC_ODR &= ~PIN3;
delay_ms(10);
PC_ODR |= PIN3;
}
TRANSACTION_START
// Init sequence
static const uint8_t init1[] = {
SSD1306_DISPLAYOFF, // 0xAE
SSD1306_SETDISPLAYCLOCKDIV, // 0xD5
0x80, // the suggested ratio 0x80
SSD1306_SETMULTIPLEX}; // 0xA8
oled_command_list(init1, sizeof(init1));
oled_command1(OLED_LCDHEIGHT - 1);
static const uint8_t init2[] = {
SSD1306_SETDISPLAYOFFSET, // 0xD3
0x0, // no offset
SSD1306_SETSTARTLINE | 0x0, // line #0
SSD1306_CHARGEPUMP}; // 0x8D
oled_command_list(init2, sizeof(init2));
oled_command1((vcs == SSD1306_EXTERNALVCC) ? 0x10 : 0x14);
static const uint8_t init3[] = {
SSD1306_SEGREMAP | 0x1,
SSD1306_COMSCANDEC};
oled_command_list(init3, sizeof(init3));
uint8_t comPins = 0x02;
uint8_t contrast = 0x8F;
#if ((OLED_LCDWIDTH == 128) && (OLED_LCDHEIGHT == 32))
comPins = 0x02;
contrast = 0x8F;
#elif ((OLED_LCDWIDTH == 128) && (OLED_LCDHEIGHT == 64))
comPins = 0x12;
contrast = (vcs == SSD1306_EXTERNALVCC) ? 0x9F : 0xCF;
#elif ((OLED_LCDWIDTH == 96) && (OLED_LCDHEIGHT == 16))
comPins = 0x2;
contrast = (vcs == SSD1306_EXTERNALVCC) ? 0x10 : 0xAF;
#endif
oled_command1(SSD1306_SETCOMPINS);
oled_command1(comPins);
oled_command1(SSD1306_SETCONTRAST);
oled_command1(contrast);
_contrast = contrast;
oled_command1(SSD1306_SETPRECHARGE); // 0xd9
oled_command1((vcs == SSD1306_EXTERNALVCC) ? 0x22 : 0xF1);
static const uint8_t init5[] = {
SSD1306_SETVCOMDETECT, // 0xDB
0x40,
SSD1306_DISPLAYALLON_RESUME, // 0xA4
SSD1306_NORMALDISPLAY, // 0xA6
SSD1306_DEACTIVATE_SCROLL,
SSD1306_DISPLAYON}; // Main screen turn on
oled_command_list(init5, sizeof(init5));
TRANSACTION_END
oled_clear_display();
return true;
}

View File

@@ -0,0 +1,12 @@
#include <stdbool.h>
#include "oled.h"
#include "spi.h"
#include "stm8s.h"
void oled_invert_display(bool i)
{
TRANSACTION_START
oled_command1(i ? SSD1306_INVERTDISPLAY : SSD1306_NORMALDISPLAY);
TRANSACTION_END
}

View File

@@ -0,0 +1,30 @@
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "oled.h"
#include "font.h"
struct SEGMENT *oled_new_segment(uint8_t page0, uint8_t fontWidth, uint8_t fontHeight, uint8_t fontSpacing, uint8_t rows, uint8_t clearChar)
{
struct SEGMENT *pSeg = malloc(sizeof(struct SEGMENT));
if (!pSeg)
return NULL;
pSeg->page0 = page0;
pSeg->fontWidth = fontWidth;
pSeg->fontHeight = fontHeight;
pSeg->rows = rows;
pSeg->clearChar = clearChar;
pSeg->fontSpacing = fontSpacing;
pSeg->bufferDepth = (OLED_LCDWIDTH / ((fontWidth * _FONT_W) + fontSpacing)) * rows;
pSeg->cols = pSeg->bufferDepth / rows;
if ((!pSeg->buffer) && !(pSeg->buffer = (uint8_t *)malloc(pSeg->bufferDepth)))
{
oled_free_segment(pSeg);
return NULL;
}
oled_clear_segment(pSeg);
return pSeg;
}

View File

@@ -0,0 +1,9 @@
#include "spi.h"
#include "stm8s.h"
void spi_init()
{
/* Initialize SPI master at 2mHz */
//SPI_CR2 = SPI_CR2_SSM | SPI_CR2_SSI;
SPI_CR1 = SPI_CR1_MSTR; // | SPI_CR1_BR(0b010);
}

View File

@@ -0,0 +1,10 @@
#include "spi.h"
#include "stm8s.h"
uint8_t spi_read()
{
spi_write(0xFF);
while (!(SPI_SR & SPI_SR_RxNE))
;
return SPI_DR;
}

View File

@@ -0,0 +1,9 @@
#include "spi.h"
#include "stm8s.h"
void spi_write(uint8_t data)
{
SPI_DR = data;
while (!(SPI_SR & SPI_SR_TXE))
;
}

View File

@@ -0,0 +1,12 @@
#include "uart.h"
#include "stm8s.h"
void uart_init() {
/* round to nearest integer */
uint16_t div = (F_CPU + BAUDRATE / 2) / BAUDRATE;
/* madness.. */
UART1_BRR2 = ((div >> 8) & 0xF0) + (div & 0x0F);
UART1_BRR1 = div >> 4;
/* enable transmitter and receiver */
UART1_CR2 = UART_CR2_TEN | UART_CR2_REN;
}

View File

@@ -0,0 +1,7 @@
#include "uart.h"
#include "stm8s.h"
uint8_t uart_read() {
while (!(UART1_SR & UART_SR_RXNE));
return UART1_DR;
}

View File

@@ -0,0 +1,10 @@
#include "uart.h"
#include "uart_stdin.h"
/*
* Redirect stdin to UART
*/
int getchar()
{
return uart_read();
}

View File

@@ -0,0 +1,11 @@
#include "uart.h"
#include "uart_stdout.h"
/*
* Redirect stdout to UART
*/
int putchar(int c)
{
uart_write(c);
return 0;
}

View File

@@ -0,0 +1,10 @@
#include "uart.h"
#include "uart_stdout.h"
void putstring(uint8_t *str)
{
while (*str !=0)
{
putchar(*str++);
}
}

View File

@@ -0,0 +1,7 @@
#include "uart.h"
#include "stm8s.h"
void uart_write(uint8_t data) {
UART1_DR = data;
while (!(UART1_SR & UART_SR_TC));
}