Exemple #1
0
/****************************************************************************
  Function:
    bool DRV_SPI_Initialize(SpiChannel chn, SpiOpenFlags oFlags, unsigned int fpbDiv)

  Summary:
    This function initializes the SPI channel and also sets the brg register.

  Description:
    This function initializes the SPI channel and also sets the brg register.
 	The SPI baudrate BR is given by: BR=Fpb/(2*(SPIBRG+1))
 	The input parametes fpbDiv specifies the Fpb divisor term (2*(SPIBRG+1)),
 	so the BRG is calculated as SPIBRG=fpbDiv/2-1.

    
  Precondition:
    None

  Parameters:
	chn 	- the channel to set
	oFlags	- a SpiOpenFlags or __SPIxCONbits_t structure that sets the module behavior
	fpbDiv	- Fpb divisor to extract the baud rate: BR=Fpb/fpbDiv.


  Returns:
     true if success
     false otherwise

  Remarks:
    - The baud rate is always obtained by dividing the Fpb to an even number
      between 2 and 1024.
	- When selecting the number of bits per character, SPI_OPEN_MODE32 has the highest priority.
	  If SPI_OPEN_MODE32 is not set, then SPI_OPEN_MODE16 selects the character width.
 	- The SPI_OPEN_SSEN is taken into account even in master mode. If it is set the library
      will properly se the SS pin as an digital output.
  ***************************************************************************/	
bool DRV_SPI_Initialize(SpiChannel chn, SpiOpenFlags oFlags, unsigned int fpbDiv)
{
#if defined (__C32__)
    SpiChnOpen(chn, oFlags, fpbDiv);
#elif defined (__C30__) 
    volatile uint16_t con1 = 0; 
    uint16_t con2 = 0;
    uint16_t con3 = 0;
    uint8_t i;

    if((SYS_CLK_PeripheralClockGet()/fpbDiv) > 10000000ul)
    {
        SYS_ASSERT(false, "Requested SPI frequency is not supported!");
        return false;
        // the SPI clock is selected more than 10MHz.
        // Select the frequency as per the data sheet of the particular 16bit device.	
    }

    for(i = 0; i < SPI_CLK_TBL_ELEMENT_COUNT; i++)
    {
        if((SYS_CLK_PeripheralClockGet()/fpbDiv) <= SpiClkTbl[i].clock)
        {
            con1 = SpiClkTbl[i].scale;
            break;
        }
    }
	
	con1 |= oFlags;
	con3 |= SPI_EN;

    switch(chn)
    {
        case 1:
			OpenSPI1(con1,con2,con3); 
            break;
            
        case 2:
            SPI2STAT &= 0x7FFF;
            OpenSPI2(con1,con2,con3);
            break;

        default:
            SYS_ASSERT(false, "Requested SPI channel is not supported!");
            return false;
    }
#endif

    return true;
}
Exemple #2
0
bool USART_INIT(SYS_MODULE_ID port_id, uint32_t baud_rate) {
    USART_MODULE_CTRL *pusart_module_ctrl;
    USART_MODULE_ID module_id;

    if(port_id == SYS_MODULE_UART_1)
        module_id = USART1_ID;
    else if(port_id == SYS_MODULE_UART_2)
        module_id = USART2_ID;
//	else if(port_id == SYS_MODULE_UART_3)
//		module_id = USART_ID_3;
//	else if(port_id == SYS_MODULE_UART_4)
//		module_id = USART_ID_4;
//	else if(port_id == SYS_MODULE_UART_5)
//		module_id = USART_ID_5;
//	else if(port_id == SYS_MODULE_UART_6)
//		module_id = USART_ID_6;
    else
        return false;

    pusart_module_ctrl = &usart_module_ctrl[module_id];
    *(pusart_module_ctrl->UxBRG) = (((SYS_CLK_PeripheralClockGet())/(baud_rate)/_USART_BAUD_RATE_FACTOR) - 1);
    *(pusart_module_ctrl->UxMODE) = 0;
    ((USART_MODE_CTRL*)(pusart_module_ctrl->UxMODE))->BRGH = 1;
    *(pusart_module_ctrl->UxSTA) = 0;
    ((USART_MODE_CTRL*)(pusart_module_ctrl->UxMODE))->UARTEN = 1;
    ((USART_MODE_CTRL*)(pusart_module_ctrl->UxMODE))->STSEL = 0;
    ((USART_STAT_CTRL*)(pusart_module_ctrl->UxSTA))->UTXEN = 1;
#if defined (__PIC32MX__)
    ((USART_STAT_CTRL*)(pusart_module_ctrl->UxSTA))->URXEN = 1;
#endif
    ((USART_STAT_CTRL*)(pusart_module_ctrl->UxSTA))->OERR = 0;

    return true;
}
Exemple #3
0
void XEEInit(void)
{
    EEPROM_CS_IO = 1;
    EEPROM_CS_TRIS = 0;     // Drive SPI EEPROM chip select pin

    EEPROM_SCK_TRIS = 0;    // Set SCK pin as an output
    EEPROM_SDI_TRIS = 1;    // Make sure SDI pin is an input
    EEPROM_SDO_TRIS = 0;    // Set SDO pin as an output

    ClearSPIDoneFlag();
    #if defined(__C30__)
        EEPROM_SPICON1 = PROPER_SPICON1; // See PROPER_SPICON1 definition above
        EEPROM_SPICON2 = 0;
        EEPROM_SPISTAT = 0;    // clear SPI
        EEPROM_SPISTATbits.SPIEN = 1;
    #elif defined(__C32__)
        EEPROM_SPIBRG = (SYS_CLK_PeripheralClockGet()-1ul)/2ul/EEPROM_MAX_SPI_FREQ;
        EEPROM_SPICON1 = PROPER_SPICON1;
    #endif
}
Exemple #4
0
/*****************************************************************************
  Function:
    void SPIFlashInit(void)

  Description:
    Initializes SPI Flash module.

  Precondition:
    None

  Parameters:
    None

  Returns:
    None

  Remarks:
    This function is only called once during the lifetime of the application.

  Internal:
    This function sends WRDI to clear any pending write operation, and also
    clears the software write-protect on all memory locations.
  ***************************************************************************/
void SPIFlashInit(void)
{
	uint8_t i;
    volatile uint8_t Dummy;
    uint8_t vSPIONSave;
    #if defined(__C30__)
    uint16_t SPICON1Save;
    #elif defined(__C32__)
    uint32_t SPICON1Save;
    #endif

    SPIFLASH_CS_IO = 1;
    SPIFLASH_CS_TRIS = 0;   // Drive SPI Flash chip select pin

    SPIFLASH_SCK_TRIS = 0;  // Set SCK pin as an output
    SPIFLASH_SDI_TRIS = 1;  // Make sure SDI pin is an input
    SPIFLASH_SDO_TRIS = 0;  // Set SDO pin as an output

    // Save SPI state (clock speed)
    SPICON1Save = SPIFLASH_SPICON1;
    vSPIONSave = SPI_ON_BIT;

    // Configure SPI
    SPI_ON_BIT = 0;
    SPIFLASH_SPICON1 = PROPER_SPICON1;
    SPI_ON_BIT = 1;

    ClearSPIDoneFlag();
    #if defined(__C30__)
        SPIFLASH_SPICON2 = 0;
        SPIFLASH_SPISTAT = 0;    // clear SPI
        SPIFLASH_SPISTATbits.SPIEN = 1;
    #elif defined(__C32__)
        SPIFLASH_SPIBRG = (SYS_CLK_PeripheralClockGet()-1ul)/2ul/SPIFLASH_MAX_SPI_FREQ;
    #endif

	// Read Device ID code to determine supported device capabilities/instructions
	{
	    // Activate chip select
	    SPIFLASH_CS_IO = 0;
	    ClearSPIDoneFlag();
	
	    // Send instruction
	    SPIFLASH_SSPBUF = RDID;
	    WaitForDataByte();
	    Dummy = SPIFLASH_SSPBUF;	
		
		// Send 3 byte address (0x000000), discard Manufacture ID, get Device ID
	    for(i = 0; i < 5; i++)
	    {
		    SPIFLASH_SSPBUF = 0x00;
		    WaitForDataByte();
		    Dummy = SPIFLASH_SSPBUF;
		}

	    // Deactivate chip select
	    SPIFLASH_CS_IO = 1;
		
		// Decode Device Capabilities Flags from Device ID
		deviceCaps.v = 0x00;
		switch(Dummy)
		{
			case 0x43:	// SST25LF020(A)	(2 Mbit)	0xAF, 14us, AAI Byte
			case 0x48:	// SST25VF512(A)	(512 Kbit)	0xAF, 14us, AAI Byte
			case 0x49:	// SST25VF010A		(1 Mbit)	0xAF, 14us, AAI Byte
				deviceCaps.bits.bWriteByteStream = 1;
				break;
				
			case 0x4B:	// SST25VF064C		(64 Mbit)	0x02, 1.5ms/256 byte page, no AAI
				deviceCaps.bits.bPageProgram = 1;
				break;

			//case 0x01:	// SST25WF512		(512 Kbit)	0xAD, 50us, AAI Word
			//case 0x02:	// SST25WF010		(1 Mbit)	0xAD, 50us, AAI Word
			//case 0x03:	// SST25WF020		(2 Mbit)	0xAD, 50us, AAI Word
			//case 0x04:	// SST25WF040		(4 Mbit)	0xAD, 50us, AAI Word
			//case 0x05:	// SST25WF080		(8 Mbit)	0xAD, 14us, AAI Word
			//case 0x41:	// SST25VF016B		(16 Mbit)	0xAD,  7us, AAI Word
			//case 0x4A:	// SST25VF032B		(32 Mbit)	0xAD,  7us, AAI Word
			//case 0x8C:	// SST25VF020B		(2 Mbit)	0xAD,  7us, AAI Word
			//case 0x8D:	// SST25VF040B		(4 Mbit)	0xAD,  7us, AAI Word
			//case 0x8E:	// SST25VF080B		(8 Mbit)	0xAD,  7us, AAI Word				
			// Assume AAI Word programming is supported for the above commented 
			// devices and unknown devices.
			default:	
				deviceCaps.bits.bWriteWordStream = 1;
		}
	}


    // Clear any pre-existing AAI write mode
    // This may occur if the PIC is reset during a write, but the Flash is
    // not tied to the same hardware reset.
    _SendCmd(WRDI);

    // Execute Enable-Write-Status-Register (EWSR) instruction
    _SendCmd(EWSR);

    // Clear Write-Protect on all memory locations
    SPIFLASH_CS_IO = 0;
    SPIFLASH_SSPBUF = WRSR;
    WaitForDataByte();
    Dummy = SPIFLASH_SSPBUF;
    SPIFLASH_SSPBUF = 0x00; // Clear all block protect bits
    WaitForDataByte();
    Dummy = SPIFLASH_SSPBUF;
    SPIFLASH_CS_IO = 1;

    // Restore SPI state
    SPI_ON_BIT = 0;
    SPIFLASH_SPICON1 = SPICON1Save;
    SPI_ON_BIT = vSPIONSave;
}