SmartSnippets DA1459x SDK
hw_gpadc.h
Go to the documentation of this file.
1 
41 #ifndef HW_GPADC_H
42 #define HW_GPADC_H
43 
44 
45 #if dg_configUSE_HW_GPADC
46 
47 #include <stdbool.h>
48 #include <stdint.h>
49 #include "sdk_defs.h"
50 
51 /***************************************************************************
52  ********* Macro, type and data-structure definitions ***************
53  ***************************************************************************/
54 
63 typedef void * HW_GPADC_ID;
64 
69 typedef void (*hw_gpadc_interrupt_cb)(void);
70 
75 typedef void (*hw_gpadc_read_cb)(void *user_data, uint32_t conv_to_go);
76 
81 #define HW_GPADC_RESULT_NOB (16U)
82 
87 #define HW_GPADC_DEFAULT_ENOB (10U)
88 
93 #define HW_GPADC_UNUSED_BITS (HW_GPADC_RESULT_NOB - HW_GPADC_DEFAULT_ENOB)
94 
100 #define HW_GPADC_MID_SCALE_ADC (1 << (HW_GPADC_RESULT_NOB - 1))
101 
107 #define HW_GPADC_OFFSET_RESET 0x200
108 
115 #define HW_GPADC_DMA_SUPPORT dg_configGPADC_DMA_SUPPORT
116 
121 typedef enum {
125 
133 typedef enum {
143 
144 #if HW_GPADC_DMA_SUPPORT
145 #include "hw_dma.h"
146 
157 typedef struct {
158  HW_DMA_CHANNEL channel;
159  HW_DMA_PRIO prio;
160  bool circular;
161  uint16 irq_nr_of_trans;
162 } gpadc_dma_cfg;
163 #endif /* HW_GPADC_DMA_SUPPORT */
164 
169 #include "hw_gpadc_v2.h"
170 
171 /***************************************************************************
172  **************** GP_ADC configuration functions *******************
173  ***************************************************************************/
174 
196 __STATIC_INLINE void hw_gpadc_set_continuous(bool enabled)
197 {
198  REG_SETF(GPADC, GP_ADC_CTRL_REG, GP_ADC_CONT, !!enabled);
199 }
200 
207 __STATIC_INLINE bool hw_gpadc_get_continuous(void)
208 {
209  return REG_GETF(GPADC, GP_ADC_CTRL_REG, GP_ADC_CONT);
210 }
211 
219 {
220  REG_SETF(GPADC, GP_ADC_CTRL_REG, GP_ADC_SE, mode);
221 }
222 
229 __STATIC_INLINE HW_GPADC_INPUT_MODE hw_gpadc_get_input_mode(void)
230 {
231  return (HW_GPADC_INPUT_MODE) REG_GETF(GPADC, GP_ADC_CTRL_REG, GP_ADC_SE);
232 }
233 
247 __STATIC_INLINE void hw_gpadc_set_oversampling(HW_GPADC_OVERSAMPLING n_samples)
248 {
249  REG_SETF(GPADC, GP_ADC_CTRL2_REG, GP_ADC_CONV_NRS, n_samples);
250 }
251 
261 {
262  return REG_GETF(GPADC, GP_ADC_CTRL2_REG, GP_ADC_CONV_NRS);
263 }
264 
276 __STATIC_INLINE void hw_gpadc_set_mute(bool enabled)
277 {
278  REG_SETF(GPADC, GP_ADC_CTRL_REG, GP_ADC_MUTE, !!enabled);
279 }
280 
289 __STATIC_INLINE bool hw_gpadc_get_mute(void)
290 {
291  return REG_GETF(GPADC, GP_ADC_CTRL_REG, GP_ADC_MUTE);
292 }
293 
302 __STATIC_INLINE void hw_gpadc_set_sign_change(bool enabled)
303 {
304  REG_SETF(GPADC, GP_ADC_CTRL_REG, GP_ADC_SIGN, !!enabled);
305 }
306 
313 __STATIC_INLINE bool hw_gpadc_get_sign_change(void)
314 {
315  return REG_GETF(GPADC, GP_ADC_CTRL_REG, GP_ADC_SIGN);
316 }
325 __STATIC_INLINE void hw_gpadc_set_chopping(bool enabled)
326 {
327  REG_SETF(GPADC, GP_ADC_CTRL_REG, GP_ADC_CHOP, !!enabled);
328 }
329 
336 __STATIC_INLINE bool hw_gpadc_get_chopping(void)
337 {
338  return REG_GETF(GPADC, GP_ADC_CTRL_REG, GP_ADC_CHOP);
339 }
340 
341 
348 __STATIC_INLINE void hw_gpadc_set_mint(bool enabled)
349 {
350  REG_SETF(GPADC, GP_ADC_CTRL_REG, GP_ADC_MINT, !!enabled);
351 }
352 
359 __STATIC_INLINE bool hw_gpadc_get_mint(void)
360 {
361  return REG_GETF(GPADC, GP_ADC_CTRL_REG, GP_ADC_MINT);
362 }
363 
372 __STATIC_INLINE void hw_gpadc_set_ldo_constant_current(bool enabled)
373 {
374  REG_SETF(GPADC, GP_ADC_CTRL2_REG, GP_ADC_I20U, !!enabled);
375 }
376 
385 __STATIC_INLINE bool hw_gpadc_get_ldo_constant_current(void)
386 {
387  return REG_GETF(GPADC, GP_ADC_CTRL2_REG, GP_ADC_I20U);
388 }
389 
400 __STATIC_INLINE void hw_gpadc_set_interval(uint8_t mult)
401 {
402  REG_SETF(GPADC, GP_ADC_CTRL3_REG, GP_ADC_INTERVAL, mult);
403 }
404 
413 __STATIC_INLINE uint8_t hw_gpadc_get_interval(void)
414 {
415  return REG_GETF(GPADC, GP_ADC_CTRL3_REG, GP_ADC_INTERVAL);
416 }
417 
424 __STATIC_INLINE void hw_gpadc_set_dma_functionality(bool enabled)
425 {
426  REG_SETF(GPADC, GP_ADC_CTRL_REG, GP_ADC_DMA_EN, !!enabled);
427 }
428 
435 __STATIC_INLINE bool hw_gpadc_get_dma_functionality(void)
436 {
437  return REG_GETF(GPADC, GP_ADC_CTRL_REG, GP_ADC_DMA_EN);
438 }
439 
444 /***************************************************************************
445  **************** GP_ADC calibration functions *******************
446  ***************************************************************************/
447 
469 __STATIC_INLINE void hw_gpadc_set_offset_positive(uint16_t offset)
470 {
471  GPADC->GP_ADC_OFFP_REG = offset & REG_MSK(GPADC, GP_ADC_OFFP_REG, GP_ADC_OFFP);
472 }
473 
482 __STATIC_INLINE uint16_t hw_gpadc_get_offset_positive(void)
483 {
484  return GPADC->GP_ADC_OFFP_REG & REG_MSK(GPADC, GP_ADC_OFFP_REG, GP_ADC_OFFP);
485 }
486 
500 __STATIC_INLINE void hw_gpadc_set_offset_negative(uint16_t offset)
501 {
502  GPADC->GP_ADC_OFFN_REG = offset & REG_MSK(GPADC, GP_ADC_OFFN_REG, GP_ADC_OFFN);
503 }
504 
505 
514 __STATIC_INLINE uint16_t hw_gpadc_get_offset_negative(void)
515 {
516  return GPADC->GP_ADC_OFFN_REG & REG_MSK(GPADC, GP_ADC_OFFN_REG, GP_ADC_OFFN);
517 }
518 
525 void hw_gpadc_store_se_gain_error(int16_t single);
526 
533 void hw_gpadc_store_diff_gain_error(int16_t diff);
534 
541 void hw_gpadc_store_se_offset_error(int16_t single);
542 
549 void hw_gpadc_store_diff_offset_error(int16_t diff);
550 
562 __STATIC_INLINE int16_t hw_gpadc_calculate_single_ended_gain_error(int16_t low, int16_t high)
563 {
564  return ((high - low + ((uint16_t)(high - low) >> 2 )) - UINT16_MAX);
565 }
566 
578 __STATIC_INLINE int16_t hw_gpadc_calculate_single_ended_offset_error(int16_t low, int16_t high)
579 {
580  return ((int16_t)((9 * low) - high) >> 3);
581 }
582 
594 __STATIC_INLINE int16_t hw_gpadc_calculate_differential_gain_error(int16_t low, int16_t high)
595 {
596  return (((high - low) + ((uint16_t)(high - low) >> 2)) - UINT16_MAX);
597 }
598 
610 __STATIC_INLINE int16_t hw_gpadc_calculate_differential_offset_error(int16_t low, int16_t high)
611 {
612  return (low + high) >> 1;
613 }
614 
622 
630 
640 void hw_gpadc_offset_calibrate(void);
641 
646 /***************************************************************************
647  **************** Basic functionality of the GPADC *******************
648  ***************************************************************************/
649 
667 void hw_gpadc_init(const hw_gpadc_config_t *cfg, bool enable);
668 
677 void hw_gpadc_configure(const hw_gpadc_config_t *cfg);
678 
691 
702 
712 
723 __STATIC_INLINE void hw_gpadc_clear_interrupt(void)
724 {
725  GPADC->GP_ADC_CLEAR_INT_REG = 1;
726 }
727 
737 __STATIC_INLINE void hw_gpadc_enable(void)
738 {
739  REG_SET_BIT(GPADC, GP_ADC_CTRL_REG, GP_ADC_EN);
740 }
741 
753 __STATIC_INLINE void hw_gpadc_disable(void)
754 {
755  REG_CLR_BIT(GPADC, GP_ADC_CTRL_REG, GP_ADC_EN);
756 }
757 
764 __STATIC_INLINE bool hw_gpadc_is_enabled(void)
765 {
766  return REG_GETF(GPADC, GP_ADC_CTRL_REG, GP_ADC_EN);
767 }
768 
777 __STATIC_INLINE void hw_gpadc_start(void)
778 {
779  REG_SETF(GPADC, GP_ADC_CTRL_REG, GP_ADC_START, 1);
780 }
781 
788 __STATIC_INLINE bool hw_gpadc_in_progress(void)
789 {
790  return REG_GETF(GPADC, GP_ADC_CTRL_REG, GP_ADC_START);
791 }
792 
802 __STATIC_INLINE uint16_t hw_gpadc_get_raw_value(void)
803 {
804  return GPADC->GP_ADC_RESULT_REG;
805 }
806 
820 uint16_t hw_gpadc_get_value(void);
821 
830 __STATIC_INLINE void hw_gpadc_adc_measure(void)
831 {
833  hw_gpadc_start();
834  while (hw_gpadc_in_progress());
835 }
836 
865 bool hw_gpadc_read(uint32_t nof_conv, uint16_t *out_buf, hw_gpadc_read_cb cb, void *user_data);
866 
876 void hw_gpadc_abort_read(void);
877 
887 uint16_t hw_gpadc_apply_correction(const hw_gpadc_config_t *cfg, uint16_t raw);
888 
902 int16_t hw_gpadc_convert_to_millivolt(const hw_gpadc_config_t *cfg, uint16_t raw);
903 
908 /***************************************************************************
909  ****************** TEMPERATURE SENSOR declarations *******************
910  ***************************************************************************/
911 
923 typedef struct {
924  int16_t temp;
925  uint16_t adc;
927 
932 #endif /* dg_configUSE_HW_GPADC */
933 
934 
935 #endif /* HW_GPADC_H */
936 
hw_gpadc_apply_correction
uint16_t hw_gpadc_apply_correction(const hw_gpadc_config_t *cfg, uint16_t raw)
Apply a fine trimming algorithm to the conversion result.
hw_dma.h
Definition of API for the DMA Low Level Driver.
hw_gpadc_get_input_mode
__STATIC_INLINE HW_GPADC_INPUT_MODE hw_gpadc_get_input_mode(void)
Get the current input mode.
Definition: hw_gpadc.h:228
hw_gpadc_clear_interrupt
__STATIC_INLINE void hw_gpadc_clear_interrupt(void)
Clear interrupt.
Definition: hw_gpadc.h:720
REG_SETF
#define REG_SETF(base, reg, field, new_val)
Set the value of a register field.
Definition: sdk_defs.h:738
hw_gpadc_read
bool hw_gpadc_read(uint32_t nof_conv, uint16_t *out_buf, hw_gpadc_read_cb cb, void *user_data)
Generic read function Follows.
REG_CLR_BIT
#define REG_CLR_BIT(base, reg, field)
Clear a bit of a register.
Definition: sdk_defs.h:781
HW_GPADC_INPUT_MODE_DIFFERENTIAL
Definition: hw_gpadc.h:121
HW_GPADC_OVERSAMPLING_32_SAMPLES
Definition: hw_gpadc.h:138
hw_gpadc_get_continuous
__STATIC_INLINE bool hw_gpadc_get_continuous(void)
Get continuous mode state.
Definition: hw_gpadc.h:206
HW_GPADC_OVERSAMPLING_64_SAMPLES
Definition: hw_gpadc.h:139
REG_SET_BIT
#define REG_SET_BIT(base, reg, field)
Set a bit of a register.
Definition: sdk_defs.h:766
hw_gpadc_get_interval
__STATIC_INLINE uint8_t hw_gpadc_get_interval(void)
Get the current interval between conversions in continuous mode.
Definition: hw_gpadc.h:412
hw_gpadc_configure
void hw_gpadc_configure(const hw_gpadc_config_t *cfg)
Configure ADC.
hw_gpadc_set_ldo_constant_current
__STATIC_INLINE void hw_gpadc_set_ldo_constant_current(bool enabled)
Set state of constant 20uA load current on ADC LDO output.
Definition: hw_gpadc.h:371
hw_gpadc_calibration_point_t
Calibration Data - (Temperature, 16-bit ADC value) pair.
Definition: hw_gpadc.h:919
ASSERT_ERROR
#define ASSERT_ERROR(a)
Assert as error macro.
Definition: sdk_defs.h:422
hw_gpadc_get_offset_negative
__STATIC_INLINE uint16_t hw_gpadc_get_offset_negative(void)
Get the current offset adjustment for negative ADC array.
Definition: hw_gpadc.h:512
hw_gpadc_set_mute
__STATIC_INLINE void hw_gpadc_set_mute(bool enabled)
Set input mute state.
Definition: hw_gpadc.h:275
hw_gpadc_get_oversampling
__STATIC_INLINE HW_GPADC_OVERSAMPLING hw_gpadc_get_oversampling(void)
Get the current oversampling.
Definition: hw_gpadc.h:259
hw_gpadc_calculate_single_ended_gain_error
__STATIC_INLINE int16_t hw_gpadc_calculate_single_ended_gain_error(int16_t low, int16_t high)
Calculate Single Ended ADC Gain Error from two points.
Definition: hw_gpadc.h:560
hw_gpadc_store_se_gain_error
void hw_gpadc_store_se_gain_error(int16_t single)
Store Single Ended ADC Gain Error.
sdk_defs.h
Central include header file with platform definitions.
hw_gpadc_set_sign_change
__STATIC_INLINE void hw_gpadc_set_sign_change(bool enabled)
Set input and output sign change.
Definition: hw_gpadc.h:301
HW_GPADC_INPUT_MODE
HW_GPADC_INPUT_MODE
ADC input mode.
Definition: hw_gpadc.h:120
hw_gpadc_get_raw_value
__STATIC_INLINE uint16_t hw_gpadc_get_raw_value(void)
Get raw ADC value.
Definition: hw_gpadc.h:799
hw_gpadc_get_single_ended_gain_error
int16_t hw_gpadc_get_single_ended_gain_error(void)
Get single ended ADC Gain Error.
hw_gpadc_start
__STATIC_INLINE void hw_gpadc_start(void)
Start conversion.
Definition: hw_gpadc.h:774
HW_GPADC_OVERSAMPLING_4_SAMPLES
Definition: hw_gpadc.h:135
hw_gpadc_interrupt_cb
void(* hw_gpadc_interrupt_cb)(void)
ADC interrupt handler.
Definition: hw_gpadc.h:68
hw_gpadc_abort_read
void hw_gpadc_abort_read(void)
Stop conversions.
hw_gpadc_v2.h
Definition of API for the GPADC Low Level Driver.
hw_gpadc_set_offset_positive
__STATIC_INLINE void hw_gpadc_set_offset_positive(uint16_t offset)
Set offset adjustment for positive ADC array. This register offers a coarse offset calibration,...
Definition: hw_gpadc.h:467
hw_gpadc_calculate_differential_gain_error
__STATIC_INLINE int16_t hw_gpadc_calculate_differential_gain_error(int16_t low, int16_t high)
Calculate Differential ADC Gain Error from two points.
Definition: hw_gpadc.h:592
hw_gpadc_set_continuous
__STATIC_INLINE void hw_gpadc_set_continuous(bool enabled)
Set continuous mode.
Definition: hw_gpadc.h:195
HW_DMA_CHANNEL
HW_DMA_CHANNEL
DMA channel number.
Definition: hw_dma.h:62
hw_gpadc_adc_measure
__STATIC_INLINE void hw_gpadc_adc_measure(void)
Start the ADC conversion engine, providing a measurement.
Definition: hw_gpadc.h:827
hw_gpadc_set_interval
__STATIC_INLINE void hw_gpadc_set_interval(uint8_t mult)
Set interval between conversions in continuous mode.
Definition: hw_gpadc.h:399
hw_gpadc_calculate_differential_offset_error
__STATIC_INLINE int16_t hw_gpadc_calculate_differential_offset_error(int16_t low, int16_t high)
Calculate Differential ADC Offset Error from two points.
Definition: hw_gpadc.h:608
hw_gpadc_unregister_interrupt_no_clear
void hw_gpadc_unregister_interrupt_no_clear(void)
Unregister interrupt handler without clearing the ADC engine interrupt. Only the NVIC interrupt is di...
hw_gpadc_set_input_mode
__STATIC_INLINE void hw_gpadc_set_input_mode(HW_GPADC_INPUT_MODE mode)
Set input mode.
Definition: hw_gpadc.h:217
HW_GPADC_OVERSAMPLING_16_SAMPLES
Definition: hw_gpadc.h:137
hw_gpadc_get_offset_positive
__STATIC_INLINE uint16_t hw_gpadc_get_offset_positive(void)
Get the current offset adjustment for positive ADC array.
Definition: hw_gpadc.h:480
hw_gpadc_offset_calibrate
void hw_gpadc_offset_calibrate(void)
Perform coarse ADC offset calibration.
hw_gpadc_get_chopping
__STATIC_INLINE bool hw_gpadc_get_chopping(void)
Get the current chopping state.
Definition: hw_gpadc.h:335
hw_gpadc_set_dma_functionality
__STATIC_INLINE void hw_gpadc_set_dma_functionality(bool enabled)
Set DMA functionality.
Definition: hw_gpadc.h:423
hw_gpadc_enable
__STATIC_INLINE void hw_gpadc_enable(void)
Enable ADC.
Definition: hw_gpadc.h:734
HW_DMA_PRIO
HW_DMA_PRIO
Channel priority.
Definition: hw_dma.h:163
hw_gpadc_read_cb
void(* hw_gpadc_read_cb)(void *user_data, uint32_t conv_to_go)
ADC callback for read function.
Definition: hw_gpadc.h:74
hw_gpadc_in_progress
__STATIC_INLINE bool hw_gpadc_in_progress(void)
Check if conversion is in progress.
Definition: hw_gpadc.h:785
HW_GPADC_OVERSAMPLING_2_SAMPLES
Definition: hw_gpadc.h:134
HW_GPADC_OVERSAMPLING
HW_GPADC_OVERSAMPLING
GPADC oversampling.
Definition: hw_gpadc.h:132
REG_MSK
#define REG_MSK(base, reg, field)
Access register field mask.
Definition: sdk_defs.h:583
mode
HW_GPIO_MODE mode
Definition: hw_gpio.h:211
hw_gpadc_unregister_interrupt
void hw_gpadc_unregister_interrupt(void)
Unregister interrupt handler.
hw_gpadc_config_t
ADC configuration.
Definition: hw_gpadc_v2.h:159
hw_gpadc_get_value
uint16_t hw_gpadc_get_value(void)
Get conversion result value with gain compensation and over sampling.
hw_gpadc_get_ldo_constant_current
__STATIC_INLINE bool hw_gpadc_get_ldo_constant_current(void)
Get the current state of constant 20uA load current on ADC LDO output.
Definition: hw_gpadc.h:384
hw_gpadc_register_interrupt
void hw_gpadc_register_interrupt(hw_gpadc_interrupt_cb cb)
Register interrupt handler.
hw_gpadc_set_mint
__STATIC_INLINE void hw_gpadc_set_mint(bool enabled)
Set masked interrupt.
Definition: hw_gpadc.h:347
hw_gpadc_get_dma_functionality
__STATIC_INLINE bool hw_gpadc_get_dma_functionality(void)
Get current state of DMA functionality.
Definition: hw_gpadc.h:434
HW_GPADC_OVERSAMPLING_8_SAMPLES
Definition: hw_gpadc.h:136
REG_GETF
#define REG_GETF(base, reg, field)
Return the value of a register field.
Definition: sdk_defs.h:711
hw_gpadc_init
void hw_gpadc_init(const hw_gpadc_config_t *cfg, bool enable)
Initialize ADC.
hw_gpadc_set_offset_negative
__STATIC_INLINE void hw_gpadc_set_offset_negative(uint16_t offset)
Set offset adjustment for negative ADC array. This register offers a coarse offset calibration,...
Definition: hw_gpadc.h:498
hw_gpadc_disable
__STATIC_INLINE void hw_gpadc_disable(void)
Disable ADC.
Definition: hw_gpadc.h:750
HW_GPADC_INPUT_MODE_SINGLE_ENDED
Definition: hw_gpadc.h:122
hw_gpadc_store_diff_gain_error
void hw_gpadc_store_diff_gain_error(int16_t diff)
Store Differential ADC Gain Error.
hw_gpadc_get_mint
__STATIC_INLINE bool hw_gpadc_get_mint(void)
Get masked interrupt state.
Definition: hw_gpadc.h:358
hw_gpadc_is_enabled
__STATIC_INLINE bool hw_gpadc_is_enabled(void)
ADC enabled state.
Definition: hw_gpadc.h:761
hw_gpadc_convert_to_millivolt
int16_t hw_gpadc_convert_to_millivolt(const hw_gpadc_config_t *cfg, uint16_t raw)
Convert a GPADC raw measurement to voltage in mVolt. If no configuration is given,...
hw_gpadc_get_sign_change
__STATIC_INLINE bool hw_gpadc_get_sign_change(void)
Get the current input and output sign change.
Definition: hw_gpadc.h:312
hw_gpadc_set_oversampling
__STATIC_INLINE void hw_gpadc_set_oversampling(HW_GPADC_OVERSAMPLING n_samples)
Set oversampling.
Definition: hw_gpadc.h:246
hw_gpadc_store_diff_offset_error
void hw_gpadc_store_diff_offset_error(int16_t diff)
Store Differential ADC Offset Error.
hw_gpadc_store_se_offset_error
void hw_gpadc_store_se_offset_error(int16_t single)
Store Single Ended ADC Offset Error.
HW_GPADC_OVERSAMPLING_128_SAMPLES
Definition: hw_gpadc.h:140
high
bool high
Definition: hw_gpio.h:213
hw_gpadc_get_mute
__STATIC_INLINE bool hw_gpadc_get_mute(void)
Get the current input mute state.
Definition: hw_gpadc.h:288
hw_gpadc_set_chopping
__STATIC_INLINE void hw_gpadc_set_chopping(bool enabled)
Set chopping state.
Definition: hw_gpadc.h:324
hw_gpadc_calculate_single_ended_offset_error
__STATIC_INLINE int16_t hw_gpadc_calculate_single_ended_offset_error(int16_t low, int16_t high)
Calculate Single Ended ADC Offset Error from two points.
Definition: hw_gpadc.h:576
HW_GPADC_OVERSAMPLING_1_SAMPLE
Definition: hw_gpadc.h:133
hw_gpadc_pre_check_for_gain_error
bool hw_gpadc_pre_check_for_gain_error(void)
Check the availability of ADC Gain Error.