📄 halstm25p40m.nc
字号:
if (!taskPosted[TASK_WRITE]) { taskPosted[TASK_WRITE] = TRUE; writeQueueAddress = address; writeQueueBuffer = buffer; writeQueueLength = length; /* if the queue is empty no task has been posted and the flash is off */ if (queueEmpty()) { flashTaskPosted = TRUE; if (flashOn) { post writeTask(); } else { post wakeupTask(); queuePutBack(TASK_WRITE); } } else queuePutBack(TASK_WRITE); return SUCCESS; } else { return FAIL; } } task void writeTask() { if (call BusArbitration.getBus() == FAIL) { queuePutFront(TASK_WRITE); return; } /*********************************************** ** select SPI bus and module 1 (micro4) ***********************************************/ call Spi.enable(BUS_CLOCK_INVERT + BUS_CLOCK_4MHZ, 1); /*********************************************** ** Write buffer to page, starting at address ***********************************************/ call HPLFlash.write(writeQueueAddress, writeQueueBuffer, writeQueueLength); /* set timer to post checkBusyTask (signalTask) */ call Timer.startOneShot(20); timerActive = TRUE; currentTask = TASK_WRITE; taskPosted[TASK_WRITE] = FALSE; call Spi.disable(); call BusArbitration.releaseBus(); return; } /********************************************************************** * Flash erase *********************************************************************/ /** * Flash sector erase. Sector size is 64 kilobytes. * Address must point at start of sector. * * \param address block address on flash * * \return SUCCESS * \return FAIL bus not free */ uint32_t sectorEraseQueueAddress; command error_t Flash.sectorErase(uint32_t address) { if (address > 0x07FFFF) return FAIL; if (!taskPosted[TASK_SECTOR_ERASE]) { taskPosted[TASK_SECTOR_ERASE] = TRUE; sectorEraseQueueAddress = address; /* if the queue is empty no task has been posted and the flash is off */ if (queueEmpty()) { flashTaskPosted = TRUE; if (flashOn) { post sectorEraseTask(); } else { post wakeupTask(); queuePutBack(TASK_SECTOR_ERASE); } } else queuePutBack(TASK_SECTOR_ERASE); return SUCCESS; } else { return FAIL; } } task void sectorEraseTask() { if (call BusArbitration.getBus() == FAIL) { queuePutFront(TASK_SECTOR_ERASE); return; } /*********************************************** ** select SPI bus and module 1 (micro4) ***********************************************/ call Spi.enable(BUS_CLOCK_INVERT + BUS_CLOCK_4MHZ, 1); /*********************************************** ** Erase sector holding address ***********************************************/ call HPLFlash.sectorErase(sectorEraseQueueAddress); /* set timer to post checkBusyTask (signalTask) */ call Timer.startOneShot(1000); timerActive = TRUE; currentTask = TASK_SECTOR_ERASE; taskPosted[TASK_SECTOR_ERASE] = FALSE; call Spi.disable(); call BusArbitration.releaseBus(); return; } /** * Flash bulk erase. * Erases entire flash * * \return SUCCESS * \return FAIL bus not free */ command error_t Flash.bulkErase() { if (!taskPosted[TASK_BULK_ERASE]) { taskPosted[TASK_BULK_ERASE] = TRUE; /* if the queue is empty no task has been posted and the flash is off */ if (queueEmpty()) { flashTaskPosted = TRUE; if (flashOn) { post bulkEraseTask(); } else { post wakeupTask(); queuePutBack(TASK_BULK_ERASE); } } else queuePutBack(TASK_BULK_ERASE); return SUCCESS; } else { return FAIL; } } task void bulkEraseTask() { if (call BusArbitration.getBus() == FAIL) { queuePutFront(TASK_BULK_ERASE); return; } /*********************************************** ** select SPI bus and module 1 (micro4) ***********************************************/ call Spi.enable(BUS_CLOCK_INVERT + BUS_CLOCK_4MHZ, 1); /*********************************************** ** Bulk erase ***********************************************/ call HPLFlash.bulkErase(); /* set timer to post checkBusyTask (signalTask) */ call Timer.startOneShot(4500); timerActive = TRUE; currentTask = TASK_BULK_ERASE; taskPosted[TASK_BULK_ERASE] = FALSE; call Spi.disable(); call BusArbitration.releaseBus(); return; } /********************************************************************** * Flash power management *********************************************************************/ /** * Deep Power-down. * * \return SUCCESS * \return FAIL bus not free */ command error_t Flash.sleep() { if (!taskPosted[TASK_SLEEP]) { taskPosted[TASK_SLEEP] = TRUE; /* if the queue is empty no task has been posted and the flash is off */ if (queueEmpty()) { flashTaskPosted = TRUE; post sleepTask(); } else { queuePutBack(TASK_SLEEP); } return SUCCESS; } else { return FAIL; } } task void sleepTask() { if (call BusArbitration.getBus() == FAIL) { queuePutFront(TASK_SLEEP); return; } /*********************************************** ** select SPI bus and module 1 (micro4) ***********************************************/ call Spi.enable(BUS_CLOCK_INVERT + BUS_CLOCK_4MHZ, 1); /*********************************************** ** Send command to enter deep power-down ***********************************************/ call HPLFlash.sleep(); flashOn = FALSE; flashTaskPosted = FALSE; taskPosted[TASK_SLEEP] = FALSE; call Spi.disable(); call BusArbitration.releaseBus(); signal sleeping(); call StdOut.print("sleep\r\n"); return; } /** * Flash signature read. Wakes the device up from power down mode. * Signature value should be 0x12 if the flash is present and working. * * \return signature value * \return -1 bus not free */ command error_t Flash.wakeUp() { if (!taskPosted[TASK_WAKEUP]) { taskPosted[TASK_WAKEUP] = TRUE; /* if the queue is empty no task has been posted and the flash is off */ if (queueEmpty()) { flashTaskPosted = TRUE; post wakeupTask(); } else { queuePutBack(TASK_WAKEUP); } return SUCCESS; } else { return FAIL; } } task void wakeupTask() { int16_t retval; call StdOut.print("wakeupTask\n\r"); if (call BusArbitration.getBus() == FAIL) { queuePutFront(TASK_WAKEUP); return; } /*********************************************** ** select SPI bus and module 1 (micro4) ***********************************************/ call Spi.enable(BUS_CLOCK_INVERT + BUS_CLOCK_4MHZ, 1); /*********************************************** ** Read signature ***********************************************/ retval = call HPLFlash.wakeUp(); call StdOut.print("wakeup: "); call StdOut.printHex(retval); call StdOut.print("\r\n"); /* check if wakeup was successfull */ if (retval != 0x12) { /* flash not ready - repost wakeup task */ post wakeupTask(); } else { /* wakeup completed */ flashOn = TRUE; flashTaskPosted = FALSE; taskPosted[TASK_WAKEUP] = FALSE; } call Spi.disable(); call BusArbitration.releaseBus(); return; } /********************************************************************** * Checks if flash is writing *********************************************************************/ task void checkBusyTask() { if (call BusArbitration.getBus() == FAIL) { postCheckBusyTask = TRUE; return; } /*********************************************** ** select SPI bus and module 1 (micro4) ***********************************************/ call Spi.enable(BUS_CLOCK_INVERT + BUS_CLOCK_4MHZ, 1); /*********************************************** ** Checks if flash is writing ***********************************************/ if (call HPLFlash.isFree() == SUCCESS) { /* signal recent task */ switch(currentTask) { case TASK_WRITE: signal Flash.writeDone(writeQueueAddress, writeQueueBuffer, writeQueueLength); break; case TASK_SECTOR_ERASE: signal Flash.sectorEraseDone(sectorEraseQueueAddress); break; case TASK_BULK_ERASE: signal Flash.bulkEraseDone(); break; default: break; } currentTask = 0; /* post - if any - next task */ flashTaskPosted = FALSE; timerActive = FALSE; postNextTask(); } else { call Timer.startOneShot(FLASH_BUSY_BACKOFF); } call Spi.disable(); call BusArbitration.releaseBus(); return; } default event void sleeping() {}; async event void StdOut.get(uint8_t data) { }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -