Skip to content

Commit 8b1fcb4

Browse files
authored
Merge branch 'main' into idf5.5.1
2 parents f8cb53c + 799f13c commit 8b1fcb4

File tree

23 files changed

+158
-69
lines changed

23 files changed

+158
-69
lines changed

extmod/vfs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
#define MP_BLOCKDEV_FLAG_CONCURRENT_WRITE_PROTECTED (0x0020)
5353
// Bit set when something has claimed the right to mutate the blockdev.
5454
#define MP_BLOCKDEV_FLAG_LOCKED (0x0040)
55+
// Ignore write protections. Used to override other flags temporarily.
56+
#define MP_BLOCKDEV_FLAG_IGNORE_WRITE_PROTECTION (0x0080)
5557

5658
// constants for block protocol ioctl
5759
#define MP_BLOCKDEV_IOCTL_INIT (1)

extmod/vfs_fat.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -427,13 +427,10 @@ static MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_statvfs_obj, fat_vfs_statvfs);
427427
static mp_obj_t vfs_fat_mount(mp_obj_t self_in, mp_obj_t readonly, mp_obj_t mkfs) {
428428
fs_user_mount_t *self = MP_OBJ_TO_PTR(self_in);
429429

430-
// Read-only device indicated by writeblocks[0] == MP_OBJ_NULL.
431-
// User can specify read-only device by:
432-
// 1. readonly=True keyword argument
433-
// 2. nonexistent writeblocks method (then writeblocks[0] == MP_OBJ_NULL already)
434-
if (mp_obj_is_true(readonly)) {
435-
self->blockdev.writeblocks[0] = MP_OBJ_NULL;
436-
}
430+
// CIRCUITPY-CHANGE: Use MP_BLOCKDEV_FLAG_USB_WRITABLE instead of writeblocks[0] =/!= MP_OBJ_NULL
431+
// to specify read-write.
432+
// If readonly to Python, it's writable by USB and vice versa.
433+
filesystem_set_writable_by_usb(self, mp_obj_is_true(readonly));
437434

438435
// check if we need to make the filesystem
439436
FRESULT res = (self->blockdev.flags & MP_BLOCKDEV_FLAG_NO_FILESYSTEM) ? FR_NO_FILESYSTEM : FR_OK;

extmod/vfs_fat_diskio.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
#include "lib/oofatfs/diskio.h"
4444
#include "extmod/vfs_fat.h"
4545

46+
// CIRCUITPY-CHANGE
47+
#include "supervisor/filesystem.h"
48+
4649
typedef void *bdev_t;
4750
static fs_user_mount_t *disk_get_device(void *bdev) {
4851
return (fs_user_mount_t *)bdev;
@@ -153,7 +156,8 @@ DRESULT disk_ioctl(
153156
if (ret != mp_const_none && MP_OBJ_SMALL_INT_VALUE(ret) != 0) {
154157
// error initialising
155158
stat = STA_NOINIT;
156-
} else if (vfs->blockdev.writeblocks[0] == MP_OBJ_NULL) {
159+
// CIRCUITPY-CHANGE: writability from Python check
160+
} else if (!filesystem_is_writable_by_python(vfs)) {
157161
stat = STA_PROTECT;
158162
} else {
159163
stat = 0;

main.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,14 @@ static void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
863863
boot_output = &boot_text;
864864
#endif
865865

866+
// Get the base filesystem.
867+
fs_user_mount_t *vfs = filesystem_circuitpy();
868+
FATFS *fs = &vfs->fatfs;
869+
870+
// Allow boot.py access to CIRCUITPY, and allow writes to boot_out.txt.
871+
// We can't use the regular flags for this, because they might get modified inside boot.py.
872+
filesystem_set_ignore_write_protection(vfs, true);
873+
866874
// Write version info
867875
mp_printf(&mp_plat_print, "%s\nBoard ID:%s\n", MICROPY_FULL_VERSION_INFO, CIRCUITPY_BOARD_ID);
868876
#if CIRCUITPY_MICROCONTROLLER && COMMON_HAL_MCU_PROCESSOR_UID_LENGTH > 0
@@ -881,10 +889,6 @@ static void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
881889

882890

883891
#ifdef CIRCUITPY_BOOT_OUTPUT_FILE
884-
// Get the base filesystem.
885-
fs_user_mount_t *vfs = filesystem_circuitpy();
886-
FATFS *fs = &vfs->fatfs;
887-
888892
boot_output = NULL;
889893
#if CIRCUITPY_STATUS_BAR
890894
supervisor_status_bar_resume();
@@ -906,16 +910,16 @@ static void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
906910
// in case power is momentary or will fail shortly due to, say a low, battery.
907911
mp_hal_delay_ms(1000);
908912

909-
// USB isn't up, so we can write the file.
910-
// operating at the oofatfs (f_open) layer means the usb concurrent write permission
911-
// is not even checked!
912913
f_open(fs, &boot_output_file, CIRCUITPY_BOOT_OUTPUT_FILE, FA_WRITE | FA_CREATE_ALWAYS);
913914
UINT chars_written;
914915
f_write(&boot_output_file, boot_text.buf, boot_text.len, &chars_written);
915916
f_close(&boot_output_file);
916917
filesystem_flush();
917918
}
918919
#endif
920+
921+
// Back to regular filesystem protections.
922+
filesystem_set_ignore_write_protection(vfs, false);
919923
}
920924

921925
cleanup_after_vm(_exec_result.exception);

ports/analog/common-hal/digitalio/DigitalInOut.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ digitalio_direction_t common_hal_digitalio_digitalinout_get_direction(
107107

108108
if (self->pin->port < 4) {
109109
// Check that I/O mode is enabled and we don't have in AND out on at the same time
110-
MP_STATIC_ASSERT(!((port->en0 & mask) && (port->inen & mask) && (port->outen & mask)));
110+
MP_STATIC_ASSERT_NONCONSTEXPR(!((port->en0 & mask) && (port->inen & mask) && (port->outen & mask)));
111111

112112
if ((port->en0 & mask) && (port->outen & mask)) {
113113
return DIRECTION_OUTPUT;

ports/atmel-samd/audio_dma.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ uint8_t find_sync_event_channel_raise(void) {
3939
}
4040

4141
void audio_dma_disable_channel(uint8_t channel) {
42-
if (channel >= AUDIO_DMA_CHANNEL_COUNT) {
42+
if (channel == NO_DMA_CHANNEL) {
4343
return;
4444
}
4545
dma_disable_channel(channel);
4646
}
4747

4848
void audio_dma_enable_channel(uint8_t channel) {
49-
if (channel >= AUDIO_DMA_CHANNEL_COUNT) {
49+
if (channel == NO_DMA_CHANNEL) {
5050
return;
5151
}
5252
dma_enable_channel(channel);
@@ -171,8 +171,8 @@ audio_dma_result audio_dma_setup_playback(audio_dma_t *dma,
171171
bool output_signed,
172172
uint32_t output_register_address,
173173
uint8_t dma_trigger_source) {
174-
uint8_t dma_channel = dma_allocate_channel(true);
175-
if (dma_channel >= AUDIO_DMA_CHANNEL_COUNT) {
174+
uint8_t dma_channel = dma_allocate_audio_channel();
175+
if (dma_channel == NO_DMA_CHANNEL) {
176176
return AUDIO_DMA_DMA_BUSY;
177177
}
178178

@@ -298,14 +298,14 @@ audio_dma_result audio_dma_setup_playback(audio_dma_t *dma,
298298

299299
void audio_dma_stop(audio_dma_t *dma) {
300300
uint8_t channel = dma->dma_channel;
301-
if (channel < AUDIO_DMA_CHANNEL_COUNT) {
301+
if (channel != NO_DMA_CHANNEL) {
302302
audio_dma_disable_channel(channel);
303303
disable_event_channel(dma->event_channel);
304304
MP_STATE_PORT(playing_audio)[channel] = NULL;
305305
audio_dma_state[channel] = NULL;
306306
dma_free_channel(dma->dma_channel);
307307
}
308-
dma->dma_channel = AUDIO_DMA_CHANNEL_COUNT;
308+
dma->dma_channel = NO_DMA_CHANNEL;
309309
dma->playing_in_progress = false;
310310
}
311311

@@ -318,7 +318,7 @@ void audio_dma_resume(audio_dma_t *dma) {
318318
}
319319

320320
bool audio_dma_get_paused(audio_dma_t *dma) {
321-
if (dma->dma_channel >= AUDIO_DMA_CHANNEL_COUNT) {
321+
if (dma->dma_channel == NO_DMA_CHANNEL) {
322322
return false;
323323
}
324324
uint32_t status = dma_transfer_status(dma->dma_channel);
@@ -327,7 +327,7 @@ bool audio_dma_get_paused(audio_dma_t *dma) {
327327
}
328328

329329
void audio_dma_init(audio_dma_t *dma) {
330-
dma->dma_channel = AUDIO_DMA_CHANNEL_COUNT;
330+
dma->dma_channel = NO_DMA_CHANNEL;
331331
}
332332

333333
void audio_dma_reset(void) {
@@ -341,7 +341,7 @@ void audio_dma_reset(void) {
341341
}
342342

343343
bool audio_dma_get_playing(audio_dma_t *dma) {
344-
if (dma->dma_channel >= AUDIO_DMA_CHANNEL_COUNT) {
344+
if (dma->dma_channel == NO_DMA_CHANNEL) {
345345
return false;
346346
}
347347
return dma->playing_in_progress;

ports/atmel-samd/common-hal/audiobusio/PDMIn.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ static uint16_t filter_sample(uint32_t pdm_samples[4]) {
364364
// output_buffer_length is the number of slots, not the number of bytes.
365365
uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t *self,
366366
uint16_t *output_buffer, uint32_t output_buffer_length) {
367-
uint8_t dma_channel = dma_allocate_channel(true);
367+
uint8_t dma_channel = dma_allocate_audio_channel();
368368
pdmin_event_channel = find_sync_event_channel_raise();
369369
pdmin_dma_block_done = false;
370370

ports/atmel-samd/common-hal/busio/SPI.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,17 @@
2121
#include "samd/dma.h"
2222
#include "samd/sercom.h"
2323

24-
void setup_pin(const mcu_pin_obj_t *pin, uint32_t pinmux);
24+
25+
static void setup_pin(const mcu_pin_obj_t *pin, uint32_t pinmux, const enum gpio_direction direction) {
26+
gpio_set_pin_direction(pin->number, direction);
27+
gpio_set_pin_pull_mode(pin->number, GPIO_PULL_OFF);
28+
gpio_set_pin_function(pin->number, pinmux);
29+
if (direction == GPIO_DIRECTION_OUT) {
30+
// Use strong drive strength for SPI outputs.
31+
hri_port_set_PINCFG_DRVSTR_bit(PORT, (enum gpio_port)GPIO_PORT(pin->number), GPIO_PIN(pin->number));
32+
}
33+
claim_pin(pin);
34+
}
2535

2636
void common_hal_busio_spi_construct(busio_spi_obj_t *self,
2737
const mcu_pin_obj_t *clock, const mcu_pin_obj_t *mosi,
@@ -128,6 +138,7 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
128138
// Pads must be set after spi_m_sync_init(), which uses default values from
129139
// the prototypical SERCOM.
130140

141+
// Set to SPI host mode and choose pads.
131142
hri_sercomspi_write_CTRLA_MODE_bf(sercom, 3);
132143
hri_sercomspi_write_CTRLA_DOPO_bf(sercom, dopo);
133144
hri_sercomspi_write_CTRLA_DIPO_bf(sercom, miso_pad);
@@ -141,20 +152,20 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
141152
mp_raise_OSError(MP_EIO);
142153
}
143154

144-
setup_pin(clock, clock_pinmux);
155+
setup_pin(clock, clock_pinmux, GPIO_DIRECTION_OUT);
145156
self->clock_pin = clock->number;
146157

147158
if (mosi_none) {
148159
self->MOSI_pin = NO_PIN;
149160
} else {
150-
setup_pin(mosi, mosi_pinmux);
161+
setup_pin(mosi, mosi_pinmux, GPIO_DIRECTION_OUT);
151162
self->MOSI_pin = mosi->number;
152163
}
153164

154165
if (miso_none) {
155166
self->MISO_pin = NO_PIN;
156167
} else {
157-
setup_pin(miso, miso_pinmux);
168+
setup_pin(miso, miso_pinmux, GPIO_DIRECTION_IN);
158169
self->MISO_pin = miso->number;
159170
}
160171

@@ -317,11 +328,3 @@ uint8_t common_hal_busio_spi_get_polarity(busio_spi_obj_t *self) {
317328
void *hw = self->spi_desc.dev.prvt;
318329
return hri_sercomspi_get_CTRLA_CPOL_bit(hw);
319330
}
320-
321-
void setup_pin(const mcu_pin_obj_t *pin, uint32_t pinmux) {
322-
gpio_set_pin_direction(pin->number, GPIO_DIRECTION_OUT);
323-
gpio_set_pin_pull_mode(pin->number, GPIO_PULL_OFF);
324-
gpio_set_pin_function(pin->number, pinmux);
325-
claim_pin(pin);
326-
hri_port_set_PINCFG_DRVSTR_bit(PORT, (enum gpio_port)GPIO_PORT(pin->number), GPIO_PIN(pin->number));
327-
}

ports/atmel-samd/common-hal/imagecapture/ParallelImageCapture.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ void common_hal_imagecapture_parallelimagecapture_singleshot_capture(imagecaptur
135135
mp_buffer_info_t bufinfo;
136136
mp_get_buffer_raise(buffer, &bufinfo, MP_BUFFER_RW);
137137

138-
uint8_t dma_channel = dma_allocate_channel(true);
138+
// Allocate a permanent channel (not really audio).
139+
uint8_t dma_channel = dma_allocate_audio_channel();
139140

140141
uint32_t *dest = bufinfo.buf;
141142
size_t count = bufinfo.len / 4; // PCC receives 4 bytes (2 pixels) at a time

ports/atmel-samd/common-hal/spitarget/SPITarget.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,15 @@ void common_hal_spitarget_spi_target_transfer_start(spitarget_spi_target_obj_t *
192192
self->miso_packet = miso_packet;
193193

194194
Sercom *sercom = self->spi_desc.dev.prvt;
195-
self->running_dma = shared_dma_transfer_start(sercom, miso_packet, &sercom->SPI.DATA.reg, &sercom->SPI.DATA.reg, mosi_packet, len, 0);
195+
shared_dma_transfer_start(&self->running_dma, sercom, miso_packet, &sercom->SPI.DATA.reg, &sercom->SPI.DATA.reg, mosi_packet, len, 0);
196196

197197
// There is an issue where if an unexpected SPI transfer is received before the user calls "end" for the in-progress, expected
198198
// transfer, the SERCOM has an error and gets confused. This can be detected from INTFLAG.ERROR. I think the code in
199199
// ports/atmel-samd/peripherals/samd/dma.c at line 277 (as of this commit; it's the part that reads s->SPI.INTFLAG.bit.RXC and
200200
// s->SPI.DATA.reg) is supposed to fix this, but experimentation seems to show that it does not in fact fix anything. Anyways, if
201201
// the ERROR bit is set, let's just reset the peripheral and then setup the transfer again -- that seems to work.
202202
if (hri_sercomspi_get_INTFLAG_ERROR_bit(sercom)) {
203-
shared_dma_transfer_close(self->running_dma);
203+
shared_dma_transfer_close(&self->running_dma);
204204

205205
// disable the sercom
206206
spi_m_sync_disable(&self->spi_desc);
@@ -223,19 +223,19 @@ void common_hal_spitarget_spi_target_transfer_start(spitarget_spi_target_obj_t *
223223
spi_m_sync_enable(&self->spi_desc);
224224
hri_sercomspi_wait_for_sync(sercom, SERCOM_SPI_SYNCBUSY_MASK);
225225

226-
self->running_dma = shared_dma_transfer_start(sercom, miso_packet, &sercom->SPI.DATA.reg, &sercom->SPI.DATA.reg, mosi_packet, len, 0);
226+
shared_dma_transfer_start(&self->running_dma, sercom, miso_packet, &sercom->SPI.DATA.reg, &sercom->SPI.DATA.reg, mosi_packet, len, 0);
227227
}
228228
}
229229

230230
bool common_hal_spitarget_spi_target_transfer_is_finished(spitarget_spi_target_obj_t *self) {
231-
return self->running_dma.failure == 1 || shared_dma_transfer_finished(self->running_dma);
231+
return self->running_dma.failure == 1 || shared_dma_transfer_finished(&self->running_dma);
232232
}
233233

234234
int common_hal_spitarget_spi_target_transfer_close(spitarget_spi_target_obj_t *self) {
235235
if (self->running_dma.failure == 1) {
236236
return 0;
237237
}
238-
int res = shared_dma_transfer_close(self->running_dma);
238+
int res = shared_dma_transfer_close(&self->running_dma);
239239
self->running_dma.failure = 1;
240240

241241
self->mosi_packet = NULL;

0 commit comments

Comments
 (0)