tm1637 function routine rewrite
[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 int ack_recv() {
123
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;
131         return 1;
132
133 }
134
135 int ack10_recv() {
136
137         int cnt = 0;
138         while(!(*I2C_SR1 & 0x8)) {
139                 cnt++;
140                 if (cnt > TIMEOUT)
141                         return 0;
142         }
143         //uint32_t a = *I2C_SR2;
144         return 1;
145
146 }
147
148 int idle() {
149         int cnt = 0;
150         while(*I2C_SR2 & 0x2) {
151                 cnt++;
152                 if (cnt > TIMEOUT)
153                         return 0;
154         }
155
156         return 1;
157 }
158
159 int delay() {
160
161         int a = 0;
162         for (int i = 0; i < TIMEOUT; i++)
163                 a++;
164 }
165
166 int delay2() {
167
168         int a = 0;
169         for (int i = 0; i < (TIMEOUT * 150 ); i++)
170                 a++;
171 }
172
173 void set_display(bool on, uint8_t degree) {
174
175         start_condition();
176
177         regw_u32(I2C_DR, 0xF1, 0, OWRITE);
178         if(!ack_recv())
179                 cputs("Can't switch on display!");
180         stop_condition();
181
182         // reset bus
183         regw_u32(I2C_CR1, 0x1, 15, SETBIT);
184
185 }
186
187
188 void set_segment(int offset, char value, bool dot) {
189
190 //      int (ack_recv*)(void) = &ack_recv;
191
192 //      if (value & 0x80)
193 //      ack_recv = &ack_recv;
194
195         if (offset > 3) {
196                 cputs("Offset incorrect");
197         }
198
199         if (dot) {
200                 value = value | 0x1;
201         }
202         int start_pos_cmd  = 0x03  | (offset & 0x01) << 7 | (offset & 0x2) << 5 ;
203
204         start_condition();
205         regw_u32(I2C_DR, 0x20, 0, OWRITE); 
206         if(!ack_recv())
207                 cputs("Error: initiating write for start segment \n");
208
209         stop_condition();
210         
211         if(!idle())
212                 cputs("Error: timeout");
213
214         start_condition();
215         regw_u32(I2C_DR, start_pos_cmd, 0, OWRITE); 
216         if(!ack_recv())
217                 cputs("Error: Can't set start segment \n");
218
219         stop_condition();
220         regw_u32(I2C_CR1, 0x1, 15, SETBIT);
221
222         tm1637_reset();
223
224         start_condition();
225         regw_u32(I2C_DR, value, 0, OWRITE); // use ack10 if higher
226         if(!ack10_recv())
227                 cputs("Error: can't set location\n");
228         stop_condition(); 
229
230
231         regw_u32(I2C_CR1, 0x1, 15, SETBIT);
232         //      if(!idle())
233         //      cputs("Error: timeout");
234
235
236         tm1637_reset();
237 }
238
239
240
241 void tm1637_start() {
242
243 unsigned char display_number[10] = {0xFC, 0x60, 0xDA, 0xF2, 0x66, 0xB6, 0xBE, 0xE0, 0xFE, 0xF6};
244
245
246         char love[4]  = { 0x1C, 0xFC, 0x7C, 0x9E };
247         
248         for (int i = 0; i < 4; i++) {
249                 set_segment(i, love[i], NODOT);
250         }
251
252         set_display(true, 0);
253
254 }
255
256