OSStatus host_platform_bus_deinit( void ) { platform_mcu_powersave_disable( ); NVIC_DisableIRQ( platform_flexcom_irq_numbers[wifi_spi.spi_id] ); pdc_disable_transfer( spi_get_pdc_base( wifi_spi.port ), PERIPH_PTCR_RXTDIS | PERIPH_PTCR_TXTDIS ); spi_disable(wifi_spi.port); /* Deinit the SPI lines */ platform_gpio_peripheral_pin_init( wifi_spi.mosi_pin, INPUT_HIGH_IMPEDANCE ); platform_gpio_peripheral_pin_init( wifi_spi.miso_pin, INPUT_HIGH_IMPEDANCE ); platform_gpio_peripheral_pin_init( wifi_spi.clock_pin, INPUT_HIGH_IMPEDANCE ); /* Deinit the interrupt input for WLAN_IRQ */ platform_gpio_init( &wifi_spi_pins[WIFI_PIN_SPI_IRQ], INPUT_HIGH_IMPEDANCE ); platform_gpio_irq_disable( &wifi_spi_pins[WIFI_PIN_SPI_IRQ] ); /* Deinit SPI slave select GPIOs */ platform_gpio_init( &wifi_spi_pins[WIFI_PIN_SPI_CS], INPUT_HIGH_IMPEDANCE ); #if defined ( MICO_WIFI_USE_GPIO_FOR_BOOTSTRAP ) platform_gpio_init( &wifi_control_pins[WIFI_PIN_BOOTSTRAP_0], INPUT_HIGH_IMPEDANCE ); platform_gpio_init( &wifi_control_pins[WIFI_PIN_BOOTSTRAP_1], INPUT_HIGH_IMPEDANCE ); #endif platform_mcu_powersave_enable( ); return kNoErr; }
wwd_result_t platform_bus_exit_powersave( void ) { if ( sdio_bus_initted == WICED_TRUE ) { uint32_t a; /* Enable SDIO peripheral clock */ for ( a = WWD_PIN_SDIO_CLK; a < WWD_PIN_SDIO_MAX; a++ ) { if( a == WWD_PIN_SDIO_OOB_IRQ ) { platform_gpio_peripheral_pin_init( &wifi_sdio_pins[a], 0 ); } else { platform_gpio_peripheral_pin_init( &wifi_sdio_pins[a], ( IOPORT_MODE_MUX_C | IOPORT_MODE_PULLUP )); } } /* Enable the MCI peripheral */ sysclk_enable_peripheral_clock( ID_HSMCI ); } return WICED_SUCCESS; }
wwd_result_t host_platform_bus_init( void ) { if ( sdio_bus_initted == WICED_FALSE ) { uint8_t a = 0; platform_mcu_powersave_disable(); /* SDIO bootstrapping: GPIO0 = 0 and GPIO1 = 0 */ #ifdef WICED_WIFI_USE_GPIO_FOR_BOOTSTRAP platform_gpio_init( &wifi_control_pins[WWD_PIN_BOOTSTRAP_0], OUTPUT_PUSH_PULL ); platform_gpio_output_low( &wifi_control_pins[WWD_PIN_BOOTSTRAP_0] ); platform_gpio_init( &wifi_control_pins[WWD_PIN_BOOTSTRAP_1], OUTPUT_PUSH_PULL ); platform_gpio_output_low( &wifi_control_pins[WWD_PIN_BOOTSTRAP_1] ); #endif /* WICED_WIFI_USE_GPIO_FOR_BOOTSTRAP */ /* Setup SDIO pins */ for ( a = WWD_PIN_SDIO_CLK; a < WWD_PIN_SDIO_MAX; a++ ) { if( a == WWD_PIN_SDIO_OOB_IRQ ) { platform_gpio_peripheral_pin_init( &wifi_sdio_pins[a], 0 ); } else { platform_gpio_peripheral_pin_init( &wifi_sdio_pins[a], ( IOPORT_MODE_MUX_C | IOPORT_MODE_PULLUP )); } } /* Enable the MCI peripheral */ sysclk_enable_peripheral_clock( ID_HSMCI ); HSMCI->HSMCI_CR = HSMCI_CR_SWRST; MCI_Disable( HSMCI ); MCI_Init( &sdio_driver, HSMCI, ID_HSMCI, CPU_CLOCK_HZ ); /* Enable SDIO interrupt */ /* Priority must be set in platform.c file function platform_init_peripheral_irq_priorities */ /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ // NVIC_SetPriority( HSMCI_IRQn, SAM4S_SDIO_IRQ_PRIO ); NVIC_EnableIRQ( HSMCI_IRQn ); // host_rtos_init_semaphore( &sdio_transfer_done_semaphore ); // enable_sdio_block_transfer_done_irq(); platform_mcu_powersave_enable(); sdio_bus_initted = WICED_TRUE; } return WICED_SUCCESS; }
platform_result_t platform_spi_init( const platform_spi_t* spi, const platform_spi_config_t* config ) { UNUSED_PARAMETER(spi); UNUSED_PARAMETER(config); platform_mcu_powersave_disable( ); Pdc* spi_pdc = spi_get_pdc_base( spi->peripheral ); /* Setup chip select pin */ platform_gpio_init( config->chip_select, OUTPUT_PUSH_PULL ); platform_gpio_output_high( config->chip_select ); /* Setup other pins */ platform_gpio_peripheral_pin_init( spi->mosi_pin, ( IOPORT_MODE_MUX_A | IOPORT_MODE_PULLUP ) ); platform_gpio_peripheral_pin_init( spi->miso_pin, ( IOPORT_MODE_MUX_A | IOPORT_MODE_PULLUP ) ); platform_gpio_peripheral_pin_init( spi->clk_pin, ( IOPORT_MODE_MUX_A | IOPORT_MODE_PULLUP ) ); /* Configure an SPI peripheral. */ pmc_enable_periph_clk( spi->peripheral_id ); spi_disable( spi->peripheral ); spi_reset( spi->peripheral ); spi_set_lastxfer( spi->peripheral ); spi_set_master_mode( spi->peripheral ); spi_disable_mode_fault_detect( spi->peripheral ); spi_set_peripheral_chip_select_value( spi->peripheral, 0 ); spi_set_clock_polarity( spi->peripheral, 0, ( ( config->mode && SPI_CLOCK_IDLE_HIGH ) ? (1) : (0) ) ); spi_set_clock_phase( spi->peripheral, 0, ( ( config->mode && SPI_CLOCK_RISING_EDGE ) ? (1) : (0) ) ); spi_set_bits_per_transfer( spi->peripheral, 0, SPI_CSR_BITS_8_BIT ); spi_set_baudrate_div( spi->peripheral, 0, (uint8_t)( CPU_CLOCK_HZ / config->speed ) ); spi_set_transfer_delay( spi->peripheral, 0, 0, 0 ); spi_enable( spi->peripheral ); pdc_disable_transfer( spi_pdc, PERIPH_PTCR_RXTDIS | PERIPH_PTCR_TXTDIS ); platform_mcu_powersave_enable( ); return PLATFORM_SUCCESS; }
OSStatus host_platform_bus_init( void ) { pdc_packet_t pdc_spi_packet; Pdc* spi_pdc = spi_get_pdc_base( wifi_spi.port ); platform_mcu_powersave_disable( ); mico_rtos_init_semaphore( &spi_transfer_finished_semaphore, 1 ); /* Setup the SPI lines */ platform_gpio_peripheral_pin_init( wifi_spi.mosi_pin, ( wifi_spi.mosi_pin_mux_mode | IOPORT_MODE_PULLUP ) ); platform_gpio_peripheral_pin_init( wifi_spi.miso_pin, ( wifi_spi.miso_pin_mux_mode | IOPORT_MODE_PULLUP ) ); platform_gpio_peripheral_pin_init( wifi_spi.clock_pin, ( wifi_spi.clock_pin_mux_mode | IOPORT_MODE_PULLUP ) ); /* Setup the interrupt input for WLAN_IRQ */ platform_gpio_init( &wifi_spi_pins[WIFI_PIN_SPI_IRQ], INPUT_HIGH_IMPEDANCE ); platform_gpio_irq_enable( &wifi_spi_pins[WIFI_PIN_SPI_IRQ], IRQ_TRIGGER_RISING_EDGE, spi_irq_handler, 0 ); /* Setup SPI slave select GPIOs */ platform_gpio_init( &wifi_spi_pins[WIFI_PIN_SPI_CS], OUTPUT_PUSH_PULL ); platform_gpio_output_high( &wifi_spi_pins[WIFI_PIN_SPI_CS] ); #if defined ( MICO_WIFI_USE_GPIO_FOR_BOOTSTRAP ) /* Set GPIO_B[1:0] to 01 to put WLAN module into gSPI mode */ platform_gpio_init( &wifi_control_pins[WIFI_PIN_BOOTSTRAP_0], OUTPUT_PUSH_PULL ); platform_gpio_output_high( &wifi_control_pins[WIFI_PIN_BOOTSTRAP_0] ); platform_gpio_init( &wifi_control_pins[WIFI_PIN_BOOTSTRAP_1], OUTPUT_PUSH_PULL ); platform_gpio_output_low( &wifi_control_pins[WIFI_PIN_BOOTSTRAP_1] ); #endif /* Enable the peripheral and set SPI mode. */ flexcom_enable( flexcom_base[ wifi_spi.spi_id ] ); flexcom_set_opmode( flexcom_base[ wifi_spi.spi_id ], FLEXCOM_SPI ); /* Init pdc, and clear RX TX. */ pdc_spi_packet.ul_addr = 0; pdc_spi_packet.ul_size = 1; pdc_tx_init( spi_pdc, &pdc_spi_packet, NULL ); pdc_rx_init( spi_pdc, &pdc_spi_packet, NULL ); spi_disable_interrupt(wifi_spi.port, 0xffffffff ); spi_disable( wifi_spi.port ); spi_reset( wifi_spi.port ); spi_set_lastxfer( wifi_spi.port ); spi_set_master_mode( wifi_spi.port ); spi_disable_mode_fault_detect( wifi_spi.port ); spi_set_clock_polarity( wifi_spi.port, 0, SPI_CLK_POLARITY ); spi_set_clock_phase( wifi_spi.port, 0, SPI_CLK_PHASE ); spi_set_bits_per_transfer( wifi_spi.port, 0, SPI_CSR_BITS_8_BIT ); spi_set_baudrate_div( wifi_spi.port, 0, (sysclk_get_cpu_hz() / SPI_BAUD_RATE) ); spi_set_transfer_delay( wifi_spi.port, 0, 0, 0 ); /* Must be lower priority than the value of configMAX_SYSCALL_INTERRUPT_PRIORITY */ /* otherwise FreeRTOS will not be able to mask the interrupt */ /* keep in mind that ARMCM4 interrupt priority logic is inverted, the highest value */ /* is the lowest priority */ /* Configure SPI interrupts . */ NVIC_EnableIRQ( platform_flexcom_irq_numbers[wifi_spi.spi_id] ); spi_enable(wifi_spi.port); platform_mcu_powersave_enable( ); return kNoErr; }
OSStatus platform_uart_init( platform_uart_driver_t* driver, const platform_uart_t* peripheral, const platform_uart_config_t* config, ring_buffer_t* optional_ring_buffer ) { pdc_packet_t pdc_uart_packet, pdc_uart_tx_packet; OSStatus err = kNoErr; sam_usart_opt_t settings; bool hardware_shaking = false; platform_mcu_powersave_disable(); require_action_quiet( ( driver != NULL ) && ( peripheral != NULL ) && ( config != NULL ), exit, err = kParamErr); require_action_quiet( (optional_ring_buffer->buffer != NULL ) && (optional_ring_buffer->size != 0), exit, err = kUnsupportedErr); driver->rx_size = 0; driver->tx_size = 0; driver->rx_ring_buffer = optional_ring_buffer; driver->last_transmit_result = kNoErr; driver->last_receive_result = kNoErr; driver->peripheral = (platform_uart_t*)peripheral; #ifndef NO_MICO_RTOS mico_rtos_init_semaphore( &driver->tx_complete, 1 ); mico_rtos_init_semaphore( &driver->rx_complete, 1 ); mico_rtos_init_semaphore( &driver->sem_wakeup, 1 ); mico_rtos_init_mutex ( &driver->tx_mutex ); #else driver->tx_complete = false; driver->rx_complete = false; #endif /* Set Tx and Rx pin mode to UART peripheral */ platform_gpio_peripheral_pin_init( peripheral->tx_pin, ( peripheral->tx_pin_mux_mode | IOPORT_MODE_PULLUP ) ); platform_gpio_peripheral_pin_init( peripheral->rx_pin, ( peripheral->rx_pin_mux_mode | IOPORT_MODE_PULLUP ) ); /* Init CTS and RTS pins (if available) */ if ( peripheral->cts_pin != NULL && (config->flow_control == FLOW_CONTROL_CTS || config->flow_control == FLOW_CONTROL_CTS_RTS) ) { hardware_shaking = true; platform_gpio_peripheral_pin_init( peripheral->cts_pin, ( peripheral->cts_pin_mux_mode | IOPORT_MODE_PULLUP ) ); } if ( peripheral->rts_pin != NULL && (config->flow_control == FLOW_CONTROL_CTS || config->flow_control == FLOW_CONTROL_CTS_RTS) ) { hardware_shaking = true; platform_gpio_peripheral_pin_init( peripheral->rts_pin, ( peripheral->rts_pin_mux_mode | IOPORT_MODE_PULLUP ) ); } /* Enable the clock. */ if( pmc_is_periph_clk_enabled( peripheral->peripheral_id ) == 0 ) { flexcom_enable( peripheral->flexcom_base ); } flexcom_set_opmode( peripheral->flexcom_base, FLEXCOM_USART ); /* Enable the receiver and transmitter. */ usart_reset_tx( peripheral->port ); usart_reset_rx( peripheral->port ); /* Disable all the interrupts. */ usart_disable_interrupt( peripheral->port, 0xffffffff ); switch ( config->parity ) { case NO_PARITY: settings.parity_type = US_MR_PAR_NO; break; case EVEN_PARITY: settings.parity_type = US_MR_PAR_EVEN; break; case ODD_PARITY: settings.parity_type = US_MR_PAR_ODD; break; default: err = kParamErr; goto exit; } switch ( config->data_width) { case DATA_WIDTH_5BIT: settings.char_length = US_MR_CHRL_5_BIT; break; case DATA_WIDTH_6BIT: settings.char_length = US_MR_CHRL_6_BIT; break; case DATA_WIDTH_7BIT: settings.char_length = US_MR_CHRL_7_BIT; break; case DATA_WIDTH_8BIT: settings.char_length = US_MR_CHRL_8_BIT; break; case DATA_WIDTH_9BIT: settings.char_length = US_MR_MODE9; break; default: err = kParamErr; goto exit; } settings.baudrate = config->baud_rate; settings.stop_bits = ( config->stop_bits == STOP_BITS_1 ) ? US_MR_NBSTOP_1_BIT : US_MR_NBSTOP_2_BIT; settings.channel_mode= US_MR_CHMODE_NORMAL; /* Configure USART in serial mode. */ if (!hardware_shaking) { usart_init_rs232( peripheral->port, &settings, sysclk_get_peripheral_hz()); } else { usart_init_hw_handshaking( peripheral->port, &settings, sysclk_get_peripheral_hz()); } /* Enable uart interrupt */ NVIC_SetPriority( platform_flexcom_irq_numbers[peripheral->uart_id], 0x06 ); NVIC_EnableIRQ( platform_flexcom_irq_numbers[peripheral->uart_id] ); /* Enable PDC transmit */ pdc_enable_transfer( usart_get_pdc_base( peripheral->port ), PERIPH_PTCR_TXTEN | PERIPH_PTCR_RXTEN ); pdc_disable_transfer( usart_get_pdc_base( driver->peripheral->port ), PERIPH_PTCR_TXTDIS ); pdc_uart_packet.ul_addr = (uint32_t)driver->rx_ring_buffer->buffer; pdc_uart_packet.ul_size = (uint32_t)driver->rx_ring_buffer->size; pdc_rx_init( usart_get_pdc_base( peripheral->port ), &pdc_uart_packet, &pdc_uart_packet ); pdc_uart_tx_packet.ul_addr = (uint32_t)0; pdc_uart_tx_packet.ul_size = (uint32_t)1; pdc_tx_init( usart_get_pdc_base( driver->peripheral->port ), &pdc_uart_tx_packet, NULL ); usart_enable_interrupt( peripheral->port, US_IER_ENDRX | US_IER_RXBUFF | US_IER_RXRDY | US_IER_ENDTX ); /* Enable the receiver and transmitter. */ usart_enable_tx( peripheral->port ); usart_enable_rx( peripheral->port ); exit: platform_mcu_powersave_enable( ); return err; }
platform_result_t platform_uart_init( platform_uart_driver_t* driver, const platform_uart_t* peripheral, const platform_uart_config_t* config, wiced_ring_buffer_t* optional_ring_buffer ) { sam_usart_opt_t settings; UNUSED_PARAMETER(driver); UNUSED_PARAMETER(peripheral); UNUSED_PARAMETER(config); UNUSED_PARAMETER(optional_ring_buffer); pdc_packet_t dma_packet; if ( config->flow_control != FLOW_CONTROL_DISABLED ) { return WICED_UNSUPPORTED; } memset( &settings, 0, sizeof( settings ) ); switch ( config->data_width ) { case DATA_WIDTH_5BIT: settings.char_length = US_MR_CHRL_5_BIT; break; case DATA_WIDTH_6BIT: settings.char_length = US_MR_CHRL_6_BIT; break; case DATA_WIDTH_7BIT: settings.char_length = US_MR_CHRL_7_BIT; break; case DATA_WIDTH_8BIT: settings.char_length = US_MR_CHRL_8_BIT; break; case DATA_WIDTH_9BIT: default: return WICED_UNSUPPORTED; } switch ( config->parity ) { case ODD_PARITY: settings.parity_type = US_MR_PAR_ODD; break; case EVEN_PARITY: settings.parity_type = US_MR_PAR_EVEN; break; case NO_PARITY: settings.parity_type = US_MR_PAR_NO; break; default: break; } switch ( config->stop_bits ) { case STOP_BITS_1: settings.stop_bits = US_MR_NBSTOP_1_BIT; break; case STOP_BITS_2: settings.stop_bits = US_MR_NBSTOP_2_BIT; break; default: break; } settings.baudrate = config->baud_rate; settings.channel_mode = US_MR_CHMODE_NORMAL; driver->peripheral = peripheral; // /* Initialise TX and RX complete semaphores */ host_rtos_init_semaphore( &driver->tx_dma_complete ); host_rtos_init_semaphore( &driver->rx_dma_complete ); /* Set Tx and Rx pin mode to UART peripheral */ platform_gpio_peripheral_pin_init( peripheral->tx_pin, ( IOPORT_MODE_MUX_A | IOPORT_MODE_PULLUP ) ); platform_gpio_peripheral_pin_init( peripheral->rx_pin, ( IOPORT_MODE_MUX_A | IOPORT_MODE_PULLUP ) ); /* Init CTS and RTS pins (if available) */ if ( peripheral->cts_pin != NULL ) { platform_gpio_peripheral_pin_init( peripheral->cts_pin, ( IOPORT_MODE_MUX_A | IOPORT_MODE_PULLUP ) ); } if ( peripheral->rts_pin != NULL ) { platform_gpio_peripheral_pin_init( peripheral->rts_pin, ( IOPORT_MODE_MUX_A | IOPORT_MODE_PULLUP ) ); } /* Enable the peripheral clock in the PMC. */ sysclk_enable_peripheral_clock( peripheral->peripheral_id ); /* Enable the receiver and transmitter. */ usart_reset_tx( peripheral->peripheral ); usart_reset_rx( peripheral->peripheral ); /* Configure USART in serial mode. */ usart_init_rs232( peripheral->peripheral, &settings, CPU_CLOCK_HZ ); /* Disable all the interrupts. */ usart_disable_interrupt( peripheral->peripheral, 0xffffffff ); /* Enable uart interrupt */ NVIC_SetPriority( platform_uarts_irq_numbers[peripheral->uart_id], 0x06 ); NVIC_EnableIRQ( platform_uarts_irq_numbers[peripheral->uart_id] ); /* Enable PDC transmit */ pdc_enable_transfer( usart_get_pdc_base( peripheral->peripheral ), PERIPH_PTCR_TXTEN | PERIPH_PTCR_RXTEN ); driver->rx_ring_buffer = optional_ring_buffer; dma_packet.ul_addr = (uint32_t)driver->rx_ring_buffer->buffer; dma_packet.ul_size = (uint32_t)driver->rx_ring_buffer->size; pdc_rx_init( usart_get_pdc_base( peripheral->peripheral ), &dma_packet, &dma_packet ); usart_enable_interrupt( peripheral->peripheral, US_IER_ENDRX | US_IER_RXBUFF | US_IER_RXRDY | US_IER_ENDTX ); /* Enable the receiver and transmitter. */ usart_enable_tx( peripheral->peripheral ); usart_enable_rx( peripheral->peripheral ); return PLATFORM_SUCCESS; }