Skip to content

Commit 7fe32a2

Browse files
author
oclyke
committed
add examples
1 parent 59b34f2 commit 7fe32a2

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed

libraries/Apollo3/examples/AnalogWrite/AnalogWrite.ino

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
*/
3535

3636
void setup() {
37+
Serial.begin(115200);
38+
Serial.println("Apollo3 - analogWrite");
39+
3740
// pinMode is not needed for PWM output
3841

3942
analogWrite(LED_BUILTIN, 127); // 50% duty cycle w/ 8-bit resolution
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
// This file is subject to the terms and conditions defined in
3+
// file 'LICENSE.md', which is part of this source code package.
4+
*/
5+
6+
#define INT_PIN 16
7+
#define AUTO_PIN D35 // connect this pin to INT_PIN for auto control
8+
9+
rtos::Thread statusThread;
10+
11+
volatile uint32_t count = 0;
12+
uint32_t expected_count = 0;
13+
14+
void status_fn( void ){
15+
while(1){
16+
rtos::ThisThread::sleep_for(1000);
17+
printf("time (ms): %d, count: %d, expected: %d, diff: %d\n", millis(), count, expected_count, (expected_count - count));
18+
digitalWrite(LED_BUILTIN, (digitalRead(LED_BUILTIN)) ? LOW : HIGH);
19+
}
20+
}
21+
22+
void myISR(void){
23+
count++; // access count because it is a global variable
24+
}
25+
26+
void myParamISR(void *arg){
27+
uint32_t* pcount = (uint32_t *)arg;
28+
*(pcount)++; // access count via the passed in argument
29+
}
30+
31+
void setup()
32+
{
33+
Serial.begin(115200);
34+
35+
printf("Apollo3 - attachInterrupt\n\n");
36+
37+
pinMode(LED_BUILTIN, OUTPUT);
38+
pinMode(AUTO_PIN, OUTPUT);
39+
40+
digitalWrite(AUTO_PIN, LOW);
41+
digitalWrite(LED_BUILTIN, LOW);
42+
43+
pinMode(INT_PIN, INPUT_PULLUP);
44+
45+
// interrupts can occur on several kinds of conditions
46+
attachInterrupt(INT_PIN, myISR, RISING);
47+
attachInterrupt(INT_PIN, myISR, FALLING);
48+
attachInterrupt(INT_PIN, myISR, LOW);
49+
attachInterrupt(INT_PIN, myISR, HIGH);
50+
attachInterrupt(INT_PIN, myISR, CHANGE);
51+
52+
// there is another function that you can use to attach an interrupt with parameters
53+
// you can supply a pointer as the 'param' argument, this will be available in the
54+
// isr as the void* parameter
55+
// attachInterruptParam(INT_PIN, myParamISR, INT_MODE, void* param);
56+
attachInterruptParam(INT_PIN, myParamISR, RISING, (void*)&count);
57+
58+
// attaching a different interrupt to the same pin overwrites the existing ISR
59+
attachInterrupt(INT_PIN, myISR, RISING);
60+
61+
// finally you may notice that this example does not use 'digitalPinToInterrupt()'
62+
// on the Apollo3 all digital pins are interrupts and they are identified by their
63+
// normal pin numbers
64+
// you may still use 'digitalPinToInterrupt()' for compatibility with legacy code
65+
// #define digitalPinToInterrupt(P) (P)
66+
// attachInterrupt(digitalPinToInterrupt(INT_PIN), myISR, INT_MODE);
67+
// attachInterruptParam(digitalPinToInterrupt(INT_PIN), myParamISR, INT_MODE, void* param);
68+
69+
statusThread.start(status_fn);
70+
}
71+
72+
void loop()
73+
{
74+
expected_count++;
75+
digitalWrite(AUTO_PIN, HIGH);
76+
digitalWrite(AUTO_PIN, LOW);
77+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
// This file is subject to the terms and conditions defined in
3+
// file 'LICENSE.md', which is part of this source code package.
4+
*/
5+
6+
// Wiring:
7+
// INT_PIN <-> AUTO_PIN (for automatic interrupt generation)
8+
9+
#define INT_PIN 16
10+
#define AUTO_PIN D35 // connect this pin to INT_PIN for auto control
11+
#define INT_TYPE RISING
12+
13+
rtos::Thread autoISRThread;
14+
rtos::Thread heartbeatThread;
15+
16+
volatile uint32_t count = 0;
17+
uint32_t expected_count = 0;
18+
volatile bool isr_flag = false;
19+
20+
void myParamISR( void *param ){
21+
isr_flag = true;
22+
volatile uint32_t* pcount = (volatile uint32_t *)param;
23+
*pcount += 1; // access count via the passed in parameter
24+
}
25+
26+
void generateAutoISRs( void ){
27+
while(1){
28+
delay(1000);
29+
expected_count++;
30+
digitalWrite(AUTO_PIN, HIGH);
31+
digitalWrite(AUTO_PIN, LOW);
32+
digitalWrite(LED_BUILTIN, (digitalRead(LED_BUILTIN)) ? LOW : HIGH);
33+
if(INT_TYPE == CHANGE){
34+
expected_count++;
35+
}
36+
}
37+
}
38+
39+
void hearbeat( void ){
40+
while(1){
41+
delay(10000);
42+
printf("blub blub. current time (ms): %d\n", millis());
43+
}
44+
}
45+
46+
void setup()
47+
{
48+
printf("Apollo3 - detachInterrupt\n\n");
49+
50+
pinMode(LED_BUILTIN, OUTPUT);
51+
digitalWrite(LED_BUILTIN, LOW);
52+
53+
pinMode(AUTO_PIN, OUTPUT);
54+
digitalWrite(AUTO_PIN, LOW);
55+
56+
pinMode(INT_PIN, INPUT_PULLUP);
57+
attachInterruptParam(INT_PIN, myParamISR, INT_TYPE, (void*)&count);
58+
59+
autoISRThread.start(generateAutoISRs);
60+
heartbeatThread.start(hearbeat);
61+
}
62+
63+
void loop()
64+
{
65+
if(isr_flag){
66+
isr_flag = false;
67+
printf("isr fired. count: %d, expected: %d, diff: %d, time (ms): %d\n", count, expected_count, (expected_count - count), millis());
68+
if(count >= 5){
69+
count = 0;
70+
printf("shutting off interrupts\n\n");
71+
detachInterrupt(INT_PIN);
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)