Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
Digital embedded electronics
J.-M Friedt
FEMTO-ST/time & frequency department
slides at jmfriedt.free.fr
November 25, 2021
1 / 14
List of presentations:
7 lessons introducing baremetal programming of the STM32:
1. Digital electronics and circuit designL3/bachelor: analog aspects, power supply and consumption, datasheet analysisOverview of the various peripherals (RS232, SPI, timer, ADC)data format (size/encoding), masks, architecture
Reminders on Atmega32U4 (Makefile, compilation, masks ...)
2. First steps with the STM32, peripheral addresses, internal architecture
3. gcc operation and optimizations:preprocess–compiler–assembler–linker, C to assembly language translation, pointers
4. libraries and softwware architecture (separating algorithm/hardware), simulators & stubslibopencm3, newlib & stubs, resources needed to execute libraries
5. Communication bus parallel/serial, synchronous/asynchrone
6. arithmetics on embedded systemsintegers v.s floating point number, converting an algorithm expressed with floats to integers, timers
7. interrupts and analog data acquisition, sampling rateinterrupt vectors, clock management on STM32, ADC
2 / 14
Communication protocolsParallel protocol:▶ one bit/wire: extensive use of hardware resources (parallel bus)▶ inductive coupling between adjacent signal at high data rates ⇒ isolation (differential pairs in
LVDS, ground between signals in SCSI)
DJIdronecamera
Serial protocols: bits are sequentially transmitted on 1 or 2 wire(s).Asynchronous v.s synchronous:▶ asynchronous: no clock sharing (historically, over telephone lines)▶ synchronous: clock shared between speakers ⇒ higher data rates 3 / 14
Asynchronous protocol (RS232)1▶ No common clock ⇒ data rate defined by agreement between speakers (baudrate)▶ Beginning: idle state (1) → start bit (0)▶ N data bits, N ∈ {7, 8}, LSB first▶ optional parity bit (E(ven), O(dd), N(one))▶ 1 or 2 stop bit(s) (1)▶ Exercise: what is the duration of a single byte transmitted at 115200 bauds in 8N1?
t0 1T
ST
AR
T
1 0 0 0
2T 3T 4T 5T 6T 7T 8T
lsb msb
9T
ST
OP
1 0 01
T=1000/baud ms1gtvhacker, Hack All The Things: 20 Devices in 45 Minutes, DEF CON 22 (2014) at
https://www.youtube.com/watch?v=h5PRvBpLuJs 4 / 14
RS232 sentence analysis
What is the content of this message?
5 / 14
RS232 sentence analysis
1. identify baudrate (shortest symbol in frame)
2. do not forget start (“s”) and stop (“S”) bits
3. resynchronization on falling edge of start bit
4. possibly parity and 2 stop bits (not used here)
6 / 14
Synchronous protocols (I2C, SPI)Hardware access: degrees of freedom
▶ clock idle state, sampling edge: defined in the I2C protocol, tunable in SPI (CPHA, CPOL)
▶ bit order: I2C : MSB first = SPI: LSB or MSB first
▶ SPI slave selected with CS# signal, I2C with address of receiver defined by hardware settings
Example of XE1203F 2: protocol close to SPI1 0 R/W# A4-A0 D7-D0
2https://datasheet.digchip.com/531/531-00053-0-XE1203.pdf7 / 14
Example: SPIXE1203F on SPI2 3 (PB12 .. PB15), manual setting of CS##i n c l u d e <l i bopencm3 /stm32/ s p i . h>
s t a t i c vo i d c l o c k s e t u p ( vo i d ){r c c c l o c k s e t u p i n h s e 8mhz ou t 7 2mhz ( ) ; // STM32F103r c c p e r i p h c l o c k e n a b l e (RCC GPIOB) ; // SPI2 = PORTBr c c p e r i p h c l o c k e n a b l e (RCC AFIO) ;r c c p e r i p h c l o c k e n a b l e ( RCC SPI2 ) ;
}
s t a t i c vo i d s p i s e t u p ( vo i d ) { // SS=PB12 , SCK=PB13 , MISO=PB14 and MOSI=PB15 ∗/gp i o s e t mode (GPIOB ,GPIO MODE OUTPUT 50 MHZ ,GPIO CNF OUTPUT ALTFN PUSHPULL , GPIO13|GPIO15 ) ;gp i o s e t mode (GPIOB , GPIO MODE OUTPUT 2 MHZ, GPIO CNF OUTPUT PUSHPULL , GPIO12 ) ;gp i o s e t mode (GPIOB , GPIO MODE INPUT , GPIO CNF INPUT FLOAT , GPIO14 ) ;s p i r e s e t ( SPI2 ) ;s p i i n i t m a s t e r ( SPI2 , SPI CR1 BAUDRATE FPCLK DIV 128 , SPI CR1 CPOL CLK TO 0 WHEN IDLE ,
SPI CR1 CPHA CLK TRANSITION 2 , SPI CR1 DFF 8BIT , SPI CR1 MSBFIRST ) ;s p i e n a b l e ( SPI2 ) ;
}
i n t s p i t x r x ( cha r c ){ s p i s e n d ( SPI2 , c ) ;
r e t u r n ( s p i r e a d ( SPI2 ) ) ; // a lways read a f t e r w r i t e to avo i d a v e r f l ow}
i n t main ( ){ [ . . . ]g p i o c l e a r (GPIOB , GPIO12 ) ;s p i t x r x (0 x80+(addr&0x1 f ) ) ; // w r i t e#s p i t x r x ( 0x55 ) ;s p i t x r x ( 0 x f f ) ;g p i o s e t (GPIOB , GPIO12 ) ;
}
3RM0008 at https://www.st.com/resource/en/reference_manual/cd00171190-stm32f101xx-stm32f102xx-stm32f103xx-stm32f105xx-and-stm32f107xx-advanced-arm-based-32-bit-mcus-stmicroelectronics.
pdf8 / 14
Reading Writing
9 / 14
STM32 asynchronous port initialization
https://www.st.com/resource/en/datasheet/
stm32f410cb.pdf
1. activate peripheral clock
2. set pin alternative function
3. configure peripheral (8N1, 115200 bauds)
#i n c l u d e <l i bopencm3 /stm32/ gp io . h>#i n c l u d e <l i bopencm3 /stm32/ u s a r t . h>
r c c p e r i p h c l o c k e n a b l e (RCC USART1) ;gp io mode se tup (GPIOA , GPIO MODE AF , GPIO PUPD NONE , GPIO9) ; //TXgp io mode se tup (GPIOA , GPIO MODE AF , GPIO PUPD NONE , GPIO10 ) ; //RXg p i o s e t a f (GPIOA , GPIO AF7 , GPIO9) ;g p i o s e t a f (GPIOA , GPIO AF7 , GPIO10 ) ;
u s a r t s e t b a u d r a t e (USART1, 115200) ;u s a r t s e t d a t a b i t s (USART1, 8) ;u s a r t s e t s t o p b i t s (USART1, USART STOPBITS 1) ;u s a r t s e t mode (USART1, USART MODE TX RX) ;u s a r t s e t p a r i t y (USART1, USART PARITY NONE) ;u s a r t s e t f l o w c o n t r o l (USART1, USART FLOWCONTROL NONE) ;u s a r t e n a b l e (USART1) ;
https://www.st.com/resource/en/reference_manual/dm00180366.pdf section 24 10 / 14
STM32 synchronous port initialization
https://www.st.com/resource/en/datasheet/
stm32f410cb.pdf
1. activate peripheral clock
2. set pin alternative function
3. set datarate (synchronous communication)
4. clock idle state and data sampling edge
#i n c l u d e <l i bopencm3 /stm32/ gp io . h>#i n c l u d e <l i bopencm3 /stm32/ s p i . h>r c c p e r i p h c l o c k e n a b l e ( RCC SPI1 ) ;gp io mode se tup (GPIOA ,GPIO MODE AF , GPIO PUPD PULLUP , GPIO5|GPIO6|GPIO7) ;g p i o s e t a f (GPIOA , GPIO AF5 , GPIO5|GPIO6|GPIO7) ; // A5=SCK 6=MISO 7=MOSIs p i d i s a b l e ( SPI1 ) ;s p i i n i t m a s t e r ( SPI1 ,
SPI CR1 BAUDRATE FPCLK DIV 256 , SPI CR1 CPOL CLK TO 1 WHEN IDLE , // c l k=1 when i d l eSPI CR1 CPHA CLK TRANSITION 1 , // f a l l i n g edge => T r a n s i t i o n 1SPI CR1 DFF 8BIT , // 8 b i t sSPI CR1 MSBFIRST ) ; // MSB f i r s t
s p i e n a b l e s o f twa r e s l a v e manag emen t ( SPI1 ) ;s p i s e t n s s h i g h ( SPI1 ) ;s p i e n a b l e ( SPI1 ) ;
https://www.st.com/resource/en/reference_manual/dm00180366.pdf section 25 11 / 14
RS232 communication4 (32U4)#i n c l u d e "avr_mcu_section.h"
AVR MCU(F CPU , "atmega32" ) ;AVR MCU VCD FILE( "trace_file.vcd" , 1000) ;con s t s t r u c t a v r mmcu vcd t r a c e t myt race [ ] MMCU = {
{ AVR MCU VCD SYMBOL( "UDR1" ) , . what = ( vo i d ∗)&UDR1, } , };
to declare the register being monitored (UDR1), then#de f i n e UART BAUDRATE (57600)//#d e f i n e UART BAUDRATE (9600)
vo i d i n i t u a r t ( vo i d ){uns i gned s h o r t baud ;UCSR1A = (1<<UDRE1) ; // impo r t a n t l y U2X1 = 0UCSR1B = (1 << RXEN1) | (1 << TXEN1) ; // enab l e r e c e i v e r and t r a n sm i t t e rUCSR1C = (1<<UCSZ11)|(1<<UCSZ10) ; // 8N1 // UCSR1D = 0 ; // no r t c / c t sbaud = ( ( ( ( F CPU / ( UART BAUDRATE ∗ 16UL) ) ) − 1) ) ;UBRR1H = ( uns i gned char ) ( baud>>8); UBRR1L = ( uns i gned char ) baud ;
}
vo i d u a r t t r a n sm i t ( uns i gned char data ){wh i l e ( ! ( UCSR1A & (1<<UDRE1) ) ) ; // wa i t wh i l e r e g i s t e r i s f r e eUDR1 = data ; // l oad data i n the r e g i s t e r
}
vo i d s end by t e ( uns i gned char c ){uns i gned char tmp ;tmp=c>>4; i f ( tmp<10) u a r t t r a n sm i t ( tmp+’0’ ) ; e l s e u a r t t r a n sm i t ( tmp+’A’−10) ;tmp=(c&0x0 f ) ; i f ( tmp<10) u a r t t r a n sm i t ( tmp+’0’ ) ; e l s e u a r t t r a n sm i t ( tmp+’A’−10) ;
}
57600 bauds:13 ms
xx 46 39 44 38 30TimeUDR1[7:0]=44
0,188 ms
9600 bauds:10 ms 20 ms
xx 46 39 44 38 30TimeUDR1[7:0]=39
1,234 ms
4J.-M Friedt, Developper sur microcontroleur sans microcontroleur : les emulateurs, GNU/Linux Magazine FranceHS 103 (Juillet-Aout 2019) 12 / 14
Laboratory session1. Configure SPI communication port2. Send a message on the SPI bus3. Check on the oscilloscope the frames transmitted over MOSI after having identified the appropriate
pin on the connector + consistency with CS# signal?4. Observe the impact of changing CPHA and CPOL on the clock signal with respect to MOSI5. Observe RS232 sentence and check the
consistency with transmitted messages (manualdecoding)
6. Write to a register of the XE1203 (SPI2) andread back the value to check it has beenproperly stored
7. CS# is manually handled by toggling PB12(Make sure to manually reset the microcontrollerafter transferring the program to flash memory)
{c}SENSeOR2011
U$1
HM
C470LP
3
U1
R1
R2
R3
C1
C2
C3
C4 C5C6
C7
C8
C11
Q3
C15 L
2
C16
C17C18 C19
C20
C21
C22
L3
L4
C25
L5
L6
C26
C27
L7
R5
C28
C29
C30
U$4
U$5
RE
SE
T
R8
C33
C34
C36 DLOAD
C10
C12
C13
D1
R4
R6
IC4
C39
X4
C9
C14
C23
C24
C31
C32
C35
C37 C38
Q4
Q5
Q_32K
_S
MD
R7
Xe1203
IC5
X2
Q1
R9
AD694
R10
X1 C40
TLV
R11
R12
R16R17
AD
M809
R13
R14
R15
D2
L1
STM32F10XRXT6
5600
5600
NC
13p
13p
13p
13p
100n
100n
100n
100n
100n
39MHz
5.6
p 22n
47p
4.7p
22p
6.8p
1p
68p
1u
22n
22n
6.8p
33n 33n
1.5p
1.5p 100n
1.2
k220p
3.3
n
1p
SAW3.3
SA
W3.3
RE
SE
T
0
100n
47u
NC
100p 100p
100p
1k
1K
FT232RL
100n
MINI-USB-SHIELD-UX60-MB-5ST
1.5
p1.5
p1.5
p1.5
p1.5
p1.5
p1.5
p
1.5
p
100nQ_8MHZ_SMD
5600
2n2222
5600
100n
5K
15k
4k710k
15k
NC
NC
BB
141
SCLK MOSIMISO CS#
UART1
13 / 14
14 / 14