ws2812.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. WS2812B CPU and memory efficient library
  3. Date: 28.9.2016
  4. Author: Martin Hubacek
  5. http://www.martinhubacek.cz
  6. @hubmartin
  7. Licence: MIT License
  8. */
  9. #ifndef WS2812B_H_
  10. #define WS2812B_H_
  11. #include "ws2812.h"
  12. // GPIO enable command
  13. #define WS2812B_GPIO_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE()
  14. // LED output port
  15. #define WS2812B_PORT GPIOC
  16. // LED output pins
  17. #define WS2812B_PINS (GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3)
  18. // How many LEDs are in the series
  19. #define WS2812B_NUMBER_OF_LEDS 60
  20. // Number of output LED strips. Each has its own buffer.
  21. #define WS2812_BUFFER_COUNT 2
  22. // Choose one of the bit-juggling setpixel implementation
  23. // *******************************************************
  24. //#define SETPIX_1 // For loop, works everywhere, slow
  25. //#define SETPIX_2 // Bit band in a loop
  26. //#define SETPIX_3 // Like SETPIX_1 but with unrolled loop
  27. #define SETPIX_4 // Fastest copying using bit-banding
  28. // DEBUG OUTPUT
  29. // ********************
  30. #define LED4_PORT GPIOC
  31. #define LED4_PIN GPIO_PIN_10
  32. #define LED5_PORT GPIOC
  33. #define LED5_PIN GPIO_PIN_10
  34. // Public functions
  35. // ****************
  36. void ws2812b_init();
  37. void ws2812b_handle();
  38. // Library structures
  39. // ******************
  40. // This value sets number of periods to generate 50uS Treset signal
  41. #define WS2812_RESET_PERIOD 12
  42. typedef struct WS2812_BufferItem {
  43. uint8_t* frameBufferPointer;
  44. uint32_t frameBufferSize;
  45. uint32_t frameBufferCounter;
  46. uint8_t channel; // digital output pin/channel
  47. } WS2812_BufferItem;
  48. typedef struct WS2812_Struct
  49. {
  50. WS2812_BufferItem item[WS2812_BUFFER_COUNT];
  51. uint8_t transferComplete;
  52. uint8_t startTransfer;
  53. uint32_t timerPeriodCounter;
  54. uint32_t repeatCounter;
  55. } WS2812_Struct;
  56. WS2812_Struct ws2812b;
  57. // Bit band stuff
  58. #define RAM_BASE 0x20000000
  59. #define RAM_BB_BASE 0x22000000
  60. #define Var_ResetBit_BB(VarAddr, BitNumber) (*(volatile uint32_t *) (RAM_BB_BASE | ((VarAddr - RAM_BASE) << 5) | ((BitNumber) << 2)) = 0)
  61. #define Var_SetBit_BB(VarAddr, BitNumber) (*(volatile uint32_t *) (RAM_BB_BASE | ((VarAddr - RAM_BASE) << 5) | ((BitNumber) << 2)) = 1)
  62. #define Var_GetBit_BB(VarAddr, BitNumber) (*(volatile uint32_t *) (RAM_BB_BASE | ((VarAddr - RAM_BASE) << 5) | ((BitNumber) << 2)))
  63. #define BITBAND_SRAM(address, bit) ( (__IO uint32_t *) (RAM_BB_BASE + (((uint32_t)address) - RAM_BASE) * 32 + (bit) * 4))
  64. #define varSetBit(var,bit) (Var_SetBit_BB((uint32_t)&var,bit))
  65. #define varResetBit(var,bit) (Var_ResetBit_BB((uint32_t)&var,bit))
  66. #define varGetBit(var,bit) (Var_GetBit_BB((uint32_t)&var,bit))
  67. static void ws2812b_set_pixel(uint8_t row, uint16_t column, uint8_t red, uint8_t green, uint8_t blue);
  68. void DMA_TransferCompleteHandler(DMA_HandleTypeDef *DmaHandle);
  69. void DMA_TransferHalfHandler(DMA_HandleTypeDef *DmaHandle);
  70. #endif /* WS2812B_H_ */