atsamd: Add a GET_FUSE() helper macro

Add a helper macro for accessing the non-volatile factory-calibrated
"fuse" values.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2019-02-22 07:31:10 -05:00
parent 5bc47d9416
commit 37797c8128
3 changed files with 19 additions and 26 deletions

View File

@ -70,12 +70,9 @@ adc_init(void)
// Enable adc clock // Enable adc clock
enable_pclock(ADC_GCLK_ID, ID_ADC); enable_pclock(ADC_GCLK_ID, ID_ADC);
// Load calibraiton info // Load calibraiton info
uint32_t v = *((uint32_t*)ADC_FUSES_BIASCAL_ADDR); uint32_t bias = GET_FUSE(ADC_FUSES_BIASCAL);
uint32_t bias = (v & ADC_FUSES_BIASCAL_Msk) >> ADC_FUSES_BIASCAL_Pos; uint32_t li0 = GET_FUSE(ADC_FUSES_LINEARITY_0);
v = *((uint32_t*)ADC_FUSES_LINEARITY_0_ADDR); uint32_t li5 = GET_FUSE(ADC_FUSES_LINEARITY_1);
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 lin = li0 | (li5 << 5); uint32_t lin = li0 | (li5 << 5);
ADC->CALIB.reg = ADC_CALIB_BIAS_CAL(bias) | ADC_CALIB_LINEARITY_CAL(lin); ADC->CALIB.reg = ADC_CALIB_BIAS_CAL(bias) | ADC_CALIB_LINEARITY_CAL(lin);
@ -92,22 +89,18 @@ adc_init(void)
// Load calibration info // Load calibration info
// ADC0 // ADC0
uint32_t v = *((uint32_t*)ADC0_FUSES_BIASREFBUF_ADDR); uint32_t refbuf = GET_FUSE(ADC0_FUSES_BIASREFBUF);
uint32_t refbuf = (v & ADC0_FUSES_BIASREFBUF_Msk) >> ADC0_FUSES_BIASREFBUF_Pos; uint32_t r2r = GET_FUSE(ADC0_FUSES_BIASR2R);
v = *((uint32_t*)ADC0_FUSES_BIASR2R_ADDR); uint32_t comp = GET_FUSE(ADC0_FUSES_BIASCOMP);
uint32_t r2r = (v & ADC0_FUSES_BIASR2R_Msk) >> ADC0_FUSES_BIASR2R_Pos; ADC0->CALIB.reg = (ADC0_FUSES_BIASREFBUF(refbuf)
v = *((uint32_t*)ADC0_FUSES_BIASCOMP_ADDR); | ADC0_FUSES_BIASR2R(r2r) | ADC0_FUSES_BIASCOMP(comp));
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);
// ADC1 // ADC1
v = *((uint32_t*)ADC1_FUSES_BIASREFBUF_ADDR); refbuf = GET_FUSE(ADC1_FUSES_BIASREFBUF);
refbuf = (v & ADC1_FUSES_BIASREFBUF_Msk) >> ADC1_FUSES_BIASREFBUF_Pos; r2r = GET_FUSE(ADC1_FUSES_BIASR2R);
v = *((uint32_t*)ADC1_FUSES_BIASR2R_ADDR); comp = GET_FUSE(ADC1_FUSES_BIASCOMP);
r2r = (v & ADC1_FUSES_BIASR2R_Msk) >> ADC1_FUSES_BIASR2R_Pos; ADC1->CALIB.reg = (ADC0_FUSES_BIASREFBUF(refbuf)
v = *((uint32_t*)ADC1_FUSES_BIASCOMP_ADDR); | ADC0_FUSES_BIASR2R(r2r) | ADC0_FUSES_BIASCOMP(comp));
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);
// Setup and enable // Setup and enable
// ADC0 // ADC0

View File

@ -15,6 +15,9 @@
#define GPIO2PORT(PIN) ((PIN) / 32) #define GPIO2PORT(PIN) ((PIN) / 32)
#define GPIO2BIT(PIN) (1<<((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); void enable_pclock(uint32_t pclk_id, uint32_t pm_id);
uint32_t get_pclock_frequency(uint32_t pclk_id); uint32_t get_pclock_frequency(uint32_t pclk_id);
void gpio_peripheral(uint32_t gpio, char ptype, int32_t pull_up); void gpio_peripheral(uint32_t gpio, char ptype, int32_t pull_up);

View File

@ -193,12 +193,9 @@ usbserial_init(void)
uint32_t ptype = CONFIG_MACH_SAMD21 ? 'G' : 'H'; uint32_t ptype = CONFIG_MACH_SAMD21 ? 'G' : 'H';
gpio_peripheral(GPIO('A', 24), ptype, 0); gpio_peripheral(GPIO('A', 24), ptype, 0);
gpio_peripheral(GPIO('A', 25), ptype, 0); gpio_peripheral(GPIO('A', 25), ptype, 0);
uint16_t trim = (readl((void*)USB_FUSES_TRIM_ADDR) uint32_t trim = GET_FUSE(USB_FUSES_TRIM);
& USB_FUSES_TRIM_Msk) >> USB_FUSES_TRIM_Pos; uint32_t transp = GET_FUSE(USB_FUSES_TRANSP);
uint16_t transp = (readl((void*)USB_FUSES_TRANSP_ADDR) uint32_t transn = GET_FUSE(USB_FUSES_TRANSN);
& 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;
USB->DEVICE.PADCAL.reg = (USB_PADCAL_TRIM(trim) | USB_PADCAL_TRANSP(transp) USB->DEVICE.PADCAL.reg = (USB_PADCAL_TRIM(trim) | USB_PADCAL_TRANSP(transp)
| USB_PADCAL_TRANSN(transn)); | USB_PADCAL_TRANSN(transn));
// Enable USB in device mode // Enable USB in device mode