]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/arm_atsam/d51_util.c
Keyboard: Added RGB toggle and cycle to default KDB6x mapping. (#4592)
[qmk_firmware.git] / tmk_core / protocol / arm_atsam / d51_util.c
1 #include "d51_util.h"
2
3 //Display unsigned 32-bit number through m15
4 //Read as follows: 1230 = || ||| |||| |  (note always ending toggle)
5 void m15_print(uint32_t x)
6 {
7     int8_t t;
8     uint32_t n;
9     uint32_t p, p2;
10
11     if      (x < 10) t = 0;
12     else if (x < 100) t = 1;
13     else if (x < 1000) t = 2;
14     else if (x < 10000) t = 3;
15     else if (x < 100000) t = 4;
16     else if (x < 1000000) t = 5;
17     else if (x < 10000000) t = 6;
18     else if (x < 100000000) t = 7;
19     else if (x < 1000000000) t = 8;
20     else t = 9;
21
22     while (t >= 0)
23     {
24         p2 = t;
25         p = 1;
26         while (p2--) p *= 10;
27         n = x / p;
28         x -= n * p;
29         while (n > 0)
30         {
31             m15_on;
32             n--;
33             m15_off;
34         }
35         //Will always end with an extra toggle
36         m15_on;
37         t--;
38         m15_off;
39     }
40 }
41
42 //Display unsigned 32-bit number through debug led
43 //Read as follows: 1230 = [*]  [* *]  [* * *]  [**]  (note zero is fast double flash)
44 #define DLED_ONTIME 1000000
45 #define DLED_PAUSE 1500000
46 volatile uint32_t w;
47 void dled_print(uint32_t x, uint8_t long_pause)
48 {
49     int8_t t;
50     uint32_t n;
51     uint32_t p, p2;
52
53     if      (x < 10) t = 0;
54     else if (x < 100) t = 1;
55     else if (x < 1000) t = 2;
56     else if (x < 10000) t = 3;
57     else if (x < 100000) t = 4;
58     else if (x < 1000000) t = 5;
59     else if (x < 10000000) t = 6;
60     else if (x < 100000000) t = 7;
61     else if (x < 1000000000) t = 8;
62     else t = 9;
63
64     while (t >= 0)
65     {
66         p2 = t;
67         p = 1;
68         while (p2--) p *= 10;
69         n = x / p;
70         x -= n * p;
71         if (!n)
72         {
73             led_on;
74             for (w = DLED_ONTIME / 4; w; w--);
75             led_off;
76             for (w = DLED_ONTIME / 4; w; w--);
77             led_on;
78             for (w = DLED_ONTIME / 4; w; w--);
79             led_off;
80             for (w = DLED_ONTIME / 4; w; w--);
81             n--;
82         }
83         else
84         {
85             while (n > 0)
86             {
87                 led_on;
88                 for (w = DLED_ONTIME; w; w--);
89                 led_off;
90                 for (w = DLED_ONTIME / 2; w; w--);
91                 n--;
92             }
93         }
94
95         for (w = DLED_PAUSE; w; w--);
96         t--;
97     }
98
99     if (long_pause)
100     {
101         for (w = DLED_PAUSE * 4; w; w--);
102     }
103 }
104
105 #ifdef DEBUG_BOOT_TRACING
106
107 volatile uint32_t debug_code;
108
109 void EIC_15_Handler()
110 {
111     //This is only for non-functional keyboard troubleshooting and should be disabled after boot
112     //Intention is to lock up the keyboard here with repeating debug led code
113     while (1)
114     {
115         dled_print(debug_code, 1);
116     }
117 }
118
119 void debug_code_init(void)
120 {
121     DBGC(DC_UNSET);
122
123     //Configure Ports for EIC on PB31
124     PORT->Group[1].DIRCLR.reg = 1 << 31; //Input
125     PORT->Group[1].OUTSET.reg = 1 << 31; //High
126     PORT->Group[1].PINCFG[31].bit.INEN = 1; //Input Enable
127     PORT->Group[1].PINCFG[31].bit.PULLEN = 1; //Pull Enable
128     PORT->Group[1].PINCFG[31].bit.PMUXEN = 1; //Mux Enable
129     PORT->Group[1].PMUX[15].bit.PMUXO = 0; //Mux A
130
131     //Enable CLK_EIC_APB
132     MCLK->APBAMASK.bit.EIC_ = 1;
133
134     //Configure EIC
135     EIC->CTRLA.bit.SWRST = 1;
136     while (EIC->SYNCBUSY.bit.SWRST) {}
137     EIC->ASYNCH.reg = 1 << 15;
138     EIC->INTENSET.reg = 1 << 15;
139     EIC->CONFIG[1].bit.SENSE7 = 2;
140     EIC->CTRLA.bit.ENABLE = 1;
141     while (EIC->SYNCBUSY.bit.ENABLE) {}
142
143     //Enable EIC IRQ
144     NVIC_EnableIRQ(EIC_15_IRQn);
145 }
146
147 void debug_code_disable(void)
148 {
149     //Disable EIC IRQ
150     NVIC_DisableIRQ(EIC_15_IRQn);
151
152     //Disable EIC
153     EIC->CTRLA.bit.ENABLE = 0;
154     while (EIC->SYNCBUSY.bit.ENABLE) {}
155
156     //Disable CLK_EIC_APB
157     MCLK->APBAMASK.bit.EIC_ = 0;
158 }
159
160 #else
161
162 void debug_code_init(void) {}
163 void debug_code_disable(void) {}
164
165 #endif //DEBUG_BOOT_TRACING