1 // Copyright 2016 Emilie Gillet.
2 //
3 // Author: Emilie Gillet (emilie.o.gillet@gmail.com)
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
22 //
23 // See http://creativecommons.org/licenses/MIT/ for more information.
24 //
25 // -----------------------------------------------------------------------------
26 //
27 // Drivers for the column of LEDs.
28
29 #include "plaits/drivers/leds.h"
30
31 #include <algorithm>
32
33 #include <stm32f37x_conf.h>
34
35 namespace plaits {
36
37 using namespace std;
38
39 const uint16_t kPinEnable = GPIO_Pin_7;
40
Init()41 void Leds::Init() {
42 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOF, ENABLE);
43 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
44 RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
45
46 GPIO_InitTypeDef gpio_init;
47
48 // SS
49 gpio_init.GPIO_Mode = GPIO_Mode_OUT;
50 gpio_init.GPIO_OType = GPIO_OType_PP;
51 gpio_init.GPIO_Speed = GPIO_Speed_2MHz;
52 gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL;
53 gpio_init.GPIO_Pin = kPinEnable;
54 GPIO_Init(GPIOF, &gpio_init);
55
56 // MOSI
57 gpio_init.GPIO_Mode = GPIO_Mode_AF;
58 gpio_init.GPIO_OType = GPIO_OType_PP;
59 gpio_init.GPIO_Speed = GPIO_Speed_2MHz;
60 gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL;
61 gpio_init.GPIO_Pin = GPIO_Pin_6;
62 GPIO_Init(GPIOF, &gpio_init);
63 GPIO_PinAFConfig(GPIOF, GPIO_PinSource6, GPIO_AF_5);
64
65 // SCK
66 gpio_init.GPIO_Mode = GPIO_Mode_AF;
67 gpio_init.GPIO_OType = GPIO_OType_PP;
68 gpio_init.GPIO_Speed = GPIO_Speed_2MHz;
69 gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL;
70 gpio_init.GPIO_Pin = GPIO_Pin_12;
71 GPIO_Init(GPIOA, &gpio_init);
72 GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_6);
73
74 // Initialize SPI
75 SPI_InitTypeDef spi_init;
76 spi_init.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
77 spi_init.SPI_Mode = SPI_Mode_Master;
78 spi_init.SPI_DataSize = SPI_DataSize_16b;
79 spi_init.SPI_CPOL = SPI_CPOL_Low;
80 spi_init.SPI_CPHA = SPI_CPHA_1Edge;
81 spi_init.SPI_NSS = SPI_NSS_Soft;
82 spi_init.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
83 spi_init.SPI_FirstBit = SPI_FirstBit_MSB;
84 spi_init.SPI_CRCPolynomial = 7;
85 SPI_Init(SPI1, &spi_init);
86 SPI_Cmd(SPI1, ENABLE);
87 Clear();
88 }
89
Clear()90 void Leds::Clear() {
91 fill(&colors_[0], &colors_[kNumLEDs], LED_COLOR_OFF);
92 }
93
94 /* static */
95 const int Leds::led_map_[8] = {
96 0, 1, 2, 3, 7, 6, 5, 4
97 };
98
Write()99 void Leds::Write() {
100 uint16_t leds_data = 0;
101 for (int i = 0; i < kNumLEDs; ++i) {
102 int j = led_map_[i];
103 leds_data <<= 2;
104 leds_data |= (colors_[j] & LED_COLOR_RED) ? 1 : 0;
105 leds_data |= (colors_[j] & LED_COLOR_GREEN) ? 2 : 0;
106 }
107 GPIOF->BSRR = kPinEnable;
108 __asm__("nop");
109 GPIOF->BRR = kPinEnable;
110 __asm__("nop");
111 SPI1->DR = leds_data;
112 }
113
114 } // namespace plaits
115