Skip to content

Commit

Permalink
Rework capture code:
Browse files Browse the repository at this point in the history
- remove double read from LCD
- implement read from 4 inch displays
- made read from display on less speed (prevent data corrupt for 4 inch)
- made capture header byte align
Fix drag marker issue, now it more stable:
- more fast touch data read
- fix 16 bit SPI bus transfer
- remove delays
- remove bidirectional/unidirectional SPI bus switch (now always use bi)
Now if ask 0 points device return all data in one req
  • Loading branch information
DiSlord committed Nov 7, 2020
1 parent 1328dd5 commit 14e9567
Show file tree
Hide file tree
Showing 13 changed files with 260 additions and 103 deletions.
61 changes: 49 additions & 12 deletions board_v2_0/board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,16 +221,26 @@ namespace board {

spi_enable_tx_dma(SPI1);

// Set to tx+rx mode
spi_set_unidirectional_mode(SPI1);

/* Enable SPI1 periph. */
spi_enable(SPI1);
}
void lcd_spi_fast() {
spi_set_baudrate_prescaler(SPI1, 0b001);
void lcd_spi_write() {
spi_set_baudrate_prescaler(SPI1,SPI_CR1_BR_FPCLK_DIV_4);
}
void lcd_spi_read() {
spi_set_baudrate_prescaler(SPI1, SPI_CR1_BR_FPCLK_DIV_8);
}
void lcd_spi_slow() {
spi_set_baudrate_prescaler(SPI1, 0b110);
spi_set_baudrate_prescaler(SPI1, SPI_CR1_BR_FPCLK_DIV_16);
}
void spi_drop_read() {
uint16_t data;
data = SPI_DR(SPI1); // Cleanup read buffers in SPI hardware
data = SPI_DR(SPI1);
}

bool lcd_spi_isDMAInProgress = false;

void lcd_spi_waitDMA() {
Expand All @@ -245,20 +255,18 @@ namespace board {
while (!(SPI_SR(SPI1) & SPI_SR_TXE));
while ((SPI_SR(SPI1) & SPI_SR_BSY));

// switch back to tx+rx mode
spi_set_unidirectional_mode(SPI1);
lcd_spi_isDMAInProgress = false;
// delayMicroseconds(10);
}

uint32_t lcd_spi_transfer(uint32_t sdi, int bits) {
if(lcd_spi_isDMAInProgress)
lcd_spi_waitDMA();
uint32_t ret = 0;
spi_drop_read();
uint32_t ret = spi_xfer(SPI1, (uint16_t) sdi);
if(bits == 16) {
ret = uint32_t(spi_xfer(SPI1, (uint16_t) (sdi >> 8))) << 8;
ret|= uint32_t(spi_xfer(SPI1, (uint16_t) (sdi >> 8))) << 8;
}
ret |= spi_xfer(SPI1, (uint16_t) sdi);
return ret;
}

Expand All @@ -267,9 +275,6 @@ namespace board {
lcd_spi_waitDMA();

lcd_spi_isDMAInProgress = true;

// switch to tx only mode (do not put garbage in rx register)
spi_set_bidirectional_transmit_only_mode(SPI1);
DMATransferParams srcParams, dstParams;
srcParams.address = buf;
srcParams.bytesPerWord = 1;
Expand All @@ -285,4 +290,36 @@ namespace board {
dmaChannelSPI.start();
//lcd_spi_waitDMA();
}

void lcd_spi_read_bulk(uint8_t* buf, int bytes) {
if(lcd_spi_isDMAInProgress)
lcd_spi_waitDMA();
// Switch to read speed
lcd_spi_read();
// Drop old data in rx buffers
spi_drop_read();
#if 0
lcd_spi_isDMAInProgress = true;
DMATransferParams srcParams, dstParams;
srcParams.address = &SPI_DR(SPI1);
srcParams.bytesPerWord = 1;
srcParams.increment = false;

dstParams.address = buf;
dstParams.bytesPerWord = 1;
dstParams.increment = true;

dmaChannelSPI.setTransferParams(srcParams, dstParams,
DMADirection::PERIPHERAL_TO_MEMORY,
bytes, false);
dmaChannelSPI.start();
lcd_spi_waitDMA();
#else
do{
*buf++= spi_xfer(SPI1, (uint16_t)0);
}while(--bytes);
#endif
// Switch back to normal
lcd_spi_write();
}
}
9 changes: 6 additions & 3 deletions board_v2_0/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,18 @@ namespace board {
// spi peripheral only manages clk, sdi, and sdo.
void lcd_spi_init();

// two speed presets for ili9341 and touch controller
void lcd_spi_fast();
// three speed presets for ili9341 write/read and touch controller
void lcd_spi_write();
void lcd_spi_read();
void lcd_spi_slow();

// bits must be 16 or 8
uint32_t lcd_spi_transfer(uint32_t sdi, int bits);

void lcd_spi_transfer_bulk(uint8_t* buf, int bytes);


void lcd_spi_read_bulk(uint8_t* buf, int bytes);

// wait for all bulk transfers to complete
void lcd_spi_waitDMA();
}
60 changes: 48 additions & 12 deletions board_v2_1/board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,16 +325,25 @@ namespace board {

spi_enable_tx_dma(SPI1);

// Set to tx+rx mode
spi_set_unidirectional_mode(SPI1);

/* Enable SPI1 periph. */
spi_enable(SPI1);
}
void lcd_spi_fast() {
spi_set_baudrate_prescaler(SPI1, 0b001);
void lcd_spi_write() {
spi_set_baudrate_prescaler(SPI1,SPI_CR1_BR_FPCLK_DIV_4);
}
void lcd_spi_read() {
spi_set_baudrate_prescaler(SPI1, SPI_CR1_BR_FPCLK_DIV_8);
}
void lcd_spi_slow() {
spi_set_baudrate_prescaler(SPI1, 0b110);
spi_set_baudrate_prescaler(SPI1, SPI_CR1_BR_FPCLK_DIV_16);
}
void spi_drop_read() {
(void)SPI_DR(SPI1); // Cleanup read buffers in SPI hardware
(void)SPI_DR(SPI1);
}

bool lcd_spi_isDMAInProgress = false;

void lcd_spi_waitDMA() {
Expand All @@ -349,19 +358,17 @@ namespace board {
while (!(SPI_SR(SPI1) & SPI_SR_TXE));
while ((SPI_SR(SPI1) & SPI_SR_BSY));

// switch back to tx+rx mode
spi_set_unidirectional_mode(SPI1);
lcd_spi_isDMAInProgress = false;
// delayMicroseconds(10);
}

uint32_t lcd_spi_transfer(uint32_t sdi, int bits) {
if(lcd_spi_isDMAInProgress)
lcd_spi_waitDMA();
uint32_t ret = 0;
ret |= spi_xfer(SPI1, (uint16_t) sdi);
spi_drop_read();
uint32_t ret = spi_xfer(SPI1, (uint16_t) sdi);
if(bits == 16) {
ret = uint32_t(spi_xfer(SPI1, (uint16_t) (sdi >> 8))) << 8;
ret|= uint32_t(spi_xfer(SPI1, (uint16_t) (sdi >> 8))) << 8;
}
return ret;
}
Expand All @@ -371,9 +378,6 @@ namespace board {
lcd_spi_waitDMA();

lcd_spi_isDMAInProgress = true;

// switch to tx only mode (do not put garbage in rx register)
spi_set_bidirectional_transmit_only_mode(SPI1);
DMATransferParams srcParams, dstParams;
srcParams.address = buf;
srcParams.bytesPerWord = 1;
Expand All @@ -389,4 +393,36 @@ namespace board {
dmaChannelSPI.start();
//lcd_spi_waitDMA();
}

void lcd_spi_read_bulk(uint8_t* buf, int bytes) {
if(lcd_spi_isDMAInProgress)
lcd_spi_waitDMA();
// Switch to read speed
lcd_spi_read();
// Drop old data in rx buffers
spi_drop_read();
#if 0
lcd_spi_isDMAInProgress = true;
DMATransferParams srcParams, dstParams;
srcParams.address = &SPI_DR(SPI1);
srcParams.bytesPerWord = 1;
srcParams.increment = false;

dstParams.address = buf;
dstParams.bytesPerWord = 1;
dstParams.increment = true;

dmaChannelSPI.setTransferParams(srcParams, dstParams,
DMADirection::PERIPHERAL_TO_MEMORY,
bytes, false);
dmaChannelSPI.start();
lcd_spi_waitDMA();
#else
do{
*buf++= spi_xfer(SPI1, (uint16_t)0);
}while(--bytes);
#endif
// Switch back to normal
lcd_spi_write();
}
}
9 changes: 6 additions & 3 deletions board_v2_1/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,18 @@ namespace board {
// spi peripheral only manages clk, sdi, and sdo.
void lcd_spi_init();

// two speed presets for ili9341 and touch controller
void lcd_spi_fast();
// three speed presets for ili9341 write/read and touch controller
void lcd_spi_write();
void lcd_spi_read();
void lcd_spi_slow();

// bits must be 16 or 8
uint32_t lcd_spi_transfer(uint32_t sdi, int bits);

void lcd_spi_transfer_bulk(uint8_t* buf, int bytes);


void lcd_spi_read_bulk(uint8_t* buf, int bytes);

// wait for all bulk transfers to complete
void lcd_spi_waitDMA();
}
61 changes: 49 additions & 12 deletions board_v2_plus/board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,16 +311,26 @@ namespace board {

spi_enable_tx_dma(SPI1);

// Set to tx+rx mode
spi_set_unidirectional_mode(SPI1);

/* Enable SPI1 periph. */
spi_enable(SPI1);
}
void lcd_spi_fast() {
spi_set_baudrate_prescaler(SPI1, 0b001);
void lcd_spi_write() {
spi_set_baudrate_prescaler(SPI1,SPI_CR1_BR_FPCLK_DIV_4);
}
void lcd_spi_read() {
spi_set_baudrate_prescaler(SPI1, SPI_CR1_BR_FPCLK_DIV_8);
}
void lcd_spi_slow() {
spi_set_baudrate_prescaler(SPI1, 0b110);
spi_set_baudrate_prescaler(SPI1, SPI_CR1_BR_FPCLK_DIV_16);
}
void spi_drop_read() {
uint16_t data;
data = SPI_DR(SPI1); // Cleanup read buffers in SPI hardware
data = SPI_DR(SPI1);
}

bool lcd_spi_isDMAInProgress = false;

void lcd_spi_waitDMA() {
Expand All @@ -335,19 +345,17 @@ namespace board {
while (!(SPI_SR(SPI1) & SPI_SR_TXE));
while ((SPI_SR(SPI1) & SPI_SR_BSY));

// switch back to tx+rx mode
spi_set_unidirectional_mode(SPI1);
lcd_spi_isDMAInProgress = false;
// delayMicroseconds(10);
}

uint32_t lcd_spi_transfer(uint32_t sdi, int bits) {
if(lcd_spi_isDMAInProgress)
lcd_spi_waitDMA();
uint32_t ret = 0;
ret |= spi_xfer(SPI1, (uint16_t) sdi);
spi_drop_read();
uint32_t ret = spi_xfer(SPI1, (uint16_t) sdi);
if(bits == 16) {
ret = uint32_t(spi_xfer(SPI1, (uint16_t) (sdi >> 8))) << 8;
ret|= uint32_t(spi_xfer(SPI1, (uint16_t) (sdi >> 8))) << 8;
}
return ret;
}
Expand All @@ -357,9 +365,6 @@ namespace board {
lcd_spi_waitDMA();

lcd_spi_isDMAInProgress = true;

// switch to tx only mode (do not put garbage in rx register)
spi_set_bidirectional_transmit_only_mode(SPI1);
DMATransferParams srcParams, dstParams;
srcParams.address = buf;
srcParams.bytesPerWord = 1;
Expand All @@ -375,4 +380,36 @@ namespace board {
dmaChannelSPI.start();
//lcd_spi_waitDMA();
}

void lcd_spi_read_bulk(uint8_t* buf, int bytes) {
if(lcd_spi_isDMAInProgress)
lcd_spi_waitDMA();
// Switch to read speed
lcd_spi_read();
// Drop old data in rx buffers
spi_drop_read();
#if 0
lcd_spi_isDMAInProgress = true;
DMATransferParams srcParams, dstParams;
srcParams.address = &SPI_DR(SPI1);
srcParams.bytesPerWord = 1;
srcParams.increment = false;

dstParams.address = buf;
dstParams.bytesPerWord = 1;
dstParams.increment = true;

dmaChannelSPI.setTransferParams(srcParams, dstParams,
DMADirection::PERIPHERAL_TO_MEMORY,
bytes, false);
dmaChannelSPI.start();
lcd_spi_waitDMA();
#else
do{
*buf++= spi_xfer(SPI1, (uint16_t)0);
}while(--bytes);
#endif
// Switch back to normal
lcd_spi_write();
}
}
9 changes: 6 additions & 3 deletions board_v2_plus/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,18 @@ namespace board {
// spi peripheral only manages clk, sdi, and sdo.
void lcd_spi_init();

// two speed presets for ili9341 and touch controller
void lcd_spi_fast();
// three speed presets for ili9341 write/read and touch controller
void lcd_spi_write();
void lcd_spi_read();
void lcd_spi_slow();

// bits must be 16 or 8
uint32_t lcd_spi_transfer(uint32_t sdi, int bits);

void lcd_spi_transfer_bulk(uint8_t* buf, int bytes);


void lcd_spi_read_bulk(uint8_t* buf, int bytes);

// wait for all bulk transfers to complete
void lcd_spi_waitDMA();
}
Loading

0 comments on commit 14e9567

Please sign in to comment.