Merge pull request #5698 from tannewt/rpi_spi

Add SPI to broadcom port
This commit is contained in:
Dan Halbert 2021-12-09 21:14:06 -05:00 committed by GitHub
commit b50443240f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 344 additions and 18 deletions

View File

@ -0,0 +1,56 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2021 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "supervisor/board.h"
#include "mpconfigboard.h"
#include "bindings/videocore/Framebuffer.h"
#include "shared-module/displayio/__init__.h"
#include "shared-bindings/framebufferio/FramebufferDisplay.h"
void board_init(void) {
videocore_framebuffer_obj_t *fb = &allocate_display_bus()->videocore;
fb->base.type = &videocore_framebuffer_type;
common_hal_videocore_framebuffer_construct(fb, 640, 480);
framebufferio_framebufferdisplay_obj_t *display = &displays[0].framebuffer_display;
display->base.type = &framebufferio_framebufferdisplay_type;
common_hal_framebufferio_framebufferdisplay_construct(
display,
MP_OBJ_FROM_PTR(fb),
0,
true);
}
bool board_requests_safe_mode(void) {
return false;
}
void reset_board(void) {
}
void board_deinit(void) {
}

View File

@ -0,0 +1 @@
#define MICROPY_HW_BOARD_NAME "Raspberry Pi Compute Module 4"

View File

@ -0,0 +1,6 @@
USB_VID = 0x2E8A
USB_PID = 0x1014
USB_PRODUCT = "Compute Module 4"
USB_MANUFACTURER = "Raspberry Pi"
CHIP_VARIANT = "bcm2711"

View File

@ -0,0 +1,48 @@
#include "shared-bindings/board/__init__.h"
#include "shared-module/displayio/__init__.h"
STATIC const mp_rom_map_elem_t board_global_dict_table[] = {
CIRCUITPYTHON_BOARD_DICT_STANDARD_ITEMS
// These match the names used in the CM4 datasheet
{ MP_ROM_QSTR(MP_QSTR_ID_SD), MP_ROM_PTR(&pin_GPIO0) },
{ MP_ROM_QSTR(MP_QSTR_ID_SC), MP_ROM_PTR(&pin_GPIO1) },
{ MP_ROM_QSTR(MP_QSTR_GPIO2), MP_ROM_PTR(&pin_GPIO2) },
{ MP_ROM_QSTR(MP_QSTR_GPIO3), MP_ROM_PTR(&pin_GPIO3) },
{ MP_ROM_QSTR(MP_QSTR_GPIO4), MP_ROM_PTR(&pin_GPIO4) },
{ MP_ROM_QSTR(MP_QSTR_GPIO5), MP_ROM_PTR(&pin_GPIO5) },
{ MP_ROM_QSTR(MP_QSTR_GPIO6), MP_ROM_PTR(&pin_GPIO6) },
{ MP_ROM_QSTR(MP_QSTR_GPIO7), MP_ROM_PTR(&pin_GPIO7) },
{ MP_ROM_QSTR(MP_QSTR_GPIO8), MP_ROM_PTR(&pin_GPIO8) },
{ MP_ROM_QSTR(MP_QSTR_GPIO9), MP_ROM_PTR(&pin_GPIO9) },
{ MP_ROM_QSTR(MP_QSTR_GPIO10), MP_ROM_PTR(&pin_GPIO10) },
{ MP_ROM_QSTR(MP_QSTR_GPIO11), MP_ROM_PTR(&pin_GPIO11) },
{ MP_ROM_QSTR(MP_QSTR_GPIO12), MP_ROM_PTR(&pin_GPIO12) },
{ MP_ROM_QSTR(MP_QSTR_GPIO13), MP_ROM_PTR(&pin_GPIO13) },
{ MP_ROM_QSTR(MP_QSTR_GPIO14), MP_ROM_PTR(&pin_GPIO14) },
{ MP_ROM_QSTR(MP_QSTR_GPIO15), MP_ROM_PTR(&pin_GPIO15) },
{ MP_ROM_QSTR(MP_QSTR_GPIO16), MP_ROM_PTR(&pin_GPIO16) },
{ MP_ROM_QSTR(MP_QSTR_GPIO17), MP_ROM_PTR(&pin_GPIO17) },
{ MP_ROM_QSTR(MP_QSTR_GPIO18), MP_ROM_PTR(&pin_GPIO18) },
{ MP_ROM_QSTR(MP_QSTR_GPIO19), MP_ROM_PTR(&pin_GPIO19) },
{ MP_ROM_QSTR(MP_QSTR_GPIO20), MP_ROM_PTR(&pin_GPIO20) },
{ MP_ROM_QSTR(MP_QSTR_GPIO21), MP_ROM_PTR(&pin_GPIO21) },
{ MP_ROM_QSTR(MP_QSTR_GPIO22), MP_ROM_PTR(&pin_GPIO22) },
{ MP_ROM_QSTR(MP_QSTR_GPIO23), MP_ROM_PTR(&pin_GPIO23) },
{ MP_ROM_QSTR(MP_QSTR_GPIO24), MP_ROM_PTR(&pin_GPIO24) },
{ MP_ROM_QSTR(MP_QSTR_GPIO25), MP_ROM_PTR(&pin_GPIO25) },
{ MP_ROM_QSTR(MP_QSTR_GPIO26), MP_ROM_PTR(&pin_GPIO26) },
{ MP_ROM_QSTR(MP_QSTR_GPIO27), MP_ROM_PTR(&pin_GPIO27) },
{ MP_ROM_QSTR(MP_QSTR_LED_nACT), MP_ROM_PTR(&pin_GPIO42) },
{ MP_ROM_QSTR(MP_QSTR_LED), MP_ROM_PTR(&pin_GPIO42) },
{ MP_ROM_QSTR(MP_QSTR_SDA0), MP_ROM_PTR(&pin_GPIO44) },
{ MP_ROM_QSTR(MP_QSTR_SCL0), MP_ROM_PTR(&pin_GPIO45) },
{ MP_ROM_QSTR(MP_QSTR_DISPLAY), MP_ROM_PTR(&displays[0].display)},
};
MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table);

View File

@ -1,5 +1,5 @@
USB_VID = 0x2E8A
USB_PID = 0xf000
USB_PID = 0x1012
USB_PRODUCT = "Compute Module 4 IO Board"
USB_MANUFACTURER = "Raspberry Pi"

View File

@ -1,5 +1,5 @@
USB_VID = 0x2E8A
USB_PID = 0xF001
USB_PID = 0x1013
USB_PRODUCT = "Raspberry Pi 4B"
USB_MANUFACTURER = "Raspberry Pi"

View File

@ -1,5 +1,5 @@
USB_VID = 0x2E8A
USB_PID = 0xF002
USB_PID = 0x1015
USB_PRODUCT = "Raspberry Pi Zero 2W"
USB_MANUFACTURER = "Raspberry Pi"

View File

@ -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) {

View File

@ -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);

View File

@ -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,

View File

@ -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

View File

@ -106,7 +106,7 @@ void reset_port(void) {
audio_dma_reset();
#endif
// reset_all_pins();
reset_all_pins();
}
void reset_to_bootloader(void) {