Skip to content

Commit 0545dac

Browse files
committed
Add ADC Example
Signed-off-by: Ajay Bhargav <[email protected]>
1 parent 3b0bbb1 commit 0545dac

File tree

8 files changed

+369
-0
lines changed

8 files changed

+369
-0
lines changed

examples/adc-example/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.pio
2+
.vscode/.browse.c_cpp.db*
3+
.vscode/c_cpp_properties.json
4+
.vscode/launch.json
5+
.vscode/ipch

examples/adc-example/.travis.yml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Continuous Integration (CI) is the practice, in software
2+
# engineering, of merging all developer working copies with a shared mainline
3+
# several times a day < https://docs.platformio.org/page/ci/index.html >
4+
#
5+
# Documentation:
6+
#
7+
# * Travis CI Embedded Builds with PlatformIO
8+
# < https://docs.travis-ci.com/user/integration/platformio/ >
9+
#
10+
# * PlatformIO integration with Travis CI
11+
# < https://docs.platformio.org/page/ci/travis.html >
12+
#
13+
# * User Guide for `platformio ci` command
14+
# < https://docs.platformio.org/page/userguide/cmd_ci.html >
15+
#
16+
#
17+
# Please choose one of the following templates (proposed below) and uncomment
18+
# it (remove "# " before each line) or use own configuration according to the
19+
# Travis CI documentation (see above).
20+
#
21+
22+
23+
#
24+
# Template #1: General project. Test it using existing `platformio.ini`.
25+
#
26+
27+
# language: python
28+
# python:
29+
# - "2.7"
30+
#
31+
# sudo: false
32+
# cache:
33+
# directories:
34+
# - "~/.platformio"
35+
#
36+
# install:
37+
# - pip install -U platformio
38+
# - platformio update
39+
#
40+
# script:
41+
# - platformio run
42+
43+
44+
#
45+
# Template #2: The project is intended to be used as a library with examples.
46+
#
47+
48+
# language: python
49+
# python:
50+
# - "2.7"
51+
#
52+
# sudo: false
53+
# cache:
54+
# directories:
55+
# - "~/.platformio"
56+
#
57+
# env:
58+
# - PLATFORMIO_CI_SRC=path/to/test/file.c
59+
# - PLATFORMIO_CI_SRC=examples/file.ino
60+
# - PLATFORMIO_CI_SRC=path/to/test/directory
61+
#
62+
# install:
63+
# - pip install -U platformio
64+
# - platformio update
65+
#
66+
# script:
67+
# - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
This directory is intended for project header files.
3+
4+
A header file is a file containing C declarations and macro definitions
5+
to be shared between several project source files. You request the use of a
6+
header file in your project source file (C, C++, etc) located in `src` folder
7+
by including it, with the C preprocessing directive `#include'.
8+
9+
```src/main.c
10+
11+
#include "header.h"
12+
13+
int main (void)
14+
{
15+
...
16+
}
17+
```
18+
19+
Including a header file produces the same results as copying the header file
20+
into each source file that needs it. Such copying would be time-consuming
21+
and error-prone. With a header file, the related declarations appear
22+
in only one place. If they need to be changed, they can be changed in one
23+
place, and programs that include the header file will automatically use the
24+
new version when next recompiled. The header file eliminates the labor of
25+
finding and changing all the copies as well as the risk that a failure to
26+
find one copy will result in inconsistencies within a program.
27+
28+
In C, the usual convention is to give header files names that end with `.h'.
29+
It is most portable to use only letters, digits, dashes, and underscores in
30+
header file names, and at most one dot.
31+
32+
Read more about using header files in official GCC documentation:
33+
34+
* Include Syntax
35+
* Include Operation
36+
* Once-Only Headers
37+
* Computed Includes
38+
39+
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html

examples/adc-example/lib/README

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
This directory is intended for project specific (private) libraries.
3+
PlatformIO will compile them to static libraries and link into executable file.
4+
5+
The source code of each library should be placed in a an own separate directory
6+
("lib/your_library_name/[here are source files]").
7+
8+
For example, see a structure of the following two libraries `Foo` and `Bar`:
9+
10+
|--lib
11+
| |
12+
| |--Bar
13+
| | |--docs
14+
| | |--examples
15+
| | |--src
16+
| | |- Bar.c
17+
| | |- Bar.h
18+
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
19+
| |
20+
| |--Foo
21+
| | |- Foo.c
22+
| | |- Foo.h
23+
| |
24+
| |- README --> THIS FILE
25+
|
26+
|- platformio.ini
27+
|--src
28+
|- main.c
29+
30+
and a contents of `src/main.c`:
31+
```
32+
#include <Foo.h>
33+
#include <Bar.h>
34+
35+
int main (void)
36+
{
37+
...
38+
}
39+
40+
```
41+
42+
PlatformIO Library Dependency Finder will find automatically dependent
43+
libraries scanning project source files.
44+
45+
More information about PlatformIO Library Dependency Finder
46+
- https://docs.platformio.org/page/librarymanager/ldf.html
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
; PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter
4+
; Upload options: custom upload port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
; Advanced options: extra scripting
7+
;
8+
; Please visit documentation for the other options and examples
9+
; https://docs.platformio.org/page/projectconf.html
10+
11+
[platformio]
12+
default_envs = s20gsm
13+
14+
; Common env setup
15+
[env]
16+
platform = siwigsm
17+
board = s20gsm
18+
framework = siwisdk
19+
monitor_speed = 115200
20+
21+
; Release env
22+
[env:s20gsm]
23+
24+
; Debug env
25+
[env:s20gsm_debug]
26+
build_type = debug
27+
debug_build_flags = -Os -g2

examples/adc-example/readme.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
How to build
2+
============
3+
4+
1. Install `Visual Studio Code <https://code.visualstudio.com/>`_
5+
2. Install `PlatformIO Extension for VSCode <https://platformio.org/platformio-ide>`_
6+
3. Install SiWi GSM Platform:
7+
8+
* Open PlatformIO Home
9+
* Go to Platforms -> Advanced Installation
10+
* Paste repository link https://github.com/siwiembedded/platform-siwigsm.git
11+
* Click Install
12+
13+
4. Download and Extract examples from github https://github.com/siwiembedded/platform-siwigsm/archive/master.zip
14+
5. Extract and Open example folder with *VSCode*
15+
6. Run following command:
16+
17+
.. code-block:: bash
18+
19+
# Build Project
20+
$ platformio run
21+
22+
# Upload Project
23+
$ platformio run --target upload

examples/adc-example/src/main.c

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/**
2+
* @file main.c
3+
* ADC Example
4+
* @author Ajay Bhargav
5+
*
6+
*/
7+
8+
#include <stdio.h>
9+
#include <unistd.h>
10+
11+
#include <lib.h>
12+
#include <ril.h>
13+
#include <os_api.h>
14+
#include <hw/adc.h>
15+
16+
#define MVOLTPERDIV 2.737f
17+
#define ADC2MVOLT(val) ((float)val * MVOLTPERDIV)
18+
19+
/**
20+
* URC Handler
21+
* @param param1 URC Code
22+
* @param param2 URC Parameter
23+
*/
24+
static void urc_callback(unsigned int param1, unsigned int param2)
25+
{
26+
switch (param1) {
27+
case URC_SYS_INIT_STATE_IND:
28+
if (param2 == SYS_STATE_SMSOK) {
29+
/* Ready for SMS */
30+
}
31+
break;
32+
case URC_SIM_CARD_STATE_IND:
33+
switch (param2) {
34+
case SIM_STAT_NOT_INSERTED:
35+
debug(DBG_OFF, "SYSTEM: SIM card not inserted!\n");
36+
break;
37+
case SIM_STAT_READY:
38+
debug(DBG_INFO, "SYSTEM: SIM card Ready!\n");
39+
break;
40+
case SIM_STAT_PIN_REQ:
41+
debug(DBG_OFF, "SYSTEM: SIM PIN required!\n");
42+
break;
43+
case SIM_STAT_PUK_REQ:
44+
debug(DBG_OFF, "SYSTEM: SIM PUK required!\n");
45+
break;
46+
case SIM_STAT_NOT_READY:
47+
debug(DBG_OFF, "SYSTEM: SIM card not recognized!\n");
48+
break;
49+
default:
50+
debug(DBG_OFF, "SYSTEM: SIM ERROR: %d\n", param2);
51+
}
52+
break;
53+
case URC_GSM_NW_STATE_IND:
54+
debug(DBG_OFF, "SYSTEM: GSM NW State: %d\n", param2);
55+
break;
56+
case URC_GPRS_NW_STATE_IND:
57+
break;
58+
case URC_CFUN_STATE_IND:
59+
break;
60+
case URC_COMING_CALL_IND:
61+
debug(DBG_OFF, "Incoming voice call from: %s\n", ((ST_ComingCall*)param2)->phoneNumber);
62+
/* Take action here, Answer/Hang-up */
63+
break;
64+
case URC_CALL_STATE_IND:
65+
switch (param2) {
66+
case CALL_STATE_BUSY:
67+
debug(DBG_OFF, "The number you dialed is busy now\n");
68+
break;
69+
case CALL_STATE_NO_ANSWER:
70+
debug(DBG_OFF, "The number you dialed has no answer\n");
71+
break;
72+
case CALL_STATE_NO_CARRIER:
73+
debug(DBG_OFF, "The number you dialed cannot reach\n");
74+
break;
75+
case CALL_STATE_NO_DIALTONE:
76+
debug(DBG_OFF, "No Dial tone\n");
77+
break;
78+
default:
79+
break;
80+
}
81+
break;
82+
case URC_NEW_SMS_IND:
83+
debug(DBG_OFF, "SMS: New SMS (%d)\n", param2);
84+
/* Handle New SMS */
85+
break;
86+
case URC_MODULE_VOLTAGE_IND:
87+
debug(DBG_INFO, "VBatt Voltage: %d\n", param2);
88+
break;
89+
case URC_ALARM_RING_IND:
90+
break;
91+
case URC_FILE_DOWNLOAD_STATUS:
92+
break;
93+
case URC_FOTA_STARTED:
94+
break;
95+
case URC_FOTA_FINISHED:
96+
break;
97+
case URC_FOTA_FAILED:
98+
break;
99+
case URC_STKPCI_RSP_IND:
100+
break;
101+
default:
102+
break;
103+
}
104+
}
105+
106+
/**
107+
* Sample Task
108+
* @param arg Task Argument
109+
*/
110+
static void adc_task(void *arg)
111+
{
112+
unsigned int adc_val;
113+
114+
/*
115+
* Configure ADC Channels
116+
*/
117+
adc_config(ADC_CH0);
118+
adc_config(ADC_CH1);
119+
adc_config(ADC_CH2);
120+
121+
while (1) {
122+
/*
123+
* Read ADC Channels
124+
*/
125+
adc_val = adc_read(ADC_CH0);
126+
printf("ADC Channel 0: %d, %.2fmV\n", adc_val, ADC2MVOLT(adc_val));
127+
adc_val = adc_read(ADC_CH1);
128+
printf("ADC Channel 1: %d, %.2fmV\n", adc_val, ADC2MVOLT(adc_val));
129+
adc_val = adc_read(ADC_CH2);
130+
printf("ADC Channel 2: %d, %.2fmV\n", adc_val, ADC2MVOLT(adc_val));
131+
sleep(1);
132+
}
133+
}
134+
135+
/**
136+
* Application main entry point
137+
*/
138+
void user_main(void)
139+
{
140+
/*
141+
* Initialize library and Setup STDIO
142+
*/
143+
siwilib_init("/dev/ttyS0", urc_callback);
144+
145+
printf("System Ready\n");
146+
147+
/* Create ADC task */
148+
os_create_task(adc_task, NULL, FALSE);
149+
150+
printf("System Initialization finished\n");
151+
}

examples/adc-example/test/README

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
This directory is intended for PIO Unit Testing and project tests.
3+
4+
Unit Testing is a software testing method by which individual units of
5+
source code, sets of one or more MCU program modules together with associated
6+
control data, usage procedures, and operating procedures, are tested to
7+
determine whether they are fit for use. Unit testing finds problems early
8+
in the development cycle.
9+
10+
More information about PIO Unit Testing:
11+
- https://docs.platformio.org/page/plus/unit-testing.html

0 commit comments

Comments
 (0)