get stuff ready fore rev4.2: remove ADC2 as Vbus reference (it sucked anyway), add DAC_nSHDN pin stuff

This commit is contained in:
Triss 2022-04-03 05:13:48 +02:00
parent d646d9ad14
commit f28889a77a
7 changed files with 42 additions and 31 deletions

View File

@ -8,16 +8,6 @@
void cli_dragonzap_test(void); void cli_dragonzap_test(void);
void cli_dragonzap_test(void) { void cli_dragonzap_test(void) {
// TODO: test:
// * ADC0,1,2 input
// -> ADC0,1: OK
// -> ADC2: ≃0xB80 is 5.04V; not ≃0xC38?
// 2.48V measured at ADC2 pin...
// * DAC control
// * MAX4619 control (enable, switch)
// * crowbar 1,2 control
// * NXS0108 functionality
zap_init_default(); zap_init_default();
zap_dac_set_enable(zap_dac_a | zap_dac_b); zap_dac_set_enable(zap_dac_a | zap_dac_b);
zap_dac_set_mulx2(zap_dac_a | zap_dac_b); zap_dac_set_mulx2(zap_dac_a | zap_dac_b);
@ -29,13 +19,13 @@ void cli_dragonzap_test(void) {
} }
while (true) { while (true) {
uint16_t adcv[3]; uint16_t adcv[2];
for (int i = 0; i < 3; ++i) { for (int i = 0; i < 2; ++i) {
adcv[i] = zap_adc_read(i); adcv[i] = zap_adc_read(i);
} }
uint32_t vbus = zap_adc_get_vbus(); /*uint32_t vbus = zap_adc_get_vbus();
float vbusf = vbus / (float)0x10000; float vbusf = vbus / (float)0x10000;*/
printf("ADC: 0x%04x, 0x%04x, 0x%04x = %.2f\n", adcv[0], adcv[1], adcv[2], vbusf); printf("ADC: 0x%04x, 0x%04x"/*", 0x%04x = %.2f"*/"\n", adcv[0], adcv[1]/*, adcv[2], vbusf*/);
zap_dac_set_a(adcv[0]); zap_dac_set_a(adcv[0]);
zap_dac_set_b(adcv[1]); zap_dac_set_b(adcv[1]);

View File

@ -23,7 +23,7 @@ enum zap_component {
static inline void zap_init(enum zap_component zc) { static inline void zap_init(enum zap_component zc) {
if (zc & zap_adc) zap_adc_init(); if (zc & zap_adc) zap_adc_init();
if (zc & zap_crowbar) zap_crowbar_init(); if (zc & zap_crowbar) zap_crowbar_init();
if (zc & zap_dac) zap_dac_init(); if (zc & zap_dac) zap_dac_init(false);
if (zc & zap_gpio) zap_gpio_init(false, true); if (zc & zap_gpio) zap_gpio_init(false, true);
if (zc & zap_max4619) zap_max_init(false, false); if (zc & zap_max4619) zap_max_init(false, false);
if (zc & zap_picoemp) zap_picoemp_init(); if (zc & zap_picoemp) zap_picoemp_init();

View File

@ -12,11 +12,11 @@ void zap_adc_init(void) {
adc_gpio_init(ZAP_ADC_A); adc_gpio_init(ZAP_ADC_A);
adc_gpio_init(ZAP_ADC_B); adc_gpio_init(ZAP_ADC_B);
adc_gpio_init(ZAP_ADC_REF); //adc_gpio_init(ZAP_ADC_REF);
} }
uint16_t zap_adc_read(int idx) { uint16_t zap_adc_read(int idx) {
if (idx < 0 || idx >= 3) return 0xf000; if (idx < 0 || idx >= 2/*3*/) return 0xf000;
adc_select_input(idx); adc_select_input(idx);
return adc_read(); return adc_read();

View File

@ -16,16 +16,16 @@ static inline uint16_t zap_adc_read_b(void) { return zap_adc_read(1); }
// sadly uuuh in practice it's 'a bit' noisy, assuming Vbus = 5V is // sadly uuuh in practice it's 'a bit' noisy, assuming Vbus = 5V is
// often more reliable than hand-tuning with this value, due to ADC // often more reliable than hand-tuning with this value, due to ADC
// inaccuracies, mismatch between R500 and R501, and so on... // inaccuracies, mismatch between R500 and R501, and so on...
static inline uint32_t zap_adc_get_vbus(void) { //static inline uint32_t zap_adc_get_vbus(void) {
// Vbus value, 0x0fff = 3V3 // // Vbus value, 0x0fff = 3V3
// (ADC2 value is Vbus divided down by 2) // // (ADC2 value is Vbus divided down by 2)
uint32_t vbus = (zap_adc_read(2) + 0x57/*hacky offset correction*/) * 2; // uint32_t vbus = (zap_adc_read(2) + 0x57/*hacky offset correction*/) * 2;
//
// shouldn't overflow // // shouldn't overflow
uint32_t vbusf = (vbus * (uint32_t)(3.3f * 0x10000)) / 0xfff; // uint32_t vbusf = (vbus * (uint32_t)(3.3f * 0x10000)) / 0xfff;
//
return vbusf; // return vbusf;
} //}
#endif #endif

View File

@ -13,19 +13,22 @@
static enum zap_dac_ch gain, enable; static enum zap_dac_ch gain, enable;
static uint16_t va, vb; static uint16_t va, vb;
void zap_dac_init(void) { void zap_dac_init(bool shdn) {
spi_init(ZAP_DAC_SPI, 1*1000*1000); // TODO: can we go higher? spi_init(ZAP_DAC_SPI, 1*1000*1000); // TODO: can we go higher?
spi_set_format(ZAP_DAC_SPI, 16, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST); spi_set_format(ZAP_DAC_SPI, 16, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST);
gpio_put(ZAP_DAC_NLATCH, true); gpio_put(ZAP_DAC_NLATCH, true);
gpio_put(ZAP_DAC_CS , true); gpio_put(ZAP_DAC_CS , true);
//gpio_put(ZAP_DAC_NSHDN , !shdn);
gpio_set_dir(ZAP_DAC_NLATCH, GPIO_OUT); gpio_set_dir(ZAP_DAC_NLATCH, GPIO_OUT);
gpio_set_dir(ZAP_DAC_CS , GPIO_OUT); gpio_set_dir(ZAP_DAC_CS , GPIO_OUT);
//gpio_set_dir(ZAP_DAC_NSHDN , GPIO_OUT);
gpio_set_function(ZAP_DAC_SCLK , GPIO_FUNC_SPI); gpio_set_function(ZAP_DAC_SCLK , GPIO_FUNC_SPI);
gpio_set_function(ZAP_DAC_TX , GPIO_FUNC_SPI); gpio_set_function(ZAP_DAC_TX , GPIO_FUNC_SPI);
gpio_set_function(ZAP_DAC_NLATCH, GPIO_FUNC_SIO); gpio_set_function(ZAP_DAC_NLATCH, GPIO_FUNC_SIO);
gpio_set_function(ZAP_DAC_CS , GPIO_FUNC_SIO); gpio_set_function(ZAP_DAC_CS , GPIO_FUNC_SIO);
//gpio_set_function(ZAP_DAC_NSHDN , GPIO_FUNC_SIO);
enable = 0; enable = 0;
gain = 3; // 1x for both gain = 3; // 1x for both
@ -34,9 +37,15 @@ void zap_dac_init(void) {
} }
static void zap_flush(enum zap_dac_ch ch) { static void zap_flush(enum zap_dac_ch ch) {
// buffer is always enabled (in MCP49x2 case, doesn't exist in MCP48x2):
// limited BW isn't really an issue (we use the MAX4619 for that anyway),
// and output swing is also limited by the output buffer which has the same
// limitation. so, shrug
if (ch & zap_dac_a) { if (ch & zap_dac_a) {
uint16_t v = va; uint16_t v = va;
v |= 0x8000*0; // channel A v |= 0x8000*0; // channel A
v |= 0x4000; // buffered (MCP49x2, don't care on 48x2)
if (gain & zap_dac_a) v |= 0x2000; if (gain & zap_dac_a) v |= 0x2000;
if (enable & zap_dac_a) v |= 0x1000; if (enable & zap_dac_a) v |= 0x1000;
@ -49,6 +58,7 @@ static void zap_flush(enum zap_dac_ch ch) {
if (ch & zap_dac_b) { if (ch & zap_dac_b) {
uint16_t v = vb; uint16_t v = vb;
v |= 0x8000*1; // channel B v |= 0x8000*1; // channel B
v |= 0x4000; // buffered (MCP49x2, don't care on 48x2)
if (gain & zap_dac_b) v |= 0x2000; if (gain & zap_dac_b) v |= 0x2000;
if (enable & zap_dac_b) v |= 0x1000; if (enable & zap_dac_b) v |= 0x1000;

View File

@ -5,12 +5,17 @@
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <hardware/gpio.h>
#include "zap_pinout.h"
enum zap_dac_ch { enum zap_dac_ch {
zap_dac_a = 1, zap_dac_a = 1,
zap_dac_b = 2 zap_dac_b = 2
}; };
void zap_dac_init(void); // "shdn": only relevant for MCP49x2
void zap_dac_init(bool shdn);
enum zap_dac_ch zap_dac_get_enable(void); enum zap_dac_ch zap_dac_get_enable(void);
void zap_dac_set_enable(enum zap_dac_ch ch); void zap_dac_set_enable(enum zap_dac_ch ch);
@ -21,6 +26,11 @@ uint16_t zap_dac_get_b(void);
void zap_dac_set_a(uint16_t a); void zap_dac_set_a(uint16_t a);
void zap_dac_set_b(uint16_t b); void zap_dac_set_b(uint16_t b);
// only relevant for MCP49x2
static inline void zap_dac_set_shutdown(bool shdn) {
//gpio_put(ZAP_DAC_NSHDN, !shdn);
}
static inline void zap_dac_set(uint16_t a, uint16_t b) { static inline void zap_dac_set(uint16_t a, uint16_t b) {
zap_dac_set_a(a); zap_dac_set_a(a);
zap_dac_set_b(b); zap_dac_set_b(b);

View File

@ -20,10 +20,11 @@
#define ZAP_DAC_SCLK 18 #define ZAP_DAC_SCLK 18
#define ZAP_DAC_TX 19 #define ZAP_DAC_TX 19
#define ZAP_DAC_CS 20 #define ZAP_DAC_CS 20
#define ZAP_DAC_NSHDN 28
#define ZAP_ADC_A 26 #define ZAP_ADC_A 26
#define ZAP_ADC_B 27 #define ZAP_ADC_B 27
#define ZAP_ADC_REF 28 /*#define ZAP_ADC_REF 28*/
#define ZAP_PICOEMP_PWM 14 #define ZAP_PICOEMP_PWM 14
#define ZAP_PICOEMP_CHG 15 #define ZAP_PICOEMP_CHG 15