ClearCore Library
Loading...
Searching...
No Matches
ClearCoreIOExpansionBoard.cpp

Return to SDK Examples for Microchip Studio

1/*
2 * Title: ClearCoreIOExpansionBoard
3 *
4 * Objective:
5 * This example demonstrates how to initialize a CCIO-8 Expansion Board and
6 * read from or write to it. Up to 8 total CCIO-8 boards can be used.
7 *
8 * Description:
9 * This example sets up COM-0 to control up to 8 CCIO-8 Expansion Boards,
10 * sets all CCIO-8 connectors to be either inputs or outputs depending on the
11 * selected mode. In input mode, pin statuses are printed to the USB serial
12 * port. In output mode, all pins outputs are sequentially turned on then off.
13 *
14 * Requirements:
15 * ** A CCIO-8 Expansion Board, with power wired, and connected to COM-0. Any
16 * other CCIO-8 boards should be chained off of this first board
17 * ** Edit the value of "inputMode" below to select input mode or output mode
18 * ** (For input mode) A number of inputs, like switches, wired to CCIO-8
19 * connectors.
20 * ** (For output mode) A number of outputs, like LEDs, wired to CCIO-8
21 * connectors.
22 *
23 * Links:
24 * ** ClearCore Documentation: https://teknic-inc.github.io/ClearCore-library/
25 * ** ClearCore Manual: https://www.teknic.com/files/downloads/clearcore_user_manual.pdf
26 *
27 *
28 * Copyright (c) 2020 Teknic Inc. This work is free to use, copy and distribute under the terms of
29 * the standard MIT permissive software license which can be found at https://opensource.org/licenses/MIT
30 */
31
32#include "ClearCore.h"
33
34// Specify which ClearCore serial COM port is connected to the "COM IN" port
35// of the CCIO-8 board. COM-1 may also be used.
36#define CcioPort ConnectorCOM0
37
38// Specify which serial to use: ConnectorUsb, ConnectorCOM0, or ConnectorCOM1.
39#define SerialPort ConnectorUsb
40
41// Set this flag to true to use the CCIO-8 connectors as digital inputs.
42// Set it to false to use the CCIO-8 connectors as digital outputs.
43const bool inputMode = true;
44
45uint8_t ccioBoardCount; // Store the number of connected CCIO-8 boards here.
46uint8_t ccioPinCount; // Store the number of connected CCIO-8 pins here.
47
48// Select the baud rate to match the target device.
49#define baudRate 9600
50
51// These will be used to format the text that is printed to the serial port.
52#define MAX_MSG_LEN 80
53char msg[MAX_MSG_LEN + 1];
54
55int main() {
56 // Set up serial communication to display CCIO-8 state.
57 SerialPort.Mode(Connector::USB_CDC);
58 SerialPort.Speed(baudRate);
59 uint32_t timeout = 5000;
60 uint32_t startTime = Milliseconds();
61 SerialPort.PortOpen();
62 while (!SerialPort && Milliseconds() - startTime < timeout) {
63 continue;
64 }
65
66 // Set up the CCIO-8 COM port.
67 CcioPort.Mode(Connector::CCIO);
68 CcioPort.PortOpen();
69
70 // Initialize the CCIO-8 board.
71 ccioBoardCount = CcioMgr.CcioCount();
72 // CCIO_PINS_PER_BOARD is a constant defined in the main ClearCore library
73 // that evaluates to 8.
74 ccioPinCount = ccioBoardCount * CCIO_PINS_PER_BOARD;
75
76 // Print the number of discovered CCIO-8 boards to the serial port.
77 snprintf(msg, MAX_MSG_LEN, "Discovered %d CCIO-8 board", ccioBoardCount);
78 SerialPort.Send(msg);
79
80 if (ccioBoardCount != 1) {
81 SerialPort.Send("s");
82 }
83 SerialPort.SendLine("...");
84 SerialPort.SendLine();
85
86 if (!inputMode) {
87 // Set each CCIO-8 pin to the correct mode. The CCIO-8 pins default to
88 // being an input so the pin mode doesn't need to be set for input mode.
89 for (uint8_t ccioPinIndex = 0; ccioPinIndex < ccioPinCount; ccioPinIndex++) {
90 CcioMgr.PinByIndex(static_cast<ClearCorePins>(CLEARCORE_PIN_CCIOA0 +
91 ccioPinIndex))->Mode(Connector::OUTPUT_DIGITAL);
92 }
93 }
94
95 while (true) {
96 // Make sure the CCIO-8 link is established.
97 if (CcioMgr.LinkBroken()) {
98 uint32_t lastStatusTime = Milliseconds();
99 SerialPort.SendLine("The CCIO-8 link is broken!");
100 // Make sure the CCIO-8 link is established.
101 while (CcioMgr.LinkBroken()) {
102 if (Milliseconds() - lastStatusTime > 1000) {
103 SerialPort.SendLine("The CCIO-8 link is still broken!");
104 lastStatusTime = Milliseconds();
105 }
106 }
107 SerialPort.SendLine("The CCIO-8 link is online again!");
108 }
109
110 // ClearCore can automatically detect when the number of attached CCIO-8
111 // boards changes. Check to see if there's been a change.
112 uint8_t newBoardCount = CcioMgr.CcioCount();
113 if (ccioBoardCount != newBoardCount) {
114 snprintf(msg, MAX_MSG_LEN, "CCIO-8 board count changed from %d to %d.",
115 ccioBoardCount, newBoardCount);
116 SerialPort.SendLine(msg);
117 ccioPinCount = newBoardCount * CCIO_PINS_PER_BOARD;
118 ccioBoardCount = newBoardCount;
119 }
120
121 // With one CCIO-8 board attached, we have control over eight additional
122 // digital I/O connectors. On these connectors, we can perform digital
123 // reads and non-PWM digital writes.
124 if (inputMode) {
125 // Read the digital state of CCIO-8 connectors 0 through 7 as inputs.
126 for (uint8_t ccioPinIndex = 0; ccioPinIndex < CCIO_PINS_PER_BOARD;
127 ccioPinIndex++) {
128 uint16_t state = CcioMgr.PinByIndex(static_cast<ClearCorePins>
129 (CLEARCORE_PIN_CCIOA0 + ccioPinIndex))->State();
130
131 snprintf(msg, MAX_MSG_LEN, "CCIO-A%d: ", ccioPinIndex);
132 SerialPort.Send(msg);
133
134 if (state) {
135 SerialPort.SendLine("ON");
136 }
137 else {
138 SerialPort.SendLine("OFF");
139 }
140 }
141
142 // If you have multiple CCIO-8 boards attached, individual printouts
143 // become hard to read. We can access all of the CCIO-8 bits at once
144 // and print them out in hex.
145 snprintf(msg, MAX_MSG_LEN, "All CCIO-8 Inputs: 0x%0*llX",
146 ccioBoardCount * 2, CcioMgr.InputState());
147 SerialPort.SendLine(msg);
148
149 // The application may be only interested in the transitions of an
150 // input. We can read the rise and fall registers and print them out.
151 snprintf(msg, MAX_MSG_LEN, "CCIO-8 Input Rise: 0x%0*llX",
152 ccioBoardCount * 2, CcioMgr.InputsRisen());
153 SerialPort.SendLine(msg);
154 snprintf(msg, MAX_MSG_LEN, "CCIO-8 Input Fall: 0x%0*llX",
155 ccioBoardCount * 2, CcioMgr.InputsFallen());
156 SerialPort.SendLine(msg);
157
158 SerialPort.SendLine("---------------------");
159
160 Delay_ms(1000);
161 }
162 else {
163 // Write digital HIGH then digital LOW to each CCIO-8 connector.
164
165 SerialPort.SendLine("Writing digital HIGH to each CCIO-8 connector...");
166
167 for (uint8_t ccioPinIndex = 0; ccioPinIndex < ccioPinCount; ccioPinIndex++) {
168 CcioMgr.PinByIndex(static_cast<ClearCorePins>(
169 CLEARCORE_PIN_CCIOA0 + ccioPinIndex))->State(true);
170 Delay_ms(500);
171 }
172
173 SerialPort.SendLine("Writing digital LOW to each CCIO-8 connector...");
174
175 for (int8_t ccioPinIndex = ccioPinCount - 1; ccioPinIndex >= 0; ccioPinIndex--) {
176 CcioMgr.PinByIndex(static_cast<ClearCorePins>(
177 CLEARCORE_PIN_CCIOA0 + ccioPinIndex))->State(false);
178 Delay_ms(500);
179 }
180 }
181 }
182}
#define CCIO_PINS_PER_BOARD
Definition CcioBoardManager.h:46
ClearCorePins
ClearCore PIN definitions.
Definition SysConnectors.h:54
@ CLEARCORE_PIN_CCIOA0
CCIO-8 board 1, connector 0.
Definition SysConnectors.h:89
void Delay_ms(uint32_t ms)
Blocks operations for ms milliseconds.
Definition SysTiming.h:287
uint32_t Milliseconds(void)
Number of milliseconds since the ClearCore was initialized.
uint64_t InputsRisen(uint64_t mask=UINT64_MAX)
Clear on read accessor for rising input edges.
CcioPin * PinByIndex(ClearCorePins connectorIndex)
Accessor for the individual CCIO-8 pin connectors.
volatile const uint8_t & CcioCount()
Accessor for the number of CCIO-8 boards connected to the ClearCore.
Definition CcioBoardManager.h:246
volatile const uint64_t & InputState()
Accessor for all the CCIO-8 pins' filtered input states.
Definition CcioBoardManager.h:349
uint64_t InputsFallen(uint64_t mask=UINT64_MAX)
Clear on read accessor for falling input edges.
volatile const bool & LinkBroken()
Accessor for the CCIO-8 link status.
Definition CcioBoardManager.h:261
int16_t State() override
In input mode, get the connector's last filtered sampled value. In output mode, get the connector's o...
virtual ConnectorModes Mode() override
Get the connector's operational mode.
Definition CcioPin.h:71