1 | #include <p18f25j10.h>
|
2 | #include <spi.h>
|
3 | #include <controller.h>
|
4 |
|
5 | void busy_polling(void);
|
6 | void byte_write_to_buffer1(void);
|
7 | void byte_write_to_mainmemory(void);
|
8 | void byte_read_from_mainmemory(void);
|
9 | unsigned char byte_read_from_buffer1(void);
|
10 |
|
11 |
|
12 | // VARIABLE Definitions
|
13 | unsigned char result;
|
14 |
|
15 | extern char gInput[20];
|
16 |
|
17 | #define SPI_CS LATCbits.LATC2
|
18 |
|
19 |
|
20 | void flash_SPI_1_Test(void)
|
21 | {
|
22 | TRISCbits.TRISC2 = 0;
|
23 |
|
24 | SPI_CS = 1;
|
25 | OpenSPI(SPI_FOSC_16, MODE_00, SMPEND);
|
26 | busy_polling();
|
27 | byte_write_to_buffer1();
|
28 | busy_polling();
|
29 | byte_write_to_mainmemory();
|
30 | busy_polling();
|
31 | byte_read_from_mainmemory();
|
32 | busy_polling();
|
33 | gInput[0] = byte_read_from_buffer1();
|
34 |
|
35 | gInput[1] = 0;
|
36 | rs485_p_Client_Output((const far rom char *) "Speicherausgabe: ");
|
37 | rs485_d_Client_Output((char *) gInput);
|
38 | rs485_p_Client_Output((const far rom char *) "\n\r");
|
39 |
|
40 | CloseSPI();
|
41 | }
|
42 |
|
43 |
|
44 | void busy_polling (void)
|
45 | {
|
46 |
|
47 | unsigned char read;
|
48 |
|
49 | do
|
50 | {
|
51 | SPI_CS = 0;
|
52 | result = putcSPI(0xD7); //Status Register Read
|
53 | if (result==-1)
|
54 | rs485_p_Client_Output((const far rom char *) "Debug: Fehler bei busy_polling() - 2\n\r");
|
55 |
|
56 | read = getcSPI();
|
57 | SPI_CS = 1;
|
58 |
|
59 | } while (read & 0x80); //stay in loop until !busy
|
60 | }
|
61 |
|
62 | void byte_write_to_buffer1 (void) //Opcode 0x84: x x x x x x x x x x x x x x x B B B B B B B B B
|
63 | {
|
64 | SPI_CS = 0;
|
65 | result = putcSPI(0x84);
|
66 | if (result==-1)
|
67 | rs485_p_Client_Output((const far rom char *) "Debug: Fehler bei byte_write_to_buffer1() - 1\n\r");
|
68 |
|
69 | result = putcSPI(0x00);
|
70 | if (result==-1)
|
71 | rs485_p_Client_Output((const far rom char *) "Debug: Fehler bei byte_write_to_buffer1() - 2\n\r");
|
72 |
|
73 | result = putcSPI(0x00);
|
74 | if (result==-1)
|
75 | rs485_p_Client_Output((const far rom char *) "Debug: Fehler bei byte_write_to_buffer1() - 3\n\r");
|
76 |
|
77 | result = putcSPI(0x00);
|
78 | if (result==-1)
|
79 | rs485_p_Client_Output((const far rom char *) "Debug: Fehler bei byte_write_to_buffer1() - 4\n\r");
|
80 |
|
81 | result = putcSPI('B');
|
82 | if (result==-1)
|
83 | rs485_p_Client_Output((const far rom char *) "Debug: Fehler bei byte_write_to_buffer1() - 5\n\r");
|
84 |
|
85 | SPI_CS = 1;
|
86 | }
|
87 |
|
88 | void byte_write_to_mainmemory (void) //Opcode 0x83: r r r r P P P P P P P P P P P x x x x x x x x x
|
89 | {
|
90 | SPI_CS = 0;
|
91 | result = putcSPI(0x83);
|
92 | if (result==-1)
|
93 | rs485_p_Client_Output((const far rom char *) "Debug: Fehler bei byte_write_to_mainmemory() - 1\n\r");
|
94 |
|
95 | result = putcSPI(0x00);
|
96 | if (result==-1)
|
97 | rs485_p_Client_Output((const far rom char *) "Debug: Fehler bei byte_write_to_mainmemory() - 2\n\r");
|
98 |
|
99 | result = putcSPI(0x00);
|
100 | if (result==-1)
|
101 | rs485_p_Client_Output((const far rom char *) "Debug: Fehler bei byte_write_to_mainmemory() - 3\n\r");
|
102 |
|
103 | result = putcSPI(0x00);
|
104 | if (result==-1)
|
105 | rs485_p_Client_Output((const far rom char *) "Debug: Fehler bei byte_write_to_mainmemory() - 4\n\r");
|
106 |
|
107 | SPI_CS = 1;
|
108 | }
|
109 |
|
110 | void byte_read_from_mainmemory (void) //Opcode 0x53: r r r r P P P P P P P P P P P x x x x x x x x x
|
111 | {
|
112 | SPI_CS = 0;
|
113 | result = putcSPI(0x53);
|
114 | if (result==-1)
|
115 | rs485_p_Client_Output((const far rom char *) "Debug: Fehler bei byte_read_from_mainmemory() - 1\n\r");
|
116 |
|
117 | result = putcSPI(0x00);
|
118 | if (result==-1)
|
119 | rs485_p_Client_Output((const far rom char *) "Debug: Fehler bei byte_read_from_mainmemory() - 2\n\r");
|
120 |
|
121 | result = putcSPI(0x00);
|
122 | if (result==-1)
|
123 | rs485_p_Client_Output((const far rom char *) "Debug: Fehler bei byte_read_from_mainmemory() - 3\n\r");
|
124 |
|
125 | result = putcSPI(0x00);
|
126 | if (result==-1)
|
127 | rs485_p_Client_Output((const far rom char *) "Debug: Fehler bei byte_read_from_mainmemory() - 4\n\r");
|
128 |
|
129 | SPI_CS = 1;
|
130 | }
|
131 |
|
132 | unsigned char byte_read_from_buffer1 (void) //Opcode 0xD4: x x x x x x x x x x x x x x x B B B B B B B B B
|
133 | {
|
134 | unsigned char read;
|
135 |
|
136 | SPI_CS = 0;
|
137 | result = putcSPI(0xD4);
|
138 | if (result==-1)
|
139 | rs485_p_Client_Output((const far rom char *) "Debug: Fehler bei byte_read_from_buffer1() - 1\n\r");
|
140 |
|
141 | result = putcSPI(0x00);
|
142 | if (result==-1)
|
143 | rs485_p_Client_Output((const far rom char *) "Debug: Fehler bei byte_read_from_buffer1() - 2\n\r");
|
144 |
|
145 | result = putcSPI(0x00);
|
146 | if (result==-1)
|
147 | rs485_p_Client_Output((const far rom char *) "Debug: Fehler bei byte_read_from_buffer1() - 3\n\r");
|
148 |
|
149 | result = putcSPI(0x00);
|
150 | if (result==-1)
|
151 | rs485_p_Client_Output((const far rom char *) "Debug: Fehler bei byte_read_from_buffer1() - 4\n\r");
|
152 |
|
153 | read = getcSPI();
|
154 |
|
155 | SPI_CS = 1;
|
156 |
|
157 | return read;
|
158 | }
|