Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
micropython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
card10
micropython
Commits
7027fd53
Commit
7027fd53
authored
9 years ago
by
Daniel Campora
Browse files
Options
Downloads
Patches
Plain Diff
cc3200: Make ADC API compatible with the pyboard.
parent
7da2fdc3
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
cc3200/mods/pybadc.c
+35
-62
35 additions, 62 deletions
cc3200/mods/pybadc.c
cc3200/qstrdefsport.h
+2
-0
2 additions, 0 deletions
cc3200/qstrdefsport.h
docs/library/pyb.ADC.rst
+10
-8
10 additions, 8 deletions
docs/library/pyb.ADC.rst
with
47 additions
and
70 deletions
cc3200/mods/pybadc.c
+
35
−
62
View file @
7027fd53
...
@@ -57,9 +57,7 @@
...
@@ -57,9 +57,7 @@
///
///
/// Usage:
/// Usage:
///
///
/// adc = pyb.ADC(channel) # create an adc object on the given channel (1 to 4)
/// adc = pyb.ADC('GP5') # create an adc object on the given pin (GP2, GP3, GP4 o GP5)
/// this automatically configures the pin associated to
/// that analog channel.
/// adc.read() # read channel value
/// adc.read() # read channel value
///
///
/// The sample rate is fixed to 62.5KHz and the resolution to 12 bits.
/// The sample rate is fixed to 62.5KHz and the resolution to 12 bits.
...
@@ -75,14 +73,23 @@
...
@@ -75,14 +73,23 @@
******************************************************************************/
******************************************************************************/
typedef
struct
{
typedef
struct
{
mp_obj_base_t
base
;
mp_obj_base_t
base
;
pin_obj_t
*
pin
;
byte
channel
;
byte
channel
;
byte
id
x
;
byte
id
;
}
pyb_adc_obj_t
;
}
pyb_adc_obj_t
;
/******************************************************************************
DECLARE PRIVATE DATA
******************************************************************************/
STATIC
pyb_adc_obj_t
pyb_adc_obj
[
PYB_ADC_NUM_CHANNELS
]
=
{
{.
pin
=
&
pin_GP2
,
.
channel
=
ADC_CH_0
,
.
id
=
1
},
{.
pin
=
&
pin_GP3
,
.
channel
=
ADC_CH_1
,
.
id
=
2
},
{.
pin
=
&
pin_GP4
,
.
channel
=
ADC_CH_2
,
.
id
=
2
},
{.
pin
=
&
pin_GP5
,
.
channel
=
ADC_CH_3
,
.
id
=
4
}
};
/******************************************************************************
/******************************************************************************
DEFINE PUBLIC FUNCTIONS
DEFINE PUBLIC FUNCTIONS
******************************************************************************/
******************************************************************************/
STATIC
void
pybadc_init
(
pyb_adc_obj_t
*
self
)
{
STATIC
void
pybadc_init
(
pyb_adc_obj_t
*
self
)
{
// configure the pin in analog mode
pin_config
(
self
->
pin
,
PIN_MODE_0
,
GPIO_DIR_MODE_IN
,
PYBPIN_ANALOG_TYPE
,
PIN_STRENGTH_2MA
);
// enable the ADC channel
// enable the ADC channel
MAP_ADCChannelEnable
(
ADC_BASE
,
self
->
channel
);
MAP_ADCChannelEnable
(
ADC_BASE
,
self
->
channel
);
// enable and configure the timer
// enable and configure the timer
...
@@ -92,69 +99,35 @@ STATIC void pybadc_init (pyb_adc_obj_t *self) {
...
@@ -92,69 +99,35 @@ STATIC void pybadc_init (pyb_adc_obj_t *self) {
MAP_ADCEnable
(
ADC_BASE
);
MAP_ADCEnable
(
ADC_BASE
);
}
}
/******************************************************************************
DECLARE PRIVATE DATA
******************************************************************************/
STATIC
pyb_adc_obj_t
pyb_adc_obj
[
PYB_ADC_NUM_CHANNELS
];
/******************************************************************************/
/******************************************************************************/
/* Micro Python bindings : adc object */
/* Micro Python bindings : adc object */
STATIC
void
adc_print
(
const
mp_print_t
*
print
,
mp_obj_t
self_in
,
mp_print_kind_t
kind
)
{
STATIC
void
adc_print
(
const
mp_print_t
*
print
,
mp_obj_t
self_in
,
mp_print_kind_t
kind
)
{
pyb_adc_obj_t
*
self
=
self_in
;
pyb_adc_obj_t
*
self
=
self_in
;
mp_printf
(
print
,
"<ADC
,
channel=%u>"
,
(
self
->
id
x
+
1
)
);
mp_printf
(
print
,
"<ADC
1
channel=%u
on %q
>"
,
self
->
id
,
self
->
pin
->
name
);
}
}
/// \classmethod \constructor(
channel
)
/// \classmethod \constructor(
pin
)
/// Create an ADC object associated with the given
channel
.
/// Create an ADC object associated with the given
pin
.
/// This allows you to then read analog values on that pin.
/// This allows you to then read analog values on that pin.
STATIC
mp_obj_t
adc_make_new
(
mp_obj_t
type_in
,
mp_uint_t
n_args
,
mp_uint_t
n_kw
,
const
mp_obj_t
*
args
)
{
STATIC
mp_obj_t
adc_make_new
(
mp_obj_t
type_in
,
mp_uint_t
n_args
,
mp_uint_t
n_kw
,
const
mp_obj_t
*
args
)
{
// check number of arguments
// check number of arguments
mp_arg_check_num
(
n_args
,
n_kw
,
1
,
1
,
false
);
mp_arg_check_num
(
n_args
,
n_kw
,
1
,
1
,
false
);
// the first argument is the channel number
// the argument passed is the pin
int32_t
idx
=
mp_obj_get_int
(
args
[
0
])
-
1
;
const
pin_obj_t
*
pin
=
(
pin_obj_t
*
)
pin_find
(
args
[
0
]);
const
pin_obj_t
*
pin
;
for
(
int32_t
idx
=
0
;
idx
<
PYB_ADC_NUM_CHANNELS
;
idx
++
)
{
uint
channel
;
if
(
pin
==
pyb_adc_obj
[
idx
].
pin
)
{
switch
(
idx
)
{
case
0
:
channel
=
ADC_CH_0
;
pin
=
&
pin_GP2
;
break
;
case
1
:
channel
=
ADC_CH_1
;
pin
=
&
pin_GP3
;
break
;
case
2
:
channel
=
ADC_CH_2
;
pin
=
&
pin_GP4
;
break
;
case
3
:
channel
=
ADC_CH_3
;
pin
=
&
pin_GP5
;
break
;
default:
nlr_raise
(
mp_obj_new_exception_msg
(
&
mp_type_ValueError
,
mpexception_value_invalid_arguments
));
break
;
}
// disable the callback before re-configuring
pyb_adc_obj_t
*
self
=
&
pyb_adc_obj
[
idx
];
pyb_adc_obj_t
*
self
=
&
pyb_adc_obj
[
idx
];
self
->
base
.
type
=
&
pyb_adc_type
;
self
->
base
.
type
=
&
pyb_adc_type
;
self
->
channel
=
channel
;
self
->
idx
=
idx
;
// configure the pin in analog mode
pin_config
((
pin_obj_t
*
)
pin
,
PIN_MODE_0
,
GPIO_DIR_MODE_IN
,
PYBPIN_ANALOG_TYPE
,
PIN_STRENGTH_2MA
);
// initialize it
pybadc_init
(
self
);
pybadc_init
(
self
);
// register it with the sleep module
// register it with the sleep module
pybsleep_add
((
const
mp_obj_t
)
self
,
(
WakeUpCB_t
)
pybadc_init
);
pybsleep_add
((
const
mp_obj_t
)
self
,
(
WakeUpCB_t
)
pybadc_init
);
return
self
;
return
self
;
}
}
}
nlr_raise
(
mp_obj_new_exception_msg
(
&
mp_type_ValueError
,
mpexception_value_invalid_arguments
));
}
/// \method read()
/// \method read()
/// Read the value on the analog pin and return it. The returned value
/// Read the value on the analog pin and return it. The returned value
...
@@ -172,19 +145,19 @@ STATIC mp_obj_t adc_read(mp_obj_t self_in) {
...
@@ -172,19 +145,19 @@ STATIC mp_obj_t adc_read(mp_obj_t self_in) {
}
}
STATIC
MP_DEFINE_CONST_FUN_OBJ_1
(
adc_read_obj
,
adc_read
);
STATIC
MP_DEFINE_CONST_FUN_OBJ_1
(
adc_read_obj
,
adc_read
);
/// \method
enable
()
/// \method
init
()
/// Enable the adc channel
/// Enable the adc channel
STATIC
mp_obj_t
adc_
enable
(
mp_obj_t
self_in
)
{
STATIC
mp_obj_t
adc_
init
(
mp_obj_t
self_in
)
{
pyb_adc_obj_t
*
self
=
self_in
;
pyb_adc_obj_t
*
self
=
self_in
;
pybadc_init
(
self
);
pybadc_init
(
self
);
return
mp_const_none
;
return
mp_const_none
;
}
}
STATIC
MP_DEFINE_CONST_FUN_OBJ_1
(
adc_
enable
_obj
,
adc_
enable
);
STATIC
MP_DEFINE_CONST_FUN_OBJ_1
(
adc_
init
_obj
,
adc_
init
);
/// \method d
isable
()
/// \method d
einit
()
/// Disable the adc channel
/// Disable the adc channel
STATIC
mp_obj_t
adc_d
isable
(
mp_obj_t
self_in
)
{
STATIC
mp_obj_t
adc_d
einit
(
mp_obj_t
self_in
)
{
pyb_adc_obj_t
*
self
=
self_in
;
pyb_adc_obj_t
*
self
=
self_in
;
MAP_ADCChannelDisable
(
ADC_BASE
,
self
->
channel
);
MAP_ADCChannelDisable
(
ADC_BASE
,
self
->
channel
);
...
@@ -192,12 +165,12 @@ STATIC mp_obj_t adc_disable(mp_obj_t self_in) {
...
@@ -192,12 +165,12 @@ STATIC mp_obj_t adc_disable(mp_obj_t self_in) {
pybsleep_remove
((
const
mp_obj_t
)
self
);
pybsleep_remove
((
const
mp_obj_t
)
self
);
return
mp_const_none
;
return
mp_const_none
;
}
}
STATIC
MP_DEFINE_CONST_FUN_OBJ_1
(
adc_d
isable
_obj
,
adc_d
isable
);
STATIC
MP_DEFINE_CONST_FUN_OBJ_1
(
adc_d
einit
_obj
,
adc_d
einit
);
STATIC
const
mp_map_elem_t
adc_locals_dict_table
[]
=
{
STATIC
const
mp_map_elem_t
adc_locals_dict_table
[]
=
{
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_read
),
(
mp_obj_t
)
&
adc_read_obj
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_read
),
(
mp_obj_t
)
&
adc_read_obj
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_
enable
),
(
mp_obj_t
)
&
adc_
enable
_obj
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_
init
),
(
mp_obj_t
)
&
adc_
init
_obj
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_d
isable
),
(
mp_obj_t
)
&
adc_d
isable
_obj
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_d
einit
),
(
mp_obj_t
)
&
adc_d
einit
_obj
},
};
};
STATIC
MP_DEFINE_CONST_DICT
(
adc_locals_dict
,
adc_locals_dict_table
);
STATIC
MP_DEFINE_CONST_DICT
(
adc_locals_dict
,
adc_locals_dict_table
);
...
...
This diff is collapsed.
Click to expand it.
cc3200/qstrdefsport.h
+
2
−
0
View file @
7027fd53
...
@@ -182,6 +182,8 @@ Q(MASTER)
...
@@ -182,6 +182,8 @@ Q(MASTER)
// for ADC class
// for ADC class
Q
(
ADC
)
Q
(
ADC
)
Q
(
read
)
Q
(
read
)
Q
(
init
)
Q
(
deinit
)
#if MICROPY_HW_HAS_SDCARD
#if MICROPY_HW_HAS_SDCARD
// for SD class
// for SD class
...
...
This diff is collapsed.
Click to expand it.
docs/library/pyb.ADC.rst
+
10
−
8
View file @
7027fd53
...
@@ -24,8 +24,10 @@ class ADC -- analog to digital conversion: read analog values on a pin
...
@@ -24,8 +24,10 @@ class ADC -- analog to digital conversion: read analog values on a pin
import pyb
import pyb
adc = pyb.ADC(
channel)
# create an analog object on one of the 4 ADC channels
adc = pyb.ADC(
pin)
# create an analog object on one of the 4 ADC channels
val = adc.read() # read an analog value
val = adc.read() # read an analog value
adc.deinit() # disable the adc channel
adc.init() # enable the adc channel
Constructors
Constructors
------------
------------
...
@@ -39,12 +41,12 @@ Constructors
...
@@ -39,12 +41,12 @@ Constructors
.. only:: port_wipy
.. only:: port_wipy
.. class:: pyb.ADC(
channel
)
.. class:: pyb.ADC(
pin
)
Create an ADC object on the given channel. Each channel is associated
Create an ADC object associated with the given pin.
to a specific pin. For more info check the `pinout and alternate functions
table. <https://raw.githubusercontent.com/wipy/wipy/master/docs/PinOUT.png>`_
This allows you to then read analog values on that pin.
This allows you to then read analog values on that pin.
For more info check the `pinout and alternate functions
table. <https://raw.githubusercontent.com/wipy/wipy/master/docs/PinOUT.png>`_
.. warning::
.. warning::
...
@@ -100,10 +102,10 @@ Methods
...
@@ -100,10 +102,10 @@ Methods
.. only:: port_wipy
.. only:: port_wipy
.. method:: adc.
enable
()
.. method:: adc.
init
()
Enable the ADC channel.
Enable the ADC channel.
.. method:: adc.d
isable
()
.. method:: adc.d
einit
()
Disable the ADC channel.
Disable the ADC channel.
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