-
Notifications
You must be signed in to change notification settings - Fork 0
/
addacmain.c
135 lines (115 loc) · 3.99 KB
/
addacmain.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
65
66
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
#include <stdio.h>
#include "pico/stdio_usb.h"
#include "pico/stdlib.h"
#include "pico/multicore.h"
#include "hardware/pwm.h"
#include "pinassigns.h"
#include "core1.h"
#include "readln.h"
// on-board LED will blink by a peculiar stype to notify this code working.
static uint ledgpio = 25; // GPIO number of LED on board
// call back for a repeating timer to blink LED
static bool timercb(repeating_timer_t *rt);
static uint slice;
static uint chan;
static void initCore0(void);
static void core0loop(void);
int main(void)
{
stdio_init_all();
//stdio_set_translate_crlf(&stdio_usb, true);
initCore0();
multicore_launch_core1(core1loop);
core0loop();
}
// core0 handles PWM output
static void initCore0(void)
{
gpio_set_function(pwmpin, GPIO_FUNC_PWM);
slice = pwm_gpio_to_slice_num(pwmpin);
chan = pwm_gpio_to_channel(pwmpin);
pwm_set_wrap(slice, wrap);
pwm_set_chan_level(slice, chan, pwmpin);
pwm_set_phase_correct(slice, false); // no phase adjust
// pwm_set_clkdiv(slice, 2.0f);
gpio_init(ledgpio);
gpio_set_dir(ledgpio, true);
}
static void core0loop(void) {
initReadln();
pwm_set_enabled(slice, true);
struct repeating_timer rt;
uint32_t onoff = 0;
add_repeating_timer_ms(100, timercb, &onoff, &rt);
uint32_t count = 0;
int32_t lastms = 0;
bool running = false;
bool guiMode = true;
while (true) {
char *bp;
bool yn;
if ((bp = readln()) != NULL) {
if (guiMode)
printf("\nread one line: %s\n", bp);
int32_t arg = uintFromHex(bp + 1);
if (isForADC(*bp)) {
if (arg >= 0)
yn = multicore_fifo_push_timeout_us((uint32_t)arg, 0);
else if (guiMode) {
printf("\n%s command needs argument\n");
}
}
else if (isForPWM(*bp)) {
if (arg >= 0)
pwm_set_chan_level(slice, chan, (uint16_t)arg);
else if (guiMode) {
printf("\n%s command needs argument\n");
}
}
else if (isForStart(*bp)) {
running = true;
}
else if (isForStop(*bp)) {
running = false;
}
else if (isForGUI(*bp)) {
guiMode = true;
}
else if (isForAppl(*bp)) {
guiMode = false;
}
else if (guiMode)
printf("\nInvalid command line: %s\n", bp);
}
uint32_t out;
uifl uf;
if (multicore_fifo_pop_timeout_us(0, &out) && running) {
// if not running, incomming fifo data are just ignored
uf.ui = out;
uint32_t mxmn = multicore_fifo_pop_blocking();
uint32_t dataCount = multicore_fifo_pop_blocking();
uint32_t errorCount = multicore_fifo_pop_blocking();
uint32_t max = (mxmn >> 16) & 0xFFFF;
uint32_t min = mxmn & 0xFFFF;
absolute_time_t at = get_absolute_time();
uint32_t ms = to_ms_since_boot(at);
if (guiMode)
/*printf("A/D readout:0x%03X [0x%03X-0x%03X E:%05x] priod:%4dmsec [%6d:%7.3f sec]\r",
out, min, max, errorCount, ms - lastms, count, ms * 0.001);*/
printf("A/D readout:%9.4f [0x%03X-0x%03X E:%05x] priod:%4dmsec [%6d:%7.3f sec]\r",
uf.fl, min, max, errorCount, ms - lastms, count, ms * 0.001);
else
printf("%9.4f, %u, %u, %u, %u, %u\n",
uf.fl, min, max, errorCount, ms, dataCount);
lastms = ms;
count ++;
}
}
}
static bool timercb(repeating_timer_t *rt) {
uint32_t *onoff = (uint32_t *)(rt->user_data);
bool nf = (((*onoff) & 0x0008) != 0)
&& (((*onoff) & 0x0001) != 0);
gpio_put(ledgpio, nf);
(*onoff) ++;
}