main.c 14.5 KB
Newer Older
Z
zohar123 已提交
1
/*
Y
yanmowudi 已提交
2
 * Copyright (c) 2006-2021, RT-Thread Development Team
Z
zohar123 已提交
3 4 5 6 7 8 9 10 11
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 */

#include <rtthread.h>
#include <rtdevice.h>
Y
yanmowudi 已提交
12 13

#define LED_PIN 11
Z
zohar123 已提交
14 15 16

int main(void)
{
17 18
    int count = 1;

Y
yanmowudi 已提交
19
    rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT);
20
    while (count++)
Z
zohar123 已提交
21
    {
22
        rt_pin_write(LED_PIN, PIN_HIGH);
Y
yanmowudi 已提交
23
        rt_thread_mdelay(1000);
24
        rt_pin_write(LED_PIN, PIN_LOW);
Y
yanmowudi 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
        rt_thread_mdelay(1000);
    }

    return RT_EOK;
}

// #ifdef RT_USING_PIN
// #define KEY1_PIN 31
// void key1_cb(void *args)
// {
//     rt_kprintf("key1 irq!\n");
// }
// static int pin_sample(int argc, char *argv[])
// {
//     rt_pin_mode(KEY1_PIN, PIN_IRQ_MODE_FALLING);
//     rt_pin_attach_irq(KEY1_PIN, PIN_IRQ_MODE_FALLING, key1_cb, RT_NULL);
//     rt_pin_irq_enable(KEY1_PIN, PIN_IRQ_ENABLE);

//     return RT_EOK;
// }
// MSH_CMD_EXPORT(pin_sample, pin sample);
// #endif

#ifdef RT_USING_ADC
#define ADC_DEV_NAME "adc1"
#define ADC_DEV_CHANNEL 0
#define REFER_VOLTAGE 330
#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;
Z
zohar123 已提交
65 66
    }

Y
yanmowudi 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    ret = rt_adc_enable(adc_dev, ADC_DEV_CHANNEL);

    value = rt_adc_read(adc_dev, ADC_DEV_CHANNEL);
    rt_kprintf("the value is :%d,", value);

    vol = value * REFER_VOLTAGE / CONVERT_BITS;
    rt_kprintf("the voltage is :%d.%02d \n", vol / 100, vol % 100);

    ret = rt_adc_disable(adc_dev, ADC_DEV_CHANNEL);

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

#ifdef RT_USING_HWTIMER
#define HWTIMER_DEV_NAME "timer0"

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("tick is :%d !\n", rt_tick_get());

    return 0;
}

static int hwtimer_sample(int argc, char *argv[])
{
    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(HWTIMER_DEV_NAME);
    if (hw_dev == RT_NULL)
    {
        rt_kprintf("hwtimer sample run failed! can't find %s device!\n", HWTIMER_DEV_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", HWTIMER_DEV_NAME);
        return ret;
    }

    rt_device_set_rx_indicate(hw_dev, timeout_cb);

    mode = HWTIMER_MODE_PERIOD;
    //mode = HWTIMER_MODE_ONESHOT;
    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 = 2;
    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;
}
MSH_CMD_EXPORT(hwtimer_sample, hwtimer sample);
#endif

#ifdef RT_USING_PWM
#define PWM_DEV_NAME "pwm0" /* PWM设备名称 */
#define PWM_DEV_CHANNEL 0   /* PWM通道 */

struct rt_device_pwm *pwm_dev; /* PWM设备句柄 */

static int pwm_sample(int argc, char *argv[])
{
    rt_uint32_t period, pulse;

    period = 500000; /* 周期为0.5ms,单位为纳秒ns */
    pulse = 250000;  /* PWM脉冲宽度值,单位为纳秒ns */

    pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME);
    if (pwm_dev == RT_NULL)
    {
        rt_kprintf("pwm sample run failed! can't find %s device!\n", PWM_DEV_NAME);
        return RT_ERROR;
    }

    rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
    rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
    return RT_EOK;
}
MSH_CMD_EXPORT(pwm_sample, pwm sample);
#endif

#ifdef RT_USING_RTC
mysterywolf's avatar
mysterywolf 已提交
172
#include <sys/time.h>
Y
yanmowudi 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
static int rtc_sample(int argc, char *argv[])
{
    rt_err_t ret = RT_EOK;
    time_t now;

    ret = set_date(2020, 2, 28);
    if (ret != RT_EOK)
    {
        rt_kprintf("set RTC date failed\n");
        return ret;
    }

    ret = set_time(23, 59, 55);
    if (ret != RT_EOK)
    {
        rt_kprintf("set RTC time failed\n");
        return ret;
    }

    //rt_thread_mdelay(3000);
    now = time(RT_NULL);
    rt_kprintf("%s\n", ctime(&now));

    return ret;
}
MSH_CMD_EXPORT(rtc_sample, rtc sample);
#endif

#ifdef RT_USING_WDT
#define WDT_DEVICE_NAME "wdt"

static rt_device_t wdg_dev;

static void idle_hook(void)
{
    rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_KEEPALIVE, RT_NULL);
    rt_kprintf("feed the dog!\n ");
}

static int wdt_sample(int argc, char *argv[])
{
    rt_err_t ret = RT_EOK;
    rt_uint32_t timeout = 1;
    char device_name[RT_NAME_MAX];

    if (argc == 2)
    {
        rt_strncpy(device_name, argv[1], RT_NAME_MAX);
    }
    else
    {
        rt_strncpy(device_name, WDT_DEVICE_NAME, RT_NAME_MAX);
    }
    wdg_dev = rt_device_find(device_name);
    if (!wdg_dev)
    {
        rt_kprintf("find %s failed!\n", device_name);
        return RT_ERROR;
    }
    ret = rt_device_init(wdg_dev);
    if (ret != RT_EOK)
    {
        rt_kprintf("initialize %s failed!\n", device_name);
        return RT_ERROR;
    }
    ret = rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, &timeout);
    if (ret != RT_EOK)
    {
        rt_kprintf("set %s timeout failed!\n", device_name);
        return RT_ERROR;
    }
    ret = rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_START, RT_NULL);
    if (ret != RT_EOK)
    {
        rt_kprintf("start %s failed!\n", device_name);
        return -RT_ERROR;
    }
    // rt_thread_idle_sethook(idle_hook);

    return ret;
}
MSH_CMD_EXPORT(wdt_sample, wdt sample);
#endif

#ifdef RT_USING_SPI
#define W25Q_SPI_DEVICE_NAME "spi00"
#define W25Q_FLASH_NAME "norflash0"

#include "drv_spi.h"
#include "spi_flash_sfud.h"
#include "dfs_posix.h"

static int rt_hw_spi_flash_init(void)
{
    rt_hw_spi_device_attach("spi0", "spi00", GPIOP, PIN22);

    if (RT_NULL == rt_sfud_flash_probe(W25Q_FLASH_NAME, W25Q_SPI_DEVICE_NAME))
    {
        return -RT_ERROR;
    };

274
    return RT_EOK;
Z
zohar123 已提交
275
}
Y
yanmowudi 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 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 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
INIT_COMPONENT_EXPORT(rt_hw_spi_flash_init);

static void spi_w25q_sample(int argc, char *argv[])
{
    struct rt_spi_device *spi_dev_w25q;
    char name[RT_NAME_MAX];
    rt_uint8_t w25x_read_id = 0x90;
    rt_uint8_t id[5] = {0};

    if (argc == 2)
    {
        rt_strncpy(name, argv[1], RT_NAME_MAX);
    }
    else
    {
        rt_strncpy(name, W25Q_SPI_DEVICE_NAME, RT_NAME_MAX);
    }

    /* 查找 spi 设备获取设备句柄 */
    spi_dev_w25q = (struct rt_spi_device *)rt_device_find(name);
    struct rt_spi_configuration cfg;
    cfg.data_width = 8;
    cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB;
    cfg.max_hz = 30 * 1000 * 1000; /* 20M */

    rt_spi_configure(spi_dev_w25q, &cfg);
    if (!spi_dev_w25q)
    {
        rt_kprintf("spi sample run failed! can't find %s device!\n", name);
    }
    else
    {
        /* 方式1:使用 rt_spi_send_then_recv()发送命令读取ID */
        rt_spi_send_then_recv(spi_dev_w25q, &w25x_read_id, 1, id, 5);
        rt_kprintf("use rt_spi_send_then_recv() read w25q ID is:%x%x\n", id[3], id[4]);

        /* 方式2:使用 rt_spi_transfer_message()发送命令读取ID */
        struct rt_spi_message msg1, msg2;

        msg1.send_buf = &w25x_read_id;
        msg1.recv_buf = RT_NULL;
        msg1.length = 1;
        msg1.cs_take = 1;
        msg1.cs_release = 0;
        msg1.next = &msg2;

        msg2.send_buf = RT_NULL;
        msg2.recv_buf = id;
        msg2.length = 5;
        msg2.cs_take = 0;
        msg2.cs_release = 1;
        msg2.next = RT_NULL;

        rt_spi_transfer_message(spi_dev_w25q, &msg1);
        rt_kprintf("use rt_spi_transfer_message() read w25q ID is:%x%x\n", id[3], id[4]);
    }
}
static void spi_flash_elmfat_sample(void)
{
    int fd, size;
    struct statfs elm_stat;
    char str[] = "elmfat mount to W25Q flash.\r\n", buf[80];

    if (dfs_mkfs("elm", W25Q_FLASH_NAME) == 0)
        rt_kprintf("make elmfat filesystem success.\n");

    if (dfs_mount(W25Q_FLASH_NAME, "/", "elm", 0, 0) == 0)
        rt_kprintf("elmfat filesystem mount success.\n");

    if (statfs("/", &elm_stat) == 0)
        rt_kprintf("elmfat filesystem block size: %d, total blocks: %d, free blocks: %d.\n",
                   elm_stat.f_bsize, elm_stat.f_blocks, elm_stat.f_bfree);

    if (mkdir("/user", 0x777) == 0)
        rt_kprintf("make a directory: '/user'.\n");

    rt_kprintf("Write string '%s' to /user/test.txt.\n", str);

    fd = open("/user/test.txt", O_WRONLY | O_CREAT);
    if (fd >= 0)
    {
        if (write(fd, str, sizeof(str)) == sizeof(str))
            rt_kprintf("Write data done.\n");

        close(fd);
    }

    fd = open("/user/test.txt", O_RDONLY);
    if (fd >= 0)
    {
        size = read(fd, buf, sizeof(buf));

        close(fd);

        if (size == sizeof(str))
            rt_kprintf("Read data from file test.txt(size: %d): %s \n", size, buf);
    }
}
MSH_CMD_EXPORT(spi_flash_elmfat_sample, spi flash elmfat sample);
MSH_CMD_EXPORT(spi_w25q_sample, spi w25q sample);
#endif

//#ifdef RT_USING_SPI
//#define SD_SPI_DEVICE_NAME "spi10"
//#define SDCARD_NAME "sd0"

//#include "drv_spi.h"
//#include "dfs_posix.h"
//#include "spi_msd.h"

//static int rt_hw_spi1_tfcard(void)
//{
//    rt_hw_spi_device_attach("spi1", SD_SPI_DEVICE_NAME, GPIOB, PIN6);
//    return msd_init(SDCARD_NAME, SD_SPI_DEVICE_NAME);
//}
//INIT_DEVICE_EXPORT(rt_hw_spi1_tfcard);

//static void elmfat_sample(void)
//{
//    int fd, size;
//    struct statfs elm_stat;
//    char str[] = "elmfat mount to sdcard.", buf[80];

//    if (dfs_mkfs("elm", SDCARD_NAME) == 0)
//        rt_kprintf("make elmfat filesystem success.\n");

//    if (dfs_mount(SDCARD_NAME, "/", "elm", 0, 0) == 0)
//        rt_kprintf("elmfat filesystem mount success.\n");

//    if (statfs("/", &elm_stat) == 0)
//        rt_kprintf("elmfat filesystem block size: %d, total blocks: %d, free blocks: %d.\n",
//                   elm_stat.f_bsize, elm_stat.f_blocks, elm_stat.f_bfree);

//    if (mkdir("/user", 0x777) == 0)
//        rt_kprintf("make a directory: '/user'.\n");

//    rt_kprintf("Write string '%s' to /user/test.txt.\n", str);

//    fd = open("/user/test.txt", O_WRONLY | O_CREAT);
//    if (fd >= 0)
//    {
//        if (write(fd, str, sizeof(str)) == sizeof(str))
//            rt_kprintf("Write data done.\n");

//        close(fd);
//    }

//    fd = open("/user/test.txt", O_RDONLY);
//    if (fd >= 0)
//    {
//        size = read(fd, buf, sizeof(buf));

//        close(fd);

//        if (size == sizeof(str))
//            rt_kprintf("Read data from file test.txt(size: %d): %s \n", size, buf);
//    }
//}
//MSH_CMD_EXPORT(elmfat_sample, elmfat sample);
//#endif

#ifdef RT_USING_SDIO
#define SDCARD_NAME "sd0"

#include "dfs_posix.h"

static void sdio_elmfat_sample(void)
{
    int fd, size;
    struct statfs elm_stat;
    char str[] = "elmfat mount to sdcard.\n", buf[80];

    if (dfs_mkfs("elm", SDCARD_NAME) == 0)
        rt_kprintf("make elmfat filesystem success.\n");

    if (dfs_mount(SDCARD_NAME, "/", "elm", 0, 0) == 0)
        rt_kprintf("elmfat filesystem mount success.\n");

    if (statfs("/", &elm_stat) == 0)
        rt_kprintf("elmfat filesystem block size: %d, total blocks: %d, free blocks: %d.\n",
                   elm_stat.f_bsize, elm_stat.f_blocks, elm_stat.f_bfree);

    if (mkdir("/user", 0x777) == 0)
        rt_kprintf("make a directory: '/user'.\n");

    rt_kprintf("Write string '%s' to /user/test.txt.\n", str);

    fd = open("/user/test.txt", O_WRONLY | O_CREAT);
    if (fd >= 0)
    {
        if (write(fd, str, sizeof(str)) == sizeof(str))
            rt_kprintf("Write data done.\n");

        close(fd);
    }

    fd = open("/user/test.txt", O_RDONLY);
    if (fd >= 0)
    {
        size = read(fd, buf, sizeof(buf));

        close(fd);

        if (size == sizeof(str))
            rt_kprintf("Read data from file test.txt(size: %d): %s \n", size, buf);
    }
}
MSH_CMD_EXPORT(sdio_elmfat_sample, sdio elmfat sample);
#endif

#ifdef RT_USING_HWCRYPTO
static void crypto_sample(void)
{
    rt_uint8_t temp[] = {0, 1, 2, 3, 4, 5, 6, 7};
    struct rt_hwcrypto_ctx *ctx;
    rt_uint32_t result = 0;
    struct hwcrypto_crc_cfg cfg =
        {
            .last_val = 0x0,
            .poly = 0x04C11DB7,
            .width = 8,
            .xorout = 0x00000000,
            .flags = 0,
        };

    ctx = rt_hwcrypto_crc_create(rt_hwcrypto_dev_default(), HWCRYPTO_CRC_CRC32);
    rt_hwcrypto_crc_cfg(ctx, &cfg);
    result = rt_hwcrypto_crc_update(ctx, temp, sizeof(temp));
    rt_kprintf("result: 0x%08x \n", result);
    rt_hwcrypto_crc_destroy(ctx);
}
MSH_CMD_EXPORT(crypto_sample, crypto sample);
#endif

#ifdef BSP_USING_NOR_FLASH
#define NORFLASH_DEV_NAME "nor"
static int norflash_sample(int argc, char *argv[])
{
    rt_err_t ret = RT_EOK;
    rt_device_t hw_dev = RT_NULL;

    hw_dev = rt_device_find(NORFLASH_DEV_NAME);
    if (hw_dev == RT_NULL)
    {
        rt_kprintf("norflash sample run failed! can't find %s device!\n", NORFLASH_DEV_NAME);
        return RT_ERROR;
    }
    else
    {
        rt_kprintf("norflash sample run success! find %s device!\n", NORFLASH_DEV_NAME);
    }

    ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
    if (ret != RT_EOK)
    {
        rt_kprintf("open %s device failed!\n", NORFLASH_DEV_NAME);
        return ret;
    }
    else
    {
        rt_kprintf("open %s device success!\n", NORFLASH_DEV_NAME);
    }

    struct rt_mtd_nor_device *hw_nor;
    hw_nor = RT_MTD_NOR_DEVICE(hw_dev);
    long id = hw_nor->ops->read_id(hw_nor);
    rt_kprintf("id = %08x!\n", id);
    // rt_device_set_rx_indicate(hw_dev, timeout_cb);

    // mode = HWTIMER_MODE_PERIOD;
    // //mode = HWTIMER_MODE_ONESHOT;
    // 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 = 2;
    // 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;
}
MSH_CMD_EXPORT(norflash_sample, norflash sample);
#endif