Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.19 KB | None | 0 0
  1. #include <stm32/rcc.h>
  2. #include <stm32/gpio.h>
  3. #include <stm32/afio.h>
  4. #include <stm32/spi.h>
  5. #include "fixed.h"
  6.  
  7. void delay(unsigned timeout) {
  8.     while(timeout--) asm volatile("nop");
  9. }
  10.  
  11. #define delay_ms(ms) delay(ms*(8000*5/4)/2/3)
  12.  
  13. /* PINOUT
  14.  *  PC0 = D/!C
  15.  *  PC1 = !EN
  16.  *  PC2 = !RST
  17.  *  PB3 = CLK
  18.  *  PB5 = MOSI
  19.  */
  20.  
  21. inline static void lcd_cmd()     { GPIOC->ODR &= ~BIT(0); }
  22. inline static void lcd_data()    { GPIOC->ODR |=  BIT(0); }
  23. inline static void lcd_enable()  { GPIOC->ODR &= ~BIT(1); }
  24. inline static void lcd_disable() { GPIOC->ODR |=  BIT(1); }
  25.  
  26. inline static void lcd_reset() {
  27.     GPIOC->ODR &= ~BIT(2);
  28.     delay_ms(10);
  29.     GPIOC->ODR |= BIT(2);
  30. }
  31.  
  32. static void lcd_write(u8 byte) {
  33.     lcd_enable();
  34.     while(!(SPI1->SR & SPI_TXE));
  35.     SPI1->DR = byte;
  36.     while(!(SPI1->SR & SPI_RXNE));
  37.     SPI1->DR;
  38.     lcd_disable();
  39. }
  40.  
  41. static void lcd_init() {
  42.     RCC->APB2ENR = IOPCEN | IOPBEN | SPI1EN | AFIOEN;
  43.     AFIO->MAPR = SWJ_CFG(SWJ_CFG_SW) | SPI1_REMAP;
  44.     GPIOC->CRL = 0x44444111; // PC[0-2] as outputs
  45.     GPIOB->CRL = 0x44949444; // PB3, PB5 as AF outputs
  46.  
  47.     SPI1->CR1 = SPI_MSTR | SPI_BR(0) | SPI_SSM | SPI_SSI | SPI_SPE;
  48.  
  49.     lcd_reset();
  50.  
  51.     lcd_cmd();
  52.     lcd_write(0x21); // !PD=ChipActive H=ExtendedCmd
  53.     lcd_write(0xe0); // set value of Vop (controls contrast) = 0x80 | 0x60 (arbitrary)
  54.     lcd_write(0x04); // set temperature coefficient
  55.     lcd_write(0x14); // set bias mode to 1:48.
  56.     lcd_write(0x22); // !PD=ChipActive !H=NormalCmd
  57.     lcd_write(0x0c); // D=NormalMode
  58. }
  59.  
  60. static void lcd_refresh(u8* buffer) {
  61.     lcd_cmd();
  62.     lcd_write(0x40);
  63.     lcd_write(0x80);
  64.  
  65.     lcd_data();
  66.     for(int i = 0; i < 6 * 84; i++)
  67.         lcd_write(buffer[i]);
  68. }
  69.  
  70. static u8 framebuffer[6*84];
  71. static u32* const frameband = (u32*)(0x2000000 + ((u8*)framebuffer));
  72. #define W 48
  73.  
  74. #define abs(f) ((f)>=0?(f):-(f))
  75. #define sq(f) ((f)*(f))
  76.  
  77. static void draw_ball(u8 x, u8 y) {
  78.     for(s8 dx = -8; dx <= 8; dx++)
  79.         for(s8 dy = -8; dy <= 8; dy++)
  80.             if(sq(dy)+sq(dx)<=sq(4)) {
  81.                 if(y+dy >= 0 && y+dy < 84 && x+dx >= 0 && x+dx < 48)
  82.                     frameband[((int)(y+dy))*W+((int)(x+dx))] = 1;
  83.             }
  84. }
  85.  
  86. s16 rand;
  87.  
  88. void main() {
  89.     RCC->CR = HSEON;
  90.     while((RCC->CR & HSERDY) == 0);
  91.  
  92.     W_SW(RCC->CFGR, SW_HSE);
  93.     while(R_SW(RCC->CFGR) != SW_HSE);
  94.  
  95.     lcd_init();
  96.  
  97.     GPIOC->CRH = 0x44444411;
  98.  
  99.     RCC->APB2ENR |= IOPAEN;
  100.     GPIOA->CRL = 0x44444448;
  101.  
  102.     fixed bx = f(50, 0), by = f(5, 0);
  103.     fixed vx = f(1, 0), vy = f(2, 0);
  104.     fixed g  = f(0, 0.1);
  105.  
  106.     while(1) {
  107.         for(int i = 0; i < 6*84; i++)
  108.             framebuffer[i] = 0;
  109.  
  110.         //vy = f_add(vy, g);
  111.  
  112.         bx = f_add(bx, vx);
  113.         //by = f_add(by, vy);
  114.  
  115.         int coll = 0;
  116.  
  117. /*
  118. #define BOUND(p, d, v, m) \
  119.         if(f_i(p) - d <= 0 && f_i(v) < 0) { \
  120.             v = f_neg(v); \
  121.             v = f_mul(v, f(0, 0.7)); \
  122.             p = f(d, 0); \
  123.             coll = 1; \
  124.         } \
  125.         if(f_i(p) + d >= m && f_i(v) > 0) { \
  126.             v = f_neg(v); \
  127.             v = f_mul(v, f(0, 0.7)); \
  128.             p = f(m - d, 0); \
  129.             while(1); \
  130.         }
  131. */
  132.  
  133.         volatile int k = f_i(bx);
  134.  
  135.         if(f_i(bx) - 4 <= 0 && f_i(vx) < 0) {
  136.             vx = f_neg(vx);
  137.             vx = f_mul(vx, f(0, 0.7));
  138.         }
  139.  
  140.         //BOUND(by, 4, vy, 48);
  141. //      BOUND(bx, 4, vx, 84);
  142.  
  143.         draw_ball(f_i(by), f_i(bx));
  144.  
  145.         lcd_refresh(framebuffer);
  146.         if(coll) GPIOC->ODR |= BIT(9);
  147.         delay_ms(100);
  148.         GPIOC->ODR &= ~BIT(9);
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement