Overoで動作するデータ収録プログラム
サブCPUからGPS時刻付きADデータを取得してSDに記録する。
Revisão | 542dc2688dc0b3810b65993a2a1ecc1b2ada784f (tree) |
---|---|
Hora | 2011-12-17 01:00:44 |
Autor | Naoya Takamura <ntaka206@user...> |
Commiter | Naoya Takamura |
check sum calc add
conf.c:
gain関係set/get add
main.c:
PICへのgain設定送信コード追加
起動時にGain設定追加
thread_disp.c:
@@ -30,7 +30,7 @@ INCDIR = ${STAGEDIR}/include | ||
30 | 30 | |
31 | 31 | TARGET = scilog |
32 | 32 | |
33 | -OBJS = scilog.o spi.o thread_rcv.o ring.o ad_ring.o thread_rec.o sts.o ad_file.o conf.o lcd.o | |
33 | +OBJS = scilog.o spi.o thread_rcv.o ring.o ad_ring.o thread_rec.o sts.o ad_file.o conf.o lcd.o thread_disp.o | |
34 | 34 | |
35 | 35 | HDRS = mes_print.h debug_print.h spi.h my_thread.h ring.h ad_ring.h thread_rec.h sts.h ad_file.h conf.h lcd.h |
36 | 36 |
@@ -30,6 +30,29 @@ pthread_mutex_lock(&mutex_conf); | ||
30 | 30 | pthread_mutex_unlock(&mutex_conf); |
31 | 31 | return f; |
32 | 32 | } |
33 | +// | |
34 | +/**** Gain *************************************************** | |
35 | +*/ | |
36 | +/* | |
37 | + 0=1/8, 1=1/4, 2=1/2, 3=1, 4=2, 5=4 | |
38 | + 6=8, 7=16, 8=32, 9=64, 10=128 | |
39 | +*/ | |
40 | +static int gain; | |
41 | + | |
42 | +void conf_gain_set(int f) | |
43 | +{ | |
44 | +pthread_mutex_lock(&mutex_conf); | |
45 | + gain = f; | |
46 | +pthread_mutex_unlock(&mutex_conf); | |
47 | +} | |
48 | +int conf_gain_get(void) | |
49 | +{ | |
50 | + int f; | |
51 | +pthread_mutex_lock(&mutex_conf); | |
52 | + f = gain; | |
53 | +pthread_mutex_unlock(&mutex_conf); | |
54 | + return f; | |
55 | +} | |
33 | 56 | |
34 | 57 | // |
35 | 58 | /**** 設定ファイル ******************************************* |
@@ -143,6 +166,11 @@ int conf_read(void) | ||
143 | 166 | conf_freq_set(f); |
144 | 167 | syslog(LOG_INFO, "freq=%d", conf_freq_get()); |
145 | 168 | } |
169 | + // Gain | |
170 | + if (sscanf(buf, "gain = %d", &f) == 1) { | |
171 | + conf_gain_set(f); | |
172 | + syslog(LOG_INFO, "gain=%d", conf_gain_get()); | |
173 | + } | |
146 | 174 | } |
147 | 175 | fclose(fp); |
148 | 176 | return 0; |
@@ -6,6 +6,10 @@ | ||
6 | 6 | |
7 | 7 | void conf_freq_set(int f); |
8 | 8 | int conf_freq_get(void); |
9 | + | |
10 | +void conf_gain_set(int f); | |
11 | +int conf_gain_get(void); | |
12 | + | |
9 | 13 | int conf_read(void); |
10 | 14 | |
11 | 15 | #endif |
@@ -2,6 +2,7 @@ | ||
2 | 2 | #define __MY_THREAD_H__ |
3 | 3 | |
4 | 4 | void* thread_rcv(void* pParam); |
5 | +void* thread_disp(void* pParam); | |
5 | 6 | |
6 | 7 | #ifdef CUNIT |
7 | 8 | #include <CUnit/CUnit.h> |
@@ -46,7 +46,7 @@ | ||
46 | 46 | /**** 設定デフォルト値 |
47 | 47 | */ |
48 | 48 | #define CONF_FREQ_DEF 50 |
49 | - | |
49 | +#define CONF_GAIN_DEF SPI_CMD_GAIN_1P4 | |
50 | 50 | |
51 | 51 | // |
52 | 52 | /**** signal *********************************************************** |
@@ -76,7 +76,7 @@ void sig_handler(int sig) | ||
76 | 76 | */ |
77 | 77 | int main (int argc, char *argv[]) |
78 | 78 | { |
79 | - pthread_t tid_rcv; | |
79 | + pthread_t tid_rcv, tid_disp; | |
80 | 80 | // char buf[512]; |
81 | 81 | |
82 | 82 | signal(SIGINT, sig_handler); |
@@ -98,9 +98,11 @@ int main (int argc, char *argv[]) | ||
98 | 98 | |
99 | 99 | // デフォルト設定 |
100 | 100 | conf_freq_set(CONF_FREQ_DEF); |
101 | + conf_gain_set(CONF_GAIN_DEF); | |
101 | 102 | // 設定ファイル読み込み |
102 | 103 | conf_read(); |
103 | 104 | PDEBUG("freq=%d\n", conf_freq_get()); |
105 | + PDEBUG("gain=%d\n", conf_gain_get()); | |
104 | 106 | lcd_print("*"); |
105 | 107 | |
106 | 108 | PDEBUG("sciLogger %s START\n", VERSION); |
@@ -115,20 +117,34 @@ int main (int argc, char *argv[]) | ||
115 | 117 | lcd_print("*"); |
116 | 118 | |
117 | 119 | // |
120 | +/**** PGA設定 | |
121 | +*/ | |
122 | + spi_cmd_send_gain(conf_gain_get()); | |
123 | + lcd_print("*"); | |
124 | +// | |
118 | 125 | /**** スレッド生成 |
119 | 126 | */ |
120 | - // SPI RCV | |
127 | + // SPI RCVと平均 | |
121 | 128 | if (pthread_create(&tid_rcv, NULL, thread_rcv, NULL) != 0) { |
122 | 129 | perror("pthread_create(SPI RCV)"); |
123 | - exit(EXIT_FAILURE); | |
130 | + goto END; | |
124 | 131 | } else { |
125 | 132 | PDEBUG("SPI RCV thread create\n"); |
126 | 133 | } |
127 | 134 | lcd_print("*"); |
135 | + // 表示 | |
136 | + if (pthread_create(&tid_disp, NULL, thread_disp, NULL) != 0) { | |
137 | + perror("pthread_create(DISP)"); | |
138 | + goto END; | |
139 | + } else { | |
140 | + PDEBUG("DISP thread create\n"); | |
141 | + } | |
142 | + lcd_print("*"); | |
128 | 143 | |
129 | 144 | // |
130 | 145 | /**** メインループ 記録 ************************ |
131 | 146 | */ |
147 | + // 記録スレッド | |
132 | 148 | thread_rec(NULL); |
133 | 149 | END: |
134 | 150 | sig_handler(0); |
@@ -3,8 +3,10 @@ | ||
3 | 3 | #include <sys/ioctl.h> |
4 | 4 | //#include <termios.h> |
5 | 5 | #include <fcntl.h> |
6 | +#include <string.h> | |
6 | 7 | |
7 | 8 | #include "spi.h" |
9 | +#include "ad_ring.h" | |
8 | 10 | |
9 | 11 | static int fd_spi = -1; |
10 | 12 |
@@ -66,3 +68,34 @@ int spi_rcvd_len_get(void) | ||
66 | 68 | spi_ioctl(CMD_RECEIVED_LEN_GET, &i); |
67 | 69 | return i; |
68 | 70 | } |
71 | +// コマンド送信 | |
72 | +void spi_cmd_send(char cmd_code, char *cmd_data, int data_len) | |
73 | +{ | |
74 | + char cmd[SPI_CMD_LEN]; | |
75 | + | |
76 | + memset(cmd, 0, SPI_CMD_LEN); | |
77 | + | |
78 | + cmd[SPI_CMD_OFS_HEAD] = SPI_HEAD_CHAR; | |
79 | + cmd[SPI_CMD_OFS_CMDCODE] = cmd_code; | |
80 | + | |
81 | + // data | |
82 | + if (data_len > (SPI_CMD_LEN - SPI_CMD_OFS_CMDDATA)) { | |
83 | + data_len = SPI_CMD_LEN - SPI_CMD_OFS_CMDDATA; | |
84 | + } | |
85 | + memcpy(&cmd[SPI_CMD_OFS_CMDDATA], cmd_data, data_len); | |
86 | + | |
87 | + spi_tx_set(cmd); | |
88 | +} | |
89 | +// Gainコマンド送信 | |
90 | +void spi_cmd_send_gain(int gain) | |
91 | +{ | |
92 | + char cmd[SPI_CMD_LEN]; | |
93 | + int i; | |
94 | + | |
95 | + memset(cmd, 0, SPI_CMD_LEN); | |
96 | + | |
97 | + for(i = 0; i < AD_CHNUM; i++) | |
98 | + cmd[i] = gain; | |
99 | + spi_cmd_send(SPI_CMDCODE_GAIN, cmd, AD_CHNUM); | |
100 | +} | |
101 | + |
@@ -1,9 +1,9 @@ | ||
1 | 1 | #if !defined(__SPI_H__) |
2 | 2 | #define __SPI_H__ |
3 | 3 | |
4 | +// ioctlコマンド | |
4 | 5 | // SPI受信データ取得 |
5 | 6 | #define CMD_RX_GET 11 |
6 | - | |
7 | 7 | // SPI送信データ長セット |
8 | 8 | #define CMD_TX_LEN 12 |
9 | 9 | // SPI送信データセット PICへのコマンド |
@@ -16,7 +16,7 @@ | ||
16 | 16 | #define CMD_RECEIVED_LEN_GET 20 |
17 | 17 | |
18 | 18 | // SPIで受信するデータ長 固定長 $含む |
19 | -#define SPI_DATA_LEN 965 | |
19 | +#define SPI_DATA_LEN (965) | |
20 | 20 | // SPI受信データの先頭文字 送信コマンドでも使用 |
21 | 21 | #define SPI_HEAD_CHAR '$' |
22 | 22 |
@@ -26,16 +26,37 @@ | ||
26 | 26 | // SPI受信データ Checksum 長さ |
27 | 27 | #define SPI_CHECKSUM_LEN 2 |
28 | 28 | |
29 | -// SPI送信するコマンド長 固定長 $含む | |
30 | -#define SPI_CMD_LEN 20 | |
31 | - | |
32 | - | |
33 | 29 | // SPI受信データの先頭からのオフセット |
34 | -#define SPI_OFS_GPS (19) | |
30 | +#define SPI_OFS_RESCODE 1 | |
31 | +#define SPI_OFS_RESDATA 2 | |
32 | +#define SPI_OFS_GPS (19) | |
35 | 33 | #define SPI_OFS_1SEC (39) |
36 | 34 | #define SPI_OFS_DATA (63) |
35 | +#define SPI_OFS_SUM 963 | |
37 | 36 | |
37 | +// SPI送信コマンド長 固定長 $含む | |
38 | +#define SPI_CMD_LEN 20 | |
39 | + | |
40 | +// SPI送信コマンド フィールドのオフセット位置 | |
41 | +#define SPI_CMD_OFS_HEAD 0 | |
42 | +#define SPI_CMD_OFS_CMDCODE 1 | |
43 | +#define SPI_CMD_OFS_CMDDATA 2 | |
38 | 44 | |
45 | +// SPIコマンドコード | |
46 | +#define SPI_CMDCODE_GAIN 1 | |
47 | + | |
48 | +// SPIコマンドGainの設定値 | |
49 | +#define SPI_CMD_GAIN_1P8 0 // 1/8 | |
50 | +#define SPI_CMD_GAIN_1P4 1 // 1/4 | |
51 | +#define SPI_CMD_GAIN_1P2 2 // 1/2 | |
52 | +#define SPI_CMD_GAIN_1 3 | |
53 | +#define SPI_CMD_GAIN_2 4 | |
54 | +#define SPI_CMD_GAIN_4 5 | |
55 | +#define SPI_CMD_GAIN_8 6 | |
56 | +#define SPI_CMD_GAIN_16 7 | |
57 | +#define SPI_CMD_GAIN_32 8 | |
58 | +#define SPI_CMD_GAIN_64 9 | |
59 | +#define SPI_CMD_GAIN_128 10 | |
39 | 60 | |
40 | 61 | |
41 | 62 | int spi_get_fd(void); |
@@ -50,4 +71,7 @@ void spi_buf_clear(void); | ||
50 | 71 | int spi_dnum_get(void); |
51 | 72 | int spi_rcvd_len_get(void); |
52 | 73 | |
74 | +void spi_cmd_send(char cmd_code, char *cmd_data, int data_len); | |
75 | +void spi_cmd_send_gain(int gain); | |
76 | + | |
53 | 77 | #endif |
@@ -0,0 +1,94 @@ | ||
1 | +#include <stdio.h> | |
2 | + | |
3 | +#include "ad_ring.h" | |
4 | +#include "lcd.h" | |
5 | + | |
6 | +static void scr_init(void) | |
7 | +{ | |
8 | + int iLeft = 0; | |
9 | + int iY = 0; | |
10 | + | |
11 | + lcd_clear(); | |
12 | + | |
13 | + lcd_pos(iLeft, iY); | |
14 | + lcd_print("TIME:"); | |
15 | + iY++; | |
16 | +// lcd_pos(iLeft, iY); | |
17 | +// lcd_print("STS:"); | |
18 | +// iY++; | |
19 | + lcd_pos(iLeft, iY); | |
20 | + lcd_print("HX:"); | |
21 | + iY++; | |
22 | + lcd_pos(iLeft, iY); | |
23 | + lcd_print("HY:"); | |
24 | + iY++; | |
25 | + lcd_pos(iLeft, iY); | |
26 | + lcd_print("HZ:"); | |
27 | + iY++; | |
28 | +} | |
29 | + | |
30 | +/* | |
31 | + | |
32 | +*/ | |
33 | +static void scr_disp_time(AdData *d) | |
34 | +{ | |
35 | + int iLeft, iY; | |
36 | + char buf[128]; | |
37 | + struct tm *t; | |
38 | + | |
39 | + t = &(d->t); | |
40 | + iLeft = 0; | |
41 | + iY = 0; | |
42 | + /* Time */ | |
43 | + lcd_pos(iLeft, iY++); | |
44 | + sprintf(buf, "%02d/%02d/%02d %02d:%02d:%02d %02X", | |
45 | + t->tm_year % 100, t->tm_mon+1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, | |
46 | + d->gps.valid); | |
47 | + lcd_print(buf); | |
48 | +} | |
49 | +/* | |
50 | + | |
51 | +*/ | |
52 | +static void scr_disp_ad(AdData *d) | |
53 | +{ | |
54 | + int iLeft, iY; | |
55 | + char buf[128]; | |
56 | + int ch = 0; | |
57 | + | |
58 | + iLeft = 0; | |
59 | + iY = 1; | |
60 | + for(ch = 0; ch < 3; ch++) { | |
61 | + sprintf(buf, "%d% 8ld", ch+1, d->data1sec[ch]); | |
62 | + lcd_pos(iLeft, iY++); | |
63 | + lcd_print(buf); | |
64 | + } | |
65 | + iLeft = 10; | |
66 | + iY = 1; | |
67 | + for(ch = 3; ch < 6; ch++) { | |
68 | + sprintf(buf, "%d% 8ld", ch+1, d->data1sec[ch]); | |
69 | + lcd_pos(iLeft, iY++); | |
70 | + lcd_print(buf); | |
71 | + } | |
72 | +} | |
73 | + | |
74 | +void* thread_disp(void* pParam) | |
75 | +{ | |
76 | + int i; | |
77 | + AdData *d; | |
78 | + | |
79 | + i = ad_ring_latest_get(); | |
80 | +// scr_init(); | |
81 | + lcd_clear(); | |
82 | + while(1) { | |
83 | + // FG表示 | |
84 | + if (i != ad_ring_latest_get()) { | |
85 | + i = ad_ring_latest_get(); | |
86 | + // データ取得 | |
87 | + d = ad_ring_get(i); | |
88 | + // 時刻表示 | |
89 | + scr_disp_time(d); | |
90 | + // AD表示 | |
91 | + scr_disp_ad(d); | |
92 | + } | |
93 | + } | |
94 | +} |
@@ -70,7 +70,7 @@ static int decode(char *buf, AdData *ad) | ||
70 | 70 | } |
71 | 71 | } |
72 | 72 | // Checksum |
73 | - memcpy(&(ad->checksum), ptr, SPI_CHECKSUM_LEN); | |
73 | + ad->checksum = *((u_int16_t*)ptr); | |
74 | 74 | |
75 | 75 | // gps->struct tm |
76 | 76 | ad->t.tm_year = gps->year - 1900; |
@@ -105,6 +105,21 @@ static void do_avg(int freq, AdData *ad) | ||
105 | 105 | } |
106 | 106 | } |
107 | 107 | } |
108 | +static unsigned int sum_calc(char *buf) | |
109 | +{ | |
110 | + int i; | |
111 | + u_int8_t suma = 0; | |
112 | + u_int8_t sumb = 0; | |
113 | + u_int16_t uint_sum; | |
114 | + | |
115 | + for(i = SPI_OFS_RESCODE; i < SPI_OFS_SUM; i++) { | |
116 | + suma += (u_int8_t)buf[i]; | |
117 | + sumb += suma; | |
118 | + } | |
119 | + uint_sum = ((sumb << 8) & 0xFF00U) | suma; | |
120 | + return uint_sum; | |
121 | +} | |
122 | + | |
108 | 123 | void* thread_rcv(void* pParam) |
109 | 124 | { |
110 | 125 |
@@ -113,6 +128,7 @@ void* thread_rcv(void* pParam) | ||
113 | 128 | int i; |
114 | 129 | int fd_spi; |
115 | 130 | AdData ad, *ad_ptr; |
131 | + unsigned int sum; | |
116 | 132 | #if 0 |
117 | 133 | char cmd[SPI_CMD_LEN]; |
118 | 134 | char c; |
@@ -135,12 +151,12 @@ c=0; | ||
135 | 151 | if(FD_ISSET(fd_spi, &fds)) { |
136 | 152 | #if 0 |
137 | 153 | // SPI送信データセット |
138 | -memset(cmd, 0, SPI_CMD_LEN); | |
139 | -cmd[0] = SPI_HEAD_CHAR; | |
140 | -cmd[1] = 1; | |
141 | -cmd[2] = 2; | |
142 | -cmd[3] = c++; | |
143 | -spi_tx_set(cmd); | |
154 | +if (c++ % 5 == 0) { | |
155 | + memset(cmd, 0, SPI_CMD_LEN); | |
156 | + for(i = 0; i < AD_CHNUM; i++) | |
157 | + cmd[i] = SPI_CMD_GAIN_128; | |
158 | + spi_cmd_send(SPI_CMDCODE_GAIN, cmd, AD_CHNUM); | |
159 | +} | |
144 | 160 | #endif |
145 | 161 | // 受信した |
146 | 162 | i = spi_dnum_get(); |
@@ -151,14 +167,19 @@ spi_tx_set(cmd); | ||
151 | 167 | spi_rx_get(buf); |
152 | 168 | // デコード |
153 | 169 | decode(buf, &ad); |
170 | + // chekcsum check | |
171 | + sum = sum_calc(buf); | |
172 | + if (sum != ad.checksum) { | |
173 | + PDEBUG("thread_rcv(): SUM ERR! CALC=%04X RCV=%04X\r\n", sum, ad.checksum); | |
174 | + } | |
175 | + // 平均 | |
154 | 176 | ad.freq = conf_freq_get(); |
155 | - // 平均 仮コード 要変更 | |
156 | 177 | do_avg(AD_SAMPLE, &ad); |
157 | 178 | #if 1 |
158 | 179 | int ch; |
159 | -PDEBUG("%04d/%02d/%02d %02d:%02d:%02d.%09ld,%6lums,%6luns,%02X", | |
180 | +PDEBUG("%04d/%02d/%02d %02d:%02d:%02d.%09ld,%6lums,%6luns,%02X,%d", | |
160 | 181 | ad.gps.year, ad.gps.month, ad.gps.day, ad.gps.hour, ad.gps.min, ad.gps.sec, ad.gps.nano, |
161 | - ad.gps.tow, ad.gps.tacc, ad.gps.valid); | |
182 | + ad.gps.tow, ad.gps.tacc, ad.gps.valid, ad.rescode); | |
162 | 183 | for(ch = 0; ch < AD_CHNUM; ch++) { |
163 | 184 | PDEBUG(",%+7ld", ad.data1sec[ch]); |
164 | 185 | } |