1 /* (CC-BY-NC-SA) ROBIN KRENS - ROBIN @ ROBINKRENS.NL
4 * 2019/8/14 - ROBIN KRENS
8 * Driver for the TM1637 for STM32 boards.
9 * The TM1637 is (4 digit) 8 segment ledclock peripheral. Communication
10 * is similar to I2C, but not completely. There is no device address
11 * selecting. Instead, you can directly send a command / write data.
13 * Most libraries for this chip that I have seen use 'Bit banging'.
14 * Manually adjusting the clock and data pin (not using the I2C)
16 * This driver is different and uses STM32 I2C hardware. A problem of the
17 * TM1637 chip, however, is that commands sometimes set bit 1 (LSB). Setting
18 * the LSB will make the STM32 MCU switch to receiver mode. (A better design
19 * choice for commands would have to keep the LSB reserved).
21 * Another thing to note is that this chip uses BIG-ENDIANESS
30 #include <sys/robsys.h>
32 #include <lib/regfunc.h>
33 #include <lib/string.h>
34 #include <lib/tinyprintf.h>
36 #include <drivers/tm1637.h>
43 #define WRITE_CMD 0x20
45 /* STM32F1 microcontrollers do not provide the ability to pull-up SDA and SCL lines. Their
46 GPIOs must be configured as open-drain. So, you have to add two additional resistors to
47 pull-up I2C lines. Something between 4K and 10K is a proven value.
52 /* Program the peripheral input clock generate correct timings.
53 * Configure the clock control registers CCR
54 * Configure the rise time register TRIS
55 * Program the I2C_CR1 register to enable the peripheral
56 * Enable GPIOB6 and B7*/
58 rsetbit(RCC_APB1ENR, 21); // enable GPIOB
59 rsetbit(RCC_APB2ENR, 3); // enable I2C
60 rwrite(GPIOB_CRL, 0xEE444444); // open-drain
61 rsetbitsfrom(I2C_CR2, 0, 0x2); // 2 MHz
62 rwrite(I2C_TRISE, 0x3); // MAX = 1000ns, TPCLK1 = 500ns (+1)
63 rwrite(I2C_CCR, 0x000A); // standard mode
64 rsetbit(I2C_CR1, 0); // enable
68 /* Reset the I2C channels and reinitialize */
71 rsetbit(I2C_CR1, 15); // master is busy, reset
72 rsetbit(RCC_APB1RSTR, 21); // reset I2C
73 rwrite(RCC_APB1RSTR, 0x00000000); // clear reset
74 rsetbit(RCC_APB1ENR, 21);
75 rsetbitsfrom(I2C_CR2, 0, 0x2); // 2 MHz
76 rwrite(I2C_TRISE, 0x3); // MAX = 1000ns, TPCLK1 = 500ns (+1)
77 rwrite(I2C_CCR, 0x000A); // standard mode
78 rsetbit(I2C_CR1, 0); // enable
83 /* Write value to grid, if no offset is given it writes to the first
84 * grid. Grid 1 an 2 have an additional dot that can be set */
85 int set_grid(uint8_t offset, char value, bool dot) {
87 //int (*ack)() = ack_recv; /* Scary function pointer :D */
90 printf("Offset incorrect");
93 // enable dot on display if desired
94 if (dot && (offset == 1 || offset == 2)) {
98 int start_pos_cmd = 0x03 | (offset & 0x01) << 7 | (offset & 0x2) << 5 ;
100 /* Initiate writing routine */
102 rwrite(I2C_DR, WRITE_CMD);
111 /* Set GRID offset */
113 rwrite(I2C_DR, start_pos_cmd);
121 /* Write value to segments */
123 rwrite(I2C_DR, value);
133 /* Turns on/off the led display. The degree of brightness can set [0..7]*/
134 int set_display(bool on, uint8_t degree) {
136 int disp_cmd = 0x1; // off
138 disp_cmd = 0xF1; // TODO
142 rwrite(I2C_DR, disp_cmd);
144 printf("Can't switch on display!");
156 void tm1637_example() {
158 unsigned char display_number[10] = {0xFC, 0x60, 0xDA, 0xF2, 0x66, 0xB6, 0xBE, 0xE0, 0xFE, 0xF6};
161 char love[4] = { 0x1C, 0xFC, 0x7C, 0x9E };
163 for (int i = 0; i < 4; i++) {
164 set_grid(i, love[i], NODOT);
167 set_display(true, 0);
171 /* HELPER ROUTINES */
173 static void start_condition() {
174 rsetbit(I2C_CR1, 8); //start bit
177 static void stop_condition() {
178 rsetbit(I2C_CR1, 9); //stop bit
181 /* Wait for an acknowledge from the peripheral */
184 while(!(*I2C_SR1 & 0x2)) {
187 printf("Error: no ack (addr bit) received \n");
191 uint32_t a = *I2C_SR2; // need to read SR2 register!
196 /* Similar, but SR2 register is not read */
199 while(!(*I2C_SR1 & 0x8)) {
202 printf("Error: no ack (addr10 bit) received \n");
210 /* Check if lines are idle (i.e. stop condition finished)*/
213 while(*I2C_SR2 & 0x2) {
216 printf("Error: busy state\n");