Example #1
0
/**
 ****************************************************************************************
 * @brief Gets ADC sample.
 *
 * @return ADC sample
 ****************************************************************************************
 */
int adc_get_sample(void){
	int cnt=100000;
    
	SetBits16(GP_ADC_CTRL_REG, GP_ADC_START, 1);
	while (cnt-- && (GetWord16(GP_ADC_CTRL_REG) & GP_ADC_START) != 0x0000); 
	SetWord16(GP_ADC_CLEAR_INT_REG, 0x0000); // Clear interrupt
	return GetWord16(GP_ADC_RESULT_REG); 

}
Example #2
0
char uart_receive_byte(void){
	#ifndef UART_ENABLED
		return 0;
#else
	do{
	}while((GetWord16(UART_LSR_REG)&0x01)==0);	// wait until serial data is ready 
	return 0xFF&GetWord16(UART_RBR_THR_DLL_REG);	// read from receive data buffer
#endif
}
Example #3
0
/**
 ****************************************************************************************
 * @brief Polls until I2C eeprom is ready
 ****************************************************************************************
 */
void i2c_wait_until_eeprom_ready(void)
{
    uint16_t abort_SR_Status; // TX Abort Source Register
    // Polling until EEPROM ACK to detect busy period
    do {
        SEND_I2C_COMMAND(0x08);                                 // Make a dummy access
        WAIT_UNTIL_I2C_FIFO_IS_EMPTY();                         // Wait until Tx FIFO is empty
        WAIT_UNTIL_NO_MASTER_ACTIVITY();                        // Wait until no master activity   
        abort_SR_Status = GetWord16(I2C_TX_ABRT_SOURCE_REG);    // Read the Tx abort source register
        GetWord16(I2C_CLR_TX_ABRT_REG);                         // Clear the Tx abort flag
    } while( (abort_SR_Status & ABRT_7B_ADDR_NOACK) != 0 );     // Repeat if not ACK    
}
void NMI_HandlerC(unsigned long *hardfault_args)
{
    // Reached this point due to a WDOG timeout
    
    SetBits16(PMU_CTRL_REG, RADIO_SLEEP, 1);        // turn off radio PD
    SetBits16(PMU_CTRL_REG, PERIPH_SLEEP, 1);       // turn off peripheral power domain
    SetBits16(CLK_RADIO_REG, BLE_LP_RESET, 1);      // reset the BLE LP timer
    NVIC_ClearPendingIRQ(BLE_WAKEUP_LP_IRQn);       // clear any pending LP IRQs

#if (DEVELOPMENT_DEBUG)
    SetWord16(SET_FREEZE_REG, FRZ_WDOG);            // Stop WDOG
    SetBits16(SYS_CTRL_REG, DEBUGGER_ENABLE, 1);    // enable debugger to be able to re-attach
    
    *(volatile unsigned long *)(STATUS_BASE       ) = hardfault_args[0];    // R0
    *(volatile unsigned long *)(STATUS_BASE + 0x04) = hardfault_args[1];    // R1
    *(volatile unsigned long *)(STATUS_BASE + 0x08) = hardfault_args[2];    // R2
    *(volatile unsigned long *)(STATUS_BASE + 0x0C) = hardfault_args[3];    // R3
    *(volatile unsigned long *)(STATUS_BASE + 0x10) = hardfault_args[4];    // R12
    *(volatile unsigned long *)(STATUS_BASE + 0x14) = hardfault_args[5];    // LR
    *(volatile unsigned long *)(STATUS_BASE + 0x18) = hardfault_args[6];    // PC
    *(volatile unsigned long *)(STATUS_BASE + 0x1C) = hardfault_args[7];    // PSR

    *(volatile unsigned long *)(STATUS_BASE + 0x20) = (*((volatile unsigned long *)(0xE000ED28)));    // CFSR
    *(volatile unsigned long *)(STATUS_BASE + 0x24) = (*((volatile unsigned long *)(0xE000ED2C)));    // HFSR
    *(volatile unsigned long *)(STATUS_BASE + 0x28) = (*((volatile unsigned long *)(0xE000ED30)));    // DFSR
    *(volatile unsigned long *)(STATUS_BASE + 0x2C) = (*((volatile unsigned long *)(0xE000ED3C)));    // AFSR
    *(volatile unsigned long *)(STATUS_BASE + 0x30) = (*((volatile unsigned long *)(0xE000ED34)));    // MMAR
    *(volatile unsigned long *)(STATUS_BASE + 0x34) = (*((volatile unsigned long *)(0xE000ED38)));    // BFAR
    
    if ((GetWord16(SYS_STAT_REG) & DBG_IS_UP) == DBG_IS_UP)
       // __asm("BKPT #0\n");
	   BKPT();
    else
    {
        while(1);
    }

#else // DEVELOPMENT_DEBUG

# ifdef PRODUCTION_DEBUG_OUTPUT
	// Power up peripherals' power domain
    SetBits16(PMU_CTRL_REG, PERIPH_SLEEP, 0);
    while (!(GetWord16(SYS_STAT_REG) & PER_IS_UP));
    
    dbg_prod_output(0, hardfault_args);
# endif    
    
    // Remap addres 0x00 to ROM and force execution
    SetWord16(SYS_CTRL_REG, (GetWord16(SYS_CTRL_REG) & ~REMAP_ADR0) | SW_RESET );
#endif // DEVELOPMENT_DEBUG
}
Example #5
0
void SPI_Handler(void)
{ 
    // Received SPI interrupt..
    SetWord16(SPI_CLEAR_INT_REG, 0x01);
    /* Save the words in memory */
    int16_t v1 = (int16_t)GetWord16(SPI_RX_TX_REG1);
    int16_t v2 = (int16_t)GetWord16(SPI_RX_TX_REG0);
        #ifndef TESTING_SPI    
            #ifdef MARK_PACKETS
    if ((spi_439_block_idx<3) || ((spi_439_block_idx>8) && (spi_439_block_idx<12)))
    {
        if ((spi_439_block_idx==0) || (spi_439_block_idx==9))
            packet_marker+=3000;
        
        if (packet_marker>30000)
            packet_marker=0;
        
        v2+=packet_marker;
        v1+=packet_marker;
    }
            #endif
    *spi_439_buf_ptr++ = v1; // (int16_t)GetWord16(SPI_RX_TX_REG1);
    *spi_439_buf_ptr++ = v2; // (int16_t)GetWord16(SPI_RX_TX_REG0);  // spitst++; // 
        #else
    int offs = 0;
    if (startpkt==5)
        offs=100;
    else 
        startpkt++;
    testctn+=testctnincr;
    *spi_439_buf_ptr++ =testctn + offs;
    testctn+=testctnincr;
    *spi_439_buf_ptr++ =testctn;
    if (testctn>16000) 
        testctnincr=-2;
    if (testctn<0)  
        testctnincr=+2;
        #endif
    
    if (spi_439_block_idx > 0)
    {
        // Start next SPI transaction
        SetWord16(SPI_RX_TX_REG1, 0);  // write high part of dataToSend
        SetWord16(SPI_RX_TX_REG0, 0);  // write low part of dataToSend - this will trigger next SPI 
        spi_439_block_idx--;
    }
    /** else ** We are done */
}
Example #6
0
/**
****************************************************************************************
* @brief SPI 439 Read
* @param[in] address: 12 bits register address on 439
* @param[in] data: 
*
* To read one data word from the SPI, 3 transactions are needed.
* - First transaction: send command&address
* - Second transaction: send dummy 0
* - Third transacation: read returned data
* This functions uses POLLING to check if SPI transaction has completed.
*
* @return  data read from the 439
****************************************************************************************
*/
uint32_t spi_getword(uint32_t address)  // IZP named changed
{
    uint32_t dataRead = 0;
    
    spi_cs_low();

    uint32_t dataToSend = ((SC14439_MEM_RD<<13) | (address&0x1FFF ));
    SetWord16(SPI_RX_TX_REG0, (uint16_t)dataToSend);    // write (low part of) dataToSend

    while (!GetBits16(SPI_CTRL_REG, SPI_INT_BIT));      // polling to wait for spi transmission
    SetWord16(SPI_CLEAR_INT_REG, 0x01);                 // clear pending flag
    
    dataToSend = 0;
    SetWord16(SPI_RX_TX_REG0, (uint16_t)dataToSend);    // write (low part of) dataToSend

    while (!GetBits16(SPI_CTRL_REG, SPI_INT_BIT));      // polling to wait for spi transmission
    SetWord16(SPI_CLEAR_INT_REG, 0x01);                 // clear pending flag

    SetWord16(SPI_RX_TX_REG0, (uint16_t)dataToSend);    // write (low part of) dataToSend
    
    while (!GetBits16(SPI_CTRL_REG, SPI_INT_BIT));      // polling to wait for spi transmission
    SetWord16(SPI_CLEAR_INT_REG, 0x01);                 // clear pending flag

    dataRead  = GetWord16(SPI_RX_TX_REG0);              //read (low part of) data from spi slave

    spi_cs_high();

    return dataRead;                                    // return data read from spi slave
}
Example #7
0
/**
 ****************************************************************************************
 * @brief Enable pad's and peripheral clocks assuming that peripherals' power domain is down. The Uart and SPi clocks are set.
 *
 * @return void
 ****************************************************************************************
 */
void periph_init(void)  // set i2c, spi, uart, uart2 serial clks
{
	// Power up peripherals' power domain
    SetBits16(PMU_CTRL_REG, PERIPH_SLEEP, 0);
    while (!(GetWord16(SYS_STAT_REG) & PER_IS_UP)) ; 
    
    SetBits16(CLK_16M_REG,XTAL16_BIAS_SH_DISABLE, 1);
	
	//rom patch
	patch_func();
	
	//Init pads
	set_pad_functions();


#if (BLE_APP_PRESENT)
    
#if BLE_PROX_MONITOR
    app_proxm_port_reinit(XCY_LED0_GPIO);
    app_button_enable();
#elif BLE_FINDME_LOCATOR
    app_button_enable();
#endif //BLE_PROX_REPORTER
#if BLE_BATTERY_SERVER
    app_batt_port_reinit();
#endif //BLE_BATTERY_SERVER

#endif //BLE_APP_PRESENT

    // Enable the pads
	SetBits16(SYS_CTRL_REG, PAD_LATCH_EN, 1);
}
Example #8
0
/**
****************************************************************************************
* @brief Read a block of data from spu flash
* @param[in] destination_buffer: buffer to put the data
* @param[in] source_addr: starting position to read the data from spi
* @param[in] len: size of data to read  
****************************************************************************************
*/
void SpiFlashRead(unsigned long destination_buffer,unsigned long source_addr,unsigned long len)
{
    unsigned long i;

    spi_mem_cs_low();
    putByte_SPI(0x03);
    putByte_SPI((source_addr&0xffffff)>>16);
    putByte_SPI((source_addr&0xffff)>>8);
    putByte_SPI(source_addr&0xff);
    SetWord16(SPI_RX_TX_REG0, (uint16) 0);

    __ASM volatile ("nop");
    __ASM volatile ("nop");
    for (i=0;i<len;i++)
    {
        SetWord16(SPI_RX_TX_REG0, (uint16) 0); 
        __ASM volatile ("nop");
        __ASM volatile ("nop");
        __ASM volatile ("nop");
        __ASM volatile ("nop");
        __ASM volatile ("nop");
        __ASM volatile ("nop");
        *(uint8*)(uint32)(destination_buffer+i)=(uint8)GetWord16(SPI_RX_TX_REG0);
    }
    spi_mem_cs_high();
} 
Example #9
0
/**
****************************************************************************************
* @brief Read a single item from serial flash via SPI
* @param[in] x: buffer to read the data 
****************************************************************************************
*/
inline void getByte_SPI(uint8* x  )
{
    SetWord16(SPI_RX_TX_REG0, (uint16) DUMMY);
    while (GetBits16(SPI_CTRL_REG, SPI_INT_BIT==0));
    SetWord16(SPI_CLEAR_INT_REG, 0x01);
    *(uint8*)x=(uint8)GetWord16(SPI_RX_TX_REG0);   
} 
Example #10
0
/**
 ****************************************************************************************
 * @brief Read single series of bytes from I2C EEPROM (for driver's internal use)
 *
 * @param[in] p    Memory address to read the series of bytes from (all in the same page)
 * @param[in] size count of bytes to read (must not cross page)
 ****************************************************************************************
 */
static void read_data_single(uint8_t **p, uint32_t address, uint32_t size)
{
    int j;
    
    i2c_send_address(address);
                
    for (j = 0; j < size; j++)
    {    
        WAIT_WHILE_I2C_FIFO_IS_FULL();              // Wait if Tx FIFO is full
        SEND_I2C_COMMAND(0x0100);                   // Set read access for <size> times
    }
    
    // Critical section
    GLOBAL_INT_DISABLE();
    
    // Get the received data
    for (j = 0; j < size; j++)                                         
    {
        WAIT_FOR_RECEIVED_BYTE();                   // Wait for received data
        **p =(0xFF & GetWord16(I2C_DATA_CMD_REG));  // Get the received byte
        (*p)++;
    }
    // End of critical section
    GLOBAL_INT_RESTORE();
}
Example #11
0
/**
 ****************************************************************************************
 * @brief Read low power clock selection from 
 *
 * The Hclk and Pclk are set
 ****************************************************************************************
*/
void select_lp_clk()
{
#if (CFG_LP_CLK == LP_CLK_FROM_OTP)
    int cnt = 100000;

# ifndef APP_BOOT_FROM_OTP    
#define XPMC_MODE_MREAD   0x1
    uint16_t *lp_clk = (uint16_t *)(0x40000 + LP_CLK_OTP_OFFSET);     //where in OTP header is IQ_Trim
    
    SetBits16(CLK_AMBA_REG, OTP_ENABLE, 1);                           //enable OTP clock	
    while ((GetWord16(ANA_STATUS_REG) & LDO_OTP_OK) != LDO_OTP_OK && cnt--)
        /* Just wait */;
        
    // set OTP in read mode 
    SetWord32(OTPC_MODE_REG, XPMC_MODE_MREAD);
# else
    uint16_t *lp_clk = (uint16_t *)(0x20000000 + LP_CLK_OTP_OFFSET);  //where in OTP header is IQ_Trim
# endif //APP_BOOT_FROM_OTP   

    lp_clk_sel  = (*lp_clk);
    SetBits16(CLK_AMBA_REG, OTP_ENABLE, 0);                           //disable OTP clock     
    
#else //CFG_LP_CLK 
    lp_clk_sel = CFG_LP_CLK; 
#endif    
}
Example #12
0
/*********************************************************************************
 *** EVENT_INT ISR
 ***/
void BLE_EVENT_Handler(void)
{
    // Check BLE interrupt status and call the appropriate handlers
    //uint32_t irq_stat = ble_intstat_get();
    uint32_t irq_stat = GetWord16(BLE_INTSTAT_REG);

    // End of event interrupt
    if (irq_stat & BLE_EVENTINTSTAT_BIT)
    {
#if (STREAMDATA_QUEUE)             
     cpt_event=1;        
#ifdef METRICS
        {
            struct lld_evt_tag *evt;
             evt = (struct lld_evt_tag *)co_list_pick(&lld_evt_env.evt_prog);
             uint8_t rx_cnt = ble_rxdesccnt_getf(evt->conhdl);
            // Event has been processed, handle the transmitted and received data
             measure_errors_received( rx_cnt - evt->rx_cnt);
        }            
#endif
#endif
        lld_evt_end_isr();
        
        rwble_last_event = BLE_EVT_END;
        
    }
}
Example #13
0
/**
 ****************************************************************************************
 * @brief Read from 3-wire SPI
 * @param[in] registerIndex: Target address (A6..A0)
 *
 * @return  byte read
 ****************************************************************************************
 */
uint8_t read_from_3wire_SPI_register(uint8_t registerIndex, uint8_t is_last_transaction)
{
    static uint8_t i, dataRead;

    GPIO_SetInactive(cs.port, cs.pin);                              // pull CS low

    GPIO_SetPinFunction( sdio.port, sdio.pin, OUTPUT, PID_SPI_DO);  // configure SDIO as output

    SetWord16(SPI_RX_TX_REG0, (uint16_t)(registerIndex) );          // MSB set to HIGH - A6..A0 Address of register to write to
    do {
    } while (GetBits16(SPI_CTRL_REG,SPI_INT_BIT)==0);               // polling to wait for spi transmission

    SetWord16(SPI_CLEAR_INT_REG, 0);                                //Clear SPI_CTRL_REG[SPI_INT_BIT] interrupt

    GPIO_SetPinFunction( sdio.port, sdio.pin, INPUT, PID_SPI_DI);   // configure SDIO as input

    for (i=0; i<TsradCounter; i++);                                 // {DEV.NOTE#: For Mouse sensor Delay > Tsrad = 4us <-- suitable counter is 6}

    SetWord16(SPI_RX_TX_REG0, (uint16_t)0x00 );                     // dummy write data - read data
    do {
    } while (GetBits16(SPI_CTRL_REG,SPI_INT_BIT)==0);               // polling to wait for spi transmission

    SetWord16(SPI_CLEAR_INT_REG, 0);                                //Clear SPI_CTRL_REG[SPI_INT_BIT] interrupt

    dataRead = GetWord16(SPI_RX_TX_REG0);                           // read received byte

    SetWord16(SPI_CLEAR_INT_REG, 0x01);                             // clear pending flag

    if(is_last_transaction)
        GPIO_SetActive(cs.port, cs.pin);                              // set CS high

    return dataRead;
}
Example #14
0
void BLE_RF_DIAG_Handler(void)
{
  uint32 irq_scr;
  uint16_t cn;    
  cn = GetWord16(RF_BMCW_REG) & 0x003F;    

  irq_scr = GetWord32(BLE_RF_DIAGIRQ_REG);  // read BLE_RF_DIAGIRQ_REG so that you clear DIAGIRQ_STAT_0 (otherwise interrupt is activated again and again!)
    
#if LUT_PATCH_ENABLED 
  const volatile struct LUT_CFG_struct *pLUT_CFG;	// = (const volatile struct LUT_CFG_struct *)(jump_table_struct[lut_cfg_pos]);
  pLUT_CFG = (const volatile struct LUT_CFG_struct *)(jump_table_struct[lut_cfg_pos]);
  if(!pLUT_CFG->HW_LUT_MODE)
  { 
    set_rf_cal_cap(cn); 
  }
#endif
  
#if MGCKMODA_PATCH_ENABLED 
  if(GetBits16(RF_MGAIN_CTRL_REG, GAUSS_GAIN_SEL) && (irq_scr & DIAGIRQ_STAT_0))  // TODO: If GAUSS_GAIN_SEL==0x1 AND it is an TX_EN interrupt (for RX_EN int it is not necessary to run)
  { 
    set_gauss_modgain(cn); 
  }
#endif
  
#ifdef PRODUCTION_TEST  
	if( irq_scr & DIAGIRQ_STAT_0)//check TXEN posedge 
	{	
		test_tx_packet_nr++;
	}
	if( irq_scr & DIAGIRQ_STAT_1)//check RXEN posedge 
	{	
		test_rx_irq_cnt++;
	}  
#endif    
}
Example #15
0
void nvds_read_bdaddr_from_otp()
{
#ifdef BDADDR_FROM_OTP

#ifndef APP_BOOT_FROM_OTP    
    int cnt=100000;
#define XPMC_MODE_MREAD   0x1
    uint8_t *otp_bdaddr = (uint8_t *)0x40000 + BDADDR_FROM_OTP;   //where in OTP header is BDADDR
    
    SetBits16(CLK_AMBA_REG, OTP_ENABLE, 1);		// enable OTP clock	
    while ((GetWord16(ANA_STATUS_REG) & LDO_OTP_OK) != LDO_OTP_OK && cnt--)
        /* Just wait */;
        
    // set OTP in read mode 
    SetWord32 (OTPC_MODE_REG,XPMC_MODE_MREAD);
#else
    uint8_t *otp_bdaddr = (uint8_t *)0x20000000 + BDADDR_FROM_OTP;   //where in OTP header is BDADDR
#endif    
    
    memcpy(&dev_bdaddr, otp_bdaddr, sizeof(dev_bdaddr));
    SetBits16(CLK_AMBA_REG, OTP_ENABLE, 0);     //disable OTP clock    
    #ifdef SUPPORT_1_8_V
        SetBits16(DCDC_CTRL2_REG, DCDC_VBAT3V_LEV, 0x0);   ///--Support 1.8V boot
    #endif

#endif    
}
Example #16
0
/**
 ****************************************************************************************
 * @brief Sets the XTAL16M trim value.
 *
 * @param[in] trim_value    Trim value.
 *
 * @return void
 ****************************************************************************************
 */
void set_xtal16m_trim_value(uint16_t trim_value)
{
    SetBits16(CLK_16M_REG, RC16M_ENABLE, 1);                        // enable RC 16MHz
    for (volatile int i = 0; i < 20; i++);

    SetBits16(CLK_CTRL_REG, SYS_CLK_SEL, 1);                        // switch to  RC16
    while( (GetWord16(CLK_CTRL_REG) & RUNNING_AT_RC16M) == 0 );     // wait for actual switch

    SetBits16(CLK_CTRL_REG, XTAL16M_DISABLE, 1);                    // disable XTAL16
    SetWord16(CLK_FREQ_TRIM_REG, trim_value);                       // set default trim value
    SetBits16(CLK_CTRL_REG, XTAL16M_DISABLE, 0);                    // enable XTAL16
    while( (GetWord16(SYS_STAT_REG) & XTAL16_SETTLED) == 0 );       // wait for XTAL16 settle

    SetBits16(CLK_CTRL_REG , SYS_CLK_SEL ,0);                       // switch to  XTAL16
    while( (GetWord16(CLK_CTRL_REG) & RUNNING_AT_XTAL16M) == 0 );   // wait for actual switch
}
/**
 ****************************************************************************************
 * @brief Enable pad's and peripheral clocks assuming that peripherals' power domain is down. The Uart and SPi clocks are set.
 *
 * @return void
 ****************************************************************************************
 */
void periph_init(void) 
{
	// Power up peripherals' power domain
    SetBits16(PMU_CTRL_REG, PERIPH_SLEEP, 0);
    while (!(GetWord16(SYS_STAT_REG) & PER_IS_UP)) ; 
    
#if ES4_CODE
    SetBits16(CLK_16M_REG,XTAL16_BIAS_SH_DISABLE, 1);
#endif    
	
	//rom patch
	patch_func();
	
	//Init pads
	set_pad_functions();

#if (BLE_APP_PRESENT)
/*
* (Re)Initialize peripherals
i.e.    
    uart_init(UART_BAUDRATE_115K2, 3);
*/        
#endif
    
    // Enable the pads
	SetBits16(SYS_CTRL_REG, PAD_LATCH_EN, 1);
}
Example #18
0
void nvds_read_bdaddr_from_otp()
{
    const uint16_t BDADDR_OFFSET = 0x7fd4; // offset of BD address in OTP header
    uint8_t *otp_bdaddr;

    if (!APP_BOOT_FROM_OTP)    
    {
        int cnt = 100000;
        #define XPMC_MODE_MREAD   0x1
        otp_bdaddr = (uint8_t *)0x40000 + BDADDR_OFFSET;   //where in OTP header is BDADDR
        
        SetBits16(CLK_AMBA_REG, OTP_ENABLE, 1);		// enable OTP clock	
        while ((GetWord16(ANA_STATUS_REG) & LDO_OTP_OK) != LDO_OTP_OK && cnt--)
            /* Just wait */;
            
        // set OTP in read mode 
        SetWord32(OTPC_MODE_REG, XPMC_MODE_MREAD);
    }
    else
        otp_bdaddr = (uint8_t *)0x20000000 + BDADDR_OFFSET;   //where in OTP header is BDADDR

#if BLE_JWAOO_TOY_SERVER
	memcpy(jwaoo_toy_device_data.bd_addr, otp_bdaddr, sizeof(jwaoo_toy_device_data.bd_addr));
#else
	memcpy(&dev_bdaddr, otp_bdaddr, sizeof(dev_bdaddr));
#endif

    SetBits16(CLK_AMBA_REG, OTP_ENABLE, 0);     //disable OTP clock    
}
Example #19
0
/**
 ****************************************************************************************
 * @brief Checks if system is in startup phase.

 * @return uint8   1 if system is in startup phase, otherwise 0.
 ****************************************************************************************
 */
uint8_t check_sys_startup_period(void)
{
   uint8_t ret_value = 0;  
    
    if (sys_startup_flag)
    {
        uint32_t current_time;
        
        current_time = lld_evt_time_get();
                
        if (current_time < startup_sleep_delay) // startup_sleep_delay after system startup to allow system to sleep
            ret_value = 1;
        else // After 2 seconds system can sleep
        {
            sys_startup_flag = false;
            if ( (app_get_sleep_mode() == 2) || (app_get_sleep_mode() == 1) )
            {
                SetWord16(SET_FREEZE_REG, FRZ_WDOG);            // Stop WDOG until debugger is removed
                while ((GetWord16(SYS_STAT_REG) & DBG_IS_UP) == DBG_IS_UP) {};
                SetBits16(SYS_CTRL_REG, DEBUGGER_ENABLE, 0);    // close debugger
            }

#if (USE_WDOG)
            SetWord16(RESET_FREEZE_REG, FRZ_WDOG);  // Start WDOG
#endif
         
            ret_value = 0;
        }
    }

    return ret_value;
}
Example #20
0
/**
 ****************************************************************************************
 * @brief Enable pad's and peripheral clocks assuming that peripherals' power domain is down.
 *
 * The Uart and SPi clocks are set. 
 ****************************************************************************************
 */
void periph_init(void)  // set i2c, spi, uart, uart2 serial clks
{
	// Power up peripherals' power domain
    SetBits16(PMU_CTRL_REG, PERIPH_SLEEP, 0);
    while (!(GetWord16(SYS_STAT_REG) & PER_IS_UP)) ; 
    
#if ES4_CODE
    SetBits16(CLK_16M_REG,XTAL16_BIAS_SH_DISABLE, 1);
#endif    
		
    // Initialize UART component
 
    SetBits16(CLK_PER_REG, UART1_ENABLE, 1);    // enable clock - always @16MHz
	
    // baudr=9-> 115k2
    // mode=3-> no parity, 1 stop bit 8 data length
#ifdef UART_MEGABIT
    uart_init(UART_BAUDRATE_1M, 3);
#else
    uart_init(UART_BAUDRATE_115K2, 3);
#endif // UART_MEGABIT
    SetBits16(CLK_PER_REG, SPI_ENABLE, 1);    // enable  clock
    SetBits16(CLK_PER_REG, SPI_DIV, 1);	    // set divider to 1	
	
	//rom patch
	patch_func();
	
	//Init pads
	set_pad_functions();

    // Enable the pads
	SetBits16(SYS_CTRL_REG, PAD_LATCH_EN, 1);
}
Example #21
0
void iq_trim_from_otp()
{
#ifdef IQTRIM_FROM_OTP

#ifndef APP_BOOT_FROM_OTP    
    int cnt=100000;
#define XPMC_MODE_MREAD   0x1
    uint16_t *iqtrim = (uint16_t *)(0x40000 + IQTRIM_FROM_OTP);   //where in OTP header is IQ_Trim
    
    SetBits16(CLK_AMBA_REG, OTP_ENABLE, 1);		// enable OTP clock	
    while ((GetWord16(ANA_STATUS_REG) & LDO_OTP_OK) != LDO_OTP_OK && cnt--)
        /* Just wait */;
        
    // set OTP in read mode 
    SetWord32 (OTPC_MODE_REG,XPMC_MODE_MREAD);
#else
    uint16_t *iqtrim = (uint16_t *)(0x20000000 + IQTRIM_FROM_OTP);   //where in OTP header is IQ_Trim
#endif    
    
    iq_trim_bias_ctrl1_reg = *iqtrim++;
    iq_trim_rf_mixer_ctrl1_reg = *iqtrim;
    SetBits16(CLK_AMBA_REG, OTP_ENABLE, 0);     //disable OTP clock    
    #ifdef SUPPORT_1_8_V
        SetBits16(DCDC_CTRL2_REG, DCDC_VBAT3V_LEV, 0x0);   ///--Support 1.8V boot
    #endif
    
#endif    
}
Example #22
0
void hci_xtal_trim_cmd(uint8_t* ptr_data)
{
    uint16_t delta = ptr_data[2]<<8|ptr_data[1];
	struct msg_xtal_trim_cfm s;
	uint8_t* bufptr;
	s.packet_type	= HCI_EVT_MSG_TYPE;
	s.event_code 	= HCI_CMD_CMPL_EVT_CODE; 
	s.length     	= 5;
    s.param0		= 0x0001;
    if(ptr_data[0] == 0)
        s.xtrim_val		= GetWord16(CLK_FREQ_TRIM_REG);
    else
        s.xtrim_val		= 0x0;
	s.param_opcode	= HCI_XTAL_TRIM_CMD_OPCODE;
	bufptr = (uint8_t*)&s;							
	uart_write(bufptr,sizeof(struct msg_xtal_trim_cfm),NULL);					

    if(ptr_data[0] == 1)
    {
        set_XTAL_trim_reg(delta);
    }
    else if(ptr_data[0] == 2)
    {
       SetWord32(TEST_CTRL_REG, 1);
       SetWord32(P05_MODE_REG, 0x300);
    }
    else if(ptr_data[0] == 3)
    {
        uint16_t reg_value = GetWord16(CLK_FREQ_TRIM_REG);   
        if (reg_value <= 0xFFFF - delta)
            set_XTAL_trim_reg(reg_value + delta);
        //for (i=0; i<400; i++); //wait for at least 200us
    }        
    else if(ptr_data[0] == 4)
    {
        uint16_t reg_value = GetWord16(CLK_FREQ_TRIM_REG);   
        if (reg_value >= delta)
            set_XTAL_trim_reg(reg_value - delta);
        //for (i=0; i<400; i++); //wait for at least 200us        
    }
    else if(ptr_data[0] == 5)
    {
       SetWord32(TEST_CTRL_REG, 0);
       SetWord32(P05_MODE_REG, 0x200);
    }
}	
Example #23
0
/**
 ****************************************************************************************
 * @brief  Acquires the raw RFADC IQ samples
 ****************************************************************************************
 */
void trng_get_raw_data(uint32 address, uint32 length)
{
    SetWord16(RFPT_ADDR_REG, address);
    SetWord16(RFPT_LEN_REG, length);
    SetWord16(RFPT_CTRL_REG, 0x1);

    while ((GetWord16(RFPT_STAT_REG) & RFPT_ACTIVE)== RFPT_ACTIVE);
}
Example #24
0
void uart_send_byte(char ch){
	#ifndef UART_ENABLED
		return ;
#else
	while((GetWord16(UART_LSR_REG)&0x20)==0);	// read status reg to check if THR is empty
	SetWord16(UART_RBR_THR_DLL_REG,(0xFF&ch)); // write to THR register
	return;
	#endif
}
/**
 ****************************************************************************************
 * @brief Initializes the LEDs
 *
 * @param   None    
 *
 * @return  void
 ****************************************************************************************
 */
void leds_init()
{
    if (!(GetWord16(SYS_STAT_REG) & DBG_IS_UP) || !dbg_uses_led_pins()) {
        GPIO_ConfigurePin(KBD_GREEN_LED_PORT,   KBD_GREEN_LED_PIN,      OUTPUT, PID_GPIO, LED_STATE_OFF); // green: off
        green_led_st    = LED_OFF;	
        GPIO_ConfigurePin(KBD_RED_LED_PORT,     KBD_RED_LED_PIN,        OUTPUT, PID_GPIO, LED_STATE_OFF); // red: off
        red_led_st      = LED_OFF;	
    }
}
Example #26
0
/**
****************************************************************************************
* @brief Write a single item to serial flash via SPI
* @param[in] x: data to send
****************************************************************************************
*/
void putByte_SPI(uint32 x)
{ 
    uint16 tmp; 
    SetWord16(SPI_RX_TX_REG0, (uint16_t)x);    // write (low part of) dataToSend
    do
    {
    } while (GetBits16(SPI_CTRL_REG, SPI_INT_BIT) == 0);  // polling to wait for spi transmission
    SetWord16(SPI_CLEAR_INT_REG, 0x01);            // clear pending flag
    tmp = GetWord16(SPI_RX_TX_REG0);
}
/**
 ****************************************************************************************
 * @brief  Sets-up the LEDS to indicate disconnection
 *
 * @param   None    
 *
 * @return  void
 ****************************************************************************************
 */
void leds_set_disconnected()
{
    high_priority_indications_active = true;
    if (!(GetWord16(SYS_STAT_REG) & DBG_IS_UP) || !dbg_uses_led_pins()) {
        green_led_off();
        red_led_on();
        app_timer_set(APP_RED_LED_TIMER, TASK_APP, RED_ON);
        leds_block_sleep();
    }
}
/**
 ****************************************************************************************
 * @brief  Sets-up the LEDS to indicate connection astablished status
 *
 * @param   None    
 *
 * @return  void
 ****************************************************************************************
 */
void leds_set_connection_established()
{
    if (!(GetWord16(SYS_STAT_REG) & DBG_IS_UP)) {
        red_led_off();
        green_led_on();
        app_timer_set(APP_GREEN_LED_TIMER, TASK_APP, GREEN_ON);
        if (app_get_sleep_mode()) {
            app_force_active_mode(); // prevent sleep only if enabled
        }
    }
}
Example #29
0
/*
 * Name         : GPIO_GetPinStatus - Gets the state of the pin
 *
 * Scope        : PUBLIC
 *
 * Arguments    : port - GPIO port
 *                pin  - pin
 *
 * Description  : Gets the GPIO status. The GPIO should have been previously configured 
 *                as input!
 *
 * Returns      : TRUE if the pin is high,
 *                FALSE if low.
 *
 */
bool GPIO_GetPinStatus( GPIO_PORT port, GPIO_PIN pin )
{
#if DEVELOPMENT__NO_OTP
    if ( !(GPIO_status & ( ((uint64_t)1 << pin) << (port * 16) )) )
                __asm("BKPT #0\n"); // this pin has not been previously reserved!        
#endif    
    
    const int data_reg = GPIO_BASE + (port << 5);
    
    return ( (GetWord16(data_reg) & (1 << pin)) != 0 );
}
Example #30
0
/**
 ****************************************************************************************
 * @brief Read single byte from I2C EEPROM.
 *
 * @param[in] Memory address to read the byte from.
 *
 * @return Read byte.
 ****************************************************************************************
 */
uint8_t i2c_eeprom_read_byte(uint32_t address)
{
    i2c_wait_until_eeprom_ready();
    i2c_send_address(address);  
    
    WAIT_WHILE_I2C_FIFO_IS_FULL();                  // Wait if Tx FIFO is full
    SEND_I2C_COMMAND(0x0100);                       // Set R/W bit to 1 (read access)
    WAIT_UNTIL_I2C_FIFO_IS_EMPTY();                 // Wait until I2C Tx FIFO empty
    WAIT_FOR_RECEIVED_BYTE();                       // Wait for received data
    return (0xFF & GetWord16(I2C_DATA_CMD_REG));    // Get received byte
}