Skip to content

Commit b1ca548

Browse files
committed
- Fixed stack & heap variables from linkerscript causing HardFaults. Placed in supervisor/port.c
- Moved LEDs & PBs from supervisor/port.c to boards/$(BOARD)/board.c for APARD - Reviewed Processor.c in common-hal/microcontroller - Prepared stubs for common-hal/microcontroller/Pin.c
1 parent 8978e3e commit b1ca548

7 files changed

Lines changed: 129 additions & 73 deletions

File tree

ports/analog/boards/APARD/board.c

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,24 @@
55
// SPDX-License-Identifier: MIT
66

77
#include "supervisor/board.h"
8+
#include "supervisor/port.h"
9+
#include "mpconfigboard.h"
10+
#include "max32_port.h"
11+
12+
// Board-level setup for MAX32690
13+
// clang-format off
14+
const mxc_gpio_cfg_t pb_pin[] = {
15+
{ MXC_GPIO1, MXC_GPIO_PIN_27, MXC_GPIO_FUNC_IN, MXC_GPIO_PAD_NONE, MXC_GPIO_VSSEL_VDDIOH, MXC_GPIO_DRVSTR_0},
16+
};
17+
const int num_pbs = (sizeof(pb_pin) / sizeof(mxc_gpio_cfg_t));
18+
19+
const mxc_gpio_cfg_t led_pin[] = {
20+
{ MXC_GPIO2, MXC_GPIO_PIN_1, MXC_GPIO_FUNC_OUT, MXC_GPIO_PAD_NONE, MXC_GPIO_VSSEL_VDDIO, MXC_GPIO_DRVSTR_0 },
21+
{ MXC_GPIO0, MXC_GPIO_PIN_11, MXC_GPIO_FUNC_OUT, MXC_GPIO_PAD_NONE, MXC_GPIO_VSSEL_VDDIO, MXC_GPIO_DRVSTR_0 },
22+
{ MXC_GPIO0, MXC_GPIO_PIN_12, MXC_GPIO_FUNC_OUT, MXC_GPIO_PAD_NONE, MXC_GPIO_VSSEL_VDDIO, MXC_GPIO_DRVSTR_0 },
23+
};
24+
const int num_leds = (sizeof(led_pin) / sizeof(mxc_gpio_cfg_t));
25+
// clang-format on
826

927
// DEFAULT: Using the weak-defined supervisor/shared/board.c functions
1028

@@ -15,7 +33,23 @@
1533
// bool board_requests_safe_mode(void);
1634

1735
// Initializes board related state once on start up.
18-
// void board_init(void);
36+
void board_init(void) {
37+
// Enable GPIO (enables clocks + common init for ports)
38+
for (int i = 0; i < MXC_CFG_GPIO_INSTANCES; i++){
39+
MXC_GPIO_Init(0x1 << i);
40+
}
41+
42+
// Init Board LEDs
43+
/* setup GPIO for the LED */
44+
for (int i = 0; i < num_leds; i++) {
45+
// Set the output value
46+
MXC_GPIO_OutClr(led_pin[i].port, led_pin[i].mask);
47+
MXC_GPIO_Config(&led_pin[i]);
48+
}
49+
50+
// Turn on one LED to indicate Sign of Life
51+
MXC_GPIO_OutSet(led_pin[2].port, led_pin[2].mask);
52+
}
1953

2054
// Reset the state of off MCU components such as neopixels.
2155
// void reset_board(void);
@@ -24,4 +58,5 @@
2458
// state. It should not prevent the user access method from working (such as
2559
// disabling USB, BLE or flash) because CircuitPython may continue to run.
2660
// void board_deinit(void);
61+
2762
/*******************************************************************/

ports/analog/boards/APARD/mpconfigboard.mk

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@
55
# SPDX-License-Identifier: MIT
66

77
INTERNAL_FLASH_FILESYSTEM = 1
8-
# FLASH: 0x10000000 to 0x10340000
8+
# FLASH: 0x10000000 to 0x10300000 (ARM)
99
# SRAM: 0x20000000 to 0x20100000
1010

1111
USB_PRODUCT = "MAX32690 APARD"
1212
USB_MANUFACTURER = "Analog Devices, Inc."
13+
14+
# NOTE: Not implementing external flash for now
1315
# CFLAGS+=-DEXT_FLASH_MX25
1416

17+
# define 13 bytes UID for memory safety (buffer gets passed as a raw ptr)
18+
COMMON_HAL_MCU_PROCESSOR_UID_LENGTH = 13
19+
1520
MCU_SERIES=max32
1621
MCU_VARIANT=max32690
1722

ports/analog/common-hal/microcontroller/Pin.c

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,62 @@
22
#include <stdbool.h>
33

44
#include "shared-bindings/microcontroller/Pin.h"
5-
// #include "shared-bindings/digitalio/DigitalInOut.h"
5+
#include "mpconfigboard.h"
6+
#include "pins.h"
67

8+
#include "mxc_sys.h"
9+
#include "max32690.h"
10+
#include "gpio.h"
711

8-
// #include "gpio.h"
9-
10-
11-
//FIXME: Implement
1212
void reset_all_pins(void) {
13-
return;
13+
// todo: this is not a good method for this long-term
14+
// Pins should be individually reset to account for never_reset pins like SWD
15+
for (int i = 0; i < NUM_GPIO_PORTS; i++) {
16+
MXC_GPIO_Reset(i);
17+
}
1418
}
1519

16-
// FIXME: Implement
20+
// todo: Implement
1721
void reset_pin_number(uint8_t pin) {
18-
return;
1922
}
2023

21-
// FIXME: Implement
24+
// todo: Implement
2225
void claim_pin(const mcu_pin_obj_t *pin) {
2326
return;
2427
}
2528

26-
// FIXME: Implement
29+
// todo: Implement
2730
bool pin_number_is_free(uint8_t pin_number) {
2831
return true;
2932
}
3033

31-
// FIXME: Implement
34+
35+
// todo: Implement
3236
void never_reset_pin_number(uint8_t pin_number) {
3337
return;
3438
}
39+
40+
//todo: implement
41+
uint8_t common_hal_mcu_pin_number(const mcu_pin_obj_t *pin) {
42+
return 0;
43+
}
44+
45+
// todo: implement
46+
bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) {
47+
return true;
48+
}
49+
50+
void common_hal_never_reset_pin(const mcu_pin_obj_t *pin) {
51+
}
52+
53+
void common_hal_reset_pin(const mcu_pin_obj_t *pin) {
54+
}
55+
56+
void common_hal_mcu_pin_claim(const mcu_pin_obj_t *pin) {
57+
}
58+
59+
void common_hal_mcu_pin_claim_number(uint8_t pin_no) {
60+
}
61+
62+
void common_hal_mcu_pin_reset_number(uint8_t pin_no) {
63+
}

ports/analog/common-hal/microcontroller/Processor.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010
#include "common-hal/microcontroller/Processor.h"
1111
#include "shared-bindings/microcontroller/ResetReason.h"
1212

13-
#include "system_max32690.h"
13+
#include "max32_port.h"
1414

15-
//
15+
// No means of getting core temperature for currently supported devices
1616
float common_hal_mcu_processor_get_temperature(void) {
1717
return NAN;
1818
}
1919

20-
// TODO: Determine if there's a means of getting core voltage
20+
// MAX32690 can measure VCORE
21+
// TODO: (low prior.) Implement ADC API under "peripherals" and use API to measure VCORE
2122
float common_hal_mcu_processor_get_voltage(void) {
2223
return NAN;
2324
}
@@ -26,11 +27,16 @@ uint32_t common_hal_mcu_processor_get_frequency(void) {
2627
return SystemCoreClock;
2728
}
2829

30+
// NOTE: COMMON_HAL_MCU_PROCESSOR_UID_LENGTH is defined in mpconfigboard.h
31+
// Use this per device to make sure raw_id is an appropriate minimum number of bytes
2932
void common_hal_mcu_processor_get_uid(uint8_t raw_id[]) {
33+
MXC_SYS_GetUSN(raw_id, NULL); // NULL checksum will not be verified by AES
3034
return;
3135
}
3236

33-
// TODO: May need to add reset reason in alarm / deepsleep cases
3437
mcu_reset_reason_t common_hal_mcu_processor_get_reset_reason(void) {
38+
#if CIRCUITPY_ALARM
39+
// TODO: (low prior.) add reset reason in alarm / deepsleep cases (should require alarm peripheral API in "peripherals")
40+
#endif
3541
return RESET_REASON_UNKNOWN;
3642
}

ports/analog/common-hal/microcontroller/__init__.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,20 @@
2222

2323

2424
#include "max32690.h"
25+
#include "mxc_delay.h"
26+
2527
/** NOTE: It is not advised to directly include the below!
2628
* These are includes taken care of by the core cmsis file.
2729
* e.g. "max32690.h". Since CMSIS is compiled as lib, these are
2830
* included there as <core_cm4.h> for example.
2931
*/
30-
// #include "core_cmFunc.h" // For enable/disable interrupts
31-
// #include "core_cm4.h" // For NVIC_SystemReset
32-
// #include "core_cmInstr.h" // For __DMB Data Memory Barrier
32+
// #include <core_cmFunc.h> // For enable/disable interrupts
33+
// #include <core_cm4.h> // For NVIC_SystemReset
34+
// #include <core_cmInstr.h> // For __DMB Data Memory Barrier (flush DBUS activity)
3335

3436
void common_hal_mcu_delay_us(uint32_t delay) {
35-
// uint32_t ticks_per_us = HAL_RCC_GetSysClockFreq() / 1000000UL;
36-
// delay *= ticks_per_us;
37-
// SysTick->VAL = 0UL;
38-
// SysTick->LOAD = delay;
39-
// SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk;
40-
// while ((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) == 0) {
41-
// }
42-
// SysTick->CTRL = 0UL;
37+
38+
MXC_Delay(MXC_DELAY_USEC(delay));
4339
}
4440

4541
volatile uint32_t nesting_count = 0;
@@ -59,7 +55,7 @@ void common_hal_mcu_enable_interrupts(void) {
5955
if (nesting_count > 0) {
6056
return;
6157
}
62-
__DMB();
58+
__DMB(); // flush internal DBUS before proceeding
6359
__enable_irq();
6460
}
6561

@@ -75,7 +71,6 @@ void common_hal_mcu_on_next_reset(mcu_runmode_t runmode) {
7571
}
7672

7773
void common_hal_mcu_reset(void) {
78-
7974
if (next_reset_to_bootloader) {
8075
reset_to_bootloader();
8176
} else {
@@ -398,6 +393,8 @@ static const mp_rom_map_elem_t mcu_pin_global_dict_table[] = {
398393
};
399394
MP_DEFINE_CONST_DICT(mcu_pin_globals, mcu_pin_global_dict_table);
400395

396+
397+
/** NOTE: Not implemented yet */
401398
// #if CIRCUITPY_INTERNAL_NVM_SIZE > 0
402399
// // The singleton nvm.ByteArray object.
403400
// const nvm_bytearray_obj_t common_hal_mcu_nvm_obj = {
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@
44

55
#include <stdint.h>
66

7-
#include "mxc_sys.h"
7+
#include "mxc_assert.h"
8+
#include "mxc_delay.h"
9+
#include "mxc_device.h"
810
#include "mxc_pins.h"
11+
#include "mxc_sys.h"
12+
913
#include "gpio.h"
10-
#include "mxc_assert.h"
14+
15+
#ifdef MQAX32690
16+
#include "system_max32690.h"
17+
#include "max32690.h"
18+
#endif
1119

1220
/** Linker variables defined....
1321
* _estack: end of the stack

ports/analog/supervisor/port.c

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,19 @@
4747
#include "mxc_delay.h"
4848
#include "rtc.h"
4949

50+
// Externs defined by linker .ld file
51+
extern uint32_t _stack, _heap, _estack, _eheap;
52+
extern uint32_t _ebss;
53+
54+
// From boards/$(BOARD)/board.c
55+
extern const mxc_gpio_cfg_t pb_pin[];
56+
extern const int num_pbs;
57+
extern const mxc_gpio_cfg_t led_pin[];
58+
extern const int num_leds;
59+
5060
//todo: define an LED HAL
5161
// #include "peripherals/led.h"
5262

53-
#ifdef MAX32690
54-
// Board-level setup for MAX32690
55-
// clang-format off
56-
const mxc_gpio_cfg_t pb_pin[] = {
57-
{ MXC_GPIO1, MXC_GPIO_PIN_27, MXC_GPIO_FUNC_IN, MXC_GPIO_PAD_NONE, MXC_GPIO_VSSEL_VDDIOH, MXC_GPIO_DRVSTR_0},
58-
};
59-
const unsigned int num_pbs = (sizeof(pb_pin) / sizeof(mxc_gpio_cfg_t));
60-
61-
const mxc_gpio_cfg_t led_pin[] = {
62-
{ MXC_GPIO2, MXC_GPIO_PIN_1, MXC_GPIO_FUNC_OUT, MXC_GPIO_PAD_NONE, MXC_GPIO_VSSEL_VDDIO, MXC_GPIO_DRVSTR_0 },
63-
{ MXC_GPIO0, MXC_GPIO_PIN_11, MXC_GPIO_FUNC_OUT, MXC_GPIO_PAD_NONE, MXC_GPIO_VSSEL_VDDIO, MXC_GPIO_DRVSTR_0 },
64-
{ MXC_GPIO0, MXC_GPIO_PIN_12, MXC_GPIO_FUNC_OUT, MXC_GPIO_PAD_NONE, MXC_GPIO_VSSEL_VDDIO, MXC_GPIO_DRVSTR_0 },
65-
};
66-
const unsigned int num_leds = (sizeof(led_pin) / sizeof(mxc_gpio_cfg_t));
67-
// clang-format on
68-
#endif
69-
7063
// For caching rtc data for ticks
7164
static uint32_t subsec, sec = 0;
7265

@@ -96,7 +89,7 @@ safe_mode_t port_init(void) {
9689
}
9790

9891
// Turn on one LED to indicate Sign of Life
99-
MXC_GPIO_OutSet(led_pin[0].port, led_pin[0].mask);
92+
MXC_GPIO_OutSet(led_pin[2].port, led_pin[2].mask);
10093

10194
// Init RTC w/ 0sec, 0subsec
10295
// Driven by 32.768 kHz ERTCO, with ssec= 1/4096 s
@@ -120,23 +113,9 @@ void reset_cpu(void) {
120113

121114
// Reset MCU state
122115
void reset_port(void) {
123-
int err;
124-
// Reset GPIO Ports
125-
// Enable GPIO (enables clocks + common init for ports)
126-
for (int i = 0; i < MXC_CFG_GPIO_INSTANCES; i++){
127-
err = MXC_GPIO_Reset(0x1 << i);
128-
if (err) {
129-
// todo: indicate some gpio error
130-
continue;
131-
}
132-
}
133-
134-
// TODO: Reset peripheral clocks
116+
reset_all_pins();
135117

136-
// Reset 1/1024 tick timer
137-
MXC_RTC_Stop();
138-
MXC_RTC_ClearFlags(0xFFFFFFFF);
139-
MXC_RTC_Init(0,0);
118+
// todo: may need rtc-related resets here later
140119
}
141120

142121
// Reset to the bootloader
@@ -154,22 +133,19 @@ void reset_to_bootloader(void) {
154133
* Return variables defined by linkerscript.
155134
*/
156135
uint32_t *port_stack_get_limit(void) {
157-
// ignore array bounds GCC warnings for stack here
136+
// ignore array bounds GCC warnings
158137
#pragma GCC diagnostic push
159138
#pragma GCC diagnostic ignored "-Warray-bounds"
160139

161140
// NOTE: Only return how much stack we have alloted for CircuitPython
162-
return (uint32_t *)(port_stack_get_top() - (CIRCUITPY_DEFAULT_STACK_SIZE + CIRCUITPY_EXCEPTION_STACK_SIZE) / sizeof(uint32_t));
163-
// return _estack;
164-
165-
// end GCC diagnostic disable
141+
return port_stack_get_top() - (CIRCUITPY_DEFAULT_STACK_SIZE + CIRCUITPY_EXCEPTION_STACK_SIZE) / sizeof(uint32_t);
166142
#pragma GCC diagnostic pop
167143
}
168144
uint32_t *port_stack_get_top(void) {
169-
return (uint32_t *)__stack;
145+
return &_stack;
170146
}
171147
uint32_t *port_heap_get_bottom(void) {
172-
return (uint32_t *)__heap;
148+
return &_heap;
173149
}
174150
uint32_t *port_heap_get_top(void) {
175151
return port_stack_get_limit();

0 commit comments

Comments
 (0)