void usart_init(usart_num num) { if(!usart_available(num)) return; usart_driver *driver = &usart_drivers[num]; const usart_dev *dev = usart_get_dev(num); rcc_clk_enable(dev->clk_id); nvic_irq_enable(dev->irq); /* initial driver */ driver->p_dev = dev; /* enable receive and transmit mode */ usart_t *usart = (usart_t *)dev->reg; usart->CR1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_UE | USART_CR1_IDLEIE; /* Parity: none, stop bit: 0, flow control: none */ usart->CR2 = 0; /* Enable TX/RX DMA */ usart->CR3 = 0; (void)usart->SR; (void)usart->DR; /* Initial DMA */ usart_dma_init(num); usart_set_used(num); }
int Ax12Class::readPosition(unsigned char ID) { TChecksum = (ID + AX_POS_LENGTH + AX_READ_DATA + AX_PRESENT_POSITION_L + AX_BYTE_READ_POS); while ( TChecksum >= 255){ TChecksum -= 255; } Checksum = 255 - TChecksum; digitalWrite(Direction_Pin,HIGH); usart_write(AX_START); usart_write(AX_START); usart_write(ID); usart_write(AX_POS_LENGTH); usart_write(AX_READ_DATA); usart_write(AX_PRESENT_POSITION_L); usart_write(AX_BYTE_READ_POS); usart_write(Checksum); delayMicroseconds(TX_DELAY_TIME); digitalWrite(Direction_Pin,LOW); // Set Rx Mode Position_Long_Byte = 0; Time_Counter = 0; while(usart_available() < 7 & Time_Counter < TIME_OUT){ Time_Counter++; delay(1); if( usart_peek() != 255 ){ usart_read(); } } while (usart_available() > 0){ Incoming_Byte = usart_read(); if ( Incoming_Byte == 255 & usart_peek() == 255 ){ usart_read(); // Start Bytes usart_read(); // Ax-12 ID usart_read(); // Length if( (Error_Byte = usart_read()) != 0 ) // Error return (Error_Byte*(-1)); Position_Low_Byte = usart_read(); // Position Bytes Position_High_Byte = usart_read(); Position_Long_Byte = Position_High_Byte << 8; Position_Long_Byte = Position_Long_Byte + Position_Low_Byte; } } return (Position_Long_Byte); // Returns the read position }
int Ax12Class::readVoltage(unsigned char ID) { TChecksum = (ID + AX_VOLT_LENGTH + AX_READ_DATA + AX_PRESENT_VOLTAGE + AX_BYTE_READ); while ( TChecksum >= 255){ TChecksum -= 255; } Checksum = 255 - TChecksum; digitalWrite(Direction_Pin,HIGH); usart_write(AX_START); usart_write(AX_START); usart_write(ID); usart_write(AX_VOLT_LENGTH); usart_write(AX_READ_DATA); usart_write(AX_PRESENT_VOLTAGE); usart_write(AX_BYTE_READ); usart_write(Checksum); delayMicroseconds(TX_DELAY_TIME); digitalWrite(Direction_Pin,LOW); // Set Rx Mode Voltage_Byte = 0; Time_Counter = 0; while(usart_available() < 6 & Time_Counter < TIME_OUT){ Time_Counter++; delay(1); if( usart_peek() != 255 ){ usart_read(); } } while (usart_available() > 0){ Incoming_Byte = usart_read(); if ( Incoming_Byte == 255 & usart_peek() == 255 ){ usart_read(); // Start Bytes usart_read(); // Ax-12 ID usart_read(); // Length if( (Error_Byte = usart_read()) != 0 ) // Error return (Error_Byte*(-1)); Voltage_Byte = usart_read(); // Voltage } } return (Voltage_Byte); // Returns the read Voltage }
int Ax12Class::read_error(void) { Time_Counter = 0; while(usart_available() < 5 & Time_Counter < TIME_OUT){ // Wait for Data Time_Counter++; delay(1); if( usart_peek() != 255 ){ usart_read(); } } while (usart_available() > 0){ Incoming_Byte = usart_read(); if ( Incoming_Byte == 255 & usart_peek() == 255 ){ usart_read(); // Start Bytes usart_read(); // Ax-12 ID usart_read(); // Length Error_Byte = usart_read(); // Error return (Error_Byte); } } return (-1); // No Ax Response }
int main(void) { int i = 10; GPIO_InitTypeDef gpio_init; USART_InitTypeDef usart_init; USART_ClockInitTypeDef usart_clk_init; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // PA9 = Tx, PA10 = Rx gpio_init.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; gpio_init.GPIO_Mode = GPIO_Mode_AF; gpio_init.GPIO_Speed = GPIO_Speed_40MHz; gpio_init.GPIO_OType = GPIO_OType_PP; gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &gpio_init); GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1); USART_ClockStructInit(&usart_clk_init); USART_ClockInit(USART1, &usart_clk_init); usart_init.USART_BaudRate = 9600; usart_init.USART_WordLength = USART_WordLength_8b; usart_init.USART_StopBits = USART_StopBits_1; usart_init.USART_Parity = USART_Parity_No ; usart_init.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; usart_init.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_Init(USART1, &usart_init); USART_Cmd(USART1,ENABLE); while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {} while (1) { if ( usart_available() ) // data available { usart_print( "Data Available: " ); uint8_t ch = usart_read(); usart_write(ch); usart_print( "\r\n" ); } } return 0; }
int main(void) { uint16_t bits; uint32_t intval = 40; uint32_t tnow; char tmp[92]; RCC_ClocksTypeDef RCC_Clocks; uint16_t i; TIM2_timer_start(); usart_begin(&USerial3, USART3, PC11, PC10, 19200); usart_print(&USerial3, "Happy are those who know they are spiritually poor; \n" "The kingdom of heaven belongs to them!\n"); usart_flush(&USerial3); RCC_GetClocksFreq(&RCC_Clocks); sprintf(tmp, "SYSCLK = %ul\n", RCC_Clocks.SYSCLK_Frequency); usart_print(&USerial3, tmp); sprintf(tmp, "PCLK1 = %ul\n", RCC_Clocks.PCLK1_Frequency); usart_flush(&USerial3); GPIOMode(PinPort(PD12), (PinBit(PD12) | PinBit(PD13) | PinBit(PD14) | PinBit(PD15)), OUTPUT, FASTSPEED, PUSHPULL, NOPULL); /* spi_begin(SPI2, PB13, PB14, PB15, PB12); digitalWrite(PB12, HIGH); */ I2C1_Init(); /* i2c_begin(&Wire1, PB9, PB8, 100000); lcd.init(&Wire1); lcd.begin(); lcd.setContrast(46); lcd.print("Yappee!"); // Classic Hello World! */ bits = GPIO_ReadOutputData(GPIOD ); GPIOWrite(GPIOD, PinBit(PD13) | (bits & 0x0fff)); delay_ms(intval); tnow = millis() / 1000; while (tnow == millis() / 1000) ; tnow = millis() / 1000; while (1) { bits = GPIO_ReadOutputData(GPIOD ); GPIOWrite(GPIOD, PinBit(PD13) | (bits & 0x0fff)); delay_ms(intval); GPIOWrite(GPIOD, PinBit(PD14) | (bits & 0x0fff)); delay_ms(intval); GPIOWrite(GPIOD, PinBit(PD15) | (bits & 0x0fff)); delay_ms(intval); GPIOWrite(GPIOD, PinBit(PD12) | (bits & 0x0fff)); delay_ms(intval); // bits &= 0x0fff; switch ((tnow % 60) / 15) { case 3: bits |= PinBit(PD12); case 2: bits |= PinBit(PD15); case 1: bits |= PinBit(PD14); case 0: default: bits |= PinBit(PD13); break; } GPIOWrite(GPIOD, bits); while (tnow == millis() / 1000); tnow = millis() / 1000; //Serial3.print(tmp); sprintf(tmp, "%04ld\n", millis()); usart_print(&USerial3, tmp); /* digitalWrite(PB12, LOW); spi_transfer(SPI2, (uint8_t *) tmp, 8); digitalWrite(PB12, HIGH); */ i = 0; if (usart_available(&USerial3) > 0) { while (usart_available(&USerial3) > 0 && i < 92) { tmp[i++] = (char) usart_read(&USerial3); } tmp[i] = 0; usart_print(&USerial3, "> "); usart_print(&USerial3, tmp); usart_print(&USerial3, "\n"); } } return 0; }