• R/O
  • HTTP
  • SSH
  • HTTPS

cpu2010: Commit

サブCPU2010のPICプログラム
ADデータ取得とGPS時刻付けを行いLinux部にデータを出力します。


Commit MetaInfo

Revisãocee8401fcb4cfdbc02b3d2602f78cf12df48e1a2 (tree)
Hora2011-12-17 00:59:43
AutorNaoya Takamura <ntaka206@user...>
CommiterNaoya Takamura

Mensagem de Log

thread_rcv.c: check sum calc add
add test.c for debug code
myint.h: add INTERRUPT_PROTECT()
util.h: new file. move mymemcpy() mymemset()

Mudança Sumário

Diff

--- a/build/default/production/main.o.d
+++ b/build/default/production/main.o.d
@@ -1,2 +1,2 @@
11 build/default/production/main.o: main.c myuart.h myspi.h debug_print.h \
2- ublox.h myad.h delay.h ring.h byte2var.h myint.h mysts.h
2+ ublox.h myad.h delay.h ring.h byte2var.h myint.h mysts.h util.h
--- a/main.c
+++ b/main.c
@@ -11,7 +11,6 @@
1111 #include <stdlib.h>
1212 #include <p24FJ64GA004.h>
1313 #include <libpic30.h>
14-//#include <uart.h>
1514 #include <string.h>
1615 #include "myuart.h"
1716 #include "myspi.h"
@@ -23,6 +22,7 @@
2322 #include "byte2var.h"
2423 #include "myint.h"
2524 #include "mysts.h"
25+#include "util.h"
2626
2727 _CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2 )
2828 //_CONFIG2( FCKSM_CSDCMD & OSCIOFNC_ON & POSCMOD_OFF & FNOSC_FRC)
@@ -33,64 +33,45 @@ _CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2 )
3333 //#define CLOCK_FREQUENCY ( ( CPU_CLOCK * CPU_PLL ) / 4 ) // 動作周波数[ Hz ]
3434 //#define FOSC 4000000L
3535 // UART2 Debug Port
36-#define BAUDRATE2 9600L
36+#define BAUDRATE2 19200L
3737 // UART1 GPS
3838 #define BAUDRATE1 9600L
3939
40+// SPI受信データの先頭文字 送信コマンドでも使用
41+#define SPI_HEAD_CHAR '$'
42+
4043 /*** ADバッファ *************************************************
4144 * 50Hzデータ
4245 * 1秒平均データ
4346 * タイムスタンプ
4447 */
4548 #define ADBUF_BYTE 3 // AD 1CH 1sample dataのbyte数
46-#define ADBUF_LEN sizeof(ADbufType) // 1dataの長さ byte $含まず
49+#define ADBUF_LEN (sizeof(ADbufType)+1) // SPI送信1packetの長さ byte $含まず DMA問題のために+1byteしている
50+
4751 #define RESDATA_LEN 17 // コマンドに対する返答データ 最大長さ
4852
53+// 送信データの先頭からのオフセット adbuf内のオフセット $含まない
54+#define ADBUF_OFS_SUM 962 // checksum
55+
4956 // SPI送信にそのまま使われるので注意!
57+// 全体のサイズが2byteの整数倍になるようにすること word align
5058 typedef struct {
5159 unsigned char rescode; // コマンドに対する返答 ACK/NAK/NUL
5260 unsigned char resdata[RESDATA_LEN]; // コマンドに対する返答データ
5361 UbloxNavTimeUtc t; // タイムスタンプ
5462 long data1sec[AD_CHNUM]; // 1sec平均値
5563 unsigned char data[AD_CHNUM][ADBUF_BYTE*AD_SAMPLE]; // AD_SAMPLE[Hz]のデータ
56- int cheksum;
64+ unsigned char suma;
65+ unsigned char sumb;
5766 } ADbufType;
5867 //} __attribute__((__packed__)) ADbufType;
5968 static ADbufType adbuf[RING_NUM];
6069
61-// SPI外部送信用バッファ AD_SAMPLE[Hz]
62-//static char adbuf[RING_NUM][AD_CHNUM][ADBUF_BYTE*AD_SAMPLE];
63-// 1sec平均値
64-//static long adbuf_1sec[RING_NUM][AD_CHNUM];
65-// timestamp
66-//static UbloxNavTimeUtc adbuf_t[RING_NUM];
67-
6870 #define adbuf_get(buf_no) (&adbuf[buf_no])
6971 #define adbuf_data_get(buf_no) (adbuf[buf_no].data)
7072 #define adbuf_1sec_get(buf_no) (adbuf[buf_no].data1sec)
7173 #define adbuf_time_get(buf_no) (&(adbuf[buf_no].t))
7274
73-inline void my_memcpy(void *dest0, void *src0, int len)
74-{
75- int i;
76- char *dest, *src;
77-
78- dest = (char*)dest0;
79- src = (char*)src0;
80- for(i = 0; i < len; i++) {
81- *dest++ = *src++;
82- }
83-}
84-inline void my_memset(void *dest0, unsigned char data, int len)
85-{
86- int i;
87- char *dest;
88-
89- dest = (char*)dest0;
90- for(i = 0; i < len; i++) {
91- *dest++ = data;
92- }
93-}
9475 inline void adbuf_write(unsigned char buf_no, unsigned char ch, int cnt, long *data)
9576 {
9677 unsigned char *ptr = (unsigned char*)data;
@@ -114,46 +95,82 @@ inline void adbuf_time_write(char buf_no, UbloxNavTimeUtc *t)
11495 // my_memcpy(&adbuf_t[buf_no], t, sizeof(UbloxNavTimeUtc));
11596 my_memcpy(adbuf_time_get(buf_no), t, sizeof(UbloxNavTimeUtc));
11697 }
98+inline void adbuf_sum_write(char buf_no, unsigned char *suma, unsigned char *sumb)
99+{
100+ adbuf[buf_no].suma = *suma;
101+ adbuf[buf_no].sumb = *sumb;
102+}
117103
118-/**** SPI CMD受信バッファ
104+/**** SPI CMD受信バッファ ********************************************
119105 */
120106 #define SPI_RX_LEN 20 // Overoから受信するコマンド長 $含む
121107 static char spi_rx_buf[SPI_RX_LEN];
122-static char spi_rx_cnt; // 受信文字数カウント
108+static char spi_rx_cnt; // 受信文字数カウント 0〜SPI_RX_LEN
109+
110+// 受信コマンド内のオフセット位置
111+#define CMD_OFS_CMDCODE 1
112+#define CMD_OFS_CMDDATA 2
113+
114+// コマンドコード
115+#define CMD_CODE_GAIN 1
116+
117+// 返答コード
118+#define RES_CODE_ACK 1
119+#define RES_CODE_NAK 2
120+#define RES_CODE_NUL 0
123121
122+// 状態
124123 #define CMD_STS_NONE 0 // 有効なコマンド無し
125124 #define CMD_STS_RCVD 1 // 有効なコマンド受け取った
126125 static char spi_cmd_sts; // コマンド受信状態
127126
128-/**** ublox ************************************************
129- */
130-static UbloxNavTimeUtc gpsNow;
131-#if 0
132-void test_ublox_rcv_mon(void)
127+#define spi_cmd_sts_set(i) INTERRUPT_PROTECT(spi_cmd_sts = i)
128+#define spi_cmd_sts_get() spi_cmd_sts
129+
130+
131+// ACK送信
132+void spi_res_send(unsigned char res_code, unsigned char *res_data, int data_len)
133133 {
134- unsigned char class, id;
135- unsigned int len;
136- unsigned char payload[128];
137- char sz[128];
138- UbloxNavTimeUtc *gps, gps1;
134+ ADbufType *ad;
135+
136+ INTERRUPT_PROTECT(
137+ ad = adbuf_get(ring_read1_get());
138+ ad->rescode = res_code;
139+ if (res_data == NULL || data_len <= 0) {
140+ my_memset(ad->resdata, 0, RESDATA_LEN);
141+ } else {
142+ my_memcpy(ad->resdata, res_data, data_len);
143+ }
144+ );
145+}
146+#define spi_res_send_ack() spi_res_send(RES_CODE_ACK, NULL, 0)
147+#define spi_res_send_nak() spi_res_send(RES_CODE_NAK, NULL, 0)
139148
140- gps = &gps1;
141-
142- while(1) {
143- ublox_rcv_pkt(&class, &id, &len, payload);
144- sprintf(sz, "CLASS=%02X ID=%02X LEN=%d\r\n", class, id, len);
145- PDEBUG(sz);
146- ublox_debug_pkt_out(payload, len);
147- ublox_decode_nav_timeutc(payload, gps);
148- sprintf(sz, "%04u/%02d/%02d %02d:%02d:%02d.%09ld %6lums %6luns %02X\r\n",
149- gps->year, gps->month, gps->day, gps->hour, gps->min, gps->sec, gps->nano,
150- gps->tow, gps->tacc, gps->valid
151- );
152- PDEBUG(sz);
153149
150+// コマンドに対応する処理
151+void spi_cmd_proc(void)
152+{
153+ int i;
154+
155+ switch(spi_rx_buf[CMD_OFS_CMDCODE]) {
156+ case CMD_CODE_GAIN:
157+ for(i = 0; i < AD_CHNUM; i++) {
158+ pga_gain_set(i, spi_rx_buf[CMD_OFS_CMDDATA + i]);
159+ }
160+ // ACK送信
161+ spi_res_send_ack();
162+ break;
163+ default:
164+ PDEBUG("spi_cmd_porc(): unknown CMD CODE\r\n");
165+ // NACK送信
166+ spi_res_send_nak();
167+ break;
154168 }
155169 }
156-#endif
170+
171+/**** ublox ************************************************
172+ */
173+static UbloxNavTimeUtc gpsNow;
157174 static void ublox_rcv(void)
158175 {
159176 unsigned char class, id;
@@ -214,130 +231,6 @@ static char *dout_ptr;
214231 /**** DRDY OUT ****************************************/
215232 #define DRDY_OUT PORTCbits.RC9
216233
217-/**** TEST *******************************************/
218-/*
219- * SPI FLAG test
220- */
221-void test_spi_flag(void)
222-{
223- char sz[128];
224- int in;
225- unsigned char txc = 0;
226- unsigned char rxc;
227-
228-// spi2_int_disable();
229-// cnint_int_disable();
230- while(1) {
231- delay_ms(1000);
232- sprintf(sz, "SPI2STAT=%04X "
233- "SPIBEC=%d SRMPT=%d SPIROV=%d SRXMPT=%d TBF=%d RBF=%d\r\n"
234- , SPI2STAT
235- , SPI2STATbits.SPIBEC
236- , SPI2STATbits.SRMPT
237- , SPI2STATbits.SPIROV
238- , SPI2STATbits.SRXMPT
239- , SPI2STATbits.SPITBF
240- , SPI2STATbits.SPIRBF
241- );
242- PDEBUG(sz);
243- if ((in = uart2_rcvbuf_getc()) >= 0) {
244- uart2_myputc(in);
245- uart2_puts("\r\n");
246-
247- switch(in) {
248- case 'r':
249- rxc = spi2_getc();
250- sprintf(sz, "RX=%02X\r\n", rxc);
251- uart2_puts(sz);
252- break;
253- case 't':
254- spi2_putc(txc++);
255- sprintf(sz, "TX=%02X\r\n", txc);
256- uart2_puts(sz);
257- break;
258- case 'v':
259- spi2_rx_overrun_clear();
260- break;
261- case 'x':
262- SPI2STATbits.SPIEN = 0;
263- asm("nop");
264- SPI2STATbits.SPIEN = 1;
265- SPI2BUF;
266- break;
267- case 'y':
268- SPI2STATbits.SRXMPT = 1;
269- break;
270- }
271- }
272- }
273-}
274-/*
275- * polling send/rcv
276- */
277-void test_spi_send(void)
278-{
279- unsigned char txc = 1;
280- unsigned char rxc;
281- char sz[128];
282-
283- spi2_int_disable();
284- cnint_int_disable();
285-// SPI2BUF = txc++;
286-// rxc = SPI2BUF;
287- while(1) {
288- while(SPI2STATbits.SPITBF == 0) {
289- // TX
290- SPI2BUF = txc++;
291- }
292- if (SPI2STATbits.SRXMPT == 0) {
293- while(SPI2STATbits.SRXMPT == 0) {
294- // RX FIFO読み出し
295- rxc = SPI2BUF;
296- }
297-#if 0
298- sprintf(sz, "SPI2STAT=%04X "
299- "SPIBEC=%d SRMPT=%d SPIROV=%d SRXMPT=%d TBF=%d RBF=%d\r\n"
300- , SPI2STAT
301- , SPI2STATbits.SPIBEC
302- , SPI2STATbits.SRMPT
303- , SPI2STATbits.SPIROV
304- , SPI2STATbits.SRXMPT
305- , SPI2STATbits.SPITBF
306- , SPI2STATbits.SPIRBF
307- );
308- PDEBUG(sz);
309-#endif
310- }
311- }
312-
313-}
314-// ADレジスタgetして表示
315-void test_ad_get_config(void)
316-{
317- int cs = AD_CH1_CS;
318- int i;
319- unsigned char reg[9];
320- char sz[64];
321-
322- ad_cs(cs);
323- ad_spi_send(ADCMD_SDATAC);
324- ad_cs_dis();
325- ad_cs(cs);
326- ad_spi_rcv(ADCMD_RREG | 0); // Register read command
327- ad_spi_rcv(9 - 1); // 9byte write
328-#if 1
329- for(i = 0; i < 9; i++) {
330- reg[i] = ad_spi_rcv(0);
331- }
332-#endif
333- ad_cs_dis();
334-
335- for(i = 0; i < 9; i++) {
336- sprintf(sz, "%d: %02X\r\n", i, reg[i]);
337- PDEBUG(sz);
338- }
339-
340-}
341234
342235 /*
343236 * main()
@@ -354,7 +247,8 @@ int main(int argc, char** argv) {
354247 unsigned int baudrate1 = ( unsigned int )( Baudrate1 + 0.5 );
355248 char sz[128];
356249 UbloxNavTimeUtc *gps = &gpsNow;
357-
250+ ADbufType *ad;
251+
358252 /**** 割り込み禁止 ****/
359253 SET_CPU_IPL( 7 );
360254 __builtin_write_OSCCONL(OSCCON & 0xbf); //clear the bit 6 of OSCCONL to unlock Pin Re-map
@@ -428,11 +322,11 @@ int main(int argc, char** argv) {
428322 spi2_init();
429323 spi1_int_disable();
430324 spi2_int_disable();
431- spi2_putc('$'); // 一番最初のデータ送信のためにセットしておく
325+ spi2_putc(SPI_HEAD_CHAR); // 一番最初のデータ送信のためにセットしておく
432326
433327 PDEBUG("START\r\n");
434- sprintf(sz, "sizeof(ADBUF)=%d\r\n", ADBUF_LEN);
435- PDEBUG(sz);
328+// sprintf(sz, "sizeof(ADBUF)=%d\r\n", ADBUF_LEN);
329+// PDEBUG(sz);
436330
437331 // GPS UART1 受信割り込み許可
438332 uart1_rx_int_enable();
@@ -495,9 +389,6 @@ int main(int argc, char** argv) {
495389 // ChangeNotification INT許可 SPI2(Overo) CS
496390 cnint_intf_clear();
497391 cnint_int_enable();
498-/****!!!!!!!!!!!!!!!!!!!!!!!!*/
499-// test_spi_flag();
500-//test_spi_send();
501392
502393 while(1) {
503394 // GPSから受信してデコード
@@ -527,13 +418,27 @@ int main(int argc, char** argv) {
527418 }
528419 PDEBUG("\r\n");
529420 #endif
421+#if 0
422+ ad = adbuf_get(ring_read2_get());
423+ sprintf(sz, "%02X %02X\r\n", ad->sumb, ad->suma);
424+ PDEBUG(sz);
425+#endif
530426 ring_read2_plus();
531427 }
532428 // SPIからコマンド受信
533- if (spi_cmd_sts == CMD_STS_RCVD) {
534- sprintf(sz, "CMD RCV: %02X %02X %02X %02X\r\n", spi_rx_buf[0], spi_rx_buf[1], spi_rx_buf[2], spi_rx_buf[3]);
535- PDEBUG(sz);
536- spi_cmd_sts = CMD_STS_NONE;
429+ if (spi_cmd_sts_get() == CMD_STS_RCVD) {
430+#ifdef DEBUG_PRINT
431+ PDEBUG("CMD RCV:");
432+ for(i = 0; i < SPI_RX_LEN; i++) {
433+ sprintf(sz, " %02X", spi_rx_buf[i]);
434+ PDEBUG(sz);
435+ }
436+ PDEBUG("\r\n");
437+#endif
438+ // コマンドに対応する処理
439+ spi_cmd_proc();
440+ // コマンド受信再開
441+ spi_cmd_sts_set(CMD_STS_NONE);
537442 }
538443
539444 }
@@ -614,10 +519,13 @@ void __attribute__((interrupt, auto_psv)) _SPI1Interrupt(void)
614519 static unsigned char in[3];
615520 unsigned char c;
616521 long l;
522+//int i;
523+//unsigned char *ptr;
617524
618525 spi1_intf_clear();
619526 c = spi1_getc();
620527 spi1_rx_overrun_clear();
528+
621529 switch(adint_sts) {
622530 case ADSTS_H:
623531 in[2] = c;
@@ -654,8 +562,18 @@ void __attribute__((interrupt, auto_psv)) _SPI1Interrupt(void)
654562 }
655563 // 1secバッファに書きこみ
656564 adbuf_1sec_write(ring_write_get(), adint_add);
565+#if 0
566+ptr = (unsigned char*)adbuf_get(ring_write_get());
567+ptr += 62;
568+ for(i = 0; i < 900; i++) {
569+ suma += *ptr++;
570+ sumb += suma;
571+ }
572+#endif
657573 // バッファ書き込み位置+1
658574 ring_write_plus();
575+ // 次のADバッファをゼロクリア
576+ my_memset(adbuf_get(ring_write_get()), 0, ADBUF_LEN);
659577 adint_cnt = 0;
660578 // DRDY->L
661579 // TODO: waitカウンタで制限
@@ -679,6 +597,8 @@ void __attribute__((interrupt, auto_psv)) _SPI1Interrupt(void)
679597 }
680598
681599 }
600+static unsigned char suma;
601+static unsigned char sumb;
682602
683603 /*
684604 * DataOut SPI2 Interrupt Handler
@@ -689,20 +609,28 @@ void __attribute__((interrupt, no_auto_psv)) _SPI2Interrupt(void)
689609 unsigned char rx;
690610
691611 spi2_intf_clear();
692- // 送信
612+ // 送信 AD DATA
693613 while(SPI2_TBF == 0) {
614+ // checksum
615+ if ((ADBUF_LEN - dataout_cnt_get()) == ADBUF_OFS_SUM) {
616+ // checksum書き込み
617+ adbuf_sum_write(ring_read1_get(), &suma, &sumb);
618+ }
619+ suma += (unsigned char)*dout_ptr;
620+ sumb += suma;
621+
694622 if (dataout_cnt_get() <= 0) break;
695623 spi2_putc(*dout_ptr++);
696624 dataout_cnt_minus();
697625 }
698- // 受信
626+ // 受信 CMD
699627 while(SPI2_SRXMPT == 0) {
700628 rx = spi2_getc();
701629 if (spi_cmd_sts == CMD_STS_NONE && spi_rx_cnt < SPI_RX_LEN) {
702630 spi_rx_buf[spi_rx_cnt] = rx;
703631 spi_rx_cnt++;
704632 if (spi_rx_cnt >= SPI_RX_LEN) {
705- if (spi_rx_buf[0] != 0) {
633+ if (spi_rx_buf[CMD_OFS_CMDCODE] != 0) {
706634 spi_cmd_sts = CMD_STS_RCVD;
707635 }
708636 }
@@ -732,7 +660,7 @@ void __attribute__((interrupt, no_auto_psv)) _CNInterrupt(void)
732660 asm("nop");
733661 SPI2BUF; // RBF->0
734662 // $->TX FIFO
735- spi2_putc('$');
663+ spi2_putc(SPI_HEAD_CHAR);
736664 // リングバッファ読み込み位置+1
737665 ring_read1_plus();
738666 } else {
@@ -741,8 +669,12 @@ void __attribute__((interrupt, no_auto_psv)) _CNInterrupt(void)
741669 dout_ptr = (char*)adbuf_get(ring_read1_get());
742670 // SPI送信の一番最初
743671 dataout_cnt_set(ADBUF_LEN);
672+ suma = 0;
673+ sumb = 0;
744674 // SPI2 TX FIFOに送信データ書き込み
745675 while(SPI2_TBF == 0) {
676+ suma += (unsigned char)*dout_ptr;
677+ sumb += suma;
746678 if (dataout_cnt_get() <= 0) break;
747679 spi2_putc(*dout_ptr++);
748680 dataout_cnt_minus();
@@ -757,100 +689,3 @@ void __attribute__((interrupt, no_auto_psv)) _CNInterrupt(void)
757689 DRDY_OUT = 1;
758690 }
759691 }
760-#if 0
761-/*
762- * CS0〜15順番にenable
763- */
764-void test_ad_cs()
765-{
766- int i;
767-
768- while(1) {
769- delay_ms(1000);
770- ad_cs(i);
771- i++;
772- if (i >= 16) i = 0;
773- }
774-}
775-
776-/*
777- Dataout SPI2 test
778- */
779-void test_spi2()
780-{
781- unsigned int c = 0x12;
782- unsigned int rx;
783- unsigned int sts = 0;
784- int i;
785-
786- i = SPI2BUF;
787- while(1) {
788- PORTCbits.RC9 = 1;
789- delay_ms(100);
790- PORTCbits.RC9 = 0;
791- delay_ms(900);
792-
793-// while(spi2_tx_fifo_is_full());
794-// spi2_putc(c++);
795-// while(spi2_rx_fifo_is_empty());
796-// rx = spi2_getc();
797-#if 0
798- while(spi2STATbits.SPIRBF == 0);
799- sts = spi2STAT;
800- if (spi2STATbits.SPITBF == 0) {
801- spi2BUF = c++;
802- }
803-// while(spi2STATbits.SPIRBF == 1) {
804- i = spi2BUF;
805-// }
806- if (i == '$') {
807- c = 0;
808- }
809- if (spi2STATbits.SPIROV == 1) spi2STATbits.SPIROV = 0;
810- sprintf(sz, "%d %04X\r\n", i, sts);
811- PDEBUG(sz);
812-#endif
813-// while(spi2STATbits.SPITBF == 1);
814-// while(spi2STATbits.SPITBF);
815-/*
816- while(!spi2_tx_fifo_is_full())
817- spi2_putc(c++);
818- while(!spi2_rx_fifo_is_empty()) {
819- rx = spi2_getc();
820- if (rx == '$') c = 0;
821- }
822-*/
823- // i = ReadUART1();
824-// WriteUART2(i);
825-
826-// PDEBUG(mes);
827-// delay();
828- }
829-}
830-#endif
831-/*
832-void test_ad1()
833-{
834- unsigned char in[3];
835- long l[6];
836- char cs;
837- while(!ad_is_drdy0_enable());
838-// PDEBUG("AD\r\n");
839- for(cs=0; cs<6; cs++) {
840- ad_cs(cs);
841- in[2] = ad_spi_rcv();
842- in[1] = ad_spi_rcv();
843- in[0] = ad_spi_rcv();
844- ad_cs_dis();
845- l[cs] = byte3_to_long(in);
846-// sprintf(sz, "%02X %02X %02X", in[2], in[1], in[0]);
847-// PDEBUG(sz);
848- }
849- for(cs=0; cs<6; cs++) {
850- sprintf(sz, "%+7ld,", l[cs]);
851- PDEBUG(sz);
852- }
853- PDEBUG("\r\n");
854-
855-}
856- */
--- a/myad.c
+++ b/myad.c
@@ -199,3 +199,24 @@ void pga_init(void)
199199 }
200200 }
201201
202+/*
203+ * PGA280 Gain set
204+ * int ch: 0〜AD_CHNUM-1
205+ * int gatin: 0〜10
206+ * 0=1/8, 1=1/4, 2=1/2, 3=1, 4=2, 5=4
207+ * 6=8, 7=16, 8=32, 9=64, 10=128
208+ */
209+void pga_gain_set(int ch, int gain)
210+{
211+ int cs;
212+ int i;
213+ for(i = 0; i < AD_CHNUM; i++) {
214+ cs = PGA_CH1_CS + ch;
215+ // Register 0
216+ // bit6-3: Gain
217+ ad_cs(cs);
218+ ad_spi_send(0x40); //(BUF OFF)
219+ ad_spi_send((gain << 3) & 0x78);
220+ ad_cs_dis();
221+ }
222+}
--- a/myad.h
+++ b/myad.h
@@ -86,5 +86,6 @@ void ad_spi_send(unsigned char c);
8686 unsigned char ad_spi_rcv(unsigned char c);
8787
8888 void pga_init(void);
89+void pga_gain_set(int ch, int gain);
8990
9091 #endif
--- a/myint.h
+++ b/myint.h
@@ -34,5 +34,10 @@
3434 #define cnint_int_enable() IEC1bits.CNIE = 1
3535 #define cnint_int_disable() IEC1bits.CNIE = 0
3636
37+#define INTERRUPT_PROTECT(x) { \
38+ char saved_ipl; \
39+ SET_AND_SAVE_CPU_IPL(saved_ipl,7); \
40+ x; \
41+ RESTORE_CPU_IPL(saved_ipl); } (void) 0;
3742
3843 #endif
--- a/nbproject/Makefile-default.mk
+++ b/nbproject/Makefile-default.mk
@@ -35,10 +35,10 @@ OBJECTDIR=build/${CND_CONF}/${IMAGE_TYPE}
3535 DISTDIR=dist/${CND_CONF}/${IMAGE_TYPE}
3636
3737 # Object Files Quoted if spaced
38-OBJECTFILES_QUOTED_IF_SPACED=${OBJECTDIR}/byte2var.o ${OBJECTDIR}/debug_print.o ${OBJECTDIR}/main.o ${OBJECTDIR}/myad.o ${OBJECTDIR}/myspi.o ${OBJECTDIR}/mysts.o ${OBJECTDIR}/myuart.o ${OBJECTDIR}/ring.o ${OBJECTDIR}/ublox.o
38+OBJECTFILES_QUOTED_IF_SPACED=${OBJECTDIR}/byte2var.o ${OBJECTDIR}/debug_print.o ${OBJECTDIR}/main.o ${OBJECTDIR}/myad.o ${OBJECTDIR}/myspi.o ${OBJECTDIR}/mysts.o ${OBJECTDIR}/myuart.o ${OBJECTDIR}/ring.o ${OBJECTDIR}/test.o ${OBJECTDIR}/ublox.o
3939
4040 # Object Files
41-OBJECTFILES=${OBJECTDIR}/byte2var.o ${OBJECTDIR}/debug_print.o ${OBJECTDIR}/main.o ${OBJECTDIR}/myad.o ${OBJECTDIR}/myspi.o ${OBJECTDIR}/mysts.o ${OBJECTDIR}/myuart.o ${OBJECTDIR}/ring.o ${OBJECTDIR}/ublox.o
41+OBJECTFILES=${OBJECTDIR}/byte2var.o ${OBJECTDIR}/debug_print.o ${OBJECTDIR}/main.o ${OBJECTDIR}/myad.o ${OBJECTDIR}/myspi.o ${OBJECTDIR}/mysts.o ${OBJECTDIR}/myuart.o ${OBJECTDIR}/ring.o ${OBJECTDIR}/test.o ${OBJECTDIR}/ublox.o
4242
4343
4444 CFLAGS=
@@ -89,6 +89,12 @@ endif
8989 # ------------------------------------------------------------------------------------
9090 # Rules for buildStep: compile
9191 ifeq ($(TYPE_IMAGE), DEBUG_RUN)
92+${OBJECTDIR}/test.o: test.c nbproject/Makefile-${CND_CONF}.mk
93+ @${MKDIR} ${OBJECTDIR}
94+ @${RM} ${OBJECTDIR}/test.o.d
95+ @${RM} ${OBJECTDIR}/test.o.ok ${OBJECTDIR}/test.o.err
96+ @${FIXDEPS} "${OBJECTDIR}/test.o.d" $(SILENT) -c ${MP_CC} $(MP_EXTRA_CC_PRE) -g -D__DEBUG -D__MPLAB_DEBUGGER_ICD3=1 -omf=elf -x c -c -mcpu=$(MP_PROCESSOR_OPTION) -mlarge-data -O1 -MMD -MF "${OBJECTDIR}/test.o.d" -o ${OBJECTDIR}/test.o test.c -Winline
97+
9298 ${OBJECTDIR}/byte2var.o: byte2var.c nbproject/Makefile-${CND_CONF}.mk
9399 @${MKDIR} ${OBJECTDIR}
94100 @${RM} ${OBJECTDIR}/byte2var.o.d
@@ -144,6 +150,12 @@ ${OBJECTDIR}/myspi.o: myspi.c nbproject/Makefile-${CND_CONF}.mk
144150 @${FIXDEPS} "${OBJECTDIR}/myspi.o.d" $(SILENT) -c ${MP_CC} $(MP_EXTRA_CC_PRE) -g -D__DEBUG -D__MPLAB_DEBUGGER_ICD3=1 -omf=elf -x c -c -mcpu=$(MP_PROCESSOR_OPTION) -mlarge-data -O1 -MMD -MF "${OBJECTDIR}/myspi.o.d" -o ${OBJECTDIR}/myspi.o myspi.c -Winline
145151
146152 else
153+${OBJECTDIR}/test.o: test.c nbproject/Makefile-${CND_CONF}.mk
154+ @${MKDIR} ${OBJECTDIR}
155+ @${RM} ${OBJECTDIR}/test.o.d
156+ @${RM} ${OBJECTDIR}/test.o.ok ${OBJECTDIR}/test.o.err
157+ @${FIXDEPS} "${OBJECTDIR}/test.o.d" $(SILENT) -c ${MP_CC} $(MP_EXTRA_CC_PRE) -g -omf=elf -x c -c -mcpu=$(MP_PROCESSOR_OPTION) -mlarge-data -O1 -MMD -MF "${OBJECTDIR}/test.o.d" -o ${OBJECTDIR}/test.o test.c -Winline
158+
147159 ${OBJECTDIR}/byte2var.o: byte2var.c nbproject/Makefile-${CND_CONF}.mk
148160 @${MKDIR} ${OBJECTDIR}
149161 @${RM} ${OBJECTDIR}/byte2var.o.d
--- a/nbproject/Makefile-genesis.properties
+++ b/nbproject/Makefile-genesis.properties
@@ -1,5 +1,5 @@
11 #
2-#Thu Dec 15 23:32:46 JST 2011
2+#Fri Dec 16 21:32:52 JST 2011
33 default.languagetoolchain.dir=/opt/microchip/mplabc30/v3.30c/bin
44 com-microchip-mplab-nbide-embedded-makeproject-MakeProject.md5=11bb82e71763925c87fa30f6c65473b1
55 default.languagetoolchain.version=3_30
--- a/nbproject/configurations.xml
+++ b/nbproject/configurations.xml
@@ -37,8 +37,10 @@
3737 <itemPath>myuart.h</itemPath>
3838 <itemPath>ring.c</itemPath>
3939 <itemPath>ring.h</itemPath>
40+ <itemPath>test.c</itemPath>
4041 <itemPath>ublox.c</itemPath>
4142 <itemPath>ublox.h</itemPath>
43+ <itemPath>util.h</itemPath>
4244 </logicalFolder>
4345 <logicalFolder name="ExternalFiles"
4446 displayName="Important Files"
@@ -105,8 +107,8 @@
105107 <property key="list-source" value="false"/>
106108 <property key="list-assembly" value="false"/>
107109 <property key="relax" value="false"/>
108- <property key="warning-level" value="emit-warnings"/>
109110 <property key="extra-include-directories-for-preprocessor" value=""/>
111+ <property key="warning-level" value="emit-warnings"/>
110112 <property key="list-section-info" value="false"/>
111113 <property key="omit-forms" value="false"/>
112114 <property key="list-symbols" value="false"/>
@@ -132,23 +134,23 @@
132134 <property key="enable-handles" value="true"/>
133135 <property key="enable-data-init" value="true"/>
134136 <property key="secure-flash" value="no_flash"/>
135- <property key="generate-cross-reference-file" value="false"/>
136- <property key="report-memory-usage" value="false"/>
137- <property key="boot-flash" value="no_flash"/>
138137 <property key="boot-write-protect" value="no_write_protect"/>
138+ <property key="boot-flash" value="no_flash"/>
139+ <property key="report-memory-usage" value="false"/>
140+ <property key="generate-cross-reference-file" value="false"/>
139141 <property key="input-libraries" value=""/>
140142 <property key="secure-ram" value="no_ram"/>
141143 <property key="enable-check-sections" value="true"/>
142144 <property key="enable-default-isr" value="true"/>
143145 <property key="symbol-stripping" value="keep-all"/>
144146 <property key="boot-ram" value="no_ram"/>
145- <property key="extra-lib-directories" value=""/>
146147 <property key="secure-eeprom" value="no_eeprom"/>
148+ <property key="extra-lib-directories" value=""/>
147149 </C30-LD>
148150 <C30Global>
149151 <property key="fast-math" value="false"/>
150- <property key="legacy-libc" value="false"/>
151152 <property key="output-file-format" value="elf"/>
153+ <property key="legacy-libc" value="false"/>
152154 </C30Global>
153155 <ICD3PlatformTool>
154156 <property key="UART 2" value="true"/>
@@ -186,14 +188,14 @@
186188 <property key="TIMER4" value="true"/>
187189 <property key="TIMER3" value="true"/>
188190 <property key="SPI 2" value="true"/>
189- <property key="SPI 1" value="true"/>
190191 <property key="TIMER2" value="true"/>
192+ <property key="SPI 1" value="true"/>
191193 <property key="TIMER1" value="true"/>
192194 <property key="CRC" value="true"/>
193195 <property key="I2C2" value="true"/>
194196 <property key="I2C1" value="true"/>
195- <property key="programoptions.preserveprogramrange.end" value="0xff"/>
196197 <property key="Freeze All Other Peripherals" value="true"/>
198+ <property key="programoptions.preserveprogramrange.end" value="0xff"/>
197199 </ICD3PlatformTool>
198200 </conf>
199201 </confs>
--- /dev/null
+++ b/test.c
@@ -0,0 +1,264 @@
1+#include <stdio.h>
2+#include <stdlib.h>
3+#include <p24FJ64GA004.h>
4+#include <libpic30.h>
5+#include <string.h>
6+#include "myuart.h"
7+#include "myspi.h"
8+#include "debug_print.h"
9+#include "ublox.h"
10+#include "myad.h"
11+#include "delay.h"
12+#include "ring.h"
13+#include "byte2var.h"
14+#include "myint.h"
15+#include "mysts.h"
16+
17+/**** TEST *******************************************/
18+/*
19+ * SPI FLAG test
20+ */
21+void test_spi_flag(void)
22+{
23+ char sz[128];
24+ int in;
25+ unsigned char txc = 0;
26+ unsigned char rxc;
27+
28+// spi2_int_disable();
29+// cnint_int_disable();
30+ while(1) {
31+ delay_ms(1000);
32+ sprintf(sz, "SPI2STAT=%04X "
33+ "SPIBEC=%d SRMPT=%d SPIROV=%d SRXMPT=%d TBF=%d RBF=%d\r\n"
34+ , SPI2STAT
35+ , SPI2STATbits.SPIBEC
36+ , SPI2STATbits.SRMPT
37+ , SPI2STATbits.SPIROV
38+ , SPI2STATbits.SRXMPT
39+ , SPI2STATbits.SPITBF
40+ , SPI2STATbits.SPIRBF
41+ );
42+ PDEBUG(sz);
43+ if ((in = uart2_rcvbuf_getc()) >= 0) {
44+ uart2_myputc(in);
45+ uart2_puts("\r\n");
46+
47+ switch(in) {
48+ case 'r':
49+ rxc = spi2_getc();
50+ sprintf(sz, "RX=%02X\r\n", rxc);
51+ uart2_puts(sz);
52+ break;
53+ case 't':
54+ spi2_putc(txc++);
55+ sprintf(sz, "TX=%02X\r\n", txc);
56+ uart2_puts(sz);
57+ break;
58+ case 'v':
59+ spi2_rx_overrun_clear();
60+ break;
61+ case 'x':
62+ SPI2STATbits.SPIEN = 0;
63+ asm("nop");
64+ SPI2STATbits.SPIEN = 1;
65+ SPI2BUF;
66+ break;
67+ case 'y':
68+ SPI2STATbits.SRXMPT = 1;
69+ break;
70+ }
71+ }
72+ }
73+}
74+/*
75+ * polling send/rcv
76+ */
77+void test_spi_send(void)
78+{
79+ unsigned char txc = 1;
80+ unsigned char rxc;
81+ char sz[128];
82+
83+ spi2_int_disable();
84+ cnint_int_disable();
85+// SPI2BUF = txc++;
86+// rxc = SPI2BUF;
87+ while(1) {
88+ while(SPI2STATbits.SPITBF == 0) {
89+ // TX
90+ SPI2BUF = txc++;
91+ }
92+ if (SPI2STATbits.SRXMPT == 0) {
93+ while(SPI2STATbits.SRXMPT == 0) {
94+ // RX FIFO読み出し
95+ rxc = SPI2BUF;
96+ }
97+#if 0
98+ sprintf(sz, "SPI2STAT=%04X "
99+ "SPIBEC=%d SRMPT=%d SPIROV=%d SRXMPT=%d TBF=%d RBF=%d\r\n"
100+ , SPI2STAT
101+ , SPI2STATbits.SPIBEC
102+ , SPI2STATbits.SRMPT
103+ , SPI2STATbits.SPIROV
104+ , SPI2STATbits.SRXMPT
105+ , SPI2STATbits.SPITBF
106+ , SPI2STATbits.SPIRBF
107+ );
108+ PDEBUG(sz);
109+#endif
110+ }
111+ }
112+
113+}
114+// ADレジスタgetして表示
115+void test_ad_get_config(void)
116+{
117+ int cs = AD_CH1_CS;
118+ int i;
119+ unsigned char reg[9];
120+ char sz[64];
121+
122+ ad_cs(cs);
123+ ad_spi_send(ADCMD_SDATAC);
124+ ad_cs_dis();
125+ ad_cs(cs);
126+ ad_spi_rcv(ADCMD_RREG | 0); // Register read command
127+ ad_spi_rcv(9 - 1); // 9byte write
128+#if 1
129+ for(i = 0; i < 9; i++) {
130+ reg[i] = ad_spi_rcv(0);
131+ }
132+#endif
133+ ad_cs_dis();
134+
135+ for(i = 0; i < 9; i++) {
136+ sprintf(sz, "%d: %02X\r\n", i, reg[i]);
137+ PDEBUG(sz);
138+ }
139+
140+}
141+#if 0
142+void test_ublox_rcv_mon(void)
143+{
144+ unsigned char class, id;
145+ unsigned int len;
146+ unsigned char payload[128];
147+ char sz[128];
148+ UbloxNavTimeUtc *gps, gps1;
149+
150+ gps = &gps1;
151+
152+ while(1) {
153+ ublox_rcv_pkt(&class, &id, &len, payload);
154+ sprintf(sz, "CLASS=%02X ID=%02X LEN=%d\r\n", class, id, len);
155+ PDEBUG(sz);
156+ ublox_debug_pkt_out(payload, len);
157+ ublox_decode_nav_timeutc(payload, gps);
158+ sprintf(sz, "%04u/%02d/%02d %02d:%02d:%02d.%09ld %6lums %6luns %02X\r\n",
159+ gps->year, gps->month, gps->day, gps->hour, gps->min, gps->sec, gps->nano,
160+ gps->tow, gps->tacc, gps->valid
161+ );
162+ PDEBUG(sz);
163+
164+ }
165+}
166+#endif
167+
168+#if 0
169+/*
170+ * CS0〜15順番にenable
171+ */
172+void test_ad_cs()
173+{
174+ int i;
175+
176+ while(1) {
177+ delay_ms(1000);
178+ ad_cs(i);
179+ i++;
180+ if (i >= 16) i = 0;
181+ }
182+}
183+
184+/*
185+ Dataout SPI2 test
186+ */
187+void test_spi2()
188+{
189+ unsigned int c = 0x12;
190+ unsigned int rx;
191+ unsigned int sts = 0;
192+ int i;
193+
194+ i = SPI2BUF;
195+ while(1) {
196+ PORTCbits.RC9 = 1;
197+ delay_ms(100);
198+ PORTCbits.RC9 = 0;
199+ delay_ms(900);
200+
201+// while(spi2_tx_fifo_is_full());
202+// spi2_putc(c++);
203+// while(spi2_rx_fifo_is_empty());
204+// rx = spi2_getc();
205+#if 0
206+ while(spi2STATbits.SPIRBF == 0);
207+ sts = spi2STAT;
208+ if (spi2STATbits.SPITBF == 0) {
209+ spi2BUF = c++;
210+ }
211+// while(spi2STATbits.SPIRBF == 1) {
212+ i = spi2BUF;
213+// }
214+ if (i == '$') {
215+ c = 0;
216+ }
217+ if (spi2STATbits.SPIROV == 1) spi2STATbits.SPIROV = 0;
218+ sprintf(sz, "%d %04X\r\n", i, sts);
219+ PDEBUG(sz);
220+#endif
221+// while(spi2STATbits.SPITBF == 1);
222+// while(spi2STATbits.SPITBF);
223+/*
224+ while(!spi2_tx_fifo_is_full())
225+ spi2_putc(c++);
226+ while(!spi2_rx_fifo_is_empty()) {
227+ rx = spi2_getc();
228+ if (rx == '$') c = 0;
229+ }
230+*/
231+ // i = ReadUART1();
232+// WriteUART2(i);
233+
234+// PDEBUG(mes);
235+// delay();
236+ }
237+}
238+#endif
239+#if 0
240+void test_ad1()
241+{
242+ unsigned char in[3];
243+ long l[6];
244+ char cs;
245+ while(!ad_is_drdy0_enable());
246+// PDEBUG("AD\r\n");
247+ for(cs=0; cs<6; cs++) {
248+ ad_cs(cs);
249+ in[2] = ad_spi_rcv();
250+ in[1] = ad_spi_rcv();
251+ in[0] = ad_spi_rcv();
252+ ad_cs_dis();
253+ l[cs] = byte3_to_long(in);
254+// sprintf(sz, "%02X %02X %02X", in[2], in[1], in[0]);
255+// PDEBUG(sz);
256+ }
257+ for(cs=0; cs<6; cs++) {
258+ sprintf(sz, "%+7ld,", l[cs]);
259+ PDEBUG(sz);
260+ }
261+ PDEBUG("\r\n");
262+
263+}
264+#endif
--- a/ublox.c
+++ b/ublox.c
@@ -25,7 +25,7 @@
2525 #define UBX_STS_SUMB 8
2626
2727 /*
28- len: payload length
28+ * lenp: payload length
2929 */
3030 static void ublox_make_sum(unsigned char *pkt, unsigned int lenp, unsigned char *suma, unsigned char *sumb)
3131 {
@@ -39,7 +39,7 @@ static void ublox_make_sum(unsigned char *pkt, unsigned int lenp, unsigned char
3939 }
4040 /*
4141 * GPSに送信するコマンドパケットの作成
42- * unsgined char len: payloadのバイト数
42+ * unsgined char lenp: payloadのバイト数
4343 * return: 作成したパケットpktのバイト数
4444 */
4545 int ublox_make_cmd(unsigned char id1, unsigned char id2, unsigned int lenp, unsigned char *payload, unsigned char pkt[])
@@ -183,21 +183,11 @@ void ublox_send_cfg_tp5_timepulse2(void)
183183
184184 payload[4] = 50; // cable delay ns
185185
186-// payload[8] = 0x80; // Freq 10MHz
187-// payload[9] = 0x96;
188-// payload[10] = 0x98;
189-// payload[11] = 0x00;
190-
191186 payload[8] = 0x00; // Freq 7.372800MHz
192187 payload[9] = 0x80;
193188 payload[10] = 0x70;
194189 payload[11] = 0x00;
195190
196-// payload[12] = 0x80; // Freq locked 10MHz
197-// payload[13] = 0x96;
198-// payload[14] = 0x98;
199-// payload[15] = 0x00;
200-
201191 payload[12] = 0x00; // Freq locked 7.372800MHz
202192 payload[13] = 0x80;
203193 payload[14] = 0x70;
@@ -257,17 +247,6 @@ void ublox_send_cfg_tp5_timepulse1(void)
257247 payload[21] = 0x86;
258248 payload[22] = 0x01;
259249 payload[23] = 0x00;
260-#if 0
261- payload[20] = 0x20; // Pulse width locked 500,000us
262- payload[21] = 0xA1;
263- payload[22] = 0x07;
264- payload[23] = 0x00;
265-
266- payload[20] = 0x00; // Pulse width locked 800,000us
267- payload[21] = 0x35;
268- payload[22] = 0x0C;
269- payload[23] = 0x00;
270-#endif
271250
272251 payload[28] = 0b11110111; // Grid=GPS,Pol=Rise
273252
--- /dev/null
+++ b/util.h
@@ -0,0 +1,26 @@
1+#ifndef __UTIL_H__
2+#define __UTIL_H__
3+
4+inline extern void my_memcpy(void *dest0, void *src0, int len)
5+{
6+ int i;
7+ char *dest, *src;
8+
9+ dest = (char*)dest0;
10+ src = (char*)src0;
11+ for(i = 0; i < len; i++) {
12+ *dest++ = *src++;
13+ }
14+}
15+inline extern void my_memset(void *dest0, unsigned char data, int len)
16+{
17+ int i;
18+ char *dest;
19+
20+ dest = (char*)dest0;
21+ for(i = 0; i < len; i++) {
22+ *dest++ = data;
23+ }
24+}
25+
26+#endif
Show on old repository browser