Пример #1
0
void tsReadZ(uint32_t* z1, uint32_t* z2)
{
  if (!_tsInitialised) tsInit();

  // XP = ADC
  // XM = GPIO Output Low
  // YP = GPIO Output High
  // YM = GPIO Input

  TS_XM_FUNC_GPIO;
  TS_YP_FUNC_GPIO;
  TS_YM_FUNC_GPIO;

  gpioSetDir (TS_XM_PORT, TS_XM_PIN, 1);
  gpioSetDir (TS_YP_PORT, TS_YP_PIN, 1);
  gpioSetDir (TS_YM_PORT, TS_YM_PIN, 0);

  gpioSetValue(TS_XM_PORT, TS_XM_PIN, 0);   // GND
  gpioSetValue(TS_YP_PORT, TS_YP_PIN, 1);   // 3.3V

  TS_XP_FUNC_ADC;
  *z1 = adcRead(TS_XP_ADC_CHANNEL);

  // XP = GPIO Input
  // XM = GPIO Output Low
  // YP = GPIO Output High
  // YM = ADC

  TS_XP_FUNC_GPIO;
  gpioSetDir (TS_YM_PORT, TS_YM_PIN, 0);

  TS_YM_FUNC_ADC;
  *z2 = adcRead(TS_YM_ADC_CHANNEL);
}
void ledsInit(void){

    uint8_t i;

    //init SPI
    SPI_Configure(SPI0, ID_SPI0, SPI_MR_MSTR | SPI_MR_MODFDIS | SPI_MR_PCS(0));
    SPI_ConfigureNPCS(SPI0, 0 , SPI_CSR_NCPHA | SPI_CSR_BITS_12_BIT | SPI_CSR_SCBR(3) | SPI_CSR_DLYBS(2) | SPI_CSR_DLYBCT(0)); //30MHz spi speed
    SPI_Enable(SPI0);
    SPI0->SPI_IDR = 0xFFFFFFFF;
    NVIC_EnableIRQ(SPI0_IRQn);

    //init pins: SPI
    gpioSetFun(MISO, GPIO_FUNC_A);
    gpioSetFun(MOSI, GPIO_FUNC_A);
    gpioSetFun(SCLK, GPIO_FUNC_A);

    //init pins: debug LED
    gpioSetFun(DBGLED, GPIO_FUNC_GPIO);
    gpioSetDir(DBGLED, 0);
    gpioSetVal(DBGLED, 0);

    //init pins: latch
    gpioSetFun(XLAT, GPIO_FUNC_GPIO);
    gpioSetDir(XLAT, 0);
    gpioSetVal(XLAT, 0);

    //init pins: blanking
    for(i = 0; i < sizeof(blanks); i++){
        gpioSetFun(blanks[i], GPIO_FUNC_GPIO);
        gpioSetDir(blanks[i], 0);
        gpioSetVal(blanks[i], 1);
    }

    periodicAdd(ledUpdate, 0, 4);
}
Пример #3
0
void nrf_init() {
    // Enable SPI correctly
    sspInit(0, sspClockPolarity_Low, sspClockPhase_RisingEdge);

    // Enable CS & CE pins
    gpioSetDir(RB_SPI_NRF_CS, gpioDirection_Output);
    gpioSetPullup(&RB_SPI_NRF_CS_IO, gpioPullupMode_Inactive);
    gpioSetDir(RB_NRF_CE, gpioDirection_Output);
    gpioSetPullup(&RB_NRF_CE_IO, gpioPullupMode_PullUp);
    CE_LOW();

    // Setup for nrf24l01+
    // power up takes 1.5ms - 3.5ms (depending on crystal)
    CS_LOW();
    nrf_write_reg(R_CONFIG,
            R_CONFIG_PRIM_RX| // Receive mode
            R_CONFIG_PWR_UP|  // Power on
            R_CONFIG_EN_CRC   // CRC on, single byte
            );
    
    nrf_write_reg(R_EN_AA, 0); // Disable Enhanced ShockBurst;

    // Set speed / strength
    nrf_write_reg(R_RF_SETUP,DEFAULT_SPEED|R_RF_SETUP_RF_PWR_3);

    // Clear MAX_RT, just in case.
    nrf_write_reg(R_STATUS,R_STATUS_MAX_RT);
};
Пример #4
0
void chb_drvr_init()
{
    // ToDo: Make sure gpioInit has been called
    // ToDo: Make sure CT32B0 has been initialised and enabled

    // config SPI for at86rf230 access
    chb_spi_init();

    // Setup 16-bit timer 0 (used for us delays)
    timer16Init(0, 0xFFFF);
    timer16Enable(0);

    // Set sleep and reset as output
    gpioSetDir(CHB_SLPTRPORT, CHB_SLPTRPIN, 1);
    gpioSetDir(CHB_RSTPORT, CHB_RSTPIN, 1);

    // configure IOs
    gpioSetValue(CHB_SLPTRPORT, CHB_SLPTRPIN, 1);  // Set sleep high
    gpioSetValue(CHB_RSTPORT, CHB_RSTPIN, 1);      // Set reset high

    // Set internal resistors
    gpioSetPullup (&CHB_SLPTRPIN_IOCONREG, gpioPullupMode_Inactive);
    gpioSetPullup (&CHB_RSTPIN_IOCONREG, gpioPullupMode_Inactive);

    // config radio
    chb_radio_init();
}
Пример #5
0
uint32_t tsReadY(void)
{
  if (!_tsInitialised) tsInit();

  // YP = GPIO Output High
  // YM = GPIO Output Low
  // XP = GPIO Input
  // XM = ADC

  TS_YP_FUNC_GPIO;
  TS_YM_FUNC_GPIO;
  TS_XP_FUNC_GPIO;

  gpioSetDir (TS_YP_PORT, TS_YP_PIN, 1);
  gpioSetDir (TS_YM_PORT, TS_YM_PIN, 1);
  gpioSetDir (TS_XP_PORT, TS_XP_PIN, 0);

  gpioSetValue(TS_YP_PORT, TS_YP_PIN, 1);   // 3.3V
  gpioSetValue(TS_YM_PORT, TS_YM_PIN, 0);   // GND

  TS_XM_FUNC_ADC;

  // Return the ADC results
  return adcRead(TS_XM_ADC_CHANNEL);
}
Пример #6
0
//Initializes the SPI module of the AVR to work with the NORDIC
void init_nordic_spi() {

	//Configure IO Directions
	// config CSN pin
	IOCON_JTAG_TDO_PIO1_1 &= ~IOCON_JTAG_TDO_PIO1_1_FUNC_MASK;
	IOCON_JTAG_TDO_PIO1_1 |= IOCON_JTAG_TDO_PIO1_1_FUNC_GPIO;
    gpioSetDir(1, NORDIC_CSN, gpioDirection_Output);
	// config CE pin
	IOCON_JTAG_nTRST_PIO1_2 &= ~IOCON_JTAG_nTRST_PIO1_2_FUNC_MASK;
	IOCON_JTAG_nTRST_PIO1_2 |= IOCON_JTAG_nTRST_PIO1_2_FUNC_GPIO;
    gpioSetDir(1, NORDIC_CE, gpioDirection_Output);
	// config IRQ pin - needs to be input actually.
	IOCON_JTAG_TMS_PIO1_0 &= ~IOCON_JTAG_TMS_PIO1_0_FUNC_MASK;
	IOCON_JTAG_TMS_PIO1_0 |= IOCON_JTAG_TMS_PIO1_0_FUNC_GPIO;
    
	//Edited to be input
	gpioSetDir(1, NORDIC_IRQ, gpioDirection_Input);
	

	//Drive nCS HIGH
	NORDIC_CSN_HIGH();

	//Configure UART1 in SPI MODE
	//CPOL and CPHA both at 0? 
	//SCK should normally be low. And the clock samples data in the middle of data bits, 
	sspInit (0, sspClockPolarity_Low, sspClockPhase_RisingEdge);

	timer16Init(0, TIMER16_DEFAULTINTERVAL);
}
Пример #7
0
void tsReadZ(uint32_t* z1, uint32_t* z2)
{
  if (!_tsInitialised) tsInit();

  // Make sure that X+/Y- are set to GPIO
  TS_XP_FUNC_GPIO;
  TS_YM_FUNC_GPIO;

  // Set X- and Y+ to inputs (necessary?)
  gpioSetDir (TS_XM_PORT, TS_XM_PIN, 0);
  gpioSetDir (TS_YP_PORT, TS_YP_PIN, 0);

  // Set X+ and Y- to output
  gpioSetDir (TS_XP_PORT, TS_XP_PIN, 1);
  gpioSetDir (TS_YM_PORT, TS_YM_PIN, 1);

  // X+ goes low, Y- goes high
  gpioSetValue(TS_XP_PORT, TS_XP_PIN, 0);   // GND
  gpioSetValue(TS_YM_PORT, TS_YM_PIN, 1);   // 3.3V

  // Set X- and Y+ to ADC
  TS_XM_FUNC_ADC;  
  TS_YP_FUNC_ADC;  

  // Get ADC results
  *z1 = adcRead(TS_YP_ADC_CHANNEL);     // Z1 (Read Y+)
  *z2 = adcRead(TS_XM_ADC_CHANNEL);     // Z2 (Read X-)
}
Пример #8
0
uint8_t lcdRead(uint8_t data)
{
    uint32_t op211cache=IOCON_PIO2_11;
    uint32_t op09cache=IOCON_PIO0_9;
    uint32_t dircache=GPIO_GPIO2DIR;
    IOCON_PIO2_11=IOCON_PIO2_11_FUNC_GPIO|IOCON_PIO2_11_MODE_PULLUP;
    IOCON_PIO0_9=IOCON_PIO0_9_FUNC_GPIO|IOCON_PIO0_9_MODE_PULLUP;
    gpioSetDir(SCK, 1);

    uint8_t i;

    gpioSetDir(SDA, 1);
    gpioSetValue(SCK, 0);
    gpioSetValue(CS, 0);
    delayms(1);

    gpioSetValue(SDA, 0);
    gpioSetValue(SCK, 1);
    delayms(1);
    
    for(i=0; i<8; i++){
        gpioSetValue(SCK, 0);
        delayms(1);
        if( data & 0x80 )
            gpioSetValue(SDA, 1);
        else
            gpioSetValue(SDA, 0);
        data <<= 1;
        gpioSetValue(SCK, 1);
        delayms(1);
    }
    uint8_t ret = 0;

    gpioSetDir(SDA, 0);
    for(i=0; i<8; i++){
        gpioSetValue(SCK, 0);
        delayms(1);
        ret <<= 1;
        ret |= gpioGetValue(SDA);
        gpioSetValue(SCK, 1);
        delayms(1);
    }
    gpioSetValue(SCK, 0);

    gpioSetValue(CS, 1);
    gpioSetDir(SDA, 1);
    IOCON_PIO2_11=op211cache;
    IOCON_PIO0_9=op09cache;
    GPIO_GPIO2DIR=dircache;
    delayms(1);
    return ret;
}
Пример #9
0
void samsungvfdInit(uint8_t brightness)
{
  // Set all pins to output
  gpioSetDir(SAMSUNGVFD_SIO_PORT, SAMSUNGVFD_SIO_PIN, gpioDirection_Output);
  gpioSetDir(SAMSUNGVFD_STB_PORT, SAMSUNGVFD_STB_PIN, gpioDirection_Output);
  gpioSetDir(SAMSUNGVFD_SCK_PORT, SAMSUNGVFD_SCK_PIN, gpioDirection_Output);

  // Set strobe and clock pins high by default
  gpioSetValue(SAMSUNGVFD_STB_PORT, SAMSUNGVFD_STB_PIN, 1);
  gpioSetValue(SAMSUNGVFD_SCK_PORT, SAMSUNGVFD_SCK_PIN, 1);

  // Default to 2x20 display (SAMSUNG 20T202DA2JA)
  samsungvfd_begin(20, 2, brightness);
}
Пример #10
0
int main ()
{
	init ();

	gpioSetDir (3, 1, 1);
	gpioSetValue (3, 1, 1);

	int availBytes;
	char buf[32];
	uint8_t frame[64];
	for (int i = 0; i < 64; ++i) {
		frame[i] = i;
	} 
	while (1) {
		systickDelay (1);
		USB_WriteEP (CDC_DEP_IN, frame, 64);
		// CDC_WrOutBuf (text, &textLen);

		CDC_OutBufAvailChar (&availBytes);
		if (availBytes > 0) {
			int bytesToRead = availBytes > 32 ? 32 : availBytes;
			int bytesRead = CDC_RdOutBuf (buf, &bytesToRead);
			gpioSetValue (3, 1, 0);
		}
	}
}
Пример #11
0
void cmdInit()
{
  #if defined CFG_INTERFACE && defined CFG_INTERFACE_UART
    // Check if UART is already initialised
    uart_pcb_t *pcb = uartGetPCB();
    if (!pcb->initialised)
    {
      uartInit(CFG_UART_BAUDRATE);
    }
  #endif

  #if CFG_INTERFACE_ENABLEIRQ != 0
    // Set IRQ pin as output
    gpioSetDir(CFG_INTERFACE_IRQPORT, CFG_INTERFACE_IRQPIN, gpioDirection_Output);
    gpioSetValue(CFG_INTERFACE_IRQPORT, CFG_INTERFACE_IRQPIN, 1);
  #endif

  // init the msg ptr
  msg_ptr = msg;

  // Show the menu
  cmdMenu();

  // Set the IRQ pin low by default
  #if CFG_INTERFACE_ENABLEIRQ  != 0
    gpioSetValue(CFG_INTERFACE_IRQPORT, CFG_INTERFACE_IRQPIN, 0);
  #endif
}
Пример #12
0
uint32_t tsReadY(void)
{
  if (!_tsInitialised) tsInit();

  lcdOrientation_t orientation;
  orientation = lcdGetOrientation();

  if (orientation == LCD_ORIENTATION_LANDSCAPE)
  {
    // Make sure Y+/Y- are set to GPIO
    TS_YP_FUNC_GPIO;
    TS_YM_FUNC_GPIO;
  
    // Set X- and X+ to inputs
    gpioSetDir (TS_XM_PORT, TS_XM_PIN, 0);
    gpioSetDir (TS_XP_PORT, TS_XP_PIN, 0);
    
    // Set Y- and Y+ to output
    gpioSetDir (TS_YM_PORT, TS_YM_PIN, 1);
    gpioSetDir (TS_YP_PORT, TS_YP_PIN, 1);
  
    // Y+ goes high, Y- goes low
    gpioSetValue(TS_YP_PORT, TS_YP_PIN, 1);   // 3.3V
    gpioSetValue(TS_YM_PORT, TS_YM_PIN, 0);   // GND
  
    // Set pin 1.0 (X+) to ADC1
    TS_XP_FUNC_ADC;  
  
    // Return the ADC results
    return adcRead(TS_XP_ADC_CHANNEL);
  }
  else
  {
    // Make sure X+/X- are set to GPIO
    TS_XP_FUNC_GPIO;
    TS_XM_FUNC_GPIO;
  
    // Set Y- and Y+ to inputs
    gpioSetDir (TS_YM_PORT, TS_YM_PIN, 0);
    gpioSetDir (TS_YP_PORT, TS_YP_PIN, 0);
    
    // Set X- and X+ to output
    gpioSetDir (TS_XM_PORT, TS_XM_PIN, 1);
    gpioSetDir (TS_XP_PORT, TS_XP_PIN, 1);
  
    // X+ goes high, X- goes low
    gpioSetValue(TS_XP_PORT, TS_XP_PIN, 1);   // 3.3V
    gpioSetValue(TS_XM_PORT, TS_XM_PIN, 0);   // GND
  
    // Set pin 0.11 (Y+) to ADC0
    TS_YP_FUNC_ADC;
  
    // Return the ADC results
    return adcRead(TS_YP_ADC_CHANNEL);
  }
}
Пример #13
0
DSTATUS dataflash_initialize() {
    sspInit(0, sspClockPolarity_Low, sspClockPhase_RisingEdge);

    gpioSetDir(RB_SPI_CS_DF, gpioDirection_Output);

    dataflash_resume();
    status &= ~STA_NOINIT;
    return status;
}
Пример #14
0
void stepperInit(uint32_t steps)
{
  // Setup motor control pins
  gpioSetDir(STEPPER_IN1_PORT, STEPPER_IN1_PIN, 1);
  gpioSetDir(STEPPER_IN2_PORT, STEPPER_IN2_PIN, 1);
  gpioSetDir(STEPPER_IN3_PORT, STEPPER_IN3_PIN, 1);
  gpioSetDir(STEPPER_IN4_PORT, STEPPER_IN4_PIN, 1);

  gpioSetValue(STEPPER_IN1_PORT, STEPPER_IN1_PIN, 0);
  gpioSetValue(STEPPER_IN2_PORT, STEPPER_IN2_PIN, 0);
  gpioSetValue(STEPPER_IN3_PORT, STEPPER_IN3_PIN, 0);
  gpioSetValue(STEPPER_IN4_PORT, STEPPER_IN4_PIN, 0);

  // Set the number of steps per rotation
  stepperStepsPerRotation = steps;

  // Set the default speed (2 rotations per second)
  stepperSetSpeed(120);
}
uint8_t i2cInit(uint8_t instance, uint32_t clock){
    uint8_t i;

    if(instance >= I2C_NUM_INSTANCES) return I2C_ERR_INVAL;

    gpioSetFun(pins[instance][0], GPIO_FUNC_GPIO);
    gpioSetFun(pins[instance][1], GPIO_FUNC_GPIO);
    gpioSetPullup(pins[instance][0], 1);
    gpioSetPullup(pins[instance][1], 1);
    gpioSetDir(pins[instance][0], 0);
    gpioSetDir(pins[instance][1], 0);
    gpioSetVal(pins[instance][0], 0);
    gpioSetVal(pins[instance][1], 0);
    sdaHi(instance);
    sclHi(instance);
    delayTicks[instance] = (TICKS_PER_MS * 1000) / (8 * clock * 2);
    i2cDelay(instance);
    i2cDelay(instance);
    for (i = 0; i < 100; i++)       // Try to reset the bus
        i2cBitTx(instance, 1);
    i2cStop(instance);

#if !I2C_BITBANG
    PMC_EnablePeripheral(instance ? ID_TWI1 : ID_TWI0);
    gpioSetFun(pins[instance][0], GPIO_FUNC_A);
    gpioSetFun(pins[instance][1], GPIO_FUNC_A);
    TWI_ConfigureMaster(instance ? TWI1 : TWI0, clock, BOARD_MCK);

#if I2C_INT_PDC
    NVIC_EnableIRQ(instance ? TWI1_IRQn : TWI0_IRQn);
    if (instance)
        TWI1->TWI_IDR = 0x00F77;
    else
        TWI0->TWI_IDR = 0x00F77;
#endif

#endif


    return I2C_ALL_OK;
}
Пример #16
0
void avrspi_select(void) {
	// init the ssp on SPI port 0 (SPI1 would be awesome)
	sspInit(0, sspClockPolarity_Low, sspClockPhase_RisingEdge);

	// set ssp clock divider so avr can handle bus speed (see avr.h)
	SCB_SSP0CLKDIV = AVR_DIVIDER;
	
	// set direction of RESET to output and bring hi (off)
	avrspi_setReset(1);
	gpioSetDir(AVR_RESET, gpioDirection_Output);
	avrspi_setReset(1);
}
Пример #17
0
void lcdInit(void)
{
  // Set control pins to output
  gpioSetDir(ST7735_PORT, ST7735_RS_PIN, 1);
  gpioSetDir(ST7735_PORT, ST7735_SDA_PIN, 1);
  gpioSetDir(ST7735_PORT, ST7735_SCL_PIN, 1);
  gpioSetDir(ST7735_PORT, ST7735_CS_PIN, 1);
  gpioSetDir(ST7735_PORT, ST7735_RES_PIN, 1);
  gpioSetDir(ST7735_PORT, ST7735_BL_PIN, 1);

  // Set pins low by default (except reset)
  CLR_RS;
  CLR_SDA;
  CLR_SCL;
  CLR_CS;
  CLR_BL;
  SET_RES;
  
  // Turn backlight on
  lcdBacklight(TRUE);

  // Reset display
  CLR_RES;
  systickDelay(50);
  SET_RES;

  // Run LCD init sequence
  st7735InitDisplay();

  // Fill black
  lcdFillRGB(COLOR_BLACK);
}
Пример #18
0
void LightCheck(void){
    int iocon;
    char iodir;

    iocon=IOCON_PIO1_11;
//    iodir=gpioGetDir(RB_LED3);
    iodir= (GPIO_GPIO1DIR & (1 << (RB_LED3) ))?1:0;

    gpioSetDir(RB_LED3, gpioDirection_Input);
    IOCON_PIO1_11 = IOCON_PIO1_11_FUNC_AD7|IOCON_PIO1_11_ADMODE_ANALOG;
    light-=light/SAMPCT;
	light += (adcRead(7)/2);
    
    gpioSetDir(RB_LED3, iodir);
    IOCON_PIO1_11=iocon;

    if(_isnight && light/SAMPCT>(threshold+RANGE))
        _isnight=0;

    if(!_isnight && light/SAMPCT<threshold)
        _isnight=1;
};
Пример #19
0
static void init_lilakit(void)
{

    lkEnabled = (lkSetI2C(LK_I2C_CR_LS0, LK_I2C_LS_OFF << 0) == I2CSTATE_ACK); // probe i2c
    if (lkEnabled == 0) {
	return;
    }

    // All LEDs off
    lkSetI2C(LK_I2C_CR_LS0, LK_I2C_LS_OFF << 0);
    lkSetI2C(LK_I2C_CR_LS0, LK_I2C_LS_OFF << 2);
    lkSetI2C(LK_I2C_CR_LS0, LK_I2C_LS_OFF << 4);
    lkSetI2C(LK_I2C_CR_LS0, LK_I2C_LS_OFF << 6);
    lkSetI2C(LK_I2C_CR_LS1, LK_I2C_LS_OFF << 0);
    lkSetI2C(LK_I2C_CR_LS1, LK_I2C_LS_OFF << 2);
    lkSetI2C(LK_I2C_CR_LS1, LK_I2C_LS_OFF << 4);
    lkSetI2C(LK_I2C_CR_LS1, LK_I2C_LS_OFF << 6);
    lkSetI2C(LK_I2C_CR_LS2, LK_I2C_LS_OFF << 0);
    lkSetI2C(LK_I2C_CR_LS2, LK_I2C_LS_OFF << 2);
    lkSetI2C(LK_I2C_CR_LS2, LK_I2C_LS_OFF << 4);
    lkSetI2C(LK_I2C_CR_LS2, LK_I2C_LS_OFF << 6);
    lkSetI2C(LK_I2C_CR_LS3, LK_I2C_LS_OFF << 0);
    lkSetI2C(LK_I2C_CR_LS3, LK_I2C_LS_OFF << 2);
    lkSetI2C(LK_I2C_CR_LS3, LK_I2C_LS_OFF << 4);
    lkSetI2C(LK_I2C_CR_LS3, LK_I2C_LS_OFF << 6);

    // All PWMs off
    lkSetI2C(LK_I2C_CR_PSC0, 0x00);
    lkSetI2C(LK_I2C_CR_PWM0, 0x00);
    lkSetI2C(LK_I2C_CR_PSC1, 0x00);
    lkSetI2C(LK_I2C_CR_PWM1, 0x00);

    // Prepare SS
    gpioSetDir(LK_PIEZO, gpioDirection_Output);
    gpioSetValue(LK_PIEZO, 1);

    // Prepare blinking
    lkSetI2C(LK_I2C_CR_PSC0, 0x23);
    lkSetI2C(LK_I2C_CR_PWM0, 0x66);
    lkSetI2C(LK_I2C_CR_PSC1, 0x75);
    lkSetI2C(LK_I2C_CR_PWM1, 0x12);

    // Enable both LEDs
    lk_ls1 |= LK_I2C_LS_PWM0 << 4;
    lk_ls1 |= LK_I2C_LS_PWM1 << 6;
    lkSetI2C(LK_I2C_CR_LS1, lk_ls1);
}
Пример #20
0
void init ()
{
	cpuInit ();
	systickInit (1);
	gpioInit ();
	pmuInit ();

	CDC_Init ();
	USB_Init ();
	USB_Connect (TRUE);

	gpioSetDir (3, 0, 1);
	gpioSetValue (3, 0, 1);
	while (!USB_Configuration) {
		systickDelay (10);
	}
	gpioSetValue (3, 0, 0); 
}
Пример #21
0
// every 10 ms
void tick_default(void) {
    static int ctr;
    ctr++;
    if(ctr>100){
        VoltageCheck();
        ctr=0;
    };
    if(ctr%5==0){
        if(GetVoltage()<3600){
            IOCON_PIO1_11 = 0x0;
            gpioSetDir(RB_LED3, gpioDirection_Output);
            if( (ctr/5)%10 == 1 )
                gpioSetValue (RB_LED3, 1);
            else
                gpioSetValue (RB_LED3, 0);
        };
    };

    return;
};
Пример #22
0
int main(void)
{
  cpuInit();

  int i=0;
  for (i=1; i<5; ++i)
  {
    gpioSetDir(2, i, gpioDirection_Output);
    gpioSetValue(2, i, 0);
  }

  gpioSetValue(2,1,1); // System started

  xTaskCreate(leduj, "LEDx", configMINIMAL_STACK_SIZE, NULL, 1, NULL);
  gpioSetValue(2,1,1); // System started
  xTaskCreate(leduj2, "LEDy", configMINIMAL_STACK_SIZE, NULL, 1, NULL);

  vTaskStartScheduler();

  // This code should never be reached
  panic();
}
Пример #23
0
void initDiscretes(void)
{

	gpioInit();
	
	// inputs
	gpioSetDir(IRQ_N, 0);
	gpioSetDir(GPIO0, 0);
	gpioSetDir(GPIO1, 0);
	gpioSetDir(GPIO2, 0);
	
	// outputs
    gpioSetDir(RXANT, 1);
	gpioSetDir(TXANT, 1);
	gpioSetDir(EN_N,  1);

	// set gpio initial output values
	gpioWrite(RXANT, 1);
	gpioWrite(TXANT, 0);
	gpioWrite(EN_N, 0);
}
Пример #24
0
void rbBacklightInit(void) {
  /* Enable the clock for CT16B1 */
  SCB_SYSAHBCLKCTRL |= (SCB_SYSAHBCLKCTRL_CT16B1);

  /* Configure PIO1.9 as Timer1_16 MAT0 Output */
  IOCON_PIO1_9 &= ~IOCON_PIO1_9_FUNC_MASK;
  IOCON_PIO1_9 |=  IOCON_PIO1_9_FUNC_CT16B1_MAT0;

  /* Set default duty cycle (MR1) */
  TMR_TMR16B1MR0 = 0; //(0xFFFF * (100 - brightness)) / 100;

  /* External Match Register Settings for PWM */
  TMR_TMR16B1EMR = TMR_TMR16B1EMR_EMC0_TOGGLE | TMR_TMR16B1EMR_EM0;

  /* enable Timer1 */
  TMR_TMR16B1TCR = TMR_TMR16B1TCR_COUNTERENABLE_ENABLED;

  /* Enable PWM0 */
  TMR_TMR16B1PWMC = TMR_TMR16B1PWMC_PWM0_ENABLED;

  // Enable Step-UP
  gpioSetDir(RB_PWR_LCDBL, gpioDirection_Output);
  gpioSetValue(RB_PWR_LCDBL, 0);
}
Пример #25
0
void lcdInit(void) {
    int id;

    sspInit(0, sspClockPolarity_Low, sspClockPhase_RisingEdge);

    gpioSetValue(RB_LCD_CS, 1);
    gpioSetValue(RB_LCD_RST, 1);

    gpioSetDir(RB_LCD_CS, gpioDirection_Output);
    gpioSetDir(RB_LCD_RST, gpioDirection_Output);

    delayms(100);
    gpioSetValue(RB_LCD_RST, 0);
    delayms(100);
    gpioSetValue(RB_LCD_RST, 1);
    delayms(100);

    id=lcdRead(220); // ID3
    
    if(id==14)
        displayType=DISPLAY_N1600;
    else /* ID3 == 48 */
        displayType=DISPLAY_N1200;
    
/* Small Nokia 1200 LCD docs:
 *           clear/ set
 *  on       0xae / 0xaf
 *  invert   0xa6 / 0xa7
 *  mirror-x 0xA0 / 0xA1
 *  mirror-y 0xc7 / 0xc8
 *
 *  0x20+x contrast (0=black - 0x2e)
 *  0x40+x offset in rows from top (-0x7f)
 *  0x80+x contrast? (0=black -0x9f?)
 *  0xd0+x black lines from top? (-0xdf?)
 *
 */
    lcd_select();

    if(displayType==DISPLAY_N1200){
        uint8_t initseq[]= { 0xE2,0xAF, // Display ON
                             0xA1, // Mirror-X
                             0xA4, 0x2F, 0xB0, 0x10};
        int i = 0;
        while(i<sizeof(initseq)){
            lcdWrite(TYPE_CMD,initseq[i++]);
            delayms(5); // actually only needed after the first
        }
    }else{ /* displayType==DISPLAY_N1600 */
        uint8_t initseq_d[] = {  
                               0x36,
                               0x29, 0xBA, 0x07,
                               0x15, 0x25, 0x3f,
                               0x11, 0x13, 0x37,
                               0x00, 0x3A, 0x05,
                               0x2A, 0, 98-1,
                               0x2B, 0, 70-1};
        uint32_t initseq_c = ~ 0x12BA7; // command/data bitstring
        int i = 0;
        lcdWrite(TYPE_CMD,0x01); //sw reset
        delayms(10);

        while(i<sizeof(initseq_d)){
            lcdWrite(initseq_c&1, initseq_d[i++]);
            initseq_c = initseq_c >> 1;
        }
    }
    lcd_deselect();
}
Пример #26
0
void rbInit() {
  RB_HB0_IO &= ~IOCON_SWDIO_PIO1_3_FUNC_MASK;
  RB_HB0_IO |=  IOCON_SWDIO_PIO1_3_FUNC_GPIO;
  RB_HB1_IO &= ~IOCON_JTAG_TCK_PIO0_10_FUNC_MASK;
  RB_HB1_IO |=  IOCON_JTAG_TCK_PIO0_10_FUNC_GPIO;

  struct {
    int port;
    int pin;
    uint32_t volatile *reg;
    gpioPullupMode_t mode;
  } const input_pins[] = {
    { RB_BTN0    , &RB_BTN0_IO    , gpioPullupMode_PullUp },
    { RB_BTN1    , &RB_BTN1_IO    , gpioPullupMode_PullUp },
    { RB_BTN2    , &RB_BTN2_IO    , gpioPullupMode_PullUp },
    { RB_BTN3    , &RB_BTN3_IO    , gpioPullupMode_PullUp },
    { RB_BTN4    , &RB_BTN4_IO    , gpioPullupMode_PullUp },
    { RB_HB0     , &RB_HB0_IO     , gpioPullupMode_PullUp },
    { RB_HB1     , &RB_HB1_IO     , gpioPullupMode_PullUp },
    { RB_PWR_CHRG, &RB_PWR_CHRG_IO, gpioPullupMode_PullUp }
  };

  for(int i = 0; i < ARRAY_SIZE(input_pins); ++i) {
    gpioSetDir(input_pins[i].port, input_pins[i].pin, gpioDirection_Input);
    gpioSetPullup(input_pins[i].reg, input_pins[i].mode);
  }

  // LED3 zur Bestimmung der Umgebungshelligkeit.
  gpioSetDir(RB_LED3, gpioDirection_Input);
  RB_LED3_IO = (RB_LED3_IO & IOCON_PIO1_11_FUNC_MASK) | IOCON_PIO1_11_FUNC_AD7;

  // prepare LEDs
  IOCON_JTAG_TDI_PIO0_11 &= ~IOCON_JTAG_TDI_PIO0_11_FUNC_MASK;
  IOCON_JTAG_TDI_PIO0_11 |=  IOCON_JTAG_TDI_PIO0_11_FUNC_GPIO;

  struct {
    int port;
    int pin;
    int value;
  } const output_pins[] = {
    { RB_PWR_GOOD , 0 },
    { USB_CONNECT , 1 },
    { RB_LCD_CS   , 1 },
    { RB_SPI_CS_DF, 1 },
    { RB_SPI_SS2 , 1 },
    { RB_SPI_SS3 , 1 },
    { RB_SPI_SS4 , 1 },
    { RB_SPI_SS5 , 1 },
    { RB_LED0    , 0 },
    { RB_LED1    , 0 },
    { RB_LED2    , 0 },
    { RB_LCD_BL  , 0 },
    { RB_HB2     , 1 },
    { RB_HB3     , 1 },
    { RB_HB4     , 1 },
    { RB_HB5     , 1 }
  };

  for(int i = 0; i < ARRAY_SIZE(output_pins); ++i) {
    gpioSetDir  (output_pins[i].port, output_pins[i].pin, gpioDirection_Output);
    gpioSetValue(output_pins[i].port, output_pins[i].pin, output_pins[i].value);
  }

  // Set P0.0 to GPIO
  RB_PWR_LCDBL_IO &= ~RB_PWR_LCDBL_IO_FUNC_MASK;
  RB_PWR_LCDBL_IO |=  RB_PWR_LCDBL_IO_FUNC_GPIO;

  gpioSetDir   ( RB_PWR_LCDBL   , gpioDirection_Input);
  gpioSetPullup(&RB_PWR_LCDBL_IO, gpioPullupMode_Inactive);

  rbBacklightInit();
  badge_display_init();
}
Пример #27
0
void ram(void)
{
    lcdLoadImage("sr.lcd");
    lcdRefresh();

    char key;
    unsigned int i;
    unsigned int cnt = 0;
    unsigned char x = 0;
    unsigned const int cnt_max = 2560;
    unsigned const char x_max = 32;
    unsigned char step;

    unsigned int con_2_4 = IOCON_PIO2_4;
    unsigned int con_2_5 = IOCON_PIO2_5;

    unsigned char led[10];
    IOCON_PIO1_11 = 0x00;

    IOCON_SWDIO_PIO1_3 = IOCON_SWDIO_PIO1_3_FUNC_GPIO;
    IOCON_PIO2_4 = IOCON_PIO2_4_FUNC_GPIO;
    IOCON_PIO2_5 = IOCON_PIO2_5_FUNC_GPIO;

    gpioSetDir(RB_LED3, gpioDirection_Output);
    gpioSetDir(RB_SPI_SS0, gpioDirection_Output);
    gpioSetDir(RB_SPI_SS1, gpioDirection_Output);
    gpioSetDir(RB_SPI_SS2, gpioDirection_Output);
    gpioSetDir(RB_SPI_SS3, gpioDirection_Output);
    gpioSetDir(RB_SPI_SS4, gpioDirection_Output);
    gpioSetDir(RB_SPI_SS5, gpioDirection_Output);

    gpioSetDir(RB_HB0, gpioDirection_Output);

    for (;;) {

        if (++cnt >= cnt_max) {
            cnt = 0;
            if (++x == x_max) {
                x = 0;
                /* set next mode here */
            }
            for (i = 0; i < 4; i++) {
                led[0] = pwm[x];
                led[1] = pwm[x];
                led[2] = pwm[(x+16) % 32];
                led[3] = pwm[(x+16) % 32];
                led[4] = pwm_bl[x];
                led[5] = pwm_bl[(x+ 4) % 32];
                led[6] = pwm_bl[(x+ 8) % 32];
                led[7] = pwm_bl[(x+12) % 32];
                led[8] = pwm_bl[(x+16) % 32];
                led[9] = pwm_bl[(x+20) % 32];
            }
//			TMR_TMR16B1MR0 = 0xFFFF * (100 - pwm_bl[x]) / 100;
        }

        step = cnt % MAX_BRIGHTNESS;

        if (step == 0) {
            gpioSetValue(RB_LED0, 1);
            gpioSetValue(RB_LED1, 1);
            gpioSetValue(RB_LED2, 1);
            gpioSetValue(RB_LED3, 1);
            gpioSetValue(RB_SPI_SS3, 1);
            gpioSetValue(RB_SPI_SS4, 1);
            gpioSetValue(RB_SPI_SS5, 1);
            gpioSetValue(RB_SPI_SS0, 1);
            gpioSetValue(RB_SPI_SS1, 1);
            gpioSetValue(RB_SPI_SS2, 1);
        }

        if (step == led[0])
            gpioSetValue(RB_LED0, 0);
        if (step == led[1])
            gpioSetValue(RB_LED1, 0);
        if (step == led[3])
            gpioSetValue(RB_LED2, 0);
        if (step == led[3])
            gpioSetValue(RB_LED3, 0);
        if (step == led[4])
            gpioSetValue(RB_SPI_SS5, 0);
        if (step == led[5])
            gpioSetValue(RB_SPI_SS4, 0);
        if (step == led[6])
            gpioSetValue(RB_SPI_SS3, 0);
        if (step == led[7])
            gpioSetValue(RB_SPI_SS2, 0);
        if (step == led[8])
            gpioSetValue(RB_SPI_SS1, 0);
        if (step == led[9])
            gpioSetValue(RB_SPI_SS0, 0);

        for (i = 0; i < 100; i++)
            __asm volatile ("nop");

        key = getInputRaw();
        if (key != BTN_NONE) {
            gpioSetValue(RB_LED0, 0);
            gpioSetValue(RB_LED1, 0);
            gpioSetValue(RB_LED2, 0);
            gpioSetValue(RB_LED3, 0);
            gpioSetValue(RB_SPI_SS0, 0);
            gpioSetValue(RB_SPI_SS1, 0);
            gpioSetValue(RB_SPI_SS2, 0);
            gpioSetValue(RB_SPI_SS3, 0);
            gpioSetValue(RB_SPI_SS4, 0);
            gpioSetValue(RB_SPI_SS5, 0);
            IOCON_PIO2_4 = con_2_4;
            IOCON_PIO2_5 = con_2_5;
            return;
        }
    }
};
Пример #28
0
static void chb_radio_init()
{
    U8 ieee_addr[8];

    // reset chip
    chb_reset();

    // disable intps while we config the radio
    chb_reg_write(IRQ_MASK, 0);

    // force transceiver off while we configure the intps
    chb_reg_read_mod_write(TRX_STATE, CMD_FORCE_TRX_OFF, 0x1F);

    // make sure the transceiver is in the off state before proceeding
    while ((chb_reg_read(TRX_STATUS) & 0x1f) != TRX_OFF);

    // set radio cfg parameters
    // **note** uncomment if these will be set to something other than default
    //chb_reg_read_mod_write(XAH_CTRL_0, CHB_MAX_FRAME_RETRIES << CHB_MAX_FRAME_RETRIES_POS, 0xF << CHB_MAX_FRAME_RETRIES_POS);
    //chb_reg_read_mod_write(XAH_CTRL_0, CHB_MAX_CSMA_RETRIES << CHB_MAX_CSMA_RETIRES_POS, 0x7 << CHB_MAX_CSMA_RETIRES_POS);
    //chb_reg_read_mod_write(CSMA_SEED_1, CHB_CSMA_SEED1 << CHB_CSMA_SEED1_POS, 0x7 << CHB_CSMA_SEED1_POS);
    //chb_ret_write(CSMA_SEED0, CHB_CSMA_SEED0);     
    //chb_reg_read_mod_write(PHY_CC_CCA, CHB_CCA_MODE << CHB_CCA_MODE_POS,0x3 << CHB_CCA_MODE_POS);
    //chb_reg_write(CCA_THRES, CHB_CCA_ED_THRES);

    // set frame version that we'll accept
    chb_reg_read_mod_write(CSMA_SEED_1, CHB_FRM_VER << CHB_FVN_POS, 3 << CHB_FVN_POS);

    // set interrupt mask
    // re-enable intps while we config the radio
    chb_reg_write(IRQ_MASK, (1<<IRQ_RX_START) | (1<<IRQ_TRX_END));

    #if (CFG_CHIBI_PROMISCUOUS == 0)
      // set autocrc mode
      chb_reg_read_mod_write(TRX_CTRL_1, 1 << CHB_AUTO_CRC_POS, 1 << CHB_AUTO_CRC_POS);
    #endif
    // set up default phy modulation, data rate and power (Ex. OQPSK, 100 kbps, 868 MHz, 3dBm)
    chb_set_mode(CFG_CHIBI_MODE);       // Defined in projectconfig.h
    chb_set_pwr(CFG_CHIBI_POWER);       // Defined in projectconfig.h
    chb_set_channel(CFG_CHIBI_CHANNEL); // Defined in projectconfig.h

    // set fsm state
    // put trx in rx auto ack mode
    chb_set_state(RX_STATE);

    // set pan ID
    chb_reg_write16(PAN_ID_0, CFG_CHIBI_PANID); // Defined in projectconfig.h

    // set short addr
    // NOTE: Possibly get this from EEPROM
    chb_reg_write16(SHORT_ADDR_0, chb_get_short_addr());

    // set long addr
    // NOTE: Possibly get this from EEPROM
    chb_get_ieee_addr(ieee_addr);
    chb_reg_write64(IEEE_ADDR_0, ieee_addr);

#if (CHB_CC1190_PRESENT)
    // set high gain mode pin to output and init to zero
    gpioSetDir (CHB_CC1190_HGM_PORT, CHB_CC1190_HGM_PIN, 1);
    gpioSetPullup (&CHB_CC1190_HGM_IOCONREG, gpioPullupMode_Inactive);
    gpioSetValue (CHB_CC1190_HGM_PORT, CHB_CC1190_HGM_PIN, 0);

    // set external power amp on AT86RF212
    chb_reg_read_mod_write(TRX_CTRL_1, 1<<CHB_PA_EXT_EN_POS, 1<<CHB_PA_EXT_EN_POS);

    // set power to lowest level possible
    chb_set_pwr(0xd);   // set to -11 dBm
#endif

    // set interrupt/gpio pin to input
    gpioSetDir (CHB_EINTPORT, CHB_EINTPIN, 0);

    // set internal resistor on EINT pin to inactive
    gpioSetPullup (&CHB_EINTPIN_IOCONREG, gpioPullupMode_Inactive);

    // configure pin for interrupt
    gpioSetInterrupt (CHB_EINTPORT,
                      CHB_EINTPIN,
                      gpioInterruptSense_Edge,        // Edge-sensitive
                      gpioInterruptEdge_Single,       // Single edge
                      gpioInterruptEvent_ActiveHigh); // High triggers interrupt

    // enable interrupt
    gpioIntEnable (CHB_EINTPORT,
                   CHB_EINTPIN); 

    if (chb_get_state() != RX_STATE)
    {
        // ERROR occurred initializing the radio. Print out error message.
        printf(chb_err_init);
    }
}
void cmd_sysinfo(uint8_t argc, char **argv)
{
  printf("%-25s : %d.%d MHz %s", "System Clock", CFG_CPU_CCLK / 1000000, CFG_CPU_CCLK % 1000000, CFG_PRINTF_NEWLINE);
  printf("%-25s : %d.%d.%d %s", "Firmware", CFG_FIRMWARE_VERSION_MAJOR, CFG_FIRMWARE_VERSION_MINOR, CFG_FIRMWARE_VERSION_REVISION, CFG_PRINTF_NEWLINE);

  // 128-bit MCU Serial Number
  IAP_return_t iap_return;
  iap_return = iapReadSerialNumber();
  if(iap_return.ReturnCode == 0)
  {
    printf("%-25s : %08X %08X %08X %08X %s", "Serial Number", iap_return.Result[0],iap_return.Result[1],iap_return.Result[2],iap_return.Result[3], CFG_PRINTF_NEWLINE);
  }

  // Check the battery voltage
  #ifdef CFG_BAT
    uint32_t c;
    gpioSetDir(CFG_BAT_ENPORT, CFG_BAT_ENPIN, gpioDirection_Output );   
    gpioSetValue(CFG_BAT_ENPORT, CFG_BAT_ENPIN, 1 );    // Enable the voltage divider
    systickDelay(5);
    c = adcRead(CFG_BAT_ADC);                           // Pre-read ADC to warm it up
    systickDelay(10);
    c = adcRead(CFG_BAT_ADC);
    c = (c * CFG_VREG_VCC_MAIN) / 1000;                 // Value in millivolts relative to supply voltage
    c = (c * CFG_BAT_MULTIPLIER) / 1000;                // Battery voltage in millivolts (depends on resistor values)
    gpioSetValue(CFG_BAT_ENPORT, CFG_BAT_ENPIN, 0 );    // Turn the voltage divider back off to save power
    printf("%-25s : %u.%u V %s", "Supply Voltage", (unsigned int)(c / 1000), (unsigned int)(c % 1000), CFG_PRINTF_NEWLINE);
  #endif

  // Wireless Settings (if CFG_CHIBI enabled)
  #ifdef CFG_CHIBI
    chb_pcb_t *pcb = chb_get_pcb();
    printf("%-25s : %s %s", "RF Transceiver", "AT86RF212", CFG_PRINTF_NEWLINE);
    #if CFG_CHIBI_PROMISCUOUS == 1
      printf("%-25s : %s %s", "RF Receive Mode", "Promiscuous", CFG_PRINTF_NEWLINE);
    #else
      printf("%-25s : %s %s", "RF Receive Mode", "Normal", CFG_PRINTF_NEWLINE);
    #endif
    printf("%-25s : 0x%04X (%d) %s", "802.15.4 PAN ID", CFG_CHIBI_PANID, CFG_CHIBI_PANID, CFG_PRINTF_NEWLINE);
    printf("%-25s : 0x%04X (%d) %s", "802.15.4 Node Address", pcb->src_addr, pcb->src_addr, CFG_PRINTF_NEWLINE);
    printf("%-25s : %d %s", "802.15.4 Channel", CFG_CHIBI_CHANNEL, CFG_PRINTF_NEWLINE);
  #endif

  // CLI and buffer Settings
  #ifdef CFG_INTERFACE
    printf("%-25s : %d bytes %s", "Max CLI Command", CFG_INTERFACE_MAXMSGSIZE, CFG_PRINTF_NEWLINE);
  #endif

  // System Uptime (based on systick timer)
  printf("%-25s : %u s %s", "System Uptime", (unsigned int)systickGetSecondsActive(), CFG_PRINTF_NEWLINE);

  // System Temperature (if LM75B Present)
  #ifdef CFG_LM75B
    int32_t temp = 0;
    lm75bGetTemperature(&temp);
    temp *= 125;
    printf("%-25s : %d.%d C %s", "Temperature", (int)(temp / 1000), (int)(temp % 1000), CFG_PRINTF_NEWLINE);
  #endif

  #ifdef CFG_SDCARD
    printf("%-25s : %s %s", "SD Card Present", gpioGetValue(CFG_SDCARD_CDPORT, CFG_SDCARD_CDPIN) ? "True" : "False", CFG_PRINTF_NEWLINE);
  #endif
}
Пример #30
0
Файл: ssp.c Проект: Bediko/r0ket
void sspInit (uint8_t portNum, sspClockPolarity_t polarity, sspClockPhase_t phase)
{
  gpioInit();

  if (portNum == 0)
  {
    /* Reset SSP */
    SCB_PRESETCTRL &= ~SCB_PRESETCTRL_SSP0_MASK;
    SCB_PRESETCTRL |= SCB_PRESETCTRL_SSP0_RESETDISABLED;
  
    /* Enable AHB clock to the SSP domain. */
    SCB_SYSAHBCLKCTRL |= (SCB_SYSAHBCLKCTRL_SSP0);
  
    /* Divide by 1 (SSPCLKDIV also enables to SSP CLK) */
    SCB_SSP0CLKDIV = SCB_SSP0CLKDIV_DIV1;
  
    /* Set P0.8 to SSP MISO */
    IOCON_PIO0_8 &= ~IOCON_PIO0_8_FUNC_MASK;
    IOCON_PIO0_8 |= IOCON_PIO0_8_FUNC_MISO0;
  
    /* Set P0.9 to SSP MOSI */
    IOCON_PIO0_9 &= ~IOCON_PIO0_9_FUNC_MASK;
    IOCON_PIO0_9 |= IOCON_PIO0_9_FUNC_MOSI0;

    /* Set 2.11 to SSP SCK (0.6 and 0.10 can also be used) */ 
    #ifdef CFG_SSP0_SCKPIN_2_11
    IOCON_SCKLOC = IOCON_SCKLOC_SCKPIN_PIO2_11; 
    IOCON_PIO2_11 = IOCON_PIO2_11_FUNC_SCK0;  
    #endif

    /* Set 0.6 to SSP SCK (2.11 and 0.10 can also be used) */ 
    #ifdef CFG_SSP0_SCKPIN_0_6
    IOCON_SCKLOC = IOCON_SCKLOC_SCKPIN_PIO0_6; 
    IOCON_PIO0_6 = IOCON_PIO0_6_FUNC_SCK;
    #endif

    /* Set P0.2/SSEL to GPIO output and high */
    IOCON_PIO0_2 &= ~IOCON_PIO0_2_FUNC_MASK;
    IOCON_PIO0_2 |= IOCON_PIO0_2_FUNC_GPIO;
    gpioSetDir(SSP0_CSPORT, SSP0_CSPIN, 1);
    gpioSetValue(SSP0_CSPORT, SSP0_CSPIN, 1);
    gpioSetPullup(&IOCON_PIO0_2, gpioPullupMode_Inactive);  // Board has external pull-up
  
    /* If SSP0CLKDIV = DIV1 -- (PCLK / (CPSDVSR × [SCR+1])) = (72,000,000 / (2 x [8 + 1])) = 4.0 MHz */
    uint32_t configReg = ( SSP_SSP0CR0_DSS_8BIT   // Data size = 8-bit
                  | SSP_SSP0CR0_FRF_SPI           // Frame format = SPI
                  | SSP_SSP0CR0_SCR_8);           // Serial clock rate = 8
  
    // Set clock polarity
    if (polarity == sspClockPolarity_High)
      configReg |= SSP_SSP0CR0_CPOL_HIGH;     // Clock polarity = High between frames
    else
      configReg &= ~SSP_SSP0CR0_CPOL_MASK;    // Clock polarity = Low between frames
  
    // Set edge transition
    if (phase == sspClockPhase_FallingEdge)
      configReg |= SSP_SSP0CR0_CPHA_SECOND;   // Clock out phase = Trailing edge clock transition
    else
      configReg &= ~SSP_SSP0CR0_CPHA_MASK;    // Clock out phase = Leading edge clock transition
  
    // Assign config values to SSP0CR0
    SSP_SSP0CR0 = configReg;
  
    /* Clock prescale register must be even and at least 2 in master mode */
    SSP_SSP0CPSR = SSP_SSP0CPSR_CPSDVSR_DIV2;
  
    /* Clear the Rx FIFO */
    uint8_t i, Dummy=Dummy;
    for ( i = 0; i < SSP_FIFOSIZE; i++ )
    {
      Dummy = SSP_SSP0DR;
    }
  
    /* Enable the SSP Interrupt */
    NVIC_EnableIRQ(SSP_IRQn);
  
    /* Set SSPINMS registers to enable interrupts
     * enable all error related interrupts        */
    SSP_SSP0IMSC = ( SSP_SSP0IMSC_RORIM_ENBL      // Enable overrun interrupt
                   | SSP_SSP0IMSC_RTIM_ENBL);     // Enable timeout interrupt
  
    /* Enable device and set it to master mode, no loopback */
    SSP_SSP0CR1 = SSP_SSP0CR1_SSE_ENABLED | SSP_SSP0CR1_MS_MASTER | SSP_SSP0CR1_LBM_NORMAL;
  }

  return;
}