Skip to content

Commit 203b7e8

Browse files
committed
Add device support for USART, I2C and SPI
1 parent f6c4d2f commit 203b7e8

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

qiling/hw/char/stm32f4xx_usart.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,11 @@ def transfer(self):
9494

9595
if not (self.usart.SR & USART_SR.RXNE):
9696
# TXE bit must had been cleared
97-
if self.can_recv():
97+
if self.has_input():
9898
self.usart.SR |= USART_SR.RXNE
9999
self.usart.DR = self.recv_from_user()
100100

101+
@QlConnectivityPeripheral.device_handler
101102
def step(self):
102103
self.transfer()
103104

qiling/hw/i2c/stm32f4xx_i2c.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
import ctypes
77

88
from qiling.hw.peripheral import QlPeripheral
9+
from qiling.hw.connectivity import QlConnectivityPeripheral
910
from qiling.hw.const.stm32f4xx_i2c import I2C_CR1, I2C_CR2, I2C_SR1, I2C_SR2, I2C_DR, I2C_OAR1, I2C_OAR2
1011

1112

12-
class STM32F4xxI2c(QlPeripheral):
13+
class STM32F4xxI2c(QlConnectivityPeripheral):
1314
class Type(ctypes.Structure):
1415
""" the structure is available in :
1516
stm32f423xx.h
@@ -52,9 +53,6 @@ def __init__(self, ql, label, ev_intn=None, er_intn=None):
5253
self.ev_intn = ev_intn # event interrupt
5354
self.er_intn = er_intn # error interrupt
5455

55-
self.devices = []
56-
self.current = None
57-
5856
self.reset()
5957

6058
def reset(self):
@@ -147,17 +145,15 @@ def generate_stop(self):
147145

148146
def send_address(self):
149147
if self.i2c.DR == self.i2c.OAR1 >> 1:
150-
for dev in self.devices:
151-
if self.i2c.DR == dev.address:
152-
self.current = dev
153148

154149
# TODO: send ACK
155150
self.i2c.SR1 |= I2C_SR1.ADDR | I2C_SR1.TXE
156151
self.send_event_interrupt()
157152

158153
def send_data(self):
159154
self.i2c.SR1 |= I2C_SR1.BTF | I2C_SR1.TXE
160-
self.current.send(self.i2c.DR)
155+
156+
self.send_to_user(self.i2c.DR)
161157
self.send_event_interrupt()
162158

163159
## I2C Status register 2 (I2C_SR2)
@@ -191,16 +187,17 @@ def is_7bit_mode(self):
191187
def fetch_device_address(self):
192188
# dual addressing mode
193189
if self.i2c.OAR2 & I2C_OAR2.ENDUAL:
194-
self.i2c.OAR1 = self.devices[0].address << 1
195-
self.i2c.OAR2 = I2C_OAR2.ENDUAL | (self.devices[1].address << 1)
190+
self.i2c.OAR1 = self.device_list[0].address << 1
191+
self.i2c.OAR2 = I2C_OAR2.ENDUAL | (self.device_list[1].address << 1)
196192

197193
# single device, 10-bit slave address
198194
elif self.i2c.OAR1 & I2C_OAR1.ADDMODE:
199-
self.i2c.OAR1 = I2C_OAR1.ADDMODE | self.devices[0].address
195+
self.i2c.OAR1 = I2C_OAR1.ADDMODE | self.device_list[0].address
200196

201197
# single device, 7-bit slave address
202198
else:
203-
self.i2c.OAR1 = self.devices[0].address << 1
199+
self.i2c.OAR1 = self.device_list[0].address << 1
204200

205-
def connect(self, dev):
206-
self.devices.append(dev)
201+
@QlConnectivityPeripheral.device_handler
202+
def step(self):
203+
pass

qiling/hw/spi/stm32f4xx_spi.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
#
55

66
import ctypes
7+
78
from qiling.hw.peripheral import QlPeripheral
9+
from qiling.hw.connectivity import QlConnectivityPeripheral
810
from qiling.hw.const.stm32f4xx_spi import SPI_CR1, SPI_CR2, SPI_SR, SPI_CRCPR, SPI_I2SCFGR, SPI_I2SPR
911

1012

11-
class STM32F4xxSpi(QlPeripheral):
13+
class STM32F4xxSpi(QlConnectivityPeripheral):
1214
class Type(ctypes.Structure):
1315
""" the structure available in :
1416
stm32f413xx.h
@@ -99,10 +101,12 @@ def write(self, offset: int, size: int, value: int):
99101
ctypes.memmove(ctypes.addressof(self.spi) + offset, data, size)
100102

101103
if self.in_field(self.struct.DR, offset, size):
102-
self.send_data()
104+
self.spi.SR |= SPI_SR.RXNE
105+
self.send_to_user(self.spi.DR)
103106

104107
def send_interrupt(self):
105108
self.ql.hw.nvic.set_pending(self.intn)
106109

107-
def send_data(self):
108-
self.spi.SR |= SPI_SR.RXNE
110+
@QlConnectivityPeripheral.device_handler
111+
def step(self):
112+
pass

0 commit comments

Comments
 (0)