- put hardware initialisation in its own file

- make fm_init more generic
- Add some coments for functions
- Change some of the fm-modulator functions to have a bool return type
This commit is contained in:
2024-08-04 19:57:56 +02:00
parent 14003f5834
commit 508e42b04b
8 changed files with 117 additions and 79 deletions

View File

@@ -1,57 +1,25 @@
#include <stdio.h>
#include <string.h>
#include "pico/stdlib.h"
#include "hardware/i2c.h"
#include <string.h>
#include "Autoradio-Zombie-Adapter.h"
#include "hardware/include/gpio.h"
#include "hardware/include/DACx3204.h"
#include "hardware/include/digitalpoti.h"
#include "hardware/include/hardware.h"
#include "general/include/fm-modulator.h"
#include "general/include/gain_control.h"
#include "general/include/measure.h"
#include "general/include/adc_measurement.h"
#include "general/include/cal_gain.h"
int main(){
bool is_gain_level;
int counter;
int freq = 1;
bool locked = false;
bool waslocked = false;
bool isCatching = false;
MeasurementValues measurementvalues;
// TODO put hardware int stuff elsewhere
// initial sleep for programming
sleep_ms(3000);
// init GPIO
initGPIO();
sleep_ms(100);
// init FM Modulator
fm_init();
sleep_ms(100);
hardware_init();
// switch antenna output to modulators
gpio_put(RF_SW, 0);
sleep_ms(100);
// test dac communication
if (dac_test_communication()) {
gpio_put(LED, 1);
}
//sleep_ms(200);
// configure dac
dac_configure();
sleep_ms(200);
// enable dac
gpio_put(FRQ_GEN_EN, 1);
sleep_ms(200);
// init digitalpoti
init_digitalpoti();
sleep_ms(200);
//cal_gain_table();
while (true) {
measurementvalues = measure_task();
printf("Frequency = %f\n", measurementvalues.frequency);
@@ -68,7 +36,10 @@ int main(){
// when suddenly unlocked
if (!locked && waslocked){
printf("catch\n");
catch_radio();
isCatching = catch_radio(0);
if(!isCatching){
printf("error catching the radio");
}
waslocked = false;
}
sleep_ms(500);

View File

@@ -98,6 +98,7 @@ pico_sdk_init()
add_executable(
Autoradio-Zombie-Adapter
Autoradio-Zombie-Adapter.c
hardware/hardware.c
hardware/gpio.c
hardware/KT0803.c
hardware/TCA9546A.c

View File

@@ -6,55 +6,59 @@
#include "hardware/include/KT0803.h"
#include "hardware/include/gpio.h"
TCA9546A tca9546a;
KT0803 kt0803;
KT0803 kt0803_2;
KT0803 kt0803[4] = {
{0, 0, MOD0_ON, 0}, // Device 1
{0, 0, MOD1_ON, 1}, // Device 2
{0, 0, MOD2_ON, 2}, // Device 3
{0, 0, MOD3_ON, 3} // Device 4
};
bool isStereo;
float frequency = 100.0;
void fm_init(){
// turn on modulator 0 in hardware
gpio_put(MOD0_ON, 1);
// init TCA9546A for channel 0
TCA9546A_init(&tca9546a, TCA9546A_DEFAULT_ADDR);
/**
* @brief initialises the given FM Modulator device
*
* This function initialises the given FM Modulator device and sets the frequency accordingly
*
* @return if initialisation was successful
*/
bool fm_init(int device){
bool isConnected;
// turn on modulator
gpio_put(kt0803[device].MOD_ON_PIN, 1);
TCA9546A_select_channel(&tca9546a, kt0803[device].TCA9546A_channel);
sleep_ms(100);
// init Modulator 0
KT0803_init(&kt0803, I2C0_ID);
KT0803_init(&kt0803[device], I2C0_ID);
sleep_ms(100);
// check if device is online
isConnected = KT0803_isConnected(&kt0803[device]);
if (!isConnected){
return false;
}
// start Modulator 0
KT0803_begin(&kt0803, frequency, false);
KT0803_begin(&kt0803[device], frequency, false);
sleep_ms(100);
//set to mono
KT0803_setMono(&kt0803, true);
KT0803_setMono(&kt0803[device], true);
sleep_ms(100);
//set gain to 0
KT0803_setPGA(&kt0803, 0);
KT0803_setPGA(&kt0803[device], 0);
sleep_ms(100);
return true;
}
void fm_init2(){
// turn on modulator 0 in hardware
gpio_put(MOD1_ON, 1);
// init TCA9546A for channel 0
//TCA9546A_init(&tca9546a, TCA9546A_DEFAULT_ADDR);
TCA9546A_select_channel(&tca9546a, 1);
sleep_ms(100);
// init Modulator 0
KT0803_init(&kt0803_2, I2C0_ID);
sleep_ms(100);
// start Modulator 0
KT0803_begin(&kt0803_2, 98.0, false);
sleep_ms(100);
//set to mono
KT0803_setMono(&kt0803_2, true);
sleep_ms(100);
//set gain to 0
KT0803_setPGA(&kt0803_2, 0);
sleep_ms(100);
}
void catch_radio(){
bool no_error;
/**
* @brief catch the radio search run
*
* This function trys to catch the radio search run by putting the frequency 2Mhz above of the last frequency
*
* @return if setting the frequency was sucessfull
*/
bool catch_radio(int device){
bool is_set;
// init TCA9546A for channel 0
//TCA9546A_select_channel(&tca9546a, TCA9546A_DEFAULT_ADDR);
sleep_ms(100);
@@ -64,12 +68,13 @@ void catch_radio(){
frequency = 88.0f;
}
printf("try setting frequency to: %f\n", frequency);
no_error = KT0803_setFrequency(&kt0803, frequency);
if(no_error){
is_set = KT0803_setFrequency(&kt0803[device], frequency);
if(is_set){
printf("frequency set sucessfull\n");
}else{
printf("frequency set not sucsessfull\n");
}
return is_set;
}
void fm_task(){

View File

@@ -1,7 +1,6 @@
void fm_init(void);
void fm_init2(void);
void fm_tast(void);
void catch_radio(void);
bool fm_init(int device);
void fm_task(void);
bool catch_radio(int device);
// debug functions
void toggleStereo(void);

57
hardware/hardware.c Normal file
View File

@@ -0,0 +1,57 @@
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/include/hardware.h"
#include "hardware/include/gpio.h"
#include "hardware/include/DACx3204.h"
#include "general/include/fm-modulator.h"
#include "hardware/include/digitalpoti.h"
#include "hardware/include/TCA9546A.h"
TCA9546A tca9546a;
/**
* @brief Hardware init
*
* This function inits all hardware and checks if its broght up succesuflly
*
* @return if the hardware was initialised correctly
*/
bool hardware_init(){
bool hw_init_ok = true;
// init GPIO
initGPIO();
sleep_ms(100);
// init TCA9546A for channel 0
TCA9546A_init(&tca9546a, TCA9546A_DEFAULT_ADDR);
sleep_ms(100);
// init FM Modulators
hw_init_ok = fm_init(0);
//hw_init_ok = fm_init(1);
//hw_init_ok = fm_init(2);
//hw_init_ok = fm_init(3);
sleep_ms(100);
// switch antenna output to modulators
gpio_put(RF_SW, 0);
sleep_ms(100);
// test dac communication
if (dac_test_communication()) {
gpio_put(LED, 1);
}
//sleep_ms(200);
// configure dac
dac_configure();
sleep_ms(200);
// enable dac
gpio_put(FRQ_GEN_EN, 1);
sleep_ms(200);
// init digitalpoti
init_digitalpoti();
sleep_ms(200);
return hw_init_ok;
}

View File

@@ -9,6 +9,8 @@
typedef struct {
uint8_t address;
i2c_inst_t *i2c;
int MOD_ON_PIN;
int TCA9546A_channel;
} KT0803;
// Function prototypes

View File

@@ -13,4 +13,6 @@ typedef struct {
void TCA9546A_init(TCA9546A *dev, uint8_t address);
uint8_t TCA9546A_get_channel(TCA9546A *dev);
uint8_t TCA9546A_select_channel(TCA9546A *dev, uint8_t channel);
uint8_t TCA9546A_next_channel(TCA9546A *dev);
uint8_t TCA9546A_next_channel(TCA9546A *dev);
extern TCA9546A tca9546a;

View File

@@ -0,0 +1 @@
bool hardware_init(void);