Skip to content

Commit b5f0016

Browse files
committed
Fix board setup of obelix_bb, disable LXT32K.
1 parent 68d0ae3 commit b5f0016

5 files changed

Lines changed: 98 additions & 13 deletions

File tree

platform/wscript

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ def is_asterix(ctx):
4747
def is_obelix(ctx):
4848
return ctx.get_board() in ('obelix', 'obelix_bb')
4949

50+
@conf
51+
def is_obelix_bb(ctx):
52+
return ctx.get_board() in ('obelix_bb')
53+
5054
@conf
5155
def get_platform_name(ctx):
5256
if is_tintin(ctx):

src/fw/board/boards/board_em_lb525.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
16+
#include "rtconfig.h"
1717
#include "board/board.h"
1818
#include "drivers/sf32lb/uart_definitions.h"
1919
#include "board_em_lb525.h"
@@ -167,6 +167,18 @@ uint32_t BSP_GetOtpBase(void)
167167
return MPI2_MEM_BASE;
168168
}
169169

170+
#define HXT_DELAY_EXP_VAL 1000
171+
static void LRC_init(void)
172+
{
173+
HAL_PMU_RC10Kconfig();
174+
175+
HAL_RC_CAL_update_reference_cycle_on_48M(LXT_LP_CYCLE);
176+
uint32_t ref_cnt = HAL_RC_CAL_get_reference_cycle_on_48M();
177+
uint32_t cycle_t = (uint32_t)ref_cnt / (48 * LXT_LP_CYCLE);
178+
179+
HAL_PMU_SET_HXT3_RDY_DELAY((HXT_DELAY_EXP_VAL / cycle_t + 1));
180+
}
181+
170182
void HAL_PreInit(void)
171183
{
172184
// __asm("B .");
@@ -192,13 +204,13 @@ void HAL_PreInit(void)
192204
HAL_PMU_LpCLockSelect(PMU_LPCLK_RC32);
193205

194206
HAL_PMU_EnableDLL(1);
195-
207+
#ifndef LXT_DISABLE
196208
HAL_PMU_EnableXTAL32();
197209
if (HAL_PMU_LXTReady() != HAL_OK)
198210
HAL_ASSERT(0);
199211
// RTC/GTIME/LPTIME Using same low power clock source
200212
HAL_RTC_ENABLE_LXT();
201-
213+
#endif
202214
{
203215
uint8_t is_enable_lxt = 1;
204216
uint32_t wdt_staus = 0xFF;
@@ -216,7 +228,9 @@ void HAL_PreInit(void)
216228

217229
HAL_RCC_LCPU_ClockSelect(RCC_CLK_MOD_LP_PERI, RCC_CLK_PERI_HXT48);
218230

219-
HAL_HPAON_CANCEL_LP_ACTIVE_REQUEST();
231+
HAL_HPAON_CANCEL_LP_ACTIVE_REQUEST();
232+
if (HAL_LXT_DISABLED())
233+
LRC_init();
220234
}
221235

222236
HAL_RCC_HCPU_ConfigHCLK(240);

src/fw/board/boards/board_obelix_bb.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
16+
#include "rtconfig.h"
1717
#include "board/board.h"
1818
#include "drivers/sf32lb/uart_definitions.h"
1919
#include "drivers/i2c.h"
@@ -251,6 +251,19 @@ uint32_t BSP_GetOtpBase(void)
251251
return MPI2_MEM_BASE;
252252
}
253253

254+
255+
#define HXT_DELAY_EXP_VAL 1000
256+
static void LRC_init(void)
257+
{
258+
HAL_PMU_RC10Kconfig();
259+
260+
HAL_RC_CAL_update_reference_cycle_on_48M(LXT_LP_CYCLE);
261+
uint32_t ref_cnt = HAL_RC_CAL_get_reference_cycle_on_48M();
262+
uint32_t cycle_t = (uint32_t)ref_cnt / (48 * LXT_LP_CYCLE);
263+
264+
HAL_PMU_SET_HXT3_RDY_DELAY((HXT_DELAY_EXP_VAL / cycle_t + 1));
265+
}
266+
254267
void HAL_PreInit(void)
255268
{
256269
// __asm("B .");
@@ -277,12 +290,14 @@ void HAL_PreInit(void)
277290

278291
HAL_PMU_EnableDLL(1);
279292

293+
#ifndef LXT_DISABLE
280294
HAL_PMU_EnableXTAL32();
281295
if (HAL_PMU_LXTReady() != HAL_OK)
282296
HAL_ASSERT(0);
283297
// RTC/GTIME/LPTIME Using same low power clock source
284298
HAL_RTC_ENABLE_LXT();
285-
299+
#endif
300+
#if 0
286301
{
287302
uint8_t is_enable_lxt = 1;
288303
uint32_t wdt_staus = 0xFF;
@@ -296,11 +311,14 @@ void HAL_PreInit(void)
296311
HAL_LCPU_CONFIG_set(HAL_LCPU_CONFIG_BT_RC_CAL_IN_L, &is_lcpu_rccal, 1);
297312
HAL_PMU_SetWdt((uint32_t)hwp_wdt2); // Add reboot cause for watchdog2
298313
}
299-
314+
#endif
300315

301316
HAL_RCC_LCPU_ClockSelect(RCC_CLK_MOD_LP_PERI, RCC_CLK_PERI_HXT48);
302317

303318
HAL_HPAON_CANCEL_LP_ACTIVE_REQUEST();
319+
if (HAL_LXT_DISABLED())
320+
LRC_init();
321+
304322
}
305323

306324
HAL_RCC_HCPU_ConfigHCLK(240);

src/fw/drivers/sf32lb/stubs/rtc.c

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "rtconfig.h"
12
#include "drivers/rtc.h"
23
#include "drivers/rtc_private.h"
34
#include "drivers/stm32f2/rtc_calibration.h"
@@ -32,7 +33,7 @@ static RTC_HandleTypeDef RTC_Handler = {
3233

3334
typedef uint32_t RtcIntervalTicks;
3435

35-
static const unsigned int LSE_FREQUENCY_HZ = 32768;
36+
//static const unsigned int LSE_FREQUENCY_HZ = 32768;
3637
#define SECONDS_IN_A_DAY (60 * 60 * 24)
3738
#define TICKS_IN_AN_INTERVAL SECONDS_IN_A_DAY
3839

@@ -157,12 +158,47 @@ static void initialize_fast_mode_state(void) {
157158
//save_rtc_time_state(0);
158159
}
159160

161+
uint32_t rtc_get_lpcycle()
162+
{
163+
uint32_t value;
164+
#ifdef BF0_HCPU
165+
value = HAL_Get_backup(RTC_BACKUP_LPCYCLE_AVE);
166+
if (value == 0)
167+
value = 1200000;
168+
value += BSP_RTC_PPM; // Calibrate in initial with 8 cycle
169+
HAL_Set_backup(RTC_BACKUP_LPCYCLE, (uint32_t)value);
170+
#else
171+
value = HAL_Get_backup(RTC_BACKUP_LPCYCLE);
172+
#endif
173+
return value;
174+
}
175+
176+
static uint32_t soft_rc10_backup = 0;
177+
static uint16_t rc10_freq_khz;
178+
179+
void drv_set_soft_rc10_backup(uint32_t backup)
180+
{
181+
soft_rc10_backup = backup;
182+
/* RC10K freq_khz = 48000 * LXT_LP_CYCLE / v; */
183+
rc10_freq_khz = (uint16_t)(48000UL * LXT_LP_CYCLE / backup);
184+
}
185+
186+
void rtc_rc10_calculate_div(RTC_HandleTypeDef *hdl, uint32_t value)
187+
{
188+
hdl->Init.DivB = RC10K_SUB_SEC_DIVB;
189+
190+
// 1 seconds has total 1/(x/(48*8))/256=1.5M/x cycles, times 2^14 for DIVA
191+
uint32_t divider = RTC_Handler.Init.DivB * value;
192+
value = ((uint64_t)48000000 * LXT_LP_CYCLE * (1 << 14) + (divider >> 1)) / divider;
193+
hdl->Init.DivAInt = (uint32_t)(value >> 14);
194+
hdl->Init.DivAFrac = (uint32_t)(value & ((1 << 14) - 1));
195+
}
196+
160197
void rtc_init(void) {
161198
periph_config_acquire_lock();
162199
rtc_enable_backup_regs();
163-
#ifdef LXT_DISABLE
164-
#error "RTC LXT is disabled, but RTC init requires it to be enabled"
165-
#endif
200+
201+
#ifndef LXT_DISABLE
166202
#ifdef SF32LB52X
167203
HAL_PMU_EnableXTAL32();
168204
if (HAL_PMU_LXTReady() != HAL_OK)
@@ -174,11 +210,23 @@ void rtc_init(void) {
174210
WTF;
175211
}
176212
HAL_RTC_ENABLE_LXT();
177-
213+
#endif
214+
178215
RTC_Handler.Init.DivAInt = 0x80;
179216
RTC_Handler.Init.DivAFrac = 0x0;
180217
RTC_Handler.Init.DivB = 0x100;
181218
uint32_t wakesrc = RTC_INIT_NORMAL;
219+
220+
#ifdef LXT_DISABLE
221+
{
222+
uint64_t value = 0;
223+
value = rtc_get_lpcycle();
224+
drv_set_soft_rc10_backup((uint32_t)value);
225+
if (value)
226+
rtc_rc10_calculate_div(&RTC_Handler, value);
227+
}
228+
#endif
229+
182230
if (HAL_RTC_Init(&RTC_Handler, wakesrc) != HAL_OK)
183231
{
184232
WTF;

third_party/hal_sifli/wscript

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ def configure(conf):
55
conf.env.append_unique('DEFINES', 'HAL_NMI_HANLDER_OVERRIDED')
66
if conf.env.MICRO_FAMILY == 'SF32LB':
77
conf.env.append_unique('DEFINES', 'SF32LB52X')
8-
8+
if conf.is_obelix_bb():
9+
conf.env.append_unique('DEFINES', 'LXT_DISABLE=1')
910

1011
def build(bld):
1112
if bld.env.MICRO_FAMILY == 'SF32LB':

0 commit comments

Comments
 (0)