Skip to content

Commit

Permalink
Watchdog timer (#60)
Browse files Browse the repository at this point in the history
* bump version

* fix tests

* Feed wdt during send repeats, and during delays

* Add 5s timeout for flushing responses

* Make mock clock run a little slower, to allow tests to pass
  • Loading branch information
ps2 authored Jun 26, 2018
1 parent 02d9af9 commit de25f35
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 17 deletions.
8 changes: 8 additions & 0 deletions hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,12 @@ void mode_registers_enact(mode_registers const *mode);
extern mode_registers __xdata tx_registers;
extern mode_registers __xdata rx_registers;

#define WDCLP1 0xA0 // Clear pattern 1
#define WDCLP2 0x50 // Clear pattern 2

inline void feed_watchdog() {
WDCTL = WDCLP1 | WDCTL_EN;
WDCTL = WDCLP2 | WDCTL_EN;
}

#endif
25 changes: 19 additions & 6 deletions mock_hardware.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
#include <time.h>
#include "hardware.h"


volatile uint8_t P0_0;
volatile uint8_t P0_1;
volatile uint8_t P0DIR;
Expand Down Expand Up @@ -79,21 +81,32 @@ volatile uint8_t U1DBUF_read;

bool mock_hardware_should_exit;

// This is 10x normal clock speed
#define MOCK_CLOCK_TICK_RATE 0.0001

void *run_mock_hardware(void *vargp) {
clock_t start_time;

// Mark oscillator as powered up and stable
SLEEP |= SLEEP_XOSC_S;

printf("starting mock hardware thread\n");

start_time = clock();

while(!mock_hardware_should_exit) {
// Run counter
if (T1CNTL == 255) {
T1CNTH += 1;

double elapsed = ((double)(clock() - start_time))/CLOCKS_PER_SEC;

if (elapsed > MOCK_CLOCK_TICK_RATE) {
// Run counter
if (T1CNTL == 255) {
T1CNTH += 1;
}
T1CNTL += 1;
t1_isr();
start_time = clock();
}
T1CNTL += 1;
t1_isr();
//printf("SLEEP = %d\n", SLEEP);

// Watch radio strobe registers
if(RFST == RFST_SIDLE) {
Expand Down
4 changes: 4 additions & 0 deletions mock_hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#include <stdint.h>
#include <stdbool.h>

#ifdef __GNUC__
#define inline static inline
#endif

#define __reentrant
#define __xdata
#define __interrupt
Expand Down
4 changes: 4 additions & 0 deletions radio.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ void send_packet_from_serial(uint8_t channel, uint8_t repeat_count, uint16_t del
if (send_count > 0 && delay_ms > 0) {
delay(delay_ms);
}
feed_watchdog();

send_from_tx_buf(channel, preamble_extend_ms);

Expand Down Expand Up @@ -321,6 +322,9 @@ uint8_t get_packet_and_write_to_serial(uint8_t channel, uint32_t timeout_ms, uin
while(MARCSTATE!=MARC_STATE_RX);

while(1) {

feed_watchdog();

// Waiting for isr to put radio bytes into rx_fifo
if (!fifo_empty(&rx_fifo)) {

Expand Down
27 changes: 24 additions & 3 deletions spi1_alt2/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
#include "radio.h"
#include "statistics.h"
#include "fifo.h"
#include "timer.h"

#define SPI_BUF_LEN 128
#define FLUSH_TIMEOUT_MS 5000

static fifo_buffer __xdata input_buffer;
static volatile uint8_t __xdata input_buffer_mem[SPI_BUF_LEN];
Expand Down Expand Up @@ -181,7 +183,9 @@ uint8_t serial_rx_byte()
{
uint8_t s_data;
if (!serial_data_available) {
while(!serial_data_available && !subg_rfspy_should_exit);
while(!serial_data_available && !subg_rfspy_should_exit) {
feed_watchdog();
}
}
s_data = fifo_get(&input_buffer);
if (fifo_empty(&input_buffer)) {
Expand Down Expand Up @@ -221,12 +225,29 @@ void serial_tx_long(uint32_t tx_long)

void serial_flush()
{
uint32_t start_time;

if (fifo_empty(&output_buffer)) {
return;
}

// Waiting for tx isr to ask for data
read_timer(&start_time);
ready_to_send = 1;
while(!fifo_empty(&output_buffer) && !subg_rfspy_should_exit);
while(slave_send_size != 0 && !subg_rfspy_should_exit);
while(!fifo_empty(&output_buffer) && !subg_rfspy_should_exit) {
feed_watchdog();
if (check_elapsed(start_time, FLUSH_TIMEOUT_MS)) {
break;
}
}

// Waiting to finish spi transfer
while(slave_send_size != 0 && !subg_rfspy_should_exit) {
feed_watchdog();
if (check_elapsed(start_time, FLUSH_TIMEOUT_MS)) {
break;
}
}
ready_to_send = 0;
}

Expand Down
3 changes: 3 additions & 0 deletions subg_rfspy.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ void subg_rfspy_main() {

subg_rfspy_init_finished = true;

// Start watchdog at 1s interval
WDCTL = WDCTL_EN;

while(!subg_rfspy_should_exit) {
get_command();
}
Expand Down
6 changes: 4 additions & 2 deletions timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void init_timer() {
uint16_t timer_ticks_per_ms = (SYSTEM_CLOCK_MHZ * 1000) / 128 / 2;

__timerCounter = 0;

T1CTL = 0x00; // disable timer
T1CNTL = 0x00; // Clear counter low

Expand Down Expand Up @@ -51,5 +51,7 @@ void t1_isr(void) __interrupt T1_VECTOR
void delay(uint32_t msec) {
uint32_t start_time;
read_timer(&start_time);
while(!check_elapsed(start_time, msec));
while(!check_elapsed(start_time, msec)) {
feed_watchdog();
}
}
5 changes: 0 additions & 5 deletions timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
#include <stdint.h>
#include <stdbool.h>

#ifdef __GNUC__
#define inline static inline
#endif


volatile extern uint32_t __timerCounter;

void init_timer();
Expand Down
2 changes: 1 addition & 1 deletion version.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef VERSION_H
#define VERSION_H

#define SUBG_RFSPY_VERSION "subg_rfspy 2.1"
#define SUBG_RFSPY_VERSION "subg_rfspy 2.2"

#endif //VERSION_H

0 comments on commit de25f35

Please sign in to comment.