Facebook
From Cute Mockingjay, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 239
  1. // ode
  2.  
  3. /*
  4.  *      startup.c
  5.  *
  6.  */
  7.  
  8. typedef unsigned char uint8_t;
  9. typedef unsigned int uint32_t;
  10.  
  11. #define GPIO_E 0x40021000
  12. #define GPIO_MODER ((volatile uint32_t *) (GPIO_E))
  13. #define GPIO_OTYPER ((volatile unsigned short *) (GPIO_E+0x4))
  14. #define GPIO_OSPEEDR ((volatile uint32_t *) (GPIO_E+0x8))
  15. #define GPIO_PUPDR ((volatile uint32_t *) (GPIO_E+0xC))
  16. #define GPIO_IDR_LOW ((volatile uint8_t *) (GPIO_E+0x10))
  17. #define GPIO_IDR_HIGH ((volatile uint8_t *) (GPIO_E+0x11))
  18. #define GPIO_ODR_LOW ((volatile uint8_t *) (GPIO_E+0x14))
  19. #define GPIO_ODR_HIGH ((volatile uint8_t *) (GPIO_E+0x15))
  20.  
  21. #define STK_CTRL ((volatile int *) 0xE000E010)
  22. #define STK_LOAD ((volatile int *) 0xE000E014)
  23. #define STK_VAL ((volatile int *) 0xE000E018)
  24.  
  25. #define B_E 0x40 //Enable
  26. #define B_RST 0x20 //Reset
  27. #define B_CS2 0x10 //CS2 - Skärmdel till höger
  28. #define B_CS1 0x8 // CS1 - Skärmdel till vänster
  29. #define B_SELECT 4 // 0 Graphics, 1 Ascii
  30. #define B_RW 2 // 0 Write, 1 Read
  31. #define B_RS 1 // 0 Command, 1 Data
  32.  
  33. #define LCD_ON 0x3F // Display on
  34. #define LCD_OFF 0x3E // Display off
  35. #define LCD_SET_ADD 0x40 // Set horizontal coordinate
  36. #define LCD_SET_PAGE 0xB8 // Set vertical coordinate
  37. #define LCD_DISP_START 0xC0 // Start address
  38. #define LCD_BUSY 0x80 // Read busy status
  39.  
  40. #define SIMULATOR
  41.  
  42.  
  43.  
  44. typedef struct tpoint{
  45.     uint8_t x;
  46.     uint8_t y;
  47. }POINT;
  48.  
  49. #define MAX_POINTS 20
  50.  
  51. typedef struct tGeometry{
  52.     uint32_t numpoints;
  53.     uint32_t sizex;
  54.     uint32_t sixey;
  55.     POINT px[MAX_POINTS];
  56. }GEOMETRY, *PGEOMETRY;
  57.  
  58. typedef struct tObj{
  59.     PGEOMETRY geo;
  60.     signed int dirx;
  61.     signed int diry;
  62.     signed int posx;
  63.     signed int posy;
  64.     void (* draw)(struct tObj*);
  65.     void (* clear)(struct tObj*);
  66.     void (* move)(struct tObj*);
  67.     void (* set_speed)(struct tObj*, signed int, signed int);
  68. }OBJECT, *POBJECT;
  69.  
  70. void startup(void) __attribute__((naked)) __attribute__((section (".start_section")) );
  71.  
  72. void startup ( void )
  73. {
  74. __asm volatile(
  75.         " LDR R0,=0x2001C000\n"         /* set stack */
  76.         " MOV SP,R0\n"
  77.         " BL main\n"                            /* call main */
  78.         "_exit: B .\n"                          /* never return */
  79.         ) ;
  80. }
  81.  
  82. //------------ObjectFunktioner----//
  83.  
  84. void set_object_speed(POBJECT o, uint32_t speedx, uint32_t speedy){
  85.     o->dirx = speedx;
  86.     o->diry = speedy;
  87. }
  88.  
  89. void draw_object (POBJECT object){
  90.     for(uint8_t p = 0; p < object->geo->numpoints; p++){
  91.         POINT centerPoint = object->geo->px[p];
  92.         pixel((object->posx + centerPoint.x),(object->posy + centerPoint.y), 1);
  93.     }
  94. }
  95.  
  96. void clear_object (POBJECT object){
  97.     for(uint8_t p = 0; p < object->geo->numpoints; p++){
  98.         POINT centerPoint = object->geo->px[p];
  99.         pixel((object->posx + centerPoint.x),(object->posy + centerPoint.y), 0);
  100.     }
  101. }
  102.  
  103. void move_object (POBJECT object){
  104.     clear_object(object);
  105.     object->posx += object->dirx;
  106.     object->posy += object->diry;
  107.     if(object->posx < 1){
  108.         object->posx = 1;
  109.         object->dirx *= -1;
  110.     }
  111.     if(object->posx > 128){
  112.         object->posx = 127;
  113.         object->dirx *= -1;
  114.     }
  115.     if(object->posy < 1){
  116.         object->posy = 1;
  117.         object->diry *= -1;
  118.     }
  119.     if(object->posy > 64){
  120.         object->posy = 63;
  121.         object->diry *= -1;
  122.     }
  123.     draw_object(object);
  124. }
  125.  
  126. //------------Drivrutiner---------//
  127.  
  128. void graphic_ctrl_bit_set(uint8_t x){
  129.     uint8_t c;
  130.     c = *GPIO_ODR_LOW;
  131.     c|=(~B_SELECT & x);
  132.     *GPIO_ODR_LOW = c;
  133. }
  134.  
  135. void graphic_ctrl_bit_clear(uint8_t x){
  136.     uint8_t c;
  137.     c = *GPIO_ODR_LOW;
  138.     c &= (~B_SELECT & ~x);
  139.     *GPIO_ODR_LOW = c;
  140. }
  141.  
  142. void select_controller(uint8_t controller){
  143.     switch(controller){
  144.         case 0:
  145.             graphic_ctrl_bit_clear(B_CS1|B_CS2);
  146.             break;
  147.         case B_CS1 :
  148.             graphic_ctrl_bit_set(B_CS1);
  149.             graphic_ctrl_bit_clear(B_CS2);
  150.             break;
  151.         case B_CS2 :
  152.             graphic_ctrl_bit_set(B_CS2);
  153.             graphic_ctrl_bit_clear(B_CS1);
  154.             break;
  155.         case B_CS1|B_CS2 :
  156.             graphic_ctrl_bit_set(B_CS1|B_CS2);
  157.             break;
  158.     }
  159. }
  160.  
  161. void graphic_wait_ready(void){
  162.     uint32_t c;
  163.     graphic_ctrl_bit_clear(B_E);
  164.     c = 0x00005555;
  165.     *GPIO_MODER = c;
  166.     graphic_ctrl_bit_clear(B_RS);
  167.     graphic_ctrl_bit_set(B_RW);
  168.     delay_500();
  169.     do{
  170.         graphic_ctrl_bit_set(B_E);
  171.         delay_500();
  172.         c = *GPIO_IDR_HIGH & LCD_BUSY;
  173.         graphic_ctrl_bit_clear(B_E);
  174.         delay_500();
  175.     }while (c);
  176.     graphic_ctrl_bit_set(B_E);
  177.     c = 0x55555555;
  178.     *GPIO_MODER = c;    
  179. }
  180.  
  181. uint8_t graphic_read (uint8_t controller){
  182.     graphic_ctrl_bit_clear(B_E);
  183.     uint32_t c = 0x00005555;
  184.     *GPIO_MODER = c;
  185.     graphic_ctrl_bit_set(B_RS);
  186.     graphic_ctrl_bit_set(B_RW);
  187.     select_controller(controller);
  188.     delay_500();
  189.     graphic_ctrl_bit_set(B_E);
  190.     delay_500();
  191.     uint32_t rv = *GPIO_IDR_HIGH;
  192.     graphic_ctrl_bit_clear(B_E);
  193.     c = 0x55555555;
  194.     *GPIO_MODER = c;
  195.     help_select_controller(controller);
  196.     return rv;
  197. }
  198.  
  199. void graphic_write(uint8_t value, uint8_t controller){
  200.     //uint32_t c = *GPIO_MODER;
  201.     *GPIO_ODR_HIGH = value;
  202.     select_controller(controller);
  203.     delay_500();
  204.     graphic_ctrl_bit_set(B_E);
  205.     delay_500();
  206.     graphic_ctrl_bit_clear(B_E);
  207.    
  208.     help_select_controller(controller);
  209.    
  210.     *GPIO_ODR_HIGH = value;
  211.     graphic_ctrl_bit_set(B_E);
  212.     select_controller(0);
  213. }
  214.  
  215. void graphic_write_command(uint8_t cmd, uint8_t controller){
  216.     graphic_ctrl_bit_clear(B_E);
  217.     select_controller(controller);
  218.     graphic_ctrl_bit_clear(B_RS);
  219.     graphic_ctrl_bit_clear(B_RW);
  220.     graphic_write(cmd, controller);
  221. }
  222.  
  223. void graphic_write_data(uint8_t data, uint8_t controller){
  224.     graphic_ctrl_bit_clear(B_E);
  225.     select_controller(controller);
  226.     graphic_ctrl_bit_set(B_RS);
  227.     graphic_ctrl_bit_clear(B_RW);
  228.     graphic_write(data, controller);
  229. }
  230.  
  231. uint8_t graphic_read_data(uint8_t controller){
  232.     graphic_read(controller);
  233.     return graphic_read(controller);
  234. }
  235.  
  236. void help_select_controller(uint8_t controller){
  237.     if(controller == B_CS1){
  238.         select_controller(B_CS1);
  239.         graphic_wait_ready();
  240.     }
  241.     if (controller == B_CS2){
  242.         select_controller(B_CS2);
  243.         graphic_wait_ready();
  244.     }
  245. }
  246.  
  247. // ------------------INIT-------------//
  248.  
  249. void app_init(void){
  250. //Display Data ut E15-8
  251. *GPIO_MODER = (*GPIO_MODER & 0) | 0x55555555;
  252. //Styrregister ut E7-0
  253. }
  254.  
  255. void graphic_initalize(void){
  256.     graphic_ctrl_bit_set(B_E);
  257.     delay_micro(10);
  258.     graphic_ctrl_bit_clear(B_CS1);
  259.     graphic_ctrl_bit_clear(B_CS2);
  260.     graphic_ctrl_bit_clear(B_RST);
  261.     graphic_ctrl_bit_clear(B_E);
  262.     delay_milli(30);
  263.     graphic_ctrl_bit_set(B_RST);
  264.     //Utelämna vid simulator?
  265.     graphic_write_command(LCD_OFF, B_CS1|B_CS2);
  266.     graphic_write_command(LCD_ON, B_CS1|B_CS2);
  267.     graphic_write_command(LCD_DISP_START, B_CS1|B_CS2);
  268.     graphic_write_command(LCD_SET_ADD, B_CS1|B_CS2);
  269.     graphic_write_command(LCD_SET_PAGE, B_CS1|B_CS2);
  270.     select_controller(0);
  271. }
  272.  
  273. void graphic_clear_screen(void){
  274.     for (uint8_t page = 0; page < 8; page++){
  275.         graphic_write_command(LCD_SET_PAGE | page, B_CS1|B_CS2);
  276.         graphic_write_command(LCD_SET_ADD| 0, B_CS1|B_CS2);
  277.         for (uint8_t add = 0; add < 64; add++){
  278.             graphic_write_data(0, B_CS1|B_CS2);
  279.         }
  280.     }
  281. }
  282.  
  283. void pixel (uint8_t x, uint8_t y, uint8_t set){
  284.     if(x >= 1 && x <= 128 && y >= 1 && y <= 64){}else{return;}
  285.     uint8_t index = (y-1)/8;
  286.     uint8_t mask = 1<<((y-1)%8);
  287.     if (set == 0){
  288.         mask = ~mask;
  289.     }
  290.     uint8_t controller = B_CS1;
  291.     uint8_t x_fysisk = x-1;
  292.     if(x>64){
  293.         controller = B_CS2;
  294.         x_fysisk = x-65;
  295.     }
  296.     graphic_write_command(LCD_SET_ADD | x_fysisk, controller);
  297.     graphic_write_command(LCD_SET_PAGE | index, controller);
  298.     uint8_t temp = graphic_read_data(controller);
  299.     graphic_write_command(LCD_SET_ADD | x_fysisk, controller);
  300.     if(set){
  301.         mask |= temp;
  302.     }else{
  303.         mask &= temp;
  304.     }
  305.     graphic_write_data(mask, controller);
  306. }
  307.  
  308. // ------------------DELAY------------//
  309.  void delay_500(void){    
  310.     #ifndef SIMULATOR
  311.         delay_250();
  312.         delay_250();
  313.     #endif      
  314.  }
  315. void delay_250(void){
  316.     *STK_CTRL = 0; //Återställer räknaren
  317.     *STK_LOAD = (168/4)-1;
  318.     *STK_VAL = 0;
  319.     *STK_CTRL = 5; //Starta räknaren
  320.     while ((*STK_CTRL & 0x10000) == 0){
  321.         }
  322.     *STK_CTRL = 0;
  323. }
  324.  
  325. void delay_micro(uint32_t us){
  326.     us *=4;
  327.     while (us>0){
  328.         delay_250();
  329.         us--;
  330.     }
  331. }
  332.  
  333. void delay_milli(uint32_t ms){
  334.    
  335.     #ifdef SIMULATOR
  336.         ms = ms/1000;
  337.         ms++;
  338.     #endif
  339.    
  340.     while(ms>0){
  341.         delay_micro(1000);
  342.         ms--;
  343.     }
  344. }
  345.  
  346. void main(void)
  347. {
  348.     uint8_t i;
  349.     app_init();
  350.     graphic_initalize();
  351.     #ifndef SIMULATOR
  352.         graphic_clear_screen();
  353.     #endif
  354.    
  355.     GEOMETRY ball_geometry = {
  356.         12,
  357.         4,4,
  358.         {
  359.         {0,1}, {0,2}, {1,0}, {1,1}, {1,2}, {1,3}, {2,0}, {2,1}, {2,2}, {2,3}, {3,2}
  360.         }
  361.     };
  362.     PGEOMETRY pball_geometry = &ball_geometry;
  363.    
  364.     OBJECT ball =
  365.     {
  366.         pball_geometry,
  367.         0,0,
  368.         1,1,
  369.         draw_object,
  370.         clear_object,
  371.         move_object,
  372.         set_object_speed
  373.     };
  374.     POBJECT p = &ball;
  375.     p->set_speed(p, 0, 4);
  376.     while(1){
  377.         p->move(p);
  378.         delay_milli(10);
  379.     }
  380.    
  381.     /*
  382.     graphic_write_command(LCD_SET_ADD | 10, B_CS1|B_CS2);
  383.     graphic_write_command(LCD_SET_PAGE | 1, B_CS1|B_CS2);
  384.     graphic_write_data(0xFF, B_CS1|B_CS2);
  385.      */
  386.      
  387.      /*
  388.      for (i = 1; i <= 128; i++){
  389.          pixel(i, 10, 1);
  390.      }
  391.      for(i = 1; i <= 64; i++){
  392.          pixel(10, i, 1);
  393.      }
  394.      //delay_milli(500);
  395.      for (i = 1; i <= 128; i++){
  396.          pixel(i, 10, 0);
  397.      }
  398.      for(i = 1; i <= 64; i++){
  399.          pixel(10, i, 0);
  400.      }
  401.      */
  402. }
  403.  
  404.