Jelajahi Sumber

Small improvement for display driver.

Vladimir N. Shilov 1 tahun lalu
induk
melakukan
87c15fde7d
2 mengubah file dengan 56 tambahan dan 21 penghapusan
  1. 56 20
      lib/st7735/st7735.c
  2. 0 1
      lib/st7735/st7735.h

+ 56 - 20
lib/st7735/st7735.c

@@ -1,4 +1,3 @@
-/* vim: set ai et ts=4 sw=4: */
 #include "ch.h"
 #include "hal.h"
 #include "malloc.h"
@@ -102,17 +101,58 @@ static void ST7735_Reset(void) {
   //HAL_GPIO_WritePin(ST7735_RES_GPIO_Port, ST7735_RES_Pin, GPIO_PIN_SET);
 }
 
+/**
+ * @brief Send command
+ * 
+ * @param cmd command for display
+ */
 static void ST7735_WriteCommand(uint8_t cmd) {
   LCD_DC_CMD;
-  // !!! TODO: немає сенсу пересилати 1 байт за допомогою DMA. треба переробити
-  spiSend(&ST7735_SPI_PORT, sizeof(cmd), &cmd);
+  
+  // wite while spi is busy
+  while (!(SPI1->SR & SPI_SR_TXE));
+  // send data
+  SPI1->DR = cmd;
+  // wite for data to be transmitted
+  while (!(SPI1->SR & SPI_SR_TXE));
+  // ... and spi is busy
+  while ((SPI1->SR & SPI_SR_BSY));
 }
 
+/**
+ * @brief Send 8-bit data using DMA
+ * 
+ * @param buff data to be sended
+ * @param buff_size amount of bytes
+ */
 static void ST7735_WriteData(uint8_t* buff, size_t buff_size) {
   LCD_DC_DATA;
   spiSend(&ST7735_SPI_PORT, buff_size, buff);
 }
 
+/**
+ * @brief Send 16-bit data trough reg
+ * 
+ * @param data to send to display
+ */
+static void ST7735_WriteData16(uint16_t data) {
+  LCD_DC_DATA;
+  //spiSend(&ST7735_SPI_PORT, buff_size, buff);
+  
+  // set 16-bit data
+  SPI1->CR1 |= SPI_CR1_DFF;
+  // wite while spi is busy
+  while (!(SPI1->SR & SPI_SR_TXE));
+  // send data
+  SPI1->DR = data;
+  // wite for data too be transmitted
+  while (!(SPI1->SR & SPI_SR_TXE));
+  // ... and spi is unbusy
+  while ((SPI1->SR & SPI_SR_BSY));
+  // set 8-bit data
+  SPI1->CR1 &= ~(SPI_CR1_DFF);
+}
+
 static void ST7735_ExecuteCommandList(const uint8_t *addr) {
   uint8_t numCommands, numArgs;
   uint16_t ms;
@@ -157,7 +197,7 @@ static void ST7735_SetAddressWindow(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t
   ST7735_WriteCommand(ST7735_RAMWR);
 }
 
-void ST7735_Init() {
+void ST7735_Init(void) {
   ST7735_Select();
   ST7735_Reset();
   ST7735_ExecuteCommandList(init_cmds1);
@@ -166,18 +206,21 @@ void ST7735_Init() {
   ST7735_Unselect();
 }
 
+/**
+ * @brief Draw single pixel
+ * 
+ * @param x coordinate
+ * @param y coordinate
+ * @param color pixel colour
+ */
 void ST7735_DrawPixel(uint16_t x, uint16_t y, uint16_t color) {
   if ((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT)) {
     return;
   }
 
   ST7735_Select();
-
   ST7735_SetAddressWindow(x, y, x+1, y+1);
-  uint8_t data[] = {color >> 8, color & 0xFF};
-  // !!! TODO: тут пересилається 2 байти за допомогою DMA. треба переробити
-  ST7735_WriteData(data, sizeof(data));
-
+  ST7735_WriteData16(color);
   ST7735_Unselect();
 }
 
@@ -189,16 +232,10 @@ static void ST7735_WriteChar(uint16_t x, uint16_t y, char ch, FontDef font, uint
   for(i = 0; i < font.height; i++) {
     b = font.data[(ch - 32) * font.height + i];
     for(j = 0; j < font.width; j++) {
-      //unnecessary transformations
-      //if ((b << j) & 0x8000) {
       if (b & 0x8000) {
-        uint8_t data[] = {color >> 8, color & 0xFF};
-  // !!! TODO: тут пересилається 2 байти за допомогою DMA. треба переробити
-        ST7735_WriteData(data, sizeof(data));
+        ST7735_WriteData16(color);
       } else {
-        uint8_t data[] = {bgcolor >> 8, bgcolor & 0xFF};
-  // !!! TODO: тут пересилається 2 байти за допомогою DMA. треба переробити
-        ST7735_WriteData(data, sizeof(data));
+        ST7735_WriteData16(bgcolor);
       }
       b <<= 1;
     }
@@ -218,7 +255,7 @@ void ST7735_WriteString(uint16_t x, uint16_t y, const char* str, FontDef font, u
 
       if (*str == ' ') {
         // skip spaces in the beginning of the new line
-        str++;
+        str ++;
         continue;
       }
     }
@@ -246,11 +283,10 @@ void ST7735_FillRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16
   ST7735_Select();
   ST7735_SetAddressWindow(x, y, x+w-1, y+h-1);
 
-  uint8_t data[] = {color >> 8, color & 0xFF};
   LCD_DC_DATA;
   for (y = h; y > 0; y--) {
     for (x = w; x > 0; x--) {
-      spiSend(&ST7735_SPI_PORT, sizeof(data), data);
+      ST7735_WriteData16(color);
     }
   }
 

+ 0 - 1
lib/st7735/st7735.h

@@ -1,4 +1,3 @@
-/* vim: set ai et ts=4 sw=4: */
 #ifndef __ST7735_H__
 #define __ST7735_H__