MCPcopy Create free account
hub / github.com/Moddable-OpenSource/moddable / modSPIWrite

Function modSPIWrite

xs/platforms/esp/xsHost.c:1550–1609  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1548}
1549
1550uint8_t modSPIWrite(uint32_t offset, uint32_t size, const uint8_t *src)
1551{
1552 uint8_t temp[512] __attribute__ ((aligned (4)));
1553 uint32_t toAlign;
1554
1555 ets_isr_mask(FLASH_INT_MASK);
1556
1557 if (offset & 3) { // long align offset
1558 toAlign = 4 - (offset & 3);
1559 c_memset(temp, 0xFF, 4);
1560 c_memcpy(temp + 4 - toAlign, src, (size < toAlign) ? size : toAlign);
1561 if (SPI_FLASH_RESULT_OK != spi_flash_write(offset & ~3, (uint32_t *)temp, 4))
1562 goto fail;
1563
1564 if (size <= toAlign)
1565 goto bail;
1566
1567 src += toAlign;
1568 offset += toAlign;
1569 size -= toAlign;
1570 }
1571
1572 toAlign = size & ~3;
1573 if (toAlign) {
1574 size -= toAlign;
1575 if (3 & (uintptr_t)src) { // src is not long aligned, copy through stack
1576 while (toAlign) {
1577 uint32_t use = (toAlign > sizeof(temp)) ? sizeof(temp) : toAlign;
1578 c_memcpy(temp, src, use);
1579 if (SPI_FLASH_RESULT_OK != spi_flash_write(offset, (uint32_t *)temp, use))
1580 goto fail;
1581
1582 toAlign -= use;
1583 src += use;
1584 offset += use;
1585 }
1586 }
1587 else {
1588 if (SPI_FLASH_RESULT_OK != spi_flash_write(offset, (uint32_t *)src, toAlign))
1589 goto fail;
1590 src += toAlign;
1591 offset += toAlign;
1592 }
1593 }
1594
1595 if (size) { // long align tail
1596 c_memset(temp, 0xFF, 4);
1597 c_memcpy(temp, src, size);
1598 if (SPI_FLASH_RESULT_OK != spi_flash_write(offset, (uint32_t *)temp, 4))
1599 goto fail;
1600 }
1601
1602bail:
1603 ets_isr_unmask(FLASH_INT_MASK);
1604 return 1;
1605
1606fail:
1607 ets_isr_unmask(FLASH_INT_MASK);

Callers 13

spiWriteFunction · 0.70
doRemoteCommandFunction · 0.70
resetPrefsFunction · 0.50
erasePrefFunction · 0.50
modPreferenceSetFunction · 0.50
spiffs_hal_writeFunction · 0.50
lfs_progFunction · 0.50
xs_flash_writeFunction · 0.50
xs_flash_writeFunction · 0.50
lfs_progFunction · 0.50
resetStorageFunction · 0.50
eraseEntryFunction · 0.50

Calls

no outgoing calls

Tested by

no test coverage detected