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

Return to SDK Examples for Microchip Studio

1/*
2 * Title: SpiComPortInitialization
3 *
4 * Objective:
5 * This example demonstrates how to configure a COM port for use with an
6 * SPI device.
7 *
8 * Description:
9 * This example will explain the basic configuration settings of an SPI
10 * device then perform a brief transaction with the SPI device connected to
11 * COM-0.
12 *
13 * Requirements:
14 * ** An SPI device connected to COM-0.
15 *
16 * Links:
17 * ** ClearCore Documentation: https://teknic-inc.github.io/ClearCore-library/
18 * ** ClearCore Manual: https://www.teknic.com/files/downloads/clearcore_user_manual.pdf
19 *
20 *
21 * Copyright (c) 2020 Teknic Inc. This work is free to use, copy and distribute under the terms of
22 * the standard MIT permissive software license which can be found at https://opensource.org/licenses/MIT
23 */
24
25#include "ClearCore.h"
26
27// Select the baud rate to match the target device.
28#define baudRate 80000
29
30// Select the clock polarity to match the target device. The clock polarity
31// setting indicates whether the device expects a low signal when idle or a
32// high signal when idle. It also indicates whether the leading or trailing
33// edge of the clock cycle are rising or falling edges.
34// Selecting SCK_LOW indicates that SCK is low when idle and the leading edge
35// of the clock cycle is a rising edge while the trailing edge is a falling
36// edge.
37// Selecting SCK_HIGH indicates that SCK is high when idle and the leading edge
38// of the clock cycle is a falling edge while the trailing edge is a rising
39// edge.
40// The default value for a COM connector's clock polarity is SCK_LOW.
41#define clockPolarity SerialDriver::SCK_LOW
42
43// Select the clock phase setting to match the target device. The clock phase
44// setting indicates whether data is sampled or changed on the leading or
45// trailing edge in the clock cycle.
46// Selecting LEAD_SAMPLE indicates that data is sampled on the leading edge and
47// changed on the trailing edge.
48// Selecting LEAD_CHANGE indicates that data is sampled on the trailing edge
49// and changed on the leading edge.
50// The default value for a COM connector's clock phase is LEAD_CHANGE.
51#define clockPhase SerialDriver::LEAD_CHANGE
52
53// Define which COM serial port connector to use: ConnectorCOM0 or ConnectorCOM1
54#define SpiPort ConnectorCOM0
55
56int main() {
57 // Configure the COM port for our SPI device then open the port.
58 SpiPort.Mode(Connector::SPI);
59 SpiPort.Speed(baudRate);
60 SpiPort.SpiClock(clockPolarity, clockPhase);
61 SpiPort.PortOpen();
62
63 // Open the SPI port on ConnectorCOM0.
64 SpiPort.SpiSsMode(SerialDriver::LINE_ON);
65 // Output some arbitrary sample data to the SPI device. This data is not
66 // required for set up, just to demonstrate the transfer process.
67 SpiPort.SpiTransferData('a');
68 SpiPort.SpiTransferData(98);
69 SpiPort.SpiTransferData(0x63);
70 // Close the port.
71 SpiPort.SpiSsMode(SerialDriver::LINE_OFF);
72}