summaryrefslogtreecommitdiff
path: root/keyboards/miiiw/blackio83/blackio83.c
blob: 0d8412624e2db9b535adcd590fec99429c1b3574 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* Copyright 2023 ArthurCyy <https://github.com/ArthurCyy>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "blackio83.h"
#include "usb_main.h"
#include "usb_util.h"

#define LOOP_10HZ_PERIOD    100
deferred_token loop10hz_token  = INVALID_DEFERRED_TOKEN;
uint32_t loop_10Hz(uint32_t trigger_time, void *cb_arg);

static const SerialConfig mwproto_uart_config = {
    .speed = MWPROTO_BITRATE,
};

static void POWER_EnterSleep(void) {
    /* Clear Wake-up flag */
    PWR->CR |= PWR_CR_CWUF | PWR_CR_CSBF;
    /* Select Sleep mode */
    /* PWR->CR |= PWR_CR_PDDS | PWR_CR_LPDS; */
    /* Set SLEEPDEEP bit of Cortex System Control Register */
    SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
    /* Request Wait For Interrupt */
    __WFI();
    /* Reset SLEEPDEEP bit of Cortex System Control Register */
    SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
    NVIC_SystemReset();
}

extern void ws2812_poweron(void);
extern void ws2812_poweroff(void);

static bool p_setup = false;
static bool s_init = false;
void ws2812_poweron(void) {
    if(p_setup) return;
    p_setup = true;
    s_init = false;
    setPinOutput(RGB_EN_PIN);
    writePinHigh(RGB_EN_PIN);
}

void ws2812_poweroff(void) {
    if(!p_setup) return;
    p_setup = false;
    setPinInputLow(WS2812_DI_PIN);
    writePinLow(RGB_EN_PIN);
}

void keyboard_pre_init_kb() {
	keyboard_pre_init_user();

    setPinInputLow(MWPROTO_STATUS_PIN);
    setPinOutput(MWPROTO_WAKEUP_PIN);
    writePinLow(MWPROTO_WAKEUP_PIN);
    wait_ms(2);
    writePinHigh(MWPROTO_WAKEUP_PIN);

	palSetLineMode(MWPROTO_TX_PIN, PAL_MODE_ALTERNATE(MWPROTO_TX_PAL_MODE) | PAL_OUTPUT_TYPE_OPENDRAIN);
	sdStart(&MWPROTO_DRIVER, &mwproto_uart_config);
}

void keyboard_post_init_kb(void) {
    keyboard_post_init_user();

    print(/* clang-format off */
        "\n<--QMK Keyboard-->\n"
        "Vendor: " STR(MANUFACTURER) "(" STR(VENDOR_ID) ")\n"
        "Product: " STR(PRODUCT) " (" STR(PRODUCT_ID) ")\n"
        "Version: " STR(DEVICE_VER) "\n"
        "BUILD: " __DATE__ "\n"
    ); /* clang-format on */

    writePinLow(MWPROTO_WAKEUP_PIN);
    wait_ms(50);
    sdPutI(&MWPROTO_DRIVER, 0xA5);
    sdPutI(&MWPROTO_DRIVER, 0x12);
    sdPutI(&MWPROTO_DRIVER, 0x01);
    sdPutI(&MWPROTO_DRIVER, 0x02);
    sdPutI(&MWPROTO_DRIVER, 0xB4);
    writePinHigh(MWPROTO_WAKEUP_PIN);

    ws2812_poweron();
    loop10hz_token = defer_exec(LOOP_10HZ_PERIOD, loop_10Hz, NULL);
}

__attribute__((weak)) void shutdown_user(void) {
#ifdef RGB_MATRIX_ENABLE
    rgb_matrix_set_suspend_state(true);
#endif  // RGB_MATRIX_ENABLE
    wait_ms(10);
    ws2812_poweroff();
}

#ifdef DIP_SWITCH_ENABLE
bool dip_switch_update_mask_kb(uint32_t state) {
    if (!dip_switch_update_mask_user(state)) { return false; }
    
    if(state & 0x01) {
        led_suspend();
        usbDisconnectBus(&USB_DRIVER);
        usbStop(&USB_DRIVER);
        shutdown_user();
        setPinInputHigh(POWER_SWITCH_PIN);
        palEnableLineEvent(POWER_SWITCH_PIN, PAL_EVENT_MODE_RISING_EDGE);
        POWER_EnterSleep();
    }
    
    return true;
}
#endif

uint32_t loop_10Hz(uint32_t trigger_time, void *cb_arg) {
    
    if(last_input_activity_elapsed() > 1000) {
        static uint32_t pmu_timer = 0;
        if(timer_elapsed32(pmu_timer) > 3000) {
            pmu_timer = timer_read32();
            writePinLow(MWPROTO_WAKEUP_PIN);
            if(readPin(MWPROTO_STATUS_PIN))
                wait_us(500);
            else
                wait_us(1500);
            sdPutI(&MWPROTO_DRIVER, 0xA5);
            sdPutI(&MWPROTO_DRIVER, 0x28);
            sdPutI(&MWPROTO_DRIVER, 0x00);
            sdPutI(&MWPROTO_DRIVER, 0x8D);
            writePinHigh(MWPROTO_WAKEUP_PIN);
        }
    }

    extern matrix_row_t matrix[MATRIX_ROWS];
    static uint32_t restore_tick = 0;
    if(matrix[0] == 0x4000 && matrix[1] == 0 && 
       matrix[2] == 0 && matrix[3] == 0 && matrix[4] == 0 && matrix[5] == 0x201) {
        if(restore_tick++ > 50) {
            restore_tick = 0;
            writePinLow(MWPROTO_WAKEUP_PIN);
            if(readPin(MWPROTO_STATUS_PIN))
                wait_us(500);
            else
                wait_us(1500);
            sdPutI(&MWPROTO_DRIVER, 0xA5);
            sdPutI(&MWPROTO_DRIVER, 0x1F);
            sdPutI(&MWPROTO_DRIVER, 0x01);
            sdPutI(&MWPROTO_DRIVER, 0x0F);
            sdPutI(&MWPROTO_DRIVER, 0xB4);
            writePinHigh(MWPROTO_WAKEUP_PIN);
            wait_ms(50);
            eeconfig_init();
    #ifdef RGB_MATRIX_ENABLE
            extern void rgb_matrix_update_pwm_buffers(void);
            for(int i = 0; i < 5; i++) {
                rgb_matrix_set_color_all(RGB_WHITE);
                rgb_matrix_update_pwm_buffers();
                wait_ms(500);
                rgb_matrix_set_color_all(RGB_OFF);
                rgb_matrix_update_pwm_buffers();
                wait_ms(500);
            }
    #endif
            wait_ms(500);
            soft_reset_keyboard();
        }
    } else {
        restore_tick = 0;
    }

    static uint32_t debug_tick = 0;
	if (debug_tick++ > 9 ) {
        dprintf("trigger %d\n", trigger_time);
		debug_tick = 0;
	}

    return LOOP_10HZ_PERIOD;
}