Debian Codegraph @

ports/stm32/boards/LEGO_HUB_NO7/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 * Copyright (c) 2022 David Lechner <[email protected]>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 */
27
28 #include "py/mphal.h"
29 #include "hub_display.h"
30
31 #include STM32_HAL_H
32
33 #define I2C_ADDR 0x28
34
35 // Registers
36 #define DEVICE_CONFIG0 0x00
37 #define DEVICE_CONFIG1 0x01
38 #define LED_CONFIG0 0x02
39 #define BANK_BRIGHTNESS 0x03
40 #define BANK_A_COLOR 0x04
41 #define BANK_B_COLOR 0x05
42 #define BANK_C_COLOR 0x06
43 #define LED0_BRIGHTNESS 0x07
44 #define LED1_BRIGHTNESS 0x08
45 #define LED2_BRIGHTNESS 0x09
46 #define LED3_BRIGHTNESS 0x0A
47 #define OUT0_COLOR 0x0B
48 #define OUT1_COLOR 0x0C
49 #define OUT2_COLOR 0x0D
50 #define OUT3_COLOR 0x0E
51 #define OUT4_COLOR 0x0F
52 #define OUT5_COLOR 0x10
53 #define OUT6_COLOR 0x11
54 #define OUT7_COLOR 0x12
55 #define OUT8_COLOR 0x13
56 #define OUT9_COLOR 0x14
57 #define OUT10_COLOR 0x15
58 #define OUT11_COLOR 0x16
59 #define RESET 0x17
60
61 // Flags
62 #define DEVICE_CONFIG0_CHIP_EN (1 << 6)
63 #define DEVICE_CONFIG1_LOG_SCALE_EN (1 << 5)
64 #define DEVICE_CONFIG1_POWER_SAVE_EN (1 << 4)
65 #define DEVICE_CONFIG1_AUTO_INCR_EN (1 << 3)
66 #define DEVICE_CONFIG1_PWM_DITHERING_EN (1 << 2)
67 #define DEVICE_CONFIG1_MAX_CURRENT_OPTION (1 << 1)
68 #define DEVICE_CONFIG1_LED_GLOBAL_OFF (1 << 0)
69 #define LED_CONFIG0_LED3_BANK_EN (1 << 3)
70 #define LED_CONFIG0_LED2_BANK_EN (1 << 2)
71 #define LED_CONFIG0_LED1_BANK_EN (1 << 1)
72 #define LED_CONFIG0_LED0_BANK_EN (1 << 0)
73
74 #define LP50XX_NUM_CH 6
75
76 // channel mapping:
77 // CH 0 = battery LED - red
78 // CH 1 = battery LED - green
79 // CH 2 = battery LED - blue
80 // CH 3 = status LED - red
81 // CH 4 = status LED - green
82 // CH 5 = status LED - blue
83
84 #define FMPI2C_CONVERT_TIMINGS(PRESC, SCLDEL, SDADEL, SCLH, SCLL) \
85 (((PRESC) << FMPI2C_TIMINGR_PRESC_Pos) | \
86 ((SCLDEL) << FMPI2C_TIMINGR_SCLDEL_Pos) | \
87 ((SDADEL) << FMPI2C_TIMINGR_SDADEL_Pos) | \
88 ((SCLH) << FMPI2C_TIMINGR_SCLH_Pos) | \
89 ((SCLL) << FMPI2C_TIMINGR_SCLL_Pos))
90
91 static FMPI2C_HandleTypeDef hub_display_i2c;
92 static bool hub_display_init;
93
94 static struct {
95 uint8_t reg;
96 uint8_t values[LP50XX_NUM_CH];
97 } __attribute__((packed)) hub_display_data = {
98 .reg = OUT0_COLOR,
99 };
100
101 void HAL_FMPI2C_MspInit(FMPI2C_HandleTypeDef *hfmpi2c) {
102 __HAL_RCC_FMPI2C1_CLK_ENABLE();
103 mp_hal_pin_config(pyb_pin_LED_SCL, MP_HAL_PIN_MODE_ALT_OPEN_DRAIN, MP_HAL_PIN_PULL_NONE, 4);
104 mp_hal_pin_config(pyb_pin_LED_SDA, MP_HAL_PIN_MODE_ALT_OPEN_DRAIN, MP_HAL_PIN_PULL_NONE, 4);
105 }
106
107 void HAL_FMPI2C_MspDeInit(FMPI2C_HandleTypeDef *hfmpi2c) {
108 __HAL_RCC_FMPI2C1_CLK_DISABLE();
109 __HAL_RCC_FMPI2C1_FORCE_RESET();
110 __HAL_RCC_FMPI2C1_RELEASE_RESET();
111 mp_hal_pin_config(pyb_pin_LED_SCL, MP_HAL_PIN_MODE_ANALOG, MP_HAL_PIN_PULL_NONE, 0);
112 mp_hal_pin_config(pyb_pin_LED_SDA, MP_HAL_PIN_MODE_ANALOG, MP_HAL_PIN_PULL_NONE, 0);
113 }
114
115 static void hub_display_i2c_init(void) {
116 hub_display_i2c.Instance = FMPI2C1;
117 hub_display_i2c.Init.Timing = FMPI2C_CONVERT_TIMINGS(0, 4, 0, 19, 28);
118 hub_display_i2c.Init.OwnAddress1 = 0;
119 hub_display_i2c.Init.AddressingMode = FMPI2C_ADDRESSINGMODE_7BIT;
120 hub_display_i2c.Init.DualAddressMode = FMPI2C_DUALADDRESS_DISABLE;
121 hub_display_i2c.Init.OwnAddress2 = 0;
122 hub_display_i2c.Init.OwnAddress2Masks = FMPI2C_OA2_NOMASK;
123 hub_display_i2c.Init.GeneralCallMode = FMPI2C_GENERALCALL_DISABLE;
124 hub_display_i2c.Init.NoStretchMode = FMPI2C_NOSTRETCH_DISABLE;
125 HAL_FMPI2C_Init(&hub_display_i2c);
126 }
127
128 void hub_display_set(uint8_t led, uint16_t value) {
129 if (led >= LP50XX_NUM_CH) {
130 return;
131 }
132
133 hub_display_data.values[led] = value >> 8;
134 }
135
136 void hub_display_update(void) {
137 if (!hub_display_init) {
138 return;
139 }
140
141 HAL_FMPI2C_Master_Transmit(&hub_display_i2c, I2C_ADDR, (uint8_t *)&hub_display_data,
142 sizeof(hub_display_data), HAL_MAX_DELAY);
143 }
144
145 void hub_display_on(void) {
146 if (hub_display_init) {
147 return;
148 }
149
150 hub_display_i2c_init();
151 mp_hal_pin_output(pyb_pin_LED_EN);
152 mp_hal_pin_high(pyb_pin_LED_EN);
153
154 static const struct {
155 uint8_t reg;
156 uint8_t values[11];
157 } __attribute__((packed)) init_data = {
158 .reg = DEVICE_CONFIG0,
159 .values = {
160 [DEVICE_CONFIG0] = DEVICE_CONFIG0_CHIP_EN,
161 [DEVICE_CONFIG1] = DEVICE_CONFIG1_POWER_SAVE_EN | DEVICE_CONFIG1_PWM_DITHERING_EN | DEVICE_CONFIG1_AUTO_INCR_EN,
162 [LED_CONFIG0] = 0,
163 [BANK_BRIGHTNESS] = 0,
164 [BANK_A_COLOR] = 0,
165 [BANK_B_COLOR] = 0,
166 [BANK_C_COLOR] = 0,
167 [LED0_BRIGHTNESS] = 51, // battery LED
168 [LED1_BRIGHTNESS] = 38, // status LED
169 [LED2_BRIGHTNESS] = 0,
170 [LED3_BRIGHTNESS] = 0,
171 }
172 };
173
174 HAL_FMPI2C_Master_Transmit(&hub_display_i2c, I2C_ADDR, (uint8_t *)&init_data,
175 sizeof(init_data), HAL_MAX_DELAY);
176
177 hub_display_init = true;
178 }
179
180 void hub_display_off(void) {
181 if (!hub_display_init) {
182 return;
183 }
184
185 HAL_FMPI2C_DeInit(&hub_display_i2c);
186
187 mp_hal_pin_config(pyb_pin_LED_EN, MP_HAL_PIN_MODE_ANALOG, MP_HAL_PIN_PULL_NONE, 0);
188
189 hub_display_init = false;
190 }