Feather M0 Express loading CircuitPython & Arduino flash access with Tony D!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Music] hey it's Tony from Adafruit and in this video we're gonna look at this board right here this is the let me get out of the light there this is the feather M 0 Express so it's a brand new feather M 0 it's got some cool features and is built specifically for circuit Python so that you can run Python code on here and we'll look at a couple neat features and extra things this has compared to some of the other feather boards that might make this really interesting so we'll dive in I'll show you how to load circuit Python on to this board because right now it's kind of a brand new board and just ships with a default kind of test sketch on it so that you can use it with Arduino but I'll show you how to load circuit python on here play a teeny bit with circuit Python but I've got all kinds of other videos that I'll put a link to in the description below so you can look at all this circuit Python and micropyle on videos to see more details on how to use it with hardware but we'll see how to load circuit Python and then we'll also briefly look at a neat little library that lets you interact with this memory chip that's on this board so that if you're using circuit Python you can store files on there it has an internal file system and you can also access those files from Arduino so it's kind of neat in that if you use circuit Python and then you realize you want to use Arduino for some other stuff you can actually share files between the two systems using a little flash library that we'll look at so a couple things will dive into here and kind of get started with so let's jump into the main view real fast and I'm hoping that the power does not go out during this stream because we're getting like gusts of 50 mile an hour winds apparently pretty soon so we'll see what happens anyway so this is the board in the upper right hand corner this is the feather M 0 and let me bring this up right here this is a guide and I'll put a link to this in the description when this is up on youtube so look below and you'll see a link to this but this is for the product so it's the feather M 0 Express brand new board from Adafruit and it's called an M 0 because it has the Sam D 21 processor right here so this is the same processor that's used in all of the other m0 boards things like the feather m0 proto board the m0 8 along there's the m0 with the Wi-Fi radio the wink 1,500 lots of fun boards have this it's based on the Arduino 0 which was this 70 21 based Arduino really nice processor has more memory and more speed than your typical Arduino but still a pretty low-cost board and what makes this one special so what the express means in the name right here because we already have a feather m0 boards and if you've seen some of the videos I've done I've shown how to load circuit Python onto those boards but the one big difference with this board is this little chip right here so this is actually an extra thing that's only on the Express boards it's a little flash memory chip so this is just like the memory that's on the processor so the processor itself has two and fifty 250 six kilobytes of flash memory which basically that's a memory that you can write to and that will be remembered when it's powered up and powered down that's where you store your code so when you write an Arduino sketch it gets stored into that 256 K of memory that's on your processor that's also where the micro Python or circuit Python code is stored so things like the firmware itself like the actual Python interpreter has to fit within that 256 kilobytes memory but then also a little bit of that memory is taken up to say you know here's where your Python scripts go because when you write a Python script it's a text file and that has to live somewhere and so on the m0 boards all of that lives together on that 256 kilobytes of flash memory on your chip now the problem is micro Python and surrogate Python firmware needs a lot of space because it needs to build in all of the potential functions that you might call because it's a dynamic language it doesn't know until it runs a program whether you're going to interact with neopixels you know maybe you're going to talk to the pulse width modulation Hardware on the board maybe you're going to talk to the analog to digital converter all kinds of different peripherals you might talk to all of that code has to be available and on the chip so that when your Python program says give me an analogue to digital converter reading or set the pulse width modulation output to this value it's got to call that code it has to be there whereas an Arduino it can actually be a little bit smart sometimes and realize you know if your sketch is only using pulse width modulation it's not using the analog to digital converter it might not even compile in the code to talk to the analog to digital converter so that can save some the space so the tricky thing is balancing how many features you build into the circuit Python or the micro Python firmware versus how much space do you leave for the programs that you want to use because all of the space that's not taken up by the micropyle on firmware is what you have left for your Python programs and on the m0 boards what we've generally done is taken 64 kilobytes and given that to you the user for your Python scripts so you can store things like you know your main dot pi any libraries that you want to put on the board other data files things like that you just have 64 kilobytes for that and then the micro Python firmware the circuit Python firmware has the remaining space like that 192 or so okay of space available so that's a little bit limiting unfortunately we've we've started to add more features circa Python we've run into a lot of problems or it's like boy it'd be really cool to add things like infrared LED support you know like IR code sending and receiving but we don't have the space maybe for that necessarily so that's where that little flash chip comes into play this thing right here so this is a two megabyte flash chip that's connected to the spy bus so it's just another spy device it's used the serial peripheral interface and it's connected to the Sandy 21 microprocessor so it's just an additional set of memory and a significant memory upgrade so two megabytes that's you know quite a bit morning that's eight times or so more space than what's on the microprocessor itself and that's really cool because with circuit Python what we can do is say all of your Python scripts and code lives on that 2 megabyte flash chip and then all of the circuit Python firmware lives on the 256 kilobytes of microprocessor flash space so that gives more space for circuit Python code the firmware can add more features and more functionality into it and then it gives you more space for your Python scripts a lot more space with two megabytes you know you can store quite a large program on there but what you'll end up really storing I better probably a lot of libraries and data files external things on there so that's really handy and you might wonder well could you also use some of that two megabytes to store like an even bigger circuit Python firmware like a two megabyte size one and you can't really unfortunately because when the processor runs it runs code out of its flash memory and as far as I know there's no way for it to run code off of an external chip because when it actually gets your Python program from this little flash chip it has to talk to it just like a spy device and in fact if you really want it you could buy a little flash chip like this put it on a breadboard or wired up to you know put on a custom circuit board and connect it to an D 21 microprocessor or even an Arduino zero or feather M zero and you could make your own version of this board just connect a flash chip over the spy interface for this and actually I think we have a little spy chip in the shop now thinking about it lab that uses a breadboard compatible interface so you could build your own but the tricky thing though is that these these processors aren't meant to run their code off of that flash chip it's more for getting data off of that flash chip and that data might be a Python file that's then loaded in memory and run my circuit Python so that's the big standout feature for the Express boards is the addition of that flash memory which will give you more space for program code and then more space for your Python code also that you're putting on here there's also one of the little change there's a little neopixel right here that well show you a little bit more about this so it's it's connected to the processor and we actually use that to do things like show some status info eventually I think will probably let you control yourself like if you want to you know have a little status indicator for your hardware so it's a handy little thing to add to the board and we'll look at what that thing does so let's kind of get started like I said it's a brand new product and everything is still kind of in beta for some of this a certified line in particular so you know we're still developing the firmware and lots of things are changing so keep an eye on kind of the latest news for it check out we also have a micro Python newsletter so if you aren't subscribed to that check out Adafruit daily comm and subscribe to that and you can keep on top of all the latest news for things that we're up to in the circuit Python and micro Python world anyways this is the guide so if you get this product you'll want to check out this guide it comes just as a feather board and you can solder some headers onto here so you can connect all the feather accessories to it you know things like the different wings that we have there's the nice little LED matrix there's LED backpacks there's Charlie flex display sensors motor drivers all kinds of fun stuff that's compatible with this and it's it's pretty straightforward just soldered those headers on and you don't even have to sign them on solder them on I don't have them on this board but yeah you know it's it's probably useful if you put headers on this board and then once you have it together then there are two things you can do so by default the board does not right now come with circuit Python installed on it so it just comes with a test sketch that's kind of validating all the hardware in the future it might come with circa Python on it but for now you'll need to load it manually or you can also load it and use it just like an Arduino M zero board or a feather M zero board so just like you've used the feather M zero you can use this in the Arduino IDE you can write code using Arduino you can use all of the Arduino libraries that support this board and even an extra one that I'll show you at the end of this to access from the flash memory but it's just like any other Arduino board and so this guide walks through how to set up the Arduino IDE so you add this extra board URL and then this will make the board available on the board manager so that you can install this and use it we'll look at this later in the video but what you're probably interested in is how do you get started on running on this board and so in this guide there's a great description here of how to load the latest version of circuit python onto here and there are two ways to do this this is another thing that's a little bit different about the feather M 0 Express board its shipping with a bootloader that's more advanced than the bootloader that we've shipped on the other m0 feather boards and this one's actually really cool it has a special USB drive mode so if you if you've loaded circuit Python on to a Sam D 21 or a feather M 0 in the past you've probably had to use a command-line tool this boss SC tool that just talked to the chip and sends it all of the data it's it works it's fine but it's a little bit tricky sometimes and you have to install some software to use it so we were looking at is there an easier way to use this with custom firmwares and I'll show you something neat so what you want to do to load the circuit python firmware onto here first you need to download the latest version of the firmware so from this guide it links to this page right here which is the releases page for circuit python so look for the latest release right now zero point nine point three the latest where these should be at the top of this page and you want to look for all of the files that are here and there's one file in particular that you want so look at Adafruit circuit Python feather M zero Express so that's the board that you're looking for and you notice there are other boards there's like the M zero ADA logger the basic M zero the huzzah that's the esp8266 the Arduino zero so all kinds of other firmwares but look for the feather M zero Express and then notice there are two files here there's a dot bin and a dot UF to file so the dot u f2 is what you want for this special USB Drive programming mode with the bootloader so click this download this file i've actually download this one ahead of time but grab this dot you have to file and then i'll show you what you need to do it's really simple and easy hoops let me load up the file here so I'll just show you here's that dot you have to file right here and then to load the firmware so forget if you've used that Bossa c2 will forget everything you know about that it's way easier now we're gonna plug in the board and after you plug in the board you want to double tap the reset button in the corner right here so that reset button you just press it twice real quickly and you'll see this red LED will start to pulse it's going on and off that's just telling you that the bootloader is loaded you might notice this thing just popped up right here so I have a new Drive that just appeared and it's called feather boot and this is really cool so this is bootloader for this Sam D 21 processor it looks like a USB Drive so I've connected it to my Mac computer it looks like a USB memory stick and it actually has I can download this current dot u f2 this is the current firmware that's loaded on this chip maybe not super useful to you right now but if you're debugging or something you can actually pull the firmware off the chip which is kind of handy but you can also say okay I want to put firmware onto this chip so I can take this dot you have to file that I downloaded earlier that feather M zero Express u f2 that has the circuit Python firmware and all I have to do is just drag it onto this Drive and it's gonna recognize that it had a u f2 file sent to it it just programmed it and you might have seen the red LED was just flashing really quickly that's just telling you that it's programming the firmware that you copied over and then you might have just noticed this little circuit PI Drive just popped up so that's cool and that's a really good sign because what excuse me what just happened was the the chip just reprogrammed itself with the circuit python firmware by just dragging that u f2 file onto the board in that bootloader mode it reprogrammed it and you didn't have to have anything installed you don't need the boss a/c tool installed you could do this on a Chromebook in theory or any board anything that has USB Drive support this should work on it so that's pretty cool and just to show that this really is circuit Python on here let's connect to serial rebel so it shows up as a USB device eleven five two hundred baht by the way from Windows you'll need to install a driver to access the serial rebel look at the guide we mentioned there's a link to an installer tool that gives you the driver that you need for this but I open this up and this is showing me the Red Bull if I press ENTER you know I've got a hello world from circuit Python so that's pretty cool and so now this board is running circuit Python now you might have noticed the neopixel was doing something it was it was lighting up it was showing different things so let's uh with this serial rebel open I'll maybe explain what the neopixel is doing I'm gonna press control D D as in dog to reset the so it's back into kind of its normal startup State and this is kind of cool so you can see the pixel is pulsing this green color and that's telling me that circuit python is running and it's in its auto reset mode so it's waiting for me to save a file onto this board like a main dot PI script and then it will run the contents of that script so let's do that let's we'll keep our serial ripple open but I'm going to open a new terminal window and I'm gonna go to the circuit python drive and if you're on a Mac it's in the slash volumes folder so under here and you can see I have a circuit PI directory so this is the circuit python drive and there's nothing in there right now but these are all the files that would be on the circuit python board and if I want to I can say let's edit my main dot pi and let's just have it print out hello world from circuit Python and let's save that oops I press the wrong key right there control there we go okay so I just saved that and this is cool so if I go back to the serial repple notice this text is new right here so it noticed that I saved the main dot PI file to the board and it ran it so you can see here's the main dot pi output hello world from circa python and you might actually see there's sometimes some noise from the way I have my USB cable setup here it's sometimes sent some noise that kills this connection so there's sometimes a little bit of lankiness hey that's it's a little bit of beta stuff but that's pretty cool that it ran my script when I saved it when the LED was in that green pulsing mode so that's telling me I'm waiting for you to save a file like a main dot PI and then it's just gonna run it and so whenever you see that green LED then you know it's in that auto run mode now you notice it just jumped into this white color and that's basically if I touch anything if I send any input in the serial ripple like I just did a moment ago that actually turns off that auto run mode and so you see the green pulsing goes away and it just turns it white right now on these colors whoo they might change in the future but for now this these are the colors uh we're showing the status with so it's just a good thing to kind of know so you can understand when you plug a board in you don't necessarily have to open up the serial repple to look and see okay like what mode is the board in is it ready to run files or if I save the file on here and I don't see the output or doing anything that I expect I can look and see oh yeah okay it's not flashing green so it must be waiting for me to type in commands and things at the serial repple here whereas you know if I reset the board again it's in that green state and it's waiting for me to save a file and it's gonna rerun it there so that's pretty cool you might also notice too like if I copy a file on to the board and no that's I don't know if I have a good example to show with that I know let's let's try something let's let's copy our circuit Python firmware onto the board well actually here let's let's copy this a zip file on the board this is file doesn't really mean anything but this will just demonstrate what's happening here so see how the LED is flashing red-and-blue and different things like that so what happened there was when I just dragged this zip file onto the board and if I look at the board you can see that zip files on there now this this flashes red to indicate that there's something being sent over the USB connection so like a file is being transferred to the board so it's just kind of good to know it's like a status indicator to say hey something's happening you know you're sending data to the board so that you can kind of know like you know if you see this thing flashing red a lot something's writing to the usb or maybe reading the data from the usb on the board so just another handy little indicator there and you can see after I copied that zip file because it was pulsing green before when it finished copying that zip file the board reset and it ran the main dot pi again you know even though I didn't copy a main dot PI file over just any time I write to the board it's gonna reset when it's in that pulsing green mode the auto run mode for this so so that's pretty cool then again if I press something in the serial terminal here in the ripple that turns off the auto run mode turns white and so now you know a copy file is over it's not going to rerun have to manually either reset the board or like login to the serial terminal here and run things itself so that's pretty cool those are the basic features of the m0 Express board so it's just like an Arduino feather' m0 but it has that extra flash chip and so that's the cool thing I mean you can notice you know on this circuit Python Drive because again we start a Python on the m0 boards we make them appear as a drive on your computer notice you know I copied over this 93 kilobyte zip file you can't do that on a basic m0 board because again we only give you 64 kilobytes of space for your user file system that's where you store your Python scripts but on the Express boards you've got up to 2 megabytes of space so that that's pretty cool you can copy larger files onto the board you can write more advanced Python code now there is a limit though because whatever Python scripts you copy onto the board still have to be able to fit into the memory of the m0 processor for it to run so you can't write like a 2 megabyte large dot PI file and run that entire file it's it's only you know you have to break things up into smaller files and import just small bits there but it's still kind of nice that you can have a lot of libraries copied onto the board and if you're not using all of them you're not gonna infer any memory cost it's only just you know you import the modules that you care about for what you want to use and then it's also handy to and that you can use that to store data files and read them from your Python script for example like you know maybe store images or text or things like that and as long as you don't try and load the whole thing into memory then you can still access it all from the board so ok so that's pretty cool let's look at 100k let's see Scott was mentioning let's see if I make a syntax error let's see oh right I forgot there is a neat thing that we do so let's edit that main dot pipe first I'm gonna reset the board so I'll show off one other feature that Scott just reminded me of so I'm in the auto run mode you know it's flashing green it's telling me hey save a file and I'll run it let's go back and edit that main dot pi that's on the board and let's put a syntax error in here so let's forget to put a parenthesis in here and let's write this out and now let's see what happens okay so it just ran the board it just ran my script and it just failed so now I have my serial repple open and so I actually saw you know ran main dot PI it found this syntax error but you can see it's actually flashing so it's if I remember quickly it's flashing out the line number that has an error so line three syntax error 'invalid syntax in my main dot pi and so you can see those three flashes that are saying hey something's wrong you know I tried to run your script and SOS you know you've got an error there so cool little indicator I mean this this could give you a workflow where like on a Chromebook or even an Android device or anything that has just USB file access as long as you've got a text editor you can write code copy it onto your board and then see if it you know if there was an error or if it ran successfully so that's pretty cool pretty handy and then you know just to show that this works if I edit this file and if I fix that error so if we put the print the seat back and write the file out again then you see it runs and then we're back to the pulsing green mode so it ran my script and if I go back to my output you can see yeah I ran my bank mean not pi and everything worked right there so kind of cool thing neat little feature of Python again because we've got that little new pixel on here we can do fun stuff like that so okay so that's one thing I wanted to show and then real quickly I'll show a little integration so again like I mentioned you can use this board with Arduino IDE so you can program this just like any other m0 board and you know use all the Arduino libraries so this is great in that you know again like circa Python is still in beta you know we're still adding new features to it it's it's you know it's a growing thing so you might run into limitations and realize oh I want to do this project but maybe I can't do it in Python yet so that's fine you can still use this with Arduino so I'm just showing you real quickly some Arduino integration with this and again check out the guide so it walks through how to set up the Arduino IDE you have to add this board to the board manager URL and then install the board and it depends on Arduino Sam D package everything's in the guide here just follow the guide it'll show you how to set it up I've done that ahead of time on this board and just to show you you know eventually you'll get this feather M zero Express in the board manager like I have right here and I can program this just like any other Arduino although one little note if you're in circuit Python mode and you want to go back to Arduino what you want to do is double tap the bootloader the reset button rather to get into the bootloader mode and you'll notice it goes into that drive mode with the feather boot and then the cool thing is this bootloader supports both the old mode of using the bossa C tool which is what Arduino uses internally or the drag-and-drop feather boot drive method so you can use either method it's fine you don't have to do anything special it's just you double tap the reset and it's waiting for either Bossa C or the drive dot u f2 file so our dueño doesn't use the uf2 file stuff but it uses that Bossa C tool internally so I can flash this blink sketch if I want so I just make sure it's pointed at my board and then I press the upload button it's going to compile this is your basic it's gonna blink that little red LED right there in a moment here after it uploads it and so it's this handy you know you can use this with our do we know IDE just like any other word we know board all the libraries for Arduino you can use with this board that support the m0 boards or the Arduino zero so it's doing its flashing thing it just finished and there you can see the red LED is blinking so that's all goodness okay so there's one cool thing like I mentioned so circuit python when you load circuit python onto your board it takes that little 2 megabyte flash chip and makes it a file system so you can store Python code you can store data files on there now one thing would be like you know ok let's say I do a little bit of things in circuit Python and then maybe I go back to Arduino and can I access those circuit Python files from Arduino and turns out yeah you can actually using a new little library that that I created that accesses the flash chip on this feather m0 Express board and it gives you a couple ways to access that flash chip so you can access the flash chip at a very low level and you can you know just read and write blocks of flash memory it's again it's just a flash chip connected to the spy bus and so if you read the datasheet you can talk to it and do whatever you want with it you know it's it's yours to control but we've made a nice little library that wraps up some kind of common functionality and simplifies it a lot because in some ways that flash chip is almost like a little SD card in a little micro SD card we're like some Arduino boards support connecting a micro SD card to it and then accessing the contents of that card through a real simple little API that Arduino has - you know you can open a file and you can write to it and read from it or you can list all the files in a directory using this SD card library so I took that SD card library interface like the exact same functions and objects and things that it deals with and just plugged in an implementation that talks to our 2 megabyte flash chip right here so you can store files you can read files you can write files and create directories you can delete directories you can do pretty much anything you can do with your computer file system on this 2 megabyte flash chip from Arduino which is pretty cool and then we went a little bit further also and made it compatible with the file system that circuit Python uses so even though I just loaded a an Arduino sketch on this microprocessor because I had circuit Python loaded on here before I had this sketch it actually formatted my flash chip with a circuit python file system it has that main dot Pi file that I was editing before you know that prints out hello world that's on there it has that zip file that I copied over that was just demonstrating the file access all of that is still on the flash chip because when you reprogram this board in Arduino it's only reprogramming the San b21 microprocessor it's not going to touch your flash chip unless you run some code that talks to the flash chip and reprograms it so that's kind of a cool handy kind of feature and that you know as long as you don't mess with that flash chip you can still access it and the contents of it from Arduino so to do that I'll show you there's this library right here this Adafruit spy flash library and eventually we'll put this in and guide the circuit python or the m0 Express guide so this guy it'll be updated a little bit later to have a little bit more detail about how to install this but I'll just run through real fast in this video this is an Arduino library so just like any other word we know library eventually this will be in the library manager and you can install it from Arduino library manager but for now I'll show you the manual install so you download the zip file for this and that will give you a file that you then let's see I'll show you right here so here's the zip file you want to open the zip file up and this is going to give you a directory you want to rename this and take the dash master off the end of it that's just something github adds to this so delete that from the end of it I've already done that and so you can see I have this directory right here and inside of here is just the code for this library so this you know it's some classes and headers and things so you want to copy this entire directory so this spy flash directory will copy that and I'm gonna put this in too and it depends on your operating system I'll put a link to a guide that we have that's how to install an Arduino library because you need to copy this into your Arduino libraries folder and it differs depending on if you're running Windows or Mac or Linux on Mac it's in your Documents our dwee no directory there's a libraries directory right here and this is where you want to copy in this Adafruit spy flash folder so I've already done this at a time but copy that folder into this libraries directory and that will make it available to our do we know and then close and reopen the arduino ide if you had it open already just so that it reloads and it should find that library but i already had a loaded and then you should be able to see in the examples there is this spy flash library there we go a different spy flash so this has a bunch of examples inside of here so there's fat FS circuit Python data logging format full usage print file and then flash a race and I'll mention real quickly kind of what all of these do but the one that we're most interested in in this video is the top one the fat FS circuit Python example because this is an example that shows that integration where if you have a flash chip that circuit Python has formatted and written files too or maybe it hasn't written any files - but if you've run circa Python on this board ahead of time then you can use this fat FS circuit Python example to read files or even write files back to that circuit python file system now the other examples the fat FS data logging format full usage and print file these actually use the flash chip just entirely on its own for Arduino so these are not compatible with circuit Python but these just show you know maybe you're not using circuit Python but you still want to read and write files to the chip you can look at the fat FS format example so you need to format the flash chip it's just like a USB Drive when you plug a USB Drive into your computer you know you need to format it ahead of time so you run this format sketch once it will write a blank file system to the chip and it warns you it'll erase anything that was on the chip so be careful but this you know you run this once that will give a blank file system then the full usage example walks through all of the different ways you can use this library so it shows you how to create directories how to check if a file exists how to create files how to write to a file how to read from a file how to delete directories delete files pretty much everything you could want to do it has an example in there and again this is all using the exact same syntax and API calls as the Arduino SD card library so if you've ever used the SD card library this should look really familiar and just a few small things that change based on how you initialize the library basically now the print file and data logging examples these are just more simplified examples so data logging will just show how to append a new line of data to a file so the scenario is you've got a sensor connected to your board and you just want to log like every minute what's the humidity or what's the temperature and so look at the data log in example this is a great real simple example just opens the file and just writes a new line every minute to it and then the print file example is a simple example of reading a file so that you run this example and it will open the file that was previously created from the data log example and it was printed out to the serial monitor so this would be a good example of you know if you maybe if your date along before and then maybe you've got some other sketch that runs and reads in your data file and does something different based on that so those are the example and then flash a race this is kind of the the big sledgehammer for if something goes wrong with the flash chip like maybe the file system gets corrupted you can run this example and it will just completely erase everything on that flash chip so if you had circuit Python code on that chip it's gone if you had stuff that you stored with Arduino it's gonna wipe that out so Tandy to have just to reset the chip and get it back into a known state but you know again be careful it warns you it you have to confirm and say okay I want to delete this chip before you you run it but like I said let's look at this circuit python example so like this example mentions you need to be running circuit python on your board ahead of time so you know make sure that you've loaded circuit Python and you know maybe you've created files with it or maybe you're gonna create some files with this example so you know do you definitely that's that's one step that you need to do before running this example and again this only works with circuit Python firmware so you know this this the way that this example works is it's not going to be able to read the file system if you use some of the other format examples so this is just for integrating with circuit Python because the file system is actually a little bit different if you use circa Python vs. if you use it just in Arduino like using that fat FS format and fat FS data login example it's slightly different in how it creates the file system so you know just be careful about how you use the examples here but I like this mention so what this example will do is when it runs it will look for a boot dot pi file on the circuit python file system if it finds that it'll print it out and then it'll look for your main dot pi and if it finds that it'll print out the contents of your main PI and then it'll make a new file data.txt and it'll just add a line to it I think it just adds a hello world line to it well look at a moment here what that looks like so but this is just showing basic usage of reading a file that circuit Python had written before like the main dot pie file and then also writing a file so writing that data dot txt which I could come back later and I'll show will reload circa Python and show that I can read that file now which is pretty cool so okay so to use this example you want to load it up and if you're using the feather M 0 Express there's nothing that you need to change it's all configured and ready to go but just to show you how this library works a little bit you create this Adafruit spy flash object and you have to tell it how to talk to this spy flash chip and this is using a hardware spy interface that's built into the feather m0 Express board but this is flexible like like I said you could wire a spy flash chip up to an Arduino with like a software spy connection and this shows like an example of connecting that and so you don't have to use them zero Express in this case and then the next thing is this magic line right here where this creates an Adafruit m0 express circuit python object this object or this type of object so this creates an object called Python FS and it needs to be told the flash chip that I created ahead of time so basically you create the interface to your flash chip that gives you all the low-level like read a block write a block functionality to talk to the flash chip and then you pass that to this circuit Python m0 express wrapper class which then takes that raw flash chip and gives you that simpler interface that gives you like the file and folder interface so instead of reading and writing the raw blocks of flash memory your reading and writing files which is what you as a user probably want to do like unless you're really hardcore and you know how to write a fat filesystem insert your Python format you probably want to use this wrapper class okay so in the setup is where all of the magic happens for this sketch so it waits for you to open the serial port right here this is just so that it doesn't immediately run and miss all the output so you load this on the board then it's only when you open the serial monitor will you actually see this start to run the very first thing it does is it initializes the flash chip right here so it calls the flash chip begin function and just make sure that that succeeds and then if it succeeds it goes on and it prints out it calls this flash get J deck ID this is just getting this ID that's burned into the flash memory it's just a good thing to check to make sure that it can talk to the flash chip I'll show you this should come back with a constant this Oh X one four zero one five zero I think value so if you see something other than that then something's wrong this is useful like if you've wired a flash chip up yourself you know you want to make sure that it can actually talk to the flash chip okay and then the next thing is it calls the Python FS object so remember that's that little wrapper object right here that wraps around the raw flash memory so we have to call the begin function on there because that will actually load the file system that circuit Python had created ahead of time and if it returns true then everything's good and the file system is loaded if it returns false then something went wrong and again like it'll mention it will tell you like hey make sure that you loaded circuit Python ahead of time because circuit Python has to create that file system before the Arduino sketch can load it and start using it and then once it finds that it's just gonna do a couple things so it's gonna look to see if your boot pi exists so this is where the high-level functions come into play on this Python FS object so you can call this dot exists function and just pass it the name of a file and it's gonna look for that file and you can pass in like different paths here like if there's a directory called foo you would just pass in foo /boot pi just like you're using an SD card you know you can have directories and things like that inside of here and so this is gonna return true if that exists if it exists then it's gonna create a file object by calling the open function on that Python FS object it's going to tell it okay open up that boot PI file and open it in file read mode so just like the SD card library there's a file read and a file write mode so depending on how you open the file that's what operations you can do with it once it opens the file then it just loops through just like with SD card library while there are bytes available so while it's not at the end of the end of file it's going to read a single byte at a time and then print it out to the serial port so it's going to do that for your boot dot pi and it's going to do that for main dot pi and then we'll come back in a second but it's also after after it prints out the boot behind the main dot pi it's gonna open a file this data dot txt file right here but it's gonna open this for file right mode so this is going to show an example of how to write a file onto the circuit python file system that later on we can go and read in circuit Python so this opens a data text file and the way the file write mode works is just like in the SD card library it opens it in append mode so that if the file exists it's going to open it and go to the very bottom of the file and start adding new data as new lines at the end of the file so it's not going to delete what was previously in the file if the file doesn't exist it will automatically create it and then it'll just start adding new data to it so again like the SD card library this is more suited towards data logging scenarios like usually you just want your sketch to get a file and just start adding new data to it so this that's what you can do but you can get much more advanced and actually if you look at the full usage example so if I look at in the spine library there we go spy flash so check out the full usage example this shows many more functions that you can use on here so like you can make directories you know you can let's see this shows like writing different objects out to the files you can delete things lots of functionality but notice the top this talks about how this has the same interface as the SD card library so you can look at the documentation for the SD card library see the functions that it exposes and it this this library has the exact same functions built into it but then this also mentions I think I mentioned it in here maybe it's not in this example but this library is built on this really excellent library called phat FS the fat file system it's it's a small fat file system implementation for small microcontrollers and it's actually the implementation of the fat file system that micro Python and circuit python use we've just taken out and put it into an Arduino library it's a really excellent library it created by this llm chan software i want to say so you know check that out and you can actually access that fat FS at a very low level yourself if you look at the format example in the library let me go down to that yeah here's the format example this actually shows how so this is a link to the fat FS API reference so you know if you use the Arduino flash library here and if you run into limitations or you want to do more stuff you can access the fat FS library yourself and call all of its functions and use it at a low level and so this format example actually does that so it's calling the functions in the fat FS library like there's this F disc function this is a part of the fat FS librarian so you can see it's a little more prickly this is AC library so it's you know it's maybe not as nice to use as Arduino libraries that try to hide a lot of the complexities like creating buffers and passing around pointers and things like that so it's a little more advanced but it gives you as much access to the chip as you want so you can do anything with this so there are more advanced ways to open files and things ultimately but again you know if you're just doing simple things like data logging you know adding new sensor routines look at the examples we have like the data log an example that just shows the basic simple usage for this anyways okay so let's come back to that circuit python example let's run this on our board so I'm gonna make sure I have the a my M 0 Express board selected it's available there so let's press the upload button and let's see what happens here in a second so it's gonna upload the sketch and again this should print out the boot pi the main dot pi and then create a file and add a line to it so just uploaded it to the board and then I need to open the serial monitor and I'm gonna open it at 11 v 200 baht is what you really want to open an app but it you faulted 9600 it's fine if you don't see any output then change it to 11 pack 200 bond and actually here I will reset the board just to show what's happening so I reset the board and then I'm gonna reopen this in the serial monitor so you can see this prints out all the text as soon as I open the serial monitor so it found here's that jtech chip ID so you want to see a value like this one four zero one five one zero one that just means that that's the ID that's burned into that flash chip if you see a value like ffff ffff like all apps that bad that means that you can't talk to the flash chip and so you probably have something wrong right here like with this connection so by default it's trying to use the hardware spy connection on the feather m0 but maybe you've wired it up to an external flash chip and so double check all those connections you know it's not able to talk to the chip correctly if you see a value that's not this one four zero one five zero one okay so this is cool so I can see it mounted the filesystem so that's got through to this line right here so you know we called that Python FS dot begin that succeeded so that's good we've mounted our circuit Python file system no boot dot pi found which is okay because there was no boot pi on this board so that's what I expected so you can see this this check to see if that existed it didn't find it so it printed out no Mutapa existed but this is kind of cool so now it found it said printing main dot pi and you can see here's the contents of that main dot pi file print hello world from circa python if you remember about 20 minutes ago or so maybe thirty minutes ago I wrote that file on the board when it was running circuit python and so now my arduino sketch is reading that main dot pi file from the flash memory chip that circa python had written on there so that's really cool and that's what I wanted to show with this library is that you know if you use circuit Python and then you go back to Arduino there's a way to talk to the file system and still get files from the board so that's cool so that's an example of reading a file and then it mentioned here it wrote a new line to the end of data dot txt and so that's like we walk through before so I created that day needed text file and then wrote so hopefully there should be a data txt file that says hello circuit python from Arduino and I think it should say it twice because I ran this sketch twice so it should hopefully have two lines inside of there so let's reload circuit python on to this board and let's see if that file exists that'll be the ultimate and end test so I'm going to double tap that now it's in its bootloader mode I've got my feather boot drive again so again just like I showed before let's load up circuit python firmware onto this board so I'm going to grab that uf2 file I will drag it on to the feather drive its programming it right now and then it just finished and then here's the cool thing so it just popped up now now certain Python is running and starter Python exposes that flash chip as a USB Drive so if I look at this USB Drive hey check this out so we still have that zip file I copied over before you know again none of that stuff is deleted unless you explicitly delete it there's my main dot pi and I should hopefully be able to see if I open this and let's not use Xcode that's a little bit heavyweight and if I open this in text editor there's hello world from surrogate Python so that my main dot pi is still there just like we saw in Arduino and then if I open up the data dot txt file let's see what's inside of here so there we go hello circuit python from arduino and it's in there twice which is what i expect to see because i ran this sketch twice like it ran once when i open the serial monitor and 9600 baud and then i reset the board open it again at eleven five two hundred baud ran again so that's how i got the two lines there and so that's cool so that that was the line that was written right here from my arduino sketch so again just showing that it's possible using this Adafruit spy flash library to talk to that flash chip and talk to the files that circuit python has written you can both read files and write new files that circuit python can then itself read from your board and I mean just to show maybe another example if I connect to the serial rebel on my board so whilst a screen dev TTY 11/5 200-odd so in a serial rebel I can open up the files like if I import the OS module oops or if I import the u OS module in micro Python u OS listener this lists all the files that are on the root of my board so you can see there's that data dot txt file and then if I say data equals open data dot txt and I want to open it in read mode then that's gonna open the file and then if I read all the contents of it hey there's my two lines there's hello circuit python from Arduino hello circuit python from Arduino so you know I could do some kind of cool example here of like maybe I have some Arduino sketch that runs on here and gets you know runs for a while and gets a bunch of data and then I want to go process that data later but I want to do it with certain Python so I loaded sir Python on to here and then it reads those files that I stored from the flash chip so it's possible to do that using this spy flash library so okay so that's it I think that's all I wanted to show in this video so if folks have questions throw them in the chat and we'll see if I get to them someone mentioned is sequel like three supported that's a good question so this sequel I'd supported in micro Python or circa Python as far as I know no it's not supported although one place to check would be in micro pythons github repository there's a micro Python Lib project and so this is basically a bunch of Python standard library modules things like the sequel Lite module potentially they've taken a bunch of Python standard modules and they've implemented them for micro Python and so if there is a sequel like module it might be in here but let's see I have a feeling sequel it-it's pretty complex well hey there's something called sequel Lite that doesn't necessarily mean that it's implemented so let's just see oh well there is an implementation here now I have a feeling just based on the fact that it's using this FFI Lib this probably only works with micro Python on UNIX systems I doubt that this would work on an ARM Cortex because micro Python can run on your computer like a UNIX based system and in those systems it could use sequel Lite because sequel Lite is just a dynamically linked library so we could load that library up whereas on your arm cortex chip you have no operating system so you have no concept of shared libraries if you try to take a UNIX dynamic library there's no way to load that code you know there's no kernel to interpret it so long story short it doesn't look like sequel Lite will work on your microcontroller with microphone right now but I bet in the future because the basic idea you know if you're if you want to use sequel Lite you probably want to store data so you could still use files you could create files and store data into them although you can't do that right now a circa Python just because we don't let you write files to the flash memory when it's exposed with USB look in the future we're going to think of some ways because the race conditions if your script is writing to a file while your computer tries to write to the file if those rights happen at the same time you can crumple file system so we don't let you do that but anyways long story short you know the the basic thing is there's no sequel light but you know look for ways to persist data using files potentially like you can use the json serialization so there's a json serializer built into micro Python you can throw any Python object into the json serializer it'll give you a json spring back and then you can write that string out to a file for example so that's a very bare-bones database quota but there's no indexing there's no query and capability for that so but you know it wouldn't surprise me if in the future with more powerful chips you know maybe someone will take a crack at implementing a tiny little sequel database that runs on these arm cortex chips that would be pretty cool so but unfortunately I know no sequel Lite support right now for circa Python a microphone on ok I think that's it that's all the questions I see so I will wrap this up we'll go back to the main shot so again this is Toni from Adafruit thanks a lot for watching check out youtube.com slash Adafruit you can see this video and all kinds of other fun project videos check out twitch.tv slash Adafruit and you can see me streaming these things live I like to do a live stream usually every Friday so this was the stream on using the feather m0 Express board so fancy new board from Adafruit it's the feather M 0 plus that little spy memory chip and neopixel that go really well with circa Python so you can store more Python code you can have more advanced firmware on this board because it's that Express board so definitely check out that board and stay tuned for lots more circuit python and micro Python stuff particularly around this board because going forward you'll see this in a lot of other future boards I don't have one near me but you know there might be a circuit playground board potentially that might have a flash chip like that so you know don't ask it's not out yet but there's some cool stuff planned in the future that's going to be using that express and that extra of flash memory that we so until next time this is Tony from Adafruit so click the like the comment the subscribe you know let us know that this is good stuff and we'll keep doing content like this so thanks a lot for watching I'll see you later bye
Info
Channel: Adafruit Industries
Views: 10,256
Rating: undefined out of 5
Keywords: adafruit, electronics, diy, arduino, hardware, opensource, projects, raspberry, computer, raspberrypi, microcontrollers, limor, limorfried, ladyada, STEAM, STEM
Id: h6V7yjsmIxU
Channel Id: undefined
Length: 53min 28sec (3208 seconds)
Published: Fri Apr 07 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.