Example #1
0
# to STM32 memory locations and device registers
#
# Created on a Pyboard (PYB) v1.1 (with STM32F405RGT6 microcontroller)
# but should be readily adaptable to other Micropython systems
#
# External hardware requirement: LED via series resistor (e.g. 330 ohm)
# to ground from X1 (PA0)
#
# See README.md for further information

import pyb
import iol

# Register definitions used throughout module

ahb1enr = iol.Reg("RCC.AHB1ENR")
apb1enr = iol.Reg("RCC.APB1ENR")

pa_moder = iol.RegArr("GPIOA.MODER", 16, 2)
pa_odr = iol.Reg("GPIOA.ODR", 16)

# Port bit definition used throughout module

PA_OUT = const(0)  # External pin X1

# Interrupt callback variables

source = None
event = None
ctr = 0
Example #2
0
if not 1 <= ADC_CONVS <= 16:
    raise ValueError("ADC number of conversions must be 1-16")

ADC_SQRL = const(17)  # Sequence length field in SQR

ADC_SAMP_VALS = (3, 15, 28, 56, 84, 112, 144, 480)  # Valid settings
ADC_SMP = ADC_SAMP_VALS.index(ADC_SAMP_CYCS)  # SMP field is 0-7

# Temperature sensor

TEMP_AIN = const(16)  # For STM32F40x and STM32F41x

# Register definitions

ahb1enr = iol.Reg("RCC.AHB1ENR")
apb2enr = iol.Reg("RCC.APB2ENR")

pa_moder = iol.RegArr("GPIOA.MODER", 16, 2)
pa_odr = iol.Reg("GPIOA.ODR", 16)

adc1_cr1 = iol.Reg("ADC1.CR1")
adc1_cr2 = iol.Reg("ADC1.CR2")
adc1_sqr = iol.RegArr("ADC1.SQR3, SQR2, SQR1", 6, 5)
adc1_smpr = iol.RegArr("ADC1.SMPR2, SMPR1", 10, 3)

adc1_sr = iol.Reg("ADC1.SR", 8)  # Read only (else must be 32)
adc1_dr = iol.Reg("ADC1.DR", 16)  # Read only (else must be 32)

adc_ccr = iol.Reg("ADC.CCR")  # Common control register ADC123
Example #3
0
import utime
import pyb
import iol

# Get APB1 clock frequency in Hz

apb1_clk = pyb.freq()[2]  # Get pclk1 value from tuple

# Port bit definitions

PA_TXD = const(0)  # External pin X1
PA_RXD = const(1)  # External pin X2

# Register definitions

pa_enr = iol.Reg("RCC.AHB1ENR")
ua_enr = iol.Reg("RCC.APB1ENR")

pa_moder = iol.RegArr("GPIOA.MODER", 16, 2)
pa_afr = iol.RegArr("GPIOA.AFR0,AFR1", 8, 4)
pa_pupdr = iol.RegArr("GPIOA.PUPDR", 16, 2)
pa_odr = iol.Reg("GPIOA.ODR")
pa_idr = iol.Reg("GPIOA.IDR")

ua_brr = iol.Reg("UART4.BRR")
ua_cr1 = iol.Reg("UART4.CR1")
ua_sr = iol.Reg("UART4.SR")
ua_dr = iol.Reg("UART4.DR")

# Calculate BRR value from clock and baud rate (assuming OVER8 = 0)
Example #4
0
# to ground from X1 (PA0) and "loop-back" connection from X1 to Y1 (PC6)
# via series resistor (e.g. 1k) or jumper wire
#
# See README.md for further information

from micropython import const
import utime
import pyb
import iol

# Get timer clock frequency in Hz
# Note that timer clock is doubled if bus prescaler factor != 1
# as per section 7.2 ("Clocks") of RM0090 Reference Manual

tmr_apb1_clk = pyb.freq()[2]  # Get pclk1 value from tuple
cfgr = iol.Reg("RCC.CFGR")  # Need PPRE1 field for APB1
if cfgr[12] == 1:  # If bits [12:10] != 0xx ...
    tmr_apb1_clk *= 2  # .. then adjust timer clock
del cfgr

# Timer parameters

PSC_CLK = const(5000)  # Prescaler clock frequency
psc_val = tmr_apb1_clk // PSC_CLK  # Calculate prescaler value
tmr_sec = tmr_apb1_clk // psc_val  # Timer period for 1 second

# Port bit definitions

PA_RED = const(13)  # Internal LEDs
PA_GRN = const(14)
PA_YEL = const(15)
Example #5
0
# Get bus clock frequency in Hz

apb1_clk = pyb.freq()[2]  # Get pclk1 value from tuple

# Port bit definitions

PB_AVDD = const(5)  # Power to MMA7660 accelerometer
# (built into Pyboard v1.1)

PB_SCL = const(6)  # I2C SCL line (X9)
PB_SDA = const(7)  # I2C SDA line (X10)

# Register definitions

ahb1enr = iol.Reg("RCC.AHB1ENR")
apb1enr = iol.Reg("RCC.APB1ENR")

pb_moder = iol.RegArr("GPIOB.MODER", 16, 2)
pb_afr = iol.RegArr("GPIOB.AFR0,AFR1", 8, 4)
pb_otyper = iol.Reg("GPIOB.OTYPER")
pb_pupdr = iol.RegArr("GPIOB.PUPDR", 16, 2)
pb_odr = iol.Reg("GPIOB.ODR", 16)

i2c_cr1 = iol.Reg("I2C1.CR1")
i2c_cr2 = iol.Reg("I2C1.CR2")
i2c_sr1 = iol.Reg("I2C1.SR1")
i2c_sr2 = iol.Reg("I2C1.SR2")
i2c_dr = iol.Reg("I2C1.DR")
i2c_ccr = iol.Reg("I2C1.CCR")
i2c_trise = iol.Reg("I2C1.TRISE")
Example #6
0
import iol

# Get bus clock frequency in Hz

apb1_clk = pyb.freq()[2]  # Get pclk1 value from tuple

# Port bit definitions

PB_NSS = const(12)  # SPI slave CSB line (Y5)
PB_SCK = const(13)  # SPI slave SCK line (Y6)
PB_MISO = const(14)  # SPI slave SDO line (Y7)
PB_MOSI = const(15)  # SPI slave SDI line (Y8)

# Register definitions

ahb1enr = iol.Reg("RCC.AHB1ENR")
apb1enr = iol.Reg("RCC.APB1ENR")

pb_moder = iol.RegArr("GPIOB.MODER", 16, 2)
pb_ospeedr = iol.RegArr("GPIOB.OSPEEDR", 16, 2)
pb_afr = iol.RegArr("GPIOB.AFR0,AFR1", 8, 4)
pb_odr = iol.Reg("GPIOB.ODR", 16)

spi_cr1 = iol.Reg("SPI2.CR1")
spi_cr2 = iol.Reg("SPI2.CR2")
spi_sr = iol.Reg("SPI2.SR")
spi_dr = iol.Reg("SPI2.DR")

# BMP280 register addresses (omitting calibration registers)

BMP280_TEMP_XLSB = const(0xFC)
Example #7
0
# Created on a Pyboard (PYB) v1.1 (with STM32F405RGT6 microcontroller)
# but should be readily adaptable to other Micropython systems
#
# See README.md for further information

from micropython import const
import pyb
import iol

# Put crystal frequency here

XTAL_FREQ = const(12000000)

# RCC clock configuration registers

pll_cfgr = iol.Reg("RCC.PLLCFGR")
rcc_cfgr = iol.Reg("RCC.CFGR")

# Obtain PLL field values

pll_q = pll_cfgr[27:24]
pll_p = pll_cfgr[17:16]
pll_n = pll_cfgr[14:6]
pll_m = pll_cfgr[5:0]

# Calculate PLL multiplier and divider factors

pll_p_div = (1 << (pll_p + 1))

if not 2 <= pll_q <= 15:
    raise ValueError("pll_q is {}, not in range 2-15}".format(pll_q))
Example #8
0

from micropython import const
import pyb
import iol


# Port bit definitions

PA_RED = const(13)
PB_SW = const(3)


# Register definitions

ahb1enr = iol.Reg("RCC.AHB1ENR")

pa_moder = iol.RegArr("GPIOA.MODER", 16, 2)
pa_odr = iol.Reg("GPIOA.ODR")
pa_set = iol.Reg("GPIOA.BSRRL")     # Note: BSRRL *sets* output!
pa_clr = iol.Reg("GPIOA.BSRRH")     # Note: BSRRH *resets* output!

pb_moder = iol.RegArr("GPIOB.MODER", 16, 2)
pb_idr = iol.Reg("GPIOB.IDR")


# Basic flash example

def flash_basic():
    ahb1enr[0] = 1                  # Port A clock
    pa_moder[PA_RED] = 1            # Output