commit 312f814f2993def08bfe174a6fb0176b2e4e9a45 from: Tracey Emery date: Fri Apr 12 21:40:15 2024 UTC needed quick library for old am2315 commit - 16654fd656ffb7c7a8095666ea9d8142bcfe0720 commit + 312f814f2993def08bfe174a6fb0176b2e4e9a45 blob - /dev/null blob + 378eac25d311703f3f2cd456d8036da525cd0366 (mode 644) --- /dev/null +++ Raspberry-Pi-Pico-AM_NEW/.gitignore @@ -0,0 +1 @@ +build blob - /dev/null blob + c32963ff4836dda623f551c08d8c2e0527652ddc (mode 644) --- /dev/null +++ Raspberry-Pi-Pico-AM_NEW/CMakeLists.txt @@ -0,0 +1,33 @@ +cmake_minimum_required(VERSION 3.13) + +include(/usr/local/raspberrypi-pico-sdk/pico_sdk_init.cmake) + +project(am2315) + +# initialize the Raspberry Pi Pico SDK +pico_sdk_init() + +add_executable(am2315 + am2315.cpp + libam2315.cpp + ) + +# pull in common dependencies +target_link_libraries(am2315 + pico_stdlib + pico_malloc + hardware_i2c + ) + +add_compile_options( + -Wall + -Wno-format + -Wno-unused-function + -Wno-maybe-uninitialized + ) + +pico_enable_stdio_usb(am2315 1) +pico_enable_stdio_uart(am2315 0) + +# create map/bin/hex/uf2 file etc. +pico_add_extra_outputs(am2315) blob - /dev/null blob + 378d9820c75ff7dbe581b04a7a538390ab3c6e48 (mode 644) --- /dev/null +++ Raspberry-Pi-Pico-AM_NEW/LICENSE.md @@ -0,0 +1,25 @@ +BSD 2-Clause License + +Copyright (c) 2024 Tracey Emery K7TLE +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. blob - /dev/null blob + 8efdae66b43bcef2749b17aa85279a5c3bca0d3e (mode 644) --- /dev/null +++ Raspberry-Pi-Pico-AM_NEW/README.md @@ -0,0 +1,35 @@ +# Raspberry Pi Pico AM2315 Library + +Raspberry Pi Pico AM2315 Library + +This AM2315 library is a driver for the AM2315 Humidity and Temperature Module. + +Usage +----- + +Clone the library to your project: + + git clone https://github.com/basepr1me/Raspberry-Pi-Pico-AM2315-Library.git + +One class instantiation method is in place: + + AM2315(config); + +Example +------- + +See the [AM2315 Example](am2315.cpp) file for more information. + +Notes +----- + +The make.sh file is designed for OpenBSD ksh. + +Author +------ + +[Tracey Emery](https://github.com/basepr1me/) + +If you like this software, consider [donating](https://k7tle.com/?donate=1). + +See the [License](LICENSE.md) file for more information. blob - /dev/null blob + 0a89f840dbe236951e36fabf3c60ced4f182b7ac (mode 644) --- /dev/null +++ Raspberry-Pi-Pico-AM_NEW/am2315.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2024 Tracey Emery + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include + +#include "pico/malloc.h" +#include "pico/stdlib.h" + +#include "hardware/i2c.h" +#include "libam2315.h" + +int main(void); + +int +main() +{ + + stdio_init_all(); + + struct env *config; + + config = (struct env *) malloc(sizeof(struct env *)); + + config->i2c_addr = AM2315_ADDR; + config->i2c_channel = I2C0; + config->i2c_speed = 400000; + config->started = false; + + AM2315 am(config); + + while(config->started == false); + + printf("\nReady to read!\n"); + free(config); + + while(1) { + am.read_all(config); + am.temperature *= 1.8; + am.temperature += 32; + printf("%.2ff, %.2f%%\n", am.temperature, am.humidity); + sleep_ms(READ_WAIT); + } + + return 0; +} blob - /dev/null blob + 0f003d7ae87b4afabd889e15cccc7beda3db11aa (mode 644) --- /dev/null +++ Raspberry-Pi-Pico-AM_NEW/libam2315.cpp @@ -0,0 +1,138 @@ +/* + * Copyright (c) 2024 Tracey Emery + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include +#include + +#include "pico/stdlib.h" +#include "hardware/i2c.h" + +#include "libam2315.h" + +#define DEBUG 0 +#define DPRINTF(x,y) do { if (DEBUG) { printf("%s", x); sleep_ms(y); }} \ + while(0) + +static void begin(struct env *); +static void write(struct env *, const uint8_t *, size_t, bool); +static void read(struct env *, uint8_t *, size_t, bool); + +AM2315::AM2315(struct env *config) +{ + if (DEBUG) + DPRINTF("Initializing I2C\n", 500); + + switch(config->i2c_channel) { + case I2C1: + config->i2c = i2c1; + i2c_init(config->i2c, config->i2c_speed); + gpio_set_function(PICO_I2C1_SDA_PIN, GPIO_FUNC_I2C); + gpio_set_function(PICO_I2C1_SCL_PIN, GPIO_FUNC_I2C); + gpio_pull_up(PICO_I2C1_SDA_PIN); + gpio_pull_up(PICO_I2C1_SCL_PIN); + break; + case I2C0: + default: + config->i2c = i2c0; + i2c_init(config->i2c, config->i2c_speed); + gpio_set_function(PICO_I2C0_SDA_PIN, GPIO_FUNC_I2C); + gpio_set_function(PICO_I2C0_SCL_PIN, GPIO_FUNC_I2C); + gpio_pull_up(PICO_I2C0_SDA_PIN); + gpio_pull_up(PICO_I2C0_SCL_PIN); + break; + } + + begin(config); +} + +void +AM2315::read_all(struct env *config) +{ + uint8_t data[8]; + int ret; + + memset(&data, 0, sizeof(data)); + write(config, &data[0], 1, false); + sleep_ms(50); + + data[0] = AM2315_REGISTER_READ; + data[1] = 0x00; + data[2] = 0x04; + + write(config, data, 3, false); + read(config, data, 8, true); + + if (data[0] != AM2315_REGISTER_READ) { + if(DEBUG) + DPRINTF("Didn't Read\n", 1000); + } + if (data[1] != 4) { + if (DEBUG) + DPRINTF("Short Read\n", 1000); + } + + humidity = data[2]; + humidity *= 256; + humidity += data[3]; + humidity /= 10; + + temperature = data[4] & 0x7F; + temperature *= 256; + temperature += data[5]; + temperature /= 10; + + if (data[4] >> 7) + temperature = -temperature; +} + +static void +write(struct env *config, const uint8_t *src, size_t len, bool nostop) +{ + int ret; + + ret = i2c_write_blocking(config->i2c, config->i2c_addr, src, len, + nostop); + sleep_us(200); + if (ret == PICO_ERROR_GENERIC) { + if (DEBUG) + DPRINTF("WRITE FAILED!\n", 0); + } +} + +static void +read(struct env *config, uint8_t *dst, size_t len, bool nostop) +{ + int ret; + + ret = i2c_read_blocking(config->i2c, config->i2c_addr, dst, len, + nostop); + sleep_us(200); + if (ret < 0) { + if (DEBUG) + DPRINTF("READ FAILED!\n", 0); + } +} + +static void +begin(struct env *config) +{ + /* give device some init time */ + sleep_ms(150); + config->started = true; +} blob - /dev/null blob + 9a364ad56c15da96ce4b3adbe64bdc2f8911994c (mode 644) --- /dev/null +++ Raspberry-Pi-Pico-AM_NEW/libam2315.h @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2022, 2024 Tracey Emery + * Copyright (c) 2021 Limor Fried/Ladyada for Adafruit Industries, for typing + * most of the SI5351 registers and values + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef _LIBAM2315_H +#define _LIBAM2315_H + +struct env { + uint8_t i2c_addr; + uint8_t i2c_channel; + uint32_t i2c_speed; + i2c_inst_t *i2c; + bool started; +}; + +enum I2C_t { + I2C0, + I2C1 +}; + +/* change to what you want */ +#define PICO_I2C0_SDA_PIN 16 +#define PICO_I2C0_SCL_PIN 17 +#define PICO_I2C1_SDA_PIN 2 +#define PICO_I2C1_SCL_PIN 3 + +/* AM2315 Address */ +#define AM2315_ADDR 0x5C + +/*AM2315 Register */ +#define AM2315_REGISTER_READ 0x03 + +#define READ_WAIT 5000 + +class +AM2315 +{ + public: + AM2315(struct env *); + void read_all(struct env *); + float temperature; + float humidity; + + private: + bool read_data(void); +}; + +#endif // _LIBAM2315_H blob - /dev/null blob + 7395a32ce9b3be3ab120794d9dd03a38f6d6c432 (mode 755) --- /dev/null +++ Raspberry-Pi-Pico-AM_NEW/make.sh @@ -0,0 +1,23 @@ +#!/bin/ksh +clear +printf "\nRUNNING THIS WILL DESTROY YOUR BUILD DIRECTORY!!!" +printf "\nDo you wish to proceed? [Y|n]: " + +read proceed + +if [[ $proceed != "y" && $proceed != "Y" && $proceed != "" ]]; then + printf "\nExitting!\n" + exit +fi + +printf "\nBuilding project...\n\n"; + +rm -rf build +mkdir build && cd build +cmake -DPICO_TOOLCHAIN_PATH=/usr/local/arm-none-eabi/bin .. +make + +printf "\nFiles built:\n\n" +ls -lah . + +printf "\nDone\n\n"; blob - /dev/null blob + 42f530b399a987e89b4b773f136b9fdeb36514b4 (mode 755) --- /dev/null +++ Raspberry-Pi-Pico-AM_NEW/make2.sh @@ -0,0 +1,12 @@ +#!/bin/ksh +clear +printf "\nBuilding project...\n\n"; + +cd build +cmake -DPICO_TOOLCHAIN_PATH=/usr/local/arm-none-eabi/bin .. +make + +printf "\nFiles built:\n\n" +ls -lah . + +printf "\nDone\n\n";