Skip to content

samd: A set of small changes and bug fixes. #11106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ports/samd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ CFLAGS += $(INC) -Wall -Werror -std=c99 -nostdlib -mthumb $(CFLAGS_MCU) -fsingle
CFLAGS += -DMCU_$(MCU_SERIES) -D__$(CMSIS_MCU)__
CFLAGS += $(CFLAGS_EXTRA)

CFLAGS += -DMICROPY_HW_CODESIZE=$(MICROPY_HW_CODESIZE)

LDFLAGS += -nostdlib $(addprefix -T,$(LD_FILES)) -Map=$@.map --cref
LDFLAGS += --defsym=_codesize=$(MICROPY_HW_CODESIZE)

LIBS += $(shell $(CC) $(CFLAGS) -print-libgcc-file-name)

Expand Down
19 changes: 9 additions & 10 deletions ports/samd/boards/MINISAM_M4/pins.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
# If the pin name is omitted, the pin number is added as name.
# Empty lines and lines not starting with PIN_ are ignored

PIN_PA02,A0_D9
PIN_PB08,A1_D10
PIN_PB09,A2_D11
PIN_PA04,A3_D12
PIN_PA05,A4_D13
PIN_PA02,A0
PIN_PB08,A1
PIN_PB09,A2
PIN_PA04,A3
PIN_PA05,A4
PIN_PA06,A5
PIN_PA16,D0
PIN_PA17,D1
PIN_PA07,A6_D2
PIN_PA07,A6
PIN_PA16,RX
PIN_PA17,TX
PIN_PA19,D3
PIN_PA20,D4
PIN_PA21,D5
PIN_PA15,LED
PIN_PA00,BUTTON
PIN_PA03,AREF
PIN_PA12,SDA
Expand All @@ -25,8 +26,6 @@ PIN_PA01,SCK
PIN_PB03,DOTSTAR_DATA
PIN_PB02,DOTSTAR_CLK

PIN_PA15,LED

PIN_PA24,USB_DM
PIN_PA25,USB_DP
PIN_PA26,USB_SOF
Expand Down
4 changes: 2 additions & 2 deletions ports/samd/boards/SEEED_WIO_TERMINAL/pins.csv
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ PIN_PD20,SWITCH_U
PIN_PC26,BUTTON_1
PIN_PC27,BUTTON_2
PIN_PC28,BUTTON_3
PIN_PC14,5V_ENABLE
PIN_PC15,3V3_ENABLE
PIN_PC14,ENABLE_5V
PIN_PC15,ENABLE_3V3
PIN_PB26,TX
PIN_PB27,RX
PIN_PA13,SDA0
Expand Down
10 changes: 10 additions & 0 deletions ports/samd/boards/deploy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
For deploying the MicroPython firmware to the SAMD module, follow
the procedure:

- Push the reset button twice or call machine.bootloader(). A drive
icon should appear representing a virtual drive.

- Copy the .uf2 file with the required firmware to that drive.

As soon as the drive disappears, the firmware is loaded and started.

14 changes: 11 additions & 3 deletions ports/samd/boards/samd21x18a.ld
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@
GNU linker script for SAMD21
*/

/*
_codesize is defined in mpconfigmcu.mk or mpconfigboard.mk as
MICROPY_HW_CODESIZE and is set in Makefile
*/

_flashsize = 256K; /* The physical flash size */
_bootloader = 8K; /* Must match the ORIGIN value of FLASH */

/* Specify the memory areas */
MEMORY
{
FLASH (rx) : ORIGIN = 0x00002000, LENGTH = 256K - 8K
FLASH (rx) : ORIGIN = 0x00002000, LENGTH = _codesize
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 32K
}

/* Top end of the stack, with room for double-tap variable */
_estack = ORIGIN(RAM) + LENGTH(RAM) - 8;
_sstack = _estack - 8K;

_oflash_fs = ORIGIN(FLASH) + 192K - 8K;
_sflash_fs = LENGTH(FLASH) - 192K + 8K - 1;
_oflash_fs = ORIGIN(FLASH) + _codesize;
_sflash_fs = _flashsize - _codesize - _bootloader;

_sheap = _ebss;
_eheap = _sstack;
14 changes: 11 additions & 3 deletions ports/samd/boards/samd51x19a.ld
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@
GNU linker script for SAMD51
*/

/*
_codesize is defined in mpconfigmcu.mk or mpconfigboard.mk as
MICROPY_HW_CODESIZE and is set in Makefile
*/

_flashsize = 512K; /* The physical flash size */
_bootloader = 16K; /* Must match the ORIGIN value of FLASH */

/* Specify the memory areas */
MEMORY
{
FLASH (rx) : ORIGIN = 0x00004000, LENGTH = 512K - 16K
FLASH (rx) : ORIGIN = 0x00004000, LENGTH = _codesize
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 192K
}

/* Top end of the stack, with room for double-tap variable */
_estack = ORIGIN(RAM) + LENGTH(RAM) - 8;
_sstack = _estack - 16K;

_oflash_fs = ORIGIN(FLASH) + 384K - 16K;
_sflash_fs = LENGTH(FLASH) - 384K + 16K - 1;
_oflash_fs = ORIGIN(FLASH) + _codesize;
_sflash_fs = _flashsize - _codesize - _bootloader;

_sheap = _ebss;
_eheap = _sstack;
14 changes: 11 additions & 3 deletions ports/samd/boards/samd51x20a.ld
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@
GNU linker script for SAMD51x20
*/

/*
_codesize is defined in mpconfigmcu.mk or mpconfigboard.mk as
MICROPY_HW_CODESIZE and is set in Makefile
*/

_flashsize = 1024K; /* The physical flash size */
_bootloader = 16K; /* Must match the ORIGIN value of FLASH */

/* Specify the memory areas */
MEMORY
{
FLASH (rx) : ORIGIN = 0x00004000, LENGTH = 1024K - 16K
FLASH (rx) : ORIGIN = 0x00004000, LENGTH = _codesize
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 256K
}

/* Top end of the stack, with room for double-tap variable */
_estack = ORIGIN(RAM) + LENGTH(RAM) - 8;
_sstack = _estack - 16K;

_oflash_fs = ORIGIN(FLASH) + 384K - 16K;
_sflash_fs = LENGTH(FLASH) - 384K + 16K - 1;
_oflash_fs = ORIGIN(FLASH) + _codesize;
_sflash_fs = _flashsize - _codesize - _bootloader;

_sheap = _ebss;
_eheap = _sstack;
16 changes: 7 additions & 9 deletions ports/samd/machine_adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@
* THE SOFTWARE.
*/

#include "py/runtime.h"

#if MICROPY_PY_MACHINE_ADC

#include <stdint.h>
#include "py/obj.h"
#include "py/runtime.h"
#include "py/mphal.h"

#include "sam.h"
Expand Down Expand Up @@ -82,14 +85,7 @@ static uint8_t resolution[] = {
ADC_CTRLB_RESSEL_8BIT_Val, ADC_CTRLB_RESSEL_10BIT_Val, ADC_CTRLB_RESSEL_12BIT_Val
};

// Calculate the floor value of log2(n)
mp_int_t log2i(mp_int_t num) {
mp_int_t res = 0;
for (; num > 1; num >>= 1) {
res += 1;
}
return res;
}
extern mp_int_t log2i(mp_int_t num);

STATIC void adc_obj_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
(void)kind;
Expand Down Expand Up @@ -274,3 +270,5 @@ static void adc_init(machine_adc_obj_t *self) {
// Set the port as given in self->id as ADC
mp_hal_set_pin_mux(self->id, ALT_FCT_ADC);
}

#endif
7 changes: 6 additions & 1 deletion ports/samd/machine_dac.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@
* THE SOFTWARE.
*/

#include "py/runtime.h"

#if MICROPY_PY_MACHINE_DAC

#include <stdint.h>
#include "py/obj.h"
#include "py/runtime.h"
#include "py/mphal.h"

#include "sam.h"
Expand Down Expand Up @@ -183,3 +186,5 @@ MP_DEFINE_CONST_OBJ_TYPE(
print, dac_print,
locals_dict, &dac_locals_dict
);

#endif
5 changes: 5 additions & 0 deletions ports/samd/machine_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
*/

#include "py/runtime.h"

#if MICROPY_PY_MACHINE_I2C

#include "py/mphal.h"
#include "py/mperrno.h"
#include "extmod/machine_i2c.h"
Expand Down Expand Up @@ -274,3 +277,5 @@ MP_DEFINE_CONST_OBJ_TYPE(
protocol, &machine_i2c_p,
locals_dict, &mp_machine_i2c_locals_dict
);

#endif
7 changes: 6 additions & 1 deletion ports/samd/machine_pwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@
* THE SOFTWARE.
*/

#include <string.h>
#include "py/runtime.h"

#if MICROPY_PY_MACHINE_PWM

#include <string.h>
#include "py/mphal.h"
#include "modmachine.h"
#include "clock_config.h"
Expand Down Expand Up @@ -393,3 +396,5 @@ STATIC void mp_machine_pwm_duty_set_ns(machine_pwm_obj_t *self, mp_int_t duty_ns
duty_type_flags[self->device] &= ~(1 << self->channel);
mp_machine_pwm_start(self);
}

#endif
17 changes: 6 additions & 11 deletions ports/samd/machine_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include "py/runtime.h"

#if MICROPY_PY_MACHINE_SPI

#include "py/mphal.h"
#include "extmod/machine_spi.h"
#include "modmachine.h"
Expand Down Expand Up @@ -57,7 +61,6 @@ typedef struct _machine_spi_obj_t {
} machine_spi_obj_t;

extern Sercom *sercom_instance[];
MP_REGISTER_ROOT_POINTER(void *sercom_table[SERCOM_INST_NUM]);

void common_spi_irq_handler(int spi_id) {
// handle Sercom IRQ RXC
Expand Down Expand Up @@ -270,16 +273,6 @@ STATIC void machine_sercom_deinit(mp_obj_base_t *self_in) {
MP_STATE_PORT(sercom_table[self->id]) = NULL;
}

void sercom_deinit_all(void) {
for (int i = 0; i < SERCOM_INST_NUM; i++) {
Sercom *spi = sercom_instance[i];
spi->SPI.INTENCLR.reg = 0xff;
sercom_register_irq(i, NULL);
sercom_enable(spi, 0);
MP_STATE_PORT(sercom_table[i]) = NULL;
}
}

STATIC void machine_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
machine_spi_obj_t *self = (machine_spi_obj_t *)self_in;

Expand Down Expand Up @@ -341,3 +334,5 @@ MP_DEFINE_CONST_OBJ_TYPE(
protocol, &machine_spi_p,
locals_dict, &mp_machine_spi_locals_dict
);

#endif
33 changes: 26 additions & 7 deletions ports/samd/machine_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
* THE SOFTWARE.
*/
#include "py/runtime.h"

#if MICROPY_PY_MACHINE_I2C || MICROPY_PY_MACHINE_SPI || MICROPY_PY_MACHINE_UART

#include "py/mphal.h"
#include "py/stream.h"
#include "py/ringbuf.h"
Expand Down Expand Up @@ -59,6 +62,28 @@ typedef struct _machine_uart_obj_t {
} machine_uart_obj_t;

Sercom *sercom_instance[] = SERCOM_INSTS;
MP_REGISTER_ROOT_POINTER(void *sercom_table[SERCOM_INST_NUM]);

// Common Sercom functions used by all Serial devices
void sercom_enable(Sercom *uart, int state) {
uart->USART.CTRLA.bit.ENABLE = state; // Set the state on/off
// Wait for the Registers to update.
while (uart->USART.SYNCBUSY.bit.ENABLE) {
}
}

void sercom_deinit_all(void) {
for (int i = 0; i < SERCOM_INST_NUM; i++) {
Sercom *uart = sercom_instance[i];
uart->USART.INTENCLR.reg = 0xff;
sercom_register_irq(i, NULL);
sercom_enable(uart, 0);
MP_STATE_PORT(sercom_table[i]) = NULL;
}
}
#endif

#if MICROPY_PY_MACHINE_UART

STATIC const char *_parity_name[] = {"None", "", "0", "1"}; // Is defined as 0, 2, 3

Expand Down Expand Up @@ -105,13 +130,6 @@ void common_uart_irq_handler(int uart_id) {
}
}

void sercom_enable(Sercom *uart, int state) {
uart->USART.CTRLA.bit.ENABLE = state; // Set the state on/off
// Wait for the Registers to update.
while (uart->USART.SYNCBUSY.bit.ENABLE) {
}
}

STATIC void machine_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_printf(print, "UART(%u, baudrate=%u, bits=%u, parity=%s, stop=%u, "
Expand Down Expand Up @@ -544,3 +562,4 @@ MP_DEFINE_CONST_OBJ_TYPE(
protocol, &uart_stream_p,
locals_dict, &machine_uart_locals_dict
);
#endif
9 changes: 8 additions & 1 deletion ports/samd/machine_wdt.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ typedef struct _machine_wdt_obj_t {
mp_obj_base_t base;
} machine_wdt_obj_t;

extern mp_int_t log2i(mp_int_t num);
// Calculate the floor value of log2(n)
mp_int_t log2i(mp_int_t num) {
mp_int_t res = 0;
for (; num > 1; num >>= 1) {
res += 1;
}
return res;
}

STATIC const machine_wdt_obj_t machine_wdt = {{&machine_wdt_type}};

Expand Down
8 changes: 7 additions & 1 deletion ports/samd/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,18 @@ void samd_main(void) {

soft_reset_exit:
mp_printf(MP_PYTHON_PRINTER, "MPY: soft reboot\n");
#if MICROPY_PY_MACHINE_ADC
adc_deinit_all();
#endif
pin_irq_deinit_all();
#if MICROPY_PY_MACHINE_PWM
pwm_deinit_all();
sercom_deinit_all();
#endif
soft_timer_deinit();
gc_sweep_all();
#if MICROPY_PY_MACHINE_I2C || MICROPY_PY_MACHINE_SPI || MICROPY_PY_MACHINE_UART
sercom_deinit_all();
#endif
mp_deinit();
}
}
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy