-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.cpp
278 lines (229 loc) · 5.55 KB
/
main.cpp
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//=====================================================================//
/*! @file
@brief R8C DS3231 RTC サンプル
@author 平松邦仁 ([email protected])
@copyright Copyright (C) 2017 Kunihito Hiramatsu @n
Released under the MIT license @n
https://github.com/hirakuni45/R8C/blob/master/LICENSE
*/
//=====================================================================//
#include "common/renesas.hpp"
#include <cstring>
#include "common/command.hpp"
#include "common/format.hpp"
#include "common/uart_io.hpp"
#include "common/fifo.hpp"
#include "common/trb_io.hpp"
#include "chip/DS3231.hpp"
namespace {
typedef device::trb_io<utils::null_task, uint8_t> timer_b;
timer_b timer_b_;
typedef utils::fifo<uint8_t, 16> buffer;
typedef device::uart_io<device::UART0, buffer, buffer> uart;
uart uart_;
// I2C ポートの定義クラス
// P4_B5: SDA
typedef device::PORT<device::PORT4, device::bitpos::B5> sda_port;
// P1_B7: SCL
typedef device::PORT<device::PORT1, device::bitpos::B7> scl_port;
typedef device::iica_io<sda_port, scl_port> iica;
iica i2c_;
chip::DS3231<iica> rtc_(i2c_);
utils::command<64> command_;
}
extern "C" {
void sci_putch(char ch) {
uart_.putch(ch);
}
char sci_getch(void) {
return uart_.getch();
}
uint16_t sci_length() {
return uart_.length();
}
void sci_puts(const char* str) {
uart_.puts(str);
}
void TIMER_RB_intr(void) {
timer_b_.itask();
}
void UART0_TX_intr(void) {
uart_.isend();
}
void UART0_RX_intr(void) {
uart_.irecv();
}
}
namespace {
time_t get_time_() {
time_t t = 0;
if(!rtc_.get_time(t)) {
sci_puts("Stall RTC read...\n");
}
return t;
}
const char* wday_[] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
const char* mon_[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
void disp_time_(time_t t) {
struct tm *m = localtime(&t);
utils::format("%s %s %d %02d:%02d:%02d %4d\n")
% wday_[m->tm_wday]
% mon_[m->tm_mon]
% static_cast<uint32_t>(m->tm_mday)
% static_cast<uint32_t>(m->tm_hour)
% static_cast<uint32_t>(m->tm_min)
% static_cast<uint32_t>(m->tm_sec)
% static_cast<uint32_t>(m->tm_year + 1900);
}
const char* get_dec_(const char* p, char tmch, int& value) {
int v = 0;
char ch;
while((ch = *p) != 0) {
++p;
if(ch == tmch) {
break;
} else if(ch >= '0' && ch <= '9') {
v *= 10;
v += ch - '0';
} else {
return nullptr;
}
}
value = v;
return p;
}
void set_time_date_()
{
time_t t = get_time_();
if(t == 0) return;
struct tm *m = gmtime(&t);
bool err = false;
if(command_.get_words() == 3) {
char buff[12];
if(command_.get_word(1, sizeof(buff), buff)) {
const char* p = buff;
int vs[3];
uint8_t i;
for(i = 0; i < 3; ++i) {
p = get_dec_(p, '/', vs[i]);
if(p == nullptr) {
break;
}
}
if(p != nullptr && p[0] == 0 && i == 3) {
if(vs[0] >= 1900 && vs[0] < 2100) m->tm_year = vs[0] - 1900;
if(vs[1] >= 1 && vs[1] <= 12) m->tm_mon = vs[1] - 1;
if(vs[2] >= 1 && vs[2] <= 31) m->tm_mday = vs[2];
} else {
err = true;
}
}
if(command_.get_word(2, sizeof(buff), buff)) {
const char* p = buff;
int vs[3];
uint8_t i;
for(i = 0; i < 3; ++i) {
p = get_dec_(p, ':', vs[i]);
if(p == nullptr) {
break;
}
}
if(p != nullptr && p[0] == 0 && (i == 2 || i == 3)) {
if(vs[0] >= 0 && vs[0] < 24) m->tm_hour = vs[0];
if(vs[1] >= 0 && vs[1] < 60) m->tm_min = vs[1];
if(i == 3 && vs[2] >= 0 && vs[2] < 60) m->tm_sec = vs[2];
else m->tm_sec = 0;
} else {
err = true;
}
}
}
if(err) {
sci_puts("Can't analize Time/Date input.\n");
return;
}
time_t tt = mktime(m);
if(!rtc_.set_time(tt)) {
sci_puts("Stall RTC write...\n");
}
}
}
// __attribute__ ((section (".exttext")))
int main(int argc, char *argv[])
{
using namespace device;
// クロック関係レジスタ・プロテクト解除
PRCR.PRC0 = 1;
// 高速オンチップオシレーターへ切り替え(20MHz)
// ※ F_CLK を設定する事(Makefile内)
OCOCR.HOCOE = 1;
utils::delay::micro_second(1); // >=30us(125KHz)
SCKCR.HSCKSEL = 1;
CKSTPR.SCKSEL = 1;
// タイマーB初期化
{
uint8_t ir_level = 2;
timer_b_.start(60, ir_level);
}
// UART の設定 (P1_4: TXD0[out], P1_5: RXD0[in])
// ※シリアルライターでは、RXD 端子は、P1_6 となっているので注意!
{
utils::PORT_MAP(utils::port_map::P14::TXD0);
utils::PORT_MAP(utils::port_map::P15::RXD0);
uint8_t ir_level = 1;
uart_.start(57600, ir_level);
}
// I2C クラスの初期化
{
i2c_.start(iica::speed::fast);
}
// RTC を開始
{
rtc_.start();
}
sci_puts("Start R8C RTC monitor\n");
command_.set_prompt("# ");
// LED シグナル用ポートを出力
PD1.B0 = 1;
uint8_t cnt = 0;
while(1) {
timer_b_.sync();
if(cnt >= 20) {
cnt = 0;
}
if(cnt < 10) P1.B0 = 1;
else P1.B0 = 0;
++cnt;
// コマンド入力と、コマンド解析
if(command_.service()) {
uint8_t cmdn = command_.get_words();
if(cmdn >= 1) {
if(command_.cmp_word(0, "date")) {
if(cmdn == 1) {
time_t t = get_time_();
if(t != 0) {
disp_time_(t);
}
} else {
set_time_date_();
}
} else if(command_.cmp_word(0, "help")) {
sci_puts("date\n");
sci_puts("date yyyy/mm/dd hh:mm[:ss]\n");
} else {
char buff[12];
if(command_.get_word(0, sizeof(buff), buff)) {
sci_puts("Command error: ");
sci_puts(buff);
sci_putch('\n');
}
}
}
}
}
}