summaryrefslogtreecommitdiff
path: root/SPI.c
blob: 1fff384ef3936307a094de5cf73c5bee9cbfb01b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * File:   SPI.c
 * Author: josh
 *
 * Created on November 2, 2023, 4:37 AM
 */


#include "SPI.h"
#include "mcc_generated_files/mssp1_spi.h"

    uint8_t WriteBuffer[MY_BUFFER_SIZE];
    uint8_t ReadBuffer[MY_BUFFER_SIZE];
    uint16_t writeData;
    uint16_t readData;
    uint8_t  addr0;
    uint8_t  addr1;
    uint8_t     write;
    unsigned int    total;

/*do
    {
        total  = MSSP1_SPI_Exchange8bitBuffer( &myWriteBuffer[total], MY_BUFFER_SIZE - total, &myWriteBuffer[total]);

        // Do something else...

    } while( total < MY_BUFFER_SIZE );*/
    
        void Message_OUT(uint8_t Ohm_150, uint8_t Ohm_68, uint8_t testLED)
        {
            if (Ohm_150 > 15) Ohm_150 = 15;
            if (Ohm_68 > 5)   Ohm_68  =  5;
            //WriteBuffer[0] = WriteBuffer[2] = WriteBuffer[4] = (write << 8) | (!write << 7) | 0b000000;
            WriteBuffer[0] = WriteBuffer[2] = WriteBuffer[4] = 0x80;
            //WriteBuffer[1] = 0xFF >> (15-Ohm_150);
            //WriteBuffer[3] = 0xFE << (7-(Ohm_150-8)) | (Ohm_68 - 4);
            //2WriteBuffer[1] = 0x0F << (4-(Ohm_68 - 1)) | (testLED << 7);
            
            if(Ohm_150>7)WriteBuffer[3] = 0x7f;
            else WriteBuffer[3] = (0x7f >> (7-Ohm_150));// + ((Ohm_68 - 4)<<7);
            WriteBuffer[5] = 0xff << (15-Ohm_150);
            if(Ohm_68<5)WriteBuffer[1] = ((0xf0 >> (Ohm_68))&0x0f)| (testLED << 7);
            else {WriteBuffer[1] = 0x0f | (testLED << 7); WriteBuffer[3] |= 0x80;}
            //WriteBuffer[1] = 0xFF;
            //if (Ohm_68 == 5) WriteBuffer[3]|= 0b10000000;
            SPI_slaveSelect();
            readData = MSSP1_SPI_Exchange8bitBuffer(WriteBuffer, 6, ReadBuffer);
            SPI_slaveDeselect();
            //if(ReadBuffer[5]>0)_LATB8 = 0;
        }
        void active(uint8_t *activeResistors)
        {
            WriteBuffer[0] = WriteBuffer[2] = WriteBuffer[4] = 0x40; //Output driver data will be received by host controller after next message is sent.
            WriteBuffer[1] = WriteBuffer[3] = WriteBuffer[5] = 0x02;
            SPI_slaveSelect();
            readData = MSSP1_SPI_Exchange8bitBuffer(WriteBuffer, 6, ReadBuffer);
            SPI_slaveDeselect();
            WriteBuffer[0] = WriteBuffer[2] = WriteBuffer[4] = 0x00; //Output diagnostic data on next frame
            WriteBuffer[1] = WriteBuffer[3] = WriteBuffer[5] = 0x00;
            SPI_slaveSelect();
            readData = MSSP1_SPI_Exchange8bitBuffer(WriteBuffer, 6, ReadBuffer);
            SPI_slaveDeselect();
            activeResistors[0] = bitcount(ReadBuffer[3]&0x7F) + bitcount(ReadBuffer[5]); //+ bitcount(ReadBuffer[3] << 1));
            //ohm150 = bitcount(ReadBuffer[3]);// + ReadBuffer[5] + ReadBuffer[3] + ReadBuffer[1];
            activeResistors[1] = /*((ReadBuffer[3] & 0x80)>>7) +*/ bitcount(ReadBuffer[1] & 0x0f);
            //activeResistors[0] = 0;
            //activeResistors[0] = 3;
        }


        void Message_BIM(uint8_t outputs[3])
        {
            WriteBuffer[0] = WriteBuffer[2] = WriteBuffer[4] = (write << 8) | (!write << 7) | 0b000001;
            WriteBuffer[1] = outputs[0];
            WriteBuffer[3] = outputs[1];
            WriteBuffer[5] = outputs[2];
        }

        void Message_MAPIN0(uint8_t outputs[3])
        {
            WriteBuffer[0] = WriteBuffer[2] = WriteBuffer[4] = (write << 8) | (!write << 7) | 0b000100;
            WriteBuffer[1] = outputs[0];
            WriteBuffer[3] = outputs[1];
            WriteBuffer[5] = outputs[2];
        }


        void Message_MAPIN1(uint8_t outputs[3])
        {
            WriteBuffer[0] = WriteBuffer[2] = WriteBuffer[4] = (write << 8) | (!write << 7) | 0b000101;
            WriteBuffer[1] = outputs[0];
            WriteBuffer[3] = outputs[1];
            WriteBuffer[5] = outputs[2];
        }

        void Message_INST(void)
        {
            WriteBuffer[0] = WriteBuffer[2] = WriteBuffer[4] = 0x46;
        }

        void Message_DAGIOL(uint8_t outputs[3])
        {
            WriteBuffer[0] = WriteBuffer[2] = WriteBuffer[4] = (write << 8) | (!write << 7) | 0b001000;
            WriteBuffer[1] = outputs[0];
            WriteBuffer[3] = outputs[1];
            WriteBuffer[5] = outputs[2];
        }

        void Message_DAGOSM(void)
        {
            WriteBuffer[0] = WriteBuffer[2] = WriteBuffer[4] = 0x49;
        }

        void Message_DAGOLON(void)
        {
            WriteBuffer[0] = WriteBuffer[2] = WriteBuffer[4] = 0x4A;
        }

        void Message_DAGOLONEN(uint8_t outputs[3])
        {
            WriteBuffer[0] = WriteBuffer[2] = WriteBuffer[4] = (write << 8) | (!write << 7) | 0b001011;
            WriteBuffer[1] = outputs[0];
            WriteBuffer[3] = outputs[1];
            WriteBuffer[5] = outputs[2];
        }
        
        void Message_HWCR()
        {
            WriteBuffer[0] = WriteBuffer[2] = WriteBuffer[4] = 0x8C;
            WriteBuffer[1] = WriteBuffer[3] = WriteBuffer[5] = 0xC0;
            SPI_slaveSelect();
            readData = MSSP1_SPI_Exchange8bitBuffer(WriteBuffer, 6, ReadBuffer);
            SPI_slaveDeselect();
        }
 
        void Message_HWCROCL()
        {
            WriteBuffer[0] = WriteBuffer[2] = WriteBuffer[4] = 0x8D;
            WriteBuffer[1] = WriteBuffer[3] = WriteBuffer[5] = 0xff;
        }

        void Message_HWCRPWM(uint8_t outputs[3])
        {
            WriteBuffer[0] = WriteBuffer[2] = WriteBuffer[4] = (write << 8) | (!write << 7) | 0b001110;
            WriteBuffer[1] = outputs[0];
            WriteBuffer[3] = outputs[1];
            WriteBuffer[5] = outputs[2];
        }
 
        void Message_PWMCR0(uint16_t outputs[3])
        {
            WriteBuffer[0] = WriteBuffer[2] = WriteBuffer[4] = (write << 8) | (!write << 7) | (0b00010000);
            WriteBuffer[0] |= (outputs[0] << 6) >> 14;
            WriteBuffer[2] |= (outputs[1] << 6) >> 14;
            WriteBuffer[4] |= (outputs[3] << 6) >> 14;
            WriteBuffer[1] = (outputs[0] << 8) >> 8;
            WriteBuffer[3] = (outputs[1] << 8) >> 8;
            WriteBuffer[5] = (outputs[2] << 8) >> 8;
        }
        
        void Message_PWMCR1(uint16_t outputs[3])
        {
            WriteBuffer[0] = WriteBuffer[2] = WriteBuffer[4] = (write << 8) | (!write << 7) | (0b00010100);
            WriteBuffer[0] |= (outputs[0] << 6) >> 14;
            WriteBuffer[2] |= (outputs[1] << 6) >> 14;
            WriteBuffer[4] |= (outputs[3] << 6) >> 14;
            WriteBuffer[1] = (outputs[0] << 8) >> 8;
            WriteBuffer[3] = (outputs[1] << 8) >> 8;
            WriteBuffer[5] = (outputs[2] << 8) >> 8;
        }
 
        void Message_PWMOUT(uint8_t outputs[3])
        {
            WriteBuffer[0] = WriteBuffer[2] = WriteBuffer[4] = (write << 8) | (!write << 7) | 0b100100;
            WriteBuffer[1] = outputs[0];
            WriteBuffer[3] = outputs[1];
            WriteBuffer[5] = outputs[2];
        }
 
        void Message_PWMMAP(uint8_t outputs[3])
        {
            WriteBuffer[0] = WriteBuffer[2] = WriteBuffer[4] = (write << 8) | (!write << 7) | 0b100101;
            WriteBuffer[1] = outputs[0];
            WriteBuffer[3] = outputs[1];
            WriteBuffer[5] = outputs[2];
        }
        
uint8_t bitcount(uint8_t n)
{
    uint8_t count = 0;
    while (n > 0)
    {
        count++;
        n = n & (n-1);
    }
    return count;
}