From 9d5526f7737831a6a7ce720be4d3a19996e7ea9c Mon Sep 17 00:00:00 2001 From: Piotr Esden-Tempski Date: Fri, 19 Apr 2013 17:19:32 -0700 Subject: [PATCH] Initial commit. --- .gitmodules | 3 + Makefile | 59 ++ README | 45 ++ examples/lm3s/Makefile.include | 137 +++++ examples/lm3s/lm3s3748-evb/lm3s3748-evb.ld | 31 ++ examples/lm3s/lm3s3748-evb/miniblink/Makefile | 25 + examples/lm3s/lm3s3748-evb/miniblink/README | 9 + .../lm3s/lm3s3748-evb/miniblink/miniblink.c | 51 ++ examples/lm4f/Makefile.include | 143 +++++ .../stellaris-ek-lm4f120xl/ek-lm4f120xl.ld | 31 ++ .../stellaris-ek-lm4f120xl/miniblink/Makefile | 25 + .../stellaris-ek-lm4f120xl/miniblink/README | 22 + .../miniblink/miniblink.c | 217 ++++++++ examples/lpc13xx/Makefile.include | 137 +++++ examples/lpc13xx/lpc-p1343/lpc-p1343.ld | 31 ++ examples/lpc13xx/lpc-p1343/miniblink/Makefile | 25 + examples/lpc13xx/lpc-p1343/miniblink/README | 10 + .../lpc13xx/lpc-p1343/miniblink/miniblink.c | 46 ++ examples/lpc17xx/Makefile.include | 137 +++++ .../blueboard-lpc1768-h.ld | 32 ++ .../blueboard-lpc1768-h/miniblink/Makefile | 24 + .../blueboard-lpc1768-h/miniblink/README | 9 + .../blueboard-lpc1768-h/miniblink/miniblink.c | 52 ++ examples/lpc43xx/Makefile.include | 140 +++++ examples/lpc43xx/diolan-lpc-4350-db1/README | 3 + .../diolan-lpc-4350-db1.ld | 32 ++ .../diolan-lpc-4350-db1/miniblink/Makefile | 24 + .../diolan-lpc-4350-db1/miniblink/README | 11 + .../diolan-lpc-4350-db1/miniblink/miniblink.c | 46 ++ examples/lpc43xx/hackrf-jellybean/README | 4 + .../lpc43xx/hackrf-jellybean/i2c/Makefile | 24 + examples/lpc43xx/hackrf-jellybean/i2c/README | 15 + .../lpc43xx/hackrf-jellybean/i2c/i2cdemo.c | 108 ++++ .../hackrf-jellybean/jellybean-lpc4330.ld | 32 ++ .../jellybean-lpc4330_rom_to_ram.ld | 36 ++ .../lpc43xx/hackrf-jellybean/jellybean_conf.h | 85 +++ .../hackrf-jellybean/miniblink/Makefile | 24 + .../lpc43xx/hackrf-jellybean/miniblink/README | 11 + .../hackrf-jellybean/miniblink/miniblink.c | 82 +++ .../miniblink_rom_to_ram/Makefile | 24 + .../miniblink_rom_to_ram/README | 12 + .../miniblink_rom_to_ram/miniblink.c | 82 +++ .../lpc43xx/hackrf-jellybean/ssp/Makefile | 24 + examples/lpc43xx/hackrf-jellybean/ssp/README | 48 ++ .../lpc43xx/hackrf-jellybean/ssp/sspdemo.c | 102 ++++ .../lpc43xx/hackrf-jellybean/systick/Makefile | 24 + .../lpc43xx/hackrf-jellybean/systick/README | 8 + .../hackrf-jellybean/systick/systickdemo.c | 184 +++++++ examples/stm32/f1/Makefile.include | 172 ++++++ examples/stm32/f1/lisa-m-1/can/Makefile | 25 + examples/stm32/f1/lisa-m-1/can/README | 4 + examples/stm32/f1/lisa-m-1/can/can.c | 237 ++++++++ examples/stm32/f1/lisa-m-1/can/can.c~ | 218 ++++++++ .../stm32/f1/lisa-m-1/fancyblink/Makefile | 25 + .../stm32/f1/lisa-m-1/fancyblink/fancyblink.c | 68 +++ examples/stm32/f1/lisa-m-1/lisa-m.ld | 32 ++ .../stm32/f1/lisa-m-1/usb_cdcacm/Makefile | 25 + examples/stm32/f1/lisa-m-1/usb_cdcacm/README | 7 + .../stm32/f1/lisa-m-1/usb_cdcacm/cdcacm.c | 261 +++++++++ .../f1/lisa-m-1/usb_cdcacm/cdcacm_test.py | 56 ++ examples/stm32/f1/lisa-m-1/usb_dfu/Makefile | 25 + examples/stm32/f1/lisa-m-1/usb_dfu/README | 7 + examples/stm32/f1/lisa-m-1/usb_dfu/usbdfu.c | 272 ++++++++++ examples/stm32/f1/lisa-m-1/usb_hid/Makefile | 25 + examples/stm32/f1/lisa-m-1/usb_hid/README | 7 + examples/stm32/f1/lisa-m-1/usb_hid/adxl345.h | 38 ++ examples/stm32/f1/lisa-m-1/usb_hid/usbhid.c | 369 +++++++++++++ examples/stm32/f1/lisa-m-2/adc_injec/Makefile | 28 + examples/stm32/f1/lisa-m-2/adc_injec/README | 11 + .../stm32/f1/lisa-m-2/adc_injec/adc_injec.c | 174 ++++++ .../f1/lisa-m-2/adc_injec_timtrig/Makefile | 28 + .../f1/lisa-m-2/adc_injec_timtrig/README | 11 + .../adc_injec_timtrig/adc_injec_timtrig.c | 195 +++++++ .../lisa-m-2/adc_injec_timtrig_irq/Makefile | 28 + .../f1/lisa-m-2/adc_injec_timtrig_irq/README | 12 + .../adc_injec_timtrig_irq.c | 211 ++++++++ .../adc_injec_timtrig_irq_4ch/Makefile | 28 + .../lisa-m-2/adc_injec_timtrig_irq_4ch/README | 12 + .../adc_injec_timtrig_irq_4ch.c | 230 ++++++++ .../stm32/f1/lisa-m-2/adc_regular/Makefile | 28 + examples/stm32/f1/lisa-m-2/adc_regular/README | 9 + examples/stm32/f1/lisa-m-2/adc_regular/adc.c | 161 ++++++ examples/stm32/f1/lisa-m-2/can/Makefile | 28 + examples/stm32/f1/lisa-m-2/can/README | 4 + examples/stm32/f1/lisa-m-2/can/can.c | 237 ++++++++ examples/stm32/f1/lisa-m-2/can/can.c~ | 232 ++++++++ .../stm32/f1/lisa-m-2/fancyblink/Makefile | 28 + .../stm32/f1/lisa-m-2/fancyblink/fancyblink.c | 148 +++++ examples/stm32/f1/lisa-m-2/lisa-m.ld | 31 ++ examples/stm32/f1/lisa-m-2/spi/Makefile | 28 + examples/stm32/f1/lisa-m-2/spi/README | 14 + examples/stm32/f1/lisa-m-2/spi/spi.c | 164 ++++++ examples/stm32/f1/lisa-m-2/spi_dma/Makefile | 28 + examples/stm32/f1/lisa-m-2/spi_dma/README | 17 + examples/stm32/f1/lisa-m-2/spi_dma/spi_dma.c | 439 +++++++++++++++ .../stm32/f1/lisa-m-2/spi_dma_adv/Makefile | 28 + examples/stm32/f1/lisa-m-2/spi_dma_adv/README | 17 + .../f1/lisa-m-2/spi_dma_adv/spi_dma_adv.c | 506 ++++++++++++++++++ examples/stm32/f1/lisa-m-2/usart/Makefile | 28 + examples/stm32/f1/lisa-m-2/usart/README | 12 + examples/stm32/f1/lisa-m-2/usart/usart.c | 86 +++ examples/stm32/f1/lisa-m-2/usart_dma/Makefile | 28 + .../stm32/f1/lisa-m-2/usart_dma/usart_dma.c | 199 +++++++ .../stm32/f1/lisa-m-2/usart_dma/usart_dma.c~ | 86 +++ examples/stm32/f1/lisa-m-2/usart_irq/Makefile | 28 + .../stm32/f1/lisa-m-2/usart_irq/usart_irq.c | 126 +++++ .../f1/lisa-m-2/usart_irq_printf/Makefile | 28 + .../usart_irq_printf/usart_irq_printf.c | 274 ++++++++++ .../stm32/f1/lisa-m-2/usart_printf/Makefile | 28 + .../f1/lisa-m-2/usart_printf/usart_printf.c | 108 ++++ examples/stm32/f1/mb525/fancyblink/Makefile | 25 + examples/stm32/f1/mb525/fancyblink/README | 10 + .../stm32/f1/mb525/fancyblink/fancyblink.c | 76 +++ examples/stm32/f1/mb525/mb525.ld | 32 ++ examples/stm32/f1/mb525/pwmleds/Makefile | 25 + examples/stm32/f1/mb525/pwmleds/README | 9 + examples/stm32/f1/mb525/pwmleds/pwmleds.c | 497 +++++++++++++++++ examples/stm32/f1/obldc-strip/can/Makefile | 25 + examples/stm32/f1/obldc-strip/can/README | 4 + examples/stm32/f1/obldc-strip/can/can.c | 205 +++++++ examples/stm32/f1/obldc-strip/can/can.c~ | 232 ++++++++ examples/stm32/f1/obldc-strip/led/Makefile | 25 + examples/stm32/f1/obldc-strip/led/led.c | 65 +++ examples/stm32/f1/obldc-strip/obldc-strip.ld | 31 ++ .../stm32/f1/obldc-strip/systick/Makefile | 25 + .../stm32/f1/obldc-strip/systick/systick.c | 82 +++ examples/stm32/f1/obldc-strip/usart/Makefile | 25 + examples/stm32/f1/obldc-strip/usart/usart.c | 87 +++ examples/stm32/f1/obldc/can/Makefile | 25 + examples/stm32/f1/obldc/can/can.c | 219 ++++++++ examples/stm32/f1/obldc/can/can.c~ | 232 ++++++++ examples/stm32/f1/obldc/led/Makefile | 25 + examples/stm32/f1/obldc/led/led.c | 71 +++ examples/stm32/f1/obldc/obldc.ld | 31 ++ examples/stm32/f1/obldc/pwmleds/Makefile | 25 + examples/stm32/f1/obldc/pwmleds/pwmleds.c | 484 +++++++++++++++++ examples/stm32/f1/obldc/systick/Makefile | 25 + examples/stm32/f1/obldc/systick/systick.c | 91 ++++ examples/stm32/f1/obldc/usart/Makefile | 25 + examples/stm32/f1/obldc/usart/usart.c | 90 ++++ examples/stm32/f1/obldc/usart_irq/Makefile | 27 + examples/stm32/f1/obldc/usart_irq/usart_irq.c | 120 +++++ .../f1/other/adc_temperature_sensor/Makefile | 23 + .../f1/other/adc_temperature_sensor/README | 10 + .../f1/other/adc_temperature_sensor/adc.c | 153 ++++++ .../f1/other/adc_temperature_sensor/adc.ld | 31 ++ examples/stm32/f1/other/dma_mem2mem/Makefile | 23 + examples/stm32/f1/other/dma_mem2mem/README | 11 + examples/stm32/f1/other/dma_mem2mem/dma.c | 140 +++++ examples/stm32/f1/other/dma_mem2mem/dma.ld | 31 ++ examples/stm32/f1/other/dogm128/Makefile | 25 + examples/stm32/f1/other/dogm128/README | 7 + examples/stm32/f1/other/dogm128/dogm128.c | 302 +++++++++++ examples/stm32/f1/other/dogm128/dogm128.h | 88 +++ examples/stm32/f1/other/dogm128/main.c | 113 ++++ examples/stm32/f1/other/dogm128/main.ld | 31 ++ .../stm32/f1/other/i2c_stts75_sensor/Makefile | 25 + .../stm32/f1/other/i2c_stts75_sensor/README | 13 + .../i2c_stts75_sensor/i2c_stts75_sensor.c | 146 +++++ .../i2c_stts75_sensor/i2c_stts75_sensor.ld | 31 ++ .../stm32/f1/other/i2c_stts75_sensor/stts75.c | 189 +++++++ .../stm32/f1/other/i2c_stts75_sensor/stts75.h | 39 ++ examples/stm32/f1/other/rtc/Makefile | 23 + examples/stm32/f1/other/rtc/README | 6 + examples/stm32/f1/other/rtc/rtc.c | 116 ++++ examples/stm32/f1/other/rtc/rtc.ld | 31 ++ examples/stm32/f1/other/systick/Makefile | 23 + examples/stm32/f1/other/systick/README | 7 + examples/stm32/f1/other/systick/systick.c | 76 +++ examples/stm32/f1/other/systick/systick.ld | 31 ++ .../stm32/f1/other/timer_interrupt/Makefile | 23 + .../stm32/f1/other/timer_interrupt/README | 7 + .../stm32/f1/other/timer_interrupt/timer.c | 85 +++ .../stm32/f1/other/timer_interrupt/timer.ld | 31 ++ examples/stm32/f1/other/usb_cdcacm/Makefile | 23 + examples/stm32/f1/other/usb_cdcacm/README | 7 + examples/stm32/f1/other/usb_cdcacm/cdcacm.c | 253 +++++++++ examples/stm32/f1/other/usb_cdcacm/cdcacm.ld | 29 + examples/stm32/f1/other/usb_dfu/Makefile | 23 + examples/stm32/f1/other/usb_dfu/README | 7 + examples/stm32/f1/other/usb_dfu/usbdfu.c | 275 ++++++++++ examples/stm32/f1/other/usb_dfu/usbdfu.ld | 29 + examples/stm32/f1/other/usb_hid/Makefile | 23 + examples/stm32/f1/other/usb_hid/README | 7 + examples/stm32/f1/other/usb_hid/usbhid.c | 283 ++++++++++ examples/stm32/f1/other/usb_hid/usbhid.ld | 29 + examples/stm32/f1/stm32-h103/button/Makefile | 25 + examples/stm32/f1/stm32-h103/button/button.c | 76 +++ .../stm32/f1/stm32-h103/exti_both/Makefile | 25 + .../stm32/f1/stm32-h103/exti_both/exti_both.c | 88 +++ .../stm32-h103/exti_rising_falling/Makefile | 25 + .../exti_rising_falling/exti_rising_falling.c | 93 ++++ .../stm32/f1/stm32-h103/fancyblink/Makefile | 25 + .../stm32/f1/stm32-h103/fancyblink/README | 10 + .../f1/stm32-h103/fancyblink/fancyblink.c | 54 ++ .../stm32/f1/stm32-h103/led_stripe/Makefile | 25 + .../f1/stm32-h103/led_stripe/led_stripe.c | 236 ++++++++ .../stm32/f1/stm32-h103/miniblink/Makefile | 25 + examples/stm32/f1/stm32-h103/miniblink/README | 10 + .../stm32/f1/stm32-h103/miniblink/miniblink.c | 71 +++ .../stm32/f1/stm32-h103/pwm_6step/Makefile | 25 + .../stm32/f1/stm32-h103/pwm_6step/pwm_6step.c | 400 ++++++++++++++ examples/stm32/f1/stm32-h103/spi/Makefile | 25 + examples/stm32/f1/stm32-h103/spi/README | 8 + examples/stm32/f1/stm32-h103/spi/spi.c | 50 ++ examples/stm32/f1/stm32-h103/stm32-h103.ld | 31 ++ examples/stm32/f1/stm32-h103/timer/Makefile | 25 + examples/stm32/f1/stm32-h103/timer/timer.c | 167 ++++++ .../stm32/f1/stm32-h103/traceswo/Makefile | 25 + examples/stm32/f1/stm32-h103/traceswo/README | 10 + .../stm32/f1/stm32-h103/traceswo/traceswo.c | 101 ++++ examples/stm32/f1/stm32-h103/usart/Makefile | 25 + examples/stm32/f1/stm32-h103/usart/README | 13 + examples/stm32/f1/stm32-h103/usart/usart.c | 123 +++++ .../stm32/f1/stm32-h103/usart_irq/Makefile | 25 + .../stm32/f1/stm32-h103/usart_irq/usart_irq.c | 118 ++++ .../f1/stm32-h103/usart_irq_printf/Makefile | 25 + .../usart_irq_printf/usart_irq_printf.c | 273 ++++++++++ .../stm32/f1/stm32-h103/usart_printf/Makefile | 25 + .../f1/stm32-h103/usart_printf/usart_printf.c | 107 ++++ .../stm32/f1/stm32-h103/usb_cdcacm/Makefile | 25 + .../stm32/f1/stm32-h103/usb_cdcacm/README | 7 + .../stm32/f1/stm32-h103/usb_cdcacm/cdcacm.c | 255 +++++++++ examples/stm32/f1/stm32-h103/usb_dfu/Makefile | 25 + examples/stm32/f1/stm32-h103/usb_dfu/README | 7 + examples/stm32/f1/stm32-h103/usb_dfu/usbdfu.c | 271 ++++++++++ examples/stm32/f1/stm32-h103/usb_hid/Makefile | 25 + examples/stm32/f1/stm32-h103/usb_hid/README | 7 + examples/stm32/f1/stm32-h103/usb_hid/usbhid.c | 287 ++++++++++ examples/stm32/f1/stm32-h103/usb_iap/Makefile | 25 + examples/stm32/f1/stm32-h103/usb_iap/README | 8 + examples/stm32/f1/stm32-h103/usb_iap/usbiap.c | 273 ++++++++++ .../stm32/f1/stm32-h107/fancyblink/Makefile | 25 + .../stm32/f1/stm32-h107/fancyblink/README | 10 + .../f1/stm32-h107/fancyblink/fancyblink.c | 58 ++ examples/stm32/f1/stm32-h107/stm32-h107.ld | 31 ++ .../stm32/f1/stm32-h107/usb_simple/Makefile | 25 + .../stm32/f1/stm32-h107/usb_simple/README | 10 + .../f1/stm32-h107/usb_simple/usb_simple.c | 120 +++++ .../stm32/f1/stm32-h107/usb_simple/usbtest.py | 20 + .../stm32/f1/stm32-maple/miniblink/Makefile | 25 + .../stm32/f1/stm32-maple/miniblink/README | 9 + .../f1/stm32-maple/miniblink/miniblink.c | 71 +++ examples/stm32/f1/stm32-maple/stm32-maple.ld | 32 ++ examples/stm32/f1/stm32-maple/usart/Makefile | 25 + examples/stm32/f1/stm32-maple/usart/README | 13 + examples/stm32/f1/stm32-maple/usart/usart.c | 82 +++ .../stm32/f1/stm32-maple/usart_irq/Makefile | 25 + .../f1/stm32-maple/usart_irq/usart_irq.c | 120 +++++ .../stm32/f1/stm32-maple/usb_cdcacm/Makefile | 25 + .../stm32/f1/stm32-maple/usb_cdcacm/README | 7 + .../stm32/f1/stm32-maple/usb_cdcacm/cdcacm.c | 266 +++++++++ .../stm32vl-discovery/adc-dac-printf/Makefile | 24 + .../stm32vl-discovery/adc-dac-printf/README | 17 + .../adc-dac-printf/adc-dac-printf.c | 164 ++++++ .../f1/stm32vl-discovery/button/Makefile | 25 + .../stm32/f1/stm32vl-discovery/button/README | 8 + .../f1/stm32vl-discovery/button/button.c | 75 +++ .../f1/stm32vl-discovery/fancyblink/Makefile | 25 + .../f1/stm32vl-discovery/fancyblink/README | 9 + .../stm32vl-discovery/fancyblink/fancyblink.c | 61 +++ .../f1/stm32vl-discovery/miniblink/Makefile | 25 + .../f1/stm32vl-discovery/miniblink/README | 9 + .../stm32vl-discovery/miniblink/miniblink.c | 71 +++ .../stm32/f1/stm32vl-discovery/rtc/Makefile | 25 + .../stm32/f1/stm32vl-discovery/rtc/README | 9 + examples/stm32/f1/stm32vl-discovery/rtc/rtc.c | 129 +++++ .../f1/stm32vl-discovery/stm32vl-discovery.ld | 31 ++ .../stm32/f1/stm32vl-discovery/usart/Makefile | 25 + .../stm32/f1/stm32vl-discovery/usart/README | 12 + .../stm32/f1/stm32vl-discovery/usart/usart.c | 87 +++ examples/stm32/f2/Makefile.include | 151 ++++++ examples/stm32/f2/jobygps/jobygps.cfg | 3 + examples/stm32/f2/jobygps/jobygps.ld | 32 ++ examples/stm32/f2/jobygps/miniblink/Makefile | 25 + examples/stm32/f2/jobygps/miniblink/README | 6 + .../stm32/f2/jobygps/miniblink/miniblink.c | 52 ++ examples/stm32/f2/jobygps/spi_test/Makefile | 25 + examples/stm32/f2/jobygps/spi_test/spi_test.c | 115 ++++ .../stm32/f2/jobygps/usart_printf/Makefile | 25 + .../f2/jobygps/usart_printf/usart_printf.c | 99 ++++ examples/stm32/f4/Makefile.include | 158 ++++++ .../f4/stm32f4-discovery/button/Makefile | 25 + .../stm32/f4/stm32f4-discovery/button/README | 8 + .../f4/stm32f4-discovery/button/button.c | 76 +++ .../f4/stm32f4-discovery/fancyblink/Makefile | 25 + .../f4/stm32f4-discovery/fancyblink/README | 9 + .../stm32f4-discovery/fancyblink/fancyblink.c | 60 +++ .../f4/stm32f4-discovery/mandelbrot/Makefile | 25 + .../f4/stm32f4-discovery/mandelbrot/README | 12 + .../f4/stm32f4-discovery/mandelbrot/mandel.c | 127 +++++ .../f4/stm32f4-discovery/miniblink/Makefile | 25 + .../f4/stm32f4-discovery/miniblink/README | 9 + .../stm32f4-discovery/miniblink/miniblink.c | 71 +++ .../f4/stm32f4-discovery/random/Makefile | 25 + .../stm32/f4/stm32f4-discovery/random/README | 5 + .../f4/stm32f4-discovery/random/random.c | 88 +++ .../f4/stm32f4-discovery/stm32f4-discovery.ld | 32 ++ .../stm32/f4/stm32f4-discovery/usart/Makefile | 25 + .../stm32/f4/stm32f4-discovery/usart/README | 11 + .../stm32/f4/stm32f4-discovery/usart/usart.c | 83 +++ .../f4/stm32f4-discovery/usart_irq/Makefile | 25 + .../f4/stm32f4-discovery/usart_irq/README | 14 + .../stm32f4-discovery/usart_irq/usart_irq.c | 115 ++++ .../stm32f4-discovery/usart_irq/usart_irq.c~ | 93 ++++ .../f4/stm32f4-discovery/usb_cdcacm/Makefile | 25 + .../f4/stm32f4-discovery/usb_cdcacm/README | 7 + .../f4/stm32f4-discovery/usb_cdcacm/cdcacm.c | 240 +++++++++ examples/stm32/l1/Makefile.include | 168 ++++++ .../button-irq-printf-lowpower/Makefile | 24 + .../button-irq-printf-lowpower/README | 18 + .../button-irq-printf-lowpower/main.c | 302 +++++++++++ .../button-irq-printf-lowpower/syscfg.h | 61 +++ .../button-irq-printf/Makefile | 24 + .../stm32l-discovery/button-irq-printf/README | 35 ++ .../stm32l-discovery/button-irq-printf/main.c | 189 +++++++ .../button-irq-printf/syscfg.h | 58 ++ .../l1/stm32l-discovery/miniblink/Makefile | 25 + .../l1/stm32l-discovery/miniblink/README | 9 + .../l1/stm32l-discovery/miniblink/miniblink.c | 72 +++ .../stm32/l1/stm32l-discovery/usart/Makefile | 25 + .../stm32/l1/stm32l-discovery/usart/README | 12 + .../stm32/l1/stm32l-discovery/usart/usart.c | 84 +++ libopencm3 | 1 + 324 files changed, 22845 insertions(+) create mode 100644 .gitmodules create mode 100644 Makefile create mode 100644 README create mode 100644 examples/lm3s/Makefile.include create mode 100644 examples/lm3s/lm3s3748-evb/lm3s3748-evb.ld create mode 100644 examples/lm3s/lm3s3748-evb/miniblink/Makefile create mode 100644 examples/lm3s/lm3s3748-evb/miniblink/README create mode 100644 examples/lm3s/lm3s3748-evb/miniblink/miniblink.c create mode 100644 examples/lm4f/Makefile.include create mode 100644 examples/lm4f/stellaris-ek-lm4f120xl/ek-lm4f120xl.ld create mode 100644 examples/lm4f/stellaris-ek-lm4f120xl/miniblink/Makefile create mode 100644 examples/lm4f/stellaris-ek-lm4f120xl/miniblink/README create mode 100644 examples/lm4f/stellaris-ek-lm4f120xl/miniblink/miniblink.c create mode 100644 examples/lpc13xx/Makefile.include create mode 100644 examples/lpc13xx/lpc-p1343/lpc-p1343.ld create mode 100644 examples/lpc13xx/lpc-p1343/miniblink/Makefile create mode 100644 examples/lpc13xx/lpc-p1343/miniblink/README create mode 100644 examples/lpc13xx/lpc-p1343/miniblink/miniblink.c create mode 100644 examples/lpc17xx/Makefile.include create mode 100644 examples/lpc17xx/blueboard-lpc1768-h/blueboard-lpc1768-h.ld create mode 100644 examples/lpc17xx/blueboard-lpc1768-h/miniblink/Makefile create mode 100644 examples/lpc17xx/blueboard-lpc1768-h/miniblink/README create mode 100644 examples/lpc17xx/blueboard-lpc1768-h/miniblink/miniblink.c create mode 100644 examples/lpc43xx/Makefile.include create mode 100644 examples/lpc43xx/diolan-lpc-4350-db1/README create mode 100644 examples/lpc43xx/diolan-lpc-4350-db1/diolan-lpc-4350-db1.ld create mode 100644 examples/lpc43xx/diolan-lpc-4350-db1/miniblink/Makefile create mode 100644 examples/lpc43xx/diolan-lpc-4350-db1/miniblink/README create mode 100644 examples/lpc43xx/diolan-lpc-4350-db1/miniblink/miniblink.c create mode 100644 examples/lpc43xx/hackrf-jellybean/README create mode 100644 examples/lpc43xx/hackrf-jellybean/i2c/Makefile create mode 100644 examples/lpc43xx/hackrf-jellybean/i2c/README create mode 100644 examples/lpc43xx/hackrf-jellybean/i2c/i2cdemo.c create mode 100644 examples/lpc43xx/hackrf-jellybean/jellybean-lpc4330.ld create mode 100644 examples/lpc43xx/hackrf-jellybean/jellybean-lpc4330_rom_to_ram.ld create mode 100644 examples/lpc43xx/hackrf-jellybean/jellybean_conf.h create mode 100644 examples/lpc43xx/hackrf-jellybean/miniblink/Makefile create mode 100644 examples/lpc43xx/hackrf-jellybean/miniblink/README create mode 100644 examples/lpc43xx/hackrf-jellybean/miniblink/miniblink.c create mode 100644 examples/lpc43xx/hackrf-jellybean/miniblink_rom_to_ram/Makefile create mode 100644 examples/lpc43xx/hackrf-jellybean/miniblink_rom_to_ram/README create mode 100644 examples/lpc43xx/hackrf-jellybean/miniblink_rom_to_ram/miniblink.c create mode 100644 examples/lpc43xx/hackrf-jellybean/ssp/Makefile create mode 100644 examples/lpc43xx/hackrf-jellybean/ssp/README create mode 100644 examples/lpc43xx/hackrf-jellybean/ssp/sspdemo.c create mode 100644 examples/lpc43xx/hackrf-jellybean/systick/Makefile create mode 100644 examples/lpc43xx/hackrf-jellybean/systick/README create mode 100644 examples/lpc43xx/hackrf-jellybean/systick/systickdemo.c create mode 100644 examples/stm32/f1/Makefile.include create mode 100644 examples/stm32/f1/lisa-m-1/can/Makefile create mode 100644 examples/stm32/f1/lisa-m-1/can/README create mode 100644 examples/stm32/f1/lisa-m-1/can/can.c create mode 100644 examples/stm32/f1/lisa-m-1/can/can.c~ create mode 100644 examples/stm32/f1/lisa-m-1/fancyblink/Makefile create mode 100644 examples/stm32/f1/lisa-m-1/fancyblink/fancyblink.c create mode 100644 examples/stm32/f1/lisa-m-1/lisa-m.ld create mode 100644 examples/stm32/f1/lisa-m-1/usb_cdcacm/Makefile create mode 100644 examples/stm32/f1/lisa-m-1/usb_cdcacm/README create mode 100644 examples/stm32/f1/lisa-m-1/usb_cdcacm/cdcacm.c create mode 100644 examples/stm32/f1/lisa-m-1/usb_cdcacm/cdcacm_test.py create mode 100644 examples/stm32/f1/lisa-m-1/usb_dfu/Makefile create mode 100644 examples/stm32/f1/lisa-m-1/usb_dfu/README create mode 100644 examples/stm32/f1/lisa-m-1/usb_dfu/usbdfu.c create mode 100644 examples/stm32/f1/lisa-m-1/usb_hid/Makefile create mode 100644 examples/stm32/f1/lisa-m-1/usb_hid/README create mode 100644 examples/stm32/f1/lisa-m-1/usb_hid/adxl345.h create mode 100644 examples/stm32/f1/lisa-m-1/usb_hid/usbhid.c create mode 100644 examples/stm32/f1/lisa-m-2/adc_injec/Makefile create mode 100644 examples/stm32/f1/lisa-m-2/adc_injec/README create mode 100644 examples/stm32/f1/lisa-m-2/adc_injec/adc_injec.c create mode 100644 examples/stm32/f1/lisa-m-2/adc_injec_timtrig/Makefile create mode 100644 examples/stm32/f1/lisa-m-2/adc_injec_timtrig/README create mode 100644 examples/stm32/f1/lisa-m-2/adc_injec_timtrig/adc_injec_timtrig.c create mode 100644 examples/stm32/f1/lisa-m-2/adc_injec_timtrig_irq/Makefile create mode 100644 examples/stm32/f1/lisa-m-2/adc_injec_timtrig_irq/README create mode 100644 examples/stm32/f1/lisa-m-2/adc_injec_timtrig_irq/adc_injec_timtrig_irq.c create mode 100644 examples/stm32/f1/lisa-m-2/adc_injec_timtrig_irq_4ch/Makefile create mode 100644 examples/stm32/f1/lisa-m-2/adc_injec_timtrig_irq_4ch/README create mode 100644 examples/stm32/f1/lisa-m-2/adc_injec_timtrig_irq_4ch/adc_injec_timtrig_irq_4ch.c create mode 100644 examples/stm32/f1/lisa-m-2/adc_regular/Makefile create mode 100644 examples/stm32/f1/lisa-m-2/adc_regular/README create mode 100644 examples/stm32/f1/lisa-m-2/adc_regular/adc.c create mode 100644 examples/stm32/f1/lisa-m-2/can/Makefile create mode 100644 examples/stm32/f1/lisa-m-2/can/README create mode 100644 examples/stm32/f1/lisa-m-2/can/can.c create mode 100644 examples/stm32/f1/lisa-m-2/can/can.c~ create mode 100644 examples/stm32/f1/lisa-m-2/fancyblink/Makefile create mode 100644 examples/stm32/f1/lisa-m-2/fancyblink/fancyblink.c create mode 100644 examples/stm32/f1/lisa-m-2/lisa-m.ld create mode 100644 examples/stm32/f1/lisa-m-2/spi/Makefile create mode 100644 examples/stm32/f1/lisa-m-2/spi/README create mode 100644 examples/stm32/f1/lisa-m-2/spi/spi.c create mode 100644 examples/stm32/f1/lisa-m-2/spi_dma/Makefile create mode 100644 examples/stm32/f1/lisa-m-2/spi_dma/README create mode 100644 examples/stm32/f1/lisa-m-2/spi_dma/spi_dma.c create mode 100644 examples/stm32/f1/lisa-m-2/spi_dma_adv/Makefile create mode 100644 examples/stm32/f1/lisa-m-2/spi_dma_adv/README create mode 100644 examples/stm32/f1/lisa-m-2/spi_dma_adv/spi_dma_adv.c create mode 100644 examples/stm32/f1/lisa-m-2/usart/Makefile create mode 100644 examples/stm32/f1/lisa-m-2/usart/README create mode 100644 examples/stm32/f1/lisa-m-2/usart/usart.c create mode 100644 examples/stm32/f1/lisa-m-2/usart_dma/Makefile create mode 100644 examples/stm32/f1/lisa-m-2/usart_dma/usart_dma.c create mode 100644 examples/stm32/f1/lisa-m-2/usart_dma/usart_dma.c~ create mode 100644 examples/stm32/f1/lisa-m-2/usart_irq/Makefile create mode 100644 examples/stm32/f1/lisa-m-2/usart_irq/usart_irq.c create mode 100644 examples/stm32/f1/lisa-m-2/usart_irq_printf/Makefile create mode 100644 examples/stm32/f1/lisa-m-2/usart_irq_printf/usart_irq_printf.c create mode 100644 examples/stm32/f1/lisa-m-2/usart_printf/Makefile create mode 100644 examples/stm32/f1/lisa-m-2/usart_printf/usart_printf.c create mode 100644 examples/stm32/f1/mb525/fancyblink/Makefile create mode 100644 examples/stm32/f1/mb525/fancyblink/README create mode 100644 examples/stm32/f1/mb525/fancyblink/fancyblink.c create mode 100644 examples/stm32/f1/mb525/mb525.ld create mode 100644 examples/stm32/f1/mb525/pwmleds/Makefile create mode 100644 examples/stm32/f1/mb525/pwmleds/README create mode 100644 examples/stm32/f1/mb525/pwmleds/pwmleds.c create mode 100644 examples/stm32/f1/obldc-strip/can/Makefile create mode 100644 examples/stm32/f1/obldc-strip/can/README create mode 100644 examples/stm32/f1/obldc-strip/can/can.c create mode 100644 examples/stm32/f1/obldc-strip/can/can.c~ create mode 100644 examples/stm32/f1/obldc-strip/led/Makefile create mode 100644 examples/stm32/f1/obldc-strip/led/led.c create mode 100644 examples/stm32/f1/obldc-strip/obldc-strip.ld create mode 100644 examples/stm32/f1/obldc-strip/systick/Makefile create mode 100644 examples/stm32/f1/obldc-strip/systick/systick.c create mode 100644 examples/stm32/f1/obldc-strip/usart/Makefile create mode 100644 examples/stm32/f1/obldc-strip/usart/usart.c create mode 100644 examples/stm32/f1/obldc/can/Makefile create mode 100644 examples/stm32/f1/obldc/can/can.c create mode 100644 examples/stm32/f1/obldc/can/can.c~ create mode 100644 examples/stm32/f1/obldc/led/Makefile create mode 100644 examples/stm32/f1/obldc/led/led.c create mode 100644 examples/stm32/f1/obldc/obldc.ld create mode 100644 examples/stm32/f1/obldc/pwmleds/Makefile create mode 100644 examples/stm32/f1/obldc/pwmleds/pwmleds.c create mode 100644 examples/stm32/f1/obldc/systick/Makefile create mode 100644 examples/stm32/f1/obldc/systick/systick.c create mode 100644 examples/stm32/f1/obldc/usart/Makefile create mode 100644 examples/stm32/f1/obldc/usart/usart.c create mode 100644 examples/stm32/f1/obldc/usart_irq/Makefile create mode 100644 examples/stm32/f1/obldc/usart_irq/usart_irq.c create mode 100644 examples/stm32/f1/other/adc_temperature_sensor/Makefile create mode 100644 examples/stm32/f1/other/adc_temperature_sensor/README create mode 100644 examples/stm32/f1/other/adc_temperature_sensor/adc.c create mode 100644 examples/stm32/f1/other/adc_temperature_sensor/adc.ld create mode 100644 examples/stm32/f1/other/dma_mem2mem/Makefile create mode 100644 examples/stm32/f1/other/dma_mem2mem/README create mode 100644 examples/stm32/f1/other/dma_mem2mem/dma.c create mode 100644 examples/stm32/f1/other/dma_mem2mem/dma.ld create mode 100644 examples/stm32/f1/other/dogm128/Makefile create mode 100644 examples/stm32/f1/other/dogm128/README create mode 100644 examples/stm32/f1/other/dogm128/dogm128.c create mode 100644 examples/stm32/f1/other/dogm128/dogm128.h create mode 100644 examples/stm32/f1/other/dogm128/main.c create mode 100644 examples/stm32/f1/other/dogm128/main.ld create mode 100644 examples/stm32/f1/other/i2c_stts75_sensor/Makefile create mode 100644 examples/stm32/f1/other/i2c_stts75_sensor/README create mode 100644 examples/stm32/f1/other/i2c_stts75_sensor/i2c_stts75_sensor.c create mode 100644 examples/stm32/f1/other/i2c_stts75_sensor/i2c_stts75_sensor.ld create mode 100644 examples/stm32/f1/other/i2c_stts75_sensor/stts75.c create mode 100644 examples/stm32/f1/other/i2c_stts75_sensor/stts75.h create mode 100644 examples/stm32/f1/other/rtc/Makefile create mode 100644 examples/stm32/f1/other/rtc/README create mode 100644 examples/stm32/f1/other/rtc/rtc.c create mode 100644 examples/stm32/f1/other/rtc/rtc.ld create mode 100644 examples/stm32/f1/other/systick/Makefile create mode 100644 examples/stm32/f1/other/systick/README create mode 100644 examples/stm32/f1/other/systick/systick.c create mode 100644 examples/stm32/f1/other/systick/systick.ld create mode 100644 examples/stm32/f1/other/timer_interrupt/Makefile create mode 100644 examples/stm32/f1/other/timer_interrupt/README create mode 100644 examples/stm32/f1/other/timer_interrupt/timer.c create mode 100644 examples/stm32/f1/other/timer_interrupt/timer.ld create mode 100644 examples/stm32/f1/other/usb_cdcacm/Makefile create mode 100644 examples/stm32/f1/other/usb_cdcacm/README create mode 100644 examples/stm32/f1/other/usb_cdcacm/cdcacm.c create mode 100644 examples/stm32/f1/other/usb_cdcacm/cdcacm.ld create mode 100644 examples/stm32/f1/other/usb_dfu/Makefile create mode 100644 examples/stm32/f1/other/usb_dfu/README create mode 100644 examples/stm32/f1/other/usb_dfu/usbdfu.c create mode 100644 examples/stm32/f1/other/usb_dfu/usbdfu.ld create mode 100644 examples/stm32/f1/other/usb_hid/Makefile create mode 100644 examples/stm32/f1/other/usb_hid/README create mode 100644 examples/stm32/f1/other/usb_hid/usbhid.c create mode 100644 examples/stm32/f1/other/usb_hid/usbhid.ld create mode 100644 examples/stm32/f1/stm32-h103/button/Makefile create mode 100644 examples/stm32/f1/stm32-h103/button/button.c create mode 100644 examples/stm32/f1/stm32-h103/exti_both/Makefile create mode 100644 examples/stm32/f1/stm32-h103/exti_both/exti_both.c create mode 100644 examples/stm32/f1/stm32-h103/exti_rising_falling/Makefile create mode 100644 examples/stm32/f1/stm32-h103/exti_rising_falling/exti_rising_falling.c create mode 100644 examples/stm32/f1/stm32-h103/fancyblink/Makefile create mode 100644 examples/stm32/f1/stm32-h103/fancyblink/README create mode 100644 examples/stm32/f1/stm32-h103/fancyblink/fancyblink.c create mode 100644 examples/stm32/f1/stm32-h103/led_stripe/Makefile create mode 100644 examples/stm32/f1/stm32-h103/led_stripe/led_stripe.c create mode 100644 examples/stm32/f1/stm32-h103/miniblink/Makefile create mode 100644 examples/stm32/f1/stm32-h103/miniblink/README create mode 100644 examples/stm32/f1/stm32-h103/miniblink/miniblink.c create mode 100644 examples/stm32/f1/stm32-h103/pwm_6step/Makefile create mode 100644 examples/stm32/f1/stm32-h103/pwm_6step/pwm_6step.c create mode 100644 examples/stm32/f1/stm32-h103/spi/Makefile create mode 100644 examples/stm32/f1/stm32-h103/spi/README create mode 100644 examples/stm32/f1/stm32-h103/spi/spi.c create mode 100644 examples/stm32/f1/stm32-h103/stm32-h103.ld create mode 100644 examples/stm32/f1/stm32-h103/timer/Makefile create mode 100644 examples/stm32/f1/stm32-h103/timer/timer.c create mode 100644 examples/stm32/f1/stm32-h103/traceswo/Makefile create mode 100644 examples/stm32/f1/stm32-h103/traceswo/README create mode 100644 examples/stm32/f1/stm32-h103/traceswo/traceswo.c create mode 100644 examples/stm32/f1/stm32-h103/usart/Makefile create mode 100644 examples/stm32/f1/stm32-h103/usart/README create mode 100644 examples/stm32/f1/stm32-h103/usart/usart.c create mode 100644 examples/stm32/f1/stm32-h103/usart_irq/Makefile create mode 100644 examples/stm32/f1/stm32-h103/usart_irq/usart_irq.c create mode 100644 examples/stm32/f1/stm32-h103/usart_irq_printf/Makefile create mode 100644 examples/stm32/f1/stm32-h103/usart_irq_printf/usart_irq_printf.c create mode 100644 examples/stm32/f1/stm32-h103/usart_printf/Makefile create mode 100644 examples/stm32/f1/stm32-h103/usart_printf/usart_printf.c create mode 100644 examples/stm32/f1/stm32-h103/usb_cdcacm/Makefile create mode 100644 examples/stm32/f1/stm32-h103/usb_cdcacm/README create mode 100644 examples/stm32/f1/stm32-h103/usb_cdcacm/cdcacm.c create mode 100644 examples/stm32/f1/stm32-h103/usb_dfu/Makefile create mode 100644 examples/stm32/f1/stm32-h103/usb_dfu/README create mode 100644 examples/stm32/f1/stm32-h103/usb_dfu/usbdfu.c create mode 100644 examples/stm32/f1/stm32-h103/usb_hid/Makefile create mode 100644 examples/stm32/f1/stm32-h103/usb_hid/README create mode 100644 examples/stm32/f1/stm32-h103/usb_hid/usbhid.c create mode 100644 examples/stm32/f1/stm32-h103/usb_iap/Makefile create mode 100644 examples/stm32/f1/stm32-h103/usb_iap/README create mode 100644 examples/stm32/f1/stm32-h103/usb_iap/usbiap.c create mode 100644 examples/stm32/f1/stm32-h107/fancyblink/Makefile create mode 100644 examples/stm32/f1/stm32-h107/fancyblink/README create mode 100644 examples/stm32/f1/stm32-h107/fancyblink/fancyblink.c create mode 100644 examples/stm32/f1/stm32-h107/stm32-h107.ld create mode 100644 examples/stm32/f1/stm32-h107/usb_simple/Makefile create mode 100644 examples/stm32/f1/stm32-h107/usb_simple/README create mode 100644 examples/stm32/f1/stm32-h107/usb_simple/usb_simple.c create mode 100644 examples/stm32/f1/stm32-h107/usb_simple/usbtest.py create mode 100644 examples/stm32/f1/stm32-maple/miniblink/Makefile create mode 100644 examples/stm32/f1/stm32-maple/miniblink/README create mode 100644 examples/stm32/f1/stm32-maple/miniblink/miniblink.c create mode 100644 examples/stm32/f1/stm32-maple/stm32-maple.ld create mode 100644 examples/stm32/f1/stm32-maple/usart/Makefile create mode 100644 examples/stm32/f1/stm32-maple/usart/README create mode 100644 examples/stm32/f1/stm32-maple/usart/usart.c create mode 100644 examples/stm32/f1/stm32-maple/usart_irq/Makefile create mode 100644 examples/stm32/f1/stm32-maple/usart_irq/usart_irq.c create mode 100644 examples/stm32/f1/stm32-maple/usb_cdcacm/Makefile create mode 100644 examples/stm32/f1/stm32-maple/usb_cdcacm/README create mode 100644 examples/stm32/f1/stm32-maple/usb_cdcacm/cdcacm.c create mode 100644 examples/stm32/f1/stm32vl-discovery/adc-dac-printf/Makefile create mode 100644 examples/stm32/f1/stm32vl-discovery/adc-dac-printf/README create mode 100644 examples/stm32/f1/stm32vl-discovery/adc-dac-printf/adc-dac-printf.c create mode 100644 examples/stm32/f1/stm32vl-discovery/button/Makefile create mode 100644 examples/stm32/f1/stm32vl-discovery/button/README create mode 100644 examples/stm32/f1/stm32vl-discovery/button/button.c create mode 100644 examples/stm32/f1/stm32vl-discovery/fancyblink/Makefile create mode 100644 examples/stm32/f1/stm32vl-discovery/fancyblink/README create mode 100644 examples/stm32/f1/stm32vl-discovery/fancyblink/fancyblink.c create mode 100644 examples/stm32/f1/stm32vl-discovery/miniblink/Makefile create mode 100644 examples/stm32/f1/stm32vl-discovery/miniblink/README create mode 100644 examples/stm32/f1/stm32vl-discovery/miniblink/miniblink.c create mode 100644 examples/stm32/f1/stm32vl-discovery/rtc/Makefile create mode 100644 examples/stm32/f1/stm32vl-discovery/rtc/README create mode 100644 examples/stm32/f1/stm32vl-discovery/rtc/rtc.c create mode 100644 examples/stm32/f1/stm32vl-discovery/stm32vl-discovery.ld create mode 100644 examples/stm32/f1/stm32vl-discovery/usart/Makefile create mode 100644 examples/stm32/f1/stm32vl-discovery/usart/README create mode 100644 examples/stm32/f1/stm32vl-discovery/usart/usart.c create mode 100644 examples/stm32/f2/Makefile.include create mode 100644 examples/stm32/f2/jobygps/jobygps.cfg create mode 100644 examples/stm32/f2/jobygps/jobygps.ld create mode 100644 examples/stm32/f2/jobygps/miniblink/Makefile create mode 100644 examples/stm32/f2/jobygps/miniblink/README create mode 100644 examples/stm32/f2/jobygps/miniblink/miniblink.c create mode 100644 examples/stm32/f2/jobygps/spi_test/Makefile create mode 100644 examples/stm32/f2/jobygps/spi_test/spi_test.c create mode 100644 examples/stm32/f2/jobygps/usart_printf/Makefile create mode 100644 examples/stm32/f2/jobygps/usart_printf/usart_printf.c create mode 100644 examples/stm32/f4/Makefile.include create mode 100644 examples/stm32/f4/stm32f4-discovery/button/Makefile create mode 100644 examples/stm32/f4/stm32f4-discovery/button/README create mode 100644 examples/stm32/f4/stm32f4-discovery/button/button.c create mode 100644 examples/stm32/f4/stm32f4-discovery/fancyblink/Makefile create mode 100644 examples/stm32/f4/stm32f4-discovery/fancyblink/README create mode 100644 examples/stm32/f4/stm32f4-discovery/fancyblink/fancyblink.c create mode 100644 examples/stm32/f4/stm32f4-discovery/mandelbrot/Makefile create mode 100644 examples/stm32/f4/stm32f4-discovery/mandelbrot/README create mode 100644 examples/stm32/f4/stm32f4-discovery/mandelbrot/mandel.c create mode 100644 examples/stm32/f4/stm32f4-discovery/miniblink/Makefile create mode 100644 examples/stm32/f4/stm32f4-discovery/miniblink/README create mode 100644 examples/stm32/f4/stm32f4-discovery/miniblink/miniblink.c create mode 100644 examples/stm32/f4/stm32f4-discovery/random/Makefile create mode 100644 examples/stm32/f4/stm32f4-discovery/random/README create mode 100644 examples/stm32/f4/stm32f4-discovery/random/random.c create mode 100644 examples/stm32/f4/stm32f4-discovery/stm32f4-discovery.ld create mode 100644 examples/stm32/f4/stm32f4-discovery/usart/Makefile create mode 100644 examples/stm32/f4/stm32f4-discovery/usart/README create mode 100644 examples/stm32/f4/stm32f4-discovery/usart/usart.c create mode 100644 examples/stm32/f4/stm32f4-discovery/usart_irq/Makefile create mode 100644 examples/stm32/f4/stm32f4-discovery/usart_irq/README create mode 100644 examples/stm32/f4/stm32f4-discovery/usart_irq/usart_irq.c create mode 100644 examples/stm32/f4/stm32f4-discovery/usart_irq/usart_irq.c~ create mode 100644 examples/stm32/f4/stm32f4-discovery/usb_cdcacm/Makefile create mode 100644 examples/stm32/f4/stm32f4-discovery/usb_cdcacm/README create mode 100644 examples/stm32/f4/stm32f4-discovery/usb_cdcacm/cdcacm.c create mode 100644 examples/stm32/l1/Makefile.include create mode 100644 examples/stm32/l1/stm32l-discovery/button-irq-printf-lowpower/Makefile create mode 100644 examples/stm32/l1/stm32l-discovery/button-irq-printf-lowpower/README create mode 100644 examples/stm32/l1/stm32l-discovery/button-irq-printf-lowpower/main.c create mode 100644 examples/stm32/l1/stm32l-discovery/button-irq-printf-lowpower/syscfg.h create mode 100644 examples/stm32/l1/stm32l-discovery/button-irq-printf/Makefile create mode 100644 examples/stm32/l1/stm32l-discovery/button-irq-printf/README create mode 100644 examples/stm32/l1/stm32l-discovery/button-irq-printf/main.c create mode 100644 examples/stm32/l1/stm32l-discovery/button-irq-printf/syscfg.h create mode 100644 examples/stm32/l1/stm32l-discovery/miniblink/Makefile create mode 100644 examples/stm32/l1/stm32l-discovery/miniblink/README create mode 100644 examples/stm32/l1/stm32l-discovery/miniblink/miniblink.c create mode 100644 examples/stm32/l1/stm32l-discovery/usart/Makefile create mode 100644 examples/stm32/l1/stm32l-discovery/usart/README create mode 100644 examples/stm32/l1/stm32l-discovery/usart/usart.c create mode 160000 libopencm3 diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..c4a0df1 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "libopencm3"] + path = libopencm3 + url = https://github.com/libopencm3/libopencm3.git diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c7fdf35 --- /dev/null +++ b/Makefile @@ -0,0 +1,59 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +PREFIX ?= arm-none-eabi +#PREFIX ?= arm-elf + +# Be silent per default, but 'make V=1' will show all compiler calls. +ifneq ($(V),1) +Q := @ +# Do not print "Entering directory ...". +MAKEFLAGS += --no-print-directory +endif + +all: build + +build: lib examples + +lib: + $(Q)$(MAKE) -C libopencm3 + +EXAMPLE_DIRS:=$(sort $(dir $(wildcard $(addsuffix /*/*/Makefile,$(addprefix examples/,$(TARGETS)))))) +$(EXAMPLE_DIRS): lib + @printf " BUILD $@\n"; + $(Q)$(MAKE) --directory=$@ + +examples: $(EXAMPLE_DIRS) + $(Q)true + +doc: + $(Q)$(MAKE) -C doc html + +# Bleh http://www.makelinux.net/make3/make3-CHP-6-SECT-1#make3-CHP-6-SECT-1 +clean: cleanheaders + $(Q)for i in $(LIB_DIRS) \ + $(EXAMPLE_DIRS); do \ + if [ -d $$i ]; then \ + printf " CLEAN $$i\n"; \ + $(MAKE) -C $$i clean SRCLIBDIR=$(SRCLIBDIR) || exit $?; \ + fi; \ + done + +.PHONY: build lib examples $(EXAMPLE_DIRS) install doc clean + diff --git a/README b/README new file mode 100644 index 0000000..b5ae142 --- /dev/null +++ b/README @@ -0,0 +1,45 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This repository contains assorted example projects for libopencm3. + +The libopencm3 project aims to create an open-source firmware library for +various ARM Cortex-M3 microcontrollers. + +For more information visit http://libopencm3.org + +The examples are meant as starting points for different subsystems on multitude +of platforms. + +Feel free to add new examples and send them to us either via the mailinglist or +preferably via a github pull request. + +Usage +----- + +The makefiles are generally useable for your own projects with +only minimal changes for the libopencm3 install path (See Installation) + +For flashing the 'miniblink' example (after you built libopencm3 and the +examples by typing 'make' at the top-level directory) onto the Olimex +STM32-H103 eval board (ST STM32F1 series microcontroller), you can execute: + + $ cd examples/stm32/f1/stm32-h103/miniblink + $ make flash + +The Makefiles of the examples are configured to use a certain OpenOCD +flash programmer, you might need to change some of the variables in the +Makefile if you use a different one. + +You can also flash manually like this: + + $ openocd -f interface/jtagkey-tiny.cfg -f target/stm32f1x.cfg + $ telnet localhost 4444 + > reset halt + > flash write_image erase foobar.hex + > reset + +Replace the "jtagkey-tiny.cfg" with whatever JTAG device you are using, and/or +replace "stm32f1x.cfg" with your respective config file. Replace "foobar.hex" +with the file name of the image you want to flash. diff --git a/examples/lm3s/Makefile.include b/examples/lm3s/Makefile.include new file mode 100644 index 0000000..c698f0c --- /dev/null +++ b/examples/lm3s/Makefile.include @@ -0,0 +1,137 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## Copyright (C) 2010 Piotr Esden-Tempski +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +PREFIX ?= arm-none-eabi +#PREFIX ?= arm-elf +CC = $(PREFIX)-gcc +LD = $(PREFIX)-gcc +OBJCOPY = $(PREFIX)-objcopy +OBJDUMP = $(PREFIX)-objdump + +TOOLCHAIN_DIR ?= ../../../.. +ifeq ($(wildcard ../../../../lib/libopencm3_lm3s.a),) +ifneq ($(strip $(shell which $(CC))),) +TOOLCHAIN_DIR := $(shell dirname `which $(CC)`)/../$(PREFIX) +endif +else +ifeq ($(V),1) +$(info We seem to be building the example in the source directory. Using local library!) +endif +endif + +CFLAGS += -O0 -g3 \ + -Wall -Wextra -Wimplicit-function-declaration \ + -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes \ + -Wundef -Wshadow \ + -I$(TOOLCHAIN_DIR)/include -fno-common \ + -mcpu=cortex-m3 -mthumb -MD -DLM3S +LDSCRIPT ?= $(BINARY).ld +LDFLAGS += -L$(TOOLCHAIN_DIR)/lib \ + -T$(LDSCRIPT) -nostartfiles -Wl,--gc-sections +OBJS += $(BINARY).o + +OOCD ?= openocd +OOCD_INTERFACE ?= flossjtag +OOCD_BOARD ?= olimex_stm32_h103 +# FIXME + +# Be silent per default, but 'make V=1' will show all compiler calls. +ifneq ($(V),1) +Q := @ +NULL := 2>/dev/null +else +LDFLAGS += -Wl,--print-gc-sections +endif + +.SUFFIXES: .elf .bin .hex .srec .list .images +.SECONDEXPANSION: +.SECONDARY: + +all: images + +images: $(BINARY).images +flash: $(BINARY).flash + +%.images: %.bin %.hex %.srec %.list + @#echo "*** $* images generated ***" + +%.bin: %.elf + @#printf " OBJCOPY $(*).bin\n" + $(Q)$(OBJCOPY) -Obinary $(*).elf $(*).bin + +%.hex: %.elf + @#printf " OBJCOPY $(*).hex\n" + $(Q)$(OBJCOPY) -Oihex $(*).elf $(*).hex + +%.srec: %.elf + @#printf " OBJCOPY $(*).srec\n" + $(Q)$(OBJCOPY) -Osrec $(*).elf $(*).srec + +%.list: %.elf + @#printf " OBJDUMP $(*).list\n" + $(Q)$(OBJDUMP) -S $(*).elf > $(*).list + +%.elf: $(OBJS) $(LDSCRIPT) $(TOOLCHAIN_DIR)/lib/libopencm3_lm3s.a + @#printf " LD $(subst $(shell pwd)/,,$(@))\n" + $(Q)$(LD) $(LDFLAGS) -o $(*).elf $(OBJS) -lopencm3_lm3s + +%.o: %.c Makefile + @#printf " CC $(subst $(shell pwd)/,,$(@))\n" + $(Q)$(CC) $(CFLAGS) -o $@ -c $< + +clean: + $(Q)rm -f *.o + $(Q)rm -f *.d + $(Q)rm -f *.elf + $(Q)rm -f *.bin + $(Q)rm -f *.hex + $(Q)rm -f *.srec + $(Q)rm -f *.list + +# FIXME: Replace STM32 stuff with proper LPC13XX OpenOCD support later. +ifeq ($(OOCD_SERIAL),) +%.flash: %.hex + @printf " FLASH $<\n" + @# IMPORTANT: Don't use "resume", only "reset" will work correctly! + $(Q)$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \ + -f board/$(OOCD_BOARD).cfg \ + -c "init" -c "reset init" \ + -c "stm32x mass_erase 0" \ + -c "flash write_image $(*).hex" \ + -c "reset" \ + -c "shutdown" $(NULL) +else +%.flash: %.hex + @printf " FLASH $<\n" + @# IMPORTANT: Don't use "resume", only "reset" will work correctly! + $(Q)$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \ + -f board/$(OOCD_BOARD).cfg \ + -c "ft2232_serial $(OOCD_SERIAL)" \ + -c "init" -c "reset init" \ + -c "stm32x mass_erase 0" \ + -c "flash write_image $(*).hex" \ + -c "reset" \ + -c "shutdown" $(NULL) +endif + +.PHONY: images clean + +-include $(OBJS:.o=.d) + diff --git a/examples/lm3s/lm3s3748-evb/lm3s3748-evb.ld b/examples/lm3s/lm3s3748-evb/lm3s3748-evb.ld new file mode 100644 index 0000000..6efc111 --- /dev/null +++ b/examples/lm3s/lm3s3748-evb/lm3s3748-evb.ld @@ -0,0 +1,31 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* Linker script for Luminary Micro (now TI) Stellaris LM3S3748-EVB. */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x00000000, LENGTH = 128K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 64K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_lm3s.ld + diff --git a/examples/lm3s/lm3s3748-evb/miniblink/Makefile b/examples/lm3s/lm3s3748-evb/miniblink/Makefile new file mode 100644 index 0000000..6a935dc --- /dev/null +++ b/examples/lm3s/lm3s3748-evb/miniblink/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2010 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = miniblink + +LDSCRIPT = ../lm3s3748-evb.ld + +include ../../Makefile.include + diff --git a/examples/lm3s/lm3s3748-evb/miniblink/README b/examples/lm3s/lm3s3748-evb/miniblink/README new file mode 100644 index 0000000..79f3941 --- /dev/null +++ b/examples/lm3s/lm3s3748-evb/miniblink/README @@ -0,0 +1,9 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This is the smallest-possible example program using libopencm3. + +It's intended for the LuminaryMicro LM3S3748-EVB. +It should blink the STATUS LED on the board. + diff --git a/examples/lm3s/lm3s3748-evb/miniblink/miniblink.c b/examples/lm3s/lm3s3748-evb/miniblink/miniblink.c new file mode 100644 index 0000000..5a3b418 --- /dev/null +++ b/examples/lm3s/lm3s3748-evb/miniblink/miniblink.c @@ -0,0 +1,51 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2011 Gareth McMullin + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include + +static void gpio_setup(void) +{ + SYSTEMCONTROL_RCGC2 |= 0x20; /* Enable GPIOF in run mode. */ + + GPIO_DIR(GPIOF) |= (1 << 0); /* Configure PF0 as output. */ + GPIO_DEN(GPIOF) |= (1 << 0); /* Enable digital function on PF0. */ +} + +int main(void) +{ + int i; + + gpio_setup(); + + /* Blink STATUS LED (PF0) on the board. */ + while (1) { + gpio_set(GPIOF, GPIO0); + + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + + gpio_clear(GPIOF, GPIO0); + + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/lm4f/Makefile.include b/examples/lm4f/Makefile.include new file mode 100644 index 0000000..91511ce --- /dev/null +++ b/examples/lm4f/Makefile.include @@ -0,0 +1,143 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## Copyright (C) 2010 Piotr Esden-Tempski +## Copyright (C) 2012 Alexandru Gagniuc +## +## This library is free software: you can redistribute it and/or modify +## it undebipr the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +PREFIX ?= arm-none-eabi +CC = $(PREFIX)-gcc +LD = $(PREFIX)-gcc +OBJCOPY = $(PREFIX)-objcopy +OBJDUMP = $(PREFIX)-objdump + +TOOLCHAIN_DIR ?= ../../../.. +ifeq ($(wildcard ../../../../lib/libopencm3_lm4f.a),) +ifneq ($(strip $(shell which $(CC))),) +TOOLCHAIN_DIR := $(shell dirname `which $(CC)`)/../$(PREFIX) +endif +else +ifeq ($(V),1) +$(info We seem to be building the example in the source directory. Using local library!) +endif +endif + +ARCH_FLAGS = -mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 +CFLAGS += -O0 -g3 \ + -Wall -Wextra -Wimplicit-function-declaration \ + -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes \ + -Wundef -Wshadow \ + -I$(TOOLCHAIN_DIR)/include \ + -fno-common $(ARCH_FLAGS) -MD -DLM4F +LDSCRIPT ?= $(BINARY).ld +LDFLAGS += --static -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group \ + -L$(TOOLCHAIN_DIR)/lib \ + -T$(LDSCRIPT) -nostartfiles -Wl,--gc-sections \ + $(ARCH_FLAGS) + +OBJS += $(BINARY).o + +# Support for ICDI is not in any openocd release as of yet +# A patch to add support for ICDI is under review +OOCD ?= openocd +OOCD_INTERFACE ?= ti-icdi +# No official board.cfg for the stellaris launchpad +OOCD_BOARD ?= stellaris_launchpad + +# Be silent per default, but 'make V=1' will show all compiler calls. +ifneq ($(V),1) +Q := @ +NULL := 2>/dev/null +else +LDFLAGS += -Wl,--print-gc-sections +endif + +.SUFFIXES: .elf .bin .hex .srec .list .images +.SECONDEXPANSION: +.SECONDARY: + +all: images + +images: $(BINARY).images +flash: $(BINARY).flash + +%.images: %.bin %.hex %.srec %.list + @#echo "*** $* images generated ***" + +%.bin: %.elf + @#printf " OBJCOPY $(*).bin\n" + $(Q)$(OBJCOPY) -Obinary $(*).elf $(*).bin + +%.hex: %.elf + @#printf " OBJCOPY $(*).hex\n" + $(Q)$(OBJCOPY) -Oihex $(*).elf $(*).hex + +%.srec: %.elf + @#printf " OBJCOPY $(*).srec\n" + $(Q)$(OBJCOPY) -Osrec $(*).elf $(*).srec + +%.list: %.elf + @#printf " OBJDUMP $(*).list\n" + $(Q)$(OBJDUMP) -S $(*).elf > $(*).list + +%.elf: $(OBJS) $(LDSCRIPT) $(TOOLCHAIN_DIR)/lib/libopencm3_lm4f.a + @#printf " LD $(subst $(shell pwd)/,,$(@))\n" + $(Q)$(LD) -o $(*).elf $(OBJS) -lopencm3_lm4f $(LDFLAGS) + +%.o: %.c Makefile + @#printf " CC $(subst $(shell pwd)/,,$(@))\n" + $(Q)$(CC) $(CFLAGS) -o $@ -c $< + +clean: + $(Q)rm -f *.o + $(Q)rm -f *.d + $(Q)rm -f *.elf + $(Q)rm -f *.bin + $(Q)rm -f *.hex + $(Q)rm -f *.srec + $(Q)rm -f *.list + +# FIXME: Replace STM32 stuff with proper Stellaris support. +ifeq ($(OOCD_SERIAL),) +%.flash: %.hex + @printf " FLASH $<\n" + @# IMPORTANT: Don't use "resume", only "reset" will work correctly! + $(Q)$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \ + -f board/$(OOCD_BOARD).cfg \ + -c "init" -c "reset init" \ + -c "stm32x mass_erase 0" \ + -c "flash write_image $(*).hex" \ + -c "reset" \ + -c "shutdown" $(NULL) +else +%.flash: %.hex + @printf " FLASH $<\n" + @# IMPORTANT: Don't use "resume", only "reset" will work correctly! + $(Q)$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \ + -f board/$(OOCD_BOARD).cfg \ + -c "ft2232_serial $(OOCD_SERIAL)" \ + -c "init" -c "reset init" \ + -c "stm32x mass_erase 0" \ + -c "flash write_image $(*).hex" \ + -c "reset" \ + -c "shutdown" $(NULL) +endif + +.PHONY: images clean + +-include $(OBJS:.o=.d) + diff --git a/examples/lm4f/stellaris-ek-lm4f120xl/ek-lm4f120xl.ld b/examples/lm4f/stellaris-ek-lm4f120xl/ek-lm4f120xl.ld new file mode 100644 index 0000000..8a096fa --- /dev/null +++ b/examples/lm4f/stellaris-ek-lm4f120xl/ek-lm4f120xl.ld @@ -0,0 +1,31 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* Linker script for TI Stellaris EX-LM4F120XL evaluation board. */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x00000000, LENGTH = 256K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 32K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_lm4f.ld + diff --git a/examples/lm4f/stellaris-ek-lm4f120xl/miniblink/Makefile b/examples/lm4f/stellaris-ek-lm4f120xl/miniblink/Makefile new file mode 100644 index 0000000..9a014a7 --- /dev/null +++ b/examples/lm4f/stellaris-ek-lm4f120xl/miniblink/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2012 Alexandru Gagniuc +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = miniblink + +LDSCRIPT = ../ek-lm4f120xl.ld + +include ../../Makefile.include + diff --git a/examples/lm4f/stellaris-ek-lm4f120xl/miniblink/README b/examples/lm4f/stellaris-ek-lm4f120xl/miniblink/README new file mode 100644 index 0000000..a19550d --- /dev/null +++ b/examples/lm4f/stellaris-ek-lm4f120xl/miniblink/README @@ -0,0 +1,22 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example demonstrates the following: + * Configuriong GPIO pins + * Toggling GPIO pins + * Setting up and using GPIO interrupts + * Unlocking protected GPIO pins + * Controlling the system clock + * Changing the system clock on the fly + +Flashes the Red, Green and Blue diodes on the board, in order. The system clock +starts at 80MHz. +Pressing SW2 toggles the system clock between 80MHz, 57MHz, 40MHz ,20MHz, and +16MHz by changing the PLL divisor. +Pressing SW1 bypasses the PLL completely, and runs off the raw 16MHz clock +provided by the external crystal oscillator. +The LEDs will toggle at different speeds, depending on the system clock. The +system clock changes are handled within the interrupt service routine. + + diff --git a/examples/lm4f/stellaris-ek-lm4f120xl/miniblink/miniblink.c b/examples/lm4f/stellaris-ek-lm4f120xl/miniblink/miniblink.c new file mode 100644 index 0000000..89aca4f --- /dev/null +++ b/examples/lm4f/stellaris-ek-lm4f120xl/miniblink/miniblink.c @@ -0,0 +1,217 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2011 Gareth McMullin + * Copyright (C) 2012-2013 Alexandru Gagniuc + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/** + * \addtogroup Examples + * + * Flashes the Red, Green and Blue diodes on the board, in order. + * + * RED controlled by PF1 + * Green controlled by PF3 + * Blue controlled by PF2 + */ +#include +#include +#include +#include +#include + +#include +#include + +/* This is how the RGB LED is connected on the stellaris launchpad */ +#define RGB_PORT GPIOF +enum { + LED_R = GPIO1, + LED_G = GPIO3, + LED_B = GPIO2, +}; + +/* This is how the user switches are connected to GPIOF */ +enum { + USR_SW1 = GPIO4, + USR_SW2 = GPIO0, +}; + +/* The divisors we loop through when the user presses SW2 */ +enum { + PLL_DIV_80MHZ = 5, + PLL_DIV_57MHZ = 7, + PLL_DIV_40MHZ = 10, + PLL_DIV_20MHZ = 20, + PLL_DIV_16MHZ = 25, +}; + +static const u8 plldiv[] = { + PLL_DIV_80MHZ, + PLL_DIV_57MHZ, + PLL_DIV_40MHZ, + PLL_DIV_20MHZ, + PLL_DIV_16MHZ, + 0 +}; +/* The PLL divisor we are currently on */ +static size_t ipll = 0; +/* Are we bypassing the PLL, or not? */ +static bool bypass = false; + +/* + * Clock setup: + * Take the main crystal oscillator at 16MHz, run it through the PLL, and divide + * the 400MHz PLL clock to get a system clock of 80MHz. + */ +static void clock_setup(void) +{ + rcc_sysclk_config(OSCSRC_MOSC, XTAL_16M, PLL_DIV_80MHZ); +} + +/* + * GPIO setup: + * Enable the pins driving the RGB LED as outputs. + */ +static void gpio_setup(void) +{ + /* + * Configure GPIOF + * This port is used to control the RGB LED + */ + periph_clock_enable(RCC_GPIOF); + const u32 outpins = (LED_R | LED_G | LED_B); + + GPIO_DIR(RGB_PORT) |= outpins; /* Configure outputs. */ + GPIO_DEN(RGB_PORT) |= outpins; /* Enable digital function on outputs. */ + + /* + * Now take care of our buttons + */ + const u32 btnpins = USR_SW1 | USR_SW2; + + /* + * PF0 is locked by default. We need to unlock the GPIO_CR register, + * then enable PF0 commit. After we do this, we can setup PF0. If we + * don't do this, any configuration done to PF0 is lost, and we will not + * have a PF0 interrupt. + */ + GPIO_LOCK(GPIOF) = 0x4C4F434B; + GPIO_CR(GPIOF) |= USR_SW2; + + /* Configure pins as inputs. */ + GPIO_DIR(GPIOF) &= ~btnpins; + /* Enable digital function on the pins. */ + GPIO_DEN(GPIOF) |= btnpins; + /* Pull-up the pins. We don't have an external pull-up */ + GPIO_PUR(GPIOF) |= btnpins; +} + +/* + * IRQ setup: + * Trigger an interrupt whenever a button is depressed. + */ +static void irq_setup(void) +{ + const u32 btnpins = USR_SW1 | USR_SW2; + /* Configure interrupt as edge-sensitive */ + GPIO_IS(GPIOF) &= ~btnpins; + /* Interrupt only respond to rising or falling edge (single-edge) */ + GPIO_IBE(GPIOF) &= ~btnpins; + /* Trigger interrupt on rising-edge (when button is depressed) */ + GPIO_IEV(GPIOF) |= btnpins; + /* Finally, Enable interrupt */ + GPIO_IM(GPIOF) |= btnpins; + /* Enable the interrupt in the NVIC as well */ + nvic_enable_irq(NVIC_GPIOF_IRQ); +} + +#define FLASH_DELAY 800000 +static void delay(void) +{ + int i; + for (i = 0; i < FLASH_DELAY; i++) /* Wait a bit. */ + __asm__("nop"); +} + +int main(void) +{ + clock_setup(); + gpio_setup(); + irq_setup(); + + /* Blink each color of the RGB LED in order. */ + while (1) { + /* + * Flash the Red diode + */ + gpio_set(RGB_PORT, LED_R); + delay(); /* Wait a bit. */ + gpio_clear(RGB_PORT, LED_R); + delay(); /* Wait a bit. */ + + /* + * Flash the Green diode + */ + gpio_set(RGB_PORT, LED_G); + delay(); /* Wait a bit. */ + gpio_clear(RGB_PORT, LED_G); + delay(); /* Wait a bit. */ + + /* + * Flash the Blue diode + */ + gpio_set(RGB_PORT, LED_B); + delay(); /* Wait a bit. */ + gpio_clear(RGB_PORT, LED_B); + delay(); /* Wait a bit. */ + } + + return 0; +} + +void gpiof_isr(void) +{ + if (GPIO_RIS(GPIOF) & USR_SW1) { + /* SW1 was just depressed */ + bypass = !bypass; + if (bypass) { + rcc_pll_bypass_enable(); + /* + * The divisor is still applied to the raw clock. + * Disable the divisor, or we'll divide the raw clock. + */ + SYSCTL_RCC &= ~SYSCTL_RCC_USESYSDIV; + } + else + { + rcc_change_pll_divisor(plldiv[ipll]); + } + /* Clear interrupt source */ + GPIO_ICR(GPIOF) = USR_SW1; + } + + if (GPIO_RIS(GPIOF) & USR_SW2) { + /* SW2 was just depressed */ + if (!bypass) { + if (plldiv[++ipll] == 0) + ipll = 0; + rcc_change_pll_divisor(plldiv[ipll]); + } + /* Clear interrupt source */ + GPIO_ICR(GPIOF) = USR_SW2; + } +} diff --git a/examples/lpc13xx/Makefile.include b/examples/lpc13xx/Makefile.include new file mode 100644 index 0000000..049382f --- /dev/null +++ b/examples/lpc13xx/Makefile.include @@ -0,0 +1,137 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## Copyright (C) 2010 Piotr Esden-Tempski +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +PREFIX ?= arm-none-eabi +#PREFIX ?= arm-elf +CC = $(PREFIX)-gcc +LD = $(PREFIX)-gcc +OBJCOPY = $(PREFIX)-objcopy +OBJDUMP = $(PREFIX)-objdump + +TOOLCHAIN_DIR ?= ../../../.. +ifeq ($(wildcard ../../../../lib/libopencm3_lpc13xx.a),) +ifneq ($(strip $(shell which $(CC))),) +TOOLCHAIN_DIR := $(shell dirname `which $(CC)`)/../$(PREFIX) +endif +else +ifeq ($(V),1) +$(info We seem to be building the example in the source directory. Using local library!) +endif +endif + +CFLAGS += -Os -g \ + -Wall -Wextra -Wimplicit-function-declaration \ + -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes \ + -Wundef -Wshadow \ + -I$(TOOLCHAIN_DIR)/include -fno-common \ + -mcpu=cortex-m3 -mthumb -MD -DLPC13XX +LDSCRIPT ?= $(BINARY).ld +LDFLAGS += -L$(TOOLCHAIN_DIR)/lib \ + -T$(LDSCRIPT) -nostartfiles -Wl,--gc-sections +OBJS += $(BINARY).o + +OOCD ?= openocd +OOCD_INTERFACE ?= flossjtag +OOCD_BOARD ?= olimex_stm32_h103 +# FIXME + +# Be silent per default, but 'make V=1' will show all compiler calls. +ifneq ($(V),1) +Q := @ +NULL := 2>/dev/null +else +LDFLAGS += -Wl,--print-gc-sections +endif + +.SUFFIXES: .elf .bin .hex .srec .list .images +.SECONDEXPANSION: +.SECONDARY: + +all: images + +images: $(BINARY).images +flash: $(BINARY).flash + +%.images: %.bin %.hex %.srec %.list + @#echo "*** $* images generated ***" + +%.bin: %.elf + @#printf " OBJCOPY $(*).bin\n" + $(Q)$(OBJCOPY) -Obinary $(*).elf $(*).bin + +%.hex: %.elf + @#printf " OBJCOPY $(*).hex\n" + $(Q)$(OBJCOPY) -Oihex $(*).elf $(*).hex + +%.srec: %.elf + @#printf " OBJCOPY $(*).srec\n" + $(Q)$(OBJCOPY) -Osrec $(*).elf $(*).srec + +%.list: %.elf + @#printf " OBJDUMP $(*).list\n" + $(Q)$(OBJDUMP) -S $(*).elf > $(*).list + +%.elf: $(OBJS) $(LDSCRIPT) $(TOOLCHAIN_DIR)/lib/libopencm3_lpc13xx.a + @#printf " LD $(subst $(shell pwd)/,,$(@))\n" + $(Q)$(LD) $(LDFLAGS) -o $(*).elf $(OBJS) -lopencm3_lpc13xx + +%.o: %.c Makefile + @#printf " CC $(subst $(shell pwd)/,,$(@))\n" + $(Q)$(CC) $(CFLAGS) -o $@ -c $< + +clean: + $(Q)rm -f *.o + $(Q)rm -f *.d + $(Q)rm -f *.elf + $(Q)rm -f *.bin + $(Q)rm -f *.hex + $(Q)rm -f *.srec + $(Q)rm -f *.list + +# FIXME: Replace STM32 stuff with proper LPC13XX OpenOCD support later. +ifeq ($(OOCD_SERIAL),) +%.flash: %.hex + @printf " FLASH $<\n" + @# IMPORTANT: Don't use "resume", only "reset" will work correctly! + $(Q)$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \ + -f board/$(OOCD_BOARD).cfg \ + -c "init" -c "reset init" \ + -c "stm32x mass_erase 0" \ + -c "flash write_image $(*).hex" \ + -c "reset" \ + -c "shutdown" $(NULL) +else +%.flash: %.hex + @printf " FLASH $<\n" + @# IMPORTANT: Don't use "resume", only "reset" will work correctly! + $(Q)$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \ + -f board/$(OOCD_BOARD).cfg \ + -c "ft2232_serial $(OOCD_SERIAL)" \ + -c "init" -c "reset init" \ + -c "stm32x mass_erase 0" \ + -c "flash write_image $(*).hex" \ + -c "reset" \ + -c "shutdown" $(NULL) +endif + +.PHONY: images clean + +-include $(OBJS:.o=.d) + diff --git a/examples/lpc13xx/lpc-p1343/lpc-p1343.ld b/examples/lpc13xx/lpc-p1343/lpc-p1343.ld new file mode 100644 index 0000000..a957c91 --- /dev/null +++ b/examples/lpc13xx/lpc-p1343/lpc-p1343.ld @@ -0,0 +1,31 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* Linker script for Olimex LPC-P1343 (LPC1343FBD48, 32K flash, 8K SRAM). */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x00000000, LENGTH = 32K + ram (rwx) : ORIGIN = 0x10000000, LENGTH = 8K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_lpc13xx.ld + diff --git a/examples/lpc13xx/lpc-p1343/miniblink/Makefile b/examples/lpc13xx/lpc-p1343/miniblink/Makefile new file mode 100644 index 0000000..8bc77f8 --- /dev/null +++ b/examples/lpc13xx/lpc-p1343/miniblink/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2010 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = miniblink + +LDSCRIPT = ../lpc-p1343.ld + +include ../../Makefile.include + diff --git a/examples/lpc13xx/lpc-p1343/miniblink/README b/examples/lpc13xx/lpc-p1343/miniblink/README new file mode 100644 index 0000000..abf4473 --- /dev/null +++ b/examples/lpc13xx/lpc-p1343/miniblink/README @@ -0,0 +1,10 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This is the smallest-possible example program using libopencm3. + +It's intended for the NXP LPC1343-based Olimex LPC-1343 eval board (see +http://olimex.com/dev/lpc-p1343.html for details). It should blink +a LED on the board. + diff --git a/examples/lpc13xx/lpc-p1343/miniblink/miniblink.c b/examples/lpc13xx/lpc-p1343/miniblink/miniblink.c new file mode 100644 index 0000000..b5e8fbb --- /dev/null +++ b/examples/lpc13xx/lpc-p1343/miniblink/miniblink.c @@ -0,0 +1,46 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +// #include +#include + +static void gpio_setup(void) +{ + GPIO3_DIR |= (1 << 0); /* Configure P3_0 as output. */ +} + +int main(void) +{ + int i; + + gpio_setup(); + + /* Blink LED0 (P3_0) on the board. */ + while (1) { + /* Manually: */ + GPIO3_DATA |= (1 << 0); /* LED off */ + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + GPIO3_DATA &= ~(1 << 0); /* LED on */ + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/lpc17xx/Makefile.include b/examples/lpc17xx/Makefile.include new file mode 100644 index 0000000..0a29041 --- /dev/null +++ b/examples/lpc17xx/Makefile.include @@ -0,0 +1,137 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## Copyright (C) 2010 Piotr Esden-Tempski +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +PREFIX ?= arm-none-eabi +#PREFIX ?= arm-elf +CC = $(PREFIX)-gcc +LD = $(PREFIX)-gcc +OBJCOPY = $(PREFIX)-objcopy +OBJDUMP = $(PREFIX)-objdump + +TOOLCHAIN_DIR ?= ../../../.. +ifeq ($(wildcard ../../../../lib/libopencm3_lpc17xx.a),) +ifneq ($(strip $(shell which $(CC))),) +TOOLCHAIN_DIR := $(shell dirname `which $(CC)`)/../$(PREFIX) +endif +else +ifeq ($(V),1) +$(info We seem to be building the example in the source directory. Using local library!) +endif +endif + +CFLAGS += -O0 -g \ + -Wall -Wextra -Wimplicit-function-declaration \ + -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes \ + -Wundef -Wshadow \ + -I$(TOOLCHAIN_DIR)/include -fno-common \ + -mcpu=cortex-m3 -mthumb -MD -DLPC17XX +LDSCRIPT ?= $(BINARY).ld +LDFLAGS += -L$(TOOLCHAIN_DIR)/lib \ + -T$(LDSCRIPT) -nostartfiles -Wl,--gc-sections +OBJS += $(BINARY).o + +OOCD ?= openocd +OOCD_INTERFACE ?= flossjtag +OOCD_BOARD ?= olimex_stm32_h103 +# FIXME + +# Be silent per default, but 'make V=1' will show all compiler calls. +ifneq ($(V),1) +Q := @ +NULL := 2>/dev/null +else +LDFLAGS += -Wl,--print-gc-sections +endif + +.SUFFIXES: .elf .bin .hex .srec .list .images +.SECONDEXPANSION: +.SECONDARY: + +all: images + +images: $(BINARY).images +flash: $(BINARY).flash + +%.images: %.bin %.hex %.srec %.list + @#echo "*** $* images generated ***" + +%.bin: %.elf + @#printf " OBJCOPY $(*).bin\n" + $(Q)$(OBJCOPY) -Obinary $(*).elf $(*).bin + +%.hex: %.elf + @#printf " OBJCOPY $(*).hex\n" + $(Q)$(OBJCOPY) -Oihex $(*).elf $(*).hex + +%.srec: %.elf + @#printf " OBJCOPY $(*).srec\n" + $(Q)$(OBJCOPY) -Osrec $(*).elf $(*).srec + +%.list: %.elf + @#printf " OBJDUMP $(*).list\n" + $(Q)$(OBJDUMP) -S $(*).elf > $(*).list + +%.elf: $(OBJS) $(LDSCRIPT) $(TOOLCHAIN_DIR)/lib/libopencm3_lpc17xx.a + @#printf " LD $(subst $(shell pwd)/,,$(@))\n" + $(Q)$(LD) $(LDFLAGS) -o $(*).elf $(OBJS) -lopencm3_lpc17xx + +%.o: %.c Makefile + @#printf " CC $(subst $(shell pwd)/,,$(@))\n" + $(Q)$(CC) $(CFLAGS) -o $@ -c $< + +clean: + $(Q)rm -f *.o + $(Q)rm -f *.d + $(Q)rm -f *.elf + $(Q)rm -f *.bin + $(Q)rm -f *.hex + $(Q)rm -f *.srec + $(Q)rm -f *.list + +# FIXME: Replace STM32 stuff with proper LPC13XX OpenOCD support later. +ifeq ($(OOCD_SERIAL),) +%.flash: %.hex + @printf " FLASH $<\n" + @# IMPORTANT: Don't use "resume", only "reset" will work correctly! + $(Q)$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \ + -f board/$(OOCD_BOARD).cfg \ + -c "init" -c "reset init" \ + -c "stm32x mass_erase 0" \ + -c "flash write_image $(*).hex" \ + -c "reset" \ + -c "shutdown" $(NULL) +else +%.flash: %.hex + @printf " FLASH $<\n" + @# IMPORTANT: Don't use "resume", only "reset" will work correctly! + $(Q)$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \ + -f board/$(OOCD_BOARD).cfg \ + -c "ft2232_serial $(OOCD_SERIAL)" \ + -c "init" -c "reset init" \ + -c "stm32x mass_erase 0" \ + -c "flash write_image $(*).hex" \ + -c "reset" \ + -c "shutdown" $(NULL) +endif + +.PHONY: images clean + +-include $(OBJS:.o=.d) + diff --git a/examples/lpc17xx/blueboard-lpc1768-h/blueboard-lpc1768-h.ld b/examples/lpc17xx/blueboard-lpc1768-h/blueboard-lpc1768-h.ld new file mode 100644 index 0000000..e2e480f --- /dev/null +++ b/examples/lpc17xx/blueboard-lpc1768-h/blueboard-lpc1768-h.ld @@ -0,0 +1,32 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* Linker script for Blueboard-LPC1768-H (LPC1768, 512K flash, 64K SRAM). */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x00000000, LENGTH = 512K + ram (rwx) : ORIGIN = 0x10000000, LENGTH = 32K + ram1 (rwx) : ORIGIN = 0x2007C000, LENGTH = 16K + ram2 (rwx) : ORIGIN = 0x20080000, LENGTH = 16K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_lpc17xx.ld diff --git a/examples/lpc17xx/blueboard-lpc1768-h/miniblink/Makefile b/examples/lpc17xx/blueboard-lpc1768-h/miniblink/Makefile new file mode 100644 index 0000000..2c8d016 --- /dev/null +++ b/examples/lpc17xx/blueboard-lpc1768-h/miniblink/Makefile @@ -0,0 +1,24 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2010 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = miniblink + +LDSCRIPT = ../blueboard-lpc1768-h.ld + +include ../../Makefile.include diff --git a/examples/lpc17xx/blueboard-lpc1768-h/miniblink/README b/examples/lpc17xx/blueboard-lpc1768-h/miniblink/README new file mode 100644 index 0000000..7354adf --- /dev/null +++ b/examples/lpc17xx/blueboard-lpc1768-h/miniblink/README @@ -0,0 +1,9 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This is the smallest-possible example program using libopencm3. + +It's intended for the NXP LPC1768-based NGX Blueboard-LPC1768-H eval board (see +http://shop.ngxtechnologies.com/product_info.php?cPath=21&products_id=65). It should blink +a LED on the board. diff --git a/examples/lpc17xx/blueboard-lpc1768-h/miniblink/miniblink.c b/examples/lpc17xx/blueboard-lpc1768-h/miniblink/miniblink.c new file mode 100644 index 0000000..760617f --- /dev/null +++ b/examples/lpc17xx/blueboard-lpc1768-h/miniblink/miniblink.c @@ -0,0 +1,52 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include + +static void gpio_setup(void) +{ + GPIO1_DIR |= (1 << 29); /* Configure P1_29 as output. */ +} + +int main(void) +{ + int i; + + gpio_setup(); + + /* Blink LED0 (P3_0) on the board. */ + while (1) { + /* Manually: */ + //GPIO1_SET = (1 << 29); /* LED on */ + //for (i = 0; i < 800000; i++) /* Wait a bit. */ + // __asm__("nop"); + //GPIO1_CLR = (1 << 29); /* LED off */ + //for (i = 0; i < 800000; i++) /* Wait a bit. */ + // __asm__("nop"); + + gpio_set(GPIO1, GPIOPIN29); /* LED on */ + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + gpio_clear(GPIO1, GPIOPIN29); /* LED off */ + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/lpc43xx/Makefile.include b/examples/lpc43xx/Makefile.include new file mode 100644 index 0000000..10121a9 --- /dev/null +++ b/examples/lpc43xx/Makefile.include @@ -0,0 +1,140 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## Copyright (C) 2010 Piotr Esden-Tempski +## Copyright (C) 2012 Michael Ossmann +## Copyright (C) 2012 Benjamin Vernoux +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +PREFIX ?= arm-none-eabi +#PREFIX ?= arm-elf +CC = $(PREFIX)-gcc +LD = $(PREFIX)-gcc +OBJCOPY = $(PREFIX)-objcopy +OBJDUMP = $(PREFIX)-objdump +GDB = $(PREFIX)-gdb + +TOOLCHAIN_DIR ?= ../../../.. +ifeq ($(wildcard ../../../../lib/libopencm3_lpc43xx.a),) +ifneq ($(strip $(shell which $(CC))),) +TOOLCHAIN_DIR := $(shell dirname `which $(CC)`)/../$(PREFIX) +endif +else +ifeq ($(V),1) +$(info We seem to be building the example in the source directory. Using local library!) +endif +endif + +CFLAGS += -O2 -g \ + -Wall -Wextra -Wimplicit-function-declaration \ + -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes \ + -Wundef -Wshadow \ + -I$(TOOLCHAIN_DIR)/include -fno-common \ + -mcpu=cortex-m4 -mthumb -MD \ + -mfloat-abi=hard -mfpu=fpv4-sp-d16 -DLPC43XX +LDSCRIPT ?= $(BINARY).ld +LDFLAGS += -L$(TOOLCHAIN_DIR)/lib \ + -T$(LDSCRIPT) -nostartfiles -Wl,--gc-sections -Xlinker -Map=$(BINARY).map +OBJS += $(BINARY).o + +OOCD ?= openocd +OOCD_INTERFACE ?= flossjtag +OOCD_BOARD ?= olimex_stm32_h103 + +# Be silent per default, but 'make V=1' will show all compiler calls. +ifneq ($(V),1) +Q := @ +NULL := 2>/dev/null +else +LDFLAGS += -Wl,--print-gc-sections +endif + +.SUFFIXES: .elf .bin .hex .srec .list .images +.SECONDEXPANSION: +.SECONDARY: + +all: images + +images: $(BINARY).images +flash: $(BINARY).flash + +%.images: %.bin %.hex %.srec %.list + @#echo "*** $* images generated ***" + +%.bin: %.elf + @#printf " OBJCOPY $(*).bin\n" + $(Q)$(OBJCOPY) -Obinary $(*).elf $(*).bin + +%.hex: %.elf + @#printf " OBJCOPY $(*).hex\n" + $(Q)$(OBJCOPY) -Oihex $(*).elf $(*).hex + +%.srec: %.elf + @#printf " OBJCOPY $(*).srec\n" + $(Q)$(OBJCOPY) -Osrec $(*).elf $(*).srec + +%.list: %.elf + @#printf " OBJDUMP $(*).list\n" + $(Q)$(OBJDUMP) -S $(*).elf > $(*).list + +%.elf: $(OBJS) $(LDSCRIPT) $(TOOLCHAIN_DIR)/lib/libopencm3_lpc43xx.a + @#printf " LD $(subst $(shell pwd)/,,$(@))\n" + $(Q)$(LD) $(LDFLAGS) -o $(*).elf $(OBJS) -lopencm3_lpc43xx + +%.o: %.c Makefile + @#printf " CC $(subst $(shell pwd)/,,$(@))\n" + $(Q)$(CC) $(CFLAGS) -o $@ -c $< + +clean: + $(Q)rm -f *.o + $(Q)rm -f *.d + $(Q)rm -f *.elf + $(Q)rm -f *.bin + $(Q)rm -f *.hex + $(Q)rm -f *.srec + $(Q)rm -f *.list + $(Q)rm -f *.map + +# FIXME: Replace STM32 stuff with proper LPC43XX OpenOCD support later. +ifeq ($(OOCD_SERIAL),) +%.flash: %.hex + @printf " FLASH $<\n" + @# IMPORTANT: Don't use "resume", only "reset" will work correctly! + $(Q)$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \ + -f board/$(OOCD_BOARD).cfg \ + -c "init" -c "reset init" \ + -c "stm32x mass_erase 0" \ + -c "flash write_image $(*).hex" \ + -c "reset" \ + -c "shutdown" $(NULL) +else +%.flash: %.hex + @printf " FLASH $<\n" + @# IMPORTANT: Don't use "resume", only "reset" will work correctly! + $(Q)$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \ + -f board/$(OOCD_BOARD).cfg \ + -c "ft2232_serial $(OOCD_SERIAL)" \ + -c "init" -c "reset init" \ + -c "stm32x mass_erase 0" \ + -c "flash write_image $(*).hex" \ + -c "reset" \ + -c "shutdown" $(NULL) +endif + +.PHONY: images clean + +-include $(OBJS:.o=.d) diff --git a/examples/lpc43xx/diolan-lpc-4350-db1/README b/examples/lpc43xx/diolan-lpc-4350-db1/README new file mode 100644 index 0000000..bff2388 --- /dev/null +++ b/examples/lpc43xx/diolan-lpc-4350-db1/README @@ -0,0 +1,3 @@ +These example programs are written for the Diolan LPC-4350-DB1: + +http://www.diolan.com/lpc4350-features.html diff --git a/examples/lpc43xx/diolan-lpc-4350-db1/diolan-lpc-4350-db1.ld b/examples/lpc43xx/diolan-lpc-4350-db1/diolan-lpc-4350-db1.ld new file mode 100644 index 0000000..92c25af --- /dev/null +++ b/examples/lpc43xx/diolan-lpc-4350-db1/diolan-lpc-4350-db1.ld @@ -0,0 +1,32 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* Linker script for HackRF Jellybean (LPC4330, 1M SPI flash, 64K SRAM). */ + +/* Define memory regions. */ +MEMORY +{ + /* rom is really the shadow region that points to SPI flash or elsewhere */ + rom (rx) : ORIGIN = 0x00000000, LENGTH = 1M + ram (rwx) : ORIGIN = 0x10000000, LENGTH = 128K + /* there are some additional RAM regions */ +} + +/* Include the common ld script. */ +INCLUDE libopencm3_lpc43xx.ld diff --git a/examples/lpc43xx/diolan-lpc-4350-db1/miniblink/Makefile b/examples/lpc43xx/diolan-lpc-4350-db1/miniblink/Makefile new file mode 100644 index 0000000..bf0ca91 --- /dev/null +++ b/examples/lpc43xx/diolan-lpc-4350-db1/miniblink/Makefile @@ -0,0 +1,24 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2010 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = miniblink + +LDSCRIPT = ../diolan-lpc-4350-db1.ld + +include ../../Makefile.include diff --git a/examples/lpc43xx/diolan-lpc-4350-db1/miniblink/README b/examples/lpc43xx/diolan-lpc-4350-db1/miniblink/README new file mode 100644 index 0000000..009b9a9 --- /dev/null +++ b/examples/lpc43xx/diolan-lpc-4350-db1/miniblink/README @@ -0,0 +1,11 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This is the smallest-possible example program using libopencm3. + +It's intended for the Diolan LPC-4350-DB1: + +http://www.diolan.com/lpc4350-features.html + +It should blink D2 on the board. diff --git a/examples/lpc43xx/diolan-lpc-4350-db1/miniblink/miniblink.c b/examples/lpc43xx/diolan-lpc-4350-db1/miniblink/miniblink.c new file mode 100644 index 0000000..20ecaf1 --- /dev/null +++ b/examples/lpc43xx/diolan-lpc-4350-db1/miniblink/miniblink.c @@ -0,0 +1,46 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Uwe Hermann + * Copyright (C) 2012 Michael Ossmann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include + +static void gpio_setup(void) +{ + GPIO0_DIR |= (1 << 5); /* Configure GPIO0[5] (P6_6) as output. */ +} + +int main(void) +{ + int i; + + gpio_setup(); + + /* Blink D2 on the board. */ + while (1) { + + gpio_set(GPIO0, GPIOPIN5); /* LED on */ + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + gpio_clear(GPIO0, GPIOPIN5); /* LED off */ + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/lpc43xx/hackrf-jellybean/README b/examples/lpc43xx/hackrf-jellybean/README new file mode 100644 index 0000000..07aaeee --- /dev/null +++ b/examples/lpc43xx/hackrf-jellybean/README @@ -0,0 +1,4 @@ +These example programs are written for the Jellybean development board from the +HackRF project: + +https://github.com/mossmann/hackrf diff --git a/examples/lpc43xx/hackrf-jellybean/i2c/Makefile b/examples/lpc43xx/hackrf-jellybean/i2c/Makefile new file mode 100644 index 0000000..b18f122 --- /dev/null +++ b/examples/lpc43xx/hackrf-jellybean/i2c/Makefile @@ -0,0 +1,24 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2010 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = i2cdemo + +LDSCRIPT = ../jellybean-lpc4330.ld + +include ../../Makefile.include diff --git a/examples/lpc43xx/hackrf-jellybean/i2c/README b/examples/lpc43xx/hackrf-jellybean/i2c/README new file mode 100644 index 0000000..86763fe --- /dev/null +++ b/examples/lpc43xx/hackrf-jellybean/i2c/README @@ -0,0 +1,15 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This program exercises the I2C peripheral on Jellybean's LPC43xx. You can +scope SCL on P6 pin 3 and SDA on P6 pin 5. If Lemondrop is connected, LED1 +will illuminate if I2C communication to the Si5351C on Lemondrop is successful. + +Required Lemondrop -> Jellybean connections: + +SCL: Lemondrop P7 pin 3 -> Jellybean P6 pin 3 +SDA: Lemondrop P7 pin 5 -> Jellybean P6 pin 5 +VCC: Lemondrop P4 pin 2, 4, or 6 -> Jellybean P17 pin 2, 4, or 6 +1V8: Lemondrop P11 pin 2, 4, or 6 -> Jellybean P16 pin 2, 4, or 6 +GND: Lemondrop P5 -> Jellybean P13 diff --git a/examples/lpc43xx/hackrf-jellybean/i2c/i2cdemo.c b/examples/lpc43xx/hackrf-jellybean/i2c/i2cdemo.c new file mode 100644 index 0000000..fd244be --- /dev/null +++ b/examples/lpc43xx/hackrf-jellybean/i2c/i2cdemo.c @@ -0,0 +1,108 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Uwe Hermann + * Copyright (C) 2012 Michael Ossmann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include + +#include "../jellybean_conf.h" + +static void gpio_setup(void) +{ + /* Configure SCU Pin Mux as GPIO */ + scu_pinmux(SCU_PINMUX_LED1, SCU_GPIO_FAST); + scu_pinmux(SCU_PINMUX_LED2, SCU_GPIO_FAST); + scu_pinmux(SCU_PINMUX_LED3, SCU_GPIO_FAST); + + scu_pinmux(SCU_PINMUX_EN1V8, SCU_GPIO_FAST); + + scu_pinmux(SCU_PINMUX_BOOT0, SCU_GPIO_FAST); + scu_pinmux(SCU_PINMUX_BOOT1, SCU_GPIO_FAST); + scu_pinmux(SCU_PINMUX_BOOT2, SCU_GPIO_FAST); + scu_pinmux(SCU_PINMUX_BOOT3, SCU_GPIO_FAST); + + /* Configure all GPIO as Input (safe state) */ + GPIO0_DIR = 0; + GPIO1_DIR = 0; + GPIO2_DIR = 0; + GPIO3_DIR = 0; + GPIO4_DIR = 0; + GPIO5_DIR = 0; + GPIO6_DIR = 0; + GPIO7_DIR = 0; + + /* Configure GPIO as Output */ + GPIO2_DIR |= (PIN_LED1|PIN_LED2|PIN_LED3); /* Configure GPIO2[1/2/8] (P4_1/2 P6_12) as output. */ + GPIO3_DIR |= PIN_EN1V8; /* GPIO3[6] on P6_10 as output. */ +} + +#define SI5351C_I2C_ADDR (0x60 << 1) + +/* write to single register */ +/* Not used! +static void si5351c_write_reg(uint8_t reg, uint8_t val) +{ + i2c0_tx_start(); + i2c0_tx_byte(SI5351C_I2C_ADDR | I2C_WRITE); + i2c0_tx_byte(reg); + i2c0_tx_byte(val); + i2c0_stop(); +} +*/ + +/* read single register */ +static uint8_t si5351c_read_reg(uint8_t reg) +{ + uint8_t val; + + /* set register address with write */ + i2c0_tx_start(); + i2c0_tx_byte(SI5351C_I2C_ADDR | I2C_WRITE); + i2c0_tx_byte(reg); + + /* read the value */ + i2c0_tx_start(); + i2c0_tx_byte(SI5351C_I2C_ADDR | I2C_READ); + val = i2c0_rx_byte(); + i2c0_stop(); + + return val; +} + +int main(void) +{ + int i; + + gpio_setup(); + i2c0_init(); + + gpio_set(PORT_EN1V8, PIN_EN1V8); /* 1V8 on */ + + while (1) { + if (si5351c_read_reg(0) == 0x10) + gpio_set(GPIO2, GPIOPIN1); /* LED on */ + else + gpio_clear(GPIO2, GPIOPIN1); /* LED off */ + + for (i = 0; i < 1000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/lpc43xx/hackrf-jellybean/jellybean-lpc4330.ld b/examples/lpc43xx/hackrf-jellybean/jellybean-lpc4330.ld new file mode 100644 index 0000000..29e5700 --- /dev/null +++ b/examples/lpc43xx/hackrf-jellybean/jellybean-lpc4330.ld @@ -0,0 +1,32 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* Linker script for HackRF Jellybean (LPC4330, 1M SPI flash, 264K SRAM). */ + +/* Define memory regions. */ +MEMORY +{ + /* rom is really the shadow region that points to SPI flash or elsewhere */ + rom (rx) : ORIGIN = 0x00000000, LENGTH = 1M + ram (rwx) : ORIGIN = 0x10000000, LENGTH = 128K + /* there are some additional RAM regions */ +} + +/* Include the common ld script. */ +INCLUDE libopencm3_lpc43xx.ld diff --git a/examples/lpc43xx/hackrf-jellybean/jellybean-lpc4330_rom_to_ram.ld b/examples/lpc43xx/hackrf-jellybean/jellybean-lpc4330_rom_to_ram.ld new file mode 100644 index 0000000..fb3d8f6 --- /dev/null +++ b/examples/lpc43xx/hackrf-jellybean/jellybean-lpc4330_rom_to_ram.ld @@ -0,0 +1,36 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Uwe Hermann + * Copyright (C) 2012 Benjamin Vernoux + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* Linker script for HackRF Jellybean (LPC4330, 1M SPI flash, 264K SRAM). */ + +/* Define memory regions. */ +MEMORY +{ + /* Physical address in Flash used to copy Code from Flash to RAM */ + rom_flash (rx) : ORIGIN = 0x80000000, LENGTH = 1M + /* rom is really the shadow region that points to SPI flash or elsewhere */ + rom (rx) : ORIGIN = 0x00000000, LENGTH = 1M + ram (rwx) : ORIGIN = 0x10000000, LENGTH = 128K + /* there are some additional RAM regions for data */ + ram_data (rw) : ORIGIN = 0x10080000, LENGTH = 72K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_lpc43xx_rom_to_ram.ld diff --git a/examples/lpc43xx/hackrf-jellybean/jellybean_conf.h b/examples/lpc43xx/hackrf-jellybean/jellybean_conf.h new file mode 100644 index 0000000..dc791b3 --- /dev/null +++ b/examples/lpc43xx/hackrf-jellybean/jellybean_conf.h @@ -0,0 +1,85 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2012 Benjamin Vernoux + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#ifndef __JELLYBEAN_CONF_H +#define __JELLYBEAN_CONF_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include + +/* + * JellyBean SCU PinMux + */ + +/* GPIO Output PinMux */ +#define SCU_PINMUX_LED1 (P4_1) /* GPIO2[1] on P4_1 */ +#define SCU_PINMUX_LED2 (P4_2) /* GPIO2[2] on P4_2 */ +#define SCU_PINMUX_LED3 (P6_12) /* GPIO2[8] on P6_12 */ + +#define SCU_PINMUX_EN1V8 (P6_10) /* GPIO3[6] on P6_10 */ + +/* GPIO Input PinMux */ +#define SCU_PINMUX_BOOT0 (P1_1) /* GPIO0[8] on P1_1 */ +#define SCU_PINMUX_BOOT1 (P1_2) /* GPIO0[9] on P1_2 */ +#define SCU_PINMUX_BOOT2 (P2_8) /* GPIO5[7] on P2_8 */ +#define SCU_PINMUX_BOOT3 (P2_9) /* GPIO1[10] on P2_9 */ + +/* SSP1 Peripheral PinMux */ +#define SCU_SSP1_MISO (P1_3) /* P1_3 */ +#define SCU_SSP1_MOSI (P1_4) /* P1_4 */ +#define SCU_SSP1_SCK (P1_19) /* P1_19 */ +#define SCU_SSP1_SSEL (P1_20) /* P1_20 */ + +/* TODO add other Pins */ + +/* + * JellyBean GPIO Pin + */ +/* GPIO Output */ +#define PIN_LED1 (BIT1) /* GPIO2[1] on P4_1 */ +#define PIN_LED2 (BIT2) /* GPIO2[2] on P4_2 */ +#define PIN_LED3 (BIT8) /* GPIO2[8] on P6_12 */ +#define PORT_LED1_3 (GPIO2) /* PORT for LED1, 2 & 3 */ + +#define PIN_EN1V8 (BIT6) /* GPIO3[6] on P6_10 */ +#define PORT_EN1V8 (GPIO3) + +/* GPIO Input */ +#define PIN_BOOT0 (BIT8) /* GPIO0[8] on P1_1 */ +#define PIN_BOOT1 (BIT9) /* GPIO0[9] on P1_2 */ +#define PIN_BOOT2 (BIT7) /* GPIO5[7] on P2_8 */ +#define PIN_BOOT3 (BIT10) /* GPIO1[10] on P2_9 */ + +/* Read GPIO Pin */ +#define BOOT0_STATE ( (GPIO0_PIN & PIN_BOOT0)==PIN_BOOT0 ) +#define BOOT1_STATE ( (GPIO0_PIN & PIN_BOOT1)==PIN_BOOT1 ) +#define BOOT2_STATE ( (GPIO5_PIN & PIN_BOOT2)==PIN_BOOT2 ) +#define BOOT3_STATE ( (GPIO1_PIN & PIN_BOOT3)==PIN_BOOT3 ) + +/* TODO add other Pins */ + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/examples/lpc43xx/hackrf-jellybean/miniblink/Makefile b/examples/lpc43xx/hackrf-jellybean/miniblink/Makefile new file mode 100644 index 0000000..32da7ff --- /dev/null +++ b/examples/lpc43xx/hackrf-jellybean/miniblink/Makefile @@ -0,0 +1,24 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2010 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = miniblink + +LDSCRIPT = ../jellybean-lpc4330.ld + +include ../../Makefile.include diff --git a/examples/lpc43xx/hackrf-jellybean/miniblink/README b/examples/lpc43xx/hackrf-jellybean/miniblink/README new file mode 100644 index 0000000..556ed92 --- /dev/null +++ b/examples/lpc43xx/hackrf-jellybean/miniblink/README @@ -0,0 +1,11 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This is the smallest-possible example program using libopencm3. + +It's intended for the Jellybean development board from the HackRF project: + +https://github.com/mossmann/hackrf + +It should blink LED1 on the board. diff --git a/examples/lpc43xx/hackrf-jellybean/miniblink/miniblink.c b/examples/lpc43xx/hackrf-jellybean/miniblink/miniblink.c new file mode 100644 index 0000000..9603391 --- /dev/null +++ b/examples/lpc43xx/hackrf-jellybean/miniblink/miniblink.c @@ -0,0 +1,82 @@ +/* +* This file is part of the libopencm3 project. +* +* Copyright (C) 2010 Uwe Hermann +* Copyright (C) 2012 Michael Ossmann +* +* This library is free software: you can redistribute it and/or modify +* it under the terms of the GNU Lesser General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public License +* along with this library. If not, see . +*/ + +#include +#include + +#include "../jellybean_conf.h" + +static void gpio_setup(void) +{ + /* Configure SCU Pin Mux as GPIO */ + scu_pinmux(SCU_PINMUX_LED1, SCU_GPIO_FAST); + scu_pinmux(SCU_PINMUX_LED2, SCU_GPIO_FAST); + scu_pinmux(SCU_PINMUX_LED3, SCU_GPIO_FAST); + + scu_pinmux(SCU_PINMUX_EN1V8, SCU_GPIO_FAST); + + scu_pinmux(SCU_PINMUX_BOOT0, SCU_GPIO_FAST); + scu_pinmux(SCU_PINMUX_BOOT1, SCU_GPIO_FAST); + scu_pinmux(SCU_PINMUX_BOOT2, SCU_GPIO_FAST); + scu_pinmux(SCU_PINMUX_BOOT3, SCU_GPIO_FAST); + + /* Configure all GPIO as Input (safe state) */ + GPIO0_DIR = 0; + GPIO1_DIR = 0; + GPIO2_DIR = 0; + GPIO3_DIR = 0; + GPIO4_DIR = 0; + GPIO5_DIR = 0; + GPIO6_DIR = 0; + GPIO7_DIR = 0; + + /* Configure GPIO as Output */ + GPIO2_DIR |= (PIN_LED1|PIN_LED2|PIN_LED3); /* Configure GPIO2[1/2/8] (P4_1/2 P6_12) as output. */ + GPIO3_DIR |= PIN_EN1V8; /* GPIO3[6] on P6_10 as output. */ +} + +u32 boot0, boot1, boot2, boot3; + +int main(void) +{ + int i; + gpio_setup(); + + /* Set 1V8 */ + gpio_set(PORT_EN1V8, PIN_EN1V8); + + /* Blink LED1/2/3 on the board and Read BOOT0/1/2/3 pins. */ + while (1) + { + boot0 = BOOT0_STATE; + boot1 = BOOT1_STATE; + boot2 = BOOT2_STATE; + boot3 = BOOT3_STATE; + + gpio_set(PORT_LED1_3, (PIN_LED1|PIN_LED2|PIN_LED3)); /* LEDs on */ + for (i = 0; i < 2000000; i++) /* Wait a bit. */ + __asm__("nop"); + gpio_clear(PORT_LED1_3, (PIN_LED1|PIN_LED2|PIN_LED3)); /* LED off */ + for (i = 0; i < 2000000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/lpc43xx/hackrf-jellybean/miniblink_rom_to_ram/Makefile b/examples/lpc43xx/hackrf-jellybean/miniblink_rom_to_ram/Makefile new file mode 100644 index 0000000..56cb540 --- /dev/null +++ b/examples/lpc43xx/hackrf-jellybean/miniblink_rom_to_ram/Makefile @@ -0,0 +1,24 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2010 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = miniblink + +LDSCRIPT = ../jellybean-lpc4330_rom_to_ram.ld + +include ../../Makefile.include diff --git a/examples/lpc43xx/hackrf-jellybean/miniblink_rom_to_ram/README b/examples/lpc43xx/hackrf-jellybean/miniblink_rom_to_ram/README new file mode 100644 index 0000000..02960fa --- /dev/null +++ b/examples/lpc43xx/hackrf-jellybean/miniblink_rom_to_ram/README @@ -0,0 +1,12 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This is the smallest-possible example program using libopencm3. + +It's intended for the Jellybean development board from the HackRF project: + +https://github.com/mossmann/hackrf + +It should blink LED1 on the board. +This example copy the Code from ROM to RAM and execute code from RAM. diff --git a/examples/lpc43xx/hackrf-jellybean/miniblink_rom_to_ram/miniblink.c b/examples/lpc43xx/hackrf-jellybean/miniblink_rom_to_ram/miniblink.c new file mode 100644 index 0000000..9603391 --- /dev/null +++ b/examples/lpc43xx/hackrf-jellybean/miniblink_rom_to_ram/miniblink.c @@ -0,0 +1,82 @@ +/* +* This file is part of the libopencm3 project. +* +* Copyright (C) 2010 Uwe Hermann +* Copyright (C) 2012 Michael Ossmann +* +* This library is free software: you can redistribute it and/or modify +* it under the terms of the GNU Lesser General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public License +* along with this library. If not, see . +*/ + +#include +#include + +#include "../jellybean_conf.h" + +static void gpio_setup(void) +{ + /* Configure SCU Pin Mux as GPIO */ + scu_pinmux(SCU_PINMUX_LED1, SCU_GPIO_FAST); + scu_pinmux(SCU_PINMUX_LED2, SCU_GPIO_FAST); + scu_pinmux(SCU_PINMUX_LED3, SCU_GPIO_FAST); + + scu_pinmux(SCU_PINMUX_EN1V8, SCU_GPIO_FAST); + + scu_pinmux(SCU_PINMUX_BOOT0, SCU_GPIO_FAST); + scu_pinmux(SCU_PINMUX_BOOT1, SCU_GPIO_FAST); + scu_pinmux(SCU_PINMUX_BOOT2, SCU_GPIO_FAST); + scu_pinmux(SCU_PINMUX_BOOT3, SCU_GPIO_FAST); + + /* Configure all GPIO as Input (safe state) */ + GPIO0_DIR = 0; + GPIO1_DIR = 0; + GPIO2_DIR = 0; + GPIO3_DIR = 0; + GPIO4_DIR = 0; + GPIO5_DIR = 0; + GPIO6_DIR = 0; + GPIO7_DIR = 0; + + /* Configure GPIO as Output */ + GPIO2_DIR |= (PIN_LED1|PIN_LED2|PIN_LED3); /* Configure GPIO2[1/2/8] (P4_1/2 P6_12) as output. */ + GPIO3_DIR |= PIN_EN1V8; /* GPIO3[6] on P6_10 as output. */ +} + +u32 boot0, boot1, boot2, boot3; + +int main(void) +{ + int i; + gpio_setup(); + + /* Set 1V8 */ + gpio_set(PORT_EN1V8, PIN_EN1V8); + + /* Blink LED1/2/3 on the board and Read BOOT0/1/2/3 pins. */ + while (1) + { + boot0 = BOOT0_STATE; + boot1 = BOOT1_STATE; + boot2 = BOOT2_STATE; + boot3 = BOOT3_STATE; + + gpio_set(PORT_LED1_3, (PIN_LED1|PIN_LED2|PIN_LED3)); /* LEDs on */ + for (i = 0; i < 2000000; i++) /* Wait a bit. */ + __asm__("nop"); + gpio_clear(PORT_LED1_3, (PIN_LED1|PIN_LED2|PIN_LED3)); /* LED off */ + for (i = 0; i < 2000000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/lpc43xx/hackrf-jellybean/ssp/Makefile b/examples/lpc43xx/hackrf-jellybean/ssp/Makefile new file mode 100644 index 0000000..8a3b1cc --- /dev/null +++ b/examples/lpc43xx/hackrf-jellybean/ssp/Makefile @@ -0,0 +1,24 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2010 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = sspdemo + +LDSCRIPT = ../jellybean-lpc4330.ld + +include ../../Makefile.include diff --git a/examples/lpc43xx/hackrf-jellybean/ssp/README b/examples/lpc43xx/hackrf-jellybean/ssp/README new file mode 100644 index 0000000..5354a53 --- /dev/null +++ b/examples/lpc43xx/hackrf-jellybean/ssp/README @@ -0,0 +1,48 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This program exercises the SSP1 peripheral on Jellybean's LPC43xx. + + Jellybean (connector) + P9 SPI + |-----------------| + | Pin2 Pin4 Pin6 | +||------| | +|| Pin1 |Pin3 Pin5 | +||------|----------| +|-------| + +SSP1_MISO: Jellybean P9 SPI Pin6 +SSP1_MOSI: Jellybean P9 SPI Pin4 +SSP1_SCK: Jellybean P9 SPI Pin2 +SSP1_SSEL: Jellybean P9 SPI Pin3 +GND: Can be connected to P12 SD Pin1 + +PCLK clock source is PLL1 288MHz (from IRC 96MHz boot from SPIFI) +Freq = PCLK / (CPSDVSR * [SCR+1]). + +By default (CPSDVSR=0 => Means MAX Divisor) +SSP1->CR0->SCR = 0x00 => CLK Freq 1.126MHz +SSP1->CR0->SCR = 0x01 => MOSI Freq 566.9KHz +... + +Test Oscilloscpe: +SCR=0, CPSDVSR=32 => CLK 9.025MHz +SCR=1, CPSDVSR=2 => CLK 73MHz +SCR=2, CPSDVSR=2 => CLK 49MHz +SCR=4, CPSDVSR=2 => CLK 29MHz +SCR=8, CPSDVSR=2 => CLK 16MHz +SCR=16, CPSDVSR=2 => CLK 8.5MHz +SCR=32, CPSDVSR=2 => CLK 4.386MHz +SCR=64, CPSDVSR=2 => CLK 2.227MHz +SCR=1, CPSDVSR=64 => CLK 2.262MHz + +Theory: +SCR=0, CPSDVSR=32 => 288MHz / (32*(0+1) = 9MHz +SCR=1, CPSDVSR=2 => 288MHz / (2*(1+1) = 72MHz +SCR=4, CPSDVSR=2 => 288MHz / (2*(4+1) = 28.8MHz +SCR=32, CPSDVSR=2 => 288MHz / (2*(32+1) = 4.364MHz +SCR=64, CPSDVSR=2 => 288MHz / (2*(64+1)) = 2.2154MHz +SCR=128, CPSDVSR=2 => 288MHz / (2*(128+1)) = 1.116MHz +SCR=1, CPSDVSR=64 => 288MHz / (64*(1+1)) = 2.25MHz diff --git a/examples/lpc43xx/hackrf-jellybean/ssp/sspdemo.c b/examples/lpc43xx/hackrf-jellybean/ssp/sspdemo.c new file mode 100644 index 0000000..65399c8 --- /dev/null +++ b/examples/lpc43xx/hackrf-jellybean/ssp/sspdemo.c @@ -0,0 +1,102 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2012 Benjamin Vernoux + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include + +#include "../jellybean_conf.h" + +static void gpio_setup(void) +{ + /* Configure all GPIO as Input (safe state) */ + GPIO0_DIR = 0; + GPIO1_DIR = 0; + GPIO2_DIR = 0; + GPIO3_DIR = 0; + GPIO4_DIR = 0; + GPIO5_DIR = 0; + GPIO6_DIR = 0; + GPIO7_DIR = 0; + + /* Configure SCU Pin Mux as GPIO */ + scu_pinmux(SCU_PINMUX_LED1, SCU_GPIO_FAST); + scu_pinmux(SCU_PINMUX_LED2, SCU_GPIO_FAST); + scu_pinmux(SCU_PINMUX_LED3, SCU_GPIO_FAST); + + scu_pinmux(SCU_PINMUX_EN1V8, SCU_GPIO_FAST); + + scu_pinmux(SCU_PINMUX_BOOT0, SCU_GPIO_FAST); + scu_pinmux(SCU_PINMUX_BOOT1, SCU_GPIO_FAST); + scu_pinmux(SCU_PINMUX_BOOT2, SCU_GPIO_FAST); + scu_pinmux(SCU_PINMUX_BOOT3, SCU_GPIO_FAST); + + /* Configure SSP1 Peripheral (to be moved later in SSP driver) */ + scu_pinmux(SCU_SSP1_MISO, (SCU_SSP_IO | SCU_CONF_FUNCTION5)); + scu_pinmux(SCU_SSP1_MOSI, (SCU_SSP_IO | SCU_CONF_FUNCTION5)); + scu_pinmux(SCU_SSP1_SCK, (SCU_SSP_IO | SCU_CONF_FUNCTION1)); + scu_pinmux(SCU_SSP1_SSEL, (SCU_SSP_IO | SCU_CONF_FUNCTION1)); + + /* Configure GPIO as Output */ + GPIO2_DIR |= (PIN_LED1|PIN_LED2|PIN_LED3); /* Configure GPIO2[1/2/8] (P4_1/2 P6_12) as output. */ + GPIO3_DIR |= PIN_EN1V8; /* GPIO3[6] on P6_10 as output. */ +} + +int main(void) +{ + int i; + u8 ssp_val; + u8 serial_clock_rate; + u8 clock_prescale_rate; + + gpio_setup(); + + /* Freq About 1.12MHz => Freq = PCLK / (CPSDVSR * [SCR+1]) with PCLK=PLL1=288MHz */ + clock_prescale_rate = 2; + serial_clock_rate = 128; + + ssp_init(SSP1_NUM, + SSP_DATA_8BITS, + SSP_FRAME_SPI, + SSP_CPOL_0_CPHA_0, + serial_clock_rate, + clock_prescale_rate, + SSP_MODE_NORMAL, + SSP_MASTER, + SSP_SLAVE_OUT_ENABLE); + + ssp_val = 0x0; + + while (1) { + + ssp_write(SSP1_NUM, (u16)ssp_val); + + gpio_set(GPIO2, GPIOPIN1); /* LED on */ + + for (i = 0; i < 1000; i++) /* Wait a bit. */ + __asm__("nop"); + + gpio_clear(GPIO2, GPIOPIN1); /* LED off */ + + ssp_val++; + } + + return 0; +} diff --git a/examples/lpc43xx/hackrf-jellybean/systick/Makefile b/examples/lpc43xx/hackrf-jellybean/systick/Makefile new file mode 100644 index 0000000..93b471e --- /dev/null +++ b/examples/lpc43xx/hackrf-jellybean/systick/Makefile @@ -0,0 +1,24 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2012 Benjamin Vernoux +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = systickdemo + +LDSCRIPT = ../jellybean-lpc4330.ld + +include ../../Makefile.include diff --git a/examples/lpc43xx/hackrf-jellybean/systick/README b/examples/lpc43xx/hackrf-jellybean/systick/README new file mode 100644 index 0000000..8c32cdc --- /dev/null +++ b/examples/lpc43xx/hackrf-jellybean/systick/README @@ -0,0 +1,8 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This program exercises the SysTick Interrupt of ARM CortexM4 on Jellybean's LPC43xx. +It also enable Cycle Counter to be used for accurate delay independant from Clock Frequency. +The Demo Use Cycle Counter and SysTick Interrupt to compute number of cycles executed per second. +The result is LED1/2 & 3 Blink with an accurate 1s Period (using SysTick) (Checked visualy and with Oscilloscope). diff --git a/examples/lpc43xx/hackrf-jellybean/systick/systickdemo.c b/examples/lpc43xx/hackrf-jellybean/systick/systickdemo.c new file mode 100644 index 0000000..d307645 --- /dev/null +++ b/examples/lpc43xx/hackrf-jellybean/systick/systickdemo.c @@ -0,0 +1,184 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2012 Benjamin Vernoux + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include + +#include "../jellybean_conf.h" + +/* Global counter incremented by SysTick Interrupt each millisecond */ +volatile u32 g_ulSysTickCount; +u32 g_NbCyclePerSecond; + +static void gpio_setup(void) +{ + /* Configure all GPIO as Input (safe state) */ + GPIO0_DIR = 0; + GPIO1_DIR = 0; + GPIO2_DIR = 0; + GPIO3_DIR = 0; + GPIO4_DIR = 0; + GPIO5_DIR = 0; + GPIO6_DIR = 0; + GPIO7_DIR = 0; + + /* Configure SCU Pin Mux as GPIO */ + scu_pinmux(SCU_PINMUX_LED1, SCU_GPIO_FAST); + scu_pinmux(SCU_PINMUX_LED2, SCU_GPIO_FAST); + scu_pinmux(SCU_PINMUX_LED3, SCU_GPIO_FAST); + + scu_pinmux(SCU_PINMUX_EN1V8, SCU_GPIO_FAST); + + scu_pinmux(SCU_PINMUX_BOOT0, SCU_GPIO_FAST); + scu_pinmux(SCU_PINMUX_BOOT1, SCU_GPIO_FAST); + scu_pinmux(SCU_PINMUX_BOOT2, SCU_GPIO_FAST); + scu_pinmux(SCU_PINMUX_BOOT3, SCU_GPIO_FAST); + + /* Configure SSP1 Peripheral (to be moved later in SSP driver) */ + scu_pinmux(SCU_SSP1_MISO, (SCU_SSP_IO | SCU_CONF_FUNCTION5)); + scu_pinmux(SCU_SSP1_MOSI, (SCU_SSP_IO | SCU_CONF_FUNCTION5)); + scu_pinmux(SCU_SSP1_SCK, (SCU_SSP_IO | SCU_CONF_FUNCTION1)); + scu_pinmux(SCU_SSP1_SSEL, (SCU_SSP_IO | SCU_CONF_FUNCTION1)); + + /* Configure GPIO as Output */ + GPIO2_DIR |= (PIN_LED1|PIN_LED2|PIN_LED3); /* Configure GPIO2[1/2/8] (P4_1/2 P6_12) as output. */ + GPIO3_DIR |= PIN_EN1V8; /* GPIO3[6] on P6_10 as output. */ +} + +static void systick_setup(void) +{ + u32 systick_reload_val; + g_ulSysTickCount = 0; + + /* Disable IRQ globally */ + asm volatile ("cpsid i"); + + /* Set processor Clock as Source Clock */ + systick_set_clocksource(STK_CTRL_CLKSOURCE); + + /* Get SysTick calibration value to obtain by default 1 tick = 10ms */ + systick_reload_val = systick_get_calib(); + /* + * Calibration seems wrong on LPC43xx(TBC) for default Freq it assume System Clock is 12MHz but it is 12*8=96MHz + * Fix the Calibration value bu multiplication by 8 + */ + systick_reload_val = (systick_reload_val*8); + + /* To obtain 1ms per tick just divide by 10 the 10ms base tick and set the reload */ + systick_reload_val = systick_reload_val/10; + systick_set_reload(systick_reload_val); + + systick_interrupt_enable(); + + /* Start counting. */ + systick_counter_enable(); + + /* Set SysTick Priority to maximum */ + nvic_set_priority(NVIC_SYSTICK_IRQ, 0xFF); + + /* Enable IRQ globally */ + asm volatile ("cpsie i"); +} + +static void scs_dwt_cycle_counter_enabled(void) +{ + SCS_DEMCR |= SCS_DEMCR_TRCENA; + SCS_DWT_CTRL |= SCS_DWT_CTRL_CYCCNTENA; +} + +static u32 sys_tick_get_time_ms(void) +{ + return g_ulSysTickCount; +} + +static u32 sys_tick_delta_time_ms(u32 start, u32 end) +{ + #define MAX_T_U32 ((2^32)-1) + u32 diff; + + if(end > start) + { + diff=end-start; + }else + { + diff=MAX_T_U32-(start-end)+1; + } + + return diff; +} + +static void sys_tick_wait_time_ms(u32 wait_ms) +{ + u32 start, end; + u32 tickms; + + start = sys_tick_get_time_ms(); + + do + { + end = sys_tick_get_time_ms(); + tickms = sys_tick_delta_time_ms(start, end); + }while(tickms < wait_ms); +} + +/* Called each 1ms/1000Hz by interrupt + 1) Count the number of cycle per second. + 2) Increment g_ulSysTickCount counter. +*/ +void sys_tick_handler(void) +{ + if(g_ulSysTickCount==0) + { + /* Clear Cycle Counter*/ + SCS_DWT_CYCCNT = 0; + }else if(g_ulSysTickCount==1000) + { + /* Capture number of cycle elapsed during 1 second */ + g_NbCyclePerSecond = SCS_DWT_CYCCNT; + } + + g_ulSysTickCount++; +} + +int main(void) +{ + systick_setup(); + + gpio_setup(); + + /* SCS & Cycle Counter enabled (used to count number of cycles executed per second see g_NbCyclePerSecond */ + scs_dwt_cycle_counter_enabled(); + + while (1) + { + gpio_set(PORT_LED1_3, (PIN_LED1|PIN_LED2|PIN_LED3)); /* LEDs on */ + + sys_tick_wait_time_ms(500); + + gpio_clear(PORT_LED1_3, (PIN_LED1|PIN_LED2|PIN_LED3)); /* LED off */ + + sys_tick_wait_time_ms(500); + } + + return 0; +} diff --git a/examples/stm32/f1/Makefile.include b/examples/stm32/f1/Makefile.include new file mode 100644 index 0000000..faf7899 --- /dev/null +++ b/examples/stm32/f1/Makefile.include @@ -0,0 +1,172 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## Copyright (C) 2010 Piotr Esden-Tempski +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +PREFIX ?= arm-none-eabi +#PREFIX ?= arm-elf +CC = $(PREFIX)-gcc +LD = $(PREFIX)-gcc +OBJCOPY = $(PREFIX)-objcopy +OBJDUMP = $(PREFIX)-objdump +GDB = $(PREFIX)-gdb + +TOOLCHAIN_DIR ?= ../../../../.. +ifeq ($(wildcard ../../../../../lib/libopencm3_stm32f1.a),) +ifneq ($(strip $(shell which $(CC))),) +TOOLCHAIN_DIR := $(shell dirname `which $(CC)`)/../$(PREFIX) +endif +else +ifeq ($(V),1) +$(info We seem to be building the example in the source directory. Using local library!) +endif +endif + +ARCH_FLAGS = -mthumb -mcpu=cortex-m3 -msoft-float +CFLAGS += -Os -g \ + -Wall -Wextra -Wimplicit-function-declaration \ + -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes \ + -Wundef -Wshadow \ + -I$(TOOLCHAIN_DIR)/include \ + -fno-common $(ARCH_FLAGS) -MD -DSTM32F1 +LDSCRIPT ?= $(BINARY).ld +LDFLAGS += --static -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group \ + -L$(TOOLCHAIN_DIR)/lib \ + -T$(LDSCRIPT) -nostartfiles -Wl,--gc-sections \ + $(ARCH_FLAGS) -mfix-cortex-m3-ldrd + +ifneq ($(OPENCM3_DIR),) +CFLAGS += -I$(OPENCM3_DIR)/include +LDFLAGS += -L$(OPENCM3_DIR)/lib -L$(OPENCM3_DIR)/lib/stm32/f1 +SCRIPT_DIR = $(OPENCM3_DIR)/share +else +SCRIPT_DIR = $(shell dirname $(shell readlink -f $(shell which $(PREFIX)-gcc)))/../$(PREFIX)/share +endif + +OBJS += $(BINARY).o + +OOCD ?= openocd +OOCD_INTERFACE ?= flossjtag +OOCD_BOARD ?= olimex_stm32_h103 +# Black magic probe specific variables +# Set the BMP_PORT to a serial port and then BMP is used for flashing +BMP_PORT ?= +# texane/stlink can be used by uncommenting this... +# or defining it in your own makefiles +#STLINK_PORT ?= :4242 + +# Be silent per default, but 'make V=1' will show all compiler calls. +ifneq ($(V),1) +Q := @ +NULL := 2>/dev/null +else +LDFLAGS += -Wl,--print-gc-sections +endif + +.SUFFIXES: .elf .bin .hex .srec .list .images +.SECONDEXPANSION: +.SECONDARY: + +all: images + +images: $(BINARY).images +flash: $(BINARY).flash + +%.images: %.bin %.hex %.srec %.list + @#echo "*** $* images generated ***" + +%.bin: %.elf + @#printf " OBJCOPY $(*).bin\n" + $(Q)$(OBJCOPY) -Obinary $(*).elf $(*).bin + +%.hex: %.elf + @#printf " OBJCOPY $(*).hex\n" + $(Q)$(OBJCOPY) -Oihex $(*).elf $(*).hex + +%.srec: %.elf + @#printf " OBJCOPY $(*).srec\n" + $(Q)$(OBJCOPY) -Osrec $(*).elf $(*).srec + +%.list: %.elf + @#printf " OBJDUMP $(*).list\n" + $(Q)$(OBJDUMP) -S $(*).elf > $(*).list + +%.elf: $(OBJS) $(LDSCRIPT) $(TOOLCHAIN_DIR)/lib/libopencm3_stm32f1.a + @#printf " LD $(subst $(shell pwd)/,,$(@))\n" + $(Q)$(LD) -o $(*).elf $(OBJS) -lopencm3_stm32f1 $(LDFLAGS) + +%.o: %.c Makefile + @#printf " CC $(subst $(shell pwd)/,,$(@))\n" + $(Q)$(CC) $(CFLAGS) -o $@ -c $< + +clean: + $(Q)rm -f *.o + $(Q)rm -f *.d + $(Q)rm -f *.elf + $(Q)rm -f *.bin + $(Q)rm -f *.hex + $(Q)rm -f *.srec + $(Q)rm -f *.list + +ifeq ($(STLINK_PORT),) +ifeq ($(BMP_PORT),) +ifeq ($(OOCD_SERIAL),) +%.flash: %.hex + @printf " FLASH $<\n" + @# IMPORTANT: Don't use "resume", only "reset" will work correctly! + $(Q)$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \ + -f board/$(OOCD_BOARD).cfg \ + -c "init" -c "reset init" \ + -c "stm32f1x mass_erase 0" \ + -c "flash write_image $(*).hex" \ + -c "reset" \ + -c "shutdown" $(NULL) +else +%.flash: %.hex + @printf " FLASH $<\n" + @# IMPORTANT: Don't use "resume", only "reset" will work correctly! + $(Q)$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \ + -f board/$(OOCD_BOARD).cfg \ + -c "ft2232_serial $(OOCD_SERIAL)" \ + -c "init" -c "reset init" \ + -c "stm32f1x mass_erase 0" \ + -c "flash write_image $(*).hex" \ + -c "reset" \ + -c "shutdown" $(NULL) +endif +else +%.flash: %.elf + @echo " GDB $(*).elf (flash)" + $(Q)$(GDB) --batch \ + -ex 'target extended-remote $(BMP_PORT)' \ + -x $(TOOLCHAIN_DIR)/scripts/black_magic_probe_flash.scr \ + $(*).elf +endif +else +%.flash: %.elf + @echo " GDB $(*).elf (flash)" + $(Q)$(GDB) --batch \ + -ex 'target extended-remote $(STLINK_PORT)' \ + -x $(SCRIPT_DIR)/libopencm3/scripts/stlink_flash.scr \ + $(*).elf +endif + +.PHONY: images clean + +-include $(OBJS:.o=.d) + diff --git a/examples/stm32/f1/lisa-m-1/can/Makefile b/examples/stm32/f1/lisa-m-1/can/Makefile new file mode 100644 index 0000000..d9858cc --- /dev/null +++ b/examples/stm32/f1/lisa-m-1/can/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = can + +LDSCRIPT = ../lisa-m.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/lisa-m-1/can/README b/examples/stm32/f1/lisa-m-1/can/README new file mode 100644 index 0000000..3a14e3b --- /dev/null +++ b/examples/stm32/f1/lisa-m-1/can/README @@ -0,0 +1,4 @@ +This test sets up the CAN interface on Lisa/M and transmits 8 bites every +100ms. The first byte is being incremented in each cycle. The demo also +receives messages and is displaing the first 4 bits of the first byte on the +board LEDs. diff --git a/examples/stm32/f1/lisa-m-1/can/can.c b/examples/stm32/f1/lisa-m-1/can/can.c new file mode 100644 index 0000000..18162c0 --- /dev/null +++ b/examples/stm32/f1/lisa-m-1/can/can.c @@ -0,0 +1,237 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * Copyright (C) 2010 Piotr Esden-Tempski + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include + +struct can_tx_msg { + u32 std_id; + u32 ext_id; + u8 ide; + u8 rtr; + u8 dlc; + u8 data[8]; +}; + +struct can_rx_msg { + u32 std_id; + u32 ext_id; + u8 ide; + u8 rtr; + u8 dlc; + u8 data[8]; + u8 fmi; +}; + +struct can_tx_msg can_tx_msg; +struct can_rx_msg can_rx_msg; + +static void gpio_setup(void) +{ + /* Enable Alternate Function clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); + + /* Enable GPIOA clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + + /* Enable GPIOB clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); + + /* Enable GPIOC clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + /* Preconfigure LEDs. */ + gpio_set(GPIOA, GPIO8); /* LED0 off */ + gpio_set(GPIOB, GPIO4); /* LED1 off */ + gpio_set(GPIOC, GPIO15); /* LED2 off */ + gpio_set(GPIOC, GPIO2); /* LED3 off */ + gpio_set(GPIOC, GPIO5); /* LED4 off */ + + /* Configure LED GPIOOs. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO8); + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO4); + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO15); + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO2); + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO5); + + /* Configure PB4 as GPIO. */ + AFIO_MAPR |= AFIO_MAPR_SWJ_CFG_FULL_SWJ_NO_JNTRST; + +} + +static void systick_setup(void) +{ + /* 72MHz / 8 => 9000000 counts per second */ + systick_set_clocksource(STK_CTRL_CLKSOURCE_AHB_DIV8); + + /* 9000000/9000 = 1000 overflows per second - every 1ms one interrupt */ + /* SysTick interrupt every N clock pulses: set reload to N-1 */ + systick_set_reload(8999); + + systick_interrupt_enable(); + + /* Start counting. */ + systick_counter_enable(); +} + +static void can_setup(void) +{ + /* Enable peripheral clocks. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_CANEN); + + AFIO_MAPR |= AFIO_MAPR_CAN1_REMAP_PORTB; + + /* Configure CAN pin: RX (input pull-up). */ + gpio_set_mode(GPIO_BANK_CAN1_PB_RX, GPIO_MODE_INPUT, + GPIO_CNF_INPUT_PULL_UPDOWN, GPIO_CAN1_PB_RX); + gpio_set(GPIOB, GPIO_CAN1_PB_RX); + + /* Configure CAN pin: TX. */ + gpio_set_mode(GPIO_BANK_CAN1_PB_TX, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_CAN1_PB_TX); + + /* NVIC setup. */ + nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ); + nvic_set_priority(NVIC_USB_LP_CAN_RX0_IRQ, 1); + + /* Reset CAN. */ + can_reset(CAN1); + + /* CAN cell init. */ + if (can_init(CAN1, + false, /* TTCM: Time triggered comm mode? */ + true, /* ABOM: Automatic bus-off management? */ + false, /* AWUM: Automatic wakeup mode? */ + false, /* NART: No automatic retransmission? */ + false, /* RFLM: Receive FIFO locked mode? */ + false, /* TXFP: Transmit FIFO priority? */ + CAN_BTR_SJW_1TQ, + CAN_BTR_TS1_3TQ, + CAN_BTR_TS2_4TQ, + 12, /* BRP+1: Baud rate prescaler */ + false, /* loopback mode */ + false)) /* silent mode */ + { + gpio_set(GPIOA, GPIO8); /* LED0 off */ + gpio_set(GPIOB, GPIO4); /* LED1 off */ + gpio_set(GPIOC, GPIO15); /* LED2 off */ + gpio_clear(GPIOC, GPIO2); /* LED3 on */ + gpio_set(GPIOC, GPIO5); /* LED4 off */ + + /* Die because we failed to initialize. */ + while (1) + __asm__("nop"); + } + + /* CAN filter 0 init. */ + can_filter_id_mask_32bit_init(CAN1, + 0, /* Filter ID */ + 0, /* CAN ID */ + 0, /* CAN ID mask */ + 0, /* FIFO assignment (here: FIFO0) */ + true); /* Enable the filter. */ + + /* Enable CAN RX interrupt. */ + can_enable_irq(CAN1, CAN_IER_FMPIE0); +} + +void sys_tick_handler(void) +{ + static int temp32 = 0; + static u8 data[8] = {0, 1, 2, 0, 0, 0, 0, 0}; + + /* We call this handler every 1ms so 100ms = 1s + * Resulting in 100Hz message frequency. + */ + if (++temp32 != 100) + return; + + temp32 = 0; + + /* Transmit CAN frame. */ + data[0]++; + if (can_transmit(CAN1, + 0, /* (EX/ST)ID: CAN ID */ + false, /* IDE: CAN ID extended? */ + false, /* RTR: Request transmit? */ + 8, /* DLC: Data length */ + data) == -1) + { + gpio_set(GPIOA, GPIO8); /* LED0 off */ + gpio_set(GPIOB, GPIO4); /* LED1 off */ + gpio_set(GPIOC, GPIO15); /* LED2 off */ + gpio_set(GPIOC, GPIO2); /* LED3 off */ + gpio_clear(GPIOC, GPIO5); /* LED4 on */ + } +} + +void usb_lp_can_rx0_isr(void) +{ + u32 id, fmi; + bool ext, rtr; + u8 length, data[8]; + + can_receive(CAN1, 0, false, &id, &ext, &rtr, &fmi, &length, data); + + if (data[0] & 1) + gpio_clear(GPIOA, GPIO8); + else + gpio_set(GPIOA, GPIO8); + + if (data[0] & 2) + gpio_clear(GPIOB, GPIO4); + else + gpio_set(GPIOB, GPIO4); + + if (data[0] & 4) + gpio_clear(GPIOC, GPIO15); + else + gpio_set(GPIOC, GPIO15); + + if (data[0] & 8) + gpio_clear(GPIOC, GPIO2); + else + gpio_set(GPIOC, GPIO2); + + can_fifo_release(CAN1, 0); +} + +int main(void) +{ + rcc_clock_setup_in_hse_12mhz_out_72mhz(); + gpio_setup(); + can_setup(); + systick_setup(); + + while (1); /* Halt. */ + + return 0; +} diff --git a/examples/stm32/f1/lisa-m-1/can/can.c~ b/examples/stm32/f1/lisa-m-1/can/can.c~ new file mode 100644 index 0000000..dc3bb07 --- /dev/null +++ b/examples/stm32/f1/lisa-m-1/can/can.c~ @@ -0,0 +1,218 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * Copyright (C) 2010 Piotr Esden-Tempski + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include + +struct can_tx_msg { + u32 std_id; + u32 ext_id; + u8 ide; + u8 rtr; + u8 dlc; + u8 data[8]; +}; + +struct can_rx_msg { + u32 std_id; + u32 ext_id; + u8 ide; + u8 rtr; + u8 dlc; + u8 data[8]; + u8 fmi; +}; + +struct can_tx_msg can_tx_msg; +struct can_rx_msg can_rx_msg; + +void gpio_setup(void) +{ + /* Enable GPIOA clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + + /* Enable GPIOB clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); + + gpio_set(GPIOA, GPIO6); /* LED0 off */ + gpio_set(GPIOA, GPIO7); /* LED1 off */ + gpio_set(GPIOB, GPIO0); /* LED2 off */ + gpio_set(GPIOB, GPIO1); /* LED3 off */ + + /* Set GPIO6/7 (in GPIO port A) to 'output push-pull' for the LEDs. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO6); + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO7); + + /* Set GPIO0/1 (in GPIO port B) to 'output push-pull' for the LEDs. */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO0); + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO1); +} + +void systick_setup(void) +{ + /* 72MHz / 8 => 9000000 counts per second */ + systick_set_clocksource(STK_CTRL_CLKSOURCE_AHB_DIV8); + + /* 9000000/9000 = 1000 overflows per second - every 1ms one interrupt */ + systick_set_reload(9000); + + systick_interrupt_enable(); + + /* Start counting. */ + systick_counter_enable(); +} + +void can_setup(void) +{ + /* Enable peripheral clocks. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_CANEN); + + AFIO_MAPR = AFIO_MAPR_CAN1_REMAP_PORTB; + + /* Configure CAN pin: RX (input pull-up). */ + gpio_set_mode(GPIOB, GPIO_MODE_INPUT, + GPIO_CNF_INPUT_PULL_UPDOWN, GPIO_CAN1_PB_RX); + gpio_set(GPIOB, GPIO_CAN1_PB_RX); + + /* Configure CAN pin: TX. */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_CAN1_PB_TX); + + /* NVIC setup. */ + nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ); + nvic_set_priority(NVIC_USB_LP_CAN_RX0_IRQ, 1); + + /* Reset CAN. */ + can_reset(CAN1); + + /* CAN cell init. */ + if (can_init(CAN1, + false, /* TTCM: Time triggered comm mode? */ + true, /* ABOM: Automatic bus-off management? */ + false, /* AWUM: Automatic wakeup mode? */ + false, /* NART: No automatic retransmission? */ + false, /* RFLM: Receive FIFO locked mode? */ + false, /* TXFP: Transmit FIFO priority? */ + CAN_BTR_SJW_1TQ, + CAN_BTR_TS1_3TQ, + CAN_BTR_TS2_4TQ, + 12)) /* BRP+1: Baud rate prescaler */ + { + gpio_set(GPIOA, GPIO6); /* LED0 off */ + gpio_set(GPIOA, GPIO7); /* LED1 off */ + gpio_set(GPIOB, GPIO0); /* LED2 off */ + gpio_clear(GPIOB, GPIO1); /* LED3 on */ + + /* Die because we failed to initialize. */ + while (1) + __asm__("nop"); + } + + /* CAN filter 0 init. */ + can_filter_id_mask_32bit_init(CAN1, + 0, /* Filter ID */ + 0, /* CAN ID */ + 0, /* CAN ID mask */ + 0, /* FIFO assignment (here: FIFO0) */ + true); /* Enable the filter. */ + + /* Enable CAN RX interrupt. */ + can_enable_irq(CAN1, CAN_IER_FMPIE0); +} + +void sys_tick_handler(void) +{ + static int temp32 = 0; + static u8 data[8] = {0, 1, 2, 0, 0, 0, 0, 0}; + + /* We call this handler every 1ms so 1000ms = 1s on/off. */ + if (++temp32 != 1000) + return; + + temp32 = 0; + + /* Transmit CAN frame. */ + data[0]++; + if (can_transmit(CAN1, + 0, /* (EX/ST)ID: CAN ID */ + false, /* IDE: CAN ID extended? */ + false, /* RTR: Request transmit? */ + 8, /* DLC: Data length */ + data) == -1) + { + gpio_set(GPIOA, GPIO6); /* LED0 off */ + gpio_set(GPIOA, GPIO7); /* LED1 off */ + gpio_clear(GPIOB, GPIO0); /* LED2 on */ + gpio_set(GPIOB, GPIO1); /* LED3 off */ + } +} + +void usb_lp_can_rx0_isr(void) +{ + u32 id, fmi; + bool ext, rtr; + u8 length, data[8]; + + can_receive(CAN1, 0, false, &id, &ext, &rtr, &fmi, &length, data); + + if (data[0] & 1) + gpio_clear(GPIOA, GPIO6); + else + gpio_set(GPIOA, GPIO6); + + if (data[0] & 2) + gpio_clear(GPIOA, GPIO7); + else + gpio_set(GPIOA, GPIO7); + + if (data[0] & 4) + gpio_clear(GPIOB, GPIO0); + else + gpio_set(GPIOB, GPIO0); + + if (data[0] & 8) + gpio_clear(GPIOB, GPIO1); + else + gpio_set(GPIOB, GPIO1); + + can_fifo_release(CAN1, 0); +} + +int main(void) +{ + rcc_clock_setup_in_hse_12mhz_out_72mhz(); + gpio_setup(); + can_setup(); + systick_setup(); + + while (1); /* Halt. */ + + return 0; +} diff --git a/examples/stm32/f1/lisa-m-1/fancyblink/Makefile b/examples/stm32/f1/lisa-m-1/fancyblink/Makefile new file mode 100644 index 0000000..e453f84 --- /dev/null +++ b/examples/stm32/f1/lisa-m-1/fancyblink/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = fancyblink + +LDSCRIPT = ../lisa-m.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/lisa-m-1/fancyblink/fancyblink.c b/examples/stm32/f1/lisa-m-1/fancyblink/fancyblink.c new file mode 100644 index 0000000..442709a --- /dev/null +++ b/examples/stm32/f1/lisa-m-1/fancyblink/fancyblink.c @@ -0,0 +1,68 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * Copyright (C) 2011 Piotr Esden-Tempski + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include + +/* Set STM32 to 72 MHz. */ +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_12mhz_out_72mhz(); + + /* Enable GPIOB, GPIOC, and AFIO clocks. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); +} + +static void gpio_setup(void) +{ + /* Set GPIO13 (in GPIO port C) to 'output push-pull'. */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO13); + + /* Set GPIO4 (in GPIO port B) to 'output push-pull'. */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO4); + + AFIO_MAPR |= AFIO_MAPR_SWJ_CFG_FULL_SWJ_NO_JNTRST; + + /* Preconfigure the LEDs. */ + gpio_set(GPIOB, GPIO4); /* Switch off LED. */ + gpio_clear(GPIOC, GPIO13); /* Switch on LED. */ +} + +int main(void) +{ + int i; + + clock_setup(); + gpio_setup(); + + /* Blink the LEDs (PC13 and PB4) on the board. */ + while (1) { + gpio_toggle(GPIOC, GPIO13); /* LED on/off */ + gpio_toggle(GPIOB, GPIO4); /* LED on/off */ + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/stm32/f1/lisa-m-1/lisa-m.ld b/examples/stm32/f1/lisa-m-1/lisa-m.ld new file mode 100644 index 0000000..e03eb80 --- /dev/null +++ b/examples/stm32/f1/lisa-m-1/lisa-m.ld @@ -0,0 +1,32 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* Linker script for Lisa-M (STM32F103RBT6, 128K flash, 20K RAM). */ +/* TODO: Chip name and sizes correct? */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_stm32f1.ld + diff --git a/examples/stm32/f1/lisa-m-1/usb_cdcacm/Makefile b/examples/stm32/f1/lisa-m-1/usb_cdcacm/Makefile new file mode 100644 index 0000000..b1c3b6c --- /dev/null +++ b/examples/stm32/f1/lisa-m-1/usb_cdcacm/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = cdcacm + +LDSCRIPT = ../lisa-m.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/lisa-m-1/usb_cdcacm/README b/examples/stm32/f1/lisa-m-1/usb_cdcacm/README new file mode 100644 index 0000000..2f1ee4b --- /dev/null +++ b/examples/stm32/f1/lisa-m-1/usb_cdcacm/README @@ -0,0 +1,7 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example implements a USB CDC-ACM device (aka Virtual Serial Port) +to demonstrate the use of the USB device stack. + diff --git a/examples/stm32/f1/lisa-m-1/usb_cdcacm/cdcacm.c b/examples/stm32/f1/lisa-m-1/usb_cdcacm/cdcacm.c new file mode 100644 index 0000000..d12d3d8 --- /dev/null +++ b/examples/stm32/f1/lisa-m-1/usb_cdcacm/cdcacm.c @@ -0,0 +1,261 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Gareth McMullin + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include + +static const struct usb_device_descriptor dev = { + .bLength = USB_DT_DEVICE_SIZE, + .bDescriptorType = USB_DT_DEVICE, + .bcdUSB = 0x0200, + .bDeviceClass = USB_CLASS_CDC, + .bDeviceSubClass = 0, + .bDeviceProtocol = 0, + .bMaxPacketSize0 = 64, + .idVendor = 0x0483, + .idProduct = 0x5740, + .bcdDevice = 0x0200, + .iManufacturer = 1, + .iProduct = 2, + .iSerialNumber = 3, + .bNumConfigurations = 1, +}; + +/* + * This notification endpoint isn't implemented. According to CDC spec it's + * optional, but its absence causes a NULL pointer dereference in the + * Linux cdc_acm driver. + */ +static const struct usb_endpoint_descriptor comm_endp[] = {{ + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x83, + .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, + .wMaxPacketSize = 16, + .bInterval = 255, +}}; + +static const struct usb_endpoint_descriptor data_endp[] = {{ + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x01, + .bmAttributes = USB_ENDPOINT_ATTR_BULK, + .wMaxPacketSize = 64, + .bInterval = 1, +}, { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x82, + .bmAttributes = USB_ENDPOINT_ATTR_BULK, + .wMaxPacketSize = 64, + .bInterval = 1, +}}; + +static const struct { + struct usb_cdc_header_descriptor header; + struct usb_cdc_call_management_descriptor call_mgmt; + struct usb_cdc_acm_descriptor acm; + struct usb_cdc_union_descriptor cdc_union; +} __attribute__((packed)) cdcacm_functional_descriptors = { + .header = { + .bFunctionLength = sizeof(struct usb_cdc_header_descriptor), + .bDescriptorType = CS_INTERFACE, + .bDescriptorSubtype = USB_CDC_TYPE_HEADER, + .bcdCDC = 0x0110, + }, + .call_mgmt = { + .bFunctionLength = + sizeof(struct usb_cdc_call_management_descriptor), + .bDescriptorType = CS_INTERFACE, + .bDescriptorSubtype = USB_CDC_TYPE_CALL_MANAGEMENT, + .bmCapabilities = 0, + .bDataInterface = 1, + }, + .acm = { + .bFunctionLength = sizeof(struct usb_cdc_acm_descriptor), + .bDescriptorType = CS_INTERFACE, + .bDescriptorSubtype = USB_CDC_TYPE_ACM, + .bmCapabilities = 0, + }, + .cdc_union = { + .bFunctionLength = sizeof(struct usb_cdc_union_descriptor), + .bDescriptorType = CS_INTERFACE, + .bDescriptorSubtype = USB_CDC_TYPE_UNION, + .bControlInterface = 0, + .bSubordinateInterface0 = 1, + } +}; + +static const struct usb_interface_descriptor comm_iface[] = {{ + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 0, + .bAlternateSetting = 0, + .bNumEndpoints = 1, + .bInterfaceClass = USB_CLASS_CDC, + .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM, + .bInterfaceProtocol = USB_CDC_PROTOCOL_AT, + .iInterface = 0, + + .endpoint = comm_endp, + + .extra = &cdcacm_functional_descriptors, + .extralen = sizeof(cdcacm_functional_descriptors) +}}; + +static const struct usb_interface_descriptor data_iface[] = {{ + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 1, + .bAlternateSetting = 0, + .bNumEndpoints = 2, + .bInterfaceClass = USB_CLASS_DATA, + .bInterfaceSubClass = 0, + .bInterfaceProtocol = 0, + .iInterface = 0, + + .endpoint = data_endp, +}}; + +static const struct usb_interface ifaces[] = {{ + .num_altsetting = 1, + .altsetting = comm_iface, +}, { + .num_altsetting = 1, + .altsetting = data_iface, +}}; + +static const struct usb_config_descriptor config = { + .bLength = USB_DT_CONFIGURATION_SIZE, + .bDescriptorType = USB_DT_CONFIGURATION, + .wTotalLength = 0, + .bNumInterfaces = 2, + .bConfigurationValue = 1, + .iConfiguration = 0, + .bmAttributes = 0x80, + .bMaxPower = 0x32, + + .interface = ifaces, +}; + +static const char *usb_strings[] = { + "Black Sphere Technologies", + "CDC-ACM Demo", + "DEMO", +}; + +static int cdcacm_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, u8 **buf, + u16 *len, void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req)) +{ + (void)complete; + (void)buf; + (void)usbd_dev; + + switch (req->bRequest) { + case USB_CDC_REQ_SET_CONTROL_LINE_STATE: { + /* + * This Linux cdc_acm driver requires this to be implemented + * even though it's optional in the CDC spec, and we don't + * advertise it in the ACM functional descriptor. + */ + char local_buf[10]; + struct usb_cdc_notification *notif = (void*)local_buf; + + /* We echo signals back to host as notification. */ + notif->bmRequestType = 0xA1; + notif->bNotification = USB_CDC_NOTIFY_SERIAL_STATE; + notif->wValue = 0; + notif->wIndex = 0; + notif->wLength = 2; + local_buf[8] = req->wValue & 3; + local_buf[9] = 0; + // usbd_ep_write_packet(0x83, buf, 10); + return 1; + } + case USB_CDC_REQ_SET_LINE_CODING: + if (*len < sizeof(struct usb_cdc_line_coding)) + return 0; + + return 1; + } + return 0; +} + +static void cdcacm_data_rx_cb(usbd_device *usbd_dev, u8 ep) +{ + (void)ep; + + char buf[64]; + int len = usbd_ep_read_packet(usbd_dev, 0x01, buf, 64); + + if (len) { + while (usbd_ep_write_packet(usbd_dev, 0x82, buf, len) == 0) + ; + buf[len] = 0; + } + + gpio_toggle(GPIOC, GPIO5); +} + +static void cdcacm_set_config(usbd_device *usbd_dev, u16 wValue) +{ + (void)wValue; + + usbd_ep_setup(usbd_dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64, cdcacm_data_rx_cb); + usbd_ep_setup(usbd_dev, 0x82, USB_ENDPOINT_ATTR_BULK, 64, NULL); + usbd_ep_setup(usbd_dev, 0x83, USB_ENDPOINT_ATTR_INTERRUPT, 16, NULL); + + usbd_register_control_callback( + usbd_dev, + USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE, + USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, + cdcacm_control_request); +} + +int main(void) +{ + int i; + + usbd_device *usbd_dev; + + rcc_clock_setup_in_hsi_out_48mhz(); + + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_OTGFSEN); + + gpio_set(GPIOC, GPIO2); + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO2); + gpio_set(GPIOC, GPIO5); + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO5); + + usbd_dev = usbd_init(&stm32f107_usb_driver, &dev, &config, usb_strings, 3); + usbd_register_set_config_callback(usbd_dev, cdcacm_set_config); + + for (i = 0; i < 0x800000; i++) + __asm__("nop"); + gpio_clear(GPIOC, GPIO2); + + while (1) + usbd_poll(usbd_dev); +} diff --git a/examples/stm32/f1/lisa-m-1/usb_cdcacm/cdcacm_test.py b/examples/stm32/f1/lisa-m-1/usb_cdcacm/cdcacm_test.py new file mode 100644 index 0000000..fb14e68 --- /dev/null +++ b/examples/stm32/f1/lisa-m-1/usb_cdcacm/cdcacm_test.py @@ -0,0 +1,56 @@ +#! /usr/bin/env python +# +# This file is part of the libopencm3 project. +# +# Copyright (C) 2011 Piotr Esden-Tempski +# +# This library is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this library. If not, see . +# + +import serial +import signal +import sys +import time + +def signal_handler(signal, frame): + print 'You pressed Ctrl+C!' + ser.close(); + sys.exit(0) + +signal.signal(signal.SIGINT, signal_handler); + +ser = serial.Serial('/dev/cu.usbmodemDEM1', 115200, timeout=0.05); + +error = 0 + +tim = time.time(); + +cycles = 0 + +while 1==1: + + ser.write("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*(){}"); + + buf = ser.read(1024); + + cycles += 1 + + if buf != "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*(){}" : + error+=1 + + print "received " + buf + " errors: " + str(error) + " cycles: " + str(cycles) + " runtime: " + str(time.time() - tim) + + #time.sleep(0.1); + +ser.close(); diff --git a/examples/stm32/f1/lisa-m-1/usb_dfu/Makefile b/examples/stm32/f1/lisa-m-1/usb_dfu/Makefile new file mode 100644 index 0000000..eab1567 --- /dev/null +++ b/examples/stm32/f1/lisa-m-1/usb_dfu/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = usbdfu + +LDSCRIPT = ../lisa-m.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/lisa-m-1/usb_dfu/README b/examples/stm32/f1/lisa-m-1/usb_dfu/README new file mode 100644 index 0000000..9c0169a --- /dev/null +++ b/examples/stm32/f1/lisa-m-1/usb_dfu/README @@ -0,0 +1,7 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example implements a USB Device Firmware Upgrade (DFU) bootloader +to demonstrate the use of the USB device stack. + diff --git a/examples/stm32/f1/lisa-m-1/usb_dfu/usbdfu.c b/examples/stm32/f1/lisa-m-1/usb_dfu/usbdfu.c new file mode 100644 index 0000000..5db6542 --- /dev/null +++ b/examples/stm32/f1/lisa-m-1/usb_dfu/usbdfu.c @@ -0,0 +1,272 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Gareth McMullin + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include + +#define APP_ADDRESS 0x08002000 + +/* Commands sent with wBlockNum == 0 as per ST implementation. */ +#define CMD_SETADDR 0x21 +#define CMD_ERASE 0x41 + +/* We need a special large control buffer for this device: */ +u8 usbd_control_buffer[1024]; + +static enum dfu_state usbdfu_state = STATE_DFU_IDLE; + +static struct { + u8 buf[sizeof(usbd_control_buffer)]; + u16 len; + u32 addr; + u16 blocknum; +} prog; + +const struct usb_device_descriptor dev = { + .bLength = USB_DT_DEVICE_SIZE, + .bDescriptorType = USB_DT_DEVICE, + .bcdUSB = 0x0200, + .bDeviceClass = 0, + .bDeviceSubClass = 0, + .bDeviceProtocol = 0, + .bMaxPacketSize0 = 64, + .idVendor = 0x0483, + .idProduct = 0xDF11, + .bcdDevice = 0x0200, + .iManufacturer = 1, + .iProduct = 2, + .iSerialNumber = 3, + .bNumConfigurations = 1, +}; + +const struct usb_dfu_descriptor dfu_function = { + .bLength = sizeof(struct usb_dfu_descriptor), + .bDescriptorType = DFU_FUNCTIONAL, + .bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH, + .wDetachTimeout = 255, + .wTransferSize = 1024, + .bcdDFUVersion = 0x011A, +}; + +const struct usb_interface_descriptor iface = { + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 0, + .bAlternateSetting = 0, + .bNumEndpoints = 0, + .bInterfaceClass = 0xFE, /* Device Firmware Upgrade */ + .bInterfaceSubClass = 1, + .bInterfaceProtocol = 2, + + /* The ST Microelectronics DfuSe application needs this string. + * The format isn't documented... */ + .iInterface = 4, + + .extra = &dfu_function, + .extralen = sizeof(dfu_function), +}; + +const struct usb_interface ifaces[] = {{ + .num_altsetting = 1, + .altsetting = &iface, +}}; + +const struct usb_config_descriptor config = { + .bLength = USB_DT_CONFIGURATION_SIZE, + .bDescriptorType = USB_DT_CONFIGURATION, + .wTotalLength = 0, + .bNumInterfaces = 1, + .bConfigurationValue = 1, + .iConfiguration = 0, + .bmAttributes = 0xC0, + .bMaxPower = 0x32, + + .interface = ifaces, +}; + +static const char *usb_strings[] = { + "Black Sphere Technologies", + "DFU Demo", + "DEMO", + /* This string is used by ST Microelectronics' DfuSe utility. */ + "@Internal Flash /0x08000000/8*001Ka,56*001Kg", +}; + +static u8 usbdfu_getstatus(u32 *bwPollTimeout) +{ + switch (usbdfu_state) { + case STATE_DFU_DNLOAD_SYNC: + usbdfu_state = STATE_DFU_DNBUSY; + *bwPollTimeout = 100; + return DFU_STATUS_OK; + case STATE_DFU_MANIFEST_SYNC: + /* Device will reset when read is complete. */ + usbdfu_state = STATE_DFU_MANIFEST; + return DFU_STATUS_OK; + default: + return DFU_STATUS_OK; + } +} + +static void usbdfu_getstatus_complete(usbd_device *usbd_dev, struct usb_setup_data *req) +{ + int i; + (void)req; + (void)usbd_dev; + + switch (usbdfu_state) { + case STATE_DFU_DNBUSY: + flash_unlock(); + if (prog.blocknum == 0) { + switch (prog.buf[0]) { + case CMD_ERASE: + { + u32 *dat = (u32 *)(prog.buf + 1); + flash_erase_page(*dat); + } + case CMD_SETADDR: + { + u32 *dat = (u32 *)(prog.buf + 1); + prog.addr = *dat; + } + } + } else { + u32 baseaddr = prog.addr + ((prog.blocknum - 2) * + dfu_function.wTransferSize); + for (i = 0; i < prog.len; i += 2) { + u16 *dat = (u16 *)(prog.buf + i); + flash_program_half_word(baseaddr + i, + *dat); + } + } + flash_lock(); + + /* Jump straight to dfuDNLOAD-IDLE, skipping dfuDNLOAD-SYNC. */ + usbdfu_state = STATE_DFU_DNLOAD_IDLE; + return; + case STATE_DFU_MANIFEST: + /* USB device must detach, we just reset... */ + scb_reset_system(); + return; /* Will never return. */ + default: + return; + } +} + +static int usbdfu_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, u8 **buf, + u16 *len, void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req)) +{ + (void)usbd_dev; + + if ((req->bmRequestType & 0x7F) != 0x21) + return 0; /* Only accept class request. */ + + switch (req->bRequest) { + case DFU_DNLOAD: + if ((len == NULL) || (*len == 0)) { + usbdfu_state = STATE_DFU_MANIFEST_SYNC; + return 1; + } else { + /* Copy download data for use on GET_STATUS. */ + prog.blocknum = req->wValue; + prog.len = *len; + memcpy(prog.buf, *buf, *len); + usbdfu_state = STATE_DFU_DNLOAD_SYNC; + return 1; + } + case DFU_CLRSTATUS: + /* Clear error and return to dfuIDLE. */ + if (usbdfu_state == STATE_DFU_ERROR) + usbdfu_state = STATE_DFU_IDLE; + return 1; + case DFU_ABORT: + /* Abort returns to dfuIDLE state. */ + usbdfu_state = STATE_DFU_IDLE; + return 1; + case DFU_UPLOAD: + /* Upload not supported for now. */ + return 0; + case DFU_GETSTATUS: { + u32 bwPollTimeout = 0; /* 24-bit integer in DFU class spec */ + (*buf)[0] = usbdfu_getstatus(&bwPollTimeout); + (*buf)[1] = bwPollTimeout & 0xFF; + (*buf)[2] = (bwPollTimeout >> 8) & 0xFF; + (*buf)[3] = (bwPollTimeout >> 16) & 0xFF; + (*buf)[4] = usbdfu_state; + (*buf)[5] = 0; /* iString not used here */ + *len = 6; + *complete = usbdfu_getstatus_complete; + return 1; + } + case DFU_GETSTATE: + /* Return state with no state transision. */ + *buf[0] = usbdfu_state; + *len = 1; + return 1; + } + + return 0; +} + +int main(void) +{ + usbd_device *usbd_dev; + + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + + if (!gpio_get(GPIOA, GPIO10)) { + /* Boot the application if it's valid. */ + if ((*(volatile u32 *)APP_ADDRESS & 0x2FFE0000) == 0x20000000) { + /* Set vector table base address. */ + SCB_VTOR = APP_ADDRESS & 0xFFFF; + /* Initialise master stack pointer. */ + asm volatile("msr msp, %0"::"g" + (*(volatile u32 *)APP_ADDRESS)); + /* Jump to application. */ + (*(void (**)())(APP_ADDRESS + 4))(); + } + } + + rcc_clock_setup_in_hsi_out_48mhz(); + + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_OTGFSEN); + + gpio_set(GPIOC, GPIO2); + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO2); + + usbd_dev = usbd_init(&stm32f107_usb_driver, &dev, &config, usb_strings, 4); + usbd_set_control_buffer_size(usbd_dev, sizeof(usbd_control_buffer)); + usbd_register_control_callback( + usbd_dev, + USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE, + USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, + usbdfu_control_request); + + gpio_clear(GPIOC, GPIO2); + + while (1) + usbd_poll(usbd_dev); +} diff --git a/examples/stm32/f1/lisa-m-1/usb_hid/Makefile b/examples/stm32/f1/lisa-m-1/usb_hid/Makefile new file mode 100644 index 0000000..00aca61 --- /dev/null +++ b/examples/stm32/f1/lisa-m-1/usb_hid/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = usbhid + +LDSCRIPT = ../lisa-m.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/lisa-m-1/usb_hid/README b/examples/stm32/f1/lisa-m-1/usb_hid/README new file mode 100644 index 0000000..92645c9 --- /dev/null +++ b/examples/stm32/f1/lisa-m-1/usb_hid/README @@ -0,0 +1,7 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example implements a USB Human Interface Device (HID) +to demonstrate the use of the USB device stack. + diff --git a/examples/stm32/f1/lisa-m-1/usb_hid/adxl345.h b/examples/stm32/f1/lisa-m-1/usb_hid/adxl345.h new file mode 100644 index 0000000..7a53b81 --- /dev/null +++ b/examples/stm32/f1/lisa-m-1/usb_hid/adxl345.h @@ -0,0 +1,38 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2011 Gareth McMullin + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#ifndef __ADXL345_H +#define __ADXL345_H + +/* Register addresses */ +#define ADXL345_DEVID 0x00 +#define ADXL345_POWER_CTL 0x2D +#define ADXL345_DATA_FORMAT 0x31 +#define ADXL345_DATAX0 0x32 +#define ADXL345_DATAX1 0x33 +#define ADXL345_DATAY0 0x34 +#define ADXL345_DATAY1 0x35 +#define ADXL345_DATAZ0 0x36 +#define ADXL345_DATAZ1 0x37 + +#define ADXL345_POWER_CTL_MEASURE (1 << 3) +#define ADXL345_DATA_FORMAT_LALIGN (1 << 2) + +#endif + diff --git a/examples/stm32/f1/lisa-m-1/usb_hid/usbhid.c b/examples/stm32/f1/lisa-m-1/usb_hid/usbhid.c new file mode 100644 index 0000000..859a3de --- /dev/null +++ b/examples/stm32/f1/lisa-m-1/usb_hid/usbhid.c @@ -0,0 +1,369 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Gareth McMullin + * Copyright (C) 2011 Piotr Esden-Tempski + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "adxl345.h" + +/* Define this to include the DFU APP interface. */ +#define INCLUDE_DFU_INTERFACE + +#ifdef INCLUDE_DFU_INTERFACE +#include +#include +#endif + +static usbd_device *usbd_dev; + +const struct usb_device_descriptor dev_descr = { + .bLength = USB_DT_DEVICE_SIZE, + .bDescriptorType = USB_DT_DEVICE, + .bcdUSB = 0x0200, + .bDeviceClass = 0, + .bDeviceSubClass = 0, + .bDeviceProtocol = 0, + .bMaxPacketSize0 = 64, + .idVendor = 0x0483, + .idProduct = 0x5710, + .bcdDevice = 0x0200, + .iManufacturer = 1, + .iProduct = 2, + .iSerialNumber = 3, + .bNumConfigurations = 1, +}; + +/* I have no idea what this means. I haven't read the HID spec. */ +static const u8 hid_report_descriptor[] = { + 0x05, 0x01, 0x09, 0x02, 0xA1, 0x01, 0x09, 0x01, + 0xA1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x03, + 0x15, 0x00, 0x25, 0x01, 0x95, 0x03, 0x75, 0x01, + 0x81, 0x02, 0x95, 0x01, 0x75, 0x05, 0x81, 0x01, + 0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 0x09, 0x38, + 0x15, 0x81, 0x25, 0x7F, 0x75, 0x08, 0x95, 0x03, + 0x81, 0x06, 0xC0, 0x09, 0x3c, 0x05, 0xff, 0x09, + 0x01, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95, + 0x02, 0xb1, 0x22, 0x75, 0x06, 0x95, 0x01, 0xb1, + 0x01, 0xc0, +}; + +static const struct { + struct usb_hid_descriptor hid_descriptor; + struct { + u8 bReportDescriptorType; + u16 wDescriptorLength; + } __attribute__((packed)) hid_report; +} __attribute__((packed)) hid_function = { + .hid_descriptor = { + .bLength = sizeof(hid_function), + .bDescriptorType = USB_DT_HID, + .bcdHID = 0x0100, + .bCountryCode = 0, + .bNumDescriptors = 1, + }, + .hid_report = { + .bReportDescriptorType = USB_DT_REPORT, + .wDescriptorLength = sizeof(hid_report_descriptor), + } +}; + +const struct usb_endpoint_descriptor hid_endpoint = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x81, + .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, + .wMaxPacketSize = 4, + .bInterval = 0x02, +}; + +const struct usb_interface_descriptor hid_iface = { + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 0, + .bAlternateSetting = 0, + .bNumEndpoints = 1, + .bInterfaceClass = USB_CLASS_HID, + .bInterfaceSubClass = 1, /* boot */ + .bInterfaceProtocol = 2, /* mouse */ + .iInterface = 0, + + .endpoint = &hid_endpoint, + + .extra = &hid_function, + .extralen = sizeof(hid_function), +}; + +#ifdef INCLUDE_DFU_INTERFACE +const struct usb_dfu_descriptor dfu_function = { + .bLength = sizeof(struct usb_dfu_descriptor), + .bDescriptorType = DFU_FUNCTIONAL, + .bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH, + .wDetachTimeout = 255, + .wTransferSize = 1024, + .bcdDFUVersion = 0x011A, +}; + +const struct usb_interface_descriptor dfu_iface = { + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 1, + .bAlternateSetting = 0, + .bNumEndpoints = 0, + .bInterfaceClass = 0xFE, + .bInterfaceSubClass = 1, + .bInterfaceProtocol = 1, + .iInterface = 0, + + .extra = &dfu_function, + .extralen = sizeof(dfu_function), +}; +#endif + +const struct usb_interface ifaces[] = {{ + .num_altsetting = 1, + .altsetting = &hid_iface, +#ifdef INCLUDE_DFU_INTERFACE +}, { + .num_altsetting = 1, + .altsetting = &dfu_iface, +#endif +}}; + +const struct usb_config_descriptor config = { + .bLength = USB_DT_CONFIGURATION_SIZE, + .bDescriptorType = USB_DT_CONFIGURATION, + .wTotalLength = 0, +#ifdef INCLUDE_DFU_INTERFACE + .bNumInterfaces = 2, +#else + .bNumInterfaces = 1, +#endif + .bConfigurationValue = 1, + .iConfiguration = 0, + .bmAttributes = 0xC0, + .bMaxPower = 0x32, + + .interface = ifaces, +}; + +static const char *usb_strings[] = { + "Black Sphere Technologies", + "HID Demo", + "DEMO", +}; + +static int hid_control_request(usbd_device *dev, struct usb_setup_data *req, u8 **buf, u16 *len, + void (**complete)(usbd_device *dev, struct usb_setup_data *req)) +{ + (void)complete; + (void)dev; + + if((req->bmRequestType != 0x81) || + (req->bRequest != USB_REQ_GET_DESCRIPTOR) || + (req->wValue != 0x2200)) + return 0; + + /* Handle the HID report descriptor. */ + *buf = (u8 *)hid_report_descriptor; + *len = sizeof(hid_report_descriptor); + + return 1; +} + +#ifdef INCLUDE_DFU_INTERFACE +static void dfu_detach_complete(usbd_device *dev, struct usb_setup_data *req) +{ + (void)req; + (void)dev; + + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, 0, GPIO15); + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO10); + gpio_set(GPIOA, GPIO10); + scb_reset_core(); +} + +static int dfu_control_request(usbd_device *dev, struct usb_setup_data *req, u8 **buf, u16 *len, + void (**complete)(usbd_device *dev, struct usb_setup_data *req)) +{ + (void)buf; + (void)len; + (void)dev; + + if ((req->bmRequestType != 0x21) || (req->bRequest != DFU_DETACH)) + return 0; /* Only accept class request. */ + + *complete = dfu_detach_complete; + + return 1; +} +#endif + +static void hid_set_config(usbd_device *dev, u16 wValue) +{ + (void)wValue; + + usbd_ep_setup(dev, 0x81, USB_ENDPOINT_ATTR_INTERRUPT, 4, NULL); + + usbd_register_control_callback( + dev, + USB_REQ_TYPE_STANDARD | USB_REQ_TYPE_INTERFACE, + USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, + hid_control_request); +#ifdef INCLUDE_DFU_INTERFACE + usbd_register_control_callback( + dev, + USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE, + USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, + dfu_control_request); +#endif + + systick_set_clocksource(STK_CTRL_CLKSOURCE_AHB_DIV8); + /* SysTick interrupt every N clock pulses: set reload to N-1 */ + systick_set_reload(99999); + systick_interrupt_enable(); + systick_counter_enable(); +} + +static u8 spi_readwrite(u32 spi, u8 data) +{ + while (SPI_SR(spi) & SPI_SR_BSY) + ; + SPI_DR(spi) = data; + while (!(SPI_SR(spi) & SPI_SR_RXNE)) + ; + return SPI_DR(spi); +} + +static u8 accel_read(u8 addr) +{ + u8 ret; + gpio_clear(GPIOB, GPIO12); + spi_readwrite(SPI2, addr | 0x80); + ret = spi_readwrite(SPI2, 0); + gpio_set(GPIOB, GPIO12); + return ret; +} + +static void accel_write(u8 addr, u8 data) +{ + gpio_clear(GPIOB, GPIO12); + spi_readwrite(SPI2, addr); + spi_readwrite(SPI2, data); + gpio_set(GPIOB, GPIO12); +} + +static void accel_get(s16 *x, s16 *y, s16 *z) +{ + if (x) + *x = accel_read(ADXL345_DATAX0) | + (accel_read(ADXL345_DATAX1) << 8); + if (y) + *y = accel_read(ADXL345_DATAY0) | + (accel_read(ADXL345_DATAY1) << 8); + if (z) + *z = accel_read(ADXL345_DATAZ0) | + (accel_read(ADXL345_DATAZ1) << 8); +} + +int main(void) +{ + int i; + + rcc_clock_setup_in_hse_12mhz_out_72mhz(); + + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_SPI2EN); + rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_OTGFSEN); + + /* Configure SPI2: PB13(SCK), PB14(MISO), PB15(MOSI). */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_10_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, + GPIO_SPI2_SCK | GPIO_SPI2_MOSI); + gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, + GPIO_SPI2_MISO); + /* Enable CS pin on PB12. */ + gpio_set(GPIOB, GPIO12); + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_10_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO12); + + /* Force to SPI mode. This should be default after reset! */ + SPI2_I2SCFGR = 0; + spi_init_master(SPI2, + SPI_CR1_BAUDRATE_FPCLK_DIV_256, + SPI_CR1_CPOL_CLK_TO_1_WHEN_IDLE, + SPI_CR1_CPHA_CLK_TRANSITION_2, + SPI_CR1_DFF_8BIT, + SPI_CR1_MSBFIRST); + /* Ignore the stupid NSS pin. */ + spi_enable_software_slave_management(SPI2); + spi_set_nss_high(SPI2); + spi_enable(SPI2); + + (void)accel_read(ADXL345_DEVID); + accel_write(ADXL345_POWER_CTL, ADXL345_POWER_CTL_MEASURE); + accel_write(ADXL345_DATA_FORMAT, ADXL345_DATA_FORMAT_LALIGN); + + /* USB_DETECT as input. */ + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO8); + + /* Green LED off, as output. */ + gpio_set(GPIOC, GPIO2); + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO2); + + usbd_dev = usbd_init(&stm32f107_usb_driver, &dev_descr, &config, usb_strings, 3); + usbd_register_set_config_callback(usbd_dev, hid_set_config); + + /* Delay some seconds to show that pull-up switch works. */ + for (i = 0; i < 0x800000; i++) + __asm__("nop"); + + /* Wait for USB Vbus. */ + while (gpio_get(GPIOA, GPIO8) == 0) + __asm__("nop"); + + /* Green LED on, connect USB. */ + gpio_clear(GPIOC, GPIO2); + // OTG_FS_GCCFG &= ~OTG_FS_GCCFG_VBUSBSEN; + + while (1) + usbd_poll(usbd_dev); +} + +void sys_tick_handler(void) +{ + s16 x, y; + u8 buf[4] = {0, 0, 0, 0}; + + accel_get(&x, &y, NULL); + buf[1] = x >> 9; + buf[2] = y >> 9; + + usbd_ep_write_packet(usbd_dev, 0x81, buf, 4); +} diff --git a/examples/stm32/f1/lisa-m-2/adc_injec/Makefile b/examples/stm32/f1/lisa-m-2/adc_injec/Makefile new file mode 100644 index 0000000..666c393 --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/adc_injec/Makefile @@ -0,0 +1,28 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = adc_injec + +# Comment the following line if you _don't_ have luftboot flashed! +LDFLAGS += -Wl,-Ttext=0x8002000 +CFLAGS += -std=c99 +LDSCRIPT = ../lisa-m.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/lisa-m-2/adc_injec/README b/examples/stm32/f1/lisa-m-2/adc_injec/README new file mode 100644 index 0000000..f0c7d62 --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/adc_injec/README @@ -0,0 +1,11 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This is a simple polling example that sends the value read out from the +temperature sensor ADC channel of the STM32 to the USART2. + +This example polls injected channels. + +The terminal settings for the receiving device/PC are 115200 8n1. + diff --git a/examples/stm32/f1/lisa-m-2/adc_injec/adc_injec.c b/examples/stm32/f1/lisa-m-2/adc_injec/adc_injec.c new file mode 100644 index 0000000..96f845e --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/adc_injec/adc_injec.c @@ -0,0 +1,174 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * Copyright (C) 2012 Piotr Esden-Tempski + * Copyright (C) 2012 Stephen Dwyer + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include + +static void usart_setup(void) +{ + /* Enable clocks for GPIO port A (for GPIO_USART1_TX) and USART1. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN); + + /* Setup GPIO pin GPIO_USART1_TX/GPIO9 on GPIO port A for transmit. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX); + + /* Setup UART parameters. */ + usart_set_baudrate(USART2, 115200); + usart_set_databits(USART2, 8); + usart_set_stopbits(USART2, USART_STOPBITS_1); + usart_set_mode(USART2, USART_MODE_TX_RX); + usart_set_parity(USART2, USART_PARITY_NONE); + usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART2); +} + +static void gpio_setup(void) +{ + /* Enable GPIO clocks. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + /* Setup the LEDs. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO8); + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO15); +} + +static void adc_setup(void) +{ + int i; + + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_ADC1EN); + + /* Make sure the ADC doesn't run during config. */ + adc_off(ADC1); + + /* We configure everything for one single injected conversion. */ + adc_disable_scan_mode(ADC1); + adc_set_single_conversion_mode(ADC1); + /* We can only use discontinuous mode on either the regular OR injected channels, not both */ + adc_disable_discontinuous_mode_regular(ADC1); + adc_enable_discontinuous_mode_injected(ADC1); + /* We want to start the injected conversion in software */ + adc_enable_external_trigger_injected(ADC1,ADC_CR2_JEXTSEL_JSWSTART); + adc_set_right_aligned(ADC1); + /* We want to read the temperature sensor, so we have to enable it. */ + adc_enable_temperature_sensor(ADC1); + adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC); + + adc_power_on(ADC1); + + /* Wait for ADC starting up. */ + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + + adc_reset_calibration(ADC1); + while ((ADC_CR2(ADC1) & ADC_CR2_RSTCAL) != 0); //added this check + adc_calibration(ADC1); + while ((ADC_CR2(ADC1) & ADC_CR2_CAL) != 0); //added this check +} + +static void my_usart_print_int(u32 usart, int value) +{ + s8 i; + u8 nr_digits = 0; + char buffer[25]; + + if (value < 0) { + usart_send_blocking(usart, '-'); + value = value * -1; + } + + while (value > 0) { + buffer[nr_digits++] = "0123456789"[value % 10]; + value /= 10; + } + + for (i = (nr_digits - 1); i >= 0; i--) { + usart_send_blocking(usart, buffer[i]); + } + + usart_send_blocking(usart, '\r'); +} + +int main(void) +{ + u8 channel_array[16]; + u16 temperature = 0; + + rcc_clock_setup_in_hse_12mhz_out_72mhz(); + gpio_setup(); + usart_setup(); + adc_setup(); + + gpio_set(GPIOA, GPIO8); /* LED1 on */ + gpio_set(GPIOC, GPIO15); /* LED2 on */ + + /* Send a message on USART1. */ + usart_send_blocking(USART2, 's'); + usart_send_blocking(USART2, 't'); + usart_send_blocking(USART2, 'm'); + usart_send_blocking(USART2, '\r'); + usart_send_blocking(USART2, '\n'); + + /* Select the channel we want to convert. 16=temperature_sensor. */ + channel_array[0] = 16; + /* Set the injected sequence here, with number of channels */ + adc_set_injected_sequence(ADC1, 1, channel_array); + + /* Continously convert and poll the temperature ADC. */ + while (1) { + /* + * If the ADC_CR2_ON bit is already set -> setting it another time + * starts a regular conversion. Injected conversion is started + * explicitly with the JSWSTART bit as an external trigger. It may + * also work by setting no regular channels and setting JAUTO to + * automatically convert the injected channels after the regular + * channels (of which there would be none). (Not tested.) + */ + adc_start_conversion_injected(ADC1); + + /* Wait for end of conversion. */ + while (!(adc_eoc_injected(ADC1))); + ADC_SR(ADC2) &= ~ADC_SR_JEOC; //clear injected end of conversion + + temperature = adc_read_injected(ADC1,1); //get the result from ADC_JDR1 on ADC1 (only bottom 16bits) + + /* + * That's actually not the real temperature - you have to compute it + * as described in the datasheet. + */ + my_usart_print_int(USART2, temperature); + + gpio_toggle(GPIOA, GPIO8); /* LED2 on */ + + } + + return 0; +} diff --git a/examples/stm32/f1/lisa-m-2/adc_injec_timtrig/Makefile b/examples/stm32/f1/lisa-m-2/adc_injec_timtrig/Makefile new file mode 100644 index 0000000..72eab49 --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/adc_injec_timtrig/Makefile @@ -0,0 +1,28 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = adc_injec_timtrig + +# Comment the following line if you _don't_ have luftboot flashed! +LDFLAGS += -Wl,-Ttext=0x8002000 +CFLAGS += -std=c99 +LDSCRIPT = ../lisa-m.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/lisa-m-2/adc_injec_timtrig/README b/examples/stm32/f1/lisa-m-2/adc_injec_timtrig/README new file mode 100644 index 0000000..d690f8a --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/adc_injec_timtrig/README @@ -0,0 +1,11 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This is a simple example that sends the value read out from the +temperature sensor ADC channel of the STM32 to the USART2. + +This example uses a timer trigger to automatically sample the adc channel. + +The terminal settings for the receiving device/PC are 115200 8n1. + diff --git a/examples/stm32/f1/lisa-m-2/adc_injec_timtrig/adc_injec_timtrig.c b/examples/stm32/f1/lisa-m-2/adc_injec_timtrig/adc_injec_timtrig.c new file mode 100644 index 0000000..704d0ad --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/adc_injec_timtrig/adc_injec_timtrig.c @@ -0,0 +1,195 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * Copyright (C) 2012 Piotr Esden-Tempski + * Copyright (C) 2012 Stephen Dwyer + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include + +static void usart_setup(void) +{ + /* Enable clocks for GPIO port A (for GPIO_USART1_TX) and USART1. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN); + + /* Setup GPIO pin GPIO_USART1_TX/GPIO9 on GPIO port A for transmit. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX); + + /* Setup UART parameters. */ + usart_set_baudrate(USART2, 115200); + usart_set_databits(USART2, 8); + usart_set_stopbits(USART2, USART_STOPBITS_1); + usart_set_mode(USART2, USART_MODE_TX_RX); + usart_set_parity(USART2, USART_PARITY_NONE); + usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART2); +} + +static void gpio_setup(void) +{ + /* Enable GPIO clocks. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + /* Setup the LEDs. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO8); + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO15); +} + +static void timer_setup(void) +{ + /* Set up the timer TIM2 for injected sampling */ + uint32_t timer; + volatile uint32_t *rcc_apbenr; + uint32_t rcc_apb; + + timer = TIM2; + rcc_apbenr = &RCC_APB1ENR; + rcc_apb = RCC_APB1ENR_TIM2EN; + + rcc_peripheral_enable_clock(rcc_apbenr, rcc_apb); + + /* Time Base configuration */ + timer_reset(timer); + timer_set_mode(timer, TIM_CR1_CKD_CK_INT, + TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); + timer_set_period(timer, 0xFF); + timer_set_prescaler(timer, 0x8); + timer_set_clock_division(timer, 0x0); + /* Generate TRGO on every update. */ + timer_set_master_mode(timer, TIM_CR2_MMS_UPDATE); + timer_enable_counter(timer); +} + +static void adc_setup(void) +{ + int i; + + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_ADC1EN); + + /* Make sure the ADC doesn't run during config. */ + adc_off(ADC1); + + /* We configure everything for one single timer triggered injected conversion. */ + adc_disable_scan_mode(ADC1); + adc_set_single_conversion_mode(ADC1); + /* We can only use discontinuous mode on either the regular OR injected channels, not both */ + adc_disable_discontinuous_mode_regular(ADC1); + adc_enable_discontinuous_mode_injected(ADC1); + /* We want to start the injected conversion with the TIM2 TRGO */ + adc_enable_external_trigger_injected(ADC1,ADC_CR2_JEXTSEL_TIM2_TRGO); + adc_set_right_aligned(ADC1); + /* We want to read the temperature sensor, so we have to enable it. */ + adc_enable_temperature_sensor(ADC1); + adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC); + + adc_power_on(ADC1); + + /* Wait for ADC starting up. */ + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + + adc_reset_calibration(ADC1); + while ((ADC_CR2(ADC1) & ADC_CR2_RSTCAL) != 0); + adc_calibration(ADC1); + while ((ADC_CR2(ADC1) & ADC_CR2_CAL) != 0); +} + +static void my_usart_print_int(u32 usart, int value) +{ + s8 i; + u8 nr_digits = 0; + char buffer[25]; + + if (value < 0) { + usart_send_blocking(usart, '-'); + value = value * -1; + } + + while (value > 0) { + buffer[nr_digits++] = "0123456789"[value % 10]; + value /= 10; + } + + for (i = (nr_digits - 1); i >= 0; i--) { + usart_send_blocking(usart, buffer[i]); + } + + usart_send_blocking(usart, '\r'); +} + +int main(void) +{ + u8 channel_array[16]; + u16 temperature = 0; + + rcc_clock_setup_in_hse_12mhz_out_72mhz(); + gpio_setup(); + usart_setup(); + timer_setup(); + adc_setup(); + + gpio_set(GPIOA, GPIO8); /* LED1 on */ + gpio_set(GPIOC, GPIO15); /* LED2 on */ + + /* Send a message on USART1. */ + usart_send_blocking(USART2, 's'); + usart_send_blocking(USART2, 't'); + usart_send_blocking(USART2, 'm'); + usart_send_blocking(USART2, '\r'); + usart_send_blocking(USART2, '\n'); + + /* Select the channel we want to convert. 16=temperature_sensor. */ + channel_array[0] = 16; + /* Set the injected sequence here, with number of channels */ + adc_set_injected_sequence(ADC1, 1, channel_array); + + /* Continously convert and poll the temperature ADC. */ + while (1) { + /* + * Since the injected sampling is triggered by the timer, it gets + * updated automatically, we just need to periodically read out the value. + * It would be better to check if the JEOC bit is set, and clear it following + * so that you do not read the same value twice, especially for a slower + * sampling rate. + */ + + temperature = adc_read_injected(ADC1,1); //get the result from ADC_JDR1 on ADC1 (only bottom 16bits) + + /* + * That's actually not the real temperature - you have to compute it + * as described in the datasheet. + */ + my_usart_print_int(USART2, temperature); + + gpio_toggle(GPIOA, GPIO8); /* LED2 on */ + + } + + return 0; +} diff --git a/examples/stm32/f1/lisa-m-2/adc_injec_timtrig_irq/Makefile b/examples/stm32/f1/lisa-m-2/adc_injec_timtrig_irq/Makefile new file mode 100644 index 0000000..8ad47e2 --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/adc_injec_timtrig_irq/Makefile @@ -0,0 +1,28 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = adc_injec_timtrig_irq + +# Comment the following line if you _don't_ have luftboot flashed! +LDFLAGS += -Wl,-Ttext=0x8002000 +CFLAGS += -std=c99 +LDSCRIPT = ../lisa-m.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/lisa-m-2/adc_injec_timtrig_irq/README b/examples/stm32/f1/lisa-m-2/adc_injec_timtrig_irq/README new file mode 100644 index 0000000..175edce --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/adc_injec_timtrig_irq/README @@ -0,0 +1,12 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This is a simple example that sends the value read out from the +temperature sensor ADC channel of the STM32 to the USART2. + +This example uses a timer trigger to sample an injected adc channel and +then uses an interrupt routine to retrieve the sample from the data register. + +The terminal settings for the receiving device/PC are 115200 8n1. + diff --git a/examples/stm32/f1/lisa-m-2/adc_injec_timtrig_irq/adc_injec_timtrig_irq.c b/examples/stm32/f1/lisa-m-2/adc_injec_timtrig_irq/adc_injec_timtrig_irq.c new file mode 100644 index 0000000..5a587a4 --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/adc_injec_timtrig_irq/adc_injec_timtrig_irq.c @@ -0,0 +1,211 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * Copyright (C) 2012 Piotr Esden-Tempski + * Copyright (C) 2012 Stephen Dwyer + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include + +volatile u16 temperature = 0; + +static void usart_setup(void) +{ + /* Enable clocks for GPIO port A (for GPIO_USART1_TX) and USART1. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN); + + /* Setup GPIO pin GPIO_USART1_TX/GPIO9 on GPIO port A for transmit. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX); + + /* Setup UART parameters. */ + usart_set_baudrate(USART2, 115200); + usart_set_databits(USART2, 8); + usart_set_stopbits(USART2, USART_STOPBITS_1); + usart_set_mode(USART2, USART_MODE_TX_RX); + usart_set_parity(USART2, USART_PARITY_NONE); + usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART2); +} + +static void gpio_setup(void) +{ + /* Enable GPIO clocks. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + /* Setup the LEDs. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO8); + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO15); +} + +static void timer_setup(void) +{ + /* Set up the timer TIM2 for injected sampling */ + uint32_t timer; + volatile uint32_t *rcc_apbenr; + uint32_t rcc_apb; + + timer = TIM2; + rcc_apbenr = &RCC_APB1ENR; + rcc_apb = RCC_APB1ENR_TIM2EN; + + rcc_peripheral_enable_clock(rcc_apbenr, rcc_apb); + + /* Time Base configuration */ + timer_reset(timer); + timer_set_mode(timer, TIM_CR1_CKD_CK_INT, + TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); + timer_set_period(timer, 0xFF); + timer_set_prescaler(timer, 0x8); + timer_set_clock_division(timer, 0x0); + /* Generate TRGO on every update. */ + timer_set_master_mode(timer, TIM_CR2_MMS_UPDATE); + timer_enable_counter(timer); +} + +static void irq_setup(void) +{ + /* Enable the adc1_2_isr() routine */ + nvic_set_priority(NVIC_ADC1_2_IRQ, 0); + nvic_enable_irq(NVIC_ADC1_2_IRQ); +} + +static void adc_setup(void) +{ + int i; + + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_ADC1EN); + + /* Make sure the ADC doesn't run during config. */ + adc_off(ADC1); + + /* We configure everything for one single timer triggered injected conversion with interrupt generation. */ + /* While not needed for a single channel, try out scan mode which does all channels in one sweep and + * generates the interrupt/EOC/JEOC flags set at the end of all channels, not each one. + */ + adc_enable_scan_mode(ADC1); + adc_set_single_conversion_mode(ADC1); + /* We want to start the injected conversion with the TIM2 TRGO */ + adc_enable_external_trigger_injected(ADC1,ADC_CR2_JEXTSEL_TIM2_TRGO); + /* Generate the ADC1_2_IRQ */ + adc_enable_eoc_interrupt_injected(ADC1); + adc_set_right_aligned(ADC1); + /* We want to read the temperature sensor, so we have to enable it. */ + adc_enable_temperature_sensor(ADC1); + adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC); + + adc_power_on(ADC1); + + /* Wait for ADC starting up. */ + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + + adc_reset_calibration(ADC1); + while ((ADC_CR2(ADC1) & ADC_CR2_RSTCAL) != 0); + adc_calibration(ADC1); + while ((ADC_CR2(ADC1) & ADC_CR2_CAL) != 0); +} + +static void my_usart_print_int(u32 usart, int value) +{ + s8 i; + u8 nr_digits = 0; + char buffer[25]; + + if (value < 0) { + usart_send_blocking(usart, '-'); + value = value * -1; + } + + while (value > 0) { + buffer[nr_digits++] = "0123456789"[value % 10]; + value /= 10; + } + + for (i = (nr_digits - 1); i >= 0; i--) { + usart_send_blocking(usart, buffer[i]); + } + + usart_send_blocking(usart, '\r'); +} + +int main(void) +{ + u8 channel_array[16]; + + rcc_clock_setup_in_hse_12mhz_out_72mhz(); + gpio_setup(); + usart_setup(); + timer_setup(); + irq_setup(); + adc_setup(); + + gpio_set(GPIOA, GPIO8); /* LED1 on */ + gpio_set(GPIOC, GPIO15); /* LED2 on */ + + /* Send a message on USART1. */ + usart_send_blocking(USART2, 's'); + usart_send_blocking(USART2, 't'); + usart_send_blocking(USART2, 'm'); + usart_send_blocking(USART2, '\r'); + usart_send_blocking(USART2, '\n'); + + /* Select the channel we want to convert. 16=temperature_sensor. */ + channel_array[0] = 16; + /* Set the injected sequence here, with number of channels */ + adc_set_injected_sequence(ADC1, 1, channel_array); + + /* Continously convert and poll the temperature ADC. */ + while (1) { + /* + * Since sampling is triggered by the timer and copying the value + * out of the data register is handled by the interrupt routine, + * we just need to print the value and toggle the LED. It may be useful + * to buffer the adc values in some cases. + */ + + /* + * That's actually not the real temperature - you have to compute it + * as described in the datasheet. + */ + my_usart_print_int(USART2, temperature); + + gpio_toggle(GPIOA, GPIO8); /* LED2 on */ + + } + + return 0; +} + +void adc1_2_isr(void) +{ + /* Clear Injected End Of Conversion (JEOC) */ + ADC_SR(ADC1) &= ~ADC_SR_JEOC; + temperature = adc_read_injected(ADC1,1); +} diff --git a/examples/stm32/f1/lisa-m-2/adc_injec_timtrig_irq_4ch/Makefile b/examples/stm32/f1/lisa-m-2/adc_injec_timtrig_irq_4ch/Makefile new file mode 100644 index 0000000..a3cccec --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/adc_injec_timtrig_irq_4ch/Makefile @@ -0,0 +1,28 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = adc_injec_timtrig_irq_4ch + +# Comment the following line if you _don't_ have luftboot flashed! +LDFLAGS += -Wl,-Ttext=0x8002000 +CFLAGS += -std=c99 +LDSCRIPT = ../lisa-m.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/lisa-m-2/adc_injec_timtrig_irq_4ch/README b/examples/stm32/f1/lisa-m-2/adc_injec_timtrig_irq_4ch/README new file mode 100644 index 0000000..0339a11 --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/adc_injec_timtrig_irq_4ch/README @@ -0,0 +1,12 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This is a simple example that sends the values read out from four ADC +channels of the STM32 to the USART2. + +This example uses a timer trigger to sample the injected adc channels and +then uses an interrupt routine to retrieve the samples from the data registers. + +The terminal settings for the receiving device/PC are 115200 8n1. + diff --git a/examples/stm32/f1/lisa-m-2/adc_injec_timtrig_irq_4ch/adc_injec_timtrig_irq_4ch.c b/examples/stm32/f1/lisa-m-2/adc_injec_timtrig_irq_4ch/adc_injec_timtrig_irq_4ch.c new file mode 100644 index 0000000..063a068 --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/adc_injec_timtrig_irq_4ch/adc_injec_timtrig_irq_4ch.c @@ -0,0 +1,230 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * Copyright (C) 2012 Piotr Esden-Tempski + * Copyright (C) 2012 Stephen Dwyer + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include + +volatile u16 temperature = 0; +volatile u16 v_refint = 0; +volatile u16 lisam_adc1 = 0; +volatile u16 lisam_adc2 = 0; +u8 channel_array[4]; /* for injected sampling, 4 channels max, for regular, 16 max */ + +static void usart_setup(void) +{ + /* Enable clocks for GPIO port A (for GPIO_USART1_TX) and USART1. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN); + + /* Setup GPIO pin GPIO_USART1_TX/GPIO9 on GPIO port A for transmit. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX); + + /* Setup UART parameters. */ + usart_set_baudrate(USART2, 115200); + usart_set_databits(USART2, 8); + usart_set_stopbits(USART2, USART_STOPBITS_1); + usart_set_mode(USART2, USART_MODE_TX_RX); + usart_set_parity(USART2, USART_PARITY_NONE); + usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART2); +} + +static void gpio_setup(void) +{ + /* Enable GPIO clocks. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + /* Setup the LEDs. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO8); + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO15); + + /* Setup Lisa/M v2 ADC1,2 on ANALOG1 connector */ + gpio_set_mode(GPIOC, GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, \ + GPIO3 | GPIO0 ); +} + +static void timer_setup(void) +{ + /* Set up the timer TIM2 for injected sampling */ + uint32_t timer; + volatile uint32_t *rcc_apbenr; + uint32_t rcc_apb; + + timer = TIM2; + rcc_apbenr = &RCC_APB1ENR; + rcc_apb = RCC_APB1ENR_TIM2EN; + + rcc_peripheral_enable_clock(rcc_apbenr, rcc_apb); + + /* Time Base configuration */ + timer_reset(timer); + timer_set_mode(timer, TIM_CR1_CKD_CK_INT, + TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); + timer_set_period(timer, 0xFF); + timer_set_prescaler(timer, 0x8); + timer_set_clock_division(timer, 0x0); + /* Generate TRGO on every update. */ + timer_set_master_mode(timer, TIM_CR2_MMS_UPDATE); + timer_enable_counter(timer); +} + +static void irq_setup(void) +{ + /* Enable the adc1_2_isr() routine */ + nvic_set_priority(NVIC_ADC1_2_IRQ, 0); + nvic_enable_irq(NVIC_ADC1_2_IRQ); +} + +static void adc_setup(void) +{ + int i; + + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_ADC1EN); + + /* Make sure the ADC doesn't run during config. */ + adc_off(ADC1); + + /* We configure everything for one single timer triggered injected conversion with interrupt generation. */ + /* While not needed for a single channel, try out scan mode which does all channels in one sweep and + * generates the interrupt/EOC/JEOC flags set at the end of all channels, not each one. + */ + adc_enable_scan_mode(ADC1); + adc_set_single_conversion_mode(ADC1); + /* We want to start the injected conversion with the TIM2 TRGO */ + adc_enable_external_trigger_injected(ADC1,ADC_CR2_JEXTSEL_TIM2_TRGO); + /* Generate the ADC1_2_IRQ */ + adc_enable_eoc_interrupt_injected(ADC1); + adc_set_right_aligned(ADC1); + /* We want to read the temperature sensor, so we have to enable it. */ + adc_enable_temperature_sensor(ADC1); + adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC); + + /* Select the channels we want to convert. + * 16=temperature_sensor, 17=Vrefint, 13=ADC1, 10=ADC2 + */ + channel_array[0] = 16; + channel_array[1] = 17; + channel_array[2] = 13; + channel_array[3] = 10; + adc_set_injected_sequence(ADC1, 4, channel_array); + + adc_power_on(ADC1); + + /* Wait for ADC starting up. */ + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + + adc_reset_calibration(ADC1); + while ((ADC_CR2(ADC1) & ADC_CR2_RSTCAL) != 0); //added this check + adc_calibration(ADC1); + while ((ADC_CR2(ADC1) & ADC_CR2_CAL) != 0); //added this check +} + +static void my_usart_print_int(u32 usart, int value) +{ + s8 i; + u8 nr_digits = 0; + char buffer[25]; + + if (value < 0) { + usart_send_blocking(usart, '-'); + value = value * -1; + } + + while (value > 0) { + buffer[nr_digits++] = "0123456789"[value % 10]; + value /= 10; + } + + for (i = (nr_digits - 1); i >= 0; i--) { + usart_send_blocking(usart, buffer[i]); + } + + //usart_send_blocking(usart, '\r'); +} + +int main(void) +{ + + rcc_clock_setup_in_hse_12mhz_out_72mhz(); + gpio_setup(); + usart_setup(); + timer_setup(); + irq_setup(); + adc_setup(); + + gpio_set(GPIOA, GPIO8); /* LED1 off */ + gpio_set(GPIOC, GPIO15); /* LED5 off */ + + /* Send a message on USART1. */ + usart_send_blocking(USART2, 's'); + usart_send_blocking(USART2, 't'); + usart_send_blocking(USART2, 'm'); + usart_send_blocking(USART2, '\r'); + usart_send_blocking(USART2, '\n'); + + /* Moved the channel selection and sequence init to adc_setup() */ + + /* Continously convert and poll the temperature ADC. */ + while (1) { + /* + * Since sampling is triggered by the timer and copying the values + * out of the data registers is handled by the interrupt routine, + * we just need to print the values and toggle the LED. It may be useful + * to buffer the adc values in some cases. + */ + + my_usart_print_int(USART2, temperature); + usart_send_blocking(USART2, ' '); + my_usart_print_int(USART2, v_refint); + usart_send_blocking(USART2, ' '); + my_usart_print_int(USART2, lisam_adc1); + usart_send_blocking(USART2, ' '); + my_usart_print_int(USART2, lisam_adc2); + usart_send_blocking(USART2, '\r'); + + gpio_toggle(GPIOA, GPIO8); /* LED2 on */ + + } + + return 0; +} + +void adc1_2_isr(void) +{ + /* Clear Injected End Of Conversion (JEOC) */ + ADC_SR(ADC1) &= ~ADC_SR_JEOC; + temperature = adc_read_injected(ADC1,1); + v_refint = adc_read_injected(ADC1,2); + lisam_adc1 = adc_read_injected(ADC1,3); + lisam_adc2 = adc_read_injected(ADC1,4); +} diff --git a/examples/stm32/f1/lisa-m-2/adc_regular/Makefile b/examples/stm32/f1/lisa-m-2/adc_regular/Makefile new file mode 100644 index 0000000..b61587f --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/adc_regular/Makefile @@ -0,0 +1,28 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = adc + +# Comment the following line if you _don't_ have luftboot flashed! +LDFLAGS += -Wl,-Ttext=0x8002000 +CFLAGS += -std=c99 +LDSCRIPT = ../lisa-m.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/lisa-m-2/adc_regular/README b/examples/stm32/f1/lisa-m-2/adc_regular/README new file mode 100644 index 0000000..3b583ff --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/adc_regular/README @@ -0,0 +1,9 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This is a simple polling example that sends the value read out from the +temperature sensor ADC channel of the STM32 to the USART2. + +The terminal settings for the receiving device/PC are 115200 8n1. + diff --git a/examples/stm32/f1/lisa-m-2/adc_regular/adc.c b/examples/stm32/f1/lisa-m-2/adc_regular/adc.c new file mode 100644 index 0000000..6bef8a0 --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/adc_regular/adc.c @@ -0,0 +1,161 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * Copyright (C) 2012 Piotr Esden-Tempski + * Copyright (C) 2012 Ken Sarkies + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include + +static void usart_setup(void) +{ + /* Enable clocks for GPIO port A (for GPIO_USART1_TX) and USART1. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN); + + /* Setup GPIO pin GPIO_USART1_TX/GPIO9 on GPIO port A for transmit. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX); + + /* Setup UART parameters. */ + usart_set_baudrate(USART2, 115200); + usart_set_databits(USART2, 8); + usart_set_stopbits(USART2, USART_STOPBITS_1); + usart_set_mode(USART2, USART_MODE_TX_RX); + usart_set_parity(USART2, USART_PARITY_NONE); + usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART2); +} + +static void gpio_setup(void) +{ + /* Enable GPIO clocks. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + /* Setup the LEDs. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO8); + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO15); +} + +static void adc_setup(void) +{ + int i; + + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_ADC1EN); + + /* Make sure the ADC doesn't run during config. */ + adc_off(ADC1); + + /* We configure everything for one single conversion. */ + adc_disable_scan_mode(ADC1); + adc_set_single_conversion_mode(ADC1); + adc_disable_external_trigger_regular(ADC1); + adc_set_right_aligned(ADC1); + /* We want to read the temperature sensor, so we have to enable it. */ + adc_enable_temperature_sensor(ADC1); + adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC); + + adc_power_on(ADC1); + + /* Wait for ADC starting up. */ + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + + adc_reset_calibration(ADC1); + adc_calibration(ADC1); +} + +static void my_usart_print_int(u32 usart, int value) +{ + s8 i; + u8 nr_digits = 0; + char buffer[25]; + + if (value < 0) { + usart_send_blocking(usart, '-'); + value = value * -1; + } + + while (value > 0) { + buffer[nr_digits++] = "0123456789"[value % 10]; + value /= 10; + } + + for (i = nr_digits; i >= 0; i--) { + usart_send_blocking(usart, buffer[i]); + } + + usart_send_blocking(usart, '\r'); +} + +int main(void) +{ + u8 channel_array[16]; + u16 temperature = 0; + + rcc_clock_setup_in_hse_12mhz_out_72mhz(); + gpio_setup(); + usart_setup(); + adc_setup(); + + gpio_set(GPIOA, GPIO8); /* LED1 on */ + gpio_set(GPIOC, GPIO15); /* LED2 off */ + + /* Send a message on USART1. */ + usart_send_blocking(USART2, 's'); + usart_send_blocking(USART2, 't'); + usart_send_blocking(USART2, 'm'); + usart_send_blocking(USART2, '\r'); + usart_send_blocking(USART2, '\n'); + + /* Select the channel we want to convert. 16=temperature_sensor. */ + channel_array[0] = 16; + adc_set_regular_sequence(ADC1, 1, channel_array); + + /* Continously convert and poll the temperature ADC. */ + while (1) { + /* + * Start the conversion directly (ie without a trigger). + */ + adc_start_conversion_direct(ADC1); + + /* Wait for end of conversion. */ + while (!(adc_eoc(ADC1))); + + temperature = adc_read_regular(ADC1); + + /* + * That's actually not the real temperature - you have to compute it + * as described in the datasheet. + */ + my_usart_print_int(USART2, temperature); + + gpio_toggle(GPIOA, GPIO8); /* LED2 on */ + + } + + return 0; +} diff --git a/examples/stm32/f1/lisa-m-2/can/Makefile b/examples/stm32/f1/lisa-m-2/can/Makefile new file mode 100644 index 0000000..71a787a --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/can/Makefile @@ -0,0 +1,28 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = can + +# Comment the following line if you _don't_ have luftboot flashed! +LDFLAGS += -Wl,-Ttext=0x8002000 +CFLAGS += -std=c99 +LDSCRIPT = ../lisa-m.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/lisa-m-2/can/README b/examples/stm32/f1/lisa-m-2/can/README new file mode 100644 index 0000000..3a14e3b --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/can/README @@ -0,0 +1,4 @@ +This test sets up the CAN interface on Lisa/M and transmits 8 bites every +100ms. The first byte is being incremented in each cycle. The demo also +receives messages and is displaing the first 4 bits of the first byte on the +board LEDs. diff --git a/examples/stm32/f1/lisa-m-2/can/can.c b/examples/stm32/f1/lisa-m-2/can/can.c new file mode 100644 index 0000000..c9fb0eb --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/can/can.c @@ -0,0 +1,237 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * Copyright (C) 2010-2011 Piotr Esden-Tempski + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include + +struct can_tx_msg { + u32 std_id; + u32 ext_id; + u8 ide; + u8 rtr; + u8 dlc; + u8 data[8]; +}; + +struct can_rx_msg { + u32 std_id; + u32 ext_id; + u8 ide; + u8 rtr; + u8 dlc; + u8 data[8]; + u8 fmi; +}; + +struct can_tx_msg can_tx_msg; +struct can_rx_msg can_rx_msg; + +static void gpio_setup(void) +{ + /* Enable Alternate Function clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); + + /* Enable GPIOA clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + + /* Enable GPIOB clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); + + /* Enable GPIOC clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + /* Preconfigure LEDs. */ + gpio_set(GPIOA, GPIO8); /* LED1 off */ + gpio_set(GPIOB, GPIO4); /* LED2 off */ + gpio_set(GPIOC, GPIO2); /* LED3 off */ + gpio_set(GPIOC, GPIO5); /* LED4 off */ + gpio_set(GPIOC, GPIO15); /* LED5 off */ + + /* Configure LED GPIOOs. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO8); + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO4); + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO2); + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO5); + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO15); + + /* Configure PB4 as GPIO. */ + AFIO_MAPR |= AFIO_MAPR_SWJ_CFG_FULL_SWJ_NO_JNTRST; + +} + +static void systick_setup(void) +{ + /* 72MHz / 8 => 9000000 counts per second */ + systick_set_clocksource(STK_CTRL_CLKSOURCE_AHB_DIV8); + + /* 9000000/9000 = 1000 overflows per second - every 1ms one interrupt */ + /* SysTick interrupt every N clock pulses: set reload to N-1 */ + systick_set_reload(8999); + + systick_interrupt_enable(); + + /* Start counting. */ + systick_counter_enable(); +} + +static void can_setup(void) +{ + /* Enable peripheral clocks. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_CAN1EN); + + AFIO_MAPR |= AFIO_MAPR_CAN1_REMAP_PORTB; + + /* Configure CAN pin: RX (input pull-up). */ + gpio_set_mode(GPIO_BANK_CAN1_PB_RX, GPIO_MODE_INPUT, + GPIO_CNF_INPUT_PULL_UPDOWN, GPIO_CAN1_PB_RX); + gpio_set(GPIO_BANK_CAN1_PB_RX, GPIO_CAN1_PB_RX); + + /* Configure CAN pin: TX. */ + gpio_set_mode(GPIO_BANK_CAN1_PB_TX, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_CAN1_PB_TX); + + /* NVIC setup. */ + nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ); + nvic_set_priority(NVIC_USB_LP_CAN_RX0_IRQ, 1); + + /* Reset CAN. */ + can_reset(CAN1); + + /* CAN cell init. */ + if (can_init(CAN1, + false, /* TTCM: Time triggered comm mode? */ + true, /* ABOM: Automatic bus-off management? */ + false, /* AWUM: Automatic wakeup mode? */ + false, /* NART: No automatic retransmission? */ + false, /* RFLM: Receive FIFO locked mode? */ + false, /* TXFP: Transmit FIFO priority? */ + CAN_BTR_SJW_1TQ, + CAN_BTR_TS1_3TQ, + CAN_BTR_TS2_4TQ, + 12, + false, + false)) /* BRP+1: Baud rate prescaler */ + { + gpio_set(GPIOA, GPIO8); /* LED1 off */ + gpio_set(GPIOB, GPIO4); /* LED2 off */ + gpio_set(GPIOC, GPIO2); /* LED3 off */ + gpio_clear(GPIOC, GPIO5); /* LED4 on */ + gpio_set(GPIOC, GPIO15); /* LED5 off */ + + /* Die because we failed to initialize. */ + while (1) + __asm__("nop"); + } + + /* CAN filter 0 init. */ + can_filter_id_mask_32bit_init(CAN1, + 0, /* Filter ID */ + 0, /* CAN ID */ + 0, /* CAN ID mask */ + 0, /* FIFO assignment (here: FIFO0) */ + true); /* Enable the filter. */ + + /* Enable CAN RX interrupt. */ + can_enable_irq(CAN1, CAN_IER_FMPIE0); +} + +void sys_tick_handler(void) +{ + static int temp32 = 0; + static u8 data[8] = {0, 1, 2, 0, 0, 0, 0, 0}; + + /* We call this handler every 1ms so every 100ms = 0.1s + * resulting in 100Hz message rate. + */ + if (++temp32 != 100) + return; + + temp32 = 0; + + /* Transmit CAN frame. */ + data[0]++; + if (can_transmit(CAN1, + 0, /* (EX/ST)ID: CAN ID */ + false, /* IDE: CAN ID extended? */ + false, /* RTR: Request transmit? */ + 8, /* DLC: Data length */ + data) == -1) + { + gpio_set(GPIOA, GPIO8); /* LED1 off */ + gpio_set(GPIOB, GPIO4); /* LED2 off */ + gpio_set(GPIOC, GPIO2); /* LED3 off */ + gpio_set(GPIOC, GPIO5); /* LED4 off */ + gpio_clear(GPIOC, GPIO15); /* LED5 on */ + } +} + +void usb_lp_can_rx0_isr(void) +{ + u32 id, fmi; + bool ext, rtr; + u8 length, data[8]; + + can_receive(CAN1, 0, false, &id, &ext, &rtr, &fmi, &length, data); + + if (data[0] & 1) + gpio_clear(GPIOA, GPIO8); + else + gpio_set(GPIOA, GPIO8); + + if (data[0] & 2) + gpio_clear(GPIOB, GPIO4); + else + gpio_set(GPIOB, GPIO4); + + if (data[0] & 4) + gpio_clear(GPIOC, GPIO2); + else + gpio_set(GPIOC, GPIO2); + + if (data[0] & 8) + gpio_clear(GPIOC, GPIO5); + else + gpio_set(GPIOC, GPIO5); + + can_fifo_release(CAN1, 0); +} + +int main(void) +{ + rcc_clock_setup_in_hse_12mhz_out_72mhz(); + gpio_setup(); + can_setup(); + systick_setup(); + + while (1); /* Halt. */ + + return 0; +} diff --git a/examples/stm32/f1/lisa-m-2/can/can.c~ b/examples/stm32/f1/lisa-m-2/can/can.c~ new file mode 100644 index 0000000..41432b6 --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/can/can.c~ @@ -0,0 +1,232 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * Copyright (C) 2010 Piotr Esden-Tempski + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include + +struct can_tx_msg { + u32 std_id; + u32 ext_id; + u8 ide; + u8 rtr; + u8 dlc; + u8 data[8]; +}; + +struct can_rx_msg { + u32 std_id; + u32 ext_id; + u8 ide; + u8 rtr; + u8 dlc; + u8 data[8]; + u8 fmi; +}; + +struct can_tx_msg can_tx_msg; +struct can_rx_msg can_rx_msg; + +void gpio_setup(void) +{ + /* Enable Alternate Function clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); + + /* Enable GPIOA clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + + /* Enable GPIOB clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); + + /* Enable GPIOC clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + /* Preconfigure LEDs. */ + gpio_set(GPIOA, GPIO8); /* LED0 off */ + gpio_set(GPIOB, GPIO4); /* LED1 off */ + gpio_set(GPIOC, GPIO15); /* LED2 off */ + gpio_set(GPIOC, GPIO2); /* LED3 off */ + gpio_set(GPIOC, GPIO5); /* LED4 off */ + + /* Configure LED GPIOOs. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO8); + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO4); + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO15); + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO2); + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO5); + + /* Configure PB4 as GPIO. */ + AFIO_MAPR |= AFIO_MAPR_SWJ_CFG_FULL_SWJ_NO_JNTRST; + +} + +void systick_setup(void) +{ + /* 72MHz / 8 => 9000000 counts per second */ + systick_set_clocksource(STK_CTRL_CLKSOURCE_AHB_DIV8); + + /* 9000000/9000 = 1000 overflows per second - every 1ms one interrupt */ + systick_set_reload(9000); + + systick_interrupt_enable(); + + /* Start counting. */ + systick_counter_enable(); +} + +void can_setup(void) +{ + /* Enable peripheral clocks. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_CANEN); + + AFIO_MAPR = AFIO_MAPR_CAN1_REMAP_PORTB; + + /* Configure CAN pin: RX (input pull-up). */ + gpio_set_mode(GPIOB, GPIO_MODE_INPUT, + GPIO_CNF_INPUT_PULL_UPDOWN, GPIO_CAN1_PB_RX); + gpio_set(GPIOB, GPIO_CAN1_PB_RX); + + /* Configure CAN pin: TX. */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_CAN1_PB_TX); + + /* NVIC setup. */ + nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ); + nvic_set_priority(NVIC_USB_LP_CAN_RX0_IRQ, 1); + + /* Reset CAN. */ + can_reset(CAN1); + + /* CAN cell init. */ + if (can_init(CAN1, + false, /* TTCM: Time triggered comm mode? */ + true, /* ABOM: Automatic bus-off management? */ + false, /* AWUM: Automatic wakeup mode? */ + false, /* NART: No automatic retransmission? */ + false, /* RFLM: Receive FIFO locked mode? */ + false, /* TXFP: Transmit FIFO priority? */ + CAN_BTR_SJW_1TQ, + CAN_BTR_TS1_3TQ, + CAN_BTR_TS2_4TQ, + 12)) /* BRP+1: Baud rate prescaler */ + { + gpio_set(GPIOA, GPIO8); /* LED0 off */ + gpio_set(GPIOB, GPIO4); /* LED1 off */ + gpio_set(GPIOC, GPIO15); /* LED2 off */ + gpio_clear(GPIOC, GPIO2); /* LED3 on */ + gpio_set(GPIOC, GPIO5); /* LED4 off */ + + /* Die because we failed to initialize. */ + while (1) + __asm__("nop"); + } + + /* CAN filter 0 init. */ + can_filter_id_mask_32bit_init(CAN1, + 0, /* Filter ID */ + 0, /* CAN ID */ + 0, /* CAN ID mask */ + 0, /* FIFO assignment (here: FIFO0) */ + true); /* Enable the filter. */ + + /* Enable CAN RX interrupt. */ + can_enable_irq(CAN1, CAN_IER_FMPIE0); +} + +void sys_tick_handler(void) +{ + static int temp32 = 0; + static u8 data[8] = {0, 1, 2, 0, 0, 0, 0, 0}; + + /* We call this handler every 1ms so 1000ms = 1s on/off. */ + if (++temp32 != 1000) + return; + + temp32 = 0; + + /* Transmit CAN frame. */ + data[0]++; + if (can_transmit(CAN1, + 0, /* (EX/ST)ID: CAN ID */ + false, /* IDE: CAN ID extended? */ + false, /* RTR: Request transmit? */ + 8, /* DLC: Data length */ + data) == -1) + { + gpio_set(GPIOA, GPIO8); /* LED0 off */ + gpio_set(GPIOB, GPIO4); /* LED1 off */ + gpio_set(GPIOC, GPIO15); /* LED2 off */ + gpio_set(GPIOC, GPIO2); /* LED3 off */ + gpio_clear(GPIOC, GPIO5); /* LED4 on */ + } +} + +void usb_lp_can_rx0_isr(void) +{ + u32 id, fmi; + bool ext, rtr; + u8 length, data[8]; + + can_receive(CAN1, 0, false, &id, &ext, &rtr, &fmi, &length, data); + + if (data[0] & 1) + gpio_clear(GPIOA, GPIO8); + else + gpio_set(GPIOA, GPIO8); + + if (data[0] & 2) + gpio_clear(GPIOB, GPIO4); + else + gpio_set(GPIOB, GPIO4); + + if (data[0] & 4) + gpio_clear(GPIOC, GPIO15); + else + gpio_set(GPIOC, GPIO15); + + if (data[0] & 8) + gpio_clear(GPIOC, GPIO2); + else + gpio_set(GPIOC, GPIO2); + + can_fifo_release(CAN1, 0); +} + +int main(void) +{ + rcc_clock_setup_in_hse_12mhz_out_72mhz(); + gpio_setup(); + can_setup(); + systick_setup(); + + while (1); /* Halt. */ + + return 0; +} diff --git a/examples/stm32/f1/lisa-m-2/fancyblink/Makefile b/examples/stm32/f1/lisa-m-2/fancyblink/Makefile new file mode 100644 index 0000000..90ce834 --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/fancyblink/Makefile @@ -0,0 +1,28 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = fancyblink + +# Comment the following line if you _don't_ have luftboot flashed! +LDFLAGS += -Wl,-Ttext=0x8002000 +CFLAGS += -std=c99 +LDSCRIPT = ../lisa-m.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/lisa-m-2/fancyblink/fancyblink.c b/examples/stm32/f1/lisa-m-2/fancyblink/fancyblink.c new file mode 100644 index 0000000..72994b2 --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/fancyblink/fancyblink.c @@ -0,0 +1,148 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * Copyright (C) 2011 Piotr Esden-Tempski + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include + +/* Set STM32 to 72 MHz. */ +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_12mhz_out_72mhz(); + + /* Enable GPIOA, GPIOB, GPIOC, and AFIO clocks. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); +} + +static void gpio_setup(void) +{ + /* LED1 */ + /* Set GPIO8 (in GPIO port A) to 'output push-pull'. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO8); + + /* LED2 */ + /* Set GPIO15 (in GPIO port C) to 'output push-pull'. */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO15); + + /* JTAG_TRST */ + /* Set GPIO4 (in GPIO port B) to 'output push-pull'. */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO4); + + AFIO_MAPR |= AFIO_MAPR_SWJ_CFG_FULL_SWJ_NO_JNTRST; + + /* ADC4 */ + /* Set GPIO5 (in GPIO port C) to 'output push-pull'. */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO5); + + /* ADC6 */ + /* Set GPIO2 (in GPIO port C) to 'output push-pull'. */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO2); + + /* Preconfigure the LEDs. */ + gpio_set(GPIOA, GPIO8); + gpio_set(GPIOC, GPIO15); + gpio_set(GPIOB, GPIO4); + gpio_set(GPIOC, GPIO5); + gpio_set(GPIOC, GPIO2); +} + +static void led_set(int id, int on) +{ + if (on) { + switch (id) { + case 0: + gpio_clear(GPIOA, GPIO8); /* LED1 On */ + break; + case 1: + gpio_clear(GPIOB, GPIO4); /* JTAG_TRST On */ + break; + case 2: + gpio_clear(GPIOC, GPIO2); /* ADC6 On */ + break; + case 3: + gpio_clear(GPIOC, GPIO5); /* ADC4 On */ + break; + case 4: + gpio_clear(GPIOC, GPIO15); /* LED2 On */ + break; + } + } else { + switch (id) { + case 0: + gpio_set(GPIOA, GPIO8); /* LED1 On */ + break; + case 1: + gpio_set(GPIOB, GPIO4); /* JTAG_TRST On */ + break; + case 2: + gpio_set(GPIOC, GPIO2); /* ADC6 On */ + break; + case 3: + gpio_set(GPIOC, GPIO5); /* ADC4 On */ + break; + case 4: + gpio_set(GPIOC, GPIO15); /* LED2 On */ + break; + } + } +} + +static void led_advance(void) +{ + static int state = 0; + + if (state < 5) { + led_set(state, 1); + } else if (state < 10) { + led_set(state - 5, 0); + } else if (state < 15) { + led_set(14 - state, 1); + } else if (state < 20) { + led_set(19 - state, 0); + } + + state++; + if(state == 20) state = 0; + +} + +int main(void) +{ + int i; + + clock_setup(); + gpio_setup(); + + /* Blink the LEDs (PC13 and PB4) on the board. */ + while (1) { + led_advance(); + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/stm32/f1/lisa-m-2/lisa-m.ld b/examples/stm32/f1/lisa-m-2/lisa-m.ld new file mode 100644 index 0000000..55711e7 --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/lisa-m.ld @@ -0,0 +1,31 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* Linker script for Lisa-M (STM32F103RBT6, 128K flash, 20K RAM). */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_stm32f1.ld + diff --git a/examples/stm32/f1/lisa-m-2/spi/Makefile b/examples/stm32/f1/lisa-m-2/spi/Makefile new file mode 100644 index 0000000..9d90a1c --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/spi/Makefile @@ -0,0 +1,28 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = spi + +# Comment the following line if you _don't_ have luftboot flashed! +LDFLAGS += -Wl,-Ttext=0x8002000 +CFLAGS += -std=c99 +LDSCRIPT = ../lisa-m.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/lisa-m-2/spi/README b/examples/stm32/f1/lisa-m-2/spi/README new file mode 100644 index 0000000..829f2f4 --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/spi/README @@ -0,0 +1,14 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example program demonstrates simple SPI transceive on Lisa/M 2.0 board +(http://paparazzi.enac.fr/wiki/Lisa/M_v20 for details). + +The terminal settings for the receiving device/PC are 9600 8n1. + +The example expects a loopback connection between the MISO and MOSI pins on +SPI1. + +A counter increments and the spi sends this byte out, after which it should +come back in to the rx buffer, and the result is reported on the uart. diff --git a/examples/stm32/f1/lisa-m-2/spi/spi.c b/examples/stm32/f1/lisa-m-2/spi/spi.c new file mode 100644 index 0000000..bf28207 --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/spi/spi.c @@ -0,0 +1,164 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * Copyright (C) 2013 Stephen Dwyer + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +int _write(int file, char *ptr, int len); + +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_12mhz_out_72mhz(); + + /* Enable GPIOA, GPIOB, GPIOC clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, + RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN | + RCC_APB2ENR_IOPCEN); + + /* Enable clocks for GPIO port A (for GPIO_USART2_TX) and USART2. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN | + RCC_APB2ENR_AFIOEN); + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN); + + /* Enable SPI1 Periph and gpio clocks */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, + RCC_APB2ENR_SPI1EN); + +} + +static void spi_setup(void) { + + /* Configure GPIOs: SS=PA4, SCK=PA5, MISO=PA6 and MOSI=PA7 */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO4 | + GPIO5 | + GPIO7 ); + + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, + GPIO6); + + /* Reset SPI, SPI_CR1 register cleared, SPI is disabled */ + spi_reset(SPI1); + + /* Set up SPI in Master mode with: + * Clock baud rate: 1/64 of peripheral clock frequency + * Clock polarity: Idle High + * Clock phase: Data valid on 2nd clock pulse + * Data frame format: 8-bit + * Frame format: MSB First + */ + spi_init_master(SPI1, SPI_CR1_BAUDRATE_FPCLK_DIV_64, SPI_CR1_CPOL_CLK_TO_1_WHEN_IDLE, + SPI_CR1_CPHA_CLK_TRANSITION_2, SPI_CR1_DFF_8BIT, SPI_CR1_MSBFIRST); + + /* + * Set NSS management to software. + * + * Note: + * Setting nss high is very important, even if we are controlling the GPIO + * ourselves this bit needs to be at least set to 1, otherwise the spi + * peripheral will not send any data out. + */ + spi_enable_software_slave_management(SPI1); + spi_set_nss_high(SPI1); + + /* Enable SPI1 periph. */ + spi_enable(SPI1); +} + +static void usart_setup(void) +{ + /* Setup GPIO pin GPIO_USART2_TX and GPIO_USART2_RX. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX); + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, + GPIO_CNF_INPUT_FLOAT, GPIO_USART2_RX); + + /* Setup UART parameters. */ + usart_set_baudrate(USART2, 9600); + usart_set_databits(USART2, 8); + usart_set_stopbits(USART2, USART_STOPBITS_1); + usart_set_mode(USART2, USART_MODE_TX_RX); + usart_set_parity(USART2, USART_PARITY_NONE); + usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART2); +} + +int _write(int file, char *ptr, int len) +{ + int i; + + if (file == 1) { + for (i = 0; i < len; i++) + usart_send_blocking(USART2, ptr[i]); + return i; + } + + errno = EIO; + return -1; +} + +static void gpio_setup(void) +{ + /* Set GPIO8 (in GPIO port A) to 'output push-pull'. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO8); +} + +int main(void) +{ + int counter = 0; + u16 rx_value = 0x42; + + clock_setup(); + gpio_setup(); + usart_setup(); + spi_setup(); + + + /* Blink the LED (PA8) on the board with every transmitted byte. */ + while (1) { + /* LED on/off */ + gpio_toggle(GPIOA, GPIO8); + + /* printf the value that SPI should send */ + printf("Counter: %i SPI Sent Byte: %i", counter, (uint8_t) counter); + /* blocking send of the byte out SPI1 */ + spi_send(SPI1, (uint8_t) counter); + /* Read the byte that just came in (use a loopback between MISO and MOSI + * to get the same byte back) + */ + rx_value = spi_read(SPI1); + /* printf the byte just received */ + printf(" SPI Received Byte: %i\r\n", rx_value); + + counter++; + + } + + return 0; +} diff --git a/examples/stm32/f1/lisa-m-2/spi_dma/Makefile b/examples/stm32/f1/lisa-m-2/spi_dma/Makefile new file mode 100644 index 0000000..9aff3f1 --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/spi_dma/Makefile @@ -0,0 +1,28 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = spi_dma + +# Comment the following line if you _don't_ have luftboot flashed! +LDFLAGS += -Wl,-Ttext=0x8002000 +CFLAGS += -std=c99 +LDSCRIPT = ../lisa-m.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/lisa-m-2/spi_dma/README b/examples/stm32/f1/lisa-m-2/spi_dma/README new file mode 100644 index 0000000..3c03e2d --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/spi_dma/README @@ -0,0 +1,17 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example program demonstrates SPI transceive with DMA on Lisa/M 2.0 board +(http://paparazzi.enac.fr/wiki/Lisa/M_v20 for details). + +The terminal settings for the receiving device/PC are 9600 8n1. + +The example expects a loopback connection between the MISO and MOSI pins on +SPI1. The DRDY and SS pins (on the Lisa/M v2.0 SPI1 connector) are used as +GPIO to time the tx and rx ISRs, respectively. Use a scope or logic analyzer. + +The tx length is incremented, followed by the rx length, after which both tx +and rx lengths are decremented together. The case where rx is longer than tx +requires greater complexity to ensure all rx data is clocked in. See the adv +example for this. diff --git a/examples/stm32/f1/lisa-m-2/spi_dma/spi_dma.c b/examples/stm32/f1/lisa-m-2/spi_dma/spi_dma.c new file mode 100644 index 0000000..e66d2b6 --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/spi_dma/spi_dma.c @@ -0,0 +1,439 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * Copyright (C) 2013 Stephen Dwyer + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef USE_16BIT_TRANSFERS +#define USE_16BIT_TRANSFERS 1 +#endif + +/* This is for the counter state flag */ +typedef enum { + TX_UP_RX_HOLD = 0, + TX_HOLD_RX_UP, + TX_DOWN_RX_DOWN +} cnt_state; + +/* This is a global spi state flag */ +typedef enum { + NONE = 0, + ONE, + DONE +} trans_status; + +volatile trans_status transceive_status; + +int _write(int file, char *ptr, int len); + +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_12mhz_out_72mhz(); + + /* Enable GPIOA, GPIOB, GPIOC clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, + RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN | + RCC_APB2ENR_IOPCEN); + + /* Enable clocks for GPIO port A (for GPIO_USART2_TX) and USART2. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN | + RCC_APB2ENR_AFIOEN); + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN); + + /* Enable SPI1 Periph and gpio clocks */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, + RCC_APB2ENR_SPI1EN); + + /* Enable DMA1 clock */ + rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_DMA1EN); +} + +static void spi_setup(void) { + + /* Configure GPIOs: SS=PA4, SCK=PA5, MISO=PA6 and MOSI=PA7 + * For now ignore the SS pin so we can use it to time the ISRs + */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, /* GPIO4 | */ + GPIO5 | + GPIO7 ); + + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, + GPIO6); + + /* Reset SPI, SPI_CR1 register cleared, SPI is disabled */ + spi_reset(SPI1); + + /* Explicitly disable I2S in favour of SPI operation */ + SPI1_I2SCFGR = 0; + + /* Set up SPI in Master mode with: + * Clock baud rate: 1/64 of peripheral clock frequency + * Clock polarity: Idle High + * Clock phase: Data valid on 2nd clock pulse + * Data frame format: 8-bit or 16-bit + * Frame format: MSB First + */ +#if USE_16BIT_TRANSFERS + spi_init_master(SPI1, SPI_CR1_BAUDRATE_FPCLK_DIV_64, SPI_CR1_CPOL_CLK_TO_1_WHEN_IDLE, + SPI_CR1_CPHA_CLK_TRANSITION_2, SPI_CR1_DFF_16BIT, SPI_CR1_MSBFIRST); +#else + spi_init_master(SPI1, SPI_CR1_BAUDRATE_FPCLK_DIV_64, SPI_CR1_CPOL_CLK_TO_1_WHEN_IDLE, + SPI_CR1_CPHA_CLK_TRANSITION_2, SPI_CR1_DFF_8BIT, SPI_CR1_MSBFIRST); +#endif + + /* + * Set NSS management to software. + * + * Note: + * Setting nss high is very important, even if we are controlling the GPIO + * ourselves this bit needs to be at least set to 1, otherwise the spi + * peripheral will not send any data out. + */ + spi_enable_software_slave_management(SPI1); + spi_set_nss_high(SPI1); + + /* Enable SPI1 periph. */ + spi_enable(SPI1); +} + +static void dma_int_enable(void) { + /* SPI1 RX on DMA1 Channel 2 */ + nvic_set_priority(NVIC_DMA1_CHANNEL2_IRQ, 0); + nvic_enable_irq(NVIC_DMA1_CHANNEL2_IRQ); + /* SPI1 TX on DMA1 Channel 3 */ + nvic_set_priority(NVIC_DMA1_CHANNEL3_IRQ, 0); + nvic_enable_irq(NVIC_DMA1_CHANNEL3_IRQ); +} + +/* Not used in this example +static void dma_int_disable(void) { + nvic_disable_irq(NVIC_DMA1_CHANNEL2_IRQ); + nvic_disable_irq(NVIC_DMA1_CHANNEL3_IRQ); +} +*/ + +static void dma_setup(void) +{ + dma_int_enable(); +} + +#if USE_16BIT_TRANSFERS +static int spi_dma_transceive(u16 *tx_buf, int tx_len, u16 *rx_buf, int rx_len) +#else +static int spi_dma_transceive(u8 *tx_buf, int tx_len, u8 *rx_buf, int rx_len) +#endif +{ + /* Check for 0 length in both tx and rx */ + if ((rx_len < 1) && (tx_len < 1)) { + /* return -1 as error */ + return -1; + } + + /* Reset DMA channels*/ + dma_channel_reset(DMA1, DMA_CHANNEL2); + dma_channel_reset(DMA1, DMA_CHANNEL3); + + /* Reset SPI data and status registers. + * Here we assume that the SPI peripheral is NOT + * busy any longer, i.e. the last activity was verified + * complete elsewhere in the program. + */ + volatile u8 temp_data __attribute__ ((unused)); + while (SPI_SR(SPI1) & (SPI_SR_RXNE | SPI_SR_OVR)) { + temp_data = SPI_DR(SPI1); + } + + /* Reset status flag appropriately (both 0 case caught above) */ + transceive_status = NONE; + if (rx_len < 1) { + transceive_status = ONE; + } + if (tx_len < 1) { + transceive_status = ONE; + } + + /* Set up rx dma, note it has higher priority to avoid overrun */ + if (rx_len > 0) { + dma_set_peripheral_address(DMA1, DMA_CHANNEL2, (u32)&SPI1_DR); + dma_set_memory_address(DMA1, DMA_CHANNEL2, (u32)rx_buf); + dma_set_number_of_data(DMA1, DMA_CHANNEL2, rx_len); + dma_set_read_from_peripheral(DMA1, DMA_CHANNEL2); + dma_enable_memory_increment_mode(DMA1, DMA_CHANNEL2); +#if USE_16BIT_TRANSFERS + dma_set_peripheral_size(DMA1, DMA_CHANNEL2, DMA_CCR_PSIZE_16BIT); + dma_set_memory_size(DMA1, DMA_CHANNEL2, DMA_CCR_MSIZE_16BIT); +#else + dma_set_peripheral_size(DMA1, DMA_CHANNEL2, DMA_CCR_PSIZE_8BIT); + dma_set_memory_size(DMA1, DMA_CHANNEL2, DMA_CCR_MSIZE_8BIT); +#endif + dma_set_priority(DMA1, DMA_CHANNEL2, DMA_CCR_PL_VERY_HIGH); + } + + /* Set up tx dma */ + if (tx_len > 0) { + dma_set_peripheral_address(DMA1, DMA_CHANNEL3, (u32)&SPI1_DR); + dma_set_memory_address(DMA1, DMA_CHANNEL3, (u32)tx_buf); + dma_set_number_of_data(DMA1, DMA_CHANNEL3, tx_len); + dma_set_read_from_memory(DMA1, DMA_CHANNEL3); + dma_enable_memory_increment_mode(DMA1, DMA_CHANNEL3); +#if USE_16BIT_TRANSFERS + dma_set_peripheral_size(DMA1, DMA_CHANNEL3, DMA_CCR_PSIZE_16BIT); + dma_set_memory_size(DMA1, DMA_CHANNEL3, DMA_CCR_MSIZE_16BIT); +#else + dma_set_peripheral_size(DMA1, DMA_CHANNEL3, DMA_CCR_PSIZE_8BIT); + dma_set_memory_size(DMA1, DMA_CHANNEL3, DMA_CCR_MSIZE_8BIT); +#endif + dma_set_priority(DMA1, DMA_CHANNEL3, DMA_CCR_PL_HIGH); + } + + /* Enable dma transfer complete interrupts */ + if (rx_len > 0) { + dma_enable_transfer_complete_interrupt(DMA1, DMA_CHANNEL2); + } + if (tx_len > 0) { + dma_enable_transfer_complete_interrupt(DMA1, DMA_CHANNEL3); + } + + /* Activate dma channels */ + if (rx_len > 0) { + dma_enable_channel(DMA1, DMA_CHANNEL2); + } + if (tx_len > 0) { + dma_enable_channel(DMA1, DMA_CHANNEL3); + } + + /* Enable the spi transfer via dma + * This will immediately start the transmission, + * after which when the receive is complete, the + * receive dma will activate + */ + if (rx_len > 0) { + spi_enable_rx_dma(SPI1); + } + if (tx_len > 0) { + spi_enable_tx_dma(SPI1); + } + + return 0; +} + +/* SPI receive completed with DMA */ +void dma1_channel2_isr(void) +{ + gpio_set(GPIOA,GPIO4); + if ((DMA1_ISR &DMA_ISR_TCIF2) != 0) { + DMA1_IFCR |= DMA_IFCR_CTCIF2; + } + + dma_disable_transfer_complete_interrupt(DMA1, DMA_CHANNEL2); + + spi_disable_rx_dma(SPI1); + + dma_disable_channel(DMA1, DMA_CHANNEL2); + + /* Increment the status to indicate one of the transfers is complete */ + transceive_status++; + gpio_clear(GPIOA,GPIO4); +} + +/* SPI transmit completed with DMA */ +void dma1_channel3_isr(void) +{ + gpio_set(GPIOB,GPIO1); + if ((DMA1_ISR &DMA_ISR_TCIF3) != 0) { + DMA1_IFCR |= DMA_IFCR_CTCIF3; + } + + dma_disable_transfer_complete_interrupt(DMA1, DMA_CHANNEL3); + + spi_disable_tx_dma(SPI1); + + dma_disable_channel(DMA1, DMA_CHANNEL3); + + /* Increment the status to indicate one of the transfers is complete */ + transceive_status++; + gpio_clear(GPIOB,GPIO1); +} + +static void usart_setup(void) +{ + /* Setup GPIO pin GPIO_USART2_TX and GPIO_USART2_RX. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX); + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, + GPIO_CNF_INPUT_FLOAT, GPIO_USART2_RX); + + /* Setup UART parameters. */ + usart_set_baudrate(USART2, 9600); + usart_set_databits(USART2, 8); + usart_set_stopbits(USART2, USART_STOPBITS_1); + usart_set_mode(USART2, USART_MODE_TX_RX); + usart_set_parity(USART2, USART_PARITY_NONE); + usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART2); +} + +int _write(int file, char *ptr, int len) +{ + int i; + + if (file == 1) { + for (i = 0; i < len; i++) + usart_send_blocking(USART2, ptr[i]); + return i; + } + + errno = EIO; + return -1; +} + +static void gpio_setup(void) +{ + /* Set GPIO8 (in GPIO port A) to 'output push-pull'. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO8); + + /* Use the extra pins to signal when the ISRs are running */ + /* First, SPI1 - SS pin on Lisa/M v2.0 */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO4); + /* Then, SPI1 - DRDY pin on Lisa/M v2.0 */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO1); + +} + +int main(void) +{ + int counter_tx = 0; + int counter_rx = 0; + + cnt_state counter_state = TX_UP_RX_HOLD; + + int i = 0; + + /* Transmit and Receive packets, set transmit to index and receive to known unused value to aid in debugging */ +#if USE_16BIT_TRANSFERS + u16 tx_packet[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; + u16 rx_packet[16] = {0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42}; +#else + u8 tx_packet[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; + u8 rx_packet[16] = {0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42}; +#endif + + transceive_status = DONE; + + clock_setup(); + gpio_setup(); + usart_setup(); + spi_setup(); + dma_setup(); + +#if USE_16BIT_TRANSFERS + printf("SPI with DMA Transfer Test using 16bit option (Use loopback)\r\n\r\n"); +#else + printf("SPI with DMA Transfer Test using 8bit option (Use loopback)\r\n\r\n"); +#endif + + /* Blink the LED (PA8) on the board with every transmitted byte. */ + while (1) { + /* LED on/off */ + gpio_toggle(GPIOA, GPIO8); + + /* Print what is going to be sent on the SPI bus */ + printf("Sending packet (tx len %02i):", counter_tx); + for (i = 0; i < counter_tx; i++) + { + printf(" 0x%02x,", tx_packet[i]); + } + printf("\r\n"); + + /* Start a transceive */ + if (spi_dma_transceive(tx_packet, counter_tx, rx_packet, counter_rx)) { + printf("Attempted 0 length tx and rx packets\r\n"); + } + + /* Wait until transceive complete. + * This checks the state flag as well as follows the + * procedure on the Reference Manual (RM0008 rev 14 + * Section 25.3.9 page 692, the note.) + */ + while (transceive_status != DONE) + ; + while (!(SPI_SR(SPI1) & SPI_SR_TXE)) + ; + while (SPI_SR(SPI1) & SPI_SR_BSY) + ; + + /* Print what was received on the SPI bus */ + printf("Received Packet (rx len %02i):", counter_rx); + for (i = 0; i < 16; i++) { + printf(" 0x%02x,", rx_packet[i]); + } + printf("\r\n\r\n"); + + /* Update counters + * If we use the loopback method, we can not + * have a rx length longer than the tx length. + * Testing rx lengths longer than tx lengths + * requires an actual slave device that will + * return data. + */ + switch (counter_state) { + case TX_UP_RX_HOLD: + counter_tx++; + if (counter_tx > 15) { + counter_state = TX_HOLD_RX_UP; + } + break; + case TX_HOLD_RX_UP: + counter_rx++; + if (counter_rx > 15) { + counter_state = TX_DOWN_RX_DOWN; + } + break; + case TX_DOWN_RX_DOWN: + counter_tx--; + counter_rx--; + if (counter_tx < 1) { + counter_state = TX_UP_RX_HOLD; + } + break; + default: + ; + } + + /* Reset receive buffer for consistency */ + for (i = 0; i < 16; i++) { + rx_packet[i] = 0x42; + } + } + + return 0; +} diff --git a/examples/stm32/f1/lisa-m-2/spi_dma_adv/Makefile b/examples/stm32/f1/lisa-m-2/spi_dma_adv/Makefile new file mode 100644 index 0000000..4e524ad --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/spi_dma_adv/Makefile @@ -0,0 +1,28 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = spi_dma_adv + +# Comment the following line if you _don't_ have luftboot flashed! +LDFLAGS += -Wl,-Ttext=0x8002000 +CFLAGS += -std=c99 +LDSCRIPT = ../lisa-m.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/lisa-m-2/spi_dma_adv/README b/examples/stm32/f1/lisa-m-2/spi_dma_adv/README new file mode 100644 index 0000000..7d279c2 --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/spi_dma_adv/README @@ -0,0 +1,17 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example program demonstrates SPI transceive with DMA on Lisa/M 2.0 board +(http://paparazzi.enac.fr/wiki/Lisa/M_v20 for details). + +The terminal settings for the receiving device/PC are 9600 8n1. + +The example expects a loopback connection between the MISO and MOSI pins on +SPI1. The DRDY and SS pins (on the Lisa/M v2.0 SPI1 connector) are used as +GPIO to time the tx and rx ISRs, respectively. Use a scope or logic analyzer. + +The tx length is incremented, followed by the rx length, after which the tx is +decremented, then the rx is decremented. This is repeated in a loop. In this +example, rx lengths longer than tx lengths are handled by using two dma transmits +one after the other, handled inside the tx dma ISR. diff --git a/examples/stm32/f1/lisa-m-2/spi_dma_adv/spi_dma_adv.c b/examples/stm32/f1/lisa-m-2/spi_dma_adv/spi_dma_adv.c new file mode 100644 index 0000000..a8054e7 --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/spi_dma_adv/spi_dma_adv.c @@ -0,0 +1,506 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * Copyright (C) 2013 Stephen Dwyer + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef USE_16BIT_TRANSFERS +#define USE_16BIT_TRANSFERS 0 +#endif + +/* This is for the counter state flag */ +typedef enum { + TX_UP_RX_HOLD = 0, + TX_HOLD_RX_UP, + TX_DOWN_RX_HOLD, + TX_HOLD_RX_DOWN +} cnt_state; + +/* This is a global spi state flag */ +typedef enum { + NONE = 0, + ONE, + DONE +} trans_status; + +volatile trans_status transceive_status; + +/* Global for dummy tx dma transfer */ +int rx_buf_remainder = 0; + +#if USE_16BIT_TRANSFERS +u16 dummy_tx_buf = 0xdd; +#else +u8 dummy_tx_buf = 0xdd; +#endif + +int _write(int file, char *ptr, int len); + +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_12mhz_out_72mhz(); + + /* Enable GPIOA, GPIOB, GPIOC clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, + RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN | + RCC_APB2ENR_IOPCEN); + + /* Enable clocks for GPIO port A (for GPIO_USART2_TX) and USART2. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN | + RCC_APB2ENR_AFIOEN); + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN); + + /* Enable SPI1 Periph and gpio clocks */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, + RCC_APB2ENR_SPI1EN); + + /* Enable DMA1 clock */ + rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_DMA1EN); +} + +static void spi_setup(void) { + + /* Configure GPIOs: SS=PA4, SCK=PA5, MISO=PA6 and MOSI=PA7 + * For now ignore the SS pin so we can use it to time the ISRs + */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, /* GPIO4 | */ + GPIO5 | + GPIO7 ); + + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, + GPIO6); + + /* Reset SPI, SPI_CR1 register cleared, SPI is disabled */ + spi_reset(SPI1); + + /* Explicitly disable I2S in favour of SPI operation */ + SPI1_I2SCFGR = 0; + + /* Set up SPI in Master mode with: + * Clock baud rate: 1/64 of peripheral clock frequency + * Clock polarity: Idle High + * Clock phase: Data valid on 2nd clock pulse + * Data frame format: 8-bit or 16-bit + * Frame format: MSB First + */ +#if USE_16BIT_TRANSFERS + spi_init_master(SPI1, SPI_CR1_BAUDRATE_FPCLK_DIV_64, SPI_CR1_CPOL_CLK_TO_1_WHEN_IDLE, + SPI_CR1_CPHA_CLK_TRANSITION_2, SPI_CR1_DFF_16BIT, SPI_CR1_MSBFIRST); +#else + spi_init_master(SPI1, SPI_CR1_BAUDRATE_FPCLK_DIV_64, SPI_CR1_CPOL_CLK_TO_1_WHEN_IDLE, + SPI_CR1_CPHA_CLK_TRANSITION_2, SPI_CR1_DFF_8BIT, SPI_CR1_MSBFIRST); +#endif + + /* + * Set NSS management to software. + * + * Note: + * Setting nss high is very important, even if we are controlling the GPIO + * ourselves this bit needs to be at least set to 1, otherwise the spi + * peripheral will not send any data out. + */ + spi_enable_software_slave_management(SPI1); + spi_set_nss_high(SPI1); + + /* Enable SPI1 periph. */ + spi_enable(SPI1); +} + +static void dma_int_enable(void) { + /* SPI1 RX on DMA1 Channel 2 */ + nvic_set_priority(NVIC_DMA1_CHANNEL2_IRQ, 0); + nvic_enable_irq(NVIC_DMA1_CHANNEL2_IRQ); + /* SPI1 TX on DMA1 Channel 3 */ + nvic_set_priority(NVIC_DMA1_CHANNEL3_IRQ, 0); + nvic_enable_irq(NVIC_DMA1_CHANNEL3_IRQ); +} + +/* Not used in this example +static void dma_int_disable(void) { + nvic_disable_irq(NVIC_DMA1_CHANNEL2_IRQ); + nvic_disable_irq(NVIC_DMA1_CHANNEL3_IRQ); +} +*/ + +static void dma_setup(void) +{ + dma_int_enable(); +} + +#if USE_16BIT_TRANSFERS +static int spi_dma_transceive(u16 *tx_buf, int tx_len, u16 *rx_buf, int rx_len) +#else +static int spi_dma_transceive(u8 *tx_buf, int tx_len, u8 *rx_buf, int rx_len) +#endif +{ + + /* Check for 0 length in both tx and rx */ + if ((rx_len < 1) && (tx_len < 1)) { + /* return -1 as error */ + return -1; + } + + /* Reset DMA channels*/ + dma_channel_reset(DMA1, DMA_CHANNEL2); + dma_channel_reset(DMA1, DMA_CHANNEL3); + + /* Reset SPI data and status registers. + * Here we assume that the SPI peripheral is NOT + * busy any longer, i.e. the last activity was verified + * complete elsewhere in the program. + */ + volatile u8 temp_data __attribute__ ((unused)); + while (SPI_SR(SPI1) & (SPI_SR_RXNE | SPI_SR_OVR)) { + temp_data = SPI_DR(SPI1); + } + + /* Reset status flag appropriately (both 0 case caught above) */ + transceive_status = NONE; + if (rx_len < 1) { + transceive_status = ONE; + } + + /* Determine tx length case to change behaviour + * If tx_len >= rx_len, then normal case, run both DMAs with normal settings + * If rx_len == 0, just don't run the rx DMA at all + * If tx_len == 0, use a dummy buf and set the tx dma to transfer the same + * amount as the rx_len, to ensure everything is clocked in + * If 0 < tx_len < rx_len, first do a normal case, then on the tx finished + * interrupt, set up a new dummyy buf tx dma transfer for the remaining + * required clock cycles (handled in tx dma complete interrupt) + */ + if ((tx_len > 0) && (tx_len < rx_len)) { + rx_buf_remainder = rx_len - tx_len; + } + + /* Set up rx dma, note it has higher priority to avoid overrun */ + if (rx_len > 0) { + dma_set_peripheral_address(DMA1, DMA_CHANNEL2, (u32)&SPI1_DR); + dma_set_memory_address(DMA1, DMA_CHANNEL2, (u32)rx_buf); + dma_set_number_of_data(DMA1, DMA_CHANNEL2, rx_len); + dma_set_read_from_peripheral(DMA1, DMA_CHANNEL2); + dma_enable_memory_increment_mode(DMA1, DMA_CHANNEL2); +#if USE_16BIT_TRANSFERS + dma_set_peripheral_size(DMA1, DMA_CHANNEL2, DMA_CCR_PSIZE_16BIT); + dma_set_memory_size(DMA1, DMA_CHANNEL2, DMA_CCR_MSIZE_16BIT); +#else + dma_set_peripheral_size(DMA1, DMA_CHANNEL2, DMA_CCR_PSIZE_8BIT); + dma_set_memory_size(DMA1, DMA_CHANNEL2, DMA_CCR_MSIZE_8BIT); +#endif + dma_set_priority(DMA1, DMA_CHANNEL2, DMA_CCR_PL_VERY_HIGH); + } + + /* Set up tx dma (must always run tx to get clock signal) */ + if (tx_len > 0) { + /* Here we have a regular tx transfer */ + dma_set_peripheral_address(DMA1, DMA_CHANNEL3, (u32)&SPI1_DR); + dma_set_memory_address(DMA1, DMA_CHANNEL3, (u32)tx_buf); + dma_set_number_of_data(DMA1, DMA_CHANNEL3, tx_len); + dma_set_read_from_memory(DMA1, DMA_CHANNEL3); + dma_enable_memory_increment_mode(DMA1, DMA_CHANNEL3); +#if USE_16BIT_TRANSFERS + dma_set_peripheral_size(DMA1, DMA_CHANNEL3, DMA_CCR_PSIZE_16BIT); + dma_set_memory_size(DMA1, DMA_CHANNEL3, DMA_CCR_MSIZE_16BIT); +#else + dma_set_peripheral_size(DMA1, DMA_CHANNEL3, DMA_CCR_PSIZE_8BIT); + dma_set_memory_size(DMA1, DMA_CHANNEL3, DMA_CCR_MSIZE_8BIT); +#endif + dma_set_priority(DMA1, DMA_CHANNEL3, DMA_CCR_PL_HIGH); + } else { + /* Here we aren't transmitting any real data, use the dummy buffer + * and set the length to the rx_len to get all rx data in, while + * not incrementing the memory pointer + */ + dma_set_peripheral_address(DMA1, DMA_CHANNEL3, (u32)&SPI1_DR); + dma_set_memory_address(DMA1, DMA_CHANNEL3, (u32)(&dummy_tx_buf)); // Change here + dma_set_number_of_data(DMA1, DMA_CHANNEL3, rx_len); // Change here + dma_set_read_from_memory(DMA1, DMA_CHANNEL3); + dma_disable_memory_increment_mode(DMA1, DMA_CHANNEL3); // Change here +#if USE_16BIT_TRANSFERS + dma_set_peripheral_size(DMA1, DMA_CHANNEL3, DMA_CCR_PSIZE_16BIT); + dma_set_memory_size(DMA1, DMA_CHANNEL3, DMA_CCR_MSIZE_16BIT); +#else + dma_set_peripheral_size(DMA1, DMA_CHANNEL3, DMA_CCR_PSIZE_8BIT); + dma_set_memory_size(DMA1, DMA_CHANNEL3, DMA_CCR_MSIZE_8BIT); +#endif + dma_set_priority(DMA1, DMA_CHANNEL3, DMA_CCR_PL_HIGH); + } + + /* Enable dma transfer complete interrupts */ + if (rx_len > 0) { + dma_enable_transfer_complete_interrupt(DMA1, DMA_CHANNEL2); + } + dma_enable_transfer_complete_interrupt(DMA1, DMA_CHANNEL3); + + /* Activate dma channels */ + if (rx_len > 0) { + dma_enable_channel(DMA1, DMA_CHANNEL2); + } + dma_enable_channel(DMA1, DMA_CHANNEL3); + + /* Enable the spi transfer via dma + * This will immediately start the transmission, + * after which when the receive is complete, the + * receive dma will activate + */ + if (rx_len > 0) { + spi_enable_rx_dma(SPI1); + } + spi_enable_tx_dma(SPI1); + + return 0; +} + +/* SPI receive completed with DMA */ +void dma1_channel2_isr(void) +{ + gpio_set(GPIOA,GPIO4); + if ((DMA1_ISR &DMA_ISR_TCIF2) != 0) { + DMA1_IFCR |= DMA_IFCR_CTCIF2; + } + + dma_disable_transfer_complete_interrupt(DMA1, DMA_CHANNEL2); + + spi_disable_rx_dma(SPI1); + + dma_disable_channel(DMA1, DMA_CHANNEL2); + + /* Increment the status to indicate one of the transfers is complete */ + transceive_status++; + gpio_clear(GPIOA,GPIO4); +} + +/* SPI transmit completed with DMA */ +void dma1_channel3_isr(void) +{ + gpio_set(GPIOB,GPIO1); + if ((DMA1_ISR &DMA_ISR_TCIF3) != 0) { + DMA1_IFCR |= DMA_IFCR_CTCIF3; + } + + dma_disable_transfer_complete_interrupt(DMA1, DMA_CHANNEL3); + + spi_disable_tx_dma(SPI1); + + dma_disable_channel(DMA1, DMA_CHANNEL3); + + /* If tx_len < rx_len, create a dummy transfer to clock in the remaining + * rx data + */ + if (rx_buf_remainder > 0) { + dma_channel_reset(DMA1, DMA_CHANNEL3); + dma_set_peripheral_address(DMA1, DMA_CHANNEL3, (u32)&SPI1_DR); + dma_set_memory_address(DMA1, DMA_CHANNEL3, (u32)(&dummy_tx_buf)); // Change here + dma_set_number_of_data(DMA1, DMA_CHANNEL3, rx_buf_remainder); // Change here + dma_set_read_from_memory(DMA1, DMA_CHANNEL3); + dma_disable_memory_increment_mode(DMA1, DMA_CHANNEL3); // Change here +#if USE_16BIT_TRANSFERS + dma_set_peripheral_size(DMA1, DMA_CHANNEL3, DMA_CCR_PSIZE_16BIT); + dma_set_memory_size(DMA1, DMA_CHANNEL3, DMA_CCR_MSIZE_16BIT); +#else + dma_set_peripheral_size(DMA1, DMA_CHANNEL3, DMA_CCR_PSIZE_8BIT); + dma_set_memory_size(DMA1, DMA_CHANNEL3, DMA_CCR_MSIZE_8BIT); +#endif + dma_set_priority(DMA1, DMA_CHANNEL3, DMA_CCR_PL_HIGH); + + rx_buf_remainder = 0; // Clear the buffer remainder to disable this section later + + dma_enable_transfer_complete_interrupt(DMA1, DMA_CHANNEL3); + dma_enable_channel(DMA1, DMA_CHANNEL3); + spi_enable_tx_dma(SPI1); + } else { + /* Increment the status to indicate one of the transfers is complete */ + transceive_status++; + } + + gpio_clear(GPIOB,GPIO1); +} + +static void usart_setup(void) +{ + /* Setup GPIO pin GPIO_USART2_TX and GPIO_USART2_RX. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX); + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, + GPIO_CNF_INPUT_FLOAT, GPIO_USART2_RX); + + /* Setup UART parameters. */ + usart_set_baudrate(USART2, 9600); + usart_set_databits(USART2, 8); + usart_set_stopbits(USART2, USART_STOPBITS_1); + usart_set_mode(USART2, USART_MODE_TX_RX); + usart_set_parity(USART2, USART_PARITY_NONE); + usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART2); +} + +int _write(int file, char *ptr, int len) +{ + int i; + + if (file == 1) { + for (i = 0; i < len; i++) + usart_send_blocking(USART2, ptr[i]); + return i; + } + + errno = EIO; + return -1; +} + +static void gpio_setup(void) +{ + /* Set GPIO8 (in GPIO port A) to 'output push-pull'. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO8); + + /* Use the extra pins to signal when the ISRs are running */ + /* First, SPI1 - SS pin on Lisa/M v2.0 */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO4); + /* Then, SPI1 - DRDY pin on Lisa/M v2.0 */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO1); + +} + +int main(void) +{ + int counter_tx = 0; + int counter_rx = 0; + + cnt_state counter_state = TX_UP_RX_HOLD; + + int i = 0; + + /* Transmit and Receive packets, set transmit to index and receive to known unused value to aid in debugging */ +#if USE_16BIT_TRANSFERS + u16 tx_packet[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; + u16 rx_packet[16] = {0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42}; +#else + u8 tx_packet[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; + u8 rx_packet[16] = {0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42}; +#endif + + transceive_status = DONE; + + clock_setup(); + gpio_setup(); + usart_setup(); + spi_setup(); + dma_setup(); + + printf("SPI with DMA Transfer Test (Use loopback)\r\n\r\n"); + + /* Blink the LED (PA8) on the board with every transmitted byte. */ + while (1) { + /* LED on/off */ + gpio_toggle(GPIOA, GPIO8); + + /* Print what is going to be sent on the SPI bus */ + printf("Sending packet (tx len %02i):", counter_tx); + for (i = 0; i < counter_tx; i++) + { + printf(" 0x%02x,", tx_packet[i]); + } + printf("\r\n"); + + /* Start a transceive */ + if (spi_dma_transceive(tx_packet, counter_tx, rx_packet, counter_rx)) { + printf("Attempted 0 length tx and rx packets\r\n"); + } + + /* Wait until transceive complete. + * This checks the state flag as well as follows the + * procedure on the Reference Manual (RM0008 rev 14 + * Section 25.3.9 page 692, the note.) + */ + while (transceive_status != DONE) + ; + while (!(SPI_SR(SPI1) & SPI_SR_TXE)) + ; + while (SPI_SR(SPI1) & SPI_SR_BSY) + ; + + /* Print what was received on the SPI bus */ + printf("Received Packet (rx len %02i):", counter_rx); + for (i = 0; i < 16; i++) { + printf(" 0x%02x,", rx_packet[i]); + } + printf("\r\n\r\n"); + + /* Update counters + * If we use the loopback method, we can not + * have a rx length longer than the tx length. + * Testing rx lengths longer than tx lengths + * requires an actual slave device that will + * return data. + */ + switch (counter_state) { + case TX_UP_RX_HOLD: + counter_tx++; + if (counter_tx > 15) { + counter_state = TX_HOLD_RX_UP; + } + break; + case TX_HOLD_RX_UP: + counter_rx++; + if (counter_rx > 15) { + counter_state = TX_DOWN_RX_HOLD; + } + break; + case TX_DOWN_RX_HOLD: + counter_tx--; + if (counter_tx < 1) { + counter_state = TX_HOLD_RX_DOWN; + } + break; + case TX_HOLD_RX_DOWN: + counter_rx--; + if (counter_rx < 1) { + counter_state = TX_UP_RX_HOLD; + } + break; + default: + ; + } + + /* Reset receive buffer for consistency */ + for (i = 0; i < 16; i++) { +#if USE_16BIT_TRANSFERS + tx_packet[i] = (u16)i; +#else + tx_packet[i] = (u8)i; +#endif + rx_packet[i] = 0x42; + } + } + + return 0; +} diff --git a/examples/stm32/f1/lisa-m-2/usart/Makefile b/examples/stm32/f1/lisa-m-2/usart/Makefile new file mode 100644 index 0000000..498cd83 --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/usart/Makefile @@ -0,0 +1,28 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = usart + +# Comment the following line if you _don't_ have luftboot flashed! +LDFLAGS += -Wl,-Ttext=0x8002000 +CFLAGS += -std=c99 +LDSCRIPT = ../lisa-m.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/lisa-m-2/usart/README b/examples/stm32/f1/lisa-m-2/usart/README new file mode 100644 index 0000000..5a3f316 --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/usart/README @@ -0,0 +1,12 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example program sends some characters on USART2 on Lisa/M 2.0 board +(see http://paparazzi.enac.fr/wiki/LisaM for details). + +The terminal settings for the receiving device/PC are 38400 8n1. + +The sending is done in a blocking way in the code, see the usart_irq example +for a more elaborate USART example. + diff --git a/examples/stm32/f1/lisa-m-2/usart/usart.c b/examples/stm32/f1/lisa-m-2/usart/usart.c new file mode 100644 index 0000000..bd3146e --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/usart/usart.c @@ -0,0 +1,86 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include + +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_12mhz_out_72mhz(); + + /* Enable GPIOA, GPIOB, GPIOC clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, + RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN | + RCC_APB2ENR_IOPCEN); + + /* Enable clocks for GPIO port B (for GPIO_USART3_TX) and USART3. */ + rcc_peripheral_enable_clock(&RCC_APB1ENR, + RCC_APB1ENR_USART2EN); +} + +static void usart_setup(void) +{ + /* Setup GPIO pin GPIO_USART2_TX. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX); + + /* Setup UART parameters. */ + usart_set_baudrate(USART2, 38400); + usart_set_databits(USART2, 8); + usart_set_stopbits(USART2, USART_STOPBITS_1); + usart_set_mode(USART2, USART_MODE_TX); + usart_set_parity(USART2, USART_PARITY_NONE); + usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART2); + +} + +static void gpio_setup(void) +{ + /* Set GPIO8 (in GPIO port A) to 'output push-pull'. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO8); +} + +int main(void) +{ + int i, j = 0, c = 0; + + clock_setup(); + gpio_setup(); + usart_setup(); + + /* Blink the LED (PA8) on the board with every transmitted byte. */ + while (1) { + gpio_toggle(GPIOA, GPIO8); /* LED on/off */ + usart_send_blocking(USART2, c + '0'); /* USART2: Send byte. */ + c = (c == 9) ? 0 : c + 1; /* Increment c. */ + if ((j++ % 80) == 0) { /* Newline after line full. */ + usart_send_blocking(USART2, '\r'); + usart_send_blocking(USART2, '\n'); + } + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/stm32/f1/lisa-m-2/usart_dma/Makefile b/examples/stm32/f1/lisa-m-2/usart_dma/Makefile new file mode 100644 index 0000000..5fa5c51 --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/usart_dma/Makefile @@ -0,0 +1,28 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = usart_dma + +# Comment the following line if you _don't_ have luftboot flashed! +LDFLAGS += -Wl,-Ttext=0x8002000 +CFLAGS += -std=c99 +LDSCRIPT = ../lisa-m.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/lisa-m-2/usart_dma/usart_dma.c b/examples/stm32/f1/lisa-m-2/usart_dma/usart_dma.c new file mode 100644 index 0000000..fa35fe6 --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/usart_dma/usart_dma.c @@ -0,0 +1,199 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include + +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_12mhz_out_72mhz(); + + /* Enable GPIOA, GPIOB, GPIOC clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, + RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN | + RCC_APB2ENR_IOPCEN); + + /* Enable clocks for GPIO port B (for GPIO_USART3_TX) and USART3. */ + rcc_peripheral_enable_clock(&RCC_APB1ENR, + RCC_APB1ENR_USART2EN); + + /* Enable DMA1 clock */ + rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_DMA1EN); +} + +static void usart_setup(void) +{ + /* Setup GPIO pin GPIO_USART2_TX and GPIO_USART2_RX. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX); + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, + GPIO_CNF_INPUT_FLOAT, GPIO_USART2_RX); + + /* Setup UART parameters. */ + usart_set_baudrate(USART2, 38400); + usart_set_databits(USART2, 8); + usart_set_stopbits(USART2, USART_STOPBITS_1); + usart_set_mode(USART2, USART_MODE_TX_RX); + usart_set_parity(USART2, USART_PARITY_NONE); + usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART2); + + nvic_set_priority(NVIC_DMA1_CHANNEL7_IRQ, 0); + nvic_enable_irq(NVIC_DMA1_CHANNEL7_IRQ); + + nvic_set_priority(NVIC_DMA1_CHANNEL6_IRQ, 0); + nvic_enable_irq(NVIC_DMA1_CHANNEL6_IRQ); + +} + +static void dma_write(char *data, int size) +{ + /* + * Using channel 7 for USART2_TX + */ + + /* Reset DMA channel*/ + dma_channel_reset(DMA1, DMA_CHANNEL7); + + dma_set_peripheral_address(DMA1, DMA_CHANNEL7, (u32)&USART2_DR); + dma_set_memory_address(DMA1, DMA_CHANNEL7, (u32)data); + dma_set_number_of_data(DMA1, DMA_CHANNEL7, size); + dma_set_read_from_memory(DMA1, DMA_CHANNEL7); + dma_enable_memory_increment_mode(DMA1, DMA_CHANNEL7); + dma_set_peripheral_size(DMA1, DMA_CHANNEL7, DMA_CCR_PSIZE_8BIT); + dma_set_memory_size(DMA1, DMA_CHANNEL7, DMA_CCR_MSIZE_8BIT); + dma_set_priority(DMA1, DMA_CHANNEL7, DMA_CCR_PL_VERY_HIGH); + + dma_enable_transfer_complete_interrupt(DMA1, DMA_CHANNEL7); + + dma_enable_channel(DMA1, DMA_CHANNEL7); + + usart_enable_tx_dma(USART2); +} + +volatile int transfered = 0; + +void dma1_channel7_isr(void) +{ + if ((DMA1_ISR &DMA_ISR_TCIF7) != 0) { + DMA1_IFCR |= DMA_IFCR_CTCIF7; + + transfered = 1; + } + + dma_disable_transfer_complete_interrupt(DMA1, DMA_CHANNEL7); + + usart_disable_tx_dma(USART2); + + dma_disable_channel(DMA1, DMA_CHANNEL7); +} + +static void dma_read(char *data, int size) +{ + /* + * Using channel 6 for USART2_RX + */ + + /* Reset DMA channel*/ + dma_channel_reset(DMA1, DMA_CHANNEL6); + + dma_set_peripheral_address(DMA1, DMA_CHANNEL6, (u32)&USART2_DR); + dma_set_memory_address(DMA1, DMA_CHANNEL6, (u32)data); + dma_set_number_of_data(DMA1, DMA_CHANNEL6, size); + dma_set_read_from_peripheral(DMA1, DMA_CHANNEL6); + dma_enable_memory_increment_mode(DMA1, DMA_CHANNEL6); + dma_set_peripheral_size(DMA1, DMA_CHANNEL6, DMA_CCR_PSIZE_8BIT); + dma_set_memory_size(DMA1, DMA_CHANNEL6, DMA_CCR_MSIZE_8BIT); + dma_set_priority(DMA1, DMA_CHANNEL6, DMA_CCR_PL_HIGH); + + dma_enable_transfer_complete_interrupt(DMA1, DMA_CHANNEL6); + + dma_enable_channel(DMA1, DMA_CHANNEL6); + + usart_enable_rx_dma(USART2); +} + +volatile int received = 0; + +void dma1_channel6_isr(void) +{ + if ((DMA1_ISR &DMA_ISR_TCIF6) != 0) { + DMA1_IFCR |= DMA_IFCR_CTCIF6; + + received = 1; + } + + dma_disable_transfer_complete_interrupt(DMA1, DMA_CHANNEL6); + + usart_disable_rx_dma(USART2); + + dma_disable_channel(DMA1, DMA_CHANNEL6); +} + +static void gpio_setup(void) +{ + /* Set GPIO8 (in GPIO port A) to 'output push-pull'. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO8); +} + +int main(void) +{ + char tx[10] = "abcdefg\r\n"; + int tx_len = 10; + char rx[7] = "bcdefg"; + int rx_len = 6; + + clock_setup(); + gpio_setup(); + usart_setup(); + + transfered = 0; + dma_write(tx, tx_len); + received = 0; + dma_read(rx, rx_len); + + /* Blink the LED (PA8) on the board with every transmitted byte. */ + while (1) { + gpio_toggle(GPIOA, GPIO8); /* LED on/off */ + while ( transfered != 1) { + if (received == 1) { + tx[1] = rx[0]; + tx[2] = rx[1]; + tx[3] = rx[2]; + tx[4] = rx[3]; + tx[5] = rx[4]; + tx[6] = rx[5]; + received = 0; + dma_read(rx, rx_len); + } + } + tx[0]++; + if (tx[0] > 'z') tx[0] = 'a'; + transfered = 0; + dma_write(tx, tx_len); + } + + return 0; +} diff --git a/examples/stm32/f1/lisa-m-2/usart_dma/usart_dma.c~ b/examples/stm32/f1/lisa-m-2/usart_dma/usart_dma.c~ new file mode 100644 index 0000000..4422b55 --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/usart_dma/usart_dma.c~ @@ -0,0 +1,86 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include + +void clock_setup(void) +{ + rcc_clock_setup_in_hse_12mhz_out_72mhz(); + + /* Enable GPIOA, GPIOB, GPIOC clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, + RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN | + RCC_APB2ENR_IOPCEN); + + /* Enable clocks for GPIO port B (for GPIO_USART3_TX) and USART3. */ + rcc_peripheral_enable_clock(&RCC_APB1ENR, + RCC_APB1ENR_USART2EN); +} + +void usart_setup(void) +{ + /* Setup GPIO pin GPIO_USART2_TX. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX); + + /* Setup UART parameters. */ + usart_set_baudrate(USART2, 38400); + usart_set_databits(USART2, 8); + usart_set_stopbits(USART2, USART_STOPBITS_1); + usart_set_mode(USART2, USART_MODE_TX); + usart_set_parity(USART2, USART_PARITY_NONE); + usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART2); + +} + +void gpio_setup(void) +{ + /* Set GPIO8 (in GPIO port A) to 'output push-pull'. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO8); +} + +int main(void) +{ + int i, j = 0, c = 0; + + clock_setup(); + gpio_setup(); + usart_setup(); + + /* Blink the LED (PA8) on the board with every transmitted byte. */ + while (1) { + gpio_toggle(GPIOA, GPIO8); /* LED on/off */ + usart_send_blocking(USART2, c + '0'); /* USART2: Send byte. */ + c = (c == 9) ? 0 : c + 1; /* Increment c. */ + if ((j++ % 80) == 0) { /* Newline after line full. */ + usart_send_blocking(USART2, '\r'); + usart_send_blocking(USART2, '\n'); + } + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/stm32/f1/lisa-m-2/usart_irq/Makefile b/examples/stm32/f1/lisa-m-2/usart_irq/Makefile new file mode 100644 index 0000000..dbab248 --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/usart_irq/Makefile @@ -0,0 +1,28 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = usart_irq + +# Comment the following line if you _don't_ have luftboot flashed! +LDFLAGS += -Wl,-Ttext=0x8002000 +CFLAGS += -std=c99 +LDSCRIPT = ../lisa-m.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/lisa-m-2/usart_irq/usart_irq.c b/examples/stm32/f1/lisa-m-2/usart_irq/usart_irq.c new file mode 100644 index 0000000..1fef959 --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/usart_irq/usart_irq.c @@ -0,0 +1,126 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include + +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_12mhz_out_72mhz(); + + /* Enable GPIOA clock (for LED GPIOs). */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPCEN); + + /* Enable clocks for GPIO port A (for GPIO_USART2_TX) and USART2. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN | + RCC_APB2ENR_AFIOEN); + + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN); +} + +static void usart_setup(void) +{ + /* Enable the USART2 interrupt. */ + nvic_enable_irq(NVIC_USART2_IRQ); + + /* Setup GPIO pin GPIO_USART2_TX on GPIO port A for transmit. */ + gpio_set_mode(GPIO_BANK_USART2_TX, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX); + + /* Setup GPIO pin GPIO_USART2_RX on GPIO port A for receive. */ + gpio_set_mode(GPIO_BANK_USART2_RX, GPIO_MODE_INPUT, + GPIO_CNF_INPUT_FLOAT, GPIO_USART2_RX); + + /* Setup UART parameters. */ + usart_set_baudrate(USART2, 230400); + usart_set_databits(USART2, 8); + usart_set_stopbits(USART2, USART_STOPBITS_1); + usart_set_parity(USART2, USART_PARITY_NONE); + usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); + usart_set_mode(USART2, USART_MODE_TX_RX); + + /* Enable USART2 Receive interrupt. */ + USART_CR1(USART2) |= USART_CR1_RXNEIE; + + /* Finally enable the USART. */ + usart_enable(USART2); +} + +static void gpio_setup(void) +{ + gpio_set(GPIOA, GPIO8); + + /* Setup GPIO8 (in GPIO port A) for LED use. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO8); + + gpio_set(GPIOC, GPIO15); + + /* Setup GPIO15 (in GPIO port C) for LED use. */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO15); +} + +void usart2_isr(void) +{ + static u8 data = 'A'; + + /* Check if we were called because of RXNE. */ + if (((USART_CR1(USART2) & USART_CR1_RXNEIE) != 0) && + ((USART_SR(USART2) & USART_SR_RXNE) != 0)) { + + /* Indicate that we got data. */ + gpio_toggle(GPIOA, GPIO8); + + /* Retrieve the data from the peripheral. */ + data = usart_recv(USART2); + + /* Enable transmit interrupt so it sends back the data. */ + USART_CR1(USART2) |= USART_CR1_TXEIE; + } + + /* Check if we were called because of TXE. */ + if (((USART_CR1(USART2) & USART_CR1_TXEIE) != 0) && + ((USART_SR(USART2) & USART_SR_TXE) != 0)) { + + /* Indicate that we are sending out data. */ + gpio_toggle(GPIOC, GPIO15); + + /* Put data into the transmit register. */ + usart_send(USART2, data); + + /* Disable the TXE interrupt as we don't need it anymore. */ + USART_CR1(USART2) &= ~USART_CR1_TXEIE; + } +} + +int main(void) +{ + clock_setup(); + gpio_setup(); + usart_setup(); + + /* Wait forever and do nothing. */ + while (1) + __asm__("nop"); + + return 0; +} diff --git a/examples/stm32/f1/lisa-m-2/usart_irq_printf/Makefile b/examples/stm32/f1/lisa-m-2/usart_irq_printf/Makefile new file mode 100644 index 0000000..1cc2ed1 --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/usart_irq_printf/Makefile @@ -0,0 +1,28 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = usart_irq_printf + +# Comment the following line if you _don't_ have luftboot flashed! +LDFLAGS += -Wl,-Ttext=0x8002000 +CFLAGS += -std=c99 +LDSCRIPT = ../lisa-m.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/lisa-m-2/usart_irq_printf/usart_irq_printf.c b/examples/stm32/f1/lisa-m-2/usart_irq_printf/usart_irq_printf.c new file mode 100644 index 0000000..d4e6805 --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/usart_irq_printf/usart_irq_printf.c @@ -0,0 +1,274 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann , + * Copyright (C) 2011 Piotr Esden-Tempski + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include + +/****************************************************************************** + * Simple ringbuffer implementation from open-bldc's libgovernor that + * you can find at: + * https://github.com/open-bldc/open-bldc/tree/master/source/libgovernor + *****************************************************************************/ + +typedef s32 ring_size_t; + +struct ring { + u8 *data; + ring_size_t size; + u32 begin; + u32 end; +}; + +#define RING_SIZE(RING) ((RING)->size - 1) +#define RING_DATA(RING) (RING)->data +#define RING_EMPTY(RING) ((RING)->begin == (RING)->end) + +int _write(int file, char *ptr, int len); + +static void ring_init(struct ring *ring, u8 *buf, ring_size_t size) +{ + ring->data = buf; + ring->size = size; + ring->begin = 0; + ring->end = 0; +} + +static s32 ring_write_ch(struct ring *ring, u8 ch) +{ + if (((ring->end + 1) % ring->size) != ring->begin) { + ring->data[ring->end++] = ch; + ring->end %= ring->size; + return (u32)ch; + } + + return -1; +} + +static s32 ring_write(struct ring *ring, u8 *data, ring_size_t size) +{ + s32 i; + + for (i = 0; i < size; i++) { + if (ring_write_ch(ring, data[i]) < 0) + return -i; + } + + return i; +} + +static s32 ring_read_ch(struct ring *ring, u8 *ch) +{ + s32 ret = -1; + + if (ring->begin != ring->end) { + ret = ring->data[ring->begin++]; + ring->begin %= ring->size; + if (ch) + *ch = ret; + } + + return ret; +} + +/* Not used! +static s32 ring_read(struct ring *ring, u8 *data, ring_size_t size) +{ + s32 i; + + for (i = 0; i < size; i++) { + if (ring_read_ch(ring, data + i) < 0) + return i; + } + + return -i; +} +*/ + +/****************************************************************************** + * The example implementation + *****************************************************************************/ + +#define BUFFER_SIZE 1024 + +struct ring output_ring; +u8 output_ring_buffer[BUFFER_SIZE]; + +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_12mhz_out_72mhz(); + + /* Enable GPIOA clock (for LED GPIOs). */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + + /* Enable clocks for GPIO port A (for GPIO_USART2_TX) and USART2. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN | + RCC_APB2ENR_AFIOEN); + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN); +} + +static void usart_setup(void) +{ + /* Initialize output ring buffer. */ + ring_init(&output_ring, output_ring_buffer, BUFFER_SIZE); + + /* Enable the USART2 interrupt. */ + nvic_enable_irq(NVIC_USART2_IRQ); + + /* Setup GPIO pin GPIO_USART2_TX on GPIO port A for transmit. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX); + + /* Setup GPIO pin GPIO_USART2_RX on GPIO port A for receive. */ + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, + GPIO_CNF_INPUT_FLOAT, GPIO_USART2_RX); + + /* Setup UART parameters. */ + usart_set_baudrate(USART2, 230400); + usart_set_databits(USART2, 8); + usart_set_stopbits(USART2, USART_STOPBITS_1); + usart_set_parity(USART2, USART_PARITY_NONE); + usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); + usart_set_mode(USART2, USART_MODE_TX_RX); + + /* Enable USART2 Receive interrupt. */ + USART_CR1(USART2) |= USART_CR1_RXNEIE; + + /* Finally enable the USART. */ + usart_enable(USART2); +} + +static void gpio_setup(void) +{ + gpio_set(GPIOA, GPIO8); + + /* Setup GPIO8 (in GPIO port A) for LED use. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO8); +} + +void usart2_isr(void) +{ + /* Check if we were called because of RXNE. */ + if (((USART_CR1(USART2) & USART_CR1_RXNEIE) != 0) && + ((USART_SR(USART2) & USART_SR_RXNE) != 0)) { + + /* Indicate that we got data. */ + gpio_toggle(GPIOA, GPIO8); + + /* Retrieve the data from the peripheral. */ + ring_write_ch(&output_ring, usart_recv(USART2)); + + /* Enable transmit interrupt so it sends back the data. */ + USART_CR1(USART2) |= USART_CR1_TXEIE; + } + + /* Check if we were called because of TXE. */ + if (((USART_CR1(USART2) & USART_CR1_TXEIE) != 0) && + ((USART_SR(USART2) & USART_SR_TXE) != 0)) { + + s32 data; + + data = ring_read_ch(&output_ring, NULL); + + if (data == -1) { + /* Disable the TXE interrupt, it's no longer needed. */ + USART_CR1(USART2) &= ~USART_CR1_TXEIE; + } else { + /* Put data into the transmit register. */ + usart_send(USART2, data); + } + } +} + +int _write(int file, char *ptr, int len) +{ + int ret; + + if (file == 1) { + ret = ring_write(&output_ring, (u8 *)ptr, len); + + if (ret < 0) + ret = -ret; + + USART_CR1(USART2) |= USART_CR1_TXEIE; + + return ret; + } + + errno = EIO; + return -1; +} + +static void systick_setup(void) +{ + /* 72MHz / 8 => 9000000 counts per second. */ + systick_set_clocksource(STK_CTRL_CLKSOURCE_AHB_DIV8); + + /* 9000000/9000 = 1000 overflows per second - every 1ms one interrupt */ + /* SysTick interrupt every N clock pulses: set reload to N-1 */ + systick_set_reload(8999); + + systick_interrupt_enable(); + + /* Start counting. */ + systick_counter_enable(); +} + +void sys_tick_handler(void) +{ + static int counter = 0; + static float fcounter = 0.0; + static double dcounter = 0.0; + static u32 temp32 = 0; + + temp32++; + + /* + * We call this handler every 1ms so we are sending hello world + * every 10ms / 100Hz. + */ + if (temp32 == 10) { + printf("Hello World! %i %f %f\r\n", counter, fcounter, + dcounter); + counter++; + fcounter += 0.01; + dcounter += 0.01; + + temp32 = 0; + } +} + +int main(void) +{ + clock_setup(); + gpio_setup(); + usart_setup(); + systick_setup(); + + while (1) + __asm__("nop"); + + return 0; +} diff --git a/examples/stm32/f1/lisa-m-2/usart_printf/Makefile b/examples/stm32/f1/lisa-m-2/usart_printf/Makefile new file mode 100644 index 0000000..6eaf005 --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/usart_printf/Makefile @@ -0,0 +1,28 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = usart_printf + +# Comment the following line if you _don't_ have luftboot flashed! +LDFLAGS += -Wl,-Ttext=0x8002000 +CFLAGS += -std=c99 +LDSCRIPT = ../lisa-m.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/lisa-m-2/usart_printf/usart_printf.c b/examples/stm32/f1/lisa-m-2/usart_printf/usart_printf.c new file mode 100644 index 0000000..eff476e --- /dev/null +++ b/examples/stm32/f1/lisa-m-2/usart_printf/usart_printf.c @@ -0,0 +1,108 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann , + * Copyright (C) 2011-2012 Piotr Esden-Tempski + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include + +int _write(int file, char *ptr, int len); + +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_12mhz_out_72mhz(); + + /* Enable GPIOA clock (for LED GPIOs). */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + /* Enable clocks for GPIO port A (for GPIO_USART2_TX) and USART2. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN | + RCC_APB2ENR_AFIOEN); + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN); +} + +static void usart_setup(void) +{ + /* Setup GPIO pin GPIO_USART2_RE_TX on GPIO port B for transmit. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX); + + /* Setup UART parameters. */ + usart_set_baudrate(USART2, 230400); + usart_set_databits(USART2, 8); + usart_set_stopbits(USART2, USART_STOPBITS_1); + usart_set_parity(USART2, USART_PARITY_NONE); + usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); + usart_set_mode(USART2, USART_MODE_TX); + + /* Finally enable the USART. */ + usart_enable(USART2); +} + +static void gpio_setup(void) +{ + gpio_set(GPIOA, GPIO8); + + /* Setup GPIO8 (in GPIO port A) for LED use. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO8); +} + +int _write(int file, char *ptr, int len) +{ + int i; + + if (file == 1) { + for (i = 0; i < len; i++) + usart_send_blocking(USART2, ptr[i]); + return i; + } + + errno = EIO; + return -1; +} + +int main(void) +{ + int counter = 0; + float fcounter = 0.0; + double dcounter = 0.0; + + clock_setup(); + gpio_setup(); + usart_setup(); + + /* + * Write Hello World, an integer, float and double all over + * again while incrementing the numbers. + */ + while (1) { + gpio_toggle(GPIOA, GPIO8); + printf("Hello World! %i %f %f\r\n", counter, fcounter, + dcounter); + counter++; + fcounter += 0.01; + dcounter += 0.01; + } + + return 0; +} diff --git a/examples/stm32/f1/mb525/fancyblink/Makefile b/examples/stm32/f1/mb525/fancyblink/Makefile new file mode 100644 index 0000000..5a79847 --- /dev/null +++ b/examples/stm32/f1/mb525/fancyblink/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = fancyblink + +LDSCRIPT = ../mb525.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/mb525/fancyblink/README b/examples/stm32/f1/mb525/fancyblink/README new file mode 100644 index 0000000..497b689 --- /dev/null +++ b/examples/stm32/f1/mb525/fancyblink/README @@ -0,0 +1,10 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This is a blink example program using libopencm3. + +It's intended for the ST STM32-based MB525 eval board (see +http://www.st.com/stonline/products/literature/um/13472.htm for details). +It should blink the LEDs on the board. + diff --git a/examples/stm32/f1/mb525/fancyblink/fancyblink.c b/examples/stm32/f1/mb525/fancyblink/fancyblink.c new file mode 100644 index 0000000..20440c0 --- /dev/null +++ b/examples/stm32/f1/mb525/fancyblink/fancyblink.c @@ -0,0 +1,76 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann , + * Copyright (C) 2010 Piotr Esden-Tempski + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include + +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + + /* Enable GPIOC clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); +} + +static void gpio_setup(void) +{ + /* Set GPIO6/7/8/9 (in GPIO port C) to 'output push-pull'. */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO6 | GPIO7 | GPIO8 | GPIO9); +} + +int main(void) +{ + int i; + + clock_setup(); + gpio_setup(); + + /* Blink the LEDs on the board. */ + gpio_set(GPIOC, GPIO6); + while (1) { + gpio_toggle(GPIOC, GPIO6); /* LED on/off */ + gpio_toggle(GPIOC, GPIO7); /* LED on/off */ + for (i = 0; i < 4000000; i++) /* Wait a bit. */ + __asm__("nop"); + gpio_toggle(GPIOC, GPIO7); /* LED on/off */ + gpio_toggle(GPIOC, GPIO8); /* LED on/off */ + for (i = 0; i < 4000000; i++) /* Wait a bit. */ + __asm__("nop"); + gpio_toggle(GPIOC, GPIO8); /* LED on/off */ + gpio_toggle(GPIOC, GPIO9); /* LED on/off */ + for (i = 0; i < 4000000; i++) /* Wait a bit. */ + __asm__("nop"); + gpio_toggle(GPIOC, GPIO8); /* LED on/off */ + gpio_toggle(GPIOC, GPIO9); /* LED on/off */ + for (i = 0; i < 4000000; i++) /* Wait a bit. */ + __asm__("nop"); + gpio_toggle(GPIOC, GPIO7); /* LED on/off */ + gpio_toggle(GPIOC, GPIO8); /* LED on/off */ + for (i = 0; i < 4000000; i++) /* Wait a bit. */ + __asm__("nop"); + gpio_toggle(GPIOC, GPIO6); /* LED on/off */ + gpio_toggle(GPIOC, GPIO7); /* LED on/off */ + for (i = 0; i < 4000000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/stm32/f1/mb525/mb525.ld b/examples/stm32/f1/mb525/mb525.ld new file mode 100644 index 0000000..c678ffb --- /dev/null +++ b/examples/stm32/f1/mb525/mb525.ld @@ -0,0 +1,32 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* TODO: Fix description and sizes. */ +/* Linker script for Olimex STM32-H103 (STM32F103RBT6, 128K flash, 20K RAM). */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_stm32f1.ld + diff --git a/examples/stm32/f1/mb525/pwmleds/Makefile b/examples/stm32/f1/mb525/pwmleds/Makefile new file mode 100644 index 0000000..498a260 --- /dev/null +++ b/examples/stm32/f1/mb525/pwmleds/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = pwmleds + +LDSCRIPT = ../mb525.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/mb525/pwmleds/README b/examples/stm32/f1/mb525/pwmleds/README new file mode 100644 index 0000000..e9c37e0 --- /dev/null +++ b/examples/stm32/f1/mb525/pwmleds/README @@ -0,0 +1,9 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This is a PWM based LED fading example using libopencm3. + +It's intended for the ST STM32-based MB525 eval board (see +http://www.st.com/stonline/products/literature/um/13472.htm for details). + diff --git a/examples/stm32/f1/mb525/pwmleds/pwmleds.c b/examples/stm32/f1/mb525/pwmleds/pwmleds.c new file mode 100644 index 0000000..e707e6a --- /dev/null +++ b/examples/stm32/f1/mb525/pwmleds/pwmleds.c @@ -0,0 +1,497 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Piotr Esden-Tempski + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include + +// #define COMPARE +// #define MOVING_FADE +#define KITT + +#ifdef COMPARE +#define GAMMA_LINEAR +#define GAMMA_1_3 +#define GAMMA_2_5 +#define GAMMA_3_0 +#endif + +#ifdef MOVING_FADE +#define GAMMA_2_2 +#define GAMMA_TABLE gamma_table_2_2 +#endif + +#ifdef KITT +#define GAMMA_2_5 +#define GAMMA_TABLE gamma_table_2_5 +#endif + +/* + * Gamma correction table + * + * The nonlinear tables are calculating with the function: + * Iout = Iin ** gamma + */ +#ifdef GAMMA_LINEAR +static const u16 gamma_table_linear[] = { + 1, 4, 9, 17, 26, 37, 51, 67, + 84, 104, 126, 149, 175, 203, 233, 265, + 299, 334, 372, 412, 454, 499, 545, 593, + 643, 695, 749, 805, 864, 924, 986, 1050, + 1116, 1185, 1255, 1327, 1402, 1478, 1556, 1637, + 1719, 1803, 1890, 1978, 2068, 2161, 2255, 2352, + 2450, 2550, 2653, 2757, 2864, 2972, 3082, 3195, + 3309, 3426, 3544, 3664, 3787, 3911, 4038, 4166, + 4296, 4429, 4563, 4700, 4838, 4978, 5121, 5265, + 5411, 5560, 5710, 5862, 6017, 6173, 6331, 6492, + 6654, 6818, 6985, 7153, 7323, 7495, 7670, 7846, + 8024, 8204, 8387, 8571, 8757, 8945, 9135, 9327, + 9521, 9718, 9916, 10116, 10318, 10522, 10728, 10936, + 11146, 11358, 11572, 11788, 12006, 12226, 12448, 12672, + 12897, 13125, 13355, 13587, 13821, 14057, 14294, 14534, + 14776, 15020, 15265, 15513, 15763, 16014, 16268, 16524, + 16781, 17041, 17302, 17566, 17831, 18099, 18368, 18640, + 18913, 19189, 19466, 19745, 20027, 20310, 20595, 20883, + 21172, 21463, 21756, 22051, 22349, 22648, 22949, 23252, + 23557, 23864, 24173, 24484, 24797, 25112, 25429, 25748, + 26069, 26391, 26716, 27043, 27372, 27702, 28035, 28370, + 28707, 29045, 29386, 29728, 30073, 30419, 30768, 31118, + 31471, 31825, 32182, 32540, 32900, 33263, 33627, 33993, + 34361, 34731, 35104, 35478, 35854, 36232, 36612, 36994, + 37378, 37764, 38152, 38542, 38933, 39327, 39723, 40121, + 40521, 40922, 41326, 41732, 42139, 42549, 42960, 43374, + 43789, 44207, 44626, 45048, 45471, 45896, 46324, 46753, + 47184, 47617, 48052, 48490, 48929, 49370, 49813, 50258, + 50705, 51154, 51604, 52057, 52512, 52969, 53428, 53888, + 54351, 54816, 55282, 55751, 56222, 56694, 57169, 57645, + 58123, 58604, 59086, 59570, 60057, 60545, 61035, 61527, + 62021, 62517, 63016, 63516, 64018, 64521, 65027, 65535, +}; +#endif + +#ifdef GAMMA_1_3 +static const u16 gamma_table_1_3[] = { + /* Gamma 1.3 */ + 0, 49, 120, 203, 296, 395, 501, 612, + 728, 848, 973, 1101, 1233, 1368, 1506, 1648, + 1792, 1939, 2088, 2241, 2395, 2552, 2711, 2872, + 3036, 3201, 3369, 3538, 3709, 3882, 4057, 4234, + 4412, 4592, 4774, 4957, 5142, 5329, 5517, 5706, + 5897, 6090, 6283, 6479, 6675, 6873, 7072, 7273, + 7475, 7678, 7882, 8088, 8294, 8502, 8711, 8922, + 9133, 9346, 9559, 9774, 9990, 10207, 10425, 10644, + 10864, 11086, 11308, 11531, 11755, 11981, 12207, 12434, + 12662, 12891, 13121, 13352, 13584, 13817, 14051, 14285, + 14521, 14757, 14994, 15233, 15472, 15712, 15952, 16194, + 16436, 16679, 16923, 17168, 17414, 17660, 17908, 18156, + 18405, 18654, 18905, 19156, 19408, 19660, 19914, 20168, + 20423, 20679, 20935, 21192, 21450, 21708, 21968, 22228, + 22488, 22750, 23012, 23275, 23538, 23802, 24067, 24332, + 24599, 24865, 25133, 25401, 25670, 25939, 26209, 26480, + 26751, 27023, 27296, 27569, 27843, 28118, 28393, 28669, + 28945, 29222, 29500, 29778, 30057, 30336, 30616, 30897, + 31178, 31460, 31742, 32025, 32308, 32592, 32877, 33162, + 33448, 33734, 34021, 34309, 34597, 34885, 35175, 35464, + 35754, 36045, 36337, 36628, 36921, 37214, 37507, 37801, + 38096, 38391, 38686, 38982, 39279, 39576, 39874, 40172, + 40471, 40770, 41070, 41370, 41670, 41972, 42273, 42576, + 42878, 43181, 43485, 43789, 44094, 44399, 44705, 45011, + 45317, 45625, 45932, 46240, 46549, 46858, 47167, 47477, + 47787, 48098, 48409, 48721, 49034, 49346, 49659, 49973, + 50287, 50602, 50917, 51232, 51548, 51864, 52181, 52498, + 52816, 53134, 53452, 53771, 54091, 54411, 54731, 55052, + 55373, 55694, 56016, 56339, 56662, 56985, 57309, 57633, + 57957, 58282, 58608, 58933, 59260, 59586, 59913, 60241, + 60569, 60897, 61226, 61555, 61884, 62214, 62545, 62875, + 63206, 63538, 63870, 64202, 64535, 64868, 65201, 65535, +}; +#endif + +#ifdef GAMMA_2_2 +static const u16 gamma_table_2_2[] = { + 0, 0, 2, 4, 7, 11, 17, 24, + 32, 42, 53, 65, 79, 94, 111, 129, + 148, 169, 192, 216, 242, 270, 299, 330, + 362, 396, 432, 469, 508, 549, 591, 635, + 681, 729, 779, 830, 883, 938, 995, 1053, + 1113, 1175, 1239, 1305, 1373, 1443, 1514, 1587, + 1663, 1740, 1819, 1900, 1983, 2068, 2155, 2243, + 2334, 2427, 2521, 2618, 2717, 2817, 2920, 3024, + 3131, 3240, 3350, 3463, 3578, 3694, 3813, 3934, + 4057, 4182, 4309, 4438, 4570, 4703, 4838, 4976, + 5115, 5257, 5401, 5547, 5695, 5845, 5998, 6152, + 6309, 6468, 6629, 6792, 6957, 7124, 7294, 7466, + 7640, 7816, 7994, 8175, 8358, 8543, 8730, 8919, + 9111, 9305, 9501, 9699, 9900, 10102, 10307, 10515, + 10724, 10936, 11150, 11366, 11585, 11806, 12029, 12254, + 12482, 12712, 12944, 13179, 13416, 13655, 13896, 14140, + 14386, 14635, 14885, 15138, 15394, 15652, 15912, 16174, + 16439, 16706, 16975, 17247, 17521, 17798, 18077, 18358, + 18642, 18928, 19216, 19507, 19800, 20095, 20393, 20694, + 20996, 21301, 21609, 21919, 22231, 22546, 22863, 23182, + 23504, 23829, 24156, 24485, 24817, 25151, 25487, 25826, + 26168, 26512, 26858, 27207, 27558, 27912, 28268, 28627, + 28988, 29351, 29717, 30086, 30457, 30830, 31206, 31585, + 31966, 32349, 32735, 33124, 33514, 33908, 34304, 34702, + 35103, 35507, 35913, 36321, 36732, 37146, 37562, 37981, + 38402, 38825, 39252, 39680, 40112, 40546, 40982, 41421, + 41862, 42306, 42753, 43202, 43654, 44108, 44565, 45025, + 45487, 45951, 46418, 46888, 47360, 47835, 48313, 48793, + 49275, 49761, 50249, 50739, 51232, 51728, 52226, 52727, + 53230, 53736, 54245, 54756, 55270, 55787, 56306, 56828, + 57352, 57879, 58409, 58941, 59476, 60014, 60554, 61097, + 61642, 62190, 62741, 63295, 63851, 64410, 64971, 65535, +}; +#endif + +#ifdef GAMMA_2_5 +static const u16 gamma_table_2_5[] = { + /* gamma = 2.5 */ + 0, 0, 0, 1, 2, 4, 6, 8, + 11, 15, 20, 25, 31, 38, 46, 55, + 65, 75, 87, 99, 113, 128, 143, 160, + 178, 197, 218, 239, 262, 286, 311, 338, + 366, 395, 425, 457, 491, 526, 562, 599, + 639, 679, 722, 765, 811, 857, 906, 956, + 1007, 1061, 1116, 1172, 1231, 1291, 1352, 1416, + 1481, 1548, 1617, 1688, 1760, 1834, 1910, 1988, + 2068, 2150, 2233, 2319, 2407, 2496, 2587, 2681, + 2776, 2874, 2973, 3075, 3178, 3284, 3391, 3501, + 3613, 3727, 3843, 3961, 4082, 4204, 4329, 4456, + 4585, 4716, 4850, 4986, 5124, 5264, 5407, 5552, + 5699, 5849, 6001, 6155, 6311, 6470, 6632, 6795, + 6962, 7130, 7301, 7475, 7650, 7829, 8009, 8193, + 8379, 8567, 8758, 8951, 9147, 9345, 9546, 9750, + 9956, 10165, 10376, 10590, 10806, 11025, 11247, 11472, + 11699, 11929, 12161, 12397, 12634, 12875, 13119, 13365, + 13614, 13865, 14120, 14377, 14637, 14899, 15165, 15433, + 15705, 15979, 16256, 16535, 16818, 17104, 17392, 17683, + 17978, 18275, 18575, 18878, 19184, 19493, 19805, 20119, + 20437, 20758, 21082, 21409, 21739, 22072, 22407, 22746, + 23089, 23434, 23782, 24133, 24487, 24845, 25206, 25569, + 25936, 26306, 26679, 27055, 27435, 27818, 28203, 28592, + 28985, 29380, 29779, 30181, 30586, 30994, 31406, 31820, + 32239, 32660, 33085, 33513, 33944, 34379, 34817, 35258, + 35702, 36150, 36602, 37056, 37514, 37976, 38441, 38909, + 39380, 39856, 40334, 40816, 41301, 41790, 42282, 42778, + 43277, 43780, 44286, 44795, 45308, 45825, 46345, 46869, + 47396, 47927, 48461, 48999, 49540, 50085, 50634, 51186, + 51742, 52301, 52864, 53431, 54001, 54575, 55153, 55734, + 56318, 56907, 57499, 58095, 58695, 59298, 59905, 60515, + 61130, 61748, 62370, 62995, 63624, 64258, 64894, 65535, +}; +#endif + +#ifdef GAMMA_3_0 +static const u16 gamma_table_3_0[] = { + /* gamma = 3.0 */ + 0, 0, 0, 0, 0, 0, 1, 1, + 2, 3, 4, 5, 7, 9, 11, 13, + 16, 19, 23, 27, 32, 37, 42, 48, + 55, 62, 69, 78, 87, 96, 107, 118, + 130, 142, 155, 169, 184, 200, 217, 234, + 253, 272, 293, 314, 337, 360, 385, 410, + 437, 465, 494, 524, 556, 588, 622, 658, + 694, 732, 771, 812, 854, 897, 942, 988, + 1036, 1085, 1136, 1189, 1243, 1298, 1356, 1415, + 1475, 1538, 1602, 1667, 1735, 1804, 1876, 1949, + 2024, 2100, 2179, 2260, 2343, 2427, 2514, 2603, + 2693, 2786, 2881, 2978, 3078, 3179, 3283, 3389, + 3497, 3607, 3720, 3835, 3952, 4072, 4194, 4319, + 4446, 4575, 4707, 4842, 4979, 5118, 5261, 5405, + 5553, 5703, 5856, 6011, 6169, 6330, 6494, 6660, + 6830, 7002, 7177, 7355, 7536, 7719, 7906, 8096, + 8289, 8484, 8683, 8885, 9090, 9298, 9510, 9724, + 9942, 10163, 10387, 10614, 10845, 11079, 11317, 11557, + 11802, 12049, 12300, 12555, 12813, 13074, 13339, 13608, + 13880, 14156, 14435, 14718, 15005, 15295, 15589, 15887, + 16189, 16494, 16803, 17117, 17433, 17754, 18079, 18408, + 18740, 19077, 19418, 19762, 20111, 20464, 20821, 21182, + 21547, 21917, 22290, 22668, 23050, 23436, 23827, 24222, + 24621, 25025, 25433, 25845, 26262, 26683, 27109, 27539, + 27974, 28413, 28857, 29306, 29759, 30217, 30680, 31147, + 31619, 32095, 32577, 33063, 33554, 34050, 34551, 35056, + 35567, 36082, 36602, 37128, 37658, 38194, 38734, 39280, + 39830, 40386, 40947, 41513, 42084, 42661, 43243, 43830, + 44422, 45019, 45622, 46231, 46844, 47463, 48088, 48718, + 49353, 49994, 50641, 51293, 51950, 52614, 53282, 53957, + 54637, 55323, 56014, 56712, 57415, 58123, 58838, 59558, + 60285, 61017, 61755, 62499, 63249, 64005, 64767, 65535, +}; +#endif + +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + + /* Enable TIM1 clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_TIM1EN); + + /* Enable GPIOC, Alternate Function clocks. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, + RCC_APB2ENR_IOPAEN | RCC_APB2ENR_AFIOEN); +} + +static void gpio_setup(void) +{ + /* + * Set GPIO_TIM1_CH1/2/3/4 (in GPIO port A) to + * 'output alternate function push-pull'. + */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, + GPIO_TIM1_CH1 | GPIO_TIM1_CH2 | + GPIO_TIM1_CH3 | GPIO_TIM1_CH4); + + /* + * Remap TIM1: + * CH1 -> PC6 + * CH2 -> PC7 + * CH3 -> PC8 + * CH4 -> PC9 + */ + // AFIO_MAPR |= AFIO_MAPR_TIM3_REMAP_FULL_REMAP; +} + +static void tim_setup(void) +{ +#if 0 + TIM1_CR1 = TIM_CR1_CMS_CENTER_1 | TIM_CR1_ARPE; + TIM1_CCMR1 = TIM_CCMR1_OC1M_PWM1 | TIM_CCMR1_OC1PE | + TIM_CCMR1_OC2M_PWM1 | TIM_CCMR1_OC2PE; + TIM1_CCMR2 = TIM_CCMR2_OC3M_PWM1 | TIM_CCMR2_OC3PE; + + TIM1_CCER &= ~TIM_CCER_CC1P; +#endif +#if 1 + TIM1_SMCR &= ~TIM_SMCR_SMS_MASK; + TIM1_CR1 &= ~TIM_CR1_CEN; + + /* Clock division and mode */ + TIM1_CR1 = TIM_CR1_CKD_CK_INT | TIM_CR1_CMS_EDGE; + /* Period */ + TIM1_ARR = 65535; + /* Prescaler */ + TIM1_PSC = 2; + TIM1_EGR = TIM_EGR_UG; + + /* ---- */ + /* Output compare 1 mode and preload */ + TIM1_CCMR1 |= TIM_CCMR1_OC1M_PWM1 | TIM_CCMR1_OC1PE; + + /* Polarity and state */ + // TIM1_CCER = TIM_CCER_CC1P | TIM_CCER_CC1E; + TIM1_CCER |= TIM_CCER_CC1E; + + /* Capture compare value */ + TIM1_CCR1 = 1000; + + /* ---- */ + /* Output compare 2 mode and preload */ + TIM1_CCMR1 |= TIM_CCMR1_OC2M_PWM1 | TIM_CCMR1_OC2PE; + + /* Polarity and state */ + // TIM1_CCER = TIM_CCER_CC1P | TIM_CCER_CC1E; + TIM1_CCER |= TIM_CCER_CC2E; + + /* Capture compare value */ + TIM1_CCR2 = 1000; + + /* ---- */ + /* Output compare 3 mode and preload */ + TIM1_CCMR2 |= TIM_CCMR2_OC3M_PWM1 | TIM_CCMR2_OC3PE; + + /* Polarity and state */ + // TIM1_CCER = TIM_CCER_CC1P | TIM_CCER_CC1E; + TIM1_CCER |= TIM_CCER_CC3E; + + /* Capture compare value */ + TIM1_CCR3 = 1000; + + /* ---- */ + /* Output compare 4 mode and preload */ + TIM1_CCMR2 |= TIM_CCMR2_OC4M_PWM1 | TIM_CCMR2_OC4PE; + + /* Polarity and state */ + // TIM1_CCER = TIM_CCER_CC1P | TIM_CCER_CC1E; + TIM1_CCER |= TIM_CCER_CC4E; + + /* Capture compare value */ + TIM1_CCR4 = 1000; + + /* ---- */ + /* ARR reload enable */ + TIM1_CR1 |= TIM_CR1_ARPE; + + TIM1_BDTR |= TIM_BDTR_MOE; + + /* Counter enable */ + TIM1_CR1 |= TIM_CR1_CEN; +#endif +} + +int main(void) +{ + int i, j0, j1, j2, j3, d0, d1, d2, d3, j, k, kd; + + clock_setup(); + gpio_setup(); + tim_setup(); + +#ifdef COMPARE + j0 = 0; + d0 = 1; + j1 = 0; + d1 = 1; + j2 = 0; + d2 = 1; + j3 = 0; + d3 = 1; + while (1) { + TIM1_CCR1 = gamma_table_linear[j0]; + j0 += d0; + if (j0 == 255) + d0 = -1; + if (j0 == 0) + d0 = 1; + TIM1_CCR2 = gamma_table_1_3[j1]; + j1 += d1; + if (j1 == 255) + d1 = -1; + if (j1 == 0) + d1 = 1; + TIM1_CCR3 = gamma_table_2_5[j2]; + j2 += d2; + if (j2 == 255) + d2 = -1; + if (j2 == 0) + d2 = 1; + TIM1_CCR4 = gamma_table_3_0[j3]; + j3 += d3; + if (j3 == 255) + d3 = -1; + if (j3 == 0) + d3 = 1; + for (i = 0; i < 50000; i++); + } +#endif + +#ifdef MOVING_FADE + j0 = 0; + d0 = 1; + j1 = 128; + d1 = 1; + j2 = 255; + d2 = -1; + j3 = 128; + d3 = -1; + while (1) { + TIM1_CCR1 = GAMMA_TABLE[j0]; + j0 += d0; + if (j0 == 255) + d0 = -1; + if (j0 == 0) + d0 = 1; + TIM1_CCR2 = GAMMA_TABLE[j1]; + j1 += d1; + if (j1 == 255) + d1 = - 1; + if (j1 == 0) + d1 = 1; + TIM1_CCR3 = GAMMA_TABLE[j2]; + j2 += d2; + if (j2 == 255) + d2 = -1; + if (j2 == 0) + d2 = 1; + TIM1_CCR4 = GAMMA_TABLE[j3]; + j3 += d3; + if (j3 == 255) + d3 = -1; + if (j3 == 0) + d3 = 1; + for (i = 0; i < 10000; i++); + } +#endif + +#ifdef KITT + j0 = 255; + d0 = -1; + j1 = 20; + d1 = -1; + j2 = 20; + d2 = -1; + j3 = 20; + d3 = -1; + j = 0; + k = 0; + kd = 1; + while (1) { + TIM1_CCR1 = GAMMA_TABLE[j0]; + j0 += d0; + if (j0 == 255) + d0 = -1; + if (j0 == 19) + j0 = 20; + TIM1_CCR2 = GAMMA_TABLE[j1]; + j1 += d1; + if (j1 == 255) + d1 = -1; + if (j1 == 19) + j1 = 20; + TIM1_CCR3 = GAMMA_TABLE[j2]; + j2 += d2; + if (j2 == 255) + d2 = -1; + if (j2 == 19) + j2 = 20; + TIM1_CCR4 = GAMMA_TABLE[j3]; + j3 += d3; + if (j3 == 255) + d3 = -1; + if (j3 == 19) + j3 = 20; + for (i = 0; i < 5000; i++); + j++; + if (j == 100) { + j = 0; + switch (k += kd) { + case 0: + j0 = 255; + break; + case 1: + j1 = 255; + break; + case 2: + j2 = 255; + break; + case 3: + j3 = 255; + break; + } + if (k == 3) + kd = -1; + if (k == 0) + kd = 1; + } + } +#endif + + return 0; +} diff --git a/examples/stm32/f1/obldc-strip/can/Makefile b/examples/stm32/f1/obldc-strip/can/Makefile new file mode 100644 index 0000000..ae14841 --- /dev/null +++ b/examples/stm32/f1/obldc-strip/can/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = can + +LDSCRIPT = ../obldc-strip.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/obldc-strip/can/README b/examples/stm32/f1/obldc-strip/can/README new file mode 100644 index 0000000..3a14e3b --- /dev/null +++ b/examples/stm32/f1/obldc-strip/can/README @@ -0,0 +1,4 @@ +This test sets up the CAN interface on Lisa/M and transmits 8 bites every +100ms. The first byte is being incremented in each cycle. The demo also +receives messages and is displaing the first 4 bits of the first byte on the +board LEDs. diff --git a/examples/stm32/f1/obldc-strip/can/can.c b/examples/stm32/f1/obldc-strip/can/can.c new file mode 100644 index 0000000..0f20360 --- /dev/null +++ b/examples/stm32/f1/obldc-strip/can/can.c @@ -0,0 +1,205 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2012 Piotr Esden-Tempski + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include + +struct can_tx_msg { + u32 std_id; + u32 ext_id; + u8 ide; + u8 rtr; + u8 dlc; + u8 data[8]; +}; + +struct can_rx_msg { + u32 std_id; + u32 ext_id; + u8 ide; + u8 rtr; + u8 dlc; + u8 data[8]; + u8 fmi; +}; + +struct can_tx_msg can_tx_msg; +struct can_rx_msg can_rx_msg; + +static void gpio_setup(void) +{ + /* Enable Alternate Function clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); + + /* Enable GPIOB clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); + + /* Preconfigure LEDs. */ + gpio_set(GPIOB, GPIO4); /* LED green off */ + gpio_set(GPIOB, GPIO5); /* LED red off */ + + /* Configure LED GPIOs. */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO4); + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO5); + + /* Configure PB4 as GPIO. */ + AFIO_MAPR |= AFIO_MAPR_SWJ_CFG_FULL_SWJ_NO_JNTRST; + +} + +static void systick_setup(void) +{ + /* 64MHz / 8 => 8000000 counts per second */ + systick_set_clocksource(STK_CTRL_CLKSOURCE_AHB_DIV8); + + /* 8000000/8000 = 1000 overflows per second - every 1ms one interrupt */ + /* SysTick interrupt every N clock pulses: set reload to N-1 */ + systick_set_reload(7999); + + systick_interrupt_enable(); + + /* Start counting. */ + systick_counter_enable(); +} + +static void can_setup(void) +{ + /* Enable peripheral clocks. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_CAN1EN); + + AFIO_MAPR |= AFIO_MAPR_CAN1_REMAP_PORTB; + + /* Configure CAN pin: RX (input pull-up). */ + gpio_set_mode(GPIO_BANK_CAN1_PB_RX, GPIO_MODE_INPUT, + GPIO_CNF_INPUT_PULL_UPDOWN, GPIO_CAN1_PB_RX); + gpio_set(GPIO_BANK_CAN1_PB_RX, GPIO_CAN1_PB_RX); + + /* Configure CAN pin: TX. */ + gpio_set_mode(GPIO_BANK_CAN1_PB_TX, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_CAN1_PB_TX); + + /* NVIC setup. */ + nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ); + nvic_set_priority(NVIC_USB_LP_CAN_RX0_IRQ, 1); + + /* Reset CAN. */ + can_reset(CAN1); + + /* CAN cell init. + * Setting the bitrate to 1MBit. APB1 = 32MHz, + * prescaler = 2 -> 16MHz time quanta frequency. + * 1tq sync + 9tq bit segment1 (TS1) + 6tq bit segment2 (TS2) = + * 16time quanto per bit period, therefor 16MHz/16 = 1MHz + */ + if (can_init(CAN1, + false, /* TTCM: Time triggered comm mode? */ + true, /* ABOM: Automatic bus-off management? */ + false, /* AWUM: Automatic wakeup mode? */ + false, /* NART: No automatic retransmission? */ + false, /* RFLM: Receive FIFO locked mode? */ + false, /* TXFP: Transmit FIFO priority? */ + CAN_BTR_SJW_1TQ, + CAN_BTR_TS1_9TQ, + CAN_BTR_TS2_6TQ, + 2, + false, + false)) /* BRP+1: Baud rate prescaler */ + { + gpio_clear(GPIOB, GPIO4); /* LED green on */ + gpio_set(GPIOB, GPIO5); /* LED red off */ + + /* Die because we failed to initialize. */ + while (1) + __asm__("nop"); + } + + /* CAN filter 0 init. */ + can_filter_id_mask_32bit_init(CAN1, + 0, /* Filter ID */ + 0, /* CAN ID */ + 0, /* CAN ID mask */ + 0, /* FIFO assignment (here: FIFO0) */ + true); /* Enable the filter. */ + + /* Enable CAN RX interrupt. */ + can_enable_irq(CAN1, CAN_IER_FMPIE0); +} + +void sys_tick_handler(void) +{ + static u8 data[8] = {0, 1, 2, 0, 0, 0, 0, 0}; + + /* We call this handler every 1ms so every 1ms = 0.001s + * resulting in 1000Hz message rate. + */ + + /* Transmit CAN frame. */ + data[0]++; + if (can_transmit(CAN1, + 0, /* (EX/ST)ID: CAN ID */ + false, /* IDE: CAN ID extended? */ + false, /* RTR: Request transmit? */ + 8, /* DLC: Data length */ + data) == -1) + { + gpio_set(GPIOB, GPIO4); /* LED green off */ + gpio_clear(GPIOB, GPIO5); /* LED red on */ + } +} + +void usb_lp_can_rx0_isr(void) +{ + u32 id, fmi; + bool ext, rtr; + u8 length, data[8]; + + can_receive(CAN1, 0, false, &id, &ext, &rtr, &fmi, &length, data); + + if (data[0] & 0x40) + gpio_clear(GPIOB, GPIO4); + else + gpio_set(GPIOB, GPIO4); + + if (data[0] & 0x80) + gpio_clear(GPIOB, GPIO5); + else + gpio_set(GPIOB, GPIO5); + + can_fifo_release(CAN1, 0); +} + +int main(void) +{ + rcc_clock_setup_in_hsi_out_64mhz(); + gpio_setup(); + can_setup(); + systick_setup(); + + while (1); /* Halt. */ + + return 0; +} diff --git a/examples/stm32/f1/obldc-strip/can/can.c~ b/examples/stm32/f1/obldc-strip/can/can.c~ new file mode 100644 index 0000000..41432b6 --- /dev/null +++ b/examples/stm32/f1/obldc-strip/can/can.c~ @@ -0,0 +1,232 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * Copyright (C) 2010 Piotr Esden-Tempski + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include + +struct can_tx_msg { + u32 std_id; + u32 ext_id; + u8 ide; + u8 rtr; + u8 dlc; + u8 data[8]; +}; + +struct can_rx_msg { + u32 std_id; + u32 ext_id; + u8 ide; + u8 rtr; + u8 dlc; + u8 data[8]; + u8 fmi; +}; + +struct can_tx_msg can_tx_msg; +struct can_rx_msg can_rx_msg; + +void gpio_setup(void) +{ + /* Enable Alternate Function clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); + + /* Enable GPIOA clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + + /* Enable GPIOB clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); + + /* Enable GPIOC clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + /* Preconfigure LEDs. */ + gpio_set(GPIOA, GPIO8); /* LED0 off */ + gpio_set(GPIOB, GPIO4); /* LED1 off */ + gpio_set(GPIOC, GPIO15); /* LED2 off */ + gpio_set(GPIOC, GPIO2); /* LED3 off */ + gpio_set(GPIOC, GPIO5); /* LED4 off */ + + /* Configure LED GPIOOs. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO8); + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO4); + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO15); + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO2); + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO5); + + /* Configure PB4 as GPIO. */ + AFIO_MAPR |= AFIO_MAPR_SWJ_CFG_FULL_SWJ_NO_JNTRST; + +} + +void systick_setup(void) +{ + /* 72MHz / 8 => 9000000 counts per second */ + systick_set_clocksource(STK_CTRL_CLKSOURCE_AHB_DIV8); + + /* 9000000/9000 = 1000 overflows per second - every 1ms one interrupt */ + systick_set_reload(9000); + + systick_interrupt_enable(); + + /* Start counting. */ + systick_counter_enable(); +} + +void can_setup(void) +{ + /* Enable peripheral clocks. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_CANEN); + + AFIO_MAPR = AFIO_MAPR_CAN1_REMAP_PORTB; + + /* Configure CAN pin: RX (input pull-up). */ + gpio_set_mode(GPIOB, GPIO_MODE_INPUT, + GPIO_CNF_INPUT_PULL_UPDOWN, GPIO_CAN1_PB_RX); + gpio_set(GPIOB, GPIO_CAN1_PB_RX); + + /* Configure CAN pin: TX. */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_CAN1_PB_TX); + + /* NVIC setup. */ + nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ); + nvic_set_priority(NVIC_USB_LP_CAN_RX0_IRQ, 1); + + /* Reset CAN. */ + can_reset(CAN1); + + /* CAN cell init. */ + if (can_init(CAN1, + false, /* TTCM: Time triggered comm mode? */ + true, /* ABOM: Automatic bus-off management? */ + false, /* AWUM: Automatic wakeup mode? */ + false, /* NART: No automatic retransmission? */ + false, /* RFLM: Receive FIFO locked mode? */ + false, /* TXFP: Transmit FIFO priority? */ + CAN_BTR_SJW_1TQ, + CAN_BTR_TS1_3TQ, + CAN_BTR_TS2_4TQ, + 12)) /* BRP+1: Baud rate prescaler */ + { + gpio_set(GPIOA, GPIO8); /* LED0 off */ + gpio_set(GPIOB, GPIO4); /* LED1 off */ + gpio_set(GPIOC, GPIO15); /* LED2 off */ + gpio_clear(GPIOC, GPIO2); /* LED3 on */ + gpio_set(GPIOC, GPIO5); /* LED4 off */ + + /* Die because we failed to initialize. */ + while (1) + __asm__("nop"); + } + + /* CAN filter 0 init. */ + can_filter_id_mask_32bit_init(CAN1, + 0, /* Filter ID */ + 0, /* CAN ID */ + 0, /* CAN ID mask */ + 0, /* FIFO assignment (here: FIFO0) */ + true); /* Enable the filter. */ + + /* Enable CAN RX interrupt. */ + can_enable_irq(CAN1, CAN_IER_FMPIE0); +} + +void sys_tick_handler(void) +{ + static int temp32 = 0; + static u8 data[8] = {0, 1, 2, 0, 0, 0, 0, 0}; + + /* We call this handler every 1ms so 1000ms = 1s on/off. */ + if (++temp32 != 1000) + return; + + temp32 = 0; + + /* Transmit CAN frame. */ + data[0]++; + if (can_transmit(CAN1, + 0, /* (EX/ST)ID: CAN ID */ + false, /* IDE: CAN ID extended? */ + false, /* RTR: Request transmit? */ + 8, /* DLC: Data length */ + data) == -1) + { + gpio_set(GPIOA, GPIO8); /* LED0 off */ + gpio_set(GPIOB, GPIO4); /* LED1 off */ + gpio_set(GPIOC, GPIO15); /* LED2 off */ + gpio_set(GPIOC, GPIO2); /* LED3 off */ + gpio_clear(GPIOC, GPIO5); /* LED4 on */ + } +} + +void usb_lp_can_rx0_isr(void) +{ + u32 id, fmi; + bool ext, rtr; + u8 length, data[8]; + + can_receive(CAN1, 0, false, &id, &ext, &rtr, &fmi, &length, data); + + if (data[0] & 1) + gpio_clear(GPIOA, GPIO8); + else + gpio_set(GPIOA, GPIO8); + + if (data[0] & 2) + gpio_clear(GPIOB, GPIO4); + else + gpio_set(GPIOB, GPIO4); + + if (data[0] & 4) + gpio_clear(GPIOC, GPIO15); + else + gpio_set(GPIOC, GPIO15); + + if (data[0] & 8) + gpio_clear(GPIOC, GPIO2); + else + gpio_set(GPIOC, GPIO2); + + can_fifo_release(CAN1, 0); +} + +int main(void) +{ + rcc_clock_setup_in_hse_12mhz_out_72mhz(); + gpio_setup(); + can_setup(); + systick_setup(); + + while (1); /* Halt. */ + + return 0; +} diff --git a/examples/stm32/f1/obldc-strip/led/Makefile b/examples/stm32/f1/obldc-strip/led/Makefile new file mode 100644 index 0000000..f8ff540 --- /dev/null +++ b/examples/stm32/f1/obldc-strip/led/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = led + +LDSCRIPT = ../obldc-strip.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/obldc-strip/led/led.c b/examples/stm32/f1/obldc-strip/led/led.c new file mode 100644 index 0000000..5fc3491 --- /dev/null +++ b/examples/stm32/f1/obldc-strip/led/led.c @@ -0,0 +1,65 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2012 Piotr Esden-Tempski + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include + +static void clock_setup(void) +{ + /* Set STM32 to 64 MHz. */ + rcc_clock_setup_in_hsi_out_64mhz(); + + /* Enable alternate function peripheral clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); + + /* Enable GPIOB clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); +} + +static void gpio_setup(void) +{ + + /* Configure PB4 as GPIO. */ + AFIO_MAPR |= AFIO_MAPR_SWJ_CFG_FULL_SWJ_NO_JNTRST; + + /* Set GPIO4 and 5 (in GPIO port B) to 'output push-pull'. */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO4 | GPIO5); + +} + +int main(void) +{ + int i; + + clock_setup(); + gpio_setup(); + + /* Blink the LEDs on the board. */ + while (1) { + gpio_toggle(GPIOB, GPIO4); /* LED on/off */ + for (i = 0; i < 8000000; i++) /* Wait a bit. */ + __asm__("nop"); + gpio_toggle(GPIOB, GPIO5); /* LED on/off */ + for (i = 0; i < 8000000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/stm32/f1/obldc-strip/obldc-strip.ld b/examples/stm32/f1/obldc-strip/obldc-strip.ld new file mode 100644 index 0000000..9778070 --- /dev/null +++ b/examples/stm32/f1/obldc-strip/obldc-strip.ld @@ -0,0 +1,31 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* Linker script for Open-BLDC (STM32F103CBT6, 128K flash, 20K RAM). */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_stm32f1.ld + diff --git a/examples/stm32/f1/obldc-strip/systick/Makefile b/examples/stm32/f1/obldc-strip/systick/Makefile new file mode 100644 index 0000000..d89dd3e --- /dev/null +++ b/examples/stm32/f1/obldc-strip/systick/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = systick + +LDSCRIPT = ../obldc-strip.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/obldc-strip/systick/systick.c b/examples/stm32/f1/obldc-strip/systick/systick.c new file mode 100644 index 0000000..fefec2c --- /dev/null +++ b/examples/stm32/f1/obldc-strip/systick/systick.c @@ -0,0 +1,82 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2012 Piotr Esden-Tempski + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include + +u32 temp32; + +static void gpio_setup(void) +{ + /* Enable alternate function peripheral clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); + + /* Enable GPIOB clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); + + gpio_clear(GPIOB, GPIO4); /* LED green on */ + gpio_set(GPIOB, GPIO5); /* LED red off */ + + /* Set GPIO4/5 (in GPIO port B) to 'output push-pull' for the LEDs. */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO4); + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO5); + + AFIO_MAPR |= AFIO_MAPR_SWJ_CFG_FULL_SWJ_NO_JNTRST; +} + +void sys_tick_handler(void) +{ + temp32++; + + /* We call this handler every 1ms so 1000ms = 1s on/off. */ + if (temp32 == 1000) { + gpio_toggle(GPIOB, GPIO4); /* LED green on/off */ + gpio_toggle(GPIOB, GPIO5); /* LED red on/off */ + temp32 = 0; + } +} + +int main(void) +{ + rcc_clock_setup_in_hsi_out_64mhz(); + gpio_setup(); + + temp32 = 0; + + /* 64MHz / 8 => 8000000 counts per second */ + systick_set_clocksource(STK_CTRL_CLKSOURCE_AHB_DIV8); + + /* 8000000/8000 = 1000 overflows per second - every 1ms one interrupt */ + /* SysTick interrupt every N clock pulses: set reload to N-1 */ + systick_set_reload(7999); + + systick_interrupt_enable(); + + /* Start counting. */ + systick_counter_enable(); + + while (1); /* Halt. */ + + return 0; +} diff --git a/examples/stm32/f1/obldc-strip/usart/Makefile b/examples/stm32/f1/obldc-strip/usart/Makefile new file mode 100644 index 0000000..40dec09 --- /dev/null +++ b/examples/stm32/f1/obldc-strip/usart/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = usart + +LDSCRIPT = ../obldc-strip.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/obldc-strip/usart/usart.c b/examples/stm32/f1/obldc-strip/usart/usart.c new file mode 100644 index 0000000..11f3403 --- /dev/null +++ b/examples/stm32/f1/obldc-strip/usart/usart.c @@ -0,0 +1,87 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2012 Piotr Esden-Tempski + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include + +static void clock_setup(void) +{ + rcc_clock_setup_in_hsi_out_64mhz(); + + /* Enable clocks for GPIO port B (for GPIO_USART1_TX and LED) and USART1. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_USART1EN); +} + +static void usart_setup(void) +{ + /* Setup GPIO6 (in GPIO port A) to 'output push-pull' for LED use. */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO5); + + AFIO_MAPR |= AFIO_MAPR_USART1_REMAP; + + /* Setup GPIO pin GPIO_USART3_TX/GPIO10 on GPIO port B for transmit. */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_RE_TX); + + /* Setup UART parameters. */ + usart_set_baudrate(USART1, 230400); + usart_set_databits(USART1, 8); + usart_set_stopbits(USART1, USART_STOPBITS_1); + usart_set_mode(USART1, USART_MODE_TX); + usart_set_parity(USART1, USART_PARITY_NONE); + usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART1); +} + +static void gpio_setup(void) +{ + /* Set GPIO5 (in GPIO port B) to 'output push-pull'. */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO5); +} + +int main(void) +{ + int i, j = 0, c = 0; + + clock_setup(); + gpio_setup(); + usart_setup(); + + /* Blink the LED (PA6) on the board with every transmitted byte. */ + while (1) { + gpio_toggle(GPIOB, GPIO5); /* LED red on/off */ + usart_send_blocking(USART1, c + '0'); /* Send a byte. */ + c = (c == 9) ? 0 : c + 1; /* Increment c. */ + if ((j++ % 80) == 0) { /* Newline after line full. */ + usart_send_blocking(USART1, '\r'); + usart_send_blocking(USART1, '\n'); + } + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/stm32/f1/obldc/can/Makefile b/examples/stm32/f1/obldc/can/Makefile new file mode 100644 index 0000000..c993a5a --- /dev/null +++ b/examples/stm32/f1/obldc/can/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = can + +LDSCRIPT = ../obldc.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/obldc/can/can.c b/examples/stm32/f1/obldc/can/can.c new file mode 100644 index 0000000..605b1a5 --- /dev/null +++ b/examples/stm32/f1/obldc/can/can.c @@ -0,0 +1,219 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * Copyright (C) 2010 Piotr Esden-Tempski + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include + +struct can_tx_msg { + u32 std_id; + u32 ext_id; + u8 ide; + u8 rtr; + u8 dlc; + u8 data[8]; +}; + +struct can_rx_msg { + u32 std_id; + u32 ext_id; + u8 ide; + u8 rtr; + u8 dlc; + u8 data[8]; + u8 fmi; +}; + +struct can_tx_msg can_tx_msg; +struct can_rx_msg can_rx_msg; + +static void gpio_setup(void) +{ + /* Enable GPIOA clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + + /* Enable GPIOB clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); + + gpio_set(GPIOA, GPIO6); /* LED0 off */ + gpio_set(GPIOA, GPIO7); /* LED1 off */ + gpio_set(GPIOB, GPIO0); /* LED2 off */ + gpio_set(GPIOB, GPIO1); /* LED3 off */ + + /* Set GPIO6/7 (in GPIO port A) to 'output push-pull' for the LEDs. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO6); + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO7); + + /* Set GPIO0/1 (in GPIO port B) to 'output push-pull' for the LEDs. */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO0); + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO1); +} + +static void systick_setup(void) +{ + /* 72MHz / 8 => 9000000 counts per second */ + systick_set_clocksource(STK_CTRL_CLKSOURCE_AHB_DIV8); + + /* 9000000/9000 = 1000 overflows per second - every 1ms one interrupt */ + /* SysTick interrupt every N clock pulses: set reload to N-1 */ + systick_set_reload(8999); + + systick_interrupt_enable(); + + /* Start counting. */ + systick_counter_enable(); +} + +static void can_setup(void) +{ + /* Enable peripheral clocks. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_CANEN); + + /* Configure CAN pin: RX (input pull-up). */ + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, + GPIO_CNF_INPUT_PULL_UPDOWN, GPIO_CAN_RX); + gpio_set(GPIOA, GPIO_CAN_RX); + + /* Configure CAN pin: TX. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_CAN_TX); + + /* NVIC setup. */ + nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ); + nvic_set_priority(NVIC_USB_LP_CAN_RX0_IRQ, 1); + + /* Reset CAN. */ + can_reset(CAN1); + + /* CAN cell init. */ + if (can_init(CAN1, + false, /* TTCM: Time triggered comm mode? */ + true, /* ABOM: Automatic bus-off management? */ + false, /* AWUM: Automatic wakeup mode? */ + false, /* NART: No automatic retransmission? */ + false, /* RFLM: Receive FIFO locked mode? */ + false, /* TXFP: Transmit FIFO priority? */ + CAN_BTR_SJW_1TQ, + CAN_BTR_TS1_3TQ, + CAN_BTR_TS2_4TQ, + 12, + false, + false)) /* BRP+1: Baud rate prescaler */ + { + gpio_set(GPIOA, GPIO6); /* LED0 off */ + gpio_set(GPIOA, GPIO7); /* LED1 off */ + gpio_set(GPIOB, GPIO0); /* LED2 off */ + gpio_clear(GPIOB, GPIO1); /* LED3 on */ + + /* Die because we failed to initialize. */ + while (1) + __asm__("nop"); + } + + /* CAN filter 0 init. */ + can_filter_id_mask_32bit_init(CAN1, + 0, /* Filter ID */ + 0, /* CAN ID */ + 0, /* CAN ID mask */ + 0, /* FIFO assignment (here: FIFO0) */ + true); /* Enable the filter. */ + + /* Enable CAN RX interrupt. */ + can_enable_irq(CAN1, CAN_IER_FMPIE0); +} + +void sys_tick_handler(void) +{ + static int temp32 = 0; + static u8 data[8] = {0, 1, 2, 0, 0, 0, 0, 0}; + + /* We call this handler every 1ms so 1000ms = 1s on/off. */ + if (++temp32 != 1000) + return; + + temp32 = 0; + + /* Transmit CAN frame. */ + data[0]++; + if (can_transmit(CAN1, + 0, /* (EX/ST)ID: CAN ID */ + false, /* IDE: CAN ID extended? */ + false, /* RTR: Request transmit? */ + 8, /* DLC: Data length */ + data) == -1) + { + gpio_set(GPIOA, GPIO6); /* LED0 off */ + gpio_set(GPIOA, GPIO7); /* LED1 off */ + gpio_clear(GPIOB, GPIO0); /* LED2 on */ + gpio_set(GPIOB, GPIO1); /* LED3 off */ + } +} + +void usb_lp_can_rx0_isr(void) +{ + u32 id, fmi; + bool ext, rtr; + u8 length, data[8]; + + can_receive(CAN1, 0, false, &id, &ext, &rtr, &fmi, &length, data); + + if (data[0] & 1) + gpio_clear(GPIOA, GPIO6); + else + gpio_set(GPIOA, GPIO6); + + if (data[0] & 2) + gpio_clear(GPIOA, GPIO7); + else + gpio_set(GPIOA, GPIO7); + + if (data[0] & 4) + gpio_clear(GPIOB, GPIO0); + else + gpio_set(GPIOB, GPIO0); + + if (data[0] & 8) + gpio_clear(GPIOB, GPIO1); + else + gpio_set(GPIOB, GPIO1); + + can_fifo_release(CAN1, 0); +} + +int main(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + gpio_setup(); + can_setup(); + systick_setup(); + + while (1); /* Halt. */ + + return 0; +} diff --git a/examples/stm32/f1/obldc/can/can.c~ b/examples/stm32/f1/obldc/can/can.c~ new file mode 100644 index 0000000..41432b6 --- /dev/null +++ b/examples/stm32/f1/obldc/can/can.c~ @@ -0,0 +1,232 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * Copyright (C) 2010 Piotr Esden-Tempski + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include + +struct can_tx_msg { + u32 std_id; + u32 ext_id; + u8 ide; + u8 rtr; + u8 dlc; + u8 data[8]; +}; + +struct can_rx_msg { + u32 std_id; + u32 ext_id; + u8 ide; + u8 rtr; + u8 dlc; + u8 data[8]; + u8 fmi; +}; + +struct can_tx_msg can_tx_msg; +struct can_rx_msg can_rx_msg; + +void gpio_setup(void) +{ + /* Enable Alternate Function clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); + + /* Enable GPIOA clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + + /* Enable GPIOB clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); + + /* Enable GPIOC clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + /* Preconfigure LEDs. */ + gpio_set(GPIOA, GPIO8); /* LED0 off */ + gpio_set(GPIOB, GPIO4); /* LED1 off */ + gpio_set(GPIOC, GPIO15); /* LED2 off */ + gpio_set(GPIOC, GPIO2); /* LED3 off */ + gpio_set(GPIOC, GPIO5); /* LED4 off */ + + /* Configure LED GPIOOs. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO8); + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO4); + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO15); + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO2); + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO5); + + /* Configure PB4 as GPIO. */ + AFIO_MAPR |= AFIO_MAPR_SWJ_CFG_FULL_SWJ_NO_JNTRST; + +} + +void systick_setup(void) +{ + /* 72MHz / 8 => 9000000 counts per second */ + systick_set_clocksource(STK_CTRL_CLKSOURCE_AHB_DIV8); + + /* 9000000/9000 = 1000 overflows per second - every 1ms one interrupt */ + systick_set_reload(9000); + + systick_interrupt_enable(); + + /* Start counting. */ + systick_counter_enable(); +} + +void can_setup(void) +{ + /* Enable peripheral clocks. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_CANEN); + + AFIO_MAPR = AFIO_MAPR_CAN1_REMAP_PORTB; + + /* Configure CAN pin: RX (input pull-up). */ + gpio_set_mode(GPIOB, GPIO_MODE_INPUT, + GPIO_CNF_INPUT_PULL_UPDOWN, GPIO_CAN1_PB_RX); + gpio_set(GPIOB, GPIO_CAN1_PB_RX); + + /* Configure CAN pin: TX. */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_CAN1_PB_TX); + + /* NVIC setup. */ + nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ); + nvic_set_priority(NVIC_USB_LP_CAN_RX0_IRQ, 1); + + /* Reset CAN. */ + can_reset(CAN1); + + /* CAN cell init. */ + if (can_init(CAN1, + false, /* TTCM: Time triggered comm mode? */ + true, /* ABOM: Automatic bus-off management? */ + false, /* AWUM: Automatic wakeup mode? */ + false, /* NART: No automatic retransmission? */ + false, /* RFLM: Receive FIFO locked mode? */ + false, /* TXFP: Transmit FIFO priority? */ + CAN_BTR_SJW_1TQ, + CAN_BTR_TS1_3TQ, + CAN_BTR_TS2_4TQ, + 12)) /* BRP+1: Baud rate prescaler */ + { + gpio_set(GPIOA, GPIO8); /* LED0 off */ + gpio_set(GPIOB, GPIO4); /* LED1 off */ + gpio_set(GPIOC, GPIO15); /* LED2 off */ + gpio_clear(GPIOC, GPIO2); /* LED3 on */ + gpio_set(GPIOC, GPIO5); /* LED4 off */ + + /* Die because we failed to initialize. */ + while (1) + __asm__("nop"); + } + + /* CAN filter 0 init. */ + can_filter_id_mask_32bit_init(CAN1, + 0, /* Filter ID */ + 0, /* CAN ID */ + 0, /* CAN ID mask */ + 0, /* FIFO assignment (here: FIFO0) */ + true); /* Enable the filter. */ + + /* Enable CAN RX interrupt. */ + can_enable_irq(CAN1, CAN_IER_FMPIE0); +} + +void sys_tick_handler(void) +{ + static int temp32 = 0; + static u8 data[8] = {0, 1, 2, 0, 0, 0, 0, 0}; + + /* We call this handler every 1ms so 1000ms = 1s on/off. */ + if (++temp32 != 1000) + return; + + temp32 = 0; + + /* Transmit CAN frame. */ + data[0]++; + if (can_transmit(CAN1, + 0, /* (EX/ST)ID: CAN ID */ + false, /* IDE: CAN ID extended? */ + false, /* RTR: Request transmit? */ + 8, /* DLC: Data length */ + data) == -1) + { + gpio_set(GPIOA, GPIO8); /* LED0 off */ + gpio_set(GPIOB, GPIO4); /* LED1 off */ + gpio_set(GPIOC, GPIO15); /* LED2 off */ + gpio_set(GPIOC, GPIO2); /* LED3 off */ + gpio_clear(GPIOC, GPIO5); /* LED4 on */ + } +} + +void usb_lp_can_rx0_isr(void) +{ + u32 id, fmi; + bool ext, rtr; + u8 length, data[8]; + + can_receive(CAN1, 0, false, &id, &ext, &rtr, &fmi, &length, data); + + if (data[0] & 1) + gpio_clear(GPIOA, GPIO8); + else + gpio_set(GPIOA, GPIO8); + + if (data[0] & 2) + gpio_clear(GPIOB, GPIO4); + else + gpio_set(GPIOB, GPIO4); + + if (data[0] & 4) + gpio_clear(GPIOC, GPIO15); + else + gpio_set(GPIOC, GPIO15); + + if (data[0] & 8) + gpio_clear(GPIOC, GPIO2); + else + gpio_set(GPIOC, GPIO2); + + can_fifo_release(CAN1, 0); +} + +int main(void) +{ + rcc_clock_setup_in_hse_12mhz_out_72mhz(); + gpio_setup(); + can_setup(); + systick_setup(); + + while (1); /* Halt. */ + + return 0; +} diff --git a/examples/stm32/f1/obldc/led/Makefile b/examples/stm32/f1/obldc/led/Makefile new file mode 100644 index 0000000..16c8c1b --- /dev/null +++ b/examples/stm32/f1/obldc/led/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = led + +LDSCRIPT = ../obldc.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/obldc/led/led.c b/examples/stm32/f1/obldc/led/led.c new file mode 100644 index 0000000..6b0ce81 --- /dev/null +++ b/examples/stm32/f1/obldc/led/led.c @@ -0,0 +1,71 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * Copyright (C) 2010 Piotr Esden-Tempski + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include + +static void clock_setup(void) +{ + /* Set STM32 to 72 MHz. */ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + + /* Enable GPIOA clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + + /* Enable GPIOB clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); +} + +static void gpio_setup(void) +{ + /* Set GPIO6 and 7 (in GPIO port A) to 'output push-pull'. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO6 | GPIO7); + + /* Set GPIO0 and 1 (in GPIO port B) to 'output push-pull'. */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO0 | GPIO1); +} + +int main(void) +{ + int i; + + clock_setup(); + gpio_setup(); + + /* Blink the LEDs on the board. */ + while (1) { + gpio_toggle(GPIOA, GPIO6); /* LED on/off */ + for (i = 0; i < 8000000; i++) /* Wait a bit. */ + __asm__("nop"); + gpio_toggle(GPIOA, GPIO7); /* LED on/off */ + for (i = 0; i < 8000000; i++) /* Wait a bit. */ + __asm__("nop"); + gpio_toggle(GPIOB, GPIO0); /* LED on/off */ + for (i = 0; i < 8000000; i++) /* Wait a bit. */ + __asm__("nop"); + gpio_toggle(GPIOB, GPIO1); /* LED on/off */ + for (i = 0; i < 8000000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/stm32/f1/obldc/obldc.ld b/examples/stm32/f1/obldc/obldc.ld new file mode 100644 index 0000000..9778070 --- /dev/null +++ b/examples/stm32/f1/obldc/obldc.ld @@ -0,0 +1,31 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* Linker script for Open-BLDC (STM32F103CBT6, 128K flash, 20K RAM). */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_stm32f1.ld + diff --git a/examples/stm32/f1/obldc/pwmleds/Makefile b/examples/stm32/f1/obldc/pwmleds/Makefile new file mode 100644 index 0000000..2b6163a --- /dev/null +++ b/examples/stm32/f1/obldc/pwmleds/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = pwmleds + +LDSCRIPT = ../obldc.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/obldc/pwmleds/pwmleds.c b/examples/stm32/f1/obldc/pwmleds/pwmleds.c new file mode 100644 index 0000000..54429e0 --- /dev/null +++ b/examples/stm32/f1/obldc/pwmleds/pwmleds.c @@ -0,0 +1,484 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Piotr Esden-Tempski + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include + +// #define COMPARE +// #define MOVING_FADE +#define KITT + +#ifdef COMPARE +#define GAMMA_LINEAR +#define GAMMA_1_3 +#define GAMMA_2_5 +#define GAMMA_3_0 +#endif + +#ifdef MOVING_FADE +#define GAMMA_2_2 +#define GAMMA_TABLE gamma_table_2_2 +#endif + +#ifdef KITT +#define GAMMA_2_5 +#define GAMMA_TABLE gamma_table_2_5 +#endif + +/* + * Gamma correction table + * + * The nonlinear tables are calculating with the function: + * Iout = Iin ** gamma + */ +#ifdef GAMMA_LINEAR +static const u16 gamma_table_linear[] = { + 1, 4, 9, 17, 26, 37, 51, 67, + 84, 104, 126, 149, 175, 203, 233, 265, + 299, 334, 372, 412, 454, 499, 545, 593, + 643, 695, 749, 805, 864, 924, 986, 1050, + 1116, 1185, 1255, 1327, 1402, 1478, 1556, 1637, + 1719, 1803, 1890, 1978, 2068, 2161, 2255, 2352, + 2450, 2550, 2653, 2757, 2864, 2972, 3082, 3195, + 3309, 3426, 3544, 3664, 3787, 3911, 4038, 4166, + 4296, 4429, 4563, 4700, 4838, 4978, 5121, 5265, + 5411, 5560, 5710, 5862, 6017, 6173, 6331, 6492, + 6654, 6818, 6985, 7153, 7323, 7495, 7670, 7846, + 8024, 8204, 8387, 8571, 8757, 8945, 9135, 9327, + 9521, 9718, 9916, 10116, 10318, 10522, 10728, 10936, + 11146, 11358, 11572, 11788, 12006, 12226, 12448, 12672, + 12897, 13125, 13355, 13587, 13821, 14057, 14294, 14534, + 14776, 15020, 15265, 15513, 15763, 16014, 16268, 16524, + 16781, 17041, 17302, 17566, 17831, 18099, 18368, 18640, + 18913, 19189, 19466, 19745, 20027, 20310, 20595, 20883, + 21172, 21463, 21756, 22051, 22349, 22648, 22949, 23252, + 23557, 23864, 24173, 24484, 24797, 25112, 25429, 25748, + 26069, 26391, 26716, 27043, 27372, 27702, 28035, 28370, + 28707, 29045, 29386, 29728, 30073, 30419, 30768, 31118, + 31471, 31825, 32182, 32540, 32900, 33263, 33627, 33993, + 34361, 34731, 35104, 35478, 35854, 36232, 36612, 36994, + 37378, 37764, 38152, 38542, 38933, 39327, 39723, 40121, + 40521, 40922, 41326, 41732, 42139, 42549, 42960, 43374, + 43789, 44207, 44626, 45048, 45471, 45896, 46324, 46753, + 47184, 47617, 48052, 48490, 48929, 49370, 49813, 50258, + 50705, 51154, 51604, 52057, 52512, 52969, 53428, 53888, + 54351, 54816, 55282, 55751, 56222, 56694, 57169, 57645, + 58123, 58604, 59086, 59570, 60057, 60545, 61035, 61527, + 62021, 62517, 63016, 63516, 64018, 64521, 65027, 65535, +}; +#endif + +#ifdef GAMMA_1_3 +static const u16 gamma_table_1_3[] = { + /* Gamma 1.3 */ + 0, 49, 120, 203, 296, 395, 501, 612, + 728, 848, 973, 1101, 1233, 1368, 1506, 1648, + 1792, 1939, 2088, 2241, 2395, 2552, 2711, 2872, + 3036, 3201, 3369, 3538, 3709, 3882, 4057, 4234, + 4412, 4592, 4774, 4957, 5142, 5329, 5517, 5706, + 5897, 6090, 6283, 6479, 6675, 6873, 7072, 7273, + 7475, 7678, 7882, 8088, 8294, 8502, 8711, 8922, + 9133, 9346, 9559, 9774, 9990, 10207, 10425, 10644, + 10864, 11086, 11308, 11531, 11755, 11981, 12207, 12434, + 12662, 12891, 13121, 13352, 13584, 13817, 14051, 14285, + 14521, 14757, 14994, 15233, 15472, 15712, 15952, 16194, + 16436, 16679, 16923, 17168, 17414, 17660, 17908, 18156, + 18405, 18654, 18905, 19156, 19408, 19660, 19914, 20168, + 20423, 20679, 20935, 21192, 21450, 21708, 21968, 22228, + 22488, 22750, 23012, 23275, 23538, 23802, 24067, 24332, + 24599, 24865, 25133, 25401, 25670, 25939, 26209, 26480, + 26751, 27023, 27296, 27569, 27843, 28118, 28393, 28669, + 28945, 29222, 29500, 29778, 30057, 30336, 30616, 30897, + 31178, 31460, 31742, 32025, 32308, 32592, 32877, 33162, + 33448, 33734, 34021, 34309, 34597, 34885, 35175, 35464, + 35754, 36045, 36337, 36628, 36921, 37214, 37507, 37801, + 38096, 38391, 38686, 38982, 39279, 39576, 39874, 40172, + 40471, 40770, 41070, 41370, 41670, 41972, 42273, 42576, + 42878, 43181, 43485, 43789, 44094, 44399, 44705, 45011, + 45317, 45625, 45932, 46240, 46549, 46858, 47167, 47477, + 47787, 48098, 48409, 48721, 49034, 49346, 49659, 49973, + 50287, 50602, 50917, 51232, 51548, 51864, 52181, 52498, + 52816, 53134, 53452, 53771, 54091, 54411, 54731, 55052, + 55373, 55694, 56016, 56339, 56662, 56985, 57309, 57633, + 57957, 58282, 58608, 58933, 59260, 59586, 59913, 60241, + 60569, 60897, 61226, 61555, 61884, 62214, 62545, 62875, + 63206, 63538, 63870, 64202, 64535, 64868, 65201, 65535, +}; +#endif + +#ifdef GAMMA_2_2 +static const u16 gamma_table_2_2[] = { + 0, 0, 2, 4, 7, 11, 17, 24, + 32, 42, 53, 65, 79, 94, 111, 129, + 148, 169, 192, 216, 242, 270, 299, 330, + 362, 396, 432, 469, 508, 549, 591, 635, + 681, 729, 779, 830, 883, 938, 995, 1053, + 1113, 1175, 1239, 1305, 1373, 1443, 1514, 1587, + 1663, 1740, 1819, 1900, 1983, 2068, 2155, 2243, + 2334, 2427, 2521, 2618, 2717, 2817, 2920, 3024, + 3131, 3240, 3350, 3463, 3578, 3694, 3813, 3934, + 4057, 4182, 4309, 4438, 4570, 4703, 4838, 4976, + 5115, 5257, 5401, 5547, 5695, 5845, 5998, 6152, + 6309, 6468, 6629, 6792, 6957, 7124, 7294, 7466, + 7640, 7816, 7994, 8175, 8358, 8543, 8730, 8919, + 9111, 9305, 9501, 9699, 9900, 10102, 10307, 10515, + 10724, 10936, 11150, 11366, 11585, 11806, 12029, 12254, + 12482, 12712, 12944, 13179, 13416, 13655, 13896, 14140, + 14386, 14635, 14885, 15138, 15394, 15652, 15912, 16174, + 16439, 16706, 16975, 17247, 17521, 17798, 18077, 18358, + 18642, 18928, 19216, 19507, 19800, 20095, 20393, 20694, + 20996, 21301, 21609, 21919, 22231, 22546, 22863, 23182, + 23504, 23829, 24156, 24485, 24817, 25151, 25487, 25826, + 26168, 26512, 26858, 27207, 27558, 27912, 28268, 28627, + 28988, 29351, 29717, 30086, 30457, 30830, 31206, 31585, + 31966, 32349, 32735, 33124, 33514, 33908, 34304, 34702, + 35103, 35507, 35913, 36321, 36732, 37146, 37562, 37981, + 38402, 38825, 39252, 39680, 40112, 40546, 40982, 41421, + 41862, 42306, 42753, 43202, 43654, 44108, 44565, 45025, + 45487, 45951, 46418, 46888, 47360, 47835, 48313, 48793, + 49275, 49761, 50249, 50739, 51232, 51728, 52226, 52727, + 53230, 53736, 54245, 54756, 55270, 55787, 56306, 56828, + 57352, 57879, 58409, 58941, 59476, 60014, 60554, 61097, + 61642, 62190, 62741, 63295, 63851, 64410, 64971, 65535, +}; +#endif + +#ifdef GAMMA_2_5 +static const u16 gamma_table_2_5[] = { + /* gamma = 2.5 */ + 0, 0, 0, 1, 2, 4, 6, 8, + 11, 15, 20, 25, 31, 38, 46, 55, + 65, 75, 87, 99, 113, 128, 143, 160, + 178, 197, 218, 239, 262, 286, 311, 338, + 366, 395, 425, 457, 491, 526, 562, 599, + 639, 679, 722, 765, 811, 857, 906, 956, + 1007, 1061, 1116, 1172, 1231, 1291, 1352, 1416, + 1481, 1548, 1617, 1688, 1760, 1834, 1910, 1988, + 2068, 2150, 2233, 2319, 2407, 2496, 2587, 2681, + 2776, 2874, 2973, 3075, 3178, 3284, 3391, 3501, + 3613, 3727, 3843, 3961, 4082, 4204, 4329, 4456, + 4585, 4716, 4850, 4986, 5124, 5264, 5407, 5552, + 5699, 5849, 6001, 6155, 6311, 6470, 6632, 6795, + 6962, 7130, 7301, 7475, 7650, 7829, 8009, 8193, + 8379, 8567, 8758, 8951, 9147, 9345, 9546, 9750, + 9956, 10165, 10376, 10590, 10806, 11025, 11247, 11472, + 11699, 11929, 12161, 12397, 12634, 12875, 13119, 13365, + 13614, 13865, 14120, 14377, 14637, 14899, 15165, 15433, + 15705, 15979, 16256, 16535, 16818, 17104, 17392, 17683, + 17978, 18275, 18575, 18878, 19184, 19493, 19805, 20119, + 20437, 20758, 21082, 21409, 21739, 22072, 22407, 22746, + 23089, 23434, 23782, 24133, 24487, 24845, 25206, 25569, + 25936, 26306, 26679, 27055, 27435, 27818, 28203, 28592, + 28985, 29380, 29779, 30181, 30586, 30994, 31406, 31820, + 32239, 32660, 33085, 33513, 33944, 34379, 34817, 35258, + 35702, 36150, 36602, 37056, 37514, 37976, 38441, 38909, + 39380, 39856, 40334, 40816, 41301, 41790, 42282, 42778, + 43277, 43780, 44286, 44795, 45308, 45825, 46345, 46869, + 47396, 47927, 48461, 48999, 49540, 50085, 50634, 51186, + 51742, 52301, 52864, 53431, 54001, 54575, 55153, 55734, + 56318, 56907, 57499, 58095, 58695, 59298, 59905, 60515, + 61130, 61748, 62370, 62995, 63624, 64258, 64894, 65535, +}; +#endif + +#ifdef GAMMA_3_0 +static const u16 gamma_table_3_0[] = { + /* gamma = 3.0 */ + 0, 0, 0, 0, 0, 0, 1, 1, + 2, 3, 4, 5, 7, 9, 11, 13, + 16, 19, 23, 27, 32, 37, 42, 48, + 55, 62, 69, 78, 87, 96, 107, 118, + 130, 142, 155, 169, 184, 200, 217, 234, + 253, 272, 293, 314, 337, 360, 385, 410, + 437, 465, 494, 524, 556, 588, 622, 658, + 694, 732, 771, 812, 854, 897, 942, 988, + 1036, 1085, 1136, 1189, 1243, 1298, 1356, 1415, + 1475, 1538, 1602, 1667, 1735, 1804, 1876, 1949, + 2024, 2100, 2179, 2260, 2343, 2427, 2514, 2603, + 2693, 2786, 2881, 2978, 3078, 3179, 3283, 3389, + 3497, 3607, 3720, 3835, 3952, 4072, 4194, 4319, + 4446, 4575, 4707, 4842, 4979, 5118, 5261, 5405, + 5553, 5703, 5856, 6011, 6169, 6330, 6494, 6660, + 6830, 7002, 7177, 7355, 7536, 7719, 7906, 8096, + 8289, 8484, 8683, 8885, 9090, 9298, 9510, 9724, + 9942, 10163, 10387, 10614, 10845, 11079, 11317, 11557, + 11802, 12049, 12300, 12555, 12813, 13074, 13339, 13608, + 13880, 14156, 14435, 14718, 15005, 15295, 15589, 15887, + 16189, 16494, 16803, 17117, 17433, 17754, 18079, 18408, + 18740, 19077, 19418, 19762, 20111, 20464, 20821, 21182, + 21547, 21917, 22290, 22668, 23050, 23436, 23827, 24222, + 24621, 25025, 25433, 25845, 26262, 26683, 27109, 27539, + 27974, 28413, 28857, 29306, 29759, 30217, 30680, 31147, + 31619, 32095, 32577, 33063, 33554, 34050, 34551, 35056, + 35567, 36082, 36602, 37128, 37658, 38194, 38734, 39280, + 39830, 40386, 40947, 41513, 42084, 42661, 43243, 43830, + 44422, 45019, 45622, 46231, 46844, 47463, 48088, 48718, + 49353, 49994, 50641, 51293, 51950, 52614, 53282, 53957, + 54637, 55323, 56014, 56712, 57415, 58123, 58838, 59558, + 60285, 61017, 61755, 62499, 63249, 64005, 64767, 65535, +}; +#endif + +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + + /* Enable TIM3 clock. */ + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_TIM3EN); + + /* Enable GPIOC, Alternate Function clocks. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, + RCC_APB2ENR_IOPAEN | + RCC_APB2ENR_IOPBEN | + RCC_APB2ENR_AFIOEN); +} + +static void gpio_setup(void) +{ + /* + * Set GPIO6 and 7 (in GPIO port A) to + * 'output alternate function push-pull'. + */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, + GPIO_TIM3_CH1 | GPIO_TIM3_CH2); + + /* + * Set GPIO0 and 1 (in GPIO port B) to + * 'output alternate function push-pull'. + */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, + GPIO_TIM3_CH3 | GPIO_TIM3_CH4); + +} + +static void tim_setup(void) +{ + /* Clock division and mode */ + TIM3_CR1 = TIM_CR1_CKD_CK_INT | TIM_CR1_CMS_EDGE; + /* Period */ + TIM3_ARR = 65535; + /* Prescaler */ + TIM3_PSC = 0; + TIM3_EGR = TIM_EGR_UG; + + /* ---- */ + /* Output compare 1 mode and preload */ + TIM3_CCMR1 |= TIM_CCMR1_OC1M_PWM1 | TIM_CCMR1_OC1PE; + + /* Polarity and state */ + TIM3_CCER |= TIM_CCER_CC1P | TIM_CCER_CC1E; + //TIM3_CCER |= TIM_CCER_CC1E; + + /* Capture compare value */ + TIM3_CCR1 = 0; + + /* ---- */ + /* Output compare 2 mode and preload */ + TIM3_CCMR1 |= TIM_CCMR1_OC2M_PWM1 | TIM_CCMR1_OC2PE; + + /* Polarity and state */ + TIM3_CCER |= TIM_CCER_CC2P | TIM_CCER_CC2E; + //TIM3_CCER |= TIM_CCER_CC2E; + + /* Capture compare value */ + TIM3_CCR2 = 0; + + /* ---- */ + /* Output compare 3 mode and preload */ + TIM3_CCMR2 |= TIM_CCMR2_OC3M_PWM1 | TIM_CCMR2_OC3PE; + + /* Polarity and state */ + TIM3_CCER |= TIM_CCER_CC3P | TIM_CCER_CC3E; + //TIM3_CCER |= TIM_CCER_CC3E; + + /* Capture compare value */ + TIM3_CCR3 = 0; + + /* ---- */ + /* Output compare 4 mode and preload */ + TIM3_CCMR2 |= TIM_CCMR2_OC4M_PWM1 | TIM_CCMR2_OC4PE; + + /* Polarity and state */ + TIM3_CCER |= TIM_CCER_CC4P | TIM_CCER_CC4E; + //TIM3_CCER |= TIM_CCER_CC4E; + + /* Capture compare value */ + TIM3_CCR4 = 0; + + /* ---- */ + /* ARR reload enable */ + TIM3_CR1 |= TIM_CR1_ARPE; + + /* Counter enable */ + TIM3_CR1 |= TIM_CR1_CEN; +} + +int main(void) +{ + int i, j0, j1, j2, j3, d0, d1, d2, d3, j, k, kd; + + clock_setup(); + gpio_setup(); + tim_setup(); + +#ifdef COMPARE + j0 = 0; + d0 = 1; + j1 = 0; + d1 = 1; + j2 = 0; + d2 = 1; + j3 = 0; + d3 = 1; + while (1) { + TIM3_CCR1 = gamma_table_linear[j0]; + j0 += d0; + if (j0 == 255) + d0 = -1; + if (j0 == 0) + d0 = 1; + TIM3_CCR2 = gamma_table_1_3[j1]; + j1 += d1; + if (j1 == 255) + d1 = -1; + if (j1 == 0) + d1 = 1; + TIM3_CCR3 = gamma_table_2_5[j2]; + j2 += d2; + if (j2 == 255) + d2 = -1; + if (j2 == 0) + d2 = 1; + TIM3_CCR4 = gamma_table_3_0[j3]; + j3 += d3; + if (j3 == 255) + d3 = -1; + if (j3 == 0) + d3 = 1; + for (i = 0; i < 50000; i++); + } +#endif + +#ifdef MOVING_FADE + j0 = 0; + d0 = 1; + j1 = 128; + d1 = 1; + j2 = 255; + d2 = -1; + j3 = 128; + d3 = -1; + while (1) { + TIM3_CCR1 = GAMMA_TABLE[j0]; + j0 += d0; + if (j0 == 255) + d0 = -1; + if (j0 == 0) + d0 = 1; + TIM3_CCR2 = GAMMA_TABLE[j1]; + j1 += d1; + if (j1 == 255) + d1 = -1; + if (j1 == 0) + d1 = 1; + TIM3_CCR3 = GAMMA_TABLE[j2]; + j2 += d2; + if (j2 == 255) + d2 = -1; + if (j2 == 0) + d2 = 1; + TIM3_CCR4 = GAMMA_TABLE[j3]; + j3 += d3; + if (j3 == 255) + d3 = -1; + if (j3 == 0) + d3 = 1; + for (i = 0; i < 10000; i++); + } +#endif + +#ifdef KITT + j0 = 255; + d0 = -1; + j1 = 20; + d1 = -1; + j2 = 20; + d2 = -1; + j3 = 20; + d3 = -1; + j = 0; + k = 0; + kd = 1; + while (1) { + TIM3_CCR1 = GAMMA_TABLE[j0]; + j0 += d0; + if (j0 == 255) + d0 = -1; + if (j0 == 19) + j0 = 20; + TIM3_CCR2 = GAMMA_TABLE[j1]; + j1 += d1; + if (j1 == 255) + d1 = -1; + if (j1 == 19) + j1 = 20; + TIM3_CCR3 = GAMMA_TABLE[j2]; + j2 += d2; + if (j2 == 255) + d2 = -1; + if (j2 == 19) + j2 = 20; + TIM3_CCR4 = GAMMA_TABLE[j3]; + j3 += d3; + if (j3 == 255) + d3 = -1; + if (j3 == 19) + j3 = 20; + for (i = 0; i < 15000; i++) + __asm__("nop"); + j++; + if (j == 100) { + j = 0; + switch (k += kd) { + case 0: + j0 = 255; + break; + case 1: + j1 = 255; + break; + case 2: + j2 = 255; + break; + case 3: + j3 = 255; + break; + } + if (k == 3) + kd = -1; + if (k == 0) + kd = 1; + } + } +#endif + + return 0; +} diff --git a/examples/stm32/f1/obldc/systick/Makefile b/examples/stm32/f1/obldc/systick/Makefile new file mode 100644 index 0000000..6268ee4 --- /dev/null +++ b/examples/stm32/f1/obldc/systick/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = systick + +LDSCRIPT = ../obldc.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/obldc/systick/systick.c b/examples/stm32/f1/obldc/systick/systick.c new file mode 100644 index 0000000..a92c0b6 --- /dev/null +++ b/examples/stm32/f1/obldc/systick/systick.c @@ -0,0 +1,91 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * Copyright (C) 2010 Piotr Esden-Tempski + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include + +u32 temp32; + +static void gpio_setup(void) +{ + /* Enable GPIOA clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + + /* Enable GPIOB clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); + + gpio_set(GPIOA, GPIO6); /* LED0 off */ + gpio_set(GPIOA, GPIO7); /* LED1 off */ + gpio_set(GPIOB, GPIO0); /* LED2 off */ + gpio_set(GPIOB, GPIO1); /* LED3 off */ + + /* Set GPIO6/7 (in GPIO port A) to 'output push-pull' for the LEDs. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO6); + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO7); + + /* Set GPIO0/1 (in GPIO port B) to 'output push-pull' for the LEDs. */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO0); + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO1); +} + +void sys_tick_handler(void) +{ + temp32++; + + /* We call this handler every 1ms so 1000ms = 1s on/off. */ + if (temp32 == 1000) { + gpio_toggle(GPIOA, GPIO6); /* LED2 on/off */ + temp32 = 0; + } +} + +int main(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + gpio_setup(); + + gpio_clear(GPIOA, GPIO7); /* LED1 on */ + gpio_set(GPIOA, GPIO6); /* LED2 off */ + + temp32 = 0; + + /* 72MHz / 8 => 9000000 counts per second */ + systick_set_clocksource(STK_CTRL_CLKSOURCE_AHB_DIV8); + + /* 9000000/9000 = 1000 overflows per second - every 1ms one interrupt */ + /* SysTick interrupt every N clock pulses: set reload to N-1 */ + systick_set_reload(8999); + + systick_interrupt_enable(); + + /* Start counting. */ + systick_counter_enable(); + + while (1); /* Halt. */ + + return 0; +} diff --git a/examples/stm32/f1/obldc/usart/Makefile b/examples/stm32/f1/obldc/usart/Makefile new file mode 100644 index 0000000..ebdc65a --- /dev/null +++ b/examples/stm32/f1/obldc/usart/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = usart + +LDSCRIPT = ../obldc.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/obldc/usart/usart.c b/examples/stm32/f1/obldc/usart/usart.c new file mode 100644 index 0000000..e1906fc --- /dev/null +++ b/examples/stm32/f1/obldc/usart/usart.c @@ -0,0 +1,90 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include + +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + + /* Enable GPIOA clock (for LED GPIOs). */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + + /* Enable clocks for GPIO port B (for GPIO_USART1_TX) and USART1. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_USART1EN); +} + +static void usart_setup(void) +{ + /* Setup GPIO6 (in GPIO port A) to 'output push-pull' for LED use. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO6); + + AFIO_MAPR |= AFIO_MAPR_USART1_REMAP; + + /* Setup GPIO pin GPIO_USART3_TX/GPIO10 on GPIO port B for transmit. */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_RE_TX); + + /* Setup UART parameters. */ + usart_set_baudrate(USART1, 230400); + usart_set_databits(USART1, 8); + usart_set_stopbits(USART1, USART_STOPBITS_1); + usart_set_mode(USART1, USART_MODE_TX); + usart_set_parity(USART1, USART_PARITY_NONE); + usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART1); +} + +static void gpio_setup(void) +{ + /* Set GPIO6 (in GPIO port A) to 'output push-pull'. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO6); +} + +int main(void) +{ + int i, j = 0, c = 0; + + clock_setup(); + gpio_setup(); + usart_setup(); + + /* Blink the LED (PA6) on the board with every transmitted byte. */ + while (1) { + gpio_toggle(GPIOA, GPIO6); /* LED on/off */ + usart_send_blocking(USART1, c + '0'); /* Send a byte. */ + c = (c == 9) ? 0 : c + 1; /* Increment c. */ + if ((j++ % 80) == 0) { /* Newline after line full. */ + usart_send_blocking(USART1, '\r'); + usart_send_blocking(USART1, '\n'); + } + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/stm32/f1/obldc/usart_irq/Makefile b/examples/stm32/f1/obldc/usart_irq/Makefile new file mode 100644 index 0000000..af12f39 --- /dev/null +++ b/examples/stm32/f1/obldc/usart_irq/Makefile @@ -0,0 +1,27 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = usart_irq + +OOCD_BOARD = open-bldc + +LDSCRIPT = ../obldc.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/obldc/usart_irq/usart_irq.c b/examples/stm32/f1/obldc/usart_irq/usart_irq.c new file mode 100644 index 0000000..5ffb54f --- /dev/null +++ b/examples/stm32/f1/obldc/usart_irq/usart_irq.c @@ -0,0 +1,120 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include + +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + + /* Enable GPIOA clock (for LED GPIOs). */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + + /* Enable clocks for GPIO port B (for GPIO_USART1_TX) and USART1. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_USART1EN); +} + +static void usart_setup(void) +{ + /* Enable the USART1 interrupt. */ + nvic_enable_irq(NVIC_USART1_IRQ); + + /* Enable USART1 pin software remapping. */ + AFIO_MAPR |= AFIO_MAPR_USART1_REMAP; + + /* Setup GPIO pin GPIO_USART1_RE_TX on GPIO port B for transmit. */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_RE_TX); + + /* Setup GPIO pin GPIO_USART1_RE_RX on GPIO port B for receive. */ + gpio_set_mode(GPIOB, GPIO_MODE_INPUT, + GPIO_CNF_INPUT_FLOAT, GPIO_USART1_RE_RX); + + /* Setup UART parameters. */ + usart_set_baudrate(USART1, 230400); + usart_set_databits(USART1, 8); + usart_set_stopbits(USART1, USART_STOPBITS_1); + usart_set_parity(USART1, USART_PARITY_NONE); + usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE); + usart_set_mode(USART1, USART_MODE_TX_RX); + + /* Enable USART1 Receive interrupt. */ + USART_CR1(USART1) |= USART_CR1_RXNEIE; + + /* Finally enable the USART. */ + usart_enable(USART1); +} + +static void gpio_setup(void) +{ + gpio_set(GPIOA, GPIO6 | GPIO7); + + /* Setup GPIO6 and 7 (in GPIO port A) for LED use. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO6 | GPIO7); +} + +void usart1_isr(void) +{ + static u8 data = 'A'; + + /* Check if we were called because of RXNE. */ + if (((USART_CR1(USART1) & USART_CR1_RXNEIE) != 0) && + ((USART_SR(USART1) & USART_SR_RXNE) != 0)) { + /* Indicate that we got data. */ + gpio_toggle(GPIOA, GPIO6); + + /* Retrieve the data from the peripheral. */ + data = usart_recv(USART1); + + /* Enable transmit interrupt so it sends back the data. */ + USART_CR1(USART1) |= USART_CR1_TXEIE; + } + + /* Check if we were called because of TXE. */ + if (((USART_CR1(USART1) & USART_CR1_TXEIE) != 0) && + ((USART_SR(USART1) & USART_SR_TXE) != 0)) { + /* Indicate that we are sending out data. */ + gpio_toggle(GPIOA, GPIO7); + + /* Put data into the transmit register. */ + usart_send(USART1, data); + + /* Disable the TXE interrupt as we don't need it anymore. */ + USART_CR1(USART1) &= ~USART_CR1_TXEIE; + } +} + +int main(void) +{ + clock_setup(); + gpio_setup(); + usart_setup(); + + /* Wait forever and do nothing. */ + while (1) + __asm__("nop"); + + return 0; +} diff --git a/examples/stm32/f1/other/adc_temperature_sensor/Makefile b/examples/stm32/f1/other/adc_temperature_sensor/Makefile new file mode 100644 index 0000000..910a03a --- /dev/null +++ b/examples/stm32/f1/other/adc_temperature_sensor/Makefile @@ -0,0 +1,23 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = adc + +include ../../Makefile.include + diff --git a/examples/stm32/f1/other/adc_temperature_sensor/README b/examples/stm32/f1/other/adc_temperature_sensor/README new file mode 100644 index 0000000..e17f8f8 --- /dev/null +++ b/examples/stm32/f1/other/adc_temperature_sensor/README @@ -0,0 +1,10 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example program sends some characters on USART1. +Afterwards it read out the internal temperature sensor of the STM32 and +sends the value read out from the ADC to the USART1. + +The terminal settings for the receiving device/PC are 115200 8n1. + diff --git a/examples/stm32/f1/other/adc_temperature_sensor/adc.c b/examples/stm32/f1/other/adc_temperature_sensor/adc.c new file mode 100644 index 0000000..47023e7 --- /dev/null +++ b/examples/stm32/f1/other/adc_temperature_sensor/adc.c @@ -0,0 +1,153 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include + +static void usart_setup(void) +{ + /* Enable clocks for GPIO port A (for GPIO_USART1_TX) and USART1. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_USART1EN); + + /* Setup GPIO pin GPIO_USART1_TX/GPIO9 on GPIO port A for transmit. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX); + + /* Setup UART parameters. */ + usart_set_baudrate(USART1, 115200); + usart_set_databits(USART1, 8); + usart_set_stopbits(USART1, USART_STOPBITS_1); + usart_set_mode(USART1, USART_MODE_TX_RX); + usart_set_parity(USART1, USART_PARITY_NONE); + usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART1); +} + +static void gpio_setup(void) +{ + /* Enable GPIOB clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); + + /* Set GPIO6/7 (in GPIO port B) to 'output push-pull' for the LEDs. */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO6); + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO7); +} + +static void adc_setup(void) +{ + int i; + + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_ADC1EN); + + /* Make sure the ADC doesn't run during config. */ + adc_off(ADC1); + + /* We configure everything for one single conversion. */ + adc_disable_scan_mode(ADC1); + adc_set_single_conversion_mode(ADC1); + adc_disable_external_trigger_regular(ADC1); + adc_set_right_aligned(ADC1); + /* We want to read the temperature sensor, so we have to enable it. */ + adc_enable_temperature_sensor(ADC1); + adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC); + + adc_power_on(ADC1); + + /* Wait for ADC starting up. */ + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + + adc_reset_calibration(ADC1); + adc_calibration(ADC1); +} + +static void my_usart_print_int(u32 usart, int value) +{ + s8 i; + u8 nr_digits = 0; + char buffer[25]; + + if (value < 0) { + usart_send(usart, '-'); + value = value * -1; + } + + while (value > 0) { + buffer[nr_digits++] = "0123456789"[value % 10]; + value /= 10; + } + + for (i = nr_digits; i >= 0; i--) + usart_send(usart, buffer[i]); +} + +int main(void) +{ + u8 channel_array[16]; + u16 temperature; + + rcc_clock_setup_in_hse_16mhz_out_72mhz(); + gpio_setup(); + usart_setup(); + adc_setup(); + + gpio_clear(GPIOB, GPIO7); /* LED1 on */ + gpio_set(GPIOB, GPIO6); /* LED2 off */ + + /* Send a message on USART1. */ + usart_send(USART1, 's'); + usart_send(USART1, 't'); + usart_send(USART1, 'm'); + usart_send(USART1, '\r'); + usart_send(USART1, '\n'); + + /* Select the channel we want to convert. 16=temperature_sensor. */ + channel_array[0] = 16; + adc_set_regular_sequence(ADC1, 1, channel_array); + + /* + * Start the conversion directly (not trigger mode). + */ + adc_start_conversion_direct(ADC1); + + /* Wait for end of conversion. */ + while (!(ADC_SR(ADC1) & ADC_SR_EOC)); + + temperature = ADC_DR(ADC1); + + /* + * That's actually not the real temperature - you have to compute it + * as described in the datasheet. + */ + my_usart_print_int(USART1, temperature); + + gpio_clear(GPIOB, GPIO6); /* LED2 on */ + + while(1); /* Halt. */ + + return 0; +} diff --git a/examples/stm32/f1/other/adc_temperature_sensor/adc.ld b/examples/stm32/f1/other/adc_temperature_sensor/adc.ld new file mode 100644 index 0000000..9c6c11a --- /dev/null +++ b/examples/stm32/f1/other/adc_temperature_sensor/adc.ld @@ -0,0 +1,31 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* Linker script for an STM32F103RBT6 board (128K flash, 20K RAM). */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_stm32f1.ld + diff --git a/examples/stm32/f1/other/dma_mem2mem/Makefile b/examples/stm32/f1/other/dma_mem2mem/Makefile new file mode 100644 index 0000000..50d63b0 --- /dev/null +++ b/examples/stm32/f1/other/dma_mem2mem/Makefile @@ -0,0 +1,23 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = dma + +include ../../Makefile.include + diff --git a/examples/stm32/f1/other/dma_mem2mem/README b/examples/stm32/f1/other/dma_mem2mem/README new file mode 100644 index 0000000..3fb5320 --- /dev/null +++ b/examples/stm32/f1/other/dma_mem2mem/README @@ -0,0 +1,11 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This program demonstrates a little DMA MEM2MEM transfer. A string is sent out +to USART1 and afterwards copied by DMA to another memory location. To check +if the transfer was successful we send the destination string also out to +USART1. + +The terminal settings for the receiving device/PC are 115200 8n1. + diff --git a/examples/stm32/f1/other/dma_mem2mem/dma.c b/examples/stm32/f1/other/dma_mem2mem/dma.c new file mode 100644 index 0000000..d9dae43 --- /dev/null +++ b/examples/stm32/f1/other/dma_mem2mem/dma.c @@ -0,0 +1,140 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include + +static void usart_setup(void) +{ + /* Enable clocks for GPIO port A (for GPIO_USART1_TX) and USART1. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_USART1EN); + + /* Setup GPIO pin GPIO_USART1_TX/GPIO9 on GPIO port A for transmit. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX); + + /* Setup UART parameters. */ + usart_set_baudrate(USART1, 115200); + usart_set_databits(USART1, 8); + usart_set_stopbits(USART1, USART_STOPBITS_1); + usart_set_mode(USART1, USART_MODE_TX_RX); + usart_set_parity(USART1, USART_PARITY_NONE); + usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART1); +} + +static void gpio_setup(void) +{ + /* Enable GPIOB clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); + + /* Set GPIO6/7 (in GPIO port B) to 'output push-pull' for the LEDs. */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO6); + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO7); +} + +static void my_usart_print_string(u32 usart, char *s) +{ + while (*s != 0) { + usart_send(usart, *s); + s++; + } +} + +int main(void) +{ + /* + * Exactly 20 bytes including '\0' at the end. + * We want to transfer 32bit * 5 so it should fit. + */ + char s1[20] = "Hello STM MEM2MEM\r\n"; + char s2[20]; + + rcc_clock_setup_in_hse_16mhz_out_72mhz(); + gpio_setup(); + usart_setup(); + + gpio_clear(GPIOB, GPIO7); /* LED1 on */ + gpio_set(GPIOB, GPIO6); /* LED2 off */ + + my_usart_print_string(USART1, "s1 "); + my_usart_print_string(USART1, s1); + + rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_DMA1EN); + + /* MEM2MEM mode for channel 1. */ + dma_enable_mem2mem_mode(DMA1, DMA_CHANNEL1); + + /* Highest priority. */ + dma_set_priority(DMA1, DMA_CHANNEL1, DMA_CCR_PL_VERY_HIGH); + + /* 32Bit wide transfer for source and destination. */ + dma_set_memory_size(DMA1, DMA_CHANNEL1, DMA_CCR_MSIZE_32BIT); + dma_set_peripheral_size(DMA1, DMA_CHANNEL1, DMA_CCR_PSIZE_32BIT); + + /* + * After every 32bits we have to increase the address because + * we use RAM. + */ + dma_enable_memory_increment_mode(DMA1, DMA_CHANNEL1); + dma_enable_peripheral_increment_mode(DMA1, DMA_CHANNEL1); + + /* We define the source as peripheral. */ + dma_set_read_from_peripheral(DMA1, DMA_CHANNEL1); + + /* We want to transfer string s1. */ + dma_set_peripheral_address(DMA1, DMA_CHANNEL1, (u32)&s1); + + /* Destination should be string s2. */ + dma_set_memory_address(DMA1, DMA_CHANNEL1, (u32)&s2); + + /* + * Set number of DATA to transfer. + * Remember that this means not necessary bytes but MSIZE or PSIZE + * depending on your source device. + */ + dma_set_number_of_data(DMA1, DMA_CHANNEL1, 5); + + /* Start DMA transfer. */ + dma_enable_channel(DMA1, DMA_CHANNEL1); + + /* TODO: Write a function to get the interrupt flags. */ + while (!(DMA_ISR(DMA1) & 0x0000001)) + ; + + dma_disable_channel(DMA1, DMA_CHANNEL1); + + /* String s1 should now already be transferred to s2. */ + my_usart_print_string(USART1, "s2 "); + my_usart_print_string(USART1, s2); + + gpio_clear(GPIOB, GPIO6); /* LED2 on */ + + while (1); /* Halt. */ + + return 0; +} diff --git a/examples/stm32/f1/other/dma_mem2mem/dma.ld b/examples/stm32/f1/other/dma_mem2mem/dma.ld new file mode 100644 index 0000000..9c6c11a --- /dev/null +++ b/examples/stm32/f1/other/dma_mem2mem/dma.ld @@ -0,0 +1,31 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* Linker script for an STM32F103RBT6 board (128K flash, 20K RAM). */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_stm32f1.ld + diff --git a/examples/stm32/f1/other/dogm128/Makefile b/examples/stm32/f1/other/dogm128/Makefile new file mode 100644 index 0000000..059fcc1 --- /dev/null +++ b/examples/stm32/f1/other/dogm128/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = main + +OBJS = dogm128.o + +include ../../Makefile.include + diff --git a/examples/stm32/f1/other/dogm128/README b/examples/stm32/f1/other/dogm128/README new file mode 100644 index 0000000..dbf82a0 --- /dev/null +++ b/examples/stm32/f1/other/dogm128/README @@ -0,0 +1,7 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example program writes some text on an DOGM128 LCD display connected +to SPI2. + diff --git a/examples/stm32/f1/other/dogm128/dogm128.c b/examples/stm32/f1/other/dogm128/dogm128.c new file mode 100644 index 0000000..0c1bf80 --- /dev/null +++ b/examples/stm32/f1/other/dogm128/dogm128.c @@ -0,0 +1,302 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include "./dogm128.h" + +u8 dogm128_ram[1024]; +u8 dogm128_cursor_x; +u8 dogm128_cursor_y; + +void dogm128_send_command(u8 command) +{ + u32 i; + + gpio_clear(DOGM128_A0_PORT, DOGM128_A0_PIN); /* A0 low for commands */ + spi_send(DOGM128_SPI, command); + + for (i = 0; i <= 500; i++) /* Wait a bit. */ + ; +} + +void dogm128_send_data(u8 data) +{ + u32 i; + + gpio_set(DOGM128_A0_PORT, DOGM128_A0_PIN); /* A0 high for data */ + spi_send(DOGM128_SPI, data); + + for (i = 0; i <= 500; i++) /* Wait a bit. */ + ; +} + +void dogm128_init(void) +{ + u32 i; + + /* Reset the display (reset low for dogm128). */ + gpio_clear(DOGM128_RESET_PORT, DOGM128_RESET_PIN); + for (i = 0; i <= 60000; i++) /* Wait a bit. */ + ; + + /* Get the display out of reset (reset high for dogm128). */ + gpio_set(DOGM128_RESET_PORT, DOGM128_RESET_PIN); + for (i = 0; i <= 60000; i++) /* Wait a bit. */ + ; + + gpio_clear(DOGM128_A0_PORT, DOGM128_A0_PIN); /* A0 low for init */ + + /* Tell the display that we want to start. */ + spi_set_nss_low(DOGM128_SPI); + + /* Init sequence. */ + dogm128_send_command(DOGM128_DISPLAY_START_ADDRESS_BASE + 0); + dogm128_send_command(DOGM128_ADC_REVERSE); + dogm128_send_command(DOGM128_COM_OUTPUT_SCAN_NORMAL); + dogm128_send_command(DOGM128_DISPLAY_NORMAL); + dogm128_send_command(DOGM128_BIAS_19); + dogm128_send_command(DOGM128_POWER_CONTROL_BASE + 0x07); + dogm128_send_command(DOGM128_BOOSTER_RATIO_SET); + dogm128_send_command(0x00); /* Booster x4 */ + dogm128_send_command(DOGM128_V0_OUTPUT_RESISTOR_BASE + 0x07); + dogm128_send_command(DOGM128_ELECTRONIC_VOLUME_MODE_SET); + dogm128_send_command(0x16); /* Contrast */ + dogm128_send_command(DOGM128_STATIC_INDICATOR_OFF); + dogm128_send_command(0x00); /* Flashing OFF */ + dogm128_send_command(DOGM128_DISPLAY_ON); + + /* End transfer. */ + spi_set_nss_high(DOGM128_SPI); +} + +void dogm128_print_char(u8 data) +{ + u8 i, page, shift, xcoord, ycoord; + + xcoord = dogm128_cursor_x; + ycoord = dogm128_cursor_y; + + /* The display consists of 8 lines a 8 dots each. */ + page = (63 - ycoord) / 8; + shift = (7 -((63 - ycoord) % 8)); /* vertical shift */ + + /* Font is 8x5 so iterate each column of the character. */ + for (i = 0; i <= 5; i++) { + /* Right border reached? */ + if ((xcoord + i) > 127) + return; + dogm128_cursor_x++; + + /* 0xAA = end of character - no dots in this line. */ + if (dogm128_font[data - 0x20][i] == 0xAA) { + dogm128_ram[(page * 128) + xcoord + i] &= + ~(0xFF >> shift); /* Clear area. */ + if ((shift > 0) && (page > 0)) + dogm128_ram[((page - 1) * 128) + xcoord + i] + &= ~(0xFF << (8 - shift)); /* Clear area. */ + return; + } + + /* Lower part. */ + dogm128_ram[(page * 128) + xcoord + i] &= + ~(0xFF >> shift); /* Clear area. */ + dogm128_ram[(page * 128) + xcoord + i] = + (dogm128_font[data - 0x20][i] >> shift); + + /* Higher part if needed. */ + if ((shift > 0) && (page > 0)) { + dogm128_ram[((page - 1) * 128) + xcoord + i] &= + ~(0xFF << (8 - shift)); /* Clear area. */ + dogm128_ram[((page - 1) * 128) + xcoord + i] = + (dogm128_font[data - 0x20][i] << (8 - shift)); + } + } +} + +void dogm128_set_cursor(u8 xcoord, u8 ycoord) +{ + dogm128_cursor_x = xcoord; + dogm128_cursor_y = ycoord; +} + +void dogm128_print_string(char *s) +{ + while (*s != 0) { + dogm128_print_char(*s); + s++; + } +} + +void dogm128_set_dot(u8 xcoord, u8 ycoord) +{ + dogm128_ram[(((63 - ycoord) / 8) * 128) + xcoord] |= + (1 << ((63 - ycoord) % 8)); +} + +void dogm128_clear_dot(u8 xcoord, u8 ycoord) +{ + dogm128_ram[(((63 - ycoord) / 8) * 128) + xcoord] &= + ~(1 << ((63 - ycoord) % 8)); +} + +void dogm128_update_display(void) +{ + u8 page, column; + + /* Tell the display that we want to start. */ + spi_set_nss_low(DOGM128_SPI); + + for (page = 0; page <= 7; page++) { + dogm128_send_command(0xB0 + page); /* Set page. */ + dogm128_send_command(0x10); /* Set column upper address to 0. */ + dogm128_send_command(0x00); /* Set column lower address to 0. */ + + for (column = 0; column <= 127; column++) + dogm128_send_data(dogm128_ram[(page * 128) + column]); + } + + spi_set_nss_high(DOGM128_SPI); +} + +void dogm128_clear(void) +{ + int i; + + for (i = 0; i <= 1023; i++) + dogm128_ram[i] = 0; + + dogm128_update_display(); +} + +/* + * This is a non-monospace font definition (upside down for better handling). + * 0xAA is the end of the character so it's not space efficient in your memory, + * but on your display. + * + * We are starting with " " as the first printable character at 0x20, so we + * have to substract 0x20 later. + * + * Its the only defined to 127/0x7F so if you have German umlauts or other + * special characters from above you have to expand this definition a + * little bit. + */ + +const u8 dogm128_font[96][6] = { + + /* 20 SPACE */ {0x00, 0x00, 0x00, 0xAA, 0xAA, 0xAA}, + /* 21 ! */ {0x5E, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}, + /* 22 " */ {0x66, 0x00, 0x66, 0xAA, 0xAA, 0xAA}, + /* 23 # */ {0x28, 0x7C, 0x28, 0x7C, 0x28, 0xAA}, + /* 24 $ */ {0x24, 0x2A, 0x7F, 0x2A, 0x10, 0xAA}, + /* 25 % */ {0x62, 0x18, 0x46, 0xAA, 0xAA, 0xAA}, + /* 26 & */ {0x30, 0x4C, 0x5A, 0x24, 0x50, 0xAA}, + /* 27 ' */ {0x06, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}, + /* 28 ( */ {0x3E, 0x41, 0xAA, 0xAA, 0xAA, 0xAA}, + /* 29 ) */ {0x41, 0x3E, 0xAA, 0xAA, 0xAA, 0xAA}, + /* 2A * */ {0x28, 0x10, 0x7C, 0x10, 0x28, 0xAA}, + /* 2B + */ {0x10, 0x38, 0x10, 0xAA, 0xAA, 0xAA}, + /* 2C , */ {0xC0, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}, + /* 2D - */ {0x10, 0x10, 0x10, 0xAA, 0xAA, 0xAA}, + /* 2E . */ {0x40, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}, + /* 2F / */ {0x60, 0x18, 0x06, 0xAA, 0xAA, 0xAA}, + + /* 30 0 */ {0x3C, 0x42, 0x42, 0x3C, 0xAA, 0xAA}, + /* 31 1 */ {0x44, 0x7E, 0x40, 0xAA, 0xAA, 0xAA}, + /* 32 2 */ {0x44, 0x62, 0x52, 0x4C, 0xAA, 0xAA}, + /* 33 3 */ {0x4A, 0x4A, 0x34, 0xAA, 0xAA, 0xAA}, + /* 34 4 */ {0x1E, 0x10, 0x78, 0x10, 0xAA, 0xAA}, + /* 35 5 */ {0x4E, 0x4A, 0x32, 0xAA, 0xAA, 0xAA}, + /* 36 6 */ {0x3C, 0x4A, 0x4A, 0x30, 0xAA, 0xAA}, + /* 37 7 */ {0x62, 0x12, 0x0E, 0xAA, 0xAA, 0xAA}, + /* 38 8 */ {0x34, 0x4A, 0x4A, 0x34, 0xAA, 0xAA}, + /* 39 9 */ {0x0C, 0x52, 0x52, 0x3C, 0xAA, 0xAA}, + /* 3A : */ {0x28, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}, + /* 3B ; */ {0xC8, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}, + /* 3C < */ {0x10, 0x28, 0x44, 0xAA, 0xAA, 0xAA}, + /* 3D = */ {0x28, 0x28, 0x28, 0xAA, 0xAA, 0xAA}, + /* 3E > */ {0x44, 0x28, 0x10, 0xAA, 0xAA, 0xAA}, + /* 3F ? */ {0x02, 0x52, 0x0C, 0xAA, 0xAA, 0xAA}, + + /* 40 @ */ {0x3C, 0x42, 0x12, 0x2A, 0x3C, 0xAA}, + /* 41 A */ {0x7C, 0x12, 0x12, 0x7C, 0xAA, 0xAA}, + /* 42 B */ {0x7E, 0x4A, 0x4A, 0x34, 0xAA, 0xAA}, + /* 43 C */ {0x3C, 0x42, 0x42, 0x24, 0xAA, 0xAA}, + /* 44 D */ {0x7E, 0x42, 0x42, 0x3C, 0xAA, 0xAA}, + /* 45 E */ {0x7E, 0x4A, 0x4A, 0xAA, 0xAA, 0xAA}, + /* 46 F */ {0x7E, 0x0A, 0x0A, 0xAA, 0xAA, 0xAA}, + /* 47 G */ {0x3C, 0x42, 0x52, 0x34, 0xAA, 0xAA}, + /* 48 H */ {0x7E, 0x08, 0x08, 0x7E, 0xAA, 0xAA}, + /* 49 I */ {0x42, 0x7E, 0x42, 0xAA, 0xAA, 0xAA}, + /* 4A J */ {0x42, 0x42, 0x3E, 0xAA, 0xAA, 0xAA}, + /* 4B K */ {0x7E, 0x08, 0x14, 0x62, 0xAA, 0xAA}, + /* 4C L */ {0x7E, 0x40, 0x40, 0xAA, 0xAA, 0xAA}, + /* 4D M */ {0x7E, 0x04, 0x08, 0x04, 0x7E, 0xAA}, + /* 4E N */ {0x7E, 0x04, 0x18, 0x20, 0x7E, 0xAA}, + /* 4F O */ {0x3C, 0x42, 0x42, 0x3C, 0xAA, 0xAA}, + + /* 50 P */ {0x7E, 0x12, 0x12, 0x0C, 0xAA, 0xAA}, + /* 51 Q */ {0x3C, 0x42, 0x42, 0xBC, 0xAA, 0xAA}, + /* 52 R */ {0x7E, 0x12, 0x12, 0x6C, 0xAA, 0xAA}, + /* 53 S */ {0x44, 0x4A, 0x4A, 0x30, 0xAA, 0xAA}, + /* 54 T */ {0x02, 0x7E, 0x02, 0xAA, 0xAA, 0xAA}, + /* 55 U */ {0x3E, 0x40, 0x40, 0x3E, 0xAA, 0xAA}, + /* 56 V */ {0x06, 0x18, 0x60, 0x18, 0x06, 0xAA}, + /* 57 W */ {0x3E, 0x40, 0x3E, 0x40, 0x3E, 0xAA}, + /* 58 X */ {0x42, 0x24, 0x18, 0x24, 0x42, 0xAA}, + /* 59 Y */ {0x9E, 0xA0, 0xA0, 0x7E, 0xAA, 0xAA}, + /* 5A Z */ {0x62, 0x52, 0x4A, 0x46, 0xAA, 0xAA}, + /* 5B [ */ {0x7E, 0x42, 0xAA, 0xAA, 0xAA, 0xAA}, + /* 5C \ */ {0x06, 0x18, 0x60, 0xAA, 0xAA, 0xAA}, + /* 5D ] */ {0x42, 0x7E, 0xAA, 0xAA, 0xAA, 0xAA}, + /* 5E ^ */ {0x04, 0x02, 0x04, 0xAA, 0xAA, 0xAA}, + /* 5F _ */ {0x40, 0x40, 0x40, 0xAA, 0xAA, 0xAA}, + + /* 60 ` */ {0x02, 0x04, 0xAA, 0xAA, 0xAA, 0xAA}, + /* 61 a */ {0x20, 0x54, 0x54, 0x78, 0xAA, 0xAA}, + /* 62 b */ {0x7E, 0x44, 0x44, 0x38, 0xAA, 0xAA}, + /* 63 c */ {0x38, 0x44, 0x44, 0x28, 0xAA, 0xAA}, + /* 64 d */ {0x38, 0x44, 0x44, 0x7E, 0xAA, 0xAA}, + /* 65 e */ {0x38, 0x54, 0x54, 0x58, 0xAA, 0xAA}, + /* 66 f */ {0x7C, 0x0A, 0xAA, 0xAA, 0xAA, 0xAA}, + /* 67 g */ {0x98, 0xA4, 0xA4, 0x7C, 0xAA, 0xAA}, + /* 68 h */ {0x7E, 0x04, 0x04, 0x78, 0xAA, 0xAA}, + /* 69 i */ {0x7A, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}, + /* 6A j */ {0x40, 0x3A, 0xAA, 0xAA, 0xAA, 0xAA}, + /* 6B k */ {0x7E, 0x10, 0x28, 0x44, 0xAA, 0xAA}, + /* 6C l */ {0x7E, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}, + /* 6D m */ {0x7C, 0x04, 0x78, 0x04, 0x78, 0xAA}, + /* 6E n */ {0x7C, 0x04, 0x04, 0x78, 0xAA, 0xAA}, + /* 6F o */ {0x38, 0x44, 0x44, 0x38, 0xAA, 0xAA}, + + /* 70 p */ {0xFC, 0x24, 0x24, 0x18, 0xAA, 0xAA}, + /* 71 q */ {0x18, 0x24, 0x24, 0xFC, 0xAA, 0xAA}, + /* 72 r */ {0x78, 0x04, 0xAA, 0xAA, 0xAA, 0xAA}, + /* 73 s */ {0x48, 0x54, 0x54, 0x20, 0xAA, 0xAA}, + /* 74 t */ {0x04, 0x3E, 0x44, 0xAA, 0xAA, 0xAA}, + /* 75 u */ {0x3C, 0x40, 0x40, 0x3C, 0xAA, 0xAA}, + /* 76 v */ {0x0C, 0x30, 0x40, 0x30, 0x0C, 0xAA}, + /* 77 w */ {0x3C, 0x40, 0x3C, 0x40, 0x3C, 0xAA}, + /* 78 x */ {0x44, 0x28, 0x10, 0x28, 0x44, 0xAA}, + /* 79 y */ {0x1C, 0xA0, 0xA0, 0x7C, 0xAA, 0xAA}, + /* 7A z */ {0x64, 0x54, 0x4C, 0xAA, 0xAA, 0xAA}, + /* 7B { */ {0x08, 0x36, 0x41, 0xAA, 0xAA, 0xAA}, + /* 7C | */ {0x7E, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}, + /* 7D } */ {0x41, 0x36, 0x08, 0xAA, 0xAA, 0xAA}, + /* 7E ~ */ {0x20, 0x10, 0x20, 0x10, 0xAA, 0xAA}, + /* 7F DEL */ {0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}, +}; diff --git a/examples/stm32/f1/other/dogm128/dogm128.h b/examples/stm32/f1/other/dogm128/dogm128.h new file mode 100644 index 0000000..33ab129 --- /dev/null +++ b/examples/stm32/f1/other/dogm128/dogm128.h @@ -0,0 +1,88 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#ifndef DOGM128_H +#define DOGM128_H + +#include +#include +#include + +/* + * PB10 GPIO - ~RESET + * PB12 SPI2_NSS - ~CS1 + * PB13 SPI2_SCK - SCL + * PB14 SPI2_MISO - A0 + * PB15 SPI2_MOSI - SI + */ + +#define DOGM128_SPI SPI2 +#define DOGM128_RESET_PIN GPIO10 +#define DOGM128_RESET_PORT GPIOB +#define DOGM128_A0_PIN GPIO14 +#define DOGM128_A0_PORT GPIOB + +/* DOGM128 display commands */ +#define DOGM128_PAGE_BASE 0xB0 +#define DOGM128_PAGE0 0xB0 +#define DOGM128_PAGE1 0xB1 +#define DOGM128_PAGE2 0xB2 +#define DOGM128_PAGE3 0xB3 +#define DOGM128_PAGE4 0xB4 +#define DOGM128_PAGE5 0xB5 +#define DOGM128_PAGE6 0xB6 +#define DOGM128_PAGE7 0xB7 +#define DOGM128_DISPLAY_ON 0xAF +#define DOGM128_DISPLAY_OFF 0xAE +#define DOGM128_DISPLAY_START_ADDRESS_BASE 0x40 +#define DOGM128_ADC_NORMAL 0xA0 +#define DOGM128_ADC_REVERSE 0xA1 +#define DOGM128_DISPLAY_NORMAL 0xA6 +#define DOGM128_DISPLAY_REVERSE 0xA7 +#define DOGM128_ALL_POINTS_ON 0xA5 +#define DOGM128_ALL_POINTS_OFF 0xA4 +#define DOGM128_BIAS_19 0xA2 +#define DOGM128_BIAS_17 0xA3 +#define DOGM128_INTERNAL_RESET 0xE2 +#define DOGM128_COM_OUTPUT_SCAN_NORMAL 0xC0 +#define DOGM128_COM_OUTPUT_SCAN_REVERSE 0xC8 +#define DOGM128_POWER_CONTROL_BASE 0x28 +#define DOGM128_V0_OUTPUT_RESISTOR_BASE 0x20 +#define DOGM128_ELECTRONIC_VOLUME_MODE_SET 0x81 +#define DOGM128_STATIC_INDICATOR_OFF 0xAC +#define DOGM128_STATIC_INDICATOR_ON 0xAD +#define DOGM128_BOOSTER_RATIO_SET 0xF8 + +extern const u8 dogm128_font[96][6]; +extern u8 dogm128_ram[1024]; +extern u8 dogm128_cursor_x; +extern u8 dogm128_cursor_y; + +void dogm128_send_command(u8 command); +void dogm128_set_cursor(u8 xcoord, u8 ycoord); +void dogm128_print_char(u8 data); +void dogm128_print_string(char *s); +void dogm128_set_dot(u8 xcoord, u8 ycoord); +void dogm128_clear_dot(u8 xcoord, u8 ycoord); +void dogm128_send_data(u8 data); +void dogm128_init(void); +void dogm128_update_display(void); +void dogm128_clear(void); + +#endif diff --git a/examples/stm32/f1/other/dogm128/main.c b/examples/stm32/f1/other/dogm128/main.c new file mode 100644 index 0000000..e9742e9 --- /dev/null +++ b/examples/stm32/f1/other/dogm128/main.c @@ -0,0 +1,113 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include "./dogm128.h" + +static void gpio_setup(void) +{ + /* Enable GPIOB clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); + + /* Set GPIO6/7 (in GPIO port B) to 'output push-pull' for the LEDs. */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO6); + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO7); + + /* A0 of DOGM128 */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO14); + /* Reset of DOGM128 */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO10); + + /* DOGM128/SPI2 clock and MOSI and NSS(CS1) */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO12); + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO13); + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO15); +} + +static void spi_setup(void) +{ + /* The DOGM128 display is connected to SPI2, so initialise it. */ + + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_SPI2EN); + + spi_set_unidirectional_mode(DOGM128_SPI); /* We want to send only. */ + spi_disable_crc(DOGM128_SPI); /* No CRC for this slave. */ + spi_set_dff_8bit(DOGM128_SPI); /* 8-bit dataword-length */ + spi_set_full_duplex_mode(DOGM128_SPI); /* Not receive-only */ + /* We want to handle the CS signal in software. */ + spi_enable_software_slave_management(DOGM128_SPI); + spi_set_nss_high(DOGM128_SPI); + /* PCLOCK/256 as clock. */ + spi_set_baudrate_prescaler(DOGM128_SPI, SPI_CR1_BR_FPCLK_DIV_256); + /* We want to control everything and generate the clock -> master. */ + spi_set_master_mode(DOGM128_SPI); + spi_set_clock_polarity_1(DOGM128_SPI); /* SCK idle state high. */ + /* Bit is taken on the second (rising edge) of SCK. */ + spi_set_clock_phase_1(DOGM128_SPI); + spi_enable_ss_output(DOGM128_SPI); + spi_enable(DOGM128_SPI); +} + +int main(void) +{ + rcc_clock_setup_in_hse_16mhz_out_72mhz(); + gpio_setup(); + spi_setup(); + + gpio_clear(GPIOB, GPIO7); /* LED1 on */ + gpio_set(GPIOB, GPIO6); /* LED2 off */ + + dogm128_init(); + dogm128_clear(); + + dogm128_set_cursor(0, 56); + dogm128_print_string("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); + dogm128_set_cursor(0, 48); + dogm128_print_string("abcdefghijklmnopqrstuvwxyz"); + dogm128_set_cursor(0, 40); + dogm128_print_string(" !#$%&'()*+,-./0123456789"); + dogm128_set_cursor(0, 32); + dogm128_print_string(":;<=>?@[\\]^_`{|}~"); + + dogm128_set_dot(10, 10); + dogm128_set_dot(20, 10); + dogm128_set_dot(30, 10); + dogm128_set_dot(40, 10); + dogm128_set_dot(50, 10); + + dogm128_update_display(); + + gpio_set(GPIOB, GPIO7); /* LED1 off */ + while (1); /* Halt. */ + + return 0; +} diff --git a/examples/stm32/f1/other/dogm128/main.ld b/examples/stm32/f1/other/dogm128/main.ld new file mode 100644 index 0000000..9c6c11a --- /dev/null +++ b/examples/stm32/f1/other/dogm128/main.ld @@ -0,0 +1,31 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* Linker script for an STM32F103RBT6 board (128K flash, 20K RAM). */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_stm32f1.ld + diff --git a/examples/stm32/f1/other/i2c_stts75_sensor/Makefile b/examples/stm32/f1/other/i2c_stts75_sensor/Makefile new file mode 100644 index 0000000..d6bd621 --- /dev/null +++ b/examples/stm32/f1/other/i2c_stts75_sensor/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = i2c_stts75_sensor + +OBJS = stts75.o + +include ../../Makefile.include + diff --git a/examples/stm32/f1/other/i2c_stts75_sensor/README b/examples/stm32/f1/other/i2c_stts75_sensor/README new file mode 100644 index 0000000..91e3ff6 --- /dev/null +++ b/examples/stm32/f1/other/i2c_stts75_sensor/README @@ -0,0 +1,13 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example program sends some characters on USART1. +Afterwards it connects to an STTS75 sensor (ST LM75 compatible) +at adress A0/1/2=0 and sets reverse polarity, 26 degree Tos and Thyst. + +It reads out the temperature and submits the temperature over USART1 in +binary format (ASCII 0/1). + +The terminal settings for the receiving device/PC are 115200 8n1. + diff --git a/examples/stm32/f1/other/i2c_stts75_sensor/i2c_stts75_sensor.c b/examples/stm32/f1/other/i2c_stts75_sensor/i2c_stts75_sensor.c new file mode 100644 index 0000000..77f15ae --- /dev/null +++ b/examples/stm32/f1/other/i2c_stts75_sensor/i2c_stts75_sensor.c @@ -0,0 +1,146 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include "stts75.h" + +static void usart_setup(void) +{ + /* Enable clocks for GPIO port A (for GPIO_USART1_TX) and USART1. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_USART1EN); + + /* Setup GPIO pin GPIO_USART1_TX/GPIO9 on GPIO port A for transmit. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX); + + /* Setup UART parameters. */ + usart_set_baudrate(USART1, 115200); + usart_set_databits(USART1, 8); + usart_set_stopbits(USART1, USART_STOPBITS_1); + usart_set_mode(USART1, USART_MODE_TX_RX); + usart_set_parity(USART1, USART_PARITY_NONE); + usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART1); +} + +static void gpio_setup(void) +{ + /* Enable GPIOB clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); + + /* Set GPIO6/7 (in GPIO port B) to 'output push-pull' for the LEDs. */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO6); + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO7); +} + +static void i2c_setup(void) +{ + /* Enable clocks for I2C2 and AFIO. */ + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_I2C2EN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); + + /* Set alternate functions for the SCL and SDA pins of I2C2. */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, + GPIO_I2C2_SCL | GPIO_I2C2_SDA); + + /* Disable the I2C before changing any configuration. */ + i2c_peripheral_disable(I2C2); + + /* APB1 is running at 36MHz. */ + i2c_set_clock_frequency(I2C2, I2C_CR2_FREQ_36MHZ); + + /* 400KHz - I2C Fast Mode */ + i2c_set_fast_mode(I2C2); + + /* + * fclock for I2C is 36MHz APB2 -> cycle time 28ns, low time at 400kHz + * incl trise -> Thigh = 1600ns; CCR = tlow/tcycle = 0x1C,9; + * Datasheet suggests 0x1e. + */ + i2c_set_ccr(I2C2, 0x1e); + + /* + * fclock for I2C is 36MHz -> cycle time 28ns, rise time for + * 400kHz => 300ns and 100kHz => 1000ns; 300ns/28ns = 10; + * Incremented by 1 -> 11. + */ + i2c_set_trise(I2C2, 0x0b); + + /* + * This is our slave address - needed only if we want to receive from + * other masters. + */ + i2c_set_own_7bit_slave_address(I2C2, 0x32); + + /* If everything is configured -> enable the peripheral. */ + i2c_peripheral_enable(I2C2); +} + +int main(void) +{ + int i = 0; + u16 temperature; + + rcc_clock_setup_in_hse_16mhz_out_72mhz(); + gpio_setup(); + usart_setup(); + i2c_setup(); + + gpio_clear(GPIOB, GPIO7); /* LED1 on */ + gpio_set(GPIOB, GPIO6); /* LED2 off */ + + /* Send a message on USART1. */ + usart_send(USART1, 's'); + usart_send(USART1, 't'); + usart_send(USART1, 'm'); + usart_send(USART1, '\r'); + usart_send(USART1, '\n'); + + stts75_write_config(I2C2, STTS75_SENSOR0); + stts75_write_temp_os(I2C2, STTS75_SENSOR0, 0x1a00); /* 26 degrees */ + stts75_write_temp_hyst(I2C2, STTS75_SENSOR0, 0x1a00); + temperature = stts75_read_temperature(I2C2, STTS75_SENSOR0); + + /* Send the temperature as binary over USART1. */ + for (i = 15; i >= 0; i--) { + if (temperature & (1 << i)) + usart_send(USART1, '1'); + else + usart_send(USART1, '0'); + } + + usart_send(USART1, '\r'); + usart_send(USART1, '\n'); + + gpio_clear(GPIOB, GPIO6); /* LED2 on */ + + while (1); /* Halt. */ + + return 0; +} diff --git a/examples/stm32/f1/other/i2c_stts75_sensor/i2c_stts75_sensor.ld b/examples/stm32/f1/other/i2c_stts75_sensor/i2c_stts75_sensor.ld new file mode 100644 index 0000000..9c6c11a --- /dev/null +++ b/examples/stm32/f1/other/i2c_stts75_sensor/i2c_stts75_sensor.ld @@ -0,0 +1,31 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* Linker script for an STM32F103RBT6 board (128K flash, 20K RAM). */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_stm32f1.ld + diff --git a/examples/stm32/f1/other/i2c_stts75_sensor/stts75.c b/examples/stm32/f1/other/i2c_stts75_sensor/stts75.c new file mode 100644 index 0000000..e211f29 --- /dev/null +++ b/examples/stm32/f1/other/i2c_stts75_sensor/stts75.c @@ -0,0 +1,189 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include "stts75.h" + +void stts75_write_config(u32 i2c, u8 sensor) +{ + u32 reg32 __attribute__((unused)); + + /* Send START condition. */ + i2c_send_start(i2c); + + /* Waiting for START is send and switched to master mode. */ + while (!((I2C_SR1(i2c) & I2C_SR1_SB) + & (I2C_SR2(i2c) & (I2C_SR2_MSL | I2C_SR2_BUSY)))); + + /* Send destination address. */ + i2c_send_7bit_address(i2c, sensor, I2C_WRITE); + + /* Waiting for address is transferred. */ + while (!(I2C_SR1(i2c) & I2C_SR1_ADDR)); + + /* Cleaning ADDR condition sequence. */ + reg32 = I2C_SR2(i2c); + + /* Sending the data. */ + i2c_send_data(i2c, 0x1); /* stts75 config register */ + while (!(I2C_SR1(i2c) & I2C_SR1_BTF)); /* Await ByteTransferedFlag. */ + /* Polarity reverse - LED glows if temp is below Tos/Thyst. */ + i2c_send_data(i2c, 0x4); + while (!(I2C_SR1(i2c) & (I2C_SR1_BTF | I2C_SR1_TxE))); + + /* Send STOP condition. */ + i2c_send_stop(i2c); +} + +void stts75_write_temp_os(u32 i2c, u8 sensor, u16 temp_os) +{ + u32 reg32 __attribute__((unused)); + + /* Send START condition. */ + i2c_send_start(i2c); + + /* Waiting for START is send and switched to master mode. */ + while (!((I2C_SR1(i2c) & I2C_SR1_SB) + & (I2C_SR2(i2c) & (I2C_SR2_MSL | I2C_SR2_BUSY)))); + + /* Send destination address. */ + i2c_send_7bit_address(i2c, sensor, I2C_WRITE); + + /* Waiting for address is transferred. */ + while (!(I2C_SR1(i2c) & I2C_SR1_ADDR)); + + /* Cleaning ADDR condition sequence. */ + reg32 = I2C_SR2(i2c); + + /* Sending the data. */ + i2c_send_data(i2c, 0x3); /* OvertemperatureShutdown register */ + while (!(I2C_SR1(i2c) & I2C_SR1_BTF)); + i2c_send_data(i2c, (u8)(temp_os >> 8)); /* MSB */ + while (!(I2C_SR1(i2c) & I2C_SR1_BTF)); + i2c_send_data(i2c, (u8)(temp_os & 0xff00)); /* LSB */ + /* After the last byte we have to wait for TxE too. */ + while (!(I2C_SR1(i2c) & (I2C_SR1_BTF | I2C_SR1_TxE))); + + /* Send STOP condition. */ + i2c_send_stop(i2c); +} + +void stts75_write_temp_hyst(u32 i2c, u8 sensor, u16 temp_hyst) +{ + u32 reg32 __attribute__((unused)); + + /* Send START condition. */ + i2c_send_start(i2c); + + /* Waiting for START is send and therefore switched to master mode. */ + while (!((I2C_SR1(i2c) & I2C_SR1_SB) + & (I2C_SR2(i2c) & (I2C_SR2_MSL | I2C_SR2_BUSY)))); + + /* Say to what address we want to talk to. */ + i2c_send_7bit_address(i2c, sensor, I2C_WRITE); + + /* Waiting for address is transferred. */ + while (!(I2C_SR1(i2c) & I2C_SR1_ADDR)); + + /* Cleaning ADDR condition sequence. */ + reg32 = I2C_SR2(i2c); + + /* Sending the data. */ + i2c_send_data(i2c, 0x2); /* TemperatureHysteresis register */ + while (!(I2C_SR1(i2c) & I2C_SR1_BTF)); + i2c_send_data(i2c, (u8)(temp_hyst >> 8)); /* MSB */ + while (!(I2C_SR1(i2c) & I2C_SR1_BTF)); + i2c_send_data(i2c, (u8)(temp_hyst & 0xff00)); /* LSB */ + /* After the last byte we have to wait for TxE too. */ + while (!(I2C_SR1(i2c) & (I2C_SR1_BTF | I2C_SR1_TxE))); + + /* Send STOP condition. */ + i2c_send_stop(i2c); +} + +u16 stts75_read_temperature(u32 i2c, u8 sensor) +{ + u32 reg32 __attribute__((unused)); + u16 temperature; + + /* Send START condition. */ + i2c_send_start(i2c); + + /* Waiting for START is send and switched to master mode. */ + while (!((I2C_SR1(i2c) & I2C_SR1_SB) + & (I2C_SR2(i2c) & (I2C_SR2_MSL | I2C_SR2_BUSY)))); + + /* Say to what address we want to talk to. */ + /* Yes, WRITE is correct - for selecting register in STTS75. */ + i2c_send_7bit_address(i2c, sensor, I2C_WRITE); + + /* Waiting for address is transferred. */ + while (!(I2C_SR1(i2c) & I2C_SR1_ADDR)); + + /* Cleaning ADDR condition sequence. */ + reg32 = I2C_SR2(i2c); + + i2c_send_data(i2c, 0x0); /* temperature register */ + while (!(I2C_SR1(i2c) & (I2C_SR1_BTF | I2C_SR1_TxE))); + + /* + * Now we transferred that we want to ACCESS the temperature register. + * Now we send another START condition (repeated START) and then + * transfer the destination but with flag READ. + */ + + /* Send START condition. */ + i2c_send_start(i2c); + + /* Waiting for START is send and switched to master mode. */ + while (!((I2C_SR1(i2c) & I2C_SR1_SB) + & (I2C_SR2(i2c) & (I2C_SR2_MSL | I2C_SR2_BUSY)))); + + /* Say to what address we want to talk to. */ + i2c_send_7bit_address(i2c, sensor, I2C_READ); + + /* 2-byte receive is a special case. See datasheet POS bit. */ + I2C_CR1(i2c) |= (I2C_CR1_POS | I2C_CR1_ACK); + + /* Waiting for address is transferred. */ + while (!(I2C_SR1(i2c) & I2C_SR1_ADDR)); + + /* Cleaning ADDR condition sequence. */ + reg32 = I2C_SR2(i2c); + + /* Cleaning I2C_SR1_ACK. */ + I2C_CR1(i2c) &= ~I2C_CR1_ACK; + + /* Now the slave should begin to send us the first byte. Await BTF. */ + while (!(I2C_SR1(i2c) & I2C_SR1_BTF)); + temperature = (u16)(I2C_DR(i2c) << 8); /* MSB */ + + /* + * Yes they mean it: we have to generate the STOP condition before + * saving the 1st byte. + */ + I2C_CR1(i2c) |= I2C_CR1_STOP; + + temperature |= I2C_DR(i2c); /* LSB */ + + /* Original state. */ + I2C_CR1(i2c) &= ~I2C_CR1_POS; + + return temperature; +} diff --git a/examples/stm32/f1/other/i2c_stts75_sensor/stts75.h b/examples/stm32/f1/other/i2c_stts75_sensor/stts75.h new file mode 100644 index 0000000..ed16dba --- /dev/null +++ b/examples/stm32/f1/other/i2c_stts75_sensor/stts75.h @@ -0,0 +1,39 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#ifndef STTS75_H +#define STTS75_H + +#include + +#define STTS75_SENSOR0 0x48 +#define STTS75_SENSOR1 0x49 +#define STTS75_SENSOR2 0x4a +#define STTS75_SENSOR3 0x4b +#define STTS75_SENSOR4 0x4c +#define STTS75_SENSOR5 0x4d +#define STTS75_SENSOR6 0x4e +#define STTS75_SENSOR7 0x4f + +void stts75_write_config(u32 i2c, u8 sensor); +void stts75_write_temp_os(u32 i2c, u8 sensor, u16 temp_os); +void stts75_write_temp_hyst(u32 i2c, u8 sensor, u16 temp_hyst); +u16 stts75_read_temperature(u32 i2c, u8 sensor); + +#endif diff --git a/examples/stm32/f1/other/rtc/Makefile b/examples/stm32/f1/other/rtc/Makefile new file mode 100644 index 0000000..859ef29 --- /dev/null +++ b/examples/stm32/f1/other/rtc/Makefile @@ -0,0 +1,23 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = rtc + +include ../../Makefile.include + diff --git a/examples/stm32/f1/other/rtc/README b/examples/stm32/f1/other/rtc/README new file mode 100644 index 0000000..f1e3975 --- /dev/null +++ b/examples/stm32/f1/other/rtc/README @@ -0,0 +1,6 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This is a small RTC example project. + diff --git a/examples/stm32/f1/other/rtc/rtc.c b/examples/stm32/f1/other/rtc/rtc.c new file mode 100644 index 0000000..5db5582 --- /dev/null +++ b/examples/stm32/f1/other/rtc/rtc.c @@ -0,0 +1,116 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Lord James + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include + +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + + /* Enable GPIOC clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + /* Enable clocks for GPIO port A (for GPIO_USART1_TX) and USART1. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_USART1EN); +} + +static void usart_setup(void) +{ + /* Setup GPIO pin GPIO_USART1_TX/GPIO9 on GPIO port A for transmit. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX); + + /* Setup UART parameters. */ + usart_set_baudrate(USART1, 38400); + usart_set_databits(USART1, 8); + usart_set_stopbits(USART1, USART_STOPBITS_1); + usart_set_mode(USART1, USART_MODE_TX); + usart_set_parity(USART1, USART_PARITY_NONE); + usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART1); +} + +static void gpio_setup(void) +{ + /* Set GPIO12 (in GPIO port C) to 'output push-pull'. */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO12); +} + +static void nvic_setup(void) +{ + /* Without this the RTC interrupt routine will never be called. */ + nvic_enable_irq(NVIC_RTC_IRQ); + nvic_set_priority(NVIC_RTC_IRQ, 1); +} + +void rtc_isr(void) +{ + volatile u32 j = 0, c = 0; + + /* The interrupt flag isn't cleared by hardware, we have to do it. */ + rtc_clear_flag(RTC_SEC); + + /* Visual output. */ + gpio_toggle(GPIOC, GPIO12); + + c = rtc_get_counter_val(); + + /* Display the current counter value in binary via USART1. */ + for (j = 0; j < 32; j++) { + if ((c & (0x80000000 >> j)) != 0) { + usart_send(USART1, '1'); + } else { + usart_send(USART1, '0'); + } + } + usart_send(USART1, '\n'); +} + +int main(void) +{ + clock_setup(); + gpio_setup(); + usart_setup(); + + /* + * If the RTC is pre-configured just allow access, don't reconfigure. + * Otherwise enable it with the LSE as clock source and 0x7fff as + * prescale value. + */ + rtc_auto_awake(LSE, 0x7fff); + + /* Setup the RTC interrupt. */ + nvic_setup(); + + /* Enable the RTC interrupt to occur off the SEC flag. */ + rtc_interrupt_enable(RTC_SEC); + + while (1); + + return 0; +} diff --git a/examples/stm32/f1/other/rtc/rtc.ld b/examples/stm32/f1/other/rtc/rtc.ld new file mode 100644 index 0000000..e15beca --- /dev/null +++ b/examples/stm32/f1/other/rtc/rtc.ld @@ -0,0 +1,31 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* Linker script for Olimex STM32-H103 (STM32F103RBT6, 128K flash, 20K RAM). */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_stm32f1.ld + diff --git a/examples/stm32/f1/other/systick/Makefile b/examples/stm32/f1/other/systick/Makefile new file mode 100644 index 0000000..96bdcc6 --- /dev/null +++ b/examples/stm32/f1/other/systick/Makefile @@ -0,0 +1,23 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = systick + +include ../../Makefile.include + diff --git a/examples/stm32/f1/other/systick/README b/examples/stm32/f1/other/systick/README new file mode 100644 index 0000000..affb4b8 --- /dev/null +++ b/examples/stm32/f1/other/systick/README @@ -0,0 +1,7 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example program blinks a LED on PortB Pin 6. 1 second on / 1 second off. +Blinking is done via the systick timer interrupt. + diff --git a/examples/stm32/f1/other/systick/systick.c b/examples/stm32/f1/other/systick/systick.c new file mode 100644 index 0000000..aa91238 --- /dev/null +++ b/examples/stm32/f1/other/systick/systick.c @@ -0,0 +1,76 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include + +u32 temp32; + +static void gpio_setup(void) +{ + /* Enable GPIOB clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); + + /* Set GPIO6/7 (in GPIO port B) to 'output push-pull' for the LEDs. */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO6); + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO7); +} + +void sys_tick_handler(void) +{ + temp32++; + + /* We call this handler every 1ms so 1000ms = 1s on/off. */ + if (temp32 == 1000) { + gpio_toggle(GPIOB, GPIO6); /* LED2 on/off */ + temp32 = 0; + } +} + +int main(void) +{ + rcc_clock_setup_in_hse_16mhz_out_72mhz(); + gpio_setup(); + + gpio_clear(GPIOB, GPIO7); /* LED1 on */ + gpio_set(GPIOB, GPIO6); /* LED2 off */ + + temp32 = 0; + + /* 72MHz / 8 => 9000000 counts per second */ + systick_set_clocksource(STK_CTRL_CLKSOURCE_AHB_DIV8); + + /* 9000000/9000 = 1000 overflows per second - every 1ms one interrupt */ + /* SysTick interrupt every N clock pulses: set reload to N-1 */ + systick_set_reload(8999); + + systick_interrupt_enable(); + + /* Start counting. */ + systick_counter_enable(); + + while (1); /* Halt. */ + + return 0; +} diff --git a/examples/stm32/f1/other/systick/systick.ld b/examples/stm32/f1/other/systick/systick.ld new file mode 100644 index 0000000..9c6c11a --- /dev/null +++ b/examples/stm32/f1/other/systick/systick.ld @@ -0,0 +1,31 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* Linker script for an STM32F103RBT6 board (128K flash, 20K RAM). */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_stm32f1.ld + diff --git a/examples/stm32/f1/other/timer_interrupt/Makefile b/examples/stm32/f1/other/timer_interrupt/Makefile new file mode 100644 index 0000000..65f5cfc --- /dev/null +++ b/examples/stm32/f1/other/timer_interrupt/Makefile @@ -0,0 +1,23 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = timer + +include ../../Makefile.include + diff --git a/examples/stm32/f1/other/timer_interrupt/README b/examples/stm32/f1/other/timer_interrupt/README new file mode 100644 index 0000000..e6a22c4 --- /dev/null +++ b/examples/stm32/f1/other/timer_interrupt/README @@ -0,0 +1,7 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example program blinks a LED on PortB Pin 6. 1 second on / 1 second off. +Blinking is made only with the timer interrupt of the TIM2 timer. + diff --git a/examples/stm32/f1/other/timer_interrupt/timer.c b/examples/stm32/f1/other/timer_interrupt/timer.c new file mode 100644 index 0000000..d4df023 --- /dev/null +++ b/examples/stm32/f1/other/timer_interrupt/timer.c @@ -0,0 +1,85 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include + +static void gpio_setup(void) +{ + /* Enable GPIOB clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); + + /* Set GPIO6/7 (in GPIO port B) to 'output push-pull' for the LEDs. */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO6); + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO7); +} + +static void nvic_setup(void) +{ + /* Without this the timer interrupt routine will never be called. */ + nvic_enable_irq(NVIC_TIM2_IRQ); + nvic_set_priority(NVIC_TIM2_IRQ, 1); +} + +void tim2_isr(void) +{ + gpio_toggle(GPIOB, GPIO6); /* LED2 on/off. */ + TIM_SR(TIM2) &= ~TIM_SR_UIF; /* Clear interrrupt flag. */ +} + +int main(void) +{ + rcc_clock_setup_in_hse_16mhz_out_72mhz(); + gpio_setup(); + nvic_setup(); + + gpio_clear(GPIOB, GPIO7); /* LED1 on */ + gpio_set(GPIOB, GPIO6); /* LED2 off */ + + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_TIM2EN); + + /* + * The goal is to let the LED2 glow for a second and then be + * off for a second. + */ + + /* Set timer start value. */ + TIM_CNT(TIM2) = 1; + + /* Set timer prescaler. 72MHz/1440 => 50000 counts per second. */ + TIM_PSC(TIM2) = 1440; + + /* End timer value. If this is reached an interrupt is generated. */ + TIM_ARR(TIM2) = 50000; + + /* Update interrupt enable. */ + TIM_DIER(TIM2) |= TIM_DIER_UIE; + + /* Start timer. */ + TIM_CR1(TIM2) |= TIM_CR1_CEN; + + while (1); /* Halt. */ + + return 0; +} diff --git a/examples/stm32/f1/other/timer_interrupt/timer.ld b/examples/stm32/f1/other/timer_interrupt/timer.ld new file mode 100644 index 0000000..9c6c11a --- /dev/null +++ b/examples/stm32/f1/other/timer_interrupt/timer.ld @@ -0,0 +1,31 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* Linker script for an STM32F103RBT6 board (128K flash, 20K RAM). */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_stm32f1.ld + diff --git a/examples/stm32/f1/other/usb_cdcacm/Makefile b/examples/stm32/f1/other/usb_cdcacm/Makefile new file mode 100644 index 0000000..362d9e1 --- /dev/null +++ b/examples/stm32/f1/other/usb_cdcacm/Makefile @@ -0,0 +1,23 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = cdcacm + +include ../../Makefile.include + diff --git a/examples/stm32/f1/other/usb_cdcacm/README b/examples/stm32/f1/other/usb_cdcacm/README new file mode 100644 index 0000000..2f1ee4b --- /dev/null +++ b/examples/stm32/f1/other/usb_cdcacm/README @@ -0,0 +1,7 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example implements a USB CDC-ACM device (aka Virtual Serial Port) +to demonstrate the use of the USB device stack. + diff --git a/examples/stm32/f1/other/usb_cdcacm/cdcacm.c b/examples/stm32/f1/other/usb_cdcacm/cdcacm.c new file mode 100644 index 0000000..c8e0933 --- /dev/null +++ b/examples/stm32/f1/other/usb_cdcacm/cdcacm.c @@ -0,0 +1,253 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Gareth McMullin + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include + +static const struct usb_device_descriptor dev = { + .bLength = USB_DT_DEVICE_SIZE, + .bDescriptorType = USB_DT_DEVICE, + .bcdUSB = 0x0200, + .bDeviceClass = USB_CLASS_CDC, + .bDeviceSubClass = 0, + .bDeviceProtocol = 0, + .bMaxPacketSize0 = 64, + .idVendor = 0x0483, + .idProduct = 0x5740, + .bcdDevice = 0x0200, + .iManufacturer = 1, + .iProduct = 2, + .iSerialNumber = 3, + .bNumConfigurations = 1, +}; + +/* + * This notification endpoint isn't implemented. According to CDC spec its + * optional, but its absence causes a NULL pointer dereference in Linux + * cdc_acm driver. + */ +static const struct usb_endpoint_descriptor comm_endp[] = {{ + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x83, + .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, + .wMaxPacketSize = 16, + .bInterval = 255, +}}; + +static const struct usb_endpoint_descriptor data_endp[] = {{ + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x01, + .bmAttributes = USB_ENDPOINT_ATTR_BULK, + .wMaxPacketSize = 64, + .bInterval = 1, +}, { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x82, + .bmAttributes = USB_ENDPOINT_ATTR_BULK, + .wMaxPacketSize = 64, + .bInterval = 1, +}}; + +static const struct { + struct usb_cdc_header_descriptor header; + struct usb_cdc_call_management_descriptor call_mgmt; + struct usb_cdc_acm_descriptor acm; + struct usb_cdc_union_descriptor cdc_union; +} __attribute__((packed)) cdcacm_functional_descriptors = { + .header = { + .bFunctionLength = sizeof(struct usb_cdc_header_descriptor), + .bDescriptorType = CS_INTERFACE, + .bDescriptorSubtype = USB_CDC_TYPE_HEADER, + .bcdCDC = 0x0110, + }, + .call_mgmt = { + .bFunctionLength = + sizeof(struct usb_cdc_call_management_descriptor), + .bDescriptorType = CS_INTERFACE, + .bDescriptorSubtype = USB_CDC_TYPE_CALL_MANAGEMENT, + .bmCapabilities = 0, + .bDataInterface = 1, + }, + .acm = { + .bFunctionLength = sizeof(struct usb_cdc_acm_descriptor), + .bDescriptorType = CS_INTERFACE, + .bDescriptorSubtype = USB_CDC_TYPE_ACM, + .bmCapabilities = 0, + }, + .cdc_union = { + .bFunctionLength = sizeof(struct usb_cdc_union_descriptor), + .bDescriptorType = CS_INTERFACE, + .bDescriptorSubtype = USB_CDC_TYPE_UNION, + .bControlInterface = 0, + .bSubordinateInterface0 = 1, + } +}; + +static const struct usb_interface_descriptor comm_iface[] = {{ + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 0, + .bAlternateSetting = 0, + .bNumEndpoints = 1, + .bInterfaceClass = USB_CLASS_CDC, + .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM, + .bInterfaceProtocol = USB_CDC_PROTOCOL_AT, + .iInterface = 0, + + .endpoint = comm_endp, + + .extra = &cdcacm_functional_descriptors, + .extralen = sizeof(cdcacm_functional_descriptors) +}}; + +static const struct usb_interface_descriptor data_iface[] = {{ + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 1, + .bAlternateSetting = 0, + .bNumEndpoints = 2, + .bInterfaceClass = USB_CLASS_DATA, + .bInterfaceSubClass = 0, + .bInterfaceProtocol = 0, + .iInterface = 0, + + .endpoint = data_endp, +}}; + +static const struct usb_interface ifaces[] = {{ + .num_altsetting = 1, + .altsetting = comm_iface, +}, { + .num_altsetting = 1, + .altsetting = data_iface, +}}; + +static const struct usb_config_descriptor config = { + .bLength = USB_DT_CONFIGURATION_SIZE, + .bDescriptorType = USB_DT_CONFIGURATION, + .wTotalLength = 0, + .bNumInterfaces = 2, + .bConfigurationValue = 1, + .iConfiguration = 0, + .bmAttributes = 0x80, + .bMaxPower = 0x32, + + .interface = ifaces, +}; + +static const char *usb_strings[] = { + "Black Sphere Technologies", + "CDC-ACM Demo", + "DEMO", +}; + +static int cdcacm_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, u8 **buf, + u16 *len, void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req)) +{ + (void)complete; + (void)buf; + (void)usbd_dev; + + switch(req->bRequest) { + case USB_CDC_REQ_SET_CONTROL_LINE_STATE: { + /* + * This Linux cdc_acm driver requires this to be implemented + * even though it's optional in the CDC spec, and we don't + * advertise it in the ACM functional descriptor. + */ + char local_buf[10]; + struct usb_cdc_notification *notif = (void *)local_buf; + + /* We echo signals back to host as notification. */ + notif->bmRequestType = 0xA1; + notif->bNotification = USB_CDC_NOTIFY_SERIAL_STATE; + notif->wValue = 0; + notif->wIndex = 0; + notif->wLength = 2; + local_buf[8] = req->wValue & 3; + local_buf[9] = 0; + // usbd_ep_write_packet(0x83, buf, 10); + return 1; + } + case USB_CDC_REQ_SET_LINE_CODING: + if(*len < sizeof(struct usb_cdc_line_coding)) + return 0; + + return 1; + } + return 0; +} + +static void cdcacm_data_rx_cb(usbd_device *usbd_dev, u8 ep) +{ + (void)ep; + + char buf[64]; + int len = usbd_ep_read_packet(usbd_dev, 0x01, buf, 64); + + if (len) { + usbd_ep_write_packet(usbd_dev, 0x82, buf, len); + buf[len] = 0; + } +} + +static void cdcacm_set_config(usbd_device *usbd_dev, u16 wValue) +{ + (void)wValue; + + usbd_ep_setup(usbd_dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64, cdcacm_data_rx_cb); + usbd_ep_setup(usbd_dev, 0x82, USB_ENDPOINT_ATTR_BULK, 64, NULL); + usbd_ep_setup(usbd_dev, 0x83, USB_ENDPOINT_ATTR_INTERRUPT, 16, NULL); + + usbd_register_control_callback( + usbd_dev, + USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE, + USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, + cdcacm_control_request); +} + +int main(void) +{ + usbd_device *usbd_dev; + + rcc_clock_setup_in_hsi_out_48mhz(); + + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); + + AFIO_MAPR |= AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_ON; + + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, 0, GPIO15); + + usbd_dev = usbd_init(&stm32f103_usb_driver, &dev, &config, usb_strings, 3); + usbd_register_set_config_callback(usbd_dev, cdcacm_set_config); + + gpio_set(GPIOA, GPIO15); + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO15); + + while (1) + usbd_poll(usbd_dev); +} diff --git a/examples/stm32/f1/other/usb_cdcacm/cdcacm.ld b/examples/stm32/f1/other/usb_cdcacm/cdcacm.ld new file mode 100644 index 0000000..a822356 --- /dev/null +++ b/examples/stm32/f1/other/usb_cdcacm/cdcacm.ld @@ -0,0 +1,29 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 64K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_stm32f1.ld + diff --git a/examples/stm32/f1/other/usb_dfu/Makefile b/examples/stm32/f1/other/usb_dfu/Makefile new file mode 100644 index 0000000..c1e30dc --- /dev/null +++ b/examples/stm32/f1/other/usb_dfu/Makefile @@ -0,0 +1,23 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = usbdfu + +include ../../Makefile.include + diff --git a/examples/stm32/f1/other/usb_dfu/README b/examples/stm32/f1/other/usb_dfu/README new file mode 100644 index 0000000..9c0169a --- /dev/null +++ b/examples/stm32/f1/other/usb_dfu/README @@ -0,0 +1,7 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example implements a USB Device Firmware Upgrade (DFU) bootloader +to demonstrate the use of the USB device stack. + diff --git a/examples/stm32/f1/other/usb_dfu/usbdfu.c b/examples/stm32/f1/other/usb_dfu/usbdfu.c new file mode 100644 index 0000000..dfd5eeb --- /dev/null +++ b/examples/stm32/f1/other/usb_dfu/usbdfu.c @@ -0,0 +1,275 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Gareth McMullin + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include + +#define APP_ADDRESS 0x08002000 + +/* Commands sent with wBlockNum == 0 as per ST implementation. */ +#define CMD_SETADDR 0x21 +#define CMD_ERASE 0x41 + +/* We need a special large control buffer for this device: */ +u8 usbd_control_buffer[1024]; + +static enum dfu_state usbdfu_state = STATE_DFU_IDLE; + +static struct { + u8 buf[sizeof(usbd_control_buffer)]; + u16 len; + u32 addr; + u16 blocknum; +} prog; + +const struct usb_device_descriptor dev = { + .bLength = USB_DT_DEVICE_SIZE, + .bDescriptorType = USB_DT_DEVICE, + .bcdUSB = 0x0200, + .bDeviceClass = 0, + .bDeviceSubClass = 0, + .bDeviceProtocol = 0, + .bMaxPacketSize0 = 64, + .idVendor = 0x0483, + .idProduct = 0xDF11, + .bcdDevice = 0x0200, + .iManufacturer = 1, + .iProduct = 2, + .iSerialNumber = 3, + .bNumConfigurations = 1, +}; + +const struct usb_dfu_descriptor dfu_function = { + .bLength = sizeof(struct usb_dfu_descriptor), + .bDescriptorType = DFU_FUNCTIONAL, + .bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH, + .wDetachTimeout = 255, + .wTransferSize = 1024, + .bcdDFUVersion = 0x011A, +}; + +const struct usb_interface_descriptor iface = { + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 0, + .bAlternateSetting = 0, + .bNumEndpoints = 0, + .bInterfaceClass = 0xFE, /* Device Firmware Upgrade */ + .bInterfaceSubClass = 1, + .bInterfaceProtocol = 2, + + /* The ST Microelectronics DfuSe application needs this string. + * The format isn't documented... */ + .iInterface = 4, + + .extra = &dfu_function, + .extralen = sizeof(dfu_function), +}; + +const struct usb_interface ifaces[] = {{ + .num_altsetting = 1, + .altsetting = &iface, +}}; + +const struct usb_config_descriptor config = { + .bLength = USB_DT_CONFIGURATION_SIZE, + .bDescriptorType = USB_DT_CONFIGURATION, + .wTotalLength = 0, + .bNumInterfaces = 1, + .bConfigurationValue = 1, + .iConfiguration = 0, + .bmAttributes = 0xC0, + .bMaxPower = 0x32, + + .interface = ifaces, +}; + +static const char *usb_strings[] = { + "Black Sphere Technologies", + "DFU Demo", + "DEMO", + /* This string is used by ST Microelectronics' DfuSe utility. */ + "@Internal Flash /0x08000000/8*001Ka,56*001Kg", +}; + +static u8 usbdfu_getstatus(u32 *bwPollTimeout) +{ + switch (usbdfu_state) { + case STATE_DFU_DNLOAD_SYNC: + usbdfu_state = STATE_DFU_DNBUSY; + *bwPollTimeout = 100; + return DFU_STATUS_OK; + case STATE_DFU_MANIFEST_SYNC: + /* Device will reset when read is complete. */ + usbdfu_state = STATE_DFU_MANIFEST; + return DFU_STATUS_OK; + default: + return DFU_STATUS_OK; + } +} + +static void usbdfu_getstatus_complete(usbd_device *usbd_dev, struct usb_setup_data *req) +{ + int i; + (void)req; + (void)usbd_dev; + + switch (usbdfu_state) { + case STATE_DFU_DNBUSY: + flash_unlock(); + if (prog.blocknum == 0) { + switch (prog.buf[0]) { + case CMD_ERASE: + { + u32 *dat = (u32 *)(prog.buf + 1); + flash_erase_page(*dat); + } + case CMD_SETADDR: + { + u32 *dat = (u32 *)(prog.buf + 1); + prog.addr = *dat; + } + } + } else { + u32 baseaddr = prog.addr + ((prog.blocknum - 2) * + dfu_function.wTransferSize); + for (i = 0; i < prog.len; i += 2) { + u16 *dat = (u16 *)(prog.buf + i); + flash_program_half_word(baseaddr + i, + *dat); + } + } + flash_lock(); + + /* Jump straight to dfuDNLOAD-IDLE, skipping dfuDNLOAD-SYNC. */ + usbdfu_state = STATE_DFU_DNLOAD_IDLE; + return; + case STATE_DFU_MANIFEST: + /* USB device must detach, we just reset... */ + scb_reset_system(); + return; /* Will never return. */ + default: + return; + } +} + +static int usbdfu_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, u8 **buf, + u16 *len, void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req)) +{ + (void)usbd_dev; + + if ((req->bmRequestType & 0x7F) != 0x21) + return 0; /* Only accept class request. */ + + switch (req->bRequest) { + case DFU_DNLOAD: + if ((len == NULL) || (*len == 0)) { + usbdfu_state = STATE_DFU_MANIFEST_SYNC; + return 1; + } else { + /* Copy download data for use on GET_STATUS. */ + prog.blocknum = req->wValue; + prog.len = *len; + memcpy(prog.buf, *buf, *len); + usbdfu_state = STATE_DFU_DNLOAD_SYNC; + return 1; + } + case DFU_CLRSTATUS: + /* Clear error and return to dfuIDLE. */ + if (usbdfu_state == STATE_DFU_ERROR) + usbdfu_state = STATE_DFU_IDLE; + return 1; + case DFU_ABORT: + /* Abort returns to dfuIDLE state. */ + usbdfu_state = STATE_DFU_IDLE; + return 1; + case DFU_UPLOAD: + /* Upload not supported for now. */ + return 0; + case DFU_GETSTATUS: { + u32 bwPollTimeout = 0; /* 24-bit integer in DFU class spec */ + (*buf)[0] = usbdfu_getstatus(&bwPollTimeout); + (*buf)[1] = bwPollTimeout & 0xFF; + (*buf)[2] = (bwPollTimeout >> 8) & 0xFF; + (*buf)[3] = (bwPollTimeout >> 16) & 0xFF; + (*buf)[4] = usbdfu_state; + (*buf)[5] = 0; /* iString not used here */ + *len = 6; + *complete = usbdfu_getstatus_complete; + return 1; + } + case DFU_GETSTATE: + /* Return state with no state transision. */ + *buf[0] = usbdfu_state; + *len = 1; + return 1; + } + + return 0; +} + +int main(void) +{ + usbd_device *usbd_dev; + + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + + if (!gpio_get(GPIOA, GPIO10)) { + /* Boot the application if it's valid. */ + if ((*(volatile u32 *)APP_ADDRESS & 0x2FFE0000) == 0x20000000) { + /* Set vector table base address. */ + SCB_VTOR = APP_ADDRESS & 0xFFFF; + /* Initialise master stack pointer. */ + asm volatile("msr msp, %0"::"g" + (*(volatile u32 *)APP_ADDRESS)); + /* Jump to application. */ + (*(void (**)())(APP_ADDRESS + 4))(); + } + } + + rcc_clock_setup_in_hsi_out_48mhz(); + + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); + + AFIO_MAPR |= AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_ON; + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, 0, GPIO15); + + rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_OTGFSEN); + + usbd_dev = usbd_init(&stm32f107_usb_driver, &dev, &config, usb_strings, 4); + usbd_set_control_buffer_size(usbd_dev, sizeof(usbd_control_buffer)); + usbd_register_control_callback( + usbd_dev, + USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE, + USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, + usbdfu_control_request); + + gpio_set(GPIOA, GPIO15); + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO15); + + while (1) + usbd_poll(usbd_dev); +} diff --git a/examples/stm32/f1/other/usb_dfu/usbdfu.ld b/examples/stm32/f1/other/usb_dfu/usbdfu.ld new file mode 100644 index 0000000..41b0f8d --- /dev/null +++ b/examples/stm32/f1/other/usb_dfu/usbdfu.ld @@ -0,0 +1,29 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 8K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 4K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_stm32f1.ld + diff --git a/examples/stm32/f1/other/usb_hid/Makefile b/examples/stm32/f1/other/usb_hid/Makefile new file mode 100644 index 0000000..a094fe9 --- /dev/null +++ b/examples/stm32/f1/other/usb_hid/Makefile @@ -0,0 +1,23 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = usbhid + +include ../../Makefile.include + diff --git a/examples/stm32/f1/other/usb_hid/README b/examples/stm32/f1/other/usb_hid/README new file mode 100644 index 0000000..92645c9 --- /dev/null +++ b/examples/stm32/f1/other/usb_hid/README @@ -0,0 +1,7 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example implements a USB Human Interface Device (HID) +to demonstrate the use of the USB device stack. + diff --git a/examples/stm32/f1/other/usb_hid/usbhid.c b/examples/stm32/f1/other/usb_hid/usbhid.c new file mode 100644 index 0000000..82b265c --- /dev/null +++ b/examples/stm32/f1/other/usb_hid/usbhid.c @@ -0,0 +1,283 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Gareth McMullin + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include + +/* Define this to include the DFU APP interface. */ +#define INCLUDE_DFU_INTERFACE + +#ifdef INCLUDE_DFU_INTERFACE +#include +#include +#endif + +static usbd_device *usbd_dev; + +const struct usb_device_descriptor dev_descr = { + .bLength = USB_DT_DEVICE_SIZE, + .bDescriptorType = USB_DT_DEVICE, + .bcdUSB = 0x0200, + .bDeviceClass = 0, + .bDeviceSubClass = 0, + .bDeviceProtocol = 0, + .bMaxPacketSize0 = 64, + .idVendor = 0x0483, + .idProduct = 0x5710, + .bcdDevice = 0x0200, + .iManufacturer = 1, + .iProduct = 2, + .iSerialNumber = 3, + .bNumConfigurations = 1, +}; + +/* I have no idea what this means. I haven't read the HID spec. */ +static const u8 hid_report_descriptor[] = { + 0x05, 0x01, 0x09, 0x02, 0xA1, 0x01, 0x09, 0x01, + 0xA1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x03, + 0x15, 0x00, 0x25, 0x01, 0x95, 0x03, 0x75, 0x01, + 0x81, 0x02, 0x95, 0x01, 0x75, 0x05, 0x81, 0x01, + 0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 0x09, 0x38, + 0x15, 0x81, 0x25, 0x7F, 0x75, 0x08, 0x95, 0x03, + 0x81, 0x06, 0xC0, 0x09, 0x3c, 0x05, 0xff, 0x09, + 0x01, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95, + 0x02, 0xb1, 0x22, 0x75, 0x06, 0x95, 0x01, 0xb1, + 0x01, 0xc0, +}; + +static const struct { + struct usb_hid_descriptor hid_descriptor; + struct { + u8 bReportDescriptorType; + u16 wDescriptorLength; + } __attribute__((packed)) hid_report; +} __attribute__((packed)) hid_function = { + .hid_descriptor = { + .bLength = sizeof(hid_function), + .bDescriptorType = USB_DT_HID, + .bcdHID = 0x0100, + .bCountryCode = 0, + .bNumDescriptors = 1, + }, + .hid_report = { + .bReportDescriptorType = USB_DT_REPORT, + .wDescriptorLength = sizeof(hid_report_descriptor), + } +}; + +const struct usb_endpoint_descriptor hid_endpoint = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x81, + .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, + .wMaxPacketSize = 4, + .bInterval = 0x20, +}; + +const struct usb_interface_descriptor hid_iface = { + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 0, + .bAlternateSetting = 0, + .bNumEndpoints = 1, + .bInterfaceClass = USB_CLASS_HID, + .bInterfaceSubClass = 1, /* boot */ + .bInterfaceProtocol = 2, /* mouse */ + .iInterface = 0, + + .endpoint = &hid_endpoint, + + .extra = &hid_function, + .extralen = sizeof(hid_function), +}; + +#ifdef INCLUDE_DFU_INTERFACE +const struct usb_dfu_descriptor dfu_function = { + .bLength = sizeof(struct usb_dfu_descriptor), + .bDescriptorType = DFU_FUNCTIONAL, + .bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH, + .wDetachTimeout = 255, + .wTransferSize = 1024, + .bcdDFUVersion = 0x011A, +}; + +const struct usb_interface_descriptor dfu_iface = { + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 1, + .bAlternateSetting = 0, + .bNumEndpoints = 0, + .bInterfaceClass = 0xFE, + .bInterfaceSubClass = 1, + .bInterfaceProtocol = 1, + .iInterface = 0, + + .extra = &dfu_function, + .extralen = sizeof(dfu_function), +}; +#endif + +const struct usb_interface ifaces[] = {{ + .num_altsetting = 1, + .altsetting = &hid_iface, +#ifdef INCLUDE_DFU_INTERFACE +}, { + .num_altsetting = 1, + .altsetting = &dfu_iface, +#endif +}}; + +const struct usb_config_descriptor config = { + .bLength = USB_DT_CONFIGURATION_SIZE, + .bDescriptorType = USB_DT_CONFIGURATION, + .wTotalLength = 0, +#ifdef INCLUDE_DFU_INTERFACE + .bNumInterfaces = 2, +#else + .bNumInterfaces = 1, +#endif + .bConfigurationValue = 1, + .iConfiguration = 0, + .bmAttributes = 0xC0, + .bMaxPower = 0x32, + + .interface = ifaces, +}; + +static const char *usb_strings[] = { + "Black Sphere Technologies", + "HID Demo", + "DEMO", +}; + +static int hid_control_request(usbd_device *dev, struct usb_setup_data *req, u8 **buf, u16 *len, + void (**complete)(usbd_device *, struct usb_setup_data *)) +{ + (void)complete; + (void)dev; + + if((req->bmRequestType != 0x81) || + (req->bRequest != USB_REQ_GET_DESCRIPTOR) || + (req->wValue != 0x2200)) + return 0; + + /* Handle the HID report descriptor. */ + *buf = (u8 *)hid_report_descriptor; + *len = sizeof(hid_report_descriptor); + + return 1; +} + +#ifdef INCLUDE_DFU_INTERFACE +static void dfu_detach_complete(usbd_device *dev, struct usb_setup_data *req) +{ + (void)req; + (void)dev; + + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, 0, GPIO15); + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO10); + gpio_set(GPIOA, GPIO10); + scb_reset_core(); +} + +static int dfu_control_request(usbd_device *dev, struct usb_setup_data *req, u8 **buf, u16 *len, + void (**complete)(usbd_device *, struct usb_setup_data *)) +{ + (void)buf; + (void)len; + (void)dev; + + if ((req->bmRequestType != 0x21) || (req->bRequest != DFU_DETACH)) + return 0; /* Only accept class request. */ + + *complete = dfu_detach_complete; + + return 1; +} +#endif + +static void hid_set_config(usbd_device *dev, u16 wValue) +{ + (void)wValue; + (void)dev; + + usbd_ep_setup(dev, 0x81, USB_ENDPOINT_ATTR_INTERRUPT, 4, NULL); + + usbd_register_control_callback( + dev, + USB_REQ_TYPE_STANDARD | USB_REQ_TYPE_INTERFACE, + USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, + hid_control_request); +#ifdef INCLUDE_DFU_INTERFACE + usbd_register_control_callback( + dev, + USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE, + USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, + dfu_control_request); +#endif + + systick_set_clocksource(STK_CTRL_CLKSOURCE_AHB_DIV8); + /* SysTick interrupt every N clock pulses: set reload to N-1 */ + systick_set_reload(99999); + systick_interrupt_enable(); + systick_counter_enable(); +} + +int main(void) +{ + rcc_clock_setup_in_hsi_out_48mhz(); + + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); + + AFIO_MAPR |= AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_ON; + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, 0, GPIO15); + + usbd_dev = usbd_init(&stm32f103_usb_driver, &dev_descr, &config, usb_strings, 3); + usbd_register_set_config_callback(usbd_dev, hid_set_config); + + gpio_set(GPIOA, GPIO15); + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO15); + + while (1) + usbd_poll(usbd_dev); +} + +void sys_tick_handler(void) +{ + static int x = 0; + static int dir = 1; + u8 buf[4] = {0, 0, 0, 0}; + + buf[1] = dir; + x += dir; + if (x > 30) + dir = -dir; + if (x < -30) + dir = -dir; + + usbd_ep_write_packet(usbd_dev, 0x81, buf, 4); +} diff --git a/examples/stm32/f1/other/usb_hid/usbhid.ld b/examples/stm32/f1/other/usb_hid/usbhid.ld new file mode 100644 index 0000000..a822356 --- /dev/null +++ b/examples/stm32/f1/other/usb_hid/usbhid.ld @@ -0,0 +1,29 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Thomas Otto + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 64K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_stm32f1.ld + diff --git a/examples/stm32/f1/stm32-h103/button/Makefile b/examples/stm32/f1/stm32-h103/button/Makefile new file mode 100644 index 0000000..910ae8a --- /dev/null +++ b/examples/stm32/f1/stm32-h103/button/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = button + +LDSCRIPT = ../stm32-h103.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/stm32-h103/button/button.c b/examples/stm32/f1/stm32-h103/button/button.c new file mode 100644 index 0000000..927dd71 --- /dev/null +++ b/examples/stm32/f1/stm32-h103/button/button.c @@ -0,0 +1,76 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann , + * Copyright (C) 2010 Piotr Esden-Tempski + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include + +u16 exti_line_state; + +/* Set STM32 to 72 MHz. */ +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); +} + +static void gpio_setup(void) +{ + /* Enable GPIOC clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + /* Set GPIO12 (in GPIO port C) to 'output push-pull'. */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO12); +} + +static void button_setup(void) +{ + /* Enable GPIOA clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + + /* Set GPIO0 (in GPIO port A) to 'input open-drain'. */ + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO0); +} + +int main(void) +{ + int i; + + clock_setup(); + gpio_setup(); + button_setup(); + + /* Blink the LED (PC12) on the board. */ + while (1) { + gpio_toggle(GPIOC, GPIO12); + + exti_line_state = GPIOA_IDR; + if ((exti_line_state & (1 << 0)) != 0) { + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/stm32/f1/stm32-h103/exti_both/Makefile b/examples/stm32/f1/stm32-h103/exti_both/Makefile new file mode 100644 index 0000000..5fcd5e3 --- /dev/null +++ b/examples/stm32/f1/stm32-h103/exti_both/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = exti_both + +LDSCRIPT = ../stm32-h103.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/stm32-h103/exti_both/exti_both.c b/examples/stm32/f1/stm32-h103/exti_both/exti_both.c new file mode 100644 index 0000000..2a086f8 --- /dev/null +++ b/examples/stm32/f1/stm32-h103/exti_both/exti_both.c @@ -0,0 +1,88 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann , + * Copyright (C) 2010 Piotr Esden-Tempski + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include + +u16 exti_line_state; + +/* Set STM32 to 72 MHz. */ +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); +} + +static void gpio_setup(void) +{ + /* Enable GPIOC clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + /* Set GPIO12 (in GPIO port C) to 'output push-pull'. */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO12); +} + +static void exti_setup(void) +{ + /* Enable GPIOA clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + + /* Enable AFIO clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); + + /* Enable EXTI0 interrupt. */ + nvic_enable_irq(NVIC_EXTI0_IRQ); + + /* Set GPIO0 (in GPIO port A) to 'input float'. */ + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO0); + + /* Configure the EXTI subsystem. */ + exti_select_source(EXTI0, GPIOA); + exti_set_trigger(EXTI0, EXTI_TRIGGER_BOTH); + exti_enable_request(EXTI0); +} + +void exti0_isr(void) +{ + exti_line_state = GPIOA_IDR; + + /* The LED (PC12) is on, but turns off when the button is pressed. */ + if ((exti_line_state & (1 << 0)) != 0) { + gpio_clear(GPIOC, GPIO12); + } else { + gpio_set(GPIOC, GPIO12); + } + + exti_reset_request(EXTI0); +} + +int main(void) +{ + clock_setup(); + gpio_setup(); + exti_setup(); + + while (1) + __asm("nop"); + + return 0; +} diff --git a/examples/stm32/f1/stm32-h103/exti_rising_falling/Makefile b/examples/stm32/f1/stm32-h103/exti_rising_falling/Makefile new file mode 100644 index 0000000..5791340 --- /dev/null +++ b/examples/stm32/f1/stm32-h103/exti_rising_falling/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = exti_rising_falling + +LDSCRIPT = ../stm32-h103.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/stm32-h103/exti_rising_falling/exti_rising_falling.c b/examples/stm32/f1/stm32-h103/exti_rising_falling/exti_rising_falling.c new file mode 100644 index 0000000..76db50f --- /dev/null +++ b/examples/stm32/f1/stm32-h103/exti_rising_falling/exti_rising_falling.c @@ -0,0 +1,93 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann , + * Copyright (C) 2010 Piotr Esden-Tempski + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include + +#define FALLING 0 +#define RISING 1 + +u16 exti_direction = FALLING; + +/* Set STM32 to 72 MHz. */ +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); +} + +static void gpio_setup(void) +{ + /* Enable GPIOC clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + /* Set GPIO12 (in GPIO port C) to 'output push-pull'. */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO12); +} + +static void exti_setup(void) +{ + /* Enable GPIOA clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + + /* Enable AFIO clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); + + /* Enable EXTI0 interrupt. */ + nvic_enable_irq(NVIC_EXTI0_IRQ); + + /* Set GPIO0 (in GPIO port A) to 'input open-drain'. */ + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO0); + + /* Configure the EXTI subsystem. */ + exti_select_source(EXTI0, GPIOA); + exti_direction = FALLING; + exti_set_trigger(EXTI0, EXTI_TRIGGER_FALLING); + exti_enable_request(EXTI0); +} + +void exti0_isr(void) +{ + exti_reset_request(EXTI0); + + if (exti_direction == FALLING) { + gpio_set(GPIOC, GPIO12); + exti_direction = RISING; + exti_set_trigger(EXTI0, EXTI_TRIGGER_RISING); + } else { + gpio_clear(GPIOC, GPIO12); + exti_direction = FALLING; + exti_set_trigger(EXTI0, EXTI_TRIGGER_FALLING); + } +} + +int main(void) +{ + clock_setup(); + gpio_setup(); + exti_setup(); + + while (1) + __asm("nop"); + + return 0; +} diff --git a/examples/stm32/f1/stm32-h103/fancyblink/Makefile b/examples/stm32/f1/stm32-h103/fancyblink/Makefile new file mode 100644 index 0000000..e88afaf --- /dev/null +++ b/examples/stm32/f1/stm32-h103/fancyblink/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = fancyblink + +LDSCRIPT = ../stm32-h103.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/stm32-h103/fancyblink/README b/examples/stm32/f1/stm32-h103/fancyblink/README new file mode 100644 index 0000000..bdb6ec8 --- /dev/null +++ b/examples/stm32/f1/stm32-h103/fancyblink/README @@ -0,0 +1,10 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This is small LED blinking example program using libopencm3. + +It's intended for the ST STM32-based Olimex STM32-H103 eval board (see +http://olimex.com/dev/stm32-h103.html for details). It should blink +the LED on the board. + diff --git a/examples/stm32/f1/stm32-h103/fancyblink/fancyblink.c b/examples/stm32/f1/stm32-h103/fancyblink/fancyblink.c new file mode 100644 index 0000000..f3a4624 --- /dev/null +++ b/examples/stm32/f1/stm32-h103/fancyblink/fancyblink.c @@ -0,0 +1,54 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include + +/* Set STM32 to 72 MHz. */ +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + + /* Enable GPIOC clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); +} + +static void gpio_setup(void) +{ + /* Set GPIO12 (in GPIO port C) to 'output push-pull'. */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO12); +} + +int main(void) +{ + int i; + + clock_setup(); + gpio_setup(); + + /* Blink the LED (PC12) on the board. */ + while (1) { + gpio_toggle(GPIOC, GPIO12); /* LED on/off */ + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/stm32/f1/stm32-h103/led_stripe/Makefile b/examples/stm32/f1/stm32-h103/led_stripe/Makefile new file mode 100644 index 0000000..8295a69 --- /dev/null +++ b/examples/stm32/f1/stm32-h103/led_stripe/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = led_stripe + +LDSCRIPT = ../stm32-h103.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/stm32-h103/led_stripe/led_stripe.c b/examples/stm32/f1/stm32-h103/led_stripe/led_stripe.c new file mode 100644 index 0000000..4407c66 --- /dev/null +++ b/examples/stm32/f1/stm32-h103/led_stripe/led_stripe.c @@ -0,0 +1,236 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2011 Piotr Esden-Tempski + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* + * This example is implementing the protocol of ZJ168 addressable LED + * strips. These strips use the LPD6803 controller. You may be able to + * find the datasheet here: + * http://www.adafruit.com/datasheets/LPD6803.pdf + */ + +#include +#include +#include + +#define SPI_BANK GPIOB +#define SCLK_PIN GPIO13 +#define MOSI_PIN GPIO15 + +#define SMALL_DELAY_VALUE 0 + +#define COLOR_COUNT 50 + +#define SCLK(VAL) \ + if (VAL) { \ + gpio_set(SPI_BANK, SCLK_PIN); \ + } else { \ + gpio_clear(SPI_BANK, SCLK_PIN); \ + } + +#define MOSI(VAL) \ + if (VAL) { \ + gpio_set(SPI_BANK, MOSI_PIN); \ + } else { \ + gpio_clear(SPI_BANK, MOSI_PIN); \ + } + +#define SMALL_DELAY() { \ + int j; \ + for (j = 0; j < SMALL_DELAY_VALUE; j++) \ + __asm__("nop"); \ + } + +struct color { + u8 r; + u8 g; + u8 b; +}; + +/* Set STM32 to 72 MHz. */ +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + + /* Enable GPIOB and GPIOC clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); +} + +static void gpio_setup(void) +{ + /* Set GPIO12 (in GPIO port C) to 'output push-pull'. */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO12); + + /* Set GPIO13 (in GPIO port B) to 'output push-pull'. */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO13); + + /* Set GPIO15 (in GPIO port B) to 'output push-pull'. */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO15); +} + +static void send_colors(struct color *colors, int count) +{ + int i, k; + + /* Initialize SPI pins. */ + SCLK(0); + MOSI(0); + + /* Start frame */ + for (i = 0; i < 32; i++) { + SCLK(1); + SMALL_DELAY(); + SCLK(0); + SMALL_DELAY(); + } + + /* Color cell output */ + for (k = 0; k < count; k++) { + /* Start bit */ + MOSI(1); + SCLK(1); + SMALL_DELAY(); + SCLK(0); + SMALL_DELAY(); + + /* Blue */ + for (i = 0; i < 5; i++) { + MOSI(((colors[k].b & ((1 << 4) >> i)) != 0)); + SCLK(1); + SMALL_DELAY(); + SCLK(0); + SMALL_DELAY(); + } + /* Red */ + for (i = 0; i < 5; i++) { + MOSI(((colors[k].r & ((1 << 4) >> i)) != 0)); + SCLK(1); + SMALL_DELAY(); + SCLK(0); + SMALL_DELAY(); + } + /* Green */ + for (i = 0; i < 5; i++) { + MOSI(((colors[k].g & ((1 << 4) >> i)) != 0)); + SCLK(1); + SMALL_DELAY(); + SCLK(0); + SMALL_DELAY(); + } + } + + /* End frame */ + MOSI(0); + for (k = 0; k < count; k++) { + SCLK(1); + SMALL_DELAY(); + SCLK(0); + SMALL_DELAY(); + } +} + +static void reset_colors(struct color *colors, int count) +{ + int i; + + for (i = 0; i < count; i++) { + colors[i].r = 0; + colors[i].g = 0; + colors[i].b = 0; + } +} + +static void init_colors(struct color *colors, int count) +{ + colors[0].r = 0x1F; + colors[0].g = 0; + colors[0].b = 0; + colors[1].r = 0; + colors[1].g = 0x1F; + colors[1].b = 0; + colors[2].r = 0; + colors[2].g = 0; + colors[2].b = 0x1F; + + count = count; +} + +static void step_colors(struct color *colors, int count) +{ + int i; + struct color tmp_color1; + struct color tmp_color2; + +#if 0 + /* Random blinking. */ + for (i = 0; i < count; i++) { + colors[i].r = rand() & 0x01; + colors[i].g = rand() & 0x01; + colors[i].b = rand() & 0x01; + } +#endif + + /* Generate next colors. */ + tmp_color1.r = colors[0].r; + tmp_color1.g = colors[0].g; + tmp_color1.b = colors[0].b; + colors[0].r = colors[count - 1].r; + colors[0].g = colors[count - 1].g; + colors[0].b = colors[count - 1].b; + for (i = 1; i < count; i++) { + tmp_color2.r = colors[i].r; + tmp_color2.g = colors[i].g; + tmp_color2.b = colors[i].b; + colors[i].r = tmp_color1.r; + colors[i].g = tmp_color1.g; + colors[i].b = tmp_color1.b; + tmp_color1.r = tmp_color2.r; + tmp_color1.g = tmp_color2.g; + tmp_color1.b = tmp_color2.b; + } +} + +int main(void) +{ + int i; + + struct color colors[COLOR_COUNT]; + + clock_setup(); + gpio_setup(); + + reset_colors(colors, COLOR_COUNT); + init_colors(colors, COLOR_COUNT); + + while (1) { + gpio_toggle(GPIOC, GPIO12); /* LED on/off */ + + send_colors(colors, COLOR_COUNT); + + step_colors(colors, COLOR_COUNT); + + for (i = 0; i < 1000000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/stm32/f1/stm32-h103/miniblink/Makefile b/examples/stm32/f1/stm32-h103/miniblink/Makefile new file mode 100644 index 0000000..723046b --- /dev/null +++ b/examples/stm32/f1/stm32-h103/miniblink/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = miniblink + +LDSCRIPT = ../stm32-h103.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/stm32-h103/miniblink/README b/examples/stm32/f1/stm32-h103/miniblink/README new file mode 100644 index 0000000..0e046f2 --- /dev/null +++ b/examples/stm32/f1/stm32-h103/miniblink/README @@ -0,0 +1,10 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This is the smallest-possible example program using libopencm3. + +It's intended for the ST STM32-based Olimex STM32-H103 eval board (see +http://olimex.com/dev/stm32-h103.html for details). It should blink +the LED on the board. + diff --git a/examples/stm32/f1/stm32-h103/miniblink/miniblink.c b/examples/stm32/f1/stm32-h103/miniblink/miniblink.c new file mode 100644 index 0000000..af8d4fd --- /dev/null +++ b/examples/stm32/f1/stm32-h103/miniblink/miniblink.c @@ -0,0 +1,71 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include + +static void gpio_setup(void) +{ + /* Enable GPIOC clock. */ + /* Manually: */ + // RCC_APB2ENR |= RCC_APB2ENR_IOPCEN; + /* Using API functions: */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + /* Set GPIO12 (in GPIO port C) to 'output push-pull'. */ + /* Manually: */ + // GPIOC_CRH = (GPIO_CNF_OUTPUT_PUSHPULL << (((12 - 8) * 4) + 2)); + // GPIOC_CRH |= (GPIO_MODE_OUTPUT_2_MHZ << ((12 - 8) * 4)); + /* Using API functions: */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO12); +} + +int main(void) +{ + int i; + + gpio_setup(); + + /* Blink the LED (PC12) on the board. */ + while (1) { + /* Manually: */ + // GPIOC_BSRR = GPIO12; /* LED off */ + // for (i = 0; i < 800000; i++) /* Wait a bit. */ + // __asm__("nop"); + // GPIOC_BRR = GPIO12; /* LED on */ + // for (i = 0; i < 800000; i++) /* Wait a bit. */ + // __asm__("nop"); + + /* Using API functions gpio_set()/gpio_clear(): */ + // gpio_set(GPIOC, GPIO12); /* LED off */ + // for (i = 0; i < 800000; i++) /* Wait a bit. */ + // __asm__("nop"); + // gpio_clear(GPIOC, GPIO12); /* LED on */ + // for (i = 0; i < 800000; i++) /* Wait a bit. */ + // __asm__("nop"); + + /* Using API function gpio_toggle(): */ + gpio_toggle(GPIOC, GPIO12); /* LED on/off */ + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/stm32/f1/stm32-h103/pwm_6step/Makefile b/examples/stm32/f1/stm32-h103/pwm_6step/Makefile new file mode 100644 index 0000000..026d5ee --- /dev/null +++ b/examples/stm32/f1/stm32-h103/pwm_6step/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = pwm_6step + +LDSCRIPT = ../stm32-h103.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/stm32-h103/pwm_6step/pwm_6step.c b/examples/stm32/f1/stm32-h103/pwm_6step/pwm_6step.c new file mode 100644 index 0000000..b0efdde --- /dev/null +++ b/examples/stm32/f1/stm32-h103/pwm_6step/pwm_6step.c @@ -0,0 +1,400 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2011 Piotr Esden-Tempski + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include + +#define FALLING 0 +#define RISING 1 + +u16 exti_direction = FALLING; + +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); +} + +static void gpio_setup(void) +{ + /* Enable GPIOC clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + /* Set GPIO12 (in GPIO port C) to 'output push-pull'. */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO12); +} + +static void exti_setup(void) +{ + /* Enable GPIOA clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + + /* Enable AFIO clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); + + /* Enable EXTI0 interrupt. */ + nvic_enable_irq(NVIC_EXTI0_IRQ); + + /* Set GPIO0 (in GPIO port A) to 'input open-drain'. */ + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO0); + + /* Configure the EXTI subsystem. */ + exti_select_source(EXTI0, GPIOA); + exti_direction = FALLING; + exti_set_trigger(EXTI0, EXTI_TRIGGER_FALLING); + exti_enable_request(EXTI0); +} + +void exti0_isr(void) +{ + exti_reset_request(EXTI0); + + if (exti_direction == FALLING) { + // gpio_toggle(GPIOA, GPIO12); + exti_direction = RISING; + exti_set_trigger(EXTI0, EXTI_TRIGGER_RISING); + } else { + // gpio_toggle(GPIOA, GPIO12); + timer_generate_event(TIM1, TIM_EGR_COMG); + exti_direction = FALLING; + exti_set_trigger(EXTI0, EXTI_TRIGGER_FALLING); + } +} + +static void tim_setup(void) +{ + /* Enable TIM1 clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_TIM1EN); + + /* Enable GPIOA, GPIOB and Alternate Function clocks. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, + RCC_APB2ENR_IOPAEN | + RCC_APB2ENR_IOPBEN | RCC_APB2ENR_AFIOEN); + + /* + * Set TIM1 channel output pins to + * 'output alternate function push-pull'. + */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, + GPIO_TIM1_CH1 | GPIO_TIM1_CH2 | GPIO_TIM1_CH3); + + /* + * Set TIM1 complementary channel output pins to + * 'output alternate function push-pull'. + */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, + GPIO_TIM1_CH1N | GPIO_TIM1_CH2N | GPIO_TIM1_CH3N); + + /* Enable TIM1 commutation interrupt. */ + nvic_enable_irq(NVIC_TIM1_TRG_COM_IRQ); + + /* Reset TIM1 peripheral. */ + timer_reset(TIM1); + + /* Timer global mode: + * - No divider + * - Alignment edge + * - Direction up + */ + timer_set_mode(TIM1, TIM_CR1_CKD_CK_INT, + TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); + + /* Reset prescaler value. */ + timer_set_prescaler(TIM1, 0); + + /* Reset repetition counter value. */ + timer_set_repetition_counter(TIM1, 0); + + /* Enable preload. */ + timer_enable_preload(TIM1); + + /* Continuous mode. */ + timer_continuous_mode(TIM1); + + /* Period (32kHz). */ + timer_set_period(TIM1, 72000000 / 32000); + + /* Configure break and deadtime. */ + timer_set_deadtime(TIM1, 10); + timer_set_enabled_off_state_in_idle_mode(TIM1); + timer_set_enabled_off_state_in_run_mode(TIM1); + timer_disable_break(TIM1); + timer_set_break_polarity_high(TIM1); + timer_disable_break_automatic_output(TIM1); + timer_set_break_lock(TIM1, TIM_BDTR_LOCK_OFF); + + /* -- OC1 and OC1N configuration -- */ + + /* Disable outputs. */ + timer_disable_oc_output(TIM1, TIM_OC1); + timer_disable_oc_output(TIM1, TIM_OC1N); + + /* Configure global mode of line 1. */ + timer_disable_oc_clear(TIM1, TIM_OC1); + timer_enable_oc_preload(TIM1, TIM_OC1); + timer_set_oc_slow_mode(TIM1, TIM_OC1); + timer_set_oc_mode(TIM1, TIM_OC1, TIM_OCM_PWM1); + + /* Configure OC1. */ + timer_set_oc_polarity_high(TIM1, TIM_OC1); + timer_set_oc_idle_state_set(TIM1, TIM_OC1); + + /* Configure OC1N. */ + timer_set_oc_polarity_high(TIM1, TIM_OC1N); + timer_set_oc_idle_state_set(TIM1, TIM_OC1N); + + /* Set the capture compare value for OC1. */ + timer_set_oc_value(TIM1, TIM_OC1, 100); + + /* Reenable outputs. */ + timer_enable_oc_output(TIM1, TIM_OC1); + timer_enable_oc_output(TIM1, TIM_OC1N); + + /* -- OC2 and OC2N configuration -- */ + + /* Disable outputs. */ + timer_disable_oc_output(TIM1, TIM_OC2); + timer_disable_oc_output(TIM1, TIM_OC2N); + + /* Configure global mode of line 2. */ + timer_disable_oc_clear(TIM1, TIM_OC2); + timer_enable_oc_preload(TIM1, TIM_OC2); + timer_set_oc_slow_mode(TIM1, TIM_OC2); + timer_set_oc_mode(TIM1, TIM_OC2, TIM_OCM_PWM1); + + /* Configure OC2. */ + timer_set_oc_polarity_high(TIM1, TIM_OC2); + timer_set_oc_idle_state_set(TIM1, TIM_OC2); + + /* Configure OC2N. */ + timer_set_oc_polarity_high(TIM1, TIM_OC2N); + timer_set_oc_idle_state_set(TIM1, TIM_OC2N); + + /* Set the capture compare value for OC1. */ + timer_set_oc_value(TIM1, TIM_OC2, 100); + + /* Reenable outputs. */ + timer_enable_oc_output(TIM1, TIM_OC2); + timer_enable_oc_output(TIM1, TIM_OC2N); + + /* -- OC3 and OC3N configuration -- */ + + /* Disable outputs. */ + timer_disable_oc_output(TIM1, TIM_OC3); + timer_disable_oc_output(TIM1, TIM_OC3N); + + /* Configure global mode of line 3. */ + timer_disable_oc_clear(TIM1, TIM_OC3); + timer_enable_oc_preload(TIM1, TIM_OC3); + timer_set_oc_slow_mode(TIM1, TIM_OC3); + timer_set_oc_mode(TIM1, TIM_OC3, TIM_OCM_PWM1); + + /* Configure OC3. */ + timer_set_oc_polarity_high(TIM1, TIM_OC3); + timer_set_oc_idle_state_set(TIM1, TIM_OC3); + + /* Configure OC3N. */ + timer_set_oc_polarity_high(TIM1, TIM_OC3N); + timer_set_oc_idle_state_set(TIM1, TIM_OC3N); + + /* Set the capture compare value for OC3. */ + timer_set_oc_value(TIM1, TIM_OC3, 100); + + /* Reenable outputs. */ + timer_enable_oc_output(TIM1, TIM_OC3); + timer_enable_oc_output(TIM1, TIM_OC3N); + + /* ---- */ + + /* ARR reload enable. */ + timer_enable_preload(TIM1); + + /* + * Enable preload of complementary channel configurations and + * update on COM event. + */ + timer_enable_preload_complementry_enable_bits(TIM1); + + /* Enable outputs in the break subsystem. */ + timer_enable_break_main_output(TIM1); + + /* Counter enable. */ + timer_enable_counter(TIM1); + + /* Enable commutation interrupt. */ + timer_enable_irq(TIM1, TIM_DIER_COMIE); +} + +void tim1_trg_com_isr(void) +{ + static int step = 0; + + /* Clear the COM trigger interrupt flag. */ + timer_clear_flag(TIM1, TIM_SR_COMIF); + + /* + * A simplified and inefficient implementation of PWM On + * scheme. Look at the implementation in Open-BLDC on + * http://open-bldc.org for the proper implementation. This + * one only serves as an example. + * + * Table of the PWM scheme zone configurations when driving: + * @verbatim + * | 1| 2| 3| 4| 5| 6| + * -+--+--+--+--+--+--+ + * A|p+|++| |p-|--| | + * -+--+--+--+--+--+--+ + * B| |p-|--| |p+|++| + * -+--+--+--+--+--+--+ + * C|--| |p+|++| |p-| + * -+--+--+--+--+--+--+ + * | | | | | | '- 360 Deg + * | | | | | '---- 300 Deg + * | | | | '------- 240 Deg + * | | | '---------- 180 Deg + * | | '------------- 120 Deg + * | '---------------- 60 Deg + * '------------------- 0 Deg + * + * Legend: + * p+: PWM on the high side + * p-: PWM on the low side + * --: Low side on + * ++: High side on + * : Floating/NC + * @endverbatim + */ + switch (step) { + case 0: /* A PWM HIGH, B OFF, C LOW */ + timer_set_oc_mode(TIM1, TIM_OC1, TIM_OCM_PWM1); + timer_set_oc_mode(TIM1, TIM_OC2, TIM_OCM_FROZEN); + timer_set_oc_mode(TIM1, TIM_OC3, TIM_OCM_FORCE_LOW); + + timer_enable_oc_output(TIM1, TIM_OC1); + timer_disable_oc_output(TIM1, TIM_OC1N); + + timer_disable_oc_output(TIM1, TIM_OC2); + timer_disable_oc_output(TIM1, TIM_OC2N); + + timer_enable_oc_output(TIM1, TIM_OC3); + timer_enable_oc_output(TIM1, TIM_OC3N); + + step++; + break; + case 1: /* A HIGH, B PWM LOW, C OFF */ + timer_set_oc_mode(TIM1, TIM_OC1, TIM_OCM_FORCE_HIGH); + timer_set_oc_mode(TIM1, TIM_OC2, TIM_OCM_PWM1); + timer_set_oc_mode(TIM1, TIM_OC3, TIM_OCM_FROZEN); + + timer_enable_oc_output(TIM1, TIM_OC1); + timer_enable_oc_output(TIM1, TIM_OC1N); + + timer_disable_oc_output(TIM1, TIM_OC2); + timer_enable_oc_output(TIM1, TIM_OC2N); + + timer_disable_oc_output(TIM1, TIM_OC3); + timer_disable_oc_output(TIM1, TIM_OC3N); + + step++; + break; + case 2: /* A OFF, B LOW, C PWM HIGH */ + timer_set_oc_mode(TIM1, TIM_OC1, TIM_OCM_FROZEN); + timer_set_oc_mode(TIM1, TIM_OC2, TIM_OCM_FORCE_LOW); + timer_set_oc_mode(TIM1, TIM_OC3, TIM_OCM_PWM1); + + timer_disable_oc_output(TIM1, TIM_OC1); + timer_disable_oc_output(TIM1, TIM_OC1N); + + timer_enable_oc_output(TIM1, TIM_OC2); + timer_enable_oc_output(TIM1, TIM_OC2N); + + timer_enable_oc_output(TIM1, TIM_OC3); + timer_disable_oc_output(TIM1, TIM_OC3N); + + step++; + break; + case 3: /* A PWM LOW, B OFF, C HIGH */ + timer_set_oc_mode(TIM1, TIM_OC1, TIM_OCM_PWM1); + timer_set_oc_mode(TIM1, TIM_OC2, TIM_OCM_FROZEN); + timer_set_oc_mode(TIM1, TIM_OC3, TIM_OCM_FORCE_HIGH); + + timer_disable_oc_output(TIM1, TIM_OC1); + timer_enable_oc_output(TIM1, TIM_OC1N); + + timer_disable_oc_output(TIM1, TIM_OC2); + timer_disable_oc_output(TIM1, TIM_OC2N); + + timer_enable_oc_output(TIM1, TIM_OC3); + timer_enable_oc_output(TIM1, TIM_OC3N); + + step++; + break; + case 4: /* A LOW, B PWM HIGH, C OFF */ + timer_set_oc_mode(TIM1, TIM_OC1, TIM_OCM_FORCE_LOW); + timer_set_oc_mode(TIM1, TIM_OC2, TIM_OCM_PWM1); + timer_set_oc_mode(TIM1, TIM_OC3, TIM_OCM_FROZEN); + + timer_enable_oc_output(TIM1, TIM_OC1); + timer_enable_oc_output(TIM1, TIM_OC1N); + + timer_enable_oc_output(TIM1, TIM_OC2); + timer_disable_oc_output(TIM1, TIM_OC2N); + + timer_disable_oc_output(TIM1, TIM_OC3); + timer_disable_oc_output(TIM1, TIM_OC3N); + + step++; + break; + case 5: /* A OFF, B HIGH, C PWM LOW */ + timer_set_oc_mode(TIM1, TIM_OC1, TIM_OCM_FROZEN); + timer_set_oc_mode(TIM1, TIM_OC2, TIM_OCM_FORCE_HIGH); + timer_set_oc_mode(TIM1, TIM_OC3, TIM_OCM_PWM1); + + timer_disable_oc_output(TIM1, TIM_OC1); + timer_disable_oc_output(TIM1, TIM_OC1N); + + timer_enable_oc_output(TIM1, TIM_OC2); + timer_enable_oc_output(TIM1, TIM_OC2N); + + timer_disable_oc_output(TIM1, TIM_OC3); + timer_enable_oc_output(TIM1, TIM_OC3N); + + step = 0; + break; + } + gpio_toggle(GPIOC, GPIO12); +} + +int main(void) +{ + clock_setup(); + gpio_setup(); + tim_setup(); + exti_setup(); + + while (1) + __asm("nop"); + + return 0; +} diff --git a/examples/stm32/f1/stm32-h103/spi/Makefile b/examples/stm32/f1/stm32-h103/spi/Makefile new file mode 100644 index 0000000..80729f7 --- /dev/null +++ b/examples/stm32/f1/stm32-h103/spi/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2010 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = spi + +LDSCRIPT = ../stm32-h103.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/stm32-h103/spi/README b/examples/stm32/f1/stm32-h103/spi/README new file mode 100644 index 0000000..d4714c2 --- /dev/null +++ b/examples/stm32/f1/stm32-h103/spi/README @@ -0,0 +1,8 @@ +------------------------------------------------------------------------------- +README +------------------------------------------------------------------------------- + +This example program repeatedly sends characters on SPI1 on the ST STM32-based +Olimex STM32-H103 eval board (see http://olimex.com/dev/stm32-h103.html +for details). + diff --git a/examples/stm32/f1/stm32-h103/spi/spi.c b/examples/stm32/f1/stm32-h103/spi/spi.c new file mode 100644 index 0000000..09344da --- /dev/null +++ b/examples/stm32/f1/stm32-h103/spi/spi.c @@ -0,0 +1,50 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include + +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); +} + +static void spi_setup(void) +{ + /* TODO */ +} + +static void gpio_setup(void) +{ + /* TODO */ +} + +int main(void) +{ + clock_setup(); + gpio_setup(); + spi_setup(); + + while (1) { + spi_write(SPI1, 0xaa); + spi_write(SPI1, 0x55); + } + + return 0; +} diff --git a/examples/stm32/f1/stm32-h103/stm32-h103.ld b/examples/stm32/f1/stm32-h103/stm32-h103.ld new file mode 100644 index 0000000..e15beca --- /dev/null +++ b/examples/stm32/f1/stm32-h103/stm32-h103.ld @@ -0,0 +1,31 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* Linker script for Olimex STM32-H103 (STM32F103RBT6, 128K flash, 20K RAM). */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_stm32f1.ld + diff --git a/examples/stm32/f1/stm32-h103/timer/Makefile b/examples/stm32/f1/stm32-h103/timer/Makefile new file mode 100644 index 0000000..b4c92cc --- /dev/null +++ b/examples/stm32/f1/stm32-h103/timer/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = timer + +LDSCRIPT = ../stm32-h103.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/stm32-h103/timer/timer.c b/examples/stm32/f1/stm32-h103/timer/timer.c new file mode 100644 index 0000000..ed157ef --- /dev/null +++ b/examples/stm32/f1/stm32-h103/timer/timer.c @@ -0,0 +1,167 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2011 Piotr Esden-Tempski + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include + +u16 frequency_sequence[18] = { + 1000, + 500, + 1000, + 500, + 1000, + 500, + 2000, + 500, + 2000, + 500, + 2000, + 500, + 1000, + 500, + 1000, + 500, + 1000, + 5000, +}; + +int frequency_sel = 0; + +u16 compare_time; +u16 new_time; +u16 frequency; +int debug = 0; + +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); +} + +static void gpio_setup(void) +{ + /* Enable GPIOC clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + /* Set GPIO12 (in GPIO port C) to 'output push-pull'. */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO12); + + gpio_set(GPIOC, GPIO12); +} + +static void tim_setup(void) +{ + /* Enable TIM2 clock. */ + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_TIM2EN); + + /* Enable TIM2 interrupt. */ + nvic_enable_irq(NVIC_TIM2_IRQ); + + /* Reset TIM2 peripheral. */ + timer_reset(TIM2); + + /* Timer global mode: + * - No divider + * - Alignment edge + * - Direction up + */ + timer_set_mode(TIM2, TIM_CR1_CKD_CK_INT, + TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); + + /* Reset prescaler value. */ + timer_set_prescaler(TIM2, 36000); + + /* Enable preload. */ + timer_disable_preload(TIM2); + + /* Continous mode. */ + timer_continuous_mode(TIM2); + + /* Period (36kHz). */ + timer_set_period(TIM2, 65535); + + /* Disable outputs. */ + timer_disable_oc_output(TIM2, TIM_OC1); + timer_disable_oc_output(TIM2, TIM_OC2); + timer_disable_oc_output(TIM2, TIM_OC3); + timer_disable_oc_output(TIM2, TIM_OC4); + + /* -- OC1 configuration -- */ + + /* Configure global mode of line 1. */ + timer_disable_oc_clear(TIM2, TIM_OC1); + timer_disable_oc_preload(TIM2, TIM_OC1); + timer_set_oc_slow_mode(TIM2, TIM_OC1); + timer_set_oc_mode(TIM2, TIM_OC1, TIM_OCM_FROZEN); + + /* Set the capture compare value for OC1. */ + timer_set_oc_value(TIM2, TIM_OC1, 1000); + + /* ---- */ + + /* ARR reload enable. */ + timer_disable_preload(TIM2); + + /* Counter enable. */ + timer_enable_counter(TIM2); + + /* Enable commutation interrupt. */ + timer_enable_irq(TIM2, TIM_DIER_CC1IE); +} + +void tim2_isr(void) +{ + if (timer_get_flag(TIM2, TIM_SR_CC1IF)) { + + /* Clear compare interrupt flag. */ + timer_clear_flag(TIM2, TIM_SR_CC1IF); + + /* + * Get current timer value to calculate next + * compare register value. + */ + compare_time = timer_get_counter(TIM2); + + /* Calculate and set the next compare value. */ + frequency = frequency_sequence[frequency_sel++]; + new_time = compare_time + frequency; + + timer_set_oc_value(TIM2, TIM_OC1, new_time); + if (frequency_sel == 18) + frequency_sel = 0; + + /* Toggle LED to indicate compare event. */ + gpio_toggle(GPIOC, GPIO12); + } +} + +int main(void) +{ + clock_setup(); + gpio_setup(); + tim_setup(); + + while (1) + __asm("nop"); + + return 0; +} diff --git a/examples/stm32/f1/stm32-h103/traceswo/Makefile b/examples/stm32/f1/stm32-h103/traceswo/Makefile new file mode 100644 index 0000000..26da42a --- /dev/null +++ b/examples/stm32/f1/stm32-h103/traceswo/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = traceswo + +LDSCRIPT = ../stm32-h103.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/stm32-h103/traceswo/README b/examples/stm32/f1/stm32-h103/traceswo/README new file mode 100644 index 0000000..6746f94 --- /dev/null +++ b/examples/stm32/f1/stm32-h103/traceswo/README @@ -0,0 +1,10 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This experimental program sends some characters on the TRACESWO pin using +the Instrumentation Trace Macrocell (ITM) and Trace Port Interface Unit (TPIU). + +The SWJ-DP port must be in SWD mode and not JTAG mode for the output +to be visible. + diff --git a/examples/stm32/f1/stm32-h103/traceswo/traceswo.c b/examples/stm32/f1/stm32-h103/traceswo/traceswo.c new file mode 100644 index 0000000..b29b9a7 --- /dev/null +++ b/examples/stm32/f1/stm32-h103/traceswo/traceswo.c @@ -0,0 +1,101 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include + +#include +#include +#include +#include + +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + + /* Enable GPIOC clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); +} + +static void trace_setup(void) +{ + /* Enable trace subsystem (we'll use ITM and TPIU). */ + SCS_DEMCR |= SCS_DEMCR_TRCENA; + + /* Use Manchester code for asynchronous transmission. */ + TPIU_SPPR = TPIU_SPPR_ASYNC_MANCHESTER; + TPIU_ACPR = 7; + + /* Data width is 1 byte. */ + TPIU_CSPSR = TPIU_CSPSR_BYTE; + + /* Formatter and flush control. */ + TPIU_FFCR &= ~TPIU_FFCR_ENFCONT; + + /* Enable TRACESWO pin for async mode. */ + DBGMCU_CR = DBGMCU_CR_TRACE_IOEN | DBGMCU_CR_TRACE_MODE_ASYNC; + + /* Unlock access to ITM registers. */ + /* FIXME: Magic numbers... Is this Cortex-M3 generic? */ + *((volatile u32 *)0xE0000FB0) = 0xC5ACCE55; + + /* Enable ITM with ID = 1. */ + ITM_TCR = (1 << 16) | ITM_TCR_ITMENA; + /* Enable stimulus port 1. */ + ITM_TER[0] = 1; +} + +static void gpio_setup(void) +{ + /* Set GPIO12 (in GPIO port C) to 'output push-pull'. */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO12); +} + +static void trace_send_blocking(char c) +{ + while (!(ITM_STIM[0] & ITM_STIM_FIFOREADY)) + ; + + ITM_STIM[0] = c; +} + +int main(void) +{ + int i, j = 0, c = 0; + + clock_setup(); + gpio_setup(); + trace_setup(); + + /* Blink the LED (PC12) on the board with every transmitted byte. */ + while (1) { + gpio_toggle(GPIOC, GPIO12); /* LED on/off */ + trace_send_blocking(c + '0'); + c = (c == 9) ? 0 : c + 1; /* Increment c. */ + if ((j++ % 80) == 0) { /* Newline after line full. */ + trace_send_blocking('\r'); + trace_send_blocking('\n'); + } + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/stm32/f1/stm32-h103/usart/Makefile b/examples/stm32/f1/stm32-h103/usart/Makefile new file mode 100644 index 0000000..d0971c7 --- /dev/null +++ b/examples/stm32/f1/stm32-h103/usart/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = usart + +LDSCRIPT = ../stm32-h103.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/stm32-h103/usart/README b/examples/stm32/f1/stm32-h103/usart/README new file mode 100644 index 0000000..ecd7269 --- /dev/null +++ b/examples/stm32/f1/stm32-h103/usart/README @@ -0,0 +1,13 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example program sends some characters on USART3 on the ST STM32-based +Olimex STM32-H103 eval board (see http://olimex.com/dev/stm32-h103.html +for details). + +The terminal settings for the receiving device/PC are 38400 8n1. + +The sending is done in a blocking way in the code, see the usart_irq example +for a more elaborate USART example. + diff --git a/examples/stm32/f1/stm32-h103/usart/usart.c b/examples/stm32/f1/stm32-h103/usart/usart.c new file mode 100644 index 0000000..846b69b --- /dev/null +++ b/examples/stm32/f1/stm32-h103/usart/usart.c @@ -0,0 +1,123 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include + +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + + /* Enable GPIOC clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, + RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN | + RCC_APB2ENR_IOPCEN); + + /* Enable clocks for GPIO port B (for GPIO_USART3_TX) and USART3. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_USART1EN); + rcc_peripheral_enable_clock(&RCC_APB1ENR, + RCC_APB1ENR_USART2EN | + RCC_APB1ENR_USART3EN); +} + +static void usart_setup(void) +{ + /* Setup GPIO pin GPIO_USART1_TX. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX); + + /* Setup UART parameters. */ + usart_set_baudrate(USART1, 38400); + usart_set_databits(USART1, 8); + usart_set_stopbits(USART1, USART_STOPBITS_1); + usart_set_mode(USART1, USART_MODE_TX); + usart_set_parity(USART1, USART_PARITY_NONE); + usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART1); + + /* Setup GPIO pin GPIO_USART2_TX. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX); + + /* Setup UART parameters. */ + usart_set_baudrate(USART2, 38400); + usart_set_databits(USART2, 8); + usart_set_stopbits(USART2, USART_STOPBITS_1); + usart_set_mode(USART2, USART_MODE_TX); + usart_set_parity(USART2, USART_PARITY_NONE); + usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART2); + + /* Setup GPIO pin GPIO_USART3_TX/GPIO10 on GPIO port B for transmit. */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART3_TX); + + /* Setup UART parameters. */ + usart_set_baudrate(USART3, 38400); + usart_set_databits(USART3, 8); + usart_set_stopbits(USART3, USART_STOPBITS_1); + usart_set_mode(USART3, USART_MODE_TX); + usart_set_parity(USART3, USART_PARITY_NONE); + usart_set_flow_control(USART3, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART3); +} + +static void gpio_setup(void) +{ + /* Set GPIO12 (in GPIO port C) to 'output push-pull'. */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO12); +} + +int main(void) +{ + int i, j = 0, c = 0; + + clock_setup(); + gpio_setup(); + usart_setup(); + + /* Blink the LED (PC12) on the board with every transmitted byte. */ + while (1) { + gpio_toggle(GPIOC, GPIO12); /* LED on/off */ + usart_send_blocking(USART1, c + '0'); /* USART1: Send byte. */ + usart_send_blocking(USART2, c + '0'); /* USART2: Send byte. */ + usart_send_blocking(USART3, c + '0'); /* USART3: Send byte. */ + c = (c == 9) ? 0 : c + 1; /* Increment c. */ + if ((j++ % 80) == 0) { /* Newline after line full. */ + usart_send_blocking(USART1, '\r'); + usart_send_blocking(USART1, '\n'); + usart_send_blocking(USART2, '\r'); + usart_send_blocking(USART2, '\n'); + usart_send_blocking(USART3, '\r'); + usart_send_blocking(USART3, '\n'); + } + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/stm32/f1/stm32-h103/usart_irq/Makefile b/examples/stm32/f1/stm32-h103/usart_irq/Makefile new file mode 100644 index 0000000..0df85d9 --- /dev/null +++ b/examples/stm32/f1/stm32-h103/usart_irq/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = usart_irq + +LDSCRIPT = ../stm32-h103.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/stm32-h103/usart_irq/usart_irq.c b/examples/stm32/f1/stm32-h103/usart_irq/usart_irq.c new file mode 100644 index 0000000..7ab4b53 --- /dev/null +++ b/examples/stm32/f1/stm32-h103/usart_irq/usart_irq.c @@ -0,0 +1,118 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include + +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + + /* Enable GPIOA clock (for LED GPIOs). */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + /* Enable clocks for GPIO port A (for GPIO_USART1_TX) and USART1. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN | + RCC_APB2ENR_AFIOEN | RCC_APB2ENR_USART1EN); +} + +static void usart_setup(void) +{ + /* Enable the USART1 interrupt. */ + nvic_enable_irq(NVIC_USART1_IRQ); + + /* Setup GPIO pin GPIO_USART1_RE_TX on GPIO port B for transmit. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX); + + /* Setup GPIO pin GPIO_USART1_RE_RX on GPIO port B for receive. */ + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, + GPIO_CNF_INPUT_FLOAT, GPIO_USART1_RX); + + /* Setup UART parameters. */ + usart_set_baudrate(USART1, 230400); + usart_set_databits(USART1, 8); + usart_set_stopbits(USART1, USART_STOPBITS_1); + usart_set_parity(USART1, USART_PARITY_NONE); + usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE); + usart_set_mode(USART1, USART_MODE_TX_RX); + + /* Enable USART1 Receive interrupt. */ + USART_CR1(USART1) |= USART_CR1_RXNEIE; + + /* Finally enable the USART. */ + usart_enable(USART1); +} + +static void gpio_setup(void) +{ + gpio_set(GPIOC, GPIO12); + + /* Setup GPIO6 and 7 (in GPIO port A) for LED use. */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO12); +} + +void usart1_isr(void) +{ + static u8 data = 'A'; + + /* Check if we were called because of RXNE. */ + if (((USART_CR1(USART1) & USART_CR1_RXNEIE) != 0) && + ((USART_SR(USART1) & USART_SR_RXNE) != 0)) { + + /* Indicate that we got data. */ + gpio_toggle(GPIOC, GPIO12); + + /* Retrieve the data from the peripheral. */ + data = usart_recv(USART1); + + /* Enable transmit interrupt so it sends back the data. */ + USART_CR1(USART1) |= USART_CR1_TXEIE; + } + + /* Check if we were called because of TXE. */ + if (((USART_CR1(USART1) & USART_CR1_TXEIE) != 0) && + ((USART_SR(USART1) & USART_SR_TXE) != 0)) { + + /* Indicate that we are sending out data. */ + // gpio_toggle(GPIOA, GPIO7); + + /* Put data into the transmit register. */ + usart_send(USART1, data); + + /* Disable the TXE interrupt as we don't need it anymore. */ + USART_CR1(USART1) &= ~USART_CR1_TXEIE; + } +} + +int main(void) +{ + clock_setup(); + gpio_setup(); + usart_setup(); + + /* Wait forever and do nothing. */ + while (1) + __asm__("nop"); + + return 0; +} diff --git a/examples/stm32/f1/stm32-h103/usart_irq_printf/Makefile b/examples/stm32/f1/stm32-h103/usart_irq_printf/Makefile new file mode 100644 index 0000000..2a14a2f --- /dev/null +++ b/examples/stm32/f1/stm32-h103/usart_irq_printf/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = usart_irq_printf + +LDSCRIPT = ../stm32-h103.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/stm32-h103/usart_irq_printf/usart_irq_printf.c b/examples/stm32/f1/stm32-h103/usart_irq_printf/usart_irq_printf.c new file mode 100644 index 0000000..e0cc1fd --- /dev/null +++ b/examples/stm32/f1/stm32-h103/usart_irq_printf/usart_irq_printf.c @@ -0,0 +1,273 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann , + * Copyright (C) 2011 Piotr Esden-Tempski + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include + +/****************************************************************************** + * Simple ringbuffer implementation from open-bldc's libgovernor that + * you can find at: + * https://github.com/open-bldc/open-bldc/tree/master/source/libgovernor + *****************************************************************************/ + +typedef s32 ring_size_t; + +struct ring { + u8 *data; + ring_size_t size; + u32 begin; + u32 end; +}; + +#define RING_SIZE(RING) ((RING)->size - 1) +#define RING_DATA(RING) (RING)->data +#define RING_EMPTY(RING) ((RING)->begin == (RING)->end) + +static void ring_init(struct ring *ring, u8 *buf, ring_size_t size) +{ + ring->data = buf; + ring->size = size; + ring->begin = 0; + ring->end = 0; +} + +static s32 ring_write_ch(struct ring *ring, u8 ch) +{ + if (((ring->end + 1) % ring->size) != ring->begin) { + ring->data[ring->end++] = ch; + ring->end %= ring->size; + return (u32)ch; + } + + return -1; +} + +static s32 ring_write(struct ring *ring, u8 *data, ring_size_t size) +{ + s32 i; + + for (i = 0; i < size; i++) { + if (ring_write_ch(ring, data[i]) < 0) + return -i; + } + + return i; +} + +static s32 ring_read_ch(struct ring *ring, u8 *ch) +{ + s32 ret = -1; + + if (ring->begin != ring->end) { + ret = ring->data[ring->begin++]; + ring->begin %= ring->size; + if (ch) + *ch = ret; + } + + return ret; +} + +/* Not used! +static s32 ring_read(struct ring *ring, u8 *data, ring_size_t size) +{ + s32 i; + + for (i = 0; i < size; i++) { + if (ring_read_ch(ring, data + i) < 0) + return i; + } + + return -i; +} +*/ + +/****************************************************************************** + * The example implementation + *****************************************************************************/ + +#define BUFFER_SIZE 1024 + +struct ring output_ring; +u8 output_ring_buffer[BUFFER_SIZE]; + +int _write(int file, char *ptr, int len); + +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + + /* Enable GPIOA clock (for LED GPIOs). */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + /* Enable clocks for GPIO port A (for GPIO_USART1_TX) and USART1. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN | + RCC_APB2ENR_AFIOEN | RCC_APB2ENR_USART1EN); +} + +static void usart_setup(void) +{ + /* Initialize output ring buffer. */ + ring_init(&output_ring, output_ring_buffer, BUFFER_SIZE); + + /* Enable the USART1 interrupt. */ + nvic_enable_irq(NVIC_USART1_IRQ); + + /* Setup GPIO pin GPIO_USART1_RE_TX on GPIO port B for transmit. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX); + + /* Setup GPIO pin GPIO_USART1_RE_RX on GPIO port B for receive. */ + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, + GPIO_CNF_INPUT_FLOAT, GPIO_USART1_RX); + + /* Setup UART parameters. */ + usart_set_baudrate(USART1, 230400); + usart_set_databits(USART1, 8); + usart_set_stopbits(USART1, USART_STOPBITS_1); + usart_set_parity(USART1, USART_PARITY_NONE); + usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE); + usart_set_mode(USART1, USART_MODE_TX_RX); + + /* Enable USART1 Receive interrupt. */ + USART_CR1(USART1) |= USART_CR1_RXNEIE; + + /* Finally enable the USART. */ + usart_enable(USART1); +} + +static void gpio_setup(void) +{ + gpio_set(GPIOC, GPIO12); + + /* Setup GPIO6 and 7 (in GPIO port A) for LED use. */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO12); +} + +void usart1_isr(void) +{ + /* Check if we were called because of RXNE. */ + if (((USART_CR1(USART1) & USART_CR1_RXNEIE) != 0) && + ((USART_SR(USART1) & USART_SR_RXNE) != 0)) { + + /* Indicate that we got data. */ + gpio_toggle(GPIOC, GPIO12); + + /* Retrieve the data from the peripheral. */ + ring_write_ch(&output_ring, usart_recv(USART1)); + + /* Enable transmit interrupt so it sends back the data. */ + USART_CR1(USART1) |= USART_CR1_TXEIE; + } + + /* Check if we were called because of TXE. */ + if (((USART_CR1(USART1) & USART_CR1_TXEIE) != 0) && + ((USART_SR(USART1) & USART_SR_TXE) != 0)) { + + s32 data; + + data = ring_read_ch(&output_ring, NULL); + + if (data == -1) { + /* Disable the TXE interrupt, it's no longer needed. */ + USART_CR1(USART1) &= ~USART_CR1_TXEIE; + } else { + /* Put data into the transmit register. */ + usart_send(USART1, data); + } + } +} + +int _write(int file, char *ptr, int len) +{ + int ret; + + if (file == 1) { + ret = ring_write(&output_ring, (u8 *)ptr, len); + + if (ret < 0) + ret = -ret; + + USART_CR1(USART1) |= USART_CR1_TXEIE; + + return ret; + } + + errno = EIO; + return -1; +} + +static void systick_setup(void) +{ + /* 72MHz / 8 => 9000000 counts per second. */ + systick_set_clocksource(STK_CTRL_CLKSOURCE_AHB_DIV8); + + /* 9000000/9000 = 1000 overflows per second - every 1ms one interrupt */ + /* SysTick interrupt every N clock pulses: set reload to N-1 */ + systick_set_reload(8999); + + systick_interrupt_enable(); + + /* Start counting. */ + systick_counter_enable(); +} + +void sys_tick_handler(void) +{ + static int counter = 0; + static float fcounter = 0.0; + static double dcounter = 0.0; + static u32 temp32 = 0; + + temp32++; + + /* + * We call this handler every 1ms so we are sending hello world + * every 10ms / 100Hz. + */ + if (temp32 == 10) { + printf("Hello World! %i %f %f\r\n", counter, fcounter, + dcounter); + counter++; + fcounter += 0.01; + dcounter += 0.01; + + temp32 = 0; + } +} + +int main(void) +{ + clock_setup(); + gpio_setup(); + usart_setup(); + systick_setup(); + + while (1) + __asm__("nop"); + + return 0; +} diff --git a/examples/stm32/f1/stm32-h103/usart_printf/Makefile b/examples/stm32/f1/stm32-h103/usart_printf/Makefile new file mode 100644 index 0000000..216e57f --- /dev/null +++ b/examples/stm32/f1/stm32-h103/usart_printf/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = usart_printf + +LDSCRIPT = ../stm32-h103.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/stm32-h103/usart_printf/usart_printf.c b/examples/stm32/f1/stm32-h103/usart_printf/usart_printf.c new file mode 100644 index 0000000..40ac7a5 --- /dev/null +++ b/examples/stm32/f1/stm32-h103/usart_printf/usart_printf.c @@ -0,0 +1,107 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann , + * Copyright (C) 2011 Piotr Esden-Tempski + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include + +int _write(int file, char *ptr, int len); + +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + + /* Enable GPIOA clock (for LED GPIOs). */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + /* Enable clocks for GPIO port A (for GPIO_USART1_TX) and USART1. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN | + RCC_APB2ENR_AFIOEN | RCC_APB2ENR_USART1EN); +} + +static void usart_setup(void) +{ + /* Setup GPIO pin GPIO_USART1_RE_TX on GPIO port B for transmit. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX); + + /* Setup UART parameters. */ + usart_set_baudrate(USART1, 230400); + usart_set_databits(USART1, 8); + usart_set_stopbits(USART1, USART_STOPBITS_1); + usart_set_parity(USART1, USART_PARITY_NONE); + usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE); + usart_set_mode(USART1, USART_MODE_TX); + + /* Finally enable the USART. */ + usart_enable(USART1); +} + +static void gpio_setup(void) +{ + gpio_set(GPIOC, GPIO12); + + /* Setup GPIO6 and 7 (in GPIO port A) for LED use. */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO12); +} + +int _write(int file, char *ptr, int len) +{ + int i; + + if (file == 1) { + for (i = 0; i < len; i++) + usart_send_blocking(USART1, ptr[i]); + return i; + } + + errno = EIO; + return -1; +} + +int main(void) +{ + int counter = 0; + float fcounter = 0.0; + double dcounter = 0.0; + + clock_setup(); + gpio_setup(); + usart_setup(); + + /* + * Write Hello World, an integer, float and double all over + * again while incrementing the numbers. + */ + while (1) { + gpio_toggle(GPIOC, GPIO12); + printf("Hello World! %i %f %f\r\n", counter, fcounter, + dcounter); + counter++; + fcounter += 0.01; + dcounter += 0.01; + } + + return 0; +} diff --git a/examples/stm32/f1/stm32-h103/usb_cdcacm/Makefile b/examples/stm32/f1/stm32-h103/usb_cdcacm/Makefile new file mode 100644 index 0000000..0ced4f8 --- /dev/null +++ b/examples/stm32/f1/stm32-h103/usb_cdcacm/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = cdcacm + +LDSCRIPT = ../stm32-h103.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/stm32-h103/usb_cdcacm/README b/examples/stm32/f1/stm32-h103/usb_cdcacm/README new file mode 100644 index 0000000..2f1ee4b --- /dev/null +++ b/examples/stm32/f1/stm32-h103/usb_cdcacm/README @@ -0,0 +1,7 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example implements a USB CDC-ACM device (aka Virtual Serial Port) +to demonstrate the use of the USB device stack. + diff --git a/examples/stm32/f1/stm32-h103/usb_cdcacm/cdcacm.c b/examples/stm32/f1/stm32-h103/usb_cdcacm/cdcacm.c new file mode 100644 index 0000000..3ed519f --- /dev/null +++ b/examples/stm32/f1/stm32-h103/usb_cdcacm/cdcacm.c @@ -0,0 +1,255 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Gareth McMullin + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include + +static const struct usb_device_descriptor dev = { + .bLength = USB_DT_DEVICE_SIZE, + .bDescriptorType = USB_DT_DEVICE, + .bcdUSB = 0x0200, + .bDeviceClass = USB_CLASS_CDC, + .bDeviceSubClass = 0, + .bDeviceProtocol = 0, + .bMaxPacketSize0 = 64, + .idVendor = 0x0483, + .idProduct = 0x5740, + .bcdDevice = 0x0200, + .iManufacturer = 1, + .iProduct = 2, + .iSerialNumber = 3, + .bNumConfigurations = 1, +}; + +/* + * This notification endpoint isn't implemented. According to CDC spec its + * optional, but its absence causes a NULL pointer dereference in Linux + * cdc_acm driver. + */ +static const struct usb_endpoint_descriptor comm_endp[] = {{ + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x83, + .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, + .wMaxPacketSize = 16, + .bInterval = 255, +}}; + +static const struct usb_endpoint_descriptor data_endp[] = {{ + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x01, + .bmAttributes = USB_ENDPOINT_ATTR_BULK, + .wMaxPacketSize = 64, + .bInterval = 1, +}, { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x82, + .bmAttributes = USB_ENDPOINT_ATTR_BULK, + .wMaxPacketSize = 64, + .bInterval = 1, +}}; + +static const struct { + struct usb_cdc_header_descriptor header; + struct usb_cdc_call_management_descriptor call_mgmt; + struct usb_cdc_acm_descriptor acm; + struct usb_cdc_union_descriptor cdc_union; +} __attribute__((packed)) cdcacm_functional_descriptors = { + .header = { + .bFunctionLength = sizeof(struct usb_cdc_header_descriptor), + .bDescriptorType = CS_INTERFACE, + .bDescriptorSubtype = USB_CDC_TYPE_HEADER, + .bcdCDC = 0x0110, + }, + .call_mgmt = { + .bFunctionLength = + sizeof(struct usb_cdc_call_management_descriptor), + .bDescriptorType = CS_INTERFACE, + .bDescriptorSubtype = USB_CDC_TYPE_CALL_MANAGEMENT, + .bmCapabilities = 0, + .bDataInterface = 1, + }, + .acm = { + .bFunctionLength = sizeof(struct usb_cdc_acm_descriptor), + .bDescriptorType = CS_INTERFACE, + .bDescriptorSubtype = USB_CDC_TYPE_ACM, + .bmCapabilities = 0, + }, + .cdc_union = { + .bFunctionLength = sizeof(struct usb_cdc_union_descriptor), + .bDescriptorType = CS_INTERFACE, + .bDescriptorSubtype = USB_CDC_TYPE_UNION, + .bControlInterface = 0, + .bSubordinateInterface0 = 1, + }, +}; + +static const struct usb_interface_descriptor comm_iface[] = {{ + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 0, + .bAlternateSetting = 0, + .bNumEndpoints = 1, + .bInterfaceClass = USB_CLASS_CDC, + .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM, + .bInterfaceProtocol = USB_CDC_PROTOCOL_AT, + .iInterface = 0, + + .endpoint = comm_endp, + + .extra = &cdcacm_functional_descriptors, + .extralen = sizeof(cdcacm_functional_descriptors), +}}; + +static const struct usb_interface_descriptor data_iface[] = {{ + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 1, + .bAlternateSetting = 0, + .bNumEndpoints = 2, + .bInterfaceClass = USB_CLASS_DATA, + .bInterfaceSubClass = 0, + .bInterfaceProtocol = 0, + .iInterface = 0, + + .endpoint = data_endp, +}}; + +static const struct usb_interface ifaces[] = {{ + .num_altsetting = 1, + .altsetting = comm_iface, +}, { + .num_altsetting = 1, + .altsetting = data_iface, +}}; + +static const struct usb_config_descriptor config = { + .bLength = USB_DT_CONFIGURATION_SIZE, + .bDescriptorType = USB_DT_CONFIGURATION, + .wTotalLength = 0, + .bNumInterfaces = 2, + .bConfigurationValue = 1, + .iConfiguration = 0, + .bmAttributes = 0x80, + .bMaxPower = 0x32, + + .interface = ifaces, +}; + +static const char *usb_strings[] = { + "Black Sphere Technologies", + "CDC-ACM Demo", + "DEMO", +}; + +static int cdcacm_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, u8 **buf, + u16 *len, void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req)) +{ + (void)complete; + (void)buf; + (void)usbd_dev; + + switch (req->bRequest) { + case USB_CDC_REQ_SET_CONTROL_LINE_STATE: { + /* + * This Linux cdc_acm driver requires this to be implemented + * even though it's optional in the CDC spec, and we don't + * advertise it in the ACM functional descriptor. + */ + char local_buf[10]; + struct usb_cdc_notification *notif = (void *)local_buf; + + /* We echo signals back to host as notification. */ + notif->bmRequestType = 0xA1; + notif->bNotification = USB_CDC_NOTIFY_SERIAL_STATE; + notif->wValue = 0; + notif->wIndex = 0; + notif->wLength = 2; + local_buf[8] = req->wValue & 3; + local_buf[9] = 0; + // usbd_ep_write_packet(0x83, buf, 10); + return 1; + } + case USB_CDC_REQ_SET_LINE_CODING: + if (*len < sizeof(struct usb_cdc_line_coding)) + return 0; + return 1; + } + return 0; +} + +static void cdcacm_data_rx_cb(usbd_device *usbd_dev, u8 ep) +{ + (void)ep; + (void)usbd_dev; + + char buf[64]; + int len = usbd_ep_read_packet(usbd_dev, 0x01, buf, 64); + + if (len) { + usbd_ep_write_packet(usbd_dev, 0x82, buf, len); + buf[len] = 0; + } +} + +static void cdcacm_set_config(usbd_device *usbd_dev, u16 wValue) +{ + (void)wValue; + (void)usbd_dev; + + usbd_ep_setup(usbd_dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64, cdcacm_data_rx_cb); + usbd_ep_setup(usbd_dev, 0x82, USB_ENDPOINT_ATTR_BULK, 64, NULL); + usbd_ep_setup(usbd_dev, 0x83, USB_ENDPOINT_ATTR_INTERRUPT, 16, NULL); + + usbd_register_control_callback( + usbd_dev, + USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE, + USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, + cdcacm_control_request); +} + +int main(void) +{ + int i; + + usbd_device *usbd_dev; + + rcc_clock_setup_in_hsi_out_48mhz(); + + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + gpio_set(GPIOC, GPIO11); + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO11); + + usbd_dev = usbd_init(&stm32f103_usb_driver, &dev, &config, usb_strings, 3); + usbd_register_set_config_callback(usbd_dev, cdcacm_set_config); + + for (i = 0; i < 0x800000; i++) + __asm__("nop"); + gpio_clear(GPIOC, GPIO11); + + while (1) + usbd_poll(usbd_dev); +} diff --git a/examples/stm32/f1/stm32-h103/usb_dfu/Makefile b/examples/stm32/f1/stm32-h103/usb_dfu/Makefile new file mode 100644 index 0000000..cc690fe --- /dev/null +++ b/examples/stm32/f1/stm32-h103/usb_dfu/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = usbdfu + +LDSCRIPT = ../stm32-h103.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/stm32-h103/usb_dfu/README b/examples/stm32/f1/stm32-h103/usb_dfu/README new file mode 100644 index 0000000..9c0169a --- /dev/null +++ b/examples/stm32/f1/stm32-h103/usb_dfu/README @@ -0,0 +1,7 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example implements a USB Device Firmware Upgrade (DFU) bootloader +to demonstrate the use of the USB device stack. + diff --git a/examples/stm32/f1/stm32-h103/usb_dfu/usbdfu.c b/examples/stm32/f1/stm32-h103/usb_dfu/usbdfu.c new file mode 100644 index 0000000..20853c1 --- /dev/null +++ b/examples/stm32/f1/stm32-h103/usb_dfu/usbdfu.c @@ -0,0 +1,271 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Gareth McMullin + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include + +#define APP_ADDRESS 0x08002000 + +/* Commands sent with wBlockNum == 0 as per ST implementation. */ +#define CMD_SETADDR 0x21 +#define CMD_ERASE 0x41 + +/* We need a special large control buffer for this device: */ +u8 usbd_control_buffer[1024]; + +static enum dfu_state usbdfu_state = STATE_DFU_IDLE; + +static struct { + u8 buf[sizeof(usbd_control_buffer)]; + u16 len; + u32 addr; + u16 blocknum; +} prog; + +const struct usb_device_descriptor dev = { + .bLength = USB_DT_DEVICE_SIZE, + .bDescriptorType = USB_DT_DEVICE, + .bcdUSB = 0x0200, + .bDeviceClass = 0, + .bDeviceSubClass = 0, + .bDeviceProtocol = 0, + .bMaxPacketSize0 = 64, + .idVendor = 0x0483, + .idProduct = 0xDF11, + .bcdDevice = 0x0200, + .iManufacturer = 1, + .iProduct = 2, + .iSerialNumber = 3, + .bNumConfigurations = 1, +}; + +const struct usb_dfu_descriptor dfu_function = { + .bLength = sizeof(struct usb_dfu_descriptor), + .bDescriptorType = DFU_FUNCTIONAL, + .bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH, + .wDetachTimeout = 255, + .wTransferSize = 1024, + .bcdDFUVersion = 0x011A, +}; + +const struct usb_interface_descriptor iface = { + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 0, + .bAlternateSetting = 0, + .bNumEndpoints = 0, + .bInterfaceClass = 0xFE, /* Device Firmware Upgrade */ + .bInterfaceSubClass = 1, + .bInterfaceProtocol = 2, + + /* The ST Microelectronics DfuSe application needs this string. + * The format isn't documented... */ + .iInterface = 4, + + .extra = &dfu_function, + .extralen = sizeof(dfu_function), +}; + +const struct usb_interface ifaces[] = {{ + .num_altsetting = 1, + .altsetting = &iface, +}}; + +const struct usb_config_descriptor config = { + .bLength = USB_DT_CONFIGURATION_SIZE, + .bDescriptorType = USB_DT_CONFIGURATION, + .wTotalLength = 0, + .bNumInterfaces = 1, + .bConfigurationValue = 1, + .iConfiguration = 0, + .bmAttributes = 0xC0, + .bMaxPower = 0x32, + + .interface = ifaces, +}; + +static const char *usb_strings[] = { + "Black Sphere Technologies", + "DFU Demo", + "DEMO", + /* This string is used by ST Microelectronics' DfuSe utility. */ + "@Internal Flash /0x08000000/8*001Ka,56*001Kg", +}; + +static u8 usbdfu_getstatus(usbd_device *usbd_dev, u32 *bwPollTimeout) +{ + (void)usbd_dev; + + switch (usbdfu_state) { + case STATE_DFU_DNLOAD_SYNC: + usbdfu_state = STATE_DFU_DNBUSY; + *bwPollTimeout = 100; + return DFU_STATUS_OK; + case STATE_DFU_MANIFEST_SYNC: + /* Device will reset when read is complete. */ + usbdfu_state = STATE_DFU_MANIFEST; + return DFU_STATUS_OK; + default: + return DFU_STATUS_OK; + } +} + +static void usbdfu_getstatus_complete(usbd_device *usbd_dev, struct usb_setup_data *req) +{ + int i; + (void)req; + (void)usbd_dev; + + switch (usbdfu_state) { + case STATE_DFU_DNBUSY: + flash_unlock(); + if (prog.blocknum == 0) { + switch (prog.buf[0]) { + case CMD_ERASE: + { + u32 *dat = (u32 *)(prog.buf + 1); + flash_erase_page(*dat); + } + case CMD_SETADDR: + { + u32 *dat = (u32 *)(prog.buf + 1); + prog.addr = *dat; + } + } + } else { + u32 baseaddr = prog.addr + ((prog.blocknum - 2) * + dfu_function.wTransferSize); + for (i = 0; i < prog.len; i += 2) { + u16 *dat = (u16 *)(prog.buf + i); + flash_program_half_word(baseaddr + i, + *dat); + } + } + flash_lock(); + + /* Jump straight to dfuDNLOAD-IDLE, skipping dfuDNLOAD-SYNC. */ + usbdfu_state = STATE_DFU_DNLOAD_IDLE; + return; + case STATE_DFU_MANIFEST: + /* USB device must detach, we just reset... */ + scb_reset_system(); + return; /* Will never return. */ + default: + return; + } +} + +static int usbdfu_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, u8 **buf, + u16 *len, void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req)) +{ + if ((req->bmRequestType & 0x7F) != 0x21) + return 0; /* Only accept class request. */ + + switch (req->bRequest) { + case DFU_DNLOAD: + if ((len == NULL) || (*len == 0)) { + usbdfu_state = STATE_DFU_MANIFEST_SYNC; + return 1; + } else { + /* Copy download data for use on GET_STATUS. */ + prog.blocknum = req->wValue; + prog.len = *len; + memcpy(prog.buf, *buf, *len); + usbdfu_state = STATE_DFU_DNLOAD_SYNC; + return 1; + } + case DFU_CLRSTATUS: + /* Clear error and return to dfuIDLE. */ + if (usbdfu_state == STATE_DFU_ERROR) + usbdfu_state = STATE_DFU_IDLE; + return 1; + case DFU_ABORT: + /* Abort returns to dfuIDLE state. */ + usbdfu_state = STATE_DFU_IDLE; + return 1; + case DFU_UPLOAD: + /* Upload not supported for now. */ + return 0; + case DFU_GETSTATUS: { + u32 bwPollTimeout = 0; /* 24-bit integer in DFU class spec */ + (*buf)[0] = usbdfu_getstatus(usbd_dev, &bwPollTimeout); + (*buf)[1] = bwPollTimeout & 0xFF; + (*buf)[2] = (bwPollTimeout >> 8) & 0xFF; + (*buf)[3] = (bwPollTimeout >> 16) & 0xFF; + (*buf)[4] = usbdfu_state; + (*buf)[5] = 0; /* iString not used here */ + *len = 6; + *complete = usbdfu_getstatus_complete; + return 1; + } + case DFU_GETSTATE: + /* Return state with no state transision. */ + *buf[0] = usbdfu_state; + *len = 1; + return 1; + } + + return 0; +} + +int main(void) +{ + usbd_device *usbd_dev; + + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + + if (!gpio_get(GPIOA, GPIO10)) { + /* Boot the application if it's valid. */ + if ((*(volatile u32 *)APP_ADDRESS & 0x2FFE0000) == 0x20000000) { + /* Set vector table base address. */ + SCB_VTOR = APP_ADDRESS & 0xFFFF; + /* Initialise master stack pointer. */ + asm volatile("msr msp, %0"::"g" + (*(volatile u32 *)APP_ADDRESS)); + /* Jump to application. */ + (*(void (**)())(APP_ADDRESS + 4))(); + } + } + + rcc_clock_setup_in_hsi_out_48mhz(); + + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO11); + gpio_set(GPIOC, GPIO11); + + usbd_dev = usbd_init(&stm32f103_usb_driver, &dev, &config, usb_strings, 4); + usbd_set_control_buffer_size(usbd_dev, sizeof(usbd_control_buffer)); + usbd_register_control_callback( + usbd_dev, + USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE, + USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, + usbdfu_control_request); + + gpio_clear(GPIOC, GPIO11); + + while (1) + usbd_poll(usbd_dev); +} diff --git a/examples/stm32/f1/stm32-h103/usb_hid/Makefile b/examples/stm32/f1/stm32-h103/usb_hid/Makefile new file mode 100644 index 0000000..fe73c62 --- /dev/null +++ b/examples/stm32/f1/stm32-h103/usb_hid/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = usbhid + +LDSCRIPT = ../stm32-h103.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/stm32-h103/usb_hid/README b/examples/stm32/f1/stm32-h103/usb_hid/README new file mode 100644 index 0000000..48a9b39 --- /dev/null +++ b/examples/stm32/f1/stm32-h103/usb_hid/README @@ -0,0 +1,7 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example implements a USB Human Interface Device (HID) +to demonstrate the use of the USB device stack. + diff --git a/examples/stm32/f1/stm32-h103/usb_hid/usbhid.c b/examples/stm32/f1/stm32-h103/usb_hid/usbhid.c new file mode 100644 index 0000000..eff800e --- /dev/null +++ b/examples/stm32/f1/stm32-h103/usb_hid/usbhid.c @@ -0,0 +1,287 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Gareth McMullin + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include + +/* Define this to include the DFU APP interface. */ +#define INCLUDE_DFU_INTERFACE + +#ifdef INCLUDE_DFU_INTERFACE +#include +#include +#endif + +const struct usb_device_descriptor dev = { + .bLength = USB_DT_DEVICE_SIZE, + .bDescriptorType = USB_DT_DEVICE, + .bcdUSB = 0x0200, + .bDeviceClass = 0, + .bDeviceSubClass = 0, + .bDeviceProtocol = 0, + .bMaxPacketSize0 = 64, + .idVendor = 0x0483, + .idProduct = 0x5710, + .bcdDevice = 0x0200, + .iManufacturer = 1, + .iProduct = 2, + .iSerialNumber = 3, + .bNumConfigurations = 1, +}; + +/* I have no idea what this means. I haven't read the HID spec. */ +static const u8 hid_report_descriptor[] = { + 0x05, 0x01, 0x09, 0x02, 0xA1, 0x01, 0x09, 0x01, + 0xA1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x03, + 0x15, 0x00, 0x25, 0x01, 0x95, 0x03, 0x75, 0x01, + 0x81, 0x02, 0x95, 0x01, 0x75, 0x05, 0x81, 0x01, + 0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 0x09, 0x38, + 0x15, 0x81, 0x25, 0x7F, 0x75, 0x08, 0x95, 0x03, + 0x81, 0x06, 0xC0, 0x09, 0x3c, 0x05, 0xff, 0x09, + 0x01, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95, + 0x02, 0xb1, 0x22, 0x75, 0x06, 0x95, 0x01, 0xb1, + 0x01, 0xc0, +}; + +static const struct { + struct usb_hid_descriptor hid_descriptor; + struct { + u8 bReportDescriptorType; + u16 wDescriptorLength; + } __attribute__((packed)) hid_report; +} __attribute__((packed)) hid_function = { + .hid_descriptor = { + .bLength = sizeof(hid_function), + .bDescriptorType = USB_DT_HID, + .bcdHID = 0x0100, + .bCountryCode = 0, + .bNumDescriptors = 1, + }, + .hid_report = { + .bReportDescriptorType = USB_DT_REPORT, + .wDescriptorLength = sizeof(hid_report_descriptor), + }, +}; + +const struct usb_endpoint_descriptor hid_endpoint = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x81, + .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, + .wMaxPacketSize = 4, + .bInterval = 0x20, +}; + +const struct usb_interface_descriptor hid_iface = { + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 0, + .bAlternateSetting = 0, + .bNumEndpoints = 1, + .bInterfaceClass = USB_CLASS_HID, + .bInterfaceSubClass = 1, /* boot */ + .bInterfaceProtocol = 2, /* mouse */ + .iInterface = 0, + + .endpoint = &hid_endpoint, + + .extra = &hid_function, + .extralen = sizeof(hid_function), +}; + +#ifdef INCLUDE_DFU_INTERFACE +const struct usb_dfu_descriptor dfu_function = { + .bLength = sizeof(struct usb_dfu_descriptor), + .bDescriptorType = DFU_FUNCTIONAL, + .bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH, + .wDetachTimeout = 255, + .wTransferSize = 1024, + .bcdDFUVersion = 0x011A, +}; + +const struct usb_interface_descriptor dfu_iface = { + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 1, + .bAlternateSetting = 0, + .bNumEndpoints = 0, + .bInterfaceClass = 0xFE, + .bInterfaceSubClass = 1, + .bInterfaceProtocol = 1, + .iInterface = 0, + + .extra = &dfu_function, + .extralen = sizeof(dfu_function), +}; +#endif + +const struct usb_interface ifaces[] = {{ + .num_altsetting = 1, + .altsetting = &hid_iface, +#ifdef INCLUDE_DFU_INTERFACE +}, { + .num_altsetting = 1, + .altsetting = &dfu_iface, +#endif +}}; + +const struct usb_config_descriptor config = { + .bLength = USB_DT_CONFIGURATION_SIZE, + .bDescriptorType = USB_DT_CONFIGURATION, + .wTotalLength = 0, +#ifdef INCLUDE_DFU_INTERFACE + .bNumInterfaces = 2, +#else + .bNumInterfaces = 1, +#endif + .bConfigurationValue = 1, + .iConfiguration = 0, + .bmAttributes = 0xC0, + .bMaxPower = 0x32, + + .interface = ifaces, +}; + +static const char *usb_strings[] = { + "Black Sphere Technologies", + "HID Demo", + "DEMO", +}; + +static int hid_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, u8 **buf, u16 *len, + void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req)) +{ + (void)complete; + (void)usbd_dev; + + if ((req->bmRequestType != 0x81) || + (req->bRequest != USB_REQ_GET_DESCRIPTOR) || + (req->wValue != 0x2200)) + return 0; + + /* Handle the HID report descriptor. */ + *buf = (u8 *)hid_report_descriptor; + *len = sizeof(hid_report_descriptor); + + return 1; +} + +#ifdef INCLUDE_DFU_INTERFACE +static void dfu_detach_complete(usbd_device *usbd_dev, struct usb_setup_data *req) +{ + (void)req; + (void)usbd_dev; + + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, 0, GPIO15); + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO10); + gpio_set(GPIOA, GPIO10); + scb_reset_core(); +} + +static int dfu_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, u8 **buf, u16 *len, + void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req)) +{ + (void)buf; + (void)len; + (void)usbd_dev; + + if ((req->bmRequestType != 0x21) || (req->bRequest != DFU_DETACH)) + return 0; /* Only accept class request. */ + + *complete = dfu_detach_complete; + + return 1; +} +#endif + +static void hid_set_config(usbd_device *usbd_dev, u16 wValue) +{ + (void)wValue; + (void)usbd_dev; + + usbd_ep_setup(usbd_dev, 0x81, USB_ENDPOINT_ATTR_INTERRUPT, 4, NULL); + + usbd_register_control_callback( + usbd_dev, + USB_REQ_TYPE_STANDARD | USB_REQ_TYPE_INTERFACE, + USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, + hid_control_request); +#ifdef INCLUDE_DFU_INTERFACE + usbd_register_control_callback( + usbd_dev, + USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE, + USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, + dfu_control_request); +#endif + + systick_set_clocksource(STK_CTRL_CLKSOURCE_AHB_DIV8); + /* SysTick interrupt every N clock pulses: set reload to N-1 */ + systick_set_reload(99999); + systick_interrupt_enable(); + systick_counter_enable(); +} + +int main(void) +{ + int i; + + usbd_device *usbd_dev; + + rcc_clock_setup_in_hsi_out_48mhz(); + + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + gpio_set(GPIOC, GPIO11); + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO11); + + usbd_dev = usbd_init(&stm32f103_usb_driver, &dev, &config, usb_strings, 3); + usbd_register_set_config_callback(usbd_dev, hid_set_config); + + for (i = 0; i < 0x80000; i++) + __asm__("nop"); + + gpio_clear(GPIOC, GPIO11); + + while (1) + usbd_poll(usbd_dev); +} + +#if 0 /* is this used? */ +void sys_tick_handler(void) +{ + static int x = 0; + static int dir = 1; + u8 buf[4] = {0, 0, 0, 0}; + + buf[1] = dir; + x += dir; + if (x > 30) + dir = -dir; + if (x < -30) + dir = -dir; + + usbd_ep_write_packet(usbd_dev, 0x81, buf, 4); +} +#endif diff --git a/examples/stm32/f1/stm32-h103/usb_iap/Makefile b/examples/stm32/f1/stm32-h103/usb_iap/Makefile new file mode 100644 index 0000000..eacc672 --- /dev/null +++ b/examples/stm32/f1/stm32-h103/usb_iap/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = usbiap + +LDSCRIPT = ../stm32-h103.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/stm32-h103/usb_iap/README b/examples/stm32/f1/stm32-h103/usb_iap/README new file mode 100644 index 0000000..2966684 --- /dev/null +++ b/examples/stm32/f1/stm32-h103/usb_iap/README @@ -0,0 +1,8 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example implements a USB bootloader for the Paparazzi project. + +TODO: Move to examples/lisa-m? + diff --git a/examples/stm32/f1/stm32-h103/usb_iap/usbiap.c b/examples/stm32/f1/stm32-h103/usb_iap/usbiap.c new file mode 100644 index 0000000..fb3bdb9 --- /dev/null +++ b/examples/stm32/f1/stm32-h103/usb_iap/usbiap.c @@ -0,0 +1,273 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Gareth McMullin + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include + +#define APP_ADDRESS 0x08002000 + +/* Commands sent with wBlockNum == 0 as per ST implementation. */ +#define CMD_SETADDR 0x21 +#define CMD_ERASE 0x41 + +/* We need a special large control buffer for this device: */ +u8 usbd_control_buffer[1024]; + +static enum dfu_state usbdfu_state = STATE_DFU_IDLE; + +static struct { + u8 buf[sizeof(usbd_control_buffer)]; + u16 len; + u32 addr; + u16 blocknum; +} prog; + +const struct usb_device_descriptor dev = { + .bLength = USB_DT_DEVICE_SIZE, + .bDescriptorType = USB_DT_DEVICE, + .bcdUSB = 0x0200, + .bDeviceClass = 0, + .bDeviceSubClass = 0, + .bDeviceProtocol = 0, + .bMaxPacketSize0 = 64, + .idVendor = 0x0483, + .idProduct = 0xDF11, + .bcdDevice = 0x0200, + .iManufacturer = 1, + .iProduct = 2, + .iSerialNumber = 3, + .bNumConfigurations = 1, +}; + +const struct usb_dfu_descriptor dfu_function = { + .bLength = sizeof(struct usb_dfu_descriptor), + .bDescriptorType = DFU_FUNCTIONAL, + .bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH, + .wDetachTimeout = 255, + .wTransferSize = 1024, + .bcdDFUVersion = 0x011A, +}; + +const struct usb_interface_descriptor iface = { + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 0, + .bAlternateSetting = 0, + .bNumEndpoints = 0, + .bInterfaceClass = 0xFE, /* Device Firmware Upgrade */ + .bInterfaceSubClass = 1, + .bInterfaceProtocol = 2, + + /* The ST Microelectronics DfuSe application needs this string. + * The format isn't documented... */ + .iInterface = 4, + + .extra = &dfu_function, + .extralen = sizeof(dfu_function), +}; + +const struct usb_interface ifaces[] = {{ + .num_altsetting = 1, + .altsetting = &iface, +}}; + +const struct usb_config_descriptor config = { + .bLength = USB_DT_CONFIGURATION_SIZE, + .bDescriptorType = USB_DT_CONFIGURATION, + .wTotalLength = 0, + .bNumInterfaces = 1, + .bConfigurationValue = 1, + .iConfiguration = 0, + .bmAttributes = 0xC0, + .bMaxPower = 0x32, + + .interface = ifaces, +}; + +static const char *usb_strings[] = { + "Black Sphere Technologies", + "DFU Demo", + "DEMO", + /* This string is used by ST Microelectronics' DfuSe utility. */ + "@Internal Flash /0x08000000/8*001Ka,56*001Kg", +}; + +static u8 usbdfu_getstatus(usbd_device *usbd_dev, u32 *bwPollTimeout) +{ + (void)usbd_dev; + + switch (usbdfu_state) { + case STATE_DFU_DNLOAD_SYNC: + usbdfu_state = STATE_DFU_DNBUSY; + *bwPollTimeout = 100; + return DFU_STATUS_OK; + case STATE_DFU_MANIFEST_SYNC: + /* Device will reset when read is complete. */ + usbdfu_state = STATE_DFU_MANIFEST; + return DFU_STATUS_OK; + default: + return DFU_STATUS_OK; + } +} + +static void usbdfu_getstatus_complete(usbd_device *usbd_dev, struct usb_setup_data *req) +{ + int i; + (void)req; + (void)usbd_dev; + + switch (usbdfu_state) { + case STATE_DFU_DNBUSY: + flash_unlock(); + if (prog.blocknum == 0) { + switch (prog.buf[0]) { + case CMD_ERASE: + { + u32 *dat = (u32 *)(prog.buf + 1); + flash_erase_page(*dat); + } + case CMD_SETADDR: + { + u32 *dat = (u32 *)(prog.buf + 1); + prog.addr = *dat; + } + } + } else { + u32 baseaddr = prog.addr + ((prog.blocknum - 2) * + dfu_function.wTransferSize); + for (i = 0; i < prog.len; i += 2) { + u16 *dat = (u16 *)(prog.buf + i); + flash_program_half_word(baseaddr + i, + *dat); + } + } + flash_lock(); + + /* Jump straight to dfuDNLOAD-IDLE, skipping dfuDNLOAD-SYNC. */ + usbdfu_state = STATE_DFU_DNLOAD_IDLE; + return; + case STATE_DFU_MANIFEST: + /* USB device must detach, we just reset... */ + scb_reset_system(); + return; /* Will never return. */ + default: + return; + } +} + +static int usbdfu_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, u8 **buf, + u16 *len, void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req)) +{ + if ((req->bmRequestType & 0x7F) != 0x21) + return 0; /* Only accept class request. */ + + switch (req->bRequest) { + case DFU_DNLOAD: + if ((len == NULL) || (*len == 0)) { + usbdfu_state = STATE_DFU_MANIFEST_SYNC; + return 1; + } else { + /* Copy download data for use on GET_STATUS. */ + prog.blocknum = req->wValue; + prog.len = *len; + memcpy(prog.buf, *buf, *len); + usbdfu_state = STATE_DFU_DNLOAD_SYNC; + return 1; + } + case DFU_CLRSTATUS: + /* Clear error and return to dfuIDLE. */ + if (usbdfu_state == STATE_DFU_ERROR) + usbdfu_state = STATE_DFU_IDLE; + return 1; + case DFU_ABORT: + /* Abort returns to dfuIDLE state. */ + usbdfu_state = STATE_DFU_IDLE; + return 1; + case DFU_UPLOAD: + /* Upload not supported for now. */ + return 0; + case DFU_GETSTATUS: { + u32 bwPollTimeout = 0; /* 24-bit integer in DFU class spec */ + (*buf)[0] = usbdfu_getstatus(usbd_dev, &bwPollTimeout); + (*buf)[1] = bwPollTimeout & 0xFF; + (*buf)[2] = (bwPollTimeout >> 8) & 0xFF; + (*buf)[3] = (bwPollTimeout >> 16) & 0xFF; + (*buf)[4] = usbdfu_state; + (*buf)[5] = 0; /* iString not used here */ + *len = 6; + *complete = usbdfu_getstatus_complete; + return 1; + } + case DFU_GETSTATE: + /* Return state with no state transision. */ + *buf[0] = usbdfu_state; + *len = 1; + return 1; + } + + return 0; +} + +int main(void) +{ + usbd_device *usbd_dev; + + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + + if (!gpio_get(GPIOA, GPIO10)) { + /* Boot the application if it's valid. */ + if ((*(volatile u32 *)APP_ADDRESS & 0x2FFE0000) == 0x20000000) { + /* Set vector table base address. */ + SCB_VTOR = APP_ADDRESS & 0xFFFF; + /* Initialise master stack pointer. */ + asm volatile("msr msp, %0"::"g" + (*(volatile u32 *)APP_ADDRESS)); + /* Jump to application. */ + (*(void (**)())(APP_ADDRESS + 4))(); + } + } + + rcc_clock_setup_in_hsi_out_48mhz(); + + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); + + AFIO_MAPR |= AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_ON; + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, 0, GPIO15); + + usbd_dev = usbd_init(&stm32f103_usb_driver, &dev, &config, usb_strings, 4); + usbd_set_control_buffer_size(usbd_dev, sizeof(usbd_control_buffer)); + usbd_register_control_callback( + usbd_dev, + USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE, + USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, + usbdfu_control_request); + + gpio_set(GPIOA, GPIO15); + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO15); + + while (1) + usbd_poll(usbd_dev); +} diff --git a/examples/stm32/f1/stm32-h107/fancyblink/Makefile b/examples/stm32/f1/stm32-h107/fancyblink/Makefile new file mode 100644 index 0000000..b0f7d1f --- /dev/null +++ b/examples/stm32/f1/stm32-h107/fancyblink/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = fancyblink + +LDSCRIPT = ../stm32-h107.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/stm32-h107/fancyblink/README b/examples/stm32/f1/stm32-h107/fancyblink/README new file mode 100644 index 0000000..6f1ac39 --- /dev/null +++ b/examples/stm32/f1/stm32-h107/fancyblink/README @@ -0,0 +1,10 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This is small LED blinking example program using libopencm3. + +It's intended for the ST STM32-based Olimex STM32-H107 eval board (see +http://olimex.com/dev/stm32-h107.html for details). It should blink +the LED on the board. + diff --git a/examples/stm32/f1/stm32-h107/fancyblink/fancyblink.c b/examples/stm32/f1/stm32-h107/fancyblink/fancyblink.c new file mode 100644 index 0000000..e36b558 --- /dev/null +++ b/examples/stm32/f1/stm32-h107/fancyblink/fancyblink.c @@ -0,0 +1,58 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * Copyright (C) 2011 Damjan Marion + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include + +/* Set STM32 to 72 MHz. */ +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + + /* Enable GPIOC clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); +} + +static void gpio_setup(void) +{ + /* Set GPIO6/7 (in GPIO port C) to 'output push-pull'. */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO6); + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO7); +} + +int main(void) +{ + int i; + + clock_setup(); + gpio_setup(); + + /* Blink the LEDs (PC6 and PC7) on the board. */ + while (1) { + gpio_toggle(GPIOC, GPIO6); /* STAT1 LED on/off */ + gpio_toggle(GPIOC, GPIO7); /* STAT2 LED on/off */ + for (i = 0; i < 8000000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/stm32/f1/stm32-h107/stm32-h107.ld b/examples/stm32/f1/stm32-h107/stm32-h107.ld new file mode 100644 index 0000000..ee8319e --- /dev/null +++ b/examples/stm32/f1/stm32-h107/stm32-h107.ld @@ -0,0 +1,31 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* Linker script for Olimex STM32-H107 (STM32F107VCT6, 256K flash, 64K RAM). */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 256K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 64K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_stm32f1.ld + diff --git a/examples/stm32/f1/stm32-h107/usb_simple/Makefile b/examples/stm32/f1/stm32-h107/usb_simple/Makefile new file mode 100644 index 0000000..673338c --- /dev/null +++ b/examples/stm32/f1/stm32-h107/usb_simple/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = usb_simple + +LDSCRIPT = ../stm32-h107.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/stm32-h107/usb_simple/README b/examples/stm32/f1/stm32-h107/usb_simple/README new file mode 100644 index 0000000..93d4f03 --- /dev/null +++ b/examples/stm32/f1/stm32-h107/usb_simple/README @@ -0,0 +1,10 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This is small USB controlled LED blinking example program using libopencm3. + +It's intended for the ST STM32-based Olimex STM32-H107 eval board (see +http://olimex.com/dev/stm32-h107.html for details). The usbtest.py +script in this directory maybe used to control the LED. + diff --git a/examples/stm32/f1/stm32-h107/usb_simple/usb_simple.c b/examples/stm32/f1/stm32-h107/usb_simple/usb_simple.c new file mode 100644 index 0000000..4b930dd --- /dev/null +++ b/examples/stm32/f1/stm32-h107/usb_simple/usb_simple.c @@ -0,0 +1,120 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2011 Gareth McMullin + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include + +const struct usb_device_descriptor dev = { + .bLength = USB_DT_DEVICE_SIZE, + .bDescriptorType = USB_DT_DEVICE, + .bcdUSB = 0x0200, + .bDeviceClass = 0xFF, + .bDeviceSubClass = 0, + .bDeviceProtocol = 0, + .bMaxPacketSize0 = 64, + .idVendor = 0xCAFE, + .idProduct = 0xCAFE, + .bcdDevice = 0x0200, + .iManufacturer = 1, + .iProduct = 2, + .iSerialNumber = 3, + .bNumConfigurations = 1, +}; + +const struct usb_interface_descriptor iface = { + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 0, + .bAlternateSetting = 0, + .bNumEndpoints = 0, + .bInterfaceClass = 0xFF, + .bInterfaceSubClass = 0, + .bInterfaceProtocol = 0, + .iInterface = 0, +}; + +const struct usb_interface ifaces[] = {{ + .num_altsetting = 1, + .altsetting = &iface, +}}; + +const struct usb_config_descriptor config = { + .bLength = USB_DT_CONFIGURATION_SIZE, + .bDescriptorType = USB_DT_CONFIGURATION, + .wTotalLength = 0, + .bNumInterfaces = 1, + .bConfigurationValue = 1, + .iConfiguration = 0, + .bmAttributes = 0x80, + .bMaxPower = 0x32, + + .interface = ifaces, +}; + +const char *usb_strings[] = { + "Black Sphere Technologies", + "Simple Device", + "1001", +}; + +static int simple_control_callback(usbd_device *usbd_dev, struct usb_setup_data *req, u8 **buf, + u16 *len, void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req)) +{ + (void)buf; + (void)len; + (void)complete; + (void)usbd_dev; + + if (req->bmRequestType != 0x40) + return 0; /* Only accept vendor request. */ + + if (req->wValue & 1) + gpio_set(GPIOC, GPIO6); + else + gpio_clear(GPIOC, GPIO6); + + return 1; +} + +int main(void) +{ + usbd_device *usbd_dev; + + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_OTGFSEN); + + /* LED output */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO6); + + usbd_dev = usbd_init(&stm32f107_usb_driver, &dev, &config, usb_strings, 3); + usbd_register_control_callback( + usbd_dev, + USB_REQ_TYPE_VENDOR, + USB_REQ_TYPE_TYPE, + simple_control_callback); + + while (1) + usbd_poll(usbd_dev); +} + diff --git a/examples/stm32/f1/stm32-h107/usb_simple/usbtest.py b/examples/stm32/f1/stm32-h107/usb_simple/usbtest.py new file mode 100644 index 0000000..85b0ade --- /dev/null +++ b/examples/stm32/f1/stm32-h107/usb_simple/usbtest.py @@ -0,0 +1,20 @@ +import usb.core + +if __name__ == "__main__": + dev = usb.core.find(idVendor=0xcafe, idProduct=0xcafe) + if dev is None: + raise ValueError('Device not found') + + dev.set_configuration() + + import gtk + w = gtk.Window() + w.connect("destroy", gtk.main_quit) + toggle = gtk.ToggleButton("Toggle LED") + def toggled(button): + dev.ctrl_transfer(0x40, 0, button.get_active(), 0, 'Hello World!') + toggle.connect("toggled", toggled) + w.add(toggle) + w.show_all() + gtk.main() + diff --git a/examples/stm32/f1/stm32-maple/miniblink/Makefile b/examples/stm32/f1/stm32-maple/miniblink/Makefile new file mode 100644 index 0000000..f29af30 --- /dev/null +++ b/examples/stm32/f1/stm32-maple/miniblink/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = miniblink + +LDSCRIPT = ../stm32-maple.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/stm32-maple/miniblink/README b/examples/stm32/f1/stm32-maple/miniblink/README new file mode 100644 index 0000000..6e38d7a --- /dev/null +++ b/examples/stm32/f1/stm32-maple/miniblink/README @@ -0,0 +1,9 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This is the smallest-possible example program using libopencm3. + +It's intended for the ST STM32-based Leaf Labs Maple board (see +http://leaflabs.com for details). It should blink the LED on the board. + diff --git a/examples/stm32/f1/stm32-maple/miniblink/miniblink.c b/examples/stm32/f1/stm32-maple/miniblink/miniblink.c new file mode 100644 index 0000000..0d5d66c --- /dev/null +++ b/examples/stm32/f1/stm32-maple/miniblink/miniblink.c @@ -0,0 +1,71 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include + +static void gpio_setup(void) +{ + /* Enable GPIOA clock. */ + /* Manually: */ + // RCC_APB2ENR |= RCC_APB2ENR_IOPCEN; + /* Using API functions: */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + /* Set GPIO5 (in GPIO port A) to 'output push-pull'. */ + /* Manually: */ + // GPIOA_CRH = (GPIO_CNF_OUTPUT_PUSHPULL << (((5 - 8) * 4) + 2)); + // GPIOA_CRH |= (GPIO_MODE_OUTPUT_2_MHZ << ((5 - 8) * 4)); + /* Using API functions: */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO5); +} + +int main(void) +{ + int i; + + gpio_setup(); + + /* Blink the LED (PA5) on the board. */ + while (1) { + /* Manually: */ + // GPIOA_BSRR = GPIO5; /* LED off */ + // for (i = 0; i < 800000; i++) /* Wait a bit. */ + // __asm__("nop"); + // GPIOA_BRR = GPIO5; /* LED on */ + // for (i = 0; i < 800000; i++) /* Wait a bit. */ + // __asm__("nop"); + + /* Using API functions gpio_set()/gpio_clear(): */ + // gpio_set(GPIOA, GPIO5); /* LED off */ + // for (i = 0; i < 800000; i++) /* Wait a bit. */ + // __asm__("nop"); + // gpio_clear(GPIOA, GPIO5); /* LED on */ + // for (i = 0; i < 800000; i++) /* Wait a bit. */ + // __asm__("nop"); + + /* Using API function gpio_toggle(): */ + gpio_toggle(GPIOA, GPIO5); /* LED on/off */ + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/stm32/f1/stm32-maple/stm32-maple.ld b/examples/stm32/f1/stm32-maple/stm32-maple.ld new file mode 100644 index 0000000..65860a4 --- /dev/null +++ b/examples/stm32/f1/stm32-maple/stm32-maple.ld @@ -0,0 +1,32 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* Linker script for Leaf Maple (STM32F103RBT6, 128K flash, 20K RAM, + * minus 20k flash and 3k RAM for the DFU bootloader). */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08005000, LENGTH = 108K + ram (rwx) : ORIGIN = 0x20000c00, LENGTH = 17K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_stm32f1.ld + diff --git a/examples/stm32/f1/stm32-maple/usart/Makefile b/examples/stm32/f1/stm32-maple/usart/Makefile new file mode 100644 index 0000000..25623b6 --- /dev/null +++ b/examples/stm32/f1/stm32-maple/usart/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = usart + +LDSCRIPT = ../stm32-maple.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/stm32-maple/usart/README b/examples/stm32/f1/stm32-maple/usart/README new file mode 100644 index 0000000..ecd7269 --- /dev/null +++ b/examples/stm32/f1/stm32-maple/usart/README @@ -0,0 +1,13 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example program sends some characters on USART3 on the ST STM32-based +Olimex STM32-H103 eval board (see http://olimex.com/dev/stm32-h103.html +for details). + +The terminal settings for the receiving device/PC are 38400 8n1. + +The sending is done in a blocking way in the code, see the usart_irq example +for a more elaborate USART example. + diff --git a/examples/stm32/f1/stm32-maple/usart/usart.c b/examples/stm32/f1/stm32-maple/usart/usart.c new file mode 100644 index 0000000..75d120a --- /dev/null +++ b/examples/stm32/f1/stm32-maple/usart/usart.c @@ -0,0 +1,82 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include + +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + + /* Enable GPIOA clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + + /* Enable clocks for GPIO port A (for GPIO_USART2_TX) and USART2. */ + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN); +} + +static void usart_setup(void) +{ + /* Setup GPIO pin GPIO_USART2_TX. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX); + + /* Setup UART parameters. */ + usart_set_baudrate(USART2, 38400); + usart_set_databits(USART2, 8); + usart_set_stopbits(USART2, USART_STOPBITS_1); + usart_set_mode(USART2, USART_MODE_TX); + usart_set_parity(USART2, USART_PARITY_NONE); + usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART2); +} + +static void gpio_setup(void) +{ + /* Set GPIO (in GPIO port A) to 'output push-pull'. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO5); +} + +int main(void) +{ + int i, j = 0, c = 0; + + clock_setup(); + gpio_setup(); + usart_setup(); + + /* Blink the LED (PA5) on the board with every transmitted byte. */ + while (1) { + gpio_toggle(GPIOA, GPIO5); /* LED on/off */ + usart_send_blocking(USART2, c + '0'); /* USART2: Send byte. */ + c = (c == 9) ? 0 : c + 1; /* Increment c. */ + if ((j++ % 80) == 0) { /* Newline after line full. */ + usart_send_blocking(USART2, '\r'); + usart_send_blocking(USART2, '\n'); + } + for (i = 0; i < 8000000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/stm32/f1/stm32-maple/usart_irq/Makefile b/examples/stm32/f1/stm32-maple/usart_irq/Makefile new file mode 100644 index 0000000..c2eed17 --- /dev/null +++ b/examples/stm32/f1/stm32-maple/usart_irq/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = usart_irq + +LDSCRIPT = ../stm32-maple.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/stm32-maple/usart_irq/usart_irq.c b/examples/stm32/f1/stm32-maple/usart_irq/usart_irq.c new file mode 100644 index 0000000..4382838 --- /dev/null +++ b/examples/stm32/f1/stm32-maple/usart_irq/usart_irq.c @@ -0,0 +1,120 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include + +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + + /* Enable GPIOA clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + + /* Enable clocks for GPIO port A (for LED GPIO_USART2_TX) and USART2. */ + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN); +} + +static void usart_setup(void) +{ + /* Enable the USART2 interrupt. */ + nvic_enable_irq(NVIC_USART2_IRQ); + + /* Setup GPIO pin GPIO_USART2_RE_TX on GPIO port A for transmit. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX); + + /* Setup GPIO pin GPIO_USART2_RE_RX on GPIO port A for receive. */ + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, + GPIO_CNF_INPUT_FLOAT, GPIO_USART2_RX); + + /* Setup UART parameters. */ + usart_set_baudrate(USART2, 38400); + usart_set_databits(USART2, 8); + usart_set_stopbits(USART2, USART_STOPBITS_1); + usart_set_parity(USART2, USART_PARITY_NONE); + usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); + usart_set_mode(USART2, USART_MODE_TX_RX); + + /* Enable USART2 Receive interrupt. */ + USART_CR1(USART2) |= USART_CR1_RXNEIE; + + /* Finally enable the USART. */ + usart_enable(USART2); +} + +static void gpio_setup(void) +{ + gpio_set(GPIOA, GPIO5); + + /* Setup GPIO5 (in GPIO port A) for LED use. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO5); +} + +void usart2_isr(void) +{ + static u8 data = 'A'; + + /* Check if we were called because of RXNE. */ + if (((USART_CR1(USART2) & USART_CR1_RXNEIE) != 0) && + ((USART_SR(USART2) & USART_SR_RXNE) != 0)) { + + /* Indicate that we got data. */ + gpio_toggle(GPIOA, GPIO5); + + /* Retrieve the data from the peripheral. */ + data = usart_recv(USART2); + + /* Enable transmit interrupt so it sends back the data. */ + USART_CR1(USART2) |= USART_CR1_TXEIE; + } + + /* Check if we were called because of TXE. */ + if (((USART_CR1(USART2) & USART_CR1_TXEIE) != 0) && + ((USART_SR(USART2) & USART_SR_TXE) != 0)) { + + /* Indicate that we are sending out data. */ + // gpio_toggle(GPIOA, GPIO5); + + /* Put data into the transmit register. */ + usart_send(USART2, data); + + /* Disable the TXE interrupt as we don't need it anymore. */ + USART_CR1(USART2) &= ~USART_CR1_TXEIE; + } +} + +int main(void) +{ + SCB_VTOR = (u32) 0x08005000; + + clock_setup(); + gpio_setup(); + usart_setup(); + + /* Wait forever and do nothing. */ + while (1) + __asm__("nop"); + + return 0; +} diff --git a/examples/stm32/f1/stm32-maple/usb_cdcacm/Makefile b/examples/stm32/f1/stm32-maple/usb_cdcacm/Makefile new file mode 100644 index 0000000..46e3d1d --- /dev/null +++ b/examples/stm32/f1/stm32-maple/usb_cdcacm/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = cdcacm + +LDSCRIPT = ../stm32-maple.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/stm32-maple/usb_cdcacm/README b/examples/stm32/f1/stm32-maple/usb_cdcacm/README new file mode 100644 index 0000000..2f1ee4b --- /dev/null +++ b/examples/stm32/f1/stm32-maple/usb_cdcacm/README @@ -0,0 +1,7 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example implements a USB CDC-ACM device (aka Virtual Serial Port) +to demonstrate the use of the USB device stack. + diff --git a/examples/stm32/f1/stm32-maple/usb_cdcacm/cdcacm.c b/examples/stm32/f1/stm32-maple/usb_cdcacm/cdcacm.c new file mode 100644 index 0000000..b0019c1 --- /dev/null +++ b/examples/stm32/f1/stm32-maple/usb_cdcacm/cdcacm.c @@ -0,0 +1,266 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Gareth McMullin + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include + +static const struct usb_device_descriptor dev = { + .bLength = USB_DT_DEVICE_SIZE, + .bDescriptorType = USB_DT_DEVICE, + .bcdUSB = 0x0200, + .bDeviceClass = USB_CLASS_CDC, + .bDeviceSubClass = 0, + .bDeviceProtocol = 0, + .bMaxPacketSize0 = 64, + .idVendor = 0x0483, + .idProduct = 0x5740, + .bcdDevice = 0x0200, + .iManufacturer = 1, + .iProduct = 2, + .iSerialNumber = 3, + .bNumConfigurations = 1, +}; + +/* + * This notification endpoint isn't implemented. According to CDC spec its + * optional, but its absence causes a NULL pointer dereference in Linux + * cdc_acm driver. + */ +static const struct usb_endpoint_descriptor comm_endp[] = {{ + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x83, + .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, + .wMaxPacketSize = 16, + .bInterval = 255, +}}; + +static const struct usb_endpoint_descriptor data_endp[] = {{ + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x01, + .bmAttributes = USB_ENDPOINT_ATTR_BULK, + .wMaxPacketSize = 64, + .bInterval = 1, +}, { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x82, + .bmAttributes = USB_ENDPOINT_ATTR_BULK, + .wMaxPacketSize = 64, + .bInterval = 1, +}}; + +static const struct { + struct usb_cdc_header_descriptor header; + struct usb_cdc_call_management_descriptor call_mgmt; + struct usb_cdc_acm_descriptor acm; + struct usb_cdc_union_descriptor cdc_union; +} __attribute__((packed)) cdcacm_functional_descriptors = { + .header = { + .bFunctionLength = sizeof(struct usb_cdc_header_descriptor), + .bDescriptorType = CS_INTERFACE, + .bDescriptorSubtype = USB_CDC_TYPE_HEADER, + .bcdCDC = 0x0110, + }, + .call_mgmt = { + .bFunctionLength = + sizeof(struct usb_cdc_call_management_descriptor), + .bDescriptorType = CS_INTERFACE, + .bDescriptorSubtype = USB_CDC_TYPE_CALL_MANAGEMENT, + .bmCapabilities = 0, + .bDataInterface = 1, + }, + .acm = { + .bFunctionLength = sizeof(struct usb_cdc_acm_descriptor), + .bDescriptorType = CS_INTERFACE, + .bDescriptorSubtype = USB_CDC_TYPE_ACM, + .bmCapabilities = 0, + }, + .cdc_union = { + .bFunctionLength = sizeof(struct usb_cdc_union_descriptor), + .bDescriptorType = CS_INTERFACE, + .bDescriptorSubtype = USB_CDC_TYPE_UNION, + .bControlInterface = 0, + .bSubordinateInterface0 = 1, + }, +}; + +static const struct usb_interface_descriptor comm_iface[] = {{ + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 0, + .bAlternateSetting = 0, + .bNumEndpoints = 1, + .bInterfaceClass = USB_CLASS_CDC, + .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM, + .bInterfaceProtocol = USB_CDC_PROTOCOL_AT, + .iInterface = 0, + + .endpoint = comm_endp, + + .extra = &cdcacm_functional_descriptors, + .extralen = sizeof(cdcacm_functional_descriptors), +}}; + +static const struct usb_interface_descriptor data_iface[] = {{ + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 1, + .bAlternateSetting = 0, + .bNumEndpoints = 2, + .bInterfaceClass = USB_CLASS_DATA, + .bInterfaceSubClass = 0, + .bInterfaceProtocol = 0, + .iInterface = 0, + + .endpoint = data_endp, +}}; + +static const struct usb_interface ifaces[] = {{ + .num_altsetting = 1, + .altsetting = comm_iface, +}, { + .num_altsetting = 1, + .altsetting = data_iface, +}}; + +static const struct usb_config_descriptor config = { + .bLength = USB_DT_CONFIGURATION_SIZE, + .bDescriptorType = USB_DT_CONFIGURATION, + .wTotalLength = 0, + .bNumInterfaces = 2, + .bConfigurationValue = 1, + .iConfiguration = 0, + .bmAttributes = 0x80, + .bMaxPower = 0x32, + + .interface = ifaces, +}; + +static const char *usb_strings[] = { + "Black Sphere Technologies", + "CDC-ACM Demo", + "DEMO", +}; + +static int cdcacm_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, u8 **buf, + u16 *len, void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req)) +{ + (void)complete; + (void)buf; + (void)usbd_dev; + + switch (req->bRequest) { + case USB_CDC_REQ_SET_CONTROL_LINE_STATE: { + /* + * This Linux cdc_acm driver requires this to be implemented + * even though it's optional in the CDC spec, and we don't + * advertise it in the ACM functional descriptor. + */ + char local_buf[10]; + struct usb_cdc_notification *notif = (void *)local_buf; + + /* We echo signals back to host as notification. */ + notif->bmRequestType = 0xA1; + notif->bNotification = USB_CDC_NOTIFY_SERIAL_STATE; + notif->wValue = 0; + notif->wIndex = 0; + notif->wLength = 2; + local_buf[8] = req->wValue & 3; + local_buf[9] = 0; + // usbd_ep_write_packet(0x83, buf, 10); + return 1; + } + case USB_CDC_REQ_SET_LINE_CODING: + if (*len < sizeof(struct usb_cdc_line_coding)) + return 0; + return 1; + } + return 0; +} + +static void cdcacm_data_rx_cb(usbd_device *usbd_dev, u8 ep) +{ + (void)ep; + (void)usbd_dev; + + char buf[64]; + int len = usbd_ep_read_packet(usbd_dev, 0x01, buf, 64); + + if (len) { + usbd_ep_write_packet(usbd_dev, 0x82, buf, len); + buf[len] = 0; + } +} + +static void cdcacm_set_config(usbd_device *usbd_dev, u16 wValue) +{ + (void)wValue; + (void)usbd_dev; + + usbd_ep_setup(usbd_dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64, cdcacm_data_rx_cb); + usbd_ep_setup(usbd_dev, 0x82, USB_ENDPOINT_ATTR_BULK, 64, NULL); + usbd_ep_setup(usbd_dev, 0x83, USB_ENDPOINT_ATTR_INTERRUPT, 16, NULL); + + usbd_register_control_callback( + usbd_dev, + USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE, + USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, + cdcacm_control_request); +} + +int main(void) +{ + int i; + + usbd_device *usbd_dev; + + SCB_VTOR = (u32) 0x08005000; + + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN | + RCC_APB2ENR_IOPCEN); + + /* Setup GPIOC Pin 12 to pull up the D+ high, so autodect works + * with the bootloader. The circuit is active low. */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_OPENDRAIN, GPIO12); + gpio_clear(GPIOC, GPIO12); + + /* Setup GPIOA Pin 5 for the LED */ + gpio_set(GPIOA, GPIO5); + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO5); + + usbd_dev = usbd_init(&stm32f103_usb_driver, &dev, &config, usb_strings, 3); + usbd_register_set_config_callback(usbd_dev, cdcacm_set_config); + + for (i = 0; i < 0x800000; i++) + __asm__("nop"); + gpio_clear(GPIOA, GPIO5); + + while (1) + usbd_poll(usbd_dev); +} diff --git a/examples/stm32/f1/stm32vl-discovery/adc-dac-printf/Makefile b/examples/stm32/f1/stm32vl-discovery/adc-dac-printf/Makefile new file mode 100644 index 0000000..979317c --- /dev/null +++ b/examples/stm32/f1/stm32vl-discovery/adc-dac-printf/Makefile @@ -0,0 +1,24 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = adc-dac-printf +LDSCRIPT = ../../../../../lib/stm32/f1/stm32f100xb.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/stm32vl-discovery/adc-dac-printf/README b/examples/stm32/f1/stm32vl-discovery/adc-dac-printf/README new file mode 100644 index 0000000..796cba2 --- /dev/null +++ b/examples/stm32/f1/stm32vl-discovery/adc-dac-printf/README @@ -0,0 +1,17 @@ +Console on PA2 (tx only) 115200@8n1 + +* Prints the ADC value on PA0 (adc channel 0) on the console +* Echos half that ADC value out to DAC channel 2 on PA5 +* Prints the ADC value of PA1 (adc channel 1) to the console. + +Recommended wiring: +* pot or any resistor ladder to PA0 +* jumper from PA5 to PA1 + +example output: +... +tick: 228: adc0= 3950, target adc1=1975, adc1=1979 +tick: 229: adc0= 3949, target adc1=1974, adc1=1978 +tick: 230: adc0= 3950, target adc1=1975, adc1=1979 +tick: 231: adc0= 3949, target adc1=1974, adc1=1978 +... diff --git a/examples/stm32/f1/stm32vl-discovery/adc-dac-printf/adc-dac-printf.c b/examples/stm32/f1/stm32vl-discovery/adc-dac-printf/adc-dac-printf.c new file mode 100644 index 0000000..7155360 --- /dev/null +++ b/examples/stm32/f1/stm32vl-discovery/adc-dac-printf/adc-dac-printf.c @@ -0,0 +1,164 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2013 Karl Palsson + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#define LED_DISCOVERY_USER_PORT GPIOC +#define LED_DISCOVERY_USER_PIN GPIO8 + +#define USART_CONSOLE USART2 + +int _write(int file, char *ptr, int len); + +static void clock_setup(void) +{ + rcc_clock_setup_in_hsi_out_24mhz(); + /* Enable clocks for USART2 and DAC*/ + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN); + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_DACEN); + + /* and the ADC and IO ports */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_ADC1EN); +} + +static void usart_setup(void) +{ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX); + + usart_set_baudrate(USART_CONSOLE, 115200); + usart_set_databits(USART_CONSOLE, 8); + usart_set_stopbits(USART_CONSOLE, USART_STOPBITS_1); + usart_set_mode(USART_CONSOLE, USART_MODE_TX); + usart_set_parity(USART_CONSOLE, USART_PARITY_NONE); + usart_set_flow_control(USART_CONSOLE, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART_CONSOLE); +} + +/** + * Use USART_CONSOLE as a console. + * This is a syscall for newlib + * @param file + * @param ptr + * @param len + * @return + */ +int _write(int file, char *ptr, int len) +{ + int i; + + if (file == STDOUT_FILENO || file == STDERR_FILENO) { + for (i = 0; i < len; i++) { + if (ptr[i] == '\n') { + usart_send_blocking(USART_CONSOLE, '\r'); + } + usart_send_blocking(USART_CONSOLE, ptr[i]); + } + return i; + } + errno = EIO; + return -1; +} + +static void adc_setup(void) +{ + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, GPIO0); + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, GPIO1); + + /* Make sure the ADC doesn't run during config. */ + adc_off(ADC1); + + /* We configure everything for one single conversion. */ + adc_disable_scan_mode(ADC1); + adc_set_single_conversion_mode(ADC1); + adc_disable_external_trigger_regular(ADC1); + adc_set_right_aligned(ADC1); + adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC); + + adc_power_on(ADC1); + + /* Wait for ADC starting up. */ + int i; + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + + adc_reset_calibration(ADC1); + adc_calibration(ADC1); +} + +static void dac_setup(void) +{ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO5); + dac_disable(CHANNEL_2); + dac_disable_waveform_generation(CHANNEL_2); + dac_enable(CHANNEL_2); + dac_set_trigger_source(DAC_CR_TSEL2_SW); +} + +static u16 read_adc_naiive(u8 channel) +{ + u8 channel_array[16]; + channel_array[0] = channel; + adc_set_regular_sequence(ADC1, 1, channel_array); + adc_start_conversion_direct(ADC1); + while (!adc_eoc(ADC1)); + u16 reg16 = adc_read_regular(ADC1); + return reg16; +} + +int main(void) +{ + int i; + int j = 0; + clock_setup(); + usart_setup(); + printf("hi guys!\n"); + adc_setup(); + dac_setup(); + gpio_set_mode(LED_DISCOVERY_USER_PORT, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, LED_DISCOVERY_USER_PIN); + + while (1) { + u16 input_adc0 = read_adc_naiive(0); + u16 target = input_adc0 / 2; + dac_load_data_buffer_single(target, RIGHT12, CHANNEL_2); + dac_software_trigger(CHANNEL_2); + u16 input_adc1 = read_adc_naiive(1); + printf("tick: %d: adc0= %u, target adc1=%d, adc1=%d\n", + j++, input_adc0, target, input_adc1); + gpio_toggle(LED_DISCOVERY_USER_PORT, LED_DISCOVERY_USER_PIN); /* LED on/off */ + for (i = 0; i < 1000000; i++) /* Wait a bit. */ + __asm__("NOP"); + } + + return 0; +} diff --git a/examples/stm32/f1/stm32vl-discovery/button/Makefile b/examples/stm32/f1/stm32vl-discovery/button/Makefile new file mode 100644 index 0000000..8941e05 --- /dev/null +++ b/examples/stm32/f1/stm32vl-discovery/button/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = button + +LDSCRIPT = ../stm32vl-discovery.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/stm32vl-discovery/button/README b/examples/stm32/f1/stm32vl-discovery/button/README new file mode 100644 index 0000000..38bc1ea --- /dev/null +++ b/examples/stm32/f1/stm32vl-discovery/button/README @@ -0,0 +1,8 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example blinks the green LED on the ST STM32VLDISCOVERY eval board. + +When you press the 'USER' button, the blinking is slower. + diff --git a/examples/stm32/f1/stm32vl-discovery/button/button.c b/examples/stm32/f1/stm32vl-discovery/button/button.c new file mode 100644 index 0000000..6b67aec --- /dev/null +++ b/examples/stm32/f1/stm32vl-discovery/button/button.c @@ -0,0 +1,75 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann , + * Copyright (C) 2010 Piotr Esden-Tempski + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include + +u16 exti_line_state; + +/* Set STM32 to 24 MHz. */ +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_8mhz_out_24mhz(); +} + +static void gpio_setup(void) +{ + /* Enable GPIOC clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + /* Set GPIO9 (in GPIO port C) to 'output push-pull'. */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO9); +} + +static void button_setup(void) +{ + /* Enable GPIOA clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + + /* Set GPIO0 (in GPIO port A) to 'input open-drain'. */ + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO0); +} + +int main(void) +{ + int i; + + clock_setup(); + gpio_setup(); + button_setup(); + + /* Blink the LED (PC9) on the board. */ + while (1) { + gpio_toggle(GPIOC, GPIO9); + + /* Upon button press, blink more slowly. */ + exti_line_state = GPIOA_IDR; + if ((exti_line_state & (1 << 0)) != 0) { + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/stm32/f1/stm32vl-discovery/fancyblink/Makefile b/examples/stm32/f1/stm32vl-discovery/fancyblink/Makefile new file mode 100644 index 0000000..7233ae8 --- /dev/null +++ b/examples/stm32/f1/stm32vl-discovery/fancyblink/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = fancyblink + +LDSCRIPT = ../stm32vl-discovery.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/stm32vl-discovery/fancyblink/README b/examples/stm32/f1/stm32vl-discovery/fancyblink/README new file mode 100644 index 0000000..b5d794c --- /dev/null +++ b/examples/stm32/f1/stm32vl-discovery/fancyblink/README @@ -0,0 +1,9 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This is the smallest-possible example program using libopencm3. + +It's intended for the ST STM32VLDISCOVERY eval board. It should blink +the LEDs on the board. + diff --git a/examples/stm32/f1/stm32vl-discovery/fancyblink/fancyblink.c b/examples/stm32/f1/stm32vl-discovery/fancyblink/fancyblink.c new file mode 100644 index 0000000..fbd0e84 --- /dev/null +++ b/examples/stm32/f1/stm32vl-discovery/fancyblink/fancyblink.c @@ -0,0 +1,61 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * Copyright (C) 2011 Damjan Marion + * Copyright (C) 2011 Mark Panajotovic + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include + +/* Set STM32 to 24 MHz. */ +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_8mhz_out_24mhz(); + + /* Enable GPIOC clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); +} + +static void gpio_setup(void) +{ + /* Set GPIO8/9 (in GPIO port C) to 'output push-pull'. */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO8); + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO9); +} + +int main(void) +{ + int i; + + clock_setup(); + gpio_setup(); + + /* Set one LED for wigwag effect when toggling. */ + gpio_set(GPIOC, GPIO8); + + /* Blink the LEDs (PC8 and PC9) on the board. */ + while (1) { + gpio_toggle(GPIOC, GPIO8 | GPIO9); /* Toggle LEDs. */ + for (i = 0; i < 2000000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/stm32/f1/stm32vl-discovery/miniblink/Makefile b/examples/stm32/f1/stm32vl-discovery/miniblink/Makefile new file mode 100644 index 0000000..b3488ad --- /dev/null +++ b/examples/stm32/f1/stm32vl-discovery/miniblink/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = miniblink + +LDSCRIPT = ../stm32vl-discovery.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/stm32vl-discovery/miniblink/README b/examples/stm32/f1/stm32vl-discovery/miniblink/README new file mode 100644 index 0000000..cffce60 --- /dev/null +++ b/examples/stm32/f1/stm32vl-discovery/miniblink/README @@ -0,0 +1,9 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This is the smallest-possible example program using libopencm3. + +It's intended for the ST STM32VLDISCOVERY eval board. It should blink +the blue LED on the board. + diff --git a/examples/stm32/f1/stm32vl-discovery/miniblink/miniblink.c b/examples/stm32/f1/stm32vl-discovery/miniblink/miniblink.c new file mode 100644 index 0000000..3f319fe --- /dev/null +++ b/examples/stm32/f1/stm32vl-discovery/miniblink/miniblink.c @@ -0,0 +1,71 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include + +static void gpio_setup(void) +{ + /* Enable GPIOC clock. */ + /* Manually: */ + // RCC_APB2ENR |= RCC_APB2ENR_IOPCEN; + /* Using API functions: */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + /* Set GPIO8 (in GPIO port C) to 'output push-pull'. */ + /* Manually: */ + // GPIOC_CRH = (GPIO_CNF_OUTPUT_PUSHPULL << (((8 - 8) * 4) + 2)); + // GPIOC_CRH |= (GPIO_MODE_OUTPUT_2_MHZ << ((8 - 8) * 4)); + /* Using API functions: */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO8); +} + +int main(void) +{ + int i; + + gpio_setup(); + + /* Blink the LED (PC8) on the board. */ + while (1) { + /* Manually: */ + // GPIOC_BSRR = GPIO8; /* LED off */ + // for (i = 0; i < 800000; i++) /* Wait a bit. */ + // __asm__("nop"); + // GPIOC_BRR = GPIO8; /* LED on */ + // for (i = 0; i < 800000; i++) /* Wait a bit. */ + // __asm__("nop"); + + /* Using API functions gpio_set()/gpio_clear(): */ + // gpio_set(GPIOC, GPIO8); /* LED off */ + // for (i = 0; i < 800000; i++) /* Wait a bit. */ + // __asm__("nop"); + // gpio_clear(GPIOC, GPIO8); /* LED on */ + // for (i = 0; i < 800000; i++) /* Wait a bit. */ + // __asm__("nop"); + + /* Using API function gpio_toggle(): */ + gpio_toggle(GPIOC, GPIO8); /* LED on/off */ + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/stm32/f1/stm32vl-discovery/rtc/Makefile b/examples/stm32/f1/stm32vl-discovery/rtc/Makefile new file mode 100644 index 0000000..111de88 --- /dev/null +++ b/examples/stm32/f1/stm32vl-discovery/rtc/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = rtc + +LDSCRIPT = ../stm32vl-discovery.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/stm32vl-discovery/rtc/README b/examples/stm32/f1/stm32vl-discovery/rtc/README new file mode 100644 index 0000000..d4509e4 --- /dev/null +++ b/examples/stm32/f1/stm32vl-discovery/rtc/README @@ -0,0 +1,9 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This is a small RTC example project. + +It blinks the ST STM32VLDISCOVERY's blue LED at 1Hz, and sends the value of +the RTC counter register down the serial line (PA9) at 38400,8N1. + diff --git a/examples/stm32/f1/stm32vl-discovery/rtc/rtc.c b/examples/stm32/f1/stm32vl-discovery/rtc/rtc.c new file mode 100644 index 0000000..c705261 --- /dev/null +++ b/examples/stm32/f1/stm32vl-discovery/rtc/rtc.c @@ -0,0 +1,129 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Lord James + * Copyright (C) 2011 Mark Panajotovic + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include + +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_8mhz_out_24mhz(); + + /* Enable GPIOC clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + /* Enable clocks for GPIO port A (for GPIO_USART1_TX) and USART1. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_USART1EN); +} + +static void usart_setup(void) +{ + /* Setup GPIO pin GPIO_USART1_TX/GPIO9 on GPIO port A for transmit. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX); + + /* Setup UART parameters. */ + // usart_set_baudrate(USART1, 38400); + /* TODO usart_set_baudrate() doesn't support 24MHz clock (yet). */ + /* This is the equivalent: */ + USART_BRR(USART1) = (u16)((24000000 << 4) / (38400 * 16)); + + usart_set_databits(USART1, 8); + usart_set_stopbits(USART1, USART_STOPBITS_1); + usart_set_mode(USART1, USART_MODE_TX); + usart_set_parity(USART1, USART_PARITY_NONE); + usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART1); +} + +static void gpio_setup(void) +{ + /* Set GPIO8 (in GPIO port C) to 'output push-pull'. */ + /* This drives the blue LED on the STM32VLDISCOVERY. */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO8); +} + +static void nvic_setup(void) +{ + /* Without this the RTC interrupt routine will never be called. */ + nvic_enable_irq(NVIC_RTC_IRQ); + nvic_set_priority(NVIC_RTC_IRQ, 1); +} + +void rtc_isr(void) +{ + volatile u32 j = 0, c = 0; + + /* The interrupt flag isn't cleared by hardware, we have to do it. */ + rtc_clear_flag(RTC_SEC); + + /* Visual output. */ + gpio_toggle(GPIOC, GPIO8); + + c = rtc_get_counter_val(); + + /* Display the current counter value in binary via USART1. */ + for (j = 0; j < 32; j++) { + if ((c & (0x80000000 >> j)) != 0) + usart_send_blocking(USART1, '1'); + else + usart_send_blocking(USART1, '0'); + } + usart_send_blocking(USART1, '\n'); + usart_send_blocking(USART1, '\r'); +} + +int main(void) +{ + clock_setup(); + gpio_setup(); + usart_setup(); + + /* + * If the RTC is pre-configured just allow access, don't reconfigure. + * Otherwise enable it with the LSE as clock source and 0x7fff as + * prescale value. + */ + rtc_auto_awake(LSE, 0x7fff); + + /* The above mode will not reset the RTC when you press the RST button. + * It will also continue to count while the MCU is held in reset. If + * you want it to reset, comment out the above and use the following: + */ + // rtc_awake_from_off(LSE); + // rtc_set_prescale_val(0x7fff); + + /* Setup the RTC interrupt. */ + nvic_setup(); + + /* Enable the RTC interrupt to occur off the SEC flag. */ + rtc_interrupt_enable(RTC_SEC); + + while(1); + + return 0; +} diff --git a/examples/stm32/f1/stm32vl-discovery/stm32vl-discovery.ld b/examples/stm32/f1/stm32vl-discovery/stm32vl-discovery.ld new file mode 100644 index 0000000..40de3d3 --- /dev/null +++ b/examples/stm32/f1/stm32vl-discovery/stm32vl-discovery.ld @@ -0,0 +1,31 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* Linker script for ST STM32VLDISCOVERY (STM32F100RB, 128K flash, 8K RAM). */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 8K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_stm32f1.ld + diff --git a/examples/stm32/f1/stm32vl-discovery/usart/Makefile b/examples/stm32/f1/stm32vl-discovery/usart/Makefile new file mode 100644 index 0000000..1ef8758 --- /dev/null +++ b/examples/stm32/f1/stm32vl-discovery/usart/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = usart + +LDSCRIPT = ../stm32vl-discovery.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f1/stm32vl-discovery/usart/README b/examples/stm32/f1/stm32vl-discovery/usart/README new file mode 100644 index 0000000..6d09139 --- /dev/null +++ b/examples/stm32/f1/stm32vl-discovery/usart/README @@ -0,0 +1,12 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example program sends some characters on USART1 on the +ST STM32VLDISCOVERY eval board. + +The terminal settings for the receiving device/PC are 38400 8n1. + +The sending is done in a blocking way in the code, see the usart_irq example +for a more elaborate USART example. + diff --git a/examples/stm32/f1/stm32vl-discovery/usart/usart.c b/examples/stm32/f1/stm32vl-discovery/usart/usart.c new file mode 100644 index 0000000..1eac6d9 --- /dev/null +++ b/examples/stm32/f1/stm32vl-discovery/usart/usart.c @@ -0,0 +1,87 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include + +static void clock_setup(void) +{ + rcc_clock_setup_in_hse_8mhz_out_24mhz(); + + /* Enable GPIOC clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + /* Enable clocks for GPIO port A (for GPIO_USART1_TX) and USART1. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_USART1EN); +} + +static void usart_setup(void) +{ + /* Setup GPIO pin GPIO_USART1_TX/GPIO9 on GPIO port A for transmit. */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX); + + /* Setup UART parameters. */ + // usart_set_baudrate(USART1, 38400); + /* TODO usart_set_baudrate() doesn't support 24MHz clock (yet). */ + /* This is the equivalent: */ + USART_BRR(USART1) = (u16)((24000000 << 4) / (38400 * 16)); + + usart_set_databits(USART1, 8); + usart_set_stopbits(USART1, USART_STOPBITS_1); + usart_set_mode(USART1, USART_MODE_TX); + usart_set_parity(USART1, USART_PARITY_NONE); + usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART1); +} + +static void gpio_setup(void) +{ + /* Set GPIO9 (in GPIO port C) to 'output push-pull'. [LED] */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO9); +} + +int main(void) +{ + int i, j = 0, c = 0; + + clock_setup(); + gpio_setup(); + usart_setup(); + + /* Blink the LED (PC9) on the board with every transmitted byte. */ + while (1) { + gpio_toggle(GPIOC, GPIO9); /* LED on/off */ + usart_send_blocking(USART1, c + '0'); /* USART1: Send byte. */ + c = (c == 9) ? 0 : c + 1; /* Increment c. */ + if ((j++ % 80) == 0) { /* Newline after line full. */ + usart_send_blocking(USART1, '\r'); + usart_send_blocking(USART1, '\n'); + } + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("NOP"); + } + + return 0; +} diff --git a/examples/stm32/f2/Makefile.include b/examples/stm32/f2/Makefile.include new file mode 100644 index 0000000..e8fea09 --- /dev/null +++ b/examples/stm32/f2/Makefile.include @@ -0,0 +1,151 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## Copyright (C) 2010 Piotr Esden-Tempski +## Copyright (C) 2011 Fergus Noble +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +PREFIX ?= arm-none-eabi +# PREFIX ?= arm-elf +CC = $(PREFIX)-gcc +LD = $(PREFIX)-gcc +OBJCOPY = $(PREFIX)-objcopy +OBJDUMP = $(PREFIX)-objdump +GDB = $(PREFIX)-gdb + +TOOLCHAIN_DIR ?= ../../../../.. +ifeq ($(wildcard ../../../../../lib/libopencm3_stm32f2.a),) +ifneq ($(strip $(shell which $(CC))),) +TOOLCHAIN_DIR := $(shell dirname `which $(CC)`)/../$(PREFIX) +endif +else +ifeq ($(V),1) +$(info We seem to be building the example in the source directory. Using local library!) +endif +endif + +CFLAGS += -Os -g \ + -Wall -Wextra -Wimplicit-function-declaration \ + -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes \ + -Wundef -Wshadow \ + -I$(TOOLCHAIN_DIR)/include \ + -fno-common -mcpu=cortex-m3 -mthumb -msoft-float -MD -DSTM32F2 +LDSCRIPT ?= $(BINARY).ld +LDFLAGS += --static -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group \ + -L$(TOOLCHAIN_DIR)/lib -L$(TOOLCHAIN_DIR)/lib/stm32/f2 \ + -T$(LDSCRIPT) -nostartfiles -Wl,--gc-sections \ + -mthumb -march=armv7 -mfix-cortex-m3-ldrd -msoft-float +OBJS += $(BINARY).o + +OOCD ?= openocd +OOCD_INTERFACE ?= flossjtag +OOCD_BOARD ?= olimex_stm32_h103 +# Black magic probe specific variables +# Set the BMP_PORT to a serial port and then BMP is used for flashing +BMP_PORT ?= + +# Be silent per default, but 'make V=1' will show all compiler calls. +ifneq ($(V),1) +Q := @ +NULL := 2>/dev/null +else +LDFLAGS += -Wl,--print-gc-sections +endif + +.SUFFIXES: .elf .bin .hex .srec .list .images +.SECONDEXPANSION: +.SECONDARY: + +all: images + +images: $(BINARY).images +flash: $(BINARY).flash + +%.images: %.bin %.hex %.srec %.list + @#echo "*** $* images generated ***" + +%.bin: %.elf + @#printf " OBJCOPY $(*).bin\n" + $(Q)$(OBJCOPY) -Obinary $(*).elf $(*).bin + +%.hex: %.elf + @#printf " OBJCOPY $(*).hex\n" + $(Q)$(OBJCOPY) -Oihex $(*).elf $(*).hex + +%.srec: %.elf + @#printf " OBJCOPY $(*).srec\n" + $(Q)$(OBJCOPY) -Osrec $(*).elf $(*).srec + +%.list: %.elf + @#printf " OBJDUMP $(*).list\n" + $(Q)$(OBJDUMP) -S $(*).elf > $(*).list + +%.elf: $(OBJS) $(LDSCRIPT) $(TOOLCHAIN_DIR)/lib/libopencm3_stm32f2.a + @#printf " LD $(subst $(shell pwd)/,,$(@))\n" + $(Q)$(LD) -o $(*).elf $(OBJS) -lopencm3_stm32f2 $(LDFLAGS) + +%.o: %.c Makefile + @#printf " CC $(subst $(shell pwd)/,,$(@))\n" + $(Q)$(CC) $(CFLAGS) -o $@ -c $< + +clean: + $(Q)rm -f *.o + $(Q)rm -f *.d + $(Q)rm -f *.elf + $(Q)rm -f *.bin + $(Q)rm -f *.hex + $(Q)rm -f *.srec + $(Q)rm -f *.list + +ifeq ($(BMP_PORT),) +ifeq ($(OOCD_SERIAL),) +%.flash: %.hex + @printf " FLASH $<\n" + @# IMPORTANT: Don't use "resume", only "reset" will work correctly! + $(Q)$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \ + -f board/$(OOCD_BOARD).cfg \ + -c "init" -c "reset init" \ + -c "stm32f2x mass_erase 0" \ + -c "flash write_image $(*).hex" \ + -c "reset" \ + -c "shutdown" $(NULL) +else +%.flash: %.hex + @printf " FLASH $<\n" + @# IMPORTANT: Don't use "resume", only "reset" will work correctly! + $(Q)$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \ + -f board/$(OOCD_BOARD).cfg \ + -c "ft2232_serial $(OOCD_SERIAL)" \ + -c "init" -c "reset init" \ + -c "stm32f2x mass_erase 0" \ + -c "flash write_image $(*).hex" \ + -c "reset" \ + -c "shutdown" $(NULL) +endif +else +%.flash: %.elf + @echo " GDB $(*).elf (flash)" + $(Q)$(GDB) --batch \ + -ex 'target extended-remote $(BMP_PORT)' \ + -x $(TOOLCHAIN_DIR)/scripts/black_magic_probe_flash.scr \ + $(*).elf +endif + +.PHONY: images clean + +-include $(OBJS:.o=.d) + diff --git a/examples/stm32/f2/jobygps/jobygps.cfg b/examples/stm32/f2/jobygps/jobygps.cfg new file mode 100644 index 0000000..7ca449e --- /dev/null +++ b/examples/stm32/f2/jobygps/jobygps.cfg @@ -0,0 +1,3 @@ +reset_config srst_only srst_push_pull +source [find target/stm32f2x.cfg] + diff --git a/examples/stm32/f2/jobygps/jobygps.ld b/examples/stm32/f2/jobygps/jobygps.ld new file mode 100644 index 0000000..73caf0e --- /dev/null +++ b/examples/stm32/f2/jobygps/jobygps.ld @@ -0,0 +1,32 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* TODO: Fix board name, chip name, and sizes. */ +/* Linker script for Olimex STM32-H103 (STM32F103RBT6, 128K flash, 20K RAM). */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 256K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 64K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_stm32f2.ld + diff --git a/examples/stm32/f2/jobygps/miniblink/Makefile b/examples/stm32/f2/jobygps/miniblink/Makefile new file mode 100644 index 0000000..d49a1df --- /dev/null +++ b/examples/stm32/f2/jobygps/miniblink/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = miniblink + +LDSCRIPT = ../jobygps.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f2/jobygps/miniblink/README b/examples/stm32/f2/jobygps/miniblink/README new file mode 100644 index 0000000..0130112 --- /dev/null +++ b/examples/stm32/f2/jobygps/miniblink/README @@ -0,0 +1,6 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This is the smallest-possible example program using libopencm3. + diff --git a/examples/stm32/f2/jobygps/miniblink/miniblink.c b/examples/stm32/f2/jobygps/miniblink/miniblink.c new file mode 100644 index 0000000..7de420c --- /dev/null +++ b/examples/stm32/f2/jobygps/miniblink/miniblink.c @@ -0,0 +1,52 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * Copyright (C) 2011 Fergus Noble + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include + +static void gpio_setup(void) +{ + /* Enable GPIOC clock. */ + rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPCEN); + + /* Set GPIO3 and GPIO4 (in GPIO port C) to 'output push-pull'. */ + gpio_mode_setup(GPIOC, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO3 | GPIO4); +} + +int main(void) +{ + int i; + + gpio_setup(); + + gpio_set(GPIOC, GPIO3); + gpio_clear(GPIOC, GPIO4); + + /* Blink the LEDs (PC3, PC4) on the board. */ + while (1) { + /* Using API function gpio_toggle(): */ + gpio_toggle(GPIOC, GPIO3); + gpio_toggle(GPIOC, GPIO4); + for (i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/stm32/f2/jobygps/spi_test/Makefile b/examples/stm32/f2/jobygps/spi_test/Makefile new file mode 100644 index 0000000..86ec3b6 --- /dev/null +++ b/examples/stm32/f2/jobygps/spi_test/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = spi_test + +LDSCRIPT = ../jobygps.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f2/jobygps/spi_test/spi_test.c b/examples/stm32/f2/jobygps/spi_test/spi_test.c new file mode 100644 index 0000000..3dbf288 --- /dev/null +++ b/examples/stm32/f2/jobygps/spi_test/spi_test.c @@ -0,0 +1,115 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2011 Fergus Noble + * Copyright (C) 2011 Henry Hallam + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include + +int _write(int file, char *ptr, int len); + +static void clock_setup(void) +{ + /* Enable clocks on all the peripherals we are going to use. */ + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_SPI2EN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_USART1EN); + rcc_peripheral_enable_clock(&RCC_AHB1ENR, + RCC_AHB1ENR_IOPCEN | RCC_AHB1ENR_IOPAEN | RCC_AHB1ENR_IOPBEN); +} + +static void spi_setup(void) +{ + gpio_mode_setup(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, + GPIO13 | GPIO14 | GPIO15); + gpio_set_af(GPIOB, GPIO_AF5, GPIO13 | GPIO14 | GPIO15); + + /* Setup SPI parameters. */ + spi_init_master(SPI2, SPI_CR1_BAUDRATE_FPCLK_DIV_256, SPI_CR1_CPOL, + SPI_CR1_CPHA, SPI_CR1_DFF_8BIT, SPI_CR1_MSBFIRST); + spi_enable_ss_output(SPI2); /* Required, see NSS, 25.3.1 section. */ + + /* Finally enable the SPI. */ + spi_enable(SPI2); +} + +static void usart_setup(void) +{ + gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO9); + gpio_set_af(GPIOA, GPIO_AF7, GPIO9 | GPIO10); + + /* Setup UART parameters. */ + usart_set_baudrate(USART1, 9600); + usart_set_databits(USART1, 8); + usart_set_stopbits(USART1, USART_STOPBITS_1); + usart_set_parity(USART1, USART_PARITY_NONE); + usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE); + usart_set_mode(USART1, USART_MODE_TX); + + /* Finally enable the USART. */ + usart_enable(USART1); +} + +static void gpio_setup(void) +{ + gpio_set(GPIOC, GPIO3); + + /* Setup GPIO3 (in GPIO port C) for LED use. */ + gpio_mode_setup(GPIOC, GPIO_MODE_OUTPUT, GPIO_MODE_OUTPUT, GPIO3); +} + +int _write(int file, char *ptr, int len) +{ + int i; + + if (file == 1) { + for (i = 0; i < len; i++) + usart_send_blocking(USART1, ptr[i]); + return i; + } + errno = EIO; + return -1; +} + +int main(void) +{ + int counter = 0; + volatile u16 dummy __attribute__((unused)); + + clock_setup(); + gpio_setup(); + usart_setup(); + spi_setup(); + + while (1) { + counter++; + printf("Hello, world! %i\r\n", counter); + /* Stops RX buffer overflow, but probably not needed. */ + dummy = spi_read(SPI2); + spi_send(SPI2, (u8) counter); + gpio_toggle(GPIOC, GPIO3); + } + + while (1); + + return 0; +} diff --git a/examples/stm32/f2/jobygps/usart_printf/Makefile b/examples/stm32/f2/jobygps/usart_printf/Makefile new file mode 100644 index 0000000..d340ca4 --- /dev/null +++ b/examples/stm32/f2/jobygps/usart_printf/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = usart_printf + +LDSCRIPT = ../jobygps.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f2/jobygps/usart_printf/usart_printf.c b/examples/stm32/f2/jobygps/usart_printf/usart_printf.c new file mode 100644 index 0000000..e37a952 --- /dev/null +++ b/examples/stm32/f2/jobygps/usart_printf/usart_printf.c @@ -0,0 +1,99 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann , + * Copyright (C) 2011 Piotr Esden-Tempski + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include + +int _write(int file, char *ptr, int len); + +static void clock_setup(void) +{ + /* Enable clocks on all the peripherals we are going to use. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_USART1EN); + rcc_peripheral_enable_clock(&RCC_AHB1ENR, + RCC_AHB1ENR_IOPCEN | RCC_AHB1ENR_IOPAEN); +} + +static void usart_setup(void) +{ + gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO9); + gpio_set_af(GPIOA, GPIO_AF7, GPIO9 | GPIO10); + + /* Setup UART parameters. */ + usart_set_baudrate(USART1, 9600); + usart_set_databits(USART1, 8); + usart_set_stopbits(USART1, USART_STOPBITS_1); + usart_set_parity(USART1, USART_PARITY_NONE); + usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE); + usart_set_mode(USART1, USART_MODE_TX); + + /* Finally enable the USART. */ + usart_enable(USART1); +} + +static void gpio_setup(void) +{ + gpio_mode_setup(GPIOC, GPIO_MODE_OUTPUT, GPIO_MODE_OUTPUT, GPIO3); + gpio_set(GPIOC, GPIO3); +} + +int _write(int file, char *ptr, int len) +{ + int i; + + if (file == 1) { + for (i = 0; i < len; i++) + usart_send_blocking(USART1, ptr[i]); + return i; + } + + errno = EIO; + return -1; +} + +int main(void) +{ + int counter = 0; + float fcounter = 0.0; + double dcounter = 0.0; + + clock_setup(); + gpio_setup(); + usart_setup(); + + /* + * Write Hello World an integer, float and double all over + * again while incrementing the numbers. + */ + while (1) { + gpio_toggle(GPIOC, GPIO3); + printf("Hello World! %i %f %f\r\n", counter, fcounter, + dcounter); + counter++; + fcounter += 0.01; + dcounter += 0.01; + } + + return 0; +} diff --git a/examples/stm32/f4/Makefile.include b/examples/stm32/f4/Makefile.include new file mode 100644 index 0000000..ede4288 --- /dev/null +++ b/examples/stm32/f4/Makefile.include @@ -0,0 +1,158 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## Copyright (C) 2010 Piotr Esden-Tempski +## Copyright (C) 2011 Fergus Noble +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +PREFIX ?= arm-none-eabi +# PREFIX ?= arm-elf +CC = $(PREFIX)-gcc +LD = $(PREFIX)-gcc +OBJCOPY = $(PREFIX)-objcopy +OBJDUMP = $(PREFIX)-objdump +GDB = $(PREFIX)-gdb +FLASH = $(shell which st-flash) + +TOOLCHAIN_DIR ?= ../../../../.. +ifeq ($(wildcard ../../../../../lib/libopencm3_stm32f4.a),) +ifneq ($(strip $(shell which $(CC))),) +TOOLCHAIN_DIR := $(shell dirname `which $(CC)`)/../$(PREFIX) +endif +else +ifeq ($(V),1) +$(info We seem to be building the example in the source directory. Using local library!) +endif +endif + +CFLAGS += -Os -g \ + -Wall -Wextra -Wimplicit-function-declaration \ + -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes \ + -Wundef -Wshadow \ + -I$(TOOLCHAIN_DIR)/include \ + -fno-common -mcpu=cortex-m4 -mthumb \ + -mfloat-abi=hard -mfpu=fpv4-sp-d16 -MD -DSTM32F4 +LDSCRIPT ?= $(BINARY).ld +LDFLAGS += --static -lc -lnosys -L$(TOOLCHAIN_DIR)/lib \ + -L$(TOOLCHAIN_DIR)/lib/stm32/f4 \ + -T$(LDSCRIPT) -nostartfiles -Wl,--gc-sections \ + -mthumb -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 +OBJS += $(BINARY).o + +OOCD ?= openocd +OOCD_INTERFACE ?= flossjtag +OOCD_BOARD ?= olimex_stm32_h103 +# Black magic probe specific variables +# Set the BMP_PORT to a serial port and then BMP is used for flashing +BMP_PORT ?= + +# Be silent per default, but 'make V=1' will show all compiler calls. +ifneq ($(V),1) +Q := @ +NULL := 2>/dev/null +else +LDFLAGS += -Wl,--print-gc-sections +endif + +.SUFFIXES: .elf .bin .hex .srec .list .images +.SECONDEXPANSION: +.SECONDARY: + +all: images + +images: $(BINARY).images +flash: $(BINARY).flash + +%.images: %.bin %.hex %.srec %.list + @#echo "*** $* images generated ***" + +%.bin: %.elf + @#printf " OBJCOPY $(*).bin\n" + $(Q)$(OBJCOPY) -Obinary $(*).elf $(*).bin + +%.hex: %.elf + @#printf " OBJCOPY $(*).hex\n" + $(Q)$(OBJCOPY) -Oihex $(*).elf $(*).hex + +%.srec: %.elf + @#printf " OBJCOPY $(*).srec\n" + $(Q)$(OBJCOPY) -Osrec $(*).elf $(*).srec + +%.list: %.elf + @#printf " OBJDUMP $(*).list\n" + $(Q)$(OBJDUMP) -S $(*).elf > $(*).list + +%.elf: $(OBJS) $(LDSCRIPT) $(TOOLCHAIN_DIR)/lib/libopencm3_stm32f4.a + @#printf " LD $(subst $(shell pwd)/,,$(@))\n" + $(Q)$(LD) -o $(*).elf $(OBJS) -lopencm3_stm32f4 $(LDFLAGS) + +%.o: %.c Makefile + @#printf " CC $(subst $(shell pwd)/,,$(@))\n" + $(Q)$(CC) $(CFLAGS) -o $@ -c $< + +clean: + $(Q)rm -f *.o + $(Q)rm -f *.d + $(Q)rm -f *.elf + $(Q)rm -f *.bin + $(Q)rm -f *.hex + $(Q)rm -f *.srec + $(Q)rm -f *.list + +%.stlink-flash: %.bin + @printf " FLASH $<\n" + $(Q)$(FLASH) write $(*).bin 0x8000000 + + +ifeq ($(BMP_PORT),) +ifeq ($(OOCD_SERIAL),) +%.flash: %.hex + @printf " FLASH $<\n" + @# IMPORTANT: Don't use "resume", only "reset" will work correctly! + $(Q)$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \ + -f board/$(OOCD_BOARD).cfg \ + -c "init" -c "reset init" \ + -c "stm32x mass_erase 0" \ + -c "flash write_image $(*).hex" \ + -c "reset" \ + -c "shutdown" $(NULL) +else +%.flash: %.hex + @printf " FLASH $<\n" + @# IMPORTANT: Don't use "resume", only "reset" will work correctly! + $(Q)$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \ + -f board/$(OOCD_BOARD).cfg \ + -c "ft2232_serial $(OOCD_SERIAL)" \ + -c "init" -c "reset init" \ + -c "stm32x mass_erase 0" \ + -c "flash write_image $(*).hex" \ + -c "reset" \ + -c "shutdown" $(NULL) +endif +else +%.flash: %.elf + @echo " GDB $(*).elf (flash)" + $(Q)$(GDB) --batch \ + -ex 'target extended-remote $(BMP_PORT)' \ + -x $(TOOLCHAIN_DIR)/scripts/black_magic_probe_flash.scr \ + $(*).elf +endif + +.PHONY: images clean + +-include $(OBJS:.o=.d) + diff --git a/examples/stm32/f4/stm32f4-discovery/button/Makefile b/examples/stm32/f4/stm32f4-discovery/button/Makefile new file mode 100644 index 0000000..22377c1 --- /dev/null +++ b/examples/stm32/f4/stm32f4-discovery/button/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = button + +LDSCRIPT = ../stm32f4-discovery.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f4/stm32f4-discovery/button/README b/examples/stm32/f4/stm32f4-discovery/button/README new file mode 100644 index 0000000..b794dda --- /dev/null +++ b/examples/stm32/f4/stm32f4-discovery/button/README @@ -0,0 +1,8 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example blinks the green LED on the ST STM32F4DISCOVERY eval board. + +When you press the 'USER' button, the blinking is slower. + diff --git a/examples/stm32/f4/stm32f4-discovery/button/button.c b/examples/stm32/f4/stm32f4-discovery/button/button.c new file mode 100644 index 0000000..709900d --- /dev/null +++ b/examples/stm32/f4/stm32f4-discovery/button/button.c @@ -0,0 +1,76 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann , + * Copyright (C) 2010 Piotr Esden-Tempski + * Copyright (C) 2011 Stephen Caudle + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include + +u16 exti_line_state; + +/* Set STM32 to 168 MHz. */ +static void clock_setup(void) +{ + rcc_clock_setup_hse_3v3(&hse_8mhz_3v3[CLOCK_3V3_168MHZ]); +} + +static void gpio_setup(void) +{ + /* Enable GPIOD clock. */ + rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN); + + /* Set GPIO12 (in GPIO port D) to 'output push-pull'. */ + gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, + GPIO_PUPD_NONE, GPIO12 | GPIO13 | GPIO14 | GPIO15); +} + +static void button_setup(void) +{ + /* Enable GPIOA clock. */ + rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN); + + /* Set GPIO0 (in GPIO port A) to 'input open-drain'. */ + gpio_mode_setup(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO0); +} + +int main(void) +{ + int i; + + clock_setup(); + button_setup(); + gpio_setup(); + + /* Blink the LED (PD12) on the board. */ + while (1) { + gpio_toggle(GPIOD, GPIO12); + + /* Upon button press, blink more slowly. */ + exti_line_state = GPIOA_IDR; + if ((exti_line_state & (1 << 0)) != 0) { + for (i = 0; i < 3000000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + for (i = 0; i < 3000000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/stm32/f4/stm32f4-discovery/fancyblink/Makefile b/examples/stm32/f4/stm32f4-discovery/fancyblink/Makefile new file mode 100644 index 0000000..d9b0d23 --- /dev/null +++ b/examples/stm32/f4/stm32f4-discovery/fancyblink/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = fancyblink + +LDSCRIPT = ../stm32f4-discovery.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f4/stm32f4-discovery/fancyblink/README b/examples/stm32/f4/stm32f4-discovery/fancyblink/README new file mode 100644 index 0000000..88e24bf --- /dev/null +++ b/examples/stm32/f4/stm32f4-discovery/fancyblink/README @@ -0,0 +1,9 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This is the smallest-possible example program using libopencm3. + +It's intended for the ST STM32F4DISCOVERY eval board. It should blink +the LEDs on the board. + diff --git a/examples/stm32/f4/stm32f4-discovery/fancyblink/fancyblink.c b/examples/stm32/f4/stm32f4-discovery/fancyblink/fancyblink.c new file mode 100644 index 0000000..e7827e5 --- /dev/null +++ b/examples/stm32/f4/stm32f4-discovery/fancyblink/fancyblink.c @@ -0,0 +1,60 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * Copyright (C) 2011 Damjan Marion + * Copyright (C) 2011 Mark Panajotovic + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include + +/* Set STM32 to 168 MHz. */ +static void clock_setup(void) +{ + rcc_clock_setup_hse_3v3(&hse_8mhz_3v3[CLOCK_3V3_168MHZ]); + + /* Enable GPIOD clock. */ + rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN); +} + +static void gpio_setup(void) +{ + /* Set GPIO12-15 (in GPIO port D) to 'output push-pull'. */ + gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, + GPIO_PUPD_NONE, GPIO12 | GPIO13 | GPIO14 | GPIO15); +} + +int main(void) +{ + int i; + + clock_setup(); + gpio_setup(); + + /* Set two LEDs for wigwag effect when toggling. */ + gpio_set(GPIOD, GPIO12 | GPIO14); + + /* Blink the LEDs (PD12, PD13, PD14 and PD15) on the board. */ + while (1) { + /* Toggle LEDs. */ + gpio_toggle(GPIOD, GPIO12 | GPIO13 | GPIO14 | GPIO15); + for (i = 0; i < 6000000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/stm32/f4/stm32f4-discovery/mandelbrot/Makefile b/examples/stm32/f4/stm32f4-discovery/mandelbrot/Makefile new file mode 100644 index 0000000..3879eca --- /dev/null +++ b/examples/stm32/f4/stm32f4-discovery/mandelbrot/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = mandel + +LDSCRIPT = ../stm32f4-discovery.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f4/stm32f4-discovery/mandelbrot/README b/examples/stm32/f4/stm32f4-discovery/mandelbrot/README new file mode 100644 index 0000000..fe4fb13 --- /dev/null +++ b/examples/stm32/f4/stm32f4-discovery/mandelbrot/README @@ -0,0 +1,12 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example program demonstrates the floating point coprocessor usage on +the ST STM32F4DISCOVERY eval board. + +A mandelbrot fractal is calculated and sent as "ascii-art" image through +the USART2. + +The terminal settings for the receiving device/PC are 38400 8n1. + diff --git a/examples/stm32/f4/stm32f4-discovery/mandelbrot/mandel.c b/examples/stm32/f4/stm32f4-discovery/mandelbrot/mandel.c new file mode 100644 index 0000000..024da37 --- /dev/null +++ b/examples/stm32/f4/stm32f4-discovery/mandelbrot/mandel.c @@ -0,0 +1,127 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * Copyright (C) 2011 Stephen Caudle + * Copyright (C) 2012 Daniel Serpell + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include + +static void clock_setup(void) +{ + /* Enable high-speed clock at 120MHz */ + rcc_clock_setup_hse_3v3(&hse_8mhz_3v3[CLOCK_3V3_120MHZ]); + + /* Enable GPIOD clock for LED & USARTs. */ + rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN); + rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN); + + /* Enable clocks for USART2. */ + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN); +} + +static void usart_setup(void) +{ + /* Setup USART2 parameters. */ + usart_set_baudrate(USART2, 38400); + usart_set_databits(USART2, 8); + usart_set_stopbits(USART2, USART_STOPBITS_1); + usart_set_mode(USART2, USART_MODE_TX); + usart_set_parity(USART2, USART_PARITY_NONE); + usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART2); +} + +static void gpio_setup(void) +{ + /* Setup GPIO pin GPIO12 on GPIO port D for LED. */ + gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO12); + + /* Setup GPIO pins for USART2 transmit. */ + gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2); + + /* Setup USART2 TX pin as alternate function. */ + gpio_set_af(GPIOA, GPIO_AF7, GPIO2); +} + +/* Maximum number of iterations for the escape-time calculation */ +#define maxIter 32 +/* This array converts the iteration count to a character representation. */ +static char color[maxIter+1] = " .:++xxXXX%%%%%%################"; + +/* Main mandelbrot calculation */ +static int iterate(float px, float py) +{ + int it=0; + float x=0,y=0; + while(it 4 ) + return it; + // Zn+1 = Zn^2 + P + y = 2*x*y + py; + x = nx - ny + px; + it++; + } + return 0; +} + +static void mandel(float cX, float cY, float scale) +{ + int x,y; + for(x=-60;x<60;x++) + { + for(y=-50;y<50;y++) + { + int i = iterate(cX+x*scale, cY+y*scale); + usart_send_blocking(USART2, color[i]); + } + usart_send_blocking(USART2, '\r'); + usart_send_blocking(USART2, '\n'); + } +} + +int main(void) +{ + float scale = 0.25f, centerX = -0.5f, centerY = 0.0f; + + clock_setup(); + gpio_setup(); + usart_setup(); + + while (1) { + /* Blink the LED (PD12) on the board with each fractal drawn. */ + gpio_toggle(GPIOD, GPIO12); /* LED on/off */ + mandel(centerX,centerY,scale); /* draw mandelbrot */ + + /* Change scale and center */ + centerX += 0.175f * scale; + centerY += 0.522f * scale; + scale *= 0.875f; + + usart_send_blocking(USART2, '\r'); + usart_send_blocking(USART2, '\n'); + } + + return 0; +} diff --git a/examples/stm32/f4/stm32f4-discovery/miniblink/Makefile b/examples/stm32/f4/stm32f4-discovery/miniblink/Makefile new file mode 100644 index 0000000..6f17986 --- /dev/null +++ b/examples/stm32/f4/stm32f4-discovery/miniblink/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = miniblink + +LDSCRIPT = ../stm32f4-discovery.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f4/stm32f4-discovery/miniblink/README b/examples/stm32/f4/stm32f4-discovery/miniblink/README new file mode 100644 index 0000000..cba9d42 --- /dev/null +++ b/examples/stm32/f4/stm32f4-discovery/miniblink/README @@ -0,0 +1,9 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This is the smallest-possible example program using libopencm3. + +It's intended for the ST STM32F4DISCOVERY eval board. It should blink +the blue LED on the board. + diff --git a/examples/stm32/f4/stm32f4-discovery/miniblink/miniblink.c b/examples/stm32/f4/stm32f4-discovery/miniblink/miniblink.c new file mode 100644 index 0000000..22f50c6 --- /dev/null +++ b/examples/stm32/f4/stm32f4-discovery/miniblink/miniblink.c @@ -0,0 +1,71 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * Copyright (C) 2011 Stephen Caudle + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include + +static void gpio_setup(void) +{ + /* Enable GPIOD clock. */ + /* Manually: */ + // RCC_AHB1ENR |= RCC_AHB1ENR_IOPDEN; + /* Using API functions: */ + rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN); + + /* Set GPIO12 (in GPIO port D) to 'output push-pull'. */ + /* Manually: */ + // GPIOD_CRH = (GPIO_CNF_OUTPUT_PUSHPULL << (((8 - 8) * 4) + 2)); + // GPIOD_CRH |= (GPIO_MODE_OUTPUT_2_MHZ << ((8 - 8) * 4)); + /* Using API functions: */ + gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO12); +} + +int main(void) +{ + int i; + + gpio_setup(); + + /* Blink the LED (PC8) on the board. */ + while (1) { + /* Manually: */ + // GPIOD_BSRR = GPIO12; /* LED off */ + // for (i = 0; i < 1000000; i++) /* Wait a bit. */ + // __asm__("nop"); + // GPIOD_BRR = GPIO12; /* LED on */ + // for (i = 0; i < 1000000; i++) /* Wait a bit. */ + // __asm__("nop"); + + /* Using API functions gpio_set()/gpio_clear(): */ + // gpio_set(GPIOD, GPIO12); /* LED off */ + // for (i = 0; i < 1000000; i++) /* Wait a bit. */ + // __asm__("nop"); + // gpio_clear(GPIOD, GPIO12); /* LED on */ + // for (i = 0; i < 1000000; i++) /* Wait a bit. */ + // __asm__("nop"); + + /* Using API function gpio_toggle(): */ + gpio_toggle(GPIOD, GPIO12); /* LED on/off */ + for (i = 0; i < 1000000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/stm32/f4/stm32f4-discovery/random/Makefile b/examples/stm32/f4/stm32f4-discovery/random/Makefile new file mode 100644 index 0000000..4f06835 --- /dev/null +++ b/examples/stm32/f4/stm32f4-discovery/random/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = random + +LDSCRIPT = ../stm32f4-discovery.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f4/stm32f4-discovery/random/README b/examples/stm32/f4/stm32f4-discovery/random/README new file mode 100644 index 0000000..875e6bf --- /dev/null +++ b/examples/stm32/f4/stm32f4-discovery/random/README @@ -0,0 +1,5 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example randomly blinks the green LED on the ST STM32F4DISCOVERY eval board. diff --git a/examples/stm32/f4/stm32f4-discovery/random/random.c b/examples/stm32/f4/stm32f4-discovery/random/random.c new file mode 100644 index 0000000..74f6775 --- /dev/null +++ b/examples/stm32/f4/stm32f4-discovery/random/random.c @@ -0,0 +1,88 @@ +/* + * This file is part of the libopencm3 project. + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + + +#include +#include +#include +#include + +static void rcc_setup(void) +{ + rcc_clock_setup_hse_3v3(&hse_8mhz_3v3[CLOCK_3V3_120MHZ]); + + /* Enable GPIOD clock for onboard leds. */ + rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN); + + /* Enable rng clock */ + rcc_peripheral_enable_clock(&RCC_AHB2ENR, RCC_AHB2ENR_RNGEN); +} + +static void rng_setup(void) +{ + /* Enable interupt */ + /* Set the IE bit in the RNG_CR register. */ + RNG_CR |= RNG_CR_IE; + /* Enable the random number generation by setting the RNGEN bit in the RNG_CR + register. This activates the analog part, the RNG_LFSR and the error detector. + */ + RNG_CR |= RNG_CR_RNGEN; +} + +static void gpio_setup(void) +{ + /* Setup onboard led */ + gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO12 | GPIO13); +} + +/* Tried to folow the guidelines in the stm32f4 user manual.*/ +static u32 random_int(void) +{ + static u32 last_value=0; + static u32 new_value=0; + u32 error_bits = 0; + error_bits = RNG_SR_SEIS | RNG_SR_CEIS; + while (new_value==last_value) { + /* Check for error flags and if data is ready. */ + if ( ((RNG_SR & error_bits) == 0) && ( (RNG_SR & RNG_SR_DRDY) == 1 ) ) + new_value=RNG_DR; + } + last_value=new_value; + return new_value; +} + + +int main(void) +{ + int i,j; + rcc_setup(); + gpio_setup(); + rng_setup(); + while(1){ + u32 rnd; + rnd = random_int(); + for(i=0;i!=32;++i){ + if ( (rnd & (1 << i))!=0 ) + gpio_set(GPIOD, GPIO12); + else + gpio_clear(GPIOD, GPIO12); + /* Delay */ + for(j=0;j!=5000000;++j) + __asm__("nop"); + } + } +} diff --git a/examples/stm32/f4/stm32f4-discovery/stm32f4-discovery.ld b/examples/stm32/f4/stm32f4-discovery/stm32f4-discovery.ld new file mode 100644 index 0000000..927c786 --- /dev/null +++ b/examples/stm32/f4/stm32f4-discovery/stm32f4-discovery.ld @@ -0,0 +1,32 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * Copyright (C) 2011 Stephen Caudle + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* Linker script for ST STM32F4DISCOVERY (STM32F407VG, 1024K flash, 128K RAM). */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 1024K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 128K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_stm32f4.ld + diff --git a/examples/stm32/f4/stm32f4-discovery/usart/Makefile b/examples/stm32/f4/stm32f4-discovery/usart/Makefile new file mode 100644 index 0000000..dd18b4c --- /dev/null +++ b/examples/stm32/f4/stm32f4-discovery/usart/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = usart + +LDSCRIPT = ../stm32f4-discovery.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f4/stm32f4-discovery/usart/README b/examples/stm32/f4/stm32f4-discovery/usart/README new file mode 100644 index 0000000..168b364 --- /dev/null +++ b/examples/stm32/f4/stm32f4-discovery/usart/README @@ -0,0 +1,11 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example program sends some characters on USART2 on the +ST STM32F4DISCOVERY eval board. + +The terminal settings for the receiving device/PC are 38400 8n1. + +The sending is done in a blocking way in the code. + diff --git a/examples/stm32/f4/stm32f4-discovery/usart/usart.c b/examples/stm32/f4/stm32f4-discovery/usart/usart.c new file mode 100644 index 0000000..cac9093 --- /dev/null +++ b/examples/stm32/f4/stm32f4-discovery/usart/usart.c @@ -0,0 +1,83 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * Copyright (C) 2011 Stephen Caudle + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include + +static void clock_setup(void) +{ + /* Enable GPIOD clock for LED & USARTs. */ + rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN); + rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN); + + /* Enable clocks for USART2. */ + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN); +} + +static void usart_setup(void) +{ + /* Setup USART2 parameters. */ + usart_set_baudrate(USART2, 38400); + usart_set_databits(USART2, 8); + usart_set_stopbits(USART2, USART_STOPBITS_1); + usart_set_mode(USART2, USART_MODE_TX); + usart_set_parity(USART2, USART_PARITY_NONE); + usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART2); +} + +static void gpio_setup(void) +{ + /* Setup GPIO pin GPIO12 on GPIO port D for LED. */ + gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO12); + + /* Setup GPIO pins for USART2 transmit. */ + gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2); + + /* Setup USART2 TX pin as alternate function. */ + gpio_set_af(GPIOA, GPIO_AF7, GPIO2); +} + +int main(void) +{ + int i, j = 0, c = 0; + + clock_setup(); + gpio_setup(); + usart_setup(); + + /* Blink the LED (PD12) on the board with every transmitted byte. */ + while (1) { + gpio_toggle(GPIOD, GPIO12); /* LED on/off */ + usart_send_blocking(USART2, c + '0'); /* USART2: Send byte. */ + c = (c == 9) ? 0 : c + 1; /* Increment c. */ + if ((j++ % 80) == 0) { /* Newline after line full. */ + usart_send_blocking(USART2, '\r'); + usart_send_blocking(USART2, '\n'); + } + for (i = 0; i < 3000000; i++) /* Wait a bit. */ + __asm__("NOP"); + } + + return 0; +} diff --git a/examples/stm32/f4/stm32f4-discovery/usart_irq/Makefile b/examples/stm32/f4/stm32f4-discovery/usart_irq/Makefile new file mode 100644 index 0000000..b7bbd14 --- /dev/null +++ b/examples/stm32/f4/stm32f4-discovery/usart_irq/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = usart_irq + +LDSCRIPT = ../stm32f4-discovery.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f4/stm32f4-discovery/usart_irq/README b/examples/stm32/f4/stm32f4-discovery/usart_irq/README new file mode 100644 index 0000000..ac859a9 --- /dev/null +++ b/examples/stm32/f4/stm32f4-discovery/usart_irq/README @@ -0,0 +1,14 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example program echoes data sent in on USART2 on the +ST STM32F4DISCOVERY eval board. Uses interrupts for that purpose. + +Connect your TTL RX device line to PA2 on the discovery board. +Connect your TTL TX device line to PA3 on the discovery board. + +The terminal settings for the receiving device/PC are 38400 8n1. + +The sending is done in a blocking way in the code. + diff --git a/examples/stm32/f4/stm32f4-discovery/usart_irq/usart_irq.c b/examples/stm32/f4/stm32f4-discovery/usart_irq/usart_irq.c new file mode 100644 index 0000000..430fcae --- /dev/null +++ b/examples/stm32/f4/stm32f4-discovery/usart_irq/usart_irq.c @@ -0,0 +1,115 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * Copyright (C) 2011 Stephen Caudle + * Copyright (C) 2013 Piotr Esden-Tempski + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include + +static void clock_setup(void) +{ + /* Enable GPIOD clock for LED & USARTs. */ + rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN); + rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN); + + /* Enable clocks for USART2. */ + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN); +} + +static void usart_setup(void) +{ + /* Enable the USART2 interrupt. */ + nvic_enable_irq(NVIC_USART2_IRQ); + + /* Setup GPIO pins for USART2 transmit. */ + gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2); + + /* Setup GPIO pins for USART2 receive. */ + gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO3); + gpio_set_output_options(GPIOA, GPIO_OTYPE_OD, GPIO_OSPEED_25MHZ, GPIO3); + + /* Setup USART2 TX and RX pin as alternate function. */ + gpio_set_af(GPIOA, GPIO_AF7, GPIO2); + gpio_set_af(GPIOA, GPIO_AF7, GPIO3); + + /* Setup USART2 parameters. */ + usart_set_baudrate(USART2, 38400); + usart_set_databits(USART2, 8); + usart_set_stopbits(USART2, USART_STOPBITS_1); + usart_set_mode(USART2, USART_MODE_TX_RX); + usart_set_parity(USART2, USART_PARITY_NONE); + usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); + + /* Enable USART2 Receive interrupt. */ + usart_enable_rx_interrupt(USART2); + + /* Finally enable the USART. */ + usart_enable(USART2); +} + +static void gpio_setup(void) +{ + /* Setup GPIO pin GPIO12 on GPIO port D for LED. */ + gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO12); +} + +int main(void) +{ + clock_setup(); + gpio_setup(); + usart_setup(); + + while (1) { + __asm__("NOP"); + } + + return 0; +} + +void usart2_isr(void) +{ + static u8 data = 'A'; + + /* Check if we were called because of RXNE. */ + if (((USART_CR1(USART2) & USART_CR1_RXNEIE) != 0) && + ((USART_SR(USART2) & USART_SR_RXNE) != 0)) { + + /* Indicate that we got data. */ + gpio_toggle(GPIOD, GPIO12); + + /* Retrieve the data from the peripheral. */ + data = usart_recv(USART2); + + /* Enable transmit interrupt so it sends back the data. */ + usart_enable_tx_interrupt(USART2); + } + + /* Check if we were called because of TXE. */ + if (((USART_CR1(USART2) & USART_CR1_TXEIE) != 0) && + ((USART_SR(USART2) & USART_SR_TXE) != 0)) { + + /* Put data into the transmit register. */ + usart_send(USART2, data); + + /* Disable the TXE interrupt as we don't need it anymore. */ + usart_disable_tx_interrupt(USART2); + } +} diff --git a/examples/stm32/f4/stm32f4-discovery/usart_irq/usart_irq.c~ b/examples/stm32/f4/stm32f4-discovery/usart_irq/usart_irq.c~ new file mode 100644 index 0000000..5ae46d3 --- /dev/null +++ b/examples/stm32/f4/stm32f4-discovery/usart_irq/usart_irq.c~ @@ -0,0 +1,93 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * Copyright (C) 2011 Stephen Caudle + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include + +void clock_setup(void) +{ + /* Enable GPIOD clock for LED & USARTs. */ + rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN); + rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN); + + /* Enable clocks for USART2. */ + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN); +} + +void usart_setup(void) +{ + /* Setup USART2 parameters. */ + usart_set_baudrate(USART2, 38400); + usart_set_databits(USART2, 8); + usart_set_stopbits(USART2, USART_STOPBITS_1); + usart_set_mode(USART2, USART_MODE_TX); + usart_set_parity(USART2, USART_PARITY_NONE); + usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART2); +} + +void gpio_setup(void) +{ + /* Setup GPIO pin GPIO12 on GPIO port D for LED. */ + gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO12); + + /* Setup GPIO pins for USART2 transmit. */ + gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2); + + + /* Setup GPIO pins for USART2 receive. */ + gpio_mode_setup(GPIOA, GPIO_MODE_INPUT, GPIO_OTYPE_OD, GPIO3); + + /* Setup USART2 TX and RX pin as alternate function. */ + gpio_set_af(GPIOA, GPIO_AF7, GPIO2); + gpio_set_af(GPIOA, GPIO_AF7, GPIO3); +} + +int main(void) +{ + int i, j = 0, c = 0; + u16 in_ch = 0; + + clock_setup(); + gpio_setup(); + usart_setup(); + + /* Blink the LED (PD12) on the board with every transmitted byte. */ + while (1) { + gpio_toggle(GPIOD, GPIO12); /* LED on/off */ + usart_send_blocking(USART2, c + '0'); /* USART2: Send byte. */ + c = (c == 9) ? 0 : c + 1; /* Increment c. */ + if ((j++ % 80) == 0) { /* Newline after line full. */ + usart_send_blocking(USART2, '\r'); + usart_send_blocking(USART2, '\n'); + } + if ((USART_SR(usart) & USART_SR_RXNE) != 0) { + in_ch = usart_recv(USART2); + usart_send_blocking(USART2, in_ch); + } + for (i = 0; i < 3000000; i++) /* Wait a bit. */ + __asm__("NOP"); + } + + return 0; +} diff --git a/examples/stm32/f4/stm32f4-discovery/usb_cdcacm/Makefile b/examples/stm32/f4/stm32f4-discovery/usb_cdcacm/Makefile new file mode 100644 index 0000000..bf398ef --- /dev/null +++ b/examples/stm32/f4/stm32f4-discovery/usb_cdcacm/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = cdcacm + +LDSCRIPT = ../stm32f4-discovery.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f4/stm32f4-discovery/usb_cdcacm/README b/examples/stm32/f4/stm32f4-discovery/usb_cdcacm/README new file mode 100644 index 0000000..2f1ee4b --- /dev/null +++ b/examples/stm32/f4/stm32f4-discovery/usb_cdcacm/README @@ -0,0 +1,7 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example implements a USB CDC-ACM device (aka Virtual Serial Port) +to demonstrate the use of the USB device stack. + diff --git a/examples/stm32/f4/stm32f4-discovery/usb_cdcacm/cdcacm.c b/examples/stm32/f4/stm32f4-discovery/usb_cdcacm/cdcacm.c new file mode 100644 index 0000000..80c700b --- /dev/null +++ b/examples/stm32/f4/stm32f4-discovery/usb_cdcacm/cdcacm.c @@ -0,0 +1,240 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Gareth McMullin + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include + +static const struct usb_device_descriptor dev = { + .bLength = USB_DT_DEVICE_SIZE, + .bDescriptorType = USB_DT_DEVICE, + .bcdUSB = 0x0200, + .bDeviceClass = USB_CLASS_CDC, + .bDeviceSubClass = 0, + .bDeviceProtocol = 0, + .bMaxPacketSize0 = 64, + .idVendor = 0x0483, + .idProduct = 0x5740, + .bcdDevice = 0x0200, + .iManufacturer = 1, + .iProduct = 2, + .iSerialNumber = 3, + .bNumConfigurations = 1, +}; + +/* + * This notification endpoint isn't implemented. According to CDC spec it's + * optional, but its absence causes a NULL pointer dereference in the + * Linux cdc_acm driver. + */ +static const struct usb_endpoint_descriptor comm_endp[] = {{ + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x83, + .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, + .wMaxPacketSize = 16, + .bInterval = 255, +}}; + +static const struct usb_endpoint_descriptor data_endp[] = {{ + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x01, + .bmAttributes = USB_ENDPOINT_ATTR_BULK, + .wMaxPacketSize = 64, + .bInterval = 1, +}, { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x82, + .bmAttributes = USB_ENDPOINT_ATTR_BULK, + .wMaxPacketSize = 64, + .bInterval = 1, +}}; + +static const struct { + struct usb_cdc_header_descriptor header; + struct usb_cdc_call_management_descriptor call_mgmt; + struct usb_cdc_acm_descriptor acm; + struct usb_cdc_union_descriptor cdc_union; +} __attribute__((packed)) cdcacm_functional_descriptors = { + .header = { + .bFunctionLength = sizeof(struct usb_cdc_header_descriptor), + .bDescriptorType = CS_INTERFACE, + .bDescriptorSubtype = USB_CDC_TYPE_HEADER, + .bcdCDC = 0x0110, + }, + .call_mgmt = { + .bFunctionLength = + sizeof(struct usb_cdc_call_management_descriptor), + .bDescriptorType = CS_INTERFACE, + .bDescriptorSubtype = USB_CDC_TYPE_CALL_MANAGEMENT, + .bmCapabilities = 0, + .bDataInterface = 1, + }, + .acm = { + .bFunctionLength = sizeof(struct usb_cdc_acm_descriptor), + .bDescriptorType = CS_INTERFACE, + .bDescriptorSubtype = USB_CDC_TYPE_ACM, + .bmCapabilities = 0, + }, + .cdc_union = { + .bFunctionLength = sizeof(struct usb_cdc_union_descriptor), + .bDescriptorType = CS_INTERFACE, + .bDescriptorSubtype = USB_CDC_TYPE_UNION, + .bControlInterface = 0, + .bSubordinateInterface0 = 1, + } +}; + +static const struct usb_interface_descriptor comm_iface[] = {{ + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 0, + .bAlternateSetting = 0, + .bNumEndpoints = 1, + .bInterfaceClass = USB_CLASS_CDC, + .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM, + .bInterfaceProtocol = USB_CDC_PROTOCOL_AT, + .iInterface = 0, + + .endpoint = comm_endp, + + .extra = &cdcacm_functional_descriptors, + .extralen = sizeof(cdcacm_functional_descriptors) +}}; + +static const struct usb_interface_descriptor data_iface[] = {{ + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 1, + .bAlternateSetting = 0, + .bNumEndpoints = 2, + .bInterfaceClass = USB_CLASS_DATA, + .bInterfaceSubClass = 0, + .bInterfaceProtocol = 0, + .iInterface = 0, + + .endpoint = data_endp, +}}; + +static const struct usb_interface ifaces[] = {{ + .num_altsetting = 1, + .altsetting = comm_iface, +}, { + .num_altsetting = 1, + .altsetting = data_iface, +}}; + +static const struct usb_config_descriptor config = { + .bLength = USB_DT_CONFIGURATION_SIZE, + .bDescriptorType = USB_DT_CONFIGURATION, + .wTotalLength = 0, + .bNumInterfaces = 2, + .bConfigurationValue = 1, + .iConfiguration = 0, + .bmAttributes = 0x80, + .bMaxPower = 0x32, + + .interface = ifaces, +}; + +static const char *usb_strings[] = { + "Black Sphere Technologies", + "CDC-ACM Demo", + "DEMO", +}; + +static int cdcacm_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, u8 **buf, + u16 *len, void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req)) +{ + (void)complete; + (void)buf; + (void)usbd_dev; + + switch (req->bRequest) { + case USB_CDC_REQ_SET_CONTROL_LINE_STATE: { + /* + * This Linux cdc_acm driver requires this to be implemented + * even though it's optional in the CDC spec, and we don't + * advertise it in the ACM functional descriptor. + */ + return 1; + } + case USB_CDC_REQ_SET_LINE_CODING: + if (*len < sizeof(struct usb_cdc_line_coding)) + return 0; + + return 1; + } + return 0; +} + +static void cdcacm_data_rx_cb(usbd_device *usbd_dev, u8 ep) +{ + (void)ep; + + char buf[64]; + int len = usbd_ep_read_packet(usbd_dev, 0x01, buf, 64); + + if (len) { + while (usbd_ep_write_packet(usbd_dev, 0x82, buf, len) == 0) + ; + } + + gpio_toggle(GPIOC, GPIO5); +} + +static void cdcacm_set_config(usbd_device *usbd_dev, u16 wValue) +{ + (void)wValue; + + usbd_ep_setup(usbd_dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64, cdcacm_data_rx_cb); + usbd_ep_setup(usbd_dev, 0x82, USB_ENDPOINT_ATTR_BULK, 64, NULL); + usbd_ep_setup(usbd_dev, 0x83, USB_ENDPOINT_ATTR_INTERRUPT, 16, NULL); + + usbd_register_control_callback( + usbd_dev, + USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE, + USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, + cdcacm_control_request); +} + +int main(void) +{ + usbd_device *usbd_dev; + + rcc_clock_setup_hse_3v3(&hse_8mhz_3v3[CLOCK_3V3_120MHZ]); + + rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN); + rcc_peripheral_enable_clock(&RCC_AHB2ENR, RCC_AHB2ENR_OTGFSEN); + + gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, + GPIO9 | GPIO11 | GPIO12); + gpio_set_af(GPIOA, GPIO_AF10, GPIO9 | GPIO11 | GPIO12); + + usbd_dev = usbd_init(&otgfs_usb_driver, &dev, &config, usb_strings, 3); + usbd_register_set_config_callback(usbd_dev, cdcacm_set_config); + + while (1) + usbd_poll(usbd_dev); +} diff --git a/examples/stm32/l1/Makefile.include b/examples/stm32/l1/Makefile.include new file mode 100644 index 0000000..e65cdb5 --- /dev/null +++ b/examples/stm32/l1/Makefile.include @@ -0,0 +1,168 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## Copyright (C) 2010 Piotr Esden-Tempski +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +PREFIX ?= arm-none-eabi +#PREFIX ?= arm-elf +CC = $(PREFIX)-gcc +LD = $(PREFIX)-gcc +OBJCOPY = $(PREFIX)-objcopy +OBJDUMP = $(PREFIX)-objdump +GDB = $(PREFIX)-gdb + +TOOLCHAIN_DIR ?= ../../../../.. +ifeq ($(wildcard ../../../../../lib/libopencm3_stm32l1.a),) +ifneq ($(strip $(shell which $(CC))),) +TOOLCHAIN_DIR := $(shell dirname `which $(CC)`)/../$(PREFIX) +endif +else +ifeq ($(V),1) +$(info We seem to be building the example in the source directory. Using local library!) +endif +endif + +ARCH_FLAGS = -mthumb -mcpu=cortex-m3 -msoft-float +CFLAGS += -Os -g \ + -Wall -Wextra -Wimplicit-function-declaration \ + -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes \ + -Wundef -Wshadow \ + -I$(TOOLCHAIN_DIR)/include \ + -fno-common $(ARCH_FLAGS) -MD -DSTM32L1 +LDSCRIPT ?= $(BINARY).ld +LDFLAGS += --static -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group \ + -L$(TOOLCHAIN_DIR)/lib \ + -T$(LDSCRIPT) -nostartfiles -Wl,--gc-sections \ + $(ARCH_FLAGS) -mfix-cortex-m3-ldrd + +CFLAGS += -I$(TOOLCHAIN_DIR)/include +LDFLAGS += -L$(TOOLCHAIN_DIR)/lib -L$(TOOLCHAIN_DIR)/lib/stm32/l1 +SCRIPT_DIR = $(TOOLCHAIN_DIR)/share + +OBJS += $(BINARY).o + +OOCD ?= openocd +OOCD_INTERFACE ?= flossjtag +OOCD_BOARD ?= olimex_stm32_h103 +# Black magic probe specific variables +# Set the BMP_PORT to a serial port and then BMP is used for flashing +BMP_PORT ?= +# texane/stlink can be used by uncommenting this... +# or defining it in your own makefiles +#STLINK_PORT ?= :4242 + +# Be silent per default, but 'make V=1' will show all compiler calls. +ifneq ($(V),1) +Q := @ +NULL := 2>/dev/null +else +LDFLAGS += -Wl,--print-gc-sections +endif + +.SUFFIXES: .elf .bin .hex .srec .list .images +.SECONDEXPANSION: +.SECONDARY: + +all: images + +images: $(BINARY).images +flash: $(BINARY).flash + +%.images: %.bin %.hex %.srec %.list + @#echo "*** $* images generated ***" + +%.bin: %.elf + @#printf " OBJCOPY $(*).bin\n" + $(Q)$(OBJCOPY) -Obinary $(*).elf $(*).bin + +%.hex: %.elf + @#printf " OBJCOPY $(*).hex\n" + $(Q)$(OBJCOPY) -Oihex $(*).elf $(*).hex + +%.srec: %.elf + @#printf " OBJCOPY $(*).srec\n" + $(Q)$(OBJCOPY) -Osrec $(*).elf $(*).srec + +%.list: %.elf + @#printf " OBJDUMP $(*).list\n" + $(Q)$(OBJDUMP) -S $(*).elf > $(*).list + +%.elf: $(OBJS) $(LDSCRIPT) $(TOOLCHAIN_DIR)/lib/libopencm3_stm32l1.a + @#printf " LD $(subst $(shell pwd)/,,$(@))\n" + $(Q)$(LD) -o $(*).elf $(OBJS) -lopencm3_stm32l1 $(LDFLAGS) + +%.o: %.c Makefile + @#printf " CC $(subst $(shell pwd)/,,$(@))\n" + $(Q)$(CC) $(CFLAGS) -o $@ -c $< + +clean: + $(Q)rm -f *.o + $(Q)rm -f *.d + $(Q)rm -f *.elf + $(Q)rm -f *.bin + $(Q)rm -f *.hex + $(Q)rm -f *.srec + $(Q)rm -f *.list + +ifeq ($(STLINK_PORT),) +ifeq ($(BMP_PORT),) +ifeq ($(OOCD_SERIAL),) +%.flash: %.hex + @printf " FLASH $<\n" + @# IMPORTANT: Don't use "resume", only "reset" will work correctly! + $(Q)$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \ + -f board/$(OOCD_BOARD).cfg \ + -c "init" -c "reset init" \ + -c "stm32l1x mass_erase 0" \ + -c "flash write_image $(*).hex" \ + -c "reset" \ + -c "shutdown" $(NULL) +else +%.flash: %.hex + @printf " FLASH $<\n" + @# IMPORTANT: Don't use "resume", only "reset" will work correctly! + $(Q)$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \ + -f board/$(OOCD_BOARD).cfg \ + -c "ft2232_serial $(OOCD_SERIAL)" \ + -c "init" -c "reset init" \ + -c "stm32l1x mass_erase 0" \ + -c "flash write_image $(*).hex" \ + -c "reset" \ + -c "shutdown" $(NULL) +endif +else +%.flash: %.elf + @echo " GDB $(*).elf (flash)" + $(Q)$(GDB) --batch \ + -ex 'target extended-remote $(BMP_PORT)' \ + -x $(TOOLCHAIN_DIR)/scripts/black_magic_probe_flash.scr \ + $(*).elf +endif +else +%.flash: %.elf + @echo " GDB $(*).elf (flash)" + $(Q)$(GDB) --batch \ + -ex 'target extended-remote $(STLINK_PORT)' \ + -x $(SCRIPT_DIR)/libopencm3/scripts/stlink_flash.scr \ + $(*).elf +endif + +.PHONY: images clean + +-include $(OBJS:.o=.d) + diff --git a/examples/stm32/l1/stm32l-discovery/button-irq-printf-lowpower/Makefile b/examples/stm32/l1/stm32l-discovery/button-irq-printf-lowpower/Makefile new file mode 100644 index 0000000..d57ea7a --- /dev/null +++ b/examples/stm32/l1/stm32l-discovery/button-irq-printf-lowpower/Makefile @@ -0,0 +1,24 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = main +LDSCRIPT = ../../../../../lib/stm32/l1/stm32l15xxb.ld + +include ../../Makefile.include + diff --git a/examples/stm32/l1/stm32l-discovery/button-irq-printf-lowpower/README b/examples/stm32/l1/stm32l-discovery/button-irq-printf-lowpower/README new file mode 100644 index 0000000..83cccfc --- /dev/null +++ b/examples/stm32/l1/stm32l-discovery/button-irq-printf-lowpower/README @@ -0,0 +1,18 @@ +This is _functionally_ identical to the "button-irq-printf" +example, but has been modified to use some low power features. + +There is a 115200@8n1 console on PA2, which prints a tick count every second, +and when the user push button is pressed, the time it is held down for is +timed (in milliseconds) + +Instead of free running timers and busy loops, this version uses the RTC +module and attempts to sleep as much as possible, including while the button +is pressed. + +Status +~~~~~~ +Only very basic power savings are done! + +Current consumption, led off/on, 16Mhz HSI: 2.7mA/5.4mA +Current consumption, led off/on, 4Mhz HSI: 1.4mA/?.?mA +Current consumption, led off/on, 4Mhz MSI: 0.9mA/?.?mA diff --git a/examples/stm32/l1/stm32l-discovery/button-irq-printf-lowpower/main.c b/examples/stm32/l1/stm32l-discovery/button-irq-printf-lowpower/main.c new file mode 100644 index 0000000..5b73c5b --- /dev/null +++ b/examples/stm32/l1/stm32l-discovery/button-irq-printf-lowpower/main.c @@ -0,0 +1,302 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2012 Karl Palsson + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "syscfg.h" + +static volatile struct state_t state; + +int _write(int file, char *ptr, int len); + +__attribute__((always_inline)) static inline void __WFI(void) +{ + __asm volatile ("wfi"); +} + +static void gpio_setup(void) +{ + /* green led for ticking, blue for button feedback */ + gpio_mode_setup(LED_DISCO_GREEN_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LED_DISCO_GREEN_PIN); + gpio_mode_setup(LED_DISCO_BLUE_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LED_DISCO_BLUE_PIN); + + /* Setup GPIO pins for USART2 transmit. */ + gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2); + + /* Setup USART2 TX pin as alternate function. */ + gpio_set_af(GPIOA, GPIO_AF7, GPIO2); +} + +void BUTTON_DISCO_USER_isr(void) +{ + exti_reset_request(BUTTON_DISCO_USER_EXTI); + state.pressed = true; + if (state.falling) { + state.falling = false; + exti_set_trigger(BUTTON_DISCO_USER_EXTI, EXTI_TRIGGER_RISING); + state.hold_time = TIM_CNT(TIMER_BUTTON_PRESS); + } else { + state.falling = true; + exti_set_trigger(BUTTON_DISCO_USER_EXTI, EXTI_TRIGGER_FALLING); + state.hold_time = TIM_CNT(TIMER_BUTTON_PRESS) = 0; + } +} + +static void setup_buttons(void) +{ + /* Enable EXTI0 interrupt. */ + nvic_enable_irq(BUTTON_DISCO_USER_NVIC); + + gpio_mode_setup(BUTTON_DISCO_USER_PORT, GPIO_MODE_INPUT, GPIO_PUPD_NONE, BUTTON_DISCO_USER_PIN); + + /* Configure the EXTI subsystem. */ + exti_select_source(BUTTON_DISCO_USER_EXTI, BUTTON_DISCO_USER_PORT); + state.falling = false; + exti_set_trigger(BUTTON_DISCO_USER_EXTI, EXTI_TRIGGER_RISING); + exti_enable_request(BUTTON_DISCO_USER_EXTI); +} + +static void usart_setup(void) +{ + usart_set_baudrate(USART_CONSOLE, 115200); + usart_set_databits(USART_CONSOLE, 8); + usart_set_stopbits(USART_CONSOLE, USART_STOPBITS_1); + usart_set_mode(USART_CONSOLE, USART_MODE_TX); + usart_set_parity(USART_CONSOLE, USART_PARITY_NONE); + usart_set_flow_control(USART_CONSOLE, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART_CONSOLE); +} + +/** + * Use USART_CONSOLE as a console. + * @param file + * @param ptr + * @param len + * @return + */ +int _write(int file, char *ptr, int len) +{ + int i; + + if (file == STDOUT_FILENO || file == STDERR_FILENO) { + for (i = 0; i < len; i++) { + if (ptr[i] == '\n') { + usart_send_blocking(USART_CONSOLE, '\r'); + } + usart_send_blocking(USART_CONSOLE, ptr[i]); + } + return i; + } + errno = EIO; + return -1; +} + +/* + * Free running ms timer. + */ +static void setup_button_press_timer(void) +{ + timer_reset(TIMER_BUTTON_PRESS); + timer_set_prescaler(TIMER_BUTTON_PRESS, 3999); // 4Mhz/1000hz - 1 + timer_set_period(TIMER_BUTTON_PRESS, 0xffff); + timer_enable_counter(TIMER_BUTTON_PRESS); +} + +static int setup_rtc(void) +{ + /* turn on power block to enable unlocking */ + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_PWREN); + pwr_disable_backup_domain_write_protect(); + + /* reset rtc */ + RCC_CSR |= RCC_CSR_RTCRST; + RCC_CSR &= ~RCC_CSR_RTCRST; + + /* We want to use the LSE fitted on the discovery board */ + rcc_osc_on(LSE); + rcc_wait_for_osc_ready(LSE); + + /* Select the LSE as rtc clock */ + rcc_rtc_select_clock(RCC_CSR_RTCSEL_LSE); + + /* ?! Stdperiph examples don't turn this on until _afterwards_ which + * simply doesn't work. It must be on at least to be able to configure it */ + RCC_CSR |= RCC_CSR_RTCEN; + + rtc_unlock(); + + /* enter init mode */ + RTC_ISR |= RTC_ISR_INIT; + while ((RTC_ISR & RTC_ISR_INITF) == 0) + ; + + /* set synch prescaler, using defaults for 1Hz out */ + u32 sync = 255; + u32 async = 127; + rtc_set_prescaler(sync, async); + + /* load time and date here if desired, and hour format */ + + /* exit init mode */ + RTC_ISR &= ~(RTC_ISR_INIT); + + /* and write protect again */ + rtc_lock(); + + /* and finally enable the clock */ + RCC_CSR |= RCC_CSR_RTCEN; + + /* And wait for synchro.. */ + rtc_wait_for_synchro(); + return 0; +} + +static int setup_rtc_wakeup(int period) +{ + rtc_unlock(); + + /* ensure wakeup timer is off */ + RTC_CR &= ~RTC_CR_WUTE; + + /* Wait until we can write */ + while ((RTC_ISR & RTC_ISR_WUTWF) == 0) + ; + + RTC_WUTR = period - 1; + + /* Use the 1Hz clock as source */ + RTC_CR &= ~(RTC_CR_WUCLKSEL_MASK << RTC_CR_WUCLKSEL_SHIFT); + RTC_CR |= (RTC_CR_WUCLKSEL_SPRE << RTC_CR_WUCLKSEL_SHIFT); + + /* Restart WakeUp unit */ + RTC_CR |= RTC_CR_WUTE; + + /* interrupt configuration */ + + /* also, let's have an interrupt */ + RTC_CR |= RTC_CR_WUTIE; + + /* done with rtc registers, lock them again */ + rtc_lock(); + + + nvic_enable_irq(NVIC_RTC_WKUP_IRQ); + + // EXTI configuration + /* Configure the EXTI subsystem. */ + // not needed, this chooses ports exti_select_source(EXTI20, BUTTON_DISCO_USER_PORT); + exti_set_trigger(EXTI20, EXTI_TRIGGER_RISING); + exti_enable_request(EXTI20); + return 0; +} + +void rtc_wkup_isr(void) +{ + /* clear flag, not write protected */ + RTC_ISR &= ~(RTC_ISR_WUTF); + exti_reset_request(EXTI20); + state.rtc_ticked = true; +} + +static int process_state(volatile struct state_t *st) +{ + if (st->rtc_ticked) { + st->rtc_ticked = 0; + printf("Tick: %x\n", (unsigned int) RTC_TR); +#if defined(FULL_USER_EXPERIENCE) + gpio_toggle(LED_DISCO_GREEN_PORT, LED_DISCO_GREEN_PIN); +#else + gpio_clear(LED_DISCO_GREEN_PORT, LED_DISCO_GREEN_PIN); +#endif + } + if (st->pressed) { + st->pressed = false; + if (st->falling) { + gpio_set(LED_DISCO_BLUE_PORT, LED_DISCO_BLUE_PIN); + printf("Pushed down!\n"); + } else { + gpio_clear(LED_DISCO_BLUE_PORT, LED_DISCO_BLUE_PIN); + printf("held: %u ms\n", st->hold_time); + } + } + return 0; +} + +static void reset_clocks(void) +{ + /* 4MHz MSI raw range 2*/ + clock_scale_t myclock_config = { + .hpre = RCC_CFGR_HPRE_SYSCLK_NODIV, + .ppre1 = RCC_CFGR_PPRE1_HCLK_NODIV, + .ppre2 = RCC_CFGR_PPRE2_HCLK_NODIV, + .voltage_scale = RANGE2, + .flash_config = FLASH_ACR_LATENCY_0WS, + .apb1_frequency = 4194000, + .apb2_frequency = 4194000, + .msi_range = RCC_ICSCR_MSIRANGE_4MHZ, + }; + rcc_clock_setup_msi(&myclock_config); + + /* buttons and uarts */ + rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_GPIOAEN); + /* user feedback leds */ + rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_GPIOBEN); + /* Enable clocks for USART2. */ + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN); + /* And a timers for button presses */ + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_TIM7EN); +} + +int main(void) +{ + reset_clocks(); + gpio_setup(); + usart_setup(); + setup_buttons(); + setup_button_press_timer(); + printf("we're awake!\n"); + + setup_rtc(); + setup_rtc_wakeup(1); + + while (1) { + PWR_CR |= PWR_CR_LPSDSR; + pwr_set_stop_mode(); + __WFI(); + reset_clocks(); + process_state(&state); + } + + return 0; +} diff --git a/examples/stm32/l1/stm32l-discovery/button-irq-printf-lowpower/syscfg.h b/examples/stm32/l1/stm32l-discovery/button-irq-printf-lowpower/syscfg.h new file mode 100644 index 0000000..8ca634b --- /dev/null +++ b/examples/stm32/l1/stm32l-discovery/button-irq-printf-lowpower/syscfg.h @@ -0,0 +1,61 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2012 Karl Palsson + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#ifndef SYSCFG_H +#define SYSCFG_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include +#include + + +#define USART_CONSOLE USART2 +#define USE_NASTYLOG 1 + +#define LED_DISCO_GREEN_PORT GPIOB +#define LED_DISCO_GREEN_PIN GPIO7 +#define LED_DISCO_BLUE_PORT GPIOB +#define LED_DISCO_BLUE_PIN GPIO6 + +#define BUTTON_DISCO_USER_PORT GPIOA +#define BUTTON_DISCO_USER_PIN GPIO0 +#define BUTTON_DISCO_USER_EXTI EXTI0 +#define BUTTON_DISCO_USER_isr exti0_isr +#define BUTTON_DISCO_USER_NVIC NVIC_EXTI0_IRQ +#define TIMER_BUTTON_PRESS TIM7 + + struct state_t { + bool falling; + bool pressed; + int rtc_ticked; + int hold_time; + }; + + +#ifdef __cplusplus +} +#endif + +#endif /* SYSCFG_H */ + diff --git a/examples/stm32/l1/stm32l-discovery/button-irq-printf/Makefile b/examples/stm32/l1/stm32l-discovery/button-irq-printf/Makefile new file mode 100644 index 0000000..d57ea7a --- /dev/null +++ b/examples/stm32/l1/stm32l-discovery/button-irq-printf/Makefile @@ -0,0 +1,24 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = main +LDSCRIPT = ../../../../../lib/stm32/l1/stm32l15xxb.ld + +include ../../Makefile.include + diff --git a/examples/stm32/l1/stm32l-discovery/button-irq-printf/README b/examples/stm32/l1/stm32l-discovery/button-irq-printf/README new file mode 100644 index 0000000..5de96a2 --- /dev/null +++ b/examples/stm32/l1/stm32l-discovery/button-irq-printf/README @@ -0,0 +1,35 @@ +* Prints to the screen when the button is pushed/released (irq driven) + 115200@8n1 console on PA2 (tx only) +* uses basic timer 6 with overflows to generate a 1ms counter (not an ideal + use, but shows some api methods and can be demoed on the disco board) +* uses basic timer 7 with the exti interrupts to do ghetto input capture. + Not as fast or precise as the real input capture modes, but can be used + on any gpio pin. + +No effort at saving power is made here. Current consumption on the Disco board +is ~7.5mA when the green tick led is off, and about 10.5mA when it is on. + +example output: + +hi guys! +TICK 0 +TICK 1 +TICK 2 +Pushed down! +held: 443 ms +Pushed down! +TICK 3 +held: 217 ms +Pushed down! +held: 99 ms +Pushed down! +TICK 4 +held: 73 ms +Pushed down! +held: 60 ms +TICK 5 +Pushed down! +held: 98 ms +Pushed down! + + diff --git a/examples/stm32/l1/stm32l-discovery/button-irq-printf/main.c b/examples/stm32/l1/stm32l-discovery/button-irq-printf/main.c new file mode 100644 index 0000000..a311d24 --- /dev/null +++ b/examples/stm32/l1/stm32l-discovery/button-irq-printf/main.c @@ -0,0 +1,189 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2012 Karl Palsson + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "syscfg.h" + +static struct state_t state; + +int _write(int file, char *ptr, int len); + +static void clock_setup(void) +{ + rcc_clock_setup_pll(&clock_config[CLOCK_VRANGE1_HSI_PLL_24MHZ]); + /* Lots of things on all ports... */ + rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_GPIOAEN); + rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_GPIOBEN); + + /* Enable clocks for USART2. */ + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN); + + /* And timers. */ + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_TIM6EN); + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_TIM7EN); + +} + +static void gpio_setup(void) +{ + /* green led for ticking, blue for button feedback */ + gpio_mode_setup(LED_DISCO_GREEN_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LED_DISCO_GREEN_PIN); + gpio_mode_setup(LED_DISCO_BLUE_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LED_DISCO_BLUE_PIN); + + /* Setup GPIO pins for USART2 transmit. */ + gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2); + + /* Setup USART2 TX pin as alternate function. */ + gpio_set_af(GPIOA, GPIO_AF7, GPIO2); +} + +static void usart_setup(void) +{ + usart_set_baudrate(USART_CONSOLE, 115200); + usart_set_databits(USART_CONSOLE, 8); + usart_set_stopbits(USART_CONSOLE, USART_STOPBITS_1); + usart_set_mode(USART_CONSOLE, USART_MODE_TX); + usart_set_parity(USART_CONSOLE, USART_PARITY_NONE); + usart_set_flow_control(USART_CONSOLE, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART_CONSOLE); +} + +/** + * Use USART_CONSOLE as a console. + * @param file + * @param ptr + * @param len + * @return + */ +int _write(int file, char *ptr, int len) +{ + int i; + + if (file == STDOUT_FILENO || file == STDERR_FILENO) { + for (i = 0; i < len; i++) { + if (ptr[i] == '\n') { + usart_send_blocking(USART_CONSOLE, '\r'); + } + usart_send_blocking(USART_CONSOLE, ptr[i]); + } + return i; + } + errno = EIO; + return -1; +} + +void BUTTON_DISCO_USER_isr(void) +{ + exti_reset_request(BUTTON_DISCO_USER_EXTI); + if (state.falling) { + gpio_clear(LED_DISCO_BLUE_PORT, LED_DISCO_BLUE_PIN); + state.falling = false; + exti_set_trigger(BUTTON_DISCO_USER_EXTI, EXTI_TRIGGER_RISING); + unsigned int x = TIM_CNT(TIM7); + printf("held: %u ms\n", x); + } else { + gpio_set(LED_DISCO_BLUE_PORT, LED_DISCO_BLUE_PIN); + printf("Pushed down!\n"); + TIM_CNT(TIM7) = 0; + state.falling = true; + exti_set_trigger(BUTTON_DISCO_USER_EXTI, EXTI_TRIGGER_FALLING); + } +} + +static volatile int t6ovf = 0; + +void tim6_isr(void) +{ + TIM_SR(TIM6) &= ~TIM_SR_UIF; + if (t6ovf++ > 1000) { + printf("TICK %d\n", state.tickcount++); + t6ovf = 0; + gpio_toggle(LED_DISCO_GREEN_PORT, LED_DISCO_GREEN_PIN); + } +} + +/* + * Another ms timer, this one used to generate an overflow interrupt at 1ms + * It is used to toggle leds and write tick counts + */ +static void setup_tim6(void) +{ + timer_reset(TIM6); + // 24Mhz / 10khz -1. + timer_set_prescaler(TIM6, 2399); // 24Mhz/10000hz - 1 + // 10khz for 10 ticks = 1 khz overflow = 1ms overflow interrupts + timer_set_period(TIM6, 10); + + nvic_enable_irq(NVIC_TIM6_IRQ); + timer_enable_update_event(TIM6); // default at reset! + timer_enable_irq(TIM6, TIM_DIER_UIE); + timer_enable_counter(TIM6); +} + +/* + * Free running ms timer. + */ +static void setup_tim7(void) +{ + timer_reset(TIM7); + timer_set_prescaler(TIM7, 23999); // 24Mhz/1000hz - 1 + timer_set_period(TIM7, 0xffff); + timer_enable_counter(TIM7); +} + +static void setup_buttons(void) +{ + /* Enable EXTI0 interrupt. */ + nvic_enable_irq(BUTTON_DISCO_USER_NVIC); + + gpio_mode_setup(BUTTON_DISCO_USER_PORT, GPIO_MODE_INPUT, GPIO_PUPD_NONE, BUTTON_DISCO_USER_PIN); + + /* Configure the EXTI subsystem. */ + exti_select_source(BUTTON_DISCO_USER_EXTI, BUTTON_DISCO_USER_PORT); + state.falling = false; + exti_set_trigger(BUTTON_DISCO_USER_EXTI, EXTI_TRIGGER_RISING); + exti_enable_request(BUTTON_DISCO_USER_EXTI); +} + +int main(void) +{ + clock_setup(); + gpio_setup(); + usart_setup(); + printf("hi guys!\n"); + setup_buttons(); + setup_tim6(); + setup_tim7(); + while (1) { + ; + } + + return 0; +} diff --git a/examples/stm32/l1/stm32l-discovery/button-irq-printf/syscfg.h b/examples/stm32/l1/stm32l-discovery/button-irq-printf/syscfg.h new file mode 100644 index 0000000..d278ac4 --- /dev/null +++ b/examples/stm32/l1/stm32l-discovery/button-irq-printf/syscfg.h @@ -0,0 +1,58 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2012 Karl Palsson + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#ifndef SYSCFG_H +#define SYSCFG_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include +#include + + +#define USART_CONSOLE USART2 +#define USE_NASTYLOG 1 + +#define LED_DISCO_GREEN_PORT GPIOB +#define LED_DISCO_GREEN_PIN GPIO7 +#define LED_DISCO_BLUE_PORT GPIOB +#define LED_DISCO_BLUE_PIN GPIO6 + +#define BUTTON_DISCO_USER_PORT GPIOA +#define BUTTON_DISCO_USER_PIN GPIO0 +#define BUTTON_DISCO_USER_EXTI EXTI0 +#define BUTTON_DISCO_USER_isr exti0_isr +#define BUTTON_DISCO_USER_NVIC NVIC_EXTI0_IRQ + + struct state_t { + bool falling; + int tickcount; + }; + + +#ifdef __cplusplus +} +#endif + +#endif /* SYSCFG_H */ + diff --git a/examples/stm32/l1/stm32l-discovery/miniblink/Makefile b/examples/stm32/l1/stm32l-discovery/miniblink/Makefile new file mode 100644 index 0000000..b0586e5 --- /dev/null +++ b/examples/stm32/l1/stm32l-discovery/miniblink/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = miniblink + +LDSCRIPT = ../../../../../lib/stm32/l1/stm32l15xxb.ld + +include ../../Makefile.include + diff --git a/examples/stm32/l1/stm32l-discovery/miniblink/README b/examples/stm32/l1/stm32l-discovery/miniblink/README new file mode 100644 index 0000000..8bdd314 --- /dev/null +++ b/examples/stm32/l1/stm32l-discovery/miniblink/README @@ -0,0 +1,9 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This is the smallest-possible example program using libopencm3. + +It's intended for the ST STM32L-DISCOVERY eval board. It should blink +the blue LED on the board. + diff --git a/examples/stm32/l1/stm32l-discovery/miniblink/miniblink.c b/examples/stm32/l1/stm32l-discovery/miniblink/miniblink.c new file mode 100644 index 0000000..b251559 --- /dev/null +++ b/examples/stm32/l1/stm32l-discovery/miniblink/miniblink.c @@ -0,0 +1,72 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * Copyright (C) 2011 Stephen Caudle + * Copyright (C) 2012 Karl Palsson + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include + +#define PORT_LED GPIOB +#define PIN_LED GPIO6 + +static void gpio_setup(void) +{ + /* Enable GPIOB clock. */ + /* Manually: */ + //RCC_AHBENR |= RCC_AHBENR_GPIOBEN; + /* Using API functions: */ + rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_GPIOBEN); + + /* Set GPIO6 (in GPIO port B) to 'output push-pull'. */ + /* Using API functions: */ + gpio_mode_setup(PORT_LED, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, PIN_LED); +} + +int main(void) +{ + int i; + + gpio_setup(); + + /* Blink the LED (PC8) on the board. */ + while (1) { + /* Manually: */ + // GPIOD_BSRR = GPIO12; /* LED off */ + // for (i = 0; i < 1000000; i++) /* Wait a bit. */ + // __asm__("nop"); + // GPIOD_BRR = GPIO12; /* LED on */ + // for (i = 0; i < 1000000; i++) /* Wait a bit. */ + // __asm__("nop"); + + /* Using API functions gpio_set()/gpio_clear(): */ + // gpio_set(GPIOD, GPIO12); /* LED off */ + // for (i = 0; i < 1000000; i++) /* Wait a bit. */ + // __asm__("nop"); + // gpio_clear(GPIOD, GPIO12); /* LED on */ + // for (i = 0; i < 1000000; i++) /* Wait a bit. */ + // __asm__("nop"); + + /* Using API function gpio_toggle(): */ + gpio_toggle(PORT_LED, PIN_LED); /* LED on/off */ + for (i = 0; i < 1000000; i++) /* Wait a bit. */ + __asm__("nop"); + } + + return 0; +} diff --git a/examples/stm32/l1/stm32l-discovery/usart/Makefile b/examples/stm32/l1/stm32l-discovery/usart/Makefile new file mode 100644 index 0000000..ac088fe --- /dev/null +++ b/examples/stm32/l1/stm32l-discovery/usart/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = usart + +LDSCRIPT = ../../../../../lib/stm32/l1/stm32l15xxb.ld + +include ../../Makefile.include + diff --git a/examples/stm32/l1/stm32l-discovery/usart/README b/examples/stm32/l1/stm32l-discovery/usart/README new file mode 100644 index 0000000..dcbc5d9 --- /dev/null +++ b/examples/stm32/l1/stm32l-discovery/usart/README @@ -0,0 +1,12 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example program sends some characters on USART2 on the +ST STM32L DISCOVERY eval board. (USART2 TX on PA2) + +The terminal settings for the receiving device/PC are 38400 8n1. + +The sending is done in a blocking way in the code, see the usart_irq example +for a more elaborate USART example. + diff --git a/examples/stm32/l1/stm32l-discovery/usart/usart.c b/examples/stm32/l1/stm32l-discovery/usart/usart.c new file mode 100644 index 0000000..faaf309 --- /dev/null +++ b/examples/stm32/l1/stm32l-discovery/usart/usart.c @@ -0,0 +1,84 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * Copyright (C) 2012 Karl Palsson + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include + +static void clock_setup(void) +{ + /* We are running on MSI after boot. */ + /* Enable GPIOD clock for LED & USARTs. */ + rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_GPIOAEN); + rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_GPIOBEN); + + /* Enable clocks for USART2. */ + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN); +} + +static void usart_setup(void) +{ + /* Setup USART2 parameters. */ + usart_set_baudrate(USART2, 38400); + usart_set_databits(USART2, 8); + usart_set_stopbits(USART2, USART_STOPBITS_1); + usart_set_mode(USART2, USART_MODE_TX); + usart_set_parity(USART2, USART_PARITY_NONE); + usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART2); +} + +static void gpio_setup(void) +{ + /* Setup GPIO pin GPIO7 on GPIO port B for Green LED. */ + gpio_mode_setup(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO7); + + /* Setup GPIO pins for USART2 transmit. */ + gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2); + + /* Setup USART2 TX pin as alternate function. */ + gpio_set_af(GPIOA, GPIO_AF7, GPIO2); +} + +int main(void) +{ + int i, j = 0, c = 0; + + clock_setup(); + gpio_setup(); + usart_setup(); + + /* Blink the LED (PD12) on the board with every transmitted byte. */ + while (1) { + gpio_toggle(GPIOB, GPIO7); /* LED on/off */ + usart_send_blocking(USART2, c + '0'); /* USART2: Send byte. */ + c = (c == 9) ? 0 : c + 1; /* Increment c. */ + if ((j++ % 80) == 0) { /* Newline after line full. */ + usart_send_blocking(USART2, '\r'); + usart_send_blocking(USART2, '\n'); + } + for (i = 0; i < 100000; i++) /* Wait a bit. */ + __asm__("NOP"); + } + + return 0; +} diff --git a/libopencm3 b/libopencm3 new file mode 160000 index 0000000..67903bf --- /dev/null +++ b/libopencm3 @@ -0,0 +1 @@ +Subproject commit 67903bfbfe3d70ac9eb5d2d0c14a14da39945846