• R/O
  • HTTP
  • SSH
  • HTTPS

cpu2010: Commit

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


Commit MetaInfo

Revisãoa7a300c1a4274c0b5927e98acdeb38b5f2f591e8 (tree)
Hora2011-12-07 18:07:06
AutorNaoya Takamura <ntaka206@user...>
CommiterNaoya Takamura

Mensagem de Log

ring.c read pointer no.2 add
main.c spi2 fifo handling

Mudança Sumário

Diff

--- a/main.c
+++ b/main.c
@@ -43,6 +43,7 @@ _CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2 )
4343 * タイムスタンプ
4444 */
4545 #define ADBUF_BYTE 3 // AD 1CH 1sample dataのbyte数
46+#define ADBUF_LEN sizeof(ADbufType) // 1dataの長さ byte
4647
4748 typedef struct {
4849 UbloxNavTimeUtc t; // タイムスタンプ
@@ -180,6 +181,16 @@ static char pps_intf;
180181 #define pps_intf_set() pps_intf = 1
181182 #define pps_is_intf_enable() (pps_intf != 0)
182183
184+/**** SPI2 *********************************************/
185+#define SPI2CSpin (PORTCbits.RC8)
186+// SPI2 未送信データバイト数
187+static int dataout_cnt;
188+#define dataout_cnt_set(i) dataout_cnt = (i)
189+#define dataout_cnt_get() dataout_cnt
190+#define dataout_cnt_minus() (dataout_cnt--)
191+/**** DRDY OUT ****************************************/
192+#define DRDY_OUT PORTCbits.RC9
193+/**** TEST *******************************************/
183194 /*
184195 * SPI FLAG test
185196 */
@@ -190,8 +201,8 @@ void test_spi_flag(void)
190201 unsigned char txc = 0;
191202 unsigned char rxc;
192203
193- spi2_int_disable();
194- cnint_int_disable();
204+// spi2_int_disable();
205+// cnint_int_disable();
195206 while(1) {
196207 delay_ms(1000);
197208 sprintf(sz, "SPI2STAT=%04X "
@@ -224,7 +235,10 @@ void test_spi_flag(void)
224235 spi2_rx_overrun_clear();
225236 break;
226237 case 'x':
227- SPI2STATbits.SRXMPT = 0;
238+ SPI2STATbits.SPIEN = 0;
239+ asm("nop");
240+ SPI2STATbits.SPIEN = 1;
241+ SPI2BUF;
228242 break;
229243 case 'y':
230244 SPI2STATbits.SRXMPT = 1;
@@ -341,6 +355,7 @@ int main(int argc, char** argv) {
341355 ad_cs_dis();
342356 sts_set(STS_NOSYNC);
343357 pps_intf_clear();
358+ DRDY_OUT = 1;
344359
345360 // GPS UART1初期設定 BRGH=Standard mode
346361 // 9600bps 8bit nonparity 1stop nonflowcnt.
@@ -361,6 +376,7 @@ int main(int argc, char** argv) {
361376 spi2_init();
362377 spi1_int_disable();
363378 spi2_int_disable();
379+ spi2_putc('$');
364380
365381 PDEBUG("START\r\n");
366382 puts("START debug\r\n");
@@ -370,7 +386,7 @@ int main(int argc, char** argv) {
370386 PDEBUG(sz);
371387 sprintf(sz, "SPI1CON2=%04X\r\n", SPI1CON2);
372388 PDEBUG(sz);
373- sprintf(sz, "SPI1STAT=%04X\r\n", SPI1STAT);
389+ sprintf(sz, "sizeof(ADBUF)=%d\r\n", ADBUF_LEN);
374390 PDEBUG(sz);
375391
376392 // GPS UART1 受信割り込み許可
@@ -425,13 +441,12 @@ int main(int argc, char** argv) {
425441 // DataOut SPI2 割り込み許可しない CSのCN INTで許可するので
426442 spi2_intf_clear();
427443 spi2_set_int_level(4);
428-// spi2_int_enable();
429444 // ChangeNotification INT許可 SPI2(Overo) CS
430445 cnint_intf_clear();
431446 cnint_int_enable();
432447 /****!!!!!!!!!!!!!!!!!!!!!!!!*/
433448 // test_spi_flag();
434-test_spi_send();
449+//test_spi_send();
435450
436451 while(1) {
437452 // GPSから受信してデコード
@@ -440,9 +455,10 @@ test_spi_send();
440455 // 時刻有効
441456 sts_set(STS_SYNCNOW0);
442457 }
443- if (ring_read_get() != ring_write_get()) {
458+ // DebugOutに1secデータ出力
459+ if (ring_read2_get() != ring_write_get()) {
444460 // タイムスタンプget
445- gps = adbuf_time_get(ring_read_get());
461+ gps = adbuf_time_get(ring_read2_get());
446462 sprintf(sz, "%04u/%02d/%02d %02d:%02d:%02d.%09ld,%6lums,%6luns,%02X",
447463 gps->year, gps->month, gps->day, gps->hour, gps->min, gps->sec, gps->nano,
448464 gps->tow, gps->tacc, gps->valid
@@ -453,14 +469,14 @@ test_spi_send();
453469 // AD data get
454470 char ch;
455471 long *lp;
456- lp = adbuf_1sec_get(ring_read_get());
472+ lp = adbuf_1sec_get(ring_read2_get());
457473 for(ch=0; ch<AD_CHNUM; ch++) {
458474 sprintf(sz, ",%+7ld", lp[ch]);
459475 PDEBUG(sz);
460476 }
461477 PDEBUG("\r\n");
462478 #endif
463- ring_read_plus();
479+ ring_read2_plus();
464480 }
465481 }
466482 /*
@@ -502,7 +518,7 @@ void __attribute__((interrupt, no_auto_psv)) _INT0Interrupt(void)
502518 adint_cnt = AD_SAMPLE - 1;
503519
504520 // ADバッファにタイムスタンプ付ける
505- adbuf_time_write(ring_read_get(), &gpsNow);
521+ adbuf_time_write(ring_write_get(), &gpsNow);
506522
507523 // AD START信号による同期
508524 if (sts_get() == STS_SYNCNOW0) {
@@ -595,16 +611,20 @@ void __attribute__((interrupt, auto_psv)) _SPI1Interrupt(void)
595611 adint_cnt++;
596612 if (adint_cnt >= AD_SAMPLE) {
597613 // 1secぶんのデータgetした
598- // 平均してバッファに書きこみ
614+ // DRDY->H
615+ DRDY_OUT = 1;
616+ // 1sec平均する
599617 char ch;
600618 for(ch = 0; ch < AD_CHNUM; ch++) {
601619 adint_add[ch] /= adint_cnt;
602620 }
603- // バッファに書きこみ
621+ // 1secバッファに書きこみ
604622 adbuf_1sec_write(ring_write_get(), adint_add);
605623 // バッファ書き込み位置+1
606624 ring_write_plus();
607625 adint_cnt = 0;
626+ // DRDY->L
627+ DRDY_OUT = 0;
608628 }
609629 spi1_int_disable(); // SPI割り込みOFF
610630 adint_sts = ADSTS_IDLE;
@@ -630,12 +650,13 @@ void __attribute__((interrupt, auto_psv)) _SPI1Interrupt(void)
630650 * TX FIFOに1byteの空きが出来ると発生
631651 */
632652 static unsigned char spi2c;
633-void __attribute__((interrupt, auto_psv)) _SPI2Interrupt(void)
653+void __attribute__((interrupt, no_auto_psv)) _SPI2Interrupt(void)
634654 {
635655 unsigned char rx;
636656 static unsigned int txlen = 512;
657+
637658 spi2_intf_clear();
638-#if 1
659+#if 0
639660 // static unsigned char c=0;
640661 while(!spi2_tx_fifo_is_full())
641662 spi2_putc(spi2c++);
@@ -644,6 +665,17 @@ void __attribute__((interrupt, auto_psv)) _SPI2Interrupt(void)
644665 // if (rx == '$') c = 0;
645666 }
646667 #endif
668+ // 送信
669+ while(SPI2_TBF == 0) {
670+ if (dataout_cnt_get() <= 0) break;
671+ spi2_putc(spi2c++);
672+ dataout_cnt_minus();
673+ }
674+ // 受信
675+ while(SPI2_SRXMPT == 0) {
676+ rx = spi2_getc();
677+ }
678+
647679 #if 0
648680 //spi2c = 123;
649681 // RX FIFOから読み出し
@@ -676,24 +708,36 @@ void __attribute__((interrupt, auto_psv)) _SPI2Interrupt(void)
676708 * Interrupt Handler
677709 *
678710 */
679-#define SPI2CSpin (PORTCbits.RC8)
680-void __attribute__((interrupt, auto_psv)) _CNInterrupt(void)
711+void __attribute__((interrupt, no_auto_psv)) _CNInterrupt(void)
681712 {
682713 cnint_intf_clear();
683714 if (SPI2CSpin == 1) {
684715 // CS=H
685716 // SPI2割り込みOFF
686717 spi2_int_disable();
687-
718+ // SPI2をリセットしてTX/RX FIFOクリア
719+ SPI2STATbits.SPIEN = 0;
720+ asm("nop");
721+ SPI2STATbits.SPIEN = 1;
722+ asm("nop");
723+ SPI2BUF; // RBF->0
724+ // $->TX FIFO
725+ spi2_putc('$');
726+ // リングバッファ読み込み位置+1
727+
688728 } else {
689729 // CS=L
690- // SPI送信の一番最初なので
691- spi2c = 0;
692-
693-// SPI2 RX FIFOクリア
694-// SPI2 TX FIFOクリア
695-// リングバッファ読み込み位置get?
696-// SPI2 TX FIFOに送信データ書き込み?
730+ // リングバッファ読み込み位置get?
731+
732+ // SPI送信の一番最初なので
733+ spi2c = 0;
734+ dataout_cnt_set(ADBUF_LEN);
735+ // SPI2 TX FIFOに送信データ書き込み
736+ while(SPI2_TBF == 0) {
737+ if (dataout_cnt_get() <= 0) break;
738+ spi2_putc(spi2c++);
739+ dataout_cnt_minus();
740+ }
697741 // SPI2割り込みON
698742 spi2_int_enable();
699743 }
--- a/myspi.h
+++ b/myspi.h
@@ -11,13 +11,16 @@ int spi2_init(void);
1111 #define spi1_rx_overrun_clear() SPI1STATbits.SPIROV = 0;
1212
1313 #define spi1_putc(c) SPI1BUF = (c & 0xFF)
14-#define spi1_getc() (SPI1BUF & 0xFF)
14+#define spi1_getc() (SPI1BUF & 0xFF)
1515
1616 #define spi1_set_int_level(i) IPC2bits.SPI1IP = (i)
1717 #define spi1_int_enable() IEC0bits.SPI1IE = 1
1818 #define spi1_int_disable() IEC0bits.SPI1IE = 0
1919 #define spi1_intf_clear() IFS0bits.SPI1IF = 0
2020
21+#define SPI2_SRXMPT SPI2STATbits.SRXMPT
22+#define SPI2_TBF SPI2STATbits.SPITBF
23+#define SPI2_RBF SPI2STATbits.SPITBF
2124 #define spi2_tx_fifo_is_full() (SPI2STATbits.SPITBF == 1) // true=full
2225 #define spi2_rx_fifo_is_full() (SPI2STATbits.SPIRBF == 1) // true=full
2326 #define spi2_rx_fifo_is_empty() (SPI2STATbits.SRXMPT == 1) // true=empty
--- a/myuart.c
+++ b/myuart.c
@@ -1,7 +1,7 @@
11 #include <p24FJ64GA004.h>
22 #include "myuart.h"
33
4-// ublox GPS受信用リングバッファ
4+// UART1 ublox GPS受信用リングバッファ
55 static unsigned char ubx_w, ubx_r;
66 static unsigned char ubx_buf[256];
77
--- a/ring.c
+++ b/ring.c
@@ -6,7 +6,8 @@
66 read位置
77 */
88 unsigned char r_write;
9-unsigned char r_read;
9+unsigned char r_read1;
10+unsigned char r_read2;
1011 unsigned char r_num;
1112
1213 /*
@@ -14,39 +15,62 @@ unsigned char r_num;
1415 void ring_init(void)
1516 {
1617 r_write = 0;
17- r_read = 0;
18+ r_read1 = 0;
19+ r_read2 = 0;
1820 r_num = RING_NUM;
1921 }
2022 /*
2123 読み出し位置
2224 */
23-void ring_read_set(unsigned char i)
25+void ring_read1_set(unsigned char i)
2426 {
25- r_read = i;
27+ r_read1 = i;
2628 }
27-void ring_read_plus(void)
29+void ring_read1_plus(void)
2830 {
29- r_read++;
30- if (r_read >= r_num) r_read = 0;
31+ r_read1++;
32+ if (r_read1 >= r_num) r_read1 = 0;
33+}
34+void ring_read2_set(unsigned char i)
35+{
36+ r_read2 = i;
37+}
38+void ring_read2_plus(void)
39+{
40+ r_read2++;
41+ if (r_read2 >= r_num) r_read2 = 0;
3142 }
3243
3344 // 読み込んでいないデータ数
34-unsigned char ring_num_get()
45+unsigned char ring_num1_get()
3546 {
3647 signed char i;
3748
38- i = (signed char)r_write - (signed char)r_read;
49+ i = (signed char)r_write - (signed char)r_read1;
3950 if (i < 0) i += r_num;
4051 return (unsigned char)i;
4152 }
4253
54+unsigned char ring_num2_get()
55+{
56+ signed char i;
57+
58+ i = (signed char)r_write - (signed char)r_read2;
59+ if (i < 0) i += r_num;
60+ return (unsigned char)i;
61+}
4362 /*
4463 パケットバッファフル?
4564 1=Full
4665 0=not Full
4766 */
48-unsigned char ring_is_full(void)
67+unsigned char ring_is_full1(void)
68+{
69+ if (ring_num1_get() >= r_num - 1) return 1;
70+ return 0;
71+}
72+unsigned char ring_is_full2(void)
4973 {
50- if (ring_num_get() >= r_num - 1) return 1;
74+ if (ring_num2_get() >= r_num - 1) return 1;
5175 return 0;
5276 }
--- a/ring.h
+++ b/ring.h
@@ -6,17 +6,23 @@
66 #define RING_NUM 6
77
88 extern unsigned char r_write;
9-extern unsigned char r_read;
9+extern unsigned char r_read1;
10+extern unsigned char r_read2;
1011 extern unsigned char r_num;
1112
1213 #define ring_clear() ring_read_set(ring_write_get(t))
1314
1415 void ring_init(void);
1516
16-//読み出し位置
17-#define ring_read_get() r_read
18-void ring_read_set(unsigned char i);
19-void ring_read_plus(void);
17+//読み出し位置1
18+#define ring_read1_get() r_read1
19+void ring_read1_set(unsigned char i);
20+void ring_read1_plus(void);
21+
22+//読み出し位置2
23+#define ring_read2_get() r_read2
24+void ring_read2_set(unsigned char i);
25+void ring_read2_plus(void);
2026
2127 // 書き込み位置
2228 #define ring_write_get() r_write
@@ -26,8 +32,11 @@ inline extern void ring_write_plus(void)
2632 if (r_write >= r_num) r_write = 0;
2733 }
2834
29-unsigned char ring_num_get();
30-unsigned char ring_is_full(void);
35+unsigned char ring_num1_get();
36+unsigned char ring_is_full1(void);
37+
38+unsigned char ring_num2_get();
39+unsigned char ring_is_full2(void);
3140
3241 #endif
3342
Show on old repository browser