Skip to content

Commit

Permalink
[update][example] update rtc/uart/usbmsc demo
Browse files Browse the repository at this point in the history
  • Loading branch information
sakumisue committed Dec 8, 2023
1 parent 81b4d94 commit 7f697ef
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 18 deletions.
23 changes: 19 additions & 4 deletions examples/peripherals/rtc/main.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
#include "bflb_mtimer.h"
#include "bflb_rtc.h"
#include "board.h"

struct bflb_device_s *rtc;

extern void board_init(void);
struct bflb_tm g_time;

int main(void)
{
board_init();

rtc = bflb_device_get_by_name("rtc");

bflb_rtc_set_time(rtc, BFLB_RTC_SEC2TIME(1));
bflb_rtc_set_time(rtc, 0);

/* 2023-11-27, 10:2:1, Monday */
g_time.tm_sec = 1;
g_time.tm_min = 2;
g_time.tm_hour = 10;
g_time.tm_wday = 1;
g_time.tm_mday = 27;
g_time.tm_mon = 10;
g_time.tm_year = 2023 - 1900;
bflb_rtc_set_utc_time(&g_time);
while (1) {
printf("time:%lld\r\n", BFLB_RTC_TIME2SEC(bflb_rtc_get_time(rtc)));
bflb_mtimer_delay_ms(2000);
bflb_rtc_get_utc_time(&g_time);
printf("utc time:%u-%u-%u, %u:%u:%u, wday:%u\r\n",
g_time.tm_year + 1900, g_time.tm_mon + 1, g_time.tm_mday,
g_time.tm_hour, g_time.tm_min, g_time.tm_sec,
g_time.tm_wday);
bflb_mtimer_delay_ms(5000);
}
}
18 changes: 10 additions & 8 deletions examples/peripherals/uart/uart_end_interrupt/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

struct bflb_device_s *uartx;

#define RX_BUFF_SIZE 128
static uint8_t uart_txbuf[128] = { 0 };
static uint8_t uart_rxbuf[128] = { };
static uint8_t rx_int_flag = 0;
static uint8_t uart_rxbuf[RX_BUFF_SIZE] = { 0 };
static volatile uint8_t tx_end_flag = 0;
static uint8_t data_count = 0;

Expand All @@ -15,7 +15,6 @@ void uart_isr(int irq, void *arg)
uint32_t intstatus = bflb_uart_get_intstatus(uartx);

if (intstatus & UART_INTSTS_RX_FIFO) {
rx_int_flag++;
while (bflb_uart_rxavailable(uartx)) {
uart_rxbuf[data_count++] = bflb_uart_getchar(uartx);
}
Expand Down Expand Up @@ -61,7 +60,7 @@ int main(void)

/* use tx end must stop tx freerun */
bflb_uart_feature_control(uartx, UART_CMD_SET_TX_FREERUN, false);

bflb_uart_feature_control(uartx, UART_CMD_SET_RX_TRANSFER_LEN, 64);
bflb_uart_feature_control(uartx, UART_CMD_SET_TX_END_INTERRUPT, true);
bflb_uart_feature_control(uartx, UART_CMD_SET_RX_END_INTERRUPT, true);
Expand All @@ -79,11 +78,14 @@ int main(void)
tx_end_flag = 0;
bflb_uart_feature_control(uartx, UART_CMD_SET_TX_EN, false);
bflb_mtimer_delay_ms(100);
if(rx_int_flag) {
rx_int_flag = 0;
for (uint8_t j = 0; j < data_count; j++){
printf("receive data:%02x\r\n",uart_rxbuf[j]);
if (data_count) {
if (data_count > RX_BUFF_SIZE) {
data_count = RX_BUFF_SIZE;
}
for (uint8_t j = 0; j < data_count; j++) {
printf("receive data:%02x\r\n", uart_rxbuf[j]);
}
data_count = 0;
}
}
}
19 changes: 15 additions & 4 deletions examples/peripherals/uart/uart_fifo_interrupt/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@

struct bflb_device_s *uartx;

#define RX_BUFF_SIZE 1024
static uint8_t uart_txbuf[128] = { 0 };
static uint8_t uart_rxbuf[RX_BUFF_SIZE] = { 0 };
static uint32_t uart_rx_count = 0;

void uart_isr(int irq, void *arg)
{
uint32_t intstatus = bflb_uart_get_intstatus(uartx);

if (intstatus & UART_INTSTS_RX_FIFO) {
printf("rx fifo\r\n");
while (bflb_uart_rxavailable(uartx)) {
printf("0x%02x\r\n", bflb_uart_getchar(uartx));
uart_rxbuf[uart_rx_count++] = bflb_uart_getchar(uartx);
}
}
if (intstatus & UART_INTSTS_RTO) {
bflb_uart_int_clear(uartx, UART_INTCLR_RTO);
printf("rto\r\n");
while (bflb_uart_rxavailable(uartx)) {
printf("0x%02x\r\n", bflb_uart_getchar(uartx));
uart_rxbuf[uart_rx_count++] = bflb_uart_getchar(uartx);
}
}
if (intstatus & UART_INTSTS_TX_FIFO) {
Expand Down Expand Up @@ -60,6 +62,15 @@ int main(void)
bflb_irq_enable(uartx->irq_num);

while (1) {
bflb_mtimer_delay_ms(2000);
if (uart_rx_count > RX_BUFF_SIZE) {
uart_rx_count = RX_BUFF_SIZE;
}
if (uart_rx_count) {
for (uint32_t i = 0; i < uart_rx_count; i++) {
printf("0x%02x\r\n", uart_rxbuf[i]);
}
uart_rx_count = 0;
}
bflb_mtimer_delay_ms(100);
}
}
50 changes: 49 additions & 1 deletion examples/peripherals/usbdev/usbd_msc_ram/msc_ram_template.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,43 @@ void usbd_event_handler(uint8_t event)
}
}

#ifdef CONFIG_BSP_SDH_SDCARD
#include "sdh_sdcard.h"

static sd_card_t gSDCardInfo;

void usbd_msc_get_cap(uint8_t lun, uint32_t *block_num, uint16_t *block_size)
{
*block_num = gSDCardInfo.blockCount; //Pretend having so many buffer,not has actually.
*block_size = gSDCardInfo.blockSize;
}

int usbd_msc_sector_read(uint32_t sector, uint8_t *buffer, uint32_t length)
{
if (SD_OK == SDH_ReadMultiBlocks(buffer, sector, gSDCardInfo.blockSize, length / gSDCardInfo.blockSize)) {
return 0;
} else {
return -1;
}
}

int usbd_msc_sector_write(uint32_t sector, uint8_t *buffer, uint32_t length)
{
status_t ret;

_retry:
ret = SDH_WriteMultiBlocks((uint8_t *)buffer, sector, gSDCardInfo.blockSize, length / gSDCardInfo.blockSize);

if (Status_Success == ret) {
return 0;
} else if (Status_Timeout == ret) {
goto _retry;
} else {
return -1;
}
}

#else
#define BLOCK_SIZE 512
#define BLOCK_COUNT 10

Expand All @@ -132,7 +169,7 @@ BLOCK_TYPE mass_block[BLOCK_COUNT];

void usbd_msc_get_cap(uint8_t lun, uint32_t *block_num, uint16_t *block_size)
{
*block_num = 1000; //Pretend having so many buffer,not has actually.
*block_num = 100000; //Pretend having so many buffer,not has actually.
*block_size = BLOCK_SIZE;
}
int usbd_msc_sector_read(uint32_t sector, uint8_t *buffer, uint32_t length)
Expand All @@ -148,11 +185,22 @@ int usbd_msc_sector_write(uint32_t sector, uint8_t *buffer, uint32_t length)
memcpy(mass_block[sector].BlockSpace, buffer, length);
return 0;
}
#endif

struct usbd_interface intf0;

void msc_ram_init(void)
{
#ifdef CONFIG_BSP_SDH_SDCARD
board_sdh_gpio_init();
if (SDH_Init(SDH_DATA_BUS_WIDTH_4BITS, &gSDCardInfo) == SD_OK) {
} else {
printf("sdh init fail\r\n");
while (1) {}
}

printf("block_num:%d,block_size:%d\r\n", gSDCardInfo.blockCount, gSDCardInfo.blockSize);
#endif
usbd_desc_register(msc_ram_descriptor);
usbd_add_interface(usbd_msc_init_intf(&intf0, MSC_OUT_EP, MSC_IN_EP));

Expand Down
3 changes: 2 additions & 1 deletion examples/peripherals/usbdev/usbd_msc_ram/proj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ set(CONFIG_CHERRYUSB_DEVICE_CDC 1)
set(CONFIG_CHERRYUSB_DEVICE_HID 1)
set(CONFIG_CHERRYUSB_DEVICE_MSC 1)
set(CONFIG_CHERRYUSB_DEVICE_AUDIO 1)
set(CONFIG_CHERRYUSB_DEVICE_VIDEO 1)
set(CONFIG_CHERRYUSB_DEVICE_VIDEO 1)
# set(CONFIG_BSP_SDH_SDCARD 1)

0 comments on commit 7f697ef

Please sign in to comment.