-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.h
144 lines (113 loc) · 3.55 KB
/
main.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* main.h
*
* Main module definition file
*
* Author: Luis Teixeira (creationfactory.co)
* Licence and copyright notice:
*
* Copyright 2020 Luis Teixeira
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, softwar
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Created on 11th June 2020, 14:38
*/
#ifndef MAIN_H
#define MAIN_H
#ifdef __cplusplus
extern "C" {
#endif
#include <pic16f628a.h>
#include "pic.h"
#define LOW_SPEED
#ifdef LOW_SPEED
#define BAUD_RATE 9600
#else
#define BAUD_RATE 115200 // note: 115200 bps is not achievable using the 4 MHz internal oscillator.
#endif
/*
* SPBRG should be calculated as:
*
* SPBRG = (unsigned char) ((_XTAL_FREQ / 16) / BAUD_RATE) - 1;
*
* For example for a 115200 baud rate and a 20 MHz clock frequency
* we have the following:
*
* SPBRG = ((20 000 000 / 16) / 115200) - 1
* SPBRG = 9.850
*
* If we round up to the nearest integer, we have:
*
* SP_BAUD_TIMER=10
*
*/
#ifdef LOW_SPEED
#define SP_BAUD_TIMER 25 // For baud rate = 9600 and 4 MHz internal oscillator
#else
#define SP_BAUD_TIMER 10 // For baud rate = 1125200 and 20 MHz crystal
#endif
/*
* The offset can be calculated as follows:
*
* TMR1_OFFSET = 65535 - (_XTAL_FREQ / (Prescaler * 40))
*
* e.g. using a 20 MHz crystal and 1:8 prescaler:
*
* 0x0BDB = 65535 - ( 20 000 000 / (8 * 40))
*/
#ifdef LOW_SPEED
#define TMR1_OFFSET 0xCF2B // Offset for calibrating the timer (4 MHz internal oscillator).
#else
#define TMR1_OFFSET 0x0BDB // Offset for calibrating the timer (20 MHz crystal)
#endif
// (watchdog) timer related settings:
#define WT_TIMEOUT 6000 // timeout period (in tenths of seconds) for the watchdog to trigger
#define POWER_OFF_DURATION 8000 // ms to keep power off for resetting the target device
#define POWER_OFF_DELAY 3000 // delay (in ms) between the poweroff command and actually powering off the device
#define WDT_PREFIX_LENGTH 15
#define WDT_CMD_LENGTH 16
#define SHUTDOWN_CMD "poweroff -f\r\n"
#define SHUTDOWN_CMD_LEN 13
unsigned int timerCount;
// Prefix for every WDT command (relatively hard to occur in the host
// serial output, other than from the job that resets the watchdog):
const unsigned char *wdtPrefix = "WDT_BhAyycbmEi_";
// Commands accepted by the WDT:
/*
This command is used for resetting the WDT:
*/
const unsigned char resetWdtCmd = '1';
/*
This command is used for disabling the WDT:
*/
const unsigned char disableWdtCmd = '2';
/*
This command is used for enabling the WDT again:
*/
unsigned char enableWdtCmd = '3';
void init(void);
void resetTimer(void);
void enableTimer(void);
void disableTimer(void);
void serialWrite(char* str, char length);
/**
* This function checks if the bytes we are receiving are part of the string
* we are expecting:
*
**/
void tokenMatchLoop(void);
void isr(void);
#ifdef __cplusplus
}
#endif
#endif /* MAIN_H */