Skip to content

Commit

Permalink
now support both sonoff black and white boxes
Browse files Browse the repository at this point in the history
  • Loading branch information
mightymos committed Jun 2, 2024
1 parent 02eb9cd commit 108e49b
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 154 deletions.
26 changes: 17 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,21 @@
# make clean

# Target MCU settings --------------------------------------------------
# uncomment only one target board
# FIXME: build all targets without need to modify makefile
# sonoff black box
TARGET_BOARD = EFM8BB1
#TARGET_BOARD = EFM8BB1LCB

#TARGET_BOARD = EFM8BB1
# low cost development board
TARGET_BOARD = EFM8BB1LCB
# sonoff white box
#TARGET_BOARD = OB38S003


# catches undefined
ifndef TARGET_BOARD
$(error Please define TARGET_BOARD in makefile)
endif

# these are the maximum clock speeds
ifeq ($(TARGET_BOARD), EFM8BB1)
# for EFM8BB1 in Sonoff v2.0 receivers (black color box)
MCU_FREQ_KHZ = 24500
Expand All @@ -62,35 +65,40 @@ MCU_FREQ_KHZ = 24500
else ifeq ($(TARGET_BOARD), OB38S003)
# for OB38S003 used in Sonoff v2.2 receivers (white color box)
MCU_FREQ_KHZ = 16000
#else
# $(error Please define TARGET_BOARD as EFM8BB1, EFM8BB1LCB, or OB38S003 in makefile)
endif

#
MEMORY_SIZES = --iram-size 256 --xram-size 256 --code-size 8192
MEMORY_MODEL = --model-small
HAS_DUAL_DPTR = n

#
# generic source directory
SOURCE_DIR = src

# target specific sources
ifeq ($(TARGET_BOARD), EFM8BB1)
DRIVER_SRC_DIR = drivers/efm8bb1/src
else ifeq ($(TARGET_BOARD), EFM8BB1LCB)
DRIVER_SRC_DIR = drivers/efm8bb1/src
else ifeq ($(TARGET_BOARD), OB38S003)
DRIVER_SRC_DIR = drivers/ob38s003/src
endif


# generic includes
INCLUDE_DIR = inc/

# target specific includes
ifeq ($(TARGET_BOARD), EFM8BB1)
DRIVER_DIR = drivers/efm8bb1/inc
else ifeq ($(TARGET_BOARD), EFM8BB1LCB)
DRIVER_DIR = drivers/efm8bb1/inc
else ifeq ($(TARGET_BOARD), OB38S003)
DRIVER_DIR = drivers/ob38s003/inc
endif


# place intermediate build files here so other directories are not cluttered
OBJECT_DIR = object
# final location of built firmware
BUILD_DIR = build

SOURCES = $(SOURCE_DIR)/main_passthrough.c \
Expand Down
32 changes: 13 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
# Introduction

This is an alternative firmware targetting the 8052 based On-Bright OB38S003 microcontroller.
This microcontroller is present in the Sonoff Bridge R2 v2.2 433 MHz radio to wifi bridge.
An alternative firmware for 433 MHz radio to wifi bridges, targetting these boards/microcontrollers:
This microcontroller is present in the 433 MHz radio to wifi bridge.

| Board | Microcontroller | Passthrough | RCSwitch | Portisch |
| ------------- | ------------- | ------------- | ------------- | ------------- |
| Sonoff Bridge R2 v1.0 (black box) | OB38S003 | Supported | Decode only | No |
| Sonoff Bridge R2 v2.2 (white box) | EFM8BB1 | Supported | Decode only | No |
| EFM8BB1 Busy Bee Low Cost Kit Board | EFM8BB1 | Supported | Decode only | No |

The OB38S003 was originally intended for radio decoding but lacked support for additional protocols.
These microcontrollers were originally intended for radio decoding but lacked support for additional protocols.
As a consequence many people just bypass the microcontroller with a hardware modification:
https://github.com/arendst/Tasmota/discussions/13283

Decoding may then be done directly on the ESP8265 (e.g., ESPHome supports rcswitch).
Decoding may then be done directly on the ESP8265 (e.g., ESPHome or Tasmota or typically used for this purpose).


The intent here is to avoid the need to perform hardware modification.
Expand All @@ -29,33 +34,22 @@ The firmware radio decoding is inspired by 1) RF-Bridge-EFM8BB1 (Portisch) and 2
THIS IS A WORK IN PROGRESS and should not be used by typical users.

Erasing and reprogramming the microcontroller is cumbersome without the official programmer.
I am using the microcontroller as a pass through to ESPHome now.
So the code is a mess for selecting other uses.

That said, decoding of protocols 1 and 2 seems to work reliably.
For example door reed sensors - both stock and modified - have been successfully decoded.
https://github.com/mightymos/ReedTripRadio

| Feature | detail | status |
| ------------- | ------------- | ------------- |
| Protocol testing | we need some scheme to evaluate reliability | TODO |
| Sniffing | allow viewing timing of unknown radio protocols | TODO |
| Transmission | operate as remote control | TODO |
| Port to R2 v1.0 bridge (black box) | requires makefile | TODO |
However, an open source and inexpensive programmer is discussed under Flasher section.

# Installation
Install SDCC compiler for your platform:
https://sdcc.sourceforge.net/

On command line run make.
For now modify Makefile to select desired target.
Built firmware placed in build directory.
See Flasher section below.

# Previous Work

An attempt was made to compile portisch with the open source SDCC compiler.
8KB code space appeared to be exceeded.
An successful attempt was made to compile "Portisch" with the open source SDCC compiler.
I personally found the source code to be difficult to read.
I hope to be able to use this work to use Portisch on the newer Sonoff boxes.
https://github.com/mightymos/SonOfPortisch

There are at least three versions of rcswitch.
Expand Down
37 changes: 13 additions & 24 deletions drivers/efm8bb1/inc/hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
* Author:
*/

#ifndef INC_GLOBALS_H_
#define INC_GLOBALS_H_
#ifndef INC_HAL_H_
#define INC_HAL_H_

#if defined(TARGET_BOARD_EFM8BB1LCB)
#include "efm8bb1lck_pins.h"
#elif defined(TARGET_BOARD_EFM8BB1)
#include "sonoffr20_pins.h"
#endif

#include <stdbool.h>
#include <stdint.h>
Expand Down Expand Up @@ -106,34 +110,19 @@ inline void uart_rx_disabled(void)
SCON0 &= ~REN__RECEIVE_ENABLED;
}

inline void debug_pin0_on(void)
inline void soft_tx_pin_on(void)
{
DEBUG_PIN0 = 1;
SOFT_TX_PIN = 1;
}

inline void debug_pin0_off(void)
inline void soft_tx_pin_off(void)
{
DEBUG_PIN0 = 0;
SOFT_TX_PIN = 0;
}

inline void debug_pin0_toggle(void)
inline void soft_tx_pin_toggle(void)
{
DEBUG_PIN0 = !DEBUG_PIN0;
}

inline void debug_pin1_on(void)
{
DEBUG_PIN1 = 1;
}

inline void debug_pin1_off(void)
{
DEBUG_PIN1 = 0;
}

inline void debug_pin1_toggle(void)
{
DEBUG_PIN1 = !DEBUG_PIN1;
SOFT_TX_PIN = !SOFT_TX_PIN;
}


Expand Down Expand Up @@ -218,4 +207,4 @@ extern unsigned char get_timer2_high(void);

extern void clear_capture_flag(void);

#endif // INC_GLOBALS_H_
#endif // INC_HAL_H_
1 change: 0 additions & 1 deletion drivers/efm8bb1/src/timer_interrupts.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ void pca0_isr(void) __interrupt (PCA0_VECTOR)

PCA0CN0 &= ~flags;

debug_pin0_toggle();

//if( (PCA0PWM & COVF__BMASK) && (PCA0PWM & ECOV__BMASK))
//{
Expand Down
17 changes: 6 additions & 11 deletions drivers/ob38s003/inc/hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,19 @@ inline bool get_radio_wake(void)
return RF_ENABLE;
}

inline void reset_pin_on(void)
inline void soft_tx_pin_on(void)
{
RESET_PIN = 1;
SOFT_TX_PIN = 1;
}

inline void reset_pin_off(void)
inline void soft_tx_pin_off(void)
{
RESET_PIN = 0;
SOFT_TX_PIN = 0;
}

inline void reset_pin_toggle(void)
inline void soft_tx_pin_toggle(void)
{
RESET_PIN = !RESET_PIN;
}

inline bool reset_level(void)
{
return RESET_PIN;
SOFT_TX_PIN = !SOFT_TX_PIN;
}

inline void tdata_on(void)
Expand Down
2 changes: 1 addition & 1 deletion drivers/ob38s003/src/timer_interrupts.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <stdint.h>
#include <stdlib.h>

// OB38S003 uses timer 2 capture mode for similar functionality to pca on EFM8BB1
// OB38S003 uses timer 2 capture mode for similar functionality to PCA on EFM8BB1
#include "OB38S003.h"


Expand Down
18 changes: 14 additions & 4 deletions inc/efm8BB1lck_pins.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
// EFM8BB1LCK board
#ifndef INC_EFM8BB1LCK_PINS_H_
#define INC_EFM8BB1LCK_PINS_H_

// EFM8BB1LCK board
#define TDATA P0_0

#define LED P1_0
// error in UG377: EFM8BB1LCK Users Guide ??
// lists PCA0 and LED pin as the same
#define LED P1_4
#define RDATA P1_3
#define BUZZER P1_6

#define DEBUG_PIN0 P1_5
#define DEBUG_PIN1 P1_6
// UART TX, RX routed to Port pins P0.4 and P0.5
#define UART_TX_PIN P0_4
#define UART_RX_PIN P0_5

// software uart
#define SOFT_TX_PIN P1_5

#endif
15 changes: 3 additions & 12 deletions inc/sonoffr20_pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,15 @@

// Sonoff with black case
#define TDATA P0_0

// error in UG377: EFM8BB1LCK Users Guide ??
// lists PCA0 and LED pin as the same
// but SonOfPortisch works with these pins
// on development board
// FIXME: handle sonoff versus development board
#define LED P1_4
#define LED P1_0
#define RDATA P1_3


#define BUZZER P1_6

// UART TX, RX routed to Port pins P0.4 and P0.5
#define UART_TX_PIN P0_4
#define UART_RX_PIN P0_5

// may be used for software uart
#define DEBUG_PIN0 P1_5
#define DEBUG_PIN1 P1_6
// software uart
#define SOFT_TX_PIN P2_0

#endif
7 changes: 4 additions & 3 deletions inc/sonoffr22_pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
#define UART_TX_PIN P1_0
#define UART_RX_PIN P1_1

// reset pin does not appear to be connected to anything except copper pad so can use as a debug output
// reset pin is connected to copper pad so can use as a debug output
// need to set as gpio in SMAP AC utility when flashing (i.e., disable reset function)
#define RESET_PIN P1_5
#define RDATA P1_6
// can be used with software uart
#define SOFT_TX_PIN P1_5
#define RDATA P1_6

// connected to resistor R23 which is 1kohm resistor as upper leg of 10kohm bottom resistor divider
#define RF_ENABLE P1_7
Expand Down
42 changes: 25 additions & 17 deletions inc/uart_software.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,50 @@

#include <stdbool.h>

#if TARGET_BOARD_EFM8BB1
#include "sonoffr20_pins.h"

// pin definitions
#define RXB DEBUG_PIN0
#define TXB DEBUG_PIN1
//#define BTN P3_2

//define baudrate const
// example formula was for STC15 8051 microcontroller with 24MHz / 3 = 8MHz max. clock at 3.3V ??
//BAUD = 65536 - FOSC/3/BAUDRATE/M (1T:M=1; 12T:M=12)
//NOTE: (FOSC/3/BAUDRATE) must be greater than 98, (RECOMMEND GREATER THAN 110)

// example for efm8bb1
// #define BAUD (65536 - (MCU_FREQ/2400/12))
// hex(65536 - (24500000/2400/12))
#if defined(TARGET_BOARD_EFM8BB1LCB)
#include "efm8bb1lck_pins.h"

// pin definitions
#define RXB BUZZER
#define TXB SOFT_TX_PIN
//#define BTN P3_2

// e.g., hex(65536 - (24500000/2400/12))
#define SOFT_BAUD 0xFCAD

#endif
#elif defined(TARGET_BOARD_EFM8BB1)

#if TARGET_BOARD_OB38S003
#include "sonoffr22_pins.h"
#include "sonoffr20_pins.h"

// pin definitions
#define RXB BUZZER
#define TXB RESET_PIN
#define TXB SOFT_TX_PIN
//#define BTN P3_2

// example
// hex(65536 - (16000000/2400/1))
// e.g., hex(65536 - (24500000/2400/12))
#define SOFT_BAUD 0xFCAD

#elif defined(TARGET_BOARD_OB38S003)

#include "sonoffr22_pins.h"

#define RXB BUZZER
#define TXB SOFT_TX_PIN
//#define BTN P3_2

// e.g, hex(65536 - (16000000/2400/1))
#define SOFT_BAUD 0xE5F5

#endif




extern unsigned char TBUF, RBUF;
extern __bit TING,RING;
extern __bit TEND,REND;
Expand Down
Loading

0 comments on commit 108e49b

Please sign in to comment.