vsdip / vsdsquadron_pio

Apache License 2.0
4 stars 2 forks source link

Documenting UART Example #2

Open yathAg opened 7 months ago

yathAg commented 7 months ago

I have tested the code for UART Tx. If someone is willing to test the code for Rx and document everything, please make a pull request for the same.

Here is the code for TX

#include <ch32v00x.h>
#include <stdio.h>
#include "debug.h"

int main(void)
{
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    SystemCoreClockUpdate();
    Delay_Init();
    USART_Printf_Init(115200);
    Delay_Ms(1000); // give serial monitor time to open
    printf("SystemClk: %u\r\n", (unsigned)SystemCoreClock);
    printf("DeviceID: %08x\r\n", (unsigned)DBGMCU_GetDEVID());

    while (1)
    {
        Delay_Ms(1000);
        printf("Hello From VSDSquadron Mini!!");
    }
}

void NMI_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void NMI_Handler(void) {}
void HardFault_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void HardFault_Handler(void)
{
    while (1)
    {
    }
}
anandr16910 commented 6 months ago

Hii Yatharth,

Is this example working in mini board. I tried this vscode but no response from board in serial monitor mode. Build and upload were successful. But no printf statement. Blank screen that's it.

yathAg commented 6 months ago

I got the output using a logic analyzer. I don't know if it will work with the serial monitor. Please look at it from your end, and I will try it out, too.

anandr16910 commented 6 months ago

Hi Yatharth, I tried using several serial monitor like miniterm putty and many more. But no response from this board. Also there in examples there is only led example given. So I tried cloning other projects from ch32v repository which had UART example but no response. Everything works builds and gets uploaded successfully but no character seen in terminal window.

davidbroughsmyth commented 6 months ago

This code looks like it is working when viewing pd5 pin on an oscilloscope. There is a delay then the Tx pin shows the serial message and repeats. IMG20240222202134

davidbroughsmyth commented 6 months ago

@yathAg RX is also tested https://github.com/davidbroughsmyth/vsdsquadron-mini-research/tree/main/uart_echo

davidbroughsmyth commented 6 months ago

Wire up like this-->

usb_serial_VSDMini
ARX-0 commented 3 months ago

@davidbroughsmyth @yathAg What if i want to display analog values in the serial monitor like in ardino is necessary or is it possible only with the USB-C

davidbroughsmyth commented 2 months ago

Do you see the Tx and Rx pin on the VSDsquadron Mini board? Simply do a wire cross over: TX (PD5) - RX, RX (PD6) - TX and you will get a serial monitor like in Arduino without the need of a USB to UART TTL converter.

Then code as normal -> https://github.com/davidbroughsmyth/vsdsquadron-mini-research/tree/main/uart_poc

ankitmawle commented 1 month ago

@davidbroughsmyth @yathAg why were X (PD5) - RX, RX (PD6) - TX connections not made internally on the pcb, any specific reason?

yathAg commented 1 month ago

Hi, please recheck the datasheet. The pins are available on J3 pin7 and J5 pin1

If u are referring to the terminal connections, thats because some applications might require you to use UART externally.

ankitmawle commented 1 month ago

You need to install wch-link Download it from the release here and install it, should resolve your issue https://github.com/Community-PIO-CH32V/wchlink-driver-windows

On Wed, 31 Jul, 2024, 10:52 am vedantmhatre2211, @.***> wrote:

Title: Unable to Upload Code to VSDsquadron Mini for Home Automation Project

Description: We are working on a project to control home appliances using the VSDsquadron Mini. While we have successfully built the code, we are facing issues with uploading it to the device. Below, we provide details of the problem, including images and the relevant code.

Problem: When attempting to upload the code to the VSDsquadron Mini, the process fails, and the code does not get uploaded. We have ensured that the device is connected correctly to COM7 on our PC, but the issue persists.

Setup:

  • VSDsquadron Mini connected to COM7
  • Asus TUF FX505DT laptop with Ryzen 7 processor
  • Using Arduino IDE (or specify the software you are using if different)

Images: [image: Connection Setup] http://path-to-your-image1.jpg ![Error Message] platformini.io.upload.error.jpg (view on web) https://github.com/user-attachments/assets/7d7e610f-6ef2-4c29-a59b-9549541153d7

main.c.error.jpg (view on web) https://github.com/user-attachments/assets/00bec68f-7eb4-41ed-b4dc-ec450d36d396

Code:

cpp platformio.ini code ; PlatformIO Project Configuration File ; ; Build options: build flags, source filter, extra scripting ; Upload options: custom port, speed and extra flags ; Library options: dependencies, extra library storages ; ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html

[env] platform = ch32v framework = noneos-sdk monitor_speed = 115200 ; uncomment this to use USB bootloader upload via WCHISP ; upload_protocol = isp

[env:vsdsquadronMini] board = vsdsquadronMini upload_protocol = wch-link build_flags = -D PIO_FRAMEWORK_NONEOS_SDK_V0

MAIN.C code:-

include

include

/* PWM Output Mode Definition

/ #define PWM_MODE1 0 #define PWM_MODE2 1 / PWM Output Mode Selection */

define PWM_MODE PWM_MODE2

/ Threshold distance in cm for water level /

define WATER_LEVEL_THRESHOLD 10

/ Function to initialize PWM on Timer 1 for the servo motor / void TIM1_PWMOut_Init(uint16_t arr, uint16_t psc, uint16_t ccp) { GPIO_InitTypeDef GPIO_InitStructure = {0}; TIM_OCInitTypeDef TIM_OCInitStructure = {0}; TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure = {0};

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // Alternate Function Push-Pull GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_Init(GPIOD, &GPIO_InitStructure);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); TIM_TimeBaseInitStructure.TIM_Period = arr; TIM_TimeBaseInitStructure.TIM_Prescaler = psc; TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStructure);

if (PWM_MODE == PWM_MODE1)

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

elif (PWM_MODE == PWM_MODE2)

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;

endif

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = ccp; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM1, &TIM_OCInitStructure); TIM_CtrlPWMOutputs(TIM1, ENABLE); TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Disable); TIM_ARRPreloadConfig(TIM1, ENABLE); TIM_Cmd(TIM1, ENABLE); }

/ Function to configure GPIO Pins / void GPIO_Config(void) { GPIO_InitTypeDef GPIO_InitStructure = {0};

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);

// Pin 3: Input for Ultrasonic sensor echo GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // Input with Pull-Up GPIO_Init(GPIOD, &GPIO_InitStructure);

// Pin 4: Output for Ultrasonic sensor trigger GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // Output Push-Pull GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure);

// Pin 6: LED indicator GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // Output Push-Pull GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure);

}

/ Function to trigger the ultrasonic sensor and read the echo duration / uint32_t Ultrasonic_Read(void) { uint32_t echoTime = 0;

GPIO_WriteBit(GPIOD, GPIO_Pin_4, SET); // Setting Trigger Pin to send pulses Delay_Us(10); // Pulse Width GPIO_WriteBit(GPIOD, GPIO_Pin_4, RESET); // Resetting Trigger Pin

while (GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_3) == Bit_RESET); // Wait for Echo to go high while (GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_3) == Bit_SET) echoTime++; // Measure the time Echo is high

return echoTime;

}

/ Function to calculate distance from echo time / float Calculate_Distance(uint32_t echoTime) { // Speed of sound in air is 340 m/s or 0.034 cm/us // Distance is (time / 2) speed_of_sound return (echoTime / 2.0) 0.034; }

/ Function to control LED blinking / void Blink_LED(uint8_t times, uint16_t on_time, uint16_t off_time) { for (uint8_t i = 0; i < times; i++) { GPIO_WriteBit(GPIOD, GPIO_Pin_6, Bit_SET); // Turn LED on Delay_Ms(on_time); // Delay for on_time

GPIO_WriteBit(GPIOD, GPIO_Pin_6, Bit_RESET); // Turn LED off
Delay_Ms(off_time); // Delay for off_time

}

}

/ Main function / int main(void) { NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); // Set priority group SystemCoreClockUpdate(); Delay_Init(); GPIO_Config(); USART_Printf_Init(115200); // Initialize debug USART

while (1) { uint32_t echoTime = Ultrasonic_Read(); float distance = Calculate_Distance(echoTime);

printf("Distance: %.2f cm\n", distance); // Print the distance

if (distance < WATER_LEVEL_THRESHOLD) // If water level is below the threshold
{
    Blink_LED(3, 200, 100); // Blink LED three times with specified on and off times
    TIM1_PWMOut_Init(100, 480 - 1, 95 <https://www.google.com/maps/search/480+-+1,+95?entry=gmail&source=g>); // Set PWM to 95% duty cycle to activate the servo motor
}
else
{
    GPIO_WriteBit(GPIOD, GPIO_Pin_6, Bit_RESET); // Turn off LED
    TIM1_PWMOut_Init(100, 480 - 1, 10); // Set PWM to 10% duty cycle to deactivate the servo motor
}

Delay_Ms(1000); // Wait for 1 second before next reading

}

}

Steps to Reproduce:

  1. Connect the VSDsquadron Mini to the PC via COM7.
  2. Open the Visual Studio Code
  3. Load the provided code.
  4. Attempt to upload the code to the VSDsquadron Mini.

Expected Behavior: The code should upload successfully, allowing us to control the home appliances.

Actual Behavior: The upload process fails with an error message (as shown in the image above).

Additional Information:

  • We have checked the connections and ensured they are secure.
  • The VSDsquadron Mini appears to be functioning correctly otherwise.

Request: We are seeking assistance to resolve this upload issue. Any guidance or suggestions would be greatly appreciated.

Thank you!

— Reply to this email directly, view it on GitHub https://github.com/vsdip/vsdsquadron_pio/issues/2#issuecomment-2259694166, or unsubscribe https://github.com/notifications/unsubscribe-auth/AIRIOJPPZSYJJFKTVWLQ3ULZPBYC7AVCNFSM6AAAAABDHSZR22VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENJZGY4TIMJWGY . You are receiving this because you commented.Message ID: @.***>

vedantmhatre2211 commented 1 month ago

I am testing my new vsdsquadron mini bored by referring to the code from the datasheet for blink but it is showing an error during building while in the documentation there wasn't any error I did everything as per the instructions can anybody please help me solve this error.

main.c code : image

platformio.ini code : image

ankitmawle commented 1 month ago

I am testing my new vsdsquadron mini bored by referring to the code from the datasheet for blink but it is showing an error during building while in the documentation there wasn't any error I did everything as per the instructions can anybody please help me solve this error.

main.c code : image

platformio.ini code : image

change NVIC_PriorityGroup_2 to NVIC_PriorityGroup_1, it should resolve the issue,

vedantmhatre2211 commented 4 weeks ago

I am testing my new vsdsquadron mini bored by referring to the code from the datasheet for blink but it is showing an error during building while in the documentation there wasn't any error I did everything as per the instructions can anybody please help me solve this error. main.c code : image platformio.ini code : image

change NVIC_PriorityGroup_2 to NVIC_PriorityGroup_1, it should resolve the issue,

how to do that ?can you please help

ankitmawle commented 4 weeks ago

I am testing my new vsdsquadron mini bored by referring to the code from the datasheet for blink but it is showing an error during building while in the documentation there wasn't any error I did everything as per the instructions can anybody please help me solve this error. main.c code : image platformio.ini code : image

change NVIC_PriorityGroup_2 to NVIC_PriorityGroup_1, it should resolve the issue,

how to do that ?can you please help

edit the code line no.15