Skip to content

Breakout I2C UART SPI AnalogRead AnalogWrite #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
204 changes: 204 additions & 0 deletions README.md

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions examples/analogRead/analogRead.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Breakout Carrier - analogRead

The sketch shows how to use analog inputs of the Breakout Carrier.

The circuit:
- Portenta H7
- BreakOut Carrier

This example code is in the public domain.
*/
#include <Arduino_PortentaBreakoutCarrier.h>

const byte ANALOG_PIN_NUMBER = 8;
const int ANALOG_RESOLUTION = 16;

// The access to each analog input is made using the peripheral silk name.
breakoutPin analogPin[] = {ANALOG_A0, ANALOG_A1, ANALOG_A2, ANALOG_A3,
ANALOG_A4, ANALOG_A5, ANALOG_A6, ANALOG_A7};

void setup() {
Serial.begin(9600);
while (!Serial);

// Max analog resolution is 16bits
Breakout.analogReadResolution(ANALOG_RESOLUTION);
}

void loop() {
// Loop through all the available inputs and print input value
for(int i = 0; i < ANALOG_PIN_NUMBER; i++) {
int w = Breakout.analogRead(analogPin[i]);
Serial.print("A");
Serial.print(i);
Serial.print(": ");
Serial.println(w);

delay(1000);
}
}
8 changes: 8 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
BreakoutCarrierClass KEYWORD1
Breakout KEYWORD1
breakoutPin KEYWORD1
I2C_0 KEYWORD1
I2C_1 KEYWORD1
I2C_2 KEYWORD1
UART0 KEYWORD1
UART1 KEYWORD1
UART2 KEYWORD1
UART3 KEYWORD1
SPI_0 KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,20 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include <Arduino.h>
#include <pins_arduino.h>
#include <mbed.h>
#include <Wire.h>
#include <SPI.h>
#include "utility/Analog/Analog.h"

#define LAST_ARDUINO_PIN_NUMBER LEDB + 1
typedef enum {
SPI0_CS = -1,
SPI0_CK = -1,
SPI0_MISO = -1,
SPI0_MOSI = -1,
SPI0_CIPO = -1,
SPI0_COPI = -1,
SPI1_CS = LAST_ARDUINO_PIN_NUMBER + PI_0,
SPI1_CK = LAST_ARDUINO_PIN_NUMBER + PI_1,
SPI1_MISO = LAST_ARDUINO_PIN_NUMBER + PC_2,
SPI1_MOSI = LAST_ARDUINO_PIN_NUMBER + PC_3,
SPI1_CIPO = LAST_ARDUINO_PIN_NUMBER + PC_2,
SPI1_COPI = LAST_ARDUINO_PIN_NUMBER + PC_3,
UART2_TX = LAST_ARDUINO_PIN_NUMBER + PG_14,
UART2_RX = LAST_ARDUINO_PIN_NUMBER + PG_9,
UART2_RTS = -1,
Expand Down Expand Up @@ -78,12 +81,12 @@ typedef enum {
PDM_CK = LAST_ARDUINO_PIN_NUMBER + PE_2,
SPDIF_RX = -1,
SPDIF_TX= -1,
USBHS_ID = LAST_ARDUINO_PIN_NUMBER + PJ_6,
USBHS_DN = -1,
USBHS_DP = -1,
USBFS_ID = -1,
USBFS_DN = LAST_ARDUINO_PIN_NUMBER + PA_12,
USBFS_DP = LAST_ARDUINO_PIN_NUMBER + PA_11,
USB1_ID = LAST_ARDUINO_PIN_NUMBER + PJ_6,
USB1_DN = -1,
USB1_DP = -1,
USB0_ID = -1,
USB0_DN = LAST_ARDUINO_PIN_NUMBER + PA_12,
USB0_DP = LAST_ARDUINO_PIN_NUMBER + PA_11,
SD_WP = -1,
SD_CD = -1,
SD_D0 = LAST_ARDUINO_PIN_NUMBER + PB_14,
Expand Down Expand Up @@ -142,8 +145,8 @@ typedef enum {
ANALOG_REFP = -1,
ANALOG_A7 = LAST_ARDUINO_PIN_NUMBER + PA_6,
ANALOG_A6 = LAST_ARDUINO_PIN_NUMBER + PA_4,
ANALOG_A5 = LAST_ARDUINO_PIN_NUMBER + PC_3,
ANALOG_A4 = LAST_ARDUINO_PIN_NUMBER + PC_2,
ANALOG_A5 = LAST_ARDUINO_PIN_NUMBER + PC_3_ALT0,
ANALOG_A4 = LAST_ARDUINO_PIN_NUMBER + PC_2_ALT0,
ANALOG_A3 = LAST_ARDUINO_PIN_NUMBER + PC_3,
ANALOG_A2 = LAST_ARDUINO_PIN_NUMBER + PC_2,
ANALOG_A1 = LAST_ARDUINO_PIN_NUMBER + PA_1,
Expand All @@ -166,6 +169,7 @@ typedef enum {
USB_EN = -1
} breakoutPin;


class BreakoutCarrierClass {
public:
int pinMode(breakoutPin pin, PinMode mode) {
Expand All @@ -182,12 +186,77 @@ class BreakoutCarrierClass {
}
return -1;
}
bool digitalRead(breakoutPin pin){
bool digitalRead(breakoutPin pin) {
if (pin > -1) {
return ::digitalRead((int)pin);
}
return -1;
}
void analogWrite(breakoutPin pin, int val) {
if (pin > -1) {
BreakoutPWM::analogWrite((int)pin, val);
}
return;
}
void analogWriteResolution(int bits) {
BreakoutPWM::analogWriteResolution(bits);
}
int analogRead(breakoutPin pin) {
if (pin > -1) {
int val;
switch(pin) {
case ANALOG_A7:
val = A7;
break;
case ANALOG_A6:
val = A6;
break;
case ANALOG_A5:
val = A5;
break;
case ANALOG_A4:
val = A4;
break;
case ANALOG_A3:
val = A3;
break;
case ANALOG_A2:
val = A2;
break;
case ANALOG_A1:
val = A1;
break;
case ANALOG_A0:
val = A0;
break;
default:
return -1;
}
return ::analogRead(val);
}
return -1;
}
void analogReadResolution(int bits) {
::analogReadResolution(bits);
}
MbedI2C I2C_0;
MbedI2C I2C_1;
MbedI2C I2C_2;
UART UART0;
UART UART1;
UART UART2;
UART UART3;
MbedSPI SPI_0;
BreakoutCarrierClass() : I2C_0(PH_8,PH_7),
I2C_1(PB_7,PB_6),
I2C_2(PH_12,PH_11),
UART0(PA_0, PI_9, NC/*PI_10*/, NC/*PI_13*/),
UART1(PA_9, PA_10, NC/*PI_14*/, NC/*PI_15*/),
UART2(PG_14, PG_9, NC, NC),
UART3(PJ_8, PJ_9, NC, NC),
SPI_0(PC_2, PC_3, PI_1)
{
}
};

BreakoutCarrierClass Breakout;
Expand Down
73 changes: 73 additions & 0 deletions src/utility/Analog/Analog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
portentaBreakoutAnalog.cpp
Part of Arduino - http://www.arduino.cc/

Copyright (c) 2018-2019 Arduino SA

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/

#include "Arduino.h"
#include "pins_arduino.h"
#include "pinDefinitions.h"
#include "Analog.h"

namespace BreakoutPWM {

static int write_resolution = 8;
HRTIMPWMClass *hrtimpwm = NULL;

void analogWrite(pin_size_t pin, int val)
{
if (pin >= PINS_COUNT) {
return;
}

float percent = (float)val/(float)((1 << write_resolution)-1);

if (digitalPinToPinName(pin) == PG_7) {
if(hrtimpwm == NULL) {
hrtimpwm = new HRTIMPWMClass();
hrtimpwm->period_us(1300); //Max period for HRTIM
}
if (percent < 0) {
delete hrtimpwm;
hrtimpwm = NULL;
} else {
hrtimpwm->write(percent);
}
} else {
mbed::PwmOut* pwm = digitalPinToPwm(pin);
if (pwm == NULL) {
pwm = new mbed::PwmOut(digitalPinToPinName(pin));
digitalPinToPwm(pin) = pwm;
pwm->period_us(1300); //700Hz
}
if (percent < 0) {
delete pwm;
digitalPinToPwm(pin) = NULL;
} else {
pwm->write(percent);
}
}
}

void analogWriteResolution(int bits)
{
write_resolution = bits;
}

}
Loading