ald_tsense.c 5.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
/**
  *********************************************************************************
  *
  * @file    ald_tsense.c
  * @brief   TSENSE module driver.
  *
  * @version V1.0
  * @date    15 Dec 2017
  * @author  AE Team
  * @note
  *
  * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
  *
  *********************************************************************************
  */

#include "ald_tsense.h"
#include "ald_bkpc.h"


/** @addtogroup ES32FXXX_ALD
  * @{
  */

/** @defgroup TSENSE TSENSE
  * @brief TSENSE module driver
  * @{
  */
#ifdef ALD_TSENSE


/** @defgroup TSENSE_Private_Variables TSENSE Private Variables
  * @{
  */
tsense_cbk __tsense_cbk;
/**
  * @}
  */

/** @defgroup TSENSE_Public_Functions TSENSE Public Functions
  * @{
  */

/** @addtogroup TSENSE_Public_Functions_Group1 Initialization functions
  * @brief Initialization functions
  *
  * @verbatim
  ==============================================================================
              ##### Initialization functions #####
  ==============================================================================
    [..]  This section provides functions allowing to initialize the TSENSE:
      (+) This parameters can be configured:
        (++) Update Cycle
        (++) Output Mode
        (++) Perscaler
      (+) Select TSENSE source clock(default LOSC)

    @endverbatim
  * @{
  */

/**
  * @brief  Initializes the TSENSE according to the specified
  * @retval None
  */
W
wangyq2018 已提交
66
void ald_tsense_init(void)
67
{
W
wangyq2018 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	uint16_t tempt, temptinv;
	uint32_t tscic, tscicinv;

	TSENSE_UNLOCK();
	TSENSE->CR = 0;

	MODIFY_REG(TSENSE->CR, TSENSE_CR_CTN_MSK, 0x1 << TSENSE_CR_CTN_POS);
	MODIFY_REG(TSENSE->CR, TSENSE_CR_TSU_MSK, 0x4 << TSENSE_CR_TSU_POSS);
	MODIFY_REG(TSENSE->CR, TSENSE_CR_TOM_MSK, 0x3 << TSENSE_CR_TOM_POSS);
	MODIFY_REG(TSENSE->PSR, TSENSE_PSR_PRS_MSK, 0x1 << TSENSE_PSR_PRS_POSS);

	TSENSE->HTGR = 0x88F18;
	TSENSE->LTGR = 0x85C39;
	tempt        = *(volatile uint16_t *)0x40348;
	temptinv     = *(volatile uint16_t *)0x4034A;
	tscic        = *(volatile uint32_t *)0x40350;
	tscicinv     = *(volatile uint32_t *)0x40358;

	if ((tempt == (uint16_t)(~temptinv)) && (tscic == (~tscicinv))) {
		TSENSE->TBDR    = tempt;
		TSENSE->TCALBDR = (tscic & 0x1FFFFFF) >> 6;
	}
	else {
		TSENSE->TBDR    = 0x1E00;
		TSENSE->TCALBDR = 0x1FE70;
	}

	TSENSE_LOCK();
	return;
97 98 99 100 101 102 103 104 105
}

/**
  * @brief  Configure the TSENSE source.
  * @param  sel: TSENSE source type.
  * @retval None
  */
void ald_tsense_source_select(tsense_source_sel_t sel)
{
W
wangyq2018 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
	assert_param(IS_TSENSE_SOURCE_SEL(sel));

	BKPC_UNLOCK();
	MODIFY_REG(BKPC->PCCR, BKPC_PCCR_TSENSECS_MSK, sel << BKPC_PCCR_TSENSECS_POSS);

	if (sel == TSENSE_SOURCE_LOSC) {
		SET_BIT(BKPC->CR, BKPC_CR_LOSCEN_MSK);
	}
	else if (sel == TSENSE_SOURCE_LRC) {
		SET_BIT(BKPC->CR, BKPC_CR_LRCEN_MSK);
	}
	else {
		; /* do nothing */
	}

	BKPC_LOCK();
	return;
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
}
/**
  * @}
  */

/** @addtogroup TSENSE_Public_Functions_Group2 Peripheral Control functions
  * @brief Peripheral Control functions
  *
  * @verbatim
  ==============================================================================
              ##### Peripheral Control functions #####
  ==============================================================================
  [..]  This section provides functions allowing to:
    (+) ald_tsense_get_value() API can get the current temperature.
    (+) ald_tsense_get_value_by_it() API can get the current temperature by interrupt.
    (+) ald_tsense_irq_handler() API can handle the interrupt request.

    @endverbatim
  * @{
  */

/**
  * @brief  Get the current temperature
  * @param  tsense: The value of current temperature.
  * @retval ALD status:
  *         @arg @ref OK    The value is valid
  *         @arg @ref ERROR The value is invalid
  */
ald_status_t ald_tsense_get_value(uint16_t *tsense)
{
W
wangyq2018 已提交
153 154 155 156 157 158 159 160
	uint32_t tmp = 0;

	TSENSE_UNLOCK();
	SET_BIT(TSENSE->IFCR, TSENSE_IFCR_TSENSE_MSK);
	SET_BIT(TSENSE->CR, TSENSE_CR_EN_MSK);
	TSENSE_LOCK();

	while ((!(READ_BIT(TSENSE->IF, TSENSE_IF_TSENSE_MSK))) && (tmp++ < 1000000));
161

W
wangyq2018 已提交
162 163 164 165 166 167
	if (tmp >= 1000000) {
		TSENSE_UNLOCK();
		CLEAR_BIT(TSENSE->CR, TSENSE_CR_EN_MSK);
		TSENSE_LOCK();
		return TIMEOUT;
	}
168

W
wangyq2018 已提交
169 170 171
	TSENSE_UNLOCK();
	SET_BIT(TSENSE->IFCR, TSENSE_IFCR_TSENSE_MSK);
	TSENSE_LOCK();
172

W
wangyq2018 已提交
173 174 175 176 177 178
	if (READ_BIT(TSENSE->DR, TSENSE_DR_ERR_MSK)) {
		TSENSE_UNLOCK();
		CLEAR_BIT(TSENSE->CR, TSENSE_CR_EN_MSK);
		TSENSE_LOCK();
		return ERROR;
	}
179

W
wangyq2018 已提交
180
	*tsense = READ_BITS(TSENSE->DR, TSENSE_DR_DATA_MSK, TSENSE_DR_DATA_POSS);
181

W
wangyq2018 已提交
182 183 184
	TSENSE_UNLOCK();
	CLEAR_BIT(TSENSE->CR, TSENSE_CR_EN_MSK);
	TSENSE_LOCK();
185

W
wangyq2018 已提交
186
	return OK;
187 188 189 190 191 192 193 194 195
}

/**
  * @brief  Get the current temperature by interrupt
  * @param  cbk: The callback function
  * @retval None
  */
void ald_tsense_get_value_by_it(tsense_cbk cbk)
{
W
wangyq2018 已提交
196
	__tsense_cbk = cbk;
197

W
wangyq2018 已提交
198 199 200 201 202
	TSENSE_UNLOCK();
	SET_BIT(TSENSE->IFCR, TSENSE_IFCR_TSENSE_MSK);
	SET_BIT(TSENSE->IE, TSENSE_IE_TSENSE_MSK);
	SET_BIT(TSENSE->CR, TSENSE_CR_EN_MSK);
	TSENSE_LOCK();
203

W
wangyq2018 已提交
204
	return;
205 206 207 208 209 210 211 212
}

/**
  * @brief  This function handles TSENSE interrupt request.
  * @retval None
  */
void ald_tsense_irq_handler(void)
{
W
wangyq2018 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
	TSENSE_UNLOCK();
	SET_BIT(TSENSE->IFCR, TSENSE_IFCR_TSENSE_MSK);
	TSENSE_LOCK();

	if (__tsense_cbk == NULL) {
		TSENSE_UNLOCK();
		CLEAR_BIT(TSENSE->CR, TSENSE_CR_EN_MSK);
		TSENSE_LOCK();
		return;
	}

	if (READ_BIT(TSENSE->DR, TSENSE_DR_ERR_MSK)) {
		TSENSE_UNLOCK();
		CLEAR_BIT(TSENSE->CR, TSENSE_CR_EN_MSK);
		TSENSE_LOCK();
		__tsense_cbk(0, ERROR);
		return;
	}

	__tsense_cbk(READ_BITS(TSENSE->DR, TSENSE_DR_DATA_MSK, TSENSE_DR_DATA_POSS), OK);

	TSENSE_UNLOCK();
	SET_BIT(TSENSE->IFCR, TSENSE_IFCR_TSENSE_MSK);
	CLEAR_BIT(TSENSE->CR, TSENSE_CR_EN_MSK);
	TSENSE_LOCK();
	return;
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
}
/**
  * @}
  */
/**
  * @}
  */
#endif /* ALD_TSENSE */
/**
  * @}
  */

/**
  * @}
  */