Skip to content
Snippets Groups Projects
Commit f5a28576 authored by Alexander Alashkin's avatar Alexander Alashkin Committed by Cesanta Bot
Browse files

Add PIC32/Harmony http_server example

PUBLISHED_FROM=c393396605d5418fda4ff386170bb9a6d6658a93
parent 2ef6f724
No related branches found
No related tags found
No related merge requests found
Showing
with 5984 additions and 0 deletions
#
# There exist several targets which are by default empty and which can be
# used for execution of your targets. These targets are usually executed
# before and after some main targets. They are:
#
# .build-pre: called before 'build' target
# .build-post: called after 'build' target
# .clean-pre: called before 'clean' target
# .clean-post: called after 'clean' target
# .clobber-pre: called before 'clobber' target
# .clobber-post: called after 'clobber' target
# .all-pre: called before 'all' target
# .all-post: called after 'all' target
# .help-pre: called before 'help' target
# .help-post: called after 'help' target
#
# Targets beginning with '.' are not intended to be called on their own.
#
# Main targets can be executed directly, and they are:
#
# build build a specific configuration
# clean remove built files from a configuration
# clobber remove all built files
# all build all configurations
# help print help mesage
#
# Targets .build-impl, .clean-impl, .clobber-impl, .all-impl, and
# .help-impl are implemented in nbproject/makefile-impl.mk.
#
# Available make variables:
#
# CND_BASEDIR base directory for relative paths
# CND_DISTDIR default top distribution directory (build artifacts)
# CND_BUILDDIR default top build directory (object files, ...)
# CONF name of current configuration
# CND_ARTIFACT_DIR_${CONF} directory of build artifact (current configuration)
# CND_ARTIFACT_NAME_${CONF} name of build artifact (current configuration)
# CND_ARTIFACT_PATH_${CONF} path to build artifact (current configuration)
# CND_PACKAGE_DIR_${CONF} directory of package (current configuration)
# CND_PACKAGE_NAME_${CONF} name of package (current configuration)
# CND_PACKAGE_PATH_${CONF} path to package (current configuration)
#
# NOCDDL
# Environment
MKDIR=mkdir
CP=cp
CCADMIN=CCadmin
RANLIB=ranlib
# build
build: .build-post
.build-pre:
# Add your pre 'build' code here...
.build-post: .build-impl
# Add your post 'build' code here...
# clean
clean: .clean-post
.clean-pre:
# Add your pre 'clean' code here...
# WARNING: the IDE does not call this target since it takes a long time to
# simply run make. Instead, the IDE removes the configuration directories
# under build and dist directly without calling make.
# This target is left here so people can do a clean when running a clean
# outside the IDE.
.clean-post: .clean-impl
# Add your post 'clean' code here...
# clobber
clobber: .clobber-post
.clobber-pre:
# Add your pre 'clobber' code here...
.clobber-post: .clobber-impl
# Add your post 'clobber' code here...
# all
all: .all-post
.all-pre:
# Add your pre 'all' code here...
.all-post: .all-impl
# Add your post 'all' code here...
# help
help: .help-post
.help-pre:
# Add your pre 'help' code here...
.help-post: .help-impl
# Add your post 'help' code here...
# include project implementation makefile
include nbproject/Makefile-impl.mk
# include project make variables
include nbproject/Makefile-variables.mk
#
#Wed Oct 26 16:05:10 EEST 2016
pic32mx_eth_sk2_encx24j600.languagetoolchain.dir=/opt/microchip/xc32/v1.42/bin
pic32mx_eth_sk2_encx24j600.com-microchip-mplab-nbide-toolchainXC32-XC32LanguageToolchain.md5=7787462309de955deefcc6f5508f88a9
pic32mx_eth_sk2_encx24j600.languagetoolchain.version=1.42
configurations-xml=2f70242829ecb692b68d01a3f7c4e1e7
com-microchip-mplab-nbide-embedded-makeproject-MakeProject.md5=02b44c60b7ea0aab28d65f169487b894
host.platform=linux
conf.ids=pic32mx_eth_sk2_encx24j600
#
# Generated Makefile - do not edit!
#
# Edit the Makefile in the project folder instead (../Makefile). Each target
# has a pre- and a post- target defined where you can add customization code.
#
# This makefile implements macros and targets common to all configurations.
#
# NOCDDL
# Building and Cleaning subprojects are done by default, but can be controlled with the SUB
# macro. If SUB=no, subprojects will not be built or cleaned. The following macro
# statements set BUILD_SUB-CONF and CLEAN_SUB-CONF to .build-reqprojects-conf
# and .clean-reqprojects-conf unless SUB has the value 'no'
SUB_no=NO
SUBPROJECTS=${SUB_${SUB}}
BUILD_SUBPROJECTS_=.build-subprojects
BUILD_SUBPROJECTS_NO=
BUILD_SUBPROJECTS=${BUILD_SUBPROJECTS_${SUBPROJECTS}}
CLEAN_SUBPROJECTS_=.clean-subprojects
CLEAN_SUBPROJECTS_NO=
CLEAN_SUBPROJECTS=${CLEAN_SUBPROJECTS_${SUBPROJECTS}}
# Project Name
PROJECTNAME=http_server.X
# Active Configuration
DEFAULTCONF=pic32mx_eth_sk2_encx24j600
CONF=${DEFAULTCONF}
# All Configurations
ALLCONFS=pic32mx_eth_sk2_encx24j600
# build
.build-impl: .build-pre
${MAKE} -f nbproject/Makefile-${CONF}.mk SUBPROJECTS=${SUBPROJECTS} .build-conf
# clean
.clean-impl: .clean-pre
${MAKE} -f nbproject/Makefile-${CONF}.mk SUBPROJECTS=${SUBPROJECTS} .clean-conf
# clobber
.clobber-impl: .clobber-pre .depcheck-impl
${MAKE} SUBPROJECTS=${SUBPROJECTS} CONF=pic32mx_eth_sk2_encx24j600 clean
# all
.all-impl: .all-pre .depcheck-impl
${MAKE} SUBPROJECTS=${SUBPROJECTS} CONF=pic32mx_eth_sk2_encx24j600 build
# dependency checking support
.depcheck-impl:
# @echo "# This code depends on make tool being used" >.dep.inc
# @if [ -n "${MAKE_VERSION}" ]; then \
# echo "DEPFILES=\$$(wildcard \$$(addsuffix .d, \$${OBJECTFILES}))" >>.dep.inc; \
# echo "ifneq (\$${DEPFILES},)" >>.dep.inc; \
# echo "include \$${DEPFILES}" >>.dep.inc; \
# echo "endif" >>.dep.inc; \
# else \
# echo ".KEEP_STATE:" >>.dep.inc; \
# echo ".KEEP_STATE_FILE:.make.state.\$${CONF}" >>.dep.inc; \
# fi
#
# Generated Makefile - do not edit!
#
#
# This file contains information about the location of compilers and other tools.
# If you commmit this file into your revision control server, you will be able to
# to checkout the project and build it from the command line with make. However,
# if more than one person works on the same project, then this file might show
# conflicts since different users are bound to have compilers in different places.
# In that case you might choose to not commit this file and let MPLAB X recreate this file
# for each user. The disadvantage of not commiting this file is that you must run MPLAB X at
# least once so the file gets created and the project can be built. Finally, you can also
# avoid using this file at all if you are only building from the command line with make.
# You can invoke make with the values of the macros:
# $ makeMP_CC="/opt/microchip/mplabc30/v3.30c/bin/pic30-gcc" ...
#
PATH_TO_IDE_BIN=/opt/microchip/mplabx/v3.40/mplab_ide/platform/../mplab_ide/modules/../../bin/
# Adding MPLAB X bin directory to path.
PATH:=/opt/microchip/mplabx/v3.40/mplab_ide/platform/../mplab_ide/modules/../../bin/:$(PATH)
# Path to java used to run MPLAB X when this makefile was created
MP_JAVA_PATH="/opt/microchip/mplabx/v3.40/sys/java/jre1.8.0_91/bin/"
OS_CURRENT="$(shell uname -s)"
MP_CC="/opt/microchip/xc32/v1.42/bin/xc32-gcc"
MP_CPPC="/opt/microchip/xc32/v1.42/bin/xc32-g++"
# MP_BC is not defined
MP_AS="/opt/microchip/xc32/v1.42/bin/xc32-as"
MP_LD="/opt/microchip/xc32/v1.42/bin/xc32-ld"
MP_AR="/opt/microchip/xc32/v1.42/bin/xc32-ar"
DEP_GEN=${MP_JAVA_PATH}java -jar "/opt/microchip/mplabx/v3.40/mplab_ide/platform/../mplab_ide/modules/../../bin/extractobjectdependencies.jar"
MP_CC_DIR="/opt/microchip/xc32/v1.42/bin"
MP_CPPC_DIR="/opt/microchip/xc32/v1.42/bin"
# MP_BC_DIR is not defined
MP_AS_DIR="/opt/microchip/xc32/v1.42/bin"
MP_LD_DIR="/opt/microchip/xc32/v1.42/bin"
MP_AR_DIR="/opt/microchip/xc32/v1.42/bin"
# MP_BC_DIR is not defined
Source diff could not be displayed: it is too large. Options to address this: view the blob.
#
# Generated - do not edit!
#
# NOCDDL
#
CND_BASEDIR=`pwd`
# pic32mx_eth_sk2_encx24j600 configuration
CND_ARTIFACT_DIR_pic32mx_eth_sk2_encx24j600=dist/pic32mx_eth_sk2_encx24j600/production
CND_ARTIFACT_NAME_pic32mx_eth_sk2_encx24j600=http_server.X.production.hex
CND_ARTIFACT_PATH_pic32mx_eth_sk2_encx24j600=dist/pic32mx_eth_sk2_encx24j600/production/http_server.X.production.hex
CND_PACKAGE_DIR_pic32mx_eth_sk2_encx24j600=${CND_DISTDIR}/pic32mx_eth_sk2_encx24j600/package
CND_PACKAGE_NAME_pic32mx_eth_sk2_encx24j600=httpserver.x.tar
CND_PACKAGE_PATH_pic32mx_eth_sk2_encx24j600=${CND_DISTDIR}/pic32mx_eth_sk2_encx24j600/package/httpserver.x.tar
#!/bin/bash -x
#
# Generated - do not edit!
#
# Macros
TOP=`pwd`
CND_CONF=pic32mx_eth_sk2_encx24j600
CND_DISTDIR=dist
TMPDIR=build/${CND_CONF}/${IMAGE_TYPE}/tmp-packaging
TMPDIRNAME=tmp-packaging
OUTPUT_PATH=dist/${CND_CONF}/${IMAGE_TYPE}/http_server.X.${IMAGE_TYPE}.${OUTPUT_SUFFIX}
OUTPUT_BASENAME=http_server.X.${IMAGE_TYPE}.${OUTPUT_SUFFIX}
PACKAGE_TOP_DIR=httpserver.x/
# Functions
function checkReturnCode
{
rc=$?
if [ $rc != 0 ]
then
exit $rc
fi
}
function makeDirectory
# $1 directory path
# $2 permission (optional)
{
mkdir -p "$1"
checkReturnCode
if [ "$2" != "" ]
then
chmod $2 "$1"
checkReturnCode
fi
}
function copyFileToTmpDir
# $1 from-file path
# $2 to-file path
# $3 permission
{
cp "$1" "$2"
checkReturnCode
if [ "$3" != "" ]
then
chmod $3 "$2"
checkReturnCode
fi
}
# Setup
cd "${TOP}"
mkdir -p ${CND_DISTDIR}/${CND_CONF}/package
rm -rf ${TMPDIR}
mkdir -p ${TMPDIR}
# Copy files and create directories and links
cd "${TOP}"
makeDirectory ${TMPDIR}/httpserver.x/bin
copyFileToTmpDir "${OUTPUT_PATH}" "${TMPDIR}/${PACKAGE_TOP_DIR}bin/${OUTPUT_BASENAME}" 0755
# Generate tar file
cd "${TOP}"
rm -f ${CND_DISTDIR}/${CND_CONF}/package/httpserver.x.tar
cd ${TMPDIR}
tar -vcf ../../../../${CND_DISTDIR}/${CND_CONF}/package/httpserver.x.tar *
checkReturnCode
# Cleanup
cd "${TOP}"
rm -rf ${TMPDIR}
<?xml version="1.0" encoding="UTF-8"?>
<configurationDescriptor version="62">
<projectmakefile>Makefile</projectmakefile>
<defaultConf>0</defaultConf>
<confs>
<conf name="pic32mx_eth_sk2_encx24j600" type="2">
<platformToolSN>:=MPLABComm-USB-Microchip:=&lt;vid>04D8:=&lt;pid>8107:=&lt;rev>0002:=&lt;man>Microchip Technology Incorporated:=&lt;prod>Explorer 16/32 PICkit on Board:=&lt;sn>BUR162971352:=&lt;drv>x:=&lt;xpt>h:=end</platformToolSN>
<languageToolchainDir>/opt/microchip/xc32/v1.42/bin</languageToolchainDir>
<mdbdebugger version="1">
<placeholder1>place holder 1</placeholder1>
<placeholder2>place holder 2</placeholder2>
</mdbdebugger>
<runprofile version="6">
<args></args>
<rundir></rundir>
<buildfirst>true</buildfirst>
<console-type>0</console-type>
<terminal-type>0</terminal-type>
<remove-instrumentation>0</remove-instrumentation>
<environment>
</environment>
</runprofile>
</conf>
</confs>
</configurationDescriptor>
<?xml version="1.0" encoding="UTF-8"?>
<project-private xmlns="http://www.netbeans.org/ns/project-private/1">
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group>
<file>file:/home/alex/go/src/cesanta.com/mongoose/examples/PIC32/http_server/firmware/src/app.c</file>
<file>file:/home/alex/go/src/cesanta.com/mongoose/examples/PIC32/http_server/firmware/src/app.h</file>
</group>
</open-files>
</project-private>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>com.microchip.mplab.nbide.embedded.makeproject</type>
<configuration>
<Harmony xmlns="Harmony" MHC="true"/>
<data xmlns="http://www.netbeans.org/ns/make-project/1">
<name>http_server</name>
<creation-uuid>e54023e9-3948-44ef-bf6f-29d8b223e555</creation-uuid>
<make-project-type>0</make-project-type>
<c-extensions>c</c-extensions>
<cpp-extensions/>
<header-extensions>h</header-extensions>
<asminc-extensions/>
<sourceEncoding>ISO-8859-1</sourceEncoding>
<make-dep-projects/>
</data>
</configuration>
</project>
#
# Application configuration file generated by
# Microchip Harmony Configurator (MHC) from Harmony version: 1.04
#
mainmenu "MPLAB Harmony & Application Configuration"
menu "Application Configuration"
config APP_INSTANCES
int "Number of Applications"
default 1
range 1 10 # Must limit to 10 or application templates will need to be updated.
config APP_AUTO_GENERATE_CODE # Used by MHC internally to selectively auto-generate app.h, app.c, and main.c
bool
default n
source "$HARMONY_VERSION_PATH/utilities/mhc/config/gencode/framework/global_event/config/global_event_gencode.hconfig"
source "$HARMONY_VERSION_PATH/utilities/mhc/config/app_name_idx_gencode.ftl" 10 instances
menu "Exception Handling"
config USE_EXCEPTION_HANDLER
bool "Use MPLAB Harmony Exception Handler Template?"
default y
---help---
Enter 'Y' if you want to include an exception handler template
(system_exceptions.c) in this system configuration. Enter 'N' if
you do not.
---endhelp---
config EXCEPTION_USE_SYS_DEBUG
bool "Use debug services in exception handler?"
depends on USE_EXCEPTION_HANDLER
default y
---help---
Enter 'Y' if you want the exception handler to use the SYS_DEBUG service to
report exception information. Enter 'N' if you do not.
---endhelp---
config EXCEPTION_BREAKPOINT
bool "Hit breakpoint when an exception occurs?"
depends on USE_EXCEPTION_HANDLER && EXCEPTION_USE_SYS_DEBUG
default y
---help---
Enter 'Y' if you want the exception handler to hit a breakpoint when
an exception occurs. Enter 'N' if you do not.
Note: Only works if debugging is enabled.
---endhelp---
endmenu # Exception Handling
#insert application specific configuration here
endmenu # Application Configuration
source "$HARMONY_VERSION_PATH/config/harmony.hconfig"
/* clang-format off */
/*******************************************************************************
MPLAB Harmony Application Source File
Company:
Microchip Technology Inc.
File Name:
app.c
Summary:
This file contains the source code for the MPLAB Harmony application.
Description:
This file contains the source code for the MPLAB Harmony application. It
implements the logic of the application's state machine and it may call
API routines of other MPLAB Harmony modules in the system, such as drivers,
system services, and middleware. However, it does not call any of the
system interfaces (such as the "Initialize" and "Tasks" functions) of any of
the modules in the system or make any assumptions about when those functions
are called. That is the responsibility of the configuration-specific system
files.
*******************************************************************************/
// DOM-IGNORE-BEGIN
/*******************************************************************************
Copyright (c) 2013-2014 released Microchip Technology Inc. All rights reserved.
Microchip licenses to you the right to use, modify, copy and distribute
Software only when embedded on a Microchip microcontroller or digital signal
controller that is integrated into your product or third party product
(pursuant to the sublicense terms in the accompanying license agreement).
You should refer to the license agreement accompanying this Software for
additional information regarding your rights and obligations.
SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
*******************************************************************************/
// DOM-IGNORE-END
// *****************************************************************************
// *****************************************************************************
// Section: Included Files
// *****************************************************************************
// *****************************************************************************
#include "app.h"
#include "./../../../../../mongoose.h"
// *****************************************************************************
// *****************************************************************************
// Section: Global Data Definitions
// *****************************************************************************
// *****************************************************************************
// *****************************************************************************
/* Application Data
Summary:
Holds application data
Description:
This structure holds the application's data.
Remarks:
This structure should be initialized by the APP_Initialize function.
Application strings and buffers are be defined outside this structure.
*/
APP_DATA appData;
struct mg_mgr mgr;
// *****************************************************************************
// *****************************************************************************
// Section: Application Callback Functions
// *****************************************************************************
// *****************************************************************************
/* TODO: Add any necessary callback funtions.
*/
// *****************************************************************************
// *****************************************************************************
// Section: Application Local Functions
// *****************************************************************************
// *****************************************************************************
// *****************************************************************************
// *****************************************************************************
// Section: Application Initialization and State Machine Functions
// *****************************************************************************
// *****************************************************************************
/*******************************************************************************
Function:
void APP_Initialize ( void )
Remarks:
See prototype in app.h.
*/
const char *s_http_port = "8000";
void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
switch (ev) {
case MG_EV_ACCEPT: {
char addr[32];
mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
SYS_PRINT("%p: Connection from %s\r\n", nc, addr);
break;
}
case MG_EV_HTTP_REQUEST: {
struct http_message *hm = (struct http_message *) ev_data;
char addr[32];
mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
SYS_PRINT("%p: %.*s %.*s\r\n", nc, (int) hm->method.len, hm->method.p,
(int) hm->uri.len, hm->uri.p);
mg_send_response_line(nc, 200,
"Content-Type: text/html\r\n"
"Connection: close");
mg_printf(nc,
"\r\n<h1>Hello, %s!</h1>\r\n"
"You asked for %.*s\r\n",
addr, (int) hm->uri.len, hm->uri.p);
nc->flags |= MG_F_SEND_AND_CLOSE;
break;
}
case MG_EV_CLOSE: {
SYS_PRINT("%p: Connection closed\r\n", nc);
break;
}
}
}
void APP_Initialize ( void )
{
/* Place the App state machine in its initial state. */
appData.state = APP_STATE_INIT;
mg_mgr_init(&mgr, NULL);
}
/******************************************************************************
Function:
void APP_Tasks ( void )
Remarks:
See prototype in app.h.
*/
void APP_Tasks ( void )
{
SYS_STATUS tcpipStat;
const char *netName, *netBiosName;
static IPV4_ADDR dwLastIP[2] = { {-1}, {-1} };
IPV4_ADDR ipAddr;
TCPIP_NET_HANDLE netH;
int i, nNets;
/* Check the application's current state. */
switch ( appData.state )
{
/* Application's initial state. */
case APP_STATE_INIT:
{
tcpipStat = TCPIP_STACK_Status(sysObj.tcpip);
if(tcpipStat < 0)
{ // some error occurred
SYS_CONSOLE_MESSAGE(" APP: TCP/IP stack initialization failed!\r\n");
appData.state = APP_DONE;
}
else if(tcpipStat == SYS_STATUS_READY)
{
// now that the stack is ready we can check the
// available interfaces
nNets = TCPIP_STACK_NumberOfNetworksGet();
for(i = 0; i < nNets; i++)
{
netH = TCPIP_STACK_IndexToNet(i);
netName = TCPIP_STACK_NetNameGet(netH);
netBiosName = TCPIP_STACK_NetBIOSName(netH);
#if defined(TCPIP_STACK_USE_NBNS)
SYS_CONSOLE_PRINT(" Interface %s on host %s - NBNS enabled\r\n", netName, netBiosName);
#else
SYS_CONSOLE_PRINT(" Interface %s on host %s - NBNS disabled\r\n", netName, netBiosName);
#endif // defined(TCPIP_STACK_USE_NBNS)
}
appData.state = APP_TCPIP_WAIT_FOR_IP;
}
break;
}
case APP_TCPIP_WAIT_FOR_IP:
// if the IP address of an interface has changed
// display the new value on the system console
nNets = TCPIP_STACK_NumberOfNetworksGet();
for (i = 0; i < nNets; i++)
{
netH = TCPIP_STACK_IndexToNet(i);
ipAddr.Val = TCPIP_STACK_NetAddress(netH);
if(dwLastIP[i].Val != ipAddr.Val)
{
dwLastIP[i].Val = ipAddr.Val;
SYS_CONSOLE_MESSAGE(TCPIP_STACK_NetNameGet(netH));
SYS_CONSOLE_MESSAGE(" IP Address: ");
SYS_CONSOLE_PRINT("%d.%d.%d.%d \r\n", ipAddr.v[0], ipAddr.v[1], ipAddr.v[2], ipAddr.v[3]);
if (ipAddr.v[0] != 0 && ipAddr.v[0] != 169) // Wait for a Valid IP
{
appData.state = APP_START_LISTENING;
}
}
}
break;
case APP_START_LISTENING:
{
SYS_CONSOLE_PRINT("Starting listening on port 8000\r\n");
struct mg_connection *nc = mg_bind(&mgr, "8000", ev_handler); // Create listening connection and add it to the event manager
if (nc == NULL) {
SYS_CONSOLE_PRINT("Failed to create listener\n\r");
appData.state = APP_DONE;
break;
}
mg_set_protocol_http_websocket(nc);
SYS_CONSOLE_PRINT("Listener started\r\n");
appData.state = APP_POLL;
break;
}
case APP_POLL:
{
mg_mgr_poll(&mgr, 1000);
break;
}
case APP_DONE:
{
SYS_CONSOLE_PRINT("Server stopped\n\r");
appData.state = APP_EMPTY;
break;
}
case APP_EMPTY:
{
break;
}
/* The default state should never be executed. */
default:
{
/* TODO: Handle error in application's state machine. */
break;
}
}
}
/*******************************************************************************
End of File
*/
/* clang-format off */
/*******************************************************************************
MPLAB Harmony Application Header File
Company:
Microchip Technology Inc.
File Name:
app.h
Summary:
This header file provides prototypes and definitions for the application.
Description:
This header file provides function prototypes and data type definitions for
the application. Some of these are required by the system (such as the
"APP_Initialize" and "APP_Tasks" prototypes) and some of them are only used
internally by the application (such as the "APP_STATES" definition). Both
are defined here for convenience.
*******************************************************************************/
//DOM-IGNORE-BEGIN
/*******************************************************************************
Copyright (c) 2013-2014 released Microchip Technology Inc. All rights reserved.
Microchip licenses to you the right to use, modify, copy and distribute
Software only when embedded on a Microchip microcontroller or digital signal
controller that is integrated into your product or third party product
(pursuant to the sublicense terms in the accompanying license agreement).
You should refer to the license agreement accompanying this Software for
additional information regarding your rights and obligations.
SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
*******************************************************************************/
//DOM-IGNORE-END
#ifndef _APP_H
#define _APP_H
// *****************************************************************************
// *****************************************************************************
// Section: Included Files
// *****************************************************************************
// *****************************************************************************
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include "system_config.h"
#include "system_definitions.h"
// *****************************************************************************
// *****************************************************************************
// Section: Type Definitions
// *****************************************************************************
// *****************************************************************************
// *****************************************************************************
/* Application States
Summary:
Application states enumeration
Description:
This enumeration defines the valid application states. These states
determine the behavior of the application at various times.
*/
typedef enum
{
/* Application's state machine's initial state. */
APP_STATE_INIT=0,
APP_TCPIP_WAIT_FOR_IP,
APP_START_LISTENING,
APP_DONE,
APP_EMPTY,
APP_POLL
} APP_STATES;
// *****************************************************************************
/* Application Data
Summary:
Holds application data
Description:
This structure holds the application's data.
Remarks:
Application strings and buffers are be defined outside this structure.
*/
typedef struct
{
/* The application's current state */
APP_STATES state;
/* TODO: Define any additional data used by the application. */
TCP_SOCKET socket;
char * host;
char * path;
uint16_t port;
} APP_DATA;
// *****************************************************************************
// *****************************************************************************
// Section: Application Callback Routines
// *****************************************************************************
// *****************************************************************************
/* These routines are called by drivers when certain events occur.
*/
// *****************************************************************************
// *****************************************************************************
// Section: Application Initialization and State Machine Functions
// *****************************************************************************
// *****************************************************************************
/*******************************************************************************
Function:
void APP_Initialize ( void )
Summary:
MPLAB Harmony application initialization routine.
Description:
This function initializes the Harmony application. It places the
application in its initial state and prepares it to run so that its
APP_Tasks function can be called.
Precondition:
All other system initialization routines should be called before calling
this routine (in "SYS_Initialize").
Parameters:
None.
Returns:
None.
Example:
<code>
APP_Initialize();
</code>
Remarks:
This routine must be called from the SYS_Initialize function.
*/
void APP_Initialize ( void );
/*******************************************************************************
Function:
void APP_Tasks ( void )
Summary:
MPLAB Harmony Demo application tasks function
Description:
This routine is the Harmony Demo application's tasks function. It
defines the application's state machine and core logic.
Precondition:
The system and application initialization ("SYS_Initialize") should be
called before calling this.
Parameters:
None.
Returns:
None.
Example:
<code>
APP_Tasks();
</code>
Remarks:
This routine must be called from SYS_Tasks() routine.
*/
void APP_Tasks ( void );
#endif /* _APP_H */
/*******************************************************************************
End of File
*/
/* clang-format off */
/*******************************************************************************
MPLAB Harmony Project Main Source File
Company:
Microchip Technology Inc.
File Name:
main.c
Summary:
This file contains the "main" function for an MPLAB Harmony project.
Description:
This file contains the "main" function for an MPLAB Harmony project. The
"main" function calls the "SYS_Initialize" function to initialize the state
machines of all MPLAB Harmony modules in the system and it calls the
"SYS_Tasks" function from within a system-wide "super" loop to maintain
their correct operation. These two functions are implemented in
configuration-specific files (usually "system_init.c" and "system_tasks.c")
in a configuration-specific folder under the "src/system_config" folder
within this project's top-level folder. An MPLAB Harmony project may have
more than one configuration, each contained within it's own folder under
the "system_config" folder.
*******************************************************************************/
// DOM-IGNORE-BEGIN
/*******************************************************************************
Copyright (c) 2013-2014 released Microchip Technology Inc. All rights reserved.
//Microchip licenses to you the right to use, modify, copy and distribute
Software only when embedded on a Microchip microcontroller or digital signal
controller that is integrated into your product or third party product
(pursuant to the sublicense terms in the accompanying license agreement).
You should refer to the license agreement accompanying this Software for
additional information regarding your rights and obligations.
SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
*******************************************************************************/
// DOM-IGNORE-END
// *****************************************************************************
// *****************************************************************************
// Section: Included Files
// *****************************************************************************
// *****************************************************************************
#include <stddef.h> // Defines NULL
#include <stdbool.h> // Defines true
#include <stdlib.h> // Defines EXIT_FAILURE
#include "system/common/sys_module.h" // SYS function prototypes
// *****************************************************************************
// *****************************************************************************
// Section: Main Entry Point
// *****************************************************************************
// *****************************************************************************
int main ( void )
{
/* Initialize all MPLAB Harmony modules, including application(s). */
SYS_Initialize ( NULL );
while ( true )
{
/* Maintain state machines of all polled MPLAB Harmony modules. */
SYS_Tasks ( );
}
/* Execution should not come here during normal operation */
return ( EXIT_FAILURE );
}
/*******************************************************************************
End of File
*/
/* clang-format off */
/*******************************************************************************
SPI Driver Interface Implementation
Company:
Microchip Technology Inc.
File Name:
drv_spi_api.i
Summary:
SPI Driver implementation for functions that the client API uses that change
depending on what compile time options there are.
Description:
The SPI Driver provides a interface to access the SPI hardware on the PIC32
microcontroller. This file implements the SPI Driver. This file
should be included in the project if SPI driver functionality is needed.
*******************************************************************************/
//DOM-IGNORE-BEGIN
/*******************************************************************************
Copyright (c) 2014 released Microchip Technology Inc. All rights reserved.
Microchip licenses to you the right to use, modify, copy and distribute
Software only when embedded on a Microchip microcontroller or digital signal
controller that is integrated into your product or third party product
(pursuant to the sublicense terms in the accompanying license agreement).
You should refer to the license agreement accompanying this Software for
additional information regarding your rights and obligations.
SOFTWARE AND DOCUMENTATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
*******************************************************************************/
//DOM-IGNORE-END
#include "driver/spi/src/dynamic/drv_spi_internal.h"
#define _SPI_DRV_VTABLE_POLLED 0x0000
#define _SPI_DRV_VTABLE_ISR 0x0001
#define _SPI_DRV_VTABLE_SLAVE 0x0000
#define _SPI_DRV_VTABLE_MASTER 0x0002
#define _SPI_DRV_VTABLE_RM 0x0000
#define _SPI_DRV_VTABLE_EBM 0x0004
#define _SPI_DRV_VTABLE_8BIT 0x0008
#define _SPI_DRV_VTABLE_16BIT 0x0010
#define _SPI_DRV_VTABLE_32BIT 0x0020
#define _SPI_DRV_VTABLE_P_S_R_8 _SPI_DRV_VTABLE_POLLED | _SPI_DRV_VTABLE_SLAVE | _SPI_DRV_VTABLE_RM | _SPI_DRV_VTABLE_8BIT
#define _SPI_DRV_VTABLE_P_S_R_16 _SPI_DRV_VTABLE_POLLED | _SPI_DRV_VTABLE_SLAVE | _SPI_DRV_VTABLE_RM | _SPI_DRV_VTABLE_16BIT
#define _SPI_DRV_VTABLE_P_S_R_32 _SPI_DRV_VTABLE_POLLED | _SPI_DRV_VTABLE_SLAVE | _SPI_DRV_VTABLE_RM | _SPI_DRV_VTABLE_32BIT
#define _SPI_DRV_VTABLE_P_S_E_8 _SPI_DRV_VTABLE_POLLED | _SPI_DRV_VTABLE_SLAVE | _SPI_DRV_VTABLE_EBM | _SPI_DRV_VTABLE_8BIT
#define _SPI_DRV_VTABLE_P_S_E_16 _SPI_DRV_VTABLE_POLLED | _SPI_DRV_VTABLE_SLAVE | _SPI_DRV_VTABLE_EBM | _SPI_DRV_VTABLE_16BIT
#define _SPI_DRV_VTABLE_P_S_E_32 _SPI_DRV_VTABLE_POLLED | _SPI_DRV_VTABLE_SLAVE | _SPI_DRV_VTABLE_EBM | _SPI_DRV_VTABLE_32BIT
#define _SPI_DRV_VTABLE_I_S_R_8 _SPI_DRV_VTABLE_ISR | _SPI_DRV_VTABLE_SLAVE | _SPI_DRV_VTABLE_RM | _SPI_DRV_VTABLE_8BIT
#define _SPI_DRV_VTABLE_I_S_R_16 _SPI_DRV_VTABLE_ISR | _SPI_DRV_VTABLE_SLAVE | _SPI_DRV_VTABLE_RM | _SPI_DRV_VTABLE_16BIT
#define _SPI_DRV_VTABLE_I_S_R_32 _SPI_DRV_VTABLE_ISR | _SPI_DRV_VTABLE_SLAVE | _SPI_DRV_VTABLE_RM | _SPI_DRV_VTABLE_32BIT
#define _SPI_DRV_VTABLE_I_S_E_8 _SPI_DRV_VTABLE_ISR | _SPI_DRV_VTABLE_SLAVE | _SPI_DRV_VTABLE_EBM | _SPI_DRV_VTABLE_8BIT
#define _SPI_DRV_VTABLE_I_S_E_16 _SPI_DRV_VTABLE_ISR | _SPI_DRV_VTABLE_SLAVE | _SPI_DRV_VTABLE_EBM | _SPI_DRV_VTABLE_16BIT
#define _SPI_DRV_VTABLE_I_S_E_32 _SPI_DRV_VTABLE_ISR | _SPI_DRV_VTABLE_SLAVE | _SPI_DRV_VTABLE_EBM | _SPI_DRV_VTABLE_32BIT
#define _SPI_DRV_VTABLE_P_M_R_8 _SPI_DRV_VTABLE_POLLED | _SPI_DRV_VTABLE_MASTER | _SPI_DRV_VTABLE_RM | _SPI_DRV_VTABLE_8BIT
#define _SPI_DRV_VTABLE_P_M_R_16 _SPI_DRV_VTABLE_POLLED | _SPI_DRV_VTABLE_MASTER | _SPI_DRV_VTABLE_RM | _SPI_DRV_VTABLE_16BIT
#define _SPI_DRV_VTABLE_P_M_R_32 _SPI_DRV_VTABLE_POLLED | _SPI_DRV_VTABLE_MASTER | _SPI_DRV_VTABLE_RM | _SPI_DRV_VTABLE_32BIT
#define _SPI_DRV_VTABLE_P_M_E_8 _SPI_DRV_VTABLE_POLLED | _SPI_DRV_VTABLE_MASTER | _SPI_DRV_VTABLE_EBM | _SPI_DRV_VTABLE_8BIT
#define _SPI_DRV_VTABLE_P_M_E_16 _SPI_DRV_VTABLE_POLLED | _SPI_DRV_VTABLE_MASTER | _SPI_DRV_VTABLE_EBM | _SPI_DRV_VTABLE_16BIT
#define _SPI_DRV_VTABLE_P_M_E_32 _SPI_DRV_VTABLE_POLLED | _SPI_DRV_VTABLE_MASTER | _SPI_DRV_VTABLE_EBM | _SPI_DRV_VTABLE_32BIT
#define _SPI_DRV_VTABLE_I_M_R_8 _SPI_DRV_VTABLE_ISR | _SPI_DRV_VTABLE_MASTER | _SPI_DRV_VTABLE_RM | _SPI_DRV_VTABLE_8BIT
#define _SPI_DRV_VTABLE_I_M_R_16 _SPI_DRV_VTABLE_ISR | _SPI_DRV_VTABLE_MASTER | _SPI_DRV_VTABLE_RM | _SPI_DRV_VTABLE_16BIT
#define _SPI_DRV_VTABLE_I_M_R_32 _SPI_DRV_VTABLE_ISR | _SPI_DRV_VTABLE_MASTER | _SPI_DRV_VTABLE_RM | _SPI_DRV_VTABLE_32BIT
#define _SPI_DRV_VTABLE_I_M_E_8 _SPI_DRV_VTABLE_ISR | _SPI_DRV_VTABLE_MASTER | _SPI_DRV_VTABLE_EBM | _SPI_DRV_VTABLE_8BIT
#define _SPI_DRV_VTABLE_I_M_E_16 _SPI_DRV_VTABLE_ISR | _SPI_DRV_VTABLE_MASTER | _SPI_DRV_VTABLE_EBM | _SPI_DRV_VTABLE_16BIT
#define _SPI_DRV_VTABLE_I_M_E_32 _SPI_DRV_VTABLE_ISR | _SPI_DRV_VTABLE_MASTER | _SPI_DRV_VTABLE_EBM | _SPI_DRV_VTABLE_32BIT
int32_t DRV_SPI_SetVTable(struct DRV_SPI_DRIVER_OBJECT * driverObject, const DRV_SPI_INIT * const pInit)
{
uint8_t mode = 0;
if (pInit->spiMode == DRV_SPI_MODE_MASTER)
{
mode = _SPI_DRV_VTABLE_MASTER;
}
else
{
SYS_ASSERT(false, "\r\nInvalid SPI Configuration.");
return -1;
}
if (pInit->taskMode == DRV_SPI_TASK_MODE_ISR)
{
mode |= DRV_SPI_TASK_MODE_ISR;
}
else
{
SYS_ASSERT(false, "\r\nInvalid SPI Configuration.");
return -1;
}
if (pInit->bufferType == DRV_SPI_BUFFER_TYPE_ENHANCED)
{
mode |= _SPI_DRV_VTABLE_EBM;
}
else
{
SYS_ASSERT(false, "\r\nInvalid SPI Configuration.");
return -1;
}
if (pInit->commWidth == SPI_COMMUNICATION_WIDTH_8BITS)
{
mode |= _SPI_DRV_VTABLE_8BIT;
}
else
{
SYS_ASSERT(false, "\r\nInvalid SPI Configuration.");
return -1;
}
switch (mode)
{
case _SPI_DRV_VTABLE_I_M_E_8:
driverObject->vfMainTask = DRV_SPI_ISRMasterEBM8BitTasks;
break;
default:
SYS_ASSERT(false, "\r\nInvalid SPI Configuration.");
return -1;
}
return 0;
}
int32_t DRV_SPI_SetupHardware(struct DRV_SPI_DRIVER_OBJECT * driverObject, const DRV_SPI_INIT * const init)
{
const register SPI_MODULE_ID spiId = init->spiId;
/* disable the SPI*/
PLIB_SPI_Disable(spiId);
/* Set up Master or Slave Mode*/
PLIB_SPI_MasterEnable ( spiId );
/* Set up if the SPI is allowed to run while the rest of the CPU is in idle mode*/
if (init->allowIdleRun)
{
PLIB_SPI_StopInIdleDisable( spiId );
}
else
{
PLIB_SPI_StopInIdleEnable( spiId );
}
/* Set up close Polarity and output data phase*/
switch(init->clockMode)
{
case DRV_SPI_CLOCK_MODE_IDLE_LOW_EDGE_RISE:
PLIB_SPI_ClockPolaritySelect( spiId, SPI_CLOCK_POLARITY_IDLE_LOW );
PLIB_SPI_OutputDataPhaseSelect( spiId, SPI_OUTPUT_DATA_PHASE_ON_IDLE_TO_ACTIVE_CLOCK );
break;
case DRV_SPI_CLOCK_MODE_IDLE_LOW_EDGE_FALL:
PLIB_SPI_ClockPolaritySelect( spiId, SPI_CLOCK_POLARITY_IDLE_LOW );
PLIB_SPI_OutputDataPhaseSelect( spiId, SPI_OUTPUT_DATA_PHASE_ON_ACTIVE_TO_IDLE_CLOCK );
break;
case DRV_SPI_CLOCK_MODE_IDLE_HIGH_EDGE_FALL: //TODO: Make sure these are right
PLIB_SPI_ClockPolaritySelect( spiId, SPI_CLOCK_POLARITY_IDLE_HIGH );
PLIB_SPI_OutputDataPhaseSelect( spiId, SPI_OUTPUT_DATA_PHASE_ON_IDLE_TO_ACTIVE_CLOCK );
break;
case DRV_SPI_CLOCK_MODE_IDLE_HIGH_EDGE_RISE:
PLIB_SPI_ClockPolaritySelect( spiId, SPI_CLOCK_POLARITY_IDLE_HIGH );
PLIB_SPI_OutputDataPhaseSelect( spiId, SPI_OUTPUT_DATA_PHASE_ON_ACTIVE_TO_IDLE_CLOCK );
break;
default:
SYS_ASSERT(false, "\r\nInvalid SPI Configuration.");
return -1;
}
// Set up the Input Sample Phase
PLIB_SPI_InputSamplePhaseSelect ( spiId, init->inputSamplePhase);
//Enable the SSx Pin on slave side if needed
if(init->spiMode == DRV_SPI_MODE_SLAVE && (init->spiProtocolType == DRV_SPI_PROTOCOL_TYPE_FRAMED || init->spiSlaveSSPin))
{
PLIB_SPI_PinEnable(spiId, SPI_PIN_SLAVE_SELECT);
}
else
{
PLIB_SPI_PinDisable(spiId, SPI_PIN_SLAVE_SELECT);
}
/* Communication Width Selection */
PLIB_SPI_CommunicationWidthSelect ( spiId, init->commWidth );
/* Baudrate selection */
PLIB_SPI_BaudRateSet( spiId , SYS_CLK_PeripheralFrequencyGet(init->spiClk), init->baudRate );
driverObject->currentBaudRate = init->baudRate;
driverObject->baudRate = init->baudRate;
switch (init->spiProtocolType)
{
case DRV_SPI_PROTOCOL_TYPE_STANDARD:
PLIB_SPI_FramedCommunicationDisable( spiId );
break;
case DRV_SPI_PROTOCOL_TYPE_FRAMED:
#if defined (PLIB_SPI_ExistsFrameSyncPulseDirection)
if (PLIB_SPI_ExistsFrameSyncPulseDirection(spiId))
{
PLIB_SPI_FrameSyncPulseDirectionSelect(spiId, init->framePulseDirection);
}
#endif
#if defined (PLIB_SPI_ExistsFrameSyncPulsePolarity)
if (PLIB_SPI_ExistsFrameSyncPulsePolarity(spiId))
{
PLIB_SPI_FrameSyncPulsePolaritySelect(spiId, init->framePulsePolarity);
}
#endif
#if defined (PLIB_SPI_ExistsFrameSyncPulseEdge)
if (PLIB_SPI_ExistsFrameSyncPulseEdge(spiId))
{
PLIB_SPI_FrameSyncPulseEdgeSelect(spiId, init->framePulseEdge);
}
#endif
#if defined (PLIB_SPI_ExistsFrameSyncPulseWidth)
if (PLIB_SPI_ExistsFrameSyncPulseWidth(spiId))
{
PLIB_SPI_FrameSyncPulseWidthSelect(spiId, init->framePulseWidth);
}
#endif
#if defined (PLIB_SPI_ExistsFrameSyncPulseCounter)
if (PLIB_SPI_ExistsFrameSyncPulseCounter(spiId))
{
PLIB_SPI_FrameSyncPulseCounterSelect(spiId, init->frameSyncPulse);
}
#endif
PLIB_SPI_FramedCommunicationEnable( spiId );
break;
case DRV_SPI_PROTOCOL_TYPE_AUDIO:
PLIB_SPI_FramedCommunicationDisable( spiId );
{
SYS_ASSERT(false, "\r\nInvalid SPI Configuration.");
return -1;
}
break;
default:
SYS_ASSERT(false, "\r\nInvalid SPI Configuration.");
return -1;
}
PLIB_SPI_FIFOEnable( spiId );
PLIB_SPI_FIFOInterruptModeSelect(spiId, SPI_FIFO_INTERRUPT_WHEN_TRANSMIT_BUFFER_IS_COMPLETELY_EMPTY);
PLIB_SPI_FIFOInterruptModeSelect(spiId, SPI_FIFO_INTERRUPT_WHEN_RECEIVE_BUFFER_IS_NOT_EMPTY);
PLIB_SPI_BufferClear( spiId );
PLIB_SPI_ReceiverOverflowClear ( spiId );
// Note: We do not enable the SPI here, that will be done by the first client.
return 0;
}
/* clang-format off */
/*******************************************************************************
SPI Driver Functions for Dynamic Standard Buffer Driver Tasks Functions
Company:
Microchip Technology Inc.
File Name:
drv_spi_dynamic_master_dma_tasks.c
Summary:
SPI driver tasks functions
Description:
The SPI device driver provides a simple interface to manage the SPI
modules on Microchip microcontrollers. This file contains implemenation
for the SPI driver.
Remarks:
This file is generated from framework/driver/spi/template/drv_spi_dynamic_ebm_tasks.c.ftl
*******************************************************************************/
//DOM-IGNORE-BEGIN
/*******************************************************************************
Copyright (c) 2013 released Microchip Technology Inc. All rights reserved.
Microchip licenses to you the right to use, modify, copy and distribute
Software only when embedded on a Microchip microcontroller or digital signal
controller that is integrated into your product or third party product
(pursuant to the sublicense terms in the accompanying license agreement).
You should refer to the license agreement accompanying this Software for
additional information regarding your rights and obligations.
SOFTWARE AND DOCUMENTATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
*******************************************************************************/
//DOM-IGNORE-END
#include "driver/spi/src/dynamic/drv_spi_internal.h"
#include <stdbool.h>
void DRV_SPI_MasterDMASendDummy8BitISR(SYS_DMA_TRANSFER_EVENT event, SYS_DMA_CHANNEL_HANDLE handle, uintptr_t contextHandle)
{
if (event != SYS_DMA_TRANSFER_EVENT_COMPLETE)
{
// Ignore for now
return;
}
struct DRV_SPI_DRIVER_OBJECT * pDrvInstance = (struct DRV_SPI_DRIVER_OBJECT *)contextHandle;
DRV_SPI_JOB_OBJECT * currentJob = pDrvInstance->currentJob;
if (currentJob->dummyLeftToTx != 0)
{
uint8_t * ptr = sDrvSpiTxDummy;
uint32_t len = MIN(MIN(MIN(PLIB_DMA_MAX_TRF_SIZE, currentJob->dummyLeftToTx), DRV_SPI_DMA_TXFER_SIZE), DRV_SPI_DMA_DUMMY_BUFFER_SIZE);
void * spiPtr = PLIB_SPI_BufferAddressGet(pDrvInstance->spiId);
SYS_DMA_ChannelTransferAdd(pDrvInstance->txDmaChannelHandle, ptr, len, spiPtr, 1, 1);
currentJob->txDMAProgressStage = DRV_SPI_DMA_DUMMY_INPROGRESS;
currentJob->dummyLeftToTx -= len;
}
else
{
// Job is done
currentJob->txDMAProgressStage = DRV_SPI_DMA_COMPLETE;
}
}
void DRV_SPI_MasterDMASendData8BitISR(SYS_DMA_TRANSFER_EVENT event, SYS_DMA_CHANNEL_HANDLE handle, uintptr_t contextHandle)
{
if (event != SYS_DMA_TRANSFER_EVENT_COMPLETE)
{
// Ignore for now
return;
}
struct DRV_SPI_DRIVER_OBJECT * pDrvInstance = (struct DRV_SPI_DRIVER_OBJECT *)contextHandle;
DRV_SPI_JOB_OBJECT * currentJob = pDrvInstance->currentJob;
if (currentJob->dataLeftToTx != 0)
{
uint8_t * ptr = &(currentJob->txBuffer[currentJob->dataTxed]);
uint32_t len = MIN(MIN(PLIB_DMA_MAX_TRF_SIZE, currentJob->dataLeftToTx), DRV_SPI_DMA_TXFER_SIZE);
void * spiPtr = PLIB_SPI_BufferAddressGet(pDrvInstance->spiId);
SYS_DMA_ChannelTransferAdd(pDrvInstance->txDmaChannelHandle, ptr, len, spiPtr, 1, 1);
currentJob->txDMAProgressStage = DRV_SPI_DMA_DATA_INPROGRESS;
currentJob->dataLeftToTx -= len;
currentJob->dataTxed += len;
}
else
{
// Job is done
currentJob->txDMAProgressStage = DRV_SPI_DMA_COMPLETE;
}
}
void DRV_SPI_ISRDMAMasterSendEventHandler8bit(SYS_DMA_TRANSFER_EVENT event, SYS_DMA_CHANNEL_HANDLE handle, uintptr_t contextHandle)
{
struct DRV_SPI_DRIVER_OBJECT * pDrvInstance = (struct DRV_SPI_DRIVER_OBJECT * )contextHandle;
register DRV_SPI_JOB_OBJECT * currentJob = pDrvInstance->currentJob;
switch(currentJob->txDMAProgressStage)
{
case DRV_SPI_DMA_DATA_INPROGRESS:
DRV_SPI_MasterDMASendData8BitISR(event, handle, contextHandle);
break;
case DRV_SPI_DMA_DUMMY_INPROGRESS:
DRV_SPI_MasterDMASendDummy8BitISR(event, handle, contextHandle);
break;
default:
break;
}
}
void DRV_SPI_MasterDMAReceiveDummy8BitISR(SYS_DMA_TRANSFER_EVENT event, SYS_DMA_CHANNEL_HANDLE handle, uintptr_t contextHandle)
{
if (event != SYS_DMA_TRANSFER_EVENT_COMPLETE)
{
// Ignore for now
return;
}
struct DRV_SPI_DRIVER_OBJECT * pDrvInstance = (struct DRV_SPI_DRIVER_OBJECT *)contextHandle;
DRV_SPI_JOB_OBJECT * currentJob = pDrvInstance->currentJob;
if (currentJob->dummyLeftToRx != 0)
{
uint8_t * ptr = sDrvSpiRxDummy;
uint32_t len = MIN(MIN(MIN(PLIB_DMA_MAX_TRF_SIZE, currentJob->dummyLeftToRx), DRV_SPI_DMA_TXFER_SIZE), DRV_SPI_DMA_DUMMY_BUFFER_SIZE);
void * spiPtr = PLIB_SPI_BufferAddressGet(pDrvInstance->spiId);
SYS_DMA_ChannelTransferAdd(pDrvInstance->rxDmaChannelHandle, spiPtr, 1, ptr, len, 1);
currentJob->rxDMAProgressStage = DRV_SPI_DMA_DUMMY_INPROGRESS;
currentJob->dummyLeftToRx -= len;
}
else
{
// Job is done
currentJob->rxDMAProgressStage = DRV_SPI_DMA_COMPLETE;
SYS_INT_SourceEnable(pDrvInstance->rxInterruptSource);
}
}
void DRV_SPI_MasterDMAReceiveData8BitISR(SYS_DMA_TRANSFER_EVENT event, SYS_DMA_CHANNEL_HANDLE handle, uintptr_t contextHandle)
{
if (event != SYS_DMA_TRANSFER_EVENT_COMPLETE)
{
// Ignore for now
return;
}
struct DRV_SPI_DRIVER_OBJECT * pDrvInstance = (struct DRV_SPI_DRIVER_OBJECT *)contextHandle;
DRV_SPI_JOB_OBJECT * currentJob = pDrvInstance->currentJob;
if (currentJob->dataLeftToRx != 0)
{
uint8_t * ptr = &(currentJob->rxBuffer[currentJob->dataRxed]);
uint32_t len = MIN(MIN(PLIB_DMA_MAX_TRF_SIZE, currentJob->dataLeftToRx), DRV_SPI_DMA_TXFER_SIZE);
void * spiPtr = PLIB_SPI_BufferAddressGet(pDrvInstance->spiId);
SYS_DMA_ChannelTransferAdd(pDrvInstance->rxDmaChannelHandle, spiPtr, 1, ptr, len, 1);
currentJob->rxDMAProgressStage = DRV_SPI_DMA_DATA_INPROGRESS;
currentJob->dataLeftToRx -= len;
currentJob->dataRxed += len;
}
else
{
// Job is done
currentJob->rxDMAProgressStage = DRV_SPI_DMA_COMPLETE;
SYS_INT_SourceEnable(pDrvInstance->rxInterruptSource);
}
}
void DRV_SPI_ISRDMAMasterReceiveEventHandler8bit(SYS_DMA_TRANSFER_EVENT event, SYS_DMA_CHANNEL_HANDLE handle, uintptr_t contextHandle)
{
struct DRV_SPI_DRIVER_OBJECT * pDrvInstance = (struct DRV_SPI_DRIVER_OBJECT * )contextHandle;
register DRV_SPI_JOB_OBJECT * currentJob = pDrvInstance->currentJob;
switch(currentJob->rxDMAProgressStage)
{
case DRV_SPI_DMA_DATA_INPROGRESS:
DRV_SPI_MasterDMAReceiveData8BitISR(event, handle, contextHandle);
break;
case DRV_SPI_DMA_DUMMY_INPROGRESS:
DRV_SPI_MasterDMAReceiveDummy8BitISR(event, handle, contextHandle);
break;
default:
break;
}
}
/* clang-format off */
/*******************************************************************************
SPI Driver Functions for Dynamic Enhanced Buffer Driver Tasks Functions
Company:
Microchip Technology Inc.
File Name:
drv_spi_dynamic_ebm_tasks.c
Summary:
SPI driver tasks functions
Description:
The SPI device driver provides a simple interface to manage the SPI
modules on Microchip microcontrollers. This file contains implemenation
for the SPI driver.
Remarks:
This file is generated from framework/driver/spi/template/drv_spi_dynamic_ebm_tasks.c.ftl
*******************************************************************************/
//DOM-IGNORE-BEGIN
/*******************************************************************************
Copyright (c) 2013 released Microchip Technology Inc. All rights reserved.
Microchip licenses to you the right to use, modify, copy and distribute
Software only when embedded on a Microchip microcontroller or digital signal
controller that is integrated into your product or third party product
(pursuant to the sublicense terms in the accompanying license agreement).
You should refer to the license agreement accompanying this Software for
additional information regarding your rights and obligations.
SOFTWARE AND DOCUMENTATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
*******************************************************************************/
//DOM-IGNORE-END
#include "driver/spi/src/dynamic/drv_spi_internal.h"
#include <stdbool.h>
int32_t DRV_SPI_MasterEBMSend8BitISR( struct DRV_SPI_DRIVER_OBJECT * pDrvInstance )
{
register SPI_MODULE_ID spiId = pDrvInstance->spiId;
register DRV_SPI_JOB_OBJECT * currentJob = pDrvInstance->currentJob;
/* Determine the maximum number of bytes we can send to the FIFO*/
uint8_t symbolsInTransit = MAX(pDrvInstance->symbolsInProgress, PLIB_SPI_FIFOCountGet(spiId, SPI_FIFO_TYPE_TRANSMIT));
uint8_t bufferBytes = PLIB_SPI_TX_8BIT_FIFO_SIZE(spiId) - symbolsInTransit;
/* Figure out how much data we can send*/
size_t dataUnits = MIN(currentJob->dataLeftToTx, bufferBytes);
size_t counter;
if (dataUnits != 0)
{
/* Adjust the maximum buffer size downwards based on how much data we'll be sending*/
bufferBytes -= dataUnits;
currentJob->dataLeftToTx -= dataUnits;
/* Set the location in the buffer of where to start sending from*/
uint8_t *bufferLoc = &(currentJob->txBuffer[currentJob->dataTxed]);
for (counter = 0; counter < dataUnits; counter++)
{
/* Send a unit/symbol of data*/
PLIB_SPI_BufferWrite(spiId, bufferLoc[counter]);
}
/* Update the number of bytes transmitted*/
currentJob->dataTxed += dataUnits;
/* Adjust the symbols in progress */
pDrvInstance->symbolsInProgress += dataUnits;
}
size_t dummyUnits = MIN(currentJob->dummyLeftToTx, bufferBytes);
if (dummyUnits != 0)
{
currentJob->dummyLeftToTx -= dummyUnits;
/* Adjust the symbols in progress */
pDrvInstance->symbolsInProgress += dummyUnits;
for (counter = 0; counter < dummyUnits; counter++)
{
PLIB_SPI_BufferWrite(spiId, 0xff);
}
}
if (currentJob->dataLeftToTx + currentJob->dummyLeftToTx == 0)
{
/* We have no more data to send, turn off the TX interrupt*/
PLIB_SPI_FIFOInterruptModeSelect(spiId, SPI_FIFO_INTERRUPT_WHEN_TRANSMIT_BUFFER_IS_COMPLETELY_EMPTY);
pDrvInstance->txEnabled = false;
/* Turn on the RX Interrupt*/
pDrvInstance->rxEnabled = true;
}
return 0;
}
int32_t DRV_SPI_MasterEBMReceive8BitISR( struct DRV_SPI_DRIVER_OBJECT * pDrvInstance )
{
register SPI_MODULE_ID spiId = pDrvInstance->spiId;
register DRV_SPI_JOB_OBJECT * currentJob = pDrvInstance->currentJob;
/* Figure out how many bytes are waiting to be received."*/
uint8_t bufferBytes = PLIB_SPI_FIFOCountGet(spiId, SPI_FIFO_TYPE_RECEIVE);
/* Calculate the maximum number of data bytes that can be received*/
size_t dataUnits = MIN(currentJob->dataLeftToRx, bufferBytes);
size_t counter;
if (dataUnits != 0)
{
bufferBytes -= dataUnits;
currentJob->dataLeftToRx -= dataUnits;
/* Set the buffer location to receive bytes from the SPI to*/
uint8_t *bufferLoc = &(currentJob->rxBuffer[currentJob->dataRxed]);
for (counter = 0; counter < dataUnits; counter++)
{
/* Receive the data from the SPI */
bufferLoc[counter] = PLIB_SPI_BufferRead(spiId);
}
/* Adjust the amount of data that has been received */
currentJob->dataRxed += dataUnits;
/* Update the symbols in progress so we can send more units later */
pDrvInstance->symbolsInProgress -= dataUnits;
}
/* Figure out the maximum number of dummy data to be received */
size_t dummyUnits = MIN(currentJob->dummyLeftToRx, bufferBytes);
if (dummyUnits != 0)
{
/* Lower the number of dummy bytes to be received */
currentJob->dummyLeftToRx -= dummyUnits;
pDrvInstance->symbolsInProgress -= dummyUnits;
for (counter = 0; counter < dummyUnits; counter++)
{
/* Receive and throw away the byte. Note: We cannot just clear the
buffer because we have to keep track of how many symbols/units we
have received, and the number may have increased since we checked
how full the buffer is.*/
PLIB_SPI_BufferRead(spiId);
}
}
/* Figure out how many bytes are left to be received */
size_t bytesLeft = currentJob->dataLeftToRx + currentJob->dummyLeftToRx;
/* If the bytes left are smaller than the HW mark we have to change the interrupt mode */
if (bytesLeft < PLIB_SPI_RX_8BIT_HW_MARK(spiId))
{
PLIB_SPI_FIFOInterruptModeSelect(spiId, SPI_FIFO_INTERRUPT_WHEN_RECEIVE_BUFFER_IS_NOT_EMPTY);
}
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment