boolean i2c_begin(I2CBus * wirex, uint32_t clkspeed) { // GPIO_InitTypeDef GPIO_InitStructure; I2C_InitTypeDef I2C_InitStructure; wirex->I2Cx = I2C1; wirex->sda = PB9; wirex->scl = PB8; /* I2C Periph clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); // RCC_APB1PeriphClockCmd(I2C1_RCC, ENABLE); /* GPIO Periph clock enable */ //RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); // PB5 (SMBA), PB6 (SCL), PB9 (SDA) // RCC_APB2PeriphClockCmd(I2C1_GPIO_RCC, ENABLE); GPIOMode(PinPort(wirex->scl), PinBit(wirex->scl), GPIO_Mode_AF, GPIO_Speed_50MHz, GPIO_OType_OD, GPIO_PuPd_UP); GPIOMode(PinPort(wirex->sda), PinBit(wirex->sda), GPIO_Mode_AF, GPIO_Speed_50MHz, GPIO_OType_OD, GPIO_PuPd_UP); /* Configure I2C pins: SCL and SDA */ GPIO_PinAFConfig(PinPort(wirex->scl), PinSource(wirex->scl), GPIO_AF_I2C1 ); GPIO_PinAFConfig(PinPort(wirex->sda), PinSource(wirex->sda), GPIO_AF_I2C1 ); //#if defined (REMAP_I2C1) //Remap_I2C1_Configuration(); //#endif switch (clkspeed) { case 100000: case 400000: break; default: clkspeed = 100000; break; } /* I2C configuration */ I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = clkspeed; /* Apply I2C configuration after enabling it */ I2C_Init(wirex->I2Cx, &I2C_InitStructure); /* I2C Peripheral Enable */ I2C_Cmd(wirex->I2Cx, ENABLE); wirex->status = NOT_READY; wirex->mode = I2C_MODE_NOTDEFINED; return true; }
String ^source; bool invert; }; ref class Pin { public: Pin(int c, int l, bool _isFixed, int ic) : C(c), L(l), IC(ic), isFixed(_isFixed), isNoise(false), source(nullptr) {} void Link(PinSource ^_source) { source = gcnew PinSource(_source); } bool IsSource() { return source == nullptr; } void Invert() { if (source != nullptr) source->Invert(); } int C; int L; int IC; bool isFixed; bool isNoise; PinSource ^source; }; }
void usart_init(usart * usx, USART_TypeDef * USARTx, GPIOPin rx, GPIOPin tx) { // uint8_t af = GPIO_AF_USART1; usx->USARTx = USARTx; if ( usx->USARTx == USART1) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); af = GPIO_AF_USART1; usx->irqn = USART1_IRQn; // usx->usid = USART1Serial; rxring[USART1Serial] = &usx->rxring; txring[USART1Serial] = &usx->txring; } else if (usx->USARTx == USART2) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); af = GPIO_AF_USART2; usx->irqn = USART2_IRQn; // usx->usid = USART2Serial; // usx->USARTx = USART2; rxring[USART2Serial] = &usx->rxring; txring[USART2Serial] = &usx->txring; } else if (usx->USARTx == USART3) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); af = GPIO_AF_USART3; usx->irqn = USART3_IRQn; // usx->usid = USART3Serial; // usx->USARTx = USART3; rxring[USART3Serial] = &usx->rxring; txring[USART3Serial] = &usx->txring; } else if (usx->USARTx == UART4) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); af = GPIO_AF_UART4; usx->irqn = UART4_IRQn; // usx->usid = UART4Serial; // usx->USARTx = UART4; rxring[UART4Serial] = &usx->rxring; txring[UART4Serial] = &usx->txring; } else if (usx->USARTx == UART5) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE); af = GPIO_AF_UART5; usx->irqn = UART5_IRQn; // usx->usid = UART5Serial; // usx->USARTx = UART5; rxring[UART5Serial] = &usx->rxring; txring[UART5Serial] = &usx->txring; } else { // Serial6 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE); af = GPIO_AF_USART6; usx->irqn = USART6_IRQn; // usx->usid = USART6Serial; // usx->USARTx = USART6; rxring[USART6Serial] = &usx->rxring; txring[USART6Serial] = &usx->txring; } GPIOMode(PinPort(rx), PinBit(rx), GPIO_Mode_AF, GPIO_Speed_50MHz, GPIO_OType_PP, GPIO_PuPd_NOPULL); GPIOMode(PinPort(tx), PinBit(tx), GPIO_Mode_AF, GPIO_Speed_50MHz, GPIO_OType_PP, GPIO_PuPd_NOPULL); GPIO_PinAFConfig(PinPort(rx), PinSource(rx), af); GPIO_PinAFConfig(PinPort(tx), PinSource(tx), af); }
void spi_init(SPIBus * spibx, SPI_TypeDef * SPIx, GPIOPin sck, GPIOPin miso, GPIOPin mosi, GPIOPin nss) { uint8_t af; // = GPIO_AF_SPI1; /* PCLK2 = HCLK/2 */ //RCC_PCLK2Config(RCC_HCLK_Div2); if ( SPIx == SPI2 ) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); af = GPIO_AF_SPI2; spibx->SPIx = SPI2; // PB12, 13, 14, 15 } else if ( SPIx == SPI3 ) { // SPI3 RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, ENABLE); af = GPIO_AF_SPI3; spibx->SPIx = SPI3; } else { //if (SPIx == SPI1) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); af = GPIO_AF_SPI1; spibx->SPIx = SPI1; // sck = PA5 / PB3, miso = PA6/ PB4, mosi = PA7 / PB5, nSS = PA4 / PA15 } GPIOMode(PinPort(sck), PinBit(sck), GPIO_Mode_AF, GPIO_Speed_25MHz, GPIO_OType_PP, GPIO_PuPd_UP); GPIOMode(PinPort(miso), PinBit(miso), GPIO_Mode_AF, GPIO_Speed_25MHz, GPIO_OType_PP, GPIO_PuPd_UP); GPIOMode(PinPort(mosi), PinBit(mosi), GPIO_Mode_AF, GPIO_Speed_25MHz, GPIO_OType_PP, GPIO_PuPd_UP); GPIO_PinAFConfig(PinPort(sck), PinSource(sck), af); GPIO_PinAFConfig(PinPort(miso), PinSource(miso), af); GPIO_PinAFConfig(PinPort(mosi), PinSource(mosi), af); // nSS by software GPIOMode(PinPort(nss), PinBit(nss), GPIO_Mode_OUT, GPIO_Speed_25MHz, GPIO_OType_PP, GPIO_PuPd_UP); digitalWrite(nss, HIGH); //GPIO_PinAFConfig(PinPort(nss), PinSource(nss), af); // set default parameters spibx->modeStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; spibx->modeStruct.SPI_Mode = SPI_Mode_Master; spibx->modeStruct.SPI_DataSize = SPI_DataSize_8b; spibx->modeStruct.SPI_CPOL = SPI_CPOL_Low; spibx->modeStruct.SPI_CPHA = SPI_CPHA_1Edge; spibx->modeStruct.SPI_NSS = SPI_NSS_Soft; spibx->modeStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; spibx->modeStruct.SPI_FirstBit = SPI_FirstBit_MSB; spibx->modeStruct.SPI_CRCPolynomial = SPI_CRC_Rx; SPI_Init(spibx->SPIx, &spibx->modeStruct); SPI_Cmd(spibx->SPIx, ENABLE); /* 5. Enable the NVIC and the corresponding interrupt using the function * SPI_ITConfig() if you need to use interrupt mode. * * 6. When using the DMA mode * - Configure the DMA using DMA_Init() function * - Active the needed channel Request using SPI_I2S_DMACmd() function * * 7. Enable the SPI using the SPI_Cmd() function or enable the I2S using * I2S_Cmd(). * * 8. Enable the DMA using the DMA_Cmd() function when using DMA mode. * * 9. Optionally, you can enable/configure the following parameters without * re-initialization (i.e there is no need to call again SPI_Init() function): * - When bidirectional mode (SPI_Direction_1Line_Rx or SPI_Direction_1Line_Tx) * is programmed as Data direction parameter using the SPI_Init() function * it can be possible to switch between SPI_Direction_Tx or SPI_Direction_Rx * using the SPI_BiDirectionalLineConfig() function. * - When SPI_NSS_Soft is selected as Slave Select Management parameter * using the SPI_Init() function it can be possible to manage the * NSS internal signal using the SPI_NSSInternalSoftwareConfig() function. * - Reconfigure the data size using the SPI_DataSizeConfig() function * - Enable or disable the SS output using the SPI_SSOutputCmd() function * * 10. To use the CRC Hardware calculation feature refer to the Peripheral * CRC hardware Calculation subsection. */ }