void serial_init(serial_t *obj, PinName tx, PinName rx) { int is_stdio_uart = 0; // determine the UART to use 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((int)uart != NC); obj->uart = (LPC_UART_TypeDef *)uart; // enable power switch (uart) { case UART_0: LPC_SC->PCONP |= 1 << 3; break; case UART_1: LPC_SC->PCONP |= 1 << 4; break; case UART_2: LPC_SC->PCONP |= 1 << 24; break; case UART_3: LPC_SC->PCONP |= 1 << 25; break; } // enable fifos and default rx trigger level obj->uart->FCR = 1 << 0 // FIFO Enable - 0 = Disables, 1 = Enabled | 0 << 1 // Rx Fifo Reset | 0 << 2 // Tx Fifo Reset | 0 << 6; // Rx irq trigger level - 0 = 1 char, 1 = 4 chars, 2 = 8 chars, 3 = 14 chars // disable irqs obj->uart->IER = 0 << 0 // Rx Data available irq enable | 0 << 1 // Tx Fifo empty irq enable | 0 << 2; // Rx Line Status irq enable // set default baud rate and format serial_baud (obj, 9600); serial_format(obj, 8, ParityNone, 1); // pinout the chosen uart pinmap_pinout(tx, PinMap_UART_TX); pinmap_pinout(rx, PinMap_UART_RX); // set rx/tx pins in PullUp mode pin_mode(tx, PullUp); pin_mode(rx, PullUp); switch (uart) { case UART_0: obj->index = 0; break; case UART_1: obj->index = 1; break; case UART_2: obj->index = 2; break; case UART_3: obj->index = 3; break; } is_stdio_uart = (uart == STDIO_UART) ? (1) : (0); if (is_stdio_uart) { stdio_uart_inited = 1; memcpy(&stdio_uart, obj, sizeof(serial_t)); } }
void serial_init(serial_t *obj, PinName tx, PinName rx) { // Determine the UART to use (UART_1, UART_2, ...) UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX); UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx); MBED_ASSERT(obj->uart != (UARTName)NC); // Enable USART clock + switch to SystemClock if (obj->uart == UART_1) { __USART1_CLK_ENABLE(); __HAL_RCC_USART1_CONFIG(RCC_USART1CLKSOURCE_SYSCLK); obj->index = 0; } if (obj->uart == UART_2) { __USART2_CLK_ENABLE(); __HAL_RCC_USART2_CONFIG(RCC_USART2CLKSOURCE_SYSCLK); obj->index = 1; } if (obj->uart == UART_3) { __USART3_CLK_ENABLE(); __HAL_RCC_USART3_CONFIG(RCC_USART3CLKSOURCE_SYSCLK); obj->index = 2; } // Configure the UART pins pinmap_pinout(tx, PinMap_UART_TX); pinmap_pinout(rx, PinMap_UART_RX); if (tx != NC) { pin_mode(tx, PullUp); } if (rx != NC) { pin_mode(rx, PullUp); } // Configure UART obj->baudrate = 9600; obj->databits = UART_WORDLENGTH_8B; obj->stopbits = UART_STOPBITS_1; obj->parity = UART_PARITY_NONE; obj->pin_tx = tx; obj->pin_rx = rx; init_uart(obj); // For stdio management if (obj->uart == STDIO_UART) { stdio_uart_inited = 1; memcpy(&stdio_uart, obj, sizeof(serial_t)); } }
void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) { // Determine the SPI to use SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI); SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO); SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK); SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL); SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso); SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl); if (obj->spi == (SPIName)NC) { error("SPI error: pinout mapping failed."); } // Enable SPI clock if (obj->spi == SPI_1) { __SPI1_CLK_ENABLE(); } if (obj->spi == SPI_2) { __SPI2_CLK_ENABLE(); } if (obj->spi == SPI_3) { __SPI3_CLK_ENABLE(); } // Configure the SPI pins pinmap_pinout(mosi, PinMap_SPI_MOSI); pinmap_pinout(miso, PinMap_SPI_MISO); pinmap_pinout(sclk, PinMap_SPI_SCLK); // Save new values obj->bits = SPI_DATASIZE_8BIT; obj->cpol = SPI_POLARITY_LOW; obj->cpha = SPI_PHASE_1EDGE; obj->br_presc = SPI_BAUDRATEPRESCALER_256; obj->pin_miso = miso; obj->pin_mosi = mosi; obj->pin_sclk = sclk; obj->pin_ssel = ssel; if (ssel == NC) { // SW NSS Master mode obj->mode = SPI_MODE_MASTER; obj->nss = SPI_NSS_SOFT; } else { // Slave pinmap_pinout(ssel, PinMap_SPI_SSEL); obj->mode = SPI_MODE_SLAVE; obj->nss = SPI_NSS_HARD_INPUT; } init_spi(obj); }
void serial_init(serial_t *obj, PinName tx, PinName rx) { // Determine the UART to use (UART_1, UART_2, ...) UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX); UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx); if (obj->uart == (UARTName)NC) { error("Serial error: pinout mapping failed."); } // Enable USART clock if (obj->uart == UART_1) { __USART1_CLK_ENABLE(); obj->index = 0; } if (obj->uart == UART_2) { __USART2_CLK_ENABLE(); obj->index = 1; } if (obj->uart == UART_3) { __USART3_CLK_ENABLE(); obj->index = 2; } if (obj->uart == UART_4) { __USART4_CLK_ENABLE(); obj->index = 3; } // Configure the UART pins pinmap_pinout(tx, PinMap_UART_TX); pinmap_pinout(rx, PinMap_UART_RX); pin_mode(tx, PullUp); pin_mode(rx, PullUp); // Configure UART obj->baudrate = 9600; obj->databits = UART_WORDLENGTH_8B; obj->stopbits = UART_STOPBITS_1; obj->parity = UART_PARITY_NONE; obj->pin_tx = tx; obj->pin_rx = rx; init_uart(obj); // For stdio management if (obj->uart == STDIO_UART) { stdio_uart_inited = 1; memcpy(&stdio_uart, obj, sizeof(serial_t)); } }
void serial_init(serial_t *obj, PinName tx, PinName rx) { int is_stdio_uart = 0; // determine the UART to use 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((int)uart != NC); obj->uart = (LPC_USART_Type *)uart; LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12); // [TODO] Consider more elegant approach // disconnect USBTX/RX mapping mux, for case when switching ports #ifdef USBTX pin_function(USBTX, 0); pin_function(USBRX, 0); #endif // enable fifos and default rx trigger level obj->uart->FCR = 1 << 0 // FIFO Enable - 0 = Disables, 1 = Enabled | 0 << 1 // Rx Fifo Reset | 0 << 2 // Tx Fifo Reset | 0 << 6; // Rx irq trigger level - 0 = 1 char, 1 = 4 chars, 2 = 8 chars, 3 = 14 chars // disable irqs obj->uart->IER = 0 << 0 // Rx Data available irq enable | 0 << 1 // Tx Fifo empty irq enable | 0 << 2; // Rx Line Status irq enable // set default baud rate and format serial_baud (obj, 9600); serial_format(obj, 8, ParityNone, 1); // pinout the chosen uart pinmap_pinout(tx, PinMap_UART_TX); pinmap_pinout(rx, PinMap_UART_RX); // set rx/tx pins in PullUp mode pin_mode(tx, PullUp); pin_mode(rx, PullUp); switch (uart) { case UART_0: obj->index = 0; break; } is_stdio_uart = (uart == STDIO_UART) ? (1) : (0); if (is_stdio_uart) { stdio_uart_inited = 1; memcpy(&stdio_uart, obj, sizeof(serial_t)); } }
void serial_init(serial_t *obj, PinName tx, PinName rx) { int is_stdio_uart = 0; // determine the UART to use 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); if ((int)uart == NC) { error("Serial pinout mapping failed"); } obj->uart = (LPC_USART_T *)uart; // enable fifos and default rx trigger level obj->uart->FCR = 1 << 0 // FIFO Enable - 0 = Disables, 1 = Enabled | 0 << 1 // Rx Fifo Reset | 0 << 2 // Tx Fifo Reset | 0 << 6; // Rx irq trigger level - 0 = 1 char, 1 = 4 chars, 2 = 8 chars, 3 = 14 chars // disable irqs obj->uart->IER = 0 << 0 // Rx Data available irq enable | 0 << 1 // Tx Fifo empty irq enable | 0 << 2; // Rx Line Status irq enable // set default baud rate and format is_stdio_uart = (uart == STDIO_UART) ? (1) : (0); serial_baud (obj, is_stdio_uart ? 115200 : 9600); serial_format(obj, 8, ParityNone, 1); // pinout the chosen uart pinmap_pinout(tx, PinMap_UART_TX); pinmap_pinout(rx, PinMap_UART_RX); // set rx/tx pins in PullUp mode pin_mode(tx, PullUp); pin_mode(rx, PullUp); switch (uart) { case UART_0: obj->index = 0; break; case UART_1: obj->index = 1; break; case UART_2: obj->index = 2; break; case UART_3: obj->index = 3; break; } uart_data[obj->index].sw_rts.pin = NC; uart_data[obj->index].sw_cts.pin = NC; serial_set_flow_control(obj, FlowControlNone, NC, NC); if (is_stdio_uart) { stdio_uart_inited = 1; memcpy(&stdio_uart, obj, sizeof(serial_t)); } }
void serial_init(serial_t *obj, PinName tx, PinName rx) { uint32_t uart_tx, uart_rx; uint32_t uart_sel; uint8_t uart_idx; PHAL_RUART_OP pHalRuartOp; PHAL_RUART_ADAPTER pHalRuartAdapter; // Determine the UART to use (UART0, UART1, or UART3) uart_tx = pinmap_peripheral(tx, PinMap_UART_TX); uart_rx = pinmap_peripheral(rx, PinMap_UART_RX); uart_sel = pinmap_merge(uart_tx, uart_rx); uart_idx = RTL_GET_PERI_IDX(uart_sel); if (unlikely(uart_idx == (uint8_t)NC)) { DBG_UART_ERR("%s: Cannot find matched UART\n", __FUNCTION__); return; } pHalRuartOp = &(obj->hal_uart_op); pHalRuartAdapter = &(obj->hal_uart_adp); if ((NULL == pHalRuartOp) || (NULL == pHalRuartAdapter)) { DBG_UART_ERR("%s: Allocate Adapter Failed\n", __FUNCTION__); return; } HalRuartOpInit((VOID*)pHalRuartOp); pHalRuartOp->HalRuartAdapterLoadDef(pHalRuartAdapter, uart_idx); pHalRuartAdapter->PinmuxSelect = RTL_GET_PERI_SEL(uart_sel); pHalRuartAdapter->BaudRate = 9600; // Configure the UART pins // TODO: // pinmap_pinout(tx, PinMap_UART_TX); // pinmap_pinout(rx, PinMap_UART_RX); // pin_mode(tx, PullUp); // pin_mode(rx, PullUp); pHalRuartOp->HalRuartInit(pHalRuartAdapter); pHalRuartOp->HalRuartRegIrq(pHalRuartAdapter); pHalRuartOp->HalRuartIntEnable(pHalRuartAdapter); #ifdef CONFIG_MBED_ENABLED // For stdio management if (uart_idx == STDIO_UART) { stdio_uart_inited = 1; memcpy(&stdio_uart, obj, sizeof(serial_t)); } #endif }
void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) { // determine the SPI to use SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI); SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO); SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK); SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL); SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso); SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); obj->spi = (SPI_TypeDef*)pinmap_merge(spi_data, spi_cntl); MBED_ASSERT((int)obj->spi != NC) // enable power and clocking switch ((int)obj->spi) { case SPI_1: RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIOBEN; RCC->APB2ENR |= RCC_APB2ENR_SPI1EN; break; case SPI_2: RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN | RCC_AHB1ENR_GPIOCEN; RCC->APB1ENR |= RCC_APB1ENR_SPI2EN; break; case SPI_3: RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN | RCC_AHB1ENR_GPIOCEN; RCC->APB1ENR |= RCC_APB1ENR_SPI3EN; break; } // set default format and frequency if (ssel == NC) { spi_format(obj, 8, 0, 0); // 8 bits, mode 0, master } else { spi_format(obj, 8, 0, 1); // 8 bits, mode 0, slave } spi_frequency(obj, 1000000); // enable the ssp channel ssp_enable(obj); // pin out the spi pins pinmap_pinout(mosi, PinMap_SPI_MOSI); pinmap_pinout(miso, PinMap_SPI_MISO); pinmap_pinout(sclk, PinMap_SPI_SCLK); if (ssel != NC) { pinmap_pinout(ssel, PinMap_SPI_SSEL); } else { // Use software slave management obj->spi->CR1 |= SPI_CR1_SSM | SPI_CR1_SSI; } }
void can_init(can_t *obj, PinName rd, PinName td) { uint32_t filter_number; CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD); CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD); obj->can = (CANName)pinmap_merge(can_rd, can_td); MBED_ASSERT((int)obj->can != NC); if(obj->can == CAN_1) { __HAL_RCC_CAN1_CLK_ENABLE(); obj->index = 0; } else { __HAL_RCC_CAN2_CLK_ENABLE(); obj->index = 1; } // Configure the CAN pins pinmap_pinout(rd, PinMap_CAN_RD); pinmap_pinout(td, PinMap_CAN_TD); if (rd != NC) { pin_mode(rd, PullUp); } if (td != NC) { pin_mode(td, PullUp); } CanHandle.Instance = (CAN_TypeDef *)(obj->can); CanHandle.Init.TTCM = DISABLE; CanHandle.Init.ABOM = DISABLE; CanHandle.Init.AWUM = DISABLE; CanHandle.Init.NART = DISABLE; CanHandle.Init.RFLM = DISABLE; CanHandle.Init.TXFP = DISABLE; CanHandle.Init.Mode = CAN_MODE_NORMAL; CanHandle.Init.SJW = CAN_SJW_1TQ; CanHandle.Init.BS1 = CAN_BS1_6TQ; CanHandle.Init.BS2 = CAN_BS2_8TQ; CanHandle.Init.Prescaler = 2; if (HAL_CAN_Init(&CanHandle) != HAL_OK) { error("Cannot initialize CAN"); } filter_number = (obj->can == CAN_1) ? 0 : 14; // Set initial CAN frequency to 100kb/s can_frequency(obj, 100000); can_filter(obj, 0, 0, CANStandard, filter_number); }
void i2c_preinit(i2c_t *obj, PinName sda, PinName scl) { I2CName i2c_sda = (I2CName) pinmap_peripheral(sda, PinMap_I2C_SDA); I2CName i2c_scl = (I2CName) pinmap_peripheral(scl, PinMap_I2C_SCL); obj->i2c.i2c = (I2C_TypeDef*) pinmap_merge(i2c_sda, i2c_scl); MBED_ASSERT(((int) obj->i2c.i2c) != NC); int loc_sda = pin_location(sda, PinMap_I2C_SDA); int loc_scl = pin_location(scl, PinMap_I2C_SCL); obj->i2c.loc = pinmap_merge(loc_sda, loc_scl); MBED_ASSERT(obj->i2c.loc != NC); obj->i2c.sda = sda; obj->i2c.scl = scl; }
void serial_init(serial_t *obj, PinName tx, PinName rx) { // Determine the UART to use UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX); UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); // Get the peripheral name from the pin and assign it to the object obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx); MBED_ASSERT(obj->uart != (UARTName)NC); // Enable USART clock if (obj->uart == UART_1) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); } if (obj->uart == UART_2) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); } if (obj->uart == UART_3) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); } // Configure the UART pins pinmap_pinout(tx, PinMap_UART_TX); pinmap_pinout(rx, PinMap_UART_RX); if (tx != NC) { pin_mode(tx, PullUp); } if (rx != NC) { pin_mode(rx, PullUp); } // Configure UART obj->baudrate = 9600; obj->databits = USART_WordLength_8b; obj->stopbits = USART_StopBits_1; obj->parity = USART_Parity_No; init_usart(obj); // The index is used by irq if (obj->uart == UART_1) obj->index = 0; if (obj->uart == UART_2) obj->index = 1; if (obj->uart == UART_3) obj->index = 2; // For stdio management if (obj->uart == STDIO_UART) { stdio_uart_inited = 1; memcpy(&stdio_uart, obj, sizeof(serial_t)); } }
void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) { // Determine the SPI to use SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI); SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO); SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK); SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL); SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso); SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl); if (obj->spi == (SPIName)NC) { error("SPI pinout mapping failed"); } // Enable SPI clock if (obj->spi == SPI_1) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); } if (obj->spi == SPI_2) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); } if (obj->spi == SPI_3) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE); } // Configure the SPI pins pinmap_pinout(mosi, PinMap_SPI_MOSI); pinmap_pinout(miso, PinMap_SPI_MISO); pinmap_pinout(sclk, PinMap_SPI_SCLK); // Save new values obj->bits = SPI_DataSize_8b; obj->cpol = SPI_CPOL_Low; obj->cpha = SPI_CPHA_1Edge; obj->br_presc = SPI_BaudRatePrescaler_256; if (ssel == NC) { // Master obj->mode = SPI_Mode_Master; obj->nss = SPI_NSS_Soft; } else { // Slave pinmap_pinout(ssel, PinMap_SPI_SSEL); obj->mode = SPI_Mode_Slave; obj->nss = SPI_NSS_Soft; } init_spi(obj); }
void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) { // Determine the SPI to use SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI); SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO); SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK); SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL); SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso); SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl); MBED_ASSERT(obj->spi != (SPIName)NC); // Enable SPI clock if (obj->spi == SPI_1) { __SPI1_CLK_ENABLE(); } #if defined(SPI2_BASE) if (obj->spi == SPI_2) { __SPI2_CLK_ENABLE(); } #endif // Configure the SPI pins pinmap_pinout(mosi, PinMap_SPI_MOSI); pinmap_pinout(miso, PinMap_SPI_MISO); pinmap_pinout(sclk, PinMap_SPI_SCLK); // Save new values obj->bits = SPI_DATASIZE_8BIT; obj->cpol = SPI_POLARITY_LOW; obj->cpha = SPI_PHASE_1EDGE; obj->br_presc = SPI_BAUDRATEPRESCALER_256; obj->pin_miso = miso; obj->pin_mosi = mosi; obj->pin_sclk = sclk; obj->pin_ssel = ssel; if (ssel != NC) { pinmap_pinout(ssel, PinMap_SPI_SSEL); } else { obj->nss = SPI_NSS_SOFT; } init_spi(obj); }
void i2c_init(i2c_t *obj, PinName sda, PinName scl) { // Determine the I2C to use I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA); I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); MBED_ASSERT(obj->i2c != (I2CName)NC); // Enable I2C1 clock and pinout if not done if ((obj->i2c == I2C_1) && !i2c1_inited) { i2c1_inited = 1; __I2C1_CLK_ENABLE(); // Configure I2C pins pinmap_pinout(sda, PinMap_I2C_SDA); pinmap_pinout(scl, PinMap_I2C_SCL); pin_mode(sda, OpenDrain); pin_mode(scl, OpenDrain); } // Enable I2C2 clock and pinout if not done if ((obj->i2c == I2C_2) && !i2c2_inited) { i2c2_inited = 1; __I2C2_CLK_ENABLE(); // Configure I2C pins pinmap_pinout(sda, PinMap_I2C_SDA); pinmap_pinout(scl, PinMap_I2C_SCL); pin_mode(sda, OpenDrain); pin_mode(scl, OpenDrain); } // Enable I2C3 clock and pinout if not done if ((obj->i2c == I2C_3) && !i2c3_inited) { i2c3_inited = 1; __I2C3_CLK_ENABLE(); // Configure I2C pins pinmap_pinout(sda, PinMap_I2C_SDA); pinmap_pinout(scl, PinMap_I2C_SCL); pin_mode(sda, OpenDrain); pin_mode(scl, OpenDrain); } // Reset to clear pending flags if any i2c_reset(obj); // I2C configuration i2c_frequency(obj, 100000); // 100 kHz per default // I2C master by default obj->slave = 0; }
void serial_init(serial_t *obj, PinName tx, PinName rx) { // determine the UART to use -- for mcu's with multiple uart connections 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); if ((int)uart == NC) { error("Serial pinout mapping failed"); } obj->uart = (NRF_UART_Type *)uart; //pin configurations -- //outputs NRF_GPIO->DIR |= (1<<tx);//TX_PIN_NUMBER); NRF_GPIO->DIR |= (1<<RTS_PIN_NUMBER); NRF_GPIO->DIR &= ~(1<<rx);//RX_PIN_NUMBER); NRF_GPIO->DIR &= ~(1<<CTS_PIN_NUMBER); obj->uart->PSELRTS = RTS_PIN_NUMBER; obj->uart->PSELTXD = tx;//TX_PIN_NUMBER; //inputs obj->uart->PSELCTS = CTS_PIN_NUMBER; obj->uart->PSELRXD = rx;//RX_PIN_NUMBER; // set default baud rate and format serial_baud (obj, 9600); serial_format(obj, 8, ParityNone, 1); obj->uart->ENABLE = (UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos);; obj->uart->TASKS_STARTTX = 1; obj->uart->TASKS_STARTRX = 1; obj->uart->EVENTS_RXDRDY =0; obj->index = 0; // set rx/tx pins in PullUp mode pin_mode(tx, PullUp); pin_mode(rx, PullUp); if (uart == STDIO_UART) { stdio_uart_inited = 1; memcpy(&stdio_uart, obj, sizeof(serial_t)); } }
/** Initialize the I2C peripheral. It sets the default parameters for I2C * peripheral, and configures its specifieds pins. * * @param obj The I2C object * @param sda The sda pin * @param scl The scl pin */ void i2c_init(i2c_t *obj, PinName sda, PinName scl) { struct i2c_s *obj_s = I2C_S(obj); /* find the I2C by pins */ uint32_t i2c_sda = pinmap_peripheral(sda, PinMap_I2C_SDA); uint32_t i2c_scl = pinmap_peripheral(scl, PinMap_I2C_SCL); obj_s->sda = sda; obj_s->scl = scl; obj_s->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); MBED_ASSERT(obj_s->i2c != (I2CName)NC); switch (obj_s->i2c) { case I2C_0: /* enable I2C0 clock and configure the pins of I2C0 */ obj_s->index = 0; rcu_periph_clock_enable(RCU_I2C0); break; case I2C_1: /* enable I2C1 clock and configure the pins of I2C1 */ obj_s->index = 1; rcu_periph_clock_enable(RCU_I2C1); break; default: break; } /* configure the pins of I2C */ pinmap_pinout(sda, PinMap_I2C_SDA); pinmap_pinout(scl, PinMap_I2C_SCL); /* 100 KHz as the default I2C frequence */ i2c_frequency(obj, 100000); obj_s->state = (operation_state_enum)I2C_STATE_NONE; obj_s->previous_state_mode = I2C_STATE_NONE; obj_s->global_trans_option = I2C_FIRST_AND_LAST_FRAME; #if DEVICE_I2CSLAVE /* I2C master by default */ obj_s->slave = 0; #endif }
void analogout_init(dac_t *obj, PinName pin) { DAC_ChannelConfTypeDef sConfig; DacHandle.Instance = DAC; // Get the peripheral name (DAC_1, ...) from the pin and assign it to the object obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); if (obj->dac == (DACName)NC) { error("DAC pin mapping failed"); } // Configure GPIO pinmap_pinout(pin, PinMap_DAC); // Save the channel for future use obj->pin = pin; // Enable DAC clock __DAC_CLK_ENABLE(); // Configure DAC sConfig.DAC_Trigger = DAC_TRIGGER_NONE; sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE; HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_1); analogout_write_u16(obj, 0); }
void pwmout_init(pwmout_t* obj, PinName pin) { // Get the peripheral name from the pin and assign it to the object obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); MBED_ASSERT(obj->pwm != (PWMName)NC); // Get the pin function and assign the used channel to the object uint32_t function = pinmap_function(pin, PinMap_PWM); MBED_ASSERT(function != (uint32_t)NC); obj->channel = STM_PIN_CHANNEL(function); obj->inverted = STM_PIN_INVERTED(function); // Enable TIM clock if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE(); #if defined(TIM3_BASE) if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE(); #endif if (obj->pwm == PWM_21) __TIM21_CLK_ENABLE(); #if defined(TIM22_BASE) if (obj->pwm == PWM_22) __TIM22_CLK_ENABLE(); #endif // Configure GPIO pinmap_pinout(pin, PinMap_PWM); obj->pin = pin; obj->period = 0; obj->pulse = 0; pwmout_period_us(obj, 20000); // 20 ms per default }
void pwmout_init(pwmout_t* obj, PinName pin) { // Get the peripheral name from the pin and assign it to the object obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); MBED_ASSERT(obj->pwm != (PWMName)NC); // Get the functions (timer channel, (non)inverted) from the pin and assign it to the object uint32_t function = pinmap_function(pin, PinMap_PWM); MBED_ASSERT(function != (uint32_t)NC); obj->channel = STM_PIN_CHANNEL(function); obj->inverted = STM_PIN_INVERTED(function); // Enable TIM clock if (obj->pwm == PWM_1) __TIM1_CLK_ENABLE(); if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE(); if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE(); // Configure GPIO pinmap_pinout(pin, PinMap_PWM); obj->pin = pin; obj->period = 0; obj->pulse = 0; obj->prescaler = 1; pwmout_period_us(obj, 20000); // 20 ms per default }
void analogout_init(dac_t *obj, PinName pin) { DAC_ChannelConfTypeDef sConfig; // Get the peripheral name from the pin and assign it to the object obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); MBED_ASSERT(obj->dac != (DACName)NC); // Configure GPIO pinmap_pinout(pin, PinMap_DAC); // Save the pin for future use obj->pin = pin; // Enable DAC clock __DAC1_CLK_ENABLE(); // Configure DAC DacHandle.Instance = (DAC_TypeDef *)(obj->dac); sConfig.DAC_Trigger = DAC_TRIGGER_NONE; sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE; HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_1); analogout_write_u16(obj, 0); }
void pwmout_init(pwmout_t* obj, PinName pin) { // determine the channel PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); MBED_ASSERT(pwm != (PWMName)NC); // power on CPGSTBCR3 &= ~(1<<0); obj->pwm = pwm; if (((uint32_t)PORT[obj->pwm] & 0x00000010) != 0) { obj->ch = 2; PWMPWPR_2_BYTE_L = 0x00; } else { obj->ch = 1; PWMPWPR_1_BYTE_L = 0x00; } // Wire pinout pinmap_pinout(pin, PinMap_PWM); // default to 491us: standard for servos, and fine for e.g. brightness control pwmout_write(obj, 0); if ((obj->ch == 2) && (init_period_ch2 == 0)) { pwmout_period_us(obj, 491); init_period_ch2 = 1; } if ((obj->ch == 1) && (init_period_ch1 == 0)) { pwmout_period_us(obj, 491); init_period_ch1 = 1; } }
/** Initialize the pwm out peripheral and configure the pin * * @param obj The pwmout object to initialize * @param pin The pwmout pin to initialize */ void pwmout_init(pwmout_t *obj, PinName pin) { /* Get the base address of the PWM register using the pinmap functions ; pwmout_s struct contains base address only */ PWMName pwm; pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); MBED_ASSERT(pwm != (PWMName)NC); pinmap_pinout(pin, PinMap_PWM); obj->pwmReg = (PwmReg_pt)pwm; MBED_ASSERT(obj->pwmReg != 0x00000000); CLOCK_ENABLE(CLOCK_PWM); /* Configuration parameters of duty cycle 0x4000B000, and prescaler 0x4000B00C, shall be set to default values */ /* Duty cycle shall be 50% and prescaler shall be disabled by default */ obj->pwmReg->DUTYCYCLE = 0x80; /* Write the PWM output enable register 0x4000B004, to 1 */ obj->pwmReg->PWM_ENABLE = 0x1; obj->pwmReg->PRESCALE_DISABLE = 0x1; }
void pwmout_init(pwmout_t* obj, PinName pin) { // determine the channel PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); if (pwm == (PWMName)NC) error("PwmOut pin mapping failed"); obj->pwm = pwm; obj->MR = PWM_MATCH[pwm]; // ensure the power is on LPC_SC->PCONP |= 1 << 6; // ensure clock to /4 LPC_SC->PCLKSEL0 &= ~(0x3 << 12); // pclk = /4 LPC_PWM1->PR = 0; // no pre-scale // ensure single PWM mode LPC_PWM1->MCR = 1 << 1; // reset TC on match 0 // enable the specific PWM output LPC_PWM1->PCR |= 1 << (8 + pwm); pwm_clock_mhz = SystemCoreClock / 4000000; // default to 20ms: standard for servos, and fine for e.g. brightness control pwmout_period_ms(obj, 20); pwmout_write (obj, 0); // Wire pinout pinmap_pinout(pin, PinMap_PWM); }
void analogout_init(dac_t *obj, PinName pin) { /* init in-memory structure */ obj->dac = (DAC_TypeDef *) pinmap_peripheral(pin, PinMap_DAC); MBED_ASSERT((int) obj->dac != NC); obj->channel = pin_location(pin, PinMap_DAC); MBED_ASSERT((int) obj->channel != NC); pin_mode(pin, Disabled); if (!dac_initialized) { /* Initialize the DAC. Will disable both DAC channels, so should only be done once */ /* Use default settings */ CMU_ClockEnable(cmuClock_DAC0, true); DAC_Init_TypeDef init = DAC_INIT_DEFAULT; /* Calculate the DAC clock prescaler value that will result in a DAC clock * close to 500kHz. Second parameter is zero. This uses the current HFPERCLK * frequency instead of setting a new one. */ init.prescale = DAC_PrescaleCalc(500000, REFERENCE_FREQUENCY); /* Set reference voltage to VDD */ init.reference = dacRefVDD; DAC_Init(obj->dac, &init); dac_initialized = 1; } /* Use default channel settings */ DAC_InitChannel_TypeDef initChannel = DAC_INITCHANNEL_DEFAULT; initChannel.enable = true; DAC_InitChannel(obj->dac, &initChannel, obj->channel); }
//****************************************************************************** void analogin_init(analogin_t *obj, PinName pin) { // Make sure pin is an analog pin we can use for ADC MBED_ASSERT((ADCName)pinmap_peripheral(pin, PinMap_ADC) != (ADCName)NC); // Set the object pointer obj->adc = MXC_ADC; obj->adccfg = MXC_ADCCFG; obj->adc_fifo = MXC_ADC_FIFO; obj->adc_pin = pin; // Set the ADC clock to the system clock frequency MXC_SET_FIELD(&MXC_CLKMAN->clk_ctrl, MXC_F_CLKMAN_CLK_CTRL_ADC_SOURCE_SELECT, (MXC_F_CLKMAN_CLK_CTRL_ADC_GATE_N | (MXC_E_CLKMAN_ADC_SOURCE_SELECT_SYSTEM << MXC_F_CLKMAN_CLK_CTRL_ADC_SOURCE_SELECT_POS))); // Enable AFE power MXC_PWRMAN->pwr_rst_ctrl |= MXC_F_PWRMAN_PWR_RST_CTRL_AFE_POWERED; // Setup and hold window MXC_SET_FIELD(&obj->adc->tg_ctrl0, MXC_F_ADC_TG_CTRL0_PGA_TRK_CNT, PGA_TRK_CNT); // Setup sampling count and timing MXC_SET_FIELD(&obj->adc->tg_ctrl1, (MXC_F_ADC_TG_CTRL1_PGA_ACQ_CNT | MXC_F_ADC_TG_CTRL1_ADC_ACQ_CNT | MXC_F_ADC_TG_CTRL1_ADC_SLP_CNT), ((ADC_PGA_CNT << MXC_F_ADC_TG_CTRL1_PGA_ACQ_CNT_POS) | (ADC_ACQ_CNT << MXC_F_ADC_TG_CTRL1_ADC_ACQ_CNT_POS) | (ADC_SLP_CNT << MXC_F_ADC_TG_CTRL1_ADC_SLP_CNT_POS) | (MXC_F_ADC_TG_CTRL1_ADC_BRST_CNT))); }
void pwmout_init(pwmout_t* obj, PinName pin) { // Get the peripheral name from the pin and assign it to the object obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); if (obj->pwm == (PWMName)NC) { error("PWM error: pinout mapping failed."); } // Enable TIM clock #if defined(TIM1_BASE) if (obj->pwm == PWM_1) __TIM1_CLK_ENABLE(); #endif #if defined(TIM2_BASE) if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE(); #endif if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE(); if (obj->pwm == PWM_14) __TIM14_CLK_ENABLE(); if (obj->pwm == PWM_15) __TIM15_CLK_ENABLE(); if (obj->pwm == PWM_16) __TIM16_CLK_ENABLE(); if (obj->pwm == PWM_17) __TIM17_CLK_ENABLE(); // Configure GPIO pinmap_pinout(pin, PinMap_PWM); obj->pin = pin; obj->period = 0; obj->pulse = 0; pwmout_period_us(obj, 20000); // 20 ms per default }
void analogout_init(dac_t *obj, PinName pin) { DAC_TypeDef *dac; DAC_InitTypeDef DAC_InitStructure; // Get the peripheral name (DAC_1, ...) from the pin and assign it to the object obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); if (obj->dac == (DACName)NC) { error("DAC pin mapping failed"); } dac = (DAC_TypeDef *)(obj->dac); // Configure GPIO pinmap_pinout(pin, PinMap_DAC); // Save the channel for future use obj->pin = pin; // Enable DAC clock RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); // Configure and enable DAC channel DAC_StructInit(&DAC_InitStructure); DAC_Init(dac, DAC_Channel_1, &DAC_InitStructure); DAC_Cmd(dac, DAC_Channel_1, ENABLE); analogout_write_u16(obj, 0); }
void pwmout_init(pwmout_t* obj, PinName pin) { // Get the peripheral name from the pin and assign it to the object obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); if (obj->pwm == (PWMName)NC) { error("PWM error: pinout mapping failed."); } // Enable TIM clock if (obj->pwm == PWM_1) __TIM1_CLK_ENABLE(); if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE(); if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE(); if (obj->pwm == PWM_4) __TIM4_CLK_ENABLE(); if (obj->pwm == PWM_9) __TIM9_CLK_ENABLE(); if (obj->pwm == PWM_10) __TIM10_CLK_ENABLE(); if (obj->pwm == PWM_11) __TIM11_CLK_ENABLE(); // Configure GPIO pinmap_pinout(pin, PinMap_PWM); obj->pin = pin; obj->period = 0; obj->pulse = 0; pwmout_period_us(obj, 20000); // 20 ms per default }
void analogin_init(analogin_t *obj, PinName pin) { static uint8_t adc_initialized = 0; /* Init structure */ obj->adc = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC); MBED_ASSERT((int) obj->adc != NC); obj->channel = pin_location(pin, PinMap_ADC); MBED_ASSERT((int) obj->channel != NC); /* Only initialize the ADC once */ if (!adc_initialized) { /* Turn on the clock */ CMU_ClockEnable(cmuClock_ADC0, true); /* Init with default settings */ ADC_Init_TypeDef init = ADC_INIT_DEFAULT; init.prescale = 4; ADC_Init(obj->adc, &init); /* Init for single conversion use */ ADC_InitSingle_TypeDef singleInit = ADC_INITSINGLE_DEFAULT; /* Measure input channel with Vdd reference. */ singleInit.reference = adcRefVDD; singleInit.resolution = adcRes12Bit; singleInit.acqTime = adcAcqTime32; ADC_InitSingle(obj->adc, &singleInit); adc_initialized = 1; } }
void analogout_init(dac_t *obj, PinName pin) { DAC_ChannelConfTypeDef sConfig; DacHandle.Instance = DAC; // Get the peripheral name (DAC_1, ...) from the pin and assign it to the object obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); MBED_ASSERT(obj->dac != (DACName)NC); // Configure GPIO pinmap_pinout(pin, PinMap_DAC); // Save the channel for future use obj->pin = pin; // Enable DAC clock __DAC_CLK_ENABLE(); // Configure DAC sConfig.DAC_Trigger = DAC_TRIGGER_NONE; sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE; if (pin == PA_4) { HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_1); pa4_used = 1; } else { // PA_5 HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_2); pa5_used = 1; } analogout_write_u16(obj, 0); }