Skip to content

Commit 9796dd9

Browse files
committed
add comment
1 parent 4bee172 commit 9796dd9

File tree

1 file changed

+205
-0
lines changed

1 file changed

+205
-0
lines changed

spi.c-use

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
/* 2016-08-14 (C) Jonas S Karlsson, [email protected] */
2+
3+
// TODO: use
4+
5+
// #include "esp/spi.h"
6+
7+
> void spi_led(int, int, int, int, int);
8+
>
9+
> PRIM led_show(lisp init, lisp digit, lisp val, lisp decode, lisp delay) {
10+
> spi_led(getint(init), getint(digit), getint(val), getint(decode), getint(delay));
11+
>
12+
> return mkint(1);
13+
> }
14+
>
15+
> int spiData[] = { 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 };
16+
>
17+
unsigned char decodeMode = 1;
18+
19+
PRIM led_data(lisp data, lisp offset) {
20+
int pos = getint(offset);
21+
int i = 0;
22+
int val = 0;
23+
while (data && ((i + pos) < 15)) {
24+
val = getint(car(data));
25+
// show nums and hex
26+
if (decodeMode == 0) {
27+
if (val > 9 && val < 16) {
28+
// offset by space ascii and start of lowercase alphabet
29+
val = val + 32 + 65 - 10;
30+
} else if (val >= 0 && val < 10) {
31+
val = val + 32 + 65 - 49;
32+
}
33+
}
34+
35+
spiData[pos + i] = val;
36+
i = i + 1;
37+
data = cdr(data);
38+
}
39+
return nil;
40+
}
41+
42+
DEFPRIM (delay, 1, delay);
43+
DEFPRIM (led_data, 2, led_data);
44+
DEFPRIM (led_show, 5, led_show);
45+
46+
DEFPRIM(delay, 1, delay);
47+
48+
DEFPRIM(load, -3, load); // -3 to get env?
49+
DEFPRIM(dir, 1, dir);
50+
DEFPRIM(cat, 1, cat);
51+
52+
// hardware spi pins
53+
int cs_pin = 12 ;
54+
int clk_pin = 14;
55+
int data_pin = 13;
56+
57+
#define MAXREG_DECODEMODE 0x09
58+
#define MAXREG_INTENSITY 0x0A
59+
#define MAXREG_SCANLIMIT 0x0B
60+
#define MAXREG_SHUTDOWN 0x0C
61+
#define MAXREG_DISPTEST 0x0F
62+
63+
void shiftOut(unsigned char* data, int delay);
64+
unsigned char sendChar(const char data, const bool dp);
65+
66+
void spi_led(int init, int digit, int val, int decode, int delay) {
67+
gpio_enable(cs_pin, GPIO_OUTPUT);
68+
gpio_enable(clk_pin, GPIO_OUTPUT);
69+
gpio_enable(data_pin, GPIO_OUTPUT);
70+
71+
// bool bSpi = spi_init(0, 2, 4, true, SPI_BIG_ENDIAN, true);
72+
// bool bSpi = spi_init(1, 0, 4, true, SPI_BIG_ENDIAN, true);
73+
74+
// const spi_settings_t my_settings = {
75+
// .mode = SPI_MODE0,
76+
// .freq_divider = SPI_FREQ_DIV_4M,
77+
// .msb = true,
78+
// .endianness = SPI_LITTLE_ENDIAN,
79+
// .minimal_pins = true
80+
// };
81+
82+
// spi_settings_t old;
83+
// spi_get_settings(1, &old); // save current settings
84+
85+
// printf("mode %d ", old.mode);
86+
// printf("dvd %d ", old.freq_divider);
87+
// printf("msb %d ", old.msb);
88+
// printf("end %d ", old.endianness);
89+
// printf("min %d ", old.minimal_pins);
90+
91+
// useful comments in this code re cpol, cpha
92+
//https://github.com/MetalPhreak/ESP8266_SPI_Driver/blob/master/driver/spi.c
93+
// settings from spi.h, look reasonable
94+
// spi_init(1, SPI_MODE0, SPI_FREQ_DIV_10M, true, SPI_LITTLE_ENDIAN, false ); //true);
95+
96+
// send two bytes, d15 first
97+
// see pdf p6 for format
98+
// Table 1. Serial-Data Format (16 Bits)
99+
// D15 D14
100+
// X
101+
// D13 D12
102+
// X X
103+
// D11 D10 D9 D8
104+
// ADDRESS
105+
// D7 D6 D5 D4
106+
// X
107+
// D3 D2 D1 D0
108+
// MSB DATA LSB
109+
unsigned char bytes[2];
110+
unsigned char initC = (unsigned char)init;
111+
if (init > 0) {
112+
if (initC & 0x04) {
113+
bytes[0] = MAXREG_SHUTDOWN;
114+
bytes[1] = 0x01;
115+
shiftOut(bytes, delay);
116+
}
117+
if (initC & 0x01) {
118+
bytes[0] = MAXREG_SCANLIMIT;
119+
bytes[1] = 0x07;
120+
shiftOut(bytes, delay);
121+
}
122+
if (initC & 0x02) {
123+
bytes[0] = MAXREG_DECODEMODE;
124+
if (decode > 0) {
125+
bytes[1] = 0xFF;
126+
decodeMode = 1;
127+
} else {
128+
bytes[1] = 0x0;
129+
decodeMode = 0;
130+
}
131+
shiftOut(bytes, delay);
132+
}
133+
if (initC & 0x08) {
134+
bytes[0] = MAXREG_DISPTEST;
135+
bytes[1] = 0x00;
136+
shiftOut(bytes, delay);
137+
}
138+
if (initC & 0x10) {
139+
bytes[0] = MAXREG_INTENSITY;
140+
bytes[1] = (unsigned char)val;
141+
shiftOut(bytes, delay);
142+
}
143+
if (initC & 0x20) {
144+
for (unsigned char i = 0; i < 8; i++) {
145+
bytes[0] = i + 1;
146+
bytes[1] = 0;
147+
shiftOut(bytes, delay);
148+
}
149+
}
150+
}
151+
for (unsigned char i = 0; i < digit; i++) {
152+
bytes[0] = 8-i; // i+ 1;
153+
if (decodeMode == 1) {
154+
bytes[1] = spiData[i];
155+
} else {
156+
bytes[1] = sendChar(spiData[i], false);
157+
}
158+
shiftOut(bytes, delay);
159+
}
160+
// spi_set_settings(1, &old); // restore saved settings
161+
}
162+
163+
// uint16_t retVal = spi_transfer_16(1, info);
164+
//
165+
// printf("rv %d ", retVal);
166+
//
167+
// return;
168+
void send2Byte(unsigned char reg, unsigned char data) {
169+
uint16_t info = reg*256+data;
170+
char i = 16;
171+
do {
172+
gpio_write(clk_pin, 0);
173+
if (info & 0x8000) {
174+
gpio_write(data_pin, 1);
175+
} else {
176+
gpio_write(data_pin, 0);
177+
}
178+
gpio_write(clk_pin, 1);
179+
info <<= 1;
180+
} while(--i);
181+
}
182+
183+
// check this page
184+
// http://www.instructables.com/id/MAX7219-8-Digit-LED-Display-Module-Driver-for-ESP8/step4/MAX7219-Driver-Implementation/
185+
// also
186+
// https://github.com/wayoda/LedControl/blob/master/src/LedControl.cpp
187+
188+
void shiftOut(unsigned char* data, int delay) {
189+
gpio_write(cs_pin, 0);
190+
send2Byte(data[0], data[1]);
191+
gpio_write(cs_pin, 1);
192+
vTaskDelay(delay);
193+
return;
194+
}
195+
196+
unsigned char sendChar(const char data, const bool dp) {
197+
unsigned char converted = 0b0000001; // hyphen as default
198+
// look up bit pattern if possible
199+
if (data >= ' ' && data <= 'z')
200+
converted = MAX7219_font[data - ' '];
201+
// 'or' in the decimal point if required
202+
if (dp)
203+
converted |= 0b10000000;
204+
return converted;
205+
}

0 commit comments

Comments
 (0)