123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- #ifndef NRF_LPCOMP_H_
- #define NRF_LPCOMP_H_
- #include <nrfx.h>
- #ifdef __cplusplus
- extern "C" {
- #endif
- typedef enum
- {
- #if (LPCOMP_REFSEL_RESOLUTION == 8) || defined(__NRFX_DOXYGEN__)
- NRF_LPCOMP_REF_SUPPLY_1_8 = LPCOMP_REFSEL_REFSEL_SupplyOneEighthPrescaling,
- NRF_LPCOMP_REF_SUPPLY_2_8 = LPCOMP_REFSEL_REFSEL_SupplyTwoEighthsPrescaling,
- NRF_LPCOMP_REF_SUPPLY_3_8 = LPCOMP_REFSEL_REFSEL_SupplyThreeEighthsPrescaling,
- NRF_LPCOMP_REF_SUPPLY_4_8 = LPCOMP_REFSEL_REFSEL_SupplyFourEighthsPrescaling,
- NRF_LPCOMP_REF_SUPPLY_5_8 = LPCOMP_REFSEL_REFSEL_SupplyFiveEighthsPrescaling,
- NRF_LPCOMP_REF_SUPPLY_6_8 = LPCOMP_REFSEL_REFSEL_SupplySixEighthsPrescaling,
- NRF_LPCOMP_REF_SUPPLY_7_8 = LPCOMP_REFSEL_REFSEL_SupplySevenEighthsPrescaling,
- #elif (LPCOMP_REFSEL_RESOLUTION == 16) || defined(__NRFX_DOXYGEN__)
- NRF_LPCOMP_REF_SUPPLY_1_8 = LPCOMP_REFSEL_REFSEL_Ref1_8Vdd,
- NRF_LPCOMP_REF_SUPPLY_2_8 = LPCOMP_REFSEL_REFSEL_Ref2_8Vdd,
- NRF_LPCOMP_REF_SUPPLY_3_8 = LPCOMP_REFSEL_REFSEL_Ref3_8Vdd,
- NRF_LPCOMP_REF_SUPPLY_4_8 = LPCOMP_REFSEL_REFSEL_Ref4_8Vdd,
- NRF_LPCOMP_REF_SUPPLY_5_8 = LPCOMP_REFSEL_REFSEL_Ref5_8Vdd,
- NRF_LPCOMP_REF_SUPPLY_6_8 = LPCOMP_REFSEL_REFSEL_Ref6_8Vdd,
- NRF_LPCOMP_REF_SUPPLY_7_8 = LPCOMP_REFSEL_REFSEL_Ref7_8Vdd,
- NRF_LPCOMP_REF_SUPPLY_1_16 = LPCOMP_REFSEL_REFSEL_Ref1_16Vdd,
- NRF_LPCOMP_REF_SUPPLY_3_16 = LPCOMP_REFSEL_REFSEL_Ref3_16Vdd,
- NRF_LPCOMP_REF_SUPPLY_5_16 = LPCOMP_REFSEL_REFSEL_Ref5_16Vdd,
- NRF_LPCOMP_REF_SUPPLY_7_16 = LPCOMP_REFSEL_REFSEL_Ref7_16Vdd,
- NRF_LPCOMP_REF_SUPPLY_9_16 = LPCOMP_REFSEL_REFSEL_Ref9_16Vdd,
- NRF_LPCOMP_REF_SUPPLY_11_16 = LPCOMP_REFSEL_REFSEL_Ref11_16Vdd,
- NRF_LPCOMP_REF_SUPPLY_13_16 = LPCOMP_REFSEL_REFSEL_Ref13_16Vdd,
- NRF_LPCOMP_REF_SUPPLY_15_16 = LPCOMP_REFSEL_REFSEL_Ref15_16Vdd,
- #endif
- NRF_LPCOMP_REF_EXT_REF0 = LPCOMP_REFSEL_REFSEL_ARef |
- (LPCOMP_EXTREFSEL_EXTREFSEL_AnalogReference0 << 16),
- NRF_LPCOMP_CONFIG_REF_EXT_REF1 = LPCOMP_REFSEL_REFSEL_ARef |
- (LPCOMP_EXTREFSEL_EXTREFSEL_AnalogReference1 << 16),
- } nrf_lpcomp_ref_t;
- typedef enum
- {
- NRF_LPCOMP_INPUT_0 = LPCOMP_PSEL_PSEL_AnalogInput0,
- NRF_LPCOMP_INPUT_1 = LPCOMP_PSEL_PSEL_AnalogInput1,
- NRF_LPCOMP_INPUT_2 = LPCOMP_PSEL_PSEL_AnalogInput2,
- NRF_LPCOMP_INPUT_3 = LPCOMP_PSEL_PSEL_AnalogInput3,
- NRF_LPCOMP_INPUT_4 = LPCOMP_PSEL_PSEL_AnalogInput4,
- NRF_LPCOMP_INPUT_5 = LPCOMP_PSEL_PSEL_AnalogInput5,
- NRF_LPCOMP_INPUT_6 = LPCOMP_PSEL_PSEL_AnalogInput6,
- NRF_LPCOMP_INPUT_7 = LPCOMP_PSEL_PSEL_AnalogInput7
- } nrf_lpcomp_input_t;
- typedef enum
- {
- NRF_LPCOMP_DETECT_CROSS = LPCOMP_ANADETECT_ANADETECT_Cross,
- NRF_LPCOMP_DETECT_UP = LPCOMP_ANADETECT_ANADETECT_Up,
- NRF_LPCOMP_DETECT_DOWN = LPCOMP_ANADETECT_ANADETECT_Down
- } nrf_lpcomp_detect_t;
- typedef enum
- {
- NRF_LPCOMP_TASK_START = offsetof(NRF_LPCOMP_Type, TASKS_START),
- NRF_LPCOMP_TASK_STOP = offsetof(NRF_LPCOMP_Type, TASKS_STOP),
- NRF_LPCOMP_TASK_SAMPLE = offsetof(NRF_LPCOMP_Type, TASKS_SAMPLE)
- } nrf_lpcomp_task_t;
- typedef enum
- {
- NRF_LPCOMP_EVENT_READY = offsetof(NRF_LPCOMP_Type, EVENTS_READY),
- NRF_LPCOMP_EVENT_DOWN = offsetof(NRF_LPCOMP_Type, EVENTS_DOWN),
- NRF_LPCOMP_EVENT_UP = offsetof(NRF_LPCOMP_Type, EVENTS_UP),
- NRF_LPCOMP_EVENT_CROSS = offsetof(NRF_LPCOMP_Type, EVENTS_CROSS)
- } nrf_lpcomp_event_t;
- typedef enum
- {
- NRF_LPCOMP_SHORT_CROSS_STOP_MASK = LPCOMP_SHORTS_CROSS_STOP_Msk,
- NRF_LPCOMP_SHORT_UP_STOP_MASK = LPCOMP_SHORTS_UP_STOP_Msk,
- NRF_LPCOMP_SHORT_DOWN_STOP_MASK = LPCOMP_SHORTS_DOWN_STOP_Msk,
- NRF_LPCOMP_SHORT_READY_STOP_MASK = LPCOMP_SHORTS_READY_STOP_Msk,
- NRF_LPCOMP_SHORT_READY_SAMPLE_MASK = LPCOMP_SHORTS_READY_SAMPLE_Msk
- } nrf_lpcomp_short_mask_t;
- #ifdef LPCOMP_FEATURE_HYST_PRESENT
- typedef enum
- {
- NRF_LPCOMP_HYST_NOHYST = LPCOMP_HYST_HYST_NoHyst,
- NRF_LPCOMP_HYST_50mV = LPCOMP_HYST_HYST_Hyst50mV
- }nrf_lpcomp_hysteresis_t;
- #endif
- typedef struct
- {
- nrf_lpcomp_ref_t reference;
- nrf_lpcomp_detect_t detection;
- #ifdef LPCOMP_FEATURE_HYST_PRESENT
- nrf_lpcomp_hysteresis_t hyst;
- #endif
- } nrf_lpcomp_config_t;
- #define NRF_LPCOMP_CONFIG_DEFAULT { NRF_LPCOMP_REF_SUPPLY_FOUR_EIGHT, NRF_LPCOMP_DETECT_DOWN }
- __STATIC_INLINE void nrf_lpcomp_configure(const nrf_lpcomp_config_t * p_config);
- __STATIC_INLINE void nrf_lpcomp_input_select(nrf_lpcomp_input_t input);
- __STATIC_INLINE void nrf_lpcomp_enable(void);
- __STATIC_INLINE void nrf_lpcomp_disable(void);
- __STATIC_INLINE uint32_t nrf_lpcomp_result_get(void);
- __STATIC_INLINE void nrf_lpcomp_int_enable(uint32_t int_mask);
- __STATIC_INLINE void nrf_lpcomp_int_disable(uint32_t int_mask);
- __STATIC_INLINE bool nrf_lpcomp_int_enable_check(uint32_t int_mask);
- __STATIC_INLINE uint32_t * nrf_lpcomp_task_address_get(nrf_lpcomp_task_t task);
- __STATIC_INLINE uint32_t * nrf_lpcomp_event_address_get(nrf_lpcomp_event_t event);
- __STATIC_INLINE void nrf_lpcomp_shorts_enable(uint32_t mask);
- __STATIC_INLINE void nrf_lpcomp_shorts_disable(uint32_t mask);
- __STATIC_INLINE void nrf_lpcomp_task_trigger(nrf_lpcomp_task_t task);
- __STATIC_INLINE void nrf_lpcomp_event_clear(nrf_lpcomp_event_t event);
- __STATIC_INLINE bool nrf_lpcomp_event_check(nrf_lpcomp_event_t event);
- #ifndef SUPPRESS_INLINE_IMPLEMENTATION
- __STATIC_INLINE void nrf_lpcomp_configure(const nrf_lpcomp_config_t * p_config)
- {
- NRF_LPCOMP->TASKS_STOP = 1;
- NRF_LPCOMP->ENABLE = LPCOMP_ENABLE_ENABLE_Disabled << LPCOMP_ENABLE_ENABLE_Pos;
- NRF_LPCOMP->REFSEL =
- (p_config->reference << LPCOMP_REFSEL_REFSEL_Pos) & LPCOMP_REFSEL_REFSEL_Msk;
-
- if ((p_config->reference & LPCOMP_REFSEL_REFSEL_ARef)==LPCOMP_REFSEL_REFSEL_ARef)
- {
- uint32_t extref = p_config->reference >> 16;
- NRF_LPCOMP->EXTREFSEL = (extref << LPCOMP_EXTREFSEL_EXTREFSEL_Pos) &
- LPCOMP_EXTREFSEL_EXTREFSEL_Msk;
- }
- NRF_LPCOMP->ANADETECT =
- (p_config->detection << LPCOMP_ANADETECT_ANADETECT_Pos) & LPCOMP_ANADETECT_ANADETECT_Msk;
- #ifdef LPCOMP_FEATURE_HYST_PRESENT
- NRF_LPCOMP->HYST = ((p_config->hyst) << LPCOMP_HYST_HYST_Pos) & LPCOMP_HYST_HYST_Msk;
- #endif
- NRF_LPCOMP->SHORTS = 0;
- NRF_LPCOMP->INTENCLR = LPCOMP_INTENCLR_CROSS_Msk | LPCOMP_INTENCLR_UP_Msk |
- LPCOMP_INTENCLR_DOWN_Msk | LPCOMP_INTENCLR_READY_Msk;
- }
- __STATIC_INLINE void nrf_lpcomp_input_select(nrf_lpcomp_input_t input)
- {
- uint32_t lpcomp_enable_state = NRF_LPCOMP->ENABLE;
- NRF_LPCOMP->ENABLE = LPCOMP_ENABLE_ENABLE_Disabled << LPCOMP_ENABLE_ENABLE_Pos;
- NRF_LPCOMP->PSEL =
- ((uint32_t)input << LPCOMP_PSEL_PSEL_Pos) | (NRF_LPCOMP->PSEL & ~LPCOMP_PSEL_PSEL_Msk);
- NRF_LPCOMP->ENABLE = lpcomp_enable_state;
- }
- __STATIC_INLINE void nrf_lpcomp_enable(void)
- {
- NRF_LPCOMP->ENABLE = LPCOMP_ENABLE_ENABLE_Enabled << LPCOMP_ENABLE_ENABLE_Pos;
- NRF_LPCOMP->EVENTS_READY = 0;
- NRF_LPCOMP->EVENTS_DOWN = 0;
- NRF_LPCOMP->EVENTS_UP = 0;
- NRF_LPCOMP->EVENTS_CROSS = 0;
- }
- __STATIC_INLINE void nrf_lpcomp_disable(void)
- {
- NRF_LPCOMP->ENABLE = LPCOMP_ENABLE_ENABLE_Disabled << LPCOMP_ENABLE_ENABLE_Pos;
- }
- __STATIC_INLINE uint32_t nrf_lpcomp_result_get(void)
- {
- return (uint32_t)NRF_LPCOMP->RESULT;
- }
- __STATIC_INLINE void nrf_lpcomp_int_enable(uint32_t int_mask)
- {
- NRF_LPCOMP->INTENSET = int_mask;
- }
- __STATIC_INLINE void nrf_lpcomp_int_disable(uint32_t int_mask)
- {
- NRF_LPCOMP->INTENCLR = int_mask;
- }
- __STATIC_INLINE bool nrf_lpcomp_int_enable_check(uint32_t int_mask)
- {
- return (NRF_LPCOMP->INTENSET & int_mask);
- }
- __STATIC_INLINE uint32_t * nrf_lpcomp_task_address_get(nrf_lpcomp_task_t task)
- {
- return (uint32_t *)((uint8_t *)NRF_LPCOMP + task);
- }
- __STATIC_INLINE uint32_t * nrf_lpcomp_event_address_get(nrf_lpcomp_event_t event)
- {
- return (uint32_t *)((uint8_t *)NRF_LPCOMP + event);
- }
- __STATIC_INLINE void nrf_lpcomp_shorts_enable(uint32_t short_mask)
- {
- NRF_LPCOMP->SHORTS |= short_mask;
- }
- __STATIC_INLINE void nrf_lpcomp_shorts_disable(uint32_t short_mask)
- {
- NRF_LPCOMP->SHORTS &= ~short_mask;
- }
- __STATIC_INLINE void nrf_lpcomp_task_trigger(nrf_lpcomp_task_t task)
- {
- *( (volatile uint32_t *)( (uint8_t *)NRF_LPCOMP + (uint32_t)task) ) = 1;
- }
- __STATIC_INLINE void nrf_lpcomp_event_clear(nrf_lpcomp_event_t event)
- {
- *( (volatile uint32_t *)( (uint8_t *)NRF_LPCOMP + (uint32_t)event) ) = 0;
- #if __CORTEX_M == 0x04
- volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)NRF_LPCOMP + (uint32_t)event));
- (void)dummy;
- #endif
- }
- __STATIC_INLINE bool nrf_lpcomp_event_check(nrf_lpcomp_event_t event)
- {
- return (bool) (*(volatile uint32_t *)( (uint8_t *)NRF_LPCOMP + (uint32_t)event));
- }
- #endif
- #ifdef __cplusplus
- }
- #endif
- #endif
|