BeagleBoard/GSoC/2021 Proposal/bela on bbai

=ProposalTemplate = About Student: Dhruva Gole Mentors: Giulio Moro Code: https://github.com/BelaPlatform/Bela Wiki: https://learn.bela.io/get-started-guide/say-hello-to-bela/#what-is-bela GSoC: GSoC entry

=Status= This project is currently just a proposal.

=Proposal= Please complete the requirements listed on the ideas page and fill out this template.

About you
IRC: dhruvag2000 Github: https://github.com/DhruvaG2000 School: Veermata Jijabai Technological Inst. Country: India Primary language : English, Marathi, Hindi Typical work hours: 10AM - 7PM Indian Standard Time Previous GSoC participation: I find embedded and IOT pretty interesting, given I have experience with ESP32, SIMCOM, ESP8266, Atmega, and many other microcontroller devices and I think I will be able to excel in this project. This is the first time i am participating in GSoC.

About your project
Project name: Bela support for the BeagleBone AI

Description
As given on the official website, Bela is a hardware and software system for creating beautiful interaction with sensors and sound. Bela consists of a Bela cape on top of a BeagleBone Black computer (uptil now). Bela has a lot of analog and digital inputs and outputs for hooking up sensors and controlling other devices, and most importantly Bela has stereo audio i/o allowing you to interact with the world of sound. Both Bela systems use the same Bela software. It uses a customised Debian distribution which - most notably - uses a Xenomai kernel instead of a stock kernel. Xenomai is co-kernel for Linux which allows to achieve hard real-time performance on Linux machines (http://xenomai.org/). It thus takes advantage of features of the BeagleBone computers and can achieve extremely fast audio and sensor processing times. Although the proposal Title mentions support for AI, I will try to develop a standardized setup that allows an easy jump across all TI chips. Bela is ideal for creating anything interactive that uses sensors and sound. So far, Bela has been used to create: and many more applications that are listed here The Beagle Black and PocketBeagle are getting outdated by the day and also it would be better to have a more standardised setup that allows to more easily jump across TI chips, soon newer boards with different and more efficient chips like the AM5X and the TI C66x digital-signal-processor (DSP) cores in the BBAI are coming up that will need compatiblity with the Bela Software and Hardware. As part of the process we will try to offload peripheral initialisation to the mcasp and spi driver to in order to make it more portable for porting to other boards in the family, at present or future. C, PRU, dtb, uboot, make
 * What is Bela?
 * Applications of Bela:
 * 1) musical instruments and audio effects
 * 2) kinetic sculptures
 * 3) wearable devices
 * 4) interactive sound installations
 * Why add support for BBAI?
 * Programming languages and tools to be used:

Proposed method of Implementation and Timeline
The Bela cape is working on the BB AI using ALSA (although one of the clocks seems to be of the wrong frequency), so we can confirm the pinout and pin settings are OK. We have not tried running the Bela code on the PRU yet. I will be learning and using pinmuxing, uboot, PRU assembly and reading about the Sitara family of SoCs and the McASP audio port. (ref. here)

It can be seen from the syntax here that we are ennumerating the hardware the code is running on as follows: typedef enum { 	BelaHw_NoHw = -1, BelaHw_Bela, BelaHw_BelaMini, BelaHw_Salt, BelaHw_CtagFace, BelaHw_CtagBeast, BelaHw_CtagFaceBela, BelaHw_CtagBeastBela, } BelaHw; Compatibility is checked here in the form of bool Bela_checkHwCompatibility(BelaHw userHw, BelaHw detectedHw);
 * Syntax Analysis

Come the BBAI, as seen here int cs_pin = 2 * 32 + 12; // P9.15a on BB AI Two lines below this have been commented out for the older BB boards, which means this code won't really work for BB Black or Blue. This ofcourse is not the ideal workflow, which is why when I will start working I will add the necessary conditional statements to avoid this and provide easier identification and execution that is processor specific To explain In the form of pseudo code how I aim to establish a common code base for supporting all boards, is by using: typedef enum { 	bb_ai, bb_black, bb_pocket } So, once we detect which board it is that we are running on start-up, the constants will be set accordingly and the rest of the code base will become much easier to use without needing to hard code the pins specific to the beagle board the user is currently using. The Solution to run the current PRU assembly (designed for pasm/uio) using clpru/rproc (as for the AI) is:
 * PRU:
 * 1) It is better to minimise PRU code changes
 * 2) Preferably changing the PRU code will not be changed in such a way that it stops working on current Bela images (which currently use  pasm / uio). In particular, we do not want to force Bela on BBB/PB to move to using clpru / rproc.
 * 1) No PRU code changes (at least not yet, we'll need to change the MCASP addresses at the very least, but at least there is no massive rewrite to do just to be able to run the code)
 * 2) The same code that is built with pasm to run with uio can be translated automatically into code that builds with clpru and runs with rproc. So any changes made to that code can be back/fwd ported between BBB and BBAI.

prudis file.bin | sed 's/^\(.*\)$/" \1\\n"/' > included_assembly.h void main {     __asm__ __volatile__ ; }
 * Workflow for building the PRU existing PRU code with clpru:
 * 1) Build the .p file as is with pasm with `-V2 -b`. This generates a .bin file that contains the assembled program
 * 2) I will be using the disassembler Giulio Moro put together hacking the one that was inside prudebug. (Find it here(built with gcc da.c -o prudis).
 * 3) Process the bin through the disassembelr and make it ready to be included inside an __asm__ directive (i.e.: add quotes and prepend a space at the beginning of each line):
 * 1) have the following in main.c:
 * 1) include "included_assembly.h"
 * 1) build main.c with the regular clpru toolchain

The above seems to work, but it would need some extra testing.

I will need to change the MCASP addresses:


 * 1) define MCASP0_BASE 0x48038000
 * 2) define MCASP1_BASE 0x4803C000


 * 1) define MCASP0_DATAPORT 0x46000000
 * 2) define MCASP1_DATAPORT 0x46400000


 * 1) define MCASP_BASE MCASP0_BASE
 * 2) define MCASP_DATAPORT MCASP0_DATAPORT

I am yet to figure out pinmuxing to make sure the relevant pins are set to the correct function (they are NOT by default). I will see here https://elinux.org/BeagleBoard/GSoC/2020_Projects/Cape_Compatibility and/or ask on the BB slack about for the best way to go about it.
 * PINMUXING

Bela can work with a PRU->ARM interrupt, which is default these days, but requires an rtdm driver, which is another layer of complications. To start off, I will try to run it without the PRU->ARM interrupt by adding BELA_USE_DEFINE=BELA_USE_POLL to my make command line.
 * PRU->ARM INTERRUPTS

I will need to change the initialisation code in PRU.cpp that is currently relying on libprussdrv and move to using rproc. Not sure if rproc provides some functionalities to access the PRU's RAM the way `prussdrv_map_prumem` used to, but that essentially gives access to a previously mmap'ed area of memory https://github.com/beagleboard/am335x_pru_package/blob/master/pru_sw/app_loader/interface/prussdrv.c, so it should be easy to replace, as long as I am able to find the correct addresses in the TRM. On the latest Bela code there's a `Mmap` class which can make this somehow simpler http://docs.bela.io/classMmap.html.
 * PRU transitioning from libprussdrv to rproc

I would need to install a xenomai kernel through whatever the default procedure to update kernel is on the BBAI. I will also need to install the user-space Xenomai library as well. That MAY be available through apt-get (Xenomai >= 3.0), or it can be easily built from source.
 * XENOMAI kernel
 * Timeline

Experience and approach

 * 1) I have used C++, C and Python programming languages over the past 3 years in a variety of projects involving embedded systems using the ESP32, Arduino UNO, ESP8266 and am also well-versed with freeRTOS.
 * 2) I have an aptitude for writing good reports and blogs, and have written a small blog on how to use a debugger.
 * 3) I recently did a project using ESP32, in which I used the DHT11 sensor to display humidity and temperature on a local HTML server . Other than that I have worked on developing hardware and making documentation for a 3 DOF arm based on an ESP32 custom board.
 * 4) I also interned at an embedded device startup where I
 * 5) Interfaced ADS1115 ADC with the ESP32 and used it to read battery voltage.
 * 6) Used UART for ESP32 and SIMCOM SIM 7600IE communication to gain LTE support.
 * 7) Published local sensor data to the cloud via LTE.
 * 8) I actively contribute to open source (most recently, I contributed to the ADS1115 library for ESP32 on the unclerus repo and can be seen here).
 * 9) Currently I am working on designing a Development board for the Raspberry Pico (RP2040) using KiCAD.
 * 10) I also do a lot of mini projects throughout the year, you can find my several more interesting projects at my github page

Contingency
I believe that if I get stuck on my project and my mentor isn’t around, I will use the resources that are available to me. Some of those information portals are listed below.


 * 1) Derek Molly's beagle bone guide provides all the information needed for getting up and running with my beagle.
 * 2) Content on e-ALE.org is very useful for information that ranges from tutorials and walkthroughs to the level of very advanced stuff.
 * 3) https://credentiality2.blogspot.com/2015/09/beaglebone-pru-gpio-example.html

Benefit
If successfully completed, this project will add support for the Bela cape + Xenomai + PRU on the BeagleBone AI, and also the code will be easier to port to other Texas Instruments systems-on-chip.

Misc
Completed all the requirements listed on the ideas page. The code for the cross-compilation task can be found here submitted through pull request #149.