OSDN Git Service

IDE MPLABXで使えるようにする
[scilog/cpu2010.git] / test.c
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