-
Notifications
You must be signed in to change notification settings - Fork 0
Tristin driver tutorial #4
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
Open
Nexnar
wants to merge
7
commits into
main
Choose a base branch
from
Tristin_Driver_Tutorial
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
114365e
modify tutorial for PlatformIO
bquan0 91e016a
update README.md to match arduino version
bquan0 c6639e1
Tristin Yun Driver Dev Tutorial 2024
Nexnar c3b85a6
V2 main.cpp
Nexnar cc9cf4c
V2 mcp23017.cpp
Nexnar 8db1e0a
Update main.cpp
Nexnar 1682050
Update main.cpp
Nexnar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| .build | ||
| .mbed | ||
| projectfiles | ||
| *.py* | ||
| mbed-os | ||
| BUILD | ||
| .pio | ||
| .vscode/.browse.c_cpp.db* | ||
| .vscode/c_cpp_properties.json | ||
| .vscode/launch.json | ||
| .vscode/ipch | ||
| *.txt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| // See http://go.microsoft.com/fwlink/?LinkId=827846 | ||
| // for the documentation about the extensions.json format | ||
| "recommendations": [ | ||
| "platformio.platformio-ide" | ||
| ], | ||
| "unwantedRecommendations": [ | ||
| "ms-vscode.cpptools-extension-pack" | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "files.associations": { | ||
| "array": "cpp", | ||
| "deque": "cpp", | ||
| "string": "cpp", | ||
| "unordered_map": "cpp", | ||
| "vector": "cpp", | ||
| "string_view": "cpp", | ||
| "initializer_list": "cpp", | ||
| "*.tcc": "cpp" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
|
|
||
| This directory is intended for project specific (private) libraries. | ||
| PlatformIO will compile them to static libraries and link into executable file. | ||
|
|
||
| The source code of each library should be placed in an own separate directory | ||
| ("lib/your_library_name/[here are source files]"). | ||
|
|
||
| For example, see a structure of the following two libraries `Foo` and `Bar`: | ||
|
|
||
| |--lib | ||
| | | | ||
| | |--Bar | ||
| | | |--docs | ||
| | | |--examples | ||
| | | |--src | ||
| | | |- Bar.c | ||
| | | |- Bar.h | ||
| | | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html | ||
| | | | ||
| | |--Foo | ||
| | | |- Foo.c | ||
| | | |- Foo.h | ||
| | | | ||
| | |- README --> THIS FILE | ||
| | | ||
| |- platformio.ini | ||
| |--src | ||
| |- main.c | ||
|
|
||
| and a contents of `src/main.c`: | ||
| ``` | ||
| #include <Foo.h> | ||
| #include <Bar.h> | ||
|
|
||
| int main (void) | ||
| { | ||
| ... | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| PlatformIO Library Dependency Finder will find automatically dependent | ||
| libraries scanning project source files. | ||
|
|
||
| More information about PlatformIO Library Dependency Finder | ||
| - https://docs.platformio.org/page/librarymanager/ldf.html |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| ; PlatformIO Project Configuration File | ||
| ; | ||
| ; Build options: build flags, source filter | ||
| ; Upload options: custom upload port, speed and extra flags | ||
| ; Library options: dependencies, extra library storages | ||
| ; Advanced options: extra scripting | ||
| ; | ||
| ; Please visit documentation for the other options and examples | ||
| ; https://docs.platformio.org/page/projectconf.html | ||
|
|
||
| [env:nucleo_f303re] | ||
| platform = ststm32 | ||
| board = nucleo_f303re | ||
| framework = arduino |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| #include <Arduino.h> | ||
| #include "mcp23017.h" | ||
|
|
||
| // TODO: declare mcp23017 object | ||
| Mcp23017 mcp23017(0x20); | ||
|
|
||
|
|
||
| void setup() { | ||
| // TODO: initialize i2c and mcp23017 object | ||
| Wire.begin(); | ||
| //Start serial | ||
| Serial.begin(9600); | ||
| Serial.println("Starting process..."); | ||
| } | ||
|
|
||
| void loop() { | ||
| // TODO: Write tests here | ||
| uint8_t directions[8] = {1, 1, 1, 1, 1, 1, 0}; | ||
| mcp23017.begin(directions); | ||
| mcp23017.get_state(0); | ||
Nexnar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| mcp23017.set_state(0, 1); | ||
bquan0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,41 +1,108 @@ | ||
| #include "mcp23017.h" | ||
| #include <errno.h> | ||
|
|
||
|
|
||
| // TODO (optional): Define macros for useful register below: | ||
|
|
||
| #define ioDirAddr 0x00 | ||
Nexnar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| #define GPIOAddr 0x09 | ||
|
|
||
| // TODO: Initialize i2cBus member | ||
| Mcp23017::Mcp23017(int addr, I2C* i2cBus) { | ||
|
|
||
| Mcp23017::Mcp23017(int addr) : addr(addr) { //member initializxation | ||
| Wire.begin(addr); // Start wire library and join i2c bus | ||
Nexnar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| uint8_t Mcp23017::get_dir(int pin) { | ||
| return 0; | ||
| // request 1 byte from the register at 0x00 (iodir) | ||
| // and store in the variable byteRead | ||
| Wire.requestFrom(ioDirAddr, 1); | ||
Nexnar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| uint8_t byteRead; | ||
| while (Wire.available()){ | ||
| byteRead = Wire.read(); | ||
| } | ||
| //moves desired bit to LSB and compares to 1 | ||
| uint8_t pinDir = (byteRead >> pin) & 1; | ||
| return pinDir; | ||
| } | ||
|
|
||
|
|
||
| // TODO: Read from state register | ||
| uint8_t Mcp23017::get_state(int pin) { | ||
| return 0; | ||
| // Same functionality as above method | ||
| Wire.requestFrom(GPIOAddr, 1); | ||
Nexnar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| uint8_t byteRead; | ||
| while (Wire.available()){ | ||
| byteRead = Wire.read(); | ||
| } | ||
| uint8_t pinDir = (byteRead >> pin) & 1; | ||
| return pinDir; | ||
| } | ||
|
|
||
| // TODO: Write to directions register | ||
| int Mcp23017::set_dir(int pin, uint8_t dir) { | ||
| return 0; | ||
| Wire.requestFrom(ioDirAddr, 1); | ||
| uint8_t byte; | ||
| while (Wire.available()){ | ||
| byte = Wire.read(); | ||
| } | ||
| if(dir == 1){ //If we want the pin to be on, we use OR logic | ||
| byte |= (dir << pin); // shift the mask by the pin amount | ||
| } | ||
| else if(dir == 0){ //Use AND NOT logic to get the pin to be off | ||
| byte &= ~(dir << pin); | ||
| } | ||
| Wire.beginTransmission(ioDirAddr); //transmit the byte | ||
Nexnar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Wire.write(byte); | ||
| Wire.endTransmission(); | ||
|
|
||
| return get_dir(pin); //checks if what we did worked | ||
| } | ||
|
|
||
| // TODO: Write to state register | ||
| int Mcp23017::set_state(int pin, uint8_t val) { | ||
| return 0; | ||
| Wire.requestFrom(GPIOAddr, 1); | ||
| uint8_t byte; | ||
| while (Wire.available()){ | ||
| byte = Wire.read(); | ||
| } | ||
| if(val == 1){ | ||
| byte |= (val << pin); | ||
| } | ||
| else if(val == 0){ | ||
| byte &= ~(val << pin); | ||
| } | ||
| Wire.beginTransmission(GPIOAddr); | ||
| Wire.write(byte); | ||
| Wire.endTransmission(); | ||
|
|
||
| return get_state(pin); | ||
| } | ||
|
|
||
|
|
||
| // Verifies that the device is accessible over I2C and sets pin directions | ||
| int Mcp23017::begin(uint8_t directions[8]) { | ||
| int rc; | ||
| Wire.beginTransmission(ioDirAddr); | ||
| //this method with nothing overloaded will return 0 if successful | ||
| //and a different integer if it fails | ||
| rc = Wire.endTransmission(); | ||
Nexnar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if(rc == 0){ | ||
| Serial.println("IODIR Address verified at 0x"); | ||
| Serial.println(ioDirAddr, HEX); | ||
| } | ||
| else { | ||
| Serial.println("No device found at address 0x"); | ||
| Serial.println(ioDirAddr, HEX); | ||
| } | ||
|
|
||
| // TODO: Add device ID check | ||
| //set the direction of each of the pins to the desired value | ||
| for(int i = 0; i < 8; i++){ | ||
| set_dir(i, directions[i]); | ||
| } | ||
|
|
||
| return 0; | ||
| //returns 1 byte from IODIR to see if the addresses were successfully changed | ||
| Wire.requestFrom(ioDirAddr, 1); | ||
Nexnar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| uint8_t byte; | ||
| while (Wire.available()){ | ||
| byte = Wire.read(); | ||
| } | ||
| return byte; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
|
|
||
| This directory is intended for PlatformIO Test Runner and project tests. | ||
|
|
||
| Unit Testing is a software testing method by which individual units of | ||
| source code, sets of one or more MCU program modules together with associated | ||
| control data, usage procedures, and operating procedures, are tested to | ||
| determine whether they are fit for use. Unit testing finds problems early | ||
| in the development cycle. | ||
|
|
||
| More information about PlatformIO Unit Testing: | ||
| - https://docs.platformio.org/en/latest/advanced/unit-testing/index.html |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.