You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.5KB

  1. // Copyright 2016 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.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. #include "plaits/drivers/leds.h"
  29. #include <algorithm>
  30. #include <stm32f37x_conf.h>
  31. namespace plaits {
  32. using namespace std;
  33. const uint16_t kPinEnable = GPIO_Pin_7;
  34. void Leds::Init() {
  35. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOF, ENABLE);
  36. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  37. RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
  38. GPIO_InitTypeDef gpio_init;
  39. // SS
  40. gpio_init.GPIO_Mode = GPIO_Mode_OUT;
  41. gpio_init.GPIO_OType = GPIO_OType_PP;
  42. gpio_init.GPIO_Speed = GPIO_Speed_2MHz;
  43. gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL;
  44. gpio_init.GPIO_Pin = kPinEnable;
  45. GPIO_Init(GPIOF, &gpio_init);
  46. // MOSI
  47. gpio_init.GPIO_Mode = GPIO_Mode_AF;
  48. gpio_init.GPIO_OType = GPIO_OType_PP;
  49. gpio_init.GPIO_Speed = GPIO_Speed_2MHz;
  50. gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL;
  51. gpio_init.GPIO_Pin = GPIO_Pin_6;
  52. GPIO_Init(GPIOF, &gpio_init);
  53. GPIO_PinAFConfig(GPIOF, GPIO_PinSource6, GPIO_AF_5);
  54. // SCK
  55. gpio_init.GPIO_Mode = GPIO_Mode_AF;
  56. gpio_init.GPIO_OType = GPIO_OType_PP;
  57. gpio_init.GPIO_Speed = GPIO_Speed_2MHz;
  58. gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL;
  59. gpio_init.GPIO_Pin = GPIO_Pin_12;
  60. GPIO_Init(GPIOA, &gpio_init);
  61. GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_6);
  62. // Initialize SPI
  63. SPI_InitTypeDef spi_init;
  64. spi_init.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  65. spi_init.SPI_Mode = SPI_Mode_Master;
  66. spi_init.SPI_DataSize = SPI_DataSize_16b;
  67. spi_init.SPI_CPOL = SPI_CPOL_Low;
  68. spi_init.SPI_CPHA = SPI_CPHA_1Edge;
  69. spi_init.SPI_NSS = SPI_NSS_Soft;
  70. spi_init.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
  71. spi_init.SPI_FirstBit = SPI_FirstBit_MSB;
  72. spi_init.SPI_CRCPolynomial = 7;
  73. SPI_Init(SPI1, &spi_init);
  74. SPI_Cmd(SPI1, ENABLE);
  75. Clear();
  76. }
  77. void Leds::Clear() {
  78. fill(&colors_[0], &colors_[kNumLEDs], LED_COLOR_OFF);
  79. }
  80. /* static */
  81. const int Leds::led_map_[8] = {
  82. 0, 1, 2, 3, 7, 6, 5, 4
  83. };
  84. void Leds::Write() {
  85. uint16_t leds_data = 0;
  86. for (int i = 0; i < kNumLEDs; ++i) {
  87. int j = led_map_[i];
  88. leds_data <<= 2;
  89. leds_data |= (colors_[j] & LED_COLOR_RED) ? 1 : 0;
  90. leds_data |= (colors_[j] & LED_COLOR_GREEN) ? 2 : 0;
  91. }
  92. GPIOF->BSRR = kPinEnable;
  93. __asm__("nop");
  94. GPIOF->BRR = kPinEnable;
  95. __asm__("nop");
  96. SPI1->DR = leds_data;
  97. }
  98. } // namespace plaits