diff --git a/meson.build b/meson.build index 6963ecf6e..a73437b85 100644 --- a/meson.build +++ b/meson.build @@ -505,6 +505,7 @@ a36plus_src = ['platform/targets/A36Plus/platform.c', 'platform/drivers/stubs/cps_io_stub.c', 'platform/drivers/stubs/nvmem_stub.c', 'platform/drivers/baseband/bk4819.c', + 'platform/drivers/baseband/bk1080.c', 'platform/drivers/baseband/radio_A36Plus.cpp'] a36plus_inc = ['platform/targets/A36Plus', diff --git a/platform/drivers/baseband/bk1080.c b/platform/drivers/baseband/bk1080.c new file mode 100644 index 000000000..a76e68a4f --- /dev/null +++ b/platform/drivers/baseband/bk1080.c @@ -0,0 +1,177 @@ +/** + * @file bk1080.c + * @author Jamiexu (doxm@foxmail.com) + * @brief + * @version 0.1 + * @date 2024-05-24 + * + * @copyright MIT License + +Copyright (c) 2024 (Jamiexu or Jamie793) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + * + */ +#include "bk1080.h" + +void i2c_start(void) +{ + BK1080_SDA_HIGH; + bk1080_delay(1); + BK1080_SCK_HIGH; + bk1080_delay(1); + BK1080_SDA_LOW; + bk1080_delay(1); + BK1080_SCK_LOW; + bk1080_delay(1); +} + +static void i2c_stop(void) +{ + BK1080_SCK_LOW; + bk1080_delay(1); + BK1080_SDA_LOW; + bk1080_delay(1); + BK1080_SCK_HIGH; + bk1080_delay(1); + BK1080_SDA_HIGH; +} + +static void i2c_write_byte(uint8_t data) +{ + for (uint8_t i = 0; i < 8; i++) + { + if (data & 0x80) + BK1080_SDA_HIGH; + else + BK1080_SDA_LOW; + bk1080_delay(1); + BK1080_SCK_HIGH; + bk1080_delay(1); + BK1080_SCK_LOW; + bk1080_delay(1); + data <<= 1; + } +} + +static uint8_t i2c_read_byte(void) +{ + uint8_t data = 0; + BK1080_SDA_DIR_IN; + bk1080_delay(5); + for (uint8_t i = 0; i < 8; i++) + { + data <<= 1; + BK1080_SCK_HIGH; + bk1080_delay(1); + data |= BK1080_SDA_READ; + BK1080_SCK_LOW; + bk1080_delay(1); + } + BK1080_SDA_DIR_OUT; + bk1080_delay(5); + return data; +} + +static void i2c_send_ack(iic_ack_t ack) +{ + if (ack) + BK1080_SDA_HIGH; + else + BK1080_SDA_LOW; + bk1080_delay(1); + BK1080_SCK_HIGH; + bk1080_delay(1); + BK1080_SCK_LOW; + bk1080_delay(1); +} + +static iic_ack_t i2c_get_ack(void) +{ + iic_ack_t ack; + BK1080_SDA_DIR_IN; + bk1080_delay(5); + BK1080_SCK_HIGH; + bk1080_delay(1); + if (BK1080_SDA_READ) + ack = I2C_NACK; + else + ack = I2C_ACK; + BK1080_SCK_LOW; + bk1080_delay(1); + BK1080_SDA_DIR_OUT; + bk1080_delay(1); + return ack; +} + +static void bk1080_delay(uint32_t count) +{ + delayUs(count); +} + +void bk1080_write_reg(bk1080_reg_t reg, uint16_t data) +{ + i2c_start(); + i2c_write_byte(BK1080_ADDRESS); + if (i2c_get_ack() == I2C_ACK) + { + i2c_write_byte(reg << 1); + if (i2c_get_ack() == I2C_ACK) + { + i2c_write_byte((data >> 8) & 0xFF); + if (i2c_get_ack() == I2C_ACK) + i2c_write_byte(data & 0xFF); + else + printf("wACK3 Not in...\r\n"); + } + else + printf("wACK2 Not in...\r\n"); + } + else + { + printf("wACK1 Not in...\r\n"); + } + i2c_stop(); +} + +uint16_t bk1080_read_reg(bk1080_reg_t reg) +{ + uint16_t data = 0; + i2c_start(); + i2c_write_byte(BK1080_ADDRESS); + if (i2c_get_ack() == I2C_ACK) + { + i2c_write_byte((reg << 1) | 0x01); + if (i2c_get_ack() == I2C_ACK) + { + data |= (i2c_read_byte() << 8); + i2c_send_ack(I2C_ACK); + data |= i2c_read_byte(); + i2c_send_ack(I2C_NACK); + } + else + printf("ACK2 Not in...\r\n"); + } + else + { + printf("ACK1 Not in...\r\n"); + } + i2c_stop(); + return data; +} diff --git a/platform/drivers/baseband/bk1080.h b/platform/drivers/baseband/bk1080.h new file mode 100644 index 000000000..541fb7fd5 --- /dev/null +++ b/platform/drivers/baseband/bk1080.h @@ -0,0 +1,61 @@ +#ifndef __BK1080_JAMIEXU_H__ +#define __BK1080_JAMIEXU_H__ +#include "gpio.h" +#include "peripherals/gpio.h" +#include "interfaces/delays.h" + +#ifdef __cplusplus +extern "C" { +#endif + + +#define BK1080_SCK_LOW gpio_clearPin(BK1080_CLK) +#define BK1080_SCK_HIGH gpio_setPin(BK1080_CLK) + +#define BK1080_SDA_LOW gpio_clearPin(BK1080_DAT) +#define BK1080_SDA_HIGH gpio_setPin(BK1080_DAT) + +#define BK1080_SDA_READ gpio_readPin(BK1080_DAT) +#define BK1080_SDA_DIR_IN gpio_setMode(BK1080_DAT, INPUT) +#define BK1080_SDA_DIR_OUT gpio_setMode(BK1080_DAT, OUTPUT) + +#define BK1080_ADDRESS 0x80 + +typedef enum +{ + BK1080_REG00 = 0X00, + BK1080_REG01 = 0X01, + BK1080_REG02 = 0X02, + BK1080_REG03 = 0X03, + BK1080_REG04 = 0X04, + BK1080_REG05 = 0X05, + BK1080_REG06 = 0X06, + BK1080_REG07 = 0X07, + BK1080_REG08 = 0X08, + BK1080_REG09 = 0X09, + BK1080_REG10 = 0X0A, + BK1080_REG11 = 0X0B +} bk1080_reg_t; + +typedef enum +{ + I2C_ACK = 0, + I2C_NACK = 1 +} iic_ack_t; + +static void i2c_start(void); +static void i2c_stop(void); +static void i2c_write_byte(uint8_t data); +static uint8_t i2c_read_byte(void); +static void i2c_send_ack(iic_ack_t ack); +static uint8_t i2c_get_ack(void); +static void bk1080_delay(uint32_t count); + +void bk1080_write_reg(bk1080_reg_t reg, uint16_t data); +uint16_t bk1080_read_reg(bk1080_reg_t reg); + +#endif + +#ifdef __cplusplus +} +#endif diff --git a/platform/drivers/baseband/radio_A36Plus.cpp b/platform/drivers/baseband/radio_A36Plus.cpp index 5e7d47275..dfb3df7ea 100644 --- a/platform/drivers/baseband/radio_A36Plus.cpp +++ b/platform/drivers/baseband/radio_A36Plus.cpp @@ -31,6 +31,7 @@ #include #include "bk4819.h" +#include "bk1080.h" #include "radioUtils.h" static const rtxStatus_t* @@ -83,11 +84,25 @@ void radio_init(const rtxStatus_t* rtxState) */ rcu_periph_clock_enable(RCU_GPIOA); rcu_periph_clock_enable(RCU_GPIOB); + rcu_periph_clock_enable(RCU_GPIOF); + gpio_setMode(BK4819_CLK, OUTPUT); gpio_setMode(BK4819_DAT, OUTPUT); gpio_setMode(BK4819_CS, OUTPUT); gpio_setMode(MIC_SPK_EN, OUTPUT); + + gpio_setMode(BK1080_DAT, OUTPUT); + gpio_setMode(BK1080_CLK, OUTPUT); + gpio_setMode(BK1080_EN, OUTPUT); + + gpio_clearPin(BK1080_EN); + // uint16_t data1 = bk1080_read_reg(BK1080_REG00); + // uint16_t data2 = bk1080_read_reg(BK1080_REG01); + // uint16_t data3 = bk1080_read_reg(BK1080_REG02); + // bk1080_write_reg(BK1080_REG02, 0x2048); + // uint16_t data4 = bk1080_read_reg(BK1080_REG02); + // printf("%d%d%d%d", data1,data2,data3,data4); gpio_clearPin(MIC_SPK_EN); bk4819_init(); @@ -159,6 +174,8 @@ void radio_enableTx() void radio_disableRtx() { // bk4819_disable_ctdcss(); + + radioStatus = OFF; } diff --git a/platform/targets/A36Plus/pinmap.h b/platform/targets/A36Plus/pinmap.h index d29a8ed8d..a1f379841 100644 --- a/platform/targets/A36Plus/pinmap.h +++ b/platform/targets/A36Plus/pinmap.h @@ -60,9 +60,9 @@ #define EFLASH_CS GPIOB,4 // BK1080 -#define BK1080_CLK GPIOC,14 -#define BK1080_DAT GPIOB,4 // Shared with external flash SCK -#define BK1080_CS GPIOC,13 +#define BK1080_CLK GPIOF,6 +#define BK1080_DAT GPIOA,3 // Shared with external flash SCK +#define BK1080_EN GPIOA,8 // BK4819 #define BK4819_CLK GPIOA,2