______________________________________________________________________ > < > SPI bus driver v1.02 < > for < > PICos18 release 2.07 < > < > PICos18 - Real-time kernel for PIC18 family < > < > < > www.picos18.com www.pragmatec.net < >______________________________________________________________________< This file contains all necessary informations to use properly the PIC18 SPI driver for PICos18 v2.07. This driver let you design a multi-task application using the same SPI port without being concerned by the SPI shared access. Then every task of your application can send messages without being necessarily synchronised to the others. ______________________________________________________________________ > < > I - Zip file content < >______________________________________________________________________< The drv_can zip file contains : - drv_spi.c - drv_spi.h : header file - drv_spi.txt : this file To use the PICos18 SPI driver, first unzip the file. Place the C and H file in your project directory. This driver is supposed to be use with PICos18 and has been tested with MPLAB 7.22 and C18 v3.0. ______________________________________________________________________ > < > II - TASCDESC settings (1) < >______________________________________________________________________< Update also the tascdesc.c file to take into account the new CAN driver in your application : /********************************************************************** * ----------------------- TASK & STACK DEFINITION -------------------- **********************************************************************/ DeclareTask(Example); DeclareTask(SPI_DRV); ... volatile unsigned char stack0[DEFAULT_STACK_SIZE]; volatile unsigned char stack_spi[DEFAULT_STACK_SIZE]; ... ______________________________________________________________________ > < > III - TASCDESC settings (2) < >______________________________________________________________________< Finally add the task and alarm descriptor to the tascdesc.c file : ... /********************************************************************** * ----------------------------- CAN task ----------------------------- **********************************************************************/ rom_desc_tsk rom_desc_SPI_DRV = { DRV_SPI_PRIO, /* prioinit from 0 to 15 */ stack_spi, /* stack address (16 bits) */ SPI_DRV, /* start address (16 bits) */ READY, /* state at init phase */ SPI_DRV_ID, /* id_tsk from 0 to 15 */ sizeof(stack_spi) /* ctx address (16 bits) */ }; ... To use the SPI we propose an EXAMPLE task that send some messages periodicaly. Because the task is a periodic one, we create an alarm : AlarmObject Alarm_list[] = { /******************************************************************* * ----------------------- Example task ---------------------------- *******************************************************************/ { OFF, /* State */ 0, /* AlarmValue */ 0, /* Cycle */ &Counter_kernel, /* ptrCounter */ Example, /* TaskID2Activate */ ALARM_EVENT, /* EventToPost */ 0 /* CallBack */ } }; ______________________________________________________________________ > < > IV - TASK Settings < >______________________________________________________________________< Open define.h and add this 3 definitions with correct value : #define SPI_NEW_MSG 0x10 #define SPI_QUEUE_EMPTY 0x40 #define SPI_DRV_ID 6 #define DRV_SPI_PRIO 8 #define EXAMPLE_ID 4 #define EXAMPLE_PRIO 5 The SPI task is considered to be a driver, that means you have to give one of the highest priority to the SPI driver to let it run each time the SPI buffer is updated. Adapt the values to your application is necessary (event values...). ______________________________________________________________________ > < > V - SPI driver usage (1) < >______________________________________________________________________< In the Example task, include the driver header: #include "drv_spi.h" And declare a SPI message (type SPI_message_t): SPI_ message_t SPI_msg; See hereafter a basic example #include "define.h" #include "drv_spi.h" /********************************************************************** * Variables shared with the rest of application. **********************************************************************/ SPI_message_t SPI_interface; #define MCP4X100_CS_PORT PORTB // CS is the pin PORTB.2 #define MCP4X100_CS_BIT 2 /********************************************************************** * ------------------------- Internal test TASK ----------------------- * **********************************************************************/ TASK(Example) { // Buffer for SPI send unsigned char cmd[2]; cmd[0] = 0x03; // instruction cmd[1] = 128; // Value // Simple SPI chip // MCP41100 digital resistor example // Just send the resistance value // buffer pointer and number of byte to send SPI_interface.SDO_num_bytes = 2; // 16 bits instruction SPI_interface.SDO_ram_data = &cmd[0]; // No reply waiting SPI_interface.SDI_num_bytes = 0; // define the CS pin // See the drv_spi.h and set the correct value MCP4X100_Interface.CS_address = (ram char*)&MCP4X100_CS_PORT; MCP4X100_Interface.CS_bit = MCP4X100_CS_BIT; GetTaskID (&SPI_interface.CallerID); // Put the message in the SPI driver queue SPI_enqMsg(&SPI_interface; SetEvent(SPI_DRV_ID, SPI_NEW_MSG); // Wait for the end of operation WaitEvent(SPI_QUEUE_EMPTY); ClearEvent(SPI_QUEUE_EMPTY); while(1); } ______________________________________________________________________ > < > IX - Conclusion < >______________________________________________________________________< The code has been tested on the PIC18F4620, without any trouble. This SPI driver is provided from the PICos18 web site with other drivers dedicated to oher peripherals. Some zip files are also provided to be used with a driver. Do not hesitate to check periodicaly the www.picos18.com web site in order to download the last versions of PIC18 drivers for PICos18. /* End of File : drv_spi.txt */