Professional Documents
Culture Documents
https://www.mouser.com/ProductDetail/GCT/USB3075-30-
A?qs=KUoIvG%2F9IlbGMJplbp1ybA%3D%3D
4. Tactile Switches
https://www.mouser.com/ProductDetail/CK/KSR221GLFS?qs=vOd3edNlRKBvF0VngmgZ
kw%3D%3D
7. Thick Film Resistors - SMD Thick Film Resistors - SMD 1/16watt 4.7Koh
https://www.mouser.com/ProductDetail/Vishay-
Dale/CRCW04024K70JNEDHP?qs=k2%2FDWSARqgGhgirK%2FulFyA%3D%3D
8. Thick Film Resistors - SMD Thick Film Resistors - SMD 10 kOhms 62.5m
https://www.mouser.com/ProductDetail/YAGEO/RC0402FR-
0710KL?qs=I1mnnYJTTsxUoNwrUsQExA%3D%3D
9. Standard LEDs
https://www.mouser.com/ProductDetail/Bivar/SM0805UBWC?qs=hWDdE2Pc5RAgQGa
mDSfd%252BQ%3D%3D
15. Thick Film Resistors - SMD Thick Film Resistors - SMD 27 OHM 1%
https://www.mouser.com/ProductDetail/Bourns/CR0402-FX-
27R0GLF?qs=SFHnMgm9IsxG%252BvpJZf0h2g%3D%3D
I2C1_SDA PB7 I2C Data I/O Alternate Function AF1 (Open Drain)
The I2C is a synchronous serial protocol. It uses only 2 bidirectional open drain pins for data
transmission. The serial Clock (SCL) role is to synchronize the data transfer between different
chips, and the Serial Data for data transfer. The implementation of the I2C protocol requires
two pull-up resistors (4.7Kohm for our design) on each line. In this project, only one master was
used to interface with at least 2 I2C devices. The master device generates the clock and initiates
and terminates the transmission. The slave receives the clock. The master and slave devices can
both receive or transmit data. Two slave devices (Proximity sensor and GPS MAX-8C) were used
in our system and both slave devices can only write to the master (the Microcontroller). In the
I2C protocol, the data line (SDA) changes only when the clock line (SCK) is LOW. SDA cannot
change when the clock line is HIGH. Notice that the START and STOP conditions are an
exception to the above rule. The START and STOP conditions are generated by setting the SCL
and SDA lines HIGH. With the SCL line HIGH, the START condition is generated by a HIGH-to-
LOW change in the SDA line and the STOP condition is generated by a LOW-to-HIGH change on
In I2C the address byte must be framed in 9-bit long with the first 8-bit put on the SDA line and
the 9th for acknowledging or not-acknowledge. If the receiver pulls the SDA line LOW, it
indicates the acknowledgment, but when the receiver does not pull the SDA line LOW it is
We said previously that the address frame is 9 bits long and 8 bits are put on the SDA line by
the transmitter. The 8th bit is the READ/WRITE control bit. Master (the Microcontroller) can
read from slave (Proximity sensor/GPS) only if the 8th bit is HIGH. Master writes to a slave only
when the 8th bit is LOW. Only the 8th bit can define the status of the 9th bit (acknowledge for
Same as the address byte, the data byte format is 9 bits long. The first 8 bits are the data to be
ACK/
START Slave Address R/W Data ACK STOP
NACK
R 0
1 1
• Any information transmitted on the SDA line must be EIGHT bits long.
• The number of bytes that can be transmitted per transfer is UNRESTRICTED.
• Each byte must be followed by an acknowledge (ACK) bit.
• Data is transferred with the MSB (Most Significant Bit) first.
• The address frame is first in any new communication sequence
• For a 7-bit address, the address is sent out a significant bit (MSB) first, followed by an
R/W bit indicating whether this is a Read (1) or Write (0) operation.
• The data frame begins transmission after the address frame is sent
• The master will simply continue generating clock pulses on SCL at regular intervals, and
the data will be placed on SDA by either the master or the slave, depending on whether
the R/W bit indicated a Read or Write operation.
• This is the speed of the I2C interface and should correspond with the bus speeds defined
in the I2C specification
• The specification defines the following modes:
Standard-Mode 100 kHz max
• Specifies the ratio between the low and tHIGH of the I2C SCL line.
• Possible values:
I2C_DUTYCYCLE_2 2:1
I2C_DUTYCYCLE_2 16:9
• By choosing the appropriate duty cycle we can pre-scale the peripheral clock to
achieve the desired I2C speed.
Datasheet:
Access line ultra-low-power 32-bit MCU Arm®-based Cortex®-M0+, up to 32KB Flash, 8KB SRAM, 1KB
EEPROM, ADC (mouser.com)
Reference Manual:
Ultra-low-power STM32L0x3 advanced ARM®-based 32-bit MCUs (mouser.com)
On the datasheet, I2C1 is connected to APB1 Bus. We need to go to the APB1EN register to
enable clock access to I2C1. We need to configure GPIO pins to work as an Alternate Function
(AF). The GPIO pins associated with I2C1 are (see datasheet page 46):
• PB6 (I2C1_SCL),
• PB7 (I2C1_SDA)
For these pins, the Alternate function 1 (AF1) is used.
We will be using DMA to implement I2C1 and the DMA request channel that needs to be used is
Channel 6 (I2C1_Tx), and channel 7 (I2C1_Rx). This information can be found on the RM0367
Reference manual on page 266.
I2C Initialization:
➔ Set PB6 and PB7 to alternate function mode. Page 246 MODER6[1:0]
PB6:
GPIOB→MODER &= ~(1U<<12) = GPIO_MODER_MODE6_0
PB7 :
GPIOB→MODER |= (1U<<13) = GPIO_MODER_MODE6_1
➔ Set output type of PB6 and PB7. (GPIOx_OTYPER) Set to open drain
GPIOB→ OTYPER |= (1<<6)
GPIOB→ OTYPER |= (1<<7)
➔ Select PB6 and PB7 alternate function types for I2C. GPIOx_AFRL
PB6: AF1 = 0001
GPIOB→AFR [0] |= (1<<24)
GPIOB→AFR [0] &= ~ (1<<25)
GPIOB→AFR [0] &= ~ (1<<26)
GPIOB→AFR [0] &= ~ (1<<27)
0: Peripheral disable
I2C1→CR1 &= ~(1U<<0) or = ~I2C_CR1_PE
DMA1_Channel7→CPAR= (uint32_t)&I2C1→TXDR
DMA1_Channel7→CPAR= (uint32_t)&I2C1→RXDR
1: Peripheral enable
I2C1→CR1 |= (1U<<0) or |= I2C_CR1_PE
➔ Disable “General call enable”: Bit 19 GCEN
0: General call disabled. Address 0b00000000 is NACKed.
1: General call enabled. Address 0b00000000 is ACKed.
1. Monitor the BUSY bit of I2C_SR2 (Status 2) register until the bus is idle
2. Write to START bit (bit 8) of I2C_CR1 (Control 1) register to generate a START condition on
the bus
3. Wait until the START condition is complete, the SB bit (bit 0) of I2C_SR1 register is set
4. Write the slave address with the R/W bit (bit 0) cleared (for a write) to I2C_DR (Data Register),
this will transmit the slave address
5. Wait until ADDR bit (bit 1) of I2C_SR1 register is set signaling that the slave address is sent
6. Read I2C_SR1 and I2C_SR2 to clear the ADDR bit
7. Wait until TxE bit of I2C_SR1 register is set signaling that the transmit data register is empty
8. Write the register/memory address to I2C_DR register
9. Wait until TxE bit of I2C_SR1 register is set signaling that the transmit data register is empty
10. Write the data to I2C_DR register
11. Wait until BTF (Byte Transfer Finished) bit of I2C_SR1 register is set signaling that the data is
sent
12. Write to STOP bit (bit 9) of I2C_CR1 register to generate a STOP condition on the bus The
procedure listed above is a simplified version
Wrong behaviors in Stop mode when waking up from Stop mode is disabled in the I2C
peripheral
Description
When wakeup from Stop mode is disabled in the I2C interface (WUPEN = 0) and the
microcontroller enters Stop mode while a transfer is ongoing on the bus, some wrong
prevents initiating a transfer in master mode, as the START condition cannot be sent when
BUSY is set.
2. If clock stretching is enabled (NOSTRETCH = 0), the I2C clock SCL may be kept low by the
I2C as long as the microcontroller remains in Stop mode. This limitation may occur when
Stop mode is entered during the address phase of an I2C bus transfer while SCL = 0.
Therefore, the transfer may be stalled as long as the microcontroller is in Stop mode. The
probability that this issue occurs depends also on the timing’s configuration, the peripheral
These behaviors can occur in Slave mode and Master mode in a multi-master topology.
Workaround
Disable the I2C interface (PE=0) before entering Stop mode and enable it again in Run
mode.
SPI Implementation:
There is a third pin, an SCK pin to synchronize the data transfer between two devices.
SCK stands for Serial Clock. SCK is used to generate a clock to synchronize data transfer
between master and slave. The clock is generated by the master.
The Slave Select (SS) pin is used to initiate and terminate the data transfer between slave
devices and the master. SS pin is used to select a particular slave when interfacing with
multiple slave devices. A total of 4 pins are implicated in the SPI operation. Unlike I2C, SPI
slaves have no addresses. The selection is simply made with the slave select line. In SPI, the
shift register is 8 bits long. So, after 8 clock pulses, the contents of the two shift registers are
interchanged. Suppose that master wants to send data to the slave device. Master will place
the data on its shift register and generates 8 clock pulses and after the generation of 8 clock
pulses, the byte is transmitted to the slave shift register. Now suppose the master would
like to receive data from the slave. The slave will place data on its shift register and after 8
clock pulses generated by the master, a byte of data will be received by the master shift
register. In SPI mode, data can be sent and received at the same time because SPI protocol
is a full duplex. In the USART implementation, we mentioned that the transmitter and
receiver must agree on a clock frequency known as the baud rate. It is different in SPI mode.
In SPI, the master and slave devices use the same clock. As only the master generates the
clock, the master and slave must agree on clock polarity and clock phase with respect to
data. In the SPI implementation for this project, we will set the clock polarity and phase.
Notice that the combination of CPOL and CPHA is referred to as the SPI bus. During our
implementation, CPOL and CPHA are set to 1. This means that the active state of the clock is
0, the Idle state of the clock is 1, and data is captured on the rising edge and the output
data is done on the falling edge.
GPIO configuration
1. Enable clock access to port of SPI pins
2. Set SPI pins mode to alternate function mode AF0
3. Set alternate type AF0
SPI configuration
1. Enable clock access to SPI module
2. Set software slave management
3. Set SPI to master mode
4. Set CPHA and CPOL
5. Set Clock Divider: Baud Rate Control
6. Select to use DMA
7. Enable SPI module
DMA Configuration
USART overview:
USART is a serial data communication that uses two methods: Synchronous and
Asynchronous. During synchronous communication, the clock is transmitted with the data,
and during asynchronous communication, there is no clock transmitted. The transmitter
and the receiver agree on the clock speed for the data transmission which is known as the
Baud rate. To implement the USART we will set up the Baud rate for both the transmitter
and receiver.
The transmission mode used for this project is a Full Duplex. In a Full Duplex mode, the data
can be transmitted in both directions at the same time. In Asynchronous transmission, the
byte to be transmitted is packed between START and STOP bits and the START bit value is a
single bit which is always 0. The stop bit value can be a single or 2 bit which is always 1. So,
transmitting a character “A” (0100 0001 in binary) will become
In the USART communication system, connection speed is very important. The baud rate is
known as the rate at which the change to the signal occurs every second when the
transmission occurs. This setup is important for USART communication. Another important
setup is Stop bit and Parity. The stop bit can be 1 bit or two bits, and the parity is either odd
or even. It is important in the USART communication system to check parity. Parity is used
for error checking. For this project, we also configure the mode of communication that
specifies whether the transmission mode is Rx mode or Tx mode. The number of the data
bit to be transmitted or received is known as Word Length and can be 8 or 9 bits. Lastly, the
Hardware Flow Control configuration to specify whether it is enabled or not.
Implementation:
A. USART2 initialization
f. Enable TCIE
Bit 6 is the transmission complete interrupt bit. This bit must be set to 1 so the
USART interrupt is generated in the USART_ISR register.
USART2->CR1 |= USART_CR1_TCIE.
B. DMA initialization
DMA is connected to the AHB bus. To enable clock access to DMA1 this must be done
through the AHB register. Bit 0 is the DMA clock enable bit (DMAEN). Writing 1 to this
bit will enable the clock access to DMA.
RCC->AHBENR |= RCC_AHBENR_DMAEN.
The global interrupt in NVIC also must be enabled. This will allow channels 4 and 5 to
access global interrupt.
NVIC_EnableIRQ (DMA1_Channel4_5_6_7_IRQn).
8. Select channel5
DMA1_CSELR->CSELR &= (uint32_t)(DMA_CSELR_C5S)
LPUART Implementation:
Initialization
Design Alternatives
I Describe the different alternatives that were considered, the tradeoffs, and the rationale
for the choices made. This should be based on concept evaluation methods communicated in Chapter 4.
Identify