GadgetSeed  0.9.6
gpio_lcd_8bit.c
[詳解]
1 /** @file
2  @brief STM32 GPIO LCD (Arduino LCD Shield)
3 
4  Registor width 16 : ILI9325 (aitendo UL024TF)
5  Registor width 8 : ILI9341 (Kuman K60)
6 
7  @date 2017.10.15
8  @author Takashi SHUDO
9 
10  PA0 LCD_RD
11  PA1 LCD_WR
12  PA4 LCD_RS YP(Analog IN) ADC1 IN4
13  PB0 LCD_CS XM(Analog IN) ADC1 IN8
14  PC1 LCD_RESET
15 
16  PA9 LCD_D0 YM(Digital)
17  PC7 LCD_D1 XP(Digital)
18  PA10 LCD_D2
19  PB3 LCD_D3
20  PB5 LCD_D4
21  PB4 LCD_D5
22  PB10 LCD_D6
23  PA8 LCD_D7
24 */
25 
26 //#define GSC_DEV_ENABLE_TOUCHSENSOR
27 
28 #include "sysconfig.h"
29 #include "device.h"
30 #include "tprintf.h"
31 #include "tkprintf.h"
32 #include "system.h"
33 #include "device/vio_ioctl.h"
34 #include "task/syscall.h"
35 #include "task/mutex.h"
36 #include "sysevent.h"
37 #include "timer.h"
38 
39 #include "stm32f4xx_hal.h"
40 
41 //#define DEBUGKBITS 0x01
42 #include "dkprintf.h"
43 
44 
45 static unsigned char fore_color_h = 0;
46 static unsigned char fore_color_l = 0;
47 static unsigned char back_color_h = 0;
48 static unsigned char back_color_l = 0;
49 
50 static unsigned long f_bsrr_a_h;
51 static unsigned long f_bsrr_b_h;
52 static unsigned long f_bsrr_c_h;
53 static unsigned long f_bsrr_a_l;
54 static unsigned long f_bsrr_b_l;
55 static unsigned long f_bsrr_c_l;
56 
57 static unsigned long b_bsrr_a_h;
58 static unsigned long b_bsrr_b_h;
59 static unsigned long b_bsrr_c_h;
60 static unsigned long b_bsrr_a_l;
61 static unsigned long b_bsrr_b_l;
62 static unsigned long b_bsrr_c_l;
63 
64 #ifdef GSC_DEV_ENABLE_TOUCHSENSOR ///< $gsc タッチセンサデバイスを有効にする
65 static struct st_mutex gpio_mutex;
66 #define lock() mutex_lock(&gpio_mutex, 0/*1000*/)
67 #define unlock() mutex_unlock(&gpio_mutex)
68 #else
69 #define lock()
70 #define unlock()
71 #endif
72 
73 #define DATA_TO_GPIOA_BSRR(data) (\
74  ((((unsigned long)(data & (1<<0))?1:0)<<9) |\
75  (((unsigned long)(data & (1<<2))?1:0)<<10) |\
76  (((unsigned long)(data & (1<<7))?1:0)<<8))\
77  |\
78  (((((unsigned long)(data & (1<<0))?0:1)<<9) |\
79  (((unsigned long)(data & (1<<2))?0:1)<<10) |\
80  (((unsigned long)(data & (1<<7))?0:1)<<8)) << 16)\
81  )
82 
83 #define DATA_TO_GPIOB_BSRR(data) (\
84  ((((unsigned long)(data & (1<<3))?1:0)<<3) |\
85  (((unsigned long)(data & (1<<4))?1:0)<<5) |\
86  (((unsigned long)(data & (1<<5))?1:0)<<4) |\
87  (((unsigned long)(data & (1<<6))?1:0)<<10))\
88  |\
89  (((((unsigned long)(data & (1<<3))?0:1)<<3) |\
90  (((unsigned long)(data & (1<<4))?0:1)<<5) |\
91  (((unsigned long)(data & (1<<5))?0:1)<<4) |\
92  (((unsigned long)(data & (1<<6))?0:1)<<10)) << 16)\
93  )
94 
95 #define DATA_TO_GPIOC_BSRR(data) (\
96  (((unsigned long)(data & (1<<1))?1:0)<<7)\
97  |\
98  ((((unsigned long)(data & (1<<1))?0:1)<<7) << 16)\
99  )
100 
101 static void MX_GPIO_Init(void)
102 {
103  GPIO_InitTypeDef GPIO_InitStruct;
104 
105  /* GPIO Ports Clock Enable */
106  __GPIOC_CLK_ENABLE();
107  __GPIOA_CLK_ENABLE();
108  __GPIOB_CLK_ENABLE();
109 
110  /*Configure GPIO pins : PC1 PC7 */
111  GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_7;
112  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
113  GPIO_InitStruct.Pull = GPIO_NOPULL;
114  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
115  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
116 
117  /*Configure GPIO pins : PA0 PA1 PA4 PA8
118  PA9 PA10 */
119  GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_8
120  |GPIO_PIN_9|GPIO_PIN_10;
121  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
122  GPIO_InitStruct.Pull = GPIO_NOPULL;
123  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
124  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
125 
126  /*Configure GPIO pins : PB0 PB10 PB3 PB4
127  PB5 PB6 */
128  GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_10|GPIO_PIN_3|GPIO_PIN_4
129  |GPIO_PIN_5|GPIO_PIN_6;
130  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
131  GPIO_InitStruct.Pull = GPIO_NOPULL;
132  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
133  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
134 
135  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_SET);
136 }
137 
138 //#define HAL_USE
139 
140 static inline void rd_active(void)
141 {
142 #ifdef HAL_USE
143  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_RESET);
144 #else
145  GPIOA->BSRR = (((unsigned long)GPIO_PIN_0) << 16);
146  nop();
147 #endif
148 }
149 
150 static inline void rd_idle(void)
151 {
152 #ifdef HAL_USE
153  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_SET);
154 #else
155  nop();
156  GPIOA->BSRR = (unsigned long)GPIO_PIN_0;
157 #endif
158 }
159 
160 static inline void wr_active(void)
161 {
162 #ifdef HAL_USE
163  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
164 #else
165  GPIOA->BSRR = (((unsigned long)GPIO_PIN_1) << 16);
166  nop();
167 #endif
168 }
169 
170 static inline void wr_idle(void)
171 {
172 #ifdef HAL_USE
173  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);
174 #else
175  nop();
176  GPIOA->BSRR = (unsigned long)GPIO_PIN_1;
177 #endif
178 }
179 
180 static inline void cd_command(void)
181 {
182  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
183 }
184 
185 static inline void cd_data(void)
186 {
187  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
188 }
189 
190 static inline void set_reset(unsigned long arg)
191 {
192  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, arg);
193 }
194 
195 static inline void set_cs(unsigned long arg)
196 {
197  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, arg);
198 }
199 
200 static void dir_out_data(void)
201 {
202  // D0-D7を出力へ
203 
204  GPIO_InitTypeDef GPIO_InitStruct;
205 
206  /*Configure GPIO pins : PC7 */
207  GPIO_InitStruct.Pin = GPIO_PIN_7;
208  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
209  GPIO_InitStruct.Pull = GPIO_NOPULL;
210  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
211  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
212 
213  /*Configure GPIO pins : PA8 PA9 PA10 */
214  GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10;
215  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
216  GPIO_InitStruct.Pull = GPIO_NOPULL;
217  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
218  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
219 
220  /*Configure GPIO pins : PB10 PB3 PB4 PB5 */
221  GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_3|GPIO_PIN_4
222  |GPIO_PIN_5;
223  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
224  GPIO_InitStruct.Pull = GPIO_NOPULL;
225  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
226  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
227 }
228 
229 static void dir_in_data(void)
230 {
231  // D0-D7を入力へ
232 
233  GPIO_InitTypeDef GPIO_InitStruct;
234 
235  /*Configure GPIO pins : PC7 */
236  GPIO_InitStruct.Pin = GPIO_PIN_7;
237  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
238  GPIO_InitStruct.Pull = GPIO_NOPULL;
239  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
240  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
241 
242  /*Configure GPIO pins : PA8 PA9 PA10 */
243  GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10;
244  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
245  GPIO_InitStruct.Pull = GPIO_NOPULL;
246  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
247  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
248 
249  /*Configure GPIO pins : PB10 PB3 PB4 PB5 */
250  GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_3|GPIO_PIN_4
251  |GPIO_PIN_5;
252  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
253  GPIO_InitStruct.Pull = GPIO_NOPULL;
254  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
255  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
256 }
257 
258 static unsigned char read_8bits(void)
259 {
260 #if 0
261  return (((unsigned char)(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_9)) << 0) |
262  ((unsigned char)(HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_7)) << 1) |
263  ((unsigned char)(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_10)) << 2) |
264  ((unsigned char)(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_3)) << 3) |
265  ((unsigned char)(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_5)) << 4) |
266  ((unsigned char)(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_4)) << 5) |
267  ((unsigned char)(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_10)) << 6) |
268  ((unsigned char)(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_8)) << 7));
269 #else
270  return
271  (((GPIOA->IDR & 0x0100) >> 8) << 7) |
272  (((GPIOB->IDR & 0x0400) >> 10) << 6) |
273  (((GPIOB->IDR & 0x0010) >> 4) << 5) |
274  (((GPIOB->IDR & 0x0020) >> 5) << 4) |
275  (((GPIOB->IDR & 0x0008) >> 3) << 3) |
276  (((GPIOA->IDR & 0x0400) >> 10) << 2) |
277  (((GPIOC->IDR & 0x0080) >> 7) << 1) |
278  (((GPIOA->IDR & 0x0200) >> 9) << 0);
279 #endif
280 }
281 
282 static void write_8bits(unsigned char data)
283 {
284 #ifdef HAL_USE
285  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, data & (1<<0));
286  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, data & (1<<1));
287  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, data & (1<<2));
288  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, data & (1<<3));
289  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, data & (1<<4));
290  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, data & (1<<5));
291  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_10, data & (1<<6));
292  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, data & (1<<7));
293 #else
294 #if 0
295  /*
296  * こうするなら他のGPIOドライバと競合しないようにMUTEXが必要
297  */
298  GPIOA->ODR = ((GPIOA->IDR & (~((1<<9) | (1<<10) | (1<<8)))) | (
299  (((unsigned short)(data & (1<<0))?1:0)<<9) |
300  (((unsigned short)(data & (1<<2))?1:0)<<10) |
301  (((unsigned short)(data & (1<<7))?1:0)<<8)));
302  GPIOB->ODR = ((GPIOB->IDR & (~((1<<3) | (1<<5) | (1<<4) | (1<<10)))) | (
303  (((unsigned short)(data & (1<<3))?1:0)<<3) |
304  (((unsigned short)(data & (1<<4))?1:0)<<5) |
305  (((unsigned short)(data & (1<<5))?1:0)<<4) |
306  (((unsigned short)(data & (1<<6))?1:0)<<10)));
307  GPIOC->ODR = ((GPIOC->IDR & (~((1<<7)))) | (
308  (((unsigned short)(data & (1<<1))?1:0)<<7)));
309 #else
310  GPIOA->BSRR = DATA_TO_GPIOA_BSRR(data);
311  GPIOB->BSRR = DATA_TO_GPIOB_BSRR(data);
312  GPIOC->BSRR = DATA_TO_GPIOC_BSRR(data);
313 #endif
314 #endif
315 }
316 
317 /*
318  *
319  */
320 
321 static int gpio_read(struct st_device *dev, void *data, unsigned int size)
322 {
323  int i;
324  unsigned char *dp = data;
325 
326  lock();
327  cd_data();
328  dir_in_data();
329 
330  for(i=0; i<size/2; i++) {
331  unsigned char oh, ol;
332 #define LCD_PIXEL_3BYTES
333 #ifdef LCD_PIXEL_3BYTES
334  unsigned char ih, im, il;
335  rd_active();
336  ih = read_8bits();
337  rd_idle();
338  DKPRINTF(0x02, " %02X", ih);
339 
340  rd_active();
341  im = read_8bits();
342  rd_idle();
343  DKPRINTF(0x02, " %02X", im);
344 
345  rd_active();
346  il = read_8bits();
347  rd_idle();
348  DKPRINTF(0x02, " %02X\n", il);
349 
350  oh = (ih & 0xf8) | ((im & 0xe0) >> 5);
351  ol = ((im & 0x1c) << 3) | ((il & 0xf8) >> 3);
352 #else
353  rd_active();
354  oh = read_8bits();
355  rd_idle();
356  DKPRINTF(0x02, " %02X", ih);
357 
358  rd_active();
359  ol = read_8bits();
360  rd_idle();
361  DKPRINTF(0x02, " %02X", im);
362 #endif
363 
364  *(dp + 0) = ol;
365  *(dp + 1) = oh;
366 
367  dp += 2;
368  }
369 
370  dir_out_data();
371  unlock();
372 
373  KXDUMP(0x01, data, size);
374 
375  return size;
376 }
377 
378 static int gpio_write(struct st_device *dev, const void *data, unsigned int size)
379 {
380  int i;
381  const unsigned char *dp = data;
382 
383  lock();
384  cd_data();
385 
386  for(i=0; i<size/2; i++) {
387  // Little Endian
388  write_8bits(*(dp + 1));
389  wr_active();
390  wr_idle();
391  write_8bits(*dp);
392  wr_active();
393  wr_idle();
394  dp += 2;
395  }
396 
397  unlock();
398 
399  return size;
400 }
401 
402 static inline void write_fore_word(void)
403 {
404  GPIOA->BSRR = f_bsrr_a_h;
405  GPIOB->BSRR = f_bsrr_b_h;
406  GPIOC->BSRR = f_bsrr_c_h;
407  wr_active();
408  wr_idle();
409 
410  GPIOA->BSRR = f_bsrr_a_l;
411  GPIOB->BSRR = f_bsrr_b_l;
412  GPIOC->BSRR = f_bsrr_c_l;
413  wr_active();
414  wr_idle();
415 }
416 
417 static inline void write_back_word(void)
418 {
419  GPIOA->BSRR = b_bsrr_a_h;
420  GPIOB->BSRR = b_bsrr_b_h;
421  GPIOC->BSRR = b_bsrr_c_h;
422  wr_active();
423  wr_idle();
424 
425  GPIOA->BSRR = b_bsrr_a_l;
426  GPIOB->BSRR = b_bsrr_b_l;
427  GPIOC->BSRR = b_bsrr_c_l;
428  wr_active();
429  wr_idle();
430 }
431 
432 static int gpio_ioctl(struct st_device *dev, unsigned int com, unsigned int arg, void *param)
433 {
434  int rtn = 0;
435 
436  DKFPRINTF(0x02, "com = %08lX, arg = %08lX\n", com, arg);
437 
438  switch(IOCTL(com)) {
439  case IOCMD_VIO_LOCK_BUS:
440  lock();
441  break;
442 
444  unlock();
445  break;
446 
447  case IOCMD_VIO_SET_RESET: // PC1(LCD_RESET)
448  lock();
449  set_reset(arg);
450  unlock();
451  break;
452 
453  case IOCMD_VIO_SET_CS: // CS ASSERT/NEGATE
454  lock();
455  set_cs(arg);
456  unlock();
457  break;
458 
460  {
461  lock();
462  cd_command();
463 
464 #ifdef LCD_ADDR_16
465  write_8bits((unsigned char)((arg >> 8) & 0xff));
466  wr_active();
467  wr_idle();
468 #endif
469 
470  write_8bits((unsigned char)((arg >> 0) & 0xff));
471  wr_active();
472  wr_idle();
473  unlock();
474  }
475  break;
476 
478  {
479 #ifdef LCD_ADDR_16
480  unsigned char ad_h = ((arg >> 24) & 0xff);
481 #endif
482  unsigned char ad_l = ((arg >> 16) & 0xff);
483  unsigned char dt_l = ((arg >> 0) & 0xff);
484 
485  lock();
486  cd_command();
487 #ifdef LCD_ADDR_16
488  write_8bits(ad_h);
489  wr_active();
490  wr_idle();
491 #endif
492  write_8bits(ad_l);
493  wr_active();
494  wr_idle();
495 
496  cd_data();
497  write_8bits(dt_l);
498  wr_active();
499  wr_idle();
500  unlock();
501  }
502  break;
503 
505  {
506 #ifdef LCD_ADDR_16
507  unsigned char ad_h = ((arg >> 24) & 0xff);
508 #endif
509  unsigned char ad_l = ((arg >> 16) & 0xff);
510  unsigned char dt_h = ((arg >> 8) & 0xff);
511  unsigned char dt_l = ((arg >> 0) & 0xff);
512 
513  lock();
514  cd_command();
515 #ifdef LCD_ADDR_16
516  write_8bits(ad_h);
517  wr_active();
518  wr_idle();
519 #endif
520  write_8bits(ad_l);
521  wr_active();
522  wr_idle();
523 
524  cd_data();
525  write_8bits(dt_h);
526  wr_active();
527  wr_idle();
528 
529  write_8bits(dt_l);
530  wr_active();
531  wr_idle();
532  unlock();
533  }
534  break;
535 
537  {
538 #ifdef LCD_ADDR_16
539  unsigned char ad_h = ((arg >> 8) & 0xff);
540 #endif
541  unsigned char ad_l = ((arg >> 0) & 0xff);
542  unsigned long dt = 0;
543 
544  lock();
545  cd_command();
546 #ifdef LCD_ADDR_16
547  write_8bits(ad_h);
548  wr_active();
549  wr_idle();
550 #endif
551  write_8bits(ad_l);
552  wr_active();
553  wr_idle();
554 
555  cd_data();
556  dir_in_data();
557 
558  rd_active();
559  dt |= read_8bits();
560  rd_idle();
561 
562  dt <<= 8;
563  rd_active();
564  dt |= read_8bits();
565  rd_idle();
566 
567  dt <<= 8;
568  rd_active();
569  dt |= read_8bits();
570  rd_idle();
571 
572  dt <<= 8;
573  rd_active();
574  dt |= read_8bits();
575  rd_idle();
576 
577  dir_out_data();
578  unlock();
579 
580  rtn = dt;
581  }
582  break;
583 
585  {
586  unsigned char dt = (arg & 0xff);
587 
588  lock();
589  cd_data();
590  write_8bits(dt);
591  wr_active();
592  wr_idle();
593  unlock();
594  }
595  break;
596 
598  {
599  unsigned char dt_h = ((arg >> 8) & 0xff);
600  unsigned char dt_l = ((arg >> 0) & 0xff);
601 
602  lock();
603  cd_data();
604  write_8bits(dt_h);
605  wr_active();
606  wr_idle();
607  write_8bits(dt_l);
608  wr_active();
609  wr_idle();
610  unlock();
611  }
612  break;
613 
615  {
616  long dt;
617 
618  lock();
619  cd_data();
620  dir_in_data();
621 
622  rd_active();
623  dt = read_8bits();
624  rd_idle();
625 
626  dir_out_data();
627  unlock();
628 
629  rtn = dt;
630  }
631  break;
632 
634  {
635  unsigned char dt_h = ((arg >> 8) & 0xff);
636  unsigned char dt_l = ((arg >> 0) & 0xff);
637 
638  cd_data();
639  write_8bits(dt_h);
640  wr_active();
641  wr_idle();
642  write_8bits(dt_l);
643  wr_active();
644  wr_idle();
645  }
646  break;
647 
649  {
650  unsigned char dt_hh = ((arg >> 24) & 0xff);
651  unsigned char dt_hl = ((arg >> 16) & 0xff);
652  unsigned char dt_lh = ((arg >> 8) & 0xff);
653  unsigned char dt_ll = ((arg >> 0) & 0xff);
654 
655  lock();
656  cd_data();
657  write_8bits(dt_hh);
658  wr_active();
659  wr_idle();
660  write_8bits(dt_hl);
661  wr_active();
662  wr_idle();
663  write_8bits(dt_lh);
664  wr_active();
665  wr_idle();
666  write_8bits(dt_ll);
667  wr_active();
668  wr_idle();
669  unlock();
670  }
671  break;
672 
674  {
675  fore_color_h = (arg >> 8) & 0xff;
676  fore_color_l = (arg >> 0) & 0xff;
677 
678  f_bsrr_a_h = DATA_TO_GPIOA_BSRR(fore_color_h);
679  f_bsrr_b_h = DATA_TO_GPIOB_BSRR(fore_color_h);
680  f_bsrr_c_h = DATA_TO_GPIOC_BSRR(fore_color_h);
681  f_bsrr_a_l = DATA_TO_GPIOA_BSRR(fore_color_l);
682  f_bsrr_b_l = DATA_TO_GPIOB_BSRR(fore_color_l);
683  f_bsrr_c_l = DATA_TO_GPIOC_BSRR(fore_color_l);
684  }
685  break;
686 
688  {
689  back_color_h = (arg >> 8) & 0xff;
690  back_color_l = (arg >> 0) & 0xff;
691 
692  b_bsrr_a_h = DATA_TO_GPIOA_BSRR(back_color_h);
693  b_bsrr_b_h = DATA_TO_GPIOB_BSRR(back_color_h);
694  b_bsrr_c_h = DATA_TO_GPIOC_BSRR(back_color_h);
695  b_bsrr_a_l = DATA_TO_GPIOA_BSRR(back_color_l);
696  b_bsrr_b_l = DATA_TO_GPIOB_BSRR(back_color_l);
697  b_bsrr_c_l = DATA_TO_GPIOC_BSRR(back_color_l);
698  }
699  break;
700 
702  {
703  long i;
704 
705  lock();
706  cd_data();
707 
708  if(fore_color_h == fore_color_l) {
709  GPIOA->BSRR = f_bsrr_a_h;
710  GPIOB->BSRR = f_bsrr_b_h;
711  GPIOC->BSRR = f_bsrr_c_h;
712  for(i=0; i<arg; i++) {
713  wr_active();
714  wr_idle();
715  wr_active();
716  wr_idle();
717  }
718  } else {
719  for(i=0; i<arg; i++) {
720  write_fore_word();
721  }
722  }
723  unlock();
724  }
725  break;
726 
728  {
729  int sbit = ((com >> 12) & 0x7);
730  int count = (com & 0x0fff);
731  unsigned char *data = (unsigned char *)param;
732  unsigned char bit = (0x80 >> sbit);
733  int i;
734 
735  lock();
736  cd_data();
737 
738  DKPRINTF(0x02, "sbit=%d count=%d, data=%p\n", sbit, count, data);
739  for(i=0; i<count; i++) {
740  if(*data & bit) {
741  write_fore_word();
742  } else {
743  write_back_word();
744  }
745  if(bit == 0x01) {
746  bit = 0x80;
747  data ++;
748  } else {
749  bit >>= 1;
750  }
751  }
752  unlock();
753  }
754  break;
755 
756  default:
757  SYSERR_PRINT("Unknow command %08lX arg %08lX\n", com, arg);
758  rtn = -1;
759  break;
760  }
761 
762  return rtn;
763 }
764 
765 #ifdef GSC_DEV_ENABLE_TOUCHSENSOR
766 ADC_HandleTypeDef hadc1;
767 
768 /* ADC1 init function */
769 static void MX_ADC1_Init(void)
770 {
771  hadc1.Instance = ADC1;
772  // hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
773  hadc1.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2;
774  hadc1.Init.Resolution = ADC_RESOLUTION_12B;
775  hadc1.Init.ScanConvMode = DISABLE;
776  hadc1.Init.ContinuousConvMode = DISABLE;
777  hadc1.Init.DiscontinuousConvMode = DISABLE;
778  hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
779  hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
780  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
781  hadc1.Init.NbrOfConversion = 1;
782  hadc1.Init.DMAContinuousRequests = DISABLE;
783  hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
784 #if 0
785  if (HAL_ADC_Init(&hadc1) != HAL_OK) {
786  SYSERR_PRINT("ADC1 Initialize error");
787  }
788 
789  /**Configure for the selected ADC regular channel its
790  * corresponding rank in the sequencer and its sample time.
791  */
792  // sConfig.Channel = ADC_CHANNEL_4;
793  sConfig.Channel = ADC_CHANNEL_8;
794  sConfig.Rank = 1;
795  sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
796  if(HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) {
797  SYSERR_PRINT("ADC1 Config error");
798  }
799 #endif
800 }
801 
802 void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
803 {
804  GPIO_InitTypeDef GPIO_InitStruct;
805  if(hadc->Instance==ADC1) {
806  __HAL_RCC_ADC1_CLK_ENABLE();
807  GPIO_InitStruct.Pin = GPIO_PIN_4;
808  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
809  GPIO_InitStruct.Pull = GPIO_NOPULL;
810  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
811 
812  GPIO_InitStruct.Pin = GPIO_PIN_0;
813  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
814  GPIO_InitStruct.Pull = GPIO_NOPULL;
815  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
816  }
817 
818 }
819 
820 /*
821  LCD Arduino STM
822  YP(AIN) A2 PA4
823  XM(AIN) A3 PB0
824  YM 8 PA9
825  XP 9 PC7
826  */
827 unsigned long read_pos_x(void)
828 {
829  GPIO_InitTypeDef GPIO_InitStruct;
830  ADC_ChannelConfTypeDef sConfig;
831  unsigned long val;
832 
833  //pinMode(_yp, INPUT);
834  // PA4をアナログ入力
835  GPIO_InitStruct.Pin = GPIO_PIN_4;
836  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
837  GPIO_InitStruct.Pull = GPIO_NOPULL;
838  //GPIO_InitStruct.Pull = GPIO_PULLUP;
839  //GPIO_InitStruct.Pull = GPIO_PULLDOWN;
840  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
841 
842  //pinMode(_ym, INPUT);
843  // PA9をハイインピーダンスに
844  GPIO_InitStruct.Pin = GPIO_PIN_9;
845  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
846  //GPIO_InitStruct.Pull = GPIO_NOPULL;
847  GPIO_InitStruct.Pull = GPIO_PULLUP;
848  //GPIO_InitStruct.Pull = GPIO_PULLDOWN;
849  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
850  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
851 
852  //digitalWrite(_yp, LOW);
853  //digitalWrite(_ym, LOW);
854 
855  //pinMode(_xp, OUTPUT);
856  //digitalWrite(_xp, HIGH);
857  // PC7をHigh
858  GPIO_InitStruct.Pin = GPIO_PIN_7;
859  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
860  GPIO_InitStruct.Pull = GPIO_NOPULL;
861  //GPIO_InitStruct.Pull = GPIO_PULLDOWN;
862  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
863  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
864  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, GPIO_PIN_SET);
865 
866  //pinMode(_xm, OUTPUT);
867  //digitalWrite(_xm, LOW);
868  // PB0をLow
869  GPIO_InitStruct.Pin = GPIO_PIN_0;
870  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
871  GPIO_InitStruct.Pull = GPIO_NOPULL;
872  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
873  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
874  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
875 
876  sConfig.Channel = ADC_CHANNEL_4;
877  sConfig.Rank = 1;
878  sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
879  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
880  {
881  SYSERR_PRINT("ADC1 Config error");
882  }
883 
884  HAL_ADC_Start(&hadc1);
885  HAL_ADC_PollForConversion(&hadc1, 100);
886  val = HAL_ADC_GetValue(&hadc1);
887  HAL_ADC_Stop(&hadc1);
888  //tkprintf("PA4 = %-6ld ", val);
889 
890  //return (1023-analogRead(_yp));
891  return val;
892 }
893 
894 unsigned int read_pos_y(void)
895 {
896  GPIO_InitTypeDef GPIO_InitStruct;
897  ADC_ChannelConfTypeDef sConfig;
898  unsigned int val;
899 
900  //pinMode(_xp, INPUT);
901  // PC7をハイインピーダンスに
902  GPIO_InitStruct.Pin = GPIO_PIN_7;
903  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
904  //GPIO_InitStruct.Pull = GPIO_NOPULL;
905  //GPIO_InitStruct.Pull = GPIO_PULLUP;
906  GPIO_InitStruct.Pull = GPIO_PULLDOWN;
907  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
908  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
909 
910  //pinMode(_xm, INPUT);
911  // PB0をアナログ入力
912  GPIO_InitStruct.Pin = GPIO_PIN_0;
913  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
914  GPIO_InitStruct.Pull = GPIO_NOPULL;
915  //GPIO_InitStruct.Pull = GPIO_PULLUP;
916  //GPIO_InitStruct.Pull = GPIO_PULLDOWN;
917  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
918 
919  //digitalWrite(_xp, LOW);
920  //digitalWrite(_xm, LOW);
921 
922  //pinMode(_yp, OUTPUT);
923  //digitalWrite(_yp, HIGH);
924  // PA4をHigh
925  GPIO_InitStruct.Pin = GPIO_PIN_4;
926  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
927  GPIO_InitStruct.Pull = GPIO_NOPULL;
928  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
929  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
930  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
931 
932  //pinMode(_ym, OUTPUT);
933  //digitalWrite(_ym, LOW);
934  // PA9をLow
935  GPIO_InitStruct.Pin = GPIO_PIN_9;
936  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
937  GPIO_InitStruct.Pull = GPIO_NOPULL;
938  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
939  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
940  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET);
941 
942  sConfig.Channel = ADC_CHANNEL_8;
943  sConfig.Rank = 1;
944  sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
945  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
946  {
947  SYSERR_PRINT("ADC1 Config error");
948  }
949 
950  HAL_ADC_Start(&hadc1);
951  HAL_ADC_PollForConversion(&hadc1, 100);
952  val = HAL_ADC_GetValue(&hadc1);
953  HAL_ADC_Stop(&hadc1);
954  //tkprintf("PB0 = %-6ld\n", val);
955 
956  return val;
957 }
958 
959 #ifdef GSC_DEV_ENABLE_TOUCHSENSOR
960 #define SIZEOFSTACK (1024*1)
961 static struct st_tcb tcb;
962 static unsigned int stack[SIZEOFSTACK/sizeof(unsigned int)];
963 #endif
964 
965 static void ts_timer_func(void *sp, unsigned long long systime)
966 {
967  task_wakeup_id_ISR(sp, tcb.id);
968 }
969 
970 static int ts_mxl = 16;
971 static int ts_xl = 576;
972 static int ts_mxr = (GSC_GRAPHICS_DISPLAY_WIDTH-16);
973 static int ts_xr = 3667;
974 
975 static int ts_myt = 16;
976 static int ts_yt = 659;
977 static int ts_myb = (GSC_GRAPHICS_DISPLAY_HEIGHT-16);
978 static int ts_yb = 3446;
979 
980 static volatile int flg_touch = 0;
981 static int lpos_x;
982 static int lpos_y;
983 static int ts_low_val[2];
984 
985 static int ts_task(char *arg)
986 {
987  struct st_sysevent ev;
988 
989  while(1) {
990  int vx, vy;
991  int px, py;
992 
993  lock();
994  //!!! MUTEXに問題ありそう(2重Lock/Unlockを検出する場合有り)
995 
996  vx = read_pos_x();
997  vy = read_pos_y();
998  ts_low_val[0] = vx;
999  ts_low_val[1] = vy;
1000  ev.private_data = (void *)ts_low_val;
1001 
1002  MX_GPIO_Init();
1003 
1004  unlock();
1005 
1006  if((vx < 4000) && (vy > 420)) {
1007  DKPRINTF(0x02, "VX= %4ld, VY= %4ld\n", vx, vy);
1008 
1009  // 座標の計算
1010  px = (((vx - ts_xl + ts_mxl) * (ts_mxr - ts_mxl)) / (ts_xr - ts_xl)) + ts_mxl;
1011  py = (((vy - ts_yt + ts_myt) * (ts_myb - ts_myt)) / (ts_yb - ts_yt)) + ts_myt;
1012  DKPRINTF(0x01, "X = %4ld, Y = %4ld\n", px, py);
1013 
1014  if(flg_touch == 0) {
1015  DKPRINTF(0x01, "TS START\n");
1016  ev.what = EVT_TOUCHSTART;
1017  ev.pos_x = px;
1018  ev.pos_y = py;
1019  lpos_x = px;
1020  lpos_y = py;
1021  flg_touch = 1;
1022  wait_utime(1000); // FIXME これがないとSTARTが取れない?
1023  set_event(&ev);
1024  } else if((px != lpos_x) || (py != lpos_y)) {
1025  ev.what = EVT_TOUCHMOVE;
1026  ev.pos_x = px;
1027  ev.pos_y = py;
1028  lpos_x = px;
1029  lpos_y = py;
1030  set_event(&ev);
1031  } else {
1032  DKPRINTF(0x01, "TS STAY\n");
1033  }
1034  } else {
1035  if(flg_touch != 0) {
1036  DKPRINTF(0x02, "TS END\n");
1037  ev.what = EVT_TOUCHEND;
1038  ev.pos_x = lpos_x;
1039  ev.pos_y = lpos_y;
1040  flg_touch = 0;
1041  set_event(&ev);
1042  }
1043  }
1044 
1045  task_pause();
1046  //task_sleep(20);
1047  }
1048 
1049  return 0;
1050 }
1051 #endif // GSC_DEV_ENABLE_TOUCHSENSOR
1052 
1053 static int gpio_register(struct st_device *dev, char *param)
1054 {
1055 #ifdef GSC_DEV_ENABLE_TOUCHSENSOR
1056  MX_ADC1_Init();
1057  __HAL_RCC_ADC1_CLK_ENABLE();
1058 #endif
1059  MX_GPIO_Init();
1060 
1061  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_SET); // LCD_RD Hi
1062  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET); // LCD_WR Hi
1063  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET); // LCD_RS Hi
1064  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET); // LCD_CS Hi
1065  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, GPIO_PIN_RESET); // LCD_RESET Lo
1066 
1067 #ifdef GSC_DEV_ENABLE_TOUCHSENSOR
1069 
1070  task_add(ts_task, "touch_sensor", 1, &tcb,
1071  stack, SIZEOFSTACK, 0);
1072 
1073  register_timer_func(ts_timer_func, 20);
1074 #endif
1075 
1076  return 0;
1077 }
1078 
1079 const struct st_device lcd_gpio_device = {
1081  .explan = "STM32F4 LCD GPIO",
1082  .register_dev = gpio_register,
1083  .read = gpio_read,
1084  .write = gpio_write,
1085  .ioctl = gpio_ioctl,
1086 };
1087 
1088 
1089 #ifdef GSC_DEV_ENABLE_TOUCHSENSOR
1090 
1091 #include "device/ts_ioctl.h"
1092 
1093 static int ts_analog_ioctl(struct st_device *dev, unsigned int com, unsigned int arg, void *param)
1094 {
1095  int rtn = 0;
1096 
1097  DKFPRINTF(0x02, "com = %08lX, arg = %08lX\n", com, arg);
1098 
1099  switch(IOCTL(com)) {
1100  case IOCMD_TS_SET_CALIB:
1101  {
1102  struct st_ts_calib_data *cd;
1103  cd = (struct st_ts_calib_data *)param;
1104 
1105  DKPRINTF(0x01, "LEFT POS=%4d, VAL=%5d\n", cd->left_pos, cd->left_val);
1106  DKPRINTF(0x01, "RIGHT POS=%4d, VAL=%5d\n", cd->right_pos, cd->right_val);
1107  DKPRINTF(0x01, "TOP POS=%4d, VAL=%5d\n", cd->top_pos, cd->top_val);
1108  DKPRINTF(0x01, "BOTTOM POS=%4d, VAL=%5d\n", cd->bottom_pos, cd->bottom_val);
1109 
1110  ts_mxl = cd->left_pos;
1111  ts_xl = cd->left_val;
1112  ts_mxr = cd->right_pos;
1113  ts_xr = cd->right_val;
1114 
1115  ts_myt = cd->top_pos;
1116  ts_yt = cd->top_val;
1117  ts_myb = cd->bottom_pos;
1118  ts_yb = cd->bottom_val;
1119  }
1120  break;
1121 
1122  default:
1123  SYSERR_PRINT("Unknow command %08lX arg %08lX\n", com, arg);
1124  rtn = -1;
1125  break;
1126  }
1127 
1128  return rtn;
1129 }
1130 
1131 const struct st_device ts_analog_device = {
1132  .name = DEF_DEV_NAME_TS,
1133  .explan = "STM32F4 LCD Touch Sensor",
1134  .ioctl = ts_analog_ioctl,
1135 };
1136 
1137 #endif // GSC_DEV_ENABLE_TOUCHSENSOR
#define IOCTL(ioctl)
ioctlコマンド
Definition: std_ioctl.h:29
#define IOCMD_VIO_READ_DATA8
コントローラデバイスから8ビットデータを読み出す
Definition: vio_ioctl.h:31
#define IOCMD_VIO_WRITE_REG16
コントローラデバイスのレジスタに16ビットデータを書き込む
Definition: vio_ioctl.h:34
char * arg
タスク実行時引数文字列
Definition: tcb.h:46
#define IOCMD_VIO_REPEAT_BITS
指定のビットデータを描画データ0、1で描画する
Definition: vio_ioctl.h:46
#define EVT_TOUCHEND
(画面に)タッチした状態から離した
Definition: sysevent.h:33
#define IOCMD_VIO_LOCK_BUS
バスをMUTEXロックする
Definition: vio_ioctl.h:18
#define IOCMD_VIO_WRITE_DATA16
コントローラデバイスに16ビットデータを書き込む
Definition: vio_ioctl.h:26
#define IOCMD_VIO_SET_RESET
デバイスのリセットを設定する
Definition: vio_ioctl.h:21
#define DEF_DEV_NAME_TS
標準タッチセンサデバイス名
Definition: ts_ioctl.h:15
#define IOCMD_VIO_WRITE_COMMAND
コントローラデバイスにコマンドを書き込む
Definition: vio_ioctl.h:24
int set_event(struct st_sysevent *event)
システムイベントを登録する
Definition: sysevent.c:128
#define IOCMD_VIO_READ_REG32
コントローラデバイスのレジスタより32ビットデータを読み出す
Definition: vio_ioctl.h:41
int task_add(task_func func, char *name, int priority, struct st_tcb *tcb, unsigned int *stack, int stack_size, char *arg)
タスクを追加する
Definition: syscall_api.c:188
int register_timer_func(timer_func func, unsigned long interval)
周期処理を追加する
Definition: timer.c:274
#define IOCMD_VIO_REPEAT_DATA
描画データ0で指定ドット数描画する
Definition: vio_ioctl.h:45
カーネルタイマ
タッチセンサドライバ ioctl 用マクロ定義
void task_pause(void)
タスクを停止する
Definition: syscall_api.c:276
カーネル用機能限定printf
unsigned short what
イベント種類
Definition: sysevent.h:13
static int ts_task(char *arg)
システムイベント
#define IOCMD_VIO_SET_WRITEDATA0
描画データ0を設定する
Definition: vio_ioctl.h:43
void * sp
スタックポインタ
Definition: tcb.h:40
unsigned short pos_x
イベント発生X座標
Definition: sysevent.h:15
#define IOCMD_VIO_NOLOCK_WRITE_DATA16
コントローラデバイスに16ビットデータを書き込む(ロックは無視)
Definition: vio_ioctl.h:29
#define IOCMD_VIO_SET_WRITEDATA1
描画データ1を設定する
Definition: vio_ioctl.h:44
#define IOCMD_VIO_SET_CS
CSを設定する
Definition: vio_ioctl.h:22
システムコール
タッチセンサキャリブレーションデータ
Definition: ts_ioctl.h:17
MUTEX制御
#define IOCMD_VIO_WRITE_DATA32
コントローラデバイスに32ビットデータを書き込む
Definition: vio_ioctl.h:28
#define EVT_TOUCHSTART
(画面に)タッチした
Definition: sysevent.h:31
#define EVT_TOUCHMOVE
(画面に)タッチしたまま動かした
Definition: sysevent.h:32
void wait_utime(unsigned int time)
指定時間待つ
Definition: timer.c:243
システムイベント
Definition: sysevent.h:12
void task_wakeup_id_ISR(void *sp, int id)
idタスクを実行する
Definition: task.c:662
#define IOCMD_VIO_UNLOCK_BUS
バスをMUTEXアンロックする
Definition: vio_ioctl.h:19
システム固有初期化関連
#define IOCMD_VIO_WRITE_DATA8
コントローラデバイスに8ビットデータを書き込む
Definition: vio_ioctl.h:25
映像関連ドライバIO ioctl 用マクロ定義
static unsigned long b_bsrr_c_l
$gsc タッチセンサデバイスを有効にする
Definition: gpio_lcd_8bit.c:62
#define IOCMD_VIO_WRITE_REG8
コントローラデバイスのレジスタに8ビットデータを書き込む
Definition: vio_ioctl.h:33
#define DEF_DEV_NAME_VIDEOIO
標準ビデオIOデバイスドライバ名
Definition: vio_ioctl.h:15
デバイスドライバAPI
unsigned short pos_y
イベント発生Y座標
Definition: sysevent.h:16
デバイスドライバ構造体
Definition: device.h:25
カーネル、ドライバ(非タスク)デバッグ用マクロ
int id
タスクID
Definition: tcb.h:38
char name[MAX_DEVNAMELRN]
デバイス名文字列
Definition: device.h:26
タスクコンテキスト
Definition: tcb.h:32
void mutex_register_ISR(struct st_mutex *mutex, const char *name)
MUTEXを登録する
Definition: mutex.c:37
#define IOCMD_TS_SET_CALIB
キャリブレーションデータを設定する
Definition: ts_ioctl.h:29
機能限定printf