Add SPI support
It uses both SPI and AUX SPI peripherals
This commit is contained in:
parent
b248cee98e
commit
6626319338
@ -34,28 +34,104 @@
|
||||
#include "common-hal/microcontroller/Pin.h"
|
||||
#include "shared-bindings/microcontroller/Pin.h"
|
||||
|
||||
#define NO_INSTANCE 0xff
|
||||
#include "peripherals/broadcom/cpu.h"
|
||||
#include "peripherals/broadcom/defines.h"
|
||||
#include "peripherals/broadcom/gpio.h"
|
||||
#include "peripherals/broadcom/pins.h"
|
||||
#include "peripherals/broadcom/vcmailbox.h"
|
||||
|
||||
STATIC bool never_reset_spi[2];
|
||||
#if BCM_VERSION == 2711
|
||||
#define NUM_SPI (7)
|
||||
STATIC SPI0_Type *spi[NUM_SPI] = {SPI0, NULL, NULL, SPI3, SPI4, SPI5, SPI6};
|
||||
STATIC SPI1_Type *aux_spi[NUM_SPI] = {NULL, SPI1, SPI2, NULL, NULL, NULL, NULL};
|
||||
#else
|
||||
#define NUM_SPI (3)
|
||||
STATIC SPI0_Type *spi[NUM_SPI] = {SPI0, NULL, NULL};
|
||||
STATIC SPI1_Type *aux_spi[NUM_SPI] = {NULL, SPI1, SPI2};
|
||||
#endif
|
||||
|
||||
STATIC bool never_reset_spi[NUM_SPI];
|
||||
STATIC bool spi_in_use[NUM_SPI];
|
||||
|
||||
void reset_spi(void) {
|
||||
for (size_t i = 0; i < 2; i++) {
|
||||
for (size_t i = 0; i < NUM_SPI; i++) {
|
||||
if (never_reset_spi[i]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// TODO
|
||||
if (i == 1 || i == 2) {
|
||||
if (i == 1) {
|
||||
AUX->ENABLES_b.SPI_1 = false;
|
||||
} else {
|
||||
AUX->ENABLES_b.SPI_2 = false;
|
||||
}
|
||||
aux_spi[i]->CNTL0 = 0;
|
||||
} else {
|
||||
// Set CS back to default. All 0 except read enable.
|
||||
spi[i]->CS = SPI0_CS_REN_Msk;
|
||||
}
|
||||
|
||||
spi_in_use[i] = false;
|
||||
}
|
||||
}
|
||||
|
||||
void common_hal_busio_spi_construct(busio_spi_obj_t *self,
|
||||
const mcu_pin_obj_t *clock, const mcu_pin_obj_t *mosi,
|
||||
const mcu_pin_obj_t *miso) {
|
||||
size_t instance_index = NUM_SPI;
|
||||
BP_Function_Enum clock_alt = 0;
|
||||
BP_Function_Enum mosi_alt = 0;
|
||||
BP_Function_Enum miso_alt = 0;
|
||||
for (size_t i = 0; i < NUM_SPI; i++) {
|
||||
if (spi_in_use[i]) {
|
||||
continue;
|
||||
}
|
||||
if (!pin_find_alt(clock, PIN_FUNCTION_SPI, i, SPI_FUNCTION_SCLK, &clock_alt)) {
|
||||
continue;
|
||||
}
|
||||
if (mosi != NULL && !pin_find_alt(mosi, PIN_FUNCTION_SPI, i, SPI_FUNCTION_MOSI, &mosi_alt)) {
|
||||
continue;
|
||||
}
|
||||
if (miso != NULL && !pin_find_alt(miso, PIN_FUNCTION_SPI, i, SPI_FUNCTION_MISO, &miso_alt)) {
|
||||
continue;
|
||||
}
|
||||
instance_index = i;
|
||||
break;
|
||||
}
|
||||
if (instance_index == NUM_SPI) {
|
||||
mp_raise_ValueError(translate("Invalid pins"));
|
||||
}
|
||||
|
||||
self->clock = clock;
|
||||
self->MOSI = mosi;
|
||||
self->MISO = miso;
|
||||
self->index = instance_index;
|
||||
spi_in_use[instance_index] = true;
|
||||
|
||||
|
||||
if (instance_index == 1) {
|
||||
AUX->ENABLES_b.SPI_1 = true;
|
||||
} else if (instance_index == 2) {
|
||||
AUX->ENABLES_b.SPI_2 = true;
|
||||
}
|
||||
|
||||
common_hal_busio_spi_configure(self, 250000, 0, 0, 8);
|
||||
|
||||
COMPLETE_MEMORY_READS;
|
||||
gpio_set_pull(clock->number, BP_PULL_NONE);
|
||||
gpio_set_function(clock->number, clock_alt);
|
||||
if (mosi != NULL) {
|
||||
gpio_set_pull(mosi->number, BP_PULL_NONE);
|
||||
gpio_set_function(mosi->number, mosi_alt);
|
||||
}
|
||||
if (miso != NULL) {
|
||||
gpio_set_pull(miso->number, BP_PULL_NONE);
|
||||
gpio_set_function(miso->number, miso_alt);
|
||||
}
|
||||
}
|
||||
|
||||
void common_hal_busio_spi_never_reset(busio_spi_obj_t *self) {
|
||||
// never_reset_spi[spi_get_index(self->peripheral)] = true;
|
||||
never_reset_spi[self->index] = true;
|
||||
|
||||
common_hal_never_reset_pin(self->clock);
|
||||
common_hal_never_reset_pin(self->MOSI);
|
||||
@ -70,12 +146,22 @@ void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
|
||||
if (common_hal_busio_spi_deinited(self)) {
|
||||
return;
|
||||
}
|
||||
// never_reset_spi[spi_get_index(self->peripheral)] = false;
|
||||
never_reset_spi[self->index] = false;
|
||||
|
||||
common_hal_reset_pin(self->clock);
|
||||
common_hal_reset_pin(self->MOSI);
|
||||
common_hal_reset_pin(self->MISO);
|
||||
self->clock = NULL;
|
||||
|
||||
if (self->index == 1 ||
|
||||
self->index == 2) {
|
||||
aux_spi[self->index]->CNTL0_b.ENABLE = false;
|
||||
if (self->index == 1) {
|
||||
AUX->ENABLES_b.SPI_1 = false;
|
||||
} else if (self->index == 2) {
|
||||
AUX->ENABLES_b.SPI_2 = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
|
||||
@ -87,13 +173,40 @@ bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
|
||||
return true;
|
||||
}
|
||||
|
||||
// TODO
|
||||
if (self->index == 1 || self->index == 2) {
|
||||
SPI1_Type *p = aux_spi[self->index];
|
||||
uint32_t source_clock = vcmailbox_get_clock_rate_measured(VCMAILBOX_CLOCK_CORE);
|
||||
uint16_t clock_divider = source_clock / baudrate;
|
||||
if (source_clock % baudrate > 0) {
|
||||
clock_divider += 2;
|
||||
}
|
||||
|
||||
p->CNTL0 = (clock_divider / 2 - 1) << SPI1_CNTL0_SPEED_Pos |
|
||||
SPI1_CNTL0_ENABLE_Msk |
|
||||
SPI1_CNTL0_MSB_FIRST_Msk |
|
||||
(polarity == 1? SPI1_CNTL0_INVERT_CLK_Msk : 0) |
|
||||
(phase == polarity? SPI1_CNTL0_IN_RISING_Msk : SPI1_CNTL0_OUT_RISING_Msk) |
|
||||
8 << SPI1_CNTL0_SHIFT_LENGTH_Pos;
|
||||
p->CNTL1 = SPI1_CNTL1_MSB_FIRST_Msk;
|
||||
self->real_frequency = source_clock / clock_divider;
|
||||
} else {
|
||||
SPI0_Type *p = spi[self->index];
|
||||
p->CS = polarity << SPI0_CS_CPOL_Pos |
|
||||
phase << SPI0_CS_CPHA_Pos;
|
||||
uint32_t source_clock = vcmailbox_get_clock_rate_measured(VCMAILBOX_CLOCK_CORE);
|
||||
uint16_t clock_divider = source_clock / baudrate;
|
||||
if (source_clock % baudrate > 0) {
|
||||
clock_divider += 2;
|
||||
}
|
||||
|
||||
p->CLK = clock_divider;
|
||||
self->real_frequency = source_clock / clock_divider;
|
||||
}
|
||||
|
||||
self->polarity = polarity;
|
||||
self->phase = phase;
|
||||
self->bits = bits;
|
||||
self->target_frequency = baudrate;
|
||||
self->real_frequency = baudrate; // TODO
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -115,18 +228,103 @@ void common_hal_busio_spi_unlock(busio_spi_obj_t *self) {
|
||||
self->has_lock = false;
|
||||
}
|
||||
|
||||
STATIC void _spi_transfer(SPI0_Type *p,
|
||||
const uint8_t *data_out, size_t out_len,
|
||||
uint8_t *data_in, size_t in_len) {
|
||||
size_t len = MAX(out_len, in_len);
|
||||
COMPLETE_MEMORY_READS;
|
||||
p->DLEN = len;
|
||||
p->CS |= SPI0_CS_TA_Msk | SPI0_CS_CLEAR_Msk;
|
||||
size_t in = 0;
|
||||
size_t out = 0;
|
||||
while (in < len) {
|
||||
while (out < len && p->CS_b.TXD == 1) {
|
||||
if (out_len == 1) {
|
||||
p->FIFO = data_out[0];
|
||||
} else {
|
||||
p->FIFO = data_out[out];
|
||||
}
|
||||
out++;
|
||||
}
|
||||
// Wait for data to read (also means data has been sent.)
|
||||
while (p->CS_b.RXD == 0) {
|
||||
RUN_BACKGROUND_TASKS;
|
||||
}
|
||||
while (p->CS_b.RXD == 1) {
|
||||
uint8_t data = p->FIFO;
|
||||
if (data_in != NULL) {
|
||||
data_in[in] = data;
|
||||
} else {
|
||||
(void)data;
|
||||
}
|
||||
in++;
|
||||
}
|
||||
}
|
||||
p->CS_b.TA = false;
|
||||
COMPLETE_MEMORY_READS;
|
||||
}
|
||||
|
||||
STATIC void _aux_spi_transfer(SPI1_Type *p,
|
||||
const uint8_t *data_out, size_t out_len,
|
||||
uint8_t *data_in, size_t in_len) {
|
||||
size_t len = MAX(out_len, in_len);
|
||||
p->CNTL0 |= SPI1_CNTL0_CLEAR_FIFOS_Msk;
|
||||
p->CNTL0 &= ~SPI1_CNTL0_CLEAR_FIFOS_Msk;
|
||||
size_t in = 0;
|
||||
size_t out = 0;
|
||||
while (in < len) {
|
||||
while (out < len && p->STAT_b.TX_FULL == 0) {
|
||||
if (out_len == 1) {
|
||||
p->TXHOLD0 = data_out[0] << 24;
|
||||
} else {
|
||||
p->TXHOLD0 = data_out[out] << 24;
|
||||
}
|
||||
out++;
|
||||
}
|
||||
// Wait for data to read (also means data has been sent.)
|
||||
while (p->STAT_b.RX_EMPTY == 1) {
|
||||
RUN_BACKGROUND_TASKS;
|
||||
}
|
||||
while (p->STAT_b.RX_EMPTY == 0) {
|
||||
uint8_t data = p->TXHOLD0;
|
||||
if (data_in != NULL) {
|
||||
data_in[in] = data;
|
||||
} else {
|
||||
(void)data;
|
||||
}
|
||||
in++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool common_hal_busio_spi_write(busio_spi_obj_t *self,
|
||||
const uint8_t *data, size_t len) {
|
||||
return false;
|
||||
if (self->index == 1 || self->index == 2) {
|
||||
_aux_spi_transfer(aux_spi[self->index], data, len, NULL, 0);
|
||||
} else {
|
||||
_spi_transfer(spi[self->index], data, len, NULL, 0);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool common_hal_busio_spi_read(busio_spi_obj_t *self,
|
||||
uint8_t *data, size_t len, uint8_t write_value) {
|
||||
return false;
|
||||
if (self->index == 1 || self->index == 2) {
|
||||
_aux_spi_transfer(aux_spi[self->index], &write_value, 1, data, len);
|
||||
} else {
|
||||
_spi_transfer(spi[self->index], &write_value, 1, data, len);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool common_hal_busio_spi_transfer(busio_spi_obj_t *self, const uint8_t *data_out, uint8_t *data_in, size_t len) {
|
||||
return false;
|
||||
bool common_hal_busio_spi_transfer(busio_spi_obj_t *self,
|
||||
const uint8_t *data_out, uint8_t *data_in, size_t len) {
|
||||
if (self->index == 1 || self->index == 2) {
|
||||
_aux_spi_transfer(aux_spi[self->index], data_out, len, data_in, len);
|
||||
} else {
|
||||
_spi_transfer(spi[self->index], data_out, len, data_in, len);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
uint32_t common_hal_busio_spi_get_frequency(busio_spi_obj_t *self) {
|
||||
|
@ -42,6 +42,7 @@ typedef struct {
|
||||
uint8_t polarity;
|
||||
uint8_t phase;
|
||||
uint8_t bits;
|
||||
uint8_t index;
|
||||
} busio_spi_obj_t;
|
||||
|
||||
void reset_spi(void);
|
||||
|
@ -148,6 +148,10 @@ void UART5_IRQHandler(void) {
|
||||
|
||||
void common_hal_busio_uart_never_reset(busio_uart_obj_t *self) {
|
||||
uart_status[self->uart_id] = STATUS_NEVER_RESET;
|
||||
common_hal_never_reset_pin(self->tx_pin);
|
||||
common_hal_never_reset_pin(self->rx_pin);
|
||||
common_hal_never_reset_pin(self->cts_pin);
|
||||
common_hal_never_reset_pin(self->rts_pin);
|
||||
}
|
||||
|
||||
void common_hal_busio_uart_construct(busio_uart_obj_t *self,
|
||||
|
@ -45,9 +45,15 @@ void never_reset_pin_number(uint8_t pin_number) {
|
||||
}
|
||||
|
||||
void reset_pin_number(uint8_t pin_number) {
|
||||
gpio_set_function(pin_number, GPIO_FUNCTION_INPUT);
|
||||
pin_in_use[pin_number] = false;
|
||||
never_reset_pin[pin_number] = false;
|
||||
// Reset JTAG pins back to JTAG.
|
||||
if (22 <= pin_number && pin_number <= 27) {
|
||||
gpio_set_function(pin_number, GPIO_FUNCTION_ALT4);
|
||||
return;
|
||||
} else {
|
||||
gpio_set_function(pin_number, GPIO_FUNCTION_INPUT);
|
||||
}
|
||||
// Set the pull to match the datasheet.
|
||||
BP_PULL_Enum pull = BP_PULL_NONE;
|
||||
if (pin_number < 9 ||
|
||||
@ -66,10 +72,16 @@ void reset_pin_number(uint8_t pin_number) {
|
||||
}
|
||||
|
||||
void common_hal_never_reset_pin(const mcu_pin_obj_t *pin) {
|
||||
if (pin == NULL) {
|
||||
return;
|
||||
}
|
||||
never_reset_pin_number(pin->number);
|
||||
}
|
||||
|
||||
void common_hal_reset_pin(const mcu_pin_obj_t *pin) {
|
||||
if (pin == NULL) {
|
||||
return;
|
||||
}
|
||||
reset_pin_number(pin->number);
|
||||
}
|
||||
|
||||
|
@ -1 +1 @@
|
||||
Subproject commit c236f5a5ff93b50b8b948c904715e89f6d4fafe8
|
||||
Subproject commit e136e387177446c3c9979bbf274a4856bf13797d
|
@ -106,7 +106,7 @@ void reset_port(void) {
|
||||
audio_dma_reset();
|
||||
#endif
|
||||
|
||||
// reset_all_pins();
|
||||
reset_all_pins();
|
||||
}
|
||||
|
||||
void reset_to_bootloader(void) {
|
||||
|
Loading…
Reference in New Issue
Block a user