Skip to content
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

Lab2A #79

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
29 changes: 29 additions & 0 deletions CMakelist.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
cmake_minimum_required(VERSION 3.13)
include(pico_sdk_import.cmake)
project(test_project C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
pico_sdk_init()

if (TARGET tinyusb_device)
add_executable(hello_pio_usb)

pico_generate_pio_header(hello_pio_usb ${CMAKE_CURRENT_LIST_DIR}/ws2812.pio OUTPUT_DIR ${CMAKE_CURRENT_LIST_DIR}/generated)


#pull in common dependencies
target_sources(hello_pio_usb PRIVATE ws2812.c ws2812.h hello_usb.c ws2812.pio)
# pull in common dependencies
target_link_libraries(hello_pio_usb pico_stdlib hardware_pio )

# enable usb output, disable uart output
pico_enable_stdio_usb(hello_pio_usb 1)
pico_enable_stdio_uart(hello_pio_usb 0)

# create map/bin/hex/uf2 file etc.
pico_add_extra_outputs(hello_pio_usb)

# add url via pico_set_program_url
elseif(PICO_ON_DEVICE)
message(WARNING "not building hello_usb because TinyUSB submodule is not initialized in the SDK")
endif()
99 changes: 94 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,98 @@
University of Pennsylvania, ESE 5190: Intro to Embedded Systems, Lab 2A

(TODO) YOUR NAME HERE
(TODO) LinkedIn, personal website, twitter, etc.
Tested on: (TODO) MacBook Pro (14-inch, 2021), macOS Monterey 12.5.1
Zihan Zhang
[email protected]
Tested on: Ubantu

#Part 3.2

Why is bit-banging impractical on your laptop, despite it having a much faster processor than the RP2040?
If the processor is interrupted to attend to one of the hard peripherals it is also responsible for, it can be fatal to the timing of any bit-banged protocol. And
the ratio between the processor speed and protocol speed is big, so the processor will spend uselessly idling in between GPIO accesses.

What are some cases where directly using the GPIO might be a better choice than using the PIO hardware?
The main case where software GPIO access is the best choice is LEDs and push buttons.

How do you get data into a PIO state machine?
Use pio_sm_put_blocking() function to push data directly into the state machine’s TXFIFO.

How do you get data out of a PIO state machine?
Use pull instruction first to take one data item from the transmit FIFO buffer, and places it in the output shift register (OSR). Then use out instruction to
write that data to some pins.

How do you program a PIO state machine?
System software loads data into the PIO's instruction memory and then set the i/o mapping.

In the example, which low-level C SDK function is directly responsible for telling the PIO to set the LED to a new color? How is this function accessed from the
main “application” code?
pio_sm_put_blocking pio_sm_put_blocking() is called with the parameters from the put_pixel().

What role does the pioasm “assembler” play in the example, and how does this interact with CMake?
Our program written in C language can be built to assembler with GCC. We can use CMake to generate uf2 file which can be recognized by the board.

#Part3.3

I put this document in part3.3.pdf<br>

<img width="276" alt="image" src="https://user-images.githubusercontent.com/114272466/196423941-5cfdee5b-59bc-41ff-942f-385e05bdb895.png"><br>
<img width="281" alt="image" src="https://user-images.githubusercontent.com/114272466/196424148-a8a5bff1-017b-418a-a24c-1534fd1f5702.png"><br>
<img width="299" alt="image" src="https://user-images.githubusercontent.com/114272466/196424198-ab6013cc-1a1d-489e-9a9f-64bf1c2d47d0.png"><br>
<img width="294" alt="image" src="https://user-images.githubusercontent.com/114272466/196424228-672ff8cc-1870-410a-9f0b-ef1c4d61ab29.png"><br>
<img width="327" alt="image" src="https://user-images.githubusercontent.com/114272466/196424293-e2f0f00f-4699-4787-bbb5-e42a1d6eb6c0.png"><br>
<img width="311" alt="image" src="https://user-images.githubusercontent.com/114272466/196424352-745eedaa-6374-472d-aa87-c06fbf5938b4.png"><br>
<img width="307" alt="image" src="https://user-images.githubusercontent.com/114272466/196424391-5b1df5cb-9d57-49cc-b82a-664ba036a73a.png"><br>

#Part3.4

I put this document in part3.4.xlsx<br>

#Part3.5

I put this document in part3.5.pdf<br>

![image](https://user-images.githubusercontent.com/114272466/196425034-956fe653-a823-48e1-892a-41bf81dc413c.png)#Part3.5<br>

#Part3.6

I put this document in part3.6.xlsx<br>

#Part3.7

I put this document in part3.7.pdf<br>
![image](https://user-images.githubusercontent.com/114272466/196425697-8027d36a-c7af-4fa4-a831-07cd2b553ab0.png)<br>

#Part4 Hello_world<br>
![image](https://user-images.githubusercontent.com/114272466/196427660-26ddf1e4-1cd5-432a-9c07-fa4104f092fe.png)<br>

For this project, I add the gpio in hello_usb.c and add ws2812.h. At the mean time, I add the fuction in ws2812.c.
Additionally, I mix two CMakelists.txt into one doucment. Finally, I made it.





























(TODO: Your README)

Include lab questions, screenshots, analysis, etc. (Remember, this is public, so don't put anything here you don't want to share with the world.)
46 changes: 46 additions & 0 deletions hello_usb.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/

#include <stdio.h>

#include "ws2812.h"


#include "pico/stdlib.h"
#include "hardware/pio.h"
#include "hardware/clocks.h"
#include "ws2812.pio.h"

#define IS_RGBW true
#define NUM_PIXELS 150

#ifdef PICO_DEFAULT_WS2812_PIN
#define WS2812_PIN PICO_DEFAULT_WS2812_PIN
#else
// default to pin 2 if the board doesn't have a default WS2812 pin defined
#define WS2812_PIN 12
#endif
int main() {
stdio_init_all();
gpio_init(11);
gpio_set_dir(11, GPIO_OUT);
gpio_put(11, 1);
PIO pio = pio0;
int sm = 0;
uint offset = pio_add_program(pio, &ws2812_program);
ws2812_program_init(pio, sm, offset, WS2812_PIN, 800000, IS_RGBW);
while (true) {
printf("Hello, world!\n");
sleep_ms(500);
set_neopixel_color(0x00337788);
sleep_ms(500);
printf("see you!\n");
set_neopixel_color(0);
sleep_ms(500);

}
return 0;
}
Binary file added part3.3.pdf
Binary file not shown.
Binary file added part3.4.xlsx
Binary file not shown.
Binary file added part3.5.pdf
Binary file not shown.
Binary file added part3.7.pdf
Binary file not shown.
73 changes: 73 additions & 0 deletions pico_sdk_import.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# This is a copy of <PICO_SDK_PATH>/external/pico_sdk_import.cmake

# This can be dropped into an external project to help locate this SDK
# It should be include()ed prior to project()

if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH))
set(PICO_SDK_PATH $ENV{PICO_SDK_PATH})
message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')")
endif ()

if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT))
set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT})
message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')")
endif ()

if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH))
set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH})
message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')")
endif ()

set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK")
set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable")
set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK")

if (NOT PICO_SDK_PATH)
if (PICO_SDK_FETCH_FROM_GIT)
include(FetchContent)
set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR})
if (PICO_SDK_FETCH_FROM_GIT_PATH)
get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}")
endif ()
# GIT_SUBMODULES_RECURSE was added in 3.17
if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.17.0")
FetchContent_Declare(
pico_sdk
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
GIT_TAG master
GIT_SUBMODULES_RECURSE FALSE
)
else ()
FetchContent_Declare(
pico_sdk
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
GIT_TAG master
)
endif ()

if (NOT pico_sdk)
message("Downloading Raspberry Pi Pico SDK")
FetchContent_Populate(pico_sdk)
set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR})
endif ()
set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE})
else ()
message(FATAL_ERROR
"SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git."
)
endif ()
endif ()

get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
if (NOT EXISTS ${PICO_SDK_PATH})
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found")
endif ()

set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake)
if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE})
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK")
endif ()

set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE)

include(${PICO_SDK_INIT_CMAKE_FILE})
Binary file added port3.6 .xlsx
Binary file not shown.
120 changes: 120 additions & 0 deletions ws2812.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/**
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/

#include <stdio.h>
#include <stdlib.h>

#include "pico/stdlib.h"
#include "hardware/pio.h"
#include "hardware/clocks.h"
#include "ws2812.pio.h"

#define IS_RGBW true
#define NUM_PIXELS 150

#ifdef PICO_DEFAULT_WS2812_PIN
#define WS2812_PIN PICO_DEFAULT_WS2812_PIN
#else
// default to pin 2 if the board doesn't have a default WS2812 pin defined
#define WS2812_PIN 12
#endif

static inline void put_pixel(uint32_t pixel_grb) {
pio_sm_put_blocking(pio0, 0, pixel_grb << 8u);
}

static inline uint32_t urgb_u32(uint8_t r, uint8_t g, uint8_t b) {
return
((uint32_t) (r) << 8) |
((uint32_t) (g) << 16) |
(uint32_t) (b);
}

void pattern_snakes(uint len, uint t) {
for (uint i = 0; i < len; ++i) {
uint x = (i + (t >> 1)) % 64;
if (x < 10)
put_pixel(urgb_u32(0xff, 0, 0));
else if (x >= 15 && x < 25)
put_pixel(urgb_u32(0, 0xff, 0));
else if (x >= 30 && x < 40)
put_pixel(urgb_u32(0, 0, 0xff));
else
put_pixel(0);
}
}

void pattern_random(uint len, uint t) {
if (t % 8)
return;
for (int i = 0; i < len; ++i)
put_pixel(rand());
}

void pattern_sparkle(uint len, uint t) {
if (t % 8)
return;
for (int i = 0; i < len; ++i)
put_pixel(rand() % 16 ? 0 : 0xffffffff);
}

void pattern_greys(uint len, uint t) {
int max = 100; // let's not draw too much current!
t %= max;
for (int i = 0; i < len; ++i) {
put_pixel(t * 0x10101);
if (++t >= max) t = 0;
}
}

void set_neopixel_color(uint32_t color){

uint8_t r=color>>16;
uint8_t g=color>>8;
uint8_t b=color;
uint32_t result=urgb_u32(r,g,b);
put_pixel(result);

}

typedef void (*pattern)(uint len, uint t);
const struct {
pattern pat;
const char *name;
} pattern_table[] = {
{pattern_snakes, "Snakes!"},
{pattern_random, "Random data"},
{pattern_sparkle, "Sparkles"},
{pattern_greys, "Greys"},
};

//int main() {
//set_sys_clock_48();
// stdio_init_all();
// printf("WS2812 Smoke Test, using pin %d", WS2812_PIN);
// gpio_init(11);
// gpio_set_dir(11,GPIO_OUT);
// gpio_put(11,1);
// // todo get free sm
// PIO pio = pio0;
// int sm = 0;
// uint offset = pio_add_program(pio, &ws2812_program);
//
// ws2812_program_init(pio, sm, offset, WS2812_PIN, 800000, IS_RGBW);
//
// int t = 0;
// while (1) {
// int pat = rand() % count_of(pattern_table);
// int dir = (rand() >> 30) & 1 ? 1 : -1;
// puts(pattern_table[pat].name);
// puts(dir == 1 ? "(forward)" : "(backward)");
// for (int i = 0; i < 1000; ++i) {
// pattern_table[pat].pat(NUM_PIXELS, t);
// sleep_ms(10);
// t += dir;
// }
// }
//}
4 changes: 4 additions & 0 deletions ws2812.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ifndef _WS2812_H
#define _WS2812_H
void set_neopixel_color(uint32_t color);
#endif
Loading