/** Initialize the SPI peripheral * * Configures the pins used by SPI, sets a default format and frequency, and enables the peripheral * @param[out] obj The SPI object to initialize * @param[in] mosi The pin to use for MOSI * @param[in] miso The pin to use for MISO * @param[in] sclk The pin to use for SCLK */ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk) { MBED_ASSERT(obj); MBED_ASSERT(mosi !=NC && miso!=NC && sclk !=NC ); if (g_sys_init == 0) { sysclk_init(); system_board_init(); g_sys_init = 1; } Spi *sercombase = pinmap_find_sercom(mosi,miso,sclk); MBED_ASSERT(sercombase!=NC); pinmap_find_spi_info(sercombase, obj); MBED_ASSERT(obj->spi.flexcom!=NC); MBED_ASSERT(obj->spi.pdc!=NC); /* Configure SPI pins */ pin_function(mosi, pinmap_find_function(mosi, PinMap_SPI_MOSI)); ioport_disable_pin(mosi); pin_function(miso, pinmap_find_function(miso, PinMap_SPI_MISO)); ioport_disable_pin(miso); pin_function(sclk, pinmap_find_function(sclk, PinMap_SPI_SCLK)); ioport_disable_pin(sclk); #if (SAMG55) /* Enable the peripheral and set SPI mode. */ flexcom_enable(obj->spi.flexcom); flexcom_set_opmode(obj->spi.flexcom, FLEXCOM_SPI); #else /* Configure an SPI peripheral. */ spi_enable_clock(sercombase); #endif spi_disable(sercombase); spi_reset(sercombase); spi_set_lastxfer(sercombase); spi_set_master_mode(sercombase); spi_disable_mode_fault_detect(sercombase); spi_set_peripheral_chip_select_value(sercombase, SPI_CHIP_SEL); spi_set_clock_polarity(sercombase, SPI_CHIP_SEL, SPI_CLK_POLARITY); spi_set_clock_phase(sercombase, SPI_CHIP_SEL, SPI_CLK_PHASE); spi_set_bits_per_transfer(sercombase, SPI_CHIP_SEL, SPI_CSR_BITS_8_BIT); spi_set_baudrate_div(sercombase, SPI_CHIP_SEL,(sysclk_get_cpu_hz() / gSPI_clock)); spi_set_transfer_delay(sercombase, SPI_CHIP_SEL, SPI_DLYBS,SPI_DLYBCT); spi_enable(sercombase); pdc_disable_transfer(obj->spi.pdc, PERIPH_PTCR_RXTDIS | PERIPH_PTCR_TXTDIS); obj->spi.spi_base=sercombase; obj->spi.cs= SPI_CHIP_SEL; obj->spi.polarity=SPI_CLK_POLARITY; obj->spi.phase=SPI_CLK_PHASE; obj->spi.transferrate=SPI_CSR_BITS_8_BIT; obj->spi.is_slave=0; }
/** * \brief Initialize system * * This function will call the various initialization functions within the * system namespace. If a given optional system module is not available, the * associated call will effectively be a NOP (No Operation). * * Currently the following initialization functions are supported: * - System clock initialization (via the SYSTEM CLOCK sub-module) * - Board hardware initialization (via the Board module) */ void system_init(void) { /* Configure GCLK and clock sources according to conf_clocks.h */ system_clock_init(); /* Initialize board hardware */ system_board_init(); }
/** * \brief Initialize system * * This function will call the various initialization functions within the * system namespace. If a given optional system module is not available, the * associated call will effectively be a NOP (No Operation). * * Currently the following initialization functions are supported: * - System clock initialization (via the SYSTEM CLOCK sub-module) * - Board hardware initialization (via the Board module) * - Event system driver initialization (via the EVSYS module) * - External Interrupt driver initialization (via the EXTINT module) */ void system_init(void) { /* Configure GCLK and clock sources according to conf_clocks.h */ system_clock_init(); /* Initialize board hardware */ system_board_init(); /* Initialize EVSYS hardware */ _system_events_init(); /* Initialize External hardware */ _system_extint_init(); }
void port_init(port_t *obj, PortName port, int mask, PinDirection dir) { MBED_ASSERT(obj); if (g_sys_init == 0) { sysclk_init(); system_board_init(); g_sys_init = 1; } obj->port = port; obj->mask = mask; switch (dir) { case PIN_INPUT : ioport_set_port_dir(port, mask, IOPORT_DIR_INPUT); break; case PIN_OUTPUT: ioport_set_port_dir(port, mask, IOPORT_DIR_OUTPUT); break; case PIN_INPUT_OUTPUT: ioport_set_port_dir(port, mask, IOPORT_DIR_OUTPUT); break; } ioport_set_port_mode(port, mask, IOPORT_MODE_PULLUP); }
void serial_init(serial_t *obj, PinName tx, PinName rx) { /* Sanity check arguments */ MBED_ASSERT(obj); int clockid = NC, flexcom = NC; /*To determine the uart peripheral associated with pins*/ UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX); UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx); MBED_ASSERT(uart != (UARTName)NC); if (g_sys_init == 0) { sysclk_init(); system_board_init(); g_sys_init = 1; } pUSART_S(obj) = uart; pSERIAL_S(obj)->uart_serial_options.baudrate = (9600UL); pSERIAL_S(obj)->uart_serial_options.charlength = US_MR_CHRL_8_BIT; pSERIAL_S(obj)->uart_serial_options.paritytype = US_MR_PAR_NO; pSERIAL_S(obj)->uart_serial_options.stopbits = US_MR_NBSTOP_1_BIT; pSERIAL_S(obj)->actrec = false; pSERIAL_S(obj)->acttra = false; /* Configure UART pins */ if(tx != NC) { pin_function(tx, pinmap_find_function(tx, PinMap_UART_TX)); ioport_disable_pin(tx); } if(rx != NC) { pin_function(rx, pinmap_find_function(rx, PinMap_UART_RX)); ioport_disable_pin(rx); } clockid = get_usart_clock_id(uart); if (clockid != NC) { sysclk_enable_peripheral_clock(clockid); } flexcom = (int)get_flexcom_id(uart); #if (!SAM4L) #if (SAMG55) /* Configure flexcom for usart */ flexcom_enable((Flexcom* )flexcom); flexcom_set_opmode((Flexcom* )flexcom, FLEXCOM_USART); #else sysclk_enable_peripheral_clock(clockid); #endif /* Configure USART */ usart_init_rs232((Usart*)uart, (sam_usart_opt_t*)&(pSERIAL_S(obj)->uart_serial_options), sysclk_get_peripheral_hz()); #endif #if (SAM4L) sysclk_enable_peripheral_clock(clockid); /* Configure USART */ usart_init_rs232((Usart*)uart, (sam_usart_opt_t*)&(pSERIAL_S(obj)->uart_serial_options, sysclk_get_peripheral_bus_hz((Usart*)uart)); #endif /* Disable rx and tx in case 1 line only required to be configured for usart */ usart_disable_tx((Usart*)uart); usart_disable_rx((Usart*)uart); /* Enable the receiver and transmitter. */ if(tx != NC) { usart_enable_tx((Usart*)uart); } if(rx != NC) { usart_enable_rx((Usart*)uart); } if(uart == STDIO_UART) { stdio_uart_inited = 1; memcpy(&stdio_uart, obj, sizeof(serial_t)); } }