Content-Length: 1077936 | pFad | http://github.com/micropython/micropython/commit/47fa723586dc4ae81df02f5a0a10b2b018045ab0

8A samd/modmachine: Make some machine classes configurable by #defines. · micropython/micropython@47fa723 · GitHub
Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 47fa723

Browse files
robert-hhdpgeorge
authored andcommittedMay 22, 2023
samd/modmachine: Make some machine classes configurable by #defines.
These include ADC, DAC, I2C, SoftI2C, SPI, SoftI2C, PWM, UART, pulse. This is useful for devices like the Adafruit Trinket series which have almost no accessible GPIO pins. Signed-off-by: robert-hh <robert@hammelrath.com>
1 parent b2df094 commit 47fa723

12 files changed

+131
-32
lines changed
 

Diff for: ‎ports/samd/machine_adc.c

+7-9
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@
2525
* THE SOFTWARE.
2626
*/
2727

28+
#include "py/runtime.h"
29+
30+
#if MICROPY_PY_MACHINE_ADC
31+
2832
#include <stdint.h>
2933
#include "py/obj.h"
30-
#include "py/runtime.h"
3134
#include "py/mphal.h"
3235

3336
#include "sam.h"
@@ -82,14 +85,7 @@ static uint8_t resolution[] = {
8285
ADC_CTRLB_RESSEL_8BIT_Val, ADC_CTRLB_RESSEL_10BIT_Val, ADC_CTRLB_RESSEL_12BIT_Val
8386
};
8487

85-
// Calculate the floor value of log2(n)
86-
mp_int_t log2i(mp_int_t num) {
87-
mp_int_t res = 0;
88-
for (; num > 1; num >>= 1) {
89-
res += 1;
90-
}
91-
return res;
92-
}
88+
extern mp_int_t log2i(mp_int_t num);
9389

9490
STATIC void adc_obj_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
9591
(void)kind;
@@ -274,3 +270,5 @@ static void adc_init(machine_adc_obj_t *self) {
274270
// Set the port as given in self->id as ADC
275271
mp_hal_set_pin_mux(self->id, ALT_FCT_ADC);
276272
}
273+
274+
#endif

Diff for: ‎ports/samd/machine_dac.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@
2525
* THE SOFTWARE.
2626
*/
2727

28+
#include "py/runtime.h"
29+
30+
#if MICROPY_PY_MACHINE_DAC
31+
2832
#include <stdint.h>
2933
#include "py/obj.h"
30-
#include "py/runtime.h"
3134
#include "py/mphal.h"
3235

3336
#include "sam.h"
@@ -183,3 +186,5 @@ MP_DEFINE_CONST_OBJ_TYPE(
183186
print, dac_print,
184187
locals_dict, &dac_locals_dict
185188
);
189+
190+
#endif

Diff for: ‎ports/samd/machine_i2c.c

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
*/
2727

2828
#include "py/runtime.h"
29+
30+
#if MICROPY_PY_MACHINE_I2C
31+
2932
#include "py/mphal.h"
3033
#include "py/mperrno.h"
3134
#include "extmod/machine_i2c.h"
@@ -274,3 +277,5 @@ MP_DEFINE_CONST_OBJ_TYPE(
274277
protocol, &machine_i2c_p,
275278
locals_dict, &mp_machine_i2c_locals_dict
276279
);
280+
281+
#endif

Diff for: ‎ports/samd/machine_pwm.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@
2525
* THE SOFTWARE.
2626
*/
2727

28-
#include <string.h>
2928
#include "py/runtime.h"
29+
30+
#if MICROPY_PY_MACHINE_PWM
31+
32+
#include <string.h>
3033
#include "py/mphal.h"
3134
#include "modmachine.h"
3235
#include "clock_config.h"
@@ -393,3 +396,5 @@ STATIC void mp_machine_pwm_duty_set_ns(machine_pwm_obj_t *self, mp_int_t duty_ns
393396
duty_type_flags[self->device] &= ~(1 << self->channel);
394397
mp_machine_pwm_start(self);
395398
}
399+
400+
#endif

Diff for: ‎ports/samd/machine_spi.c

+6-11
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2525
* THE SOFTWARE.
2626
*/
27+
2728
#include "py/runtime.h"
29+
30+
#if MICROPY_PY_MACHINE_SPI
31+
2832
#include "py/mphal.h"
2933
#include "extmod/machine_spi.h"
3034
#include "modmachine.h"
@@ -57,7 +61,6 @@ typedef struct _machine_spi_obj_t {
5761
} machine_spi_obj_t;
5862

5963
extern Sercom *sercom_instance[];
60-
MP_REGISTER_ROOT_POINTER(void *sercom_table[SERCOM_INST_NUM]);
6164

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

273-
void sercom_deinit_all(void) {
274-
for (int i = 0; i < SERCOM_INST_NUM; i++) {
275-
Sercom *spi = sercom_instance[i];
276-
spi->SPI.INTENCLR.reg = 0xff;
277-
sercom_register_irq(i, NULL);
278-
sercom_enable(spi, 0);
279-
MP_STATE_PORT(sercom_table[i]) = NULL;
280-
}
281-
}
282-
283276
STATIC void machine_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
284277
machine_spi_obj_t *self = (machine_spi_obj_t *)self_in;
285278

@@ -341,3 +334,5 @@ MP_DEFINE_CONST_OBJ_TYPE(
341334
protocol, &machine_spi_p,
342335
locals_dict, &mp_machine_spi_locals_dict
343336
);
337+
338+
#endif

Diff for: ‎ports/samd/machine_uart.c

+26-7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
* THE SOFTWARE.
2626
*/
2727
#include "py/runtime.h"
28+
29+
#if MICROPY_PY_MACHINE_I2C || MICROPY_PY_MACHINE_SPI || MICROPY_PY_MACHINE_UART
30+
2831
#include "py/mphal.h"
2932
#include "py/stream.h"
3033
#include "py/ringbuf.h"
@@ -59,6 +62,28 @@ typedef struct _machine_uart_obj_t {
5962
} machine_uart_obj_t;
6063

6164
Sercom *sercom_instance[] = SERCOM_INSTS;
65+
MP_REGISTER_ROOT_POINTER(void *sercom_table[SERCOM_INST_NUM]);
66+
67+
// Common Sercom functions used by all Serial devices
68+
void sercom_enable(Sercom *uart, int state) {
69+
uart->USART.CTRLA.bit.ENABLE = state; // Set the state on/off
70+
// Wait for the Registers to update.
71+
while (uart->USART.SYNCBUSY.bit.ENABLE) {
72+
}
73+
}
74+
75+
void sercom_deinit_all(void) {
76+
for (int i = 0; i < SERCOM_INST_NUM; i++) {
77+
Sercom *uart = sercom_instance[i];
78+
uart->USART.INTENCLR.reg = 0xff;
79+
sercom_register_irq(i, NULL);
80+
sercom_enable(uart, 0);
81+
MP_STATE_PORT(sercom_table[i]) = NULL;
82+
}
83+
}
84+
#endif
85+
86+
#if MICROPY_PY_MACHINE_UART
6287

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

@@ -105,13 +130,6 @@ void common_uart_irq_handler(int uart_id) {
105130
}
106131
}
107132

108-
void sercom_enable(Sercom *uart, int state) {
109-
uart->USART.CTRLA.bit.ENABLE = state; // Set the state on/off
110-
// Wait for the Registers to update.
111-
while (uart->USART.SYNCBUSY.bit.ENABLE) {
112-
}
113-
}
114-
115133
STATIC void machine_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
116134
machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
117135
mp_printf(print, "UART(%u, baudrate=%u, bits=%u, parity=%s, stop=%u, "
@@ -544,3 +562,4 @@ MP_DEFINE_CONST_OBJ_TYPE(
544562
protocol, &uart_stream_p,
545563
locals_dict, &machine_uart_locals_dict
546564
);
565+
#endif

Diff for: ‎ports/samd/machine_wdt.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,14 @@ typedef struct _machine_wdt_obj_t {
3636
mp_obj_base_t base;
3737
} machine_wdt_obj_t;
3838

39-
extern mp_int_t log2i(mp_int_t num);
39+
// Calculate the floor value of log2(n)
40+
mp_int_t log2i(mp_int_t num) {
41+
mp_int_t res = 0;
42+
for (; num > 1; num >>= 1) {
43+
res += 1;
44+
}
45+
return res;
46+
}
4047

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

Diff for: ‎ports/samd/main.c

+6
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,18 @@ void samd_main(void) {
8181

8282
soft_reset_exit:
8383
mp_printf(MP_PYTHON_PRINTER, "MPY: soft reboot\n");
84+
#if MICROPY_PY_MACHINE_ADC
8485
adc_deinit_all();
86+
#endif
8587
pin_irq_deinit_all();
88+
#if MICROPY_PY_MACHINE_PWM
8689
pwm_deinit_all();
90+
#endif
8791
soft_timer_deinit();
8892
gc_sweep_all();
93+
#if MICROPY_PY_MACHINE_I2C || MICROPY_PY_MACHINE_SPI || MICROPY_PY_MACHINE_UART
8994
sercom_deinit_all();
95+
#endif
9096
mp_deinit();
9197
}
9298
}

Diff for: ‎ports/samd/modmachine.c

+18
Original file line numberDiff line numberDiff line change
@@ -234,17 +234,33 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
234234
{ MP_ROM_QSTR(MP_QSTR_mem32), MP_ROM_PTR(&machine_mem32_obj) },
235235
{ MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_obj) },
236236

237+
#if MICROPY_PY_MACHINE_ADC
237238
{ MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) },
239+
#endif
240+
#if MICROPY_PY_MACHINE_DAC
238241
{ MP_ROM_QSTR(MP_QSTR_DAC), MP_ROM_PTR(&machine_dac_type) },
242+
#endif
239243
{ MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) },
240244
{ MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) },
245+
#if MICROPY_PY_MACHINE_PWM
241246
{ MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_pwm_type) },
247+
#endif
248+
#if MICROPY_PY_MACHINE_SOFTI2C
242249
{ MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) },
250+
#endif
251+
#if MICROPY_PY_MACHINE_I2C
243252
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) },
253+
#endif
254+
#if MICROPY_PY_MACHINE_SOFTSPI
244255
{ MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) },
256+
#endif
257+
#if MICROPY_PY_MACHINE_SPI
245258
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_spi_type) },
259+
#endif
246260
{ MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) },
261+
#if MICROPY_PY_MACHINE_UART
247262
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_uart_type) },
263+
#endif
248264
{ MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&machine_wdt_type) },
249265
#if MICROPY_PY_MACHINE_RTC
250266
{ MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&machine_rtc_type) },
@@ -254,7 +270,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
254270
{ MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) },
255271
{ MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) },
256272
{ MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) },
273+
#if MICROPY_PY_MACHINE_PULSE
257274
{ MP_ROM_QSTR(MP_QSTR_time_pulse_us), MP_ROM_PTR(&machine_time_pulse_us_obj) },
275+
#endif
258276
{ MP_ROM_QSTR(MP_QSTR_lightsleep), MP_ROM_PTR(&machine_lightsleep_obj) },
259277
{ MP_ROM_QSTR(MP_QSTR_deepsleep), MP_ROM_PTR(&machine_lightsleep_obj) },
260278

Diff for: ‎ports/samd/modmachine.h

+12
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,26 @@
2929
#include "py/obj.h"
3030
#include "shared/timeutils/timeutils.h"
3131

32+
#if MICROPY_PY_MACHINE_ADC
3233
extern const mp_obj_type_t machine_adc_type;
34+
#endif
35+
#if MICROPY_PY_MACHINE_DAC
3336
extern const mp_obj_type_t machine_dac_type;
37+
#endif
38+
#if MICROPY_PY_MACHINE_I2C
3439
extern const mp_obj_type_t machine_i2c_type;
40+
#endif
3541
extern const mp_obj_type_t machine_pin_type;
42+
#if MICROPY_PY_MACHINE_PWM
3643
extern const mp_obj_type_t machine_pwm_type;
44+
#endif
45+
#if MICROPY_PY_MACHINE_SPI
3746
extern const mp_obj_type_t machine_spi_type;
47+
#endif
3848
extern const mp_obj_type_t machine_timer_type;
49+
#if MICROPY_PY_MACHINE_UART
3950
extern const mp_obj_type_t machine_uart_type;
51+
#endif
4052
extern const mp_obj_type_t machine_wdt_type;
4153
#if MICROPY_PY_MACHINE_RTC
4254
extern const mp_obj_type_t machine_rtc_type;

Diff for: ‎ports/samd/mpconfigport.h

+23-2
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,39 @@
9494
#define MICROPY_PY_URANDOM (1)
9595
#define MICROPY_PY_UZLIB (1)
9696
#define MICROPY_PY_UASYNCIO (1)
97-
#define MICROPY_PY_MACHINE_I2C (1)
9897
#define MICROPY_PY_MACHINE_RTC (1)
99-
#define MICROPY_PY_MACHINE_SOFTI2C (1)
98+
#ifndef MICROPY_PY_MACHINE_ADC
99+
#define MICROPY_PY_MACHINE_ADC (1)
100+
#endif
101+
#ifndef MICROPY_PY_MACHINE_DAC
102+
#define MICROPY_PY_MACHINE_DAC (1)
103+
#endif
104+
#ifndef MICROPY_PY_MACHINE_I2C
105+
#define MICROPY_PY_MACHINE_I2C (1)
106+
#endif
107+
#ifndef MICROPY_PY_MACHINE_SPI
100108
#define MICROPY_PY_MACHINE_SPI (1)
109+
#endif
110+
#ifndef MICROPY_PY_MACHINE_SOFTI2C
111+
#define MICROPY_PY_MACHINE_SOFTI2C (1)
112+
#endif
113+
#ifndef MICROPY_PY_MACHINE_SOFTSPI
101114
#define MICROPY_PY_MACHINE_SOFTSPI (1)
115+
#endif
116+
#ifndef MICROPY_PY_MACHINE_UART
117+
#define MICROPY_PY_MACHINE_UART (1)
118+
#endif
102119
#define MICROPY_PY_MACHINE_TIMER (1)
103120
#define MICROPY_SOFT_TIMER_TICKS_MS systick_ms
104121
#define MICROPY_PY_OS_DUPTERM (3)
105122
#define MICROPY_PY_MACHINE_BITSTREAM (1)
123+
#ifndef MICROPY_PY_MACHINE_PULSE
106124
#define MICROPY_PY_MACHINE_PULSE (1)
125+
#endif
126+
#ifndef MICROPY_PY_MACHINE_PWM
107127
#define MICROPY_PY_MACHINE_PWM (1)
108128
#define MICROPY_PY_MACHINE_PWM_INCLUDEFILE "ports/samd/machine_pwm.c"
129+
#endif
109130
#define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new
110131

111132
#define MP_STATE_PORT MP_STATE_VM

Diff for: ‎ports/samd/pin_af.c

+8
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ const char *pin_name(int id) {
121121
return "-";
122122
}
123123

124+
#if MICROPY_PY_MACHINE_I2C || MICROPY_PY_MACHINE_SPI || MICROPY_PY_MACHINE_UART
124125
// Test, whether the given pin is defined and has signals for sercom.
125126
// If that applies return the alt_fct and pad_nr.
126127
// If not, an error will be raised.
@@ -135,7 +136,9 @@ sercom_pad_config_t get_sercom_config(int pin_id, uint8_t sercom_nr) {
135136
mp_raise_ValueError(MP_ERROR_TEXT("wrong serial device"));
136137
}
137138
}
139+
#endif
138140

141+
#if MICROPY_PY_MACHINE_ADC
139142
// Test, whether the given pin is defined as ADC.
140143
// If that applies return the adc instance and channel.
141144
// If not, an error will be raised.
@@ -152,6 +155,9 @@ adc_config_t get_adc_config(int pin_id, int32_t flag) {
152155
mp_raise_ValueError(MP_ERROR_TEXT("ADC pin used"));
153156
}
154157
}
158+
#endif
159+
160+
#if MICROPY_PY_MACHINE_PWM
155161

156162
// Test, whether the given pin is defined and has signals for pwm.
157163
// If that applies return the alt_fct, tcc number and channel number.
@@ -188,3 +194,5 @@ pwm_config_t get_pwm_config(int pin_id, int wanted_dev, uint8_t device_status[])
188194
}
189195
mp_raise_ValueError(MP_ERROR_TEXT("not a PWM Pin"));
190196
}
197+
198+
#endif

0 commit comments

Comments
 (0)
Failed to load comments.








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/micropython/micropython/commit/47fa723586dc4ae81df02f5a0a10b2b018045ab0

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy