Skip to content

Commit d72618a

Browse files
committed
pybricks.hubs.MoveHub: refactor busy wait
Ensure we always call MICROPY_EVENT_POLL_HOOK, and reduce code size using a dedicated function to wait. Fixes 199cc82
1 parent 69f954d commit d72618a

File tree

1 file changed

+17
-23
lines changed

1 file changed

+17
-23
lines changed

pybricks/hubs/pb_type_movehub.c

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,15 @@
6666

6767
#define BYTE_ACCESS(r) (*(volatile uint8_t *)&(r))
6868

69-
typedef struct {
70-
mp_obj_base_t base;
71-
} hubs_MoveHub_IMU_obj_t;
69+
// Wait for spi operation to complete
70+
static void motion_spi_wait() {
71+
do {
72+
while (!(SPI1->SR & SPI_SR_RXNE)) {
73+
MICROPY_EVENT_POLL_HOOK;
74+
}
75+
MICROPY_EVENT_POLL_HOOK;
76+
} while (SPI1->SR & SPI_SR_BSY);
77+
}
7278

7379
static void motion_spi_read(uint8_t reg, uint8_t *value) {
7480
uint8_t dummy;
@@ -79,11 +85,7 @@ static void motion_spi_read(uint8_t reg, uint8_t *value) {
7985
BYTE_ACCESS(SPI1->DR) = READ_FLAG | reg;
8086

8187
// busy wait
82-
do {
83-
while (!(SPI1->SR & SPI_SR_RXNE)) {
84-
MICROPY_EVENT_POLL_HOOK;
85-
}
86-
} while (SPI1->SR & SPI_SR_BSY);
88+
motion_spi_wait();
8789

8890
while (SPI1->SR & SPI_SR_RXNE) {
8991
dummy = BYTE_ACCESS(SPI1->DR);
@@ -93,11 +95,7 @@ static void motion_spi_read(uint8_t reg, uint8_t *value) {
9395
BYTE_ACCESS(SPI1->DR) = 0;
9496

9597
// busy wait
96-
do {
97-
while (!(SPI1->SR & SPI_SR_RXNE)) {
98-
MICROPY_EVENT_POLL_HOOK
99-
}
100-
} while (SPI1->SR & SPI_SR_BSY);
98+
motion_spi_wait();
10199

102100
*value = BYTE_ACCESS(SPI1->DR);
103101

@@ -112,20 +110,12 @@ static void motion_spi_write(uint8_t reg, uint8_t value) {
112110
BYTE_ACCESS(SPI1->DR) = reg;
113111

114112
// busy wait
115-
do {
116-
while (!(SPI1->SR & SPI_SR_RXNE)) {
117-
MICROPY_EVENT_POLL_HOOK;
118-
}
119-
} while (SPI1->SR & SPI_SR_BSY);
113+
motion_spi_wait();
120114

121115
BYTE_ACCESS(SPI1->DR) = value;
122116

123117
// busy wait
124-
do {
125-
while (!(SPI1->SR & SPI_SR_RXNE)) {
126-
MICROPY_EVENT_POLL_HOOK;
127-
}
128-
} while (SPI1->SR & SPI_SR_BSY);
118+
motion_spi_wait();
129119

130120
// clear chip select
131121
GPIOA->BSRR = GPIO_BSRR_BS_4;
@@ -138,6 +128,10 @@ static void motion_get_acceleration(int8_t *data) {
138128
data[1] = -data[1];
139129
}
140130

131+
typedef struct {
132+
mp_obj_base_t base;
133+
} hubs_MoveHub_IMU_obj_t;
134+
141135
// This is an integer version of pybricks._common.IMU.up
142136
STATIC mp_obj_t hubs_MoveHub_IMU_up(mp_obj_t self_in) {
143137

0 commit comments

Comments
 (0)