]> cloudbase.mooo.com Git - z180-stamp.git/blob - stm32/z180-stamp-stm32.c
ce161d815f1769d8cb015f6f4dc98f82d5ab05a8
[z180-stamp.git] / stm32 / z180-stamp-stm32.c
1 /*
2 */
3
4 #include <errno.h>
5 #include <stdio.h>
6 #include <unistd.h>
7
8 #include <libopencmsis/core_cm3.h>
9 #include <libopencm3/cm3/nvic.h>
10 #include <libopencm3/cm3/systick.h>
11 #include <libopencm3/stm32/rtc.h>
12 #include <libopencm3/stm32/usart.h>
13 #include <libopencm3/stm32/rcc.h>
14 #include <libopencm3/stm32/gpio.h>
15 #include <libopencm3/stm32/timer.h>
16
17 #define ODR 0x0c
18 #define IDR 0x08
19
20
21
22 #include "z80-if.h"
23 #include "hdrom.h"
24
25 #define USART_CONSOLE USART1
26
27 int _write(int fd, char *ptr, int len) __attribute__((used));
28
29 #define S_10MS_TO (1<<0)
30
31 /*
32 * LED Connections
33 */
34
35 #define LED_PORT GPIOC
36 #define LED_BLUE_PIN GPIO8
37 #define BLUE 8
38 #define LED_GREEN_PIN GPIO9
39 #define GREEN 9
40
41
42 #define LED_BLUE_ON() BBIO_PERIPH(LED_PORT+ODR, BLUE) = 1
43 #define LED_BLUE_OFF() BBIO_PERIPH(LED_PORT+ODR, BLUE) = 0
44 #define LED_BLUE_TOGGLE() BBIO_PERIPH(LED_PORT+ODR, BLUE) = !BBIO_PERIPH(LED_PORT+ODR, BLUE)
45
46 #define LED_GREEN_ON() BBIO_PERIPH(LED_PORT+ODR, GREEN) = 1
47 #define LED_GREEN_OFF() BBIO_PERIPH(LED_PORT+ODR, GREEN) = 0
48 #define LED_GREEN_TOGGLE() BBIO_PERIPH(LED_PORT+ODR, GREEN) = !BBIO_PERIPH(LED_PORT+ODR, GREEN)
49
50
51 /*
52 * Button connections
53 */
54
55 //BBIO_PERIPH(GPIOA+IDR, 0);
56
57 #define KEY_PORT GPIOA_IDR
58 #define KEY0 GPIO0
59 //#define KEY1 GPIO1
60 //#define KEY2 GPIO2
61
62 #define REPEAT_MASK KEY0 // repeat: key0
63 #define REPEAT_START 100 // after 1s
64 #define REPEAT_NEXT 20 // every 200ms
65
66
67 typedef enum {
68 NOTHING, PULSE, BLINK1, BLINK2
69 } LED_MODE;
70
71 typedef struct {
72 uint8_t mode;
73 uint8_t ontime, offtime;
74 } led_stat_t;
75
76 volatile uint8_t led_timer[2];
77 led_stat_t led_stat[2];
78
79 volatile int timeout_1s;
80 volatile uint32_t Stat;
81
82
83 /*--------------------------------------------------------------------------*/
84
85
86 static void clock_setup(void)
87 {
88 //rcc_clock_setup_in_hse_8mhz_out_24mhz();
89 rcc_clock_setup_in_hsi_out_24mhz();
90
91 /* Enable clocks for:
92 GPIO port A (for GPIO_USART1_TX and Button)
93 GPIO port C (LEDs)
94 USART1
95 TIM16 (RST-Pin)
96 TIM1 (IOCS1)
97 TODO: USART1 --> USART_CONSOLE
98 */
99 rcc_peripheral_enable_clock(&RCC_APB2ENR,
100 RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN
101 | RCC_APB2ENR_IOPCEN | RCC_APB2ENR_IOPDEN
102 | RCC_APB2ENR_USART1EN | RCC_APB2ENR_AFIOEN
103 | RCC_APB2ENR_TIM1EN | RCC_APB2ENR_TIM16EN);
104 /* Enable clocks for:
105 TIM3
106 */
107 rcc_peripheral_enable_clock(&RCC_APB1ENR,
108 RCC_APB1ENR_TIM3EN);
109
110 /* Enable clocks for:
111 DMA1
112 */
113 rcc_peripheral_enable_clock(&RCC_AHBENR,
114 RCC_AHBENR_DMA1EN);
115 }
116
117 static void systick_setup(void)
118 {
119 /* SysTick interrupt every N clock pulses: set reload to N-1 */
120 STK_RVR = 24000000/1000 - 1;
121
122 /* Set source to core clock, enable int and start counting. */
123 STK_CSR = STK_CSR_CLKSOURCE_AHB | STK_CSR_TICKINT | STK_CSR_ENABLE;
124 }
125
126 #if 0
127 static void nvic_setup(void)
128 {
129 // nvic_enable_irq(NVIC_RTC_IRQ);
130 // nvic_set_priority(NVIC_RTC_IRQ, 1);
131 }
132 #endif
133
134 static void tim3_setup(void)
135 {
136 TIM3_CR1 = TIM_CR1_CMS_EDGE | TIM_CR1_DIR_UP;
137
138 TIM3_CCMR2 = 0
139 | TIM_CCMR2_OC4M_FORCE_LOW
140 /* | TIM_CCMR2_OC4M_FORCE_HIGH */
141 /* | TIM_CCMR2_OC4M_PWM2 */
142
143 /* | TIM_CCMR2_OC4PE */
144 /* | TIM_CCMR2_OC4FE */
145 | TIM_CCMR2_CC4S_OUT;
146
147 TIM3_CCER = TIM_CCER_CC4E
148 | TIM_CCER_CC4P;
149
150 TIM3_ARR = 48; /* default */
151 TIM3_CCR4 = 1; /* */
152 }
153
154 static void gpio_setup(void)
155 {
156
157 /* Disable JTAG-DP, but leave SW-DP Enabled. (free PA15, PB3, PB4)
158 Remap SPI1 to PB3, PB4, PB5 and PA15.
159 Remap TIM3 (CH1/PC6, CH2/PC7, CH3/PC8, CH4/PC9)
160 Port D0/Port D1 mapping on OSC_IN/OSC_OUT
161 */
162 gpio_primary_remap(AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_ON,
163 AFIO_MAPR_SPI1_REMAP
164 | AFIO_MAPR_TIM3_REMAP_FULL_REMAP
165 | AFIO_MAPR_PD01_REMAP);
166
167 /* LEDs and User Button. */
168 gpio_set_mode(LED_PORT, GPIO_MODE_OUTPUT_2_MHZ,
169 GPIO_CNF_OUTPUT_PUSHPULL, LED_BLUE_PIN);
170 gpio_set_mode(LED_PORT, GPIO_MODE_OUTPUT_10_MHZ,
171 GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, LED_GREEN_PIN);
172 gpio_set_mode(GPIOA, GPIO_MODE_INPUT,
173 GPIO_CNF_INPUT_FLOAT, GPIO0);
174 }
175
176
177 static void usart_setup(void)
178 {
179 /* Setup GPIO pin GPIO_USART1_TX/LED_GREEN_PIN on GPIO port A for transmit. */
180 /* TODO: USART1 --> USART_CONSOLE */
181
182 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
183 GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);
184
185 /* Setup UART parameters. */
186 // usart_set_baudrate(USART_CONSOLE, 38400);
187 usart_set_baudrate(USART_CONSOLE, 115200);
188 usart_set_databits(USART_CONSOLE, 8);
189 usart_set_stopbits(USART_CONSOLE, USART_STOPBITS_1);
190 usart_set_mode(USART_CONSOLE, USART_MODE_TX_RX);
191 usart_set_parity(USART_CONSOLE, USART_PARITY_NONE);
192 usart_set_flow_control(USART_CONSOLE, USART_FLOWCONTROL_NONE);
193
194 /* Finally enable the USART. */
195 usart_enable(USART_CONSOLE);
196 }
197
198 /*--------------------------------------------------------------------------*/
199
200 /**
201 * Use USART_CONSOLE as a console.
202 * This is a syscall for newlib
203 * @param fd
204 * @param ptr
205 * @param len
206 * @return
207 */
208 int _write(int fd, char *ptr, int len)
209 {
210 int i;
211
212 if (fd == STDOUT_FILENO || fd == STDERR_FILENO) {
213 for (i = 0; i < len; i++) {
214 if (ptr[i] == '\n') {
215 usart_send_blocking(USART_CONSOLE, '\r');
216 }
217 usart_send_blocking(USART_CONSOLE, ptr[i]);
218 }
219 return i;
220 }
221 errno = EIO;
222 return -1;
223 }
224
225
226 /*--------------------------------------------------------------------------*/
227
228 void delay_systicks(int ticks)
229 {
230 int start, stop, now;
231
232 start = STK_CVR;
233 stop = start - ticks;
234 if (stop < 0) {
235 stop += STK_RVR;
236 do {
237 now = STK_CVR;
238 } while ((now > stop) || (now <= start));
239 } else {
240 do {
241 now = STK_CVR;
242 } while ((now > stop) && (now <= start));
243 }
244 }
245
246
247 /*--------------------------------------------------------------------------*/
248
249 static void led_toggle(uint8_t lednr) {
250 if (lednr == 0)
251 LED_BLUE_TOGGLE();
252 else if (lednr == 1)
253 LED_GREEN_TOGGLE();
254 }
255
256 static void led_on(uint8_t lednr) {
257 if (lednr == 0)
258 LED_BLUE_ON();
259 else if (lednr == 1)
260 LED_GREEN_ON();
261 }
262
263 static void led_off(uint8_t lednr) {
264 if (lednr == 0)
265 LED_BLUE_OFF();
266 else if (lednr == 1)
267 LED_GREEN_OFF();
268 }
269
270 static uint8_t led_is_on(uint8_t lednr) {
271 if (lednr == 0)
272 return BBIO_PERIPH(LED_PORT+ODR, BLUE);
273 else if (lednr == 1)
274 return BBIO_PERIPH(LED_PORT+ODR, GREEN);
275 else
276 return 0;
277 }
278
279 static void ledset(uint8_t lednr, uint8_t what, uint8_t len) {
280
281 led_stat[lednr].mode = what;
282 switch (what) {
283 case PULSE:
284 led_stat[lednr].ontime = len;
285 led_stat[lednr].offtime = 0;
286 led_timer[lednr] = len;
287 led_on(lednr);
288 break;
289 case BLINK1:
290 case BLINK2:
291 if (what == BLINK1)
292 led_stat[lednr].offtime = 100 - len;
293 else
294 led_stat[lednr].offtime = 200 - len;
295 led_stat[lednr].ontime = len;
296 led_timer[lednr] = len;
297 led_on(lednr);
298 break;
299 default:
300 break;
301 }
302 }
303
304 /*--------------------------------------------------------------------------*/
305
306 static volatile uint16_t key_state,
307 key_press, // key press detect
308 key_rpt; // key long press and repeat
309
310
311 static uint16_t get_key_press(uint16_t key_mask) {
312 __disable_irq();
313 // read and clear atomic !
314 key_mask &= key_press; // read key(s)
315 key_press ^= key_mask; // clear key(s)
316 __enable_irq();
317 return key_mask;
318 }
319
320 static uint16_t get_key_rpt(uint16_t key_mask) {
321 __disable_irq();
322 // read and clear atomic !
323 key_mask &= key_rpt; // read key(s)
324 key_rpt ^= key_mask; // clear key(s)
325 __enable_irq();
326 return key_mask;
327 }
328
329 static uint16_t get_key_short(uint16_t key_mask) {
330 __disable_irq();
331 // read key state and key press atomic !
332 return get_key_press(key_state & key_mask);
333 }
334
335 /*
336 static uint16_t get_key_long(uint16_t key_mask) {
337 return get_key_press(get_key_rpt(key_mask));
338 }
339 */
340
341 static void key_timerproc() {
342 static uint16_t key_in_last, rpt;
343 uint16_t key_in, c;
344
345 key_in = KEY_PORT;
346
347 c = key_in_last & key_in & ~key_state;
348
349 // key_state = key_state & key_in_last | (key_state | key_in_last) & key_in;
350 // key_state = key_state & key_in | (key_state | key_in) & key_in_last;
351
352 key_state = c | ((key_in_last | key_in) & key_state);
353
354 // key_state = (key_state&key_in_last) | (key_state&key_in) | (key_in_last&key_in);
355
356 key_press |= c;
357
358 key_in_last = key_in;
359
360
361 if ((key_state & REPEAT_MASK) == 0) // check repeat function
362 rpt = REPEAT_START;
363 if (--rpt == 0) {
364 rpt = REPEAT_NEXT; // repeat delay
365 key_rpt |= key_state & REPEAT_MASK;
366 }
367
368
369 #if 0
370
371 static char ds[30];
372 int dsi = 0;
373
374 ds[dsi++] = key_state & 1 ? '1' : '0';
375 ds[dsi++] = key_in_last & 1 ? '1' : '0';
376 ds[dsi++] = key_in & 1 ? '1' : '0';
377 ds[dsi++] = ' ';
378
379 //ds[dsi++] = key_state & 1 ? '1' : '0';
380 //ds[dsi++] = key_in_last & 1 ? '1' : '0';
381
382 //ds[dsi++] = ' ';
383 //ds[dsi++] = ' ';
384 ds[dsi++] = 0;
385 puts(ds);
386 #endif
387
388 }
389
390 /*--------------------------------------------------------------------------*/
391
392 void sys_tick_handler(void)
393 {
394 static int tick_10ms = 0;
395 static int count_ms = 0;
396
397 int i;
398
399 ++tick_10ms;
400 if (tick_10ms == 10)
401 {
402 Stat |= S_10MS_TO;
403
404 tick_10ms = 0;
405
406 i = led_timer[0];
407 if (i)
408 led_timer[0] = i - 1;
409 i = led_timer[1];
410 if (i)
411 led_timer[1] = i - 1;
412
413 key_timerproc();
414
415 /* Drive timer procedure of low level disk I/O module */
416 //disk_timerproc();
417 }
418
419 count_ms++;
420 if (count_ms == 1000) {
421 count_ms = 0;
422
423 i = timeout_1s;
424 if (i)
425 timeout_1s = i - 1;
426 }
427 }
428
429 void rtc_isr(void)
430 {
431 /* The interrupt flag isn't cleared by hardware, we have to do it. */
432 rtc_clear_flag(RTC_SEC);
433
434 }
435
436 /*--------------------------------------------------------------------------*/
437
438 void tim3_set(int mode)
439 {
440 uint16_t cc_mode;
441
442 cc_mode = TIM_CCMR2_CC4S_OUT;
443
444 TIM3_CR1 = TIM_CR1_CMS_EDGE | TIM_CR1_DIR_UP /*| TIM_CR1_OPM */ ;
445
446 if (mode < 0)
447 cc_mode |= TIM_CCMR2_OC4M_FORCE_LOW;
448 else if (mode == 0)
449 cc_mode |= TIM_CCMR2_OC4M_FORCE_HIGH;
450 else {
451 TIM3_ARR = mode;
452 TIM3_CCR4 = mode/2;
453 cc_mode |= TIM_CCMR2_OC4M_PWM2;
454 }
455
456 TIM3_CCMR2 = cc_mode;
457
458 if (mode > 0)
459 TIM3_CR1 |= TIM_CR1_CEN;
460 }
461
462 /*--------------------------------------------------------------------------*/
463
464 static uint32_t z80_sram_cmp(uint32_t addr, int length, uint8_t wval, int inc)
465 {
466 uint8_t rval;
467 int errors = 0;
468
469 printf("SRAM: Check %#.5x byte... ", length); //fflush(stdout);
470 while (length--) {
471 if ((rval = z80_read(addr)) != wval) {
472 if (errors == 0) {
473 printf("\nSRAM: Address W R\n" \
474 " -------------\n");
475 // 12345 00 11
476 }
477 printf(" %.5lx %.2x %.2x\n", addr, wval, rval);
478
479 if (++errors > 16 )
480 break;
481 }
482 addr++;
483 wval += inc;
484 }
485 printf("Done.\n");
486
487 return addr;
488 }
489
490 #if 0
491 static void z80_sram_fill(uint32_t addr, int length, uint8_t startval, int inc)
492 {
493 printf("SRAM: Write %#.5x byte... ", length); //fflush(stdout);
494 while (length--) {
495 z80_write(addr, startval);
496 ++addr;
497 startval += inc;
498 }
499 printf("Done.\n");
500 }
501
502
503 void z80_sram_fill_string(uint32_t addr, int length, const char *text)
504 {
505 char c;
506 const char *p = text;
507
508 while (length--) {
509 z80_write(addr++, c = *p++);
510 if (c == 0)
511 p = text;
512 }
513 }
514
515
516 uint32_t z80_sram_cmp_string(uint32_t addr, int length, const char *text)
517 {
518 char c;
519 const char *p = text;
520
521 while (length--) {
522 c = *p++;
523 if (z80_read(addr) != c)
524 break;
525 ++addr;
526 if (c == 0)
527 p = text;
528 }
529 return addr;
530 }
531
532 const char * const qbfox = "Zhe quick brown fox jumps over the lazy dog!";
533 const char * const qbcat = "Zhe quick brown fox jumps over the lazy cat!";
534
535 #endif
536
537 uint8_t z80_get_byte(uint32_t adr)
538 {
539 uint8_t data;
540
541 z80_request_bus();
542 data = z80_read(adr),
543 z80_release_bus();
544
545 return data;
546 }
547
548
549 /*--------------------------------------------------------------------------*/
550
551 static void do_10ms(void)
552 {
553 for (uint_fast8_t i = 0; i < 2; i++) {
554 switch (led_stat[i].mode) {
555 case PULSE:
556 if (led_timer[i] == 0) {
557 led_off(i);
558 led_stat[i].mode = NOTHING;
559 }
560 break;
561 case BLINK1:
562 case BLINK2:
563 if (led_timer[i] == 0) {
564 if (led_is_on(i))
565 led_timer[i] = led_stat[i].offtime;
566 else
567 led_timer[i] = led_stat[i].ontime;
568 led_toggle(i);
569 }
570 break;
571 default:
572 break;
573 }
574 }
575 }
576
577 void wait_for_z80_init_done(void)
578 {
579 uint8_t buf, out_i, in_i, mask;
580 int to;
581
582 timeout_1s = 10;
583 to = 0;
584 while (timeout_1s) {
585 if (to != timeout_1s) {
586 buf = z80_get_byte(tx_fifo - 0);
587 out_i = z80_get_byte(tx_fifo - 1);
588 in_i = z80_get_byte(tx_fifo - 2);
589 mask = z80_get_byte(tx_fifo - 3);
590 printf(" %.2x %.2x %.2x %.2x\n", buf, out_i, in_i, mask);
591 to = timeout_1s;
592
593 if ((out_i == 0) && (mask == 0x7f))
594 timeout_1s = 0;
595 }
596 }
597 }
598
599 /*--------------------------------------------------------------------------*/
600
601 int main(void)
602 {
603 //uint32_t led_state = LED_BLUE_PIN;
604 //uint32_t rc;
605 //uint8_t startval = 0;
606 //int count;
607 int stat, ch;
608 uint8_t c;
609
610 clock_setup();
611 gpio_setup();
612 tim3_setup();
613 setvbuf(stdout, NULL, _IONBF, 0);
614 usart_setup();
615 printf("\n(STM32F100+HD64180)_stamp Tester\n");
616
617 z80_setup_io_infifo();
618 z80_setup_bus();
619 printf("z80_setup_bus done.\n");
620
621 /*
622 * If the RTC is pre-configured just allow access, don't reconfigure.
623 * Otherwise enable it with the LSE as clock source and 0x7fff as
624 * prescale value.
625 */
626 rtc_auto_awake(LSE, 0x7fff);
627
628 systick_setup();
629 ///* Setup the RTC interrupt. */
630 //nvic_setup();
631
632 /* Enable the RTC interrupt to occur off the SEC flag. */
633 //rtc_interrupt_enable(RTC_SEC);
634
635 printf("get bus...");
636 z80_busreq(LOW);
637 z80_reset(HIGH);
638 z80_request_bus();
639 printf(" got it!\n");
640
641 z80_memset(0, 0x76, 0x80000);
642 //z80_sram_fill(0, 512 * 1024, 0x76, 0);
643 z80_sram_cmp(0, 512 * 1024, 0x76, 0);
644
645 z80_write_block((unsigned char *) hdrom, 0, hdrom_length);
646 z80_reset(LOW);
647 printf("bus released!\n");
648 z80_release_bus();
649 z80_reset(HIGH);
650 printf(" reset released!\n");
651
652 wait_for_z80_init_done();
653 z80_memfifo_init();
654
655 ledset(0, BLINK1, 50);
656
657 while (1) {
658 // static int tickstat = 0;
659
660 if (Stat & S_10MS_TO) {
661 Stat &= ~S_10MS_TO;
662 do_10ms();
663 }
664
665
666 // if (get_key_long(KEY0))
667 // ledset(1, PULSE, 100);
668
669 if (get_key_short(KEY0)) {
670 z80_reset_pulse();
671 wait_for_z80_init_done();
672 z80_memfifo_init();
673 }
674
675
676 /*
677 switch (tickstat) {
678
679 case 0:
680 if (BBIO_PERIPH(GPIOA+IDR, 0))
681 {
682 tickstat = 1;
683
684 LED_GREEN_ON();
685 LED_GREEN_OFF();
686 LED_GREEN_ON();
687 delay_systicks(12);
688 LED_GREEN_OFF();
689 }
690 break;
691 default:
692 if (!BBIO_PERIPH(GPIOA+IDR, 0))
693 tickstat = 0;
694 }
695 */
696
697 //BBIO_PERIPH(LED_PORT+0x0C, 9) = BBIO_PERIPH(GPIOA+0x08, 0);
698
699 //BBIO_PERIPH(LED_PORT+0x0C, 9) = !z80_stat_halt();
700
701 //BBIO_PERIPH(LED_PORT+0x0C, 9) = (~key_state & KEY0) != 0;
702
703
704 /*
705 stat = z80_fifo_is_not_full(rx_fifo);
706 if(stat) {
707 z80_fifo_putc(rx_fifo, 'y');
708 if (++count == 154) {
709 putchar('\n');
710 putchar('\r');
711 count = 0;
712 }
713
714 }
715 */
716
717 stat = usart_get_flag(USART_CONSOLE, USART_SR_RXNE);
718 if (stat) {
719 c = usart_recv(USART_CONSOLE) & 0xff;
720 switch (c) {
721 case 'H':
722 tim3_set(-1);
723 break;
724 case 'L':
725 tim3_set(0);
726 break;
727 case 'P':
728 tim3_set(24000000/1000000 * 5); /* 5 us */
729 break;
730 default:
731 z80_memfifo_putc(fifo_out, c);
732 }
733 }
734
735 if (timeout_1s == 0) {
736
737 while (!z80_memfifo_is_empty(fifo_in)) {
738 // LED_GREEN_ON();
739 c = z80_memfifo_getc(fifo_in);
740 putchar(c);
741 // LED_GREEN_OFF();
742 }
743
744 timeout_1s = 1;
745 }
746
747 while ((ch = z80_io_infifo_getc()) >= 0) {
748 static int linepos;
749
750 if (linepos == 0)
751 printf("\n");
752 printf(" 0x%.2X ", ch);
753 linepos = (linepos + 1) % 16;
754 }
755 }
756
757 return 0;
758 }
759
760 #if 0
761
762 static char ds[30];
763 int dsi = 0;
764
765 ds[dsi++] = key_state1 & 1 ? '1' : '0';
766 ds[dsi++] = key_in_last & 1 ? '1' : '0';
767 ds[dsi++] = key_in & 1 ? '1' : '0';
768 ds[dsi++] = ' ';
769 ds[dsi++] = key_state1 & 1 ? '1' : '0';
770 ds[dsi++] = key_in_last & 1 ? '1' : '0';
771
772 ds[dsi++] = ' ';
773 ds[dsi++] = ' ';
774 ds[dsi++] = key_state & 1 ? '1' : '0';
775 ds[dsi++] = ct1 & 1 ? '0' : '1';
776 ds[dsi++] = ct0 & 1 ? '0' : '1';
777 ds[dsi++] = ' ';
778 ds[dsi++] = key_state & 1 ? '1' : '0';
779 //ds[dsi++] = '\r';
780 //ds[dsi++] = '\n';
781 ds[dsi++] = 0;
782 puts(ds);
783 #endif