aboutsummaryrefslogtreecommitdiff
path: root/atmel-samd/common-hal/microcontroller
diff options
context:
space:
mode:
authorScott Shawcroft <scott@tannewt.org>2017-09-22 18:05:51 -0700
committerDan Halbert <halbert@halwitz.org>2017-09-22 21:05:51 -0400
commit6839fff31303463d5c7942387fa498895edf1abf (patch)
treef4c5b94c6534c8d5c65ea6b7209cd2d681508f5f /atmel-samd/common-hal/microcontroller
parent3ad01ddb04b160b74b4baee9fce043da17dbcef5 (diff)
Move to ASF4 and introduce SAMD51 support. (#258)3.0.0-alpha.0
* atmel-samd: Remove ASF3. This will break builds. * atmel-samd: Add ASF4 for the SAMD21 and SAMD51. * Introduce the supervisor concept to facilitate porting. The supervisor is the code which runs individual MicroPython VMs. By splitting it out we make it more consistent and easier to find. This also adds very basic SAMD21 and SAMD51 support using the supervisor. Only the REPL currently works. This begins the work for #178.
Diffstat (limited to 'atmel-samd/common-hal/microcontroller')
-rw-r--r--atmel-samd/common-hal/microcontroller/Pin.c88
-rw-r--r--atmel-samd/common-hal/microcontroller/Pin.h30
-rw-r--r--atmel-samd/common-hal/microcontroller/Processor.c292
-rw-r--r--atmel-samd/common-hal/microcontroller/__init__.c25
4 files changed, 225 insertions, 210 deletions
diff --git a/atmel-samd/common-hal/microcontroller/Pin.c b/atmel-samd/common-hal/microcontroller/Pin.c
index d814eb89c..a7b494940 100644
--- a/atmel-samd/common-hal/microcontroller/Pin.c
+++ b/atmel-samd/common-hal/microcontroller/Pin.c
@@ -26,10 +26,16 @@
#include "shared-bindings/microcontroller/Pin.h"
-#include "asf/sam0/drivers/port/port.h"
+#include "atmel_start_pins.h"
+#include "hal/include/hal_gpio.h"
-#include "rgb_led_status.h"
+#include "supervisor/shared/rgb_led_status.h"
+#ifdef SAMD21
#include "samd21_pins.h"
+#endif
+#ifdef SAMD51
+#include "samd51_pins.h"
+#endif
#ifdef MICROPY_HW_NEOPIXEL
bool neopixel_in_use;
@@ -43,22 +49,39 @@ bool speaker_enable_in_use;
#endif
void reset_all_pins(void) {
- struct system_pinmux_config config;
- system_pinmux_get_config_defaults(&config);
- config.powersave = true;
-
- uint32_t pin_mask[2] = PORT_OUT_IMPLEMENTED;
+ uint32_t pin_mask[PORT_BITS / 32 + 1] = PORT_OUT_IMPLEMENTED;
// Do not full reset USB or SWD lines.
- pin_mask[0] &= ~(PORT_PA24 | PORT_PA25 | PORT_PA30 | PORT_PA31);
+ pin_mask[0] &= ~(PORT_PA24 | PORT_PA25 | PORT_PA30);
- system_pinmux_group_set_config(&(PORT->Group[0]), pin_mask[0] & ~MICROPY_PORT_A, &config);
- system_pinmux_group_set_config(&(PORT->Group[1]), pin_mask[1] & ~MICROPY_PORT_B, &config);
+ // The SWO pin changes between the 21 and 51.
+ #ifdef PORT_PB30H_CM4_SWO
+ pin_mask[1] &= ~(PORT_PB30);
+ #else
+ pin_mask[0] &= ~(PORT_PA31);
+ #endif
+
+ //gpio_set_port_direction(GPIO_PORTA, pin_mask[0] & ~MICROPY_PORT_A, GPIO_DIRECTION_OFF);
+ gpio_set_port_direction(GPIO_PORTB, pin_mask[1] & ~MICROPY_PORT_B, GPIO_DIRECTION_OFF);
+ #if PORT_BITS > 64
+ gpio_set_port_direction(GPIO_PORTC, pin_mask[2] & ~MICROPY_PORT_C, GPIO_DIRECTION_OFF);
+ #endif
+ #if PORT_BITS > 96
+ gpio_set_port_direction(GPIO_PORTD, pin_mask[3] & ~MICROPY_PORT_D, GPIO_DIRECTION_OFF);
+ #endif
// Configure SWD
- system_pinmux_get_config_defaults(&config);
- config.mux_position = 0x6;
- system_pinmux_group_set_config(&(PORT->Group[0]), PORT_PA30 | PORT_PA31, &config);
+ gpio_set_pin_direction(PIN_PA30, GPIO_DIRECTION_OUT);
+ #ifdef SAMD51
+ gpio_set_pin_function(PIN_PB30, MUX_PB30H_CM4_SWO);
+ gpio_set_pin_direction(PIN_PB30, GPIO_DIRECTION_OUT);
+ gpio_set_pin_function(PIN_PB30, MUX_PB30H_CM4_SWO);
+ #endif
+ #ifdef SAMD21
+ //gpio_set_pin_function(PIN_PA30, GPIO_PIN_FUNCTION_G);
+ //gpio_set_pin_direction(PIN_PA31, GPIO_DIRECTION_OUT);
+ //gpio_set_pin_function(PIN_PA31, GPIO_PIN_FUNCTION_G);
+ #endif
#ifdef MICROPY_HW_NEOPIXEL
neopixel_in_use = false;
@@ -71,12 +94,9 @@ void reset_all_pins(void) {
// After configuring SWD because it may be shared.
#ifdef SPEAKER_ENABLE_PIN
speaker_enable_in_use = false;
- struct port_config pin_conf;
- port_get_config_defaults(&pin_conf);
-
- pin_conf.direction = PORT_PIN_DIR_OUTPUT;
- port_pin_set_config(SPEAKER_ENABLE_PIN->pin, &pin_conf);
- port_pin_set_output_level(SPEAKER_ENABLE_PIN->pin, false);
+ gpio_set_pin_function(SPEAKER_ENABLE_PIN->pin, GPIO_PIN_FUNCTION_OFF);
+ gpio_set_pin_direction(SPEAKER_ENABLE_PIN->pin, GPIO_DIRECTION_OUT);
+ gpio_set_pin_level(SPEAKER_ENABLE_PIN->pin, false);
#endif
}
@@ -104,24 +124,26 @@ void reset_pin(uint8_t pin) {
}
#endif
- struct system_pinmux_config config;
- system_pinmux_get_config_defaults(&config);
- if (pin == PIN_PA30 || pin == PIN_PA31) {
- config.mux_position = 0x6;
+ if (pin == PIN_PA30
+ #ifdef SAMD51
+ || pin == PIN_PB30) {
+ gpio_set_pin_function(pin, GPIO_PIN_FUNCTION_H);
+ #endif
+ #ifdef SAMD21
+ || pin == PIN_PA31) {
+ gpio_set_pin_function(pin, GPIO_PIN_FUNCTION_G);
+ #endif
} else {
- config.powersave = true;
+ gpio_set_pin_direction(pin, GPIO_DIRECTION_OFF);
+ gpio_set_pin_function(pin, GPIO_PIN_FUNCTION_OFF);
}
- system_pinmux_pin_set_config(pin, &config);
#ifdef SPEAKER_ENABLE_PIN
if (pin == SPEAKER_ENABLE_PIN->pin) {
speaker_enable_in_use = false;
- struct port_config pin_conf;
- port_get_config_defaults(&pin_conf);
-
- pin_conf.direction = PORT_PIN_DIR_OUTPUT;
- port_pin_set_config(SPEAKER_ENABLE_PIN->pin, &pin_conf);
- port_pin_set_output_level(SPEAKER_ENABLE_PIN->pin, false);
+ gpio_set_pin_function(pin, GPIO_PIN_FUNCTION_OFF);
+ gpio_set_pin_direction(SPEAKER_ENABLE_PIN->pin, GPIO_DIRECTION_OUT);
+ gpio_set_pin_level(SPEAKER_ENABLE_PIN->pin, false);
}
#endif
}
@@ -169,8 +191,8 @@ bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t* pin) {
}
#endif
- PortGroup *const port = system_pinmux_get_group_from_gpio_pin(pin->pin);
- uint32_t pin_index = (pin->pin);
+ PortGroup *const port = &PORT->Group[(enum gpio_port)GPIO_PORT(pin->pin)];
+ uint8_t pin_index = GPIO_PIN(pin->pin);
volatile PORT_PINCFG_Type *state = &port->PINCFG[pin_index];
volatile PORT_PMUX_Type *pmux = &port->PMUX[pin_index / 2];
diff --git a/atmel-samd/common-hal/microcontroller/Pin.h b/atmel-samd/common-hal/microcontroller/Pin.h
index 31c083311..154ea1e47 100644
--- a/atmel-samd/common-hal/microcontroller/Pin.h
+++ b/atmel-samd/common-hal/microcontroller/Pin.h
@@ -27,17 +27,11 @@
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_MICROCONTROLLER_PIN_H
#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_MICROCONTROLLER_PIN_H
-// Don't reorder these includes because they are dependencies of adc_feature.h.
-// They should really be included by adc_feature.h.
-#include "compiler.h"
-#include "asf/sam0/drivers/system/clock/gclk.h"
-#include "asf/sam0/utils/cmsis/samd21/include/component/adc.h"
-#include "asf/sam0/drivers/adc/adc_sam_d_r/adc_feature.h" // for adc_positive_input
+#include "py/obj.h"
-#include "asf/sam0/drivers/tc/tc.h"
-#include "asf/sam0/drivers/tcc/tcc.h"
+#include "mpconfigport.h"
-#include "py/obj.h"
+#include "include/component/sercom.h"
typedef struct {
Sercom *const sercom;
@@ -49,24 +43,32 @@ typedef struct {
Tc *const tc;
Tcc *const tcc;
};
+ #ifdef SAMD21
bool is_tc:1;
- uint8_t channel:3;
+ #endif
uint8_t wave_output:4;
} pin_timer_t;
+#ifdef SAMD21
+ #define NUM_TIMERS_PER_PIN 2
+ #define NUM_ADC_PER_PIN 1
+#endif
+#ifdef SAMD51
+ #define NUM_TIMERS_PER_PIN 3
+ #define NUM_ADC_PER_PIN 2
+#endif
#define NUM_SERCOMS_PER_PIN 2
+
typedef struct {
mp_obj_base_t base;
qstr name;
uint8_t pin;
bool has_extint:1;
uint8_t extint_channel:7;
- bool has_adc:1;
- enum adc_positive_input adc_input:7;
bool has_touch:1;
uint8_t touch_y_line:7; // 0 - 15. Assumed to be Y channel.
- pin_timer_t primary_timer;
- pin_timer_t secondary_timer;
+ uint8_t adc_input[NUM_ADC_PER_PIN];
+ pin_timer_t timer[NUM_TIMERS_PER_PIN];
pin_sercom_t sercom[NUM_SERCOMS_PER_PIN];
} mcu_pin_obj_t;
diff --git a/atmel-samd/common-hal/microcontroller/Processor.c b/atmel-samd/common-hal/microcontroller/Processor.c
index c398c5e6f..a037c42db 100644
--- a/atmel-samd/common-hal/microcontroller/Processor.c
+++ b/atmel-samd/common-hal/microcontroller/Processor.c
@@ -63,175 +63,169 @@
#include "common-hal/microcontroller/Processor.h"
-// Don't reorder these includes because they are dependencies of adc_feature.h.
-// They should really be included by adc_feature.h.
-#include <compiler.h>
-#include "asf/sam0/drivers/system/clock/gclk.h"
-#include "asf/sam0/utils/cmsis/samd21/include/component/adc.h"
-#include "asf/sam0/drivers/adc/adc_sam_d_r/adc_feature.h"
-#include "asf/sam0/drivers/adc/adc.h"
-
-#define ADC_TEMP_SAMPLE_LENGTH 4
-#define INT1V_VALUE_FLOAT 1.0
-#define INT1V_DIVIDER_1000 1000.0
-#define ADC_12BIT_FULL_SCALE_VALUE_FLOAT 4095.0
-
-typedef struct nvm_calibration_data_t {
- float tempR; // Production Room temperature
- float tempH; // Production Hot temperature
- float INT1VR; // Room temp 2's complement of the internal 1V reference value
- float INT1VH; // Hot temp 2's complement of the internal 1V reference value
- uint16_t ADCR; // Production Room temperature ADC value
- uint16_t ADCH; // Production Hot temperature ADC value
- float VADCR; // Room temperature ADC voltage
- float VADCH; // Hot temperature ADC voltage
-} nvm_calibration_data_t;
+// #define ADC_TEMP_SAMPLE_LENGTH 4
+// #define INT1V_VALUE_FLOAT 1.0
+// #define INT1V_DIVIDER_1000 1000.0
+// #define ADC_12BIT_FULL_SCALE_VALUE_FLOAT 4095.0
+//
+// typedef struct nvm_calibration_data_t {
+// float tempR; // Production Room temperature
+// float tempH; // Production Hot temperature
+// float INT1VR; // Room temp 2's complement of the internal 1V reference value
+// float INT1VH; // Hot temp 2's complement of the internal 1V reference value
+// uint16_t ADCR; // Production Room temperature ADC value
+// uint16_t ADCH; // Production Hot temperature ADC value
+// float VADCR; // Room temperature ADC voltage
+// float VADCH; // Hot temperature ADC voltage
+// } nvm_calibration_data_t;
// Decimal to fraction conversion. (adapted from ASF sample).
-STATIC float convert_dec_to_frac(uint8_t val) {
- float float_val = (float)val;
- if (val < 10) {
- return (float_val/10.0);
- } else if (val < 100) {
- return (float_val/100.0);
- } else {
- return (float_val/1000.0);
- }
-}
-
-STATIC void configure_adc_temp(struct adc_module *adc_instance) {
- struct adc_config config_adc;
- adc_get_config_defaults(&config_adc);
-
- // The parameters chosen here are from the temperature example in:
- // http://www.atmel.com/images/Atmel-42645-ADC-Configurations-with-Examples_ApplicationNote_AT11481.pdf
- // That note also recommends in general:
- // "Discard the first conversion result whenever there is a change
- // in ADC configuration like voltage reference / ADC channel change."
-
- config_adc.clock_prescaler = ADC_CLOCK_PRESCALER_DIV16;
- config_adc.reference = ADC_REFERENCE_INT1V;
- config_adc.positive_input = ADC_POSITIVE_INPUT_TEMP;
- config_adc.negative_input = ADC_NEGATIVE_INPUT_GND;
- config_adc.sample_length = ADC_TEMP_SAMPLE_LENGTH;
-
- adc_init(adc_instance, ADC, &config_adc);
-
- // Oversample and decimate. A higher samplenum produces a more stable result.
- ADC->AVGCTRL.reg = ADC_AVGCTRL_ADJRES(2) | ADC_AVGCTRL_SAMPLENUM_4;
- //ADC->AVGCTRL.reg = ADC_AVGCTRL_ADJRES(4) | ADC_AVGCTRL_SAMPLENUM_16;
-}
+// STATIC float convert_dec_to_frac(uint8_t val) {
+// float float_val = (float)val;
+// if (val < 10) {
+// return (float_val/10.0);
+// } else if (val < 100) {
+// return (float_val/100.0);
+// } else {
+// return (float_val/1000.0);
+// }
+// }
+
+// STATIC void configure_adc_temp(struct adc_module *adc_instance) {
+// struct adc_config config_adc;
+// adc_get_config_defaults(&config_adc);
+//
+// // The parameters chosen here are from the temperature example in:
+// // http://www.atmel.com/images/Atmel-42645-ADC-Configurations-with-Examples_ApplicationNote_AT11481.pdf
+// // That note also recommends in general:
+// // "Discard the first conversion result whenever there is a change
+// // in ADC configuration like voltage reference / ADC channel change."
+//
+// config_adc.clock_prescaler = ADC_CLOCK_PRESCALER_DIV16;
+// config_adc.reference = ADC_REFERENCE_INT1V;
+// config_adc.positive_input = ADC_POSITIVE_INPUT_TEMP;
+// config_adc.negative_input = ADC_NEGATIVE_INPUT_GND;
+// config_adc.sample_length = ADC_TEMP_SAMPLE_LENGTH;
+//
+// adc_init(adc_instance, ADC, &config_adc);
+//
+// // Oversample and decimate. A higher samplenum produces a more stable result.
+// ADC->AVGCTRL.reg = ADC_AVGCTRL_ADJRES(2) | ADC_AVGCTRL_SAMPLENUM_4;
+// //ADC->AVGCTRL.reg = ADC_AVGCTRL_ADJRES(4) | ADC_AVGCTRL_SAMPLENUM_16;
+// }
// Extract the production calibration data information from NVM (adapted from ASF sample).
//
-STATIC void load_calibration_data(nvm_calibration_data_t *cal) {
- volatile uint32_t val1; /* Temperature Log Row Content first 32 bits */
- volatile uint32_t val2; /* Temperature Log Row Content another 32 bits */
- uint8_t room_temp_val_int; /* Integer part of room temperature in °C */
- uint8_t room_temp_val_dec; /* Decimal part of room temperature in °C */
- uint8_t hot_temp_val_int; /* Integer part of hot temperature in °C */
- uint8_t hot_temp_val_dec; /* Decimal part of hot temperature in °C */
- int8_t room_int1v_val; /* internal 1V reference drift at room temperature */
- int8_t hot_int1v_val; /* internal 1V reference drift at hot temperature*/
-
- uint32_t *temp_log_row_ptr = (uint32_t *)NVMCTRL_TEMP_LOG;
-
- val1 = *temp_log_row_ptr;
- temp_log_row_ptr++;
- val2 = *temp_log_row_ptr;
-
- room_temp_val_int = (uint8_t)((val1 & NVMCTRL_FUSES_ROOM_TEMP_VAL_INT_Msk) >> NVMCTRL_FUSES_ROOM_TEMP_VAL_INT_Pos);
- room_temp_val_dec = (uint8_t)((val1 & NVMCTRL_FUSES_ROOM_TEMP_VAL_DEC_Msk) >> NVMCTRL_FUSES_ROOM_TEMP_VAL_DEC_Pos);
-
- hot_temp_val_int = (uint8_t)((val1 & NVMCTRL_FUSES_HOT_TEMP_VAL_INT_Msk) >> NVMCTRL_FUSES_HOT_TEMP_VAL_INT_Pos);
- hot_temp_val_dec = (uint8_t)((val1 & NVMCTRL_FUSES_HOT_TEMP_VAL_DEC_Msk) >> NVMCTRL_FUSES_HOT_TEMP_VAL_DEC_Pos);
-
- room_int1v_val = (int8_t)((val1 & NVMCTRL_FUSES_ROOM_INT1V_VAL_Msk) >> NVMCTRL_FUSES_ROOM_INT1V_VAL_Pos);
- hot_int1v_val = (int8_t)((val2 & NVMCTRL_FUSES_HOT_INT1V_VAL_Msk) >> NVMCTRL_FUSES_HOT_INT1V_VAL_Pos);
-
- cal->ADCR = (uint16_t)((val2 & NVMCTRL_FUSES_ROOM_ADC_VAL_Msk) >> NVMCTRL_FUSES_ROOM_ADC_VAL_Pos);
-
- cal->ADCH = (uint16_t)((val2 & NVMCTRL_FUSES_HOT_ADC_VAL_Msk) >> NVMCTRL_FUSES_HOT_ADC_VAL_Pos);
-
- cal->tempR = room_temp_val_int + convert_dec_to_frac(room_temp_val_dec);
- cal->tempH = hot_temp_val_int + convert_dec_to_frac(hot_temp_val_dec);
-
- cal->INT1VR = 1 - ((float)room_int1v_val/INT1V_DIVIDER_1000);
- cal->INT1VH = 1 - ((float)hot_int1v_val/INT1V_DIVIDER_1000);
-
- cal->VADCR = ((float)cal->ADCR * cal->INT1VR)/ADC_12BIT_FULL_SCALE_VALUE_FLOAT;
- cal->VADCH = ((float)cal->ADCH * cal->INT1VH)/ADC_12BIT_FULL_SCALE_VALUE_FLOAT;
-}
+// STATIC void load_calibration_data(nvm_calibration_data_t *cal) {
+// volatile uint32_t val1; /* Temperature Log Row Content first 32 bits */
+// volatile uint32_t val2; /* Temperature Log Row Content another 32 bits */
+// uint8_t room_temp_val_int; /* Integer part of room temperature in °C */
+// uint8_t room_temp_val_dec; /* Decimal part of room temperature in °C */
+// uint8_t hot_temp_val_int; /* Integer part of hot temperature in °C */
+// uint8_t hot_temp_val_dec; /* Decimal part of hot temperature in °C */
+// int8_t room_int1v_val; /* internal 1V reference drift at room temperature */
+// int8_t hot_int1v_val; /* internal 1V reference drift at hot temperature*/
+//
+// uint32_t *temp_log_row_ptr = (uint32_t *)NVMCTRL_TEMP_LOG;
+//
+// val1 = *temp_log_row_ptr;
+// temp_log_row_ptr++;
+// val2 = *temp_log_row_ptr;
+//
+// room_temp_val_int = (uint8_t)((val1 & NVMCTRL_FUSES_ROOM_TEMP_VAL_INT_Msk) >> NVMCTRL_FUSES_ROOM_TEMP_VAL_INT_Pos);
+// room_temp_val_dec = (uint8_t)((val1 & NVMCTRL_FUSES_ROOM_TEMP_VAL_DEC_Msk) >> NVMCTRL_FUSES_ROOM_TEMP_VAL_DEC_Pos);
+//
+// hot_temp_val_int = (uint8_t)((val1 & NVMCTRL_FUSES_HOT_TEMP_VAL_INT_Msk) >> NVMCTRL_FUSES_HOT_TEMP_VAL_INT_Pos);
+// hot_temp_val_dec = (uint8_t)((val1 & NVMCTRL_FUSES_HOT_TEMP_VAL_DEC_Msk) >> NVMCTRL_FUSES_HOT_TEMP_VAL_DEC_Pos);
+//
+// room_int1v_val = (int8_t)((val1 & NVMCTRL_FUSES_ROOM_INT1V_VAL_Msk) >> NVMCTRL_FUSES_ROOM_INT1V_VAL_Pos);
+// hot_int1v_val = (int8_t)((val2 & NVMCTRL_FUSES_HOT_INT1V_VAL_Msk) >> NVMCTRL_FUSES_HOT_INT1V_VAL_Pos);
+//
+// cal->ADCR = (uint16_t)((val2 & NVMCTRL_FUSES_ROOM_ADC_VAL_Msk) >> NVMCTRL_FUSES_ROOM_ADC_VAL_Pos);
+//
+// cal->ADCH = (uint16_t)((val2 & NVMCTRL_FUSES_HOT_ADC_VAL_Msk) >> NVMCTRL_FUSES_HOT_ADC_VAL_Pos);
+//
+// cal->tempR = room_temp_val_int + convert_dec_to_frac(room_temp_val_dec);
+// cal->tempH = hot_temp_val_int + convert_dec_to_frac(hot_temp_val_dec);
+//
+// cal->INT1VR = 1 - ((float)room_int1v_val/INT1V_DIVIDER_1000);
+// cal->INT1VH = 1 - ((float)hot_int1v_val/INT1V_DIVIDER_1000);
+//
+// cal->VADCR = ((float)cal->ADCR * cal->INT1VR)/ADC_12BIT_FULL_SCALE_VALUE_FLOAT;
+// cal->VADCH = ((float)cal->ADCH * cal->INT1VH)/ADC_12BIT_FULL_SCALE_VALUE_FLOAT;
+// }
/*
* Calculate fine temperature using Equation1 and Equation
* 1b as mentioned in data sheet section "Temperature Sensor Characteristics"
* of Electrical Characteristics. (adapted from ASF sample code).
*/
-STATIC float calculate_temperature(uint16_t raw_code, nvm_calibration_data_t *cal)
-{
- float VADC; /* Voltage calculation using ADC result for Coarse Temp calculation */
- float VADCM; /* Voltage calculation using ADC result for Fine Temp calculation. */
- float INT1VM; /* Voltage calculation for reality INT1V value during the ADC conversion */
-
- VADC = ((float)raw_code * INT1V_VALUE_FLOAT)/ADC_12BIT_FULL_SCALE_VALUE_FLOAT;
-
- // Hopefully compiler will remove common subepxressions here.
-
- /* Coarse Temp Calculation by assume INT1V=1V for this ADC conversion */
- float coarse_temp = cal->tempR + (((cal->tempH - cal->tempR)/(cal->VADCH - cal->VADCR)) * (VADC - cal->VADCR));
-
- /* Calculation to find the real INT1V value during the ADC conversion */
- INT1VM = cal->INT1VR + (((cal->INT1VH - cal->INT1VR) * (coarse_temp - cal->tempR))/(cal->tempH - cal->tempR));
-
- VADCM = ((float)raw_code * INT1VM)/ADC_12BIT_FULL_SCALE_VALUE_FLOAT;
-
- /* Fine Temp Calculation by replace INT1V=1V by INT1V = INT1Vm for ADC conversion */
- float fine_temp = cal->tempR + (((cal->tempH - cal->tempR)/(cal->VADCH - cal->VADCR)) * (VADCM - cal->VADCR));
-
- return fine_temp;
-}
+// STATIC float calculate_temperature(uint16_t raw_code, nvm_calibration_data_t *cal)
+// {
+// float VADC; /* Voltage calculation using ADC result for Coarse Temp calculation */
+// float VADCM; /* Voltage calculation using ADC result for Fine Temp calculation. */
+// float INT1VM; /* Voltage calculation for reality INT1V value during the ADC conversion */
+//
+// VADC = ((float)raw_code * INT1V_VALUE_FLOAT)/ADC_12BIT_FULL_SCALE_VALUE_FLOAT;
+//
+// // Hopefully compiler will remove common subepxressions here.
+//
+// /* Coarse Temp Calculation by assume INT1V=1V for this ADC conversion */
+// float coarse_temp = cal->tempR + (((cal->tempH - cal->tempR)/(cal->VADCH - cal->VADCR)) * (VADC - cal->VADCR));
+//
+// /* Calculation to find the real INT1V value during the ADC conversion */
+// INT1VM = cal->INT1VR + (((cal->INT1VH - cal->INT1VR) * (coarse_temp - cal->tempR))/(cal->tempH - cal->tempR));
+//
+// VADCM = ((float)raw_code * INT1VM)/ADC_12BIT_FULL_SCALE_VALUE_FLOAT;
+//
+// /* Fine Temp Calculation by replace INT1V=1V by INT1V = INT1Vm for ADC conversion */
+// float fine_temp = cal->tempR + (((cal->tempH - cal->tempR)/(cal->VADCH - cal->VADCR)) * (VADCM - cal->VADCR));
+//
+// return fine_temp;
+// }
// External interface.
//
float common_hal_mcu_processor_get_temperature(void) {
- struct adc_module adc_instance_struct;
-
- system_voltage_reference_enable(SYSTEM_VOLTAGE_REFERENCE_TEMPSENSE);
- configure_adc_temp(&adc_instance_struct);
- nvm_calibration_data_t nvm_calibration_data;
- load_calibration_data(&nvm_calibration_data);
-
- adc_enable(&adc_instance_struct);
-
- uint16_t data;
- enum status_code status;
-
- // Read twice and discard first result, as recommended in section 14 of
- // http://www.atmel.com/images/Atmel-42645-ADC-Configurations-with-Examples_ApplicationNote_AT11481.pdf
- // "Discard the first conversion result whenever there is a change in ADC configuration
- // like voltage reference / ADC channel change"
- // Empirical observation shows the first reading is quite different than subsequent ones.
-
- adc_start_conversion(&adc_instance_struct);
- do {
- status = adc_read(&adc_instance_struct, &data);
- } while (status == STATUS_BUSY);
-
- adc_start_conversion(&adc_instance_struct);
- do {
- status = adc_read(&adc_instance_struct, &data);
- } while (status == STATUS_BUSY);
-
- // Disable so that someone else can use the adc with different settings.
- adc_disable(&adc_instance_struct);
- return calculate_temperature(data, &nvm_calibration_data);
+ // struct adc_module adc_instance_struct;
+ //
+ // system_voltage_reference_enable(SYSTEM_VOLTAGE_REFERENCE_TEMPSENSE);
+ // configure_adc_temp(&adc_instance_struct);
+ // nvm_calibration_data_t nvm_calibration_data;
+ // load_calibration_data(&nvm_calibration_data);
+ //
+ // adc_enable(&adc_instance_struct);
+ //
+ // uint16_t data;
+ // enum status_code status;
+ //
+ // // Read twice and discard first result, as recommended in section 14 of
+ // // http://www.atmel.com/images/Atmel-42645-ADC-Configurations-with-Examples_ApplicationNote_AT11481.pdf
+ // // "Discard the first conversion result whenever there is a change in ADC configuration
+ // // like voltage reference / ADC channel change"
+ // // Empirical observation shows the first reading is quite different than subsequent ones.
+ //
+ // adc_start_conversion(&adc_instance_struct);
+ // do {
+ // status = adc_read(&adc_instance_struct, &data);
+ // } while (status == STATUS_BUSY);
+ //
+ // adc_start_conversion(&adc_instance_struct);
+ // do {
+ // status = adc_read(&adc_instance_struct, &data);
+ // } while (status == STATUS_BUSY);
+ //
+ // // Disable so that someone else can use the adc with different settings.
+ // adc_disable(&adc_instance_struct);
+ // return calculate_temperature(data, &nvm_calibration_data);
+ return 0;
}
uint32_t common_hal_mcu_processor_get_frequency(void) {
- return system_cpu_clock_get_hz();
+ return 0;
+ //return system_cpu_clock_get_hz();
}
diff --git a/atmel-samd/common-hal/microcontroller/__init__.c b/atmel-samd/common-hal/microcontroller/__init__.c
index 84cea008b..5c119c439 100644
--- a/atmel-samd/common-hal/microcontroller/__init__.c
+++ b/atmel-samd/common-hal/microcontroller/__init__.c
@@ -26,6 +26,7 @@
#include "py/mphal.h"
#include "py/obj.h"
+#include "hal/include/hal_atomic.h"
#include "samd21_pins.h"
@@ -38,17 +39,13 @@ void common_hal_mcu_delay_us(uint32_t delay) {
// Interrupt flags that will be saved and restored during disable/Enable
// interrupt functions below.
-static irqflags_t irq_flags;
+volatile hal_atomic_t flags;
void common_hal_mcu_disable_interrupts(void) {
- // Disable all interrupt sources for timing critical sections.
- // Disable ASF-based interrupts.
- irq_flags = cpu_irq_save();
+ atomic_enter_critical(&flags);
}
void common_hal_mcu_enable_interrupts(void) {
- // Enable all interrupt sources after timing critical sections.
- // Restore ASF-based interrupts.
- cpu_irq_restore(irq_flags);
+ atomic_leave_critical(&flags);
}
// The singleton microcontroller.Processor object, bound to microcontroller.cpu
@@ -62,13 +59,13 @@ mcu_processor_obj_t common_hal_mcu_processor_obj = {
// NVM is only available on Express boards for now.
#if CIRCUITPY_INTERNAL_NVM_SIZE > 0
// The singleton nvm.ByteArray object.
-nvm_bytearray_obj_t common_hal_mcu_nvm_obj = {
- .base = {
- .type = &nvm_bytearray_type,
- },
- .len = NVMCTRL_ROW_SIZE,
- .start_address = (uint8_t*) (FLASH_SIZE - NVMCTRL_ROW_SIZE)
-};
+// nvm_bytearray_obj_t common_hal_mcu_nvm_obj = {
+// .base = {
+// .type = &nvm_bytearray_type,
+// },
+// .len = NVMCTRL_ROW_SIZE,
+// .start_address = (uint8_t*) (FLASH_SIZE - NVMCTRL_ROW_SIZE)
+// };
#endif
// This maps MCU pin names to pin objects.