Skip to content

Commit 3d30d3f

Browse files
committed
pbio/drv/block_device: Add test driver.
This can be used for new ports that should have pbsys enabled but don't have program download implemented yet.
1 parent db107e1 commit 3d30d3f

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

bricks/_common/sources.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ PBIO_SRC_C = $(addprefix lib/pbio/,\
120120
drv/battery/battery_test.c \
121121
drv/battery/battery_virtual.c \
122122
drv/block_device/block_device_flash_stm32.c \
123+
drv/block_device/block_device_test.c \
123124
drv/block_device/block_device_w25qxx_stm32.c \
124125
drv/bluetooth/bluetooth_btstack_control_gpio.c \
125126
drv/bluetooth/bluetooth_btstack_run_loop_contiki.c \
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// SPDX-License-Identifier: MIT
2+
// Copyright (c) 2024 The Pybricks Authors
3+
4+
// Block device dummy driver with a simple program to simplify making new ports.
5+
6+
#include <pbdrv/config.h>
7+
8+
#if PBDRV_CONFIG_BLOCK_DEVICE_TEST
9+
10+
#include <stdint.h>
11+
#include <string.h>
12+
13+
#include <contiki.h>
14+
15+
#include <pbdrv/block_device.h>
16+
17+
/**
18+
* The following script is compiled using pybricksdev compile hello.py
19+
* in MULTI_MPY_V6.
20+
*
21+
* from pybricks.tools import wait
22+
*
23+
* print("Hello!")
24+
* wait(1000)
25+
* print("World!")
26+
* wait(1000)
27+
*
28+
*/
29+
const uint8_t script[] = {
30+
0x6E, 0x00, 0x00, 0x00, 0x5F, 0x5F, 0x6D, 0x61,
31+
0x69, 0x6E, 0x5F, 0x5F, 0x00, 0x4D, 0x06, 0x00,
32+
0x1F, 0x07, 0x00, 0x10, 0x68, 0x65, 0x6C, 0x6C,
33+
0x6F, 0x2E, 0x70, 0x79, 0x00, 0x0F, 0x08, 0x77,
34+
0x61, 0x69, 0x74, 0x00, 0x1C, 0x70, 0x79, 0x62,
35+
0x72, 0x69, 0x63, 0x6B, 0x73, 0x2E, 0x74, 0x6F,
36+
0x6F, 0x6C, 0x73, 0x00, 0x0C, 0x48, 0x65, 0x6C,
37+
0x6C, 0x6F, 0x21, 0x00, 0x0C, 0x57, 0x6F, 0x72,
38+
0x6C, 0x64, 0x21, 0x00, 0x81, 0x77, 0x83, 0x18,
39+
0x08, 0x0A, 0x01, 0x4C, 0x27, 0x28, 0x27, 0x80,
40+
0x10, 0x02, 0x2A, 0x01, 0x1B, 0x03, 0x1C, 0x02,
41+
0x16, 0x02, 0x59, 0x11, 0x06, 0x10, 0x04, 0x34,
42+
0x01, 0x59, 0x11, 0x02, 0x22, 0x87, 0x68, 0x34,
43+
0x01, 0x59, 0x11, 0x06, 0x10, 0x05, 0x34, 0x01,
44+
0x59, 0x11, 0x02, 0x22, 0x87, 0x68, 0x34, 0x01,
45+
0x59, 0x51, 0x63,
46+
};
47+
48+
#define MAX_PROGRAM_SIZE ((PBDRV_CONFIG_BLOCK_DEVICE_TEST_SIZE)-(PBDRV_CONFIG_BLOCK_DEVICE_TEST_SIZE_USER)-sizeof(uint32_t) * 2)
49+
50+
/**
51+
* Mimics data structure expected by pbsys.
52+
*/
53+
static struct {
54+
uint32_t write_size;
55+
uint8_t user_data[PBDRV_CONFIG_BLOCK_DEVICE_TEST_SIZE_USER];
56+
uint32_t program_size;
57+
uint8_t program_data[MAX_PROGRAM_SIZE] __attribute__((aligned(sizeof(void *))));
58+
} blockdev = { 0 };
59+
60+
void pbdrv_block_device_init(void) {
61+
blockdev.program_size = sizeof(script);
62+
memcpy(blockdev.program_data, script, sizeof(script));
63+
blockdev.write_size = sizeof(blockdev) - sizeof(blockdev.program_data) + blockdev.program_size;
64+
}
65+
66+
PT_THREAD(pbdrv_block_device_read(struct pt *pt, uint32_t offset, uint8_t *buffer, uint32_t size, pbio_error_t *err)) {
67+
68+
PT_BEGIN(pt);
69+
70+
// Exit on invalid size.
71+
if (size == 0 || offset + size > PBDRV_CONFIG_BLOCK_DEVICE_TEST_SIZE) {
72+
*err = PBIO_ERROR_INVALID_ARG;
73+
PT_EXIT(pt);
74+
}
75+
76+
// Copy requested data to RAM.
77+
memcpy(buffer, (uint8_t *)&blockdev + offset, size);
78+
*err = PBIO_SUCCESS;
79+
80+
PT_END(pt);
81+
}
82+
83+
// Don't store any data in this implementation.
84+
PT_THREAD(pbdrv_block_device_store(struct pt *pt, uint8_t *buffer, uint32_t size, pbio_error_t *err)) {
85+
PT_BEGIN(pt);
86+
*err = PBIO_SUCCESS;
87+
PT_END(pt);
88+
}
89+
90+
#endif // PBDRV_CONFIG_BLOCK_DEVICE_FLASH_STM32

0 commit comments

Comments
 (0)