OSDN Git Service

myint.h int2 add
[scilog/cpu2010.git] / main.c
diff --git a/main.c b/main.c
index 9b9aa97..7dd2fe1 100644 (file)
--- a/main.c
+++ b/main.c
@@ -181,6 +181,99 @@ static char pps_intf;
 #define pps_is_intf_enable()    (pps_intf != 0)
 
 /*
+ * SPI FLAG test
+ */
+void test_spi_flag(void)
+{
+    char    sz[128];
+    int in;
+    unsigned char txc = 0;
+    unsigned char rxc;
+
+    spi2_int_disable();
+    cnint_int_disable();
+    while(1) {
+        delay_ms(1000);
+        sprintf(sz, "SPI2STAT=%04X "
+                "SPIBEC=%d SRMPT=%d SPIROV=%d SRXMPT=%d TBF=%d RBF=%d\r\n"
+                , SPI2STAT
+                , SPI2STATbits.SPIBEC
+                , SPI2STATbits.SRMPT
+                , SPI2STATbits.SPIROV
+                , SPI2STATbits.SRXMPT
+                , SPI2STATbits.SPITBF
+                , SPI2STATbits.SPIRBF
+                );
+        PDEBUG(sz);
+        if ((in = uart2_rcvbuf_getc()) >= 0) {
+            uart2_myputc(in);
+            uart2_puts("\r\n");
+
+            switch(in) {
+                case 'r':
+                    rxc = spi2_getc();
+                    sprintf(sz, "RX=%02X\r\n", rxc);
+                    uart2_puts(sz);
+                    break;
+                case 't':
+                    spi2_putc(txc++);
+                    sprintf(sz, "TX=%02X\r\n", txc);
+                    uart2_puts(sz);
+                    break;
+                case 'v':
+                    spi2_rx_overrun_clear();
+                    break;
+                case 'x':
+                    SPI2STATbits.SRXMPT = 0;
+                    break;
+                case 'y':
+                    SPI2STATbits.SRXMPT = 1;
+                    break;
+            }
+        }
+    }
+}
+/*
+ * polling send/rcv
+ */
+void test_spi_send(void)
+{
+    unsigned char txc = 1;
+    unsigned char rxc;
+    char    sz[128];
+
+    spi2_int_disable();
+    cnint_int_disable();
+//    SPI2BUF = txc++;
+//    rxc = SPI2BUF;
+    while(1) {
+        while(SPI2STATbits.SPITBF == 0) {
+            // TX
+            SPI2BUF = txc++;
+        }
+        if (SPI2STATbits.SRXMPT == 0) {
+            while(SPI2STATbits.SRXMPT == 0) {
+                // RX FIFO読み出し
+                rxc = SPI2BUF;
+            }
+#if 0
+            sprintf(sz, "SPI2STAT=%04X "
+                    "SPIBEC=%d SRMPT=%d SPIROV=%d SRXMPT=%d TBF=%d RBF=%d\r\n"
+                    , SPI2STAT
+                    , SPI2STATbits.SPIBEC
+                    , SPI2STATbits.SRMPT
+                    , SPI2STATbits.SPIROV
+                    , SPI2STATbits.SRXMPT
+                    , SPI2STATbits.SPITBF
+                    , SPI2STATbits.SPIRBF
+                    );
+            PDEBUG(sz);
+#endif
+        }
+    }
+
+}
+/*
  * main()
  */
 int main(int argc, char** argv) {
@@ -199,6 +292,8 @@ int main(int argc, char** argv) {
     /**** 割り込み禁止 ****/
     SET_CPU_IPL( 7 );
     __builtin_write_OSCCONL(OSCCON & 0xbf); //clear the bit 6 of OSCCONL to unlock Pin Re-map
+    // 割り込みネストenable
+    int_nest_enable();
 
     // UART1ピン割り当て
     RPINR18bits.U1RXR = 6;     // UART1 RX to RP6
@@ -226,13 +321,21 @@ int main(int argc, char** argv) {
     CLKDIV = 0;                 // CPU Peripheral Clock Ratio=1:1
     AD1PCFG = 0xFFFF;           // AN1-12input pin = Digital mode
 
-    // INT1(RP12 RB12) AD -DRDY0 Int enable
+    // INT1(RP12 RB12) AD -DRDY0 setup
     RPINR0bits.INT1R = 12;  // RP12(RB12)
+    int1_int_disable();
     int1_level_set(4);    // Int priority
     int1_edge_neg(); // Negative Edge
-    // INT0(RB7) GPS +PPS Int enable
+    // INT0(RB7) GPS +PPS setup
+    int0_int_disable();
     int0_level_set(4);    // INT Priority
     int0_edge_pos(); // Positive Edge
+    // ChangeNotification INT, CN20(RC8), SPI2(Overo) CS0
+    cnint_int_disable();
+    CNEN2bits.CN20IE = 1;
+    CNPU2bits.CN20PUE = 1;  // Pullup ON
+    cnint_level_set(4);
+
 
     adint_cnt = 0;
     ad_cs_dis();
@@ -256,6 +359,8 @@ int main(int argc, char** argv) {
 
     spi1_init();
     spi2_init();
+    spi1_int_disable();
+    spi2_int_disable();
 
     PDEBUG("START\r\n");
     puts("START debug\r\n");
@@ -271,9 +376,9 @@ int main(int argc, char** argv) {
     // GPS UART1 受信割り込み許可
     uart1_rx_int_enable();
     uart1_set_int_level(4);  // 割り込みレベル デフォルト4
-    // DataOut SPI2 割り込み許可
-//    spi2_int_enable();
-//    spi2_set_int_level(4);
+    // DebugOut UART2 受信割り込み許可
+    uart2_rx_int_enable();
+    uart2_set_int_level(4);  // 割り込みレベル デフォルト4
 
     //**** 割り込み許可 **************************************************
     // CPU割り込み優先レベル デフォルト0 ペリフェラルはデフォルト4
@@ -317,6 +422,16 @@ int main(int argc, char** argv) {
     // INT0割り込み許可
     int0_intf_clear();
     int0_int_enable();    // Int enable
+    // DataOut SPI2 割り込み許可しない CSのCN INTで許可するので
+    spi2_intf_clear();
+    spi2_set_int_level(4);
+//    spi2_int_enable();
+    // ChangeNotification INT許可 SPI2(Overo) CS
+    cnint_intf_clear();
+    cnint_int_enable();
+/****!!!!!!!!!!!!!!!!!!!!!!!!*/
+//    test_spi_flag();
+test_spi_send();
 
     while(1) {
         // GPSから受信してデコード
@@ -446,8 +561,8 @@ void __attribute__((interrupt, auto_psv)) _SPI1Interrupt(void)
     unsigned char c;
     long    l;
 
+    spi1_intf_clear();
     c = spi1_getc();
-//    spi1_intf_clear();
     spi1_rx_overrun_clear();
 //spi1_int_disable();
 //ad_cs_dis();
@@ -514,20 +629,23 @@ void __attribute__((interrupt, auto_psv)) _SPI1Interrupt(void)
  * DataOut SPI2 Interrupt Handler
  * TX FIFOに1byteの空きが出来ると発生
  */
-void __attribute__((interrupt, no_auto_psv)) _SPI2Interrupt(void)
-{
 static unsigned char   spi2c;
+void __attribute__((interrupt, auto_psv)) _SPI2Interrupt(void)
+{
     unsigned char   rx;
     static unsigned int txlen = 512;
-/*
+    spi2_intf_clear();
+#if 1
+//    static unsigned char   c=0;
     while(!spi2_tx_fifo_is_full())
-        spi2_putc(c++);
+        spi2_putc(spi2c++);
     while(!spi2_rx_fifo_is_empty()) {
         rx = spi2_getc();
-        if (rx == '$') c = 0;
+//        if (rx == '$') c = 0;
     }
-*/
-//spi2c = 123;
+#endif
+#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()) {
@@ -546,6 +664,7 @@ static unsigned char   spi2c;
             }
         }
     }
+#endif
     // オーバーランエラーならばクリアする
     // spi2_rx_is_overrun()
     if (SPI2STATbits.SPIROV == 1) SPI2STATbits.SPIROV = 0;
@@ -553,31 +672,32 @@ static unsigned char   spi2c;
     // TODO:Interrupt Flag@IEC Clear
 }
 /*
-void test_ad1()
+ * Overo SPI2 CS Input Change Notification
+ * Interrupt Handler
+ *
+ */
+#define SPI2CSpin  (PORTCbits.RC8)
+void __attribute__((interrupt, auto_psv)) _CNInterrupt(void)
 {
-        unsigned char in[3];
-        long l[6];
-        char cs;
-        while(!ad_is_drdy0_enable());
-//        PDEBUG("AD\r\n");
-        for(cs=0; cs<6; cs++) {
-            ad_cs(cs);
-                in[2] = ad_spi_rcv();
-                in[1] = ad_spi_rcv();
-                in[0] = ad_spi_rcv();
-            ad_cs_dis();
-            l[cs] = byte3_to_long(in);
-//            sprintf(sz, "%02X %02X %02X", in[2], in[1], in[0]);
-//            PDEBUG(sz);
-        }
-        for(cs=0; cs<6; cs++) {
-            sprintf(sz, "%+7ld,", l[cs]);
-            PDEBUG(sz);
-        }
-        PDEBUG("\r\n");
-
+    cnint_intf_clear();
+    if (SPI2CSpin == 1) {
+        // CS=H
+        // SPI2割り込みOFF
+        spi2_int_disable();
+
+    } else {
+        // CS=L
+    // SPI送信の一番最初なので
+    spi2c = 0;
+
+// SPI2 RX FIFOクリア
+// SPI2 TX FIFOクリア
+// リングバッファ読み込み位置get?
+// SPI2 TX FIFOに送信データ書き込み?
+        // SPI2割り込みON
+        spi2_int_enable();
+    }
 }
- */
 /*
  * CS0〜15順番にenable
  */
@@ -647,3 +767,29 @@ void test_spi2()
 //        delay();
     }
 }
+/*
+void test_ad1()
+{
+        unsigned char in[3];
+        long l[6];
+        char cs;
+        while(!ad_is_drdy0_enable());
+//        PDEBUG("AD\r\n");
+        for(cs=0; cs<6; cs++) {
+            ad_cs(cs);
+                in[2] = ad_spi_rcv();
+                in[1] = ad_spi_rcv();
+                in[0] = ad_spi_rcv();
+            ad_cs_dis();
+            l[cs] = byte3_to_long(in);
+//            sprintf(sz, "%02X %02X %02X", in[2], in[1], in[0]);
+//            PDEBUG(sz);
+        }
+        for(cs=0; cs<6; cs++) {
+            sprintf(sz, "%+7ld,", l[cs]);
+            PDEBUG(sz);
+        }
+        PDEBUG("\r\n");
+
+}
+ */