6 #include <libopencmsis/core_cm3.h>
7 #include <libopencm3/cm3/nvic.h>
8 #include <libopencm3/cm3/systick.h>
9 #include <libopencm3/stm32/rtc.h>
10 #include <libopencm3/stm32/rcc.h>
11 #include <libopencm3/stm32/gpio.h>
12 #include <libopencm3/stm32/timer.h>
24 #define ESCCHAR ('^'-0x40)
26 #define S_10MS_TO (1<<0)
32 #define LED_PORT GPIOC
33 #define LED_BLUE_PIN GPIO8
35 #define LED_GREEN_PIN GPIO9
39 #define LED_BLUE_ON() BBIO_PERIPH(LED_PORT+ODR, BLUE) = 1
40 #define LED_BLUE_OFF() BBIO_PERIPH(LED_PORT+ODR, BLUE) = 0
41 #define LED_BLUE_TOGGLE() BBIO_PERIPH(LED_PORT+ODR, BLUE) = !BBIO_PERIPH(LED_PORT+ODR, BLUE)
43 #define LED_GREEN_ON() BBIO_PERIPH(LED_PORT+ODR, GREEN) = 1
44 #define LED_GREEN_OFF() BBIO_PERIPH(LED_PORT+ODR, GREEN) = 0
45 #define LED_GREEN_TOGGLE() BBIO_PERIPH(LED_PORT+ODR, GREEN) = !BBIO_PERIPH(LED_PORT+ODR, GREEN)
52 //BBIO_PERIPH(GPIOA+IDR, 0);
54 #define KEY_PORT GPIOA_IDR
59 #define REPEAT_MASK KEY0 // repeat: key0
60 #define REPEAT_START 100 // after 1s
61 #define REPEAT_NEXT 20 // every 200ms
65 NOTHING
, PULSE
, BLINK1
, BLINK2
70 uint8_t ontime
, offtime
;
73 volatile uint8_t led_timer
[2];
74 led_stat_t led_stat
[2];
76 volatile int timeout_1s
;
77 volatile uint32_t Stat
;
80 /*--------------------------------------------------------------------------*/
83 static void clock_setup(void)
85 //rcc_clock_setup_in_hse_8mhz_out_24mhz();
86 rcc_clock_setup_in_hsi_out_24mhz();
89 GPIO port A (for GPIO_USART1_TX and Button)
95 rcc_peripheral_enable_clock(&RCC_APB2ENR
,
96 RCC_APB2ENR_IOPAEN
| RCC_APB2ENR_IOPBEN
97 | RCC_APB2ENR_IOPCEN
| RCC_APB2ENR_IOPDEN
98 | RCC_APB2ENR_USART1EN
| RCC_APB2ENR_AFIOEN
99 | RCC_APB2ENR_TIM1EN
| RCC_APB2ENR_TIM16EN
);
100 /* Enable clocks for:
103 rcc_peripheral_enable_clock(&RCC_APB1ENR
,
106 /* Enable clocks for:
109 rcc_peripheral_enable_clock(&RCC_AHBENR
,
113 static void systick_setup(void)
115 /* SysTick interrupt every N clock pulses: set reload to N-1 */
116 STK_RVR
= 24000000/1000 - 1;
118 /* Set source to core clock, enable int and start counting. */
119 STK_CSR
= STK_CSR_CLKSOURCE_AHB
| STK_CSR_TICKINT
| STK_CSR_ENABLE
;
123 static void nvic_setup(void)
125 // nvic_enable_irq(NVIC_RTC_IRQ);
126 // nvic_set_priority(NVIC_RTC_IRQ, 1);
130 static void tim3_setup(void)
132 TIM3_CR1
= TIM_CR1_CMS_EDGE
| TIM_CR1_DIR_UP
;
135 | TIM_CCMR2_OC4M_FORCE_LOW
136 /* | TIM_CCMR2_OC4M_FORCE_HIGH */
137 /* | TIM_CCMR2_OC4M_PWM2 */
139 /* | TIM_CCMR2_OC4PE */
140 /* | TIM_CCMR2_OC4FE */
141 | TIM_CCMR2_CC4S_OUT
;
143 TIM3_CCER
= TIM_CCER_CC4E
146 TIM3_ARR
= 48; /* default */
150 static void gpio_setup(void)
153 /* Disable JTAG-DP, but leave SW-DP Enabled. (free PA15, PB3, PB4)
154 Remap SPI1 to PB3, PB4, PB5 and PA15.
155 Remap TIM3 (CH1/PC6, CH2/PC7, CH3/PC8, CH4/PC9)
156 Port D0/Port D1 mapping on OSC_IN/OSC_OUT
158 gpio_primary_remap(AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_ON
,
160 | AFIO_MAPR_TIM3_REMAP_FULL_REMAP
161 | AFIO_MAPR_PD01_REMAP
);
163 /* LEDs and User Button. */
164 gpio_set_mode(LED_PORT
, GPIO_MODE_OUTPUT_2_MHZ
,
165 GPIO_CNF_OUTPUT_PUSHPULL
, LED_BLUE_PIN
);
166 gpio_set_mode(LED_PORT
, GPIO_MODE_OUTPUT_10_MHZ
,
167 GPIO_CNF_OUTPUT_ALTFN_PUSHPULL
, LED_GREEN_PIN
);
168 gpio_set_mode(GPIOA
, GPIO_MODE_INPUT
,
169 GPIO_CNF_INPUT_FLOAT
, GPIO0
);
173 /*--------------------------------------------------------------------------*/
175 void delay_systicks(int ticks
)
177 int start
, stop
, now
;
180 stop
= start
- ticks
;
185 } while ((now
> stop
) || (now
<= start
));
189 } while ((now
> stop
) && (now
<= start
));
194 /*--------------------------------------------------------------------------*/
196 static void led_toggle(uint8_t lednr
) {
203 static void led_on(uint8_t lednr
) {
210 static void led_off(uint8_t lednr
) {
217 static uint8_t led_is_on(uint8_t lednr
) {
219 return BBIO_PERIPH(LED_PORT
+ODR
, BLUE
);
221 return BBIO_PERIPH(LED_PORT
+ODR
, GREEN
);
226 static void ledset(uint8_t lednr
, uint8_t what
, uint8_t len
) {
228 led_stat
[lednr
].mode
= what
;
231 led_stat
[lednr
].ontime
= len
;
232 led_stat
[lednr
].offtime
= 0;
233 led_timer
[lednr
] = len
;
239 led_stat
[lednr
].offtime
= 100 - len
;
241 led_stat
[lednr
].offtime
= 200 - len
;
242 led_stat
[lednr
].ontime
= len
;
243 led_timer
[lednr
] = len
;
251 /*--------------------------------------------------------------------------*/
253 static volatile uint16_t key_state
,
254 key_press
, // key press detect
255 key_rpt
; // key long press and repeat
258 static uint16_t get_key_press(uint16_t key_mask
) {
260 // read and clear atomic !
261 key_mask
&= key_press
; // read key(s)
262 key_press
^= key_mask
; // clear key(s)
268 static uint16_t get_key_rpt(uint16_t key_mask) {
270 // read and clear atomic !
271 key_mask &= key_rpt; // read key(s)
272 key_rpt ^= key_mask; // clear key(s)
278 static uint16_t get_key_short(uint16_t key_mask
) {
280 // read key state and key press atomic !
281 return get_key_press(key_state
& key_mask
);
285 static uint16_t get_key_long(uint16_t key_mask) {
286 return get_key_press(get_key_rpt(key_mask));
290 static void key_timerproc() {
291 static uint16_t key_in_last
, rpt
;
296 c
= key_in_last
& key_in
& ~key_state
;
298 // key_state = key_state & key_in_last | (key_state | key_in_last) & key_in;
299 // key_state = key_state & key_in | (key_state | key_in) & key_in_last;
301 key_state
= c
| ((key_in_last
| key_in
) & key_state
);
303 // key_state = (key_state&key_in_last) | (key_state&key_in) | (key_in_last&key_in);
307 key_in_last
= key_in
;
310 if ((key_state
& REPEAT_MASK
) == 0) // check repeat function
313 rpt
= REPEAT_NEXT
; // repeat delay
314 key_rpt
|= key_state
& REPEAT_MASK
;
319 /*--------------------------------------------------------------------------*/
321 void sys_tick_handler(void)
323 static int tick_10ms
= 0;
324 static int count_ms
= 0;
337 led_timer
[0] = i
- 1;
340 led_timer
[1] = i
- 1;
344 /* Drive timer procedure of low level disk I/O module */
349 if (count_ms
== 1000) {
360 /* The interrupt flag isn't cleared by hardware, we have to do it. */
361 rtc_clear_flag(RTC_SEC
);
365 /*--------------------------------------------------------------------------*/
367 void tim3_set(int mode
)
371 cc_mode
= TIM_CCMR2_CC4S_OUT
;
373 TIM3_CR1
= TIM_CR1_CMS_EDGE
| TIM_CR1_DIR_UP
/*| TIM_CR1_OPM */ ;
376 cc_mode
|= TIM_CCMR2_OC4M_FORCE_LOW
;
378 cc_mode
|= TIM_CCMR2_OC4M_FORCE_HIGH
;
382 cc_mode
|= TIM_CCMR2_OC4M_PWM2
;
385 TIM3_CCMR2
= cc_mode
;
388 TIM3_CR1
|= TIM_CR1_CEN
;
391 /*--------------------------------------------------------------------------*/
393 static uint32_t z80_sram_cmp(uint32_t addr
, int length
, uint8_t wval
, int inc
)
398 DBG_P(1, "SRAM: Check %#.5x byte... ", length
);
400 if ((rval
= z80_read(addr
)) != wval
) {
402 printf("\nSRAM: Address W R\n" \
406 printf(" %.5lx %.2x %.2x\n", addr
, wval
, rval
);
420 static void z80_sram_fill(uint32_t addr
, int length
, uint8_t startval
, int inc
)
422 printf("SRAM: Write %#.5x byte... ", length
); //fflush(stdout);
424 z80_write(addr
, startval
);
432 void z80_sram_fill_string(uint32_t addr
, int length
, const char *text
)
435 const char *p
= text
;
438 z80_write(addr
++, c
= *p
++);
445 uint32_t z80_sram_cmp_string(uint32_t addr
, int length
, const char *text
)
448 const char *p
= text
;
452 if (z80_read(addr
) != c
)
461 const char * const qbfox
= "Zhe quick brown fox jumps over the lazy dog!";
462 const char * const qbcat
= "Zhe quick brown fox jumps over the lazy cat!";
466 uint8_t z80_get_byte(uint32_t adr
)
471 data
= z80_read(adr
),
478 /*--------------------------------------------------------------------------*/
480 static void do_10ms(void)
482 for (uint_fast8_t i
= 0; i
< 2; i
++) {
483 switch (led_stat
[i
].mode
) {
485 if (led_timer
[i
] == 0) {
487 led_stat
[i
].mode
= NOTHING
;
492 if (led_timer
[i
] == 0) {
494 led_timer
[i
] = led_stat
[i
].offtime
;
496 led_timer
[i
] = led_stat
[i
].ontime
;
507 void do_msg_init(int len
, uint8_t* msg
)
513 switch (sub_fct
= *msg
++) {
518 uint32_t fifoadr
= 0;
520 fifoadr
= (fifoadr
<< 8) + msg
[len
];
522 z80_init_msg_fifo(fifoadr
);
524 z80_memfifo_init(sub_fct
- 1, fifoadr
);
526 /* garbage from z180 */
530 /* garbage from z180 */
537 void do_msg_char(int len
, uint8_t* msg
)
543 switch (sub_fct
= *msg
++) {
544 case 1: /* console output */
553 void do_message(int len
, uint8_t *msg
)
560 case 0: /* init functions */
561 do_msg_init(len
, msg
);
563 case 1: /* character i/o functions */
564 do_msg_char(len
, msg
);
567 /* no more functions definded yet*/
571 /* shoudn't happen */
576 #define CTRBUF_LEN 20
578 static void check_msg_fifo(void)
582 static int msglen
,idx
;
583 static uint8_t buffer
[CTRBUF_LEN
];
585 while (state
!= 3 && (ch
= z80_msg_fifo_getc()) >= 0) {
587 case 0: /* wait for start of message */
594 case 1: /* get msg len */
595 if (ch
> 0 && ch
<= CTRBUF_LEN
) {
601 case 2: /* get message */
610 do_message(msglen
, buffer
);
616 void z80_load_mem(void)
619 DBG_P(1, "Loading z80 memory... \n");
622 uint32_t sec_base
= hdrom_start
;
624 while (sec
< hdrom_sections
) {
625 DBG_P(2, " From: 0x%.5lX to: 0x%.5lX (%5li bytes)\n",
627 hdrom_address
[sec
]+hdrom_length_of_sections
[sec
] - 1,
628 hdrom_length_of_sections
[sec
]);
630 z80_write_block((unsigned char *) &hdrom
[sec_base
], /* src */
631 hdrom_address
[sec
], /* dest */
632 hdrom_length_of_sections
[sec
]); /* len */
633 sec_base
+=hdrom_length_of_sections
[sec
];
637 /*--------------------------------------------------------------------------*/
641 //uint32_t led_state = LED_BLUE_PIN;
643 //uint8_t startval = 0;
651 setvbuf(stdout
, NULL
, _IONBF
, 0);
653 printf("\n(STM32F100+HD64180)_stamp Tester\n");
655 DBG_P(1, "z80_setup_bus... ");
656 z80_setup_msg_fifo();
661 * If the RTC is pre-configured just allow access, don't reconfigure.
662 * Otherwise enable it with the LSE as clock source and 0x7fff as
665 rtc_auto_awake(LSE
, 0x7fff);
669 DBG_P(1, "Get bus... ");
673 DBG_P(1, "got it!\n");
675 z80_memset(0, 0x76, 0x80000);
676 //z80_sram_fill(0, 512 * 1024, 0x76, 0);
677 z80_sram_cmp(0, 512 * 1024, 0x76, 0);
681 DBG_P(1, "Bus released!\n");
684 DBG_P(1, "Reset released!\n");
687 ledset(0, BLINK1
, 50);
691 if (Stat
& S_10MS_TO
) {
696 if (get_key_short(KEY0
)) {
700 if ((ch
= serial_getc()) >= 0) {
705 /* TODO: Timer starten */
707 z80_memfifo_putc(fifo_out
, ch
);
712 case 'h': /* test: green led on */
715 case 'l': /* test: green led off */
718 case 'p': /* test: pulse on led pin */
719 tim3_set(24000000 / 1000000 * 5); /* 5 us */
727 z80_memfifo_putc(fifo_out
, ch
);