Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Introduce hardware abstraction layer #64

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
# generated files:
*.hex
*.ihx
*.bin
.*.swp
build/*
config

# vim
.*.swp

# ctags
tags

# vs/platformio
.pioenvs
.piolibdeps
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/.browse.c_cpp.db*

# misc
config
*.diff
diff
22 changes: 19 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
SDCC ?= sdcc
STCCODESIZE ?= 4089
SDCCOPTS ?= --code-size $(STCCODESIZE) --xram-size 0 --data-loc 0x30 --disable-warning 126 --disable-warning 59
SDCCREV ?= -Dstc15f204ea
STCGAL ?= stcgal/stcgal.py
STCGALOPTS ?=
STCGALPORT ?= /dev/ttyUSB0
Expand All @@ -10,6 +9,23 @@ FLASHFILE ?= main.hex
SYSCLK ?= 11059
CFLAGS ?= -DWITH_ALT_LED9 -DWITHOUT_LEDTABLE_RELOC -DSHOW_TEMP_DATE_WEEKDAY

#
# You need to select which hardware you want to compile for:
#

# the most common one:
HARDWARE=default

# STC15W408AS + voice + 3 buttons:
#HARDWARE=stc15w408as

# one (rare) variant
#HARDWARE=modelc



SDCCOPTS+=-DHARDWARE=\"hal/$(HARDWARE).h\"

SRC = src/adc.c src/ds1302.c

OBJ=$(patsubst src%.c,build%.rel, $(SRC))
Expand All @@ -18,10 +34,10 @@ all: main

build/%.rel: src/%.c src/%.h
mkdir -p $(dir $@)
$(SDCC) $(SDCCOPTS) $(SDCCREV) -o $@ -c $<
$(SDCC) $(SDCCOPTS) -o $@ -c $<

main: $(OBJ)
$(SDCC) -o build/ src/[email protected] $(SDCCOPTS) $(SDCCREV) $(CFLAGS) $^
$(SDCC) -o build/ src/[email protected] $(SDCCOPTS) $(CFLAGS) $^
@ tail -n 5 build/main.mem | head -n 2
@ tail -n 1 build/main.mem
cp build/[email protected] [email protected]
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ choose platformio (preferred) or traditional make build
* choose which mcu you are building for by uncommenting one `env_default` in `platformio.ini`
* adjust `upload_port` as needed in `platformio.ini`

Then you can use the [standard platformio commands](https://docs.platformio.org/en/stable/core/quickstart.html#process-project)
```
pio run
pio run --target upload
```

### traditional make
```
make clean
Expand Down
8 changes: 1 addition & 7 deletions docs/nmea/nmea_howto.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@ requires STC15W408AS cpu with > 4K flash.
STCCODESIZE ?= 8184
SDCCOPTS ?= --code-size $(STCCODESIZE) --stack-auto --iram-size 256 --xram-size 256 --data-loc 0x30 --disable-warning 126 --disable-warning 59

if you have a clock with STC15W408AS + voice + 3 buttons, compile it with

SDCCREV ?= -Dstc15w408as

otherwise, e.g. if you swapped STC15W404AS -> STC15W408AS, keep using

SDCCREV ?= -Dstc15w404as
Double check the hardware target at the beginning of the Makefile ("HARDWARE" variable)

- timezone and dst settings are saved to EEPROM and reset on each reflash,
so cycle power of the clock after setting them for proper synchronization
Expand Down
2 changes: 1 addition & 1 deletion src/ds1302.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ uint8_t ds_readbyte(uint8_t addr) {
void ds_readburst() {
// ds1302 burst-read 8 bytes into struct
uint8_t j, b;
b = DS_CMD | DS_CMD_CLOCK | DS_BURST_MODE << 1 | DS_CMD_READ;
b = DS_CMD | DS_CMD_CLOCK | (DS_BURST_MODE << 1) | DS_CMD_READ;
DS_CE = 0;
DS_SCLK = 0;
DS_CE = 1;
Expand Down
10 changes: 5 additions & 5 deletions src/ds1302.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

#include "stc15.h"
#include <stdint.h>
#include "hwconfig.h"
#include HARDWARE

#define _nop_ __asm nop __endasm;

#define DS_CMD 1 << 7
#define DS_CMD_READ 1
#define DS_CMD (1u << 7)
#define DS_CMD_READ 1u
#define DS_CMD_WRITE 0
#define DS_CMD_RAM 1 << 6
#define DS_CMD_CLOCK 0 << 6
#define DS_CMD_CLOCK (0u << 6)

#define DS_ADDR_SECONDS 0
#define DS_ADDR_MINUTES 1
Expand All @@ -34,7 +34,7 @@
#define DS_TC_D2_4KO 0b1010
#define DS_TC_D2_8KO 0b1011

#define DS_BURST_MODE 31
#define DS_BURST_MODE 31u

// DS_ADDR_SECONDS c111_1111 0_0-5_9 c=clock_halt
// DS_ADDR_MINUTES x111_1111 0_0-5_9
Expand Down
46 changes: 46 additions & 0 deletions src/hal/default.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#ifndef HWCONFIG_H
#define HWCONFIG_H

/*
* the main model, based either on stc15f204ea or stc15w404as MCU
*/

// alias for relay and buzzer outputs, using relay to drive led for indication of main loop status
#define BUZZER P1_5
#define BUZZER_ON BUZZER = 0
#define BUZZER_OFF BUZZER = 1

// 7-seg led port setup

// which port the segments are connected to
#define LED_SEGMENT_PORT P2
// which port controls the digits
#define LED_DIGITS_PORT P3

// offset where the digits start on LED_DIGITS_PORT
#define LED_DIGITS_PORT_BASE 2

// setup macro mask to turn off digits
#define LED_DIGITS_OFF() ( LED_DIGITS_PORT |= (0b1111 << LED_DIGITS_PORT_BASE))
// setup macro to turn on single digit
#define LED_DIGIT_ON(digit) (LED_DIGITS_PORT &= ~((1<<LED_DIGITS_PORT_BASE) << digit))

// adc channels for sensors, P1_n
#define ADC_LIGHT 6
#define ADC_TEMP 7

// button switch aliases
#define SW1 P3_1
#define SW2 P3_0
#define NUM_SW 2

// ds1302 pins
#define DS_CE P1_0
#define DS_IO P1_1
#define DS_SCLK P1_2
// needed for asm optimizations
#define _DS_IO _P1_1
#define _DS_SCLK _P1_2

#endif // #ifndef HWCONFIG_H

47 changes: 47 additions & 0 deletions src/hal/modelc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#ifndef HWCONFIG_H
#define HWCONFIG_H

/*
* The variant based on MCU stc15f204 and described here:
* https://github.com/zerog2k/stc_diyclock/issues/20
*/

// alias for relay and buzzer outputs, using relay to drive led for indication of main loop status
#define BUZZER P3_3
#define BUZZER_ON BUZZER = 0
#define BUZZER_OFF BUZZER = 1

// 7-seg led port setup

// which port the segments are connected to
#define LED_SEGMENT_PORT P2
// which port controls the digits
#define LED_DIGITS_PORT P3

// offset where the digits start on LED_DIGITS_PORT
#define LED_DIGITS_PORT_BASE 4

// setup macro mask to turn off digits
#define LED_DIGITS_OFF() ( LED_DIGITS_PORT |= (0b1111 << LED_DIGITS_PORT_BASE))
// setup macro to turn on single digit
#define LED_DIGIT_ON(digit) (LED_DIGITS_PORT &= ~((1<<LED_DIGITS_PORT_BASE) << digit))

// adc channels for sensors, P1_n
#define ADC_LIGHT 3
#define ADC_TEMP 6

// button switch aliases
#define SW1 P3_1
#define SW2 P3_0
#define NUM_SW 2

// ds1302 pins
#define DS_CE P0_0
#define DS_IO P0_1
#define DS_SCLK P3_2
// needed for asm optimizations
#define _DS_IO _P0_1
#define _DS_SCLK _P3_2

#endif // #ifndef HWCONFIG_H

48 changes: 48 additions & 0 deletions src/hal/stc15w408as.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#ifndef HWCONFIG_H
#define HWCONFIG_H

/*
* hardware with stc15w408as and voice chips
*/

#define LED P1_5
// no buzzer ?
#define BUZZER_ON
#define BUZZER_OFF

// 7-seg led port setup

// which port the segments are connected to
#define LED_SEGMENT_PORT P2
// which port controls the digits
#define LED_DIGITS_PORT P3

// offset where the digits start on LED_DIGITS_PORT
#define LED_DIGITS_PORT_BASE 2

// setup macro mask to turn off digits
#define LED_DIGITS_OFF() ( LED_DIGITS_PORT |= (0b1111 << LED_DIGITS_PORT_BASE))
// setup macro to turn on single digit
#define LED_DIGIT_ON(digit) (LED_DIGITS_PORT &= ~((1<<LED_DIGITS_PORT_BASE) << digit))

// adc channels for sensors, P1_n
#define ADC_LIGHT 6
#define ADC_TEMP 7

// button switch aliases
// the stc15w408as has a third one
#define SW1 P3_1
#define SW2 P3_0
#define SW3 P1_4
#define NUM_SW 3

// ds1302 pins
#define DS_CE P1_0
#define DS_IO P1_1
#define DS_SCLK P1_2
// needed for asm optimizations
#define _DS_IO _P1_1
#define _DS_SCLK _P1_2

#endif // #ifndef HWCONFIG_H

92 changes: 0 additions & 92 deletions src/hwconfig.h

This file was deleted.

Loading