Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f6dfed0

Browse filesBrowse files
shenkimithro
authored andcommitted
lm32: Add leds module
This lets micropython control the LEDs >>> import litex >>> led1 = litex.LED(1) >>> led1 LED(1) >>> led1.on() >>> led1.off() >>> led1.read() False >>> led1.on() >>> led1.read() False Signed-off-by: Joel Stanley <joel@jms.id.au>
1 parent e341311 commit f6dfed0
Copy full SHA for f6dfed0

File tree

Expand file treeCollapse file tree

3 files changed

+94
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+94
-0
lines changed
Open diff view settings
Collapse file

‎lm32/Makefile‎

Copy file name to clipboardExpand all lines: lm32/Makefile
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ SRC_C = \
2929
isr.c \
3030
modmachine.c \
3131
modlitex.c \
32+
litex_leds.c \
3233
lib/utils/stdout_helpers.c \
3334
lib/utils/interrupt_char.c \
3435
lib/utils/pyexec.c \
Collapse file

‎lm32/litex_leds.c‎

Copy file name to clipboard
+89Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
2+
#include "py/nlr.h"
3+
#include "py/obj.h"
4+
#include "py/runtime.h"
5+
#include "py/objexcept.h"
6+
7+
#include "generated/csr.h"
8+
9+
const mp_obj_type_t litex_led_type;
10+
11+
typedef struct _litex_led_obj_t {
12+
mp_obj_base_t base;
13+
int num;
14+
} litex_led_obj_t;
15+
16+
STATIC litex_led_obj_t litex_leds[8] = {
17+
{{&litex_led_type}, 1},
18+
{{&litex_led_type}, 2},
19+
{{&litex_led_type}, 3},
20+
{{&litex_led_type}, 4},
21+
{{&litex_led_type}, 5},
22+
{{&litex_led_type}, 6},
23+
{{&litex_led_type}, 7},
24+
{{&litex_led_type}, 8}
25+
};
26+
27+
STATIC mp_obj_t litex_led_make_new(const mp_obj_type_t *type_in,
28+
size_t n_args, size_t n_kw, const mp_obj_t *args) {
29+
mp_arg_check_num(n_args, n_kw, 1, 1, false);
30+
31+
mp_uint_t led_num = mp_obj_get_int(args[0]);
32+
33+
switch (led_num) {
34+
case 1 ... 8:
35+
return &litex_leds[led_num - 1];
36+
default:
37+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
38+
"not a valid LED number: %d", led_num));
39+
}
40+
}
41+
42+
void litex_led_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
43+
litex_led_obj_t *self = self_in;
44+
mp_printf(print, "LED(%u)", self->num);
45+
}
46+
47+
STATIC mp_obj_t litex_led_read(mp_obj_t self_in) {
48+
litex_led_obj_t *led = self_in;
49+
bool state = cas_leds_out_read() & (led->num - 1);
50+
51+
return mp_obj_new_bool(state);
52+
}
53+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(litex_led_read_obj, litex_led_read);
54+
55+
STATIC mp_obj_t litex_led_on(mp_obj_t self_in) {
56+
litex_led_obj_t *led = self_in;
57+
char value = cas_leds_out_read();
58+
59+
cas_leds_out_write(value | (1 << (led->num - 1)));
60+
61+
return mp_const_none;
62+
}
63+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(litex_led_on_obj, litex_led_on);
64+
65+
STATIC mp_obj_t litex_led_off(mp_obj_t self_in) {
66+
litex_led_obj_t *led = self_in;
67+
char value = cas_leds_out_read();
68+
69+
cas_leds_out_write(value & ~(1 << (led->num - 1)));
70+
71+
return mp_const_none;
72+
}
73+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(litex_led_off_obj, litex_led_off);
74+
75+
76+
STATIC const mp_map_elem_t litex_led_locals_dict_table[] = {
77+
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&litex_led_read_obj },
78+
{ MP_OBJ_NEW_QSTR(MP_QSTR_on), (mp_obj_t)&litex_led_on_obj },
79+
{ MP_OBJ_NEW_QSTR(MP_QSTR_off), (mp_obj_t)&litex_led_off_obj },
80+
};
81+
STATIC MP_DEFINE_CONST_DICT(litex_led_locals_dict, litex_led_locals_dict_table);
82+
83+
const mp_obj_type_t litex_led_type = {
84+
{ &mp_type_type },
85+
.name = MP_QSTR_LED,
86+
.print = litex_led_print,
87+
.make_new = litex_led_make_new,
88+
.locals_dict = (mp_obj_t)&litex_led_locals_dict,
89+
};
Collapse file

‎lm32/modlitex.c‎

Copy file name to clipboardExpand all lines: lm32/modlitex.c
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#include "py/obj.h"
22

3+
extern const mp_obj_type_t litex_led_type;
4+
35
STATIC const mp_rom_map_elem_t litex_module_globals_table[] = {
46
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_litex) },
7+
8+
{ MP_ROM_QSTR(MP_QSTR_LED), MP_ROM_PTR(&litex_led_type) },
59
};
610

711
STATIC MP_DEFINE_CONST_DICT(litex_module_globals, litex_module_globals_table);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.