Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: SDFileSystem app epson mbed msp430 pl tests
hal.cpp
00001 // 00002 // Filename: hal.cpp 00003 // 00004 // Flexbook Hardware Abstraction Layer. 00005 // 00006 00007 #include "hal.h" 00008 00009 #include "mcp23s17.h" 00010 00011 #include "mbed.h" 00012 00013 #include <stdio.h> 00014 00015 // Initialisation values for MCP23S17 registers. 00016 uint8_t MCP23S17value[0x16] = 00017 { 00018 0x00, // IODIRA 00019 0x00, // IODIRB 00020 0xff, // IPOLA 00021 0x00, // IPOLB 00022 0x00, // GPINTENA 00023 0x00, // GPINTENB 00024 0x00, // DEFVALA 00025 0x00, // DEFVALB 00026 0x00, // INTCONA 00027 0x00, // INTCONB 00028 0x00, // IOCON1 00029 0x00, // IOCON2 00030 0x00, // GPPUA 00031 0x00, // GPPUB 00032 0x00, // INTFA 00033 0x00, // INTFB 00034 0x00, // INTCAPA 00035 0x00, // INTCAPB 00036 0x00, // GPIOA 00037 0x00, // GPIOB 00038 0x00, // OLATA 00039 0x00, // OLATB 00040 }; 00041 00042 namespace HAL { 00043 00044 MCP23S17 &GetMCP23S17() 00045 { 00046 // The MCP23S17 uses pins 5, 6, 7 (SPI MOSI, MISO, SCK) and 19 (CS). 00047 static SPI spi(p5, p6, p7); 00048 static DigitalOut cs(p19); 00049 static MCP23S17 mcp23s17(0x00, spi, cs); 00050 00051 return mcp23s17; 00052 } 00053 00054 // Initialise the HAL. 00055 void Initialise() 00056 { 00057 // Setup MCP23S17 I/O expander. 00058 for(unsigned int reg = 0; reg < sizeof(MCP23S17value); reg++) 00059 GetMCP23S17().Write((REG_MCP23S17) reg, MCP23S17value[reg]); 00060 00061 GetMCP23S17().Write(GPIOA, 0); 00062 GetMCP23S17().Write(GPIOB, 0); 00063 } 00064 00065 void EnablePowerToPages(Flexbook::PageType pages) 00066 { 00067 // Power on the board(s). 00068 GetMCP23S17().Write(GPIOB, pages); 00069 00070 // Wait for the power to stabilize. 00071 wait(0.1); 00072 00073 // Enable the I2C connection. 00074 GetMCP23S17().Write(GPIOA, pages); 00075 } 00076 00077 Flexbook::PageType GetPageType() 00078 { 00079 using namespace Flexbook; 00080 00081 static PageType lastype = Flexbook::PageType_PageNull; 00082 00083 //printf("%x\n", GetMCP23S17().Read(GPIOA)); 00084 00085 switch(GetMCP23S17().Read(GPIOA)) 00086 { 00087 case 0: 00088 break; 00089 00090 //case PageType_PageSensor: 00091 case 0xFE: 00092 lastype = PageType_PageSensor; 00093 break; 00094 00095 case 0xF7: 00096 lastype = PageType_PageDice; 00097 break; 00098 00099 case PageType_PageOLED: 00100 lastype = PageType_PageOLED; 00101 break; 00102 00103 //case PageType_PageTouch: 00104 case 0xFD: 00105 lastype = PageType_PageTouch; 00106 break; 00107 00108 case PageType_PageFlexEnable: 00109 lastype = PageType_PageFlexEnable; 00110 break; 00111 00112 case PageType_PageNFC: 00113 lastype = PageType_PageNFC; 00114 break; 00115 00116 default: 00117 break; 00118 } 00119 00120 return lastype; 00121 } 00122 00123 } // End HAL namespace. 00124 00125 00126 00127
Generated on Tue Jul 12 2022 21:14:34 by
