WS2812B Liblary this use SPI
WS2812B.cpp@1:c7bb475f0022, 2019-01-20 (annotated)
- Committer:
- Suzutomo
- Date:
- Sun Jan 20 09:05:07 2019 +0000
- Revision:
- 1:c7bb475f0022
- Parent:
- 0:6a2dcf5cd545
- Child:
- 3:c0a82b9775e6
change F446 SPIfre 2300000 -> 2250000
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Suzutomo | 0:6a2dcf5cd545 | 1 | //-------------------------------------------------------- |
Suzutomo | 0:6a2dcf5cd545 | 2 | // SPI を使って WS2812B を点灯するためのクラス |
Suzutomo | 0:6a2dcf5cd545 | 3 | // サポートするボード: Nucleo-F401RE, Nucleo-F446RE |
Suzutomo | 0:6a2dcf5cd545 | 4 | // |
Suzutomo | 0:6a2dcf5cd545 | 5 | // 2016/11/21, Copyright (c) 2016 MIKAMI, Naoki |
Suzutomo | 0:6a2dcf5cd545 | 6 | //-------------------------------------------------------- |
Suzutomo | 0:6a2dcf5cd545 | 7 | |
Suzutomo | 0:6a2dcf5cd545 | 8 | #include "WS2812B.h" |
Suzutomo | 0:6a2dcf5cd545 | 9 | #include "PeripheralPins.h" // for pinmap_peripheral() |
Suzutomo | 0:6a2dcf5cd545 | 10 | |
Suzutomo | 0:6a2dcf5cd545 | 11 | WS2812B::WS2812B(PinName pin, bool inv) |
Suzutomo | 0:6a2dcf5cd545 | 12 | : spi_(pin, NC, NC), |
Suzutomo | 0:6a2dcf5cd545 | 13 | mySpi_((SPI_TypeDef *)pinmap_peripheral(pin, PinMap_SPI_MOSI)) |
Suzutomo | 0:6a2dcf5cd545 | 14 | { |
Suzutomo | 0:6a2dcf5cd545 | 15 | spi_.format(8, 0); |
Suzutomo | 0:6a2dcf5cd545 | 16 | // クロックを 23 MHz 以下で最大の値に設定 |
Suzutomo | 0:6a2dcf5cd545 | 17 | // F401RE: 21.0 MHz |
Suzutomo | 0:6a2dcf5cd545 | 18 | // F446RE: 22.5 MHz |
Suzutomo | 0:6a2dcf5cd545 | 19 | #if defined(STM32F446xx) |
Suzutomo | 1:c7bb475f0022 | 20 | spi_.frequency(22500000); |
Suzutomo | 0:6a2dcf5cd545 | 21 | #elif defined(STM32F401xE) |
Suzutomo | 0:6a2dcf5cd545 | 22 | spi_.frequency(21000000); |
Suzutomo | 0:6a2dcf5cd545 | 23 | #else |
Suzutomo | 0:6a2dcf5cd545 | 24 | #error This code is not move this board. |
Suzutomo | 0:6a2dcf5cd545 | 25 | #endif |
Suzutomo | 0:6a2dcf5cd545 | 26 | if (!inv) fp = &WS2812B::SendByteNorm; |
Suzutomo | 0:6a2dcf5cd545 | 27 | else fp = &WS2812B::SendByteInv; |
Suzutomo | 0:6a2dcf5cd545 | 28 | } |
Suzutomo | 0:6a2dcf5cd545 | 29 | |
Suzutomo | 0:6a2dcf5cd545 | 30 | void WS2812B::Write(uint32_t x) |
Suzutomo | 0:6a2dcf5cd545 | 31 | { |
Suzutomo | 0:6a2dcf5cd545 | 32 | static const uint32_t bit23 = 0x800000; |
Suzutomo | 0:6a2dcf5cd545 | 33 | for (int n=0; n<24; n++) |
Suzutomo | 0:6a2dcf5cd545 | 34 | { |
Suzutomo | 0:6a2dcf5cd545 | 35 | if ((x & bit23) == bit23) T1HL(); |
Suzutomo | 0:6a2dcf5cd545 | 36 | else T0HL(); |
Suzutomo | 0:6a2dcf5cd545 | 37 | x <<= 1; |
Suzutomo | 0:6a2dcf5cd545 | 38 | } |
Suzutomo | 0:6a2dcf5cd545 | 39 | } |
Suzutomo | 0:6a2dcf5cd545 | 40 | |
Suzutomo | 0:6a2dcf5cd545 | 41 | void WS2812B::Write(uint32_t x, int k) |
Suzutomo | 0:6a2dcf5cd545 | 42 | { |
Suzutomo | 0:6a2dcf5cd545 | 43 | for (int n=0; n<k; n++) Write(x); |
Suzutomo | 0:6a2dcf5cd545 | 44 | } |
Suzutomo | 0:6a2dcf5cd545 | 45 | |
Suzutomo | 0:6a2dcf5cd545 | 46 | void WS2812B::Clear(int k) |
Suzutomo | 0:6a2dcf5cd545 | 47 | { |
Suzutomo | 0:6a2dcf5cd545 | 48 | for (int n=0; n<k; n++) Write(0x000000); |
Suzutomo | 0:6a2dcf5cd545 | 49 | Reset(); |
Suzutomo | 0:6a2dcf5cd545 | 50 | } |
Suzutomo | 0:6a2dcf5cd545 | 51 | |
Suzutomo | 0:6a2dcf5cd545 | 52 | void WS2812B::Send3Bytes(uint8_t x0, uint8_t x1, uint8_t x2) |
Suzutomo | 0:6a2dcf5cd545 | 53 | { |
Suzutomo | 0:6a2dcf5cd545 | 54 | SendByte(x0); |
Suzutomo | 0:6a2dcf5cd545 | 55 | SendByte(x1); |
Suzutomo | 0:6a2dcf5cd545 | 56 | SendByte(x2); |
Suzutomo | 0:6a2dcf5cd545 | 57 | } |
Suzutomo | 0:6a2dcf5cd545 | 58 | |
Suzutomo | 0:6a2dcf5cd545 | 59 | void WS2812B::SendByteNorm(uint8_t x) |
Suzutomo | 0:6a2dcf5cd545 | 60 | { |
Suzutomo | 0:6a2dcf5cd545 | 61 | while ((mySpi_->SR & SPI_SR_TXE) != SPI_SR_TXE) {} |
Suzutomo | 0:6a2dcf5cd545 | 62 | mySpi_->DR = x; |
Suzutomo | 0:6a2dcf5cd545 | 63 | } |
Suzutomo | 0:6a2dcf5cd545 | 64 | |
Suzutomo | 0:6a2dcf5cd545 | 65 | void WS2812B::SendByteInv(uint8_t x) |
Suzutomo | 0:6a2dcf5cd545 | 66 | { |
Suzutomo | 0:6a2dcf5cd545 | 67 | while ((mySpi_->SR & SPI_SR_TXE) != SPI_SR_TXE) {} |
Suzutomo | 0:6a2dcf5cd545 | 68 | mySpi_->DR = ~x; |
Suzutomo | 0:6a2dcf5cd545 | 69 | } |
Suzutomo | 0:6a2dcf5cd545 | 70 |