static platform_result_t spi_dma_transfer( const platform_spi_t* spi, const platform_spi_config_t* config ) { uint32_t loop_count; /* Enable dma channels that have just been configured */ DMA_Cmd( spi->tx_dma.stream, ENABLE ); DMA_Cmd( spi->rx_dma.stream, ENABLE ); /* Wait for DMA to complete */ /* TODO: This should wait on a semaphore that is triggered from an IRQ */ loop_count = 0; while ( ( DMA_GetFlagStatus( spi->tx_dma.stream, spi->tx_dma.complete_flags ) == RESET ) ) { loop_count++; /* Check if we've run out of time */ if ( loop_count >= (uint32_t) SPI_DMA_TIMEOUT_LOOPS ) { platform_gpio_output_high( config->chip_select ); return WICED_TIMEOUT; } } platform_gpio_output_high( config->chip_select ); return WICED_SUCCESS; }
void platform_init_external_devices( void ) { //wiced_bool_t button1_pressed; /* Initialise buttons to input by default */ platform_gpio_init( &platform_gpio_pins[BTN1], INPUT_PULL_UP ); #if 0 button1_pressed = wiced_gpio_input_get( BTN1 ) ? WICED_FALSE : WICED_TRUE; /* The button has inverse logic */ if ( button1_pressed != WICED_TRUE ) { platform_gpio_init( &platform_gpio_pins[LED], OUTPUT_PUSH_PULL ); platform_gpio_output_high( &platform_gpio_pins[LED] ); } #endif /* Initialise LEDs and turn off by default */ platform_gpio_init( &platform_gpio_pins[LED_R], OUTPUT_PUSH_PULL ); platform_gpio_init( &platform_gpio_pins[LED_G], OUTPUT_PUSH_PULL ); platform_gpio_init( &platform_gpio_pins[LED_B], OUTPUT_PUSH_PULL ); platform_gpio_output_high( &platform_gpio_pins[LED_R] ); platform_gpio_output_high( &platform_gpio_pins[LED_G] ); platform_gpio_output_high( &platform_gpio_pins[LED_B] ); #ifndef WICED_DISABLE_STDIO /* Initialise UART standard I/O */ platform_stdio_init( &platform_uart_drivers[STDIO_UART], &platform_uart_peripherals[STDIO_UART], &stdio_config ); #endif }
/* Checks if a factory reset is requested */ wiced_bool_t platform_check_factory_reset( void ) { uint32_t factory_reset_counter = 0; int led_state = 0; while ( ( 0 == platform_gpio_input_get( &platform_gpio_pins[ WICED_BUTTON1 ] ) ) && ( ( factory_reset_counter += 100 ) <= 5000 ) /* &&( WICED_SUCCESS == (wiced_result_t)host_rtos_delay_milliseconds( 100 ) ) */ ) { /* Factory reset button is being pressed. */ /* User Must press it for 5 seconds to ensure it was not accidental */ /* Toggle LED every 100ms */ if ( led_state == 0 ) { platform_gpio_output_high( &platform_gpio_pins[ WICED_LED1 ] ); led_state = 1; } else { platform_gpio_output_low( &platform_gpio_pins[ WICED_LED1 ] ); led_state = 0; } if ( factory_reset_counter == 5000 ) { return WICED_TRUE; } } return WICED_FALSE; }
/* Checks if a factory reset is requested */ wiced_bool_t platform_check_factory_reset( void ) { uint32_t factory_reset_counter = 0; #ifndef GPIO_LED_NOT_SUPPORTED int led_state = 0; #endif while ( ( 0 == platform_gpio_input_get( &platform_gpio_pins[ WICED_BUTTON1 ] ) ) &&( ( factory_reset_counter += PLATFORM_FACTORY_RESET_CHECK_PERIOD ) <= PLATFORM_FACTORY_RESET_TIMEOUT ) &&( WICED_SUCCESS == (wiced_result_t)host_rtos_delay_milliseconds( PLATFORM_FACTORY_RESET_CHECK_PERIOD ) ) ) { /* Factory reset button is being pressed. */ /* User Must press it for 5 seconds to ensure it was not accidental */ #ifndef GPIO_LED_NOT_SUPPORTED /* Toggle LED every 100ms */ if ( led_state == 0 ) { platform_gpio_output_high( &platform_gpio_pins[ WICED_LED1 ] ); led_state = 1; } else { platform_gpio_output_low( &platform_gpio_pins[ WICED_LED1 ] ); led_state = 0; } #endif if ( factory_reset_counter == 5000 ) { return WICED_TRUE; } } return WICED_FALSE; }
OSStatus host_platform_spi_transfer( bus_transfer_direction_t dir, uint8_t* buffer, uint16_t buffer_length ) { OSStatus result; pdc_packet_t pdc_spi_packet = { (uint32_t)buffer, buffer_length }; Pdc* spi_pdc = spi_get_pdc_base( wifi_spi.port ); platform_mcu_powersave_disable(); platform_gpio_output_low( &wifi_spi_pins[WIFI_PIN_SPI_CS] ); pdc_tx_init( spi_pdc, &pdc_spi_packet, NULL); if ( dir == BUS_READ ) { pdc_rx_init( spi_pdc, &pdc_spi_packet, NULL); spi_enable_interrupt(wifi_spi.port, SPI_IER_RXBUFF ); pdc_enable_transfer( spi_pdc, PERIPH_PTCR_TXTEN | PERIPH_PTCR_RXTEN ); } if ( dir == BUS_WRITE ) { spi_enable_interrupt( wifi_spi.port, SPI_IER_ENDTX ); pdc_enable_transfer( spi_pdc, PERIPH_PTCR_TXTEN ); } result = mico_rtos_get_semaphore( &spi_transfer_finished_semaphore, 100 ); platform_gpio_output_high( &wifi_spi_pins[WIFI_PIN_SPI_CS] ); platform_mcu_powersave_enable(); return result; }
uint32_t platform_get_factory_reset_button_time ( uint32_t max_time ) { uint32_t button_press_timer = 0; int led_state = 0; /* Initialise input */ platform_gpio_init( &platform_gpio_pins[ PLATFORM_FACTORY_RESET_BUTTON_GPIO ], INPUT_PULL_UP ); while ( (PLATFORM_FACTORY_RESET_PRESSED_STATE == platform_gpio_input_get(&platform_gpio_pins[ PLATFORM_FACTORY_RESET_BUTTON_GPIO ])) ) { /* How long is the "Factory Reset" button being pressed. */ host_rtos_delay_milliseconds( PLATFORM_FACTORY_RESET_CHECK_PERIOD ); /* Toggle LED every PLATFORM_FACTORY_RESET_CHECK_PERIOD */ if ( led_state == 0 ) { platform_gpio_output_high( &platform_gpio_pins[ PLATFORM_FACTORY_RESET_LED_GPIO ] ); led_state = 1; } else { platform_gpio_output_low( &platform_gpio_pins[ PLATFORM_FACTORY_RESET_LED_GPIO ] ); led_state = 0; } button_press_timer += PLATFORM_FACTORY_RESET_CHECK_PERIOD; if ((max_time > 0) && (button_press_timer >= max_time)) { break; } } /* turn off the LED */ if (PLATFORM_FACTORY_RESET_LED_ON_STATE == 1) { platform_gpio_output_low( &platform_gpio_pins[ PLATFORM_FACTORY_RESET_LED_GPIO ] ); } else { platform_gpio_output_high( &platform_gpio_pins[ PLATFORM_FACTORY_RESET_LED_GPIO ] ); } return button_press_timer; }
void host_platform_power_wifi( bool power_enabled ) { #if defined ( MICO_USE_WIFI_POWER_PIN ) && defined ( MICO_USE_WIFI_POWER_PIN_ACTIVE_HIGH ) ( power_enabled == true ) ? platform_gpio_output_high( &wifi_control_pins[WIFI_PIN_POWER] ) : platform_gpio_output_low ( &wifi_control_pins[WIFI_PIN_POWER] ); #elif defined ( MICO_USE_WIFI_POWER_PIN ) ( power_enabled == true ) ? platform_gpio_output_low ( &wifi_control_pins[WIFI_PIN_POWER] ) : platform_gpio_output_high( &wifi_control_pins[WIFI_PIN_POWER] ); #else UNUSED_PARAMETER( power_enabled ); #endif }
/***************************************************************************** ** ** Function UPIO_Set ** ** Description ** This function sets one or more GPIO devices to the given state. ** Multiple GPIOs of the same type can be masked together to set more ** than one GPIO. This function can only be used on types UPIO_LED and ** UPIO_GENERAL. ** ** Input Parameters: ** type The type of device. ** pio Indicates the particular GPIOs. ** state The desired state. ** ** Output Parameter: ** None. ** ** Returns: ** None. ** *****************************************************************************/ UDRV_API void UPIO_Set( tUPIO_TYPE type, tUPIO pio, tUPIO_STATE state ) { //DRV_TRACE_DEBUG2("UPIO_Set %d, %s", pio, UPIO_OFF == state ? "UPIO_OFF" : "UPIO_ON"); #if HCILP_INCLUDED if(UPIO_OFF == state) { platform_gpio_output_low( mico_bt_control_pins[MICO_BT_PIN_DEVICE_WAKE] ); }else { platform_gpio_output_high( mico_bt_control_pins[MICO_BT_PIN_DEVICE_WAKE] ); } #endif }
static int h4_set_baudrate(uint32_t baudrate){ #ifdef _STM32F205RGT6_ // directly use STM peripheral functions to change baud rate dynamically // set TX to high log_info("h4_set_baudrate %u", (int) baudrate); const platform_gpio_t* gpio = wiced_bt_uart_pins[WICED_BT_PIN_UART_TX]; platform_gpio_output_high(gpio); // reconfigure TX pin as GPIO GPIO_InitTypeDef gpio_init_structure; gpio_init_structure.GPIO_Speed = GPIO_Speed_50MHz; gpio_init_structure.GPIO_Mode = GPIO_Mode_OUT; gpio_init_structure.GPIO_OType = GPIO_OType_PP; gpio_init_structure.GPIO_PuPd = GPIO_PuPd_NOPULL; gpio_init_structure.GPIO_Pin = (uint32_t) ( 1 << gpio->pin_number ); GPIO_Init( gpio->port, &gpio_init_structure ); // disable USART USART_Cmd( wiced_bt_uart_peripheral->port, DISABLE ); // setup init structure USART_InitTypeDef uart_init_structure; uart_init_structure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; uart_init_structure.USART_BaudRate = baudrate; uart_init_structure.USART_WordLength = USART_WordLength_8b; uart_init_structure.USART_StopBits = USART_StopBits_1; uart_init_structure.USART_Parity = USART_Parity_No; uart_init_structure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS; #ifdef WICED_BT_UART_MANUAL_CTS_RTS uart_init_structure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; #endif USART_Init(wiced_bt_uart_peripheral->port, &uart_init_structure); // enable USART again USART_Cmd( wiced_bt_uart_peripheral->port, ENABLE ); // set TX pin as USART again gpio_init_structure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init( gpio->port, &gpio_init_structure ); #else log_error("h4_set_baudrate not implemented for this WICED Platform"); #endif return 0; }
OSStatus host_platform_spi_transfer( bus_transfer_direction_t dir, uint8_t* buffer, uint16_t buffer_length ) { OSStatus result; uint32_t junk; platform_mcu_powersave_disable(); wifi_spi.tx_dma.stream->NDTR = buffer_length; wifi_spi.tx_dma.stream->M0AR = (uint32_t) buffer; if ( dir == BUS_READ ) { wifi_spi.rx_dma.stream->NDTR = buffer_length; wifi_spi.rx_dma.stream->M0AR = (uint32_t) buffer; wifi_spi.rx_dma.stream->CR |= DMA_MemoryInc_Enable | ( 1 << 4); } else { wifi_spi.rx_dma.stream->NDTR = buffer_length; wifi_spi.rx_dma.stream->M0AR = (uint32_t) &junk; wifi_spi.rx_dma.stream->CR &= ( ~DMA_MemoryInc_Enable ) | ( 1 << 4); } platform_gpio_output_low( &wifi_spi_pins[WIFI_PIN_SPI_CS] ); DMA_Cmd( wifi_spi.rx_dma.stream, ENABLE ); DMA_Cmd( wifi_spi.tx_dma.stream, ENABLE ); /* Wait for DMA TX to complete */ result = mico_rtos_get_semaphore( &spi_transfer_finished_semaphore, 100 ); // loop_count = 0; // while ( ( DMA_GetFlagStatus( SPIX_DMA_RX_STREAM, DMA_FLAG_TCIF3 ) == RESET ) && ( loop_count < (uint32_t) DMA_TIMEOUT_LOOPS ) ) // { // loop_count++; // } DMA_Cmd( wifi_spi.rx_dma.stream, DISABLE ); DMA_Cmd( wifi_spi.tx_dma.stream, DISABLE ); /* Clear the CS pin and the DMA status flag */ platform_gpio_output_high( &wifi_spi_pins[WIFI_PIN_SPI_CS] ); /* CS high (to deselect) */ clear_dma_interrupts( wifi_spi.rx_dma.stream, wifi_spi.rx_dma.complete_flags ); clear_dma_interrupts( wifi_spi.tx_dma.stream, wifi_spi.tx_dma.complete_flags ); platform_mcu_powersave_enable(); return result; }
OSStatus platform_gpio_output_trigger( const platform_gpio_t* gpio ) { OSStatus err = kNoErr; uint32_t regValue; uint32_t mask = (uint32_t)1 << gpio->pin; platform_mcu_powersave_disable(); require_action_quiet( gpio != NULL, exit, err = kParamErr); regValue = GpioGetReg( GPIO_A_OUT + gpio->port ); (regValue&mask)? platform_gpio_output_low( gpio ) : platform_gpio_output_high( gpio ); exit: platform_mcu_powersave_enable(); return err; }
wwd_result_t host_platform_bus_init( void ) { /* Setup the interrupt input for WLAN_IRQ */ platform_gpio_init( &wifi_spi_pins[WWD_PIN_SPI_IRQ], INPUT_HIGH_IMPEDANCE ); //platform_gpio_irq_enable( &wifi_control_pins[WWD_PIN_IRQ], IRQ_TRIGGER_RISING_EDGE, spi_irq_handler, 0 ); #ifdef WICED_WIFI_USE_GPIO_FOR_BOOTSTRAP_0 /* Set GPIO_B[1:0] to 01 to put WLAN module into gSPI mode */ platform_gpio_init( &wifi_control_pins[WWD_PIN_BOOTSTRAP_0], OUTPUT_PUSH_PULL ); platform_gpio_output_high( &wifi_control_pins[WWD_PIN_BOOTSTRAP_0] ); #endif #ifdef WICED_WIFI_USE_GPIO_FOR_BOOTSTRAP_1 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 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; }
static wiced_result_t h4_rx_worker_receive_packet(void * arg){ #ifdef WICED_BT_UART_MANUAL_CTS_RTS platform_gpio_output_low(wiced_bt_uart_pins[WICED_BT_PIN_UART_RTS]); #endif while (1){ rx_worker_read_pos = 0; h4_rx_worker_receive_bytes(1); switch (hci_packet[0]){ case HCI_EVENT_PACKET: h4_rx_worker_receive_bytes(HCI_EVENT_HEADER_SIZE); h4_rx_worker_receive_bytes(hci_packet[2]); break; case HCI_ACL_DATA_PACKET: h4_rx_worker_receive_bytes(HCI_ACL_HEADER_SIZE); h4_rx_worker_receive_bytes(little_endian_read_16( hci_packet, 3)); break; case HCI_SCO_DATA_PACKET: h4_rx_worker_receive_bytes(HCI_SCO_HEADER_SIZE); h4_rx_worker_receive_bytes(hci_packet[3]); break; default: // try again log_error("h4_rx_worker_receive_packet: invalid packet type 0x%02x", hci_packet[0]); continue; } #ifdef WICED_BT_UART_MANUAL_CTS_RTS platform_gpio_output_high(wiced_bt_uart_pins[WICED_BT_PIN_UART_RTS]); #endif // deliver packet on main thread btstack_run_loop_wiced_execute_code_on_main_thread(&h4_main_deliver_packet, NULL); return WICED_SUCCESS; } }
platform_result_t platform_spi_transfer( const platform_spi_t* spi, const platform_spi_config_t* config, const platform_spi_message_segment_t* segments, uint16_t number_of_segments ) { int i = 0; platform_result_t result; platform_mcu_powersave_disable( ); platform_gpio_output_low( config->chip_select ); for ( i = 0; i < number_of_segments; i++ ) { result = sam4s_spi_transfer_internal( spi, segments[i].tx_buffer, segments[i].rx_buffer, segments[i].length ); if ( result != PLATFORM_SUCCESS ) { return result; } } platform_gpio_output_high( config->chip_select ); platform_mcu_powersave_enable( ); return PLATFORM_SUCCESS; }
// Magicoe OSStatus platform_spi_transfer( const platform_spi_t* spi, const platform_spi_config_t* config, const platform_spi_message_segment_t* segments, uint16_t number_of_segments ) OSStatus platform_spi_transfer( platform_spi_driver_t* driver, const platform_spi_config_t* config, const platform_spi_message_segment_t* segments, uint16_t number_of_segments ) { OSStatus err = kNoErr; uint32_t count = 0; uint32_t i; const platform_spi_message_segment_t *pSeg; uint32_t dmaXferLen; DMA_CHDESC_T *pTxDesc, *pRxDesc; LPC_SPI_T *pSPI; uint32_t dmaRxChnNdx, dmaTxChnNdx; const uint8_t *pcTx; uint8_t *pRx; require_action_quiet( ( driver != NULL ) && ( config != NULL ) && ( segments != NULL ) && ( number_of_segments != 0 ), exit, err = kParamErr); // save the driver pointer so that in DMA IRQ callback we can access its members platform_mcu_powersave_disable(); pSeg = segments; pTxDesc = (DMA_CHDESC_T *) g_pDMA->SRAMBASE + driver->peripheral->dmaTxChnNdx; pRxDesc = (DMA_CHDESC_T *) g_pDMA->SRAMBASE + driver->peripheral->dmaRxChnNdx; pSPI = driver->peripheral->port; if (pSPI == LPC_SPI0) s_pSPIDrvs[0] = driver; dmaRxChnNdx = driver->peripheral->dmaRxChnNdx , dmaTxChnNdx = driver->peripheral->dmaTxChnNdx; driver->xferErr = 0; /* Activate chip select */ platform_gpio_output_low( config->chip_select ); for ( i = 0; i < number_of_segments; i++, pSeg++ ) { // transfer one seg count = pSeg->length; if (0 == count) continue; pcTx = pSeg->tx_buffer , pRx = pSeg->rx_buffer; do { dmaXferLen = count > DMA_MAX_XFER_CNT ? DMA_MAX_XFER_CNT : count; count -= dmaXferLen; driver->isRxDone = driver->isTxDone = 0; #if 0 { if (pRx != 0) { pSPI->TXCTRL &= ~(1UL<<22); if (pSPI->STAT & SPI_STAT_RXRDY) pSPI->RXDAT; while (dmaXferLen--) { while (!(pSPI->STAT & SPI_STAT_TXRDY)); pSPI->TXDAT = *pcTx++; while (!(pSPI->STAT & SPI_STAT_RXRDY)); *pRx++ = (uint8_t) pSPI->RXDAT; } } else { pSPI->TXCTRL |= (1UL<<22); while (dmaXferLen--) { while (!(pSPI->STAT & SPI_STAT_TXRDY)); pSPI->TXDAT = *pcTx++; } } while (!(pSPI->STAT & SPI_STAT_TXRDY)); } #else pTxDesc->next = 0; pTxDesc->dest = DMA_ADDR(&pSPI->TXDAT); pTxDesc->source = DMA_ADDR(pcTx) + dmaXferLen - 1; pTxDesc->xfercfg = DMA_XFERCFG_CFGVALID | DMA_XFERCFG_SETINTA | DMA_XFERCFG_SWTRIG | DMA_XFERCFG_WIDTH_8 | DMA_XFERCFG_SRCINC_1 | DMA_XFERCFG_DSTINC_0 | DMA_XFERCFG_XFERCOUNT(dmaXferLen); if (pRx != 0) { pSPI->TXCTRL &= ~(1UL<<22); driver->isRx = 1; pRxDesc->next = 0; pRxDesc->source = DMA_ADDR(&pSPI->RXDAT); pRxDesc->dest = DMA_ADDR(pRx) + dmaXferLen - 1; pRxDesc->xfercfg = DMA_XFERCFG_CFGVALID | DMA_XFERCFG_SETINTA | DMA_XFERCFG_SWTRIG | DMA_XFERCFG_WIDTH_8 | DMA_XFERCFG_DSTINC_1 | DMA_XFERCFG_SRCINC_0 | DMA_XFERCFG_XFERCOUNT(dmaXferLen); // start RX DMA g_pDMA->DMACH[dmaRxChnNdx].XFERCFG = pRxDesc->xfercfg; } else { driver->isRx = 0; pSPI->TXCTRL |= (1UL<<22); } // start TX DMA g_pDMA->DMACH[dmaTxChnNdx].XFERCFG = pTxDesc->xfercfg; #ifndef NO_MICO_RTOS mico_rtos_get_semaphore(&driver->sem_xfer_done, MICO_WAIT_FOREVER); #else while(1) { if (driver->isTxDone) { if (!driver->isRx || driver->isRxDone) break; } __WFI(); } #endif #endif if (driver->xferErr) { err = kGeneralErr; break; } // >>> update read and/or write pointers pcTx += dmaXferLen; if (pRx != 0) pRx += dmaXferLen; // <<< } while (count); } platform_gpio_output_high( config->chip_select ); exit: platform_mcu_powersave_enable( ); return err; }
void host_platform_reset_wifi( bool reset_asserted ) { #if defined (MICO_USE_WIFI_RESET_PIN ) ( reset_asserted == true ) ? platform_gpio_output_low( &wifi_control_pins[ WIFI_PIN_RESET ] ) : platform_gpio_output_high( &wifi_control_pins[ WIFI_PIN_RESET ] ); #else UNUSED_PARAMETER( reset_asserted ); #endif }
wiced_result_t bluetooth_wiced_init_platform( void ) { if ( wiced_bt_control_pins[ WICED_BT_PIN_HOST_WAKE ] != NULL ) { RETURN_IF_FAILURE( platform_gpio_init( wiced_bt_control_pins[WICED_BT_PIN_HOST_WAKE], INPUT_HIGH_IMPEDANCE ) ); } if ( wiced_bt_control_pins[ WICED_BT_PIN_DEVICE_WAKE ] != NULL ) { RETURN_IF_FAILURE( platform_gpio_init( wiced_bt_control_pins[ WICED_BT_PIN_DEVICE_WAKE ], OUTPUT_PUSH_PULL ) ); RETURN_IF_FAILURE( platform_gpio_output_low( wiced_bt_control_pins[ WICED_BT_PIN_DEVICE_WAKE ] ) ); wiced_rtos_delay_milliseconds( 100 ); } /* Configure Reg Enable pin to output. Set to HIGH */ if ( wiced_bt_control_pins[ WICED_BT_PIN_POWER ] != NULL ) { RETURN_IF_FAILURE( platform_gpio_init( wiced_bt_control_pins[ WICED_BT_PIN_POWER ], OUTPUT_OPEN_DRAIN_PULL_UP ) ); RETURN_IF_FAILURE( platform_gpio_output_high( wiced_bt_control_pins[ WICED_BT_PIN_POWER ] ) ); } if ( wiced_bt_uart_config.flow_control == FLOW_CONTROL_DISABLED ) { /* Configure RTS pin to output. Set to HIGH */ RETURN_IF_FAILURE( platform_gpio_init( wiced_bt_uart_pins[WICED_BT_PIN_UART_RTS], OUTPUT_OPEN_DRAIN_PULL_UP ) ); RETURN_IF_FAILURE( platform_gpio_output_high( wiced_bt_uart_pins[WICED_BT_PIN_UART_RTS] ) ); /* Configure CTS pin to input pull-up */ RETURN_IF_FAILURE( platform_gpio_init( wiced_bt_uart_pins[WICED_BT_PIN_UART_CTS], INPUT_PULL_UP ) ); } if ( wiced_bt_control_pins[ WICED_BT_PIN_RESET ] != NULL ) { RETURN_IF_FAILURE( platform_gpio_init( wiced_bt_control_pins[ WICED_BT_PIN_RESET ], OUTPUT_PUSH_PULL ) ); RETURN_IF_FAILURE( platform_gpio_output_high( wiced_bt_control_pins[ WICED_BT_PIN_RESET ] ) ); /* Configure USART comms */ RETURN_IF_FAILURE( bluetooth_wiced_init_config_uart( &wiced_bt_uart_config ) ); /* Reset bluetooth chip */ RETURN_IF_FAILURE( platform_gpio_output_low( wiced_bt_control_pins[ WICED_BT_PIN_RESET ] ) ); wiced_rtos_delay_milliseconds( 10 ); RETURN_IF_FAILURE( platform_gpio_output_high( wiced_bt_control_pins[ WICED_BT_PIN_RESET ] ) ); } else { /* Configure USART comms */ RETURN_IF_FAILURE( bluetooth_wiced_init_config_uart( &wiced_bt_uart_config ) ); } wiced_rtos_delay_milliseconds( BLUETOOTH_CHIP_STABILIZATION_DELAY ); if ( wiced_bt_uart_config.flow_control == FLOW_CONTROL_DISABLED ) { /* Bluetooth chip is ready. Pull host's RTS low */ RETURN_IF_FAILURE( platform_gpio_output_low( wiced_bt_uart_pins[WICED_BT_PIN_UART_RTS] ) ); } /* Wait for Bluetooth chip to pull its RTS (host's CTS) low. From observation using CRO, it takes the bluetooth chip > 170ms to pull its RTS low after CTS low */ while ( platform_gpio_input_get( wiced_bt_uart_pins[ WICED_BT_PIN_UART_CTS ] ) == WICED_TRUE ) { wiced_rtos_delay_milliseconds( 10 ); } return WICED_SUCCESS; }
OSStatus platform_spi_transfer( platform_spi_driver_t* driver, const platform_spi_config_t* config, const platform_spi_message_segment_t* segments, uint16_t number_of_segments ) { OSStatus err = kNoErr; uint32_t count = 0; uint16_t i; platform_mcu_powersave_disable(); require_action_quiet( ( driver != NULL ) && ( config != NULL ) && ( segments != NULL ) && ( number_of_segments != 0 ), exit, err = kParamErr); /* Activate chip select */ platform_gpio_output_low( config->chip_select ); for ( i = 0; i < number_of_segments; i++ ) { /* Check if we are using DMA */ if ( config->mode & SPI_USE_DMA ) { if( segments[ i ].length != 0){ //platform_log( "length: %d, i:%d", segments[ i ].length, i ); spi_dma_config( driver->peripheral, &segments[ i ] ); err = spi_dma_transfer( driver->peripheral, config ); require_noerr(err, cleanup_transfer); } } else { count = segments[i].length; /* in interrupt-less mode */ if ( config->bits == 8 ) { const uint8_t* send_ptr = ( const uint8_t* )segments[i].tx_buffer; uint8_t* rcv_ptr = ( uint8_t* )segments[i].rx_buffer; while ( count-- ) { uint16_t data = 0xFF; if ( send_ptr != NULL ) { data = *send_ptr++; } data = spi_transfer( driver->peripheral, data ); if ( rcv_ptr != NULL ) { *rcv_ptr++ = (uint8_t)data; } } } else if ( config->bits == 16 ) { const uint16_t* send_ptr = (const uint16_t *) segments[i].tx_buffer; uint16_t* rcv_ptr = (uint16_t *) segments[i].rx_buffer; /* Check that the message length is a multiple of 2 */ require_action_quiet( ( count % 2 ) == 0, cleanup_transfer, err = kSizeErr); /* Transmit/receive data stream, 16-bit at time */ while ( count != 0 ) { uint16_t data = 0xFFFF; if ( send_ptr != NULL ) { data = *send_ptr++; } data = spi_transfer( driver->peripheral, data ); if ( rcv_ptr != NULL ) { *rcv_ptr++ = data; } count -= 2; } } } } cleanup_transfer: /* Deassert chip select */ platform_gpio_output_high( config->chip_select ); exit: platform_mcu_powersave_enable( ); return err; }
OSStatus platform_spi_init( platform_spi_driver_t* driver, const platform_spi_t* peripheral, const platform_spi_config_t* config ) { SPI_InitTypeDef spi_init; OSStatus err; platform_mcu_powersave_disable(); require_action_quiet( ( driver != NULL ) && ( peripheral != NULL ) && ( config != NULL ), exit, err = kParamErr); /* Calculate prescaler */ err = calculate_prescaler( config->speed, &spi_init.SPI_BaudRatePrescaler ); require_noerr(err, exit); /* Configure data-width */ if ( config->bits == 8 ) { spi_init.SPI_DataSize = SPI_DataSize_8b; } else if ( config->bits == 16 ) { require_action( !(config->mode & SPI_USE_DMA), exit, err = kUnsupportedErr); spi_init.SPI_DataSize = SPI_DataSize_16b; } else { err = kUnsupportedErr; goto exit; } /* Configure MSB or LSB */ if ( config->mode & SPI_MSB_FIRST ) { spi_init.SPI_FirstBit = SPI_FirstBit_MSB; } else { spi_init.SPI_FirstBit = SPI_FirstBit_LSB; } /* Configure mode CPHA and CPOL */ if ( config->mode & SPI_CLOCK_IDLE_HIGH ) { spi_init.SPI_CPOL = SPI_CPOL_High; } else { spi_init.SPI_CPOL = SPI_CPOL_Low; } if ( config->mode & SPI_CLOCK_RISING_EDGE ) { spi_init.SPI_CPHA = ( config->mode & SPI_CLOCK_IDLE_HIGH ) ? SPI_CPHA_2Edge : SPI_CPHA_1Edge; } else { spi_init.SPI_CPHA = ( config->mode & SPI_CLOCK_IDLE_HIGH ) ? SPI_CPHA_1Edge : SPI_CPHA_2Edge; } driver->peripheral = (platform_spi_t *)peripheral; /* Init SPI GPIOs */ platform_gpio_set_alternate_function( peripheral->pin_clock->port, peripheral->pin_clock->pin_number, GPIO_OType_PP, GPIO_PuPd_NOPULL, peripheral->gpio_af ); platform_gpio_set_alternate_function( peripheral->pin_mosi->port, peripheral->pin_mosi->pin_number, GPIO_OType_PP, GPIO_PuPd_NOPULL, peripheral->gpio_af ); platform_gpio_set_alternate_function( peripheral->pin_miso->port, peripheral->pin_miso->pin_number, GPIO_OType_PP, GPIO_PuPd_UP, peripheral->gpio_af ); /* Init the chip select GPIO */ platform_gpio_init( config->chip_select, OUTPUT_PUSH_PULL ); platform_gpio_output_high( config->chip_select ); /* Enable SPI peripheral clock */ (peripheral->peripheral_clock_func)( peripheral->peripheral_clock_reg, ENABLE ); (peripheral->peripheral_clock_func)( peripheral->peripheral_clock_reg, ENABLE ); SPI_I2S_DeInit( peripheral->port ); spi_init.SPI_Direction = SPI_Direction_2Lines_FullDuplex; spi_init.SPI_Mode = SPI_Mode_Master; spi_init.SPI_NSS = SPI_NSS_Soft; spi_init.SPI_CRCPolynomial = 0x7; /* reset value */ SPI_CalculateCRC( peripheral->port, DISABLE ); /* Init and enable SPI */ SPI_Init( peripheral->port, &spi_init ); SPI_I2S_DMACmd( peripheral->port, SPI_I2S_DMAReq_Rx, DISABLE ); SPI_I2S_DMACmd( peripheral->port, SPI_I2S_DMAReq_Tx, DISABLE ); SPI_Cmd ( peripheral->port, ENABLE ); if ( config->mode & SPI_USE_DMA ){ DMA_DeInit( peripheral->rx_dma.stream ); DMA_DeInit( peripheral->tx_dma.stream ); if ( peripheral->tx_dma.controller == DMA1 ) { RCC->AHB1ENR |= RCC_AHB1Periph_DMA1; } else { RCC->AHB1ENR |= RCC_AHB1Periph_DMA2; } if ( peripheral->rx_dma.controller == DMA1 ) { RCC->AHB1ENR |= RCC_AHB1Periph_DMA1; } else { RCC->AHB1ENR |= RCC_AHB1Periph_DMA2; } SPI_I2S_DMACmd( peripheral->port, SPI_I2S_DMAReq_Rx, ENABLE ); SPI_I2S_DMACmd( peripheral->port, SPI_I2S_DMAReq_Tx, ENABLE ); } exit: platform_mcu_powersave_enable(); return err; }
platform_result_t platform_spi_transfer( const platform_spi_t* spi, const platform_spi_config_t* config, const platform_spi_message_segment_t* segments, uint16_t number_of_segments ) { platform_result_t result = PLATFORM_SUCCESS; uint32_t count = 0; uint16_t i; wiced_assert( "bad argument", ( spi != NULL ) && ( config != NULL ) && ( segments != NULL ) && ( number_of_segments != 0 ) ); platform_mcu_powersave_disable(); /* Activate chip select */ platform_gpio_output_low( config->chip_select ); for ( i = 0; i < number_of_segments; i++ ) { /* Check if we are using DMA */ if ( config->mode & SPI_USE_DMA ) { spi_dma_config( spi, &segments[ i ] ); result = spi_dma_transfer( spi, config ); if ( result != PLATFORM_SUCCESS ) { goto cleanup_transfer; } } else { count = segments[i].length; /* in interrupt-less mode */ if ( config->bits == 8 ) { const uint8_t* send_ptr = ( const uint8_t* )segments[i].tx_buffer; uint8_t* rcv_ptr = ( uint8_t* )segments[i].rx_buffer; while ( count-- ) { uint16_t data = 0xFF; if ( send_ptr != NULL ) { data = *send_ptr++; } data = spi_transfer( spi, data ); if ( rcv_ptr != NULL ) { *rcv_ptr++ = (uint8_t)data; } } } else if ( config->bits == 16 ) { const uint16_t* send_ptr = (const uint16_t *) segments[i].tx_buffer; uint16_t* rcv_ptr = (uint16_t *) segments[i].rx_buffer; /* Check that the message length is a multiple of 2 */ if ( ( count % 2 ) != 0 ) { result = WICED_ERROR; goto cleanup_transfer; } /* Transmit/receive data stream, 16-bit at time */ while ( count != 0 ) { uint16_t data = 0xFFFF; if ( send_ptr != NULL ) { data = *send_ptr++; } data = spi_transfer( spi, data ); if ( rcv_ptr != NULL ) { *rcv_ptr++ = data; } count -= 2; } } } } cleanup_transfer: /* Deassert chip select */ platform_gpio_output_high( config->chip_select ); platform_mcu_powersave_enable( ); return result; }
// Magicoe OSStatus platform_spi_init( const platform_spi_t* spi, const platform_spi_config_t* config ) OSStatus platform_spi_init( platform_spi_driver_t* driver, const platform_spi_t* peripheral, const platform_spi_config_t* config ) { // SPI_InitTypeDef spi_init; OSStatus err = kNoErr; platform_mcu_powersave_disable(); uint32_t t1; //general var require_action_quiet( ( peripheral != NULL ) && ( config != NULL ), exit, err = kParamErr); require_action_quiet( ( peripheral->port == LPC_SPI0 ), exit, err = kParamErr); driver->isRxDone = driver->isTxDone = 0; #ifndef NO_MICO_RTOS if (driver->sem_xfer_done == 0) mico_rtos_init_semaphore(&driver->sem_xfer_done, 1); #endif // >>> Init Pin mux t1 = IOCON_MODE_INACT | IOCON_DIGITAL_EN | IOCON_INPFILT_OFF; if (peripheral->port == LPC_SPI0) { if (config->speed >= 24000000) t1 |= 1UL<<9; // enable fast slew g_pIO->PIO[0][11] = IOCON_FUNC1 | t1;/* SPI0_SCK */ g_pIO->PIO[0][12] = IOCON_FUNC1 | t1; /* SPI0_MOSI */ g_pIO->PIO[0][13] = IOCON_FUNC1 | t1; /* SPI0_MISO */ // config CS pin as GPIO g_pIO->PIO[config->chip_select->port][config->chip_select->pin_number] = IOCON_FUNC0 | IOCON_MODE_INACT | IOCON_DIGITAL_EN | IOCON_INPFILT_OFF; platform_gpio_output_high(config->chip_select); platform_gpio_init(config->chip_select, OUTPUT_PUSH_PULL); } // <<< // >>> // initialize SPI peripheral { uint32_t bv; LPC_SPI_T *pSPI = peripheral->port; // >>> bv = pSPI == LPC_SPI0 ? 1UL << 9 : 1UL<<10; g_pASys->ASYNCAPBCLKCTRLSET = bv; // enable clock to SPI g_pASys->ASYNCPRESETCTRLSET = bv; g_pASys->ASYNCPRESETCTRLCLR = bv; // <<< // predly | postDly| fraDly | xferDly pSPI->DLY = 1UL<<0 | 1UL<<4 | 1UL<<8 | 1UL<<12; pSPI->DLY = 0; t1 = Chip_Clock_GetAsyncSyscon_ClockRate(); t1 = (t1 + config->speed - 1) / config->speed; pSPI->DIV = t1 - 1; // proper division pSPI->TXCTRL = (config->bits - 1) << 24; // 8 bits per frame // Enable | master | no loopback t1 = 1UL<<0 | 1UL<<2 | 0UL<<7; // determine SPI mode if (config->mode & SPI_CLOCK_IDLE_HIGH) { t1 |= 1UL<<5; // CPOL = 1 if (t1 & SPI_CLOCK_RISING_EDGE) t1 |= 1UL<<4; // CPHA = 1 } else { if (!(t1 & SPI_CLOCK_RISING_EDGE)) t1 |= 1UL<<4; } pSPI->CFG = t1; } // <<< g_pDMA->DMACOMMON[0].ENABLESET = (1UL<<peripheral->dmaRxChnNdx) | (1UL<<peripheral->dmaTxChnNdx); g_pDMA->DMACOMMON[0].INTENSET = (1UL<<peripheral->dmaRxChnNdx) | (1UL<<peripheral->dmaTxChnNdx); g_pDMA->DMACH[peripheral->dmaRxChnNdx].CFG = DMA_CFG_PERIPHREQEN | DMA_CFG_TRIGBURST_SNGL | DMA_CFG_CHPRIORITY(1); g_pDMA->DMACH[peripheral->dmaTxChnNdx].CFG = DMA_CFG_PERIPHREQEN | DMA_CFG_TRIGBURST_SNGL | DMA_CFG_CHPRIORITY(1); exit: platform_mcu_powersave_enable(); return err; }
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 host_platform_bus_init( void ) { SPI_InitTypeDef spi_init; DMA_InitTypeDef dma_init_structure; uint32_t a; platform_mcu_powersave_disable(); mico_rtos_init_semaphore(&spi_transfer_finished_semaphore, 1); /* Enable SPI_SLAVE DMA clock */ if ( wifi_spi.tx_dma.controller == DMA1 ) { RCC->AHB1ENR |= RCC_AHB1Periph_DMA1; } else { RCC->AHB1ENR |= RCC_AHB1Periph_DMA2; } if ( wifi_spi.rx_dma.controller == DMA1 ) { RCC->AHB1ENR |= RCC_AHB1Periph_DMA1; } else { RCC->AHB1ENR |= RCC_AHB1Periph_DMA2; } /* Enable SPI_SLAVE Periph clock */ (wifi_spi.peripheral_clock_func)( wifi_spi.peripheral_clock_reg, ENABLE ); /* Enable SYSCFG. Needed for selecting EXTI interrupt line */ RCC_APB2PeriphClockCmd( RCC_APB2Periph_SYSCFG, ENABLE ); /* 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] ); /* Setup the SPI lines */ for ( a = WIFI_PIN_SPI_CLK; a < WIFI_PIN_SPI_MAX; a++ ) { platform_gpio_set_alternate_function( wifi_spi_pins[ a ].port, wifi_spi_pins[ a ].pin_number, GPIO_OType_PP, GPIO_PuPd_NOPULL, wifi_spi.gpio_af ); } #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 /* Setup DMA for SPIX RX */ DMA_DeInit( wifi_spi.rx_dma.stream ); dma_init_structure.DMA_Channel = wifi_spi.rx_dma.channel; dma_init_structure.DMA_PeripheralBaseAddr = (uint32_t) &(wifi_spi.port->DR); dma_init_structure.DMA_Memory0BaseAddr = 0; dma_init_structure.DMA_DIR = DMA_DIR_PeripheralToMemory; dma_init_structure.DMA_BufferSize = 0; dma_init_structure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; dma_init_structure.DMA_MemoryInc = DMA_MemoryInc_Enable; dma_init_structure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; dma_init_structure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; dma_init_structure.DMA_Mode = DMA_Mode_Normal; dma_init_structure.DMA_Priority = DMA_Priority_VeryHigh; dma_init_structure.DMA_FIFOMode = DMA_FIFOMode_Disable; dma_init_structure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full; dma_init_structure.DMA_MemoryBurst = DMA_MemoryBurst_Single; dma_init_structure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init( wifi_spi.rx_dma.stream, &dma_init_structure ); /* Setup DMA for SPIX TX */ DMA_DeInit( wifi_spi.tx_dma.stream ); dma_init_structure.DMA_Channel = wifi_spi.tx_dma.channel; dma_init_structure.DMA_PeripheralBaseAddr = (uint32_t) &(wifi_spi.port->DR); dma_init_structure.DMA_Memory0BaseAddr = 0; dma_init_structure.DMA_DIR = DMA_DIR_MemoryToPeripheral; dma_init_structure.DMA_BufferSize = 0; dma_init_structure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; dma_init_structure.DMA_MemoryInc = DMA_MemoryInc_Enable; dma_init_structure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; dma_init_structure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; dma_init_structure.DMA_Mode = DMA_Mode_Normal; dma_init_structure.DMA_Priority = DMA_Priority_VeryHigh; dma_init_structure.DMA_FIFOMode = DMA_FIFOMode_Disable; dma_init_structure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full; dma_init_structure.DMA_MemoryBurst = DMA_MemoryBurst_Single; dma_init_structure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init( wifi_spi.tx_dma.stream, &dma_init_structure ); /* 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 ARMCM3 interrupt priority logic is inverted, the highest value */ /* is the lowest priority */ NVIC_EnableIRQ( wifi_spi.rx_dma.irq_vector ); /* Enable DMA for TX */ SPI_I2S_DMACmd( wifi_spi.port, SPI_I2S_DMAReq_Tx | SPI_I2S_DMAReq_Rx, ENABLE ); /* Setup SPI */ spi_init.SPI_Direction = SPI_Direction_2Lines_FullDuplex; spi_init.SPI_Mode = SPI_Mode_Master; spi_init.SPI_DataSize = SPI_DataSize_8b; spi_init.SPI_CPOL = SPI_CPOL_High; spi_init.SPI_CPHA = SPI_CPHA_2Edge; spi_init.SPI_NSS = SPI_NSS_Soft; spi_init.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; spi_init.SPI_FirstBit = SPI_FirstBit_MSB; spi_init.SPI_CRCPolynomial = (uint16_t) 7; /* Init SPI and enable it */ SPI_Init( wifi_spi.port, &spi_init ); SPI_Cmd( wifi_spi.port, ENABLE ); platform_mcu_powersave_enable(); return kNoErr; }
OSStatus MicoGpioOutputHigh( mico_gpio_t gpio ) { if ( gpio >= MICO_GPIO_NONE ) return kUnsupportedErr; return (OSStatus) platform_gpio_output_high( &platform_gpio_pins[gpio] ); }
platform_result_t platform_spi_init( const platform_spi_t* spi, const platform_spi_config_t* config ) { SPI_InitTypeDef spi_init; platform_result_t result; uint8_t spi_number; wiced_assert( "bad argument", ( spi != NULL ) && ( config != NULL ) ); platform_mcu_powersave_disable(); spi_number = platform_spi_get_port_number( spi->port ); /* Init SPI GPIOs */ platform_gpio_set_alternate_function( spi->pin_clock->port, spi->pin_clock->pin_number, GPIO_OType_PP, GPIO_PuPd_NOPULL, spi->gpio_af ); platform_gpio_set_alternate_function( spi->pin_mosi->port, spi->pin_mosi->pin_number, GPIO_OType_PP, GPIO_PuPd_NOPULL, spi->gpio_af ); platform_gpio_set_alternate_function( spi->pin_miso->port, spi->pin_miso->pin_number, GPIO_OType_PP, GPIO_PuPd_NOPULL, spi->gpio_af ); /* Init the chip select GPIO */ platform_gpio_init( config->chip_select, OUTPUT_PUSH_PULL ); platform_gpio_output_high( config->chip_select ); /* Calculate prescaler */ result = calculate_prescaler( config->speed, &spi_init.SPI_BaudRatePrescaler ); if ( result != PLATFORM_SUCCESS ) { platform_mcu_powersave_enable(); return result; } /* Configure data-width */ if ( config->bits == 8 ) { spi_init.SPI_DataSize = SPI_DataSize_8b; } else if ( config->bits == 16 ) { if ( config->mode & SPI_USE_DMA ) { platform_mcu_powersave_enable(); /* 16 bit mode is not supported for a DMA */ return PLATFORM_UNSUPPORTED; } spi_init.SPI_DataSize = SPI_DataSize_16b; } else { platform_mcu_powersave_enable(); /* Requested mode is not supported */ return PLATFORM_UNSUPPORTED; } /* Configure MSB or LSB */ if ( config->mode & SPI_MSB_FIRST ) { spi_init.SPI_FirstBit = SPI_FirstBit_MSB; } else { spi_init.SPI_FirstBit = SPI_FirstBit_LSB; } /* Configure mode CPHA and CPOL */ if ( config->mode & SPI_CLOCK_IDLE_HIGH ) { spi_init.SPI_CPOL = SPI_CPOL_High; } else { spi_init.SPI_CPOL = SPI_CPOL_Low; } if ( config->mode & SPI_CLOCK_RISING_EDGE ) { spi_init.SPI_CPHA = ( config->mode & SPI_CLOCK_IDLE_HIGH ) ? SPI_CPHA_2Edge : SPI_CPHA_1Edge; } else { spi_init.SPI_CPHA = ( config->mode & SPI_CLOCK_IDLE_HIGH ) ? SPI_CPHA_1Edge : SPI_CPHA_2Edge; } /* Enable SPI peripheral clock */ spi_peripheral_clock_functions[ spi_number ]( spi_peripheral_clocks[ spi_number ], ENABLE ); spi_init.SPI_Direction = SPI_Direction_2Lines_FullDuplex; spi_init.SPI_Mode = SPI_Mode_Master; spi_init.SPI_NSS = SPI_NSS_Soft; spi_init.SPI_CRCPolynomial = 0x7; /* reset value */ SPI_CalculateCRC( spi->port, DISABLE ); /* Init and enable SPI */ SPI_Init( spi->port, &spi_init ); SPI_Cmd ( spi->port, ENABLE ); platform_mcu_powersave_enable(); return WICED_SUCCESS; }