Exemplo n.º 1
0
Arquivo: main.c Projeto: gstroe/Arm
/**
 * \brief UART transfer with interrupt in UART loop back mode
 */
static void UartTransfer(void)
{
	uint8_t *pBuffer = &pTxBuffer[0];

	PMC_EnablePeripheral(BASE_ID);
	UART_Configure(BASE_UART, (UART_MR_PAR_NO | UART_MR_CHMODE_LOCAL_LOOPBACK),
			115200, BOARD_MCK);

	NVIC_ClearPendingIRQ(BASE_IRQ);
	NVIC_SetPriority(BASE_IRQ ,1);

	/* Enables the UART to transfer and receive data. */
	UART_SetTransmitterEnabled (BASE_UART , 1);
	UART_SetReceiverEnabled (BASE_UART , 1);

	UART_EnableIt(BASE_UART, (UART_IER_RXRDY | UART_IER_OVRE | UART_IER_FRAME
			| UART_IER_PARE));
	/* Enable interrupt  */
	NVIC_EnableIRQ(BASE_IRQ);

	while (*pBuffer != '\0') {
		UART_PutChar(BASE_UART, *pBuffer);
		pBuffer++;
	}
	UART_PutChar(BASE_UART, *pBuffer);
}
Exemplo n.º 2
0
/**
 * \brief Initializes the UartDma structure and the corresponding UART & DMA hardware.
 * select value.
 * The driver will uses DMA channel 0 for RX and DMA channel 1 for TX.
 * The DMA channels are freed automatically when no UART command processing.
 *
 * \param pUartd    Pointer to a UartDma instance.
 * \param pUartHw   Associated UART peripheral.
 * \param uartId    UART peripheral identifier.
 * \param uartMode  UART peripheral identifier.*
 * \param baud      UART baud rate
 * \param clk       UART ref clock
 * \param pXdmad    Pointer to a Dmad instance. 
 */
uint32_t UARTD_Configure( UartDma *pUartd ,
        Uart *pUartHw ,
        uint8_t uartId,
        uint32_t uartMode,
        uint32_t baud,
        uint32_t clk,
        sXdmad *pXdmad )
{
    /* Initialize the UART structure */
    pUartd->pUartHw = pUartHw;
    pUartd->uartId  = uartId;
    pUartd->pXdmad = pXdmad;

    /* Enable the UART Peripheral ,Execute a software reset of the UART, Configure UART in Master Mode*/
    UART_Configure ( pUartHw, uartMode, baud, clk);
    
    /* Enables the USART to transfer data. */
    UART_SetTransmitterEnabled ( pUartHw , 1);
    
    /* Enables the USART to receive data. */
    UART_SetReceiverEnabled ( pUartHw , 1);

    /* Driver initialize */
    XDMAD_Initialize(  pUartd->pXdmad, 0 );
    /* Configure and enable interrupt on RC compare */ 
    NVIC_ClearPendingIRQ(XDMAC_IRQn);
    NVIC_SetPriority( XDMAC_IRQn ,1);
    
    
    
    return 0;
}
Exemplo n.º 3
0
/**
 *  Initializes the U(S)ART Console
 *
 *  \param dwBaudRate  U(S)ART baudrate.
 *  \param dwMCk  Master clock frequency.
 */
extern void TRACE_CONFIGURE( uint32_t dwBaudRate, uint32_t dwMCk )
{
    const Pin pinsUART0[] = { PINS_UART } ;

    PIO_Configure( pinsUART0, PIO_LISTSIZE( pinsUART0 ) ) ;

    UART_Configure( dwBaudRate, dwMCk ) ;
}
Exemplo n.º 4
0
/**
 * \brief Check if there is Input from UART line.
 *
 * \return true if there is Input.
 */
extern uint32_t UART_IsRxReady( void )
{
    Uart *pUart=CONSOLE_USART ;

    if ( !_ucIsConsoleInitialized )
    {
        UART_Configure( CONSOLE_BAUDRATE, BOARD_MCK ) ;
    }

    return (pUart->UART_SR & UART_SR_RXRDY) > 0 ;
}
Exemplo n.º 5
0
/**
 * \brief Input a character from the UART line.
 *
 * \note This function is synchronous
 * \return character received.
 */
extern uint32_t UART_GetChar( void )
{
    Uart *pUart=CONSOLE_USART ;

    if ( !_ucIsConsoleInitialized )
    {
        UART_Configure(CONSOLE_BAUDRATE, BOARD_MCK);
    }

    while ( (pUart->UART_SR & UART_SR_RXRDY) == 0 ) ;

    return pUart->UART_RHR ;
}
Exemplo n.º 6
0
int main()
{
#if 1
    WDT_Disable(WDT); 
    /* Set 3 FWS for Embedded Flash Access */
    EFC->EEFC_FMR = EEFC_FMR_FWS(3);
    CLOCK_SetConfig(2);
    /* I don't know why, the baudrate is 38400 = 115200/3 */
    UART_Configure(115200, 64000000/3);// so I add this to solve the problem
    printf("Start GaInOS-TK.\r\n");
#endif   
    StartOS(OSDEFAULTAPPMODE);
    return 0;
}
Exemplo n.º 7
0
/**
 * \brief Outputs a character on the UART line.
 *
 * \note This function is synchronous (i.e. uses polling).
 * \param c  Character to send.
 */
extern void UART_PutChar( uint8_t c )
{
    Uart *pUart=CONSOLE_USART ;

    if ( !_ucIsConsoleInitialized )
    {
        UART_Configure( CONSOLE_BAUDRATE, BOARD_MCK ) ;
    }

    /* Wait for the transmitter to be ready */
    while ( (pUart->UART_SR & UART_SR_TXEMPTY) == 0 ) ;

    /* Send character */
    pUart->UART_THR=c ;

}
Exemplo n.º 8
0
/**
 * \brief Input a character from the UART line.
 *
 * \note This function uses polling.
 *       Use UART_SetTimeout() to set a timeout.
 * \return character received or -1 if timed out.
 */
extern int UART_Get( void )
{
    Uart *pUart=UART_UART ;
	uint32_t started;

    if ( !_ucIsConsoleInitialized )
        UART_Configure(UART_BAUDRATE, BOARD_MCK);

    /* Wait for the receiver to be ready */
	started = timetick();
	while ( (pUart->UART_SR & UART_SR_RXRDY) == 0 ) 
		if ((timetick() - started) > _xferTimeout) 
			return -1;

    return pUart->UART_RHR ;
}
Exemplo n.º 9
0
/**
 * \brief  Applet main entry. This function decodes received command and executes it.
 *
 * \param argc  always 1
 * \param argv  Address of the argument area..
 */
int main(int argc, char **argv)
{
    struct _Mailbox *pMailbox = (struct _Mailbox *) argv;
    uint32_t mode, crystalFreq, extClk, comType;

    // ----------------------------------------------------------
    // INIT:
    // ----------------------------------------------------------
    if (pMailbox->command == APPLET_CMD_INIT) {

        mode = pMailbox->argument.inputInit.mode;
        crystalFreq = pMailbox->argument.inputInit.crystalFreq;
        extClk = pMailbox->argument.inputInit.extClk;
        comType = pMailbox->argument.inputInit.comType;

        switch (mode) {
        case EK_MODE: 
            EK_LowLevelInit();
            pMailbox->status = APPLET_SUCCESS;
            break;
        case USER_DEFINED_CRYSTAL:
            user_defined_LowlevelInit(crystalFreq);
            pMailbox->status = APPLET_SUCCESS;
            break;
        case BYASS_MODE:
            bypass_LowLevelInit(extClk);
            pMailbox->status = APPLET_SUCCESS;
            break;
        default:
            pMailbox->status = APPLET_DEV_UNKNOWN;
            break;
        }
    } else {
        pMailbox->status = APPLET_DEV_UNKNOWN;
    }

    UART_Configure(115200, BOARD_MCK);

    /* Notify the host application of the end of the command processing */
    pMailbox->command = ~(pMailbox->command);
    if (comType == DBGU_COM_TYPE) {
        UART_PutChar(0x6);
    }

    return 0;
}
Exemplo n.º 10
0
/**
 * \brief Outputs a character on the UART line.
 *
 * \note This function uses polling.
 *       Use UART_SetTimeout() to set a timeout.
 * \param c  Character to send.
 * \return -1 if timed out, 1 otherwise
 */
extern int UART_Put( uint8_t c )
{
    Uart *pUart=UART_UART ;
	uint32_t started;

    if ( !_ucIsConsoleInitialized )
        UART_Configure(UART_BAUDRATE, BOARD_MCK);

    /* Wait for the transmitter to be ready */
	started = timetick();
    while ( (pUart->UART_SR & UART_SR_TXRDY) == 0 ) 
		if ((timetick() - started) > _xferTimeout)
			return -1;

    /* Send character */
    pUart->UART_THR=c ;
	return 1;

}
Exemplo n.º 11
0
/**
 * \brief Initializes the UartDma structure and the corresponding UART & DMA .
 * hardware select value.
 * The driver will uses DMA channel 0 for RX and DMA channel 1 for TX.
 * The DMA channels are freed automatically when no UART command processing.
 *
 * \param pUartd    Pointer to a UartDma instance.
 * \param pUartHw   Associated UART peripheral.
 * \param uartId    UART peripheral identifier.
 * \param uartMode  UART peripheral identifier.*
 * \param baud      UART baud rate
 * \param clk       UART ref clock
 * \param pXdmad    Pointer to a Dmad instance. 
 */
uint32_t UARTD_Configure( UartDma *pUartd ,
		uint8_t uartId,
		uint32_t uartMode,
		uint32_t baud,
		uint32_t clk)
{
	/* Enable the peripheral clock in the PMC*/
	PMC_EnablePeripheral( uartId );
	
	/* Initialize the UART structure */
	pUartd->uartId  = uartId;
	
	if (uartId == ID_UART0)
	  pUartd->pUartHw = UART0;
	if (uartId == ID_UART1)
	  pUartd->pUartHw = UART1;
	if (uartId == ID_UART2)
	  pUartd->pUartHw = UART2;
	if (uartId == ID_UART3)
	  pUartd->pUartHw = UART3;
	if (uartId == ID_UART4)
	  pUartd->pUartHw = UART4;
	
	pUartd->pXdmad->pXdmacs = XDMAC;

	/* Enable the UART Peripheral ,Execute a software reset of the UART, 
		Configure UART in Master Mode*/
	UART_Configure ( pUartd->pUartHw, uartMode, baud, clk);
	
	/* Driver initialize */
	XDMAD_Initialize(  pUartd->pXdmad, 0 );
	
	/* Check if DMA IRQ is enable; if not clear pending IRQs in init it */
	if(!(NVIC_GetActive(XDMAC_IRQn))) {
	  NVIC_ClearPendingIRQ(XDMAC_IRQn);
	}
	return 0;
}
Exemplo n.º 12
0
//*****************************************************************************
//
// main() -- Execution Starts Here
//
//*****************************************************************************
int main(void)
{
    // Enable lazy stacking for interrupt handlers.  This allows floating-point
    // instructions to be used within interrupt handlers, but at the expense of
    // extra stack usage.
    ROM_FPULazyStackingEnable ();

    // Set the clocking to run directly from the crystal.
    SysCtlClockSet(
        SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN
        | SYSCTL_XTAL_16MHZ);

    // Initialize the I/O Ports (pins)
    PortFunctionInit();

    // Initialize the UART.
    UART_Configure();

    // Initialize the I2C3 port as a slave to the cRIO
    Slave_Configure();

    // Initialize the I2C1 port as master to Gyro
    Gyro_Configure();

    // Say Hello on debug port
    UARTprintf("\nReady.\n");

    int16_t     i16Data = 0;               // holds data read from a gyro register
    uint8_t     ui8Status = 0;             // holds data read from gyro status register
    uint32_t    ui32Iteration = 0;         // iterations counter used to average first N samples
    float       fAvgDrift = 0.0;           // average of 1st N samples, used as an offset to subtract
    int16_t     i16MaxDrift = 0.0;
    int16_t     i16MinDrift = 0.0;
    float       fRate = 0.0;               // rate of change in Z in degs/sec
    float       fAngleDegrees = 0.0;       // running Z orientation in degrees
    uint16_t    ui16AngleScaled = 0.0;     // running Z orientation scaled to LSB = 0.01 degrees (i.e. value = degrees * 100)

    // Configure the L3G4200D Gyro chip
    Gyro_RegWrite(GYRO_CTRL_REG5, 0x80);     //BOOT:1 (reboot memory)
    SysCtlDelay(SysCtlClockGet() / 200);     //delay ~1/200 sec
    Gyro_RegWrite(GYRO_CTRL_REG1, 0x0C);     //DR:00 (100 Hz) PD:1 (enabled) XYZen:100 (Z only enabled)
    SysCtlDelay(SysCtlClockGet() / 200);     //delay ~1/200 sec
    Gyro_RegWrite(GYRO_CTRL_REG4, 0x10);     //FS:01 (500dps)
    SysCtlDelay(SysCtlClockGet() / 200);     //delay ~1/200 sec
    Gyro_RegWrite(GYRO_CTRL_REG5, 0x42);     //FIFO_EN:1 (enable FIFO) OUT_SEL:10 (LPF1+LPF2)
    SysCtlDelay(SysCtlClockGet() / 200);     //delay ~1/200 sec
    Gyro_RegWrite(GYRO_FIFO_CTRL_REG, 0x4F); //FM:010 (Stream Mode), WTM:01111
    SysCtlDelay(SysCtlClockGet() / 200);     //delay ~1/200 sec


    //Main Gyro processing loop
    while (1)
    {
        ui8Status = Gyro_RegRead(GYRO_FIFO_SRC_REG);
        //UARTprintf("FIFO_SRC_REG: 0x%x\n", ui8Status);

        // if Gyro's FIFO is not empty
        if (!(ui8Status & 0x20))
        {
            //report the depth of FIFO:
            //if (DEBUG_MODE) UARTprintf("N:0x%02x : ", ui8Status & 0x1F);

            // Read X,Y, and Z values.
            // NOTE: Found that the FIFo does not empty unless all 3 are read
            // TODO: Create a Gyro_RegRead6() to read all 3 values at once
            // Cast from unsigned16 to signed16
            i16Data = (int16_t) Gyro_RegRead2(GYRO_OUT_X_L);  // read and throw away value
            i16Data = (int16_t) Gyro_RegRead2(GYRO_OUT_Y_L);  // read and throw away value
            i16Data = (int16_t) Gyro_RegRead2(GYRO_OUT_Z_L);  // read and keep value

            //Average the first 100 iterations
            if (ui32Iteration < 500)
            {

                //Note min/max
                if (i16Data < i16MinDrift) i16MinDrift = i16Data;
                if (i16Data > i16MaxDrift) i16MaxDrift = i16Data;

                // compute the running average by taking (average * iteration), which is the sum of all prior iterations,
                // adding this iteration's value, and dividing by (iteration+1).
                fAvgDrift = ( (fAvgDrift * (float)ui32Iteration) + (float)i16Data ) / (float)(ui32Iteration + 1);
                ui32Iteration++;

                if (DEBUG_MODE)
                {
                    UARTprintf("Z: %d :", i16Data);
                    UARTprintf("A: %d :", (int16_t)(fAvgDrift * 100.0));
                    UARTprintf("m: %d :", i16MinDrift);
                    UARTprintf("M: %d\n", i16MaxDrift);
                }
            }
            else
            {
                //remove static drift
                fRate = (float)i16Data - fAvgDrift;

                //threshold filter (ignore samples below a threshold)
                if ( (-THRESHOLD_FILTER < fRate) && (fRate < THRESHOLD_FILTER) )
                {
                    fRate = 0.0;
                }

                // Convert rate to ( degrees / sec), scale by 1/SAMPLE_RATE_HZ, and sum into degrees
                fAngleDegrees += (fRate * GYRO_GAIN / SAMPLE_RATE_HZ) / 2.0;

                // wrap angle to be between 0 and 360
                //TODO: is there a better way to do this??
                while (fAngleDegrees < 0.0) fAngleDegrees += 360.0;
                while (fAngleDegrees >= 360.0) fAngleDegrees -= 360.0;

                // scale value to LSB = 0.01 degrees (i.e. value = degrees * 100)
                ui16AngleScaled = (uint16_t)(fAngleDegrees * 100.0);

                // send scaled value out slave i2c port
                Slave_SetLatestValue( ui16AngleScaled );

                if (DEBUG_MODE)
                {
                    UARTprintf("Z: %d : ", ui16AngleScaled);
                    UARTprintf("D: %d\n", (int16_t)(fAvgDrift * 100.0));
                }

            }

            LED_DoBlink();
        }
    }

}
Exemplo n.º 13
0
/**
 * \brief  Applet main entry. This function decodes received command and executes it.
 *
 * \param argc  always 1
 * \param argv  Address of the argument area..
 */
int main(int argc, char **argv)
{
    struct _Mailbox *pMailbox = (struct _Mailbox *) argv;

    uint32_t bytesToWrite, bufferAddr, memoryOffset;
    uint32_t l_start, l_end;
    uint32_t *pActualStart = NULL;
    uint32_t *pActualEnd = NULL;
    uint8_t error ;
    uint32_t writeSize;
    /* Save info of communication link */
    comType = pMailbox->argument.inputInit.comType;

    /*----------------------------------------------------------
     * INIT:
     *----------------------------------------------------------*/
    if (pMailbox->command == APPLET_CMD_INIT) {
        /*  Re-configurate UART   (MCK maybe change in LowLevelInit())  */
        UART_Configure(115200, BOARD_MCK);

        /* Set 6 WS for internal Flash writing (refer to errata) */
        EFC_SetWaitState(EFC0, 6);
#if defined (EFC1)
        EFC_SetWaitState(EFC1, 6);
#endif

#if (DYN_TRACES == 1)
        dwTraceLevel = pMailbox->argument.inputInit.traceLevel;
#endif

        flashBaseAddr       = IFLASH_ADDR;
        flashBaseAddrInit   = IFLASH_ADDR;
        flashSize           = IFLASH_SIZE;
        flashPageSize       = IFLASH_PAGE_SIZE;
        flashNbLockBits     = IFLASH_NB_OF_LOCK_BITS;
        flashLockRegionSize = IFLASH_LOCK_REGION_SIZE;

        TRACE_INFO("-- Internal Flash SAM-BA Applet %s --\n\r", SAM_BA_APPLETS_VERSION);
        TRACE_INFO("-- %s\n\r", BOARD_NAME);
        TRACE_INFO("-- Compiled: %s %s --\n\r", __DATE__, __TIME__);

        /* Initialize flash driver */
        FLASHD_Initialize(BOARD_MCK, 1);

        /* flash accesses must be 4 bytes aligned */
        pMailbox->argument.outputInit.bufferAddress = ((uint32_t) &end);

        bufferSize = IRAM_SIZE                            /* sram size */
                     - ( ((uint32_t) &end) - IRAM_ADDR )  /* program size (romcode, code+data) */
                     - STACK_SIZE;                         /* stack size at the end */
        /* integer number of pages can be contained in each buffer */
        /* operation is : buffersize -= bufferSize % flashPageSize */
        /* modulo can be done with a mask since flashpagesize is a power of two integer */
        bufferSize = bufferSize > SECTOR_SIZE ? SECTOR_SIZE: bufferSize;
        pMailbox->argument.outputInit.bufferSize = bufferSize;

        pMailbox->argument.outputInit.memorySize = flashSize;
        pMailbox->argument.outputInit.memoryInfo.lockRegionSize = flashLockRegionSize;
        pMailbox->argument.outputInit.memoryInfo.numbersLockBits = flashNbLockBits;

        TRACE_INFO("bufferSize : %d  bufferAddr: 0x%x \n\r",
               (int)pMailbox->argument.outputInit.bufferSize,
               (unsigned int) &end );

        TRACE_INFO("memorySize : %d lockRegionSize : 0x%x numbersLockBits : 0x%x \n\r",
               (int)pMailbox->argument.outputInit.memorySize,
               (unsigned int)pMailbox->argument.outputInit.memoryInfo.lockRegionSize,
               (unsigned int)pMailbox->argument.outputInit.memoryInfo.numbersLockBits);

        pMailbox->status = APPLET_SUCCESS;
    }

    /*----------------------------------------------------------
     * WRITE:
     *----------------------------------------------------------*/
    else if (pMailbox->command == APPLET_CMD_WRITE) {

        memoryOffset  = pMailbox->argument.inputWrite.memoryOffset;
        bufferAddr    = pMailbox->argument.inputWrite.bufferAddr;
        bytesToWrite  = pMailbox->argument.inputWrite.bufferSize;

        TRACE_INFO("WRITE at offset: 0x%x buffer at : 0x%x of: 0x%x Bytes (flash base addr : 0x%x)\n\r",
                   (unsigned int)memoryOffset, (unsigned int)bufferAddr,
                   (unsigned int)bytesToWrite, (unsigned int)flashBaseAddr);

        /* Check the giving sector have been locked before. */
        if (FLASHD_IsLocked(flashBaseAddr + memoryOffset, flashBaseAddr + memoryOffset + bytesToWrite) != 0) {

            TRACE_INFO("Error page locked\n\r");
            pMailbox->argument.outputWrite.bytesWritten = 0;
            pMailbox->status = APPLET_PROTECT_FAIL;
            goto exit;
        }

        if (memoryOffset % SECTOR_SIZE) {
            writeSize = SECTOR_SIZE - memoryOffset % SECTOR_SIZE;
            writeSize = writeSize > bufferSize ? bufferSize : writeSize;
        } else {
            writeSize = bytesToWrite;
        }
        TRACE_INFO("Write <%x> bytes from <#%x> \n\r", (unsigned int )writeSize, (unsigned int )memoryOffset );
        
        l_start = memoryOffset / SECTOR_SIZE;
        if ((memoryOffset % SECTOR_SIZE == 0) || 
              ((memoryOffset % SECTOR_SIZE != 0) && ( memoryOffset != (lastWrittenAddr + 1)))) {
            TRACE_INFO("Erase sector <#%d> \n\r", (unsigned int )l_start );
            TRACE_INFO("Unlock from <#%x> to <#%x> for sector erasing \n\r", 
                (unsigned int )(l_start * SECTOR_SIZE), (unsigned int)((l_start + 1) * SECTOR_SIZE -1));
            FLASHD_Unlock(flashBaseAddr + l_start * SECTOR_SIZE, flashBaseAddr + (l_start + 1) * SECTOR_SIZE -1 , pActualStart, pActualEnd);
#if defined (EFC1)
            if (memoryOffset < SECTOR_SIZE) {
                FLASHD_EraseSector(IFLASH_ADDR);             /* Small sector 0: 8K */
                FLASHD_EraseSector(IFLASH_ADDR + 1024 * 8);  /* Small sector 1: 8K */
                FLASHD_EraseSector(IFLASH_ADDR + 1024 * 16); /* Large sector  : 48K */

            } else if ((memoryOffset >= IFLASH_SIZE / 2) && (memoryOffset < (IFLASH_SIZE / 2 + SECTOR_SIZE))) {
                FLASHD_EraseSector(IFLASH_ADDR + IFLASH_SIZE / 2); /* Small sector 0: 8K */
                FLASHD_EraseSector(IFLASH_ADDR + IFLASH_SIZE / 2 + 1024 * 8);  /* Small sector 1: 8K */
                FLASHD_EraseSector(IFLASH_ADDR + IFLASH_SIZE / 2 + 1024 * 16); /* Large sector  : 48K */
            } else {
                /* Common erase Other sectors */
                FLASHD_EraseSector(flashBaseAddr + memoryOffset);
            }
#else
            if (memoryOffset < SECTOR_SIZE) {
                FLASHD_EraseSector(IFLASH_ADDR);             /* Small sector 0: 8K */
                FLASHD_EraseSector(IFLASH_ADDR + 1024 * 8);  /* Small sector 1: 8K */
                FLASHD_EraseSector(IFLASH_ADDR + 1024 * 16); /* Large sector  : 48K */
            } else {
                /* Common erase Other sectors */
                FLASHD_EraseSector(flashBaseAddr + memoryOffset);
            }
#endif
        }
        /* Write data */
        if (FLASHD_Write(flashBaseAddr + memoryOffset, (const void *)bufferAddr, writeSize) != 0) {

            TRACE_INFO("Error write operation\n\r");
            pMailbox->argument.outputWrite.bytesWritten = writeSize ;
            pMailbox->status = APPLET_WRITE_FAIL;
            goto exit;
        }
        lastWrittenAddr = memoryOffset + writeSize - 1;
        TRACE_INFO("Write achieved\n\r");
        pMailbox->argument.outputWrite.bytesWritten = writeSize;
        pMailbox->status = APPLET_SUCCESS;
    }

    /*----------------------------------------------------------
     * READ:
     *----------------------------------------------------------*/
    else if (pMailbox->command == APPLET_CMD_READ) {

        memoryOffset = pMailbox->argument.inputRead.memoryOffset;
        bufferAddr   = pMailbox->argument.inputRead.bufferAddr;
        bufferSize   = pMailbox->argument.inputRead.bufferSize;
        TRACE_INFO("READ at offset: 0x%x buffer at : 0x%x of: 0x%x Bytes (flash base addr : 0x%x)\n\r",
                   (unsigned int)memoryOffset, (unsigned int)bufferAddr,
                   (unsigned int)bufferSize,
                   (unsigned int)flashBaseAddr);

        /* read data */
        memcpy((void *)bufferAddr, (void *)(flashBaseAddr + memoryOffset), bufferSize);
        /* workaound for sam4s-xpld */
        memcpy((void *)bufferAddr, (void *)(flashBaseAddr + memoryOffset), bufferSize);
        TRACE_INFO("Read achieved\n\r");
        pMailbox->argument.outputRead.bytesRead = bufferSize;
        pMailbox->status = APPLET_SUCCESS;
    }

    /*----------------------------------------------------------
     * FULL ERASE:
     *----------------------------------------------------------*/
    else if (pMailbox->command == APPLET_CMD_FULL_ERASE) {
        TRACE_INFO("FULL ERASE command \n\r");

        /* Check if at least one page has been locked */
        if (FLASHD_IsLocked(flashBaseAddr, flashBaseAddr + flashSize) != 0) {

            TRACE_INFO("Error page locked \n\r");
            pMailbox->status = APPLET_PROTECT_FAIL;
            goto exit;
        }
        /* Implement the erase all command */
        /* Erase the flash */
        if (FLASHD_Erase(flashBaseAddr) != 0) {

            TRACE_INFO("Flash erase failed! \n\r");
            pMailbox->status = APPLET_ERASE_FAIL;
            goto exit;
        }
#if defined (EFC1)
        /* Erase the flash1 */
        if (FLASHD_Erase(flashBaseAddr + flashSize / 2) != 0) {

            TRACE_INFO("Full erase failed! \n\r");
            pMailbox->status = APPLET_ERASE_FAIL;
            goto exit;
        }
#endif
        lastWrittenAddr = 0;
        TRACE_INFO("Full erase achieved\n\r");

        pMailbox->status = APPLET_SUCCESS;
    }
    /*----------------------------------------------------------
     * LOCK SECTOR:
     *----------------------------------------------------------*/
    else if (pMailbox->command == APPLET_CMD_LOCK) {
        TRACE_INFO("LOCK command \n\r");
        l_start = (pMailbox->argument.inputLock.sector * flashLockRegionSize) + flashBaseAddr;
        l_end = l_start + flashLockRegionSize;

        if( FLASHD_Lock(l_start, l_end, pActualStart, pActualEnd) != 0) {

            TRACE_INFO("Lock failed! \n\r");
            pMailbox->status = APPLET_PROTECT_FAIL;
            goto exit;
        }

        TRACE_INFO("Lock sector achieved\n\r");

        pMailbox->status = APPLET_SUCCESS;
    }

    /*----------------------------------------------------------
     * UNLOCK SECTOR:
     *----------------------------------------------------------*/
    else if (pMailbox->command == APPLET_CMD_UNLOCK) {
        TRACE_INFO("UNLOCK command \n\r");
        l_start = (pMailbox->argument.inputLock.sector * flashLockRegionSize) + flashBaseAddr;
        l_end = l_start + flashLockRegionSize;
        
        if( FLASHD_Unlock(l_start, l_end, pActualStart, pActualEnd) != 0) {

            TRACE_INFO("Unlock failed! \n\r");
            pMailbox->status = APPLET_UNPROTECT_FAIL;
            goto exit;
        }

        TRACE_INFO("Unlock sector achieved\n\r");
        pMailbox->status = APPLET_SUCCESS;
    }

    /*----------------------------------------------------------
     * GPNVM :
     *----------------------------------------------------------*/
    else if (pMailbox->command == APPLET_CMD_GPNVM) {
        if( pMailbox->argument.inputGPNVM.action == 0) {
            TRACE_INFO("DEACTIVATES GPNVM command \n\r");
            error = FLASHD_ClearGPNVM(pMailbox->argument.inputGPNVM.bitsOfNVM);
        }
        else {
            TRACE_INFO("ACTIVATES GPNVM command \n\r");
            error = FLASHD_SetGPNVM(pMailbox->argument.inputGPNVM.bitsOfNVM);
        }

        if(error != 0) {
            TRACE_INFO("GPNVM failed! \n\r");
            pMailbox->status = APPLET_FAIL;
            goto exit;
        }

        TRACE_INFO("GPNVM achieved\n\r");
        pMailbox->status = APPLET_SUCCESS;
    }

    /*----------------------------------------------------------
     * READ UNIQUE ID :
     *----------------------------------------------------------*/
    else if (pMailbox->command == APPLET_CMD_READ_UNIQUE_ID) {
        TRACE_INFO("READ UNIQUE ID command \n\r");

        if (FLASHD_ReadUniqueID ((uint32_t *)(pMailbox->argument.inputReadUniqueID.bufferAddr)) != 0) {
            TRACE_INFO("Read Unique ID failed! \n\r");
            pMailbox->status = APPLET_FAIL;
            goto exit;
        }

        TRACE_INFO("Read Unique ID achieved\n\r");
        pMailbox->status = APPLET_SUCCESS;
    }

exit:
    /* Acknowledge the end of command */
    TRACE_INFO("\tEnd of Applet %x %x.\n\r",
              (unsigned int)pMailbox->command,
              (unsigned int)pMailbox->status);
    /* Notify the host application of the end of the command processing */
    pMailbox->command = ~(pMailbox->command);

    if (comType == DBGU_COM_TYPE) {
        UART_PutChar(0x6);
    }

    return 0;
}
Exemplo n.º 14
0
/**
 * \brief  Applet main entry. This function decodes received command and executes it.
 *
 * \param argc  always 1
 * \param argv  Address of the argument area..
 */
int main(int argc, char **argv)
{
    struct _Mailbox *pMailbox = (struct _Mailbox *) argv;
    uint32_t bufferSize, bufferAddr, memoryOffset, bytesToWrite;
    uint16_t pagesToWrite, pagesToRead;
    uint32_t bytesWritten = 0;
    uint32_t bytesRead = 0;
    uint32_t nbBadBlocks = 0;
    uint32_t nbBlocks = 0;
    /* Temporary buffer used for non block aligned read / write */
    uint32_t tempBufferAddr;
    uint16_t block, page, offset, i;
    uint8_t unAlignedPage;
    /* Index in source buffer during buffer copy */
    uint32_t offsetInSourceBuff;
    /* Index in destination buffer during buffer copy */
    uint32_t offsetInTargetBuff;
    /* Errors returned by SkipNandFlash functions */
    uint8_t error = 0;
    
    /* Global DMA driver instance for all DMA transfers in application. */
    sDmad dmad;

    /*----------------------------------------------------------
     * INIT:
     *----------------------------------------------------------*/
    if (pMailbox->command == APPLET_CMD_INIT) {
        /* Save info of communication link */
        comType = pMailbox->argument.inputInit.comType;

        /*  Re-configurate UART   (MCK maybe change in LowLevelInit())  */
        UART_Configure(115200, BOARD_MCK);

#if (DYN_TRACES == 1)
        dwTraceLevel = pMailbox->argument.inputInit.traceLevel;
#endif

        TRACE_INFO("-- NandFlash SAM-BA applet %s --\n\r", SAM_BA_APPLETS_VERSION);
        TRACE_INFO("-- %s\n\r", BOARD_NAME);
        TRACE_INFO("-- Compiled: %s %s --\n\r", __DATE__, __TIME__);
        TRACE_INFO("INIT command\n\r");

        /* Initialize DMA driver instance with polling mode */
        DMAD_Initialize( &dmad, 1 );
        if ( NandFlashConfigureDmaChannels( &dmad ))
        {
            TRACE_INFO ("-E- Initialize DMA failed !");
        }
        TRACE_INFO ("-I- Initialize DMA done.\n\r");

        /* Configure SMC for Nandflash accesses */
        BOARD_ConfigureNandFlash(SMC);
        PIO_PinConfigure(pPinsNf, PIO_LISTSIZE(pPinsNf));

        if (pPinsNf->pio == 0) {
            pMailbox->status = APPLET_NO_DEV;
            pMailbox->argument.outputInit.bufferSize = 0;
            pMailbox->argument.outputInit.memorySize = 0;
            pMailbox->argument.outputInit.bufferAddress = (uint32_t) &end;
            TRACE_INFO("INIT command: No Nandflash defined for this board\n\r");
        }
        else {

            memset(&skipBlockNf, 0, sizeof(skipBlockNf));

            if (SkipBlockNandFlash_Initialize(&skipBlockNf,
                                              0,
                                              cmdBytesAddr,
                                              addrBytesAddr,
                                              dataBytesAddr,
                                              nfCePin,
                                              nfRbPin)) {

                pMailbox->status = APPLET_DEV_UNKNOWN;
                pMailbox->argument.outputInit.bufferSize = 0;
                pMailbox->argument.outputInit.memorySize = 0;
                TRACE_INFO("\tDevice Unknown\n\r");
            }
            else {

                TRACE_INFO("\tNandflash driver initialized\n\r");

                /* Get device parameters */
                memSize = NandFlashModel_GetDeviceSizeInBytes(&skipBlockNf.ecc.raw.model);
                blockSize = NandFlashModel_GetBlockSizeInBytes(&skipBlockNf.ecc.raw.model);
                numBlocks = NandFlashModel_GetDeviceSizeInBlocks(&skipBlockNf.ecc.raw.model);
                pageSize = NandFlashModel_GetPageDataSize(&skipBlockNf.ecc.raw.model);
                numPagesPerBlock = NandFlashModel_GetBlockSizeInPages(&skipBlockNf.ecc.raw.model);
                latestErasedBlock = 0xFFFF;

                pMailbox->status = APPLET_SUCCESS;
                pMailbox->argument.outputInit.bufferAddress = (uint32_t)EBI_SDRAMC_ADDR;
                pMailbox->argument.outputInit.bufferSize = blockSize;

                pMailbox->argument.outputInit.memorySize = memSize;
                TRACE_INFO("\t pageSize : 0x%x blockSize : 0x%x blockNb : 0x%x \n\r",  
                            (unsigned int)pageSize, (unsigned int)blockSize, (unsigned int)numBlocks);
            }
        }
    }

    /*----------------------------------------------------------
     * WRITE:
     *----------------------------------------------------------*/
    else if (pMailbox->command == APPLET_CMD_WRITE) {

        memoryOffset = pMailbox->argument.inputWrite.memoryOffset;
        bufferAddr = pMailbox->argument.inputWrite.bufferAddr;
        tempBufferAddr = bufferAddr + blockSize;
        bytesToWrite = pMailbox->argument.inputWrite.bufferSize;
        unAlignedPage = 0;
        TRACE_INFO("WRITE arguments : offset 0x%x, buffer at 0x%x, of 0x%x Bytes\n\r",
               (unsigned int)memoryOffset, (unsigned int)bufferAddr, (unsigned int)bytesToWrite);

        pMailbox->argument.outputWrite.bytesWritten = 0;

        /* Check word alignment */
        if (memoryOffset % 4) {

            pMailbox->status = APPLET_ALIGN_ERROR;
            goto exit;
        }

        /* Retrieve page and block addresses */
        if (NandFlashModel_TranslateAccess(&(skipBlockNf.ecc.raw.model),
                                           memoryOffset,
                                           bytesToWrite,
                                           &block,
                                           &page,
                                           &offset)) {
            pMailbox->status = APPLET_FAIL;
            goto exit;
        }
        if (block != latestErasedBlock ){
            /* Erase target block */
            error = SkipBlockNandFlash_EraseBlock(&skipBlockNf, block, NORMAL_ERASE);
            if (error == NandCommon_ERROR_BADBLOCK) {
                pMailbox->status = APPLET_BAD_BLOCK;
                goto exit;
            }
            if (error) {
                pMailbox->status = APPLET_FAIL;
                goto exit;
            }
            latestErasedBlock = block;
            latestErasedPage = 0xff;
        }
        if (page <= ((uint8_t)(latestErasedPage + 1))){
            error = SkipBlockNandFlash_EraseBlock(&skipBlockNf, block, NORMAL_ERASE);
            if (error == NandCommon_ERROR_BADBLOCK) {
                pMailbox->status = APPLET_BAD_BLOCK;
                goto exit;
            }
            if (error) {
                pMailbox->status = APPLET_FAIL;
                goto exit;
            }
            latestErasedBlock = block;
            latestErasedPage = page;
        }

        offsetInSourceBuff = 0;
        TRACE_INFO("WRITE at block 0x%x, page 0x%x, offset 0x%x in page \n\r", 
                    (unsigned int)block, (unsigned int)page, (unsigned int)offset);
        if (offset ) {

            /* We are not page aligned. */
            offsetInTargetBuff = offset;
            memset((uint32_t *)tempBufferAddr, 0xFF, pageSize);
            while (offsetInTargetBuff < pageSize) {

                *(uint32_t *)(tempBufferAddr + offsetInTargetBuff) = *(uint32_t *)(bufferAddr + offsetInSourceBuff);
                offsetInSourceBuff += 4;
                offsetInTargetBuff += 4;
                bytesWritten += 4;
            }
            error = SkipBlockNandFlash_WritePage(&skipBlockNf, block, page, ( void *)tempBufferAddr ,0);
            if (error == NandCommon_ERROR_BADBLOCK) {
                pMailbox->status = APPLET_BAD_BLOCK;
                goto exit;
            }
            if (error) {
                pMailbox->status = APPLET_FAIL;
                goto exit;
            }
            unAlignedPage++;
        }

        pagesToWrite = (bytesToWrite - bytesWritten) / pageSize;
        if (bytesToWrite - ((pagesToWrite + unAlignedPage)  * pageSize)) {
            pagesToWrite++;
        }
        if ((pagesToWrite + page ) > numPagesPerBlock)
        {
            pagesToWrite = numPagesPerBlock - page;
        }
        /* Write target block */
        error = SkipBlockNandFlash_WriteBlockUnaligned(&skipBlockNf, block, (page + unAlignedPage), pagesToWrite, ( void *)(bufferAddr + offsetInSourceBuff));
        bytesWritten += pagesToWrite * pageSize;
        if (bytesWritten > bytesToWrite) {
            bytesWritten = bytesToWrite;
        }
        if (error == NandCommon_ERROR_BADBLOCK) {

            pMailbox->status = APPLET_BAD_BLOCK;
            goto exit;
        }
        if (error) {

            pMailbox->status = APPLET_FAIL;
            goto exit;
        }

        pMailbox->argument.outputWrite.bytesWritten = bytesWritten;
        pMailbox->status = APPLET_SUCCESS;
    }

    /*----------------------------------------------------------
     * READ:
     *----------------------------------------------------------*/
    else if (pMailbox->command == APPLET_CMD_READ) {

        memoryOffset = pMailbox->argument.inputRead.memoryOffset;
        bufferAddr   = pMailbox->argument.inputRead.bufferAddr;
        tempBufferAddr = bufferAddr + blockSize;

        bufferSize   = pMailbox->argument.inputRead.bufferSize;

        TRACE_INFO("READ at offset: 0x%x buffer at : 0x%x of: 0x%x Bytes\n\r",
               (unsigned int)memoryOffset, (unsigned int)bufferAddr, (unsigned int)bufferSize);

        pMailbox->argument.outputRead.bytesRead = 0;
        unAlignedPage = 0;
        /* Check word alignment */

        if (memoryOffset % 4) {

            pMailbox->status = APPLET_ALIGN_ERROR;
            goto exit;
        }

        /* Retrieve page and block addresses */
        if (NandFlashModel_TranslateAccess(&(skipBlockNf.ecc.raw.model),
                                           memoryOffset,
                                           bufferSize,
                                           &block,
                                           &page,
                                           &offset)) {
            pMailbox->status = APPLET_FAIL;
            goto exit;
        }

        TRACE_INFO("READ at block 0x%x, page 0x%x, offset in page 0x%x\n\r", 
        (unsigned int)block, (unsigned int)page, (unsigned int)offset);
        offsetInTargetBuff = 0;
        if (offset) {
            memset((uint32_t *)tempBufferAddr, 0xFF, pageSize);
            error = SkipBlockNandFlash_ReadPage(&skipBlockNf, block, page, ( void *)tempBufferAddr ,0);
            if (error == NandCommon_ERROR_BADBLOCK) {
                pMailbox->status = APPLET_BAD_BLOCK;
                goto exit;
            }
            if (error) {
                pMailbox->status = APPLET_FAIL;
                goto exit;
            }

            /* Fill dest buffer with read data */
            offsetInSourceBuff = offset;
            while (offsetInSourceBuff < pageSize) {
                *(uint32_t *)(bufferAddr + offsetInTargetBuff) = *(uint32_t *)(tempBufferAddr + offsetInSourceBuff);
                offsetInSourceBuff += 4;
                offsetInTargetBuff += 4;
                bytesRead += 4;
            }
            unAlignedPage++;
        }
        pagesToRead = (bufferSize - bytesRead) / pageSize;
        if (bufferSize - ((pagesToRead + unAlignedPage)* pageSize)) {
            pagesToRead++;
        }
        if ((pagesToRead + page ) > numPagesPerBlock)
        {
            pagesToRead = numPagesPerBlock - page;
        }
        /* Read target block */
        error = SkipBlockNandFlash_ReadBlockUnaligned(&skipBlockNf, block, (page + unAlignedPage), pagesToRead, ( void *)(bufferAddr + offsetInTargetBuff));
        bytesRead += pagesToRead * pageSize;
        if (bytesRead > bufferSize) {
            bytesRead = bufferSize;
        }
        if (error == NandCommon_ERROR_BADBLOCK) {

            pMailbox->status = APPLET_BAD_BLOCK;
            goto exit;
        }
        if (error) {
            pMailbox->status = APPLET_FAIL;
            goto exit;
        }

        pMailbox->argument.outputRead.bytesRead = bytesRead;
        pMailbox->status = APPLET_SUCCESS;
    }

    /*----------------------------------------------------------
     * FULL ERASE:
     *----------------------------------------------------------*/
    else if (pMailbox->command == APPLET_CMD_FULL_ERASE) {

        TRACE_INFO("FULL ERASE command\n\r");
        TRACE_INFO("\tForce erase flag: 0x%x\n\r", (unsigned int)pMailbox->argument.inputFullErase.eraseType);

        for (i = 0; i < numBlocks; i++) {

            /* Erase the page */
            if (SkipBlockNandFlash_EraseBlock(&skipBlockNf, i, pMailbox->argument.inputFullErase.eraseType)) {

                TRACE_INFO("Found block #%d BAD, skip it\n\r", (unsigned int)i);
            }
        }

        TRACE_INFO("Full Erase achieved\n\r");
        pMailbox->status = APPLET_SUCCESS;
    }

    /*----------------------------------------------------------
     * BATCH FULL ERASE:
     *----------------------------------------------------------*/
    else if (pMailbox->command == APPLET_CMD_BATCH_ERASE) {

        TRACE_INFO("BATCH ERASE command\n\r");
        block = pMailbox->argument.inputBatchErase.batch * (numBlocks / ERASE_BATCH);

        TRACE_INFO("Erase block from #%d to #%d\n\r", (unsigned int)block, (unsigned int)(block + (numBlocks / ERASE_BATCH)));
        for (i = block ; i < block + (numBlocks / ERASE_BATCH) ; i++) {

            /* Erase the block */
            if (SkipBlockNandFlash_EraseBlock(&skipBlockNf, i, pMailbox->argument.inputBatchErase.eraseType)) {

                TRACE_INFO("Found block #%d BAD, skip it\n\r", (unsigned int)i);
            }
        }

        if ((pMailbox->argument.inputBatchErase.batch + 1) == ERASE_BATCH) {
            TRACE_INFO("Full Erase achieved, erase type is %d\n\r", (unsigned int)pMailbox->argument.inputBatchErase.eraseType);
            pMailbox->argument.outputBatchErase.nextBatch = 0;
        }
        else {
            pMailbox->argument.outputBatchErase.nextBatch =  pMailbox->argument.inputBatchErase.batch + 1;
            TRACE_INFO("Batch Erase achieved\n\r");
        }
        pMailbox->status = APPLET_SUCCESS;
    }

    /*----------------------------------------------------------
     * ERASE_BLOCKS:
     *----------------------------------------------------------*/
    else if (pMailbox->command == APPLET_CMD_ERASE_BLOCKS) {

        TRACE_INFO("BLOCKS ERASE command\n\r");
        memoryOffset = pMailbox->argument.inputBlocksErase.memoryOffsetStart;
        if ((pMailbox->argument.inputBlocksErase.memoryOffsetEnd > memSize) || (pMailbox->argument.inputBlocksErase.memoryOffsetEnd < memoryOffset) ) {
            TRACE_INFO("Out of memory space\n\r");
            pMailbox->status = APPLET_ERASE_FAIL;
            goto exit;
        }
        nbBlocks = ((pMailbox->argument.inputBlocksErase.memoryOffsetEnd- memoryOffset)/ blockSize) + 1;

        TRACE_INFO("Erase blocks from %d  to %d \n\r",  (unsigned int)(memoryOffset / blockSize),(unsigned int)((memoryOffset / blockSize)+ nbBlocks) );
        /* Erase blocks */
        for (i =  memoryOffset / blockSize; i < memoryOffset / blockSize + nbBlocks ; i++) {
            if (SkipBlockNandFlash_EraseBlock(&skipBlockNf,  i , NORMAL_ERASE)) {
                 TRACE_INFO("Found block #%d BAD, skip it\n\r",  (unsigned int)i);
            }
        }
        TRACE_INFO("Blocks Erase achieved\n\r");
        pMailbox->status = APPLET_SUCCESS;
    }

    /*----------------------------------------------------------
     * LIST BAD BLOCKS:
     *----------------------------------------------------------*/
    else if (pMailbox->command == APPLET_CMD_LIST_BAD_BLOCKS) {

        TRACE_INFO("LIST BAD BLOCKS command\n\r");

        nbBadBlocks = 0;
        bufferAddr = (uint32_t) &end;
        pMailbox->argument.outputListBadBlocks.bufferAddress = bufferAddr;

        for (i = 0; i < numBlocks; i++) {

            /* Erase the page */
            if (SkipBlockNandFlash_CheckBlock(&skipBlockNf, i) == BADBLOCK) {

                nbBadBlocks++;
                *((uint32_t *)bufferAddr) = i;
                bufferAddr += 4;
                TRACE_INFO("Found block #%d BAD\n\r", i);
            }
        }

        TRACE_INFO("LIST BAD BLOCKS achieved\n\r");

        pMailbox->argument.outputListBadBlocks.nbBadBlocks = nbBadBlocks;

        pMailbox->status = APPLET_SUCCESS;
    }

    /*----------------------------------------------------------
     * TAG BLOCK:
     *----------------------------------------------------------*/
    else if (pMailbox->command == APPLET_CMD_TAG_BLOCK) {

        TRACE_INFO("TAG BLOCK command\n\r");

        bufferAddr = (uint32_t) &end;
        block = pMailbox->argument.inputTagBlock.blockId;

        /* To tag the block as good, just erase it without bad block check */
        if ((uint8_t)pMailbox->argument.inputTagBlock.tag == 0xFF)
        {
            if (SkipBlockNandFlash_EraseBlock(&skipBlockNf, block, SCRUB_ERASE)) {

                TRACE_INFO("Cannot erase block %d\n\r", (unsigned int)block);
                pMailbox->status = APPLET_FAIL;
                goto exit;
            }
        }
        else {

            for (i = 0; i < 2; i++) {

                /* Start by reading the spare */
                memset((uint8_t *)bufferAddr, 0xFF, NandCommon_MAXSPAREECCBYTES);

                TRACE_INFO("Tag to write : 0x%x\n\r", (unsigned int)pMailbox->argument.inputTagBlock.tag);

                NandSpareScheme_WriteBadBlockMarker((struct NandSpareScheme *)(NandFlashModel_GetScheme((struct NandFlashModel *)(&skipBlockNf))),
                                                    (uint8_t *)bufferAddr,
                                                    ((uint8_t)pMailbox->argument.inputTagBlock.tag));

                if (RawNandFlash_WritePage((struct RawNandFlash *)(&skipBlockNf), block, i, 0, (uint8_t *)bufferAddr)) {

                    TRACE_ERROR("Failed to write spare data of page %d of block %d\n\r", i, block);
                    pMailbox->status = APPLET_FAIL;
                    goto exit;
                }
            }
        }

        TRACE_INFO("TAG BLOCK achieved\n\r");

        pMailbox->status = APPLET_SUCCESS;
    }

exit :
    /* Acknowledge the end of command */
    TRACE_INFO("\tEnd of applet (command : %x --- status : %x)\n\r", (unsigned int)pMailbox->command, (unsigned int)pMailbox->status);

    /* Notify the host application of the end of the command processing */
    pMailbox->command = ~(pMailbox->command);
    if (comType == DBGU_COM_TYPE) {
        UART_PutChar(0x6);
    }

    return 0;
}
Exemplo n.º 15
0
/**
 * \brief Applet code for initializing the external RAM.
 */
int main(int argc, char **argv)
{
    struct _Mailbox *pMailbox = (struct _Mailbox *) argv;
    uint32_t ramType = 0;
    uint32_t dataBusWidth = 0;
    uint32_t ddrModel = 0;
    uint32_t comType = 0;

    /* ---------------------------------------------------------- */
    /* INIT: */
    /* ---------------------------------------------------------- */
    if (pMailbox->command == APPLET_CMD_INIT) {
        /* Save info of communication link */
        comType = pMailbox->argument.inputInit.comType;

        /*  Re-configurate UART   (MCK maybe change in LowLevelInit())  */
        UART_Configure(115200, BOARD_MCK);

        ramType = pMailbox->argument.inputInit.ramType;
        dataBusWidth = pMailbox->argument.inputInit.dataBusWidth;
        ddrModel = pMailbox->argument.inputInit.ddrModel;

#if (DYN_TRACES == 1)
        dwTraceLevel = pMailbox->argument.inputInit.traceLevel;
#endif

        TRACE_INFO("-- EXTRAM Applet %s --\n\r", SAM_BA_APPLETS_VERSION);
        TRACE_INFO("-- %s\n\r", BOARD_NAME);
        TRACE_INFO("INIT command:\n\r");

        TRACE_INFO("\tCommunication link type : %d\n\r",(unsigned int)( pMailbox->argument.inputInit.comType));
        TRACE_INFO("\tData bus width : %d bits\n\r", (unsigned int)dataBusWidth);
        if (ramType == TYPE_SDRAM) {
           TRACE_INFO("\tExternal RAM type : %s\n\r", "SDRAM");
        }
        else {
            TRACE_INFO("\tUnknow External RAM type\n\r");
        }

        if (pMailbox->argument.inputInit.ramType == TYPE_SDRAM) {
            /* Configure SDRAM controller */
            TRACE_INFO("\tInit SDRAM...\n\r");
            BOARD_ConfigureSdram();
        }

        /* Test external RAM access */
        if (ExtRAM_TestOk()) {

            pMailbox->status = APPLET_SUCCESS;
            TRACE_INFO("\tInit successful.\n\r");
        }
        else {
            pMailbox->status = APPLET_FAIL;
            TRACE_INFO("\tInit failure.\n\r");
        }

        pMailbox->argument.outputInit.bufferAddress = ((uint32_t) &end);
        pMailbox->argument.outputInit.bufferSize = 0;
        pMailbox->argument.outputInit.memorySize = EXTRAM_SIZE;
    }

    /* Acknowledge the end of command */
    TRACE_INFO("\tEnd of applet (command : %x --- status : %x)\n\r", (unsigned int)(pMailbox->command), (unsigned int)(pMailbox->status));

    /* Notify the host application of the end of the command processing */
    pMailbox->command = ~(pMailbox->command);
    if (comType == DBGU_COM_TYPE) {
        UART_PutChar(0x6);
    }

    return 0;
}