Browse Source

Implemented output to MAX7219.

Vladimir N. Shilov 5 years ago
parent
commit
b167a89a25
9 changed files with 163 additions and 100 deletions
  1. 1 0
      .gitignore
  2. 11 5
      VAPC2.cbp
  3. 1 1
      lib/ina219.h
  4. 37 28
      lib/max7219.c
  5. 42 50
      lib/max7219.h
  6. 3 0
      lib/rtos.c
  7. 0 1
      lib/rtos.h
  8. 58 7
      src/main.c
  9. 10 8
      src/stm8s_it.c

+ 1 - 0
.gitignore

@@ -7,3 +7,4 @@ EWSTM8/Debug
 EWSTM8/Release
 EWSTM8/settings
 EWSTM8/*.dep
+*.log

+ 11 - 5
VAPC2.cbp

@@ -3,22 +3,28 @@
 	<FileVersion major="1" minor="6" />
 	<Project>
 		<Option title="VAPC2" />
+		<Option makefile_is_custom="1" />
 		<Option pch_mode="2" />
-		<Option compiler="iar_stm8_compiler" />
+		<Option compiler="copy_of_iar_stm8_compiler" />
+		<MakeCommands>
+			<Build command="make" />
+			<CompileFile command="" />
+			<Clean command="make clean" />
+			<DistClean command="" />
+			<AskRebuildNeeded command="" />
+			<SilentBuild command="make &gt; $(CMD_NULL)" />
+		</MakeCommands>
 		<Build>
 			<Target title="Release">
 				<Option output="bin/Release/VAPC2" prefix_auto="1" extension_auto="1" />
 				<Option object_output="obj/Release/" />
 				<Option type="1" />
-				<Option compiler="iar_stm8_compiler" />
+				<Option compiler="copy_of_iar_stm8_compiler" />
 				<Compiler>
 					<Add option="-Oh" />
 				</Compiler>
 			</Target>
 		</Build>
-		<Compiler>
-			<Add option="--remarks" />
-		</Compiler>
 		<Unit filename="StdPerphDrv/inc/stm8s.h" />
 		<Unit filename="StdPerphDrv/inc/stm8s_adc1.h" />
 		<Unit filename="StdPerphDrv/inc/stm8s_adc2.h" />

+ 1 - 1
lib/ina219.h

@@ -21,7 +21,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #ifndef INA219_h
 #define INA219_h
 
-#include "stm32f0xx.h"
+#include "stm8s.h"
 
 #define CURRENT_SHUNT_RESISTENCE    20
 #define INA219_ADDRESS              (uint8_t)(0x40<<1)

+ 37 - 28
lib/max7219.c

@@ -12,28 +12,39 @@
 
 #include "max7219.h"
 
-#define SPI_PORT        GPIOB
-#define SPI_CS          GPIO_Pin_4
-#define SPI_SCK         GPIO_Pin_5
-#define SPI_MOSI        GPIO_Pin_6
+#define SPI_PORT        GPIOC
+#define SPI_SCK         GPIO_PIN_5
+#define SPI_MOSI        GPIO_PIN_6
 #define SPI_PINS        (SPI_SCK|SPI_MOSI)
+#define SPI_LOAD_PORT   GPIOD
+#define SPI_LOAD        GPIO_PIN_3
+
+#define GPIO_SetBits(port, pin)     port->ODR |= (uint8_t)pin
+#define GPIO_ResetBits(port, pin)   GPIOx->ODR &= (uint8_t)(~PortPins)
 
 void MAX7219_Config(void) {
-    SPI_DeInit(SPI1);
-    /* Enable clock for SPI */
-    CLK_PeripheralClockConfig(CLK_Peripheral_SPI1, ENABLE);
-    /* Set the MOSI,MISO and SCK at high level */
-    GPIO_ExternalPullUpConfig(SPI_PORT, SPI_PINS, ENABLE);
-    /* Configure LOAD pin */
-    GPIO_Init(SPI_PORT, SPI_CS, GPIO_Mode_Out_PP_High_Fast);
-    GPIO_SetBits(SPI_PORT, SPI_CS);
-    /* Init SPI */
-    SPI_Init(SPI1, SPI_FirstBit_MSB, SPI_BaudRatePrescaler_2, SPI_Mode_Master,
-             SPI_CPOL_High, SPI_CPHA_2Edge, SPI_Direction_1Line_Tx, SPI_NSS_Soft, 0x00);
-    /* SPI Enable */
-    SPI_Cmd(SPI1, ENABLE);
+  /* Set the MOSI,MISO and SCK at high level */
+  SPI_PORT->CR1 |= (uint8_t)SPI_PINS;
+
+  /* Configure LOAD pin to Push-Pull, High, Fast*/
+  SPI_LOAD_PORT->ODR |= SPI_LOAD;
+  SPI_LOAD_PORT->DDR |= SPI_LOAD;
+  SPI_LOAD_PORT->CR1 |= SPI_LOAD;
+  SPI_LOAD_PORT->CR2 &= (uint8_t)(~(SPI_LOAD));
+
+  /* Enable clock for SPI */
+  CLK->PCKENR1 |= 1 << 0x01;
+
+  /* SPI_MODE_MASTER, SPI_FIRSTBIT_MSB, SPI_BAUDRATEPRESCALER_2, SPI_CLOCKPOLARITY_HIGH, SPI_CLOCKPHASE_2EDGE */
+  SPI->CR1 = 0x04 | 0x00 | 0x00 | 0x02 | 0x01;
+
+  /* SPI_DATADIRECTION_1LINE_TX, SPI_NSS_SOFT */
+  SPI->CR2 = 0xC0 | 0x02 | SPI_CR2_SSI;
+
+  /* SPI Enable */
+  SPI->CR1 |= SPI_CR1_SPE;
 
-    /* Настройка MAX71219 */
+  /* Настройка MAX71219 */
 	MAX7219_WriteData(RegDecodeMode, 0x00);            // все без BCD декодирования
 	MAX7219_WriteData(RegScanLimit, MAX7219_DIGITS);   // сколько цифр используем
 	MAX7219_WriteData(RegIntensity, MAX7219_BRIGHT);   // яркость из 16
@@ -44,24 +55,22 @@ void MAX7219_Config(void) {
 void MAX7219_WriteData(max7219_reg_t reg, uint8_t data)
 {
     /*!< Wait wait until the completion of the transfer. */
-    while (SPI_GetFlagStatus(SPI1, SPI_FLAG_BSY) == SET) {}
+    while (SPI_GetFlagStatus(SPI_FLAG_BSY) == SET) {}
     /* Down LOAD pin */
-    GPIO_ResetBits(SPI_PORT, SPI_CS);
+    SPI_LOAD_PORT->ODR &= (uint8_t)(~SPI_LOAD);
 
     /*!< Wait until the transmit buffer is empty */
-    while (SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET) {}
+    while (SPI_GetFlagStatus(SPI_FLAG_TXE) == RESET) {}
     /* Send the register where the data will be stored */
-    //SPI_SendData(SPI1, reg);
-    SPI1->DR = reg; /* Write in the DR register the data to be sent*/
+    SPI->DR = reg; /* Write in the DR register the data to be sent*/
 
     /*!< Wait until the transmit buffer is empty */
-    while (SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET) {}
+    while (SPI_GetFlagStatus(SPI_FLAG_TXE) == RESET) {}
     /* Send the data to be stored */
-    //SPI_SendData(SPI1, data);
-    SPI1->DR = data; /* Write in the DR register the data to be sent*/
+    SPI->DR = data; /* Write in the DR register the data to be sent*/
 
     /*!< Wait wait until the completion of the transfer. */
-    while (SPI_GetFlagStatus(SPI1, SPI_FLAG_BSY) == SET) {}
+    while (SPI_GetFlagStatus(SPI_FLAG_BSY) == SET) {}
     /* Up LOAD pin */
-    GPIO_SetBits(SPI_PORT, SPI_CS);
+    SPI_LOAD_PORT->ODR |= SPI_LOAD;
 }

+ 42 - 50
lib/max7219.h

@@ -19,12 +19,6 @@
 #include "stm8s.h"
 
 /* Exported defines ----------------------------------------------------------*/
-#define SYM_DOT   0x10
-
-// symbols Для BCD
-#define MAX7219_CHAR_BLANK	0x0F
-#define MAX7219_CHAR_FULL	  0x88
-
 #define MAX7219_ON			0x01
 #define MAX7219_OFF			0x00
 #define MAX7219_BRIGHT	0x08
@@ -35,14 +29,14 @@
 /* Exported types ------------------------------------------------------------*/
 typedef enum _max7219_reg {
   RegNoOp        = 0x00,
-  RegDigit0      = 0x03,
-  RegDigit1      = 0x08,
-  RegDigit2      = 0x02,
-  RegDigit3      = 0x01,
-  RegDigit4      = 0x07,
+  RegDigit0      = 0x01,
+  RegDigit1      = 0x07,
+  RegDigit2      = 0x03,
+  RegDigit3      = 0x06,
+  RegDigit4      = 0x05,
   RegDigit5      = 0x04,
-  RegDigit6      = 0x06,
-  RegDigit7      = 0x05,
+  RegDigit6      = 0x08,
+  RegDigit7      = 0x02,
   RegDecodeMode  = 0x09,
   RegIntensity   = 0x0A,
   RegScanLimit   = 0x0B,
@@ -52,49 +46,47 @@ typedef enum _max7219_reg {
 
 // соответсвие бит сегментам
 typedef enum _max7219_seg {
-  SegA    = 1,
-  SegB    = 3,
-  SegC    = 2,
-  SegD    = 5,
-  SegE    = 6,
-  SegF    = 0,
-  SegG    = 7,
-  SegDP   = 4,
+  SegA    = 4,
+  SegB    = 0,
+  SegC    = 1,
+  SegD    = 7,
+  SegE    = 3,
+  SegF    = 2,
+  SegG    = 5,
+  SegDP   = 6,
 } max7219_seg_t;
 
 // symbols без кодирования
 typedef enum _max7219_sym {
-  Sym_0       = 0x6F,
-  Sym_1       = 0x0C,
-  Sym_2       = 0xEA,
-  Sym_3       = 0xAE,
-  Sym_4       = 0x8D,
-  Sym_5       = 0xA7,
-  Sym_6       = 0xE7,
-  Sym_7       = 0x0E,
-  Sym_8       = 0xEF,
-  Sym_9       = 0xAF,
-  Sym_A       = 0xCF,
-  Sym_b       = 0xE5,
-  Sym_c       = 0xE0,
-  Sym_C       = 0x63,
-  Sym_d       = 0xEC,
-  Sym_E       = 0xE3,
-  Sym_F       = 0xC3,
-  Sym_h       = 0xC5,
-  Sym_H       = 0xCD,
-  Sym_P       = 0xCB,
-  Sym_t       = 0xE1,
-  Sym_u       = 0x64,
-  Sym_U       = 0x6D,
-  Sym_Gradus	= 0x8B,
-  Sym_LGradus	= 0xE4,
-  Sym_Temp	  = 0xE1,
-  Sym_Minus	  = 0x80,
-  Sym_Plus	  = 0xD1,
+  Sym_0       = 0x9F,
+  Sym_1       = 0x03,
+  Sym_2       = 0xB9,
+  Sym_3       = 0xB3,
+  Sym_4       = 0x27,
+  Sym_5       = 0xB6,
+  Sym_6       = 0xBe,
+  Sym_7       = 0x13,
+  Sym_8       = 0xBF,
+  Sym_9       = 0xB7,
+  Sym_A       = 0x3F,
+  Sym_b       = 0xAE,
+  Sym_c       = 0xA8,
+  Sym_C       = 0x9C,
+  Sym_d       = 0xAB,
+  Sym_E       = 0xBC,
+  Sym_F       = 0x3C,
+  Sym_h       = 0x2E,
+  Sym_H       = 0x2F,
+  Sym_o     	= 0xAA,
+  Sym_P       = 0x3D,
+  Sym_t       = 0xAC,
+  Sym_u       = 0x8A,
+  Sym_U       = 0x8F,
+  Sym_Gradus	= 0x35,
+  Sym_Minus	  = 0x20,
   Sym_BLANK	  = 0x00,
   Sym_FULL	  = 0xFF,
-  Sym_Dot     = 0x10
+  Sym_Dot     = 0x40
 } max7219_sym_t;
 
 /* Exported constants --------------------------------------------------------*/

+ 3 - 0
lib/rtos.c

@@ -201,6 +201,8 @@ static void TimingDelay_Decrement(void)
   }
 }
 */
+
+#ifdef USE_RTOS
 /**
   * @brief TIM4 Update/Overflow/Trigger Interrupt routine.
   * @param  None
@@ -232,3 +234,4 @@ INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler,23)
    }
 
 }
+#endif // USE_RTOS

+ 0 - 1
lib/rtos.h

@@ -44,7 +44,6 @@ typedef struct task
 /******************************************************************************************
  * Ïåðåìåííûå
  */
-#define USE_RTOS  1
 
 /******************************************************************************************
  * Ïðîòîòèïû ôóêíöèé

+ 58 - 7
src/main.c

@@ -1,9 +1,9 @@
 /**
   ******************************************************************************
-  * @file    Project/main.c 
+  * @file    Project/main.c
   * @author  "Vladimir N. Shilov" <shilow@ukr.net>
-  * @version V0.0.1
-  * @date    18-December-2018
+  * @version v.0.1
+  * @date    08-July-2019
   * @brief   Main program body
    ******************************************************************************
   * @attention
@@ -14,27 +14,78 @@
   * this stuff is worth it, you can buy me a beer in return.   Shilov V.N.
   *
   ******************************************************************************
-  */ 
+  */
 
 
 /* Includes ------------------------------------------------------------------*/
 #include "stm8s.h"
+#include "max7219.h"
 
 /* Private defines -----------------------------------------------------------*/
+const static max7219_reg_t digitPosition[8] = {
+  RegDigit0, RegDigit1, RegDigit2, RegDigit3,
+  RegDigit4, RegDigit5, RegDigit6, RegDigit7
+};
+const static uint8_t digitValue[16] = {
+  Sym_0, Sym_1, Sym_2, Sym_3, Sym_4, Sym_5, Sym_6, Sym_7,
+  Sym_8, Sym_9, Sym_A, Sym_b, Sym_c, Sym_d, Sym_E, Sym_F
+};
+
 /* Private function prototypes -----------------------------------------------*/
+static void boardInit(void);
+
 /* Private functions ---------------------------------------------------------*/
 
 void main(void)
 {
+  boardInit();
+
+  MAX7219_Config();
+  uint8_t i;
+  for (i=1; i<9; i++) {
+    MAX7219_WriteData(digitPosition[i], digitValue[i]);
+  }
+
   /* Infinite loop */
   while (1)
   {
   }
-  
+
 }
 
-#ifdef USE_FULL_ASSERT
+/* Private functions ---------------------------------------------------------*/
+static void boardInit(void) {
+  /* Íàñêîëüêî ÿ ïîíÿë, äëÿ ÷àñòîò âûøå 16ÌÃö íóæíî ââîäèòü çàäåðæêó ïðè ðàáîòå ñ FLASH:
+    - Before using the HSE clock make sure that the "Flash_Wait_States" is set to 1.
+    - To do so :
+     - with STVD (menu: Debug Instrument -> MCU configuration -> Options)
+     - with EWSTM8 (menu: ST-LINK -> Option bytes ->Flash_Wait_States: 1)
+  */
+
+  ErrorStatus status = ERROR;
 
+  CLK_DeInit();
+
+  /* Configure the Fcpu to DIV1*/
+  CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);
+
+  /* Configure the HSI prescaler to the optimal value */
+  CLK_SYSCLKConfig(CLK_PRESCALER_HSIDIV1);
+
+  /* Output Fcpu on CLK_CCO pin */
+  CLK_CCOConfig(CLK_OUTPUT_CPU);
+
+  /* Configure the system clock to use HSE clock source and to run at Crystal Mhz */
+  status = CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSE, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE);
+
+  if (status == ERROR) {
+    /* Configure the system clock to use HSI clock source and to run at 16Mhz */
+    status = CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE);
+    // set FLAG for
+  }
+}
+
+#ifdef USE_FULL_ASSERT
 /**
   * @brief  Reports the name of the source file and the source line number
   *   where the assert_param error has occurred.
@@ -43,7 +94,7 @@ void main(void)
   * @retval : None
   */
 void assert_failed(u8* file, u32 line)
-{ 
+{
   /* User can add his own implementation to report the file name and line number,
      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
 

+ 10 - 8
src/stm8s_it.c

@@ -5,7 +5,7 @@
   * @version V2.3.0
   * @date    16-June-2017
   * @brief   Main Interrupt Service Routines.
-  *          This file provides template for all peripherals interrupt service 
+  *          This file provides template for all peripherals interrupt service
   *          routine.
    ******************************************************************************
   * @attention
@@ -18,14 +18,14 @@
   *
   *        http://www.st.com/software_license_agreement_liberty_v2
   *
-  * Unless required by applicable law or agreed to in writing, software 
-  * distributed under the License is distributed on an "AS IS" BASIS, 
+  * Unless required by applicable law or agreed to in writing, software
+  * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   *
   ******************************************************************************
-  */ 
+  */
 
 /* Includes ------------------------------------------------------------------*/
 #include "stm8s_it.h"
@@ -167,7 +167,7 @@ INTERRUPT_HANDLER(EXTI_PORTE_IRQHandler, 7)
   */
 }
 
-#if defined (STM8S903) || defined (STM8AF622x) 
+#if defined (STM8S903) || defined (STM8AF622x)
 /**
   * @brief External Interrupt PORTF Interrupt routine.
   * @param  None
@@ -255,7 +255,7 @@ INTERRUPT_HANDLER(TIM1_CAP_COM_IRQHandler, 12)
      it is recommended to set a breakpoint on the following instruction.
   */
  }
- 
+
 /**
   * @brief Timer5 Capture/Compare Interrupt routine.
   * @param  None
@@ -455,7 +455,7 @@ INTERRUPT_HANDLER(I2C_IRQHandler, 19)
   * @brief ADC1 interrupt routine.
   * @par Parameters:
   * None
-  * @retval 
+  * @retval
   * None
   */
  INTERRUPT_HANDLER(ADC1_IRQHandler, 22)
@@ -484,12 +484,14 @@ INTERRUPT_HANDLER(TIM6_UPD_OVF_TRG_IRQHandler, 23)
   * @param  None
   * @retval None
   */
+  #ifndef USE_RTOS
  INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23)
  {
   /* In order to detect unexpected events during development,
      it is recommended to set a breakpoint on the following instruction.
   */
  }
+ #endif // USE_RTOS
 #endif /* (STM8S903) || (STM8AF622x)*/
 
 /**
@@ -509,4 +511,4 @@ INTERRUPT_HANDLER(EEPROM_EEC_IRQHandler, 24)
   */
 
 
-/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/