OSDN Git Service

adbuf_write() bug fix.
authorNaoya Takamura <ntaka206@users.sourceforge.jp>
Fri, 9 Dec 2011 08:21:24 +0000 (17:21 +0900)
committerNaoya Takamura <ntaka206@users.sourceforge.jp>
Fri, 9 Dec 2011 08:21:24 +0000 (17:21 +0900)
main.c

diff --git a/main.c b/main.c
index 5a86cec..9012b01 100644 (file)
--- a/main.c
+++ b/main.c
@@ -43,7 +43,7 @@ _CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2 )
  * タイムスタンプ
  */
 #define ADBUF_BYTE  3   // AD 1CH 1sample dataのbyte数
-#define ADBUF_LEN   sizeof(ADbufType)   // 1dataの長さ byte
+#define ADBUF_LEN   sizeof(ADbufType)   // 1dataの長さ byte $含まず
 
 typedef struct {
     UbloxNavTimeUtc t;  // タイムスタンプ
@@ -91,7 +91,7 @@ inline void adbuf_write(unsigned char buf_no, unsigned char ch, int cnt, long *d
     unsigned char   i;
 
     for(i = 0; i < ADBUF_BYTE; i++) {
-        adbuf->data[ch][ADBUF_BYTE*cnt + i] = *(ptr++);
+        adbuf[buf_no].data[ch][ADBUF_BYTE*cnt + i] = *(ptr++);
     }
 }
 // 1
@@ -181,15 +181,22 @@ static char pps_intf;
 #define pps_intf_set()  pps_intf = 1
 #define pps_is_intf_enable()    (pps_intf != 0)
 
-/**** SPI2 *********************************************/
+/**** DataOut SPI2 *********************************************/
 #define SPI2CSpin  (PORTCbits.RC8)
+
 // SPI2 未送信データバイト数
 static int  dataout_cnt;
 #define dataout_cnt_set(i)  dataout_cnt = (i)
 #define dataout_cnt_get()  dataout_cnt
 #define dataout_cnt_minus()  (dataout_cnt--)
+
+// 送信位置ポインタ
+static char    *dout_ptr;
+
+
 /**** DRDY OUT ****************************************/
 #define DRDY_OUT    PORTCbits.RC9
+
 /**** TEST *******************************************/
 /*
  * SPI FLAG test
@@ -580,10 +587,6 @@ void __attribute__((interrupt, auto_psv)) _SPI1Interrupt(void)
     spi1_intf_clear();
     c = spi1_getc();
     spi1_rx_overrun_clear();
-//spi1_int_disable();
-//ad_cs_dis();
-//return;
-//c=1;
     switch(adint_sts) {
         case ADSTS_H:
             in[2] = c;
@@ -611,7 +614,7 @@ void __attribute__((interrupt, auto_psv)) _SPI1Interrupt(void)
                 adint_cnt++;
                 if (adint_cnt >= AD_SAMPLE) {
                     // 1secぶんのデータgetした
-                    // DRDY->H
+                    // DRDY->H DRDY=Lの時でもDRDYのエッジを作るため
                     DRDY_OUT = 1;
                     // 1sec平均する
                     char ch;
@@ -624,6 +627,7 @@ void __attribute__((interrupt, auto_psv)) _SPI1Interrupt(void)
                     ring_write_plus();
                     adint_cnt = 0;
                     // DRDY->L
+// TODO: waitカウンタで制限
                     DRDY_OUT = 0;
                 }
                 spi1_int_disable(); // SPI割り込みOFF
@@ -649,26 +653,17 @@ void __attribute__((interrupt, auto_psv)) _SPI1Interrupt(void)
  * DataOut SPI2 Interrupt Handler
  * TX FIFOに1byteの空きが出来ると発生
  */
-static unsigned char   spi2c;
+//static unsigned char   spi2c;
 void __attribute__((interrupt, no_auto_psv)) _SPI2Interrupt(void)
 {
     unsigned char   rx;
-    static unsigned int txlen = 512;
 
     spi2_intf_clear();
-#if 0
-//    static unsigned char   c=0;
-    while(!spi2_tx_fifo_is_full())
-        spi2_putc(spi2c++);
-    while(!spi2_rx_fifo_is_empty()) {
-        rx = spi2_getc();
-//        if (rx == '$') c = 0;
-    }
-#endif
     // 送信
     while(SPI2_TBF == 0) {
         if (dataout_cnt_get() <= 0) break;
-        spi2_putc(spi2c++);
+//        spi2_putc(spi2c++);
+        spi2_putc(*dout_ptr++);
         dataout_cnt_minus();
     }
     // 受信
@@ -676,32 +671,10 @@ void __attribute__((interrupt, no_auto_psv)) _SPI2Interrupt(void)
         rx = spi2_getc();
     }
 
-#if 0
-    //spi2c = 123;
-// RX FIFOから読み出し
-//    while(!spi2_rx_fifo_is_empty() || (!spi2_tx_fifo_is_full() && txlen > 0)) {
-    while(!spi2_rx_fifo_is_empty()) {
-        if (!spi2_rx_fifo_is_empty()) {
-            rx = spi2_getc();
-            if (rx == '$') {
-                spi2c = 0;
-                txlen = 3;
-            }
-        }
-        // TX FIFOに書き込み
-        if (!spi2_tx_fifo_is_full()) {
-            if (txlen > 0) {
-                spi2_putc(spi2c++);
-                txlen--;
-            }
-        }
-    }
-#endif
     // オーバーランエラーならばクリアする
     // spi2_rx_is_overrun()
     if (SPI2STATbits.SPIROV == 1) SPI2STATbits.SPIROV = 0;
 
-    // TODO:Interrupt Flag@IEC Clear
 }
 /*
  * Overo SPI2 CS Input Change Notification
@@ -724,24 +697,28 @@ void __attribute__((interrupt, no_auto_psv)) _CNInterrupt(void)
         // $->TX FIFO
         spi2_putc('$');
         // リングバッファ読み込み位置+1
-        
+        ring_read1_plus();
     } else {
         // CS=L
-        // リングバッファ読み込み位置get?
-
-        // SPI送信の一番最初なので
-        spi2c = 0;
+        // リングバッファ読み込み位置get
+        dout_ptr = (char*)adbuf_get(ring_read1_get());
+        // SPI送信の一番最初
+//        spi2c = 0;
         dataout_cnt_set(ADBUF_LEN);
         // SPI2 TX FIFOに送信データ書き込み
         while(SPI2_TBF == 0) {
             if (dataout_cnt_get() <= 0) break;
-            spi2_putc(spi2c++);
+//            spi2_putc(spi2c++);
+            spi2_putc(*dout_ptr++);
             dataout_cnt_minus();
         }
         // SPI2割り込みON
         spi2_int_enable();
+        // DRDY=H
+        DRDY_OUT = 1;
     }
 }
+#if 0
 /*
  * CS0〜15順番にenable
  */
@@ -811,6 +788,7 @@ void test_spi2()
 //        delay();
     }
 }
+#endif
 /*
 void test_ad1()
 {