Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
embedded-systems
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
James Immanuel Magsino
embedded-systems
Commits
84bf82db
Commit
84bf82db
authored
6 months ago
by
jamesimmanuel.magsino@stud.h-da.de
Browse files
Options
Downloads
Patches
Plain Diff
improving percentage calculation
parent
1eb61c88
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
pico-sdk/src/rp2_common/pico_clib_interface/newlib_interface.c
+60
-34
60 additions, 34 deletions
...sdk/src/rp2_common/pico_clib_interface/newlib_interface.c
src/main.c
+18
-6
18 additions, 6 deletions
src/main.c
with
78 additions
and
40 deletions
pico-sdk/src/rp2_common/pico_clib_interface/newlib_interface.c
+
60
−
34
View file @
84bf82db
...
@@ -33,21 +33,25 @@
...
@@ -33,21 +33,25 @@
extern
char
__StackLimit
;
/* Set by linker. */
extern
char
__StackLimit
;
/* Set by linker. */
#define STDIO_HANDLE_STDIN
0
#define STDIO_HANDLE_STDIN 0
#define STDIO_HANDLE_STDOUT 1
#define STDIO_HANDLE_STDOUT 1
#define STDIO_HANDLE_STDERR 2
#define STDIO_HANDLE_STDERR 2
void
__attribute__
((
noreturn
))
__weak
_exit
(
__unused
int
status
)
{
void
__attribute__
((
noreturn
))
__weak
_exit
(
__unused
int
status
)
{
#if PICO_ENTER_USB_BOOT_ON_EXIT
#if PICO_ENTER_USB_BOOT_ON_EXIT
reset_usb_boot
(
0
,
0
);
reset_usb_boot
(
0
,
0
);
#else
#else
while
(
1
)
{
while
(
1
)
{
__breakpoint
();
__breakpoint
();
}
}
#endif
#endif
}
}
__weak
void
*
_sbrk
(
int
incr
)
{
__weak
void
*
_sbrk
(
int
incr
)
{
extern
char
end
;
/* Set by linker. */
extern
char
end
;
/* Set by linker. */
static
char
*
heap_end
;
static
char
*
heap_end
;
char
*
prev_heap_end
;
char
*
prev_heap_end
;
...
@@ -58,26 +62,30 @@ __weak void *_sbrk(int incr) {
...
@@ -58,26 +62,30 @@ __weak void *_sbrk(int incr) {
prev_heap_end
=
heap_end
;
prev_heap_end
=
heap_end
;
char
*
next_heap_end
=
heap_end
+
incr
;
char
*
next_heap_end
=
heap_end
+
incr
;
if
(
__builtin_expect
(
next_heap_end
>
(
&
__StackLimit
),
false
))
{
if
(
__builtin_expect
(
next_heap_end
>
(
&
__StackLimit
),
false
))
{
#if PICO_USE_OPTIMISTIC_SBRK
#if PICO_USE_OPTIMISTIC_SBRK
if
(
heap_end
==
&
__StackLimit
)
{
if
(
heap_end
==
&
__StackLimit
)
// errno = ENOMEM;
{
return
(
char
*
)
-
1
;
// errno = ENOMEM;
return
(
char
*
)
-
1
;
}
}
next_heap_end
=
&
__StackLimit
;
next_heap_end
=
&
__StackLimit
;
#else
#else
return
(
char
*
)
-
1
;
return
(
char
*
)
-
1
;
#endif
#endif
}
}
heap_end
=
next_heap_end
;
heap_end
=
next_heap_end
;
return
(
void
*
)
prev_heap_end
;
return
(
void
*
)
prev_heap_end
;
}
}
static
int64_t
epoch_time_us_since_boot
;
static
int64_t
epoch_time_us_since_boot
;
__weak
int
_gettimeofday
(
struct
timeval
*
__restrict
tv
,
__unused
void
*
__restrict
tz
)
{
__weak
int
_gettimeofday
(
struct
timeval
*
__restrict
tv
,
__unused
void
*
__restrict
tz
)
if
(
tv
)
{
{
if
(
tv
)
{
int64_t
us_since_epoch
=
((
int64_t
)
to_us_since_boot
(
get_absolute_time
()))
-
epoch_time_us_since_boot
;
int64_t
us_since_epoch
=
((
int64_t
)
to_us_since_boot
(
get_absolute_time
()))
-
epoch_time_us_since_boot
;
tv
->
tv_sec
=
(
time_t
)(
us_since_epoch
/
1000000
);
tv
->
tv_sec
=
(
time_t
)(
us_since_epoch
/
1000000
);
tv
->
tv_usec
=
(
suseconds_t
)(
us_since_epoch
%
1000000
);
tv
->
tv_usec
=
(
suseconds_t
)(
us_since_epoch
%
1000000
);
...
@@ -85,15 +93,18 @@ __weak int _gettimeofday (struct timeval *__restrict tv, __unused void *__restri
...
@@ -85,15 +93,18 @@ __weak int _gettimeofday (struct timeval *__restrict tv, __unused void *__restri
return
0
;
return
0
;
}
}
__weak
int
settimeofday
(
__unused
const
struct
timeval
*
tv
,
__unused
const
struct
timezone
*
tz
)
{
__weak
int
settimeofday
(
__unused
const
struct
timeval
*
tv
,
__unused
const
struct
timezone
*
tz
)
if
(
tv
)
{
{
if
(
tv
)
{
int64_t
us_since_epoch
=
tv
->
tv_sec
*
1000000
+
tv
->
tv_usec
;
int64_t
us_since_epoch
=
tv
->
tv_sec
*
1000000
+
tv
->
tv_usec
;
epoch_time_us_since_boot
=
(
int64_t
)
to_us_since_boot
(
get_absolute_time
())
-
us_since_epoch
;
epoch_time_us_since_boot
=
(
int64_t
)
to_us_since_boot
(
get_absolute_time
())
-
us_since_epoch
;
}
}
return
0
;
return
0
;
}
}
__weak
int
_times
(
struct
tms
*
tms
)
{
__weak
int
_times
(
struct
tms
*
tms
)
{
#if CLOCKS_PER_SEC >= 1000000
#if CLOCKS_PER_SEC >= 1000000
tms
->
tms_utime
=
(
clock_t
)(
to_us_since_boot
(
get_absolute_time
())
*
(
CLOCKS_PER_SEC
/
1000000
));
tms
->
tms_utime
=
(
clock_t
)(
to_us_since_boot
(
get_absolute_time
())
*
(
CLOCKS_PER_SEC
/
1000000
));
#else
#else
...
@@ -105,26 +116,32 @@ __weak int _times(struct tms *tms) {
...
@@ -105,26 +116,32 @@ __weak int _times(struct tms *tms) {
return
0
;
return
0
;
}
}
__weak
pid_t
_getpid
(
void
)
{
__weak
pid_t
_getpid
(
void
)
{
return
0
;
return
0
;
}
}
__weak
int
_kill
(
__unused
pid_t
pid
,
__unused
int
sig
)
{
__weak
int
_kill
(
__unused
pid_t
pid
,
__unused
int
sig
)
{
return
-
1
;
return
-
1
;
}
}
int
__attribute__
((
weak
))
_read
(
int
handle
,
char
*
buffer
,
int
length
)
{
int
__attribute__
((
weak
))
_read
(
int
handle
,
char
*
buffer
,
int
length
)
{
#if LIB_PICO_STDIO
#if LIB_PICO_STDIO
if
(
handle
==
STDIO_HANDLE_STDIN
)
{
if
(
handle
==
STDIO_HANDLE_STDIN
)
{
return
stdio_get_until
(
buffer
,
length
,
at_the_end_of_time
);
return
stdio_get_until
(
buffer
,
length
,
at_the_end_of_time
);
}
}
#endif
#endif
return
-
1
;
return
-
1
;
}
}
int
__attribute__
((
weak
))
_write
(
int
handle
,
char
*
buffer
,
int
length
)
{
int
__attribute__
((
weak
))
_write
(
int
handle
,
char
*
buffer
,
int
length
)
{
#if LIB_PICO_STDIO
#if LIB_PICO_STDIO
if
(
handle
==
STDIO_HANDLE_STDOUT
||
handle
==
STDIO_HANDLE_STDERR
)
{
if
(
handle
==
STDIO_HANDLE_STDOUT
||
handle
==
STDIO_HANDLE_STDERR
)
{
stdio_put_string
(
buffer
,
length
,
false
,
true
);
stdio_put_string
(
buffer
,
length
,
false
,
true
);
return
length
;
return
length
;
}
}
...
@@ -132,35 +149,41 @@ int __attribute__((weak)) _write(int handle, char *buffer, int length) {
...
@@ -132,35 +149,41 @@ int __attribute__((weak)) _write(int handle, char *buffer, int length) {
return
-
1
;
return
-
1
;
}
}
int
__attribute__
((
weak
))
_open
(
__unused
const
char
*
fn
,
__unused
int
oflag
,
...)
{
int
__attribute__
((
weak
))
_open
(
__unused
const
char
*
fn
,
__unused
int
oflag
,
...)
{
return
-
1
;
return
-
1
;
}
}
int
__attribute__
((
weak
))
_close
(
__unused
int
fd
)
{
int
__attribute__
((
weak
))
_close
(
__unused
int
fd
)
{
return
-
1
;
return
-
1
;
}
}
off_t
__attribute__
((
weak
))
_lseek
(
__unused
int
fd
,
__unused
off_t
pos
,
__unused
int
whence
)
{
off_t
__attribute__
((
weak
))
_lseek
(
__unused
int
fd
,
__unused
off_t
pos
,
__unused
int
whence
)
{
return
-
1
;
return
-
1
;
}
}
int
__attribute__
((
weak
))
_fstat
(
__unused
int
fd
,
__unused
struct
stat
*
buf
)
{
int
__attribute__
((
weak
))
_fstat
(
__unused
int
fd
,
__unused
struct
stat
*
buf
)
{
return
-
1
;
return
-
1
;
}
}
int
__attribute__
((
weak
))
_isatty
(
int
fd
)
{
int
__attribute__
((
weak
))
_isatty
(
int
fd
)
{
return
fd
==
STDIO_HANDLE_STDIN
||
fd
==
STDIO_HANDLE_STDOUT
||
fd
==
STDIO_HANDLE_STDERR
;
return
fd
==
STDIO_HANDLE_STDIN
||
fd
==
STDIO_HANDLE_STDOUT
||
fd
==
STDIO_HANDLE_STDERR
;
}
}
// exit is not useful... no desire to pull in __call_exitprocs
// exit is not useful... no desire to pull in __call_exitprocs
void
exit
(
int
status
)
{
void
exit
(
int
status
)
{
_exit
(
status
);
_exit
(
status
);
}
}
// incorrect warning from GCC 6
// incorrect warning from GCC 6
GCC_Pragma
(
"GCC diagnostic push"
)
GCC_Pragma
(
"GCC diagnostic push"
)
GCC_Pragma
(
"GCC diagnostic ignored
\"
-Wsuggest-attribute=format
\"
"
)
GCC_Pragma
(
"GCC diagnostic ignored
\"
-Wsuggest-attribute=format
\"
"
)
void
__weak
__assert_func
(
const
char
*
file
,
int
line
,
const
char
*
func
,
const
char
*
failedexpr
)
void
__weak
__assert_func
(
const
char
*
file
,
int
line
,
const
char
*
func
,
const
char
*
failedexpr
)
{
{
weak_raw_printf
(
"assertion
\"
%s
\"
failed: file
\"
%s
\"
, line %d%s%s
\n
"
,
weak_raw_printf
(
"assertion
\"
%s
\"
failed: file
\"
%s
\"
, line %d%s%s
\n
"
,
failedexpr
,
file
,
line
,
func
?
", function: "
:
""
,
failedexpr
,
file
,
line
,
func
?
", function: "
:
""
,
func
?
func
:
""
);
func
?
func
:
""
);
...
@@ -169,9 +192,11 @@ void __weak __assert_func(const char *file, int line, const char *func, const ch
...
@@ -169,9 +192,11 @@ void __weak __assert_func(const char *file, int line, const char *func, const ch
}
}
GCC_Pragma
(
"GCC diagnostic pop"
)
GCC_Pragma
(
"GCC diagnostic pop"
)
void
runtime_init
(
void
)
{
void
runtime_init
(
void
)
{
#ifndef NDEBUG
#ifndef NDEBUG
if
(
__get_current_exception
())
{
if
(
__get_current_exception
())
{
// crap; started in exception handler
// crap; started in exception handler
__breakpoint
();
__breakpoint
();
}
}
...
@@ -185,7 +210,7 @@ void runtime_init(void) {
...
@@ -185,7 +210,7 @@ void runtime_init(void) {
// todo maybe we want to do this in the future, but it does stuff like register_tm_clones
// todo maybe we want to do this in the future, but it does stuff like register_tm_clones
// which we didn't do in previous SDKs
// which we didn't do in previous SDKs
//extern void __libc_init_array(void);
//
extern void __libc_init_array(void);
//__libc_init_array();
//__libc_init_array();
// ... so instead just do the __preinit_array
// ... so instead just do the __preinit_array
...
@@ -193,7 +218,8 @@ void runtime_init(void) {
...
@@ -193,7 +218,8 @@ void runtime_init(void) {
// ... and the __init_array
// ... and the __init_array
extern
void
(
*
__init_array_start
)(
void
);
extern
void
(
*
__init_array_start
)(
void
);
extern
void
(
*
__init_array_end
)(
void
);
extern
void
(
*
__init_array_end
)(
void
);
for
(
void
(
**
p
)(
void
)
=
&
__init_array_start
;
p
<
&
__init_array_end
;
++
p
)
{
for
(
void
(
**
p
)(
void
)
=
&
__init_array_start
;
p
<
&
__init_array_end
;
++
p
)
{
(
*
p
)();
(
*
p
)();
}
}
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/main.c
+
18
−
6
View file @
84bf82db
...
@@ -108,6 +108,7 @@ const uint8_t better_matrix[65][17] = {
...
@@ -108,6 +108,7 @@ const uint8_t better_matrix[65][17] = {
void
set_progress
(
double
percentage
)
void
set_progress
(
double
percentage
)
{
{
int
numerator
=
(
percentage
*
32
)
/
100
;
int
numerator
=
(
percentage
*
32
)
/
100
;
for
(
int
i
=
0
;
i
<=
numerator
;
i
++
)
for
(
int
i
=
0
;
i
<=
numerator
;
i
++
)
...
@@ -152,15 +153,15 @@ int main()
...
@@ -152,15 +153,15 @@ int main()
gpio_set_function
(
28
,
GPIO_FUNC_PWM
);
gpio_set_function
(
28
,
GPIO_FUNC_PWM
);
uint
slice_num
=
pwm_gpio_to_slice_num
(
28
);
volatile
uint
slice_num
=
pwm_gpio_to_slice_num
(
28
);
pwm_set_clkdiv
(
slice_num
,
133
);
pwm_set_clkdiv
(
slice_num
,
133
);
pwm_set_wrap
(
slice_num
,
0xFFFF
);
pwm_set_wrap
(
slice_num
,
0xFFFF
);
pwm_set_chan_level
(
slice_num
,
PWM_CHAN_A
,
10
);
pwm_set_chan_level
(
slice_num
,
PWM_CHAN_A
,
10
);
pwm_set_enabled
(
slice_num
,
true
);
pwm_set_enabled
(
slice_num
,
true
);
uint
another_slice_num
=
pwm_gpio_to_slice_num
(
27
);
volatile
uint
another_slice_num
=
pwm_gpio_to_slice_num
(
27
);
pwm_set_clkdiv
(
another_slice_num
,
1
33
);
pwm_set_clkdiv
(
another_slice_num
,
1
);
pwm_set_wrap
(
another_slice_num
,
0xFFFF
);
pwm_set_wrap
(
another_slice_num
,
0xFFFF
);
pwm_set_clkdiv_mode
(
another_slice_num
,
PWM_DIV_FREE_RUNNING
);
pwm_set_clkdiv_mode
(
another_slice_num
,
PWM_DIV_FREE_RUNNING
);
pwm_set_enabled
(
slice_num
,
true
);
pwm_set_enabled
(
slice_num
,
true
);
...
@@ -177,13 +178,24 @@ int main()
...
@@ -177,13 +178,24 @@ int main()
gpio_put
(
28
,
false
);
gpio_put
(
28
,
false
);
while
(
!
gpio_get
(
27
))
while
(
!
gpio_get
(
27
))
;
;
uint32_t
start
=
time_us_32
();
volatile
uint32_t
start
=
time_us_32
();
while
(
gpio_get
(
27
))
while
(
gpio_get
(
27
))
;
;
uint32_t
end
=
time_us_32
();
volatile
uint32_t
end
=
time_us_32
();
uint32_t
duration
=
end
-
start
;
uint32_t
duration
=
end
-
start
;
volatile
float
distance
=
duration
*
314
/
2000
;
volatile
float
distance
=
(
duration
*
314
)
/
(
20000
);
if
(
distance
>
20
)
{
distance
=
20
;
}
distance
=
20
-
distance
;
volatile
int
percentage
=
(
distance
/
10
)
*
100
;
set_progress
(
percentage
);
asm
(
"BKPT #0"
);
}
}
a
=
3
;
a
=
3
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment