void main(void) { HAL_BOARD_INIT(); #if HAL_OTA_XNV_IS_SPI XNV_SPI_INIT(); #endif /* This is in place of calling HalDmaInit() which would require init of the * other 4 DMA descriptors in addition to just Channel 0. */ HAL_DMA_SET_ADDR_DESC0( &dmaCh0 ); while (1) { HalFlashRead(HAL_OTA_CRC_ADDR / HAL_FLASH_PAGE_SIZE, HAL_OTA_CRC_ADDR % HAL_FLASH_PAGE_SIZE, (uint8 *)&OTA_crcControl, sizeof(OTA_crcControl)); if (OTA_crcControl.crc[0] == OTA_crcControl.crc[1]) { break; } else if ((OTA_crcControl.crc[0] != 0) && (OTA_crcControl.crc[0] == crcCalc())) { OTA_crcControl.crc[1] = OTA_crcControl.crc[0]; HalFlashWrite((HAL_OTA_CRC_ADDR / HAL_FLASH_WORD_SIZE), (uint8 *)OTA_crcControl.crc, 1); } else { dl2rc(); } } // Simulate a reset for the Application code by an absolute jump to location 0x0800. asm("LJMP 0x800\n"); }
/************************************************************************************************** * @fn crcCheck * * @brief Calculate the image CRC and set it ready-to-run if it is good. * * input parameters * * @param page - Flash page on which to beging the CRC calculation. * * output parameters * * None. * * @return None, but no return from this function if the CRC check is good. ************************************************************************************************** */ static void crcCheck(uint8 page, uint16 *crc) { HAL_BOARD_INIT(); /* This is in place of calling HalDmaInit() which would require init of the other 4 DMA * descriptors in addition to just Channel 0. */ //P0DIR |= 1; //P0_0 = 0; //P0_0 = 1; //P0_0 = 0; //P0_0 = 1; //P0_0 = 0; //P0_0 = 1; if (crc[0] == crcCalcDMA(page)) { //P0_0 = 0; uint16 addr = page * (HAL_FLASH_PAGE_SIZE / HAL_FLASH_WORD_SIZE) + BIM_CRC_OSET / HAL_FLASH_WORD_SIZE; crc[1] = crc[0]; crc[0] = 0xFFFF; HAL_DMA_SET_ADDR_DESC0(&dmaCh0); HalFlashWrite(addr, (uint8 *)crc, 1); HAL_SYSTEM_RESET(); } }
/************************************************************************************************** * @fn sblInit * * @brief SBL initialization. * * input parameters * * None. * * output parameters * * None. * * @return None. */ static void sblInit(void) { HAL_USB_PULLUP_DISABLE(); /* This is in place of calling HalDmaInit() which would require init of the other 4 DMA * descriptors in addition to just Channel 0. */ HAL_DMA_SET_ADDR_DESC0(&dmaCh0); HalUARTInitUSB(); SB_INIT_LEDS(); }
/****************************************************************************** * @fn HalDMAInit * * @brief DMA Interrupt Service Routine * * @param None * * @return None *****************************************************************************/ void HalDmaInit( void ) { HAL_DMA_SET_ADDR_DESC0( &dmaCh0 ); HAL_DMA_SET_ADDR_DESC1234( dmaCh1234 ); #if (HAL_UART_DMA || \ ((defined HAL_SPI) && (HAL_SPI == TRUE)) || \ ((defined HAL_IRGEN) && (HAL_IRGEN == TRUE))) DMAIE = 1; #endif }
/************************************************************************************************** * @fn sblInit * * @brief Initialization for SBL. * * input parameters * * None. * * output parameters * * None. * * @return None. ************************************************************************************************** */ static void sblInit(void) { HAL_BOARD_INIT(); vddWait(VDD_MIN_RUN); magicByte = SB_MAGIC_VALUE; /* This is in place of calling HalDmaInit() which would require init of the other 4 DMA * descriptors in addition to just Channel 0. */ HAL_DMA_SET_ADDR_DESC0(&dmaCh0); HalUARTInitUSB(); }
/************************************************************************************************** * @fn sblInit * * @brief Initialization for SBL. * * input parameters * * None. * * output parameters * * None. * * @return None. ************************************************************************************************** */ static void sblInit(void) { HAL_BOARD_INIT(); vddWait(VDD_MIN_RUN); magicByte = SB_MAGIC_VALUE; /* This is in place of calling HalDmaInit() which would require init of the other 4 DMA * descriptors in addition to just Channel 0. */ HAL_DMA_SET_ADDR_DESC0(&dmaCh0); #if defined CC2530_MK znpCfg1 = ZNP_CFG1_SPI; #else znpCfg1 = P2_0; #endif if (znpCfg1 == ZNP_CFG1_SPI) { SRDY_CLR(); // Select general purpose on I/O pins. P0SEL &= ~(NP_RDYIn_BIT); // P0.3 MRDY - GPIO P0SEL &= ~(NP_RDYOut_BIT); // P0.4 SRDY - GPIO // Select GPIO direction. P0DIR &= ~NP_RDYIn_BIT; // P0.3 MRDY - IN P0DIR |= NP_RDYOut_BIT; // P0.4 SRDY - OUT P0INP &= ~NP_RDYIn_BIT; // Pullup/down enable of MRDY input. P2INP &= ~BV(5); // Pullup all P0 inputs. HalUARTInitSPI(); } else { halUARTCfg_t uartConfig; HalUARTInitISR(); uartConfig.configured = TRUE; uartConfig.baudRate = HAL_UART_BR_115200; uartConfig.flowControl = FALSE; uartConfig.flowControlThreshold = 0; // CC2530 by #define - see hal_board_cfg.h uartConfig.rx.maxBufSize = 0; // CC2530 by #define - see hal_board_cfg.h uartConfig.tx.maxBufSize = 0; // CC2530 by #define - see hal_board_cfg.h uartConfig.idleTimeout = 0; // CC2530 by #define - see hal_board_cfg.h uartConfig.intEnable = TRUE; uartConfig.callBackFunc = NULL; HalUARTOpenISR(&uartConfig); } }
/************************************************************************************************** * @fn crcCheck * * @brief Calculate the image CRC and set it ready-to-run if it is good. * * input parameters * * @param page - Flash page on which to beging the CRC calculation. * * output parameters * * None. * * @return None, but no return from this function if the CRC check is good. ************************************************************************************************** */ static void crcCheck(uint8 page, uint16 *crc) { HAL_BOARD_INIT(); if (crc[0] == crcCalcDMA(page)) { //P0_0 = 0; uint16 addr = page * (HAL_FLASH_PAGE_SIZE / HAL_FLASH_WORD_SIZE) + BIM_CRC_OSET / HAL_FLASH_WORD_SIZE; crc[1] = crc[0]; crc[0] = 0xFFFF; HAL_DMA_SET_ADDR_DESC0(&dmaCh0); HalFlashWrite(addr, (uint8 *)crc, 1); HAL_SYSTEM_RESET(); } }
/************************************************************************************************** * @fn sblInit * * @brief SBL initialization. * * input parameters * * None. * * output parameters * * None. * * @return None. */ static void sblInit(uint8 bootloaderForcedByMainApp) { HAL_BOARD_INIT(); if (! bootloaderForcedByMainApp) { // If power on reset (i.e. reset not due to main software request) - pull down D+. // If reset is due to software request - do not touch the pull, otherwise the USB bus will reset and re-enumerate on the host. HAL_USB_PULLUP_DISABLE(); } /* This is in place of calling HalDmaInit() which would require init of the other 4 DMA * descriptors in addition to just Channel 0. */ HAL_DMA_SET_ADDR_DESC0(&dmaCh0); HalUARTInitUSB(); SB_INIT_LEDS(); }
/************************************************************************************************** * @fn main * * @brief C-code main function. * * input parameters * * None. * * output parameters * * None. * * @return None. ************************************************************************************************** */ void main(void) { vddWait(); HAL_BOARD_INIT(); /* This is in place of calling HalDmaInit() which would require init of the other 4 DMA * descriptors in addition to just Channel 0. */ HAL_DMA_SET_ADDR_DESC0(&dmaCh0); // Map flash bank #7 into XDATA for access to "ROM mapped as data". MEMCTR = (MEMCTR & 0xF8) | 0x07; imgSelect(); // Attempt to select and run an image. SLEEPCMD |= 0x03; // PM3, All clock oscillators off, voltage regulator off. halSleepExec(); HAL_SYSTEM_RESET(); // Should not get here. }
/************************************************************************************************** * @fn sblInit * * @brief SBL initialization. * * input parameters * * None. * * output parameters * * None. * * @return None. */ static void sblInit(void) { halUARTCfg_t uartConfig; /* This is in place of calling HalDmaInit() which would require init of the other 4 DMA * descriptors in addition to just Channel 0. */ HAL_DMA_SET_ADDR_DESC0(&dmaCh0); HalUARTInitISR(); uartConfig.configured = TRUE; uartConfig.baudRate = HAL_UART_BR_115200; uartConfig.flowControl = FALSE; uartConfig.flowControlThreshold = 0; // CC2530 by #define - see hal_board_cfg.h uartConfig.rx.maxBufSize = 0; // CC2530 by #define - see hal_board_cfg.h uartConfig.tx.maxBufSize = 0; // CC2530 by #define - see hal_board_cfg.h uartConfig.idleTimeout = 0; // CC2530 by #define - see hal_board_cfg.h uartConfig.intEnable = TRUE; uartConfig.callBackFunc = NULL; HalUARTOpenISR(&uartConfig); SB_INIT_LEDS(); }
void main(void) { uint16 crc[2]; HAL_BOARD_INIT(); #if HAL_OAD_XNV_IS_SPI XNV_SPI_INIT(); #endif /* This is in place of calling HalDmaInit() which would require init of the other 4 DMA * descriptors in addition to just Channel 0. */ HAL_DMA_SET_ADDR_DESC0( &dmaCh0 ); HalFlashInit(); HalFlashRead(HAL_OAD_CRC_ADDR / HAL_FLASH_PAGE_SIZE, HAL_OAD_CRC_ADDR % HAL_FLASH_PAGE_SIZE, (uint8 *)crc, sizeof(crc)); if (crc[0] != crc[1]) { // If the CRC is erased or the RC code fails a sanity check, instantiate the DL code (again). if ((crc[0] == 0) || (crc[0] != crcCalc())) { dl2rc(); /* If dl2rc() fails, a flawed image is allowed to run - * maybe the damage is not fatal to OTA ops? */ } else { crc[1] = crc[0]; HalFlashWrite((HAL_OAD_CRC_ADDR / HAL_FLASH_WORD_SIZE), (uint8 *)crc, 1); } } // Simulate a reset for the Application code by an absolute jump to location 0x0800. asm("LJMP 0x800\n"); }
/************************************************************************************************** * @fn DMAExecCrc * * @brief This function assumes CRC has been initialized and sets up and * starts a dma tranfer from a flash page to the CRC HW module. * * @note This function assumes DMA channel 0 is available for use. * * input parameters * * @param page - A valid flash page number. * @param offset - A valid offset into the page. * @param len - A valid number of bytes to calculate crc of. * * @return None. ************************************************************************************************** */ void DMAExecCrc(uint8 page, uint16 offset, uint16 len) { uint8 memctr = MEMCTR; // Save to restore. // Calculate the offset into the containing flash bank as it gets mapped into XDATA. uint16 address = (offset + HAL_FLASH_PAGE_MAP) + ((page % HAL_FLASH_PAGE_PER_BANK) * HAL_FLASH_PAGE_SIZE); // Pointer to DMA config structure halDMADesc_t *dmaCh0_p = &dmaCh0; #if !defined HAL_OAD_BOOT_CODE halIntState_t is; #endif page /= HAL_FLASH_PAGE_PER_BANK; // Calculate the flash bank from the flash page. #if !defined HAL_OAD_BOOT_CODE HAL_ENTER_CRITICAL_SECTION(is); #endif // Calculate and map the containing flash bank into XDATA. MEMCTR = (MEMCTR & 0xF8) | page; // page is actually bank // Start address for CRC calculation in the XDATA mapped flash bank HAL_DMA_SET_SOURCE(dmaCh0_p, address); // Destination for data transfer, RNDH mapped to XDATA HAL_DMA_SET_DEST(dmaCh0_p, 0x70BD); // One whole page (or len) at a time HAL_DMA_SET_LEN(dmaCh0_p, len); // 8-bit, block, no trigger HAL_DMA_SET_WORD_SIZE(dmaCh0_p, HAL_DMA_WORDSIZE_BYTE); HAL_DMA_SET_TRIG_MODE(dmaCh0_p, HAL_DMA_TMODE_BLOCK); HAL_DMA_SET_TRIG_SRC(dmaCh0_p, HAL_DMA_TRIG_NONE); // SRC += 1, DST = constant, no IRQ, all 8 bits, high priority HAL_DMA_SET_SRC_INC(dmaCh0_p, HAL_DMA_SRCINC_1); HAL_DMA_SET_DST_INC(dmaCh0_p, HAL_DMA_DSTINC_0); HAL_DMA_SET_IRQ(dmaCh0_p, HAL_DMA_IRQMASK_DISABLE); HAL_DMA_SET_M8(dmaCh0_p, HAL_DMA_M8_USE_8_BITS); HAL_DMA_SET_PRIORITY(dmaCh0_p, HAL_DMA_PRI_HIGH); // Tell DMA Controller where above configuration can be found HAL_DMA_SET_ADDR_DESC0(&dmaCh0); // Arm the DMA channel (0) HAL_DMA_ARM_CH(0); // 9 cycles wait asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); // Start DMA tranfer HAL_DMA_MAN_TRIGGER(0); // Wait for dma to finish. while(DMAREQ & 0x1); // Restore bank mapping MEMCTR = memctr; #if !defined HAL_OAD_BOOT_CODE HAL_EXIT_CRITICAL_SECTION(is); #endif }
/****************************************************************************** * @fn HalDMAInit * * @brief DMA Interrupt Service Routine * * @param None * * @return None *****************************************************************************/ void HalDmaInit( void ) { HAL_DMA_SET_ADDR_DESC0( &dmaCh0 ); HAL_DMA_SET_ADDR_DESC1234( dmaCh1234 ); DMAIE = 1; }