Quellcode durchsuchen

Code refactoring.

Vladimir N. Shilov vor 2 Jahren
Ursprung
Commit
d413623dba
5 geänderte Dateien mit 139 neuen und 100 gelöschten Zeilen
  1. 16 9
      inc/board.h
  2. 4 4
      inc/main.h
  3. 0 2
      inc/rtos.h
  4. 117 83
      src/board.c
  5. 2 2
      src/rtos.c

+ 16 - 9
inc/board.h

@@ -6,21 +6,28 @@
 #include "main.h"
 
 /* Type Defs */
+typedef enum {
+  Disable = 0,
+  Emable = 1
+} dis_en_t;
+
+
+/* Defines */
+#define FRAMEBUFFER_ROTATE
 
 /* Exported macros */
 /* Constants */
-/* Defines */
 /* Variables */
+extern uint8_t display_Buffer[];
+
 /* Exported funcions */
 void Board_Init(void);
-void HT1632C_Init(void);
-void HT1632C_Write_bits(uint16_t data, uint16_t nbits);
-void HT1632C_Write_Cmd(uint16_t data, uint16_t nbits);
-void HT1632C_Write_Data(const uint8_t * data, uint8_t addr, uint8_t len);
-void HT1632C_Write_Buffer(void);
-void HT1632C_PWM(uint8_t pwm);
-void HT1632C_Blink(uint8_t state);
-void HT1632C_LedSt(uint8_t state);
+void display_Init(void);
+void display_WriteData(const uint8_t * data, uint8_t addr, uint8_t len);
+void display_WriteBuffer(void);
+void display_PWM(uint8_t pwm);
+void display_BlinkState(dis_en_t state);
+void display_LedState(dis_en_t state);
 void EXTI4_15_IRQHandler(void);
 void DMA1_Channel2_3_IRQHandler(void);
 void display_test(void);

+ 4 - 4
inc/main.h

@@ -30,13 +30,13 @@ extern "C" {
 
 /* Exported types ------------------------------------------------------------*/
 typedef enum {
-  DOWN = 0,
-  UP = 1
+  Down = 0,
+  Up = 1
 } updown_t;
 
 typedef enum {
-  OFF = 0,
-  ON = 1
+  Off = 0,
+  On = 1
 } onoff_t;
 
 typedef struct t_flag {

+ 0 - 2
inc/rtos.h

@@ -19,8 +19,6 @@
 #ifndef RTOS_H
 #define RTOS_H
 
-#include "stm32f0xx.h"
-
 /**
  * @brief Number of tasks
  */

+ 117 - 83
src/board.c

@@ -1,7 +1,7 @@
 #include "main.h"
 
 /* private defines */
-#define SPI_BUFFER_SIZE   (uint16_t)(24)
+#define DISPLAY_COLUMNS   24
 
 /* private macros */
 #define HT1632C_CS_ON     GPIOA->BRR = (1<<15)
@@ -12,19 +12,22 @@
 #define HT1632C_DATA_1    GPIOB->BSRR = (1<<5)
 
 /* private variables */
-static uint8_t display_Buffer[SPI_BUFFER_SIZE] = {0};
+uint8_t display_Buffer[DISPLAY_COLUMNS] = {0};
 
 /* private typedef */
 /* private functions */
 static void GPIO_Init(void);
 static void I2C1_Init(void);
 static void SPI1_Init(void);
+static void GPIO_SPI_SW(void);
+static void GPIO_SPI_HW(void);
 //static void TIM1_Init(void);
 //static void TIM3_Init(void);
 //static void TIM14_Init(void);
 //static void TIM16_Init(void);
 //static void TIM17_Init(void);
 //static void USART1_UART_Init(void);
+static void _display_WriteBits(uint16_t data, uint16_t nbits);
 
 /* Board perephireal Configuration  */
 void Board_Init(void)
@@ -73,6 +76,8 @@ void Board_Init(void)
 //  TIM16_Init();
 //  TIM17_Init();
 //  USART1_UART_Init();
+
+  display_Init();
 }
 
 /* output 'L', 'G', '5' */
@@ -108,9 +113,9 @@ void display_test(void) {
   display_Buffer[22] = 0x44;
   display_Buffer[23] = 0x28;
 
-//  HT1632C_Write_Data(display_Buffer, 0x0, SPI_BUFFER_SIZE);
+//  display_WriteData(display_Buffer, 0x0, DISPLAY_COLUMNS);
 //  tdelay_ms(1000);
-  HT1632C_Write_Buffer();
+  display_WriteBuffer();
 }
 
 /**
@@ -173,6 +178,24 @@ static void GPIO_Init(void)
                 |GPIO_PUPDR_PUPDR6_0|GPIO_PUPDR_PUPDR7_0|GPIO_PUPDR_PUPDR12_0);
 }
 
+/**
+  * @brief Configure SPI pin for software mode
+  */
+static void GPIO_SPI_SW(void) {
+  // GPIO pin mode
+  GPIOB->MODER &= ~(GPIO_MODER_MODER3 | GPIO_MODER_MODER5);
+  GPIOB->MODER |= (GPIO_MODER_MODER3_0 | GPIO_MODER_MODER5_0);
+}
+
+/**
+  * @brief Configure SPI pin for hardware mode
+  */
+static void GPIO_SPI_HW(void) {
+  // SPI pin mode
+  GPIOB->MODER &= ~(GPIO_MODER_MODER3 | GPIO_MODER_MODER5);
+  GPIOB->MODER |= (GPIO_MODER_MODER3_1 | GPIO_MODER_MODER5_1);
+}
+
 /**
   * @brief I2C1 Initialization Function
   * @param None
@@ -216,28 +239,27 @@ static void SPI1_Init(void)
 /**
  * @brief Initialization HT1632C
  */
-void HT1632C_Init(void) {
-  // GPIO pin mode
-  GPIOB->MODER &= ~(GPIO_MODER_MODER3 | GPIO_MODER_MODER5);
-  GPIOB->MODER |= (GPIO_MODER_MODER3_0 | GPIO_MODER_MODER5_0);
+void display_Init(void) {
+  /* Wait for SPI */
+  while ((SPI1->SR & SPI_SR_BSY) != 0) { __NOP(); }
 
-  //HT1632C_Write_Cmd(0x802, 0x800);
-  //HT1632C_Write_Cmd(0x806, 0x800);
+  GPIO_SPI_SW();
   HT1632C_CS_ON;
-  HT1632C_Write_bits(0x802, 0x800); // Turn on system oscillator
-  HT1632C_Write_bits(0x6, 0x100); // Turn on LED duty cycle generator
-  HT1632C_CS_OFF;
 
-  // SPI pin mode
-  GPIOB->MODER &= ~(GPIO_MODER_MODER3 | GPIO_MODER_MODER5);
-  GPIOB->MODER |= (GPIO_MODER_MODER3_1 | GPIO_MODER_MODER5_1);
+  // Turn on system oscillator
+  _display_WriteBits(0x802, 0x800);
+  // Turn on LED duty cycle generator
+  _display_WriteBits(0x6, 0x100);
+
+  HT1632C_CS_OFF;
+  GPIO_SPI_HW();
 }
 
 /**
  * @brief Set HT1632C PWM Value
  * @param pwm value in 0-15
  */
-void HT1632C_PWM(uint8_t pwm) {
+void display_PWM(uint8_t pwm) {
   // check value
   if (pwm > 15) {
     pwm = 15;
@@ -245,68 +267,66 @@ void HT1632C_PWM(uint8_t pwm) {
   // align value
   pwm <<= 1;
 
-  // GPIO pin mode
-  GPIOB->MODER &= ~(GPIO_MODER_MODER3 | GPIO_MODER_MODER5);
-  GPIOB->MODER |= (GPIO_MODER_MODER3_0 | GPIO_MODER_MODER5_0);
+  /* Wait for SPI */
+  while ((SPI1->SR & SPI_SR_BSY) != 0) { __NOP(); }
+
+  GPIO_SPI_SW();
 
   HT1632C_CS_ON;
-  HT1632C_Write_bits((0x940|pwm), 0x800);
+  _display_WriteBits((0x940|pwm), 0x800);
   HT1632C_CS_OFF;
 
-  // SPI pin mode
-  GPIOB->MODER &= ~(GPIO_MODER_MODER3 | GPIO_MODER_MODER5);
-  GPIOB->MODER |= (GPIO_MODER_MODER3_1 | GPIO_MODER_MODER5_1);
+  GPIO_SPI_HW();
 }
 
 /**
  * @brief Set HT1632C Blink state
  * @param state 0 for off, any other value for on
  */
-void HT1632C_BlinkState(uint8_t state) {
-  // GPIO pin mode
-  GPIOB->MODER &= ~(GPIO_MODER_MODER3 | GPIO_MODER_MODER5);
-  GPIOB->MODER |= (GPIO_MODER_MODER3_0 | GPIO_MODER_MODER5_0);
+void display_BlinkState(dis_en_t state) {
+  /* Wait for SPI */
+  while ((SPI1->SR & SPI_SR_BSY) != 0) { __NOP(); }
+
+  GPIO_SPI_SW();
 
   HT1632C_CS_ON;
-  if (state == 0) {
-    HT1632C_Write_bits(0x810, 0x800); // Blink off
+  if (state == Disable) {
+    _display_WriteBits(0x810, 0x800); // Blink off
   } else {
-    HT1632C_Write_bits(0x812, 0x800); // Blink on
+    _display_WriteBits(0x812, 0x800); // Blink on
   }
   HT1632C_CS_OFF;
 
-  // SPI pin mode
-  GPIOB->MODER &= ~(GPIO_MODER_MODER3 | GPIO_MODER_MODER5);
-  GPIOB->MODER |= (GPIO_MODER_MODER3_1 | GPIO_MODER_MODER5_1);
+  GPIO_SPI_HW();
 }
 
 /**
  * @brief Set HT1632C Led state
  * @param state 0 for off, any other value for on
  */
-void HT1632C_LedState(uint8_t state) {
-  // GPIO pin mode
-  GPIOB->MODER &= ~(GPIO_MODER_MODER3 | GPIO_MODER_MODER5);
-  GPIOB->MODER |= (GPIO_MODER_MODER3_0 | GPIO_MODER_MODER5_0);
+void display_LedState(dis_en_t state) {
+  /* Wait for SPI */
+  while ((SPI1->SR & SPI_SR_BSY) != 0) { __NOP(); }
+
+  GPIO_SPI_SW();
 
   HT1632C_CS_ON;
-  if (state == 0) {
-    HT1632C_Write_bits(0x804, 0x800); // Led off
+  if (state == Disable) {
+    _display_WriteBits(0x804, 0x800); // Led off
   } else {
-    HT1632C_Write_bits(0x806, 0x800); // Led on
+    _display_WriteBits(0x806, 0x800); // Led on
   }
   HT1632C_CS_OFF;
 
-  // SPI pin mode
-  GPIOB->MODER &= ~(GPIO_MODER_MODER3 | GPIO_MODER_MODER5);
-  GPIOB->MODER |= (GPIO_MODER_MODER3_1 | GPIO_MODER_MODER5_1);
+  GPIO_SPI_HW();
 }
 
 /**
  * @brief Write <nbits> bit of data to selected HT1632Cs
- * @param data words to write, nbits of bits (1<<(num-1))
+ * @param data words to write
+ * @param nbits num of bits (1<<(num-1))
  */
-void HT1632C_Write_bits(const uint16_t data, uint16_t nbits) {
+void _display_WriteBits(const uint16_t data, uint16_t nbits) {
   do {
     HT1632C_WR_LOW;
 
@@ -315,19 +335,10 @@ void HT1632C_Write_bits(const uint16_t data, uint16_t nbits) {
     } else {
       HT1632C_DATA_0;
     }
-//    _delay_half_us();
-    HT1632C_WR_HGH;
-//    _delay_half_us();
-  } while (nbits >>= 1);
-}
 
-/**
- * @brief Write CMD to selected HT1632Cs
- * @param data words to write
- * @param nbits cmd len in bits (1<<(num-1))
- */
-void HT1632C_Write_Cmd(const uint16_t data, uint16_t nbits) {
+    HT1632C_WR_HGH;
 
+  } while (nbits >>= 1);
 }
 
 /**
@@ -336,41 +347,57 @@ void HT1632C_Write_Cmd(const uint16_t data, uint16_t nbits) {
  * @param addr begin address
  * @param len bytes to write
  */
-void HT1632C_Write_Data(const uint8_t * data, const uint8_t addr, const uint8_t len) {
-  uint16_t a = 0x280 | (addr & 0x7f);
+void display_WriteData(const uint8_t * data, uint8_t addr, uint8_t len) {
+  /* check given values */
+  if (addr >= DISPLAY_COLUMNS) {
+    addr = DISPLAY_COLUMNS - 1;
+  }
+  if ((addr + len) > DISPLAY_COLUMNS) {
+    len = DISPLAY_COLUMNS - addr;
+  }
+
+#ifdef FRAMEBUFFER_ROTATE
+  /* Copy given data to framebuffer */
+  int i;
+  for (i=0; i<len; i++) {
+    addr += i;
+    display_Buffer[addr] = data[addr];
+  }
+  display_WriteBuffer();
+#else
+  uint16_t a = 0x280 | addr;
+
+  /* Wait for SPI */
+  while ((SPI1->SR & SPI_SR_BSY) != 0) { __NOP(); }
 
   /* DMA Source addr: Address of the SPI buffer. */
-  DMA1_Channel3->CMAR = (uint32_t)&data[0];
+  DMA1_Channel3->CMAR = data;
   /* Set DMA data transfer length (SPI buffer length). */
   DMA1_Channel3->CNDTR = len;
 
-  // GPIO pin mode
-  GPIOB->MODER &= ~(GPIO_MODER_MODER3 | GPIO_MODER_MODER5);
-  GPIOB->MODER |= (GPIO_MODER_MODER3_0 | GPIO_MODER_MODER5_0);
+  GPIO_SPI_SW();
 
   HT1632C_CS_ON;
-  HT1632C_Write_bits(a, 0x200);
+  _display_WriteBits(a, 0x200);
 
-  // SPI pin mode
-  GPIOB->MODER &= ~(GPIO_MODER_MODER3 | GPIO_MODER_MODER5);
-  GPIOB->MODER |= (GPIO_MODER_MODER3_1 | GPIO_MODER_MODER5_1);
+  GPIO_SPI_HW();
 
   // start transfer
   SPI1->CR2 |= (SPI_CR2_TXDMAEN);
   DMA1_Channel3->CCR |= DMA_CCR_EN;
   // End of transaction in DMA1_Channel2_3_IRQHandler()
+#endif /* FRAMEBUFFER_ROTATE */
 }
 
 /**
  * @brief Write all buffer to HT1632C
  */
-void HT1632C_Write_Buffer(void) {
-static uint8_t spi_buf[SPI_BUFFER_SIZE] = {0};
+void display_WriteBuffer(void) {
+  /* Wait for SPI */
+  while ((SPI1->SR & SPI_SR_BSY) != 0) { __NOP(); }
 
-  /* DMA Source addr: Address of the SPI buffer. */
-  DMA1_Channel3->CMAR = (uint32_t)&spi_buf;
-  /* Set DMA data transfer length (SPI buffer length). */
-  DMA1_Channel3->CNDTR = SPI_BUFFER_SIZE;
+#ifdef FRAMEBUFFER_ROTATE
+  static uint8_t spi_buf[DISPLAY_COLUMNS] = {0};
 
   /* Rotate display buffer */
   uint8_t i, j, p = 0x01;
@@ -407,19 +434,25 @@ static uint8_t spi_buf[SPI_BUFFER_SIZE] = {0};
     p <<= 1;
   }
 
-  /* Toggle pins to GPIO mode */
-  GPIOB->MODER &= ~(GPIO_MODER_MODER3 | GPIO_MODER_MODER5);
-  GPIOB->MODER |= (GPIO_MODER_MODER3_0 | GPIO_MODER_MODER5_0);
+  /* DMA Source addr: Address of the SPI buffer. */
+  DMA1_Channel3->CMAR = (uint32_t)&spi_buf;
+#else
+  /* DMA Source addr: Address of the SPI buffer. */
+  DMA1_Channel3->CMAR = (uint32_t)&display_Buffer;
+#endif /* FRAMEBUFFER_ROTATE */
+
+  /* Set DMA data transfer length (SPI buffer length). */
+  DMA1_Channel3->CNDTR = DISPLAY_COLUMNS;
+
+  GPIO_SPI_SW();
 
   HT1632C_CS_ON;
-  HT1632C_Write_bits(0x280, 0x200);
+  _display_WriteBits(0x280, 0x200);
 
-  /* Toggle pins to SPI mode */
-  GPIOB->MODER &= ~(GPIO_MODER_MODER3 | GPIO_MODER_MODER5);
-  GPIOB->MODER |= (GPIO_MODER_MODER3_1 | GPIO_MODER_MODER5_1);
+  GPIO_SPI_HW();
 
   /* start transfer */
-  SPI1->CR2 |= (SPI_CR2_TXDMAEN);
+  SPI1->CR2 |= SPI_CR2_TXDMAEN;
   DMA1_Channel3->CCR |= DMA_CCR_EN;
   /* End of transaction in DMA1_Channel2_3_IRQHandler() */
 }
@@ -445,9 +478,10 @@ void DMA1_Channel2_3_IRQHandler(void) {
     // clear interrupt flag
     DMA1->IFCR = DMA_IFCR_CTCIF3;
     // disable transfer
+    SPI1->CR2 &= ~(SPI_CR2_TXDMAEN);
     DMA1_Channel3->CCR &= ~(DMA_CCR_EN);
-    // wite for spi
-    while ((SPI1->SR & SPI_SR_BSY) != 0) { __NOP(); };
+    // wite for spi transaction
+    while ((SPI1->SR & SPI_SR_BSY) != 0) { __NOP(); }
     // deselect chip
     HT1632C_CS_OFF;
   }

+ 2 - 2
src/rtos.c

@@ -8,14 +8,14 @@ typedef struct task
    void (*pFunc) (void);  /**< @brief function pointer */
    uint32_t delay;  /**< @brief delay before the first start of the task */
    uint32_t period; /**< @brief task start period */ 
-   uint8_t run;     /**< @brief task ready flag */
+        int run;    /**< @brief task ready flag */
 } task_t;
 
 /*
  * Local ariables
  */
 static volatile task_t TaskArray[MAX_TASKS];/**< @brief task queue */
-static volatile uint8_t arrayTail;          /**< @brief "tail" of the queue */
+static volatile int arrayTail;              /**< @brief "tail" of the queue */
 
 static volatile uint32_t TDelay;            /**< @brief delay counter */