@ -21,10 +21,10 @@
# include "hal.h"
# include "hal.h"
# include "nanovna.h"
# include "nanovna.h"
uint16_t spi_buffer [ 1024 ] ;
uint16_t spi_buffer [ SPI_BUFFER_SIZE ] ;
// Default foreground & background colors
// Default foreground & background colors
uint16_t foreground_color = DEFAULT_FG_COLOR ;
uint16_t foreground_color = 0 ;
uint16_t background_color = DEFAULT_BG_COLOR ;
uint16_t background_color = 0 ;
// Display width and height definition
// Display width and height definition
# define ILI9341_WIDTH 320
# define ILI9341_WIDTH 320
@ -129,7 +129,8 @@ uint16_t background_color=DEFAULT_BG_COLOR;
# define DISPLAY_ROTATION_270 (ILI9341_MADCTL_MX | ILI9341_MADCTL_BGR)
# define DISPLAY_ROTATION_270 (ILI9341_MADCTL_MX | ILI9341_MADCTL_BGR)
# define DISPLAY_ROTATION_90 (ILI9341_MADCTL_MY | ILI9341_MADCTL_BGR)
# define DISPLAY_ROTATION_90 (ILI9341_MADCTL_MY | ILI9341_MADCTL_BGR)
# define DISPLAY_ROTATION_0 (ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR)
# define DISPLAY_ROTATION_0 (ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR)
# define DISPLAY_ROTATION_180 (ILI9341_MADCTL_MX | ILI9341_MADCTL_MY | ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR)
# define DISPLAY_ROTATION_180 (ILI9341_MADCTL_MX | ILI9341_MADCTL_MY \
| ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR )
//
//
// Pin macros
// Pin macros
@ -179,14 +180,17 @@ uint16_t background_color=DEFAULT_BG_COLOR;
# define SPI_READ_DATA SPI1->DR
# define SPI_READ_DATA SPI1->DR
# ifdef __USE_DISPLAY_DMA__
# ifdef __USE_DISPLAY_DMA__
static const stm32_dma_stream_t * dmatx = STM32_DMA_STREAM ( STM32_SPI_SPI1_TX_DMA_STREAM ) ;
static const stm32_dma_stream_t * dmatx =
static uint32_t txdmamode = STM32_DMA_CR_CHSEL ( SPI1_TX_DMA_CHANNEL ) // Select SPI1 Tx DMA
STM32_DMA_STREAM ( STM32_SPI_SPI1_TX_DMA_STREAM ) ;
static uint32_t txdmamode =
STM32_DMA_CR_CHSEL ( SPI1_TX_DMA_CHANNEL ) // Select SPI1 Tx DMA
| STM32_DMA_CR_PL ( STM32_SPI_SPI1_DMA_PRIORITY ) // Set priority
| STM32_DMA_CR_PL ( STM32_SPI_SPI1_DMA_PRIORITY ) // Set priority
| STM32_DMA_CR_DIR_M2P // Memory to Spi
| STM32_DMA_CR_DIR_M2P // Memory to Spi
| STM32_DMA_CR_DMEIE //
| STM32_DMA_CR_DMEIE //
| STM32_DMA_CR_TEIE ;
| STM32_DMA_CR_TEIE ;
static void spi_lld_serve_tx_interrupt ( SPIDriver * spip , uint32_t flags ) {
static void spi_lld_serve_tx_interrupt ( SPIDriver * spip , uint32_t flags )
{
( void ) spip ;
( void ) spip ;
( void ) flags ;
( void ) flags ;
}
}
@ -199,13 +203,15 @@ static uint32_t rxdmamode = STM32_DMA_CR_CHSEL(SPI1_RX_DMA_CHANNEL)
| STM32_DMA_CR_DMEIE
| STM32_DMA_CR_DMEIE
| STM32_DMA_CR_TEIE ;
| STM32_DMA_CR_TEIE ;
static void spi_lld_serve_rx_interrupt ( SPIDriver * spip , uint32_t flags ) {
static void spi_lld_serve_rx_interrupt ( SPIDriver * spip , uint32_t flags )
{
( void ) spip ;
( void ) spip ;
( void ) flags ;
( void ) flags ;
}
}
static void dmaStreamFlush ( uint32_t len ) {
static void dmaStreamFlush ( uint32_t len )
while ( len ) {
{
while ( len ) {
// DMA data transfer limited by 65535
// DMA data transfer limited by 65535
uint16_t tx_size = len > 65535 ? 65535 : len ;
uint16_t tx_size = len > 65535 ? 65535 : len ;
dmaStreamSetTransactionSize ( dmatx , tx_size ) ;
dmaStreamSetTransactionSize ( dmatx , tx_size ) ;
@ -223,7 +229,7 @@ static void spi_init(void)
SPI1 - > CR1 = SPI_CR1_MSTR // SPI is MASTER
SPI1 - > CR1 = SPI_CR1_MSTR // SPI is MASTER
| SPI_CR1_SSM // Software slave management (The external NSS pin is free for other application uses)
| SPI_CR1_SSM // Software slave management (The external NSS pin is free for other application uses)
| SPI_CR1_SSI ; // Internal slave select (This bit has an effect only when the SSM bit is set. Allow use NSS pin as I/O)
| SPI_CR1_SSI ; // Internal slave select (This bit has an effect only when the SSM bit is set. Allow use NSS pin as I/O)
// | SPI_CR1_BR_1; // Baud rate control
// | SPI_CR1_BR_1; // Baud rate control
SPI1 - > CR2 = SPI_CR2_8BIT // SPI data size, set to 8 bit
SPI1 - > CR2 = SPI_CR2_8BIT // SPI data size, set to 8 bit
| SPI_CR2_FRXTH ; // SPI_SR_RXNE generated every 8 bit data
| SPI_CR2_FRXTH ; // SPI_SR_RXNE generated every 8 bit data
@ -247,15 +253,17 @@ static void spi_init(void)
static void __attribute__ ( ( noinline ) ) send_command ( uint8_t cmd , uint8_t len , const uint8_t * data )
static void __attribute__ ( ( noinline ) ) send_command ( uint8_t cmd , uint8_t len , const uint8_t * data )
{
{
CS_LOW ;
CS_LOW ;
// while (SPI_TX_IS_NOT_EMPTY);
// while (SPI_TX_IS_NOT_EMPTY);
DC_CMD ;
DC_CMD ;
SPI_WRITE_8BIT ( cmd ) ;
SPI_WRITE_8BIT ( cmd ) ;
// Need wait transfer complete and set data bit
// Need wait transfer complete and set data bit
while ( SPI_IS_BUSY ) ;
while ( SPI_IS_BUSY )
;
// Send command data (if need)
// Send command data (if need)
DC_DATA ;
DC_DATA ;
while ( len - - > 0 ) {
while ( len - - > 0 ) {
while ( SPI_TX_IS_NOT_EMPTY ) ;
while ( SPI_TX_IS_NOT_EMPTY )
;
SPI_WRITE_8BIT ( * data + + ) ;
SPI_WRITE_8BIT ( * data + + ) ;
}
}
//CS_HIGH;
//CS_HIGH;
@ -301,13 +309,13 @@ static const uint8_t ili9341_init_seq[] = {
// gamma set for curve 01/2/04/08
// gamma set for curve 01/2/04/08
ILI9341_GAMMA_SET , 1 , 0x01 ,
ILI9341_GAMMA_SET , 1 , 0x01 ,
// positive gamma correction
// positive gamma correction
// ILI9341_POSITIVE_GAMMA_CORRECTION, 15, 0x1F, 0x1A, 0x18, 0x0A, 0x0F, 0x06, 0x45, 0x87, 0x32, 0x0A, 0x07, 0x02, 0x07, 0x05, 0x00,
// ILI9341_POSITIVE_GAMMA_CORRECTION, 15, 0x1F, 0x1A, 0x18, 0x0A, 0x0F, 0x06, 0x45, 0x87, 0x32, 0x0A, 0x07, 0x02, 0x07, 0x05, 0x00,
// negativ gamma correction
// negativ gamma correction
// ILI9341_NEGATIVE_GAMMA_CORRECTION, 15, 0x00, 0x25, 0x27, 0x05, 0x10, 0x09, 0x3A, 0x78, 0x4D, 0x05, 0x18, 0x0D, 0x38, 0x3A, 0x1F,
// ILI9341_NEGATIVE_GAMMA_CORRECTION, 15, 0x00, 0x25, 0x27, 0x05, 0x10, 0x09, 0x3A, 0x78, 0x4D, 0x05, 0x18, 0x0D, 0x38, 0x3A, 0x1F,
// Column Address Set
// Column Address Set
// ILI9341_COLUMN_ADDRESS_SET, 4, 0x00, 0x00, 0x01, 0x3f, // width 320
// ILI9341_COLUMN_ADDRESS_SET, 4, 0x00, 0x00, 0x01, 0x3f, // width 320
// Page Address Set
// Page Address Set
// ILI9341_PAGE_ADDRESS_SET, 4, 0x00, 0x00, 0x00, 0xef, // height 240
// ILI9341_PAGE_ADDRESS_SET, 4, 0x00, 0x00, 0x00, 0xef, // height 240
// entry mode
// entry mode
ILI9341_ENTRY_MODE_SET , 1 , 0x06 ,
ILI9341_ENTRY_MODE_SET , 1 , 0x06 ,
// display function control
// display function control
@ -343,33 +351,35 @@ void ili9341_init(void)
# ifndef __USE_DISPLAY_DMA__
# ifndef __USE_DISPLAY_DMA__
void ili9341_fill ( int x , int y , int w , int h , int color )
void ili9341_fill ( int x , int y , int w , int h , int color )
{
{
// uint8_t xx[4] = { x >> 8, x, (x+w-1) >> 8, (x+w-1) };
// uint8_t xx[4] = { x >> 8, x, (x+w-1) >> 8, (x+w-1) };
// uint8_t yy[4] = { y >> 8, y, (y+h-1) >> 8, (y+h-1) };
// uint8_t yy[4] = { y >> 8, y, (y+h-1) >> 8, (y+h-1) };
uint32_t xx = __REV16 ( x | ( ( x + w - 1 ) < < 16 ) ) ;
uint32_t xx = __REV16 ( x | ( ( x + w - 1 ) < < 16 ) ) ;
uint32_t yy = __REV16 ( y | ( ( y + h - 1 ) < < 16 ) ) ;
uint32_t yy = __REV16 ( y | ( ( y + h - 1 ) < < 16 ) ) ;
send_command ( ILI9341_COLUMN_ADDRESS_SET , 4 , ( uint8_t * ) & xx ) ;
send_command ( ILI9341_COLUMN_ADDRESS_SET , 4 , ( uint8_t * ) & xx ) ;
send_command ( ILI9341_PAGE_ADDRESS_SET , 4 , ( uint8_t * ) & yy ) ;
send_command ( ILI9341_PAGE_ADDRESS_SET , 4 , ( uint8_t * ) & yy ) ;
send_command ( ILI9341_MEMORY_WRITE , 0 , NULL ) ;
send_command ( ILI9341_MEMORY_WRITE , 0 , NULL ) ;
int32_t len = w * h ;
int32_t len = w * h ;
while ( len - - > 0 ) {
while ( len - - > 0 ) {
while ( SPI_TX_IS_NOT_EMPTY ) ;
while ( SPI_TX_IS_NOT_EMPTY )
;
SPI_WRITE_16BIT ( color ) ;
SPI_WRITE_16BIT ( color ) ;
}
}
}
}
void ili9341_bulk ( int x , int y , int w , int h )
void ili9341_bulk ( int x , int y , int w , int h )
{
{
// uint8_t xx[4] = { x >> 8, x, (x+w-1) >> 8, (x+w-1) };
// uint8_t xx[4] = { x >> 8, x, (x+w-1) >> 8, (x+w-1) };
// uint8_t yy[4] = { y >> 8, y, (y+h-1) >> 8, (y+h-1) };
// uint8_t yy[4] = { y >> 8, y, (y+h-1) >> 8, (y+h-1) };
uint16_t * buf = spi_buffer ;
uint16_t * buf = spi_buffer ;
uint32_t xx = __REV16 ( x | ( ( x + w - 1 ) < < 16 ) ) ;
uint32_t xx = __REV16 ( x | ( ( x + w - 1 ) < < 16 ) ) ;
uint32_t yy = __REV16 ( y | ( ( y + h - 1 ) < < 16 ) ) ;
uint32_t yy = __REV16 ( y | ( ( y + h - 1 ) < < 16 ) ) ;
send_command ( ILI9341_COLUMN_ADDRESS_SET , 4 , ( uint8_t * ) & xx ) ;
send_command ( ILI9341_COLUMN_ADDRESS_SET , 4 , ( uint8_t * ) & xx ) ;
send_command ( ILI9341_PAGE_ADDRESS_SET , 4 , ( uint8_t * ) & yy ) ;
send_command ( ILI9341_PAGE_ADDRESS_SET , 4 , ( uint8_t * ) & yy ) ;
send_command ( ILI9341_MEMORY_WRITE , 0 , NULL ) ;
send_command ( ILI9341_MEMORY_WRITE , 0 , NULL ) ;
int32_t len = w * h ;
int32_t len = w * h ;
while ( len - - > 0 ) {
while ( len - - > 0 ) {
while ( SPI_TX_IS_NOT_EMPTY ) ;
while ( SPI_TX_IS_NOT_EMPTY )
;
SPI_WRITE_16BIT ( * buf + + ) ;
SPI_WRITE_16BIT ( * buf + + ) ;
}
}
}
}
@ -378,18 +388,18 @@ static uint8_t ssp_sendrecvdata(void)
{
{
// Start RX clock (by sending data)
// Start RX clock (by sending data)
SPI_WRITE_8BIT ( 0 ) ;
SPI_WRITE_8BIT ( 0 ) ;
while ( SPI_RX_IS_EMPTY & & SPI_IS_BUSY )
while ( SPI_RX_IS_EMPTY & & SPI_IS_BUSY )
;
;
return SPI_READ_DATA ;
return SPI_READ_DATA ;
}
}
void ili9341_read_memory ( int x , int y , int w , int h , int len , uint16_t * out )
void ili9341_read_memory ( int x , int y , int w , int h , int len , uint16_t * out )
{
{
// uint8_t xx[4] = { x >> 8, x, (x+w-1) >> 8, (x+w-1) };
// uint8_t xx[4] = { x >> 8, x, (x+w-1) >> 8, (x+w-1) };
// uint8_t yy[4] = { y >> 8, y, (y+h-1) >> 8, (y+h-1) };
// uint8_t yy[4] = { y >> 8, y, (y+h-1) >> 8, (y+h-1) };
uint32_t xx = __REV16 ( x | ( ( x + w - 1 ) < < 16 ) ) ;
uint32_t xx = __REV16 ( x | ( ( x + w - 1 ) < < 16 ) ) ;
uint32_t yy = __REV16 ( y | ( ( y + h - 1 ) < < 16 ) ) ;
uint32_t yy = __REV16 ( y | ( ( y + h - 1 ) < < 16 ) ) ;
send_command ( ILI9341_COLUMN_ADDRESS_SET , 4 , ( uint8_t * ) & xx ) ;
send_command ( ILI9341_COLUMN_ADDRESS_SET , 4 , ( uint8_t * ) & xx ) ;
send_command ( ILI9341_PAGE_ADDRESS_SET , 4 , ( uint8_t * ) & yy ) ;
send_command ( ILI9341_PAGE_ADDRESS_SET , 4 , ( uint8_t * ) & yy ) ;
send_command ( ILI9341_MEMORY_READ , 0 , NULL ) ;
send_command ( ILI9341_MEMORY_READ , 0 , NULL ) ;
@ -403,7 +413,7 @@ void ili9341_read_memory(int x, int y, int w, int h, int len, uint16_t *out)
uint8_t r = ssp_sendrecvdata ( ) ;
uint8_t r = ssp_sendrecvdata ( ) ;
uint8_t g = ssp_sendrecvdata ( ) ;
uint8_t g = ssp_sendrecvdata ( ) ;
uint8_t b = ssp_sendrecvdata ( ) ;
uint8_t b = ssp_sendrecvdata ( ) ;
* out + + = RGB565 ( r , g , b ) ;
* out + + = RGB565 ( r , g , b ) ;
}
}
CS_HIGH ;
CS_HIGH ;
}
}
@ -415,28 +425,48 @@ void ili9341_read_memory(int x, int y, int w, int h, int len, uint16_t *out)
// Fill region by some color
// Fill region by some color
void ili9341_fill ( int x , int y , int w , int h , int color )
void ili9341_fill ( int x , int y , int w , int h , int color )
{
{
uint32_t xx = __REV16 ( x | ( ( x + w - 1 ) < < 16 ) ) ;
uint32_t xx = __REV16 ( x | ( ( x + w - 1 ) < < 16 ) ) ;
uint32_t yy = __REV16 ( y | ( ( y + h - 1 ) < < 16 ) ) ;
uint32_t yy = __REV16 ( y | ( ( y + h - 1 ) < < 16 ) ) ;
send_command ( ILI9341_COLUMN_ADDRESS_SET , 4 , ( uint8_t * ) & xx ) ;
send_command ( ILI9341_COLUMN_ADDRESS_SET , 4 , ( uint8_t * ) & xx ) ;
send_command ( ILI9341_PAGE_ADDRESS_SET , 4 , ( uint8_t * ) & yy ) ;
send_command ( ILI9341_PAGE_ADDRESS_SET , 4 , ( uint8_t * ) & yy ) ;
send_command ( ILI9341_MEMORY_WRITE , 0 , NULL ) ;
send_command ( ILI9341_MEMORY_WRITE , 0 , NULL ) ;
dmaStreamSetMemory0 ( dmatx , & color ) ;
dmaStreamSetMemory0 ( dmatx , & color ) ;
dmaStreamSetMode ( dmatx , txdmamode | STM32_DMA_CR_PSIZE_HWORD | STM32_DMA_CR_MSIZE_HWORD ) ;
dmaStreamSetMode ( dmatx , txdmamode | STM32_DMA_CR_PSIZE_HWORD | STM32_DMA_CR_MSIZE_HWORD ) ;
dmaStreamFlush ( w * h ) ;
dmaStreamFlush ( w * h ) ;
}
}
void ili9341_bulk_8bit ( int x , int y , int w , int h , uint16_t * palette )
{
uint32_t xx = __REV16 ( x | ( ( x + w - 1 ) < < 16 ) ) ;
uint32_t yy = __REV16 ( y | ( ( y + h - 1 ) < < 16 ) ) ;
send_command ( ILI9341_COLUMN_ADDRESS_SET , 4 , ( uint8_t * ) & xx ) ;
send_command ( ILI9341_PAGE_ADDRESS_SET , 4 , ( uint8_t * ) & yy ) ;
send_command ( ILI9341_MEMORY_WRITE , 0 , NULL ) ;
uint8_t * buf = ( uint8_t * ) spi_buffer ;
int32_t len = w * h ;
while ( len - - > 0 ) {
uint16_t color = palette [ * buf + + ] ;
while ( SPI_TX_IS_NOT_EMPTY )
;
SPI_WRITE_16BIT ( color ) ;
}
}
// Copy spi_buffer to region
// Copy spi_buffer to region
void ili9341_bulk ( int x , int y , int w , int h )
void ili9341_bulk ( int x , int y , int w , int h )
{
{
uint32_t xx = __REV16 ( x | ( ( x + w - 1 ) < < 16 ) ) ;
uint32_t xx = __REV16 ( x | ( ( x + w - 1 ) < < 16 ) ) ;
uint32_t yy = __REV16 ( y | ( ( y + h - 1 ) < < 16 ) ) ;
uint32_t yy = __REV16 ( y | ( ( y + h - 1 ) < < 16 ) ) ;
send_command ( ILI9341_COLUMN_ADDRESS_SET , 4 , ( uint8_t * ) & xx ) ;
send_command ( ILI9341_COLUMN_ADDRESS_SET , 4 , ( uint8_t * ) & xx ) ;
send_command ( ILI9341_PAGE_ADDRESS_SET , 4 , ( uint8_t * ) & yy ) ;
send_command ( ILI9341_PAGE_ADDRESS_SET , 4 , ( uint8_t * ) & yy ) ;
send_command ( ILI9341_MEMORY_WRITE , 0 , NULL ) ;
send_command ( ILI9341_MEMORY_WRITE , 0 , NULL ) ;
// Init Tx DMA mem->spi, set size, mode (spi and mem data size is 16 bit)
// Init Tx DMA mem->spi, set size, mode (spi and mem data size is 16 bit)
dmaStreamSetMemory0 ( dmatx , spi_buffer ) ;
dmaStreamSetMemory0 ( dmatx , spi_buffer ) ;
dmaStreamSetMode ( dmatx , txdmamode | STM32_DMA_CR_PSIZE_HWORD | STM32_DMA_CR_MSIZE_HWORD | STM32_DMA_CR_MINC ) ;
dmaStreamSetMode ( dmatx , txdmamode | STM32_DMA_CR_PSIZE_HWORD |
STM32_DMA_CR_MSIZE_HWORD | STM32_DMA_CR_MINC ) ;
dmaStreamFlush ( w * h ) ;
dmaStreamFlush ( w * h ) ;
}
}
@ -445,20 +475,20 @@ void ili9341_bulk(int x, int y, int w, int h)
void ili9341_read_memory ( int x , int y , int w , int h , int len , uint16_t * out )
void ili9341_read_memory ( int x , int y , int w , int h , int len , uint16_t * out )
{
{
uint8_t dummy_tx = 0 ;
uint8_t dummy_tx = 0 ;
uint8_t * rgbbuf = ( uint8_t * ) out ;
uint8_t * rgbbuf = ( uint8_t * ) out ;
uint16_t data_size = len * 3 + 1 ;
uint16_t data_size = len * 3 + 1 ;
uint32_t xx = __REV16 ( x | ( ( x + w - 1 ) < < 16 ) ) ;
uint32_t xx = __REV16 ( x | ( ( x + w - 1 ) < < 16 ) ) ;
uint32_t yy = __REV16 ( y | ( ( y + h - 1 ) < < 16 ) ) ;
uint32_t yy = __REV16 ( y | ( ( y + h - 1 ) < < 16 ) ) ;
send_command ( ILI9341_COLUMN_ADDRESS_SET , 4 , ( uint8_t * ) & xx ) ;
send_command ( ILI9341_COLUMN_ADDRESS_SET , 4 , ( uint8_t * ) & xx ) ;
send_command ( ILI9341_PAGE_ADDRESS_SET , 4 , ( uint8_t * ) & yy ) ;
send_command ( ILI9341_PAGE_ADDRESS_SET , 4 , ( uint8_t * ) & yy ) ;
send_command ( ILI9341_MEMORY_READ , 0 , NULL ) ;
send_command ( ILI9341_MEMORY_READ , 0 , NULL ) ;
// Skip SPI rx buffer
// Skip SPI rx buffer
while ( SPI_RX_IS_NOT_EMPTY )
while ( SPI_RX_IS_NOT_EMPTY ) ( void ) SPI_READ_DATA ;
( void ) SPI_READ_DATA ;
// Init Rx DMA buffer, size, mode (spi and mem data size is 8 bit)
// Init Rx DMA buffer, size, mode (spi and mem data size is 8 bit)
dmaStreamSetMemory0 ( dmarx , rgbbuf ) ;
dmaStreamSetMemory0 ( dmarx , rgbbuf ) ;
dmaStreamSetTransactionSize ( dmarx , data_size ) ;
dmaStreamSetTransactionSize ( dmarx , data_size ) ;
dmaStreamSetMode ( dmarx , rxdmamode | STM32_DMA_CR_PSIZE_BYTE | STM32_DMA_CR_MSIZE_BYTE | STM32_DMA_CR_MINC ) ;
dmaStreamSetMode ( dmarx , rxdmamode | STM32_DMA_CR_PSIZE_BYTE | STM32_DMA_CR_MSIZE_BYTE |
STM32_DMA_CR_MINC ) ;
// Init dummy Tx DMA (for rx clock), size, mode (spi and mem data size is 8 bit)
// Init dummy Tx DMA (for rx clock), size, mode (spi and mem data size is 8 bit)
dmaStreamSetMemory0 ( dmatx , & dummy_tx ) ;
dmaStreamSetMemory0 ( dmatx , & dummy_tx ) ;
dmaStreamSetTransactionSize ( dmatx , data_size ) ;
dmaStreamSetTransactionSize ( dmatx , data_size ) ;
@ -481,23 +511,39 @@ void ili9341_read_memory(int x, int y, int w, int h, int len, uint16_t *out)
r = rgbbuf [ 0 ] ;
r = rgbbuf [ 0 ] ;
g = rgbbuf [ 1 ] ;
g = rgbbuf [ 1 ] ;
b = rgbbuf [ 2 ] ;
b = rgbbuf [ 2 ] ;
* out + + = RGB565 ( r , g , b ) ;
* out + + = RGB565 ( r , g , b ) ;
rgbbuf + = 3 ;
rgbbuf + = 3 ;
}
}
}
}
# endif
# endif
void setForegroundColor ( uint16_t fg ) { foreground_color = fg ; }
void ili9341_clear_screen ( void )
void setBackgroundColor ( uint16_t bg ) { background_color = bg ; }
{
ili9341_fill ( 0 , 0 , ILI9341_WIDTH , ILI9341_HEIGHT , background_color ) ;
}
void ili9341_set_foreground ( uint16_t fg )
{
foreground_color = fg ;
}
void ili9341_set_background ( uint16_t bg )
{
background_color = bg ;
}
void ili9341_setRotation ( uint8_t r ) {
void ili9341_set_rotation ( uint8_t r )
// static const uint8_t rotation_const[]={DISPLAY_ROTATION_0, DISPLAY_ROTATION_90, DISPLAY_ROTATION_180, DISPLAY_ROTATION_270};
{
// static const uint8_t rotation_const[]={DISPLAY_ROTATION_0, DISPLAY_ROTATION_90,
// DISPLAY_ROTATION_180, DISPLAY_ROTATION_270};
send_command ( ILI9341_MEMORY_ACCESS_CONTROL , 1 , & r ) ;
send_command ( ILI9341_MEMORY_ACCESS_CONTROL , 1 , & r ) ;
}
}
void blit8BitWidthBitmap ( uint16_t x , uint16_t y , uint16_t width , uint16_t height , const uint8_t * bitmap ) {
void blit8BitWidthBitmap ( uint16_t x , uint16_t y , uint16_t width , uint16_t height ,
const uint8_t * bitmap )
{
uint16_t * buf = spi_buffer ;
uint16_t * buf = spi_buffer ;
for ( uint16_t c = 0 ; c < height ; c + + ) {
for ( uint16_t c = 0 ; c < height ; c + + ) {
uint8_t bits = * bitmap + + ;
uint8_t bits = * bitmap + + ;
for ( uint16_t r = 0 ; r < width ; r + + ) {
for ( uint16_t r = 0 ; r < width ; r + + ) {
* buf + + = ( 0x80 & bits ) ? foreground_color : background_color ;
* buf + + = ( 0x80 & bits ) ? foreground_color : background_color ;
@ -507,9 +553,11 @@ void blit8BitWidthBitmap(uint16_t x, uint16_t y, uint16_t width, uint16_t height
ili9341_bulk ( x , y , width , height ) ;
ili9341_bulk ( x , y , width , height ) ;
}
}
void blit16BitWidthBitmap ( uint16_t x , uint16_t y , uint16_t width , uint16_t height , const uint16_t * bitmap ) {
static void blit16BitWidthBitmap ( uint16_t x , uint16_t y , uint16_t width , uint16_t height ,
const uint16_t * bitmap )
{
uint16_t * buf = spi_buffer ;
uint16_t * buf = spi_buffer ;
for ( uint16_t c = 0 ; c < height ; c + + ) {
for ( uint16_t c = 0 ; c < height ; c + + ) {
uint16_t bits = * bitmap + + ;
uint16_t bits = * bitmap + + ;
for ( uint16_t r = 0 ; r < width ; r + + ) {
for ( uint16_t r = 0 ; r < width ; r + + ) {
* buf + + = ( 0x8000 & bits ) ? foreground_color : background_color ;
* buf + + = ( 0x8000 & bits ) ? foreground_color : background_color ;
@ -531,36 +579,38 @@ void ili9341_drawstring(const char *str, int x, int y)
const uint8_t * char_buf = FONT_GET_DATA ( ch ) ;
const uint8_t * char_buf = FONT_GET_DATA ( ch ) ;
uint16_t w = FONT_GET_WIDTH ( ch ) ;
uint16_t w = FONT_GET_WIDTH ( ch ) ;
blit8BitWidthBitmap ( x , y , w , FONT_GET_HEIGHT , char_buf ) ;
blit8BitWidthBitmap ( x , y , w , FONT_GET_HEIGHT , char_buf ) ;
x + = w ;
x + = w ;
}
}
}
}
void ili9341_drawstringV ( const char * str , int x , int y ) {
void ili9341_drawstringV ( const char * str , int x , int y )
ili9341_setRotation ( DISPLAY_ROTATION_270 ) ;
{
ili9341_set_rotation ( DISPLAY_ROTATION_270 ) ;
ili9341_drawstring ( str , ILI9341_HEIGHT - y , x ) ;
ili9341_drawstring ( str , ILI9341_HEIGHT - y , x ) ;
ili9341_setR otation( DISPLAY_ROTATION_0 ) ;
ili9341_set_r otation( DISPLAY_ROTATION_0 ) ;
}
}
int ili9341_drawchar_size ( uint8_t ch , int x , int y , uint8_t size )
int ili9341_drawchar_size ( uint8_t ch , int x , int y , uint8_t size )
{
{
uint16_t * buf = spi_buffer ;
uint16_t * buf = spi_buffer ;
const uint8_t * char_buf = FONT_GET_DATA ( ch ) ;
const uint8_t * char_buf = FONT_GET_DATA ( ch ) ;
uint16_t w = FONT_GET_WIDTH ( ch ) ;
uint16_t w = FONT_GET_WIDTH ( ch ) ;
for ( int c = 0 ; c < FONT_GET_HEIGHT ; c + + , char_buf + + ) {
for ( int c = 0 ; c < FONT_GET_HEIGHT ; c + + , char_buf + + ) {
for ( int i = 0 ; i < size ; i + + ) {
for ( int i = 0 ; i < size ; i + + ) {
uint8_t bits = * char_buf ;
uint8_t bits = * char_buf ;
for ( int r = 0 ; r < w ; r + + , bits < < = 1 )
for ( int r = 0 ; r < w ; r + + , bits < < = 1 )
for ( int j = 0 ; j < size ; j + + )
for ( int j = 0 ; j < size ; j + + )
* buf + + = ( 0x80 & bits ) ? foreground_color : background_color ;
* buf + + = ( 0x80 & bits ) ? foreground_color : background_color ;
}
}
}
}
ili9341_bulk ( x , y , w * size , FONT_GET_HEIGHT * size ) ;
ili9341_bulk ( x , y , w * size , FONT_GET_HEIGHT * size ) ;
return w * size ;
return w * size ;
}
}
void ili9341_drawfont ( uint8_t ch , int x , int y )
void ili9341_drawfont ( uint8_t ch , int x , int y )
{
{
blit16BitWidthBitmap ( x , y , NUM_FONT_GET_WIDTH , NUM_FONT_GET_HEIGHT , NUM_FONT_GET_DATA ( ch ) ) ;
blit16BitWidthBitmap ( x , y , NUM_FONT_GET_WIDTH , NUM_FONT_GET_HEIGHT ,
NUM_FONT_GET_DATA ( ch ) ) ;
}
}
void ili9341_drawstring_size ( const char * str , int x , int y , uint8_t size )
void ili9341_drawstring_size ( const char * str , int x , int y , uint8_t size )
@ -568,10 +618,35 @@ void ili9341_drawstring_size(const char *str, int x, int y, uint8_t size)
while ( * str )
while ( * str )
x + = ili9341_drawchar_size ( * str + + , x , y , size ) ;
x + = ili9341_drawchar_size ( * str + + , x , y , size ) ;
}
}
#if 0
static void ili9341_pixel ( int x , int y , uint16_t color )
{
uint32_t xx = __REV16 ( x | ( ( x ) < < 16 ) ) ;
uint32_t yy = __REV16 ( y | ( ( y ) < < 16 ) ) ;
send_command ( ILI9341_COLUMN_ADDRESS_SET , 4 , ( uint8_t * ) & xx ) ;
send_command ( ILI9341_PAGE_ADDRESS_SET , 4 , ( uint8_t * ) & yy ) ;
send_command ( ILI9341_MEMORY_WRITE , 2 , & color ) ;
}
# endif
# define SWAP(x, y) { int z = x; x = y; y = z; }
# define SWAP(x,y) { int z=x; x = y; y = z; }
void ili9341_line ( int x0 , int y0 , int x1 , int y1 )
void ili9341_line ( int x0 , int y0 , int x1 , int y1 )
{
{
#if 0
// modifed Bresenham's line algorithm, see https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
int dx = x1 - x0 , sx = 1 ; if ( dx < 0 ) { dx = - dx ; sx = - 1 ; }
int dy = y1 - y0 , sy = 1 ; if ( dy < 0 ) { dy = - dy ; sy = - 1 ; }
int err = ( dx > dy ? dx : - dy ) / 2 ;
while ( 1 ) {
ili9341_pixel ( x0 , y0 , DEFAULT_FG_COLOR ) ;
if ( x0 = = x1 & & y0 = = y1 )
break ;
int e2 = err ;
if ( e2 > - dx ) { err - = dy ; x0 + = sx ; }
if ( e2 < dy ) { err + = dx ; y0 + = sy ; }
}
# endif
if ( x0 > x1 ) {
if ( x0 > x1 ) {
SWAP ( x0 , x1 ) ;
SWAP ( x0 , x1 ) ;
@ -611,20 +686,8 @@ static const uint16_t colormap[] = {
RGBHEX ( 0x00ffff ) , RGBHEX ( 0xff00ff ) , RGBHEX ( 0xffff00 )
RGBHEX ( 0x00ffff ) , RGBHEX ( 0xff00ff ) , RGBHEX ( 0xffff00 )
} ;
} ;
static void ili9341_pixel ( int x , int y , int color )
{
uint8_t xx [ 4 ] = { x > > 8 , x , ( x + 1 ) > > 8 , ( x + 1 ) } ;
uint8_t yy [ 4 ] = { y > > 8 , y , ( y + 1 ) > > 8 , ( y + 1 ) } ;
uint8_t cc [ 2 ] = { color > > 8 , color } ;
send_command ( ILI9341_COLUMN_ADDRESS_SET , 4 , xx ) ;
send_command ( ILI9341_PAGE_ADDRESS_SET , 4 , yy ) ;
send_command ( ILI9341_MEMORY_WRITE , 2 , cc ) ;
//send_command16(ILI9341_MEMORY_WRITE, color);
}
void ili9341_test ( int mode )
void ili9341_test ( int mode )
{
{
chMtxLock ( & mutex_ili9341 ) ;
int x , y ;
int x , y ;
int i ;
int i ;
switch ( mode ) {
switch ( mode ) {
@ -666,6 +729,5 @@ void ili9341_test(int mode)
ili9341_line ( 0 , 100 , 100 , 0 ) ;
ili9341_line ( 0 , 100 , 100 , 0 ) ;
break ;
break ;
}
}
chMtxUnlock ( & mutex_ili9341 ) ;
}
}
# endif
# endif