diff --git a/am_fix.c b/am_fix.c index 8983a83..56114fb 100644 --- a/am_fix.c +++ b/am_fix.c @@ -81,11 +81,6 @@ typedef struct // // these 4 tables need a measuring/calibration update // -// -// QUESTION: why do I have to surround the negative numbers in brackets ??? -// if I don't add the brackets, reading the table returns unexpected/different values !!! -// -// //// static const int16_t lna_short_dB[] = { -19, -16, -11, 0}; // was (but wrong) // static const int16_t lna_short_dB[] = { (-33), (-30), (-24), 0}; // corrected'ish // static const int16_t lna_dB[] = { (-24), (-19), (-14), ( -9), (-6), (-4), (-2), 0}; @@ -220,7 +215,7 @@ unsigned int hold_counter[2] = {0, 0}; int16_t rssi_gain_diff[2] = {0, 0}; // used to limit the max RF gain -unsigned int max_index = ARRAY_SIZE(gain_table) - 1; +const unsigned max_index = ARRAY_SIZE(gain_table) - 1; // -89dBm, any higher and the AM demodulator starts to saturate/clip/distort const int16_t desired_rssi = (-89 + 160) * 2; @@ -230,13 +225,13 @@ void AM_fix_init(void) for (int i = 0; i < 2; i++) { gain_table_index[i] = original_index; // re-start with original QS setting } - - // use the full range of available gains - max_index = ARRAY_SIZE(gain_table) - 1; } -void AM_fix_reset(const int vfo) +void AM_fix_reset(const unsigned vfo) { // reset the AM fixer upper + if (vfo > 1) + return; + #ifdef ENABLE_AM_FIX_SHOW_DATA counter = 0; #endif @@ -254,10 +249,10 @@ void AM_fix_reset(const int vfo) // won't/don't do it for itself, we're left to bodging it ourself by // playing with the RF front end gain setting // -void AM_fix_10ms(const int vfo) +void AM_fix_10ms(const unsigned vfo) { - int16_t diff_dB; - int16_t rssi; + if(vfo > 1) + return; switch (gCurrentFunction) { @@ -286,6 +281,7 @@ void AM_fix_10ms(const int vfo) } #endif + int16_t rssi; { // sample the current RSSI level // average it with the previous rssi (a bit of noise/spike immunity) const int16_t new_rssi = BK4819_GetRSSI(); @@ -310,7 +306,6 @@ void AM_fix_10ms(const int vfo) gCurrentRSSI[vfo] = rssi - rssi_gain_diff[vfo]; #endif - // automatically adjust the RF RX gain // update the gain hold counter @@ -318,7 +313,7 @@ void AM_fix_10ms(const int vfo) hold_counter[vfo]--; // dB difference between actual and desired RSSI level - diff_dB = (rssi - desired_rssi) / 2; + int16_t diff_dB = (rssi - desired_rssi) / 2; if (diff_dB > 0) { // decrease gain unsigned int index = gain_table_index[vfo]; // current position we're at @@ -359,7 +354,6 @@ void AM_fix_10ms(const int vfo) { // apply the new settings to the front end registers - const unsigned int index = gain_table_index[vfo]; // remember the new table index @@ -385,15 +379,20 @@ void AM_fix_10ms(const int vfo) } #ifdef ENABLE_AM_FIX_SHOW_DATA - -void AM_fix_print_data(const int vfo, char *s) { - if (s != NULL && vfo >= 0 && vfo < (int)ARRAY_SIZE(gain_table_index)) { +void AM_fix_print_data(const unsigned vfo, char *s) { + if (s != NULL && vfo < ARRAY_SIZE(gain_table_index)) { const unsigned int index = gain_table_index[vfo]; sprintf(s, "%2u %4ddB %3u", index, gain_table[index].gain_dB, prev_rssi[vfo]); counter = 0; } } - #endif +int16_t AM_fix_get_rssi_gain_diff(const unsigned vfo) +{ + if(vfo > 1) + return 0; + return rssi_gain_diff[vfo]; +} + #endif diff --git a/am_fix.h b/am_fix.h index 3c94d17..3ee6739 100644 --- a/am_fix.h +++ b/am_fix.h @@ -21,14 +21,13 @@ #include #ifdef ENABLE_AM_FIX - extern int16_t rssi_gain_diff[2]; - void AM_fix_init(void); - void AM_fix_reset(const int vfo); - void AM_fix_10ms(const int vfo); + void AM_fix_reset(const unsigned vfo); + void AM_fix_10ms(const unsigned vfo); #ifdef ENABLE_AM_FIX_SHOW_DATA - void AM_fix_print_data(const int vfo, char *s); + void AM_fix_print_data(const unsigned vfo, char *s); #endif + int16_t AM_fix_get_rssi_gain_diff(const unsigned vfo); #endif diff --git a/app/app.c b/app/app.c index d029974..0f2f321 100644 --- a/app/app.c +++ b/app/app.c @@ -16,6 +16,7 @@ #include +#include "am_fix.h" #include "app/action.h" #ifdef ENABLE_AIRCOPY #include "app/aircopy.h" @@ -44,7 +45,6 @@ #include "driver/keyboard.h" #include "driver/st7565.h" #include "driver/system.h" -#include "am_fix.h" #include "dtmf.h" #include "external/printf/printf.h" #include "frequencies.h" @@ -73,7 +73,7 @@ static void UpdateRSSI(const int vfo) #ifdef ENABLE_AM_FIX // add RF gain adjust compensation if (gEeprom.VfoInfo[vfo].Modulation == MODULATION_AM && gSetting_AM_fix) - rssi -= rssi_gain_diff[vfo]; + rssi -= AM_fix_get_rssi_gain_diff(vfo); #endif if (gCurrentRSSI[vfo] == rssi) @@ -171,8 +171,6 @@ static void CheckForIncoming(void) static void HandleIncoming(void) { - bool bFlag; - if (!g_SquelchLost) { // squelch is closed #ifdef ENABLE_DTMF_CALLING if (gDTMF_RX_index > 0) @@ -185,7 +183,7 @@ static void HandleIncoming(void) return; } - bFlag = (gScanStateDir == SCAN_OFF && gCurrentCodeType == CODE_TYPE_OFF); + bool bFlag = (gScanStateDir == SCAN_OFF && gCurrentCodeType == CODE_TYPE_OFF); #ifdef ENABLE_NOAA if (IS_NOAA_CHANNEL(gRxVfo->CHANNEL_SAVE) && gNOAACountdown_10ms > 0) { @@ -199,7 +197,9 @@ static void HandleIncoming(void) gFoundCTCSS = false; } - if (g_CDCSS_Lost && gCDCSSCodeType == CDCSS_POSITIVE_CODE && (gCurrentCodeType == CODE_TYPE_DIGITAL || gCurrentCodeType == CODE_TYPE_REVERSE_DIGITAL)) { + if (g_CDCSS_Lost && gCDCSSCodeType == CDCSS_POSITIVE_CODE + && (gCurrentCodeType == CODE_TYPE_DIGITAL || gCurrentCodeType == CODE_TYPE_REVERSE_DIGITAL)) + { gFoundCDCSS = false; } else if (!bFlag) @@ -444,11 +444,11 @@ static void HandleFunction(void) } } -void APP_StartListening(FUNCTION_Type_t Function, const bool reset_am_fix) +void APP_StartListening(FUNCTION_Type_t function, const bool reset_am_fix) { (void)reset_am_fix; - const unsigned int chan = gEeprom.RX_VFO; + const unsigned int vfo = gEeprom.RX_VFO; // const unsigned int chan = gRxVfo->CHANNEL_SAVE; #ifdef ENABLE_DTMF_CALLING @@ -462,7 +462,7 @@ void APP_StartListening(FUNCTION_Type_t Function, const bool reset_am_fix) #endif // clear the other vfo's rssi level (to hide the antenna symbol) - gVFO_RSSI_bar_level[(chan + 1) & 1u] = 0; + gVFO_RSSI_bar_level[!vfo] = 0; AUDIO_AudioPathOn(); gEnableSpeaker = true; @@ -478,7 +478,7 @@ void APP_StartListening(FUNCTION_Type_t Function, const bool reset_am_fix) gRxVfo->CHANNEL_SAVE = gNoaaChannel + NOAA_CHANNEL_FIRST; gRxVfo->pRX->Frequency = NoaaFrequencyTable[gNoaaChannel]; gRxVfo->pTX->Frequency = NoaaFrequencyTable[gNoaaChannel]; - gEeprom.ScreenChannel[chan] = gRxVfo->CHANNEL_SAVE; + gEeprom.ScreenChannel[vfo] = gRxVfo->CHANNEL_SAVE; gNOAA_Countdown_10ms = 500; // 5 sec gScheduleNOAA = false; @@ -504,35 +504,28 @@ void APP_StartListening(FUNCTION_Type_t Function, const bool reset_am_fix) #ifdef ENABLE_AM_FIX if (gRxVfo->Modulation == MODULATION_AM && gSetting_AM_fix) { // AM RX mode if (reset_am_fix) - AM_fix_reset(chan); // TODO: only reset it when moving channel/frequency - AM_fix_10ms(chan); + AM_fix_reset(vfo); // TODO: only reset it when moving channel/frequency + AM_fix_10ms(vfo); } #endif - // AF gain - original QS values - // if (gRxVfo->Modulation != MODULATION_FM){ - // BK4819_WriteRegister(BK4819_REG_48, 0xB3A8); - // } - // else - { BK4819_WriteRegister(BK4819_REG_48, (11u << 12) | // ??? .. 0 to 15, doesn't seem to make any difference ( 0u << 10) | // AF Rx Gain-1 (gEeprom.VOLUME_GAIN << 4) | // AF Rx Gain-2 (gEeprom.DAC_GAIN << 0)); // AF DAC Gain (after Gain-1 and Gain-2) - } #ifdef ENABLE_VOICE if (gVoiceWriteIndex == 0) // AM/FM RX mode will be set when the voice has finished #endif RADIO_SetModulation(gRxVfo->Modulation); // no need, set it now - FUNCTION_Select(Function); + FUNCTION_Select(function); #ifdef ENABLE_FMRADIO - if (Function == FUNCTION_MONITOR || gFmRadioMode) + if (function == FUNCTION_MONITOR || gFmRadioMode) #else - if (Function == FUNCTION_MONITOR) + if (function == FUNCTION_MONITOR) #endif { // squelch is disabled if (gScreenToDisplay != DISPLAY_MENU) // 1of11 .. don't close the menu diff --git a/app/app.h b/app/app.h index ef3b706..0c1b85d 100644 --- a/app/app.h +++ b/app/app.h @@ -24,7 +24,7 @@ #include "radio.h" void APP_EndTransmission(void); -void APP_StartListening(FUNCTION_Type_t Function, const bool reset_am_fix); +void APP_StartListening(FUNCTION_Type_t function, const bool reset_am_fix); uint32_t APP_SetFreqByStepAndLimits(VFO_Info_t *pInfo, int8_t direction, uint32_t lower, uint32_t upper); uint32_t APP_SetFrequencyByStep(VFO_Info_t *pInfo, int8_t direction); void APP_Update(void); diff --git a/app/spectrum.c b/app/spectrum.c index a71620c..eece6d2 100644 --- a/app/spectrum.c +++ b/app/spectrum.c @@ -282,14 +282,13 @@ uint16_t GetRssi() { while ((BK4819_ReadRegister(0x63) & 0b11111111) >= 255) { SYSTICK_DelayUs(100); } - if(settings.modulationType == MODULATION_AM) - { - return BK4819_GetRSSI() - rssi_gain_diff[vfo]; //return the corrected RSSI to allow for AM_Fixs AGC action. - } - else - { - return BK4819_GetRSSI(); - } + if(settings.modulationType == MODULATION_AM) { + //return the corrected RSSI to allow for AM_Fixs AGC action. + return BK4819_GetRSSI() - AM_fix_get_rssi_gain_diff(vfo); + } + else { + return BK4819_GetRSSI(); + } } diff --git a/board.c b/board.c index facaffc..2bf95ea 100644 --- a/board.c +++ b/board.c @@ -493,7 +493,7 @@ void BOARD_Init(void) BOARD_GPIO_Init(); BACKLIGHT_InitHardware(); BOARD_ADC_Init(); - ST7565_Init(true); + ST7565_Init(); #ifdef ENABLE_FMRADIO BK1080_Init(0, false); #endif diff --git a/driver/st7565.c b/driver/st7565.c index 212d188..832f18d 100644 --- a/driver/st7565.c +++ b/driver/st7565.c @@ -25,118 +25,51 @@ #include "driver/system.h" #include "misc.h" -uint8_t gStatusLine[128]; -uint8_t gFrameBuffer[7][128]; - -void ST7565_DrawLine(const unsigned int Column, const unsigned int Line, const unsigned int Size, const uint8_t *pBitmap) -{ - unsigned int i; - - SPI_ToggleMasterMode(&SPI0->CR, false); - - ST7565_SelectColumnAndLine(Column + 4U, Line); +uint8_t gStatusLine[LCD_WIDTH]; +uint8_t gFrameBuffer[FRAME_LINES][LCD_WIDTH]; +static void DrawLine(uint8_t column, uint8_t line, const uint8_t * lineBuffer, unsigned size_defVal) +{ + ST7565_SelectColumnAndLine(column + 4, line); GPIO_SetBit(&GPIOB->DATA, GPIOB_PIN_ST7565_A0); - - if (pBitmap != NULL) - { - for (i = 0; i < Size; i++) - { - while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {} - SPI0->WDR = pBitmap[i]; - } + for (unsigned i = 0; i < size_defVal; i++) { + while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {} + SPI0->WDR = lineBuffer ? lineBuffer[i] : size_defVal; } - else - { - for (i = 0; i < Size; i++) - { - while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {} - SPI0->WDR = 0; - } - } - SPI_WaitForUndocumentedTxFifoStatusBit(); +} +void ST7565_DrawLine(const unsigned int Column, const unsigned int Line, const uint8_t *pBitmap, const unsigned int Size) +{ + SPI_ToggleMasterMode(&SPI0->CR, false); + DrawLine(Column, Line, pBitmap, Size); SPI_ToggleMasterMode(&SPI0->CR, true); } void ST7565_BlitFullScreen(void) { - unsigned int Line; - SPI_ToggleMasterMode(&SPI0->CR, false); - ST7565_WriteByte(0x40); - - for (Line = 0; Line < ARRAY_SIZE(gFrameBuffer); Line++) - { - unsigned int Column; - ST7565_SelectColumnAndLine(4, Line + 1); - GPIO_SetBit(&GPIOB->DATA, GPIOB_PIN_ST7565_A0); - for (Column = 0; Column < ARRAY_SIZE(gFrameBuffer[0]); Column++) - { - while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {} - SPI0->WDR = gFrameBuffer[Line][Column]; - } - SPI_WaitForUndocumentedTxFifoStatusBit(); + for (unsigned line = 0; line < FRAME_LINES; line++) { + DrawLine(0, line+1, gFrameBuffer[line], LCD_WIDTH); } - - #if 0 - // whats the delay for I wonder, it holds things up :( - SYSTEM_DelayMs(20); - #else -// SYSTEM_DelayMs(1); - #endif - SPI_ToggleMasterMode(&SPI0->CR, true); } void ST7565_BlitStatusLine(void) { // the top small text line on the display - - unsigned int i; - SPI_ToggleMasterMode(&SPI0->CR, false); - ST7565_WriteByte(0x40); // start line ? - - ST7565_SelectColumnAndLine(4, 0); - - GPIO_SetBit(&GPIOB->DATA, GPIOB_PIN_ST7565_A0); - - for (i = 0; i < ARRAY_SIZE(gStatusLine); i++) - { - while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {} - SPI0->WDR = gStatusLine[i]; - } - - SPI_WaitForUndocumentedTxFifoStatusBit(); - + DrawLine(0, 0, gStatusLine, LCD_WIDTH); SPI_ToggleMasterMode(&SPI0->CR, true); } -void ST7565_FillScreen(uint8_t Value) +void ST7565_FillScreen(uint8_t value) { - unsigned int i; - - // reset some of the displays settings to try and overcome the radios hardware problem - RF corrupting the display - ST7565_Init(false); - SPI_ToggleMasterMode(&SPI0->CR, false); - - for (i = 0; i < 8; i++) - { - unsigned int j; - ST7565_SelectColumnAndLine(0, i); - GPIO_SetBit(&GPIOB->DATA, GPIOB_PIN_ST7565_A0); - for (j = 0; j < 132; j++) - { - while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {} - SPI0->WDR = Value; - } - SPI_WaitForUndocumentedTxFifoStatusBit(); + for (unsigned i = 0; i < 8; i++) { + DrawLine(0, i, NULL, value); } - SPI_ToggleMasterMode(&SPI0->CR, true); } @@ -205,40 +138,33 @@ uint8_t cmds[] = { ST7565_CMD_DISPLAY_ON_OFF | 1, // Display ON/OFF: ON }; -void ST7565_Init(const bool full) +void ST7565_Init(void) { - if (full) { - SPI0_Init(); - ST7565_HardwareReset(); - SPI_ToggleMasterMode(&SPI0->CR, false); - ST7565_WriteByte(ST7565_CMD_SOFTWARE_RESET); // software reset - SYSTEM_DelayMs(120); - } - else - SPI_ToggleMasterMode(&SPI0->CR, false); + SPI0_Init(); + ST7565_HardwareReset(); + SPI_ToggleMasterMode(&SPI0->CR, false); + ST7565_WriteByte(ST7565_CMD_SOFTWARE_RESET); // software reset + SYSTEM_DelayMs(120); for(uint8_t i = 0; i < 8; i++) ST7565_WriteByte(cmds[i]); - if (full) { - ST7565_WriteByte(ST7565_CMD_POWER_CIRCUIT | 0b011); // VB=0 VR=1 VF=1 - SYSTEM_DelayMs(1); - ST7565_WriteByte(ST7565_CMD_POWER_CIRCUIT | 0b110); // VB=1 VR=1 VF=0 - SYSTEM_DelayMs(1); + ST7565_WriteByte(ST7565_CMD_POWER_CIRCUIT | 0b011); // VB=0 VR=1 VF=1 + SYSTEM_DelayMs(1); + ST7565_WriteByte(ST7565_CMD_POWER_CIRCUIT | 0b110); // VB=1 VR=1 VF=0 + SYSTEM_DelayMs(1); + + for(uint8_t i = 0; i < 4; i++) // why 4 times? + ST7565_WriteByte(ST7565_CMD_POWER_CIRCUIT | 0b111); // VB=1 VR=1 VF=1 + + SYSTEM_DelayMs(40); - for(uint8_t i = 0; i < 4; i++) // why 4 times? - ST7565_WriteByte(ST7565_CMD_POWER_CIRCUIT | 0b111); // VB=1 VR=1 VF=1 - - SYSTEM_DelayMs(40); - } - ST7565_WriteByte(ST7565_CMD_SET_START_LINE | 0); // line 0 ST7565_WriteByte(ST7565_CMD_DISPLAY_ON_OFF | 1); // D=1 SPI_WaitForUndocumentedTxFifoStatusBit(); SPI_ToggleMasterMode(&SPI0->CR, true); - if (full) - ST7565_FillScreen(0x00); + ST7565_FillScreen(0x00); } void ST7565_FixInterfGlitch(void) diff --git a/driver/st7565.h b/driver/st7565.h index 6d49fe6..c063734 100644 --- a/driver/st7565.h +++ b/driver/st7565.h @@ -22,15 +22,16 @@ #define LCD_WIDTH 128 #define LCD_HEIGHT 64 +#define FRAME_LINES 7 -extern uint8_t gStatusLine[128]; -extern uint8_t gFrameBuffer[7][128]; +extern uint8_t gStatusLine[LCD_WIDTH]; +extern uint8_t gFrameBuffer[FRAME_LINES][LCD_WIDTH]; -void ST7565_DrawLine(const unsigned int Column, const unsigned int Line, const unsigned int Size, const uint8_t *pBitmap); +void ST7565_DrawLine(const unsigned int Column, const unsigned int Line, const uint8_t *pBitmap, const unsigned int Size); void ST7565_BlitFullScreen(void); void ST7565_BlitStatusLine(void); void ST7565_FillScreen(uint8_t Value); -void ST7565_Init(const bool full); +void ST7565_Init(void); void ST7565_FixInterfGlitch(void); void ST7565_HardwareReset(void); void ST7565_SelectColumnAndLine(uint8_t Column, uint8_t Line); diff --git a/ui/battery.c b/ui/battery.c index 0441994..1a3e8a0 100644 --- a/ui/battery.c +++ b/ui/battery.c @@ -52,5 +52,5 @@ void UI_DisplayBattery(uint8_t level, uint8_t blink) { uint8_t bitmap[sizeof(BITMAP_BatteryLevel1)]; UI_DrawBattery(bitmap, level, blink); - ST7565_DrawLine(LCD_WIDTH - sizeof(bitmap), 0, sizeof(bitmap), bitmap); + ST7565_DrawLine(LCD_WIDTH - sizeof(bitmap), 0, bitmap, sizeof(bitmap)); }