n32_msp.c 15.2 KB
Newer Older
1
/*
L
linyuanbo_breo_server 已提交
2
 * Copyright (c) 2006-2021, RT-Thread Development Team
3 4 5 6 7
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
8
 * 2021-08-20     breo.com     first version
9 10 11 12
 */


#include <rtthread.h>
13
#include <rtdevice.h>
14 15 16 17 18 19 20 21 22 23 24 25
#include "n32g45x.h"
#include "n32_msp.h"

#ifdef BSP_USING_UART
void n32_msp_usart_init(void *Instance)
{
    GPIO_InitType GPIO_InitCtlStruct;
    USART_Module *USARTx = (USART_Module *)Instance;

    GPIO_InitStruct(&GPIO_InitCtlStruct);
    GPIO_InitCtlStruct.GPIO_Speed = GPIO_Speed_50MHz;
#ifdef BSP_USING_UART1
1
192.168.1.134 已提交
26
    if (USART1 == USARTx)
27 28 29 30 31 32 33 34 35 36 37 38 39
    {
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_USART1, ENABLE);
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
        GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitCtlStruct.Pin = GPIO_PIN_9;
        GPIO_InitPeripheral(GPIOA, &GPIO_InitCtlStruct);

        GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_InitCtlStruct.Pin = GPIO_PIN_10;
        GPIO_InitPeripheral(GPIOA, &GPIO_InitCtlStruct);
    }
#endif
#ifdef BSP_USING_UART2
1
192.168.1.134 已提交
40
    if (USART2 == USARTx)
41 42 43 44 45 46 47 48 49 50 51 52 53
    {
        RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_USART2, ENABLE);
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
        GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitCtlStruct.Pin = GPIO_PIN_2;
        GPIO_InitPeripheral(GPIOA, &GPIO_InitCtlStruct);

        GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_InitCtlStruct.Pin = GPIO_PIN_3;
        GPIO_InitPeripheral(GPIOA, &GPIO_InitCtlStruct);
    }
#endif
#ifdef BSP_USING_UART3
1
192.168.1.134 已提交
54
    if (USART3 == USARTx)
55 56 57 58 59 60 61 62 63 64 65 66
    {
        RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_USART3, ENABLE);
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
        GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitCtlStruct.Pin = GPIO_PIN_10;
        GPIO_InitPeripheral(GPIOB, &GPIO_InitCtlStruct);

        GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_InitCtlStruct.Pin = GPIO_PIN_11;
        GPIO_InitPeripheral(GPIOB, &GPIO_InitCtlStruct);
    }
#endif
67
#ifdef BSP_USING_UART4
1
192.168.1.134 已提交
68
    if (UART4 == USARTx)
69 70 71 72 73 74 75 76 77 78 79 80
    {
        RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_UART4, ENABLE);
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
        GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitCtlStruct.Pin = GPIO_PIN_10;
        GPIO_InitPeripheral(GPIOB, &GPIO_InitCtlStruct);

        GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_InitCtlStruct.Pin = GPIO_PIN_11;
        GPIO_InitPeripheral(GPIOB, &GPIO_InitCtlStruct);
    }
#endif
81 82 83 84 85 86 87 88 89 90 91 92 93
    /* Add others */
}
#endif /* BSP_USING_SERIAL */

#ifdef BSP_USING_SPI
void n32_msp_spi_init(void *Instance)
{
    GPIO_InitType GPIO_InitCtlStruct;
    SPI_Module *SPIx = (SPI_Module *)Instance;

    GPIO_InitStruct(&GPIO_InitCtlStruct);
    GPIO_InitCtlStruct.GPIO_Speed = GPIO_Speed_50MHz;
#ifdef BSP_USING_SPI1
1
192.168.1.134 已提交
94
    if (SPI1 == SPIx)
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    {
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_SPI1, ENABLE);
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);

        GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_Out_PP;
        GPIO_InitCtlStruct.Pin = GPIO_PIN_4;
        GPIO_InitPeripheral(GPIOA, &GPIO_InitCtlStruct);
        GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitCtlStruct.Pin = GPIO_PIN_5 | GPIO_PIN_7;
        GPIO_InitPeripheral(GPIOA, &GPIO_InitCtlStruct);
        GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_InitCtlStruct.Pin = GPIO_PIN_6;
        GPIO_InitPeripheral(GPIOA, &GPIO_InitCtlStruct);
    }
#endif
#ifdef BSP_USING_SPI2
1
192.168.1.134 已提交
111
    if (SPI2 == SPIx)
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    {
        RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_SPI2, ENABLE);
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);

        GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_Out_PP;
        GPIO_InitCtlStruct.Pin = GPIO_PIN_12;
        GPIO_InitPeripheral(GPIOB, &GPIO_InitCtlStruct);
        GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitCtlStruct.Pin = GPIO_PIN_13 | GPIO_PIN_15;
        GPIO_InitPeripheral(GPIOB, &GPIO_InitCtlStruct);
        GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_InitCtlStruct.Pin = GPIO_PIN_14;
        GPIO_InitPeripheral(GPIOB, &GPIO_InitCtlStruct);
    }
#endif
    /* Add others */
}
#endif /* BSP_USING_SPI */

#ifdef BSP_USING_SDIO
void n32_msp_sdio_init(void *Instance)
{
    GPIO_InitType GPIO_InitCtlStructure;
    SDIO_Module *SDIOx = (SDIO_Module *)Instance;

    GPIO_InitStruct(&GPIO_InitCtlStructure);
    GPIO_InitCtlStructure.GPIO_Speed = GPIO_Speed_50MHz;

1
192.168.1.134 已提交
140
    if (SDIO == SDIOx)
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    {
        /* if used dma ... */
        RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_DMA2, ENABLE);

        RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_SDIO, ENABLE);
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC | RCC_APB2_PERIPH_GPIOD, ENABLE);
        GPIO_InitCtlStructure.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12;
        GPIO_InitCtlStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitPeripheral(GPIOC, &GPIO_InitCtlStructure);

        GPIO_InitCtlStructure.Pin = GPIO_PIN_2;
        GPIO_InitCtlStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitPeripheral(GPIOD, &GPIO_InitCtlStructure);
    }
}
#endif /* BSP_USING_SDIO */

#ifdef BSP_USING_PWM
159
void n32_msp_tim_init(void *Instance)
160 161 162 163 164
{
    GPIO_InitType GPIO_InitCtlStructure;
    GPIO_InitStruct(&GPIO_InitCtlStructure);
    TIM_Module *TIMx = (TIM_Module *)Instance;

1
192.168.1.134 已提交
165
    if (TIMx == TIM1)
166 167 168 169 170 171 172 173 174
    {
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_TIM1, ENABLE);
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
        GPIO_InitCtlStructure.Pin = GPIO_PIN_8 | GPIO_PIN_11;
        GPIO_InitCtlStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitCtlStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitPeripheral(GPIOA, &GPIO_InitCtlStructure);
    }

1
192.168.1.134 已提交
175
    if (TIMx == TIM2)
176 177 178
    {
        RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM2, ENABLE);
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
1
192.168.1.134 已提交
179
        GPIO_InitCtlStructure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3;
180 181 182 183
        GPIO_InitCtlStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitCtlStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitPeripheral(GPIOA, &GPIO_InitCtlStructure);
    }
L
linyuanbo_breo_server 已提交
184

1
192.168.1.134 已提交
185
    if (TIMx == TIM3)
186
    {
L
linyuanbo_breo_server 已提交
187
        RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM3, ENABLE);
1
192.168.1.134 已提交
188
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_GPIOB, ENABLE);
189 190
        GPIO_InitCtlStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitCtlStructure.GPIO_Speed = GPIO_Speed_50MHz;
1
192.168.1.134 已提交
191
        GPIO_InitCtlStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7;
192 193
        GPIO_InitPeripheral(GPIOA, &GPIO_InitCtlStructure);
        GPIO_InitCtlStructure.Pin = GPIO_PIN_0 | GPIO_PIN_1;
L
linyuanbo_breo_server 已提交
194
        GPIO_InitPeripheral(GPIOB, &GPIO_InitCtlStructure);
195
    }
1
192.168.1.134 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225

    if (TIMx == TIM4)
    {
        RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM4, ENABLE);
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
        GPIO_InitCtlStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitCtlStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitCtlStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9;
        GPIO_InitPeripheral(GPIOB, &GPIO_InitCtlStructure);
    }

    if (TIMx == TIM5)
    {
        RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM5, ENABLE);
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
        GPIO_InitCtlStructure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3;
        GPIO_InitCtlStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitCtlStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitPeripheral(GPIOA, &GPIO_InitCtlStructure);
    }

    if (TIMx == TIM8)
    {
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_TIM8, ENABLE);
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC, ENABLE);
        GPIO_InitCtlStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9;
        GPIO_InitCtlStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitCtlStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitPeripheral(GPIOC, &GPIO_InitCtlStructure);
    }
226 227 228 229 230 231 232 233 234 235 236
}
#endif /* BSP_USING_PWM */

#ifdef BSP_USING_ADC
void n32_msp_adc_init(void *Instance)
{
    GPIO_InitType GPIO_InitCtlStruct;
    GPIO_InitStruct(&GPIO_InitCtlStruct);
    ADC_Module *ADCx = (ADC_Module *)Instance;

#ifdef BSP_USING_ADC1
1
192.168.1.134 已提交
237
    if (ADCx == ADC1)
238 239 240
    {
        /* ADC1 & GPIO clock enable */
        RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_ADC1, ENABLE);
1
192.168.1.134 已提交
241
        ADC_ConfigClk(ADC_CTRL3_CKMOD_AHB, RCC_ADCHCLK_DIV8);
242 243 244
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC, ENABLE);

        /* Configure ADC Channel as analog input */
1
192.168.1.134 已提交
245
        GPIO_InitCtlStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3;
246 247 248 249 250 251 252
        GPIO_InitCtlStruct.GPIO_Speed = GPIO_Speed_2MHz;
        GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_AIN;
        GPIO_InitPeripheral(GPIOC, &GPIO_InitCtlStruct);
    }
#endif

#ifdef BSP_USING_ADC2
1
192.168.1.134 已提交
253
    if (ADCx == ADC2)
254 255 256
    {
        /* ADC2 & GPIO clock enable */
        RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_ADC2, ENABLE);
1
192.168.1.134 已提交
257
        ADC_ConfigClk(ADC_CTRL3_CKMOD_AHB, RCC_ADCHCLK_DIV8);
258 259 260 261 262 263 264 265 266 267 268 269 270
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC, ENABLE);

        /* Configure ADC Channel as analog input */
        GPIO_InitCtlStruct.Pin = GPIO_PIN_1;
        GPIO_InitCtlStruct.GPIO_Speed = GPIO_Speed_2MHz;
        GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_AIN;
        GPIO_InitPeripheral(GPIOC, &GPIO_InitCtlStruct);
    }
#endif
}
#endif /* BSP_USING_ADC */

#ifdef BSP_USING_HWTIMER
271
void n32_msp_hwtim_init(void *Instance)
272 273 274 275
{
    TIM_Module *TIMx = (TIM_Module *)Instance;

#ifdef BSP_USING_HWTIM3
1
192.168.1.134 已提交
276
    if (TIMx == TIM3)
277 278 279 280 281 282 283
    {
        /* TIM3 clock enable */
        RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM3, ENABLE);
    }
#endif

#ifdef BSP_USING_HWTIM4
1
192.168.1.134 已提交
284
    if (TIMx == TIM4)
285 286 287 288 289 290 291
    {
        /* TIM4 clock enable */
        RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM4, ENABLE);
    }
#endif

#ifdef BSP_USING_HWTIM5
1
192.168.1.134 已提交
292
    if (TIMx == TIM5)
293 294 295 296 297
    {
        /* TIM5 clock enable */
        RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM5, ENABLE);
    }
#endif
298 299

#ifdef BSP_USING_HWTIM6
1
192.168.1.134 已提交
300 301 302 303 304
    if (TIMx == TIM6)
    {
        /* TIM6 clock enable */
        RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM6, ENABLE);
    }
305 306 307
#endif

#ifdef BSP_USING_HWTIM7
1
192.168.1.134 已提交
308 309 310 311 312
    if (TIMx == TIM7)
    {
        /* TIM7 clock enable */
        RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM7, ENABLE);
    }
313
#endif
314 315 316 317 318 319 320 321 322 323 324 325
}
#endif

#ifdef BSP_USING_CAN
void n32_msp_can_init(void *Instance)
{
    GPIO_InitType GPIO_InitCtlStruct;
    CAN_Module *CANx = (CAN_Module *)Instance;

    GPIO_InitStruct(&GPIO_InitCtlStruct);
    GPIO_InitCtlStruct.GPIO_Speed = GPIO_Speed_50MHz;
#ifdef BSP_USING_CAN1
1
192.168.1.134 已提交
326
    if (CAN1 == CANx)
327 328 329 330 331 332 333 334 335 336 337 338 339
    {
        RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_CAN1, ENABLE);
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
        GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitCtlStruct.Pin = GPIO_PIN_12;
        GPIO_InitPeripheral(GPIOA, &GPIO_InitCtlStruct);

        GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_InitCtlStruct.Pin = GPIO_PIN_11;
        GPIO_InitPeripheral(GPIOA, &GPIO_InitCtlStruct);
    }
#endif
#ifdef BSP_USING_CAN2
1
192.168.1.134 已提交
340
    if (CAN2 == CANx)
341 342 343 344
    {
        RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_CAN2, ENABLE);
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_AFIO, ENABLE);
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
345
        //        GPIO_PinsRemapConfig(AFIO_MAP6_CAN2_0001, ENABLE);
346 347 348 349 350 351 352 353 354 355 356 357
        GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitCtlStruct.Pin = GPIO_PIN_6;
        GPIO_InitPeripheral(GPIOB, &GPIO_InitCtlStruct);

        GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_InitCtlStruct.Pin = GPIO_PIN_5;
        GPIO_InitPeripheral(GPIOB, &GPIO_InitCtlStruct);
    }
#endif
}
#endif /* BSP_USING_CAN */

358 359
#ifdef RT_USING_FINSH
#include <finsh.h>
360
#if defined(BSP_USING_UART2) || defined(BSP_USING_UART3)
361 362 363 364 365
static void uart_test_rw(rt_device_t uartx, const char *name)
{
    if (uartx == NULL)
    {
        uartx = rt_device_find(name);
1
192.168.1.134 已提交
366
        rt_err_t err = rt_device_open(uartx, RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_RX);
367 368 369 370 371 372 373 374
        RT_ASSERT(err == RT_EOK);
    }
    rt_device_write(uartx, 0, name, strlen(name));
    rt_device_write(uartx, 0, "\r\n", 2);
    uint8_t recv_buf[64] = {0x0};
    int ret = rt_device_read(uartx, 0, recv_buf, sizeof(recv_buf));
    if (ret != 0)
    {
1
192.168.1.134 已提交
375
        for (int i = 0; i < ret; ++i)
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
            rt_kprintf("[%02x]", recv_buf[i]);
    }
    rt_device_write(uartx, 0, "\r\n", 2);
}
static void uart_test(void)
{
#ifdef BSP_USING_UART2
    static rt_device_t u2 = NULL;
    uart_test_rw(u2, "uart2");
#endif
#ifdef BSP_USING_UART2
    static rt_device_t u3 = NULL;
    uart_test_rw(u3, "uart3");
#endif
}
MSH_CMD_EXPORT(uart_test, uart_test)
#endif

#ifdef BSP_USING_ADC
#ifdef BSP_USING_ADC1
1
192.168.1.134 已提交
396
    #define ADC_DEV_NAME        "adc1"
397
#else
1
192.168.1.134 已提交
398
    #define ADC_DEV_NAME        "adc2"
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
#endif
#define REFER_VOLTAGE       3300
#define CONVERT_BITS        (1 << 12)
static int adc_vol_sample(int argc, char *argv[])
{
    rt_adc_device_t adc_dev;
    rt_uint32_t value, vol;
    rt_err_t ret = RT_EOK;

    adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
    if (adc_dev == RT_NULL)
    {
        rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
        return RT_ERROR;
    }

1
192.168.1.134 已提交
415
    for (int i = 6; i <= 9; ++i)
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
    {
        ret = rt_adc_enable(adc_dev, i);
        value = rt_adc_read(adc_dev, i);
        rt_kprintf("ch=[%d] the value is :[%d] \n", i, value);
        vol = value * REFER_VOLTAGE / CONVERT_BITS;
        rt_kprintf("ch=[%d] the voltage is :[%d] \n", i, vol);
    }

    return ret;
}
MSH_CMD_EXPORT(adc_vol_sample, adc voltage convert sample);
#endif

#ifdef BSP_USING_HWTIMER
static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
{
    rt_kprintf("this is hwtimer timeout callback fucntion!\n");
    rt_kprintf("timer name is :%s.\n", dev->parent.name);
    rt_kprintf("tick is :%d !\n", rt_tick_get());

    return 0;
}
static int hwtimer_init(const char *name)
{
    rt_err_t ret = RT_EOK;
    rt_hwtimerval_t timeout_s;
    rt_device_t hw_dev = RT_NULL;
    rt_hwtimer_mode_t mode;
    hw_dev = rt_device_find(name);
    if (hw_dev == RT_NULL)
    {
        rt_kprintf("hwtimer sample run failed! can't find %s device!\n", name);
        return RT_ERROR;
    }
    ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
    if (ret != RT_EOK)
    {
        rt_kprintf("open %s device failed!\n", name);
        return ret;
    }
    rt_device_set_rx_indicate(hw_dev, timeout_cb);
    mode = HWTIMER_MODE_PERIOD;
    ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
    if (ret != RT_EOK)
    {
        rt_kprintf("set mode failed! ret is :%d\n", ret);
        return ret;
    }
    timeout_s.sec = 5;
    timeout_s.usec = 0;
    if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
    {
        rt_kprintf("set timeout value failed\n");
        return RT_ERROR;
    }

    rt_thread_mdelay(3500);

    rt_device_read(hw_dev, 0, &timeout_s, sizeof(timeout_s));
    rt_kprintf("Read: Sec = %d, Usec = %d\n", timeout_s.sec, timeout_s.usec);

    return ret;
}

static int hwtimer_sample(int argc, char *argv[])
{
#ifdef BSP_USING_HWTIM6
    hwtimer_init("timer6");
#endif
#ifdef BSP_USING_HWTIM7
    hwtimer_init("timer7");
#endif
    return RT_EOK;
}
MSH_CMD_EXPORT(hwtimer_sample, hwtimer sample);
#endif


#endif