]> git.gag.com Git - fw/altos/blob - src/samd21/ao_neopixel.c
altos/samd21: Automatically erase flash when writing
[fw/altos] / src / samd21 / ao_neopixel.c
1 /*
2  * Copyright © 2019 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #include <ao.h>
20 #include <ao_snek.h>
21
22 void
23 ao_snek_neopixel_write(void *gpio, uint8_t pin, int npixel, const struct snek_neopixel *pixels)
24 {
25         volatile uint32_t *outtgl = &(((struct samd21_port *) gpio)->outtgl);
26         uint32_t value = ((uint32_t) 1 << pin);
27
28         while (npixel--) {
29                 int32_t p = pixels->p;
30                 uint8_t bit;
31                 pixels++;
32
33                 ao_arch_block_interrupts();
34                 for (bit = 0; bit < 24; bit++) {
35                         *outtgl = value;
36                         ao_arch_nop();
37                         ao_arch_nop();
38                         ao_arch_nop();
39                         ao_arch_nop();
40                         ao_arch_nop();
41
42                         ao_arch_nop();
43                         ao_arch_nop();
44                         ao_arch_nop();
45                         if (p < 0) {
46                                 ao_arch_nop();
47                                 ao_arch_nop();
48                                 ao_arch_nop();
49                                 ao_arch_nop();
50                                 ao_arch_nop();
51
52                                 ao_arch_nop();
53                                 ao_arch_nop();
54                                 ao_arch_nop();
55                                 ao_arch_nop();
56                                 ao_arch_nop();
57
58                                 ao_arch_nop();
59                                 ao_arch_nop();
60                                 ao_arch_nop();
61                                 ao_arch_nop();
62                                 ao_arch_nop();
63
64                                 ao_arch_nop();
65                                 ao_arch_nop();
66                                 ao_arch_nop();
67
68                                 *outtgl = value;
69                         } else {
70                                 *outtgl = value;
71                                 ao_arch_nop();
72                                 ao_arch_nop();
73                                 ao_arch_nop();
74                                 ao_arch_nop();
75                                 ao_arch_nop();
76
77                                 ao_arch_nop();
78                                 ao_arch_nop();
79                                 ao_arch_nop();
80                                 ao_arch_nop();
81                                 ao_arch_nop();
82
83                                 ao_arch_nop();
84                                 ao_arch_nop();
85                                 ao_arch_nop();
86
87                         }
88                         ao_arch_nop();
89                         ao_arch_nop();
90                         ao_arch_nop();
91                         ao_arch_nop();
92                         ao_arch_nop();
93
94                         ao_arch_nop();
95
96                         p <<= 1;
97                 }
98                 ao_arch_release_interrupts();
99         }
100 }