Skip to content

Commit 8388ec4

Browse files
committed
stmhal: Update to new STM Cube HAL library.
This upgrades the HAL to the versions: - F4 V1.16.0 - F7 V1.7.0 - L4 V1.8.1 The main changes were in the SD card driver. The vendor changed the SD read/write functions to accept block number intead of byte address, so there is no longer any need for a custom patch for this in stm32lib. The CardType values also changed, so pyb.SDCard().info() will return different values for the 3rd element of the tuple, but this function was never documented.
1 parent fe6f035 commit 8388ec4

5 files changed

Lines changed: 60 additions & 34 deletions

File tree

lib/stm32lib

Submodule stm32lib updated 609 files

stmhal/can.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ STATIC mp_obj_t pyb_can_recv(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_
527527

528528
// receive the data
529529
CanRxMsgTypeDef rx_msg;
530-
self->can.pRxMsg = &rx_msg;
530+
self->can.pRxMsg = self->can.pRx1Msg = &rx_msg;
531531
HAL_StatusTypeDef status = HAL_CAN_Receive(&self->can, args[0].u_int, args[1].u_int);
532532

533533
if (status != HAL_OK) {

stmhal/modmachine.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,7 @@
5454
#include "wdt.h"
5555
#include "genhdr/pllfreqtable.h"
5656

57-
#if defined(MCU_SERIES_F4)
58-
// the HAL does not define these constants
59-
#define RCC_CSR_IWDGRSTF (0x20000000)
60-
#define RCC_CSR_PINRSTF (0x04000000)
61-
#elif defined(MCU_SERIES_L4)
57+
#if defined(MCU_SERIES_L4)
6258
// L4 does not have a POR, so use BOR instead
6359
#define RCC_CSR_PORRSTF RCC_CSR_BORRSTF
6460
#endif

stmhal/sdcard.c

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -191,16 +191,15 @@ bool sdcard_power_on(void) {
191191
sd_handle.Init.ClockDiv = SDIO_TRANSFER_CLK_DIV;
192192

193193
// init the SD interface, with retry if it's not ready yet
194-
HAL_SD_CardInfoTypedef cardinfo;
195-
for (int retry = 10; HAL_SD_Init(&sd_handle, &cardinfo) != SD_OK; retry--) {
194+
for (int retry = 10; HAL_SD_Init(&sd_handle) != HAL_OK; retry--) {
196195
if (retry == 0) {
197196
goto error;
198197
}
199198
mp_hal_delay_ms(50);
200199
}
201200

202201
// configure the SD bus width for wide operation
203-
if (HAL_SD_WideBusOperation_Config(&sd_handle, SDIO_BUS_WIDE_4B) != SD_OK) {
202+
if (HAL_SD_ConfigWideBusOperation(&sd_handle, SDIO_BUS_WIDE_4B) != HAL_OK) {
204203
HAL_SD_DeInit(&sd_handle);
205204
goto error;
206205
}
@@ -224,9 +223,9 @@ uint64_t sdcard_get_capacity_in_bytes(void) {
224223
if (sd_handle.Instance == NULL) {
225224
return 0;
226225
}
227-
HAL_SD_CardInfoTypedef cardinfo;
228-
HAL_SD_Get_CardInfo(&sd_handle, &cardinfo);
229-
return cardinfo.CardCapacity;
226+
HAL_SD_CardInfoTypeDef cardinfo;
227+
HAL_SD_GetCardInfo(&sd_handle, &cardinfo);
228+
return (uint64_t)cardinfo.LogBlockNbr * (uint64_t)cardinfo.LogBlockSize;
230229
}
231230

232231
void SDIO_IRQHandler(void) {
@@ -243,13 +242,37 @@ void SDMMC2_IRQHandler(void) {
243242
}
244243
#endif
245244

245+
STATIC HAL_StatusTypeDef sdcard_wait_finished(SD_HandleTypeDef *sd, uint32_t timeout) {
246+
// Wait for HAL driver to be ready (eg for DMA to finish)
247+
uint32_t start = HAL_GetTick();
248+
while (sd->State == HAL_SD_STATE_BUSY) {
249+
if (HAL_GetTick() - start >= timeout) {
250+
return HAL_TIMEOUT;
251+
}
252+
}
253+
// Wait for SD card to complete the operation
254+
for (;;) {
255+
HAL_SD_CardStateTypeDef state = HAL_SD_GetCardState(sd);
256+
if (state == HAL_SD_CARD_TRANSFER) {
257+
return HAL_OK;
258+
}
259+
if (!(state == HAL_SD_CARD_SENDING || state == HAL_SD_CARD_RECEIVING || state == HAL_SD_CARD_PROGRAMMING)) {
260+
return HAL_ERROR;
261+
}
262+
if (HAL_GetTick() - start >= timeout) {
263+
return HAL_TIMEOUT;
264+
}
265+
}
266+
return HAL_OK;
267+
}
268+
246269
mp_uint_t sdcard_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks) {
247270
// check that SD card is initialised
248271
if (sd_handle.Instance == NULL) {
249-
return SD_ERROR;
272+
return HAL_ERROR;
250273
}
251274

252-
HAL_SD_ErrorTypedef err = SD_OK;
275+
HAL_StatusTypeDef err = HAL_OK;
253276

254277
// check that dest pointer is aligned on a 4-byte boundary
255278
uint8_t *orig_dest = NULL;
@@ -280,18 +303,20 @@ mp_uint_t sdcard_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blo
280303
// from reading the peripheral the CPU then reads the new data
281304
MP_HAL_CLEANINVALIDATE_DCACHE(dest, num_blocks * SDCARD_BLOCK_SIZE);
282305

283-
err = HAL_SD_ReadBlocks_BlockNumber_DMA(&sd_handle, (uint32_t*)dest, block_num, SDCARD_BLOCK_SIZE, num_blocks);
284-
if (err == SD_OK) {
285-
// wait for DMA transfer to finish, with a large timeout
286-
err = HAL_SD_CheckReadOperation(&sd_handle, 100000000);
306+
err = HAL_SD_ReadBlocks_DMA(&sd_handle, dest, block_num, num_blocks);
307+
if (err == HAL_OK) {
308+
err = sdcard_wait_finished(&sd_handle, 60000);
287309
}
288310

289311
dma_deinit(&SDMMC_RX_DMA);
290312
sd_handle.hdmarx = NULL;
291313

292314
restore_irq_pri(basepri);
293315
} else {
294-
err = HAL_SD_ReadBlocks_BlockNumber(&sd_handle, (uint32_t*)dest, block_num, SDCARD_BLOCK_SIZE, num_blocks);
316+
err = HAL_SD_ReadBlocks(&sd_handle, dest, block_num, num_blocks, 60000);
317+
if (err == HAL_OK) {
318+
err = sdcard_wait_finished(&sd_handle, 60000);
319+
}
295320
}
296321

297322
if (orig_dest != NULL) {
@@ -306,22 +331,22 @@ mp_uint_t sdcard_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blo
306331
mp_uint_t sdcard_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_blocks) {
307332
// check that SD card is initialised
308333
if (sd_handle.Instance == NULL) {
309-
return SD_ERROR;
334+
return HAL_ERROR;
310335
}
311336

312-
HAL_SD_ErrorTypedef err = SD_OK;
337+
HAL_StatusTypeDef err = HAL_OK;
313338

314339
// check that src pointer is aligned on a 4-byte boundary
315340
if (((uint32_t)src & 3) != 0) {
316341
// pointer is not aligned, so allocate a temporary block to do the write
317342
uint8_t *src_aligned = m_new_maybe(uint8_t, SDCARD_BLOCK_SIZE);
318343
if (src_aligned == NULL) {
319-
return SD_ERROR;
344+
return HAL_ERROR;
320345
}
321346
for (size_t i = 0; i < num_blocks; ++i) {
322347
memcpy(src_aligned, src + i * SDCARD_BLOCK_SIZE, SDCARD_BLOCK_SIZE);
323348
err = sdcard_write_blocks(src_aligned, block_num + i, 1);
324-
if (err != SD_OK) {
349+
if (err != HAL_OK) {
325350
break;
326351
}
327352
}
@@ -339,17 +364,19 @@ mp_uint_t sdcard_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t n
339364
// make sure cache is flushed to RAM so the DMA can read the correct data
340365
MP_HAL_CLEAN_DCACHE(src, num_blocks * SDCARD_BLOCK_SIZE);
341366

342-
err = HAL_SD_WriteBlocks_BlockNumber_DMA(&sd_handle, (uint32_t*)src, block_num, SDCARD_BLOCK_SIZE, num_blocks);
343-
if (err == SD_OK) {
344-
// wait for DMA transfer to finish, with a large timeout
345-
err = HAL_SD_CheckWriteOperation(&sd_handle, 100000000);
367+
err = HAL_SD_WriteBlocks_DMA(&sd_handle, (uint8_t*)src, block_num, num_blocks);
368+
if (err == HAL_OK) {
369+
err = sdcard_wait_finished(&sd_handle, 60000);
346370
}
347371
dma_deinit(&SDMMC_TX_DMA);
348372
sd_handle.hdmatx = NULL;
349373

350374
restore_irq_pri(basepri);
351375
} else {
352-
err = HAL_SD_WriteBlocks_BlockNumber(&sd_handle, (uint32_t*)src, block_num, SDCARD_BLOCK_SIZE, num_blocks);
376+
err = HAL_SD_WriteBlocks(&sd_handle, (uint8_t*)src, block_num, num_blocks, 60000);
377+
if (err == HAL_OK) {
378+
err = sdcard_wait_finished(&sd_handle, 60000);
379+
}
353380
}
354381

355382
return err;
@@ -392,12 +419,12 @@ STATIC mp_obj_t sd_info(mp_obj_t self) {
392419
if (sd_handle.Instance == NULL) {
393420
return mp_const_none;
394421
}
395-
HAL_SD_CardInfoTypedef cardinfo;
396-
HAL_SD_Get_CardInfo(&sd_handle, &cardinfo);
422+
HAL_SD_CardInfoTypeDef cardinfo;
423+
HAL_SD_GetCardInfo(&sd_handle, &cardinfo);
397424
// cardinfo.SD_csd and cardinfo.SD_cid have lots of info but we don't use them
398425
mp_obj_t tuple[3] = {
399-
mp_obj_new_int_from_ull(cardinfo.CardCapacity),
400-
mp_obj_new_int_from_uint(cardinfo.CardBlockSize),
426+
mp_obj_new_int_from_ull((uint64_t)cardinfo.LogBlockNbr * (uint64_t)cardinfo.LogBlockSize),
427+
mp_obj_new_int_from_uint(cardinfo.LogBlockSize),
401428
mp_obj_new_int(cardinfo.CardType),
402429
};
403430
return mp_obj_new_tuple(3, tuple);

stmhal/system_stm32.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,10 @@ void __fatal_error(const char *msg);
113113
#define CONFIG_RCC_CR_2ND (RCC_CR_HSEON || RCC_CR_CSSON || RCC_CR_PLLON)
114114
#define CONFIG_RCC_PLLCFGR (0x24003010)
115115

116-
#if defined(MCU_SERIES_F7)
116+
#if defined(MCU_SERIES_F4)
117+
const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
118+
const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
119+
#elif defined(MCU_SERIES_F7)
117120
const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
118121
const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
119122
#endif

0 commit comments

Comments
 (0)