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(); }
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); };
void ds18b20Init(uint32_t portNumber, uint32_t bitPosition, volatile uint32_t *ioconReg) { portNum = portNumber; bitPos = bitPosition; timer16Init(1, TIMER16_DEFAULTINTERVAL); timer16Enable(1); gpioSetPullup(ioconReg, gpioPullupMode_Inactive); }
void usbHIDInit (void) { // Setup USB clock SCB_PDRUNCFG &= ~(SCB_PDSLEEPCFG_USBPAD_PD); // Power-up USB PHY SCB_PDRUNCFG &= ~(SCB_PDSLEEPCFG_USBPLL_PD); // Power-up USB PLL SCB_USBPLLCLKSEL = SCB_USBPLLCLKSEL_SOURCE_MAINOSC; // Select PLL Input SCB_USBPLLCLKUEN = SCB_USBPLLCLKUEN_UPDATE; // Update Clock Source SCB_USBPLLCLKUEN = SCB_USBPLLCLKUEN_DISABLE; // Toggle Update Register SCB_USBPLLCLKUEN = SCB_USBPLLCLKUEN_UPDATE; // Wait until the USB clock is updated while (!(SCB_USBPLLCLKUEN & SCB_USBPLLCLKUEN_UPDATE)); // Set USB clock to 48MHz (12MHz x 4) SCB_USBPLLCTRL = (SCB_USBPLLCTRL_MULT_4); while (!(SCB_USBPLLSTAT & SCB_USBPLLSTAT_LOCK)); // Wait Until PLL Locked SCB_USBCLKSEL = SCB_USBCLKSEL_SOURCE_USBPLLOUT; // Set USB pin functions IOCON_PIO0_1 &= ~IOCON_PIO0_1_FUNC_MASK; IOCON_PIO0_1 |= IOCON_PIO0_1_FUNC_CLKOUT; // CLK OUT IOCON_PIO0_3 &= ~IOCON_PIO0_3_FUNC_MASK; IOCON_PIO0_3 |= IOCON_PIO0_3_FUNC_USB_VBUS; // VBus IOCON_PIO0_6 &= ~IOCON_PIO0_6_FUNC_MASK; IOCON_PIO0_6 |= IOCON_PIO0_6_FUNC_USB_CONNECT; // Soft Connect // Disable internal resistor on VBUS (0.3) gpioSetPullup(&IOCON_PIO0_3, gpioPullupMode_Inactive); // HID Device Info volatile int n; HidDevInfo.idVendor = USB_VENDOR_ID; HidDevInfo.idProduct = USB_PROD_ID; HidDevInfo.bcdDevice = USB_DEVICE; HidDevInfo.StrDescPtr = (uint32_t)&USB_HIDStringDescriptor[0]; HidDevInfo.InReportCount = sizeof(usbhid_out_t); HidDevInfo.OutReportCount = 1; HidDevInfo.SampleInterval = 0x20; HidDevInfo.InReport = usbHIDGetInReport; HidDevInfo.OutReport = usbHIDSetOutReport; DeviceInfo.DevType = USB_DEVICE_CLASS_HUMAN_INTERFACE; DeviceInfo.DevDetailPtr = (uint32_t)&HidDevInfo; /* Enable Timer32_1, IOCON, and USB blocks (for USB ROM driver) */ SCB_SYSAHBCLKCTRL |= (SCB_SYSAHBCLKCTRL_CT32B1 | SCB_SYSAHBCLKCTRL_IOCON | SCB_SYSAHBCLKCTRL_USB_REG); /* Use pll and pin init function in rom */ /* Warning: This will also set the system clock to 48MHz! */ // (*rom)->pUSBD->init_clk_pins(); /* insert a delay between clk init and usb init */ for (n = 0; n < 75; n++) {__asm("nop");} (*rom)->pUSBD->init(&DeviceInfo); /* USB Initialization */ (*rom)->pUSBD->connect(true); /* USB Connect */ }
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; }
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(); }
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 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; }
void usbMSCInit(void) { dataflash_initialize(); // Setup USB clock SCB_PDRUNCFG &= ~(SCB_PDSLEEPCFG_USBPAD_PD); // Power-up USB PHY SCB_PDRUNCFG &= ~(SCB_PDSLEEPCFG_USBPLL_PD); // Power-up USB PLL SCB_USBPLLCLKSEL = SCB_USBPLLCLKSEL_SOURCE_MAINOSC; // Select PLL Input SCB_USBPLLCLKUEN = SCB_USBPLLCLKUEN_UPDATE; // Update Clock Source SCB_USBPLLCLKUEN = SCB_USBPLLCLKUEN_DISABLE; // Toggle Update Register SCB_USBPLLCLKUEN = SCB_USBPLLCLKUEN_UPDATE; // Wait until the USB clock is updated while (!(SCB_USBPLLCLKUEN & SCB_USBPLLCLKUEN_UPDATE)); // Set USB clock to 48MHz (12MHz x 4) SCB_USBPLLCTRL = (SCB_USBPLLCTRL_MULT_4); while (!(SCB_USBPLLSTAT & SCB_USBPLLSTAT_LOCK)); // Wait Until PLL Locked SCB_USBCLKSEL = SCB_USBCLKSEL_SOURCE_USBPLLOUT; // Set USB pin functions // bsx says, its only needed for usb-hid. And it conflicts with btn_0 // IOCON_PIO0_1 &= ~IOCON_PIO0_1_FUNC_MASK; // IOCON_PIO0_1 |= IOCON_PIO0_1_FUNC_CLKOUT; // CLK OUT IOCON_PIO0_3 &= ~IOCON_PIO0_3_FUNC_MASK; IOCON_PIO0_3 |= IOCON_PIO0_3_FUNC_USB_VBUS; // VBus IOCON_PIO0_6 &= ~IOCON_PIO0_6_FUNC_MASK; IOCON_PIO0_6 |= IOCON_PIO0_6_FUNC_USB_CONNECT; // Soft Connect // Disable internal resistor on VBUS (0.3) gpioSetPullup(&IOCON_PIO0_3, gpioPullupMode_Inactive); // workaround for long connect delay *((uint32_t *)(0x10000054)) = 0x0; // HID Device Info volatile int n; MscDevInfo.idVendor = USB_VENDOR_ID; MscDevInfo.idProduct = USB_PROD_ID; MscDevInfo.bcdDevice = USB_DEVICE; MscDevInfo.StrDescPtr = (uint32_t)&USB_MSCStringDescriptor[0]; MscDevInfo.MSCInquiryStr = (uint32_t)&"r0ket DataFlash "; // 28 char response to SCSI INQUIRY MscDevInfo.BlockCount = 1024; MscDevInfo.BlockSize = 512; MscDevInfo.MemorySize = 1024*512; MscDevInfo.MSC_Write = &usbMSCWrite; MscDevInfo.MSC_Read = &usbMSCRead; DeviceInfo.DevType = USB_DEVICE_CLASS_STORAGE; DeviceInfo.DevDetailPtr = (uint32_t)&MscDevInfo; /* Enable Timer32_1, IOCON, and USB blocks (for USB ROM driver) */ SCB_SYSAHBCLKCTRL |= (SCB_SYSAHBCLKCTRL_CT32B1 | SCB_SYSAHBCLKCTRL_IOCON | SCB_SYSAHBCLKCTRL_USB_REG); /* Use pll and pin init function in rom */ /* Warning: This will also set the system clock to 48MHz! */ // (*rom)->pUSBD->init_clk_pins(); /* insert a delay between clk init and usb init */ for (n = 0; n < 75; n++) {__asm("nop");} (*rom)->pUSBD->init(&DeviceInfo); /* USB Initialization */ (*rom)->pUSBD->connect(true); /* USB Connect */ usbMSCenabled|=USB_MSC_ENABLEFLAG; }
void pmuDeepSleep(uint32_t sleepCtrl, uint32_t wakeupSeconds) { // Setup the board for deep sleep mode, shutting down certain // peripherals and remapping pins for lower power pmuSetupHW(); SCB_PDAWAKECFG = SCB_PDRUNCFG; sleepCtrl |= (1 << 9) | (1 << 11); SCB_PDSLEEPCFG = sleepCtrl; SCB_SCR |= SCB_SCR_SLEEPDEEP; /* Configure system to run from WDT and set TMR32B0 for wakeup */ if (wakeupSeconds > 0) { // Make sure WDTOSC isn't disabled in PDSLEEPCFG SCB_PDSLEEPCFG &= ~(SCB_PDSLEEPCFG_WDTOSC_PD); // Disable 32-bit timer 0 if currently in use TMR_TMR32B0TCR = TMR_TMR32B0TCR_COUNTERENABLE_DISABLED; // Disable internal pullup on 0.1 gpioSetPullup(&IOCON_PIO0_1, gpioPullupMode_Inactive); /* Enable the clock for CT32B0 */ SCB_SYSAHBCLKCTRL |= (SCB_SYSAHBCLKCTRL_CT32B0); /* Configure 0.1 as Timer0_32 MAT2 */ IOCON_PIO0_1 &= ~IOCON_PIO0_1_FUNC_MASK; IOCON_PIO0_1 |= IOCON_PIO0_1_FUNC_CT32B0_MAT2; /* Set appropriate timer delay */ TMR_TMR32B0MR0 = PMU_WDTCLOCKSPEED_HZ * wakeupSeconds; /* Configure match control register to raise an interrupt and reset on MR0 */ TMR_TMR32B0MCR |= (TMR_TMR32B0MCR_MR0_INT_ENABLED | TMR_TMR32B0MCR_MR0_RESET_ENABLED); /* Configure external match register to set 0.1 high on match */ TMR_TMR32B0EMR &= ~(0xFF<<4); // Clear EMR config bits TMR_TMR32B0EMR |= TMR_TMR32B0EMR_EMC2_HIGH; // Set MAT2 (0.1) high on match /* Enable wakeup interrupts (any I/O pin can be used as a wakeup source) */ //NVIC_EnableIRQ(WAKEUP0_IRQn); // P0.0 NVIC_EnableIRQ(WAKEUP1_IRQn); // P0.1 (CT32B0_MAT2) //NVIC_EnableIRQ(WAKEUP2_IRQn); // P0.2 //NVIC_EnableIRQ(WAKEUP3_IRQn); // P0.3 //NVIC_EnableIRQ(WAKEUP4_IRQn); // P0.4 //NVIC_EnableIRQ(WAKEUP5_IRQn); // P0.5 //NVIC_EnableIRQ(WAKEUP6_IRQn); // P0.6 //NVIC_EnableIRQ(WAKEUP7_IRQn); // P0.7 //NVIC_EnableIRQ(WAKEUP8_IRQn); // P0.8 //NVIC_EnableIRQ(WAKEUP9_IRQn); // P0.9 //NVIC_EnableIRQ(WAKEUP10_IRQn); // P0.10 //NVIC_EnableIRQ(WAKEUP11_IRQn); // P0.11 //NVIC_EnableIRQ(WAKEUP12_IRQn); // P1.0 //NVIC_EnableIRQ(WAKEUP13_IRQn); // P1.1 //NVIC_EnableIRQ(WAKEUP14_IRQn); // P1.2 //NVIC_EnableIRQ(WAKEUP15_IRQn); // P1.3 //NVIC_EnableIRQ(WAKEUP16_IRQn); // P1.4 //NVIC_EnableIRQ(WAKEUP17_IRQn); // P1.5 //NVIC_EnableIRQ(WAKEUP18_IRQn); // P1.6 //NVIC_EnableIRQ(WAKEUP19_IRQn); // P1.7 //NVIC_EnableIRQ(WAKEUP20_IRQn); // P1.8 //NVIC_EnableIRQ(WAKEUP21_IRQn); // P1.9 //NVIC_EnableIRQ(WAKEUP22_IRQn); // P1.10 //NVIC_EnableIRQ(WAKEUP23_IRQn); // P1.11 //NVIC_EnableIRQ(WAKEUP24_IRQn); // P2.0 //NVIC_EnableIRQ(WAKEUP25_IRQn); // P2.1 //NVIC_EnableIRQ(WAKEUP26_IRQn); // P2.2 //NVIC_EnableIRQ(WAKEUP27_IRQn); // P2.3 //NVIC_EnableIRQ(WAKEUP28_IRQn); // P2.4 //NVIC_EnableIRQ(WAKEUP29_IRQn); // P2.5 //NVIC_EnableIRQ(WAKEUP30_IRQn); // P2.6 //NVIC_EnableIRQ(WAKEUP31_IRQn); // P2.7 //NVIC_EnableIRQ(WAKEUP32_IRQn); // P2.8 //NVIC_EnableIRQ(WAKEUP33_IRQn); // P2.9 //NVIC_EnableIRQ(WAKEUP34_IRQn); // P2.10 //NVIC_EnableIRQ(WAKEUP35_IRQn); // P2.11 //NVIC_EnableIRQ(WAKEUP36_IRQn); // P3.0 //NVIC_EnableIRQ(WAKEUP37_IRQn); // P3.1 //NVIC_EnableIRQ(WAKEUP38_IRQn); // P3.2 //NVIC_EnableIRQ(WAKEUP39_IRQn); // P3.3 /* Use RISING EDGE for wakeup detection. */ SCB_STARTAPRP0 |= SCB_STARTAPRP0_APRPIO0_1; /* Clear all wakeup sources */ SCB_STARTRSRP0CLR = SCB_STARTRSRP0CLR_MASK; /* Enable Port 0.1 as wakeup source. */ SCB_STARTERP0 |= SCB_STARTERP0_ERPIO0_1; // Reconfigure clock to run from WDTOSC pmuWDTClockInit(); /* Start the timer */ TMR_TMR32B0TCR = TMR_TMR32B0TCR_COUNTERENABLE_ENABLED; } __asm volatile ("WFI"); return; }
void rbInit() { // TODO FIXME special port disable ? LEDs BTNs ? // prepare power // TODO FIXME more power init needed ? chrg + volt input ? // enable external vcc gpioSetDir(RB_PWR_GOOD, gpioDirection_Output); gpioSetValue (RB_PWR_GOOD, 0); // Disable USB Connect (we don't want USB by default) gpioSetDir(USB_CONNECT, gpioDirection_Output); gpioSetValue(USB_CONNECT, 1); // prepare buttons gpioSetDir(RB_BTN0, gpioDirection_Input); gpioSetPullup (&RB_BTN0_IO, gpioPullupMode_PullUp); gpioSetDir(RB_BTN1, gpioDirection_Input); gpioSetPullup (&RB_BTN1_IO, gpioPullupMode_PullUp); gpioSetDir(RB_BTN2, gpioDirection_Input); gpioSetPullup (&RB_BTN2_IO, gpioPullupMode_PullUp); gpioSetDir(RB_BTN3, gpioDirection_Input); gpioSetPullup (&RB_BTN3_IO, gpioPullupMode_PullUp); gpioSetDir(RB_BTN4, gpioDirection_Input); gpioSetPullup (&RB_BTN4_IO, gpioPullupMode_PullUp); // prepate chrg_stat gpioSetDir(RB_PWR_CHRG, gpioDirection_Input); gpioSetPullup (&RB_PWR_CHRG_IO, gpioPullupMode_PullUp); // 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; gpioSetDir(RB_LED0, gpioDirection_Output); gpioSetValue (RB_LED0, 0); gpioSetDir(RB_LED1, gpioDirection_Output); gpioSetValue (RB_LED1, 0); gpioSetDir(RB_LED2, gpioDirection_Output); gpioSetValue (RB_LED2, 0); gpioSetDir(RB_LED3, gpioDirection_Output); gpioSetValue (RB_LED3, 0); // Set LED3 to ? IOCON_PIO1_11 = 0x41; // prepare lcd // TODO FIXME more init needed ? gpioSetDir(RB_LCD_BL, gpioDirection_Output); gpioSetValue (RB_LCD_BL, 0); // 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); // prepare I2C #ifdef __I2C_h i2cInit(I2CMASTER); // TODO FIXME via define ? #endif // prepare SPI/SS // TODO FIXME init miso/mosi/sck somehow ? gpioSetDir(RB_SPI_SS0, gpioDirection_Output); gpioSetValue (RB_SPI_SS0, 1); gpioSetDir(RB_SPI_SS1, gpioDirection_Output); gpioSetValue (RB_SPI_SS1, 1); gpioSetDir(RB_SPI_SS2, gpioDirection_Output); gpioSetValue (RB_SPI_SS2, 1); gpioSetDir(RB_SPI_SS3, gpioDirection_Output); gpioSetValue (RB_SPI_SS3, 1); gpioSetDir(RB_SPI_SS4, gpioDirection_Output); gpioSetValue (RB_SPI_SS4, 1); gpioSetDir(RB_SPI_SS5, gpioDirection_Output); gpioSetValue (RB_SPI_SS5, 1); // prepare hackerbus gpioSetDir(RB_HB0, gpioDirection_Output); gpioSetValue (RB_HB0, 1); gpioSetDir(RB_HB1, gpioDirection_Output); gpioSetValue (RB_HB1, 1); gpioSetDir(RB_HB2, gpioDirection_Output); gpioSetValue (RB_HB2, 1); gpioSetDir(RB_HB3, gpioDirection_Output); gpioSetValue (RB_HB3, 1); gpioSetDir(RB_HB4, gpioDirection_Output); gpioSetValue (RB_HB4, 1); gpioSetDir(RB_HB5, gpioDirection_Output); gpioSetValue (RB_HB5, 1); // prepare BUSINT interrupt gpioSetDir(RB_BUSINT, gpioDirection_Input); gpioSetPullup (&RB_BUSINT_IO, gpioPullupMode_PullUp); gpioSetInterrupt(RB_BUSINT, gpioInterruptSense_Edge, gpioInterruptEdge_Single, gpioInterruptEvent_ActiveLow); gpioIntEnable(RB_BUSINT); //nrf_init(); backlightInit(); font=&Font_7x8; ECIES_setup(); }
void systemInit() { cpuInit(); systickInit((CFG_CPU_CCLK / 1000) * CFG_SYSTICK_DELAY_IN_MS); gpioInit(); pmuInit(); adcInit(); // Init adc pins to avoid wasting 60uA in deep sleep #ifdef CFG_PRINTF_UART // Initialise UART with the default baud rate (set in projectconfig.h) uartInit(CFG_UART_BAUDRATE); #endif // Switch to 3.3V if TPS780 (etc.) is being used #if defined CFG_VREG_ALT_PRESENT && CFG_VREG_ALT_PRESENT == 1 gpioSetDir(CFG_VREG_ALT_PORT, CFG_VREG_ALT_PIN, gpioDirection_Output); gpioSetValue(CFG_VREG_ALT_PORT, CFG_VREG_ALT_PIN, 0); gpioSetPullup(&CFG_VREG_ALT_REG32, gpioPullupMode_Inactive); #endif // Set LED pin as output and turn LED off gpioSetDir(CFG_LED_PORT, CFG_LED_PIN, 1); gpioSetValue(CFG_LED_PORT, CFG_LED_PIN, CFG_LED_OFF); // Initialise the ST7565 128x64 pixel display #ifdef CFG_ST7565 st7565Init(); st7565ClearScreen(); // Clear the screen st7565Backlight(1); // Enable the backlight #endif // Initialise the SSD1306 OLED display #ifdef CFG_SSD1306 ssd1306Init(SSD1306_SWITCHCAPVCC); ssd1306ClearScreen(); // Clear the screen #endif // Initialise EEPROM #ifdef CFG_I2CEEPROM mcp24aaInit(); #endif // Initialise Chibi #ifdef CFG_CHIBI // Write addresses to EEPROM for the first time if necessary // uint16_t addr_short = 0x1166; // uint64_t addr_ieee = addr_short; // mcp24aaWriteBuffer(CFG_CHIBI_EEPROM_SHORTADDR, (uint8_t *)&addr_short, 2); // mcp24aaWriteBuffer(CFG_CHIBI_EEPROM_IEEEADDR, (uint8_t *)&addr_ieee, 8); chb_init(); #endif // Setup SD Card #ifdef CFG_SDCARD // Turn off SD card by default (saves power) gpioSetDir(CFG_SDCARD_ENPORT, CFG_SDCARD_ENPIN, gpioDirection_Output); /* Set enable pin to output */ gpioSetValue(CFG_SDCARD_ENPORT, CFG_SDCARD_ENPIN, 0); /* Disable card by setting ENPIN low */ gpioSetPullup(&CFG_SDCARD_ENREG32, gpioPullupMode_Inactive); #endif #ifdef CFG_LM75B // Initialise LM75B lm75bInit(); // Read temp once to make sure we are in sleep mode int32_t temp; lm75bGetTemperature(&temp); #endif #ifdef CFG_BAT // Turn off battery voltage divider by default gpioSetDir(CFG_BAT_ENPORT, CFG_BAT_ENPIN, gpioDirection_Output ); // Set voltage divider enable pin to output gpioSetValue(CFG_BAT_ENPORT, CFG_BAT_ENPIN, 0 ); // Disable the voltage divider by default gpioSetPullup(&CFG_BAT_ENREG32, gpioPullupMode_Inactive); #endif // Start the command line interface (if requested) #ifdef CFG_INTERFACE printf("%sType '?' for a list of available commands%s", CFG_PRINTF_NEWLINE, CFG_PRINTF_NEWLINE); cmdInit(); #endif }
void rbInit() { // TODO FIXME special port disable ? LEDs BTNs ? // prepare power // TODO FIXME more power init needed ? chrg + volt input ? // enable external vcc gpioSetDir(RB_PWR_GOOD, gpioDirection_Output); gpioSetValue (RB_PWR_GOOD, 0); // Disable USB Connect (we don't want USB by default) gpioSetDir(USB_CONNECT, gpioDirection_Output); gpioSetValue(USB_CONNECT, 1); static uint8_t ports[] = { RB_BTN0, RB_BTN1, RB_BTN2, RB_BTN3, RB_BTN4, RB_LED0, RB_LED1, RB_LED2, RB_SPI_SS0, RB_SPI_SS1, RB_SPI_SS2, RB_SPI_SS3, RB_SPI_SS4, RB_SPI_SS5, RB_HB0, RB_HB1, RB_HB2, RB_HB3, RB_HB4, RB_HB5}; volatile uint32_t * regs[] = {&RB_BTN0_IO, &RB_BTN1_IO, &RB_BTN2_IO, &RB_BTN3_IO, &RB_BTN4_IO}; int i = 0; while( i<10 ){ gpioSetDir(ports[i], ports[i+1], gpioDirection_Input); gpioSetPullup(regs[i/2], gpioPullupMode_PullUp); i+=2; } // prepate chrg_stat gpioSetDir(RB_PWR_CHRG, gpioDirection_Input); gpioSetPullup (&RB_PWR_CHRG_IO, gpioPullupMode_PullUp); gpioSetDir(RB_LED3, gpioDirection_Input); IOCON_PIO1_11 = 0x41; // 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; while( i<16 ){ gpioSetDir(ports[i],ports[i+1], gpioDirection_Output); gpioSetValue (ports[i], ports[i+1], 0); i+=2; } // Set LED3 to ? IOCON_PIO1_11 = 0x41; // prepare lcd // TODO FIXME more init needed ? gpioSetDir(RB_LCD_BL, gpioDirection_Output); gpioSetValue (RB_LCD_BL, 0); // 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); // prepare I2C #ifdef __I2C_h i2cInit(I2CMASTER); // TODO FIXME via define ? #endif // prepare SPI/SS // TODO FIXME init miso/mosi/sck somehow ? // prepare hackerbus while(i<sizeof(ports)){ gpioSetDir(ports[i],ports[i+1], gpioDirection_Output); gpioSetValue (ports[i], ports[i+1], 1); i+=2; } // prepare BUSINT interrupt //gpioSetDir(RB_BUSINT, gpioDirection_Input); //gpioSetPullup (&RB_BUSINT_IO, gpioPullupMode_PullUp); //gpioSetInterrupt(RB_BUSINT, gpioInterruptSense_Edge, gpioInterruptEdge_Single, gpioInterruptEvent_ActiveLow); //gpioIntEnable(RB_BUSINT); //nrf_init(); backlightInit(); font=&Font_7x8; }