OSDN Git Service

IDE MPLABXで使えるようにする
[scilog/cpu2010.git] / myspi.c
1 #include <p24FJ64GA004.h>
2
3 #include "myspi.h"
4
5
6 void spi1_rx_fifo_clear(void)
7 {
8     int i;
9     unsigned char   c;
10     for(i = 0; i < 16; i++) c = SPI1BUF;
11 }
12 void spi2_rx_fifo_clear(void)
13 {
14     int i;
15     unsigned char   c;
16     for(i = 0; i < 16; i++) c = SPI2BUF;
17 }
18 /*
19  AD SPI1 Init
20  Enhanced Buffer OFF
21  master mode
22 */
23 int spi1_init(void)
24 {
25     int i;
26     unsigned char   c;
27
28     SPI1STAT = 0;
29     //1. Clear the SPIxBUF register.
30     for(i = 0; i < 16; i++) c = SPI1BUF;
31     spi1_intf_clear();
32
33     //2. Write the desired settings to the SPIxCON register with MSTEN (SPIxCON1<5>) = 1.
34     // bit12 DISSCK=0 0=Internal SPI clock is enabled
35     // bit11 DISSDO=0 0 = SDOx pin is controlled by the module
36     // bit10 MODE16=0 8bit
37     // bit9  SMP=0
38     //   @MasterMode
39     //   1 = Input data sampled at end of data output time
40     //   0 = Input data sampled at middle of data output time
41     // bit8  CKE=0 DATA CHANGE=CLK Idle to Active =LtoH
42     // bit7  SSEN=0 SS pin disable
43     // bit6  CKP=0 CLK IDLE=LOW
44     // bit5  MSTEN=1 Master
45     // bit4-2 SPRE=0b111 Secondary Prescale 1:1
46     //  111 = Secondary prescale 1:1
47     //  110 = Secondary prescale 2:1
48     //  ...
49     //  000 = Secondary prescale 8:1
50     // bit1-0 PPRE=0b01 Primarily Prescale 4:1
51     //  11 = Primary prescale 1:1
52     //  10 = Primary prescale 4:1
53     //  01 = Primary prescale 16:1
54     //  00 = Primary prescale 64:1
55     //           5432109876543210
56     SPI1CON1 = 0b0000000000111110;
57
58     // Enahanced Buffer(FIFO) Disable
59     // bit0 SPIBEN: Enhanced Buffer Enable bit
60     //  1 = Enhanced Buffer enabled
61     //  0 = Enhanced Buffer disabled (Legacy mode)
62     SPI1CON2 = 0x00;
63 //    SPI1CON2bits.SPIBEN = 1;
64
65     // bit 4-2 SISEL2:SISEL0: SPIx Buffer Interrupt Mode bits (valid in Enhanced Buffer mode)
66     //  001 = Interrupt when data is available in receive buffer (SRMPT bit is set)
67 //    SPI1STATbits.SISEL = 0b001;
68
69     //3. Clear the SPIROV bit (SPIxSTAT<6>).
70     SPI1STATbits.SPIROV = 0;
71
72     //4. Enable SPIx operation by setting the SPIEN bit (SPIxSTAT<15>).
73     SPI1STATbits.SPIEN = 1;
74
75 }
76 /*
77  * DataOut SPI2 Init
78  * Enhanced Buffer ON
79  * Slave mode
80  * SSpin enable
81  * INT:
82 */
83 int spi2_init(void)
84 {
85     int i;
86     unsigned char   c;
87
88     SPI2STAT = 0;
89     //1. Clear the SPIxBUF register.
90     for(i = 0; i < 16; i++) c = SPI2BUF;
91     //2. If using interrupts:
92     //• Clear the SPIxIF bit in the respective IFSx register.
93     IFS2bits.SPI2IF = 0;
94 //• Set the SPIxIE bit in the respective IECx register.
95 //• Write the SPIxIP bits in the respective IPCx register to set the interrupt priority.
96
97     // 割り込み要因設定
98     // 0b100 = Interrupt when one data is shifted into the SPIxSR, as a result, the TX FIFO has one open spot
99     //  TX FIFOに1byte空き生じた
100     SPI2STATbits.SISEL = 0b100;
101
102     //3. Write the desired settings to the SPIxCON1 and SPIxCON2 registers with MSTEN (SPIxCON1<5>) = 0.
103     // SLAVE,8bit,CLK IDLE=LOW, DATA CHANGE=CLK HtoL, SS pin enable
104     //           5432109876543210
105 //  SPI1CON1 = 0b0000000110000000;
106     SPI2CON1 = 0b0000000100000000;
107     // Enahanced Buffer(FIFO) Disable
108     SPI2CON2 = 0x00;
109     //4. Clear the SMP bit. SPI1CON1<9>
110     SPI2CON1bits.SMP = 0;
111     //5. If the CKE<8> bit is set, then the SSEN<7> bit must be set, thus enabling the SSx pin.
112     SPI2CON1bits.SSEN = 1;
113     //6. Clear the SPIROV bit (SPIxSTAT<6>).
114     SPI2STATbits.SPIROV = 0;
115     //7. Select Enhanced Buffer mode by setting the SPIBEN bit (SPIxCON2<0>).
116     SPI2CON2bits.SPIBEN = 1;
117
118     //8. Enable SPI operation by setting the SPIEN bit (SPIxSTAT<15>).
119     SPI2STATbits.SPIEN = 1;
120 }