acknowledges for send bits
[cortex-from-scratch] / drivers / tm1637.c
1 /* (CC-BY-NC-SA) ROBIN KRENS - ROBIN @ ROBINKRENS.NL
2  * 
3  * $LOG$
4  * 2019/7/25 - ROBIN KRENS      
5  * Initial version 
6  * 
7  * $DESCRIPTION$
8  * Basic driver for the TM1637. The TM1637 is 8 segment
9  * ledclock peripheral. Communication is similar to I2C,
10  * but not completely. There is no address selecting.
11  *
12  * Alternative I2C protocol : (W)rite or (R)ead
13  * | Start | ADDRESS | W | ACK/NACK | DATA | ACK/NACK | (d+a*n) | St
14  * | Start | ADDRESS | R | ACK/NACK | DATA | ACK/NACK | (d+a*n) | St
15  *
16  * */
17
18 #include <stdbool.h>
19 #include <stddef.h>
20 #include <stdint.h>
21
22 #include <sys/mmap.h>
23 #include <sys/robsys.h>
24
25 #include <lib/regfunc.h>
26 #include <lib/string.h>
27 #include <lib/stdio.h>
28
29 #include <drivers/tm1637.h>
30
31 #define TIMEOUT 1000
32
33 #define DOT true
34 #define NODOT false
35
36
37 /* 
38 0 = 0x00
39 1 = 0x60
40 2 = 0xDA
41 3 = 0xF2
42 4 = 0x66
43 5 = 0xB6
44 6 = 0xBE
45 7 = 0xE0
46 8 = 0xFF
47 9 = 0xF6
48 */
49
50 /* STM32F1 microcontrollers do not provide the ability to pull-up SDA and SCL lines. Their
51 GPIOs must be configured as open-drain. So, you have to add two additional resistors to
52 pull-up I2C lines. Something between 4K and 10K is a proven value.
53 */
54
55 /* BIG ENDIAN! */
56
57 void tm1637_init() {
58
59  /* Program the peripheral input clock in I2C_CR2 Register in order to generate correct timings
60  Configure the clock control registers CCR
61  Configure the rise time register TRIS
62  Program the I2C_CR1 register to enable the peripheral
63
64  ENABLE GPIOB6 and B7*/
65
66  regw_u32(RCC_APB1ENR, 0x1, 21, SETBIT);
67  regw_u32(RCC_APB2ENR, 0x1, 3, SETBIT);
68  // //regw_u8(AFIO_EVCR, 0x89, 0, SETBIT);// set event control register, output on ?
69  
70  regw_u32(GPIOB_CRL, 0xEE444444, 0, OWRITE);
71
72  regw_u32(I2C_CR2, 0x2, 0, OWRITE); //2 MHz 
73  regw_u8(I2C_TRISE, 0x3, 0, OWRITE); // MAX = 1000ns, TPCLK1 = 500ns (+1)
74  regw_u32(I2C_CCR, 0x000A, 0, OWRITE); // standard mode, output 100 kHz (100hz* / perip)
75  
76  regw_u32(I2C_CR1, 0x1, 0, OWRITE); // enable
77
78 }
79
80 void tm1637_reset() {
81
82  regw_u32(RCC_APB1RSTR, 0x1, 21, SETBIT);
83
84  regw_u32(RCC_APB1RSTR, 0x00000000, 0, OWRITE); // clr
85  //regw_u32(RCC_APB2ENR, 0x1, 3, SETBIT);
86  // //regw_u8(AFIO_EVCR, 0x89, 0, SETBIT);// set event control register, output on ?
87  
88  regw_u32(RCC_APB1ENR, 0x1, 21, SETBIT);
89  //regw_u32(GPIOB_CRL, 0xEE444444, 0, OWRITE);
90
91  regw_u32(I2C_CR2, 0x2, 0, OWRITE); //2 MHz 
92  regw_u8(I2C_TRISE, 0x3, 0, OWRITE); // MAX = 1000ns, TPCLK1 = 500ns (+1)
93  regw_u32(I2C_CCR, 0x000A, 0, OWRITE); // standard mode, output 100 kHz (100hz* / perip)
94
95  regw_u32(I2C_CR1, 0x1, 0, OWRITE); // enable
96
97
98 }
99
100 static void start_condition() {
101
102         regw_u32(I2C_CR1, 0x1, 8, SETBIT); //start
103
104 }
105
106 static void stop_condition() {
107
108         regw_u32(I2C_CR1, 0x1, 9, SETBIT); //stop
109 }
110
111 static int buf_empty() {
112         int cnt = 0;
113         while(!(*I2C_SR1 & 0x80)) {
114                 cnt++;
115                 if (cnt > TIMEOUT) {
116                         return 0;
117                 }
118         }
119         return 1;
120 }
121
122 /* Wait for an acknowledge from the peripheral */
123 int ack_recv() {
124         int cnt = 0;
125         while(!(*I2C_SR1 & 0x2)) {
126                 cnt++;
127                 if (cnt > TIMEOUT)
128                         return 0;
129         }
130         uint32_t a = *I2C_SR2; // need to read SR2 register!
131         return 1;
132
133 }
134
135 /* Similar, but SR2 register is not read */
136 int ack10_recv() {
137         int cnt = 0;
138         while(!(*I2C_SR1 & 0x8)) {
139                 cnt++;
140                 if (cnt > TIMEOUT)
141                         return 0;
142         }
143         return 1;
144
145 }
146
147 int idle() {
148         int cnt = 0;
149         while(*I2C_SR2 & 0x2) {
150                 cnt++;
151                 if (cnt > TIMEOUT)
152                         return 0;
153         }
154
155         return 1;
156 }
157
158 int delay() {
159
160         int a = 0;
161         for (int i = 0; i < TIMEOUT; i++)
162                 a++;
163 }
164
165 void set_display(bool on, uint8_t degree) {
166
167         start_condition();
168
169         regw_u32(I2C_DR, 0xF1, 0, OWRITE);
170         if(!ack_recv())
171                 cputs("Can't switch on display!");
172         stop_condition();
173
174         // reset bus
175         regw_u32(I2C_CR1, 0x1, 15, SETBIT);
176
177 }
178
179
180 void set_segment(int offset, char value, bool dot) {
181
182         int (*ack)() = ack_recv; /* Scary function pointer :D */
183
184
185         if (offset > 3) {
186                 cputs("Offset incorrect");
187         }
188
189         if (dot) {
190                 value = value | 0x1;
191         }
192         int start_pos_cmd  = 0x03  | (offset & 0x01) << 7 | (offset & 0x2) << 5 ;
193
194         start_condition();
195         regw_u32(I2C_DR, 0x20, 0, OWRITE); 
196         if(!ack_recv())
197                 cputs("Error: initiating write for start segment \n");
198
199         stop_condition();
200         
201         if(!idle())
202                 cputs("Error: timeout");
203
204
205         start_condition();
206         regw_u32(I2C_DR, start_pos_cmd, 0, OWRITE); 
207         if(!ack())
208                 cputs("Error: Can't set start segment \n");
209
210         stop_condition();
211         regw_u32(I2C_CR1, 0x1, 15, SETBIT);
212
213         tm1637_reset();
214
215 //      if (value & 0xF0)
216 //              ack = &ack10_recv;
217
218         start_condition();
219         regw_u32(I2C_DR, value, 0, OWRITE); // use ack10 if higher
220         if(!ack_recv())
221                 cputs("Error: can't set location\n");
222         stop_condition(); 
223
224
225         regw_u32(I2C_CR1, 0x1, 15, SETBIT);
226
227         tm1637_reset();
228 }
229
230
231
232 void tm1637_start() {
233
234 unsigned char display_number[10] = {0xFC, 0x60, 0xDA, 0xF2, 0x66, 0xB6, 0xBE, 0xE0, 0xFE, 0xF6};
235
236
237         char love[4]  = { 0x1C, 0xFC, 0x7C, 0x9E };
238         
239         for (int i = 0; i < 4; i++) {
240                 set_segment(i, love[i], NODOT);
241         }
242
243         set_display(true, 0);
244
245 }
246
247