GPIO toggle test on NUCLEO F411RE DigitalOut 4.46MHz, 11210msec BSRRL/BSRRH register 20.8MHz, 2402msec Bit banding 15.6MHz, 3203msec
GPIO toggle test on NUCLEO F411RE
- DigitalOut 4.46MHz, 11210msec
- BSRRL/BSRRH register 20.8MHz, 2402msec
- Bit banding 15.6MHz, 3203msec
main.cpp@1:02a561139720, 2017-09-07 (annotated)
- Committer:
- peu605
- Date:
- Thu Sep 07 14:53:32 2017 +0000
- Revision:
- 1:02a561139720
- Parent:
- 0:ab159002ec37
change to BSRR
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
peu605 | 0:ab159002ec37 | 1 | #include "mbed.h" |
peu605 | 0:ab159002ec37 | 2 | |
peu605 | 0:ab159002ec37 | 3 | /** |
peu605 | 0:ab159002ec37 | 4 | * GPIO toggle test on ST Nucleo F411RE |
peu605 | 0:ab159002ec37 | 5 | * |
peu605 | 0:ab159002ec37 | 6 | * Resuts: |
peu605 | 0:ab159002ec37 | 7 | * 1. DigitalOut 4.46MHz, 11210msec |
peu605 | 0:ab159002ec37 | 8 | * 2. BSRRL/BSRRH register 20.8MHz, 2402msec |
peu605 | 0:ab159002ec37 | 9 | * 3. bit banding 15.6MHz, 3203msec |
peu605 | 0:ab159002ec37 | 10 | * |
peu605 | 0:ab159002ec37 | 11 | * @author masuda, Masuda Naika |
peu605 | 0:ab159002ec37 | 12 | */ |
peu605 | 0:ab159002ec37 | 13 | |
peu605 | 0:ab159002ec37 | 14 | #define BITBAND_PERIPH(addr, bit) \ |
peu605 | 0:ab159002ec37 | 15 | (volatile uint32_t*)(PERIPH_BB_BASE+((uint32_t)addr-PERIPH_BASE)*32+(bit*4)) |
peu605 | 0:ab159002ec37 | 16 | |
peu605 | 1:02a561139720 | 17 | #define LOOP_COUNT 10000000 |
peu605 | 0:ab159002ec37 | 18 | |
peu605 | 0:ab159002ec37 | 19 | PinName pinName = D10; |
peu605 | 0:ab159002ec37 | 20 | DigitalOut dout(pinName, 0); |
peu605 | 0:ab159002ec37 | 21 | Timer t; |
peu605 | 0:ab159002ec37 | 22 | Serial pc(SERIAL_TX, SERIAL_RX); |
peu605 | 0:ab159002ec37 | 23 | |
peu605 | 0:ab159002ec37 | 24 | GPIO_TypeDef *gpio_port_reg; |
peu605 | 1:02a561139720 | 25 | volatile uint32_t gpio_reg_set_mask; |
peu605 | 1:02a561139720 | 26 | volatile uint32_t gpio_reg_reset_mask; |
peu605 | 0:ab159002ec37 | 27 | volatile uint32_t *bb_gpio_port; |
peu605 | 0:ab159002ec37 | 28 | |
peu605 | 0:ab159002ec37 | 29 | void digitalOutTest(); |
peu605 | 0:ab159002ec37 | 30 | void bsrrTest(); |
peu605 | 0:ab159002ec37 | 31 | void bitbandTest(); |
peu605 | 0:ab159002ec37 | 32 | |
peu605 | 0:ab159002ec37 | 33 | |
peu605 | 0:ab159002ec37 | 34 | int main() { |
peu605 | 0:ab159002ec37 | 35 | |
peu605 | 0:ab159002ec37 | 36 | //set gpio port addresses and masks |
peu605 | 0:ab159002ec37 | 37 | uint32_t port_index = (uint32_t) pinName >> 4; |
peu605 | 0:ab159002ec37 | 38 | gpio_port_reg = (GPIO_TypeDef *) (GPIOA_BASE + (port_index << 10)); |
peu605 | 1:02a561139720 | 39 | gpio_reg_set_mask = 1 << ((uint32_t) pinName & 0xf); |
peu605 | 1:02a561139720 | 40 | gpio_reg_reset_mask = gpio_reg_set_mask << 16; |
peu605 | 0:ab159002ec37 | 41 | |
peu605 | 0:ab159002ec37 | 42 | // set bit band addresses |
peu605 | 0:ab159002ec37 | 43 | uint8_t port_bit = (uint32_t) pinName & 0xf; |
peu605 | 0:ab159002ec37 | 44 | bb_gpio_port = BITBAND_PERIPH(&gpio_port_reg->ODR, port_bit); |
peu605 | 0:ab159002ec37 | 45 | |
peu605 | 0:ab159002ec37 | 46 | dout = 0; |
peu605 | 0:ab159002ec37 | 47 | wait_ms(2000); |
peu605 | 0:ab159002ec37 | 48 | t.start(); |
peu605 | 0:ab159002ec37 | 49 | |
peu605 | 0:ab159002ec37 | 50 | while(true) { |
peu605 | 0:ab159002ec37 | 51 | digitalOutTest(); |
peu605 | 0:ab159002ec37 | 52 | bsrrTest(); |
peu605 | 0:ab159002ec37 | 53 | bitbandTest(); |
peu605 | 0:ab159002ec37 | 54 | } |
peu605 | 0:ab159002ec37 | 55 | } |
peu605 | 0:ab159002ec37 | 56 | |
peu605 | 0:ab159002ec37 | 57 | void digitalOutTest() { |
peu605 | 0:ab159002ec37 | 58 | |
peu605 | 0:ab159002ec37 | 59 | uint32_t l = LOOP_COUNT; |
peu605 | 0:ab159002ec37 | 60 | int t1 = t.read_ms(); |
peu605 | 0:ab159002ec37 | 61 | |
peu605 | 1:02a561139720 | 62 | while(l--) { |
peu605 | 0:ab159002ec37 | 63 | dout = 1; |
peu605 | 0:ab159002ec37 | 64 | dout = 0; |
peu605 | 0:ab159002ec37 | 65 | dout = 1; |
peu605 | 0:ab159002ec37 | 66 | dout = 0; |
peu605 | 0:ab159002ec37 | 67 | dout = 1; |
peu605 | 0:ab159002ec37 | 68 | dout = 0; |
peu605 | 0:ab159002ec37 | 69 | dout = 1; |
peu605 | 0:ab159002ec37 | 70 | dout = 0; |
peu605 | 0:ab159002ec37 | 71 | dout = 1; |
peu605 | 0:ab159002ec37 | 72 | dout = 0; |
peu605 | 0:ab159002ec37 | 73 | } |
peu605 | 0:ab159002ec37 | 74 | |
peu605 | 0:ab159002ec37 | 75 | int t2 = t.read_ms(); |
peu605 | 0:ab159002ec37 | 76 | pc.printf("DigitalOut: %d msec\n", t2 - t1); |
peu605 | 0:ab159002ec37 | 77 | wait_ms(1000); |
peu605 | 0:ab159002ec37 | 78 | } |
peu605 | 0:ab159002ec37 | 79 | |
peu605 | 0:ab159002ec37 | 80 | void bsrrTest() { |
peu605 | 0:ab159002ec37 | 81 | |
peu605 | 0:ab159002ec37 | 82 | uint32_t l = LOOP_COUNT; |
peu605 | 0:ab159002ec37 | 83 | int t1 = t.read_ms(); |
peu605 | 0:ab159002ec37 | 84 | |
peu605 | 1:02a561139720 | 85 | // __asm("ALIGN 4"); |
peu605 | 1:02a561139720 | 86 | while(l--) { |
peu605 | 1:02a561139720 | 87 | gpio_port_reg->BSRR = gpio_reg_set_mask; |
peu605 | 1:02a561139720 | 88 | gpio_port_reg->BSRR = gpio_reg_reset_mask; |
peu605 | 1:02a561139720 | 89 | gpio_port_reg->BSRR = gpio_reg_set_mask; |
peu605 | 1:02a561139720 | 90 | gpio_port_reg->BSRR = gpio_reg_reset_mask; |
peu605 | 1:02a561139720 | 91 | gpio_port_reg->BSRR = gpio_reg_set_mask; |
peu605 | 1:02a561139720 | 92 | gpio_port_reg->BSRR = gpio_reg_reset_mask; |
peu605 | 1:02a561139720 | 93 | gpio_port_reg->BSRR = gpio_reg_set_mask; |
peu605 | 1:02a561139720 | 94 | gpio_port_reg->BSRR = gpio_reg_reset_mask; |
peu605 | 1:02a561139720 | 95 | gpio_port_reg->BSRR = gpio_reg_set_mask; |
peu605 | 1:02a561139720 | 96 | gpio_port_reg->BSRR = gpio_reg_reset_mask; |
peu605 | 0:ab159002ec37 | 97 | } |
peu605 | 0:ab159002ec37 | 98 | |
peu605 | 0:ab159002ec37 | 99 | int t2 = t.read_ms(); |
peu605 | 0:ab159002ec37 | 100 | pc.printf("BSRR: %d msec\n", t2 - t1); |
peu605 | 0:ab159002ec37 | 101 | wait_ms(1000); |
peu605 | 0:ab159002ec37 | 102 | } |
peu605 | 0:ab159002ec37 | 103 | |
peu605 | 0:ab159002ec37 | 104 | void bitbandTest() { |
peu605 | 0:ab159002ec37 | 105 | |
peu605 | 0:ab159002ec37 | 106 | uint32_t l = LOOP_COUNT; |
peu605 | 0:ab159002ec37 | 107 | int t1 = t.read_ms(); |
peu605 | 0:ab159002ec37 | 108 | |
peu605 | 1:02a561139720 | 109 | // __asm(".align 4"); |
peu605 | 1:02a561139720 | 110 | while(l--) { |
peu605 | 0:ab159002ec37 | 111 | *bb_gpio_port = 1; |
peu605 | 0:ab159002ec37 | 112 | *bb_gpio_port = 0; |
peu605 | 0:ab159002ec37 | 113 | *bb_gpio_port = 1; |
peu605 | 0:ab159002ec37 | 114 | *bb_gpio_port = 0; |
peu605 | 0:ab159002ec37 | 115 | *bb_gpio_port = 1; |
peu605 | 0:ab159002ec37 | 116 | *bb_gpio_port = 0; |
peu605 | 0:ab159002ec37 | 117 | *bb_gpio_port = 1; |
peu605 | 0:ab159002ec37 | 118 | *bb_gpio_port = 0; |
peu605 | 0:ab159002ec37 | 119 | *bb_gpio_port = 1; |
peu605 | 0:ab159002ec37 | 120 | *bb_gpio_port = 0; |
peu605 | 0:ab159002ec37 | 121 | } |
peu605 | 0:ab159002ec37 | 122 | |
peu605 | 0:ab159002ec37 | 123 | int t2 = t.read_ms(); |
peu605 | 0:ab159002ec37 | 124 | pc.printf("Bit band: %d msec\n", t2 - t1); |
peu605 | 0:ab159002ec37 | 125 | wait_ms(1000); |
peu605 | 0:ab159002ec37 | 126 | } |