/* ================================================================== >>>>>>>>>>>>>>>>>>>>>>> COPYRIGHT NOTICE <<<<<<<<<<<<<<<<<<<<<<<<< ------------------------------------------------------------------ Copyright (c) 2019-2020 by Lattice Semiconductor Corporation ALL RIGHTS RESERVED ------------------------------------------------------------------ IMPORTANT: THIS FILE IS USED BY OR GENERATED BY the LATTICE PROPELâ„¢ DEVELOPMENT SUITE, WHICH INCLUDES PROPEL BUILDER AND PROPEL SDK. Lattice grants permission to use this code pursuant to the terms of the Lattice Propel License Agreement. DISCLAIMER: LATTICE MAKES NO WARRANTIES ON THIS FILE OR ITS CONTENTS, WHETHER EXPRESSED, IMPLIED, STATUTORY, OR IN ANY PROVISION OF THE LATTICE PROPEL LICENSE AGREEMENT OR COMMUNICATION WITH LICENSEE, AND LATTICE SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. LATTICE DOES NOT WARRANT THAT THE FUNCTIONS CONTAINED HEREIN WILL MEET LICENSEE 'S REQUIREMENTS, OR THAT LICENSEE' S OPERATION OF ANY DEVICE, SOFTWARE OR SYSTEM USING THIS FILE OR ITS CONTENTS WILL BE UNINTERRUPTED OR ERROR FREE, OR THAT DEFECTS HEREIN WILL BE CORRECTED. LICENSEE ASSUMES RESPONSIBILITY FOR SELECTION OF MATERIALS TO ACHIEVE ITS INTENDED RESULTS, AND FOR THE PROPER INSTALLATION, USE, AND RESULTS OBTAINED THEREFROM. LICENSEE ASSUMES THE ENTIRE RISK OF THE FILE AND ITS CONTENTS PROVING DEFECTIVE OR FAILING TO PERFORM PROPERLY AND IN SUCH EVENT, LICENSEE SHALL ASSUME THE ENTIRE COST AND RISK OF ANY REPAIR, SERVICE, CORRECTION, OR ANY OTHER LIABILITIES OR DAMAGES CAUSED BY OR ASSOCIATED WITH THE SOFTWARE.IN NO EVENT SHALL LATTICE BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS FILE OR ITS CONTENTS, EVEN IF LATTICE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. LATTICE 'S SOLE LIABILITY, AND LICENSEE' S SOLE REMEDY, IS SET FORTH ABOVE. LATTICE DOES NOT WARRANT OR REPRESENT THAT THIS FILE, ITS CONTENTS OR USE THEREOF DOES NOT INFRINGE ON THIRD PARTIES' INTELLECTUAL PROPERTY RIGHTS, INCLUDING ANY PATENT. IT IS THE USER' S RESPONSIBILITY TO VERIFY THE USER SOFTWARE DESIGN FOR CONSISTENCY AND FUNCTIONALITY THROUGH THE USE OF FORMAL SOFTWARE VALIDATION METHODS. ------------------------------------------------------------------ ================================================================== */ #include "cstm_pld.h" #include "hal.h" unsigned char cstm_pld_init(struct cstm_pld_instance *this_cstm_pld, unsigned int base_addr) { volatile struct cstm_pld_dev *dev; if (NULL == this_cstm_pld) { return 1; } this_cstm_pld->base_addr = base_addr; dev = (struct cstm_pld_dev *) this_cstm_pld->base_addr; this_cstm_pld->msg_in_rd_index = 0; this_cstm_pld->msg_in_wr_index = 0; this_cstm_pld->msg_out_rd_index = 0; this_cstm_pld->msg_out_wr_index = 0; this_cstm_pld->b_msg_rcvd = false; this_cstm_pld->b_msg_sent = true; // for Customer PLD has no parameters, so enable the int here. dev->int_enable = MSG_RCVD_MASK | MSG_SENT_MASK; return 0; } unsigned char cstm_pld_int_set(struct cstm_pld_instance *this_cstm_pld, unsigned int ints) { volatile struct cstm_pld_dev *dev; if (NULL == this_cstm_pld) { return 1; } dev = (struct cstm_pld_dev *) this_cstm_pld->base_addr; dev->int_set = ints; return 0; } unsigned char cstm_pld_int_status_get(struct cstm_pld_instance *this_cstm_pld, unsigned int *ints) { volatile struct cstm_pld_dev *dev; if (NULL == this_cstm_pld) { return 1; } dev = (struct cstm_pld_dev *) this_cstm_pld->base_addr; *ints = dev->int_status; return 0; } unsigned char cstm_pld_msg_receive(struct cstm_pld_instance *this_cstm_pld, unsigned char *msg) { unsigned char ret_status = 1; if (NULL == this_cstm_pld) { return 1; } #if CSTM_PLD_INT_EN // in interrupt mode, all msg is stored in buffer if (this_cstm_pld->b_msg_rcvd == true) { *msg = this_cstm_pld->msg_rcvd[this_cstm_pld-> msg_in_rd_index++]; this_cstm_pld->b_msg_rcvd = false; ret_status = 0; } if (this_cstm_pld->msg_in_rd_index >= MSG_BUFFER_SIZE) { this_cstm_pld->msg_in_rd_index = 0; } // cstm_pld_int_set(this_cstm_pld, MSG_SENT_MASK); #else volatile struct cstm_pld_dev *dev; dev = (struct cstm_pld_dev *) this_cstm_pld->base_addr; if (dev->int_status & MSG_RCVD_MASK) { // read back the message and clear the interrupt *msg = (dev->pld2fw_msg & 0xff); dev->int_status = MSG_RCVD_MASK; ret_status = 0; } #endif return ret_status; } unsigned char cstm_pld_msg_send(struct cstm_pld_instance *this_cstm_pld, unsigned char *msg) { volatile struct cstm_pld_dev *dev; dev = (struct cstm_pld_dev *) this_cstm_pld->base_addr; unsigned int fw2pld_msg = 0; if (NULL == this_cstm_pld) { return 1; } #if CSTM_PLD_INT_EN if (this_cstm_pld->b_msg_sent == true) // ready to sent msg, sent at once { dev->fw2pld_msg = (*msg | MSG_VALID_MASK); this_cstm_pld->b_msg_sent = false; // set the flag false } else { this_cstm_pld->msg_sent[this_cstm_pld-> msg_out_wr_index++] = *msg; if (this_cstm_pld->msg_out_wr_index >= MSG_BUFFER_SIZE) { this_cstm_pld->msg_out_wr_index = 0; } } #else //check whether is ready for sending msg to pld dev->fw2pld_msg = (*msg | MSG_VALID_MASK); do { fw2pld_msg = dev->fw2pld_msg; } while (fw2pld_msg & MSG_VALID_MASK); #endif return 0; } void cstm_pld_isr(void *ctx) { struct cstm_pld_instance *this_cstm_pld; volatile struct cstm_pld_dev *dev; unsigned int int_status = 0; this_cstm_pld = (struct cstm_pld_instance *) ctx; dev = (struct cstm_pld_dev *) this_cstm_pld->base_addr; int_status = dev->int_status; // received message from pld if (int_status & MSG_RCVD_MASK) { this_cstm_pld->msg_rcvd[this_cstm_pld->msg_in_wr_index++] = (dev->pld2fw_msg & 0xff); if (this_cstm_pld->msg_in_wr_index >= MSG_BUFFER_SIZE) { this_cstm_pld->msg_in_wr_index = 0; } // clear the interrupt dev->int_status = MSG_RCVD_MASK; this_cstm_pld->b_msg_rcvd = true; } // pld ack the message sent if (int_status & MSG_SENT_MASK) { // clear the interrupt dev->int_status = MSG_SENT_MASK; if (this_cstm_pld->msg_out_rd_index < this_cstm_pld->msg_out_wr_index) { dev->fw2pld_msg = (this_cstm_pld-> msg_sent[this_cstm_pld-> msg_out_rd_index++] | MSG_VALID_MASK); } else { this_cstm_pld->b_msg_sent = true; } if (this_cstm_pld->msg_out_rd_index >= MSG_BUFFER_SIZE) { this_cstm_pld->msg_out_rd_index = 0; } } }