Debian Codegraph @

ports/stm32/boards/LEGO_HUB_NO6/hub_display.c

raw
1 /*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2021 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
27 #include "py/mphal.h"
28 #include "hub_display.h"
29
30 // Map pixel number 0-24, and 25-36 to TLC bit number.
31 static const uint8_t hub_display_pixel_map[] = {
32 // 5x5 display
33 9, 11, 6, 1, 14,
34 10, 19, 8, 0, 26,
35 23, 18, 3, 2, 24,
36 21, 20, 15, 13, 25,
37 22, 7, 17, 12, 38,
38 // RGB Bluetooth button
39 27, 28, 29,
40 // RGB middle button (left and right)
41 39, 40, 41, 42, 43, 44,
42 // RGB battery indicator
43 45, 46, 47,
44 };
45
46 static bool hub_display_init;
47 static uint8_t hub_display_gs_state[96];
48
49 static void hub_display_tim_init(void) {
50 // TLC maximum GSCLK freq: 33MHz
51
52 // tim12, ch2, pwm
53
54 TIM_TypeDef *tim = TIM12;
55
56 __HAL_RCC_TIM12_CLK_ENABLE();
57 tim->CR1 = TIM_CR1_ARPE;
58
59 // Configure PWM mode.
60 uint32_t ch = 1; // ch2
61 uint32_t reg = 6 << TIM_CCMR1_OC1M_Pos // PWM1 mode
62 | 1 << TIM_CCMR1_OC1PE_Pos // preload enabled
63 | 0 << TIM_CCMR1_CC1S_Pos // output mode
64 ;
65 uint32_t shift = 8 * (ch & 1);
66 tim->CCMR1 = (tim->CCMR1 & ~(0xff << shift)) | reg << shift;
67
68 // Enable output on pin, active high for normal channel.
69 reg = TIM_CCER_CC1E;
70 shift = 4 * ch;
71 tim->CCER = (tim->CCER & ~(0xf << shift)) | reg << shift;
72
73 // Enable the timer if it's not already running.
74 tim->CR1 |= TIM_CR1_CEN;
75
76 // fast
77 tim->PSC = 0;
78 tim->ARR = 2;
79
80 // 50% duty
81 tim->CCR2 = 2;
82 tim->EGR = 1; // UG
83
84 mp_hal_pin_config(pyb_pin_TLC_GS_CLK, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, 9);
85 }
86
87 static void hub_display_spi_init(void) {
88 // TLC maximum SPI freq: 25MHz
89
90 SPI_TypeDef *spi = SPI1;
91
92 __HAL_RCC_SPI1_CLK_ENABLE();
93 spi->CR1 = SPI_CR1_SSM | SPI_CR1_SSI | 0 << SPI_CR1_BR_Pos | SPI_CR1_MSTR;
94 spi->CR1 |= SPI_CR1_SPE;
95
96 mp_hal_pin_config(pyb_pin_TLC_SCLK, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, 5);
97 mp_hal_pin_config(pyb_pin_TLC_SOUT, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, 5);
98 mp_hal_pin_config(pyb_pin_TLC_SIN, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, 5);
99 }
100
101 static void hub_display_spi_write(uint8_t value) {
102 SPI_TypeDef *spi = SPI1;
103 spi->DR = value;
104 while (!(spi->SR & SPI_SR_TXE)) {
105 }
106 }
107
108 // dc: dot control
109 // mc: maximum current
110 // bc: global brightness control
111 // fc: function control
112 static void hub_display_latch_ctrl(uint8_t dc, uint32_t mc, uint32_t bc, uint8_t fc) {
113 hub_display_spi_write(1); // bit 768
114 hub_display_spi_write(0x96); // bits 760-767
115 for (int i = 0; i < 48; ++i) {
116 hub_display_spi_write(0);
117 }
118 hub_display_spi_write(fc >> 2); // bits 368-375
119 hub_display_spi_write(fc << 6 | bc >> 15); // bits 360-367
120 hub_display_spi_write(bc >> 7); // bits 352-359
121 hub_display_spi_write(bc << 1 | mc >> 8); // bits 344-351
122 hub_display_spi_write(mc); // bits 336-343
123 for (int i = 0; i < 42; ++i) {
124 hub_display_spi_write(dc);
125 }
126 mp_hal_pin_high(pyb_pin_TLC_LAT);
127 mp_hal_delay_us(1);
128 mp_hal_pin_low(pyb_pin_TLC_LAT);
129 }
130
131 void hub_display_set(uint8_t led, uint16_t value) {
132 led = hub_display_pixel_map[led];
133 hub_display_gs_state[led * 2] = value;
134 hub_display_gs_state[led * 2 + 1] = value >> 8;
135 }
136
137 void hub_display_update(void) {
138 if (!hub_display_init) {
139 return;
140 }
141 hub_display_spi_write(0);
142 for (int i = 0; i < 96; ++i) {
143 hub_display_spi_write(hub_display_gs_state[95 - i]);
144 }
145 mp_hal_pin_high(pyb_pin_TLC_LAT);
146 mp_hal_delay_us(1);
147 mp_hal_pin_low(pyb_pin_TLC_LAT);
148 }
149
150 void hub_display_on(void) {
151 if (hub_display_init) {
152 return;
153 }
154 mp_hal_pin_output(pyb_pin_TLC_LAT);
155 mp_hal_pin_low(pyb_pin_TLC_LAT);
156 hub_display_spi_init();
157 for (int i = 0; i < 2; ++i) {
158 hub_display_latch_ctrl(0xff, 0, 0x1fffff, 0x11);
159 }
160 hub_display_tim_init();
161 hub_display_init = true;
162 }
163
164 void hub_display_off(void) {
165 if (!hub_display_init) {
166 return;
167 }
168 __HAL_RCC_TIM12_CLK_DISABLE();
169 __HAL_RCC_TIM12_FORCE_RESET();
170 __HAL_RCC_TIM12_RELEASE_RESET();
171 __HAL_RCC_SPI1_CLK_DISABLE();
172 __HAL_RCC_SPI1_FORCE_RESET();
173 __HAL_RCC_SPI1_RELEASE_RESET();
174 mp_hal_pin_config(pyb_pin_TLC_SCLK, MP_HAL_PIN_MODE_ANALOG, MP_HAL_PIN_PULL_NONE, 0);
175 mp_hal_pin_config(pyb_pin_TLC_SOUT, MP_HAL_PIN_MODE_ANALOG, MP_HAL_PIN_PULL_NONE, 0);
176 mp_hal_pin_config(pyb_pin_TLC_SIN, MP_HAL_PIN_MODE_ANALOG, MP_HAL_PIN_PULL_NONE, 0);
177 mp_hal_pin_config(pyb_pin_TLC_LAT, MP_HAL_PIN_MODE_ANALOG, MP_HAL_PIN_PULL_NONE, 0);
178 mp_hal_pin_config(pyb_pin_TLC_GS_CLK, MP_HAL_PIN_MODE_ANALOG, MP_HAL_PIN_PULL_NONE, 0);
179 hub_display_init = false;
180 }