Skip to content

Commit b372156

Browse files
committed
esp8266: Change software SPI driver to use general pin HAL.
1 parent 67a6d31 commit b372156

2 files changed

Lines changed: 18 additions & 17 deletions

File tree

esp8266/esp_mphal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,6 @@ void mp_hal_pin_config_od(mp_hal_pin_obj_t pin);
7575
else { gpio_output_set(1 << (p), 0, 1 << (p), 0); } \
7676
} while (0)
7777
#define mp_hal_pin_read(p) pin_get(p)
78+
#define mp_hal_pin_write(p, v) pin_set((p), (v))
7879

7980
#endif // _INCLUDED_MPHAL_H_

esp8266/modpybspi.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,16 @@
3535

3636
#include "py/runtime.h"
3737
#include "py/stream.h"
38-
#include "modpyb.h"
38+
#include "py/mphal.h"
3939

4040
typedef struct _pyb_spi_obj_t {
4141
mp_obj_base_t base;
4242
uint32_t baudrate;
4343
uint8_t polarity;
4444
uint8_t phase;
45-
pyb_pin_obj_t *sck;
46-
pyb_pin_obj_t *mosi;
47-
pyb_pin_obj_t *miso;
45+
mp_hal_pin_obj_t sck;
46+
mp_hal_pin_obj_t mosi;
47+
mp_hal_pin_obj_t miso;
4848
} pyb_spi_obj_t;
4949

5050
STATIC void mp_hal_spi_transfer(pyb_spi_obj_t *self, size_t src_len, const uint8_t *src_buf, size_t dest_len, uint8_t *dest_buf) {
@@ -59,20 +59,20 @@ STATIC void mp_hal_spi_transfer(pyb_spi_obj_t *self, size_t src_len, const uint8
5959
}
6060
uint8_t data_in = 0;
6161
for (int j = 0; j < 8; ++j, data_out <<= 1) {
62-
pin_set(self->mosi->phys_port, (data_out >> 7) & 1);
62+
mp_hal_pin_write(self->mosi, (data_out >> 7) & 1);
6363
if (self->phase == 0) {
6464
ets_delay_us(delay_half);
65-
pin_set(self->sck->phys_port, 1 - self->polarity);
65+
mp_hal_pin_write(self->sck, 1 - self->polarity);
6666
} else {
67-
pin_set(self->sck->phys_port, 1 - self->polarity);
67+
mp_hal_pin_write(self->sck, 1 - self->polarity);
6868
ets_delay_us(delay_half);
6969
}
70-
data_in = (data_in << 1) | pin_get(self->miso->phys_port);
70+
data_in = (data_in << 1) | mp_hal_pin_read(self->miso);
7171
if (self->phase == 0) {
7272
ets_delay_us(delay_half);
73-
pin_set(self->sck->phys_port, self->polarity);
73+
mp_hal_pin_write(self->sck, self->polarity);
7474
} else {
75-
pin_set(self->sck->phys_port, self->polarity);
75+
mp_hal_pin_write(self->sck, self->polarity);
7676
ets_delay_us(delay_half);
7777
}
7878
}
@@ -90,7 +90,7 @@ STATIC void mp_hal_spi_transfer(pyb_spi_obj_t *self, size_t src_len, const uint8
9090
STATIC void pyb_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
9191
pyb_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
9292
mp_printf(print, "SPI(baudrate=%u, polarity=%u, phase=%u, sck=%u, mosi=%u, miso=%u)",
93-
self->baudrate, self->polarity, self->phase, self->sck->phys_port, self->mosi->phys_port, self->miso->phys_port);
93+
self->baudrate, self->polarity, self->phase, self->sck, self->mosi, self->miso);
9494
}
9595

9696
STATIC void pyb_spi_init_helper(pyb_spi_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
@@ -116,13 +116,13 @@ STATIC void pyb_spi_init_helper(pyb_spi_obj_t *self, size_t n_args, const mp_obj
116116
self->phase = args[ARG_phase].u_int;
117117
}
118118
if (args[ARG_sck].u_obj != MP_OBJ_NULL) {
119-
self->sck = mp_obj_get_pin_obj(args[ARG_sck].u_obj);
119+
self->sck = mp_hal_get_pin_obj(args[ARG_sck].u_obj);
120120
}
121121
if (args[ARG_mosi].u_obj != MP_OBJ_NULL) {
122-
self->mosi = mp_obj_get_pin_obj(args[ARG_mosi].u_obj);
122+
self->mosi = mp_hal_get_pin_obj(args[ARG_mosi].u_obj);
123123
}
124124
if (args[ARG_miso].u_obj != MP_OBJ_NULL) {
125-
self->miso = mp_obj_get_pin_obj(args[ARG_miso].u_obj);
125+
self->miso = mp_hal_get_pin_obj(args[ARG_miso].u_obj);
126126
}
127127
}
128128

@@ -134,9 +134,9 @@ STATIC mp_obj_t pyb_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_
134134
self->baudrate = 500000;
135135
self->polarity = 0;
136136
self->phase = 0;
137-
self->sck = NULL;
138-
self->mosi = NULL;
139-
self->miso = NULL;
137+
self->sck = 14;
138+
self->mosi = 13;
139+
self->miso = 12;
140140
mp_map_t kw_args;
141141
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
142142
pyb_spi_init_helper(self, n_args, args, &kw_args);

0 commit comments

Comments
 (0)