Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.81 KB | None | 0 0
  1. /*
  2. * startup.c
  3. *
  4. */
  5.  
  6. #define B_RS 0x01
  7. #define B_RW 0x02
  8. #define B_SELECT 0x04
  9. #define B_CS1 0x08
  10. #define B_CS2 0x10
  11. #define B_RST 0x20
  12. #define B_E 0x40
  13.  
  14.  
  15.  
  16. #define LCD_ON 0x3F
  17. #define LCD_OFF 0x3E
  18. #define LCD_set_addr 0x40
  19. #define LCD_set_page 0xB8
  20. #define LCD_disp_start 0xC0
  21.  
  22. #define GPIO_E 0x40021000 /* MD407 port E */
  23. #define portModer ((volatile unsigned long *) (GPIO_E))
  24. #define portOtyper ((volatile unsigned short *) (GPIO_E+0x4))
  25. #define portIdr ((volatile unsigned short *)(GPIO_E+0x10))
  26.  
  27.  
  28.  
  29. #define portIdrLow ((volatile unsigned char *) (GPIO_E+0x10))
  30. #define portIdrHigh ((volatile unsigned char *) (GPIO_E+0x10+1))
  31. #define portOdrLow ((volatile unsigned char*) (GPIO_E+0x14))
  32. #define portOdrHigh ((volatile unsigned char*) (GPIO_E+0x15))
  33.  
  34. #define ball_img (char[]) {0,0, -1,0, 1,0, 0,-1, 0,1}
  35. #define ball_img_points 5
  36.  
  37. unsigned volatile long * kp_O = 0x40020C15; //O-port
  38. unsigned volatile long * kp_I = 0x40020C11; //I-port
  39. unsigned volatile long * dip_O= 0x40020C14; //O-port
  40.  
  41. unsigned long STK_CTRL = 0xE000E010;
  42. unsigned long STK_LOAD = 0xE000E014;
  43. unsigned volatile long STK_VAL = 0xE000E018;
  44.  
  45. typedef unsigned char unit_8;
  46. typedef unsigned short unit_16;
  47. typedef unsigned long unit_32;
  48.  
  49.  
  50. typedef struct balll{
  51. unit_8 cord_x;
  52. unit_8 cord_y;
  53. char dir_x;
  54. char dir_y;
  55. char image[ball_img_points*2];
  56. void (*draw_ball)(struct balll *);
  57.  
  58. } ball;
  59.  
  60.  
  61.  
  62. void startup(void) __attribute__((naked)) __attribute__((section (".start_section")) );
  63.  
  64. void startup ( void )
  65. {
  66. __asm volatile(
  67. " LDR R0,=0x2001C000\n" /* set stack */
  68. " MOV SP,R0\n"
  69. " BL main\n" /* call main */
  70. "_exit: B .\n" /* never return */
  71. ) ;
  72. }
  73.  
  74. void delay_250ns(){
  75.  
  76.  
  77. *((unsigned long *)STK_CTRL) = 0x00;
  78. *((unsigned long *)STK_LOAD )=((168/4)-1);
  79. *((unsigned long *)STK_VAL )=0;
  80. *((unsigned long *)STK_CTRL) =5;
  81. while(*((unsigned long *)STK_CTRL) < 0x8){
  82. }
  83. *((unsigned long *)STK_CTRL) = 0;
  84. }
  85. void delay_500ns(){
  86.  
  87. delay_250ns();
  88. delay_250ns();
  89.  
  90. }
  91. void delay_mikro(unsigned int ms){
  92. for (unsigned int j = ms/4; j > 0 ; j--){
  93. delay_250ns();
  94.  
  95. }
  96.  
  97. }
  98. void delay_milli(unsigned int ms){
  99.  
  100.  
  101. #ifdef SIMULATOR
  102. ms=ms/1000;
  103. ms++;
  104. #endif
  105. for(unsigned int i= ms*500; i > 0; i--){
  106. delay_mikro(2);
  107. }
  108. }
  109.  
  110.  
  111.  
  112. void graphic_ctrl_bit_set( unit_8 x ) { /* Funktion för att 1-ställa bitar */
  113. unit_8 c;
  114. c = *portOdrLow;
  115. c &= ~B_SELECT;
  116. c |= (~B_SELECT & x);
  117. *portOdrLow = c;
  118. }
  119. void graphic_ctrl_bit_clear( unit_8 x ){
  120. unit_8 c;
  121. c =*portOdrLow;
  122. c &=~B_SELECT;
  123. c &=~x;
  124. *portOdrLow=c;
  125. }
  126.  
  127. void select_controller(unit_8 s){
  128. switch(s){
  129. case B_CS1 :
  130. graphic_ctrl_bit_clear(B_CS2);
  131. graphic_ctrl_bit_set(B_CS1);
  132. break;
  133. case B_CS2 :
  134. graphic_ctrl_bit_clear(B_CS1);
  135. graphic_ctrl_bit_set(B_CS2);
  136. break;
  137. case B_CS1|B_CS2 :
  138. graphic_ctrl_bit_set(B_CS1|B_CS2);
  139. break;
  140. case 0:
  141. graphic_ctrl_bit_clear(B_CS1|B_CS2);
  142. break;
  143. }
  144. }
  145. void graphic_wait_ready(void){
  146. unit_8 c;
  147. graphic_ctrl_bit_clear(B_E);
  148. *portModer= (0x00005555); //sets port 15-8 to inputs and 7-0 to outputs
  149. *portOdrLow = ((*portOdrLow & ~0x01)| 0x02); //turns off RS and RW on
  150. delay_500ns();
  151.  
  152. while(1){
  153. graphic_ctrl_bit_set(B_E);
  154. delay_500ns();
  155. c =*portIdrHigh & 0x80;
  156. graphic_ctrl_bit_clear(B_E);
  157. delay_500ns();
  158. if(c == 0){
  159. break;
  160. }
  161. }
  162.  
  163. *portModer= (0x55555555); //sets port 15-8 to outputs
  164.  
  165. }
  166.  
  167. unit_8 graphic_read_controller( unit_8 controller ){
  168. graphic_ctrl_bit_clear( B_E );
  169. *portModer= (0x00005555);
  170. *portOdrLow = (*portOdrLow | 0x03); //turns on RS and RW
  171. select_controller(controller);
  172. delay_500ns();
  173. graphic_ctrl_bit_set(B_E);
  174. delay_500ns();
  175. unit_8 rv = *portIdrHigh;
  176. graphic_ctrl_bit_clear(B_E);
  177. *portModer= (0x55555555);
  178.  
  179. if(controller & B_CS1){
  180. select_controller(B_CS1);
  181. graphic_wait_ready();
  182. }
  183. if (controller & B_CS2){
  184. select_controller(B_CS2);
  185. graphic_wait_ready();
  186. }
  187. return rv;
  188. }
  189. unit_8 graphic_read_data(unit_8 controller){
  190. unit_8 rv;
  191. for(unit_8 i=0;i<2;i++){ //so that it executes it two times
  192. //nessecary for some reason
  193.  
  194. graphic_ctrl_bit_clear( B_E );
  195. *portModer= (0x00005555);
  196. *portOdrLow = (*portOdrLow | 0x03); //turns on RS and RW
  197. select_controller(controller);
  198. delay_500ns();
  199. graphic_ctrl_bit_set(B_E);
  200. delay_500ns();
  201.  
  202. rv = *portIdrHigh;
  203. graphic_ctrl_bit_clear(B_E);
  204. *portModer= (0x55555555);
  205.  
  206. if(controller & B_CS1){
  207. select_controller(B_CS1);
  208. graphic_wait_ready();
  209. }
  210. if (controller & B_CS2){
  211. select_controller(B_CS2);
  212. graphic_wait_ready();
  213. }
  214.  
  215. }
  216. return rv;
  217.  
  218. }
  219.  
  220. void graphic_write(unit_8 value, unit_8 controller){
  221.  
  222. *portOdrHigh = value;
  223. select_controller(controller);
  224. delay_500ns();
  225. graphic_ctrl_bit_set(B_E);
  226. delay_500ns();
  227. graphic_ctrl_bit_clear(B_E);
  228.  
  229. select_controller(controller);
  230. graphic_wait_ready();
  231.  
  232. *portOdrHigh=0x00;
  233. graphic_ctrl_bit_set(B_E);
  234. select_controller(0x00);
  235.  
  236. }
  237. void graphic_write_command(unit_8 cmd, unit_8 controller){
  238.  
  239. graphic_ctrl_bit_clear(B_E);
  240. //select_controller(controller);
  241. *portOdrLow = (*portOdrLow & ~0x03); //turns off RS and RW
  242. graphic_write(cmd , controller);
  243. }
  244. void graphic_write_data(unit_8 data, unit_8 controller){
  245.  
  246. graphic_ctrl_bit_clear(B_E);
  247. select_controller(controller);
  248. *portOdrLow = ((*portOdrLow & ~0x02)| 0x01); //turns off RW and RS on
  249. graphic_write(data , controller);
  250. }
  251.  
  252. void graphic_clear_screen(){
  253.  
  254. graphic_write_command(LCD_set_page,B_CS1|B_CS2);
  255. graphic_write_command(LCD_set_addr,B_CS1|B_CS2);
  256. for(unit_8 i=0;i<8;i++){
  257. graphic_write_command(LCD_set_page | i,B_CS1|B_CS2);
  258. for(unit_8 j=0;j<64;j++){
  259. graphic_write_command(LCD_set_addr | j,B_CS1|B_CS2);
  260. graphic_write_data(0,B_CS1|B_CS2);
  261. }
  262. }
  263. }
  264. void lcd_set_data(unit_8 page,unit_8 adress,unit_8 data){
  265. unit_8 cs = B_CS1;
  266. if(adress>64){
  267. cs= B_CS2;
  268. }
  269. graphic_write_command(LCD_set_addr|(adress % 64),cs);
  270. graphic_write_command(LCD_set_page|page,cs);
  271. graphic_write_data(data,cs);
  272. }
  273. unit_8 lcd_read_data(unit_8 page,unit_8 adress){
  274. unit_8 cs = B_CS1;
  275. if(adress>64){
  276. cs= B_CS2;
  277. }
  278. graphic_write_command(LCD_set_addr|(adress % 64),cs);
  279. graphic_write_command(LCD_set_page|page,cs);
  280. return graphic_read_data(cs);
  281. }
  282.  
  283.  
  284. void pixel(unit_8 x,unit_8 y, unit_8 set){
  285. if((x<1)||(y<1)||(x>128)||(y>64)) return;
  286. unit_8 mask, controller,c;
  287. int index;
  288. index = (y-1)/8;
  289. mask = 1 << ((y-1)%8);
  290. if(set == 0){ mask = ~mask;
  291. }
  292.  
  293. if(x >64){
  294. controller =B_CS2;
  295. x =x-65;
  296. } else {
  297. controller =B_CS1;
  298. x =x-1;
  299. }
  300. graphic_write_command(LCD_set_addr |x,controller );
  301. graphic_write_command(LCD_set_page |index,controller );
  302.  
  303. c =graphic_read_data(controller);
  304. graphic_write_command(LCD_set_addr |x,controller);
  305. if(set){mask = mask | c;
  306. }
  307. else {mask = mask & c;
  308. }
  309.  
  310. graphic_write_data(mask,controller);
  311. }
  312.  
  313.  
  314. void graphic_init(){
  315.  
  316. graphic_ctrl_bit_set(B_E);
  317. delay_mikro(10);
  318.  
  319. graphic_ctrl_bit_clear(B_CS1|B_CS2|B_RST|B_E);
  320. delay_milli(30);
  321. graphic_ctrl_bit_set(B_RST);
  322. //delay_milli(100);
  323. delay_milli(10);
  324. graphic_write_command(LCD_OFF, B_CS1|B_CS2);//LCD off
  325. graphic_write_command(LCD_ON, B_CS1|B_CS2);//LCD on
  326. graphic_write_command(LCD_disp_start, B_CS1|B_CS2);//start=0
  327. graphic_write_command(LCD_set_addr, B_CS1|B_CS2);//startadress =0
  328. graphic_write_command(LCD_set_page, B_CS1|B_CS2);//page = 0
  329. select_controller(0x00);
  330.  
  331. }
  332.  
  333. unsigned char checkkey(){
  334. unsigned volatile char r=0x08;
  335. unsigned volatile char ret=0x00 ;
  336. for (int row = 0; row <4; row++){
  337. r = r << 1;
  338. *((unsigned char *)kp_O) = r;
  339. //*((unsigned char *)kp_O) = 0x80;
  340. unsigned volatile char c = (0x0F & (unsigned char) * ((unsigned char *) kp_I));
  341. if (c > 0){
  342. switch(c){
  343. case 0x01:
  344. switch(r)
  345. {
  346. case 0x10:// knapp 1
  347. ret=0x06;
  348. return ret;
  349. case 0x20:// knapp 4
  350. ret=0x66;
  351. return ret;
  352. case 0x40:// knapp 7
  353. ret=0x07;
  354. return ret;
  355. case 0x80:
  356. ret=0x79;
  357. return ret; //* key
  358.  
  359. }// case r
  360. case 0x02:
  361. switch(r)
  362. {
  363. case 0x10: // knapp 2
  364. ret=0x5b;
  365. return ret;
  366. case 0x20: // knapp 5
  367. ret=0x6d;
  368. return ret;
  369. case 0x40: // knapp 8
  370. ret=0x7F;
  371. return ret;
  372. case 0x80: // knapp 0
  373. ret=0x3F;
  374. return ret;
  375. }// case r
  376. case 0x04:
  377. switch(r)
  378. {
  379. case 0x10: // knapp 3
  380. ret=0x4F;
  381. return ret;
  382. case 0x20: // knapp 6
  383. ret=0x7D;
  384. return ret;
  385. case 0x40: // knapp 9
  386. ret=0x6F;
  387. return ret;
  388. case 0x80:
  389. ret=0x71;
  390. return ret; //# key
  391. }// case r
  392. case 0x08:
  393. switch(r)
  394. {
  395. case 0x10:
  396. ret=0x77;
  397. return ret;//A key
  398. case 0x20:
  399. ret=0x7C;
  400. return ret;//B key
  401. case 0x40:
  402. ret=0x39;
  403. return ret;//C key
  404. case 0x80:
  405. ret=0x5E;
  406. return ret; //D key
  407. }// case r
  408. }// case c
  409. }// if c < 0
  410. } //for int row
  411. return ret;
  412. }//check key
  413. void draw_ball(ball *self) {
  414. for(unit_8 i = 0; i <(ball_img_points*2);i = i+2){
  415. pixel(((self->cord_x)+self->image[i]),((self->cord_y)+self->image[i+1]),1);
  416. }
  417.  
  418. }
  419.  
  420. void move(ball* b){
  421.  
  422. unit_8 x = b->cord_x + b->dir_x;
  423. unit_8 y = b->cord_y + b->dir_y;
  424. if((x>128) || (x<0)){b->dir_x = -b->dir_x;}
  425. if((y>64) || (y<0)){b->dir_y = -b->dir_y;}
  426.  
  427. x = b->cord_x + b->dir_x;
  428. y = b->cord_y + b->dir_y;
  429.  
  430. //pixel(b->cord_x,b->cord_y,0);
  431. graphic_clear_screen();
  432. b->cord_x = x;
  433. b->cord_y = y;
  434. b->draw_ball(b);
  435. //pixel(b->cord_x,b->cord_y,1);
  436. }
  437. void readAndWait(ball* b){
  438.  
  439. *((unsigned long *)STK_CTRL) = 0x00;
  440. *((unsigned long *)STK_LOAD )=0xFFFFF0;
  441. //*((unsigned long *)STK_LOAD )=0x200;
  442. *((unsigned long *)STK_VAL )=0;
  443. *((unsigned long *)STK_CTRL) =5;
  444. while(*((unsigned long *)STK_CTRL) < 0x8){
  445.  
  446. //ADD KEYPAD STUFF
  447. unit_8 kp = checkkey();
  448. switch (kp){
  449. case (0x5b)://knapp 2
  450. b->dir_y = -2;
  451. b->dir_x = 0;
  452. break;
  453. case (0x7F)://knapp 8
  454. b->dir_y = 2;
  455. b->dir_x = 0;
  456. break;
  457. case (0x66): //knapp 4
  458. b->dir_x = -2;
  459. b->dir_y = 0;
  460. break;
  461. case (0x7D): //knapp 6
  462. b->dir_x = 2;
  463. b->dir_y = 0;
  464. break;
  465.  
  466. }
  467.  
  468.  
  469.  
  470. }
  471. *((unsigned long *)STK_CTRL) = 0;
  472.  
  473. }
  474.  
  475.  
  476. void init_app(void){
  477.  
  478. *((unsigned long *)0x40023830)=0x18;
  479. __asm volatile(" LDR R0,=0x08000209\n BLX R0 \n");
  480.  
  481. *portModer = 0x55555555;
  482. *((unsigned long *) 0x40020C00)= 0x55005555; //Config IO dip (D)
  483. *((unsigned long *) 0x40020C04)= 0x00000000; //OTYPER
  484. *((unsigned long *) 0x40020C0C)= 0x00AA0000; //PUPDR
  485. }
  486.  
  487. int main(void){
  488.  
  489. init_app();
  490. graphic_init();
  491. graphic_clear_screen();
  492.  
  493. ball ball1 = {
  494. 30, 20 ,0 ,0,
  495. {0,0, -1,0, 1,0, 0,-1, 0,1},
  496. draw_ball
  497. };
  498.  
  499.  
  500. while(1){
  501.  
  502. move(&ball1);
  503. //delay_milli(1000);
  504. readAndWait(&ball1);
  505.  
  506. }
  507.  
  508.  
  509. return 0;
  510.  
  511. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement