Skip to content

Commit

Permalink
add in periodic led toggle to show life; commented debug statements; …
Browse files Browse the repository at this point in the history
…removed unused variables
  • Loading branch information
mightymos committed Jun 27, 2024
1 parent b5e81bd commit 0d88cad
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 112 deletions.
3 changes: 3 additions & 0 deletions inc/sonoffr20_pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@
// software uart
#define SOFT_TX_PIN P2_0

// this is the buzzer pin so we would not normally use this on hardware without removing buzzer
#define DEBUG_PIN01 P1_6

#endif
3 changes: 0 additions & 3 deletions inc/state_machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,4 @@ void radio_rfin(void);
void radio_decode_debug(void);
void radio_decode_timings(void);


//extern __xdata uint8_t lengthExpected;

#endif // STATE_MACHINE_H
4 changes: 3 additions & 1 deletion src/main_passthrough.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ int main (void)

// just to give some startup time
delay1ms(500);


// shows power is on
led_on();

// watchdog will force a reset, unless we periodically write to it, demonstrating loop is not stuck somewhere
enable_watchdog();
Expand Down
109 changes: 46 additions & 63 deletions src/main_rcswitch.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
// the classic library for radio packet decoding
#include "rcswitch.h"

//
// similar to portisch commands
#include "state_machine.h"

// generic tick logic independent of controller
Expand All @@ -60,6 +60,7 @@
// hardware specific
#include "timer_interrupts.h"

// ring buffer supported
#include "uart.h"


Expand All @@ -74,50 +75,39 @@
// for software uart
// FIXME: if reset pin is set to reset function, instead of gpio, does this interfere with anything (e.g., software serial?)
extern void tm0(void) __interrupt (d_T0_Vector);
// tick
// supports timeout
extern void timer1_isr(void) __interrupt (d_T1_Vector);
// pca like capture mode for radio decoding
extern void timer2_isr(void) __interrupt (d_T2_Vector);
// hardware uart
extern void uart_isr(void) __interrupt (d_UART0_Vector);

#elif defined(TARGET_BOARD_EFM8BB1)
extern void tm0(void) __interrupt (TIMER0_VECTOR);
extern void timer2_isr(void) __interrupt (TIMER2_VECTOR);
extern void uart_isr(void) __interrupt (UART0_VECTOR);
extern void pca0_isr(void) __interrupt (PCA0_VECTOR);

#elif defined(TARGET_BOARD_EFM8BB1LCB)
#elif defined(TARGET_BOARD_EFM8BB1) || defined(TARGET_BOARD_EFM8BB1LCB)

// software uart
extern void tm0(void) __interrupt (TIMER0_VECTOR);
// tick
// supports timeout
extern void timer2_isr(void) __interrupt (TIMER2_VECTOR);
// hardware uart (uses timer 1)
extern void uart_isr(void) __interrupt (UART0_VECTOR);
// radio decoding
extern void pca0_isr(void) __interrupt (PCA0_VECTOR);

// unique ID is stored in xram (MSB at address 0xFF)
#define ID0_ADDR_RAM 0xFF
#define ID1_ADDR_RAM 0xFE
#define ID2_ADDR_RAM 0xFD
#define ID3_ADDR_RAM 0xFC


//
//static const __xdata unsigned char *guid0 = (__xdata unsigned char*) ID0_ADDR_RAM;
//static const __xdata unsigned char *guid1 = (__xdata unsigned char*) ID1_ADDR_RAM;
//static const __xdata unsigned char *guid2 = (__xdata unsigned char*) ID2_ADDR_RAM;
//static const __xdata unsigned char *guid3 = (__xdata unsigned char*) ID3_ADDR_RAM;

void startup_uid(void)
{
puthex2(*((__xdata unsigned char*) ID0_ADDR_RAM));
puthex2(*((__xdata unsigned char*) ID1_ADDR_RAM));
puthex2(*((__xdata unsigned char*) ID2_ADDR_RAM));
puthex2(*((__xdata unsigned char*) ID3_ADDR_RAM));
}
//#define ID0_ADDR_RAM 0xFF
//#define ID1_ADDR_RAM 0xFE
//#define ID2_ADDR_RAM 0xFD
//#define ID3_ADDR_RAM 0xFC

// this will fail if we assign external ram to values which are initialized
// and we really do not need the feature anyway
//void startup_uid(void)
//{
// puthex2(*((__xdata unsigned char*) ID0_ADDR_RAM));
// puthex2(*((__xdata unsigned char*) ID1_ADDR_RAM));
// puthex2(*((__xdata unsigned char*) ID2_ADDR_RAM));
// puthex2(*((__xdata unsigned char*) ID3_ADDR_RAM));
//}

#else
#error Please define TARGET_BOARD in makefile
Expand Down Expand Up @@ -177,26 +167,17 @@ void startup_blink(void)
// ----------------------------------------------------------------------------
int main (void)
{

// holdover from when we considered using rtos
//const __idata unsigned char* stackStart = (__idata unsigned char*) get_stack_pointer() + 1;

// have only tested decoding with two protocols so far
//const uint8_t repeats = 8;
// FIXME: comment on what this does
// lowest ID is 1
//const uint8_t protocolId = 1;

// track elapsed time for doing something periodically (e.g., toggle LED every 10 seconds)
//unsigned long previousTimeSendRadio = 0;
//unsigned long elapsedTimeSendRadio;
//__xdata uint16_t previousTimeHeartbeat = 0;
//__xdata uint16_t elapsedTimeHeartbeat;

// just track how many loops have transpired as a very rough way of tracking time
__xdata uint32_t ticks = 0;

// set a threshold
// about every six seconds @ 24500000 MHz
const uint32_t heartbeat = 0x80000;

// upper eight bits hold error or no data flags
__xdata unsigned int rxdataWithFlags = UART_NO_DATA;

// allows communication between uart state machine and radio state machine
__xdata RF_COMMAND_T rfCommand = NONE;


Expand All @@ -211,8 +192,10 @@ int main (void)
buzzer_off();
tdata_off();

//
debug_pin01_off();
// DEBUG:
// on some boards, "debug pin" is actually buzzer
// so we do not want to use it for debugging unless buzzer has been removed
//debug_pin01_off();

//
startup_blink();
Expand Down Expand Up @@ -290,6 +273,8 @@ int main (void)
// just to give some startup time
delay1ms(500);

// shows power is on
led_on();

// watchdog will force a reset, unless we periodically write to it, demonstrating loop is not stuck somewhere
enable_watchdog();
Expand All @@ -299,11 +284,11 @@ int main (void)
putstring("boot\r\n");
#endif

#if defined(TARGET_BOARD_EFM8BB1) || defined(TARGET_BOARD_EFM8BB1LCB)
putstring("uid:");
startup_uid();
putstring("\r\n");
#endif
//#if defined(TARGET_BOARD_EFM8BB1) || defined(TARGET_BOARD_EFM8BB1LCB)
// putstring("uid:");
// startup_uid();
// putstring("\r\n");
//#endif

while (true)
{
Expand Down Expand Up @@ -359,26 +344,24 @@ int main (void)

// FIXME: if we use software uart to send debug output, this will be slow to be re-enabled
enable_capture_interrupt();

}


#if 0
// do a task like blink led about every XX seconds to show loop is alive
//elapsedTimeHeartbeat = get_elapsed_timer1(previousTimeHeartbeat);
#if 1

//
//if (elapsedTimeHeartbeat >= 1000000)
if (elapsedTimeHeartbeat >= 2^16)
//if (elapsedTimeHeartbeat >= 10)
// track time roughly
ticks++;

// compare to threshold
if (ticks > heartbeat)
{
// DEBUG
//debug_pin01_toggle();

led_toggle();
// previousTimeHeartbeat = get_current_timer1();
// reset count
ticks = 0;
}

#endif
Expand Down
40 changes: 17 additions & 23 deletions src/rcswitch.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,10 @@ void transmit(const bool invertedSignal, uint16_t delayHigh, uint16_t delayLow)
__xdata uint8_t firstLogicLevel = invertedSignal ? 0 : 1;
__xdata uint8_t secondLogicLevel = invertedSignal ? 1 : 0;

//__xdata uint16_t previous;
//__xdata uint16_t elapsed;
__xdata uint16_t delay;

set_tdata(firstLogicLevel);
// DEBUG: mirror transmitted pulses to another pin for easier probing by oscilloscope
set_debug_pin01(firstLogicLevel);
//set_debug_pin01(firstLogicLevel);

init_delay_timer_us(1, delayHigh);
wait_delay_timer_finished();
Expand All @@ -303,7 +300,7 @@ void transmit(const bool invertedSignal, uint16_t delayHigh, uint16_t delayLow)

set_tdata(secondLogicLevel);
// DEBUG:
set_debug_pin01(secondLogicLevel);
//set_debug_pin01(secondLogicLevel);

init_delay_timer_us(1, delayLow);
wait_delay_timer_finished();
Expand All @@ -322,29 +319,19 @@ void transmit(const bool invertedSignal, uint16_t delayHigh, uint16_t delayLow)
* RfRaw AAA80401D0035855
*/
//void sendByProtocol(const int nProtocol, const unsigned int length)
void send(struct Pulse* pro, unsigned char* packetStart, const unsigned char bitsInPacket)
void send(struct Pulse* pulses, unsigned char* packetStart, const unsigned char bitsInPacket)
{
// FIXME: it might just be easier to make this global
// and possibly share with receive protocol, if they are never used at the same time
//struct Protocol pro;

//int nRepeat;
//int index;

// this allows us to send an abitrary amount of bits from a byte array
uint8_t bitIndex;
uint8_t currentBit;
uint8_t currentByte;

// track packet repeat count
uint8_t nRepeat;

// pointer to byte intended to be sent currently
unsigned char* packetPtr;

// also checks for out of bound index (e.g., less than one)
//setProtocol(nProtocol);

// FIXME: consider checking index out of bound
//memcpy(&pro, &protocols[nProtocol-1], sizeof(struct Protocol));



// must repeat sent packet so that receiver interprets it as valid
Expand All @@ -358,8 +345,13 @@ void send(struct Pulse* pro, unsigned char* packetStart, const unsigned char bit
// make a copy of current byte in order to shift that copy
currentByte = *packetPtr;

// moved sync pulse sending here to match manchester encoding style shown in application notes
transmit(pulses->invertedSignal, pulses->syncHigh, pulses->syncLow);

// we must send repeat transmission for decoder to accept radio packet
for (bitIndex = 0; bitIndex < bitsInPacket; bitIndex++)
{

if (currentBit == 8)
{
// FIXME:
Expand All @@ -375,11 +367,11 @@ void send(struct Pulse* pro, unsigned char* packetStart, const unsigned char bit
// mask out all but left most bit value, and if byte is not equal to zero (i.e. left most bit must be one) then send one level
if ((currentByte & 0x80) == 0x80)
{
transmit(pro->invertedSignal, pro->oneHigh, pro->oneLow);
transmit(pulses->invertedSignal, pulses->oneHigh, pulses->oneLow);
}
else
{
transmit(pro->invertedSignal, pro->zeroHigh, pro->zeroLow);
transmit(pulses->invertedSignal, pulses->zeroHigh, pulses->zeroLow);
}

//
Expand All @@ -389,12 +381,14 @@ void send(struct Pulse* pro, unsigned char* packetStart, const unsigned char bit
currentBit++;
}

transmit(pro->invertedSignal, pro->syncHigh, pro->syncLow);
// FIXME: sync is actually supposed to be transmitted before data
// even if rcswitch ignores the first sync pulse and just looks for gaps (the sync) between repeat transmissions
//transmit(pulses->invertedSignal, pulses->syncHigh, pulses->syncLow);
}

// disable transmit after sending (i.e., for inverted protocols)
tdata_off();

// enable receiver again if we just disabled it
// we do this outside of the function
//radio_receiver_on();
}
Loading

0 comments on commit 0d88cad

Please sign in to comment.