aboutsummaryrefslogtreecommitdiff
path: root/src/tm1637.c
blob: 4e6e68c6e14bc0f404c4568859db9062a0baa1d1 (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
#include "main.h"
#include "tm1637.h"
#include "stm32f091xc.h"

void _tm1637_micro_delay() {
	timer_delay(10);
	// for(unsigned int x = 0; x < 10e4; x++);
}

/** @brief reset gpio mode registers for dio */
void _tm1637_GPIOMODE_reset() {
	GPIOA->MODER &= ~(0b11 << (PINOUT_DISP_DIO * 2));
	GPIOA->PUPDR &= ~(0b11 << (PINOUT_DISP_DIO * 2));
	GPIOA->OTYPER &= ~(0b1 << PINOUT_DISP_DIO);
}

/** @brief set dio to general-purpose output mode */
void _tm1637_GPIOMODE_gp_output() {
	_tm1637_GPIOMODE_reset();

	GPIOA->MODER |= (0b01 << (PINOUT_DISP_DIO * 2));
}

/** @brief set dio to open drain input mode */
void _tm1637_GPIOMODE_open_drain() {
	_tm1637_GPIOMODE_reset();

	GPIOA->MODER |= (0b10 << (PINOUT_DISP_DIO * 2));
	GPIOA->OTYPER |= (0b1 << PINOUT_DISP_DIO);
}

/**
 * @brief gpio write abstraction function
 * @param pin pin to write
 * @param state state to set `pin` to
 */
void _tm1637_pin_write(uint32_t pin, bool state) {
	GPIOA->ODR ^= (((GPIOA->ODR & (1 << pin)) >> pin) ^ state) << pin;

	if (pin == PINOUT_DISP_CLK) led_write(0, state);
	if (pin == PINOUT_DISP_DIO) led_write(1, state);
}

void tm1637_begin() {
	_tm1637_GPIOMODE_gp_output();

	// general purpose output mode for clk
	GPIOA->MODER &= ~(0b11 << (PINOUT_DISP_CLK * 2));
	GPIOA->MODER |=  (0b01 << (PINOUT_DISP_CLK * 2));

	_tm1637_pin_write(PINOUT_DISP_CLK, 1);
	_tm1637_pin_write(PINOUT_DISP_DIO, 1);

	_tm1637_micro_delay();
}

void _tm1637_start() {
	_tm1637_pin_write(PINOUT_DISP_DIO, 0);
	_tm1637_micro_delay();
}

void _tm1637_stop() {
	_tm1637_pin_write(PINOUT_DISP_CLK, 1);
	_tm1637_pin_write(PINOUT_DISP_DIO, 1);
	_tm1637_micro_delay();
}

bool _tm1637_ack() {
	_tm1637_pin_write(PINOUT_DISP_CLK, 0);
	_tm1637_pin_write(PINOUT_DISP_DIO, 0);
	_tm1637_GPIOMODE_open_drain();
	_tm1637_micro_delay();
	_tm1637_pin_write(PINOUT_DISP_CLK, 1);
	_tm1637_micro_delay();
	_tm1637_pin_write(PINOUT_DISP_CLK, 0);
	_tm1637_GPIOMODE_gp_output();
	_tm1637_micro_delay();
	return true;
}

/**
 * @brief send data to TM1637
 * @param command command to send
 * @return response data as TM1637Response struct
 */
TM1637Sequence _tm1637_send(TM1637Sequence command) {
	TM1637Sequence response = { .data = 0, .length = 0 };

	_tm1637_start();

	for (uint32_t byte = 0; byte < command.length; byte++) {
		for (uint8_t bit = 0; bit < 8; bit++) {
			uint8_t rev_bit = 7 - bit;
			_tm1637_pin_write(PINOUT_DISP_CLK, 0);
			_tm1637_pin_write(PINOUT_DISP_DIO, (command.data[byte] & (1 << rev_bit)) >> rev_bit);
			_tm1637_micro_delay();
			_tm1637_pin_write(PINOUT_DISP_CLK, 1);
			_tm1637_micro_delay();
		}
		//TODO: confirm ack
		/*bool ack =*/ _tm1637_ack();
	}

	_tm1637_micro_delay();

	// stop condition
	_tm1637_stop();

	return response;
}