|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <cstring> |
| 4 | + |
| 5 | +#include "pico/stdlib.h" |
| 6 | +#include "hardware/spi.h" |
| 7 | +#include "hardware/gpio.h" |
| 8 | +#include "common/pimoroni_common.hpp" |
| 9 | +#include "common/pimoroni_bus.hpp" |
| 10 | +#include "libraries/pico_graphics/pico_graphics.hpp" |
| 11 | + |
| 12 | + |
| 13 | +// 8 MB PSRam |
| 14 | + |
| 15 | +namespace pimoroni { |
| 16 | + |
| 17 | + class PSRamDisplay : public IDirectDisplayDriver<uint8_t> { |
| 18 | + //-------------------------------------------------- |
| 19 | + // Variables |
| 20 | + //-------------------------------------------------- |
| 21 | + private: |
| 22 | + spi_inst_t *spi = PIMORONI_SPI_DEFAULT_INSTANCE; |
| 23 | + |
| 24 | + // interface pins with our standard defaults where appropriate |
| 25 | + uint CS = 3; |
| 26 | + uint DC = PIN_UNUSED; |
| 27 | + uint SCK = SPI_DEFAULT_SCK; |
| 28 | + uint MOSI = SPI_DEFAULT_MOSI; |
| 29 | + uint MISO = SPI_DEFAULT_MISO; |
| 30 | + |
| 31 | + uint32_t start_address = 0; |
| 32 | + uint16_t width = 0; |
| 33 | + uint16_t height = 0; |
| 34 | + |
| 35 | + absolute_time_t timeout; |
| 36 | + |
| 37 | + bool blocking = false; |
| 38 | + |
| 39 | + public: |
| 40 | + enum colour : uint8_t { |
| 41 | + BLACK = 0, |
| 42 | + WHITE = 1, |
| 43 | + GREEN = 2, |
| 44 | + BLUE = 3, |
| 45 | + RED = 4, |
| 46 | + YELLOW = 5, |
| 47 | + ORANGE = 6, |
| 48 | + CLEAN = 7 |
| 49 | + }; |
| 50 | + |
| 51 | + PSRamDisplay(uint16_t width, uint16_t height) : PSRamDisplay(width, height, {PIMORONI_SPI_DEFAULT_INSTANCE, 3, SPI_DEFAULT_SCK, SPI_DEFAULT_MOSI, PIN_UNUSED, PIN_UNUSED, PIN_UNUSED}) {}; |
| 52 | + |
| 53 | + PSRamDisplay(uint16_t width, uint16_t height, SPIPins pins) : |
| 54 | + spi(pins.spi), |
| 55 | + CS(pins.cs), DC(pins.dc), SCK(pins.sck), MOSI(pins.mosi), |
| 56 | + width(width), height(height) { |
| 57 | + init(); |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + //-------------------------------------------------- |
| 62 | + // Methods |
| 63 | + //-------------------------------------------------- |
| 64 | + public: |
| 65 | + void test(void){ |
| 66 | + |
| 67 | + char writeBuffer[1024]; |
| 68 | + char readBuffer[1024]; |
| 69 | + |
| 70 | + uint mb = 8; |
| 71 | + |
| 72 | + for(uint k = 0; k < 1024*mb; k++) |
| 73 | + { |
| 74 | + sprintf(writeBuffer, "%u", k); |
| 75 | + |
| 76 | + write(k*1024, strlen(writeBuffer)+1, (uint8_t *)writeBuffer); |
| 77 | + } |
| 78 | + |
| 79 | + bool bSame = true; |
| 80 | + for(uint k = 0; k < 1024*mb && bSame; k++) |
| 81 | + { |
| 82 | + sprintf(writeBuffer, "%u", k); |
| 83 | + read(k*1024, strlen(writeBuffer)+1, (uint8_t *)readBuffer); |
| 84 | + bSame = strcmp(writeBuffer, readBuffer) ==0; |
| 85 | + printf("[%u] %s == %s ? %s\n", k, writeBuffer, readBuffer, bSame ? "Success" : "Failure"); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + void write_pixel(const Point &p, uint8_t colour) override; |
| 90 | + void write_pixel_span(const Point &p, uint l, uint8_t colour) override; |
| 91 | + void read_pixel_span(const Point &p, uint l, uint8_t *data) override; |
| 92 | + |
| 93 | + int __not_in_flash_func(SpiSetBlocking)(const uint16_t uSrc, size_t uLen) |
| 94 | + { |
| 95 | + invalid_params_if(SPI, 0 > (int)uLen); |
| 96 | + // Deliberately overflow FIFO, then clean up afterward, to minimise amount |
| 97 | + // of APB polling required per halfword |
| 98 | + for (size_t i = 0; i < uLen; ++i) { |
| 99 | + while (!spi_is_writable(spi)) |
| 100 | + tight_loop_contents(); |
| 101 | + spi_get_hw(spi)->dr = uSrc; |
| 102 | + } |
| 103 | + |
| 104 | + while (spi_is_readable(spi)) |
| 105 | + (void)spi_get_hw(spi)->dr; |
| 106 | + while (spi_get_hw(spi)->sr & SPI_SSPSR_BSY_BITS) |
| 107 | + tight_loop_contents(); |
| 108 | + while (spi_is_readable(spi)) |
| 109 | + (void)spi_get_hw(spi)->dr; |
| 110 | + |
| 111 | + // Don't leave overrun flag set |
| 112 | + spi_get_hw(spi)->icr = SPI_SSPICR_RORIC_BITS; |
| 113 | + |
| 114 | + return (int)uLen; |
| 115 | + } |
| 116 | + |
| 117 | + private: |
| 118 | + void init(); |
| 119 | + void write(uint32_t address, size_t len, const uint8_t *data); |
| 120 | + void write(uint32_t address, size_t len, const uint8_t byte); |
| 121 | + void read(uint32_t address, size_t len, uint8_t *data); |
| 122 | + |
| 123 | + uint32_t pointToAddress(const Point &p) |
| 124 | + { |
| 125 | + return start_address + (p.y * width) + p.x; |
| 126 | + } |
| 127 | + }; |
| 128 | +} |
0 commit comments