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>
25 #define USART_CONSOLE USART1
27 int _write(int fd
, char *ptr
, int len
) __attribute__((used
));
29 #define S_10MS_TO (1<<0)
35 #define LED_PORT GPIOC
36 #define LED_BLUE_PIN GPIO8
38 #define LED_GREEN_PIN GPIO9
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)
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)
55 //BBIO_PERIPH(GPIOA+IDR, 0);
57 #define KEY_PORT GPIOA_IDR
62 #define REPEAT_MASK KEY0 // repeat: key0
63 #define REPEAT_START 100 // after 1s
64 #define REPEAT_NEXT 20 // every 200ms
68 NOTHING
, PULSE
, BLINK1
, BLINK2
73 uint8_t ontime
, offtime
;
76 volatile uint8_t led_timer
[2];
77 led_stat_t led_stat
[2];
79 volatile int timeout_1s
;
80 volatile uint32_t Stat
;
83 /*--------------------------------------------------------------------------*/
86 static void clock_setup(void)
88 //rcc_clock_setup_in_hse_8mhz_out_24mhz();
89 rcc_clock_setup_in_hsi_out_24mhz();
92 GPIO port A (for GPIO_USART1_TX and Button)
97 TODO: USART1 --> USART_CONSOLE
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:
107 rcc_peripheral_enable_clock(&RCC_APB1ENR
,
110 /* Enable clocks for:
113 rcc_peripheral_enable_clock(&RCC_AHBENR
,
117 static void systick_setup(void)
119 /* SysTick interrupt every N clock pulses: set reload to N-1 */
120 STK_RVR
= 24000000/1000 - 1;
122 /* Set source to core clock, enable int and start counting. */
123 STK_CSR
= STK_CSR_CLKSOURCE_AHB
| STK_CSR_TICKINT
| STK_CSR_ENABLE
;
127 static void nvic_setup(void)
129 // nvic_enable_irq(NVIC_RTC_IRQ);
130 // nvic_set_priority(NVIC_RTC_IRQ, 1);
134 static void tim3_setup(void)
136 TIM3_CR1
= TIM_CR1_CMS_EDGE
| TIM_CR1_DIR_UP
;
139 | TIM_CCMR2_OC4M_FORCE_LOW
140 /* | TIM_CCMR2_OC4M_FORCE_HIGH */
141 /* | TIM_CCMR2_OC4M_PWM2 */
143 /* | TIM_CCMR2_OC4PE */
144 /* | TIM_CCMR2_OC4FE */
145 | TIM_CCMR2_CC4S_OUT
;
147 TIM3_CCER
= TIM_CCER_CC4E
150 TIM3_ARR
= 48; /* default */
154 static void gpio_setup(void)
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
162 gpio_primary_remap(AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_ON
,
164 | AFIO_MAPR_TIM3_REMAP_FULL_REMAP
165 | AFIO_MAPR_PD01_REMAP
);
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
);
177 static void usart_setup(void)
179 /* Setup GPIO pin GPIO_USART1_TX/LED_GREEN_PIN on GPIO port A for transmit. */
180 /* TODO: USART1 --> USART_CONSOLE */
182 gpio_set_mode(GPIOA
, GPIO_MODE_OUTPUT_50_MHZ
,
183 GPIO_CNF_OUTPUT_ALTFN_PUSHPULL
, GPIO_USART1_TX
);
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
);
194 /* Finally enable the USART. */
195 usart_enable(USART_CONSOLE
);
198 /*--------------------------------------------------------------------------*/
201 * Use USART_CONSOLE as a console.
202 * This is a syscall for newlib
208 int _write(int fd
, char *ptr
, int len
)
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');
217 usart_send_blocking(USART_CONSOLE
, ptr
[i
]);
226 /*--------------------------------------------------------------------------*/
228 void delay_systicks(int ticks
)
230 int start
, stop
, now
;
233 stop
= start
- ticks
;
238 } while ((now
> stop
) || (now
<= start
));
242 } while ((now
> stop
) && (now
<= start
));
247 /*--------------------------------------------------------------------------*/
249 static void led_toggle(uint8_t lednr
) {
256 static void led_on(uint8_t lednr
) {
263 static void led_off(uint8_t lednr
) {
270 static uint8_t led_is_on(uint8_t lednr
) {
272 return BBIO_PERIPH(LED_PORT
+ODR
, BLUE
);
274 return BBIO_PERIPH(LED_PORT
+ODR
, GREEN
);
279 static void ledset(uint8_t lednr
, uint8_t what
, uint8_t len
) {
281 led_stat
[lednr
].mode
= what
;
284 led_stat
[lednr
].ontime
= len
;
285 led_stat
[lednr
].offtime
= 0;
286 led_timer
[lednr
] = len
;
292 led_stat
[lednr
].offtime
= 100 - len
;
294 led_stat
[lednr
].offtime
= 200 - len
;
295 led_stat
[lednr
].ontime
= len
;
296 led_timer
[lednr
] = len
;
304 /*--------------------------------------------------------------------------*/
306 static volatile uint16_t key_state
,
307 key_press
, // key press detect
308 key_rpt
; // key long press and repeat
311 static uint16_t get_key_press(uint16_t key_mask
) {
313 // read and clear atomic !
314 key_mask
&= key_press
; // read key(s)
315 key_press
^= key_mask
; // clear key(s)
320 static uint16_t get_key_rpt(uint16_t key_mask
) {
322 // read and clear atomic !
323 key_mask
&= key_rpt
; // read key(s)
324 key_rpt
^= key_mask
; // clear key(s)
329 static uint16_t get_key_short(uint16_t key_mask
) {
331 // read key state and key press atomic !
332 return get_key_press(key_state
& key_mask
);
336 static uint16_t get_key_long(uint16_t key_mask) {
337 return get_key_press(get_key_rpt(key_mask));
341 static void key_timerproc() {
342 static uint16_t key_in_last
, rpt
;
347 c
= key_in_last
& key_in
& ~key_state
;
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;
352 key_state
= c
| ((key_in_last
| key_in
) & key_state
);
354 // key_state = (key_state&key_in_last) | (key_state&key_in) | (key_in_last&key_in);
358 key_in_last
= key_in
;
361 if ((key_state
& REPEAT_MASK
) == 0) // check repeat function
364 rpt
= REPEAT_NEXT
; // repeat delay
365 key_rpt
|= key_state
& REPEAT_MASK
;
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';
379 //ds[dsi++] = key_state & 1 ? '1' : '0';
380 //ds[dsi++] = key_in_last & 1 ? '1' : '0';
390 /*--------------------------------------------------------------------------*/
392 void sys_tick_handler(void)
394 static int tick_10ms
= 0;
395 static int count_ms
= 0;
408 led_timer
[0] = i
- 1;
411 led_timer
[1] = i
- 1;
415 /* Drive timer procedure of low level disk I/O module */
420 if (count_ms
== 1000) {
431 /* The interrupt flag isn't cleared by hardware, we have to do it. */
432 rtc_clear_flag(RTC_SEC
);
436 /*--------------------------------------------------------------------------*/
438 void tim3_set(int mode
)
442 cc_mode
= TIM_CCMR2_CC4S_OUT
;
444 TIM3_CR1
= TIM_CR1_CMS_EDGE
| TIM_CR1_DIR_UP
/*| TIM_CR1_OPM */ ;
447 cc_mode
|= TIM_CCMR2_OC4M_FORCE_LOW
;
449 cc_mode
|= TIM_CCMR2_OC4M_FORCE_HIGH
;
453 cc_mode
|= TIM_CCMR2_OC4M_PWM2
;
456 TIM3_CCMR2
= cc_mode
;
459 TIM3_CR1
|= TIM_CR1_CEN
;
462 /*--------------------------------------------------------------------------*/
464 static uint32_t z80_sram_cmp(uint32_t addr
, int length
, uint8_t wval
, int inc
)
469 printf("SRAM: Check %#.5x byte... ", length
); //fflush(stdout);
471 if ((rval
= z80_read(addr
)) != wval
) {
473 printf("\nSRAM: Address W R\n" \
477 printf(" %.5lx %.2x %.2x\n", addr
, wval
, rval
);
491 static void z80_sram_fill(uint32_t addr
, int length
, uint8_t startval
, int inc
)
493 printf("SRAM: Write %#.5x byte... ", length
); //fflush(stdout);
495 z80_write(addr
, startval
);
503 void z80_sram_fill_string(uint32_t addr
, int length
, const char *text
)
506 const char *p
= text
;
509 z80_write(addr
++, c
= *p
++);
516 uint32_t z80_sram_cmp_string(uint32_t addr
, int length
, const char *text
)
519 const char *p
= text
;
523 if (z80_read(addr
) != c
)
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!";
537 uint8_t z80_get_byte(uint32_t adr
)
542 data
= z80_read(adr
),
549 /*--------------------------------------------------------------------------*/
551 static void do_10ms(void)
553 for (uint_fast8_t i
= 0; i
< 2; i
++) {
554 switch (led_stat
[i
].mode
) {
556 if (led_timer
[i
] == 0) {
558 led_stat
[i
].mode
= NOTHING
;
563 if (led_timer
[i
] == 0) {
565 led_timer
[i
] = led_stat
[i
].offtime
;
567 led_timer
[i
] = led_stat
[i
].ontime
;
577 void wait_for_z80_init_done(void)
579 uint8_t buf
, out_i
, in_i
, mask
;
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
);
593 if ((out_i
== 0) && (mask
== 0x7f))
599 /*--------------------------------------------------------------------------*/
603 //uint32_t led_state = LED_BLUE_PIN;
605 //uint8_t startval = 0;
613 setvbuf(stdout
, NULL
, _IONBF
, 0);
615 printf("\n(STM32F100+HD64180)_stamp Tester\n");
617 z80_setup_io_infifo();
619 printf("z80_setup_bus done.\n");
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
626 rtc_auto_awake(LSE
, 0x7fff);
629 ///* Setup the RTC interrupt. */
632 /* Enable the RTC interrupt to occur off the SEC flag. */
633 //rtc_interrupt_enable(RTC_SEC);
635 printf("get bus...");
639 printf(" got it!\n");
641 z80_memset(0, 0x76, 0x80000);
642 //z80_sram_fill(0, 512 * 1024, 0x76, 0);
643 z80_sram_cmp(0, 512 * 1024, 0x76, 0);
645 z80_write_block((unsigned char *) hdrom
, 0, hdrom_length
);
647 printf("bus released!\n");
650 printf(" reset released!\n");
652 wait_for_z80_init_done();
655 ledset(0, BLINK1
, 50);
658 // static int tickstat = 0;
660 if (Stat
& S_10MS_TO
) {
666 // if (get_key_long(KEY0))
667 // ledset(1, PULSE, 100);
669 if (get_key_short(KEY0
)) {
671 wait_for_z80_init_done();
680 if (BBIO_PERIPH(GPIOA+IDR, 0))
692 if (!BBIO_PERIPH(GPIOA+IDR, 0))
697 //BBIO_PERIPH(LED_PORT+0x0C, 9) = BBIO_PERIPH(GPIOA+0x08, 0);
699 //BBIO_PERIPH(LED_PORT+0x0C, 9) = !z80_stat_halt();
701 //BBIO_PERIPH(LED_PORT+0x0C, 9) = (~key_state & KEY0) != 0;
705 stat = z80_fifo_is_not_full(rx_fifo);
707 z80_fifo_putc(rx_fifo, 'y');
708 if (++count == 154) {
717 stat
= usart_get_flag(USART_CONSOLE
, USART_SR_RXNE
);
719 c
= usart_recv(USART_CONSOLE
) & 0xff;
728 tim3_set(24000000/1000000 * 5); /* 5 us */
731 z80_memfifo_putc(fifo_out
, c
);
735 if (timeout_1s
== 0) {
737 while (!z80_memfifo_is_empty(fifo_in
)) {
739 c
= z80_memfifo_getc(fifo_in
);
747 while ((ch
= z80_io_infifo_getc()) >= 0) {
752 printf(" 0x%.2X ", ch
);
753 linepos
= (linepos
+ 1) % 16;
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';
769 ds
[dsi
++] = key_state1
& 1 ? '1' : '0';
770 ds
[dsi
++] = key_in_last
& 1 ? '1' : '0';
774 ds
[dsi
++] = key_state
& 1 ? '1' : '0';
775 ds
[dsi
++] = ct1
& 1 ? '0' : '1';
776 ds
[dsi
++] = ct0
& 1 ? '0' : '1';
778 ds
[dsi
++] = key_state
& 1 ? '1' : '0';