From b8cf25e2ba80c484846437059563779d9ca923cc Mon Sep 17 00:00:00 2001 From: Joshua Drake Date: Fri, 3 Nov 2023 06:07:59 -0500 Subject: Initial commit. --- SPI.c | 74 ++++++++++ SPI.h | 495 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.c | 70 ++++++++++ 3 files changed, 639 insertions(+) create mode 100644 SPI.c create mode 100644 SPI.h create mode 100644 main.c diff --git a/SPI.c b/SPI.c new file mode 100644 index 0000000..c490ea1 --- /dev/null +++ b/SPI.c @@ -0,0 +1,74 @@ +/* + * File: SPI.c + * Author: josh + * + * Created on November 2, 2023, 4:37 AM + */ + + +#include "SPI.h" + + uint16_t WriteBuffer[MY_BUFFER_SIZE]; + uint16_t ReadBuffer[MY_BUFFER_SIZE]; + uint16_t writeData; + uint16_t readData; + uint8_t addr0; + uint8_t addr1; + bool write; + unsigned int total; + +/*do + { + total = MSSP1_SPI_Exchange8bitBuffer( &myWriteBuffer[total], MY_BUFFER_SIZE - total, &myWriteBuffer[total]); + + // Do something else... + + } while( total < MY_BUFFER_SIZE );*/ + + void Message_OUT(uint8_t Ohm_150, uint8_t Ohm_68, bool testLED) + { + if (Ohm_150 > 15) Ohm_150 = 15; + if (Ohm_68 > 5) Ohm_68 = 5; + WriteBuffer[0] = WriteBuffer[2] = WriteBuffer[4] = (write << 8) + (!write << 7) + 0b000000; + WriteBuffer[1] = (0xFF << Ohm_150) ^ (0xFF); + WriteBuffer[3] = (0xFF << (Ohm_150 - 8)) ^ (0xFF); + WriteBuffer[5] = (0xFF << (Ohm_68 - 4)) ^ (0xFF) + (testLED << 8); + if (Ohm_68 == 5) WriteBuffer[3]+= 0b10000000; + } + + void Message_BIM(uint8_t outputs[3]) + + void Message_MAPIN0(uint8_t outputs[3]) + + + void Message_MAPIN1(uint8_t outputs[3]) + + void Message_INST(void) + + void Message_DAGIOL(uint8_t outputs[3]) + + void Message_DAGOSM(void) + + void Message_DAGOLON(void) + + void Message_DAGOLONEN(uint8_t outputs[3]) + + void Message_HWCR(uint8_t outputs[3]) + + void Message_HWCROCL(uint8_t outputs[3]) + + void Message_HWCRPWM(uint8_t outputs[3]) + + void Message_PWMCR0(uint16_t outputs[3]) + + void Message_PWMCR1(uint16_t outputs[3]) + + void Message_PWMOUT(uint8_t outputs[3]) + + void Message_PWMMAP(uint8_t outputs[3]) + { + WriteBuffer[0] = WriteBuffer[2] = WriteBuffer[4] = (write << 8) + (!write << 7) + 0b100101; + WriteBuffer[1] = outputs[0]; + WriteBuffer[3] = outputs[1]; + WriteBuffer[5] = outputs[2]; + } diff --git a/SPI.h b/SPI.h new file mode 100644 index 0000000..3c7cf70 --- /dev/null +++ b/SPI.h @@ -0,0 +1,495 @@ +/* + * File: SPI.h + * Author: Joshua Ellis Drake + */ + +// This is a guard condition so that contents of this file are not included +// more than once. +#ifndef XC_SPI_H +#define XC_SPI_H + +#include "mssp1_spi.h" + +#define MY_BUFFER_SIZE 6 //3 Devices, each of which is 16 bit. (16/8)*3=6 + + enum messages + { + OUT = 0, //Output control On or Off + BIM, //Sets Latch off or Auto Restart Mode + MAPIN0, //Mapping of IN0 to OUTx + MAPIN1, //Mapping of IN1 to OUTx + INST, //Reports Transmission Bit Errors and Reports INx pin Status + OLDCC, //Open Load Diagnostic Current Control + OSM, //Output Status Monitor | Reports open circuit conditions in OFF sate | Reports output status in the ON state. + OLOM, //Open Load On Monitor + HWCR, //Hardware Configuration Register | HWCR.ACT Active Mode Transitions | HWCR.RST SPI Register Reset | HWCR.PAR Sets Parallel Channel Operation + OCL, //Output Clear Latch | Clears the error latch for a selected output + PWMCR, //PWM Configuration Register | HWCR_PWM.ADJ Base PWM frequency adjust | HWCR_PWM.PWM1 PWM Generator 1 Activation | HWCR_PWM.PWM0 PWM Generator 0 Activation + PWMGC0, //PWM_CR0.FREQ Sets internal divide by clock of PWM generator for Gen 0 | PWM_CR0.DC Sets PWM Generator 0 duty cycle + PWMGC1, //PWM_CR1.FREQ Sets internal divide by clock of PWM generator for Gen 1 | PWM_CR1.DC Sets PWM Generator 1 duty cycle + PWMOC, //Selects the active PWM generator for OUTx + PWMMAP //Selects one of the two PWM generators + }; + + void Message_OUT(uint8_t Ohm_150, uint8_t Ohm_68, bool testLED); +/** + @Summary + Enables a selected quantity of 150 ohm resistors and 68 ohm resistors. + + @Description + In 400V mode the user can select a certain quantity of both 68 and 150 ohm resistors to be enabled, + while in 550V mode the user can only access the 150ohm resistors. A test LED can also be enabled. + + @Preconditions + The MSSP1_SPI_Initialize routine must have been called for the specified + MSSP1_SPI driver instance. System must be in 400V mode for 68 ohm resistors to be accessible. + + @Param + 150Ohm - Number of 150 ohm resistors to enable (up to 15). + 68Ohm - Number of 68 ohm resistors to enable (up to 5). + testLED - True enables the test LED. + + @Returns + Void. + + @Example + Message_OUT(15, 5, false); //Max Load with testLED turned off. + +*/ + void Message_BIM(uint8_t outputs[3]); +/** + @Summary + Chooses whether an output pin will latch off in the event of an overload or restart. + + @Description +Bulb Inrush Mode bits (BIM.OUTn) +0B (default) Output latches off with overload +1B Output restarts with overload +DATA = Channel number 7 to 0 (7:0) + + @Preconditions + The MSSP1_SPI_Initialize routine must have been called for the specified + MSSP1_SPI driver instance. + + @Param + outputs[] - An array containing the data to be written to each of the three registers. + + @Returns + Void. + + @Example + Message_BIM({0xFF, 0x11, 0x00}); //All outputs of the first chip will restart with overload, two of the second chip's outputs will restart on overload, + and all of the remaining pins will latch off with overload. + +*/ + void Message_MAPIN0(uint8_t outputs[3]); +/** + @Summary + Chooses which output pins are controlled by IN0. + + @Description +Input Mapping (IN0) bits (MAPIN0.OUTn) +0B (default) No connection to input pin +1B Output is connected to the input pin +DATA = Channel number 7 to 0 (7:0) +Note ? Channel 2 has the corresponding bit set to ?1? by default + + @Preconditions + The MSSP1_SPI_Initialize routine must have been called for the specified + MSSP1_SPI driver instance. + + @Param + outputs[] - An array containing the data to be written to each of the three registers. + + @Returns + Void. + + @Example + Message_MAPIN0({0x00, 0x00, 0x00}); //All outputs of each of the three driver ICs are not connected to IN0. + +*/ + + void Message_MAPIN1(uint8_t outputs[3]); +/** + @Summary + Chooses which output pins are controlled by IN0. + + @Description +Input Mapping (IN1) bits (MAPIN1.OUTn) +0B (default) No connection to input pin +1B Output is connected to the input pin +DATA = Channel number 7 to 0 (7:0) +Note ? Channel 3 has the corresponding bit set to ?1? by default + + @Preconditions + The MSSP1_SPI_Initialize routine must have been called for the specified + MSSP1_SPI driver instance. + + @Param + outputs[] - An array containing the data to be written to each of the three registers. + + @Returns + Void. + + @Example + Message_MAPIN1({0x00, 0x00, 0x00}); //All outputs of each of the three driver ICs are not connected to IN1. + +*/ + void Message_INST(void); +/** + @Summary + Retrieves data regarding the input pins and TER bit. + + @Description +Input Status Monitor +TER bit bit (TER) (7) +0B Previous transmission was successful +1B (default) Previous transmission failed + * +Inx Bit bits (INST.RES) (6:2) ? reserved, bits (INST.INn) (1:0) +0B (default) The input pin is set low +1B The input pin is set high + + @Preconditions + The MSSP1_SPI_Initialize routine must have been called for the specified + MSSP1_SPI driver instance. + + @Param + Void. + + @Returns + Void. + + @Example + Message_INST(void); // :) + +*/ + void Message_DAGIOL(uint8_t outputs[3]); +/** + @Summary + Open Load Diagnostic Current Control + + @Description +bits (DIAG_IOL.OUTn) +0B (default) Diagnostic current is not enabled +1B Diagnostic current is enabled +DATA = Channel number 7 to 0 (7:0) + + @Preconditions + The MSSP1_SPI_Initialize routine must have been called for the specified + MSSP1_SPI driver instance. + + @Param + outputs[] - An array containing the channel numbers from which to write or obtain information. + + @Returns + Void. + + @Example + Message_DAGIOL({0xFF, 0xFF, 0xFF}); //Obtains information from all channels of the three connected devices. + +*/ + void Message_DAGOSM(void); +/** + @Summary + Output Status Monitor Bits (DIAG_OSM.OUTn) + + @Description +0B (default) Voutx is less than the Output Status Monitor Threshold Voltage 3.3 V (typ) +1B Voutx is more than the Output Status Monitor Threshold Voltage 3.3 V (typ) +DATA = Channel number 7 to 0 (7:0) + + @Preconditions + The MSSP1_SPI_Initialize routine must have been called for the specified + MSSP1_SPI driver instance. + + @Param + Void. + + @Returns + Void. + + @Example + Message_DAGOSM(void); + +*/ + void Message_DAGOLON(void); +/** + @Summary + Open Load On Monitor Bits (DIAG_OLON.OUTn) + + @Description +0B (default) Normal operation or diagnostic performed with channel off +1B Open load On detected +DATA = Channel number 7 to 0 (7:0) + + @Preconditions + The MSSP1_SPI_Initialize routine must have been called for the specified + MSSP1_SPI driver instance. + + @Param + Void. + + @Returns + Void. + + @Example + Message_DAGOLON(void); + +*/ + void Message_DAGOLONEN(uint8_t outputs[3]); +/** + @Summary + Open Load On Diagnostic Control + + @Description +bits (7:4) ? reserved, +bits 1000B , 1001B , 1011B , 1100B , 1101B , 1100B ? reserved +bits (DIAG_OLONEN.MUX) (3:0) +0000B Open Load ON active channel 0 +0001B Open Load ON active channel 1 +0010B Open Load ON active channel 2 +0011B Open Load ON active channel 3 +0100B Open Load ON active channel 4 +0101B Open Load ON active channel 5 +0110B Open Load ON active channel 6 +0111B Open Load ON active channel 7 +1010B Open Load ON Diagnostic Loop Start +1111B (default) Open Load ON not active + + @Preconditions + The MSSP1_SPI_Initialize routine must have been called for the specified + MSSP1_SPI driver instance. + + @Param + outputs[] - An array containing the status of the specified channel of each of the three ICs. + + @Returns + Void. + + @Example + Message_DAGOLONEN({0b00000000, 0xFF, 0xFF}); //Open Load ON active on channel 0 of the first IC. + +*/ + void Message_HWCR(uint8_t outputs[3]); +/** + @Summary + Hardware Configuration Register Bits + + @Description +Active Mode bits (HWCR.ACT) (7) +0B (default) Normal operation +or device leaves Active Mode +1B Device enters Active Mode + * +SPI Register Reset bits (HWCR.RST) (6) +0B (default) Normal operation +1B Reset command executed +ERRn bits are not cleared by a reset command for safety reasons + * +Channels Operating in Parallel bits (HWCR.PAR) (3:0) +0B (default) Normal operation +1B Two neighboring channels have overload and overtemperature +synchronized. See section ?Outputs in Parallel? for output combinations + + @Preconditions + The MSSP1_SPI_Initialize routine must have been called for the specified + MSSP1_SPI driver instance. + + @Param + outputs[] - An array containing the Active Mode, SPI Register, and "Channels Operating in Parallel" bits for each device. + + @Returns + Void. + + @Example + Message_DAGOLONEN({0b11xx0000, 0b00xx0000, 0b00xx0000}); //Device 1 enters active mode, resets SPI operation, and maintains normal latch operation. + +*/ + void Message_HWCROCL(uint8_t outputs[3]); +/** + @Summary + Output Latch (ERRn) Clear bits (HWCR_OCL.OUTn) + + @Description +0B (default) Normal operation +1B Clear the error latch for the selected output +The HWCR_OCL.OUTn bit is set back to ?0? internally after de?latching the +channel +DATA = 7 to 0 (7:0) + + @Preconditions + The MSSP1_SPI_Initialize routine must have been called for the specified + MSSP1_SPI driver instance. + + @Param + outputs[] - An array containing bits corresponding to the latch error reset of each channel. + + @Returns + Void. + + @Example + Message_DAGOLONEN({0xFF, 0xFF, 0xFF}); //Reset Latch Errors are reset. + +*/ + void Message_HWCRPWM(uint8_t outputs[3]); +/** + @Summary + Hardware Clear for all PWM registers. + + @Description +PWM Configuration Register (HWCR_PWM.RES) (3:2) (reserved) +PWM Adjustment bits (HWCR_PWM.ADJ) (7:4) +HWCR_PWM.ADJ Bit +Absolute delta for fINT Relative delta between steps +0000B (reserved) +0001B Base Frequency ?35.0% ?35.0% (66.3 kHz[typ]) +0010B Base Frequency ?30.0% +0011B Base Frequency ?25.0% +0100B Base Frequency ?20.0% +0101B Base Frequency ?15.0% +0110B Base Frequency ?10.0% +0111B Base Frequency ?5.0% +1000B Base Frequency fINT (102 kHz [typ])(default) +1001B Base Frequency +5.0% +1010B Base Frequency +10.0% +1011B Base Frequency +15.0% +1100B Base Frequency +20.0% +1101B Base Frequency +25.0% +1110B Base Frequency +30.0% +1111B Base Frequency +35.0% +35.0 (137.7 kHz[typ]) + +PWM1 Active bits (HWCR_PWM.PWM1) (1) +0B (default) PWM Generator 1 not active +1B PWM Generator 1 active +PWM0 Active (HWCR_PWM.PWM0) (0) +0B (default) PWM Generator 0 not active +1B PWM Generator 0 active + + @Preconditions + The MSSP1_SPI_Initialize routine must have been called for the specified + MSSP1_SPI driver instance. + + @Param + outputs[] - An array containing bits corresponding to the PWM Hardware. + + @Returns + Void. + + @Example + Message_HWCRPWM({0b1000xx00, 0b1000xx00, 0b1000xx00}); //Set all three ICs to typical base frequency and disable both PWM Generators. + +*/ + void Message_PWMCR0(uint16_t outputs[3]); +/** + @Summary + PWM Generator 0 Configuration + + @Description +CR0 Frequency (PWM_CR0.FREQ) (9:8) +00B Internal clock divided by 1024 (100 Hz) (default) +01B Internal clock divided by 512 (200 Hz) +10B Internal clock divided by 256 (400 Hz) +11B 100% Duty Cycle. +CR0 generator on/off control (PWM_CRO.DC) (7:0) +00000000B PWM generator is off. (default) +11111111B PWM generator is On (99.61% DC). + + @Preconditions + The MSSP1_SPI_Initialize routine must have been called for the specified + MSSP1_SPI driver instance. + + @Param + outputs[] - A 16 bit array containing bits which adjust the PWM0 frequency as well as enable/disable the generator itself. + + @Returns + Void. + + @Example + Message_PWMCR0({0xX000, 0xX000, 0xX000}); //Set the PWM frequency to be 100Hz and disable PWM0. + +*/ + void Message_PWMCR1(uint16_t outputs[3]); +/** + @Summary + PWM Generator 1 Configuration + + @Description +CR0 Frequency (PWM_CR1.FREQ) (9:8) +00B Internal clock divided by 1024 (100 Hz) (default) +01B Internal clock divided by 512 (200 Hz) +10B Internal clock divided by 256 (400 Hz) +11B 100% Duty Cycle. +CR1 generator on/off control (PWM_CR1.DC) (7:0) +00000000B PWM generator is off. (default) +11111111B PWM generator is On (99.61% DC). + + @Preconditions + The MSSP1_SPI_Initialize routine must have been called for the specified + MSSP1_SPI driver instance. + + @Param + outputs[] - A 16 bit array containing bits which adjust the PWM1 frequency as well as enable/disable the generator itself. + + @Returns + Void. + + @Example + Message_PWMCR1({0xX000, 0xX000, 0xX000}); //Set the PWM frequency to be 100Hz and disable PWM1. + +*/ + void Message_PWMOUT(uint8_t outputs[3]); +/** + @Summary + Select which outputs are connected to any PWM generator. + + @Description +PWM Generator Output Control (PWM_OUT.OUTn) +0B (default) The selected output is not driven by one of the two PWM generators +1B The selected output is connected to a PWM generator +DATA = Channel number 0 to 7 + + @Preconditions + The MSSP1_SPI_Initialize routine must have been called for the specified + MSSP1_SPI driver instance. + + @Param + outputs[] - An array containing bits which determine which, if any, outputs are connected to the PWM generators. + + @Returns + Void. + + @Example + Message_PWMOUT({0x00, 0x00, 0x00}); //All outputs are not connected to any of the PWM generators. + +*/ + void Message_PWMMAP(uint8_t outputs[3]); +/** + @Summary + Select which PWM generator corresponds to which output. + + @Description +PWM Generator Output Mapping (PWM_MAP.OUTn) +0B (default) The selected output is connected to PWM Generator 0 +1B The selected output is connected to PWM Generator 1 +DATA = Channel number 0 to 7 +Works in conjunction with PWM_OUT + + @Preconditions + The MSSP1_SPI_Initialize routine must have been called for the specified + MSSP1_SPI driver instance. + + @Param + outputs[] - An array containing bits which map the PWM generators to specific outputs. + + @Returns + Void. + + @Example + Message_PWMMAP({0x00, 0x00, 0x00}); //All outputs are connected to PWM0. + +*/ + + +#endif /* XC_SPI_H */ + diff --git a/main.c b/main.c new file mode 100644 index 0000000..3fa5877 --- /dev/null +++ b/main.c @@ -0,0 +1,70 @@ +/** + Generated main.c file from MPLAB Code Configurator + + @Company + Microchip Technology Inc. + + @File Name + main.c + + @Summary + This is the generated main.c using PIC24 / dsPIC33 / PIC32MM MCUs. + + @Description + This source file provides main entry point for system initialization and application code development. + Generation Information : + Product Revision : PIC24 / dsPIC33 / PIC32MM MCUs - 1.171.4 + Device : PIC24FV16KM202 + The generated drivers are tested against the following: + Compiler : XC16 v2.10 + MPLAB : MPLAB X v6.05 +*/ + +/* + (c) 2020 Microchip Technology Inc. and its subsidiaries. You may use this + software and any derivatives exclusively with Microchip products. + + THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER + EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED + WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A + PARTICULAR PURPOSE, OR ITS INTERACTION WITH MICROCHIP PRODUCTS, COMBINATION + WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION. + + IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, + INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND + WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS + BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE + FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN + ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, + THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. + + MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE + TERMS. +*/ + +/** + Section: Included Files +*/ +#include "mcc_generated_files/system.h" +#include "SPI.h" + +/* + Main application + */ +int main(void) +{ + // initialize the device + SYSTEM_Initialize(); + + while (1) + { + // Add your application code + total = 0; + } + + return 1; +} +/** + End of File +*/ + -- cgit v1.2.3