void spi_free(spi_t *obj) { // Reset SPI and disable clock if (obj->spi == SPI_1) { RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, ENABLE); RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, DISABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, DISABLE); } if (obj->spi == SPI_2) { RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, ENABLE); RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, DISABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, DISABLE); } if (obj->spi == SPI_3) { RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, ENABLE); RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, DISABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, DISABLE); } // Configure GPIOs pin_function(obj->pin_miso, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)); pin_function(obj->pin_mosi, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)); pin_function(obj->pin_sclk, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)); pin_function(obj->pin_ssel, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)); }
/** Configure the SPI format * * Set the number of bits per frame, configure clock polarity and phase, shift order and master/slave mode. * The default bit order is MSB. * @param[in,out] obj The SPI object to configure * @param[in] bits The number of bits per frame * @param[in] mode The SPI mode (clock polarity, phase, and shift direction) * @param[in] slave Zero for master mode or non-zero for slave mode */ void spi_format(spi_t *obj, int bits, int mode, int slave) { struct spi_s *spiobj = SPI_S(obj); spiobj->spi_struct.frame_size = (bits == 16) ? SPI_FRAMESIZE_16BIT : SPI_FRAMESIZE_8BIT; /* Config polarity and phase of SPI */ switch (mode) { case 0: spiobj->spi_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE; break; case 1: spiobj->spi_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_2EDGE; break; case 2: spiobj->spi_struct.clock_polarity_phase = SPI_CK_PL_HIGH_PH_1EDGE; break; default: spiobj->spi_struct.clock_polarity_phase = SPI_CK_PL_HIGH_PH_2EDGE; break; } if (spiobj->spi_struct.nss != SPI_NSS_SOFT) { if (slave) { pin_function(spiobj->pin_mosi, MODE_IN_FLOATING); pin_function(spiobj->pin_sclk, MODE_IN_FLOATING); pin_function(spiobj->pin_ssel, MODE_IN_FLOATING); spi_nss_output_disable(spiobj->spi); } } /* Select SPI as master or slave */ spiobj->spi_struct.device_mode = (slave) ? SPI_SLAVE : SPI_MASTER; dev_spi_struct_init(obj); }
void serial_free(serial_t *obj) { // Reset UART and disable clock if (obj->uart == UART_1) { __USART1_FORCE_RESET(); __USART1_RELEASE_RESET(); __USART1_CLK_DISABLE(); } if (obj->uart == UART_2) { __USART2_FORCE_RESET(); __USART2_RELEASE_RESET(); __USART2_CLK_DISABLE(); } if (obj->uart == UART_3) { __USART3_FORCE_RESET(); __USART3_RELEASE_RESET(); __USART3_CLK_DISABLE(); } // Configure GPIOs pin_function(obj->pin_tx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); pin_function(obj->pin_rx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); serial_irq_ids[obj->index] = 0; }
/** Release a SPI object * * TODO: spi_free is currently unimplemented * This will require reference counting at the C++ level to be safe * * Return the pins owned by the SPI object to their reset state * Disable the SPI peripheral * Disable the SPI clock * @param[in] obj The SPI object to deinitialize */ void spi_free(spi_t *obj) { struct spi_s *spiobj = SPI_S(obj); spi_disable(spiobj->spi); /* Disable and deinit SPI */ if (spiobj->spi == SPI0) { spi_i2s_deinit(SPI0); rcu_periph_clock_disable(RCU_SPI0); } if (spiobj->spi == SPI1) { spi_i2s_deinit(SPI1); rcu_periph_clock_disable(RCU_SPI1); } if (spiobj->spi == SPI2) { spi_i2s_deinit(SPI2); rcu_periph_clock_disable(RCU_SPI2); } /* Deinit GPIO mode of SPI pins */ pin_function(spiobj->pin_miso, MODE_IN_FLOATING); pin_function(spiobj->pin_mosi, MODE_IN_FLOATING); pin_function(spiobj->pin_sclk, MODE_IN_FLOATING); if (spiobj->spi_struct.nss != SPI_NSS_SOFT) { pin_function(spiobj->pin_ssel, MODE_IN_FLOATING); } }
void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, PinName txflow) { /* Sanity check arguments */ MBED_ASSERT(obj); if(FlowControlNone == type) { /* Disable Hardware Handshaking. */ _USART(obj)->US_MR = (_USART(obj)->US_MR & ~US_MR_USART_MODE_Msk) | US_MR_USART_MODE_NORMAL; return; } /*To determine the uart peripheral associated with pins*/ UARTName uart_cts = (UARTName)pinmap_peripheral(txflow, PinMap_UART_CTS); UARTName uart_rts = (UARTName)pinmap_peripheral(rxflow, PinMap_UART_RTS); UARTName uart = (UARTName)pinmap_merge(uart_cts, uart_rts); MBED_ASSERT(uart != (UARTName)NC); if((FlowControlCTS == type) || (FlowControlRTSCTS== type)) { /* Configure CTS pin. */ pin_function(txflow, pinmap_find_function(txflow, PinMap_UART_CTS)); ioport_disable_pin(txflow); } if((FlowControlRTS == type) || (FlowControlRTSCTS== type)) { /* Configure CTS pin. */ pin_function(rxflow, pinmap_find_function(rxflow, PinMap_UART_RTS)); ioport_disable_pin(rxflow); } /* Set hardware handshaking mode. */ _USART(obj)->US_MR = (_USART(obj)->US_MR & ~US_MR_USART_MODE_Msk) | US_MR_USART_MODE_HW_HANDSHAKING; }
void spi_free(spi_t *obj) { // Reset SPI and disable clock if (obj->spi == SPI_1) { __SPI1_FORCE_RESET(); __SPI1_RELEASE_RESET(); __SPI1_CLK_DISABLE(); } if (obj->spi == SPI_2) { __SPI2_FORCE_RESET(); __SPI2_RELEASE_RESET(); __SPI2_CLK_DISABLE(); } if (obj->spi == SPI_3) { __SPI3_FORCE_RESET(); __SPI3_RELEASE_RESET(); __SPI3_CLK_DISABLE(); } // Configure GPIOs pin_function(obj->pin_miso, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); pin_function(obj->pin_mosi, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); pin_function(obj->pin_sclk, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); pin_function(obj->pin_ssel, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); }
void gpio_dir(gpio_t *obj, PinDirection direction) { if (direction == PIN_OUTPUT) { pin_function(obj->pin, STM_PIN_DATA(GPIO_Mode_OUT, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)); } else { // PIN_INPUT pin_function(obj->pin, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF)); } }
/** 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; }
/* * Initializes i/o pins for i2c sda/scl. */ static void i2c_init_pins(i2c_obj_t *obj) { /* MBED driver reserves pins for I2C as Ditigal IO while doing I2C error * recovery in constructor. */ pin_function(obj->pin_sda, pinmap_function(obj->pin_sda, PinMap_I2C_SDA)); pin_function(obj->pin_scl, pinmap_function(obj->pin_scl, PinMap_I2C_SCL)); }
void gpio_dir(gpio_t *obj, PinDirection direction) { MBED_ASSERT(obj->pin != (PinName)NC); if (direction == PIN_OUTPUT) { pin_function(obj->pin, STM_PIN_DATA(STM_MODE_OUTPUT_PP, GPIO_NOPULL, 0)); } else { // PIN_INPUT pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); } }
void gpio_dir(gpio_t *obj, PinDirection direction) { if (direction == PIN_OUTPUT) { pin_function(obj->pin, STM_PIN_DATA(GPIO_Mode_Out_PP, 0)); } else { // PIN_INPUT pin_function(obj->pin, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)); } }
USBHAL::USBHAL(void) { NVIC_DisableIRQ(OTG_FS_IRQn); epCallback[0] = &USBHAL::EP1_OUT_callback; epCallback[1] = &USBHAL::EP1_IN_callback; epCallback[2] = &USBHAL::EP2_OUT_callback; epCallback[3] = &USBHAL::EP2_IN_callback; epCallback[4] = &USBHAL::EP3_OUT_callback; epCallback[5] = &USBHAL::EP3_IN_callback; // Enable power and clocking RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; #if defined(TARGET_STM32F407VG) || defined(TARGET_STM32F401RE) || defined(TARGET_STM32F411RE) || defined(TARGET_STM32F412ZG) || defined(TARGET_STM32F429ZI) pin_function(PA_8, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS)); pin_function(PA_9, STM_PIN_DATA(STM_MODE_INPUT, GPIO_PULLDOWN, GPIO_AF10_OTG_FS)); pin_function(PA_10, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_PULLUP, GPIO_AF10_OTG_FS)); pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS)); pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS)); #else pin_function(PA_8, STM_PIN_DATA(2, 10)); pin_function(PA_9, STM_PIN_DATA(0, 0)); pin_function(PA_10, STM_PIN_DATA(2, 10)); pin_function(PA_11, STM_PIN_DATA(2, 10)); pin_function(PA_12, STM_PIN_DATA(2, 10)); // Set ID pin to open drain with pull-up resistor pin_mode(PA_10, OpenDrain); GPIOA->PUPDR &= ~(0x3 << 20); GPIOA->PUPDR |= 1 << 20; // Set VBUS pin to open drain pin_mode(PA_9, OpenDrain); #endif RCC->AHB2ENR |= RCC_AHB2ENR_OTGFSEN; // Enable interrupts OTG_FS->GREGS.GAHBCFG |= (1 << 0); // Turnaround time to maximum value - too small causes packet loss OTG_FS->GREGS.GUSBCFG |= (0xF << 10); // Unmask global interrupts OTG_FS->GREGS.GINTMSK |= (1 << 3) | // SOF (1 << 4) | // RX FIFO not empty (1 << 12); // USB reset OTG_FS->DREGS.DCFG |= (0x3 << 0) | // Full speed (1 << 2); // Non-zero-length status OUT handshake OTG_FS->GREGS.GCCFG |= (1 << 19) | // Enable VBUS sensing (1 << 16); // Power Up instance = this; NVIC_SetVector(OTG_FS_IRQn, (uint32_t)&_usbisr); NVIC_SetPriority(OTG_FS_IRQn, 1); }
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 if (tx != NC) { pin_mode(tx, PullUp); } if (rx != NC) { 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 port_dir(port_t *obj, PinDirection dir) { uint32_t i; obj->direction = dir; for (i = 0; i < 16; i++) { // Process all pins if (obj->mask & (1 << i)) { // If the pin is used if (dir == PIN_OUTPUT) { pin_function(port_pin(obj->port, i), STM_PIN_DATA(GPIO_Mode_OUT, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)); } else { // PIN_INPUT pin_function(port_pin(obj->port, i), STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF)); } } } }
void spi_free(spi_t *obj) { // Reset SPI and disable clock switch(obj->spi.module) { case 0: __SPI1_FORCE_RESET(); __SPI1_RELEASE_RESET(); __SPI1_CLK_DISABLE(); break; case 1: __SPI2_FORCE_RESET(); __SPI2_RELEASE_RESET(); __SPI2_CLK_DISABLE(); break; case 2: __SPI3_FORCE_RESET(); __SPI3_RELEASE_RESET(); __SPI3_CLK_DISABLE(); break; #if MODULES_SIZE_SPI > 3 case 3: __SPI4_FORCE_RESET(); __SPI4_RELEASE_RESET(); __SPI4_CLK_DISABLE(); break; #endif #if MODULES_SIZE_SPI > 4 case 4: __SPI5_FORCE_RESET(); __SPI5_RELEASE_RESET(); __SPI5_CLK_DISABLE(); break; #endif #if MODULES_SIZE_SPI > 5 case 5: __SPI6_FORCE_RESET(); __SPI6_RELEASE_RESET(); __SPI6_CLK_DISABLE(); break; #endif default: break; } // Configure GPIOs pin_function(obj->spi.pin_miso, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); pin_function(obj->spi.pin_mosi, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); pin_function(obj->spi.pin_sclk, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); DEBUG_PRINTF("SPI%u: Free\n", obj->spi.module+1); }
int SPIHalfDuplex::write(int value) { int t_bits = _bits; pin_function(_mosi, SPI_MODE); int ret_val = SPI::write(value); if (ret_val != value) { return -1; } format(_sbits, _mode); pin_function(_mosi, GPIO_MODE); ret_val = SPI::write(0x55); format(t_bits, _mode); pin_function(_mosi, SPI_MODE); return ret_val; }
/** Release the serial peripheral, not currently invoked. It requires further * resource management. * * @param obj The serial object */ void serial_free(serial_t *obj) { struct serial_s *p_obj = GET_SERIAL_S(obj); rcu_periph_enum rcu_periph = usart_clk[p_obj->index]; /* reset USART and disable clock */ usart_deinit(p_obj->uart); rcu_periph_clock_disable(rcu_periph); serial_irq_ids[p_obj->index] = 0; /* reset the GPIO state */ pin_function(p_obj->pin_tx, MODE_IN_FLOATING); pin_function(p_obj->pin_rx, MODE_IN_FLOATING); }
int pin_pwm_control(unsigned char pin, unsigned char duty_cycle) { unsigned char pf = pin_function(pin); if(pf != PIN_FUNCTION_PWM) { return PIN_STAT_ERR_UNSUPFUNC; } int port; int bit; if((port = pin2port(pin)) < 0) return port; if((bit = pin2bit(pin)) < 0) return bit; if(port == 1) { // e.g. DC=50%: (20000 / 100) * (128 / 2.55) ~ 10000 TA0CCR1 = (TA0CCR0 / 100) * (duty_cycle / 2.55); } else { // e.g. DC=50%: (20000 / 100) * (128 / 2.55) ~ 10000 TA1CCR1 = (TA1CCR0 / 100) * (duty_cycle / 2.55); } return PIN_STAT_OK; }
uint32_t gpio_set(PinName pin) { if (pin == NC) return 0; pin_function(pin, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)); return (uint32_t)(1 << ((uint32_t)pin & 0xF)); // Return the pin mask }
uint32_t gpio_set(PinName pin) { MBED_ASSERT(pin != (PinName)NC); pin_function(pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); return (uint32_t)(1 << ((uint32_t)pin & 0xF)); // Return the pin mask }
void analogout_free(dac_t *obj) { // Reset DAC and disable clock if (obj->pin == PA_4) { pa4_used = 0; } if (obj->pin == PA_5) { pa5_used = 0; } if ((pa4_used == 0) && (pa5_used == 0)) { __HAL_RCC_DAC1_FORCE_RESET(); __HAL_RCC_DAC1_RELEASE_RESET(); __HAL_RCC_DAC1_CLK_DISABLE(); } #if defined(DAC2) if (obj->pin == PA_6) { __HAL_RCC_DAC2_FORCE_RESET(); __HAL_RCC_DAC2_RELEASE_RESET(); __HAL_RCC_DAC2_CLK_DISABLE(); } #endif // Configure GPIO pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); }
void gpio_dir(gpio_t *obj, PinDirection direction) { MBED_ASSERT(obj->pin != (PinName)NC); obj->direction = direction; pin_function(obj->pin, WIZ_PIN_DATA(obj->direction, obj->mode, 1)); }
uint32_t gpio_set(PinName pin) { if (pin == NC) return 0; pin_function(pin, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF)); return (uint32_t)(1 << ((uint32_t)pin & 0xF)); // Return the pin mask }
uint32_t gpio_set(PinName pin) { MBED_ASSERT(pin != (PinName)NC); uint32_t pin_num = pin & 0xFF; pin_function(pin, (int)kPortMuxAsGpio); return 1 << pin_num; }
int pin_pwm_function(unsigned char pin, int period) { unsigned char pf = pin_function(pin); if(pf != PIN_FUNCTION_PWM) { return PIN_STAT_ERR_UNSUPFUNC; } int port; int bit; if((port = pin2port(pin)) < 0) return port; if((bit = pin2bit(pin)) < 0) return bit; if(port == 1) { TA0CCR0 = period - 1; // Set period TA0CCTL1 = OUTMOD_7; // CCR1 reset/set TA0CTL = TASSEL_2 + MC_1; // SMCLK, up mode } else { TA1CCR0 = period - 1; // Set period TA1CCTL1 = OUTMOD_7; // CCR1 reset/set TA1CTL = TASSEL_2 + MC_1; // SMCLK, up mode } return PIN_STAT_OK; }
void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable) { uint32_t mode = STM_MODE_INPUT; uint32_t pull = GPIO_NOPULL; if (enable) { pull = GPIO_NOPULL; if (event == IRQ_RISE) { if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) { mode = STM_MODE_IT_RISING_FALLING; obj->event = EDGE_BOTH; } else { // NONE or RISE mode = STM_MODE_IT_RISING; obj->event = EDGE_RISE; } } if (event == IRQ_FALL) { if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) { mode = STM_MODE_IT_RISING_FALLING; obj->event = EDGE_BOTH; } else { // NONE or FALL mode = STM_MODE_IT_FALLING; obj->event = EDGE_FALL; } } } else { mode = STM_MODE_INPUT; pull = GPIO_NOPULL; obj->event = EDGE_NONE; } pin_function(obj->pin, STM_PIN_DATA(mode, pull, 0)); }
void pwmout_free(pwmout_t* obj) { TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); HAL_TIM_PWM_DeInit(&TimHandle); pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); }
uint32_t gpio_set(PinName pin) { MBED_ASSERT(pin != (PinName)NC); pin_function(pin, 0); return (1 << ((int)pin & 0x1F)); }
void initialize() { // Disable timers 0, 1 LPC_CT16B0->TCR = TCR_OFF; LPC_CT16B1->TCR = TCR_OFF; //Power timers 0, 1 LPC_SYSCON->SYSAHBCLKCTRL |= 1 << (7 + 0); LPC_SYSCON->SYSAHBCLKCTRL |= 1 << (7 + 1); // Enable counter mode (non PWM) for timers 0, 1 LPC_CT16B0->PWMC = 0b0000; LPC_CT16B1->PWMC = 0b0000; // Reset functionality on MR0 controlling the counter time period LPC_CT16B0->MCR = (1 << 1); // Timer0: reset counter 0 o0 MR0 match LPC_CT16B1->MCR = (1 << 4); // Timer1: reset counter 1 on MR1 match // Set prescalers to 1. Timer is incremented 'SystemCoreClock' times a second. LPC_CT16B0->PR = 0; LPC_CT16B1->PR = 0; // TODO: is this reset needed? LPC_CT16B0->TCR = TCR_RESET; LPC_CT16B1->TCR = TCR_RESET; // Clear output on match (tone are off, keep outputs low). LPC_CT16B0->EMR = (0b01 << 4); // Timer0: output LOW on MR0 match LPC_CT16B1->EMR = (0b10 << 6); // Timer1: output HIGH on MR1 match // Set arbitrary cycle, just to have counter matches which sets // the outputs to desired values based on EMR setting. // Values don't matter much since we override latter when dialing // the tones. LPC_CT16B0->MR0 = COUNT(1000); LPC_CT16B1->MR1 = COUNT(1300); LPC_CT16B0->TCR = TCR_EN; LPC_CT16B1->TCR = TCR_EN; // Pinout // TODO: define a const for the PWM pin function 2. pin_function(P0_8, 2); // CT16B0_MAT0 pin_mode(P0_8, PullNone); pin_function(P0_22, 2); // CT16B1_MAT1 pin_mode(P0_22, PullNone); }
void analogout_free(dac_t *obj) { DAC_TypeDef *dac = (DAC_TypeDef *)(obj->dac); // Disable DAC DAC_DeInit(dac); RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, DISABLE); // Configure GPIO pin_function(obj->pin, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF)); }