Cleanup 3rd party samples; update README.md; add some missing copyright, fix builds for boards without certain pin definitions

This commit is contained in:
Graham Sanderson
2021-10-28 14:56:05 -05:00
committed by GitHub
parent 1300621684
commit fa09f2c88e
16 changed files with 111 additions and 117 deletions

View File

@@ -25,43 +25,37 @@ int main() {
uint rx_gpio = 15; // choose which GPIO pin is connected to the IR detector
// configure and enable the state machines
//
int tx_sm = nec_tx_init (pio, tx_gpio); // uses two state machines, 16 instructions and one IRQ
int rx_sm = nec_rx_init (pio, rx_gpio); // uses one state machine and 9 instructions
int tx_sm = nec_tx_init(pio, tx_gpio); // uses two state machines, 16 instructions and one IRQ
int rx_sm = nec_rx_init(pio, rx_gpio); // uses one state machine and 9 instructions
if (tx_sm == -1 || rx_sm == -1) {
printf ("could not configure PIO\n");
printf("could not configure PIO\n");
return -1;
}
// transmit and receive frames
//
uint8_t tx_address = 0x00, tx_data = 0x00, rx_address, rx_data;
while (true) {
// create a 32-bit frame and add it to the transmit FIFO
//
uint32_t tx_frame = nec_encode_frame (tx_address, tx_data);
pio_sm_put (pio, tx_sm, tx_frame);
printf ("\nsent: %02x, %02x", tx_address, tx_data);
uint32_t tx_frame = nec_encode_frame(tx_address, tx_data);
pio_sm_put(pio, tx_sm, tx_frame);
printf("\nsent: %02x, %02x", tx_address, tx_data);
// allow time for the frame to be transmitted (optional)
//
sleep_ms (100);
sleep_ms(100);
// display any frames in the receive FIFO
//
while (!pio_sm_is_rx_fifo_empty (pio, rx_sm)) {
uint32_t rx_frame = pio_sm_get (pio, rx_sm);
while (!pio_sm_is_rx_fifo_empty(pio, rx_sm)) {
uint32_t rx_frame = pio_sm_get(pio, rx_sm);
if (nec_decode_frame (rx_frame, &rx_address, &rx_data)) {
printf ("\treceived: %02x, %02x", rx_address, rx_data);
if (nec_decode_frame(rx_frame, &rx_address, &rx_data)) {
printf("\treceived: %02x, %02x", rx_address, rx_data);
} else {
printf ("\treceived: %08x", rx_frame);
printf("\treceived: %08x", rx_frame);
}
}
sleep_ms (900);
sleep_ms(900);
tx_data += 1;
}
}

View File

@@ -4,56 +4,40 @@
* SPDX-License-Identifier: BSD-3-Clause
*/
// SDK types and declarations
//
#include "pico/stdlib.h"
#include "hardware/pio.h"
#include "hardware/clocks.h" // for clock_get_hz()
// declare the public API functions
//
#include "nec_receive.h"
// import the assembled PIO state machine program
//
#include "nec_receive.pio.h"
// define the public API functions
//
// Claim an unused state machine on the specified PIO and configure it
// to receive NEC IR frames on the given GPIO pin.
//
// Returns: the state machine number on success, otherwise -1
//
int nec_rx_init(PIO pio, uint pin_num) {
// disable pull-up and pull-down on gpio pin
//
gpio_disable_pulls (pin_num);
gpio_disable_pulls(pin_num);
// install the program in the PIO shared instruction space
//
uint offset;
if (pio_can_add_program (pio, &nec_receive_program)) {
offset = pio_add_program (pio, &nec_receive_program);
if (pio_can_add_program(pio, &nec_receive_program)) {
offset = pio_add_program(pio, &nec_receive_program);
} else {
return -1; // the program could not be added
}
// claim an unused state machine on this PIO
//
int sm = pio_claim_unused_sm (pio, true);
int sm = pio_claim_unused_sm(pio, true);
if (sm == -1) {
return -1; // we were unable to claim a state machine
}
// configure and enable the state machine
//
nec_receive_program_init(pio, sm, offset, pin_num);
return sm;
@@ -64,8 +48,7 @@ int nec_rx_init(PIO pio, uint pin_num) {
// provided.
//
// Returns: `true` if the frame was valid, otherwise `false`
//
bool nec_decode_frame (uint32_t frame, uint8_t *p_address, uint8_t *p_data) {
bool nec_decode_frame(uint32_t frame, uint8_t *p_address, uint8_t *p_data) {
// access the frame data as four 8-bit fields
//
@@ -83,14 +66,12 @@ bool nec_decode_frame (uint32_t frame, uint8_t *p_address, uint8_t *p_data) {
// a valid (non-extended) 'NEC' frame should contain 8 bit
// address, inverted address, data and inverted data
//
if (f.address != (f.inverted_address ^ 0xff) ||
f.data != (f.inverted_data ^ 0xff)) {
return false;
}
// store the validated address and data
//
*p_address = f.address;
*p_data = f.data;

View File

@@ -1,7 +1,13 @@
/**
* Copyright (c) 2021 mjcross
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "pico/stdlib.h"
#include "hardware/pio.h"
// public API
//
int nec_rx_init (PIO pio, uint pin);
bool nec_decode_frame (uint32_t sm, uint8_t *p_address, uint8_t *p_data);
int nec_rx_init(PIO pio, uint pin);
bool nec_decode_frame(uint32_t sm, uint8_t *p_address, uint8_t *p_data);

View File

@@ -10,48 +10,34 @@
#include "pico/stdlib.h"
#include "hardware/pio.h"
#include "hardware/clocks.h" // for clock_get_hz()
// declare the public API functions
//
#include "nec_transmit.h"
// import the assembled PIO state machine programs
//
#include "nec_carrier_burst.pio.h"
#include "nec_carrier_control.pio.h"
// define the public API functions
//
// Claim an unused state machine on the specified PIO and configure it
// to transmit NEC IR frames on the specificied GPIO pin.
//
// Returns: on success, the number of the carrier_control state machine
// otherwise -1
//
int nec_tx_init(PIO pio, uint pin_num) {
// install the carrier_burst program in the PIO shared instruction space
//
uint carrier_burst_offset;
if (pio_can_add_program (pio, &nec_carrier_burst_program)) {
if (pio_can_add_program(pio, &nec_carrier_burst_program)) {
carrier_burst_offset = pio_add_program(pio, &nec_carrier_burst_program);
} else {
return -1;
}
// claim an unused state machine on this PIO
//
int carrier_burst_sm = pio_claim_unused_sm(pio, true);
if (carrier_burst_sm == -1) {
return -1;
}
// configure and enable the state machine
//
nec_carrier_burst_program_init(pio,
carrier_burst_sm,
carrier_burst_offset,
@@ -59,27 +45,24 @@ int nec_tx_init(PIO pio, uint pin_num) {
38.222e3); // 38.222 kHz carrier
// install the carrier_control program in the PIO shared instruction space
//
uint carrier_control_offset;
if (pio_can_add_program (pio, &nec_carrier_control_program)) {
if (pio_can_add_program(pio, &nec_carrier_control_program)) {
carrier_control_offset = pio_add_program(pio, &nec_carrier_control_program);
} else {
return -1;
}
// claim an unused state machine on this PIO
//
int carrier_control_sm = pio_claim_unused_sm(pio, true);
if (carrier_control_sm == -1) {
return -1;
}
// configure and enable the state machine
//
nec_carrier_control_program_init(pio,
carrier_control_sm,
carrier_control_offset,
2 * (1 / 562.5e-6), // 2 ticks per 562.5us carrier burst
2 * (1 / 562.5e-6f), // 2 ticks per 562.5us carrier burst
32); // 32 bits per frame
return carrier_control_sm;
@@ -89,9 +72,7 @@ int nec_tx_init(PIO pio, uint pin_num) {
// Create a frame in `NEC` format from the provided 8-bit address and data
//
// Returns: a 32-bit encoded frame
//
uint32_t nec_encode_frame (uint8_t address, uint8_t data) {
uint32_t nec_encode_frame(uint8_t address, uint8_t data) {
// a normal 32-bit frame is encoded as address, inverted address, data, inverse data,
//
return address | (address ^ 0xff) << 8 | data << 16 | (data ^ 0xff) << 24;
}

View File

@@ -1,7 +1,13 @@
/**
* Copyright (c) 2021 mjcross
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "pico/stdlib.h"
#include "hardware/pio.h"
// public API
//
int nec_tx_init(PIO pio, uint pin);
uint32_t nec_encode_frame (uint8_t address, uint8_t data);
uint32_t nec_encode_frame(uint8_t address, uint8_t data);