Guest User

Untitled

a guest
Nov 13th, 2017
85
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "inc/stm8s.h"
  2. #include "inc/stm8s_gpio.h"
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5.  
  6.  
  7. const uint16_t clk_start = CLK_BaseAddress;
  8.  
  9. const char *clk_regs_names[] =
  10. {
  11.  "ICKR      =",
  12.  "ECKR      =",
  13.  "RESERVED  =",
  14.  "CMSR      =",
  15.  "SWR       =",
  16.  "SWCR      =",
  17.  "CKDIVR    =",
  18.  "PCKENR1   =",
  19.  "CSSR      =",
  20.  "CCOR      =",
  21.  "PCKENR2   =",
  22.  "RESERVED1 =",
  23.  "HSITRIMR  =",
  24.  "SWIMCCR   ="
  25. };
  26.  
  27. uint8_t clk_regs_values[sizeof(clk_regs_names)];
  28.  
  29.  
  30. unsigned char out_str[32];
  31.  
  32. void uart_tx_string(unsigned char *str);
  33.  
  34.  
  35.  
  36. //================================ UART ======================================//
  37.  
  38.  void UART1_init(void)
  39.  {
  40.    // Baudrate 9600 for F_CPU = 16 MHz
  41.    UART1->BRR2 = 0x03;
  42.    UART1->BRR1 = 0x68;
  43.  
  44.    UART1->CR1 = 0x00;
  45.    UART1->CR2 = /*UART1_CR2_REN | UART1_CR2_RIEN |*/ UART1_CR2_TEN;
  46.  }
  47.  
  48.  // Отправка байта
  49.  inline void uart_tx_byte(uint8_t data)
  50.  {
  51.      while(!(UART1->SR & UART1_SR_TXE));
  52.      UART1->DR = data;
  53.  }
  54.  
  55.  // For debugging feature
  56.  void uart_tx_string(unsigned char *str)
  57.  {
  58.    while(*str != 0) {uart_tx_byte(*str++);};
  59.  }
  60.  
  61. //============================ UART END ======================================//
  62.  
  63.  
  64. void main(void)
  65. {
  66.     // UART TX pin
  67.     GPIOD->ODR  = 0x00;
  68.     GPIOD->DDR |= GPIO_PIN_5;
  69.     GPIOD->CR1 |= GPIO_PIN_5;
  70.     GPIOD->CR2 |= GPIO_PIN_5;
  71.    
  72.     for (uint8_t i = 0; i < sizeof(clk_regs_values); i++)
  73.     {
  74.         clk_regs_values[i] = *((uint8_t *)(clk_start +i));
  75.     }
  76.    
  77.   // Disable clock prescaler. F_CPU = 16 MHz
  78.   CLK->CKDIVR = 0x00;
  79.    
  80.   UART1_init();
  81.    
  82.     uart_tx_byte('\n');
  83.     for (uint8_t i = 0; i < sizeof(clk_regs_names)/sizeof(clk_regs_names[0]); i++)
  84.     {
  85.         sprintf(out_str, "%-s 0x%02X\n", clk_regs_names[i], clk_regs_values[i]);
  86.         uart_tx_string(out_str);
  87.     }  
  88.  
  89.   uart_tx_string("Initialization complete.\n");
  90.    
  91.   while(1)
  92.   {
  93.   }
  94. }
RAW Paste Data