diff --git a/src/atsamd/adc.c b/src/atsamd/adc.c index 59b12163..1322a46c 100644 --- a/src/atsamd/adc.c +++ b/src/atsamd/adc.c @@ -70,12 +70,9 @@ adc_init(void) // Enable adc clock enable_pclock(ADC_GCLK_ID, ID_ADC); // Load calibraiton info - uint32_t v = *((uint32_t*)ADC_FUSES_BIASCAL_ADDR); - uint32_t bias = (v & ADC_FUSES_BIASCAL_Msk) >> ADC_FUSES_BIASCAL_Pos; - v = *((uint32_t*)ADC_FUSES_LINEARITY_0_ADDR); - uint32_t li0 = (v & ADC_FUSES_LINEARITY_0_Msk) >> ADC_FUSES_LINEARITY_0_Pos; - v = *((uint32_t*)ADC_FUSES_LINEARITY_1_ADDR); - uint32_t li5 = (v & ADC_FUSES_LINEARITY_1_Msk) >> ADC_FUSES_LINEARITY_1_Pos; + uint32_t bias = GET_FUSE(ADC_FUSES_BIASCAL); + uint32_t li0 = GET_FUSE(ADC_FUSES_LINEARITY_0); + uint32_t li5 = GET_FUSE(ADC_FUSES_LINEARITY_1); uint32_t lin = li0 | (li5 << 5); ADC->CALIB.reg = ADC_CALIB_BIAS_CAL(bias) | ADC_CALIB_LINEARITY_CAL(lin); @@ -92,22 +89,18 @@ adc_init(void) // Load calibration info // ADC0 - uint32_t v = *((uint32_t*)ADC0_FUSES_BIASREFBUF_ADDR); - uint32_t refbuf = (v & ADC0_FUSES_BIASREFBUF_Msk) >> ADC0_FUSES_BIASREFBUF_Pos; - v = *((uint32_t*)ADC0_FUSES_BIASR2R_ADDR); - uint32_t r2r = (v & ADC0_FUSES_BIASR2R_Msk) >> ADC0_FUSES_BIASR2R_Pos; - v = *((uint32_t*)ADC0_FUSES_BIASCOMP_ADDR); - uint32_t comp = (v & ADC0_FUSES_BIASCOMP_Msk) >> ADC0_FUSES_BIASCOMP_Pos; - ADC0->CALIB.reg = ADC0_FUSES_BIASREFBUF(refbuf) | ADC0_FUSES_BIASR2R(r2r) | ADC0_FUSES_BIASCOMP(comp); + uint32_t refbuf = GET_FUSE(ADC0_FUSES_BIASREFBUF); + uint32_t r2r = GET_FUSE(ADC0_FUSES_BIASR2R); + uint32_t comp = GET_FUSE(ADC0_FUSES_BIASCOMP); + ADC0->CALIB.reg = (ADC0_FUSES_BIASREFBUF(refbuf) + | ADC0_FUSES_BIASR2R(r2r) | ADC0_FUSES_BIASCOMP(comp)); // ADC1 - v = *((uint32_t*)ADC1_FUSES_BIASREFBUF_ADDR); - refbuf = (v & ADC1_FUSES_BIASREFBUF_Msk) >> ADC1_FUSES_BIASREFBUF_Pos; - v = *((uint32_t*)ADC1_FUSES_BIASR2R_ADDR); - r2r = (v & ADC1_FUSES_BIASR2R_Msk) >> ADC1_FUSES_BIASR2R_Pos; - v = *((uint32_t*)ADC1_FUSES_BIASCOMP_ADDR); - comp = (v & ADC1_FUSES_BIASCOMP_Msk) >> ADC1_FUSES_BIASCOMP_Pos; - ADC1->CALIB.reg = ADC1_FUSES_BIASREFBUF(refbuf) | ADC1_FUSES_BIASR2R(r2r) | ADC1_FUSES_BIASCOMP(comp); + refbuf = GET_FUSE(ADC1_FUSES_BIASREFBUF); + r2r = GET_FUSE(ADC1_FUSES_BIASR2R); + comp = GET_FUSE(ADC1_FUSES_BIASCOMP); + ADC1->CALIB.reg = (ADC0_FUSES_BIASREFBUF(refbuf) + | ADC0_FUSES_BIASR2R(r2r) | ADC0_FUSES_BIASCOMP(comp)); // Setup and enable // ADC0 diff --git a/src/atsamd/internal.h b/src/atsamd/internal.h index ca748492..45d478b0 100644 --- a/src/atsamd/internal.h +++ b/src/atsamd/internal.h @@ -15,6 +15,9 @@ #define GPIO2PORT(PIN) ((PIN) / 32) #define GPIO2BIT(PIN) (1<<((PIN) % 32)) +#define GET_FUSE(REG) \ + ((*((uint32_t*)(REG##_ADDR)) & (REG##_Msk)) >> (REG##_Pos)) + void enable_pclock(uint32_t pclk_id, uint32_t pm_id); uint32_t get_pclock_frequency(uint32_t pclk_id); void gpio_peripheral(uint32_t gpio, char ptype, int32_t pull_up); diff --git a/src/atsamd/usbserial.c b/src/atsamd/usbserial.c index 3ae73d9b..6b7e46b6 100644 --- a/src/atsamd/usbserial.c +++ b/src/atsamd/usbserial.c @@ -193,12 +193,9 @@ usbserial_init(void) uint32_t ptype = CONFIG_MACH_SAMD21 ? 'G' : 'H'; gpio_peripheral(GPIO('A', 24), ptype, 0); gpio_peripheral(GPIO('A', 25), ptype, 0); - uint16_t trim = (readl((void*)USB_FUSES_TRIM_ADDR) - & USB_FUSES_TRIM_Msk) >> USB_FUSES_TRIM_Pos; - uint16_t transp = (readl((void*)USB_FUSES_TRANSP_ADDR) - & USB_FUSES_TRANSP_Msk) >> USB_FUSES_TRANSP_Pos; - uint16_t transn = (readl((void*)USB_FUSES_TRANSN_ADDR) - & USB_FUSES_TRANSN_Msk) >> USB_FUSES_TRANSN_Pos; + uint32_t trim = GET_FUSE(USB_FUSES_TRIM); + uint32_t transp = GET_FUSE(USB_FUSES_TRANSP); + uint32_t transn = GET_FUSE(USB_FUSES_TRANSN); USB->DEVICE.PADCAL.reg = (USB_PADCAL_TRIM(trim) | USB_PADCAL_TRANSP(transp) | USB_PADCAL_TRANSN(transn)); // Enable USB in device mode