/***************************************************************************//** * @brief * SPI/USART Initialization * * @param[in] bit_rate * Bit rate for SPI Bus in MHz: Either 2000000 or 12000000 * ******************************************************************************/ void spi_init(int bit_rate) { CMU_ClockEnable(cmuClock_GPIO, true); CMU_ClockEnable(SPI_cmuClock, true); gpio_init(spi_pins, sizeof(spi_pins)/sizeof(gpio_init_t)); USART_Reset(SPI_PORT); USART_InitSync(SPI_PORT, &spiInit); SPI_PORT->CTRL |= USART_CTRL_TXBIL_HALFFULL; SPI_PORT->ROUTE = USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_CLKPEN | (USART_LOC << _USART_ROUTE_LOCATION_SHIFT); if (bit_rate) USART_BaudrateSyncSet(SPI_PORT, 0, bit_rate); USART_IntClear(SPI_PORT, USART_IF_RXDATAV | USART_IF_TXBL); NVIC_ClearPendingIRQ(SPI_TX_IRQn); NVIC_EnableIRQ(SPI_TX_IRQn); NVIC_ClearPendingIRQ(SPI_RX_IRQn); NVIC_EnableIRQ(SPI_RX_IRQn); so_busy = false; so_irq_count = 0; so_spurious_irq_count = 0; so_irq_fake_count = 0; // register (but don't enable) rx pin GPIO interrupt gpio_irq_handler_install(RX_PORT, RX_PIN, SO_IRQ, NULL); }
/*---------------------------------------------------------------------------*/ void spi2_init(void) { USART_InitSync_TypeDef bcinit = USART_INITSYNC_DEFAULT; CMU_ClockEnable(cmuClock_HFPER, true); CMU_ClockEnable(cmuClock_USART2, true); if(_u8_spi2_initialized != 0) { return; } // MSB first bcinit.msbf = true; bcinit.baudrate = 7000000; USART_InitSync(USART2, &bcinit); // Enable AutoCS //USART2->CTRL |= USART_CTRL_AUTOCS; USART2->ROUTE = USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_CLKPEN | (USART2_LOCATION << _USART_ROUTE_LOCATION_SHIFT); #ifdef WITH_PM power_register(&spi2_pm_ops); #endif #ifdef WITH_DMA usart2_dma_init(); #endif mutex_init(&spi2_mutex); _u8_spi2_initialized = 1; }
/***************************************************************************//** * @brief * Init USART for I2S mode. * * @details * This function will configure basic settings in order to operate in I2S * mode. * * Special control setup not covered by this function must be done after * using this function by direct modification of the CTRL and I2SCTRL * registers. * * Notice that pins used by the USART module must be properly configured * by the user explicitly, in order for the USART to work as intended. * (When configuring pins, one should remember to consider the sequence of * configuration, in order to avoid unintended pulses/glitches on output * pins.) * * @param[in] usart * Pointer to USART peripheral register block. (UART does not support this * mode.) * * @param[in] init * Pointer to initialization structure used to configure basic I2S setup. * * @note * This function does not apply to all USART's. Refer to chip manuals. * ******************************************************************************/ void USART_InitI2s(USART_TypeDef *usart, USART_InitI2s_TypeDef *init) { USART_Enable_TypeDef enable; /* Make sure the module exists on the selected chip */ EFM_ASSERT(USART_I2S_VALID(usart)); /* Override the enable setting. */ enable = init->sync.enable; init->sync.enable = usartDisable; /* Init USART as a sync device. */ USART_InitSync(usart, &init->sync); /* Configure and enable I2CCTRL register acording to selected mode. */ usart->I2SCTRL = ((uint32_t) init->format) | ((uint32_t) init->justify) | (init->delay ? USART_I2SCTRL_DELAY : 0) | (init->dmaSplit ? USART_I2SCTRL_DMASPLIT : 0) | (init->mono ? USART_I2SCTRL_MONO : 0) | (USART_I2SCTRL_EN); if (enable != usartDisable) { USART_Enable(usart, enable); } }
/**************************************************************************//** * @brief Initialize the PAL SPI interface * * @detail This function initializes all resources required to support the * PAL SPI inteface functions for the textdisplay example on * EFM32GG_STK3700. * * @return EMSTATUS code of the operation. *****************************************************************************/ EMSTATUS PAL_SpiInit (void) { EMSTATUS status = PAL_EMSTATUS_OK; USART_InitSync_TypeDef usartInit = USART_INITSYNC_DEFAULT; /* Initialize USART for SPI transaction */ CMU_ClockEnable( PAL_SPI_USART_CLOCK, true ); usartInit.baudrate = PAL_SPI_BAUDRATE; USART_InitSync( PAL_SPI_USART_UNIT, &usartInit ); #if defined( USART_ROUTEPEN_TXPEN ) PAL_SPI_USART_UNIT->ROUTEPEN = USART_ROUTEPEN_TXPEN | USART_ROUTEPEN_CLKPEN; PAL_SPI_USART_UNIT->ROUTELOC0 = ( PAL_SPI_USART_UNIT->ROUTELOC0 & ~( _USART_ROUTELOC0_TXLOC_MASK | _USART_ROUTELOC0_CLKLOC_MASK ) ) | ( PAL_SPI_USART_LOCATION_TX << _USART_ROUTELOC0_TXLOC_SHIFT ) | ( PAL_SPI_USART_LOCATION_SCLK << _USART_ROUTELOC0_CLKLOC_SHIFT ); #else PAL_SPI_USART_UNIT->ROUTE = (USART_ROUTE_CLKPEN | USART_ROUTE_TXPEN | PAL_SPI_USART_LOCATION); #endif return status; }
/* functions */ void RADIO_Init() { // usart 0 location 2 // enable pins GPIO_PinModeSet(NRF_CE_PORT, NRF_CE_PIN, gpioModePushPull, 0); GPIO_PinModeSet(NRF_CSN_PORT, NRF_CSN_PIN, gpioModePushPull, 1); GPIO_PinModeSet(NRF_INT_PORT, NRF_INT_PIN, gpioModeInput, 0); GPIO_PinModeSet(NRF_RXEN_PORT, NRF_RXEN_PIN, gpioModePushPull, 0); GPIO_PinModeSet(gpioPortC, 11, gpioModePushPull, 1); GPIO_PinModeSet(gpioPortC, 10, gpioModeInput, 0); GPIO_PinModeSet(gpioPortC, 9, gpioModePushPull, 0); USART_InitSync_TypeDef usartInit = USART_INITSYNC_DEFAULT; usartInit.msbf = true; usartInit.clockMode = usartClockMode0; usartInit.baudrate = 1000000; USART_InitSync(NRF_USART, &usartInit); NRF_USART->ROUTE = (NRF_USART->ROUTE & ~_USART_ROUTE_LOCATION_MASK) | USART_ROUTE_LOCATION_LOC2; NRF_USART->ROUTE |= USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_CLKPEN; // configure gpio interrupt GPIO_IntClear(1 << 0); writeRegister(NRF_STATUS,0x70); GPIO_IntConfig(NRF_INT_PORT,NRF_INT_PIN,false,true,true); // initial config writeRegister(NRF_EN_AA,0x00); writeRegister(NRF_EN_RXADDR,0x3F); writeRegister(NRF_SETUP_AW,0x03); writeRegister(NRF_SETUP_RETR,0x00); writeRegister(NRF_RF_CH,NODE_CH); writeRegister(NRF_RF_SETUP,0x0F); uint8_t addr_array[5]; addr_array[0] = 0xE7; addr_array[1] = 0xE7; addr_array[2] = 0xE7; addr_array[3] = 0xE7; addr_array[4] = 0xE7; writeRegisterMulti(NRF_TX_ADDR, addr_array, 5); writeRegisterMulti(NRF_RX_ADDR_P0, addr_array, 5); writeRegister(NRF_RX_PW_P0,0x20); writeRegister(NRF_DYNPD, 0x00); writeRegister(NRF_FEATURE, 0x00); }
/**************************************************************************//** * @brief Initialize the PAL SPI interface * * @detail This function initializes all resources required to support the * PAL SPI inteface functions for the textdisplay example on * EFM32GG_STK3700. * * @return EMSTATUS code of the operation. *****************************************************************************/ EMSTATUS PAL_SpiInit (void) { EMSTATUS status = PAL_EMSTATUS_OK; USART_InitSync_TypeDef usartInit = USART_INITSYNC_DEFAULT; /* Initialize USART for SPI transaction */ CMU_ClockEnable( PAL_SPI_USART_CLOCK, true ); usartInit.baudrate = PAL_SPI_BAUDRATE; usartInit.databits = usartDatabits16; USART_InitSync( PAL_SPI_USART_UNIT, &usartInit ); PAL_SPI_USART_UNIT->ROUTE = (USART_ROUTE_CLKPEN | USART_ROUTE_TXPEN | PAL_SPI_USART_LOCATION); return status; }
static void usart_init(spi_t *obj, uint32_t baudrate, USART_Databits_TypeDef databits, bool master, USART_ClockMode_TypeDef clockMode ) { USART_InitSync_TypeDef init = USART_INITSYNC_DEFAULT; init.enable = usartDisable; init.baudrate = baudrate; init.databits = databits; init.master = master; init.msbf = 1; init.clockMode = clockMode; /* Determine the reference clock, because the correct clock may not be set up at init time (e.g. before main()) */ init.refFreq = REFERENCE_FREQUENCY; USART_InitSync(obj->spi.spi, &init); }
/* * Hardware initialization for the radio connection */ void CC1101_Radio::initializeInterface() { CMU_ClockEnable( cmuClock_USART0, true ); USART_InitSync( cc1101USART, &interfaceInit ); USART_Enable( cc1101USART, usartEnable ); cc1101USART->ROUTE = USART_ROUTE_RXPEN | // Enable the MISO-Pin USART_ROUTE_TXPEN | // Enable the MOSI-Pin USART_ROUTE_CLKPEN | // Enable the SPI-Clock Pin cc1101Location; GPIO_PinModeSet( _CC1101_SPI_MOSI_PIN, gpioModePushPull, 1 ); GPIO_PinModeSet( _CC1101_SPI_MISO_PIN, gpioModeInputPull, 1 ); GPIO_PinModeSet( _CC1101_SPI_CLK_PIN, gpioModePushPull, 1 ); GPIO_PinModeSet( _CC1101_SPI_CS_PIN, gpioModePushPull, 1 ); }
/** * Initializes the SPI pins, frequency, and SPI mode configuration. */ void enc28j60_spi_init(void) { /* * UEXT connections with STM32-P152: * 1 +3.3V * 2 GND * 3 LEDA = TX_US1 * 4 WOL = RX_US1 * 5 INT = PD7 * 6 RST = PD6 * 7 MISO = PE11 * 8 MOSI = PE10 * 9 SCK = PE12 * 10 CS = PE13 */ // Enable clocks. CMU_ClockEnable(cmuClock_GPIO, true); CMU_ClockEnable(cmuClock_USART0, true); // Configure GPIO pins. // Set CS to 1 (inactive) GPIO_PinModeSet(gpioPortE, 13, gpioModePushPull, 1); GPIO_PinModeSet(gpioPortE, 10, gpioModePushPull, 0); GPIO_PinModeSet(gpioPortE, 12, gpioModePushPull, 0); GPIO_PinModeSet(gpioPortE, 11, gpioModeInput, 0); // Configure SPI. USART_Reset(USART0); USART_InitSync(USART0, &spiConfig); USART0->ROUTE = USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_CLKPEN | USART_ROUTE_LOCATION_LOC0; #ifdef ENC28J60_USE_INTERRUPTS // Configure UEXT enc28j60 interrupt line. CMU_ClockEnable(cmuClock_GPIO, true); GPIO_PinModeSet(gpioPortD, 7, gpioModeInputPullFilter, 1); GPIO_IntConfig(gpioPortD, 7, false, true, true); NVIC_EnableIRQ(GPIO_ODD_IRQn); NVIC_SetPriority(GPIO_ODD_IRQn, PORT_PENDSV_PRI - 1); #endif }
void initSpi(void) { CMU_ClockEnable(cmuClock_GPIO, true); CMU_ClockEnable(cmuClock_USART1, true); port_init(spi_pins, sizeof(spi_pins)/sizeof(port_init_t)); USART_InitSync(SPI_USART, & spi_init); SPI_USART->ROUTE = USART_ROUTE_TXPEN | USART_ROUTE_CLKPEN | (SPI_USART_LOC << _USART_ROUTE_LOCATION_SHIFT); //SPI_USART->CTRL |= USART_CTRL_AUTOCS; // NOTE: we can't use USART_ROUTE_CSPEN in the USART ROUTE register // and AUTO_CS in the USART CTRL register for two reasons: // 1. we have chip selects for two DACs // 2. our polled transmit using USART_SpiTransfer() doesn't keep // the tx buffer full, so CS would deassert between bytes //NVIC_EnableIRQ(SPI_USART_IRQ); }
void Spi_Init(void) { USART_Reset(USART1); CMU_ClockEnable(cmuClock_USART1, true); USART_InitSync_TypeDef spiinit = USART_INITSYNC_DEFAULT; spiinit.baudrate = 1000000; spiinit.msbf=1; USART_InitSync(USART1, &spiinit); GPIO_PinModeSet(SPI_MOSI_PORT, SPI_MOSI_PIN, gpioModePushPull, 1); GPIO_PinModeSet(SPI_MISO_PORT, SPI_MISO_PIN, gpioModeInput, 0); GPIO_PinModeSet(SPI_CLK_PORT, SPI_CLK_PIN, gpioModePushPull, 1); GPIO_PinModeSet(SPI_CS_PORT, SPI_CS_PIN, gpioModePushPull, 1); GPIO_PinModeSet(RF_SDN_PORT, RF_SDN_PIN, gpioModePushPull, 0); GPIO_PinModeSet(RF_nIRQ_PORT, RF_nIRQ_PIN, gpioModeInput, 0); USART1->ROUTE |= USART_ROUTE_CLKPEN | USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_LOCATION_LOC1; }
/*! * This function is used to initialize the SPI port. * * @return None */ void vSpiInitialize(void) { USART_InitSync_TypeDef init = USART_INITSYNC_DEFAULT; /* Configure SPI */ USART_Reset(USART1); init.baudrate = 5000000; init.databits = usartDatabits8; init.msbf = 1; init.master = 1; init.clockMode = usartClockMode0; init.prsRxEnable = 0; init.autoTx = 0; USART_InitSync(USART1, &init); /* IO configuration (USART 1, Location #1) */ GPIO_PinModeSet(SPI_MOSI_PORT, SPI_MOSI_PIN, gpioModePushPull, 1); GPIO_PinModeSet(SPI_MISO_PORT, SPI_MISO_PIN, gpioModeInput, 0); GPIO_PinModeSet(SPI_CLK_PORT, SPI_CLK_PIN, gpioModePushPull, 0); GPIO_PinModeSet(SPI_CS_PORT, SPI_CS_PIN, gpioModePushPull, 1); }
/**************************************************************************//** * @brief * Initialize the SPI peripheral for microSD card usage. * SPI pins and speed etc. is defined in microsdconfig.h. *****************************************************************************/ void MICROSD_Init(void) { USART_InitSync_TypeDef init = USART_INITSYNC_DEFAULT; /* Enabling clock to USART 0 */ CMU_ClockEnable(MICROSD_CMUCLOCK, true); CMU_ClockEnable(cmuClock_GPIO, true); /* Initialize USART in SPI master mode. */ xfersPrMsec = MICROSD_LO_SPI_FREQ / 8000; init.baudrate = MICROSD_LO_SPI_FREQ; init.msbf = true; USART_InitSync(MICROSD_USART, &init); /* Enabling pins and setting location, SPI CS not enable */ MICROSD_USART->ROUTE = USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_CLKPEN | MICROSD_LOC; /* IO configuration */ GPIO_PinModeSet(MICROSD_GPIOPORT, MICROSD_MOSIPIN, gpioModePushPull, 0); /* MOSI */ GPIO_PinModeSet(MICROSD_GPIOPORT, MICROSD_MISOPIN, gpioModeInputPull, 1); /* MISO */ GPIO_PinModeSet(MICROSD_GPIOPORT, MICROSD_CSPIN, gpioModePushPull, 1); /* CS */ GPIO_PinModeSet(MICROSD_GPIOPORT, MICROSD_CLKPIN, gpioModePushPull, 0); /* CLK */ }
/**************************************************************************//** * @brief Setup SPI as Master *****************************************************************************/ void setupSpi(void) { USART_InitSync_TypeDef usartInit = USART_INITSYNC_DEFAULT; /* Initialize SPI */ usartInit.databits = usartDatabits8; /* 8 bits of data */ usartInit.baudrate = SPI_BAUDRATE; /* Clock frequency */ usartInit.master = true; /* Master mode */ usartInit.msbf = true; /* Most Significant Bit first */ usartInit.clockMode = usartClockMode0; /* Clock idle low, sample on rising edge */ USART_InitSync(SPI_CHANNEL, &usartInit); /* Enable SPI transmit and receive */ USART_Enable(SPI_CHANNEL, usartEnable); /* Configure GPIO pins for SPI */ //GPIO_PinModeSet(SPI_PORT_MOSI, SPI_PIN_MOSI, gpioModePushPull, 0); /* MOSI */ //GPIO_PinModeSet(SPI_PORT_MISO, SPI_PIN_MISO, gpioModeInput, 0); /* MISO */ //GPIO_PinModeSet(SPI_PORT_CLK, SPI_PIN_CLK, gpioModePushPull, 0); /* CLK */ //GPIO_PinModeSet(SPI_PORT_CS, SPI_PIN_CS, gpioModePushPull, 1); /* CS */ //edit: do this via the hw_gpio_configure_pin interface to signal that the pins are in use //note: normally this should be done in the platform-specific initialisation code BUT, since this is a driver for a device (uart) that is //an integral part of the MCU we are certain this code will NOT be used in combination with a different MCU so we can do this here error_t err; err = hw_gpio_configure_pin(SPI_PIN_MOSI, false, gpioModePushPull, 0); assert(err == SUCCESS); /* MOSI */ err = hw_gpio_configure_pin(SPI_PIN_MISO, false, gpioModeInput, 0); assert(err == SUCCESS); /* MISO */ err = hw_gpio_configure_pin(SPI_PIN_CLK, false, gpioModePushPull, 0); assert(err == SUCCESS); /* CLK */ err = hw_gpio_configure_pin(SPI_PIN_CS, false, gpioModePushPull, 1); assert(err == SUCCESS); /* CS */ /* Enable routing for SPI pins from USART to location 1 */ SPI_CHANNEL->ROUTE = USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_CLKPEN | SPI_ROUTE_LOCATION; }
/**************************************************************************//** * @brief Setup SPI as Master *****************************************************************************/ void setupSpi(void) { USART_InitSync_TypeDef usartInit = USART_INITSYNC_DEFAULT; /* Initialize SPI */ usartInit.databits = usartDatabits8; usartInit.baudrate = 1000000; USART_InitSync(USART1, &usartInit); /* Turn on automatic Chip Select control */ USART1->CTRL |= USART_CTRL_AUTOCS; /* Enable SPI transmit and receive */ USART_Enable(USART1, usartEnable); /* Configure GPIO pins for SPI */ GPIO_PinModeSet(gpioPortD, 0, gpioModePushPull, 0); /* MOSI */ GPIO_PinModeSet(gpioPortD, 1, gpioModeInput, 0); /* MISO */ GPIO_PinModeSet(gpioPortD, 2, gpioModePushPull, 0); /* CLK */ GPIO_PinModeSet(gpioPortD, 3, gpioModePushPull, 1); /* CS */ /* Enable routing for SPI pins from USART to location 1 */ USART1->ROUTE = USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_CSPEN | USART_ROUTE_CLKPEN | USART_ROUTE_LOCATION_LOC1; /* Configure interrupt for TX/RX, but do not enable them */ /* Interrupts will be enabled only for reading last 3 bytes * when using AUTOTX */ NVIC_ClearPendingIRQ(USART1_RX_IRQn); NVIC_EnableIRQ(USART1_RX_IRQn); NVIC_ClearPendingIRQ(USART1_TX_IRQn); NVIC_EnableIRQ(USART1_TX_IRQn); }
/***************************************************************************//** * @brief * Initialize the specified USART unit * * @details * * @note * * @param[in] device * Pointer to device descriptor * * @param[in] unitNumber * Unit number * * @param[in] location * Pin location number * * @param[in] flag * Configuration flag * * @param[in] dmaChannel * DMA channel number for TX * * @param[in] console * Indicate if using as console * * @return * Pointer to USART device ******************************************************************************/ static struct efm32_usart_device_t *rt_hw_usart_unit_init( rt_device_t device, rt_uint8_t unitNumber, rt_uint8_t location, rt_uint32_t flag, rt_uint32_t dmaChannel, rt_uint8_t config) { struct efm32_usart_device_t *usart; struct efm32_usart_dma_mode_t *dma_mode; DMA_CB_TypeDef *callback; CMU_Clock_TypeDef usartClock; rt_uint32_t txDmaSelect; GPIO_Port_TypeDef port_tx, port_rx, port_clk, port_cs; rt_uint32_t pin_tx, pin_rx, pin_clk, pin_cs; efm32_irq_hook_init_t hook; do { /* Allocate device */ usart = rt_malloc(sizeof(struct efm32_usart_device_t)); if (usart == RT_NULL) { usart_debug("USART%d err: no mem\n", usart->unit); break; } usart->counter = 0; usart->unit = unitNumber; usart->state = config; usart->tx_mode = RT_NULL; usart->rx_mode = RT_NULL; /* Allocate TX */ dma_mode = RT_NULL; if (flag & RT_DEVICE_FLAG_DMA_TX) { usart->tx_mode = dma_mode = rt_malloc(sizeof(struct efm32_usart_dma_mode_t)); if (dma_mode == RT_NULL) { usart_debug("USART%d err: no mem for DMA TX\n", usart->unit); break; } dma_mode->dma_channel = dmaChannel; } /* Allocate RX */ if (flag & RT_DEVICE_FLAG_INT_RX) { usart->rx_mode = rt_malloc(sizeof(struct efm32_usart_int_mode_t)); if (usart->rx_mode == RT_NULL) { usart_debug("USART%d err: no mem for INT RX\n", usart->unit); break; } } /* Initialization */ #if defined(UART_PRESENT) if ((!(config & USART_STATE_ASYNC_ONLY) && (unitNumber >= USART_COUNT)) || \ ((config & USART_STATE_ASYNC_ONLY) && (unitNumber >= UART_COUNT))) #else if (unitNumber >= USART_COUNT) #endif { break; } switch (unitNumber) { case 0: #if defined(UART_PRESENT) if (config & USART_STATE_ASYNC_ONLY) { usart->usart_device = UART0; usartClock = (CMU_Clock_TypeDef)cmuClock_UART0; txDmaSelect = DMAREQ_UART0_TXBL; port_tx = AF_UART0_TX_PORT(location); pin_tx = AF_UART0_TX_PIN(location); port_rx = AF_UART0_RX_PORT(location); pin_rx = AF_UART0_RX_PIN(location); } else #endif { usart->usart_device = USART0; usartClock = (CMU_Clock_TypeDef)cmuClock_USART0; txDmaSelect = DMAREQ_USART0_TXBL; port_tx = AF_USART0_TX_PORT(location); pin_tx = AF_USART0_TX_PIN(location); port_rx = AF_USART0_RX_PORT(location); pin_rx = AF_USART0_RX_PIN(location); port_clk = AF_USART0_CLK_PORT(location); pin_clk = AF_USART0_CLK_PIN(location); port_cs = AF_USART0_CS_PORT(location); pin_cs = AF_USART0_CS_PIN(location); } break; #if ((defined(USART_PRESENT) && (USART_COUNT > 1)) || \ (defined(UART_PRESENT) && (UART_COUNT > 1))) case 1: #if (defined(UART_PRESENT) && (UART_COUNT > 1)) if (config & USART_STATE_ASYNC_ONLY) { usart->usart_device = UART1; usartClock = (CMU_Clock_TypeDef)cmuClock_UART1; txDmaSelect = DMAREQ_UART1_TXBL; port_tx = AF_UART1_TX_PORT(location); pin_tx = AF_UART1_TX_PIN(location); port_rx = AF_UART1_RX_PORT(location); pin_rx = AF_UART1_RX_PIN(location); } else #endif { usart->usart_device = USART1; usartClock = (CMU_Clock_TypeDef)cmuClock_USART1; txDmaSelect = DMAREQ_USART1_TXBL; port_tx = AF_USART1_TX_PORT(location); pin_tx = AF_USART1_TX_PIN(location); port_rx = AF_USART1_RX_PORT(location); pin_rx = AF_USART1_RX_PIN(location); port_clk = AF_USART1_CLK_PORT(location); pin_clk = AF_USART1_CLK_PIN(location); port_cs = AF_USART1_CS_PORT(location); pin_cs = AF_USART1_CS_PIN(location); } break; #endif #if ((defined(USART_PRESENT) && (USART_COUNT > 2)) || \ (defined(UART_PRESENT) && (UART_COUNT > 2))) case 2: #if (defined(UART_PRESENT) && (UART_COUNT > 2)) if (config & USART_STATE_ASYNC_ONLY) { usart->usart_device = UART2; usartClock = (CMU_Clock_TypeDef)cmuClock_UART2; txDmaSelect = DMAREQ_UART2_TXBL; port_tx = AF_UART2_TX_PORT(location); pin_tx = AF_UART2_TX_PIN(location); port_rx = AF_UART2_RX_PORT(location); pin_rx = AF_UART2_RX_PIN(location); } else #endif { usart->usart_device = USART2; usartClock = (CMU_Clock_TypeDef)cmuClock_USART2; txDmaSelect = DMAREQ_USART2_TXBL; port_tx = AF_USART2_TX_PORT(location); pin_tx = AF_USART2_TX_PIN(location); port_rx = AF_USART2_RX_PORT(location); pin_rx = AF_USART2_RX_PIN(location); port_clk = AF_USART2_CLK_PORT(location); pin_clk = AF_USART2_CLK_PIN(location); port_cs = AF_USART2_CS_PORT(location); pin_cs = AF_USART2_CS_PIN(location); } break; #endif default: break; } /* Enable USART clock */ CMU_ClockEnable(usartClock, true); /* Config GPIO */ GPIO_PinModeSet( port_tx, pin_tx, gpioModePushPull, 0); GPIO_PinModeSet( port_rx, pin_rx, gpioModeInputPull, 1); if (config & USART_STATE_SYNC) { GPIO_PinModeSet( port_clk, pin_clk, gpioModePushPull, 0); } if (config & USART_STATE_AUTOCS) { GPIO_PinModeSet( port_cs, pin_cs, gpioModePushPull, 1); } /* Config interrupt and NVIC */ if (flag & RT_DEVICE_FLAG_INT_RX) { hook.type = efm32_irq_type_usart; hook.unit = unitNumber * 2 + 1; #if defined(UART_PRESENT) if (config & USART_STATE_ASYNC_ONLY) { hook.unit += USART_COUNT * 2; } #endif hook.cbFunc = rt_hw_usart_rx_isr; hook.userPtr = device; efm32_irq_hook_register(&hook); } /* Config DMA */ if (flag & RT_DEVICE_FLAG_DMA_TX) { DMA_CfgChannel_TypeDef chnlCfg; DMA_CfgDescr_TypeDef descrCfg; hook.type = efm32_irq_type_dma; hook.unit = dmaChannel; hook.cbFunc = rt_hw_usart_dma_tx_isr; hook.userPtr = device; efm32_irq_hook_register(&hook); callback = (DMA_CB_TypeDef *)rt_malloc(sizeof(DMA_CB_TypeDef)); if (callback == RT_NULL) { usart_debug("USART%d err: no mem for callback\n", usart->unit); break; } callback->cbFunc = DMA_IRQHandler_All; callback->userPtr = RT_NULL; callback->primary = 0; /* Setting up DMA channel */ chnlCfg.highPri = false; /* Can't use with peripherals */ chnlCfg.enableInt = true; /* Interrupt for callback function */ chnlCfg.select = txDmaSelect; chnlCfg.cb = callback; DMA_CfgChannel(dmaChannel, &chnlCfg); /* Setting up DMA channel descriptor */ descrCfg.dstInc = dmaDataIncNone; descrCfg.srcInc = dmaDataInc1; descrCfg.size = dmaDataSize1; descrCfg.arbRate = dmaArbitrate1; descrCfg.hprot = 0; DMA_CfgDescr(dmaChannel, true, &descrCfg); } /* Init specified USART unit */ if (config & USART_STATE_SYNC) { USART_InitSync_TypeDef init_sync = USART_INITSYNC_DEFAULT; init_sync.enable = usartEnable; init_sync.refFreq = 0; init_sync.baudrate = SPI_BAUDRATE; if (config & USART_STATE_9BIT) { init_sync.databits = usartDatabits9; } else { init_sync.databits = usartDatabits8; } if (config & USART_STATE_MASTER) { init_sync.master = true; } else { init_sync.master = false; } init_sync.msbf = true; switch (USART_CLK_MODE_GET(config)) { case 0: init_sync.clockMode = usartClockMode0; break; case 1: init_sync.clockMode = usartClockMode1; break; case 2: init_sync.clockMode = usartClockMode2; break; case 3: init_sync.clockMode = usartClockMode3; break; } USART_InitSync(usart->usart_device, &init_sync); } else { USART_InitAsync_TypeDef init_async = USART_INITASYNC_DEFAULT; init_async.enable = usartEnable; init_async.refFreq = 0; init_async.baudrate = UART_BAUDRATE; init_async.oversampling = USART_CTRL_OVS_X4; init_async.databits = USART_FRAME_DATABITS_EIGHT; init_async.parity = USART_FRAME_PARITY_NONE; init_async.stopbits = USART_FRAME_STOPBITS_ONE; USART_InitAsync(usart->usart_device, &init_async); } /* Enable RX and TX pins and set location */ usart->usart_device->ROUTE = USART_ROUTE_RXPEN | USART_ROUTE_TXPEN | \ (location << _USART_ROUTE_LOCATION_SHIFT); if (config & USART_STATE_SYNC) { usart->usart_device->ROUTE |= USART_ROUTE_CLKPEN; } if (config & USART_STATE_AUTOCS) { usart->usart_device->ROUTE |= USART_ROUTE_CSPEN; if (config & USART_STATE_MASTER) { usart->usart_device->CTRL |= USART_CTRL_AUTOCS; } } /* Clear RX/TX buffers */ usart->usart_device->CMD = USART_CMD_CLEARRX | USART_CMD_CLEARTX; return usart; } while(0); if (usart->rx_mode) { rt_free(usart->rx_mode); } if (usart->tx_mode) { rt_free(usart->tx_mode); } if (usart) { rt_free(usart); } if (callback) { rt_free(callback); } #if defined(UART_PRESENT) if (config & USART_STATE_ASYNC_ONLY) { usart_debug("UART%d err: init failed!\n", unitNumber); } else #endif { usart_debug("USART%d err: init failed!\n", unitNumber); } return RT_NULL; }
void IO_Init(void) { I2C_Init_TypeDef i2cInit = I2C_INIT_DEFAULT; RTC_Init_TypeDef rtcInit = RTC_INIT_DEFAULT; USART_InitSync_TypeDef usartInit = USART_INITSYNC_DEFAULT; RTC_Init_TypeDef rtcInits = RTC_INIT_DEFAULT; ADC_Init_TypeDef adcInit = ADC_INIT_DEFAULT; volatile uint32_t test = 0; /* Enable LE clock and LFXO oscillator */ CMU->HFCORECLKEN0 |= CMU_HFCORECLKEN0_LE; CMU->OSCENCMD |= CMU_OSCENCMD_LFXOEN; /* Wait until LFXO ready */ /* Note that this could be done more energy friendly with an interrupt in EM1 */ while (!(CMU->STATUS & CMU_STATUS_LFXORDY)) ; /* Select LFXO as clock source for LFACLK */ CMU->LFCLKSEL = (CMU->LFCLKSEL & ~_CMU_LFCLKSEL_LFA_MASK) | CMU_LFCLKSEL_LFA_LFXO; CMU->OSCENCMD = CMU_OSCENCMD_LFRCOEN; /* Enable GPIO clock */ CMU->HFPERCLKEN0 |= CMU_HFPERCLKEN0_GPIO; /* Initialize USART */ CMU->HFPERCLKEN0 |= CMU_HFPERCLKEN0_UART1; GPIO->P[4].DOUT |= (1 << 2); GPIO->P[4].MODEL = GPIO_P_MODEL_MODE2_PUSHPULL | GPIO_P_MODEL_MODE3_INPUT; GPIO->P[1].DOUT |= (1 << 3); /* Pin PB3 is configured to Push-pull */ GPIO->P[1].MODEL = (GPIO->P[1].MODEL & ~_GPIO_P_MODEL_MODE3_MASK) | GPIO_P_MODEL_MODE3_PUSHPULL; /* Pin PB4 is configured to Input enabled */ GPIO->P[1].MODEL = (GPIO->P[1].MODEL & ~_GPIO_P_MODEL_MODE4_MASK) | GPIO_P_MODEL_MODE4_INPUT; /* Pin PB5 is configured to Push-pull */ GPIO->P[1].MODEL = (GPIO->P[1].MODEL & ~_GPIO_P_MODEL_MODE5_MASK) | GPIO_P_MODEL_MODE5_PUSHPULL; /* To avoid false start, configure output NRF_CSN as high on PB2 */ GPIO->P[1].DOUT |= (1 << 2); /* Pin PB2 (NRF_CSN) is configured to Push-pull */ GPIO->P[1].MODEL = (GPIO->P[1].MODEL & ~_GPIO_P_MODEL_MODE2_MASK) | GPIO_P_MODEL_MODE2_PUSHPULL; /* NRF_CE set to initial low */ GPIO->P[1].DOUT &= ~(1 << 1); GPIO->P[1].MODEL = (GPIO->P[1].MODEL & ~_GPIO_P_MODEL_MODE1_MASK) | GPIO_P_MODEL_MODE1_PUSHPULL; // NRF_INT GPIO->P[1].MODEL = (GPIO->P[1].MODEL & ~_GPIO_P_MODEL_MODE0_MASK) | GPIO_P_MODEL_MODE0_INPUT; // RXEN GPIO->P[0].DOUT &= ~(1 << 2); GPIO->P[0].MODEL = (GPIO->P[0].MODEL & ~_GPIO_P_MODEL_MODE2_MASK) | GPIO_P_MODEL_MODE2_PUSHPULL; // US2_CS (MX25U64) GPIO->P[1].DOUT |= (1 << 6); GPIO->P[1].MODEL = (GPIO->P[1].MODEL & ~_GPIO_P_MODEL_MODE6_MASK) | GPIO_P_MODEL_MODE6_PUSHPULL; // I2C0 SCL // I2C0 SDA /* Enable clock for I2C0 */ CMU->HFPERCLKEN0 |= CMU_HFPERCLKEN0_I2C0; /* Enable signals SDA, SCL */ I2C0->ROUTE |= I2C_ROUTE_SDAPEN | I2C_ROUTE_SCLPEN | I2C_ROUTE_LOCATION_LOC3; /* Enable clock for LETIMER0 */ CMU->LFACLKEN0 |= CMU_LFACLKEN0_LETIMER0; /* Enable clock for RTC */ CMU->LFACLKEN0 |= CMU_LFACLKEN0_RTC; CMU->HFPERCLKEN0 |= CMU_HFPERCLKEN0_USART2; usartInit.msbf = true; usartInit.clockMode = usartClockMode0; usartInit.baudrate = 7000000; USART_InitSync(USART2, &usartInit); //USART_Enable(USART1, usartEnable); USART2->ROUTE = (USART2->ROUTE & ~_USART_ROUTE_LOCATION_MASK) | USART_ROUTE_LOCATION_LOC1; /* Enable signals TX, RX, CLK */ USART2->ROUTE |= USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_CLKPEN; GPIO_PinModeSet(gpioPortA, 0, gpioModeWiredAnd, 1); GPIO_PinModeSet(gpioPortA, 1, gpioModeWiredAnd, 1); GPIO_PinModeSet(gpioPortA, 3, gpioModeWiredAnd, 1); /* Use location 3: SDA - Pin D14, SCL - Pin D15 */ /* Output value must be set to 1 to not drive lines low... We set */ /* SCL first, to ensure it is high before changing SDA. */ GPIO_PinModeSet(gpioPortD, 15, gpioModeWiredAnd, 1); GPIO_PinModeSet(gpioPortD, 14, gpioModeWiredAnd, 1); I2C_Init(I2C0, &i2cInit); I2C0->CLKDIV=1; // MMA_INT GPIO_PinModeSet(gpioPortA, 15, gpioModeInput, 0); GPIO_IntConfig(gpioPortA, 15, false, true, true); // NRF_INT GPIO_IntConfig(gpioPortB, 0, false, true, true); NVIC_ClearPendingIRQ(GPIO_EVEN_IRQn); NVIC_EnableIRQ(GPIO_EVEN_IRQn); NVIC_ClearPendingIRQ(GPIO_ODD_IRQn); NVIC_EnableIRQ(GPIO_ODD_IRQn); CMU_ClockEnable(cmuClock_RTC, true); CMU_ClockEnable(cmuClock_ADC0, true); /* Each RTC tick is second */ CMU_ClockDivSet(cmuClock_RTC, cmuClkDiv_1); RTC_Reset(); /* Enable RTC */ //RTC_Enable(true); rtcInits.comp0Top = false; RTC_Init(&rtcInits); // RTC_Init(&rtcInit); //test = NRF_Status(); //buf[0] = test; }
/**************************************************************************//** * @brief Main function *****************************************************************************/ int main(void) { /* Chip errata */ CHIP_Init(); // Turn on the peripheral clocks CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFXO); CMU_ClockEnable(cmuClock_GPIO, true); CMU_ClockEnable(cmuClock_USART1, true); Ecode_t result; // Initialize DMA. result = DMADRV_Init(); if (result != ECODE_EMDRV_DMADRV_OK) { DEBUG_BREAK } // Request a DMA channel. result = DMADRV_AllocateChannel( &dma_channel, NULL ); if (result != ECODE_EMDRV_DMADRV_OK) { DEBUG_BREAK } // Configure the USART peripheral USART_InitSync_TypeDef init = USART_INITSYNC_DEFAULT; init.baudrate = 50000; init.msbf = true; // MSB first, per the TLC5940 spec USART_InitSync(USART1, &init); // Route the peripheral to the GPIO block USART1->ROUTE = USART_ROUTE_TXPEN | // US1_TX USART_ROUTE_CLKPEN | // US1_CLK USART_ROUTE_LOCATION_LOC1; // Location #1 // Configure both the USART and control pins in GPIO GPIO_DriveModeSet(gpioPortD, gpioDriveModeLowest); GPIO_PinModeSet(CONTROL_PORT, SIN_PIN, gpioModePushPullDrive, 0); GPIO_PinModeSet(CONTROL_PORT, SCLK_PIN, gpioModePushPullDrive, 0); GPIO_PinModeSet(CONTROL_PORT, XLAT_PIN, gpioModePushPullDrive, 0); GPIO_PinModeSet(CONTROL_PORT, VPRG_PIN, gpioModePushPullDrive, 0); GPIO_PinModeSet(CONTROL_PORT, BLANK_PIN, gpioModePushPullDrive, 0); GPIO_PinModeSet(CONTROL_PORT, GSCLK_PIN, gpioModePushPullDrive, 0); // Start with DC Data stream.mode = DOT_CORRECTION_MODE; for (int i=0; i<MAX_CHANNELS; i++) { // stream.channel[i].dot_correction = 0b100001; // Test pattern // stream.channel[i].grayscale = 0b100000000001; // Test pattern stream.channel[i].dot_correction = 0xF; //MAX_DOT_CORRECTION; stream.channel[i].grayscale = 0; //MAX_GRAYSCALE; } // Write the DC Data write_serial_stream(); //all_white(); //rgb_display(); yellow_purple(); int count = 0; while (1) { if (count %2) { GPIO_PinOutSet(CONTROL_PORT, GSCLK_PIN); } else { GPIO_PinOutClear(CONTROL_PORT, GSCLK_PIN); } count++; if (count > 4096) { GPIO_PinOutSet(CONTROL_PORT, BLANK_PIN); for (volatile int i=0; i < 10; i++) ; GPIO_PinOutClear(CONTROL_PORT, BLANK_PIN); count = 0; } for (volatile int i=0; i < 10; i++) ; } }
void SPIClass::begin() { DEBUGPRINTLN("ASDFASDF"); GPIO_PinModeSet( (GPIO_Port_TypeDef)dPorts[ pinSettings->mosiPin], dPins[ pinSettings->mosiPin], gpioModeWiredAndPullUpFilter, 1); GPIO_PinModeSet( (GPIO_Port_TypeDef)dPorts[ pinSettings->misoPin], dPins[ pinSettings->misoPin], gpioModeInput, 0); //This needs to be push pull or the CC3000 doesn't work!!!!! GPIO_PinModeSet( (GPIO_Port_TypeDef)dPorts[ pinSettings->clkPin], dPins[ pinSettings->clkPin], gpioModePushPull, 1); /*//setup SPI Pins pinMode( pinSettings->mosiPin, gpioModeWiredAndPullUpFilter); digitalWrite(pinSettings->mosiPin, 1); pinMode( pinSettings->misoPin, gpioModeInput); //digitalWrite(pinSettings->misoPin, 0); pinMode( 35, gpioModeInput); //digitalWrite(35, 0); pinMode( 34, gpioModeInput); //digitalWrite(34, 0); pinMode( pinSettings->clkPin, gpioModeWiredAndPullUpFilter); digitalWrite(pinSettings->clkPin, 1);*/ // NPCS control is left to the user CMU_ClockEnable(cmuClock_HFPER, true); CMU_ClockEnable(spi_clk, true); //CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFRCO); DEBUGPRINTLN("CONFIG"); DEBUGPRINTLN(id); DEBUGPRINTLN(spi_clk); DEBUGPRINTLN(mode[0]); DEBUGPRINTLN(bitOrder[0] ? 1 : 0); DEBUGPRINTLN("ENDCONFIG"); // Default speed set to 4Mhz //usartInit.msbf = true;//bitOrder[0]; //usartInit.clockMode = usartClockMode0; //usartInit.baudrate = 1000000;//84000000 / divider[0]; usartInit.refFreq = 14000000; usartInit.baudrate = 100000; //usartInit.baudrate = 1000000; usartInit.databits = usartDatabits8; usartInit.msbf = bitOrder[0] ? 1 : 0; usartInit.master = 1; usartInit.clockMode = mode[0]; usartInit.prsRxEnable = 0; usartInit.autoTx = 0; USART_InitSync(spi, &usartInit); //USART_Enable(spi, usartEnable); // Enable receiver and transmitter /* Module USART2 is configured to location 1 */ spi->ROUTE = id; /* Enable signals TX, RX, CLK */ spi->ROUTE |= USART_ROUTE_TXPEN | USART_ROUTE_CLKPEN | USART_ROUTE_RXPEN; // //spi->CLKDIV = 0x200; }