/* init51.c - initialize the C8051F381 chip for the lfxmit configuration.
 *  Copyright (C) 2022 by Jed Marti
 *   This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see https://www.gnu.org/licenses/.
 */

#include <C8051F380.h>
#include <stdint.h>

#include "config.h"
#include "lfxmit.h"

void init51(void)
// Specific to a particular board and processor.
{
  // Switch to 48 MHz.
  CLKSEL = 0x03;

  // P0
  // P0.0 - SPI SCK (PP = 1)
  // P0.1 - SPI MISO (OD = 1)
  // P0.2 - SPI MOSI (PP = 1)
  // P0.3 - SPI CS (PP = 1) skipped, we're in 3 pin mode.
  // P0.4 - TX0 UART (PP = 1)
  // P0.5 - RX0 UART (OD = 1)
  // P0.6 - SDA0 I2C (OD = 1) DS1085L frequency generator I2C
  // P0.7 - SCL0 I2C (OD = 1)
  P0MDIN = 0xFF;               // All digital.
  P0MDOUT = 0x1F;
  P0SKIP = 0x08;               // Skip CS as we're in 3 wire mode.
  P0 = 0xFF;

  // P1
  // P1.0 - LED0 (PP = 0)
  // P1.1 - LED1 (PP = 0)
  // P1.2 - LED2 (PP = 0)
  // P1.3 - LED3 (PP = 0)
  // P1.4 - NC (OD = 1)
  // P1.5 - NC (OD = 1)
  // P1.6 - NC (OD = 1)
  // P1.7 - NC (OD = 1)
  P1MDIN = 0xFF;              // All digital.
  P1MDOUT = 0x0F;             // PP for LED's.
  P1SKIP = 0x00;
  P1 = 0xF0;                  // LED's off.

  // P2
  // P2.0 - DS1085L control bit CTRL0. (PP = 0)
  // P2.1 - DS1085L control bit CTRL1. (PP = 0)
  // P2.2 - Morse code out. (PP = 0)
  // P2.3 - DIG0 (PP = 0)
  // P2.4 - DIG1 (PP = 0)
  // P2.5 - NC (OD = 1)
  // P2.6 - NC (OD = 1)
  // P2.7 - NC (OD = 1)
  P2MDIN = 0xFF;              // All digital.
  P2MDOUT = 0x1F;             // Digital all push/pull
  P2SKIP = 0x00;
  P2 = 0x00;

  // SPI FS control.
  SPI0CFG = 0x60;             // Master mode.
  SPI0CKR = 0x16;
  SPI0CN = 0x01;              // SPI enabled, 3 wire mode.

  XBR0 = 0x07;                // UART0, SPI0, SMBus 0.
  XBR2 = 0x00;                // No UART1, no SMBUS1.
  XBR1 = 0x40;                // Weak pullups.
}


// EOF
